]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
c6a8e374d8b78b7f9094f27613a99d06f31c3c54
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46
47 \f
48 /* The lexer. */
49
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
52
53 static cp_token eof_token =
54 {
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
56 };
57
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
115 };
116
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
127 };
128
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
152
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
174
175 RT_SELECT, /* selection-statement */
176 RT_INTERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
183 };
184
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
187
188 class type_id_in_expr_sentinel
189 {
190 cp_parser *parser;
191 bool saved;
192 public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
199 };
200
201 /* Prototypes. */
202
203 static cp_lexer *cp_lexer_new_main
204 (void);
205 static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207 static void cp_lexer_destroy
208 (cp_lexer *);
209 static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211 static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215 static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217 static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225 static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227 static void cp_lexer_purge_token
228 (cp_lexer *);
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231 static void cp_lexer_save_tokens
232 (cp_lexer *);
233 static void cp_lexer_commit_tokens
234 (cp_lexer *);
235 static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237 static void cp_lexer_print_token
238 (FILE *, cp_token *);
239 static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241 static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245
246 static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
248
249 static void cp_parser_initial_pragma
250 (cp_token *);
251
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
262
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
266
267 /* Variables. */
268
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream;
271
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand;
275
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
281
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
286 {
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
290
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
292
293 if (buffer == NULL)
294 return;
295
296 if (num == 0)
297 num = buffer->length ();
298
299 if (start_token == NULL)
300 start_token = buffer->address ();
301
302 if (start_token > buffer->address ())
303 {
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
306 }
307
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
311 {
312 if (token == start_token)
313 do_print = true;
314
315 if (!do_print)
316 continue;
317
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
321
322 cp_lexer_print_token (file, token);
323
324 if (token == curr_token)
325 fprintf (file, "]]");
326
327 switch (token->type)
328 {
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
335
336 default:
337 fputc (' ', file);
338 }
339 }
340
341 if (i == num && i < buffer->length ())
342 {
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
345 }
346
347 fprintf (file, "\n");
348 }
349
350
351 /* Dump all tokens in BUFFER to stderr. */
352
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
355 {
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
357 }
358
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
361 {
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
363 }
364
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
367 {
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
372 }
373
374
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
377
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
380 {
381 if (t)
382 {
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
385 }
386 }
387
388
389 /* Dump parser context C to FILE. */
390
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
393 {
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
398 }
399
400
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
402
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
405 {
406 unsigned i;
407 cp_parser_context *c;
408
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
411 {
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
414 }
415 }
416
417
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
419
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
422 {
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
425 }
426
427
428 /* Print an unparsed function entry UF to FILE. */
429
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
432 {
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
436
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
441 {
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
447 }
448
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
452 {
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
455 }
456 fprintf (file, "\n");
457
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
461 {
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
464 }
465 fprintf (file, "\n");
466 }
467
468
469 /* Print the stack of unparsed member functions S to FILE. */
470
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
474 {
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
477
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
480 {
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
483 }
484 }
485
486
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
489
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
492 {
493 cp_token *next_token, *first_token, *start_token;
494
495 if (file == NULL)
496 file = stderr;
497
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
505 }
506
507
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
510
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
513 {
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
517
518 if (file == NULL)
519 file = stderr;
520
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
594 }
595
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
598 {
599 cp_debug_parser (stderr, &ref);
600 }
601
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
604 {
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
609 }
610
611 /* Allocate memory for a new lexer object and return it. */
612
613 static cp_lexer *
614 cp_lexer_alloc (void)
615 {
616 cp_lexer *lexer;
617
618 c_common_no_more_pch ();
619
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
622
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
625
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
627
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
630
631 return lexer;
632 }
633
634
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
637
638 static cp_lexer *
639 cp_lexer_new_main (void)
640 {
641 cp_lexer *lexer;
642 cp_token token;
643
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
648
649 lexer = cp_lexer_alloc ();
650
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
653
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
656 {
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
659 }
660
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
667
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
671
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
674 }
675
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
678
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
681 {
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
685
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
690
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
692
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
695
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
698 }
699
700 /* Frees all resources associated with LEXER. */
701
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
704 {
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
708 }
709
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
714
715 #define LEXER_DEBUGGING_ENABLED_P false
716
717 /* Returns nonzero if debugging information should be output. */
718
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
721 {
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
724
725 return lexer->debugging_p;
726 }
727
728
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
731 {
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
733
734 return lexer->next_token - previous_p;
735 }
736
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
739 {
740 return pos;
741 }
742
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
745 {
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
747 }
748
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
751 {
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
756 }
757
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
760 {
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
762
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
765 {
766 gcc_assert (tp != vec_safe_address (lexer->buffer));
767 tp--;
768 }
769
770 return cp_lexer_token_at (lexer, tp);
771 }
772
773 /* nonzero if we are presently saving tokens. */
774
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
777 {
778 return lexer->saved_tokens.length () != 0;
779 }
780
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
785
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
788 {
789 static int is_extern_c = 0;
790
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
798
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
805
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
808 {
809 if (IDENTIFIER_KEYWORD_P (token->u.value))
810 {
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
815 }
816 else
817 {
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
821 {
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
827
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
831 }
832
833 token->keyword = RID_MAX;
834 }
835 }
836 else if (token->type == CPP_AT_NAME)
837 {
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
841 {
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
849 */
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
859 }
860 }
861 }
862
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
866 {
867 if (token->type != CPP_EOF)
868 {
869 input_location = token->location;
870 }
871 }
872
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
876 {
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
879 }
880
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
883
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
886 {
887 if (cp_lexer_debugging_p (lexer))
888 {
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
892 }
893 return lexer->next_token;
894 }
895
896 /* Return true if the next token has the indicated TYPE. */
897
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
900 {
901 return cp_lexer_peek_token (lexer)->type == type;
902 }
903
904 /* Return true if the next token does not have the indicated TYPE. */
905
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
908 {
909 return !cp_lexer_next_token_is (lexer, type);
910 }
911
912 /* Return true if the next token is the indicated KEYWORD. */
913
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
916 {
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
918 }
919
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
922 {
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
924 }
925
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
928 {
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
930 }
931
932 /* Return true if the next token is not the indicated KEYWORD. */
933
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
936 {
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
938 }
939
940 /* Return true if KEYWORD can start a decl-specifier. */
941
942 bool
943 cp_keyword_starts_decl_specifier_p (enum rid keyword)
944 {
945 switch (keyword)
946 {
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
984
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
991 }
992 }
993
994 /* Return true if the next token is a keyword for a decl-specifier. */
995
996 static bool
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
998 {
999 cp_token *token;
1000
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1003 }
1004
1005 /* Returns TRUE iff the token T begins a decltype type. */
1006
1007 static bool
1008 token_is_decltype (cp_token *t)
1009 {
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1012 }
1013
1014 /* Returns TRUE iff the next token begins a decltype type. */
1015
1016 static bool
1017 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1018 {
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1021 }
1022
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1025
1026 static tree
1027 saved_checks_value (struct tree_check *check_value)
1028 {
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1034 {
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1040 }
1041 /* Return the stored value. */
1042 return check_value->value;
1043 }
1044
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1050
1051 static cp_token *
1052 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1053 {
1054 cp_token *token;
1055
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1058
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1062
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1067 {
1068 ++token;
1069 if (token == lexer->last_token)
1070 {
1071 token = &eof_token;
1072 break;
1073 }
1074
1075 if (!token->purged_p)
1076 --n;
1077 }
1078
1079 if (cp_lexer_debugging_p (lexer))
1080 {
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1083 }
1084
1085 return token;
1086 }
1087
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1090
1091 static cp_token *
1092 cp_lexer_consume_token (cp_lexer* lexer)
1093 {
1094 cp_token *token = lexer->next_token;
1095
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1098
1099 do
1100 {
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1103 {
1104 lexer->next_token = &eof_token;
1105 break;
1106 }
1107
1108 }
1109 while (lexer->next_token->purged_p);
1110
1111 cp_lexer_set_source_position_from_token (token);
1112
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1115 {
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1119 }
1120
1121 return token;
1122 }
1123
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1127
1128 static void
1129 cp_lexer_purge_token (cp_lexer *lexer)
1130 {
1131 cp_token *tok = lexer->next_token;
1132
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1138
1139 do
1140 {
1141 tok++;
1142 if (tok == lexer->last_token)
1143 {
1144 tok = &eof_token;
1145 break;
1146 }
1147 }
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1150 }
1151
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1155
1156 static void
1157 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1158 {
1159 cp_token *peek = lexer->next_token;
1160
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1163
1164 gcc_assert (tok < peek);
1165
1166 for ( tok += 1; tok != peek; tok += 1)
1167 {
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1172 }
1173 }
1174
1175 /* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1177
1178 static void
1179 cp_lexer_save_tokens (cp_lexer* lexer)
1180 {
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1184
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1186 }
1187
1188 /* Commit to the portion of the token stream most recently saved. */
1189
1190 static void
1191 cp_lexer_commit_tokens (cp_lexer* lexer)
1192 {
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1196
1197 lexer->saved_tokens.pop ();
1198 }
1199
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1202
1203 static void
1204 cp_lexer_rollback_tokens (cp_lexer* lexer)
1205 {
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1209
1210 lexer->next_token = lexer->saved_tokens.pop ();
1211 }
1212
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1217
1218 struct saved_token_sentinel
1219 {
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1224 {
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1227 }
1228 void rollback ()
1229 {
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1232 }
1233 ~saved_token_sentinel()
1234 {
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1238 }
1239 };
1240
1241 /* Print a representation of the TOKEN on the STREAM. */
1242
1243 static void
1244 cp_lexer_print_token (FILE * stream, cp_token *token)
1245 {
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1252 TTYPE_TABLE
1253 #undef OP
1254 #undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1259 };
1260
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1263 {
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1273
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1281
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value);
1284 break;
1285
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1294 }
1295 }
1296
1297 DEBUG_FUNCTION void
1298 debug (cp_token &ref)
1299 {
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1302 }
1303
1304 DEBUG_FUNCTION void
1305 debug (cp_token *ptr)
1306 {
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1311 }
1312
1313
1314 /* Start emitting debugging information. */
1315
1316 static void
1317 cp_lexer_start_debugging (cp_lexer* lexer)
1318 {
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1322
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1325 }
1326
1327 /* Stop emitting debugging information. */
1328
1329 static void
1330 cp_lexer_stop_debugging (cp_lexer* lexer)
1331 {
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1335
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1338 }
1339
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1341
1342 static cp_token_cache *
1343 cp_token_cache_new (cp_token *first, cp_token *last)
1344 {
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1349 }
1350
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1353
1354 static inline void
1355 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1356 {
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1358 {
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1362 }
1363 }
1364
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1367
1368 static inline void
1369 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1370 {
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1372 {
1373 if (fndecl == error_mark_node)
1374 {
1375 parser->omp_declare_simd = NULL;
1376 return;
1377 }
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1379 {
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1382 }
1383 }
1384 }
1385
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1388
1389 static inline void
1390 cp_ensure_no_oacc_routine (cp_parser *parser)
1391 {
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1393 {
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1398 }
1399 }
1400 \f
1401 /* Decl-specifiers. */
1402
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1404
1405 static void
1406 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1407 {
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1409 }
1410
1411 /* Declarators. */
1412
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1417
1418 static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420 static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422 static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424 static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426 static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1428
1429 /* An erroneous declarator. */
1430 static cp_declarator *cp_error_declarator;
1431
1432 /* The obstack on which declarators and related data structures are
1433 allocated. */
1434 static struct obstack declarator_obstack;
1435
1436 /* Alloc BYTES from the declarator memory pool. */
1437
1438 static inline void *
1439 alloc_declarator (size_t bytes)
1440 {
1441 return obstack_alloc (&declarator_obstack, bytes);
1442 }
1443
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1446
1447 static cp_declarator *
1448 make_declarator (cp_declarator_kind kind)
1449 {
1450 cp_declarator *declarator;
1451
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1459
1460 return declarator;
1461 }
1462
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1468
1469 static cp_declarator *
1470 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1472 {
1473 cp_declarator *declarator;
1474
1475 /* It is valid to write:
1476
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1480
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1487
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1491
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1496
1497 return declarator;
1498 }
1499
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1504
1505 cp_declarator *
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1508 {
1509 cp_declarator *declarator;
1510
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1516 {
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1520 }
1521 else
1522 declarator->parameter_pack_p = false;
1523
1524 declarator->std_attributes = attributes;
1525
1526 return declarator;
1527 }
1528
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1532
1533 cp_declarator *
1534 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1536 {
1537 cp_declarator *declarator;
1538
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1544 {
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1548 }
1549 else
1550 declarator->parameter_pack_p = false;
1551
1552 declarator->std_attributes = attributes;
1553
1554 return declarator;
1555 }
1556
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1560
1561 cp_declarator *
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1565 {
1566 cp_declarator *declarator;
1567
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1572
1573 if (pointee)
1574 {
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1577 }
1578 else
1579 declarator->parameter_pack_p = false;
1580
1581 declarator->std_attributes = attributes;
1582
1583 return declarator;
1584 }
1585
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1590
1591 cp_declarator *
1592 make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1601 {
1602 cp_declarator *declarator;
1603
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1615 {
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1619 }
1620 else
1621 declarator->parameter_pack_p = false;
1622
1623 return declarator;
1624 }
1625
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1628
1629 cp_declarator *
1630 make_array_declarator (cp_declarator *element, tree bounds)
1631 {
1632 cp_declarator *declarator;
1633
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1638 {
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1642 }
1643 else
1644 declarator->parameter_pack_p = false;
1645
1646 return declarator;
1647 }
1648
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651 static bool
1652 declarator_can_be_parameter_pack (cp_declarator *declarator)
1653 {
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1657
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1664 {
1665 switch ((int)declarator->kind)
1666 {
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1672
1673 case cdk_error:
1674 return true;
1675
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1679 }
1680 }
1681
1682 return !found;
1683 }
1684
1685 cp_parameter_declarator *no_parameters;
1686
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1689
1690 cp_parameter_declarator *
1691 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1695 {
1696 cp_parameter_declarator *parameter;
1697
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1708
1709 return parameter;
1710 }
1711
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1713
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1716 {
1717 while (declarator)
1718 {
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1727 }
1728 return false;
1729 }
1730
1731 /* The parser. */
1732
1733 /* Overview
1734 --------
1735
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1743
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1747
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1756
1757 Methodology
1758 -----------
1759
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1776
1777 Future Improvements
1778 -------------------
1779
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1785
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1788
1789 enum
1790 {
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1807 };
1808
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1812
1813 /* The different kinds of declarators we want to parse. */
1814
1815 enum cp_parser_declarator_kind
1816 {
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1823 };
1824
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1828
1829 enum cp_parser_prec
1830 {
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1844 };
1845
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1848
1849 struct cp_parser_binary_operations_map_node
1850 {
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1857 };
1858
1859 struct cp_parser_expression_stack_entry
1860 {
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1873 };
1874
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1880
1881 /* Prototypes. */
1882
1883 /* Constructors and destructors. */
1884
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1887
1888 /* Class variables. */
1889
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1891
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1895
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1899
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1903
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1906
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1909
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1914
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1917
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1919
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1921
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1923
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1925
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1927 };
1928
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1933
1934 /* Constructors and destructors. */
1935
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1938
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1941 {
1942 cp_parser_context *context;
1943
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1946 {
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1951 }
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1954
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1960 {
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1966 }
1967
1968 return context;
1969 }
1970
1971 /* Managing the unparsed function queues. */
1972
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1981
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1984 {
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1987 }
1988
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1991 {
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1994 }
1995
1996 /* Prototypes. */
1997
1998 /* Constructors and destructors. */
1999
2000 static cp_parser *cp_parser_new
2001 (void);
2002
2003 /* Routines to parse various constructs.
2004
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2014
2015 /* Lexical conventions [gram.lex] */
2016
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2027
2028 /* Basic concepts [gram.basic] */
2029
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2032
2033 /* Expressions [gram.expr] */
2034
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool, bool = false);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2101
2102 /* Statements [gram.stmt.stmt] */
2103
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2138
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2143
2144 /* Declarations [gram.dcl.dcl] */
2145
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2205
2206 /* Declarators [gram.dcl.decl] */
2207
2208 static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211 static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227 static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229 static tree cp_parser_type_id
2230 (cp_parser *);
2231 static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233 static tree cp_parser_trailing_type_id (cp_parser *);
2234 static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238 static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242 static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246 static void cp_parser_function_body
2247 (cp_parser *, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2256
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2259
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2262
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2265
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2268
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271 static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2273
2274 /* Classes [gram.class] */
2275
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2294
2295 /* Derived classes [gram.class.derived] */
2296
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2301
2302 /* Special member functions [gram.special] */
2303
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2318
2319 /* Overloading [gram.over] */
2320
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *);
2325
2326 /* Templates [gram.temp] */
2327
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2348
2349 /* Exception handling [gram.exception] */
2350
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static bool cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2367
2368 /* GNU Extensions */
2369
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2406
2407 /* Concept Extensions */
2408
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2431
2432 /* Transactional Memory Extensions */
2433
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2442
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2449 };
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2452
2453 /* Objective-C++ Productions */
2454
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2491
2492 /* Utility Routines */
2493
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2651
2652 /* Concept-related syntactic transformations */
2653
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2656
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2659 //
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2662 {
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2665 }
2666
2667 cp_unevaluated::~cp_unevaluated ()
2668 {
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2671 }
2672
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2675
2676 /* Returns nonzero if we are parsing tentatively. */
2677
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2680 {
2681 return parser->context->next != NULL;
2682 }
2683
2684 /* Returns nonzero if TOKEN is a string literal. */
2685
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2688 {
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2694 }
2695
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2698
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2701 {
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2708 }
2709
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2714 {
2715 return token->keyword == keyword;
2716 }
2717
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2720
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2723 {
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 }
2729
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2740
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2744 {
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2754
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2759
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2766
2767 return true;
2768 }
2769
2770 /* If not parsing tentatively, issue a diagnostic of the form
2771 FILE:LINE: MESSAGE before TOKEN
2772 where TOKEN is the next token in the input stream. MESSAGE
2773 (specified by the caller) is usually of the form "expected
2774 OTHER-TOKEN". */
2775
2776 static void
2777 cp_parser_error (cp_parser* parser, const char* gmsgid)
2778 {
2779 if (!cp_parser_simulate_error (parser))
2780 {
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2782 /* This diagnostic makes more sense if it is tagged to the line
2783 of the token we just peeked at. */
2784 cp_lexer_set_source_position_from_token (token);
2785
2786 if (token->type == CPP_PRAGMA)
2787 {
2788 error_at (token->location,
2789 "%<#pragma%> is not allowed here");
2790 cp_parser_skip_to_pragma_eol (parser, token);
2791 return;
2792 }
2793
2794 /* If this is actually a conflict marker, report it as such. */
2795 if (token->type == CPP_LSHIFT
2796 || token->type == CPP_RSHIFT
2797 || token->type == CPP_EQ_EQ)
2798 {
2799 location_t loc;
2800 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2801 {
2802 error_at (loc, "version control conflict marker in file");
2803 return;
2804 }
2805 }
2806
2807 c_parse_error (gmsgid,
2808 /* Because c_parser_error does not understand
2809 CPP_KEYWORD, keywords are treated like
2810 identifiers. */
2811 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2812 token->u.value, token->flags);
2813 }
2814 }
2815
2816 /* Issue an error about name-lookup failing. NAME is the
2817 IDENTIFIER_NODE DECL is the result of
2818 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2819 the thing that we hoped to find. */
2820
2821 static void
2822 cp_parser_name_lookup_error (cp_parser* parser,
2823 tree name,
2824 tree decl,
2825 name_lookup_error desired,
2826 location_t location)
2827 {
2828 /* If name lookup completely failed, tell the user that NAME was not
2829 declared. */
2830 if (decl == error_mark_node)
2831 {
2832 if (parser->scope && parser->scope != global_namespace)
2833 error_at (location, "%<%E::%E%> has not been declared",
2834 parser->scope, name);
2835 else if (parser->scope == global_namespace)
2836 error_at (location, "%<::%E%> has not been declared", name);
2837 else if (parser->object_scope
2838 && !CLASS_TYPE_P (parser->object_scope))
2839 error_at (location, "request for member %qE in non-class type %qT",
2840 name, parser->object_scope);
2841 else if (parser->object_scope)
2842 error_at (location, "%<%T::%E%> has not been declared",
2843 parser->object_scope, name);
2844 else
2845 error_at (location, "%qE has not been declared", name);
2846 }
2847 else if (parser->scope && parser->scope != global_namespace)
2848 {
2849 switch (desired)
2850 {
2851 case NLE_TYPE:
2852 error_at (location, "%<%E::%E%> is not a type",
2853 parser->scope, name);
2854 break;
2855 case NLE_CXX98:
2856 error_at (location, "%<%E::%E%> is not a class or namespace",
2857 parser->scope, name);
2858 break;
2859 case NLE_NOT_CXX98:
2860 error_at (location,
2861 "%<%E::%E%> is not a class, namespace, or enumeration",
2862 parser->scope, name);
2863 break;
2864 default:
2865 gcc_unreachable ();
2866
2867 }
2868 }
2869 else if (parser->scope == global_namespace)
2870 {
2871 switch (desired)
2872 {
2873 case NLE_TYPE:
2874 error_at (location, "%<::%E%> is not a type", name);
2875 break;
2876 case NLE_CXX98:
2877 error_at (location, "%<::%E%> is not a class or namespace", name);
2878 break;
2879 case NLE_NOT_CXX98:
2880 error_at (location,
2881 "%<::%E%> is not a class, namespace, or enumeration",
2882 name);
2883 break;
2884 default:
2885 gcc_unreachable ();
2886 }
2887 }
2888 else
2889 {
2890 switch (desired)
2891 {
2892 case NLE_TYPE:
2893 error_at (location, "%qE is not a type", name);
2894 break;
2895 case NLE_CXX98:
2896 error_at (location, "%qE is not a class or namespace", name);
2897 break;
2898 case NLE_NOT_CXX98:
2899 error_at (location,
2900 "%qE is not a class, namespace, or enumeration", name);
2901 break;
2902 default:
2903 gcc_unreachable ();
2904 }
2905 }
2906 }
2907
2908 /* If we are parsing tentatively, remember that an error has occurred
2909 during this tentative parse. Returns true if the error was
2910 simulated; false if a message should be issued by the caller. */
2911
2912 static bool
2913 cp_parser_simulate_error (cp_parser* parser)
2914 {
2915 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2916 {
2917 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2918 return true;
2919 }
2920 return false;
2921 }
2922
2923 /* This function is called when a type is defined. If type
2924 definitions are forbidden at this point, an error message is
2925 issued. */
2926
2927 static bool
2928 cp_parser_check_type_definition (cp_parser* parser)
2929 {
2930 /* If types are forbidden here, issue a message. */
2931 if (parser->type_definition_forbidden_message)
2932 {
2933 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2934 in the message need to be interpreted. */
2935 error (parser->type_definition_forbidden_message);
2936 return false;
2937 }
2938 return true;
2939 }
2940
2941 /* This function is called when the DECLARATOR is processed. The TYPE
2942 was a type defined in the decl-specifiers. If it is invalid to
2943 define a type in the decl-specifiers for DECLARATOR, an error is
2944 issued. TYPE_LOCATION is the location of TYPE and is used
2945 for error reporting. */
2946
2947 static void
2948 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2949 tree type, location_t type_location)
2950 {
2951 /* [dcl.fct] forbids type definitions in return types.
2952 Unfortunately, it's not easy to know whether or not we are
2953 processing a return type until after the fact. */
2954 while (declarator
2955 && (declarator->kind == cdk_pointer
2956 || declarator->kind == cdk_reference
2957 || declarator->kind == cdk_ptrmem))
2958 declarator = declarator->declarator;
2959 if (declarator
2960 && declarator->kind == cdk_function)
2961 {
2962 error_at (type_location,
2963 "new types may not be defined in a return type");
2964 inform (type_location,
2965 "(perhaps a semicolon is missing after the definition of %qT)",
2966 type);
2967 }
2968 }
2969
2970 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2971 "<" in any valid C++ program. If the next token is indeed "<",
2972 issue a message warning the user about what appears to be an
2973 invalid attempt to form a template-id. LOCATION is the location
2974 of the type-specifier (TYPE) */
2975
2976 static void
2977 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2978 tree type,
2979 enum tag_types tag_type,
2980 location_t location)
2981 {
2982 cp_token_position start = 0;
2983
2984 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2985 {
2986 if (TREE_CODE (type) == TYPE_DECL)
2987 type = TREE_TYPE (type);
2988 if (TYPE_P (type) && !template_placeholder_p (type))
2989 error_at (location, "%qT is not a template", type);
2990 else if (identifier_p (type))
2991 {
2992 if (tag_type != none_type)
2993 error_at (location, "%qE is not a class template", type);
2994 else
2995 error_at (location, "%qE is not a template", type);
2996 }
2997 else
2998 error_at (location, "invalid template-id");
2999 /* Remember the location of the invalid "<". */
3000 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3001 start = cp_lexer_token_position (parser->lexer, true);
3002 /* Consume the "<". */
3003 cp_lexer_consume_token (parser->lexer);
3004 /* Parse the template arguments. */
3005 cp_parser_enclosed_template_argument_list (parser);
3006 /* Permanently remove the invalid template arguments so that
3007 this error message is not issued again. */
3008 if (start)
3009 cp_lexer_purge_tokens_after (parser->lexer, start);
3010 }
3011 }
3012
3013 /* If parsing an integral constant-expression, issue an error message
3014 about the fact that THING appeared and return true. Otherwise,
3015 return false. In either case, set
3016 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3017
3018 static bool
3019 cp_parser_non_integral_constant_expression (cp_parser *parser,
3020 non_integral_constant thing)
3021 {
3022 parser->non_integral_constant_expression_p = true;
3023 if (parser->integral_constant_expression_p)
3024 {
3025 if (!parser->allow_non_integral_constant_expression_p)
3026 {
3027 const char *msg = NULL;
3028 switch (thing)
3029 {
3030 case NIC_FLOAT:
3031 pedwarn (input_location, OPT_Wpedantic,
3032 "ISO C++ forbids using a floating-point literal "
3033 "in a constant-expression");
3034 return true;
3035 case NIC_CAST:
3036 error ("a cast to a type other than an integral or "
3037 "enumeration type cannot appear in a "
3038 "constant-expression");
3039 return true;
3040 case NIC_TYPEID:
3041 error ("%<typeid%> operator "
3042 "cannot appear in a constant-expression");
3043 return true;
3044 case NIC_NCC:
3045 error ("non-constant compound literals "
3046 "cannot appear in a constant-expression");
3047 return true;
3048 case NIC_FUNC_CALL:
3049 error ("a function call "
3050 "cannot appear in a constant-expression");
3051 return true;
3052 case NIC_INC:
3053 error ("an increment "
3054 "cannot appear in a constant-expression");
3055 return true;
3056 case NIC_DEC:
3057 error ("an decrement "
3058 "cannot appear in a constant-expression");
3059 return true;
3060 case NIC_ARRAY_REF:
3061 error ("an array reference "
3062 "cannot appear in a constant-expression");
3063 return true;
3064 case NIC_ADDR_LABEL:
3065 error ("the address of a label "
3066 "cannot appear in a constant-expression");
3067 return true;
3068 case NIC_OVERLOADED:
3069 error ("calls to overloaded operators "
3070 "cannot appear in a constant-expression");
3071 return true;
3072 case NIC_ASSIGNMENT:
3073 error ("an assignment cannot appear in a constant-expression");
3074 return true;
3075 case NIC_COMMA:
3076 error ("a comma operator "
3077 "cannot appear in a constant-expression");
3078 return true;
3079 case NIC_CONSTRUCTOR:
3080 error ("a call to a constructor "
3081 "cannot appear in a constant-expression");
3082 return true;
3083 case NIC_TRANSACTION:
3084 error ("a transaction expression "
3085 "cannot appear in a constant-expression");
3086 return true;
3087 case NIC_THIS:
3088 msg = "this";
3089 break;
3090 case NIC_FUNC_NAME:
3091 msg = "__FUNCTION__";
3092 break;
3093 case NIC_PRETTY_FUNC:
3094 msg = "__PRETTY_FUNCTION__";
3095 break;
3096 case NIC_C99_FUNC:
3097 msg = "__func__";
3098 break;
3099 case NIC_VA_ARG:
3100 msg = "va_arg";
3101 break;
3102 case NIC_ARROW:
3103 msg = "->";
3104 break;
3105 case NIC_POINT:
3106 msg = ".";
3107 break;
3108 case NIC_STAR:
3109 msg = "*";
3110 break;
3111 case NIC_ADDR:
3112 msg = "&";
3113 break;
3114 case NIC_PREINCREMENT:
3115 msg = "++";
3116 break;
3117 case NIC_PREDECREMENT:
3118 msg = "--";
3119 break;
3120 case NIC_NEW:
3121 msg = "new";
3122 break;
3123 case NIC_DEL:
3124 msg = "delete";
3125 break;
3126 default:
3127 gcc_unreachable ();
3128 }
3129 if (msg)
3130 error ("%qs cannot appear in a constant-expression", msg);
3131 return true;
3132 }
3133 }
3134 return false;
3135 }
3136
3137 /* Emit a diagnostic for an invalid type name. This function commits
3138 to the current active tentative parse, if any. (Otherwise, the
3139 problematic construct might be encountered again later, resulting
3140 in duplicate error messages.) LOCATION is the location of ID. */
3141
3142 static void
3143 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3144 location_t location)
3145 {
3146 tree decl, ambiguous_decls;
3147 cp_parser_commit_to_tentative_parse (parser);
3148 /* Try to lookup the identifier. */
3149 decl = cp_parser_lookup_name (parser, id, none_type,
3150 /*is_template=*/false,
3151 /*is_namespace=*/false,
3152 /*check_dependency=*/true,
3153 &ambiguous_decls, location);
3154 if (ambiguous_decls)
3155 /* If the lookup was ambiguous, an error will already have
3156 been issued. */
3157 return;
3158 /* If the lookup found a template-name, it means that the user forgot
3159 to specify an argument list. Emit a useful error message. */
3160 if (DECL_TYPE_TEMPLATE_P (decl))
3161 {
3162 error_at (location,
3163 "invalid use of template-name %qE without an argument list",
3164 decl);
3165 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3166 inform (location, "class template argument deduction is only available "
3167 "with -std=c++1z or -std=gnu++1z");
3168 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3169 }
3170 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3171 error_at (location, "invalid use of destructor %qD as a type", id);
3172 else if (TREE_CODE (decl) == TYPE_DECL)
3173 /* Something like 'unsigned A a;' */
3174 error_at (location, "invalid combination of multiple type-specifiers");
3175 else if (!parser->scope)
3176 {
3177 /* Issue an error message. */
3178 const char *suggestion = NULL;
3179 if (TREE_CODE (id) == IDENTIFIER_NODE)
3180 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3181 if (suggestion)
3182 {
3183 gcc_rich_location richloc (location);
3184 richloc.add_fixit_replace (suggestion);
3185 error_at_rich_loc (&richloc,
3186 "%qE does not name a type; did you mean %qs?",
3187 id, suggestion);
3188 }
3189 else
3190 error_at (location, "%qE does not name a type", id);
3191 /* If we're in a template class, it's possible that the user was
3192 referring to a type from a base class. For example:
3193
3194 template <typename T> struct A { typedef T X; };
3195 template <typename T> struct B : public A<T> { X x; };
3196
3197 The user should have said "typename A<T>::X". */
3198 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3199 inform (location, "C++11 %<constexpr%> only available with "
3200 "-std=c++11 or -std=gnu++11");
3201 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3202 inform (location, "C++11 %<noexcept%> only available with "
3203 "-std=c++11 or -std=gnu++11");
3204 else if (cxx_dialect < cxx11
3205 && TREE_CODE (id) == IDENTIFIER_NODE
3206 && id_equal (id, "thread_local"))
3207 inform (location, "C++11 %<thread_local%> only available with "
3208 "-std=c++11 or -std=gnu++11");
3209 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3210 inform (location, "%<concept%> only available with -fconcepts");
3211 else if (processing_template_decl && current_class_type
3212 && TYPE_BINFO (current_class_type))
3213 {
3214 tree b;
3215
3216 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3217 b;
3218 b = TREE_CHAIN (b))
3219 {
3220 tree base_type = BINFO_TYPE (b);
3221 if (CLASS_TYPE_P (base_type)
3222 && dependent_type_p (base_type))
3223 {
3224 tree field;
3225 /* Go from a particular instantiation of the
3226 template (which will have an empty TYPE_FIELDs),
3227 to the main version. */
3228 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3229 for (field = TYPE_FIELDS (base_type);
3230 field;
3231 field = DECL_CHAIN (field))
3232 if (TREE_CODE (field) == TYPE_DECL
3233 && DECL_NAME (field) == id)
3234 {
3235 inform (location,
3236 "(perhaps %<typename %T::%E%> was intended)",
3237 BINFO_TYPE (b), id);
3238 break;
3239 }
3240 if (field)
3241 break;
3242 }
3243 }
3244 }
3245 }
3246 /* Here we diagnose qualified-ids where the scope is actually correct,
3247 but the identifier does not resolve to a valid type name. */
3248 else if (parser->scope != error_mark_node)
3249 {
3250 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3251 {
3252 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3253 error_at (location_of (id),
3254 "%qE in namespace %qE does not name a template type",
3255 id, parser->scope);
3256 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3257 error_at (location_of (id),
3258 "%qE in namespace %qE does not name a template type",
3259 TREE_OPERAND (id, 0), parser->scope);
3260 else
3261 error_at (location_of (id),
3262 "%qE in namespace %qE does not name a type",
3263 id, parser->scope);
3264 if (DECL_P (decl))
3265 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3266 }
3267 else if (CLASS_TYPE_P (parser->scope)
3268 && constructor_name_p (id, parser->scope))
3269 {
3270 /* A<T>::A<T>() */
3271 error_at (location, "%<%T::%E%> names the constructor, not"
3272 " the type", parser->scope, id);
3273 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3274 error_at (location, "and %qT has no template constructors",
3275 parser->scope);
3276 }
3277 else if (TYPE_P (parser->scope)
3278 && dependent_scope_p (parser->scope))
3279 {
3280 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3281 error_at (location,
3282 "need %<typename%> before %<%T::%D::%E%> because "
3283 "%<%T::%D%> is a dependent scope",
3284 TYPE_CONTEXT (parser->scope),
3285 TYPENAME_TYPE_FULLNAME (parser->scope),
3286 id,
3287 TYPE_CONTEXT (parser->scope),
3288 TYPENAME_TYPE_FULLNAME (parser->scope));
3289 else
3290 error_at (location, "need %<typename%> before %<%T::%E%> because "
3291 "%qT is a dependent scope",
3292 parser->scope, id, parser->scope);
3293 }
3294 else if (TYPE_P (parser->scope))
3295 {
3296 if (!COMPLETE_TYPE_P (parser->scope))
3297 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3298 parser->scope);
3299 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3300 error_at (location_of (id),
3301 "%qE in %q#T does not name a template type",
3302 id, parser->scope);
3303 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3304 error_at (location_of (id),
3305 "%qE in %q#T does not name a template type",
3306 TREE_OPERAND (id, 0), parser->scope);
3307 else
3308 error_at (location_of (id),
3309 "%qE in %q#T does not name a type",
3310 id, parser->scope);
3311 if (DECL_P (decl))
3312 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3313 }
3314 else
3315 gcc_unreachable ();
3316 }
3317 }
3318
3319 /* Check for a common situation where a type-name should be present,
3320 but is not, and issue a sensible error message. Returns true if an
3321 invalid type-name was detected.
3322
3323 The situation handled by this function are variable declarations of the
3324 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3325 Usually, `ID' should name a type, but if we got here it means that it
3326 does not. We try to emit the best possible error message depending on
3327 how exactly the id-expression looks like. */
3328
3329 static bool
3330 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3331 {
3332 tree id;
3333 cp_token *token = cp_lexer_peek_token (parser->lexer);
3334
3335 /* Avoid duplicate error about ambiguous lookup. */
3336 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3337 {
3338 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3339 if (next->type == CPP_NAME && next->error_reported)
3340 goto out;
3341 }
3342
3343 cp_parser_parse_tentatively (parser);
3344 id = cp_parser_id_expression (parser,
3345 /*template_keyword_p=*/false,
3346 /*check_dependency_p=*/true,
3347 /*template_p=*/NULL,
3348 /*declarator_p=*/true,
3349 /*optional_p=*/false);
3350 /* If the next token is a (, this is a function with no explicit return
3351 type, i.e. constructor, destructor or conversion op. */
3352 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3353 || TREE_CODE (id) == TYPE_DECL)
3354 {
3355 cp_parser_abort_tentative_parse (parser);
3356 return false;
3357 }
3358 if (!cp_parser_parse_definitely (parser))
3359 return false;
3360
3361 /* Emit a diagnostic for the invalid type. */
3362 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3363 out:
3364 /* If we aren't in the middle of a declarator (i.e. in a
3365 parameter-declaration-clause), skip to the end of the declaration;
3366 there's no point in trying to process it. */
3367 if (!parser->in_declarator_p)
3368 cp_parser_skip_to_end_of_block_or_statement (parser);
3369 return true;
3370 }
3371
3372 /* Consume tokens up to, and including, the next non-nested closing `)'.
3373 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3374 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3375 found an unnested token of that type. */
3376
3377 static int
3378 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3379 bool recovering,
3380 cpp_ttype or_ttype,
3381 bool consume_paren)
3382 {
3383 unsigned paren_depth = 0;
3384 unsigned brace_depth = 0;
3385 unsigned square_depth = 0;
3386
3387 if (recovering && or_ttype == CPP_EOF
3388 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3389 return 0;
3390
3391 while (true)
3392 {
3393 cp_token * token = cp_lexer_peek_token (parser->lexer);
3394
3395 /* Have we found what we're looking for before the closing paren? */
3396 if (token->type == or_ttype && or_ttype != CPP_EOF
3397 && !brace_depth && !paren_depth && !square_depth)
3398 return -1;
3399
3400 switch (token->type)
3401 {
3402 case CPP_EOF:
3403 case CPP_PRAGMA_EOL:
3404 /* If we've run out of tokens, then there is no closing `)'. */
3405 return 0;
3406
3407 /* This is good for lambda expression capture-lists. */
3408 case CPP_OPEN_SQUARE:
3409 ++square_depth;
3410 break;
3411 case CPP_CLOSE_SQUARE:
3412 if (!square_depth--)
3413 return 0;
3414 break;
3415
3416 case CPP_SEMICOLON:
3417 /* This matches the processing in skip_to_end_of_statement. */
3418 if (!brace_depth)
3419 return 0;
3420 break;
3421
3422 case CPP_OPEN_BRACE:
3423 ++brace_depth;
3424 break;
3425 case CPP_CLOSE_BRACE:
3426 if (!brace_depth--)
3427 return 0;
3428 break;
3429
3430 case CPP_OPEN_PAREN:
3431 if (!brace_depth)
3432 ++paren_depth;
3433 break;
3434
3435 case CPP_CLOSE_PAREN:
3436 if (!brace_depth && !paren_depth--)
3437 {
3438 if (consume_paren)
3439 cp_lexer_consume_token (parser->lexer);
3440 return 1;
3441 }
3442 break;
3443
3444 default:
3445 break;
3446 }
3447
3448 /* Consume the token. */
3449 cp_lexer_consume_token (parser->lexer);
3450 }
3451 }
3452
3453 /* Consume tokens up to, and including, the next non-nested closing `)'.
3454 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3455 are doing error recovery. Returns -1 if OR_COMMA is true and we
3456 found an unnested token of that type. */
3457
3458 static int
3459 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3460 bool recovering,
3461 bool or_comma,
3462 bool consume_paren)
3463 {
3464 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3465 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3466 ttype, consume_paren);
3467 }
3468
3469 /* Consume tokens until we reach the end of the current statement.
3470 Normally, that will be just before consuming a `;'. However, if a
3471 non-nested `}' comes first, then we stop before consuming that. */
3472
3473 static void
3474 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3475 {
3476 unsigned nesting_depth = 0;
3477
3478 /* Unwind generic function template scope if necessary. */
3479 if (parser->fully_implicit_function_template_p)
3480 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3481
3482 while (true)
3483 {
3484 cp_token *token = cp_lexer_peek_token (parser->lexer);
3485
3486 switch (token->type)
3487 {
3488 case CPP_EOF:
3489 case CPP_PRAGMA_EOL:
3490 /* If we've run out of tokens, stop. */
3491 return;
3492
3493 case CPP_SEMICOLON:
3494 /* If the next token is a `;', we have reached the end of the
3495 statement. */
3496 if (!nesting_depth)
3497 return;
3498 break;
3499
3500 case CPP_CLOSE_BRACE:
3501 /* If this is a non-nested '}', stop before consuming it.
3502 That way, when confronted with something like:
3503
3504 { 3 + }
3505
3506 we stop before consuming the closing '}', even though we
3507 have not yet reached a `;'. */
3508 if (nesting_depth == 0)
3509 return;
3510
3511 /* If it is the closing '}' for a block that we have
3512 scanned, stop -- but only after consuming the token.
3513 That way given:
3514
3515 void f g () { ... }
3516 typedef int I;
3517
3518 we will stop after the body of the erroneously declared
3519 function, but before consuming the following `typedef'
3520 declaration. */
3521 if (--nesting_depth == 0)
3522 {
3523 cp_lexer_consume_token (parser->lexer);
3524 return;
3525 }
3526 break;
3527
3528 case CPP_OPEN_BRACE:
3529 ++nesting_depth;
3530 break;
3531
3532 default:
3533 break;
3534 }
3535
3536 /* Consume the token. */
3537 cp_lexer_consume_token (parser->lexer);
3538 }
3539 }
3540
3541 /* This function is called at the end of a statement or declaration.
3542 If the next token is a semicolon, it is consumed; otherwise, error
3543 recovery is attempted. */
3544
3545 static void
3546 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3547 {
3548 /* Look for the trailing `;'. */
3549 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3550 {
3551 /* If there is additional (erroneous) input, skip to the end of
3552 the statement. */
3553 cp_parser_skip_to_end_of_statement (parser);
3554 /* If the next token is now a `;', consume it. */
3555 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3556 cp_lexer_consume_token (parser->lexer);
3557 }
3558 }
3559
3560 /* Skip tokens until we have consumed an entire block, or until we
3561 have consumed a non-nested `;'. */
3562
3563 static void
3564 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3565 {
3566 int nesting_depth = 0;
3567
3568 /* Unwind generic function template scope if necessary. */
3569 if (parser->fully_implicit_function_template_p)
3570 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3571
3572 while (nesting_depth >= 0)
3573 {
3574 cp_token *token = cp_lexer_peek_token (parser->lexer);
3575
3576 switch (token->type)
3577 {
3578 case CPP_EOF:
3579 case CPP_PRAGMA_EOL:
3580 /* If we've run out of tokens, stop. */
3581 return;
3582
3583 case CPP_SEMICOLON:
3584 /* Stop if this is an unnested ';'. */
3585 if (!nesting_depth)
3586 nesting_depth = -1;
3587 break;
3588
3589 case CPP_CLOSE_BRACE:
3590 /* Stop if this is an unnested '}', or closes the outermost
3591 nesting level. */
3592 nesting_depth--;
3593 if (nesting_depth < 0)
3594 return;
3595 if (!nesting_depth)
3596 nesting_depth = -1;
3597 break;
3598
3599 case CPP_OPEN_BRACE:
3600 /* Nest. */
3601 nesting_depth++;
3602 break;
3603
3604 default:
3605 break;
3606 }
3607
3608 /* Consume the token. */
3609 cp_lexer_consume_token (parser->lexer);
3610 }
3611 }
3612
3613 /* Skip tokens until a non-nested closing curly brace is the next
3614 token, or there are no more tokens. Return true in the first case,
3615 false otherwise. */
3616
3617 static bool
3618 cp_parser_skip_to_closing_brace (cp_parser *parser)
3619 {
3620 unsigned nesting_depth = 0;
3621
3622 while (true)
3623 {
3624 cp_token *token = cp_lexer_peek_token (parser->lexer);
3625
3626 switch (token->type)
3627 {
3628 case CPP_EOF:
3629 case CPP_PRAGMA_EOL:
3630 /* If we've run out of tokens, stop. */
3631 return false;
3632
3633 case CPP_CLOSE_BRACE:
3634 /* If the next token is a non-nested `}', then we have reached
3635 the end of the current block. */
3636 if (nesting_depth-- == 0)
3637 return true;
3638 break;
3639
3640 case CPP_OPEN_BRACE:
3641 /* If it the next token is a `{', then we are entering a new
3642 block. Consume the entire block. */
3643 ++nesting_depth;
3644 break;
3645
3646 default:
3647 break;
3648 }
3649
3650 /* Consume the token. */
3651 cp_lexer_consume_token (parser->lexer);
3652 }
3653 }
3654
3655 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3656 parameter is the PRAGMA token, allowing us to purge the entire pragma
3657 sequence. */
3658
3659 static void
3660 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3661 {
3662 cp_token *token;
3663
3664 parser->lexer->in_pragma = false;
3665
3666 do
3667 token = cp_lexer_consume_token (parser->lexer);
3668 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3669
3670 /* Ensure that the pragma is not parsed again. */
3671 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3672 }
3673
3674 /* Require pragma end of line, resyncing with it as necessary. The
3675 arguments are as for cp_parser_skip_to_pragma_eol. */
3676
3677 static void
3678 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3679 {
3680 parser->lexer->in_pragma = false;
3681 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3682 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3683 }
3684
3685 /* This is a simple wrapper around make_typename_type. When the id is
3686 an unresolved identifier node, we can provide a superior diagnostic
3687 using cp_parser_diagnose_invalid_type_name. */
3688
3689 static tree
3690 cp_parser_make_typename_type (cp_parser *parser, tree id,
3691 location_t id_location)
3692 {
3693 tree result;
3694 if (identifier_p (id))
3695 {
3696 result = make_typename_type (parser->scope, id, typename_type,
3697 /*complain=*/tf_none);
3698 if (result == error_mark_node)
3699 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3700 return result;
3701 }
3702 return make_typename_type (parser->scope, id, typename_type, tf_error);
3703 }
3704
3705 /* This is a wrapper around the
3706 make_{pointer,ptrmem,reference}_declarator functions that decides
3707 which one to call based on the CODE and CLASS_TYPE arguments. The
3708 CODE argument should be one of the values returned by
3709 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3710 appertain to the pointer or reference. */
3711
3712 static cp_declarator *
3713 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3714 cp_cv_quals cv_qualifiers,
3715 cp_declarator *target,
3716 tree attributes)
3717 {
3718 if (code == ERROR_MARK)
3719 return cp_error_declarator;
3720
3721 if (code == INDIRECT_REF)
3722 if (class_type == NULL_TREE)
3723 return make_pointer_declarator (cv_qualifiers, target, attributes);
3724 else
3725 return make_ptrmem_declarator (cv_qualifiers, class_type,
3726 target, attributes);
3727 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3728 return make_reference_declarator (cv_qualifiers, target,
3729 false, attributes);
3730 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3731 return make_reference_declarator (cv_qualifiers, target,
3732 true, attributes);
3733 gcc_unreachable ();
3734 }
3735
3736 /* Create a new C++ parser. */
3737
3738 static cp_parser *
3739 cp_parser_new (void)
3740 {
3741 cp_parser *parser;
3742 cp_lexer *lexer;
3743 unsigned i;
3744
3745 /* cp_lexer_new_main is called before doing GC allocation because
3746 cp_lexer_new_main might load a PCH file. */
3747 lexer = cp_lexer_new_main ();
3748
3749 /* Initialize the binops_by_token so that we can get the tree
3750 directly from the token. */
3751 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3752 binops_by_token[binops[i].token_type] = binops[i];
3753
3754 parser = ggc_cleared_alloc<cp_parser> ();
3755 parser->lexer = lexer;
3756 parser->context = cp_parser_context_new (NULL);
3757
3758 /* For now, we always accept GNU extensions. */
3759 parser->allow_gnu_extensions_p = 1;
3760
3761 /* The `>' token is a greater-than operator, not the end of a
3762 template-id. */
3763 parser->greater_than_is_operator_p = true;
3764
3765 parser->default_arg_ok_p = true;
3766
3767 /* We are not parsing a constant-expression. */
3768 parser->integral_constant_expression_p = false;
3769 parser->allow_non_integral_constant_expression_p = false;
3770 parser->non_integral_constant_expression_p = false;
3771
3772 /* Local variable names are not forbidden. */
3773 parser->local_variables_forbidden_p = false;
3774
3775 /* We are not processing an `extern "C"' declaration. */
3776 parser->in_unbraced_linkage_specification_p = false;
3777
3778 /* We are not processing a declarator. */
3779 parser->in_declarator_p = false;
3780
3781 /* We are not processing a template-argument-list. */
3782 parser->in_template_argument_list_p = false;
3783
3784 /* We are not in an iteration statement. */
3785 parser->in_statement = 0;
3786
3787 /* We are not in a switch statement. */
3788 parser->in_switch_statement_p = false;
3789
3790 /* We are not parsing a type-id inside an expression. */
3791 parser->in_type_id_in_expr_p = false;
3792
3793 /* Declarations aren't implicitly extern "C". */
3794 parser->implicit_extern_c = false;
3795
3796 /* String literals should be translated to the execution character set. */
3797 parser->translate_strings_p = true;
3798
3799 /* We are not parsing a function body. */
3800 parser->in_function_body = false;
3801
3802 /* We can correct until told otherwise. */
3803 parser->colon_corrects_to_scope_p = true;
3804
3805 /* The unparsed function queue is empty. */
3806 push_unparsed_function_queues (parser);
3807
3808 /* There are no classes being defined. */
3809 parser->num_classes_being_defined = 0;
3810
3811 /* No template parameters apply. */
3812 parser->num_template_parameter_lists = 0;
3813
3814 /* Special parsing data structures. */
3815 parser->omp_declare_simd = NULL;
3816 parser->cilk_simd_fn_info = NULL;
3817 parser->oacc_routine = NULL;
3818
3819 /* Not declaring an implicit function template. */
3820 parser->auto_is_implicit_function_template_parm_p = false;
3821 parser->fully_implicit_function_template_p = false;
3822 parser->implicit_template_parms = 0;
3823 parser->implicit_template_scope = 0;
3824
3825 /* Allow constrained-type-specifiers. */
3826 parser->prevent_constrained_type_specifiers = 0;
3827
3828 return parser;
3829 }
3830
3831 /* Create a cp_lexer structure which will emit the tokens in CACHE
3832 and push it onto the parser's lexer stack. This is used for delayed
3833 parsing of in-class method bodies and default arguments, and should
3834 not be confused with tentative parsing. */
3835 static void
3836 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3837 {
3838 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3839 lexer->next = parser->lexer;
3840 parser->lexer = lexer;
3841
3842 /* Move the current source position to that of the first token in the
3843 new lexer. */
3844 cp_lexer_set_source_position_from_token (lexer->next_token);
3845 }
3846
3847 /* Pop the top lexer off the parser stack. This is never used for the
3848 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3849 static void
3850 cp_parser_pop_lexer (cp_parser *parser)
3851 {
3852 cp_lexer *lexer = parser->lexer;
3853 parser->lexer = lexer->next;
3854 cp_lexer_destroy (lexer);
3855
3856 /* Put the current source position back where it was before this
3857 lexer was pushed. */
3858 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3859 }
3860
3861 /* Lexical conventions [gram.lex] */
3862
3863 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3864 identifier. */
3865
3866 static cp_expr
3867 cp_parser_identifier (cp_parser* parser)
3868 {
3869 cp_token *token;
3870
3871 /* Look for the identifier. */
3872 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3873 /* Return the value. */
3874 if (token)
3875 return cp_expr (token->u.value, token->location);
3876 else
3877 return error_mark_node;
3878 }
3879
3880 /* Parse a sequence of adjacent string constants. Returns a
3881 TREE_STRING representing the combined, nul-terminated string
3882 constant. If TRANSLATE is true, translate the string to the
3883 execution character set. If WIDE_OK is true, a wide string is
3884 invalid here.
3885
3886 C++98 [lex.string] says that if a narrow string literal token is
3887 adjacent to a wide string literal token, the behavior is undefined.
3888 However, C99 6.4.5p4 says that this results in a wide string literal.
3889 We follow C99 here, for consistency with the C front end.
3890
3891 This code is largely lifted from lex_string() in c-lex.c.
3892
3893 FUTURE: ObjC++ will need to handle @-strings here. */
3894 static cp_expr
3895 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3896 bool lookup_udlit = true)
3897 {
3898 tree value;
3899 size_t count;
3900 struct obstack str_ob;
3901 cpp_string str, istr, *strs;
3902 cp_token *tok;
3903 enum cpp_ttype type, curr_type;
3904 int have_suffix_p = 0;
3905 tree string_tree;
3906 tree suffix_id = NULL_TREE;
3907 bool curr_tok_is_userdef_p = false;
3908
3909 tok = cp_lexer_peek_token (parser->lexer);
3910 if (!cp_parser_is_string_literal (tok))
3911 {
3912 cp_parser_error (parser, "expected string-literal");
3913 return error_mark_node;
3914 }
3915
3916 location_t loc = tok->location;
3917
3918 if (cpp_userdef_string_p (tok->type))
3919 {
3920 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3921 curr_type = cpp_userdef_string_remove_type (tok->type);
3922 curr_tok_is_userdef_p = true;
3923 }
3924 else
3925 {
3926 string_tree = tok->u.value;
3927 curr_type = tok->type;
3928 }
3929 type = curr_type;
3930
3931 /* Try to avoid the overhead of creating and destroying an obstack
3932 for the common case of just one string. */
3933 if (!cp_parser_is_string_literal
3934 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3935 {
3936 cp_lexer_consume_token (parser->lexer);
3937
3938 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3939 str.len = TREE_STRING_LENGTH (string_tree);
3940 count = 1;
3941
3942 if (curr_tok_is_userdef_p)
3943 {
3944 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3945 have_suffix_p = 1;
3946 curr_type = cpp_userdef_string_remove_type (tok->type);
3947 }
3948 else
3949 curr_type = tok->type;
3950
3951 strs = &str;
3952 }
3953 else
3954 {
3955 location_t last_tok_loc = tok->location;
3956 gcc_obstack_init (&str_ob);
3957 count = 0;
3958
3959 do
3960 {
3961 cp_lexer_consume_token (parser->lexer);
3962 count++;
3963 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3964 str.len = TREE_STRING_LENGTH (string_tree);
3965
3966 if (curr_tok_is_userdef_p)
3967 {
3968 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3969 if (have_suffix_p == 0)
3970 {
3971 suffix_id = curr_suffix_id;
3972 have_suffix_p = 1;
3973 }
3974 else if (have_suffix_p == 1
3975 && curr_suffix_id != suffix_id)
3976 {
3977 error ("inconsistent user-defined literal suffixes"
3978 " %qD and %qD in string literal",
3979 suffix_id, curr_suffix_id);
3980 have_suffix_p = -1;
3981 }
3982 curr_type = cpp_userdef_string_remove_type (tok->type);
3983 }
3984 else
3985 curr_type = tok->type;
3986
3987 if (type != curr_type)
3988 {
3989 if (type == CPP_STRING)
3990 type = curr_type;
3991 else if (curr_type != CPP_STRING)
3992 {
3993 rich_location rich_loc (line_table, tok->location);
3994 rich_loc.add_range (last_tok_loc, false);
3995 error_at_rich_loc (&rich_loc,
3996 "unsupported non-standard concatenation "
3997 "of string literals");
3998 }
3999 }
4000
4001 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4002
4003 last_tok_loc = tok->location;
4004
4005 tok = cp_lexer_peek_token (parser->lexer);
4006 if (cpp_userdef_string_p (tok->type))
4007 {
4008 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4009 curr_type = cpp_userdef_string_remove_type (tok->type);
4010 curr_tok_is_userdef_p = true;
4011 }
4012 else
4013 {
4014 string_tree = tok->u.value;
4015 curr_type = tok->type;
4016 curr_tok_is_userdef_p = false;
4017 }
4018 }
4019 while (cp_parser_is_string_literal (tok));
4020
4021 /* A string literal built by concatenation has its caret=start at
4022 the start of the initial string, and its finish at the finish of
4023 the final string literal. */
4024 loc = make_location (loc, loc, get_finish (last_tok_loc));
4025
4026 strs = (cpp_string *) obstack_finish (&str_ob);
4027 }
4028
4029 if (type != CPP_STRING && !wide_ok)
4030 {
4031 cp_parser_error (parser, "a wide string is invalid in this context");
4032 type = CPP_STRING;
4033 }
4034
4035 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4036 (parse_in, strs, count, &istr, type))
4037 {
4038 value = build_string (istr.len, (const char *)istr.text);
4039 free (CONST_CAST (unsigned char *, istr.text));
4040
4041 switch (type)
4042 {
4043 default:
4044 case CPP_STRING:
4045 case CPP_UTF8STRING:
4046 TREE_TYPE (value) = char_array_type_node;
4047 break;
4048 case CPP_STRING16:
4049 TREE_TYPE (value) = char16_array_type_node;
4050 break;
4051 case CPP_STRING32:
4052 TREE_TYPE (value) = char32_array_type_node;
4053 break;
4054 case CPP_WSTRING:
4055 TREE_TYPE (value) = wchar_array_type_node;
4056 break;
4057 }
4058
4059 value = fix_string_type (value);
4060
4061 if (have_suffix_p)
4062 {
4063 tree literal = build_userdef_literal (suffix_id, value,
4064 OT_NONE, NULL_TREE);
4065 if (lookup_udlit)
4066 value = cp_parser_userdef_string_literal (literal);
4067 else
4068 value = literal;
4069 }
4070 }
4071 else
4072 /* cpp_interpret_string has issued an error. */
4073 value = error_mark_node;
4074
4075 if (count > 1)
4076 obstack_free (&str_ob, 0);
4077
4078 return cp_expr (value, loc);
4079 }
4080
4081 /* Look up a literal operator with the name and the exact arguments. */
4082
4083 static tree
4084 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4085 {
4086 tree decl;
4087 decl = lookup_name (name);
4088 if (!decl || !is_overloaded_fn (decl))
4089 return error_mark_node;
4090
4091 for (lkp_iterator iter (decl); iter; ++iter)
4092 {
4093 unsigned int ix;
4094 bool found = true;
4095 tree fn = *iter;
4096 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4097 if (parmtypes != NULL_TREE)
4098 {
4099 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4100 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4101 {
4102 tree tparm = TREE_VALUE (parmtypes);
4103 tree targ = TREE_TYPE ((*args)[ix]);
4104 bool ptr = TYPE_PTR_P (tparm);
4105 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4106 if ((ptr || arr || !same_type_p (tparm, targ))
4107 && (!ptr || !arr
4108 || !same_type_p (TREE_TYPE (tparm),
4109 TREE_TYPE (targ))))
4110 found = false;
4111 }
4112 if (found
4113 && ix == vec_safe_length (args)
4114 /* May be this should be sufficient_parms_p instead,
4115 depending on how exactly should user-defined literals
4116 work in presence of default arguments on the literal
4117 operator parameters. */
4118 && parmtypes == void_list_node)
4119 return decl;
4120 }
4121 }
4122
4123 return error_mark_node;
4124 }
4125
4126 /* Parse a user-defined char constant. Returns a call to a user-defined
4127 literal operator taking the character as an argument. */
4128
4129 static cp_expr
4130 cp_parser_userdef_char_literal (cp_parser *parser)
4131 {
4132 cp_token *token = cp_lexer_consume_token (parser->lexer);
4133 tree literal = token->u.value;
4134 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4135 tree value = USERDEF_LITERAL_VALUE (literal);
4136 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4137 tree decl, result;
4138
4139 /* Build up a call to the user-defined operator */
4140 /* Lookup the name we got back from the id-expression. */
4141 vec<tree, va_gc> *args = make_tree_vector ();
4142 vec_safe_push (args, value);
4143 decl = lookup_literal_operator (name, args);
4144 if (!decl || decl == error_mark_node)
4145 {
4146 error ("unable to find character literal operator %qD with %qT argument",
4147 name, TREE_TYPE (value));
4148 release_tree_vector (args);
4149 return error_mark_node;
4150 }
4151 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4152 release_tree_vector (args);
4153 return result;
4154 }
4155
4156 /* A subroutine of cp_parser_userdef_numeric_literal to
4157 create a char... template parameter pack from a string node. */
4158
4159 static tree
4160 make_char_string_pack (tree value)
4161 {
4162 tree charvec;
4163 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4164 const char *str = TREE_STRING_POINTER (value);
4165 int i, len = TREE_STRING_LENGTH (value) - 1;
4166 tree argvec = make_tree_vec (1);
4167
4168 /* Fill in CHARVEC with all of the parameters. */
4169 charvec = make_tree_vec (len);
4170 for (i = 0; i < len; ++i)
4171 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4172
4173 /* Build the argument packs. */
4174 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4175
4176 TREE_VEC_ELT (argvec, 0) = argpack;
4177
4178 return argvec;
4179 }
4180
4181 /* A subroutine of cp_parser_userdef_numeric_literal to
4182 create a char... template parameter pack from a string node. */
4183
4184 static tree
4185 make_string_pack (tree value)
4186 {
4187 tree charvec;
4188 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4189 const unsigned char *str
4190 = (const unsigned char *) TREE_STRING_POINTER (value);
4191 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4192 int len = TREE_STRING_LENGTH (value) / sz - 1;
4193 tree argvec = make_tree_vec (2);
4194
4195 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4196 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4197
4198 /* First template parm is character type. */
4199 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4200
4201 /* Fill in CHARVEC with all of the parameters. */
4202 charvec = make_tree_vec (len);
4203 for (int i = 0; i < len; ++i)
4204 TREE_VEC_ELT (charvec, i)
4205 = double_int_to_tree (str_char_type_node,
4206 double_int::from_buffer (str + i * sz, sz));
4207
4208 /* Build the argument packs. */
4209 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4210
4211 TREE_VEC_ELT (argvec, 1) = argpack;
4212
4213 return argvec;
4214 }
4215
4216 /* Parse a user-defined numeric constant. returns a call to a user-defined
4217 literal operator. */
4218
4219 static cp_expr
4220 cp_parser_userdef_numeric_literal (cp_parser *parser)
4221 {
4222 cp_token *token = cp_lexer_consume_token (parser->lexer);
4223 tree literal = token->u.value;
4224 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4225 tree value = USERDEF_LITERAL_VALUE (literal);
4226 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4227 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4228 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4229 tree decl, result;
4230 vec<tree, va_gc> *args;
4231
4232 /* Look for a literal operator taking the exact type of numeric argument
4233 as the literal value. */
4234 args = make_tree_vector ();
4235 vec_safe_push (args, value);
4236 decl = lookup_literal_operator (name, args);
4237 if (decl && decl != error_mark_node)
4238 {
4239 result = finish_call_expr (decl, &args, false, true,
4240 tf_warning_or_error);
4241
4242 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4243 {
4244 warning_at (token->location, OPT_Woverflow,
4245 "integer literal exceeds range of %qT type",
4246 long_long_unsigned_type_node);
4247 }
4248 else
4249 {
4250 if (overflow > 0)
4251 warning_at (token->location, OPT_Woverflow,
4252 "floating literal exceeds range of %qT type",
4253 long_double_type_node);
4254 else if (overflow < 0)
4255 warning_at (token->location, OPT_Woverflow,
4256 "floating literal truncated to zero");
4257 }
4258
4259 release_tree_vector (args);
4260 return result;
4261 }
4262 release_tree_vector (args);
4263
4264 /* If the numeric argument didn't work, look for a raw literal
4265 operator taking a const char* argument consisting of the number
4266 in string format. */
4267 args = make_tree_vector ();
4268 vec_safe_push (args, num_string);
4269 decl = lookup_literal_operator (name, args);
4270 if (decl && decl != error_mark_node)
4271 {
4272 result = finish_call_expr (decl, &args, false, true,
4273 tf_warning_or_error);
4274 release_tree_vector (args);
4275 return result;
4276 }
4277 release_tree_vector (args);
4278
4279 /* If the raw literal didn't work, look for a non-type template
4280 function with parameter pack char.... Call the function with
4281 template parameter characters representing the number. */
4282 args = make_tree_vector ();
4283 decl = lookup_literal_operator (name, args);
4284 if (decl && decl != error_mark_node)
4285 {
4286 tree tmpl_args = make_char_string_pack (num_string);
4287 decl = lookup_template_function (decl, tmpl_args);
4288 result = finish_call_expr (decl, &args, false, true,
4289 tf_warning_or_error);
4290 release_tree_vector (args);
4291 return result;
4292 }
4293
4294 release_tree_vector (args);
4295
4296 error ("unable to find numeric literal operator %qD", name);
4297 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4298 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4299 "to enable more built-in suffixes");
4300 return error_mark_node;
4301 }
4302
4303 /* Parse a user-defined string constant. Returns a call to a user-defined
4304 literal operator taking a character pointer and the length of the string
4305 as arguments. */
4306
4307 static tree
4308 cp_parser_userdef_string_literal (tree literal)
4309 {
4310 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4311 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4312 tree value = USERDEF_LITERAL_VALUE (literal);
4313 int len = TREE_STRING_LENGTH (value)
4314 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4315 tree decl, result;
4316 vec<tree, va_gc> *args;
4317
4318 /* Build up a call to the user-defined operator. */
4319 /* Lookup the name we got back from the id-expression. */
4320 args = make_tree_vector ();
4321 vec_safe_push (args, value);
4322 vec_safe_push (args, build_int_cst (size_type_node, len));
4323 decl = lookup_literal_operator (name, args);
4324
4325 if (decl && decl != error_mark_node)
4326 {
4327 result = finish_call_expr (decl, &args, false, true,
4328 tf_warning_or_error);
4329 release_tree_vector (args);
4330 return result;
4331 }
4332 release_tree_vector (args);
4333
4334 /* Look for a template function with typename parameter CharT
4335 and parameter pack CharT... Call the function with
4336 template parameter characters representing the string. */
4337 args = make_tree_vector ();
4338 decl = lookup_literal_operator (name, args);
4339 if (decl && decl != error_mark_node)
4340 {
4341 tree tmpl_args = make_string_pack (value);
4342 decl = lookup_template_function (decl, tmpl_args);
4343 result = finish_call_expr (decl, &args, false, true,
4344 tf_warning_or_error);
4345 release_tree_vector (args);
4346 return result;
4347 }
4348 release_tree_vector (args);
4349
4350 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4351 name, TREE_TYPE (value), size_type_node);
4352 return error_mark_node;
4353 }
4354
4355
4356 /* Basic concepts [gram.basic] */
4357
4358 /* Parse a translation-unit.
4359
4360 translation-unit:
4361 declaration-seq [opt]
4362
4363 Returns TRUE if all went well. */
4364
4365 static bool
4366 cp_parser_translation_unit (cp_parser* parser)
4367 {
4368 /* The address of the first non-permanent object on the declarator
4369 obstack. */
4370 static void *declarator_obstack_base;
4371
4372 bool success;
4373
4374 /* Create the declarator obstack, if necessary. */
4375 if (!cp_error_declarator)
4376 {
4377 gcc_obstack_init (&declarator_obstack);
4378 /* Create the error declarator. */
4379 cp_error_declarator = make_declarator (cdk_error);
4380 /* Create the empty parameter list. */
4381 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4382 /* Remember where the base of the declarator obstack lies. */
4383 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4384 }
4385
4386 cp_parser_declaration_seq_opt (parser);
4387
4388 /* If there are no tokens left then all went well. */
4389 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4390 {
4391 /* Get rid of the token array; we don't need it any more. */
4392 cp_lexer_destroy (parser->lexer);
4393 parser->lexer = NULL;
4394
4395 /* This file might have been a context that's implicitly extern
4396 "C". If so, pop the lang context. (Only relevant for PCH.) */
4397 if (parser->implicit_extern_c)
4398 {
4399 pop_lang_context ();
4400 parser->implicit_extern_c = false;
4401 }
4402
4403 /* Finish up. */
4404 finish_translation_unit ();
4405
4406 success = true;
4407 }
4408 else
4409 {
4410 cp_parser_error (parser, "expected declaration");
4411 success = false;
4412 }
4413
4414 /* Make sure the declarator obstack was fully cleaned up. */
4415 gcc_assert (obstack_next_free (&declarator_obstack)
4416 == declarator_obstack_base);
4417
4418 /* All went well. */
4419 return success;
4420 }
4421
4422 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4423 decltype context. */
4424
4425 static inline tsubst_flags_t
4426 complain_flags (bool decltype_p)
4427 {
4428 tsubst_flags_t complain = tf_warning_or_error;
4429 if (decltype_p)
4430 complain |= tf_decltype;
4431 return complain;
4432 }
4433
4434 /* We're about to parse a collection of statements. If we're currently
4435 parsing tentatively, set up a firewall so that any nested
4436 cp_parser_commit_to_tentative_parse won't affect the current context. */
4437
4438 static cp_token_position
4439 cp_parser_start_tentative_firewall (cp_parser *parser)
4440 {
4441 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4442 return 0;
4443
4444 cp_parser_parse_tentatively (parser);
4445 cp_parser_commit_to_topmost_tentative_parse (parser);
4446 return cp_lexer_token_position (parser->lexer, false);
4447 }
4448
4449 /* We've finished parsing the collection of statements. Wrap up the
4450 firewall and replace the relevant tokens with the parsed form. */
4451
4452 static void
4453 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4454 tree expr)
4455 {
4456 if (!start)
4457 return;
4458
4459 /* Finish the firewall level. */
4460 cp_parser_parse_definitely (parser);
4461 /* And remember the result of the parse for when we try again. */
4462 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4463 token->type = CPP_PREPARSED_EXPR;
4464 token->u.value = expr;
4465 token->keyword = RID_MAX;
4466 cp_lexer_purge_tokens_after (parser->lexer, start);
4467 }
4468
4469 /* Like the above functions, but let the user modify the tokens. Used by
4470 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4471 later parses, so it makes sense to localize the effects of
4472 cp_parser_commit_to_tentative_parse. */
4473
4474 struct tentative_firewall
4475 {
4476 cp_parser *parser;
4477 bool set;
4478
4479 tentative_firewall (cp_parser *p): parser(p)
4480 {
4481 /* If we're currently parsing tentatively, start a committed level as a
4482 firewall and then an inner tentative parse. */
4483 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4484 {
4485 cp_parser_parse_tentatively (parser);
4486 cp_parser_commit_to_topmost_tentative_parse (parser);
4487 cp_parser_parse_tentatively (parser);
4488 }
4489 }
4490
4491 ~tentative_firewall()
4492 {
4493 if (set)
4494 {
4495 /* Finish the inner tentative parse and the firewall, propagating any
4496 uncommitted error state to the outer tentative parse. */
4497 bool err = cp_parser_error_occurred (parser);
4498 cp_parser_parse_definitely (parser);
4499 cp_parser_parse_definitely (parser);
4500 if (err)
4501 cp_parser_simulate_error (parser);
4502 }
4503 }
4504 };
4505
4506 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4507 enclosing parentheses. */
4508
4509 static cp_expr
4510 cp_parser_statement_expr (cp_parser *parser)
4511 {
4512 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4513
4514 /* Consume the '('. */
4515 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4516 cp_lexer_consume_token (parser->lexer);
4517 /* Start the statement-expression. */
4518 tree expr = begin_stmt_expr ();
4519 /* Parse the compound-statement. */
4520 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4521 /* Finish up. */
4522 expr = finish_stmt_expr (expr, false);
4523 /* Consume the ')'. */
4524 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4525 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4526 cp_parser_skip_to_end_of_statement (parser);
4527
4528 cp_parser_end_tentative_firewall (parser, start, expr);
4529 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4530 return cp_expr (expr, combined_loc);
4531 }
4532
4533 /* Expressions [gram.expr] */
4534
4535 /* Parse a fold-operator.
4536
4537 fold-operator:
4538 - * / % ^ & | = < > << >>
4539 = -= *= /= %= ^= &= |= <<= >>=
4540 == != <= >= && || , .* ->*
4541
4542 This returns the tree code corresponding to the matched operator
4543 as an int. When the current token matches a compound assignment
4544 opertor, the resulting tree code is the negative value of the
4545 non-assignment operator. */
4546
4547 static int
4548 cp_parser_fold_operator (cp_token *token)
4549 {
4550 switch (token->type)
4551 {
4552 case CPP_PLUS: return PLUS_EXPR;
4553 case CPP_MINUS: return MINUS_EXPR;
4554 case CPP_MULT: return MULT_EXPR;
4555 case CPP_DIV: return TRUNC_DIV_EXPR;
4556 case CPP_MOD: return TRUNC_MOD_EXPR;
4557 case CPP_XOR: return BIT_XOR_EXPR;
4558 case CPP_AND: return BIT_AND_EXPR;
4559 case CPP_OR: return BIT_IOR_EXPR;
4560 case CPP_LSHIFT: return LSHIFT_EXPR;
4561 case CPP_RSHIFT: return RSHIFT_EXPR;
4562
4563 case CPP_EQ: return -NOP_EXPR;
4564 case CPP_PLUS_EQ: return -PLUS_EXPR;
4565 case CPP_MINUS_EQ: return -MINUS_EXPR;
4566 case CPP_MULT_EQ: return -MULT_EXPR;
4567 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4568 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4569 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4570 case CPP_AND_EQ: return -BIT_AND_EXPR;
4571 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4572 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4573 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4574
4575 case CPP_EQ_EQ: return EQ_EXPR;
4576 case CPP_NOT_EQ: return NE_EXPR;
4577 case CPP_LESS: return LT_EXPR;
4578 case CPP_GREATER: return GT_EXPR;
4579 case CPP_LESS_EQ: return LE_EXPR;
4580 case CPP_GREATER_EQ: return GE_EXPR;
4581
4582 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4583 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4584
4585 case CPP_COMMA: return COMPOUND_EXPR;
4586
4587 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4588 case CPP_DEREF_STAR: return MEMBER_REF;
4589
4590 default: return ERROR_MARK;
4591 }
4592 }
4593
4594 /* Returns true if CODE indicates a binary expression, which is not allowed in
4595 the LHS of a fold-expression. More codes will need to be added to use this
4596 function in other contexts. */
4597
4598 static bool
4599 is_binary_op (tree_code code)
4600 {
4601 switch (code)
4602 {
4603 case PLUS_EXPR:
4604 case POINTER_PLUS_EXPR:
4605 case MINUS_EXPR:
4606 case MULT_EXPR:
4607 case TRUNC_DIV_EXPR:
4608 case TRUNC_MOD_EXPR:
4609 case BIT_XOR_EXPR:
4610 case BIT_AND_EXPR:
4611 case BIT_IOR_EXPR:
4612 case LSHIFT_EXPR:
4613 case RSHIFT_EXPR:
4614
4615 case MODOP_EXPR:
4616
4617 case EQ_EXPR:
4618 case NE_EXPR:
4619 case LE_EXPR:
4620 case GE_EXPR:
4621 case LT_EXPR:
4622 case GT_EXPR:
4623
4624 case TRUTH_ANDIF_EXPR:
4625 case TRUTH_ORIF_EXPR:
4626
4627 case COMPOUND_EXPR:
4628
4629 case DOTSTAR_EXPR:
4630 case MEMBER_REF:
4631 return true;
4632
4633 default:
4634 return false;
4635 }
4636 }
4637
4638 /* If the next token is a suitable fold operator, consume it and return as
4639 the function above. */
4640
4641 static int
4642 cp_parser_fold_operator (cp_parser *parser)
4643 {
4644 cp_token* token = cp_lexer_peek_token (parser->lexer);
4645 int code = cp_parser_fold_operator (token);
4646 if (code != ERROR_MARK)
4647 cp_lexer_consume_token (parser->lexer);
4648 return code;
4649 }
4650
4651 /* Parse a fold-expression.
4652
4653 fold-expression:
4654 ( ... folding-operator cast-expression)
4655 ( cast-expression folding-operator ... )
4656 ( cast-expression folding operator ... folding-operator cast-expression)
4657
4658 Note that the '(' and ')' are matched in primary expression. */
4659
4660 static cp_expr
4661 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4662 {
4663 cp_id_kind pidk;
4664
4665 // Left fold.
4666 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4667 {
4668 cp_lexer_consume_token (parser->lexer);
4669 int op = cp_parser_fold_operator (parser);
4670 if (op == ERROR_MARK)
4671 {
4672 cp_parser_error (parser, "expected binary operator");
4673 return error_mark_node;
4674 }
4675
4676 tree expr = cp_parser_cast_expression (parser, false, false,
4677 false, &pidk);
4678 if (expr == error_mark_node)
4679 return error_mark_node;
4680 return finish_left_unary_fold_expr (expr, op);
4681 }
4682
4683 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4684 int op = cp_parser_fold_operator (parser);
4685 if (op == ERROR_MARK)
4686 {
4687 cp_parser_error (parser, "expected binary operator");
4688 return error_mark_node;
4689 }
4690
4691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4692 {
4693 cp_parser_error (parser, "expected ...");
4694 return error_mark_node;
4695 }
4696 cp_lexer_consume_token (parser->lexer);
4697
4698 /* The operands of a fold-expression are cast-expressions, so binary or
4699 conditional expressions are not allowed. We check this here to avoid
4700 tentative parsing. */
4701 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4702 /* OK, the expression was parenthesized. */;
4703 else if (is_binary_op (TREE_CODE (expr1)))
4704 error_at (location_of (expr1),
4705 "binary expression in operand of fold-expression");
4706 else if (TREE_CODE (expr1) == COND_EXPR)
4707 error_at (location_of (expr1),
4708 "conditional expression in operand of fold-expression");
4709
4710 // Right fold.
4711 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4712 return finish_right_unary_fold_expr (expr1, op);
4713
4714 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4715 {
4716 cp_parser_error (parser, "mismatched operator in fold-expression");
4717 return error_mark_node;
4718 }
4719 cp_lexer_consume_token (parser->lexer);
4720
4721 // Binary left or right fold.
4722 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4723 if (expr2 == error_mark_node)
4724 return error_mark_node;
4725 return finish_binary_fold_expr (expr1, expr2, op);
4726 }
4727
4728 /* Parse a primary-expression.
4729
4730 primary-expression:
4731 literal
4732 this
4733 ( expression )
4734 id-expression
4735 lambda-expression (C++11)
4736
4737 GNU Extensions:
4738
4739 primary-expression:
4740 ( compound-statement )
4741 __builtin_va_arg ( assignment-expression , type-id )
4742 __builtin_offsetof ( type-id , offsetof-expression )
4743
4744 C++ Extensions:
4745 __has_nothrow_assign ( type-id )
4746 __has_nothrow_constructor ( type-id )
4747 __has_nothrow_copy ( type-id )
4748 __has_trivial_assign ( type-id )
4749 __has_trivial_constructor ( type-id )
4750 __has_trivial_copy ( type-id )
4751 __has_trivial_destructor ( type-id )
4752 __has_virtual_destructor ( type-id )
4753 __is_abstract ( type-id )
4754 __is_base_of ( type-id , type-id )
4755 __is_class ( type-id )
4756 __is_empty ( type-id )
4757 __is_enum ( type-id )
4758 __is_final ( type-id )
4759 __is_literal_type ( type-id )
4760 __is_pod ( type-id )
4761 __is_polymorphic ( type-id )
4762 __is_std_layout ( type-id )
4763 __is_trivial ( type-id )
4764 __is_union ( type-id )
4765
4766 Objective-C++ Extension:
4767
4768 primary-expression:
4769 objc-expression
4770
4771 literal:
4772 __null
4773
4774 ADDRESS_P is true iff this expression was immediately preceded by
4775 "&" and therefore might denote a pointer-to-member. CAST_P is true
4776 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4777 true iff this expression is a template argument.
4778
4779 Returns a representation of the expression. Upon return, *IDK
4780 indicates what kind of id-expression (if any) was present. */
4781
4782 static cp_expr
4783 cp_parser_primary_expression (cp_parser *parser,
4784 bool address_p,
4785 bool cast_p,
4786 bool template_arg_p,
4787 bool decltype_p,
4788 cp_id_kind *idk)
4789 {
4790 cp_token *token = NULL;
4791
4792 /* Assume the primary expression is not an id-expression. */
4793 *idk = CP_ID_KIND_NONE;
4794
4795 /* Peek at the next token. */
4796 token = cp_lexer_peek_token (parser->lexer);
4797 switch ((int) token->type)
4798 {
4799 /* literal:
4800 integer-literal
4801 character-literal
4802 floating-literal
4803 string-literal
4804 boolean-literal
4805 pointer-literal
4806 user-defined-literal */
4807 case CPP_CHAR:
4808 case CPP_CHAR16:
4809 case CPP_CHAR32:
4810 case CPP_WCHAR:
4811 case CPP_UTF8CHAR:
4812 case CPP_NUMBER:
4813 case CPP_PREPARSED_EXPR:
4814 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4815 return cp_parser_userdef_numeric_literal (parser);
4816 token = cp_lexer_consume_token (parser->lexer);
4817 if (TREE_CODE (token->u.value) == FIXED_CST)
4818 {
4819 error_at (token->location,
4820 "fixed-point types not supported in C++");
4821 return error_mark_node;
4822 }
4823 /* Floating-point literals are only allowed in an integral
4824 constant expression if they are cast to an integral or
4825 enumeration type. */
4826 if (TREE_CODE (token->u.value) == REAL_CST
4827 && parser->integral_constant_expression_p
4828 && pedantic)
4829 {
4830 /* CAST_P will be set even in invalid code like "int(2.7 +
4831 ...)". Therefore, we have to check that the next token
4832 is sure to end the cast. */
4833 if (cast_p)
4834 {
4835 cp_token *next_token;
4836
4837 next_token = cp_lexer_peek_token (parser->lexer);
4838 if (/* The comma at the end of an
4839 enumerator-definition. */
4840 next_token->type != CPP_COMMA
4841 /* The curly brace at the end of an enum-specifier. */
4842 && next_token->type != CPP_CLOSE_BRACE
4843 /* The end of a statement. */
4844 && next_token->type != CPP_SEMICOLON
4845 /* The end of the cast-expression. */
4846 && next_token->type != CPP_CLOSE_PAREN
4847 /* The end of an array bound. */
4848 && next_token->type != CPP_CLOSE_SQUARE
4849 /* The closing ">" in a template-argument-list. */
4850 && (next_token->type != CPP_GREATER
4851 || parser->greater_than_is_operator_p)
4852 /* C++0x only: A ">>" treated like two ">" tokens,
4853 in a template-argument-list. */
4854 && (next_token->type != CPP_RSHIFT
4855 || (cxx_dialect == cxx98)
4856 || parser->greater_than_is_operator_p))
4857 cast_p = false;
4858 }
4859
4860 /* If we are within a cast, then the constraint that the
4861 cast is to an integral or enumeration type will be
4862 checked at that point. If we are not within a cast, then
4863 this code is invalid. */
4864 if (!cast_p)
4865 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4866 }
4867 return cp_expr (token->u.value, token->location);
4868
4869 case CPP_CHAR_USERDEF:
4870 case CPP_CHAR16_USERDEF:
4871 case CPP_CHAR32_USERDEF:
4872 case CPP_WCHAR_USERDEF:
4873 case CPP_UTF8CHAR_USERDEF:
4874 return cp_parser_userdef_char_literal (parser);
4875
4876 case CPP_STRING:
4877 case CPP_STRING16:
4878 case CPP_STRING32:
4879 case CPP_WSTRING:
4880 case CPP_UTF8STRING:
4881 case CPP_STRING_USERDEF:
4882 case CPP_STRING16_USERDEF:
4883 case CPP_STRING32_USERDEF:
4884 case CPP_WSTRING_USERDEF:
4885 case CPP_UTF8STRING_USERDEF:
4886 /* ??? Should wide strings be allowed when parser->translate_strings_p
4887 is false (i.e. in attributes)? If not, we can kill the third
4888 argument to cp_parser_string_literal. */
4889 return cp_parser_string_literal (parser,
4890 parser->translate_strings_p,
4891 true);
4892
4893 case CPP_OPEN_PAREN:
4894 /* If we see `( { ' then we are looking at the beginning of
4895 a GNU statement-expression. */
4896 if (cp_parser_allow_gnu_extensions_p (parser)
4897 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4898 {
4899 /* Statement-expressions are not allowed by the standard. */
4900 pedwarn (token->location, OPT_Wpedantic,
4901 "ISO C++ forbids braced-groups within expressions");
4902
4903 /* And they're not allowed outside of a function-body; you
4904 cannot, for example, write:
4905
4906 int i = ({ int j = 3; j + 1; });
4907
4908 at class or namespace scope. */
4909 if (!parser->in_function_body
4910 || parser->in_template_argument_list_p)
4911 {
4912 error_at (token->location,
4913 "statement-expressions are not allowed outside "
4914 "functions nor in template-argument lists");
4915 cp_parser_skip_to_end_of_block_or_statement (parser);
4916 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4917 cp_lexer_consume_token (parser->lexer);
4918 return error_mark_node;
4919 }
4920 else
4921 return cp_parser_statement_expr (parser);
4922 }
4923 /* Otherwise it's a normal parenthesized expression. */
4924 {
4925 cp_expr expr;
4926 bool saved_greater_than_is_operator_p;
4927
4928 location_t open_paren_loc = token->location;
4929
4930 /* Consume the `('. */
4931 cp_lexer_consume_token (parser->lexer);
4932 /* Within a parenthesized expression, a `>' token is always
4933 the greater-than operator. */
4934 saved_greater_than_is_operator_p
4935 = parser->greater_than_is_operator_p;
4936 parser->greater_than_is_operator_p = true;
4937
4938 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4939 /* Left fold expression. */
4940 expr = NULL_TREE;
4941 else
4942 /* Parse the parenthesized expression. */
4943 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4944
4945 token = cp_lexer_peek_token (parser->lexer);
4946 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4947 {
4948 expr = cp_parser_fold_expression (parser, expr);
4949 if (expr != error_mark_node
4950 && cxx_dialect < cxx1z
4951 && !in_system_header_at (input_location))
4952 pedwarn (input_location, 0, "fold-expressions only available "
4953 "with -std=c++1z or -std=gnu++1z");
4954 }
4955 else
4956 /* Let the front end know that this expression was
4957 enclosed in parentheses. This matters in case, for
4958 example, the expression is of the form `A::B', since
4959 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4960 not. */
4961 expr = finish_parenthesized_expr (expr);
4962
4963 /* DR 705: Wrapping an unqualified name in parentheses
4964 suppresses arg-dependent lookup. We want to pass back
4965 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4966 (c++/37862), but none of the others. */
4967 if (*idk != CP_ID_KIND_QUALIFIED)
4968 *idk = CP_ID_KIND_NONE;
4969
4970 /* The `>' token might be the end of a template-id or
4971 template-parameter-list now. */
4972 parser->greater_than_is_operator_p
4973 = saved_greater_than_is_operator_p;
4974
4975 /* Consume the `)'. */
4976 token = cp_lexer_peek_token (parser->lexer);
4977 location_t close_paren_loc = token->location;
4978 expr.set_range (open_paren_loc, close_paren_loc);
4979 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4980 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4981 cp_parser_skip_to_end_of_statement (parser);
4982
4983 return expr;
4984 }
4985
4986 case CPP_OPEN_SQUARE:
4987 {
4988 if (c_dialect_objc ())
4989 {
4990 /* We might have an Objective-C++ message. */
4991 cp_parser_parse_tentatively (parser);
4992 tree msg = cp_parser_objc_message_expression (parser);
4993 /* If that works out, we're done ... */
4994 if (cp_parser_parse_definitely (parser))
4995 return msg;
4996 /* ... else, fall though to see if it's a lambda. */
4997 }
4998 cp_expr lam = cp_parser_lambda_expression (parser);
4999 /* Don't warn about a failed tentative parse. */
5000 if (cp_parser_error_occurred (parser))
5001 return error_mark_node;
5002 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5003 return lam;
5004 }
5005
5006 case CPP_OBJC_STRING:
5007 if (c_dialect_objc ())
5008 /* We have an Objective-C++ string literal. */
5009 return cp_parser_objc_expression (parser);
5010 cp_parser_error (parser, "expected primary-expression");
5011 return error_mark_node;
5012
5013 case CPP_KEYWORD:
5014 switch (token->keyword)
5015 {
5016 /* These two are the boolean literals. */
5017 case RID_TRUE:
5018 cp_lexer_consume_token (parser->lexer);
5019 return cp_expr (boolean_true_node, token->location);
5020 case RID_FALSE:
5021 cp_lexer_consume_token (parser->lexer);
5022 return cp_expr (boolean_false_node, token->location);
5023
5024 /* The `__null' literal. */
5025 case RID_NULL:
5026 cp_lexer_consume_token (parser->lexer);
5027 return cp_expr (null_node, token->location);
5028
5029 /* The `nullptr' literal. */
5030 case RID_NULLPTR:
5031 cp_lexer_consume_token (parser->lexer);
5032 return cp_expr (nullptr_node, token->location);
5033
5034 /* Recognize the `this' keyword. */
5035 case RID_THIS:
5036 cp_lexer_consume_token (parser->lexer);
5037 if (parser->local_variables_forbidden_p)
5038 {
5039 error_at (token->location,
5040 "%<this%> may not be used in this context");
5041 return error_mark_node;
5042 }
5043 /* Pointers cannot appear in constant-expressions. */
5044 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5045 return error_mark_node;
5046 return cp_expr (finish_this_expr (), token->location);
5047
5048 /* The `operator' keyword can be the beginning of an
5049 id-expression. */
5050 case RID_OPERATOR:
5051 goto id_expression;
5052
5053 case RID_FUNCTION_NAME:
5054 case RID_PRETTY_FUNCTION_NAME:
5055 case RID_C99_FUNCTION_NAME:
5056 {
5057 non_integral_constant name;
5058
5059 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5060 __func__ are the names of variables -- but they are
5061 treated specially. Therefore, they are handled here,
5062 rather than relying on the generic id-expression logic
5063 below. Grammatically, these names are id-expressions.
5064
5065 Consume the token. */
5066 token = cp_lexer_consume_token (parser->lexer);
5067
5068 switch (token->keyword)
5069 {
5070 case RID_FUNCTION_NAME:
5071 name = NIC_FUNC_NAME;
5072 break;
5073 case RID_PRETTY_FUNCTION_NAME:
5074 name = NIC_PRETTY_FUNC;
5075 break;
5076 case RID_C99_FUNCTION_NAME:
5077 name = NIC_C99_FUNC;
5078 break;
5079 default:
5080 gcc_unreachable ();
5081 }
5082
5083 if (cp_parser_non_integral_constant_expression (parser, name))
5084 return error_mark_node;
5085
5086 /* Look up the name. */
5087 return finish_fname (token->u.value);
5088 }
5089
5090 case RID_VA_ARG:
5091 {
5092 tree expression;
5093 tree type;
5094 source_location type_location;
5095 location_t start_loc
5096 = cp_lexer_peek_token (parser->lexer)->location;
5097 /* The `__builtin_va_arg' construct is used to handle
5098 `va_arg'. Consume the `__builtin_va_arg' token. */
5099 cp_lexer_consume_token (parser->lexer);
5100 /* Look for the opening `('. */
5101 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5102 /* Now, parse the assignment-expression. */
5103 expression = cp_parser_assignment_expression (parser);
5104 /* Look for the `,'. */
5105 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5106 type_location = cp_lexer_peek_token (parser->lexer)->location;
5107 /* Parse the type-id. */
5108 {
5109 type_id_in_expr_sentinel s (parser);
5110 type = cp_parser_type_id (parser);
5111 }
5112 /* Look for the closing `)'. */
5113 location_t finish_loc
5114 = cp_lexer_peek_token (parser->lexer)->location;
5115 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5116 /* Using `va_arg' in a constant-expression is not
5117 allowed. */
5118 if (cp_parser_non_integral_constant_expression (parser,
5119 NIC_VA_ARG))
5120 return error_mark_node;
5121 /* Construct a location of the form:
5122 __builtin_va_arg (v, int)
5123 ~~~~~~~~~~~~~~~~~~~~~^~~~
5124 with the caret at the type, ranging from the start of the
5125 "__builtin_va_arg" token to the close paren. */
5126 location_t combined_loc
5127 = make_location (type_location, start_loc, finish_loc);
5128 return build_x_va_arg (combined_loc, expression, type);
5129 }
5130
5131 case RID_OFFSETOF:
5132 return cp_parser_builtin_offsetof (parser);
5133
5134 case RID_HAS_NOTHROW_ASSIGN:
5135 case RID_HAS_NOTHROW_CONSTRUCTOR:
5136 case RID_HAS_NOTHROW_COPY:
5137 case RID_HAS_TRIVIAL_ASSIGN:
5138 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5139 case RID_HAS_TRIVIAL_COPY:
5140 case RID_HAS_TRIVIAL_DESTRUCTOR:
5141 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5142 case RID_HAS_VIRTUAL_DESTRUCTOR:
5143 case RID_IS_ABSTRACT:
5144 case RID_IS_AGGREGATE:
5145 case RID_IS_BASE_OF:
5146 case RID_IS_CLASS:
5147 case RID_IS_EMPTY:
5148 case RID_IS_ENUM:
5149 case RID_IS_FINAL:
5150 case RID_IS_LITERAL_TYPE:
5151 case RID_IS_POD:
5152 case RID_IS_POLYMORPHIC:
5153 case RID_IS_SAME_AS:
5154 case RID_IS_STD_LAYOUT:
5155 case RID_IS_TRIVIAL:
5156 case RID_IS_TRIVIALLY_ASSIGNABLE:
5157 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5158 case RID_IS_TRIVIALLY_COPYABLE:
5159 case RID_IS_UNION:
5160 case RID_IS_ASSIGNABLE:
5161 case RID_IS_CONSTRUCTIBLE:
5162 return cp_parser_trait_expr (parser, token->keyword);
5163
5164 // C++ concepts
5165 case RID_REQUIRES:
5166 return cp_parser_requires_expression (parser);
5167
5168 /* Objective-C++ expressions. */
5169 case RID_AT_ENCODE:
5170 case RID_AT_PROTOCOL:
5171 case RID_AT_SELECTOR:
5172 return cp_parser_objc_expression (parser);
5173
5174 case RID_TEMPLATE:
5175 if (parser->in_function_body
5176 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5177 == CPP_LESS))
5178 {
5179 error_at (token->location,
5180 "a template declaration cannot appear at block scope");
5181 cp_parser_skip_to_end_of_block_or_statement (parser);
5182 return error_mark_node;
5183 }
5184 /* FALLTHRU */
5185 default:
5186 cp_parser_error (parser, "expected primary-expression");
5187 return error_mark_node;
5188 }
5189
5190 /* An id-expression can start with either an identifier, a
5191 `::' as the beginning of a qualified-id, or the "operator"
5192 keyword. */
5193 case CPP_NAME:
5194 case CPP_SCOPE:
5195 case CPP_TEMPLATE_ID:
5196 case CPP_NESTED_NAME_SPECIFIER:
5197 {
5198 id_expression:
5199 cp_expr id_expression;
5200 cp_expr decl;
5201 const char *error_msg;
5202 bool template_p;
5203 bool done;
5204 cp_token *id_expr_token;
5205
5206 /* Parse the id-expression. */
5207 id_expression
5208 = cp_parser_id_expression (parser,
5209 /*template_keyword_p=*/false,
5210 /*check_dependency_p=*/true,
5211 &template_p,
5212 /*declarator_p=*/false,
5213 /*optional_p=*/false);
5214 if (id_expression == error_mark_node)
5215 return error_mark_node;
5216 id_expr_token = token;
5217 token = cp_lexer_peek_token (parser->lexer);
5218 done = (token->type != CPP_OPEN_SQUARE
5219 && token->type != CPP_OPEN_PAREN
5220 && token->type != CPP_DOT
5221 && token->type != CPP_DEREF
5222 && token->type != CPP_PLUS_PLUS
5223 && token->type != CPP_MINUS_MINUS);
5224 /* If we have a template-id, then no further lookup is
5225 required. If the template-id was for a template-class, we
5226 will sometimes have a TYPE_DECL at this point. */
5227 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5228 || TREE_CODE (id_expression) == TYPE_DECL)
5229 decl = id_expression;
5230 /* Look up the name. */
5231 else
5232 {
5233 tree ambiguous_decls;
5234
5235 /* If we already know that this lookup is ambiguous, then
5236 we've already issued an error message; there's no reason
5237 to check again. */
5238 if (id_expr_token->type == CPP_NAME
5239 && id_expr_token->error_reported)
5240 {
5241 cp_parser_simulate_error (parser);
5242 return error_mark_node;
5243 }
5244
5245 decl = cp_parser_lookup_name (parser, id_expression,
5246 none_type,
5247 template_p,
5248 /*is_namespace=*/false,
5249 /*check_dependency=*/true,
5250 &ambiguous_decls,
5251 id_expr_token->location);
5252 /* If the lookup was ambiguous, an error will already have
5253 been issued. */
5254 if (ambiguous_decls)
5255 return error_mark_node;
5256
5257 /* In Objective-C++, we may have an Objective-C 2.0
5258 dot-syntax for classes here. */
5259 if (c_dialect_objc ()
5260 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5261 && TREE_CODE (decl) == TYPE_DECL
5262 && objc_is_class_name (decl))
5263 {
5264 tree component;
5265 cp_lexer_consume_token (parser->lexer);
5266 component = cp_parser_identifier (parser);
5267 if (component == error_mark_node)
5268 return error_mark_node;
5269
5270 tree result = objc_build_class_component_ref (id_expression,
5271 component);
5272 /* Build a location of the form:
5273 expr.component
5274 ~~~~~^~~~~~~~~
5275 with caret at the start of the component name (at
5276 input_location), ranging from the start of the id_expression
5277 to the end of the component name. */
5278 location_t combined_loc
5279 = make_location (input_location, id_expression.get_start (),
5280 get_finish (input_location));
5281 protected_set_expr_location (result, combined_loc);
5282 return result;
5283 }
5284
5285 /* In Objective-C++, an instance variable (ivar) may be preferred
5286 to whatever cp_parser_lookup_name() found.
5287 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5288 rest of c-family, we have to do a little extra work to preserve
5289 any location information in cp_expr "decl". Given that
5290 objc_lookup_ivar is implemented in "c-family" and "objc", we
5291 have a trip through the pure "tree" type, rather than cp_expr.
5292 Naively copying it back to "decl" would implicitly give the
5293 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5294 store an EXPR_LOCATION. Hence we only update "decl" (and
5295 hence its location_t) if we get back a different tree node. */
5296 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5297 id_expression);
5298 if (decl_tree != decl.get_value ())
5299 decl = cp_expr (decl_tree);
5300
5301 /* If name lookup gives us a SCOPE_REF, then the
5302 qualifying scope was dependent. */
5303 if (TREE_CODE (decl) == SCOPE_REF)
5304 {
5305 /* At this point, we do not know if DECL is a valid
5306 integral constant expression. We assume that it is
5307 in fact such an expression, so that code like:
5308
5309 template <int N> struct A {
5310 int a[B<N>::i];
5311 };
5312
5313 is accepted. At template-instantiation time, we
5314 will check that B<N>::i is actually a constant. */
5315 return decl;
5316 }
5317 /* Check to see if DECL is a local variable in a context
5318 where that is forbidden. */
5319 if (parser->local_variables_forbidden_p
5320 && local_variable_p (decl))
5321 {
5322 /* It might be that we only found DECL because we are
5323 trying to be generous with pre-ISO scoping rules.
5324 For example, consider:
5325
5326 int i;
5327 void g() {
5328 for (int i = 0; i < 10; ++i) {}
5329 extern void f(int j = i);
5330 }
5331
5332 Here, name look up will originally find the out
5333 of scope `i'. We need to issue a warning message,
5334 but then use the global `i'. */
5335 decl = check_for_out_of_scope_variable (decl);
5336 if (local_variable_p (decl))
5337 {
5338 error_at (id_expr_token->location,
5339 "local variable %qD may not appear in this context",
5340 decl.get_value ());
5341 return error_mark_node;
5342 }
5343 }
5344 }
5345
5346 decl = (finish_id_expression
5347 (id_expression, decl, parser->scope,
5348 idk,
5349 parser->integral_constant_expression_p,
5350 parser->allow_non_integral_constant_expression_p,
5351 &parser->non_integral_constant_expression_p,
5352 template_p, done, address_p,
5353 template_arg_p,
5354 &error_msg,
5355 id_expression.get_location ()));
5356 if (error_msg)
5357 cp_parser_error (parser, error_msg);
5358 decl.set_location (id_expr_token->location);
5359 return decl;
5360 }
5361
5362 /* Anything else is an error. */
5363 default:
5364 cp_parser_error (parser, "expected primary-expression");
5365 return error_mark_node;
5366 }
5367 }
5368
5369 static inline cp_expr
5370 cp_parser_primary_expression (cp_parser *parser,
5371 bool address_p,
5372 bool cast_p,
5373 bool template_arg_p,
5374 cp_id_kind *idk)
5375 {
5376 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5377 /*decltype*/false, idk);
5378 }
5379
5380 /* Parse an id-expression.
5381
5382 id-expression:
5383 unqualified-id
5384 qualified-id
5385
5386 qualified-id:
5387 :: [opt] nested-name-specifier template [opt] unqualified-id
5388 :: identifier
5389 :: operator-function-id
5390 :: template-id
5391
5392 Return a representation of the unqualified portion of the
5393 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5394 a `::' or nested-name-specifier.
5395
5396 Often, if the id-expression was a qualified-id, the caller will
5397 want to make a SCOPE_REF to represent the qualified-id. This
5398 function does not do this in order to avoid wastefully creating
5399 SCOPE_REFs when they are not required.
5400
5401 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5402 `template' keyword.
5403
5404 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5405 uninstantiated templates.
5406
5407 If *TEMPLATE_P is non-NULL, it is set to true iff the
5408 `template' keyword is used to explicitly indicate that the entity
5409 named is a template.
5410
5411 If DECLARATOR_P is true, the id-expression is appearing as part of
5412 a declarator, rather than as part of an expression. */
5413
5414 static cp_expr
5415 cp_parser_id_expression (cp_parser *parser,
5416 bool template_keyword_p,
5417 bool check_dependency_p,
5418 bool *template_p,
5419 bool declarator_p,
5420 bool optional_p)
5421 {
5422 bool global_scope_p;
5423 bool nested_name_specifier_p;
5424
5425 /* Assume the `template' keyword was not used. */
5426 if (template_p)
5427 *template_p = template_keyword_p;
5428
5429 /* Look for the optional `::' operator. */
5430 global_scope_p
5431 = (!template_keyword_p
5432 && (cp_parser_global_scope_opt (parser,
5433 /*current_scope_valid_p=*/false)
5434 != NULL_TREE));
5435
5436 /* Look for the optional nested-name-specifier. */
5437 nested_name_specifier_p
5438 = (cp_parser_nested_name_specifier_opt (parser,
5439 /*typename_keyword_p=*/false,
5440 check_dependency_p,
5441 /*type_p=*/false,
5442 declarator_p,
5443 template_keyword_p)
5444 != NULL_TREE);
5445
5446 /* If there is a nested-name-specifier, then we are looking at
5447 the first qualified-id production. */
5448 if (nested_name_specifier_p)
5449 {
5450 tree saved_scope;
5451 tree saved_object_scope;
5452 tree saved_qualifying_scope;
5453 cp_expr unqualified_id;
5454 bool is_template;
5455
5456 /* See if the next token is the `template' keyword. */
5457 if (!template_p)
5458 template_p = &is_template;
5459 *template_p = cp_parser_optional_template_keyword (parser);
5460 /* Name lookup we do during the processing of the
5461 unqualified-id might obliterate SCOPE. */
5462 saved_scope = parser->scope;
5463 saved_object_scope = parser->object_scope;
5464 saved_qualifying_scope = parser->qualifying_scope;
5465 /* Process the final unqualified-id. */
5466 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5467 check_dependency_p,
5468 declarator_p,
5469 /*optional_p=*/false);
5470 /* Restore the SAVED_SCOPE for our caller. */
5471 parser->scope = saved_scope;
5472 parser->object_scope = saved_object_scope;
5473 parser->qualifying_scope = saved_qualifying_scope;
5474
5475 return unqualified_id;
5476 }
5477 /* Otherwise, if we are in global scope, then we are looking at one
5478 of the other qualified-id productions. */
5479 else if (global_scope_p)
5480 {
5481 cp_token *token;
5482 tree id;
5483
5484 /* Peek at the next token. */
5485 token = cp_lexer_peek_token (parser->lexer);
5486
5487 /* If it's an identifier, and the next token is not a "<", then
5488 we can avoid the template-id case. This is an optimization
5489 for this common case. */
5490 if (token->type == CPP_NAME
5491 && !cp_parser_nth_token_starts_template_argument_list_p
5492 (parser, 2))
5493 return cp_parser_identifier (parser);
5494
5495 cp_parser_parse_tentatively (parser);
5496 /* Try a template-id. */
5497 id = cp_parser_template_id (parser,
5498 /*template_keyword_p=*/false,
5499 /*check_dependency_p=*/true,
5500 none_type,
5501 declarator_p);
5502 /* If that worked, we're done. */
5503 if (cp_parser_parse_definitely (parser))
5504 return id;
5505
5506 /* Peek at the next token. (Changes in the token buffer may
5507 have invalidated the pointer obtained above.) */
5508 token = cp_lexer_peek_token (parser->lexer);
5509
5510 switch (token->type)
5511 {
5512 case CPP_NAME:
5513 return cp_parser_identifier (parser);
5514
5515 case CPP_KEYWORD:
5516 if (token->keyword == RID_OPERATOR)
5517 return cp_parser_operator_function_id (parser);
5518 /* Fall through. */
5519
5520 default:
5521 cp_parser_error (parser, "expected id-expression");
5522 return error_mark_node;
5523 }
5524 }
5525 else
5526 return cp_parser_unqualified_id (parser, template_keyword_p,
5527 /*check_dependency_p=*/true,
5528 declarator_p,
5529 optional_p);
5530 }
5531
5532 /* Parse an unqualified-id.
5533
5534 unqualified-id:
5535 identifier
5536 operator-function-id
5537 conversion-function-id
5538 ~ class-name
5539 template-id
5540
5541 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5542 keyword, in a construct like `A::template ...'.
5543
5544 Returns a representation of unqualified-id. For the `identifier'
5545 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5546 production a BIT_NOT_EXPR is returned; the operand of the
5547 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5548 other productions, see the documentation accompanying the
5549 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5550 names are looked up in uninstantiated templates. If DECLARATOR_P
5551 is true, the unqualified-id is appearing as part of a declarator,
5552 rather than as part of an expression. */
5553
5554 static cp_expr
5555 cp_parser_unqualified_id (cp_parser* parser,
5556 bool template_keyword_p,
5557 bool check_dependency_p,
5558 bool declarator_p,
5559 bool optional_p)
5560 {
5561 cp_token *token;
5562
5563 /* Peek at the next token. */
5564 token = cp_lexer_peek_token (parser->lexer);
5565
5566 switch ((int) token->type)
5567 {
5568 case CPP_NAME:
5569 {
5570 tree id;
5571
5572 /* We don't know yet whether or not this will be a
5573 template-id. */
5574 cp_parser_parse_tentatively (parser);
5575 /* Try a template-id. */
5576 id = cp_parser_template_id (parser, template_keyword_p,
5577 check_dependency_p,
5578 none_type,
5579 declarator_p);
5580 /* If it worked, we're done. */
5581 if (cp_parser_parse_definitely (parser))
5582 return id;
5583 /* Otherwise, it's an ordinary identifier. */
5584 return cp_parser_identifier (parser);
5585 }
5586
5587 case CPP_TEMPLATE_ID:
5588 return cp_parser_template_id (parser, template_keyword_p,
5589 check_dependency_p,
5590 none_type,
5591 declarator_p);
5592
5593 case CPP_COMPL:
5594 {
5595 tree type_decl;
5596 tree qualifying_scope;
5597 tree object_scope;
5598 tree scope;
5599 bool done;
5600
5601 /* Consume the `~' token. */
5602 cp_lexer_consume_token (parser->lexer);
5603 /* Parse the class-name. The standard, as written, seems to
5604 say that:
5605
5606 template <typename T> struct S { ~S (); };
5607 template <typename T> S<T>::~S() {}
5608
5609 is invalid, since `~' must be followed by a class-name, but
5610 `S<T>' is dependent, and so not known to be a class.
5611 That's not right; we need to look in uninstantiated
5612 templates. A further complication arises from:
5613
5614 template <typename T> void f(T t) {
5615 t.T::~T();
5616 }
5617
5618 Here, it is not possible to look up `T' in the scope of `T'
5619 itself. We must look in both the current scope, and the
5620 scope of the containing complete expression.
5621
5622 Yet another issue is:
5623
5624 struct S {
5625 int S;
5626 ~S();
5627 };
5628
5629 S::~S() {}
5630
5631 The standard does not seem to say that the `S' in `~S'
5632 should refer to the type `S' and not the data member
5633 `S::S'. */
5634
5635 /* DR 244 says that we look up the name after the "~" in the
5636 same scope as we looked up the qualifying name. That idea
5637 isn't fully worked out; it's more complicated than that. */
5638 scope = parser->scope;
5639 object_scope = parser->object_scope;
5640 qualifying_scope = parser->qualifying_scope;
5641
5642 /* Check for invalid scopes. */
5643 if (scope == error_mark_node)
5644 {
5645 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5646 cp_lexer_consume_token (parser->lexer);
5647 return error_mark_node;
5648 }
5649 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5650 {
5651 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5652 error_at (token->location,
5653 "scope %qT before %<~%> is not a class-name",
5654 scope);
5655 cp_parser_simulate_error (parser);
5656 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5657 cp_lexer_consume_token (parser->lexer);
5658 return error_mark_node;
5659 }
5660 gcc_assert (!scope || TYPE_P (scope));
5661
5662 /* If the name is of the form "X::~X" it's OK even if X is a
5663 typedef. */
5664 token = cp_lexer_peek_token (parser->lexer);
5665 if (scope
5666 && token->type == CPP_NAME
5667 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5668 != CPP_LESS)
5669 && (token->u.value == TYPE_IDENTIFIER (scope)
5670 || (CLASS_TYPE_P (scope)
5671 && constructor_name_p (token->u.value, scope))))
5672 {
5673 cp_lexer_consume_token (parser->lexer);
5674 return build_nt (BIT_NOT_EXPR, scope);
5675 }
5676
5677 /* ~auto means the destructor of whatever the object is. */
5678 if (cp_parser_is_keyword (token, RID_AUTO))
5679 {
5680 if (cxx_dialect < cxx14)
5681 pedwarn (input_location, 0,
5682 "%<~auto%> only available with "
5683 "-std=c++14 or -std=gnu++14");
5684 cp_lexer_consume_token (parser->lexer);
5685 return build_nt (BIT_NOT_EXPR, make_auto ());
5686 }
5687
5688 /* If there was an explicit qualification (S::~T), first look
5689 in the scope given by the qualification (i.e., S).
5690
5691 Note: in the calls to cp_parser_class_name below we pass
5692 typename_type so that lookup finds the injected-class-name
5693 rather than the constructor. */
5694 done = false;
5695 type_decl = NULL_TREE;
5696 if (scope)
5697 {
5698 cp_parser_parse_tentatively (parser);
5699 type_decl = cp_parser_class_name (parser,
5700 /*typename_keyword_p=*/false,
5701 /*template_keyword_p=*/false,
5702 typename_type,
5703 /*check_dependency=*/false,
5704 /*class_head_p=*/false,
5705 declarator_p);
5706 if (cp_parser_parse_definitely (parser))
5707 done = true;
5708 }
5709 /* In "N::S::~S", look in "N" as well. */
5710 if (!done && scope && qualifying_scope)
5711 {
5712 cp_parser_parse_tentatively (parser);
5713 parser->scope = qualifying_scope;
5714 parser->object_scope = NULL_TREE;
5715 parser->qualifying_scope = NULL_TREE;
5716 type_decl
5717 = cp_parser_class_name (parser,
5718 /*typename_keyword_p=*/false,
5719 /*template_keyword_p=*/false,
5720 typename_type,
5721 /*check_dependency=*/false,
5722 /*class_head_p=*/false,
5723 declarator_p);
5724 if (cp_parser_parse_definitely (parser))
5725 done = true;
5726 }
5727 /* In "p->S::~T", look in the scope given by "*p" as well. */
5728 else if (!done && object_scope)
5729 {
5730 cp_parser_parse_tentatively (parser);
5731 parser->scope = object_scope;
5732 parser->object_scope = NULL_TREE;
5733 parser->qualifying_scope = NULL_TREE;
5734 type_decl
5735 = cp_parser_class_name (parser,
5736 /*typename_keyword_p=*/false,
5737 /*template_keyword_p=*/false,
5738 typename_type,
5739 /*check_dependency=*/false,
5740 /*class_head_p=*/false,
5741 declarator_p);
5742 if (cp_parser_parse_definitely (parser))
5743 done = true;
5744 }
5745 /* Look in the surrounding context. */
5746 if (!done)
5747 {
5748 parser->scope = NULL_TREE;
5749 parser->object_scope = NULL_TREE;
5750 parser->qualifying_scope = NULL_TREE;
5751 if (processing_template_decl)
5752 cp_parser_parse_tentatively (parser);
5753 type_decl
5754 = cp_parser_class_name (parser,
5755 /*typename_keyword_p=*/false,
5756 /*template_keyword_p=*/false,
5757 typename_type,
5758 /*check_dependency=*/false,
5759 /*class_head_p=*/false,
5760 declarator_p);
5761 if (processing_template_decl
5762 && ! cp_parser_parse_definitely (parser))
5763 {
5764 /* We couldn't find a type with this name. If we're parsing
5765 tentatively, fail and try something else. */
5766 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5767 {
5768 cp_parser_simulate_error (parser);
5769 return error_mark_node;
5770 }
5771 /* Otherwise, accept it and check for a match at instantiation
5772 time. */
5773 type_decl = cp_parser_identifier (parser);
5774 if (type_decl != error_mark_node)
5775 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5776 return type_decl;
5777 }
5778 }
5779 /* If an error occurred, assume that the name of the
5780 destructor is the same as the name of the qualifying
5781 class. That allows us to keep parsing after running
5782 into ill-formed destructor names. */
5783 if (type_decl == error_mark_node && scope)
5784 return build_nt (BIT_NOT_EXPR, scope);
5785 else if (type_decl == error_mark_node)
5786 return error_mark_node;
5787
5788 /* Check that destructor name and scope match. */
5789 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5790 {
5791 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5792 error_at (token->location,
5793 "declaration of %<~%T%> as member of %qT",
5794 type_decl, scope);
5795 cp_parser_simulate_error (parser);
5796 return error_mark_node;
5797 }
5798
5799 /* [class.dtor]
5800
5801 A typedef-name that names a class shall not be used as the
5802 identifier in the declarator for a destructor declaration. */
5803 if (declarator_p
5804 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5805 && !DECL_SELF_REFERENCE_P (type_decl)
5806 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5807 error_at (token->location,
5808 "typedef-name %qD used as destructor declarator",
5809 type_decl);
5810
5811 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5812 }
5813
5814 case CPP_KEYWORD:
5815 if (token->keyword == RID_OPERATOR)
5816 {
5817 cp_expr id;
5818
5819 /* This could be a template-id, so we try that first. */
5820 cp_parser_parse_tentatively (parser);
5821 /* Try a template-id. */
5822 id = cp_parser_template_id (parser, template_keyword_p,
5823 /*check_dependency_p=*/true,
5824 none_type,
5825 declarator_p);
5826 /* If that worked, we're done. */
5827 if (cp_parser_parse_definitely (parser))
5828 return id;
5829 /* We still don't know whether we're looking at an
5830 operator-function-id or a conversion-function-id. */
5831 cp_parser_parse_tentatively (parser);
5832 /* Try an operator-function-id. */
5833 id = cp_parser_operator_function_id (parser);
5834 /* If that didn't work, try a conversion-function-id. */
5835 if (!cp_parser_parse_definitely (parser))
5836 id = cp_parser_conversion_function_id (parser);
5837 else if (UDLIT_OPER_P (id))
5838 {
5839 /* 17.6.3.3.5 */
5840 const char *name = UDLIT_OP_SUFFIX (id);
5841 if (name[0] != '_' && !in_system_header_at (input_location)
5842 && declarator_p)
5843 warning (OPT_Wliteral_suffix,
5844 "literal operator suffixes not preceded by %<_%>"
5845 " are reserved for future standardization");
5846 }
5847
5848 return id;
5849 }
5850 /* Fall through. */
5851
5852 default:
5853 if (optional_p)
5854 return NULL_TREE;
5855 cp_parser_error (parser, "expected unqualified-id");
5856 return error_mark_node;
5857 }
5858 }
5859
5860 /* Parse an (optional) nested-name-specifier.
5861
5862 nested-name-specifier: [C++98]
5863 class-or-namespace-name :: nested-name-specifier [opt]
5864 class-or-namespace-name :: template nested-name-specifier [opt]
5865
5866 nested-name-specifier: [C++0x]
5867 type-name ::
5868 namespace-name ::
5869 nested-name-specifier identifier ::
5870 nested-name-specifier template [opt] simple-template-id ::
5871
5872 PARSER->SCOPE should be set appropriately before this function is
5873 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5874 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5875 in name lookups.
5876
5877 Sets PARSER->SCOPE to the class (TYPE) or namespace
5878 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5879 it unchanged if there is no nested-name-specifier. Returns the new
5880 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5881
5882 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5883 part of a declaration and/or decl-specifier. */
5884
5885 static tree
5886 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5887 bool typename_keyword_p,
5888 bool check_dependency_p,
5889 bool type_p,
5890 bool is_declaration,
5891 bool template_keyword_p /* = false */)
5892 {
5893 bool success = false;
5894 cp_token_position start = 0;
5895 cp_token *token;
5896
5897 /* Remember where the nested-name-specifier starts. */
5898 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5899 {
5900 start = cp_lexer_token_position (parser->lexer, false);
5901 push_deferring_access_checks (dk_deferred);
5902 }
5903
5904 while (true)
5905 {
5906 tree new_scope;
5907 tree old_scope;
5908 tree saved_qualifying_scope;
5909
5910 /* Spot cases that cannot be the beginning of a
5911 nested-name-specifier. */
5912 token = cp_lexer_peek_token (parser->lexer);
5913
5914 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5915 the already parsed nested-name-specifier. */
5916 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5917 {
5918 /* Grab the nested-name-specifier and continue the loop. */
5919 cp_parser_pre_parsed_nested_name_specifier (parser);
5920 /* If we originally encountered this nested-name-specifier
5921 with IS_DECLARATION set to false, we will not have
5922 resolved TYPENAME_TYPEs, so we must do so here. */
5923 if (is_declaration
5924 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5925 {
5926 new_scope = resolve_typename_type (parser->scope,
5927 /*only_current_p=*/false);
5928 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5929 parser->scope = new_scope;
5930 }
5931 success = true;
5932 continue;
5933 }
5934
5935 /* Spot cases that cannot be the beginning of a
5936 nested-name-specifier. On the second and subsequent times
5937 through the loop, we look for the `template' keyword. */
5938 if (success && token->keyword == RID_TEMPLATE)
5939 ;
5940 /* A template-id can start a nested-name-specifier. */
5941 else if (token->type == CPP_TEMPLATE_ID)
5942 ;
5943 /* DR 743: decltype can be used in a nested-name-specifier. */
5944 else if (token_is_decltype (token))
5945 ;
5946 else
5947 {
5948 /* If the next token is not an identifier, then it is
5949 definitely not a type-name or namespace-name. */
5950 if (token->type != CPP_NAME)
5951 break;
5952 /* If the following token is neither a `<' (to begin a
5953 template-id), nor a `::', then we are not looking at a
5954 nested-name-specifier. */
5955 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5956
5957 if (token->type == CPP_COLON
5958 && parser->colon_corrects_to_scope_p
5959 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5960 {
5961 gcc_rich_location richloc (token->location);
5962 richloc.add_fixit_replace ("::");
5963 error_at_rich_loc (&richloc,
5964 "found %<:%> in nested-name-specifier, "
5965 "expected %<::%>");
5966 token->type = CPP_SCOPE;
5967 }
5968
5969 if (token->type != CPP_SCOPE
5970 && !cp_parser_nth_token_starts_template_argument_list_p
5971 (parser, 2))
5972 break;
5973 }
5974
5975 /* The nested-name-specifier is optional, so we parse
5976 tentatively. */
5977 cp_parser_parse_tentatively (parser);
5978
5979 /* Look for the optional `template' keyword, if this isn't the
5980 first time through the loop. */
5981 if (success)
5982 template_keyword_p = cp_parser_optional_template_keyword (parser);
5983
5984 /* Save the old scope since the name lookup we are about to do
5985 might destroy it. */
5986 old_scope = parser->scope;
5987 saved_qualifying_scope = parser->qualifying_scope;
5988 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5989 look up names in "X<T>::I" in order to determine that "Y" is
5990 a template. So, if we have a typename at this point, we make
5991 an effort to look through it. */
5992 if (is_declaration
5993 && !typename_keyword_p
5994 && parser->scope
5995 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5996 parser->scope = resolve_typename_type (parser->scope,
5997 /*only_current_p=*/false);
5998 /* Parse the qualifying entity. */
5999 new_scope
6000 = cp_parser_qualifying_entity (parser,
6001 typename_keyword_p,
6002 template_keyword_p,
6003 check_dependency_p,
6004 type_p,
6005 is_declaration);
6006 /* Look for the `::' token. */
6007 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6008
6009 /* If we found what we wanted, we keep going; otherwise, we're
6010 done. */
6011 if (!cp_parser_parse_definitely (parser))
6012 {
6013 bool error_p = false;
6014
6015 /* Restore the OLD_SCOPE since it was valid before the
6016 failed attempt at finding the last
6017 class-or-namespace-name. */
6018 parser->scope = old_scope;
6019 parser->qualifying_scope = saved_qualifying_scope;
6020
6021 /* If the next token is a decltype, and the one after that is a
6022 `::', then the decltype has failed to resolve to a class or
6023 enumeration type. Give this error even when parsing
6024 tentatively since it can't possibly be valid--and we're going
6025 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6026 won't get another chance.*/
6027 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6028 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6029 == CPP_SCOPE))
6030 {
6031 token = cp_lexer_consume_token (parser->lexer);
6032 error_at (token->location, "decltype evaluates to %qT, "
6033 "which is not a class or enumeration type",
6034 token->u.tree_check_value->value);
6035 parser->scope = error_mark_node;
6036 error_p = true;
6037 /* As below. */
6038 success = true;
6039 cp_lexer_consume_token (parser->lexer);
6040 }
6041
6042 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6043 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6044 {
6045 /* If we have a non-type template-id followed by ::, it can't
6046 possibly be valid. */
6047 token = cp_lexer_peek_token (parser->lexer);
6048 tree tid = token->u.tree_check_value->value;
6049 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6050 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6051 {
6052 tree tmpl = NULL_TREE;
6053 if (is_overloaded_fn (tid))
6054 {
6055 tree fns = get_fns (tid);
6056 if (OVL_SINGLE_P (fns))
6057 tmpl = OVL_FIRST (fns);
6058 error_at (token->location, "function template-id %qD "
6059 "in nested-name-specifier", tid);
6060 }
6061 else
6062 {
6063 /* Variable template. */
6064 tmpl = TREE_OPERAND (tid, 0);
6065 gcc_assert (variable_template_p (tmpl));
6066 error_at (token->location, "variable template-id %qD "
6067 "in nested-name-specifier", tid);
6068 }
6069 if (tmpl)
6070 inform (DECL_SOURCE_LOCATION (tmpl),
6071 "%qD declared here", tmpl);
6072
6073 parser->scope = error_mark_node;
6074 error_p = true;
6075 /* As below. */
6076 success = true;
6077 cp_lexer_consume_token (parser->lexer);
6078 cp_lexer_consume_token (parser->lexer);
6079 }
6080 }
6081
6082 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6083 break;
6084 /* If the next token is an identifier, and the one after
6085 that is a `::', then any valid interpretation would have
6086 found a class-or-namespace-name. */
6087 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6088 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6089 == CPP_SCOPE)
6090 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6091 != CPP_COMPL))
6092 {
6093 token = cp_lexer_consume_token (parser->lexer);
6094 if (!error_p)
6095 {
6096 if (!token->error_reported)
6097 {
6098 tree decl;
6099 tree ambiguous_decls;
6100
6101 decl = cp_parser_lookup_name (parser, token->u.value,
6102 none_type,
6103 /*is_template=*/false,
6104 /*is_namespace=*/false,
6105 /*check_dependency=*/true,
6106 &ambiguous_decls,
6107 token->location);
6108 if (TREE_CODE (decl) == TEMPLATE_DECL)
6109 error_at (token->location,
6110 "%qD used without template parameters",
6111 decl);
6112 else if (ambiguous_decls)
6113 {
6114 // cp_parser_lookup_name has the same diagnostic,
6115 // thus make sure to emit it at most once.
6116 if (cp_parser_uncommitted_to_tentative_parse_p
6117 (parser))
6118 {
6119 error_at (token->location,
6120 "reference to %qD is ambiguous",
6121 token->u.value);
6122 print_candidates (ambiguous_decls);
6123 }
6124 decl = error_mark_node;
6125 }
6126 else
6127 {
6128 if (cxx_dialect != cxx98)
6129 cp_parser_name_lookup_error
6130 (parser, token->u.value, decl, NLE_NOT_CXX98,
6131 token->location);
6132 else
6133 cp_parser_name_lookup_error
6134 (parser, token->u.value, decl, NLE_CXX98,
6135 token->location);
6136 }
6137 }
6138 parser->scope = error_mark_node;
6139 error_p = true;
6140 /* Treat this as a successful nested-name-specifier
6141 due to:
6142
6143 [basic.lookup.qual]
6144
6145 If the name found is not a class-name (clause
6146 _class_) or namespace-name (_namespace.def_), the
6147 program is ill-formed. */
6148 success = true;
6149 }
6150 cp_lexer_consume_token (parser->lexer);
6151 }
6152 break;
6153 }
6154 /* We've found one valid nested-name-specifier. */
6155 success = true;
6156 /* Name lookup always gives us a DECL. */
6157 if (TREE_CODE (new_scope) == TYPE_DECL)
6158 new_scope = TREE_TYPE (new_scope);
6159 /* Uses of "template" must be followed by actual templates. */
6160 if (template_keyword_p
6161 && !(CLASS_TYPE_P (new_scope)
6162 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6163 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6164 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6165 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6166 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6167 == TEMPLATE_ID_EXPR)))
6168 permerror (input_location, TYPE_P (new_scope)
6169 ? G_("%qT is not a template")
6170 : G_("%qD is not a template"),
6171 new_scope);
6172 /* If it is a class scope, try to complete it; we are about to
6173 be looking up names inside the class. */
6174 if (TYPE_P (new_scope)
6175 /* Since checking types for dependency can be expensive,
6176 avoid doing it if the type is already complete. */
6177 && !COMPLETE_TYPE_P (new_scope)
6178 /* Do not try to complete dependent types. */
6179 && !dependent_type_p (new_scope))
6180 {
6181 new_scope = complete_type (new_scope);
6182 /* If it is a typedef to current class, use the current
6183 class instead, as the typedef won't have any names inside
6184 it yet. */
6185 if (!COMPLETE_TYPE_P (new_scope)
6186 && currently_open_class (new_scope))
6187 new_scope = TYPE_MAIN_VARIANT (new_scope);
6188 }
6189 /* Make sure we look in the right scope the next time through
6190 the loop. */
6191 parser->scope = new_scope;
6192 }
6193
6194 /* If parsing tentatively, replace the sequence of tokens that makes
6195 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6196 token. That way, should we re-parse the token stream, we will
6197 not have to repeat the effort required to do the parse, nor will
6198 we issue duplicate error messages. */
6199 if (success && start)
6200 {
6201 cp_token *token;
6202
6203 token = cp_lexer_token_at (parser->lexer, start);
6204 /* Reset the contents of the START token. */
6205 token->type = CPP_NESTED_NAME_SPECIFIER;
6206 /* Retrieve any deferred checks. Do not pop this access checks yet
6207 so the memory will not be reclaimed during token replacing below. */
6208 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6209 token->u.tree_check_value->value = parser->scope;
6210 token->u.tree_check_value->checks = get_deferred_access_checks ();
6211 token->u.tree_check_value->qualifying_scope =
6212 parser->qualifying_scope;
6213 token->keyword = RID_MAX;
6214
6215 /* Purge all subsequent tokens. */
6216 cp_lexer_purge_tokens_after (parser->lexer, start);
6217 }
6218
6219 if (start)
6220 pop_to_parent_deferring_access_checks ();
6221
6222 return success ? parser->scope : NULL_TREE;
6223 }
6224
6225 /* Parse a nested-name-specifier. See
6226 cp_parser_nested_name_specifier_opt for details. This function
6227 behaves identically, except that it will an issue an error if no
6228 nested-name-specifier is present. */
6229
6230 static tree
6231 cp_parser_nested_name_specifier (cp_parser *parser,
6232 bool typename_keyword_p,
6233 bool check_dependency_p,
6234 bool type_p,
6235 bool is_declaration)
6236 {
6237 tree scope;
6238
6239 /* Look for the nested-name-specifier. */
6240 scope = cp_parser_nested_name_specifier_opt (parser,
6241 typename_keyword_p,
6242 check_dependency_p,
6243 type_p,
6244 is_declaration);
6245 /* If it was not present, issue an error message. */
6246 if (!scope)
6247 {
6248 cp_parser_error (parser, "expected nested-name-specifier");
6249 parser->scope = NULL_TREE;
6250 }
6251
6252 return scope;
6253 }
6254
6255 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6256 this is either a class-name or a namespace-name (which corresponds
6257 to the class-or-namespace-name production in the grammar). For
6258 C++0x, it can also be a type-name that refers to an enumeration
6259 type or a simple-template-id.
6260
6261 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6262 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6263 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6264 TYPE_P is TRUE iff the next name should be taken as a class-name,
6265 even the same name is declared to be another entity in the same
6266 scope.
6267
6268 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6269 specified by the class-or-namespace-name. If neither is found the
6270 ERROR_MARK_NODE is returned. */
6271
6272 static tree
6273 cp_parser_qualifying_entity (cp_parser *parser,
6274 bool typename_keyword_p,
6275 bool template_keyword_p,
6276 bool check_dependency_p,
6277 bool type_p,
6278 bool is_declaration)
6279 {
6280 tree saved_scope;
6281 tree saved_qualifying_scope;
6282 tree saved_object_scope;
6283 tree scope;
6284 bool only_class_p;
6285 bool successful_parse_p;
6286
6287 /* DR 743: decltype can appear in a nested-name-specifier. */
6288 if (cp_lexer_next_token_is_decltype (parser->lexer))
6289 {
6290 scope = cp_parser_decltype (parser);
6291 if (TREE_CODE (scope) != ENUMERAL_TYPE
6292 && !MAYBE_CLASS_TYPE_P (scope))
6293 {
6294 cp_parser_simulate_error (parser);
6295 return error_mark_node;
6296 }
6297 if (TYPE_NAME (scope))
6298 scope = TYPE_NAME (scope);
6299 return scope;
6300 }
6301
6302 /* Before we try to parse the class-name, we must save away the
6303 current PARSER->SCOPE since cp_parser_class_name will destroy
6304 it. */
6305 saved_scope = parser->scope;
6306 saved_qualifying_scope = parser->qualifying_scope;
6307 saved_object_scope = parser->object_scope;
6308 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6309 there is no need to look for a namespace-name. */
6310 only_class_p = template_keyword_p
6311 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6312 if (!only_class_p)
6313 cp_parser_parse_tentatively (parser);
6314 scope = cp_parser_class_name (parser,
6315 typename_keyword_p,
6316 template_keyword_p,
6317 type_p ? class_type : none_type,
6318 check_dependency_p,
6319 /*class_head_p=*/false,
6320 is_declaration,
6321 /*enum_ok=*/cxx_dialect > cxx98);
6322 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6323 /* If that didn't work, try for a namespace-name. */
6324 if (!only_class_p && !successful_parse_p)
6325 {
6326 /* Restore the saved scope. */
6327 parser->scope = saved_scope;
6328 parser->qualifying_scope = saved_qualifying_scope;
6329 parser->object_scope = saved_object_scope;
6330 /* If we are not looking at an identifier followed by the scope
6331 resolution operator, then this is not part of a
6332 nested-name-specifier. (Note that this function is only used
6333 to parse the components of a nested-name-specifier.) */
6334 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6335 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6336 return error_mark_node;
6337 scope = cp_parser_namespace_name (parser);
6338 }
6339
6340 return scope;
6341 }
6342
6343 /* Return true if we are looking at a compound-literal, false otherwise. */
6344
6345 static bool
6346 cp_parser_compound_literal_p (cp_parser *parser)
6347 {
6348 /* Consume the `('. */
6349 cp_lexer_consume_token (parser->lexer);
6350
6351 cp_lexer_save_tokens (parser->lexer);
6352
6353 /* Skip tokens until the next token is a closing parenthesis.
6354 If we find the closing `)', and the next token is a `{', then
6355 we are looking at a compound-literal. */
6356 bool compound_literal_p
6357 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6358 /*consume_paren=*/true)
6359 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6360
6361 /* Roll back the tokens we skipped. */
6362 cp_lexer_rollback_tokens (parser->lexer);
6363
6364 return compound_literal_p;
6365 }
6366
6367 /* Parse a postfix-expression.
6368
6369 postfix-expression:
6370 primary-expression
6371 postfix-expression [ expression ]
6372 postfix-expression ( expression-list [opt] )
6373 simple-type-specifier ( expression-list [opt] )
6374 typename :: [opt] nested-name-specifier identifier
6375 ( expression-list [opt] )
6376 typename :: [opt] nested-name-specifier template [opt] template-id
6377 ( expression-list [opt] )
6378 postfix-expression . template [opt] id-expression
6379 postfix-expression -> template [opt] id-expression
6380 postfix-expression . pseudo-destructor-name
6381 postfix-expression -> pseudo-destructor-name
6382 postfix-expression ++
6383 postfix-expression --
6384 dynamic_cast < type-id > ( expression )
6385 static_cast < type-id > ( expression )
6386 reinterpret_cast < type-id > ( expression )
6387 const_cast < type-id > ( expression )
6388 typeid ( expression )
6389 typeid ( type-id )
6390
6391 GNU Extension:
6392
6393 postfix-expression:
6394 ( type-id ) { initializer-list , [opt] }
6395
6396 This extension is a GNU version of the C99 compound-literal
6397 construct. (The C99 grammar uses `type-name' instead of `type-id',
6398 but they are essentially the same concept.)
6399
6400 If ADDRESS_P is true, the postfix expression is the operand of the
6401 `&' operator. CAST_P is true if this expression is the target of a
6402 cast.
6403
6404 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6405 class member access expressions [expr.ref].
6406
6407 Returns a representation of the expression. */
6408
6409 static cp_expr
6410 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6411 bool member_access_only_p, bool decltype_p,
6412 cp_id_kind * pidk_return)
6413 {
6414 cp_token *token;
6415 location_t loc;
6416 enum rid keyword;
6417 cp_id_kind idk = CP_ID_KIND_NONE;
6418 cp_expr postfix_expression = NULL_TREE;
6419 bool is_member_access = false;
6420 int saved_in_statement = -1;
6421
6422 /* Peek at the next token. */
6423 token = cp_lexer_peek_token (parser->lexer);
6424 loc = token->location;
6425 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6426
6427 /* Some of the productions are determined by keywords. */
6428 keyword = token->keyword;
6429 switch (keyword)
6430 {
6431 case RID_DYNCAST:
6432 case RID_STATCAST:
6433 case RID_REINTCAST:
6434 case RID_CONSTCAST:
6435 {
6436 tree type;
6437 cp_expr expression;
6438 const char *saved_message;
6439 bool saved_in_type_id_in_expr_p;
6440
6441 /* All of these can be handled in the same way from the point
6442 of view of parsing. Begin by consuming the token
6443 identifying the cast. */
6444 cp_lexer_consume_token (parser->lexer);
6445
6446 /* New types cannot be defined in the cast. */
6447 saved_message = parser->type_definition_forbidden_message;
6448 parser->type_definition_forbidden_message
6449 = G_("types may not be defined in casts");
6450
6451 /* Look for the opening `<'. */
6452 cp_parser_require (parser, CPP_LESS, RT_LESS);
6453 /* Parse the type to which we are casting. */
6454 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6455 parser->in_type_id_in_expr_p = true;
6456 type = cp_parser_type_id (parser);
6457 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6458 /* Look for the closing `>'. */
6459 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6460 /* Restore the old message. */
6461 parser->type_definition_forbidden_message = saved_message;
6462
6463 bool saved_greater_than_is_operator_p
6464 = parser->greater_than_is_operator_p;
6465 parser->greater_than_is_operator_p = true;
6466
6467 /* And the expression which is being cast. */
6468 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6469 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6470 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6471 RT_CLOSE_PAREN);
6472 location_t end_loc = close_paren ?
6473 close_paren->location : UNKNOWN_LOCATION;
6474
6475 parser->greater_than_is_operator_p
6476 = saved_greater_than_is_operator_p;
6477
6478 /* Only type conversions to integral or enumeration types
6479 can be used in constant-expressions. */
6480 if (!cast_valid_in_integral_constant_expression_p (type)
6481 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6482 {
6483 postfix_expression = error_mark_node;
6484 break;
6485 }
6486
6487 switch (keyword)
6488 {
6489 case RID_DYNCAST:
6490 postfix_expression
6491 = build_dynamic_cast (type, expression, tf_warning_or_error);
6492 break;
6493 case RID_STATCAST:
6494 postfix_expression
6495 = build_static_cast (type, expression, tf_warning_or_error);
6496 break;
6497 case RID_REINTCAST:
6498 postfix_expression
6499 = build_reinterpret_cast (type, expression,
6500 tf_warning_or_error);
6501 break;
6502 case RID_CONSTCAST:
6503 postfix_expression
6504 = build_const_cast (type, expression, tf_warning_or_error);
6505 break;
6506 default:
6507 gcc_unreachable ();
6508 }
6509
6510 /* Construct a location e.g. :
6511 reinterpret_cast <int *> (expr)
6512 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6513 ranging from the start of the "*_cast" token to the final closing
6514 paren, with the caret at the start. */
6515 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6516 postfix_expression.set_location (cp_cast_loc);
6517 }
6518 break;
6519
6520 case RID_TYPEID:
6521 {
6522 tree type;
6523 const char *saved_message;
6524 bool saved_in_type_id_in_expr_p;
6525
6526 /* Consume the `typeid' token. */
6527 cp_lexer_consume_token (parser->lexer);
6528 /* Look for the `(' token. */
6529 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6530 /* Types cannot be defined in a `typeid' expression. */
6531 saved_message = parser->type_definition_forbidden_message;
6532 parser->type_definition_forbidden_message
6533 = G_("types may not be defined in a %<typeid%> expression");
6534 /* We can't be sure yet whether we're looking at a type-id or an
6535 expression. */
6536 cp_parser_parse_tentatively (parser);
6537 /* Try a type-id first. */
6538 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6539 parser->in_type_id_in_expr_p = true;
6540 type = cp_parser_type_id (parser);
6541 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6542 /* Look for the `)' token. Otherwise, we can't be sure that
6543 we're not looking at an expression: consider `typeid (int
6544 (3))', for example. */
6545 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6546 RT_CLOSE_PAREN);
6547 /* If all went well, simply lookup the type-id. */
6548 if (cp_parser_parse_definitely (parser))
6549 postfix_expression = get_typeid (type, tf_warning_or_error);
6550 /* Otherwise, fall back to the expression variant. */
6551 else
6552 {
6553 tree expression;
6554
6555 /* Look for an expression. */
6556 expression = cp_parser_expression (parser, & idk);
6557 /* Compute its typeid. */
6558 postfix_expression = build_typeid (expression, tf_warning_or_error);
6559 /* Look for the `)' token. */
6560 close_paren
6561 = cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6562 }
6563 /* Restore the saved message. */
6564 parser->type_definition_forbidden_message = saved_message;
6565 /* `typeid' may not appear in an integral constant expression. */
6566 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6567 postfix_expression = error_mark_node;
6568
6569 /* Construct a location e.g. :
6570 typeid (expr)
6571 ^~~~~~~~~~~~~
6572 ranging from the start of the "typeid" token to the final closing
6573 paren, with the caret at the start. */
6574 if (close_paren)
6575 {
6576 location_t typeid_loc
6577 = make_location (start_loc, start_loc, close_paren->location);
6578 postfix_expression.set_location (typeid_loc);
6579 }
6580 }
6581 break;
6582
6583 case RID_TYPENAME:
6584 {
6585 tree type;
6586 /* The syntax permitted here is the same permitted for an
6587 elaborated-type-specifier. */
6588 ++parser->prevent_constrained_type_specifiers;
6589 type = cp_parser_elaborated_type_specifier (parser,
6590 /*is_friend=*/false,
6591 /*is_declaration=*/false);
6592 --parser->prevent_constrained_type_specifiers;
6593 postfix_expression = cp_parser_functional_cast (parser, type);
6594 }
6595 break;
6596
6597 case RID_CILK_SPAWN:
6598 {
6599 location_t cilk_spawn_loc
6600 = cp_lexer_peek_token (parser->lexer)->location;
6601 cp_lexer_consume_token (parser->lexer);
6602 token = cp_lexer_peek_token (parser->lexer);
6603 if (token->type == CPP_SEMICOLON)
6604 {
6605 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6606 "an expression");
6607 postfix_expression = error_mark_node;
6608 break;
6609 }
6610 else if (!current_function_decl)
6611 {
6612 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6613 "inside a function");
6614 postfix_expression = error_mark_node;
6615 break;
6616 }
6617 else
6618 {
6619 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6620 saved_in_statement = parser->in_statement;
6621 parser->in_statement |= IN_CILK_SPAWN;
6622 }
6623 cfun->calls_cilk_spawn = 1;
6624 postfix_expression =
6625 cp_parser_postfix_expression (parser, false, false,
6626 false, false, &idk);
6627 if (!flag_cilkplus)
6628 {
6629 error_at (token->location, "-fcilkplus must be enabled to use"
6630 " %<_Cilk_spawn%>");
6631 cfun->calls_cilk_spawn = 0;
6632 }
6633 else if (saved_in_statement & IN_CILK_SPAWN)
6634 {
6635 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6636 "are not permitted");
6637 postfix_expression = error_mark_node;
6638 cfun->calls_cilk_spawn = 0;
6639 }
6640 else
6641 {
6642 location_t loc = postfix_expression.get_location ();
6643 postfix_expression = build_cilk_spawn (token->location,
6644 postfix_expression);
6645 /* Build a location of the form:
6646 _Cilk_spawn expr
6647 ~~~~~~~~~~~~^~~~
6648 with caret at the expr, ranging from the start of the
6649 _Cilk_spawn token to the end of the expression. */
6650 location_t combined_loc =
6651 make_location (loc, cilk_spawn_loc, get_finish (loc));
6652 postfix_expression.set_location (combined_loc);
6653 if (postfix_expression != error_mark_node)
6654 SET_EXPR_LOCATION (postfix_expression, input_location);
6655 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6656 }
6657 break;
6658 }
6659
6660 case RID_ADDRESSOF:
6661 case RID_BUILTIN_SHUFFLE:
6662 case RID_BUILTIN_LAUNDER:
6663 {
6664 vec<tree, va_gc> *vec;
6665 unsigned int i;
6666 tree p;
6667
6668 cp_lexer_consume_token (parser->lexer);
6669 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6670 /*cast_p=*/false, /*allow_expansion_p=*/true,
6671 /*non_constant_p=*/NULL);
6672 if (vec == NULL)
6673 {
6674 postfix_expression = error_mark_node;
6675 break;
6676 }
6677
6678 FOR_EACH_VEC_ELT (*vec, i, p)
6679 mark_exp_read (p);
6680
6681 switch (keyword)
6682 {
6683 case RID_ADDRESSOF:
6684 if (vec->length () == 1)
6685 postfix_expression
6686 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6687 else
6688 {
6689 error_at (loc, "wrong number of arguments to "
6690 "%<__builtin_addressof%>");
6691 postfix_expression = error_mark_node;
6692 }
6693 break;
6694
6695 case RID_BUILTIN_LAUNDER:
6696 if (vec->length () == 1)
6697 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6698 tf_warning_or_error);
6699 else
6700 {
6701 error_at (loc, "wrong number of arguments to "
6702 "%<__builtin_launder%>");
6703 postfix_expression = error_mark_node;
6704 }
6705 break;
6706
6707 case RID_BUILTIN_SHUFFLE:
6708 if (vec->length () == 2)
6709 postfix_expression
6710 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6711 (*vec)[1], tf_warning_or_error);
6712 else if (vec->length () == 3)
6713 postfix_expression
6714 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6715 (*vec)[2], tf_warning_or_error);
6716 else
6717 {
6718 error_at (loc, "wrong number of arguments to "
6719 "%<__builtin_shuffle%>");
6720 postfix_expression = error_mark_node;
6721 }
6722 break;
6723
6724 default:
6725 gcc_unreachable ();
6726 }
6727 break;
6728 }
6729
6730 default:
6731 {
6732 tree type;
6733
6734 /* If the next thing is a simple-type-specifier, we may be
6735 looking at a functional cast. We could also be looking at
6736 an id-expression. So, we try the functional cast, and if
6737 that doesn't work we fall back to the primary-expression. */
6738 cp_parser_parse_tentatively (parser);
6739 /* Look for the simple-type-specifier. */
6740 ++parser->prevent_constrained_type_specifiers;
6741 type = cp_parser_simple_type_specifier (parser,
6742 /*decl_specs=*/NULL,
6743 CP_PARSER_FLAGS_NONE);
6744 --parser->prevent_constrained_type_specifiers;
6745 /* Parse the cast itself. */
6746 if (!cp_parser_error_occurred (parser))
6747 postfix_expression
6748 = cp_parser_functional_cast (parser, type);
6749 /* If that worked, we're done. */
6750 if (cp_parser_parse_definitely (parser))
6751 break;
6752
6753 /* If the functional-cast didn't work out, try a
6754 compound-literal. */
6755 if (cp_parser_allow_gnu_extensions_p (parser)
6756 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6757 {
6758 cp_expr initializer = NULL_TREE;
6759
6760 cp_parser_parse_tentatively (parser);
6761
6762 /* Avoid calling cp_parser_type_id pointlessly, see comment
6763 in cp_parser_cast_expression about c++/29234. */
6764 if (!cp_parser_compound_literal_p (parser))
6765 cp_parser_simulate_error (parser);
6766 else
6767 {
6768 /* Parse the type. */
6769 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6770 parser->in_type_id_in_expr_p = true;
6771 type = cp_parser_type_id (parser);
6772 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6773 /* Look for the `)'. */
6774 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6775 }
6776
6777 /* If things aren't going well, there's no need to
6778 keep going. */
6779 if (!cp_parser_error_occurred (parser))
6780 {
6781 bool non_constant_p;
6782 /* Parse the brace-enclosed initializer list. */
6783 initializer = cp_parser_braced_list (parser,
6784 &non_constant_p);
6785 }
6786 /* If that worked, we're definitely looking at a
6787 compound-literal expression. */
6788 if (cp_parser_parse_definitely (parser))
6789 {
6790 /* Warn the user that a compound literal is not
6791 allowed in standard C++. */
6792 pedwarn (input_location, OPT_Wpedantic,
6793 "ISO C++ forbids compound-literals");
6794 /* For simplicity, we disallow compound literals in
6795 constant-expressions. We could
6796 allow compound literals of integer type, whose
6797 initializer was a constant, in constant
6798 expressions. Permitting that usage, as a further
6799 extension, would not change the meaning of any
6800 currently accepted programs. (Of course, as
6801 compound literals are not part of ISO C++, the
6802 standard has nothing to say.) */
6803 if (cp_parser_non_integral_constant_expression (parser,
6804 NIC_NCC))
6805 {
6806 postfix_expression = error_mark_node;
6807 break;
6808 }
6809 /* Form the representation of the compound-literal. */
6810 postfix_expression
6811 = finish_compound_literal (type, initializer,
6812 tf_warning_or_error, fcl_c99);
6813 postfix_expression.set_location (initializer.get_location ());
6814 break;
6815 }
6816 }
6817
6818 /* It must be a primary-expression. */
6819 postfix_expression
6820 = cp_parser_primary_expression (parser, address_p, cast_p,
6821 /*template_arg_p=*/false,
6822 decltype_p,
6823 &idk);
6824 }
6825 break;
6826 }
6827
6828 /* Note that we don't need to worry about calling build_cplus_new on a
6829 class-valued CALL_EXPR in decltype when it isn't the end of the
6830 postfix-expression; unary_complex_lvalue will take care of that for
6831 all these cases. */
6832
6833 /* Keep looping until the postfix-expression is complete. */
6834 while (true)
6835 {
6836 if (idk == CP_ID_KIND_UNQUALIFIED
6837 && identifier_p (postfix_expression)
6838 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6839 /* It is not a Koenig lookup function call. */
6840 postfix_expression
6841 = unqualified_name_lookup_error (postfix_expression);
6842
6843 /* Peek at the next token. */
6844 token = cp_lexer_peek_token (parser->lexer);
6845
6846 switch (token->type)
6847 {
6848 case CPP_OPEN_SQUARE:
6849 if (cp_next_tokens_can_be_std_attribute_p (parser))
6850 {
6851 cp_parser_error (parser,
6852 "two consecutive %<[%> shall "
6853 "only introduce an attribute");
6854 return error_mark_node;
6855 }
6856 postfix_expression
6857 = cp_parser_postfix_open_square_expression (parser,
6858 postfix_expression,
6859 false,
6860 decltype_p);
6861 postfix_expression.set_range (start_loc,
6862 postfix_expression.get_location ());
6863
6864 idk = CP_ID_KIND_NONE;
6865 is_member_access = false;
6866 break;
6867
6868 case CPP_OPEN_PAREN:
6869 /* postfix-expression ( expression-list [opt] ) */
6870 {
6871 bool koenig_p;
6872 bool is_builtin_constant_p;
6873 bool saved_integral_constant_expression_p = false;
6874 bool saved_non_integral_constant_expression_p = false;
6875 tsubst_flags_t complain = complain_flags (decltype_p);
6876 vec<tree, va_gc> *args;
6877 location_t close_paren_loc = UNKNOWN_LOCATION;
6878
6879 is_member_access = false;
6880
6881 is_builtin_constant_p
6882 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6883 if (is_builtin_constant_p)
6884 {
6885 /* The whole point of __builtin_constant_p is to allow
6886 non-constant expressions to appear as arguments. */
6887 saved_integral_constant_expression_p
6888 = parser->integral_constant_expression_p;
6889 saved_non_integral_constant_expression_p
6890 = parser->non_integral_constant_expression_p;
6891 parser->integral_constant_expression_p = false;
6892 }
6893 args = (cp_parser_parenthesized_expression_list
6894 (parser, non_attr,
6895 /*cast_p=*/false, /*allow_expansion_p=*/true,
6896 /*non_constant_p=*/NULL,
6897 /*close_paren_loc=*/&close_paren_loc));
6898 if (is_builtin_constant_p)
6899 {
6900 parser->integral_constant_expression_p
6901 = saved_integral_constant_expression_p;
6902 parser->non_integral_constant_expression_p
6903 = saved_non_integral_constant_expression_p;
6904 }
6905
6906 if (args == NULL)
6907 {
6908 postfix_expression = error_mark_node;
6909 break;
6910 }
6911
6912 /* Function calls are not permitted in
6913 constant-expressions. */
6914 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6915 && cp_parser_non_integral_constant_expression (parser,
6916 NIC_FUNC_CALL))
6917 {
6918 postfix_expression = error_mark_node;
6919 release_tree_vector (args);
6920 break;
6921 }
6922
6923 koenig_p = false;
6924 if (idk == CP_ID_KIND_UNQUALIFIED
6925 || idk == CP_ID_KIND_TEMPLATE_ID)
6926 {
6927 if (identifier_p (postfix_expression))
6928 {
6929 if (!args->is_empty ())
6930 {
6931 koenig_p = true;
6932 if (!any_type_dependent_arguments_p (args))
6933 postfix_expression
6934 = perform_koenig_lookup (postfix_expression, args,
6935 complain);
6936 }
6937 else
6938 postfix_expression
6939 = unqualified_fn_lookup_error (postfix_expression);
6940 }
6941 /* We do not perform argument-dependent lookup if
6942 normal lookup finds a non-function, in accordance
6943 with the expected resolution of DR 218. */
6944 else if (!args->is_empty ()
6945 && is_overloaded_fn (postfix_expression))
6946 {
6947 tree fn = get_first_fn (postfix_expression);
6948 fn = STRIP_TEMPLATE (fn);
6949
6950 /* Do not do argument dependent lookup if regular
6951 lookup finds a member function or a block-scope
6952 function declaration. [basic.lookup.argdep]/3 */
6953 if (!DECL_FUNCTION_MEMBER_P (fn)
6954 && !DECL_LOCAL_FUNCTION_P (fn))
6955 {
6956 koenig_p = true;
6957 if (!any_type_dependent_arguments_p (args))
6958 postfix_expression
6959 = perform_koenig_lookup (postfix_expression, args,
6960 complain);
6961 }
6962 }
6963 }
6964
6965 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6966 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6967 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6968 && vec_safe_length (args) == 3)
6969 {
6970 tree arg0 = (*args)[0];
6971 tree arg1 = (*args)[1];
6972 tree arg2 = (*args)[2];
6973 int literal_mask = ((!!integer_zerop (arg1) << 1)
6974 | (!!integer_zerop (arg2) << 2));
6975 if (TREE_CODE (arg2) == CONST_DECL)
6976 arg2 = DECL_INITIAL (arg2);
6977 warn_for_memset (input_location, arg0, arg2, literal_mask);
6978 }
6979
6980 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6981 {
6982 tree instance = TREE_OPERAND (postfix_expression, 0);
6983 tree fn = TREE_OPERAND (postfix_expression, 1);
6984
6985 if (processing_template_decl
6986 && (type_dependent_object_expression_p (instance)
6987 || (!BASELINK_P (fn)
6988 && TREE_CODE (fn) != FIELD_DECL)
6989 || type_dependent_expression_p (fn)
6990 || any_type_dependent_arguments_p (args)))
6991 {
6992 maybe_generic_this_capture (instance, fn);
6993 postfix_expression
6994 = build_min_nt_call_vec (postfix_expression, args);
6995 release_tree_vector (args);
6996 break;
6997 }
6998
6999 if (BASELINK_P (fn))
7000 {
7001 postfix_expression
7002 = (build_new_method_call
7003 (instance, fn, &args, NULL_TREE,
7004 (idk == CP_ID_KIND_QUALIFIED
7005 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7006 : LOOKUP_NORMAL),
7007 /*fn_p=*/NULL,
7008 complain));
7009 }
7010 else
7011 postfix_expression
7012 = finish_call_expr (postfix_expression, &args,
7013 /*disallow_virtual=*/false,
7014 /*koenig_p=*/false,
7015 complain);
7016 }
7017 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7018 || TREE_CODE (postfix_expression) == MEMBER_REF
7019 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7020 postfix_expression = (build_offset_ref_call_from_tree
7021 (postfix_expression, &args,
7022 complain));
7023 else if (idk == CP_ID_KIND_QUALIFIED)
7024 /* A call to a static class member, or a namespace-scope
7025 function. */
7026 postfix_expression
7027 = finish_call_expr (postfix_expression, &args,
7028 /*disallow_virtual=*/true,
7029 koenig_p,
7030 complain);
7031 else
7032 /* All other function calls. */
7033 postfix_expression
7034 = finish_call_expr (postfix_expression, &args,
7035 /*disallow_virtual=*/false,
7036 koenig_p,
7037 complain);
7038
7039 if (close_paren_loc != UNKNOWN_LOCATION)
7040 {
7041 location_t combined_loc = make_location (token->location,
7042 start_loc,
7043 close_paren_loc);
7044 postfix_expression.set_location (combined_loc);
7045 }
7046
7047 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7048 idk = CP_ID_KIND_NONE;
7049
7050 release_tree_vector (args);
7051 }
7052 break;
7053
7054 case CPP_DOT:
7055 case CPP_DEREF:
7056 /* postfix-expression . template [opt] id-expression
7057 postfix-expression . pseudo-destructor-name
7058 postfix-expression -> template [opt] id-expression
7059 postfix-expression -> pseudo-destructor-name */
7060
7061 /* Consume the `.' or `->' operator. */
7062 cp_lexer_consume_token (parser->lexer);
7063
7064 postfix_expression
7065 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7066 postfix_expression,
7067 false, &idk, loc);
7068
7069 is_member_access = true;
7070 break;
7071
7072 case CPP_PLUS_PLUS:
7073 /* postfix-expression ++ */
7074 /* Consume the `++' token. */
7075 cp_lexer_consume_token (parser->lexer);
7076 /* Generate a representation for the complete expression. */
7077 postfix_expression
7078 = finish_increment_expr (postfix_expression,
7079 POSTINCREMENT_EXPR);
7080 /* Increments may not appear in constant-expressions. */
7081 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7082 postfix_expression = error_mark_node;
7083 idk = CP_ID_KIND_NONE;
7084 is_member_access = false;
7085 break;
7086
7087 case CPP_MINUS_MINUS:
7088 /* postfix-expression -- */
7089 /* Consume the `--' token. */
7090 cp_lexer_consume_token (parser->lexer);
7091 /* Generate a representation for the complete expression. */
7092 postfix_expression
7093 = finish_increment_expr (postfix_expression,
7094 POSTDECREMENT_EXPR);
7095 /* Decrements may not appear in constant-expressions. */
7096 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7097 postfix_expression = error_mark_node;
7098 idk = CP_ID_KIND_NONE;
7099 is_member_access = false;
7100 break;
7101
7102 default:
7103 if (pidk_return != NULL)
7104 * pidk_return = idk;
7105 if (member_access_only_p)
7106 return is_member_access
7107 ? postfix_expression
7108 : cp_expr (error_mark_node);
7109 else
7110 return postfix_expression;
7111 }
7112 }
7113
7114 /* We should never get here. */
7115 gcc_unreachable ();
7116 return error_mark_node;
7117 }
7118
7119 /* This function parses Cilk Plus array notations. If a normal array expr. is
7120 parsed then the array index is passed back to the caller through *INIT_INDEX
7121 and the function returns a NULL_TREE. If array notation expr. is parsed,
7122 then *INIT_INDEX is ignored by the caller and the function returns
7123 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7124 error_mark_node. */
7125
7126 static tree
7127 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7128 tree array_value)
7129 {
7130 cp_token *token = NULL;
7131 tree length_index, stride = NULL_TREE, value_tree, array_type;
7132 if (!array_value || array_value == error_mark_node)
7133 {
7134 cp_parser_skip_to_end_of_statement (parser);
7135 return error_mark_node;
7136 }
7137
7138 array_type = TREE_TYPE (array_value);
7139
7140 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7141 parser->colon_corrects_to_scope_p = false;
7142 token = cp_lexer_peek_token (parser->lexer);
7143
7144 if (!token)
7145 {
7146 cp_parser_error (parser, "expected %<:%> or numeral");
7147 return error_mark_node;
7148 }
7149 else if (token->type == CPP_COLON)
7150 {
7151 /* Consume the ':'. */
7152 cp_lexer_consume_token (parser->lexer);
7153
7154 /* If we are here, then we have a case like this A[:]. */
7155 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7156 {
7157 cp_parser_error (parser, "expected %<]%>");
7158 cp_parser_skip_to_end_of_statement (parser);
7159 return error_mark_node;
7160 }
7161 *init_index = NULL_TREE;
7162 stride = NULL_TREE;
7163 length_index = NULL_TREE;
7164 }
7165 else
7166 {
7167 /* If we are here, then there are three valid possibilities:
7168 1. ARRAY [ EXP ]
7169 2. ARRAY [ EXP : EXP ]
7170 3. ARRAY [ EXP : EXP : EXP ] */
7171
7172 *init_index = cp_parser_expression (parser);
7173 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7174 {
7175 /* This indicates that we have a normal array expression. */
7176 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7177 return NULL_TREE;
7178 }
7179
7180 /* Consume the ':'. */
7181 cp_lexer_consume_token (parser->lexer);
7182 length_index = cp_parser_expression (parser);
7183 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7184 {
7185 cp_lexer_consume_token (parser->lexer);
7186 stride = cp_parser_expression (parser);
7187 }
7188 }
7189 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7190
7191 if (*init_index == error_mark_node || length_index == error_mark_node
7192 || stride == error_mark_node || array_type == error_mark_node)
7193 {
7194 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7195 cp_lexer_consume_token (parser->lexer);
7196 return error_mark_node;
7197 }
7198 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7199
7200 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7201 length_index, stride, array_type);
7202 return value_tree;
7203 }
7204
7205 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7206 by cp_parser_builtin_offsetof. We're looking for
7207
7208 postfix-expression [ expression ]
7209 postfix-expression [ braced-init-list ] (C++11)
7210
7211 FOR_OFFSETOF is set if we're being called in that context, which
7212 changes how we deal with integer constant expressions. */
7213
7214 static tree
7215 cp_parser_postfix_open_square_expression (cp_parser *parser,
7216 tree postfix_expression,
7217 bool for_offsetof,
7218 bool decltype_p)
7219 {
7220 tree index = NULL_TREE;
7221 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7222 bool saved_greater_than_is_operator_p;
7223
7224 /* Consume the `[' token. */
7225 cp_lexer_consume_token (parser->lexer);
7226
7227 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7228 parser->greater_than_is_operator_p = true;
7229
7230 /* Parse the index expression. */
7231 /* ??? For offsetof, there is a question of what to allow here. If
7232 offsetof is not being used in an integral constant expression context,
7233 then we *could* get the right answer by computing the value at runtime.
7234 If we are in an integral constant expression context, then we might
7235 could accept any constant expression; hard to say without analysis.
7236 Rather than open the barn door too wide right away, allow only integer
7237 constant expressions here. */
7238 if (for_offsetof)
7239 index = cp_parser_constant_expression (parser);
7240 else
7241 {
7242 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7243 {
7244 bool expr_nonconst_p;
7245 cp_lexer_set_source_position (parser->lexer);
7246 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7247 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7248 if (flag_cilkplus
7249 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7250 {
7251 error_at (cp_lexer_peek_token (parser->lexer)->location,
7252 "braced list index is not allowed with array "
7253 "notation");
7254 cp_parser_skip_to_end_of_statement (parser);
7255 return error_mark_node;
7256 }
7257 }
7258 else if (flag_cilkplus)
7259 {
7260 /* Here are have these two options:
7261 ARRAY[EXP : EXP] - Array notation expr with default
7262 stride of 1.
7263 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7264 stride. */
7265 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7266 postfix_expression);
7267 if (an_exp)
7268 return an_exp;
7269 }
7270 else
7271 index = cp_parser_expression (parser);
7272 }
7273
7274 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7275
7276 /* Look for the closing `]'. */
7277 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7278
7279 /* Build the ARRAY_REF. */
7280 postfix_expression = grok_array_decl (loc, postfix_expression,
7281 index, decltype_p);
7282
7283 /* When not doing offsetof, array references are not permitted in
7284 constant-expressions. */
7285 if (!for_offsetof
7286 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7287 postfix_expression = error_mark_node;
7288
7289 return postfix_expression;
7290 }
7291
7292 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7293 by cp_parser_builtin_offsetof. We're looking for
7294
7295 postfix-expression . template [opt] id-expression
7296 postfix-expression . pseudo-destructor-name
7297 postfix-expression -> template [opt] id-expression
7298 postfix-expression -> pseudo-destructor-name
7299
7300 FOR_OFFSETOF is set if we're being called in that context. That sorta
7301 limits what of the above we'll actually accept, but nevermind.
7302 TOKEN_TYPE is the "." or "->" token, which will already have been
7303 removed from the stream. */
7304
7305 static tree
7306 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7307 enum cpp_ttype token_type,
7308 cp_expr postfix_expression,
7309 bool for_offsetof, cp_id_kind *idk,
7310 location_t location)
7311 {
7312 tree name;
7313 bool dependent_p;
7314 bool pseudo_destructor_p;
7315 tree scope = NULL_TREE;
7316 location_t start_loc = postfix_expression.get_start ();
7317
7318 /* If this is a `->' operator, dereference the pointer. */
7319 if (token_type == CPP_DEREF)
7320 postfix_expression = build_x_arrow (location, postfix_expression,
7321 tf_warning_or_error);
7322 /* Check to see whether or not the expression is type-dependent and
7323 not the current instantiation. */
7324 dependent_p = type_dependent_object_expression_p (postfix_expression);
7325 /* The identifier following the `->' or `.' is not qualified. */
7326 parser->scope = NULL_TREE;
7327 parser->qualifying_scope = NULL_TREE;
7328 parser->object_scope = NULL_TREE;
7329 *idk = CP_ID_KIND_NONE;
7330
7331 /* Enter the scope corresponding to the type of the object
7332 given by the POSTFIX_EXPRESSION. */
7333 if (!dependent_p)
7334 {
7335 scope = TREE_TYPE (postfix_expression);
7336 /* According to the standard, no expression should ever have
7337 reference type. Unfortunately, we do not currently match
7338 the standard in this respect in that our internal representation
7339 of an expression may have reference type even when the standard
7340 says it does not. Therefore, we have to manually obtain the
7341 underlying type here. */
7342 scope = non_reference (scope);
7343 /* The type of the POSTFIX_EXPRESSION must be complete. */
7344 /* Unlike the object expression in other contexts, *this is not
7345 required to be of complete type for purposes of class member
7346 access (5.2.5) outside the member function body. */
7347 if (postfix_expression != current_class_ref
7348 && scope != error_mark_node
7349 && !(processing_template_decl
7350 && current_class_type
7351 && (same_type_ignoring_top_level_qualifiers_p
7352 (scope, current_class_type))))
7353 {
7354 scope = complete_type (scope);
7355 if (!COMPLETE_TYPE_P (scope)
7356 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7357 && EXPR_P (postfix_expression))
7358 {
7359 /* In a template, be permissive by treating an object expression
7360 of incomplete type as dependent (after a pedwarn). */
7361 diagnostic_t kind = (processing_template_decl
7362 ? DK_PEDWARN
7363 : DK_ERROR);
7364 cxx_incomplete_type_diagnostic
7365 (location_of (postfix_expression),
7366 postfix_expression, scope, kind);
7367 if (processing_template_decl)
7368 {
7369 dependent_p = true;
7370 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7371 }
7372 }
7373 }
7374
7375 if (!dependent_p)
7376 {
7377 /* Let the name lookup machinery know that we are processing a
7378 class member access expression. */
7379 parser->context->object_type = scope;
7380 /* If something went wrong, we want to be able to discern that case,
7381 as opposed to the case where there was no SCOPE due to the type
7382 of expression being dependent. */
7383 if (!scope)
7384 scope = error_mark_node;
7385 /* If the SCOPE was erroneous, make the various semantic analysis
7386 functions exit quickly -- and without issuing additional error
7387 messages. */
7388 if (scope == error_mark_node)
7389 postfix_expression = error_mark_node;
7390 }
7391 }
7392
7393 if (dependent_p)
7394 /* Tell cp_parser_lookup_name that there was an object, even though it's
7395 type-dependent. */
7396 parser->context->object_type = unknown_type_node;
7397
7398 /* Assume this expression is not a pseudo-destructor access. */
7399 pseudo_destructor_p = false;
7400
7401 /* If the SCOPE is a scalar type, then, if this is a valid program,
7402 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7403 is type dependent, it can be pseudo-destructor-name or something else.
7404 Try to parse it as pseudo-destructor-name first. */
7405 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7406 {
7407 tree s;
7408 tree type;
7409
7410 cp_parser_parse_tentatively (parser);
7411 /* Parse the pseudo-destructor-name. */
7412 s = NULL_TREE;
7413 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7414 &s, &type);
7415 if (dependent_p
7416 && (cp_parser_error_occurred (parser)
7417 || !SCALAR_TYPE_P (type)))
7418 cp_parser_abort_tentative_parse (parser);
7419 else if (cp_parser_parse_definitely (parser))
7420 {
7421 pseudo_destructor_p = true;
7422 postfix_expression
7423 = finish_pseudo_destructor_expr (postfix_expression,
7424 s, type, location);
7425 }
7426 }
7427
7428 if (!pseudo_destructor_p)
7429 {
7430 /* If the SCOPE is not a scalar type, we are looking at an
7431 ordinary class member access expression, rather than a
7432 pseudo-destructor-name. */
7433 bool template_p;
7434 cp_token *token = cp_lexer_peek_token (parser->lexer);
7435 /* Parse the id-expression. */
7436 name = (cp_parser_id_expression
7437 (parser,
7438 cp_parser_optional_template_keyword (parser),
7439 /*check_dependency_p=*/true,
7440 &template_p,
7441 /*declarator_p=*/false,
7442 /*optional_p=*/false));
7443 /* In general, build a SCOPE_REF if the member name is qualified.
7444 However, if the name was not dependent and has already been
7445 resolved; there is no need to build the SCOPE_REF. For example;
7446
7447 struct X { void f(); };
7448 template <typename T> void f(T* t) { t->X::f(); }
7449
7450 Even though "t" is dependent, "X::f" is not and has been resolved
7451 to a BASELINK; there is no need to include scope information. */
7452
7453 /* But we do need to remember that there was an explicit scope for
7454 virtual function calls. */
7455 if (parser->scope)
7456 *idk = CP_ID_KIND_QUALIFIED;
7457
7458 /* If the name is a template-id that names a type, we will get a
7459 TYPE_DECL here. That is invalid code. */
7460 if (TREE_CODE (name) == TYPE_DECL)
7461 {
7462 error_at (token->location, "invalid use of %qD", name);
7463 postfix_expression = error_mark_node;
7464 }
7465 else
7466 {
7467 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7468 {
7469 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7470 {
7471 error_at (token->location, "%<%D::%D%> is not a class member",
7472 parser->scope, name);
7473 postfix_expression = error_mark_node;
7474 }
7475 else
7476 name = build_qualified_name (/*type=*/NULL_TREE,
7477 parser->scope,
7478 name,
7479 template_p);
7480 parser->scope = NULL_TREE;
7481 parser->qualifying_scope = NULL_TREE;
7482 parser->object_scope = NULL_TREE;
7483 }
7484 if (parser->scope && name && BASELINK_P (name))
7485 adjust_result_of_qualified_name_lookup
7486 (name, parser->scope, scope);
7487 postfix_expression
7488 = finish_class_member_access_expr (postfix_expression, name,
7489 template_p,
7490 tf_warning_or_error);
7491 /* Build a location e.g.:
7492 ptr->access_expr
7493 ~~~^~~~~~~~~~~~~
7494 where the caret is at the deref token, ranging from
7495 the start of postfix_expression to the end of the access expr. */
7496 location_t end_loc
7497 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7498 location_t combined_loc
7499 = make_location (input_location, start_loc, end_loc);
7500 protected_set_expr_location (postfix_expression, combined_loc);
7501 }
7502 }
7503
7504 /* We no longer need to look up names in the scope of the object on
7505 the left-hand side of the `.' or `->' operator. */
7506 parser->context->object_type = NULL_TREE;
7507
7508 /* Outside of offsetof, these operators may not appear in
7509 constant-expressions. */
7510 if (!for_offsetof
7511 && (cp_parser_non_integral_constant_expression
7512 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7513 postfix_expression = error_mark_node;
7514
7515 return postfix_expression;
7516 }
7517
7518 /* Parse a parenthesized expression-list.
7519
7520 expression-list:
7521 assignment-expression
7522 expression-list, assignment-expression
7523
7524 attribute-list:
7525 expression-list
7526 identifier
7527 identifier, expression-list
7528
7529 CAST_P is true if this expression is the target of a cast.
7530
7531 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7532 argument pack.
7533
7534 Returns a vector of trees. Each element is a representation of an
7535 assignment-expression. NULL is returned if the ( and or ) are
7536 missing. An empty, but allocated, vector is returned on no
7537 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7538 if we are parsing an attribute list for an attribute that wants a
7539 plain identifier argument, normal_attr for an attribute that wants
7540 an expression, or non_attr if we aren't parsing an attribute list. If
7541 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7542 not all of the expressions in the list were constant.
7543 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7544 will be written to with the location of the closing parenthesis. If
7545 an error occurs, it may or may not be written to. */
7546
7547 static vec<tree, va_gc> *
7548 cp_parser_parenthesized_expression_list (cp_parser* parser,
7549 int is_attribute_list,
7550 bool cast_p,
7551 bool allow_expansion_p,
7552 bool *non_constant_p,
7553 location_t *close_paren_loc)
7554 {
7555 vec<tree, va_gc> *expression_list;
7556 bool fold_expr_p = is_attribute_list != non_attr;
7557 tree identifier = NULL_TREE;
7558 bool saved_greater_than_is_operator_p;
7559
7560 /* Assume all the expressions will be constant. */
7561 if (non_constant_p)
7562 *non_constant_p = false;
7563
7564 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7565 return NULL;
7566
7567 expression_list = make_tree_vector ();
7568
7569 /* Within a parenthesized expression, a `>' token is always
7570 the greater-than operator. */
7571 saved_greater_than_is_operator_p
7572 = parser->greater_than_is_operator_p;
7573 parser->greater_than_is_operator_p = true;
7574
7575 /* Consume expressions until there are no more. */
7576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7577 while (true)
7578 {
7579 tree expr;
7580
7581 /* At the beginning of attribute lists, check to see if the
7582 next token is an identifier. */
7583 if (is_attribute_list == id_attr
7584 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7585 {
7586 cp_token *token;
7587
7588 /* Consume the identifier. */
7589 token = cp_lexer_consume_token (parser->lexer);
7590 /* Save the identifier. */
7591 identifier = token->u.value;
7592 }
7593 else
7594 {
7595 bool expr_non_constant_p;
7596
7597 /* Parse the next assignment-expression. */
7598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7599 {
7600 /* A braced-init-list. */
7601 cp_lexer_set_source_position (parser->lexer);
7602 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7603 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7604 if (non_constant_p && expr_non_constant_p)
7605 *non_constant_p = true;
7606 }
7607 else if (non_constant_p)
7608 {
7609 expr = (cp_parser_constant_expression
7610 (parser, /*allow_non_constant_p=*/true,
7611 &expr_non_constant_p));
7612 if (expr_non_constant_p)
7613 *non_constant_p = true;
7614 }
7615 else
7616 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7617 cast_p);
7618
7619 if (fold_expr_p)
7620 expr = instantiate_non_dependent_expr (expr);
7621
7622 /* If we have an ellipsis, then this is an expression
7623 expansion. */
7624 if (allow_expansion_p
7625 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7626 {
7627 /* Consume the `...'. */
7628 cp_lexer_consume_token (parser->lexer);
7629
7630 /* Build the argument pack. */
7631 expr = make_pack_expansion (expr);
7632 }
7633
7634 /* Add it to the list. We add error_mark_node
7635 expressions to the list, so that we can still tell if
7636 the correct form for a parenthesized expression-list
7637 is found. That gives better errors. */
7638 vec_safe_push (expression_list, expr);
7639
7640 if (expr == error_mark_node)
7641 goto skip_comma;
7642 }
7643
7644 /* After the first item, attribute lists look the same as
7645 expression lists. */
7646 is_attribute_list = non_attr;
7647
7648 get_comma:;
7649 /* If the next token isn't a `,', then we are done. */
7650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7651 break;
7652
7653 /* Otherwise, consume the `,' and keep going. */
7654 cp_lexer_consume_token (parser->lexer);
7655 }
7656
7657 if (close_paren_loc)
7658 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7659
7660 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7661 {
7662 int ending;
7663
7664 skip_comma:;
7665 /* We try and resync to an unnested comma, as that will give the
7666 user better diagnostics. */
7667 ending = cp_parser_skip_to_closing_parenthesis (parser,
7668 /*recovering=*/true,
7669 /*or_comma=*/true,
7670 /*consume_paren=*/true);
7671 if (ending < 0)
7672 goto get_comma;
7673 if (!ending)
7674 {
7675 parser->greater_than_is_operator_p
7676 = saved_greater_than_is_operator_p;
7677 return NULL;
7678 }
7679 }
7680
7681 parser->greater_than_is_operator_p
7682 = saved_greater_than_is_operator_p;
7683
7684 if (identifier)
7685 vec_safe_insert (expression_list, 0, identifier);
7686
7687 return expression_list;
7688 }
7689
7690 /* Parse a pseudo-destructor-name.
7691
7692 pseudo-destructor-name:
7693 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7694 :: [opt] nested-name-specifier template template-id :: ~ type-name
7695 :: [opt] nested-name-specifier [opt] ~ type-name
7696
7697 If either of the first two productions is used, sets *SCOPE to the
7698 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7699 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7700 or ERROR_MARK_NODE if the parse fails. */
7701
7702 static void
7703 cp_parser_pseudo_destructor_name (cp_parser* parser,
7704 tree object,
7705 tree* scope,
7706 tree* type)
7707 {
7708 bool nested_name_specifier_p;
7709
7710 /* Handle ~auto. */
7711 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7712 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7713 && !type_dependent_expression_p (object))
7714 {
7715 if (cxx_dialect < cxx14)
7716 pedwarn (input_location, 0,
7717 "%<~auto%> only available with "
7718 "-std=c++14 or -std=gnu++14");
7719 cp_lexer_consume_token (parser->lexer);
7720 cp_lexer_consume_token (parser->lexer);
7721 *scope = NULL_TREE;
7722 *type = TREE_TYPE (object);
7723 return;
7724 }
7725
7726 /* Assume that things will not work out. */
7727 *type = error_mark_node;
7728
7729 /* Look for the optional `::' operator. */
7730 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7731 /* Look for the optional nested-name-specifier. */
7732 nested_name_specifier_p
7733 = (cp_parser_nested_name_specifier_opt (parser,
7734 /*typename_keyword_p=*/false,
7735 /*check_dependency_p=*/true,
7736 /*type_p=*/false,
7737 /*is_declaration=*/false)
7738 != NULL_TREE);
7739 /* Now, if we saw a nested-name-specifier, we might be doing the
7740 second production. */
7741 if (nested_name_specifier_p
7742 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7743 {
7744 /* Consume the `template' keyword. */
7745 cp_lexer_consume_token (parser->lexer);
7746 /* Parse the template-id. */
7747 cp_parser_template_id (parser,
7748 /*template_keyword_p=*/true,
7749 /*check_dependency_p=*/false,
7750 class_type,
7751 /*is_declaration=*/true);
7752 /* Look for the `::' token. */
7753 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7754 }
7755 /* If the next token is not a `~', then there might be some
7756 additional qualification. */
7757 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7758 {
7759 /* At this point, we're looking for "type-name :: ~". The type-name
7760 must not be a class-name, since this is a pseudo-destructor. So,
7761 it must be either an enum-name, or a typedef-name -- both of which
7762 are just identifiers. So, we peek ahead to check that the "::"
7763 and "~" tokens are present; if they are not, then we can avoid
7764 calling type_name. */
7765 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7766 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7767 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7768 {
7769 cp_parser_error (parser, "non-scalar type");
7770 return;
7771 }
7772
7773 /* Look for the type-name. */
7774 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7775 if (*scope == error_mark_node)
7776 return;
7777
7778 /* Look for the `::' token. */
7779 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7780 }
7781 else
7782 *scope = NULL_TREE;
7783
7784 /* Look for the `~'. */
7785 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7786
7787 /* Once we see the ~, this has to be a pseudo-destructor. */
7788 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7789 cp_parser_commit_to_topmost_tentative_parse (parser);
7790
7791 /* Look for the type-name again. We are not responsible for
7792 checking that it matches the first type-name. */
7793 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7794 }
7795
7796 /* Parse a unary-expression.
7797
7798 unary-expression:
7799 postfix-expression
7800 ++ cast-expression
7801 -- cast-expression
7802 unary-operator cast-expression
7803 sizeof unary-expression
7804 sizeof ( type-id )
7805 alignof ( type-id ) [C++0x]
7806 new-expression
7807 delete-expression
7808
7809 GNU Extensions:
7810
7811 unary-expression:
7812 __extension__ cast-expression
7813 __alignof__ unary-expression
7814 __alignof__ ( type-id )
7815 alignof unary-expression [C++0x]
7816 __real__ cast-expression
7817 __imag__ cast-expression
7818 && identifier
7819 sizeof ( type-id ) { initializer-list , [opt] }
7820 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7821 __alignof__ ( type-id ) { initializer-list , [opt] }
7822
7823 ADDRESS_P is true iff the unary-expression is appearing as the
7824 operand of the `&' operator. CAST_P is true if this expression is
7825 the target of a cast.
7826
7827 Returns a representation of the expression. */
7828
7829 static cp_expr
7830 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7831 bool address_p, bool cast_p, bool decltype_p)
7832 {
7833 cp_token *token;
7834 enum tree_code unary_operator;
7835
7836 /* Peek at the next token. */
7837 token = cp_lexer_peek_token (parser->lexer);
7838 /* Some keywords give away the kind of expression. */
7839 if (token->type == CPP_KEYWORD)
7840 {
7841 enum rid keyword = token->keyword;
7842
7843 switch (keyword)
7844 {
7845 case RID_ALIGNOF:
7846 case RID_SIZEOF:
7847 {
7848 tree operand, ret;
7849 enum tree_code op;
7850 location_t start_loc = token->location;
7851
7852 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7853 /* Consume the token. */
7854 cp_lexer_consume_token (parser->lexer);
7855 /* Parse the operand. */
7856 operand = cp_parser_sizeof_operand (parser, keyword);
7857
7858 if (TYPE_P (operand))
7859 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7860 else
7861 {
7862 /* ISO C++ defines alignof only with types, not with
7863 expressions. So pedwarn if alignof is used with a non-
7864 type expression. However, __alignof__ is ok. */
7865 if (id_equal (token->u.value, "alignof"))
7866 pedwarn (token->location, OPT_Wpedantic,
7867 "ISO C++ does not allow %<alignof%> "
7868 "with a non-type");
7869
7870 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7871 }
7872 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7873 SIZEOF_EXPR with the original operand. */
7874 if (op == SIZEOF_EXPR && ret != error_mark_node)
7875 {
7876 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7877 {
7878 if (!processing_template_decl && TYPE_P (operand))
7879 {
7880 ret = build_min (SIZEOF_EXPR, size_type_node,
7881 build1 (NOP_EXPR, operand,
7882 error_mark_node));
7883 SIZEOF_EXPR_TYPE_P (ret) = 1;
7884 }
7885 else
7886 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7887 TREE_SIDE_EFFECTS (ret) = 0;
7888 TREE_READONLY (ret) = 1;
7889 }
7890 }
7891
7892 /* Construct a location e.g. :
7893 alignof (expr)
7894 ^~~~~~~~~~~~~~
7895 with start == caret at the start of the "alignof"/"sizeof"
7896 token, with the endpoint at the final closing paren. */
7897 location_t finish_loc
7898 = cp_lexer_previous_token (parser->lexer)->location;
7899 location_t compound_loc
7900 = make_location (start_loc, start_loc, finish_loc);
7901
7902 cp_expr ret_expr (ret);
7903 ret_expr.set_location (compound_loc);
7904 return ret_expr;
7905 }
7906
7907 case RID_NEW:
7908 return cp_parser_new_expression (parser);
7909
7910 case RID_DELETE:
7911 return cp_parser_delete_expression (parser);
7912
7913 case RID_EXTENSION:
7914 {
7915 /* The saved value of the PEDANTIC flag. */
7916 int saved_pedantic;
7917 tree expr;
7918
7919 /* Save away the PEDANTIC flag. */
7920 cp_parser_extension_opt (parser, &saved_pedantic);
7921 /* Parse the cast-expression. */
7922 expr = cp_parser_simple_cast_expression (parser);
7923 /* Restore the PEDANTIC flag. */
7924 pedantic = saved_pedantic;
7925
7926 return expr;
7927 }
7928
7929 case RID_REALPART:
7930 case RID_IMAGPART:
7931 {
7932 tree expression;
7933
7934 /* Consume the `__real__' or `__imag__' token. */
7935 cp_lexer_consume_token (parser->lexer);
7936 /* Parse the cast-expression. */
7937 expression = cp_parser_simple_cast_expression (parser);
7938 /* Create the complete representation. */
7939 return build_x_unary_op (token->location,
7940 (keyword == RID_REALPART
7941 ? REALPART_EXPR : IMAGPART_EXPR),
7942 expression,
7943 tf_warning_or_error);
7944 }
7945 break;
7946
7947 case RID_TRANSACTION_ATOMIC:
7948 case RID_TRANSACTION_RELAXED:
7949 return cp_parser_transaction_expression (parser, keyword);
7950
7951 case RID_NOEXCEPT:
7952 {
7953 tree expr;
7954 const char *saved_message;
7955 bool saved_integral_constant_expression_p;
7956 bool saved_non_integral_constant_expression_p;
7957 bool saved_greater_than_is_operator_p;
7958
7959 cp_lexer_consume_token (parser->lexer);
7960 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7961
7962 saved_message = parser->type_definition_forbidden_message;
7963 parser->type_definition_forbidden_message
7964 = G_("types may not be defined in %<noexcept%> expressions");
7965
7966 saved_integral_constant_expression_p
7967 = parser->integral_constant_expression_p;
7968 saved_non_integral_constant_expression_p
7969 = parser->non_integral_constant_expression_p;
7970 parser->integral_constant_expression_p = false;
7971
7972 saved_greater_than_is_operator_p
7973 = parser->greater_than_is_operator_p;
7974 parser->greater_than_is_operator_p = true;
7975
7976 ++cp_unevaluated_operand;
7977 ++c_inhibit_evaluation_warnings;
7978 ++cp_noexcept_operand;
7979 expr = cp_parser_expression (parser);
7980 --cp_noexcept_operand;
7981 --c_inhibit_evaluation_warnings;
7982 --cp_unevaluated_operand;
7983
7984 parser->greater_than_is_operator_p
7985 = saved_greater_than_is_operator_p;
7986
7987 parser->integral_constant_expression_p
7988 = saved_integral_constant_expression_p;
7989 parser->non_integral_constant_expression_p
7990 = saved_non_integral_constant_expression_p;
7991
7992 parser->type_definition_forbidden_message = saved_message;
7993
7994 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7995 return finish_noexcept_expr (expr, tf_warning_or_error);
7996 }
7997
7998 default:
7999 break;
8000 }
8001 }
8002
8003 /* Look for the `:: new' and `:: delete', which also signal the
8004 beginning of a new-expression, or delete-expression,
8005 respectively. If the next token is `::', then it might be one of
8006 these. */
8007 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8008 {
8009 enum rid keyword;
8010
8011 /* See if the token after the `::' is one of the keywords in
8012 which we're interested. */
8013 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8014 /* If it's `new', we have a new-expression. */
8015 if (keyword == RID_NEW)
8016 return cp_parser_new_expression (parser);
8017 /* Similarly, for `delete'. */
8018 else if (keyword == RID_DELETE)
8019 return cp_parser_delete_expression (parser);
8020 }
8021
8022 /* Look for a unary operator. */
8023 unary_operator = cp_parser_unary_operator (token);
8024 /* The `++' and `--' operators can be handled similarly, even though
8025 they are not technically unary-operators in the grammar. */
8026 if (unary_operator == ERROR_MARK)
8027 {
8028 if (token->type == CPP_PLUS_PLUS)
8029 unary_operator = PREINCREMENT_EXPR;
8030 else if (token->type == CPP_MINUS_MINUS)
8031 unary_operator = PREDECREMENT_EXPR;
8032 /* Handle the GNU address-of-label extension. */
8033 else if (cp_parser_allow_gnu_extensions_p (parser)
8034 && token->type == CPP_AND_AND)
8035 {
8036 tree identifier;
8037 tree expression;
8038 location_t start_loc = token->location;
8039
8040 /* Consume the '&&' token. */
8041 cp_lexer_consume_token (parser->lexer);
8042 /* Look for the identifier. */
8043 location_t finish_loc
8044 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8045 identifier = cp_parser_identifier (parser);
8046 /* Construct a location of the form:
8047 &&label
8048 ^~~~~~~
8049 with caret==start at the "&&", finish at the end of the label. */
8050 location_t combined_loc
8051 = make_location (start_loc, start_loc, finish_loc);
8052 /* Create an expression representing the address. */
8053 expression = finish_label_address_expr (identifier, combined_loc);
8054 if (cp_parser_non_integral_constant_expression (parser,
8055 NIC_ADDR_LABEL))
8056 expression = error_mark_node;
8057 return expression;
8058 }
8059 }
8060 if (unary_operator != ERROR_MARK)
8061 {
8062 cp_expr cast_expression;
8063 cp_expr expression = error_mark_node;
8064 non_integral_constant non_constant_p = NIC_NONE;
8065 location_t loc = token->location;
8066 tsubst_flags_t complain = complain_flags (decltype_p);
8067
8068 /* Consume the operator token. */
8069 token = cp_lexer_consume_token (parser->lexer);
8070 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8071
8072 /* Parse the cast-expression. */
8073 cast_expression
8074 = cp_parser_cast_expression (parser,
8075 unary_operator == ADDR_EXPR,
8076 /*cast_p=*/false,
8077 /*decltype*/false,
8078 pidk);
8079
8080 /* Make a location:
8081 OP_TOKEN CAST_EXPRESSION
8082 ^~~~~~~~~~~~~~~~~~~~~~~~~
8083 with start==caret at the operator token, and
8084 extending to the end of the cast_expression. */
8085 loc = make_location (loc, loc, cast_expression.get_finish ());
8086
8087 /* Now, build an appropriate representation. */
8088 switch (unary_operator)
8089 {
8090 case INDIRECT_REF:
8091 non_constant_p = NIC_STAR;
8092 expression = build_x_indirect_ref (loc, cast_expression,
8093 RO_UNARY_STAR,
8094 complain);
8095 /* TODO: build_x_indirect_ref does not always honor the
8096 location, so ensure it is set. */
8097 expression.set_location (loc);
8098 break;
8099
8100 case ADDR_EXPR:
8101 non_constant_p = NIC_ADDR;
8102 /* Fall through. */
8103 case BIT_NOT_EXPR:
8104 expression = build_x_unary_op (loc, unary_operator,
8105 cast_expression,
8106 complain);
8107 /* TODO: build_x_unary_op does not always honor the location,
8108 so ensure it is set. */
8109 expression.set_location (loc);
8110 break;
8111
8112 case PREINCREMENT_EXPR:
8113 case PREDECREMENT_EXPR:
8114 non_constant_p = unary_operator == PREINCREMENT_EXPR
8115 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8116 /* Fall through. */
8117 case NEGATE_EXPR:
8118 /* Immediately fold negation of a constant, unless the constant is 0
8119 (since -0 == 0) or it would overflow. */
8120 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8121 && CONSTANT_CLASS_P (cast_expression)
8122 && !integer_zerop (cast_expression)
8123 && !TREE_OVERFLOW (cast_expression))
8124 {
8125 tree folded = fold_build1 (unary_operator,
8126 TREE_TYPE (cast_expression),
8127 cast_expression);
8128 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8129 {
8130 expression = cp_expr (folded, loc);
8131 break;
8132 }
8133 }
8134 /* Fall through. */
8135 case UNARY_PLUS_EXPR:
8136 case TRUTH_NOT_EXPR:
8137 expression = finish_unary_op_expr (loc, unary_operator,
8138 cast_expression, complain);
8139 break;
8140
8141 default:
8142 gcc_unreachable ();
8143 }
8144
8145 if (non_constant_p != NIC_NONE
8146 && cp_parser_non_integral_constant_expression (parser,
8147 non_constant_p))
8148 expression = error_mark_node;
8149
8150 return expression;
8151 }
8152
8153 return cp_parser_postfix_expression (parser, address_p, cast_p,
8154 /*member_access_only_p=*/false,
8155 decltype_p,
8156 pidk);
8157 }
8158
8159 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8160 unary-operator, the corresponding tree code is returned. */
8161
8162 static enum tree_code
8163 cp_parser_unary_operator (cp_token* token)
8164 {
8165 switch (token->type)
8166 {
8167 case CPP_MULT:
8168 return INDIRECT_REF;
8169
8170 case CPP_AND:
8171 return ADDR_EXPR;
8172
8173 case CPP_PLUS:
8174 return UNARY_PLUS_EXPR;
8175
8176 case CPP_MINUS:
8177 return NEGATE_EXPR;
8178
8179 case CPP_NOT:
8180 return TRUTH_NOT_EXPR;
8181
8182 case CPP_COMPL:
8183 return BIT_NOT_EXPR;
8184
8185 default:
8186 return ERROR_MARK;
8187 }
8188 }
8189
8190 /* Parse a new-expression.
8191
8192 new-expression:
8193 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8194 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8195
8196 Returns a representation of the expression. */
8197
8198 static tree
8199 cp_parser_new_expression (cp_parser* parser)
8200 {
8201 bool global_scope_p;
8202 vec<tree, va_gc> *placement;
8203 tree type;
8204 vec<tree, va_gc> *initializer;
8205 tree nelts = NULL_TREE;
8206 tree ret;
8207
8208 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8209
8210 /* Look for the optional `::' operator. */
8211 global_scope_p
8212 = (cp_parser_global_scope_opt (parser,
8213 /*current_scope_valid_p=*/false)
8214 != NULL_TREE);
8215 /* Look for the `new' operator. */
8216 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8217 /* There's no easy way to tell a new-placement from the
8218 `( type-id )' construct. */
8219 cp_parser_parse_tentatively (parser);
8220 /* Look for a new-placement. */
8221 placement = cp_parser_new_placement (parser);
8222 /* If that didn't work out, there's no new-placement. */
8223 if (!cp_parser_parse_definitely (parser))
8224 {
8225 if (placement != NULL)
8226 release_tree_vector (placement);
8227 placement = NULL;
8228 }
8229
8230 /* If the next token is a `(', then we have a parenthesized
8231 type-id. */
8232 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8233 {
8234 cp_token *token;
8235 const char *saved_message = parser->type_definition_forbidden_message;
8236
8237 /* Consume the `('. */
8238 cp_lexer_consume_token (parser->lexer);
8239
8240 /* Parse the type-id. */
8241 parser->type_definition_forbidden_message
8242 = G_("types may not be defined in a new-expression");
8243 {
8244 type_id_in_expr_sentinel s (parser);
8245 type = cp_parser_type_id (parser);
8246 }
8247 parser->type_definition_forbidden_message = saved_message;
8248
8249 /* Look for the closing `)'. */
8250 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8251 token = cp_lexer_peek_token (parser->lexer);
8252 /* There should not be a direct-new-declarator in this production,
8253 but GCC used to allowed this, so we check and emit a sensible error
8254 message for this case. */
8255 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8256 {
8257 error_at (token->location,
8258 "array bound forbidden after parenthesized type-id");
8259 inform (token->location,
8260 "try removing the parentheses around the type-id");
8261 cp_parser_direct_new_declarator (parser);
8262 }
8263 }
8264 /* Otherwise, there must be a new-type-id. */
8265 else
8266 type = cp_parser_new_type_id (parser, &nelts);
8267
8268 /* If the next token is a `(' or '{', then we have a new-initializer. */
8269 cp_token *token = cp_lexer_peek_token (parser->lexer);
8270 if (token->type == CPP_OPEN_PAREN
8271 || token->type == CPP_OPEN_BRACE)
8272 initializer = cp_parser_new_initializer (parser);
8273 else
8274 initializer = NULL;
8275
8276 /* A new-expression may not appear in an integral constant
8277 expression. */
8278 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8279 ret = error_mark_node;
8280 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8281 of a new-type-id or type-id of a new-expression, the new-expression shall
8282 contain a new-initializer of the form ( assignment-expression )".
8283 Additionally, consistently with the spirit of DR 1467, we want to accept
8284 'new auto { 2 }' too. */
8285 else if ((ret = type_uses_auto (type))
8286 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8287 && (vec_safe_length (initializer) != 1
8288 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8289 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8290 {
8291 error_at (token->location,
8292 "initialization of new-expression for type %<auto%> "
8293 "requires exactly one element");
8294 ret = error_mark_node;
8295 }
8296 else
8297 {
8298 /* Construct a location e.g.:
8299 ptr = new int[100]
8300 ^~~~~~~~~~~~
8301 with caret == start at the start of the "new" token, and the end
8302 at the end of the final token we consumed. */
8303 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8304 location_t end_loc = get_finish (end_tok->location);
8305 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8306
8307 /* Create a representation of the new-expression. */
8308 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8309 tf_warning_or_error);
8310 protected_set_expr_location (ret, combined_loc);
8311 }
8312
8313 if (placement != NULL)
8314 release_tree_vector (placement);
8315 if (initializer != NULL)
8316 release_tree_vector (initializer);
8317
8318 return ret;
8319 }
8320
8321 /* Parse a new-placement.
8322
8323 new-placement:
8324 ( expression-list )
8325
8326 Returns the same representation as for an expression-list. */
8327
8328 static vec<tree, va_gc> *
8329 cp_parser_new_placement (cp_parser* parser)
8330 {
8331 vec<tree, va_gc> *expression_list;
8332
8333 /* Parse the expression-list. */
8334 expression_list = (cp_parser_parenthesized_expression_list
8335 (parser, non_attr, /*cast_p=*/false,
8336 /*allow_expansion_p=*/true,
8337 /*non_constant_p=*/NULL));
8338
8339 if (expression_list && expression_list->is_empty ())
8340 error ("expected expression-list or type-id");
8341
8342 return expression_list;
8343 }
8344
8345 /* Parse a new-type-id.
8346
8347 new-type-id:
8348 type-specifier-seq new-declarator [opt]
8349
8350 Returns the TYPE allocated. If the new-type-id indicates an array
8351 type, *NELTS is set to the number of elements in the last array
8352 bound; the TYPE will not include the last array bound. */
8353
8354 static tree
8355 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8356 {
8357 cp_decl_specifier_seq type_specifier_seq;
8358 cp_declarator *new_declarator;
8359 cp_declarator *declarator;
8360 cp_declarator *outer_declarator;
8361 const char *saved_message;
8362
8363 /* The type-specifier sequence must not contain type definitions.
8364 (It cannot contain declarations of new types either, but if they
8365 are not definitions we will catch that because they are not
8366 complete.) */
8367 saved_message = parser->type_definition_forbidden_message;
8368 parser->type_definition_forbidden_message
8369 = G_("types may not be defined in a new-type-id");
8370 /* Parse the type-specifier-seq. */
8371 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8372 /*is_trailing_return=*/false,
8373 &type_specifier_seq);
8374 /* Restore the old message. */
8375 parser->type_definition_forbidden_message = saved_message;
8376
8377 if (type_specifier_seq.type == error_mark_node)
8378 return error_mark_node;
8379
8380 /* Parse the new-declarator. */
8381 new_declarator = cp_parser_new_declarator_opt (parser);
8382
8383 /* Determine the number of elements in the last array dimension, if
8384 any. */
8385 *nelts = NULL_TREE;
8386 /* Skip down to the last array dimension. */
8387 declarator = new_declarator;
8388 outer_declarator = NULL;
8389 while (declarator && (declarator->kind == cdk_pointer
8390 || declarator->kind == cdk_ptrmem))
8391 {
8392 outer_declarator = declarator;
8393 declarator = declarator->declarator;
8394 }
8395 while (declarator
8396 && declarator->kind == cdk_array
8397 && declarator->declarator
8398 && declarator->declarator->kind == cdk_array)
8399 {
8400 outer_declarator = declarator;
8401 declarator = declarator->declarator;
8402 }
8403
8404 if (declarator && declarator->kind == cdk_array)
8405 {
8406 *nelts = declarator->u.array.bounds;
8407 if (*nelts == error_mark_node)
8408 *nelts = integer_one_node;
8409
8410 if (outer_declarator)
8411 outer_declarator->declarator = declarator->declarator;
8412 else
8413 new_declarator = NULL;
8414 }
8415
8416 return groktypename (&type_specifier_seq, new_declarator, false);
8417 }
8418
8419 /* Parse an (optional) new-declarator.
8420
8421 new-declarator:
8422 ptr-operator new-declarator [opt]
8423 direct-new-declarator
8424
8425 Returns the declarator. */
8426
8427 static cp_declarator *
8428 cp_parser_new_declarator_opt (cp_parser* parser)
8429 {
8430 enum tree_code code;
8431 tree type, std_attributes = NULL_TREE;
8432 cp_cv_quals cv_quals;
8433
8434 /* We don't know if there's a ptr-operator next, or not. */
8435 cp_parser_parse_tentatively (parser);
8436 /* Look for a ptr-operator. */
8437 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8438 /* If that worked, look for more new-declarators. */
8439 if (cp_parser_parse_definitely (parser))
8440 {
8441 cp_declarator *declarator;
8442
8443 /* Parse another optional declarator. */
8444 declarator = cp_parser_new_declarator_opt (parser);
8445
8446 declarator = cp_parser_make_indirect_declarator
8447 (code, type, cv_quals, declarator, std_attributes);
8448
8449 return declarator;
8450 }
8451
8452 /* If the next token is a `[', there is a direct-new-declarator. */
8453 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8454 return cp_parser_direct_new_declarator (parser);
8455
8456 return NULL;
8457 }
8458
8459 /* Parse a direct-new-declarator.
8460
8461 direct-new-declarator:
8462 [ expression ]
8463 direct-new-declarator [constant-expression]
8464
8465 */
8466
8467 static cp_declarator *
8468 cp_parser_direct_new_declarator (cp_parser* parser)
8469 {
8470 cp_declarator *declarator = NULL;
8471
8472 while (true)
8473 {
8474 tree expression;
8475 cp_token *token;
8476
8477 /* Look for the opening `['. */
8478 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8479
8480 token = cp_lexer_peek_token (parser->lexer);
8481 expression = cp_parser_expression (parser);
8482 /* The standard requires that the expression have integral
8483 type. DR 74 adds enumeration types. We believe that the
8484 real intent is that these expressions be handled like the
8485 expression in a `switch' condition, which also allows
8486 classes with a single conversion to integral or
8487 enumeration type. */
8488 if (!processing_template_decl)
8489 {
8490 expression
8491 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8492 expression,
8493 /*complain=*/true);
8494 if (!expression)
8495 {
8496 error_at (token->location,
8497 "expression in new-declarator must have integral "
8498 "or enumeration type");
8499 expression = error_mark_node;
8500 }
8501 }
8502
8503 /* Look for the closing `]'. */
8504 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8505
8506 /* Add this bound to the declarator. */
8507 declarator = make_array_declarator (declarator, expression);
8508
8509 /* If the next token is not a `[', then there are no more
8510 bounds. */
8511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8512 break;
8513 }
8514
8515 return declarator;
8516 }
8517
8518 /* Parse a new-initializer.
8519
8520 new-initializer:
8521 ( expression-list [opt] )
8522 braced-init-list
8523
8524 Returns a representation of the expression-list. */
8525
8526 static vec<tree, va_gc> *
8527 cp_parser_new_initializer (cp_parser* parser)
8528 {
8529 vec<tree, va_gc> *expression_list;
8530
8531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8532 {
8533 tree t;
8534 bool expr_non_constant_p;
8535 cp_lexer_set_source_position (parser->lexer);
8536 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8537 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8538 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8539 expression_list = make_tree_vector_single (t);
8540 }
8541 else
8542 expression_list = (cp_parser_parenthesized_expression_list
8543 (parser, non_attr, /*cast_p=*/false,
8544 /*allow_expansion_p=*/true,
8545 /*non_constant_p=*/NULL));
8546
8547 return expression_list;
8548 }
8549
8550 /* Parse a delete-expression.
8551
8552 delete-expression:
8553 :: [opt] delete cast-expression
8554 :: [opt] delete [ ] cast-expression
8555
8556 Returns a representation of the expression. */
8557
8558 static tree
8559 cp_parser_delete_expression (cp_parser* parser)
8560 {
8561 bool global_scope_p;
8562 bool array_p;
8563 tree expression;
8564
8565 /* Look for the optional `::' operator. */
8566 global_scope_p
8567 = (cp_parser_global_scope_opt (parser,
8568 /*current_scope_valid_p=*/false)
8569 != NULL_TREE);
8570 /* Look for the `delete' keyword. */
8571 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8572 /* See if the array syntax is in use. */
8573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8574 {
8575 /* Consume the `[' token. */
8576 cp_lexer_consume_token (parser->lexer);
8577 /* Look for the `]' token. */
8578 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8579 /* Remember that this is the `[]' construct. */
8580 array_p = true;
8581 }
8582 else
8583 array_p = false;
8584
8585 /* Parse the cast-expression. */
8586 expression = cp_parser_simple_cast_expression (parser);
8587
8588 /* A delete-expression may not appear in an integral constant
8589 expression. */
8590 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8591 return error_mark_node;
8592
8593 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8594 tf_warning_or_error);
8595 }
8596
8597 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8598 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8599 0 otherwise. */
8600
8601 static int
8602 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8603 {
8604 cp_token *token = cp_lexer_peek_token (parser->lexer);
8605 switch (token->type)
8606 {
8607 case CPP_COMMA:
8608 case CPP_SEMICOLON:
8609 case CPP_QUERY:
8610 case CPP_COLON:
8611 case CPP_CLOSE_SQUARE:
8612 case CPP_CLOSE_PAREN:
8613 case CPP_CLOSE_BRACE:
8614 case CPP_OPEN_BRACE:
8615 case CPP_DOT:
8616 case CPP_DOT_STAR:
8617 case CPP_DEREF:
8618 case CPP_DEREF_STAR:
8619 case CPP_DIV:
8620 case CPP_MOD:
8621 case CPP_LSHIFT:
8622 case CPP_RSHIFT:
8623 case CPP_LESS:
8624 case CPP_GREATER:
8625 case CPP_LESS_EQ:
8626 case CPP_GREATER_EQ:
8627 case CPP_EQ_EQ:
8628 case CPP_NOT_EQ:
8629 case CPP_EQ:
8630 case CPP_MULT_EQ:
8631 case CPP_DIV_EQ:
8632 case CPP_MOD_EQ:
8633 case CPP_PLUS_EQ:
8634 case CPP_MINUS_EQ:
8635 case CPP_RSHIFT_EQ:
8636 case CPP_LSHIFT_EQ:
8637 case CPP_AND_EQ:
8638 case CPP_XOR_EQ:
8639 case CPP_OR_EQ:
8640 case CPP_XOR:
8641 case CPP_OR:
8642 case CPP_OR_OR:
8643 case CPP_EOF:
8644 case CPP_ELLIPSIS:
8645 return 0;
8646
8647 case CPP_OPEN_PAREN:
8648 /* In ((type ()) () the last () isn't a valid cast-expression,
8649 so the whole must be parsed as postfix-expression. */
8650 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8651 != CPP_CLOSE_PAREN;
8652
8653 case CPP_OPEN_SQUARE:
8654 /* '[' may start a primary-expression in obj-c++ and in C++11,
8655 as a lambda-expression, eg, '(void)[]{}'. */
8656 if (cxx_dialect >= cxx11)
8657 return -1;
8658 return c_dialect_objc ();
8659
8660 case CPP_PLUS_PLUS:
8661 case CPP_MINUS_MINUS:
8662 /* '++' and '--' may or may not start a cast-expression:
8663
8664 struct T { void operator++(int); };
8665 void f() { (T())++; }
8666
8667 vs
8668
8669 int a;
8670 (int)++a; */
8671 return -1;
8672
8673 default:
8674 return 1;
8675 }
8676 }
8677
8678 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8679 in the order: const_cast, static_cast, reinterpret_cast.
8680
8681 Don't suggest dynamic_cast.
8682
8683 Return the first legal cast kind found, or NULL otherwise. */
8684
8685 static const char *
8686 get_cast_suggestion (tree dst_type, tree orig_expr)
8687 {
8688 tree trial;
8689
8690 /* Reuse the parser logic by attempting to build the various kinds of
8691 cast, with "complain" disabled.
8692 Identify the first such cast that is valid. */
8693
8694 /* Don't attempt to run such logic within template processing. */
8695 if (processing_template_decl)
8696 return NULL;
8697
8698 /* First try const_cast. */
8699 trial = build_const_cast (dst_type, orig_expr, tf_none);
8700 if (trial != error_mark_node)
8701 return "const_cast";
8702
8703 /* If that fails, try static_cast. */
8704 trial = build_static_cast (dst_type, orig_expr, tf_none);
8705 if (trial != error_mark_node)
8706 return "static_cast";
8707
8708 /* Finally, try reinterpret_cast. */
8709 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8710 if (trial != error_mark_node)
8711 return "reinterpret_cast";
8712
8713 /* No such cast possible. */
8714 return NULL;
8715 }
8716
8717 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8718 suggesting how to convert a C-style cast of the form:
8719
8720 (DST_TYPE)ORIG_EXPR
8721
8722 to a C++-style cast.
8723
8724 The primary range of RICHLOC is asssumed to be that of the original
8725 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8726 of the parens in the C-style cast. */
8727
8728 static void
8729 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8730 location_t close_paren_loc, tree orig_expr,
8731 tree dst_type)
8732 {
8733 /* This function is non-trivial, so bail out now if the warning isn't
8734 going to be emitted. */
8735 if (!warn_old_style_cast)
8736 return;
8737
8738 /* Try to find a legal C++ cast, trying them in order:
8739 const_cast, static_cast, reinterpret_cast. */
8740 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8741 if (!cast_suggestion)
8742 return;
8743
8744 /* Replace the open paren with "CAST_SUGGESTION<". */
8745 pretty_printer pp;
8746 pp_printf (&pp, "%s<", cast_suggestion);
8747 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8748
8749 /* Replace the close paren with "> (". */
8750 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8751
8752 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8753 rich_loc->add_fixit_insert_after (")");
8754 }
8755
8756
8757 /* Parse a cast-expression.
8758
8759 cast-expression:
8760 unary-expression
8761 ( type-id ) cast-expression
8762
8763 ADDRESS_P is true iff the unary-expression is appearing as the
8764 operand of the `&' operator. CAST_P is true if this expression is
8765 the target of a cast.
8766
8767 Returns a representation of the expression. */
8768
8769 static cp_expr
8770 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8771 bool decltype_p, cp_id_kind * pidk)
8772 {
8773 /* If it's a `(', then we might be looking at a cast. */
8774 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8775 {
8776 tree type = NULL_TREE;
8777 cp_expr expr (NULL_TREE);
8778 int cast_expression = 0;
8779 const char *saved_message;
8780
8781 /* There's no way to know yet whether or not this is a cast.
8782 For example, `(int (3))' is a unary-expression, while `(int)
8783 3' is a cast. So, we resort to parsing tentatively. */
8784 cp_parser_parse_tentatively (parser);
8785 /* Types may not be defined in a cast. */
8786 saved_message = parser->type_definition_forbidden_message;
8787 parser->type_definition_forbidden_message
8788 = G_("types may not be defined in casts");
8789 /* Consume the `('. */
8790 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8791 location_t open_paren_loc = open_paren->location;
8792 location_t close_paren_loc = UNKNOWN_LOCATION;
8793
8794 /* A very tricky bit is that `(struct S) { 3 }' is a
8795 compound-literal (which we permit in C++ as an extension).
8796 But, that construct is not a cast-expression -- it is a
8797 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8798 is legal; if the compound-literal were a cast-expression,
8799 you'd need an extra set of parentheses.) But, if we parse
8800 the type-id, and it happens to be a class-specifier, then we
8801 will commit to the parse at that point, because we cannot
8802 undo the action that is done when creating a new class. So,
8803 then we cannot back up and do a postfix-expression.
8804
8805 Another tricky case is the following (c++/29234):
8806
8807 struct S { void operator () (); };
8808
8809 void foo ()
8810 {
8811 ( S()() );
8812 }
8813
8814 As a type-id we parse the parenthesized S()() as a function
8815 returning a function, groktypename complains and we cannot
8816 back up in this case either.
8817
8818 Therefore, we scan ahead to the closing `)', and check to see
8819 if the tokens after the `)' can start a cast-expression. Otherwise
8820 we are dealing with an unary-expression, a postfix-expression
8821 or something else.
8822
8823 Yet another tricky case, in C++11, is the following (c++/54891):
8824
8825 (void)[]{};
8826
8827 The issue is that usually, besides the case of lambda-expressions,
8828 the parenthesized type-id cannot be followed by '[', and, eg, we
8829 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8830 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8831 we don't commit, we try a cast-expression, then an unary-expression.
8832
8833 Save tokens so that we can put them back. */
8834 cp_lexer_save_tokens (parser->lexer);
8835
8836 /* We may be looking at a cast-expression. */
8837 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8838 /*consume_paren=*/true))
8839 cast_expression
8840 = cp_parser_tokens_start_cast_expression (parser);
8841
8842 /* Roll back the tokens we skipped. */
8843 cp_lexer_rollback_tokens (parser->lexer);
8844 /* If we aren't looking at a cast-expression, simulate an error so
8845 that the call to cp_parser_error_occurred below returns true. */
8846 if (!cast_expression)
8847 cp_parser_simulate_error (parser);
8848 else
8849 {
8850 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8851 parser->in_type_id_in_expr_p = true;
8852 /* Look for the type-id. */
8853 type = cp_parser_type_id (parser);
8854 /* Look for the closing `)'. */
8855 cp_token *close_paren
8856 = cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8857 if (close_paren)
8858 close_paren_loc = close_paren->location;
8859 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8860 }
8861
8862 /* Restore the saved message. */
8863 parser->type_definition_forbidden_message = saved_message;
8864
8865 /* At this point this can only be either a cast or a
8866 parenthesized ctor such as `(T ())' that looks like a cast to
8867 function returning T. */
8868 if (!cp_parser_error_occurred (parser))
8869 {
8870 /* Only commit if the cast-expression doesn't start with
8871 '++', '--', or '[' in C++11. */
8872 if (cast_expression > 0)
8873 cp_parser_commit_to_topmost_tentative_parse (parser);
8874
8875 expr = cp_parser_cast_expression (parser,
8876 /*address_p=*/false,
8877 /*cast_p=*/true,
8878 /*decltype_p=*/false,
8879 pidk);
8880
8881 if (cp_parser_parse_definitely (parser))
8882 {
8883 /* Warn about old-style casts, if so requested. */
8884 if (warn_old_style_cast
8885 && !in_system_header_at (input_location)
8886 && !VOID_TYPE_P (type)
8887 && current_lang_name != lang_name_c)
8888 {
8889 gcc_rich_location rich_loc (input_location);
8890 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
8891 expr, type);
8892 warning_at_rich_loc (&rich_loc, OPT_Wold_style_cast,
8893 "use of old-style cast to %qT", type);
8894 }
8895
8896 /* Only type conversions to integral or enumeration types
8897 can be used in constant-expressions. */
8898 if (!cast_valid_in_integral_constant_expression_p (type)
8899 && cp_parser_non_integral_constant_expression (parser,
8900 NIC_CAST))
8901 return error_mark_node;
8902
8903 /* Perform the cast. */
8904 /* Make a location:
8905 (TYPE) EXPR
8906 ^~~~~~~~~~~
8907 with start==caret at the open paren, extending to the
8908 end of "expr". */
8909 location_t cast_loc = make_location (open_paren_loc,
8910 open_paren_loc,
8911 expr.get_finish ());
8912 expr = build_c_cast (cast_loc, type, expr);
8913 return expr;
8914 }
8915 }
8916 else
8917 cp_parser_abort_tentative_parse (parser);
8918 }
8919
8920 /* If we get here, then it's not a cast, so it must be a
8921 unary-expression. */
8922 return cp_parser_unary_expression (parser, pidk, address_p,
8923 cast_p, decltype_p);
8924 }
8925
8926 /* Parse a binary expression of the general form:
8927
8928 pm-expression:
8929 cast-expression
8930 pm-expression .* cast-expression
8931 pm-expression ->* cast-expression
8932
8933 multiplicative-expression:
8934 pm-expression
8935 multiplicative-expression * pm-expression
8936 multiplicative-expression / pm-expression
8937 multiplicative-expression % pm-expression
8938
8939 additive-expression:
8940 multiplicative-expression
8941 additive-expression + multiplicative-expression
8942 additive-expression - multiplicative-expression
8943
8944 shift-expression:
8945 additive-expression
8946 shift-expression << additive-expression
8947 shift-expression >> additive-expression
8948
8949 relational-expression:
8950 shift-expression
8951 relational-expression < shift-expression
8952 relational-expression > shift-expression
8953 relational-expression <= shift-expression
8954 relational-expression >= shift-expression
8955
8956 GNU Extension:
8957
8958 relational-expression:
8959 relational-expression <? shift-expression
8960 relational-expression >? shift-expression
8961
8962 equality-expression:
8963 relational-expression
8964 equality-expression == relational-expression
8965 equality-expression != relational-expression
8966
8967 and-expression:
8968 equality-expression
8969 and-expression & equality-expression
8970
8971 exclusive-or-expression:
8972 and-expression
8973 exclusive-or-expression ^ and-expression
8974
8975 inclusive-or-expression:
8976 exclusive-or-expression
8977 inclusive-or-expression | exclusive-or-expression
8978
8979 logical-and-expression:
8980 inclusive-or-expression
8981 logical-and-expression && inclusive-or-expression
8982
8983 logical-or-expression:
8984 logical-and-expression
8985 logical-or-expression || logical-and-expression
8986
8987 All these are implemented with a single function like:
8988
8989 binary-expression:
8990 simple-cast-expression
8991 binary-expression <token> binary-expression
8992
8993 CAST_P is true if this expression is the target of a cast.
8994
8995 The binops_by_token map is used to get the tree codes for each <token> type.
8996 binary-expressions are associated according to a precedence table. */
8997
8998 #define TOKEN_PRECEDENCE(token) \
8999 (((token->type == CPP_GREATER \
9000 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9001 && !parser->greater_than_is_operator_p) \
9002 ? PREC_NOT_OPERATOR \
9003 : binops_by_token[token->type].prec)
9004
9005 static cp_expr
9006 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9007 bool no_toplevel_fold_p,
9008 bool decltype_p,
9009 enum cp_parser_prec prec,
9010 cp_id_kind * pidk)
9011 {
9012 cp_parser_expression_stack stack;
9013 cp_parser_expression_stack_entry *sp = &stack[0];
9014 cp_parser_expression_stack_entry current;
9015 cp_expr rhs;
9016 cp_token *token;
9017 enum tree_code rhs_type;
9018 enum cp_parser_prec new_prec, lookahead_prec;
9019 tree overload;
9020
9021 /* Parse the first expression. */
9022 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9023 ? TRUTH_NOT_EXPR : ERROR_MARK);
9024 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9025 cast_p, decltype_p, pidk);
9026 current.prec = prec;
9027
9028 if (cp_parser_error_occurred (parser))
9029 return error_mark_node;
9030
9031 for (;;)
9032 {
9033 /* Get an operator token. */
9034 token = cp_lexer_peek_token (parser->lexer);
9035
9036 if (warn_cxx11_compat
9037 && token->type == CPP_RSHIFT
9038 && !parser->greater_than_is_operator_p)
9039 {
9040 if (warning_at (token->location, OPT_Wc__11_compat,
9041 "%<>>%> operator is treated"
9042 " as two right angle brackets in C++11"))
9043 inform (token->location,
9044 "suggest parentheses around %<>>%> expression");
9045 }
9046
9047 new_prec = TOKEN_PRECEDENCE (token);
9048 if (new_prec != PREC_NOT_OPERATOR
9049 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9050 /* This is a fold-expression; handle it later. */
9051 new_prec = PREC_NOT_OPERATOR;
9052
9053 /* Popping an entry off the stack means we completed a subexpression:
9054 - either we found a token which is not an operator (`>' where it is not
9055 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9056 will happen repeatedly;
9057 - or, we found an operator which has lower priority. This is the case
9058 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9059 parsing `3 * 4'. */
9060 if (new_prec <= current.prec)
9061 {
9062 if (sp == stack)
9063 break;
9064 else
9065 goto pop;
9066 }
9067
9068 get_rhs:
9069 current.tree_type = binops_by_token[token->type].tree_type;
9070 current.loc = token->location;
9071
9072 /* We used the operator token. */
9073 cp_lexer_consume_token (parser->lexer);
9074
9075 /* For "false && x" or "true || x", x will never be executed;
9076 disable warnings while evaluating it. */
9077 if (current.tree_type == TRUTH_ANDIF_EXPR)
9078 c_inhibit_evaluation_warnings +=
9079 cp_fully_fold (current.lhs) == truthvalue_false_node;
9080 else if (current.tree_type == TRUTH_ORIF_EXPR)
9081 c_inhibit_evaluation_warnings +=
9082 cp_fully_fold (current.lhs) == truthvalue_true_node;
9083
9084 /* Extract another operand. It may be the RHS of this expression
9085 or the LHS of a new, higher priority expression. */
9086 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9087 ? TRUTH_NOT_EXPR : ERROR_MARK);
9088 rhs = cp_parser_simple_cast_expression (parser);
9089
9090 /* Get another operator token. Look up its precedence to avoid
9091 building a useless (immediately popped) stack entry for common
9092 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9093 token = cp_lexer_peek_token (parser->lexer);
9094 lookahead_prec = TOKEN_PRECEDENCE (token);
9095 if (lookahead_prec != PREC_NOT_OPERATOR
9096 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9097 lookahead_prec = PREC_NOT_OPERATOR;
9098 if (lookahead_prec > new_prec)
9099 {
9100 /* ... and prepare to parse the RHS of the new, higher priority
9101 expression. Since precedence levels on the stack are
9102 monotonically increasing, we do not have to care about
9103 stack overflows. */
9104 *sp = current;
9105 ++sp;
9106 current.lhs = rhs;
9107 current.lhs_type = rhs_type;
9108 current.prec = new_prec;
9109 new_prec = lookahead_prec;
9110 goto get_rhs;
9111
9112 pop:
9113 lookahead_prec = new_prec;
9114 /* If the stack is not empty, we have parsed into LHS the right side
9115 (`4' in the example above) of an expression we had suspended.
9116 We can use the information on the stack to recover the LHS (`3')
9117 from the stack together with the tree code (`MULT_EXPR'), and
9118 the precedence of the higher level subexpression
9119 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9120 which will be used to actually build the additive expression. */
9121 rhs = current.lhs;
9122 rhs_type = current.lhs_type;
9123 --sp;
9124 current = *sp;
9125 }
9126
9127 /* Undo the disabling of warnings done above. */
9128 if (current.tree_type == TRUTH_ANDIF_EXPR)
9129 c_inhibit_evaluation_warnings -=
9130 cp_fully_fold (current.lhs) == truthvalue_false_node;
9131 else if (current.tree_type == TRUTH_ORIF_EXPR)
9132 c_inhibit_evaluation_warnings -=
9133 cp_fully_fold (current.lhs) == truthvalue_true_node;
9134
9135 if (warn_logical_not_paren
9136 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9137 && current.lhs_type == TRUTH_NOT_EXPR
9138 /* Avoid warning for !!x == y. */
9139 && (TREE_CODE (current.lhs) != NE_EXPR
9140 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9141 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9142 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9143 /* Avoid warning for !b == y where b is boolean. */
9144 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9145 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9146 != BOOLEAN_TYPE))))
9147 /* Avoid warning for !!b == y where b is boolean. */
9148 && (!DECL_P (current.lhs)
9149 || TREE_TYPE (current.lhs) == NULL_TREE
9150 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9151 warn_logical_not_parentheses (current.loc, current.tree_type,
9152 current.lhs, maybe_constant_value (rhs));
9153
9154 overload = NULL;
9155
9156 location_t combined_loc = make_location (current.loc,
9157 current.lhs.get_start (),
9158 rhs.get_finish ());
9159
9160 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9161 ERROR_MARK for everything that is not a binary expression.
9162 This makes warn_about_parentheses miss some warnings that
9163 involve unary operators. For unary expressions we should
9164 pass the correct tree_code unless the unary expression was
9165 surrounded by parentheses.
9166 */
9167 if (no_toplevel_fold_p
9168 && lookahead_prec <= current.prec
9169 && sp == stack)
9170 current.lhs = build2_loc (combined_loc,
9171 current.tree_type,
9172 TREE_CODE_CLASS (current.tree_type)
9173 == tcc_comparison
9174 ? boolean_type_node : TREE_TYPE (current.lhs),
9175 current.lhs, rhs);
9176 else
9177 {
9178 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9179 current.lhs, current.lhs_type,
9180 rhs, rhs_type, &overload,
9181 complain_flags (decltype_p));
9182 /* TODO: build_x_binary_op doesn't always honor the location. */
9183 current.lhs.set_location (combined_loc);
9184 }
9185 current.lhs_type = current.tree_type;
9186
9187 /* If the binary operator required the use of an overloaded operator,
9188 then this expression cannot be an integral constant-expression.
9189 An overloaded operator can be used even if both operands are
9190 otherwise permissible in an integral constant-expression if at
9191 least one of the operands is of enumeration type. */
9192
9193 if (overload
9194 && cp_parser_non_integral_constant_expression (parser,
9195 NIC_OVERLOADED))
9196 return error_mark_node;
9197 }
9198
9199 return current.lhs;
9200 }
9201
9202 static cp_expr
9203 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9204 bool no_toplevel_fold_p,
9205 enum cp_parser_prec prec,
9206 cp_id_kind * pidk)
9207 {
9208 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9209 /*decltype*/false, prec, pidk);
9210 }
9211
9212 /* Parse the `? expression : assignment-expression' part of a
9213 conditional-expression. The LOGICAL_OR_EXPR is the
9214 logical-or-expression that started the conditional-expression.
9215 Returns a representation of the entire conditional-expression.
9216
9217 This routine is used by cp_parser_assignment_expression.
9218
9219 ? expression : assignment-expression
9220
9221 GNU Extensions:
9222
9223 ? : assignment-expression */
9224
9225 static tree
9226 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9227 {
9228 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9229 cp_expr assignment_expr;
9230 struct cp_token *token;
9231 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9232
9233 /* Consume the `?' token. */
9234 cp_lexer_consume_token (parser->lexer);
9235 token = cp_lexer_peek_token (parser->lexer);
9236 if (cp_parser_allow_gnu_extensions_p (parser)
9237 && token->type == CPP_COLON)
9238 {
9239 pedwarn (token->location, OPT_Wpedantic,
9240 "ISO C++ does not allow ?: with omitted middle operand");
9241 /* Implicit true clause. */
9242 expr = NULL_TREE;
9243 c_inhibit_evaluation_warnings +=
9244 folded_logical_or_expr == truthvalue_true_node;
9245 warn_for_omitted_condop (token->location, logical_or_expr);
9246 }
9247 else
9248 {
9249 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9250 parser->colon_corrects_to_scope_p = false;
9251 /* Parse the expression. */
9252 c_inhibit_evaluation_warnings +=
9253 folded_logical_or_expr == truthvalue_false_node;
9254 expr = cp_parser_expression (parser);
9255 c_inhibit_evaluation_warnings +=
9256 ((folded_logical_or_expr == truthvalue_true_node)
9257 - (folded_logical_or_expr == truthvalue_false_node));
9258 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9259 }
9260
9261 /* The next token should be a `:'. */
9262 cp_parser_require (parser, CPP_COLON, RT_COLON);
9263 /* Parse the assignment-expression. */
9264 assignment_expr = cp_parser_assignment_expression (parser);
9265 c_inhibit_evaluation_warnings -=
9266 folded_logical_or_expr == truthvalue_true_node;
9267
9268 /* Make a location:
9269 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9270 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9271 with the caret at the "?", ranging from the start of
9272 the logical_or_expr to the end of the assignment_expr. */
9273 loc = make_location (loc,
9274 logical_or_expr.get_start (),
9275 assignment_expr.get_finish ());
9276
9277 /* Build the conditional-expression. */
9278 return build_x_conditional_expr (loc, logical_or_expr,
9279 expr,
9280 assignment_expr,
9281 tf_warning_or_error);
9282 }
9283
9284 /* Parse an assignment-expression.
9285
9286 assignment-expression:
9287 conditional-expression
9288 logical-or-expression assignment-operator assignment_expression
9289 throw-expression
9290
9291 CAST_P is true if this expression is the target of a cast.
9292 DECLTYPE_P is true if this expression is the operand of decltype.
9293
9294 Returns a representation for the expression. */
9295
9296 static cp_expr
9297 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9298 bool cast_p, bool decltype_p)
9299 {
9300 cp_expr expr;
9301
9302 /* If the next token is the `throw' keyword, then we're looking at
9303 a throw-expression. */
9304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9305 expr = cp_parser_throw_expression (parser);
9306 /* Otherwise, it must be that we are looking at a
9307 logical-or-expression. */
9308 else
9309 {
9310 /* Parse the binary expressions (logical-or-expression). */
9311 expr = cp_parser_binary_expression (parser, cast_p, false,
9312 decltype_p,
9313 PREC_NOT_OPERATOR, pidk);
9314 /* If the next token is a `?' then we're actually looking at a
9315 conditional-expression. */
9316 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9317 return cp_parser_question_colon_clause (parser, expr);
9318 else
9319 {
9320 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9321
9322 /* If it's an assignment-operator, we're using the second
9323 production. */
9324 enum tree_code assignment_operator
9325 = cp_parser_assignment_operator_opt (parser);
9326 if (assignment_operator != ERROR_MARK)
9327 {
9328 bool non_constant_p;
9329
9330 /* Parse the right-hand side of the assignment. */
9331 cp_expr rhs = cp_parser_initializer_clause (parser,
9332 &non_constant_p);
9333
9334 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9335 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9336
9337 /* An assignment may not appear in a
9338 constant-expression. */
9339 if (cp_parser_non_integral_constant_expression (parser,
9340 NIC_ASSIGNMENT))
9341 return error_mark_node;
9342 /* Build the assignment expression. Its default
9343 location:
9344 LHS = RHS
9345 ~~~~^~~~~
9346 is the location of the '=' token as the
9347 caret, ranging from the start of the lhs to the
9348 end of the rhs. */
9349 loc = make_location (loc,
9350 expr.get_start (),
9351 rhs.get_finish ());
9352 expr = build_x_modify_expr (loc, expr,
9353 assignment_operator,
9354 rhs,
9355 complain_flags (decltype_p));
9356 /* TODO: build_x_modify_expr doesn't honor the location,
9357 so we must set it here. */
9358 expr.set_location (loc);
9359 }
9360 }
9361 }
9362
9363 return expr;
9364 }
9365
9366 /* Parse an (optional) assignment-operator.
9367
9368 assignment-operator: one of
9369 = *= /= %= += -= >>= <<= &= ^= |=
9370
9371 GNU Extension:
9372
9373 assignment-operator: one of
9374 <?= >?=
9375
9376 If the next token is an assignment operator, the corresponding tree
9377 code is returned, and the token is consumed. For example, for
9378 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9379 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9380 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9381 operator, ERROR_MARK is returned. */
9382
9383 static enum tree_code
9384 cp_parser_assignment_operator_opt (cp_parser* parser)
9385 {
9386 enum tree_code op;
9387 cp_token *token;
9388
9389 /* Peek at the next token. */
9390 token = cp_lexer_peek_token (parser->lexer);
9391
9392 switch (token->type)
9393 {
9394 case CPP_EQ:
9395 op = NOP_EXPR;
9396 break;
9397
9398 case CPP_MULT_EQ:
9399 op = MULT_EXPR;
9400 break;
9401
9402 case CPP_DIV_EQ:
9403 op = TRUNC_DIV_EXPR;
9404 break;
9405
9406 case CPP_MOD_EQ:
9407 op = TRUNC_MOD_EXPR;
9408 break;
9409
9410 case CPP_PLUS_EQ:
9411 op = PLUS_EXPR;
9412 break;
9413
9414 case CPP_MINUS_EQ:
9415 op = MINUS_EXPR;
9416 break;
9417
9418 case CPP_RSHIFT_EQ:
9419 op = RSHIFT_EXPR;
9420 break;
9421
9422 case CPP_LSHIFT_EQ:
9423 op = LSHIFT_EXPR;
9424 break;
9425
9426 case CPP_AND_EQ:
9427 op = BIT_AND_EXPR;
9428 break;
9429
9430 case CPP_XOR_EQ:
9431 op = BIT_XOR_EXPR;
9432 break;
9433
9434 case CPP_OR_EQ:
9435 op = BIT_IOR_EXPR;
9436 break;
9437
9438 default:
9439 /* Nothing else is an assignment operator. */
9440 op = ERROR_MARK;
9441 }
9442
9443 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9444 if (op != ERROR_MARK
9445 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9446 op = ERROR_MARK;
9447
9448 /* If it was an assignment operator, consume it. */
9449 if (op != ERROR_MARK)
9450 cp_lexer_consume_token (parser->lexer);
9451
9452 return op;
9453 }
9454
9455 /* Parse an expression.
9456
9457 expression:
9458 assignment-expression
9459 expression , assignment-expression
9460
9461 CAST_P is true if this expression is the target of a cast.
9462 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9463 except possibly parenthesized or on the RHS of a comma (N3276).
9464
9465 Returns a representation of the expression. */
9466
9467 static cp_expr
9468 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9469 bool cast_p, bool decltype_p)
9470 {
9471 cp_expr expression = NULL_TREE;
9472 location_t loc = UNKNOWN_LOCATION;
9473
9474 while (true)
9475 {
9476 cp_expr assignment_expression;
9477
9478 /* Parse the next assignment-expression. */
9479 assignment_expression
9480 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9481
9482 /* We don't create a temporary for a call that is the immediate operand
9483 of decltype or on the RHS of a comma. But when we see a comma, we
9484 need to create a temporary for a call on the LHS. */
9485 if (decltype_p && !processing_template_decl
9486 && TREE_CODE (assignment_expression) == CALL_EXPR
9487 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9488 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9489 assignment_expression
9490 = build_cplus_new (TREE_TYPE (assignment_expression),
9491 assignment_expression, tf_warning_or_error);
9492
9493 /* If this is the first assignment-expression, we can just
9494 save it away. */
9495 if (!expression)
9496 expression = assignment_expression;
9497 else
9498 {
9499 /* Create a location with caret at the comma, ranging
9500 from the start of the LHS to the end of the RHS. */
9501 loc = make_location (loc,
9502 expression.get_start (),
9503 assignment_expression.get_finish ());
9504 expression = build_x_compound_expr (loc, expression,
9505 assignment_expression,
9506 complain_flags (decltype_p));
9507 expression.set_location (loc);
9508 }
9509 /* If the next token is not a comma, or we're in a fold-expression, then
9510 we are done with the expression. */
9511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9512 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9513 break;
9514 /* Consume the `,'. */
9515 loc = cp_lexer_peek_token (parser->lexer)->location;
9516 cp_lexer_consume_token (parser->lexer);
9517 /* A comma operator cannot appear in a constant-expression. */
9518 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9519 expression = error_mark_node;
9520 }
9521
9522 return expression;
9523 }
9524
9525 /* Parse a constant-expression.
9526
9527 constant-expression:
9528 conditional-expression
9529
9530 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9531 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9532 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9533 is false, NON_CONSTANT_P should be NULL. */
9534
9535 static cp_expr
9536 cp_parser_constant_expression (cp_parser* parser,
9537 bool allow_non_constant_p,
9538 bool *non_constant_p)
9539 {
9540 bool saved_integral_constant_expression_p;
9541 bool saved_allow_non_integral_constant_expression_p;
9542 bool saved_non_integral_constant_expression_p;
9543 cp_expr expression;
9544
9545 /* It might seem that we could simply parse the
9546 conditional-expression, and then check to see if it were
9547 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9548 one that the compiler can figure out is constant, possibly after
9549 doing some simplifications or optimizations. The standard has a
9550 precise definition of constant-expression, and we must honor
9551 that, even though it is somewhat more restrictive.
9552
9553 For example:
9554
9555 int i[(2, 3)];
9556
9557 is not a legal declaration, because `(2, 3)' is not a
9558 constant-expression. The `,' operator is forbidden in a
9559 constant-expression. However, GCC's constant-folding machinery
9560 will fold this operation to an INTEGER_CST for `3'. */
9561
9562 /* Save the old settings. */
9563 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9564 saved_allow_non_integral_constant_expression_p
9565 = parser->allow_non_integral_constant_expression_p;
9566 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9567 /* We are now parsing a constant-expression. */
9568 parser->integral_constant_expression_p = true;
9569 parser->allow_non_integral_constant_expression_p
9570 = (allow_non_constant_p || cxx_dialect >= cxx11);
9571 parser->non_integral_constant_expression_p = false;
9572 /* Although the grammar says "conditional-expression", we parse an
9573 "assignment-expression", which also permits "throw-expression"
9574 and the use of assignment operators. In the case that
9575 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9576 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9577 actually essential that we look for an assignment-expression.
9578 For example, cp_parser_initializer_clauses uses this function to
9579 determine whether a particular assignment-expression is in fact
9580 constant. */
9581 expression = cp_parser_assignment_expression (parser);
9582 /* Restore the old settings. */
9583 parser->integral_constant_expression_p
9584 = saved_integral_constant_expression_p;
9585 parser->allow_non_integral_constant_expression_p
9586 = saved_allow_non_integral_constant_expression_p;
9587 if (cxx_dialect >= cxx11)
9588 {
9589 /* Require an rvalue constant expression here; that's what our
9590 callers expect. Reference constant expressions are handled
9591 separately in e.g. cp_parser_template_argument. */
9592 tree decay = expression;
9593 if (TREE_TYPE (expression)
9594 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9595 decay = build_address (expression);
9596 bool is_const = potential_rvalue_constant_expression (decay);
9597 parser->non_integral_constant_expression_p = !is_const;
9598 if (!is_const && !allow_non_constant_p)
9599 require_potential_rvalue_constant_expression (decay);
9600 }
9601 if (allow_non_constant_p)
9602 *non_constant_p = parser->non_integral_constant_expression_p;
9603 parser->non_integral_constant_expression_p
9604 = saved_non_integral_constant_expression_p;
9605
9606 return expression;
9607 }
9608
9609 /* Parse __builtin_offsetof.
9610
9611 offsetof-expression:
9612 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9613
9614 offsetof-member-designator:
9615 id-expression
9616 | offsetof-member-designator "." id-expression
9617 | offsetof-member-designator "[" expression "]"
9618 | offsetof-member-designator "->" id-expression */
9619
9620 static cp_expr
9621 cp_parser_builtin_offsetof (cp_parser *parser)
9622 {
9623 int save_ice_p, save_non_ice_p;
9624 tree type;
9625 cp_expr expr;
9626 cp_id_kind dummy;
9627 cp_token *token;
9628 location_t finish_loc;
9629
9630 /* We're about to accept non-integral-constant things, but will
9631 definitely yield an integral constant expression. Save and
9632 restore these values around our local parsing. */
9633 save_ice_p = parser->integral_constant_expression_p;
9634 save_non_ice_p = parser->non_integral_constant_expression_p;
9635
9636 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9637
9638 /* Consume the "__builtin_offsetof" token. */
9639 cp_lexer_consume_token (parser->lexer);
9640 /* Consume the opening `('. */
9641 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9642 /* Parse the type-id. */
9643 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9644 type = cp_parser_type_id (parser);
9645 /* Look for the `,'. */
9646 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9647 token = cp_lexer_peek_token (parser->lexer);
9648
9649 /* Build the (type *)null that begins the traditional offsetof macro. */
9650 tree object_ptr
9651 = build_static_cast (build_pointer_type (type), null_pointer_node,
9652 tf_warning_or_error);
9653
9654 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9655 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9656 true, &dummy, token->location);
9657 while (true)
9658 {
9659 token = cp_lexer_peek_token (parser->lexer);
9660 switch (token->type)
9661 {
9662 case CPP_OPEN_SQUARE:
9663 /* offsetof-member-designator "[" expression "]" */
9664 expr = cp_parser_postfix_open_square_expression (parser, expr,
9665 true, false);
9666 break;
9667
9668 case CPP_DEREF:
9669 /* offsetof-member-designator "->" identifier */
9670 expr = grok_array_decl (token->location, expr,
9671 integer_zero_node, false);
9672 /* FALLTHRU */
9673
9674 case CPP_DOT:
9675 /* offsetof-member-designator "." identifier */
9676 cp_lexer_consume_token (parser->lexer);
9677 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9678 expr, true, &dummy,
9679 token->location);
9680 break;
9681
9682 case CPP_CLOSE_PAREN:
9683 /* Consume the ")" token. */
9684 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9685 cp_lexer_consume_token (parser->lexer);
9686 goto success;
9687
9688 default:
9689 /* Error. We know the following require will fail, but
9690 that gives the proper error message. */
9691 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9692 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9693 expr = error_mark_node;
9694 goto failure;
9695 }
9696 }
9697
9698 success:
9699 /* Make a location of the form:
9700 __builtin_offsetof (struct s, f)
9701 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9702 with caret at the type-id, ranging from the start of the
9703 "_builtin_offsetof" token to the close paren. */
9704 loc = make_location (loc, start_loc, finish_loc);
9705 /* The result will be an INTEGER_CST, so we need to explicitly
9706 preserve the location. */
9707 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9708
9709 failure:
9710 parser->integral_constant_expression_p = save_ice_p;
9711 parser->non_integral_constant_expression_p = save_non_ice_p;
9712
9713 return expr;
9714 }
9715
9716 /* Parse a trait expression.
9717
9718 Returns a representation of the expression, the underlying type
9719 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9720
9721 static tree
9722 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9723 {
9724 cp_trait_kind kind;
9725 tree type1, type2 = NULL_TREE;
9726 bool binary = false;
9727 bool variadic = false;
9728
9729 switch (keyword)
9730 {
9731 case RID_HAS_NOTHROW_ASSIGN:
9732 kind = CPTK_HAS_NOTHROW_ASSIGN;
9733 break;
9734 case RID_HAS_NOTHROW_CONSTRUCTOR:
9735 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9736 break;
9737 case RID_HAS_NOTHROW_COPY:
9738 kind = CPTK_HAS_NOTHROW_COPY;
9739 break;
9740 case RID_HAS_TRIVIAL_ASSIGN:
9741 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9742 break;
9743 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9744 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9745 break;
9746 case RID_HAS_TRIVIAL_COPY:
9747 kind = CPTK_HAS_TRIVIAL_COPY;
9748 break;
9749 case RID_HAS_TRIVIAL_DESTRUCTOR:
9750 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9751 break;
9752 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9753 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9754 break;
9755 case RID_HAS_VIRTUAL_DESTRUCTOR:
9756 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9757 break;
9758 case RID_IS_ABSTRACT:
9759 kind = CPTK_IS_ABSTRACT;
9760 break;
9761 case RID_IS_AGGREGATE:
9762 kind = CPTK_IS_AGGREGATE;
9763 break;
9764 case RID_IS_BASE_OF:
9765 kind = CPTK_IS_BASE_OF;
9766 binary = true;
9767 break;
9768 case RID_IS_CLASS:
9769 kind = CPTK_IS_CLASS;
9770 break;
9771 case RID_IS_EMPTY:
9772 kind = CPTK_IS_EMPTY;
9773 break;
9774 case RID_IS_ENUM:
9775 kind = CPTK_IS_ENUM;
9776 break;
9777 case RID_IS_FINAL:
9778 kind = CPTK_IS_FINAL;
9779 break;
9780 case RID_IS_LITERAL_TYPE:
9781 kind = CPTK_IS_LITERAL_TYPE;
9782 break;
9783 case RID_IS_POD:
9784 kind = CPTK_IS_POD;
9785 break;
9786 case RID_IS_POLYMORPHIC:
9787 kind = CPTK_IS_POLYMORPHIC;
9788 break;
9789 case RID_IS_SAME_AS:
9790 kind = CPTK_IS_SAME_AS;
9791 binary = true;
9792 break;
9793 case RID_IS_STD_LAYOUT:
9794 kind = CPTK_IS_STD_LAYOUT;
9795 break;
9796 case RID_IS_TRIVIAL:
9797 kind = CPTK_IS_TRIVIAL;
9798 break;
9799 case RID_IS_TRIVIALLY_ASSIGNABLE:
9800 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9801 binary = true;
9802 break;
9803 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9804 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9805 variadic = true;
9806 break;
9807 case RID_IS_TRIVIALLY_COPYABLE:
9808 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9809 break;
9810 case RID_IS_UNION:
9811 kind = CPTK_IS_UNION;
9812 break;
9813 case RID_UNDERLYING_TYPE:
9814 kind = CPTK_UNDERLYING_TYPE;
9815 break;
9816 case RID_BASES:
9817 kind = CPTK_BASES;
9818 break;
9819 case RID_DIRECT_BASES:
9820 kind = CPTK_DIRECT_BASES;
9821 break;
9822 case RID_IS_ASSIGNABLE:
9823 kind = CPTK_IS_ASSIGNABLE;
9824 binary = true;
9825 break;
9826 case RID_IS_CONSTRUCTIBLE:
9827 kind = CPTK_IS_CONSTRUCTIBLE;
9828 variadic = true;
9829 break;
9830 default:
9831 gcc_unreachable ();
9832 }
9833
9834 /* Consume the token. */
9835 cp_lexer_consume_token (parser->lexer);
9836
9837 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9838
9839 {
9840 type_id_in_expr_sentinel s (parser);
9841 type1 = cp_parser_type_id (parser);
9842 }
9843
9844 if (type1 == error_mark_node)
9845 return error_mark_node;
9846
9847 if (binary)
9848 {
9849 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9850
9851 {
9852 type_id_in_expr_sentinel s (parser);
9853 type2 = cp_parser_type_id (parser);
9854 }
9855
9856 if (type2 == error_mark_node)
9857 return error_mark_node;
9858 }
9859 else if (variadic)
9860 {
9861 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9862 {
9863 cp_lexer_consume_token (parser->lexer);
9864 tree elt = cp_parser_type_id (parser);
9865 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9866 {
9867 cp_lexer_consume_token (parser->lexer);
9868 elt = make_pack_expansion (elt);
9869 }
9870 if (elt == error_mark_node)
9871 return error_mark_node;
9872 type2 = tree_cons (NULL_TREE, elt, type2);
9873 }
9874 }
9875
9876 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9877
9878 /* Complete the trait expression, which may mean either processing
9879 the trait expr now or saving it for template instantiation. */
9880 switch (kind)
9881 {
9882 case CPTK_UNDERLYING_TYPE:
9883 return finish_underlying_type (type1);
9884 case CPTK_BASES:
9885 return finish_bases (type1, false);
9886 case CPTK_DIRECT_BASES:
9887 return finish_bases (type1, true);
9888 default:
9889 return finish_trait_expr (kind, type1, type2);
9890 }
9891 }
9892
9893 /* Lambdas that appear in variable initializer or default argument scope
9894 get that in their mangling, so we need to record it. We might as well
9895 use the count for function and namespace scopes as well. */
9896 static GTY(()) tree lambda_scope;
9897 static GTY(()) int lambda_count;
9898 struct GTY(()) tree_int
9899 {
9900 tree t;
9901 int i;
9902 };
9903 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9904
9905 static void
9906 start_lambda_scope (tree decl)
9907 {
9908 tree_int ti;
9909 gcc_assert (decl);
9910 /* Once we're inside a function, we ignore other scopes and just push
9911 the function again so that popping works properly. */
9912 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9913 decl = current_function_decl;
9914 ti.t = lambda_scope;
9915 ti.i = lambda_count;
9916 vec_safe_push (lambda_scope_stack, ti);
9917 if (lambda_scope != decl)
9918 {
9919 /* Don't reset the count if we're still in the same function. */
9920 lambda_scope = decl;
9921 lambda_count = 0;
9922 }
9923 }
9924
9925 static void
9926 record_lambda_scope (tree lambda)
9927 {
9928 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9929 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9930 }
9931
9932 static void
9933 finish_lambda_scope (void)
9934 {
9935 tree_int *p = &lambda_scope_stack->last ();
9936 if (lambda_scope != p->t)
9937 {
9938 lambda_scope = p->t;
9939 lambda_count = p->i;
9940 }
9941 lambda_scope_stack->pop ();
9942 }
9943
9944 /* Parse a lambda expression.
9945
9946 lambda-expression:
9947 lambda-introducer lambda-declarator [opt] compound-statement
9948
9949 Returns a representation of the expression. */
9950
9951 static cp_expr
9952 cp_parser_lambda_expression (cp_parser* parser)
9953 {
9954 tree lambda_expr = build_lambda_expr ();
9955 tree type;
9956 bool ok = true;
9957 cp_token *token = cp_lexer_peek_token (parser->lexer);
9958 cp_token_position start = 0;
9959
9960 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9961
9962 if (cp_unevaluated_operand)
9963 {
9964 if (!token->error_reported)
9965 {
9966 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9967 "lambda-expression in unevaluated context");
9968 token->error_reported = true;
9969 }
9970 ok = false;
9971 }
9972 else if (parser->in_template_argument_list_p)
9973 {
9974 if (!token->error_reported)
9975 {
9976 error_at (token->location, "lambda-expression in template-argument");
9977 token->error_reported = true;
9978 }
9979 ok = false;
9980 }
9981
9982 /* We may be in the middle of deferred access check. Disable
9983 it now. */
9984 push_deferring_access_checks (dk_no_deferred);
9985
9986 cp_parser_lambda_introducer (parser, lambda_expr);
9987
9988 type = begin_lambda_type (lambda_expr);
9989 if (type == error_mark_node)
9990 return error_mark_node;
9991
9992 record_lambda_scope (lambda_expr);
9993
9994 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9995 determine_visibility (TYPE_NAME (type));
9996
9997 /* Now that we've started the type, add the capture fields for any
9998 explicit captures. */
9999 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10000
10001 {
10002 /* Inside the class, surrounding template-parameter-lists do not apply. */
10003 unsigned int saved_num_template_parameter_lists
10004 = parser->num_template_parameter_lists;
10005 unsigned char in_statement = parser->in_statement;
10006 bool in_switch_statement_p = parser->in_switch_statement_p;
10007 bool fully_implicit_function_template_p
10008 = parser->fully_implicit_function_template_p;
10009 tree implicit_template_parms = parser->implicit_template_parms;
10010 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10011 bool auto_is_implicit_function_template_parm_p
10012 = parser->auto_is_implicit_function_template_parm_p;
10013
10014 parser->num_template_parameter_lists = 0;
10015 parser->in_statement = 0;
10016 parser->in_switch_statement_p = false;
10017 parser->fully_implicit_function_template_p = false;
10018 parser->implicit_template_parms = 0;
10019 parser->implicit_template_scope = 0;
10020 parser->auto_is_implicit_function_template_parm_p = false;
10021
10022 /* By virtue of defining a local class, a lambda expression has access to
10023 the private variables of enclosing classes. */
10024
10025 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10026
10027 if (ok && cp_parser_error_occurred (parser))
10028 ok = false;
10029
10030 if (ok)
10031 {
10032 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10033 && cp_parser_start_tentative_firewall (parser))
10034 start = token;
10035 cp_parser_lambda_body (parser, lambda_expr);
10036 }
10037 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10038 {
10039 if (cp_parser_skip_to_closing_brace (parser))
10040 cp_lexer_consume_token (parser->lexer);
10041 }
10042
10043 /* The capture list was built up in reverse order; fix that now. */
10044 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10045 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10046
10047 if (ok)
10048 maybe_add_lambda_conv_op (type);
10049
10050 type = finish_struct (type, /*attributes=*/NULL_TREE);
10051
10052 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10053 parser->in_statement = in_statement;
10054 parser->in_switch_statement_p = in_switch_statement_p;
10055 parser->fully_implicit_function_template_p
10056 = fully_implicit_function_template_p;
10057 parser->implicit_template_parms = implicit_template_parms;
10058 parser->implicit_template_scope = implicit_template_scope;
10059 parser->auto_is_implicit_function_template_parm_p
10060 = auto_is_implicit_function_template_parm_p;
10061 }
10062
10063 /* This field is only used during parsing of the lambda. */
10064 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10065
10066 /* This lambda shouldn't have any proxies left at this point. */
10067 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10068 /* And now that we're done, push proxies for an enclosing lambda. */
10069 insert_pending_capture_proxies ();
10070
10071 if (ok)
10072 lambda_expr = build_lambda_object (lambda_expr);
10073 else
10074 lambda_expr = error_mark_node;
10075
10076 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10077
10078 pop_deferring_access_checks ();
10079
10080 return lambda_expr;
10081 }
10082
10083 /* Parse the beginning of a lambda expression.
10084
10085 lambda-introducer:
10086 [ lambda-capture [opt] ]
10087
10088 LAMBDA_EXPR is the current representation of the lambda expression. */
10089
10090 static void
10091 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10092 {
10093 /* Need commas after the first capture. */
10094 bool first = true;
10095
10096 /* Eat the leading `['. */
10097 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10098
10099 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10100 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10101 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10102 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10103 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10104 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10105
10106 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10107 {
10108 cp_lexer_consume_token (parser->lexer);
10109 first = false;
10110 }
10111
10112 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10113 {
10114 cp_token* capture_token;
10115 tree capture_id;
10116 tree capture_init_expr;
10117 cp_id_kind idk = CP_ID_KIND_NONE;
10118 bool explicit_init_p = false;
10119
10120 enum capture_kind_type
10121 {
10122 BY_COPY,
10123 BY_REFERENCE
10124 };
10125 enum capture_kind_type capture_kind = BY_COPY;
10126
10127 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10128 {
10129 error ("expected end of capture-list");
10130 return;
10131 }
10132
10133 if (first)
10134 first = false;
10135 else
10136 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10137
10138 /* Possibly capture `this'. */
10139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10140 {
10141 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10142 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10143 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10144 "with by-copy capture default");
10145 cp_lexer_consume_token (parser->lexer);
10146 add_capture (lambda_expr,
10147 /*id=*/this_identifier,
10148 /*initializer=*/finish_this_expr (),
10149 /*by_reference_p=*/true,
10150 explicit_init_p);
10151 continue;
10152 }
10153
10154 /* Possibly capture `*this'. */
10155 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10156 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10157 {
10158 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10159 if (cxx_dialect < cxx1z)
10160 pedwarn (loc, 0, "%<*this%> capture only available with "
10161 "-std=c++1z or -std=gnu++1z");
10162 cp_lexer_consume_token (parser->lexer);
10163 cp_lexer_consume_token (parser->lexer);
10164 add_capture (lambda_expr,
10165 /*id=*/this_identifier,
10166 /*initializer=*/finish_this_expr (),
10167 /*by_reference_p=*/false,
10168 explicit_init_p);
10169 continue;
10170 }
10171
10172 /* Remember whether we want to capture as a reference or not. */
10173 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10174 {
10175 capture_kind = BY_REFERENCE;
10176 cp_lexer_consume_token (parser->lexer);
10177 }
10178
10179 /* Get the identifier. */
10180 capture_token = cp_lexer_peek_token (parser->lexer);
10181 capture_id = cp_parser_identifier (parser);
10182
10183 if (capture_id == error_mark_node)
10184 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10185 delimiters, but I modified this to stop on unnested ']' as well. It
10186 was already changed to stop on unnested '}', so the
10187 "closing_parenthesis" name is no more misleading with my change. */
10188 {
10189 cp_parser_skip_to_closing_parenthesis (parser,
10190 /*recovering=*/true,
10191 /*or_comma=*/true,
10192 /*consume_paren=*/true);
10193 break;
10194 }
10195
10196 /* Find the initializer for this capture. */
10197 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10198 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10199 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10200 {
10201 bool direct, non_constant;
10202 /* An explicit initializer exists. */
10203 if (cxx_dialect < cxx14)
10204 pedwarn (input_location, 0,
10205 "lambda capture initializers "
10206 "only available with -std=c++14 or -std=gnu++14");
10207 capture_init_expr = cp_parser_initializer (parser, &direct,
10208 &non_constant);
10209 explicit_init_p = true;
10210 if (capture_init_expr == NULL_TREE)
10211 {
10212 error ("empty initializer for lambda init-capture");
10213 capture_init_expr = error_mark_node;
10214 }
10215 }
10216 else
10217 {
10218 const char* error_msg;
10219
10220 /* Turn the identifier into an id-expression. */
10221 capture_init_expr
10222 = cp_parser_lookup_name_simple (parser, capture_id,
10223 capture_token->location);
10224
10225 if (capture_init_expr == error_mark_node)
10226 {
10227 unqualified_name_lookup_error (capture_id);
10228 continue;
10229 }
10230 else if (DECL_P (capture_init_expr)
10231 && (!VAR_P (capture_init_expr)
10232 && TREE_CODE (capture_init_expr) != PARM_DECL))
10233 {
10234 error_at (capture_token->location,
10235 "capture of non-variable %qD ",
10236 capture_init_expr);
10237 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10238 "%q#D declared here", capture_init_expr);
10239 continue;
10240 }
10241 if (VAR_P (capture_init_expr)
10242 && decl_storage_duration (capture_init_expr) != dk_auto)
10243 {
10244 if (pedwarn (capture_token->location, 0, "capture of variable "
10245 "%qD with non-automatic storage duration",
10246 capture_init_expr))
10247 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10248 "%q#D declared here", capture_init_expr);
10249 continue;
10250 }
10251
10252 capture_init_expr
10253 = finish_id_expression
10254 (capture_id,
10255 capture_init_expr,
10256 parser->scope,
10257 &idk,
10258 /*integral_constant_expression_p=*/false,
10259 /*allow_non_integral_constant_expression_p=*/false,
10260 /*non_integral_constant_expression_p=*/NULL,
10261 /*template_p=*/false,
10262 /*done=*/true,
10263 /*address_p=*/false,
10264 /*template_arg_p=*/false,
10265 &error_msg,
10266 capture_token->location);
10267
10268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10269 {
10270 cp_lexer_consume_token (parser->lexer);
10271 capture_init_expr = make_pack_expansion (capture_init_expr);
10272 }
10273 else
10274 check_for_bare_parameter_packs (capture_init_expr);
10275 }
10276
10277 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10278 && !explicit_init_p)
10279 {
10280 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10281 && capture_kind == BY_COPY)
10282 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10283 "of %qD redundant with by-copy capture default",
10284 capture_id);
10285 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10286 && capture_kind == BY_REFERENCE)
10287 pedwarn (capture_token->location, 0, "explicit by-reference "
10288 "capture of %qD redundant with by-reference capture "
10289 "default", capture_id);
10290 }
10291
10292 add_capture (lambda_expr,
10293 capture_id,
10294 capture_init_expr,
10295 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10296 explicit_init_p);
10297 }
10298
10299 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10300 }
10301
10302 /* Parse the (optional) middle of a lambda expression.
10303
10304 lambda-declarator:
10305 < template-parameter-list [opt] >
10306 ( parameter-declaration-clause [opt] )
10307 attribute-specifier [opt]
10308 decl-specifier-seq [opt]
10309 exception-specification [opt]
10310 lambda-return-type-clause [opt]
10311
10312 LAMBDA_EXPR is the current representation of the lambda expression. */
10313
10314 static bool
10315 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10316 {
10317 /* 5.1.1.4 of the standard says:
10318 If a lambda-expression does not include a lambda-declarator, it is as if
10319 the lambda-declarator were ().
10320 This means an empty parameter list, no attributes, and no exception
10321 specification. */
10322 tree param_list = void_list_node;
10323 tree attributes = NULL_TREE;
10324 tree exception_spec = NULL_TREE;
10325 tree template_param_list = NULL_TREE;
10326 tree tx_qual = NULL_TREE;
10327 cp_decl_specifier_seq lambda_specs;
10328 clear_decl_specs (&lambda_specs);
10329
10330 /* The template-parameter-list is optional, but must begin with
10331 an opening angle if present. */
10332 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10333 {
10334 if (cxx_dialect < cxx14)
10335 pedwarn (parser->lexer->next_token->location, 0,
10336 "lambda templates are only available with "
10337 "-std=c++14 or -std=gnu++14");
10338 else
10339 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10340 "ISO C++ does not support lambda templates");
10341
10342 cp_lexer_consume_token (parser->lexer);
10343
10344 template_param_list = cp_parser_template_parameter_list (parser);
10345
10346 cp_parser_skip_to_end_of_template_parameter_list (parser);
10347
10348 /* We just processed one more parameter list. */
10349 ++parser->num_template_parameter_lists;
10350 }
10351
10352 /* The parameter-declaration-clause is optional (unless
10353 template-parameter-list was given), but must begin with an
10354 opening parenthesis if present. */
10355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10356 {
10357 cp_lexer_consume_token (parser->lexer);
10358
10359 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10360
10361 /* Parse parameters. */
10362 param_list = cp_parser_parameter_declaration_clause (parser);
10363
10364 /* Default arguments shall not be specified in the
10365 parameter-declaration-clause of a lambda-declarator. */
10366 if (cxx_dialect < cxx14)
10367 for (tree t = param_list; t; t = TREE_CHAIN (t))
10368 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10369 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10370 "default argument specified for lambda parameter");
10371
10372 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10373
10374 attributes = cp_parser_attributes_opt (parser);
10375
10376 /* In the decl-specifier-seq of the lambda-declarator, each
10377 decl-specifier shall either be mutable or constexpr. */
10378 int declares_class_or_enum;
10379 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10380 cp_parser_decl_specifier_seq (parser,
10381 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10382 &lambda_specs, &declares_class_or_enum);
10383 if (lambda_specs.storage_class == sc_mutable)
10384 {
10385 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10386 if (lambda_specs.conflicting_specifiers_p)
10387 error_at (lambda_specs.locations[ds_storage_class],
10388 "duplicate %<mutable%>");
10389 }
10390
10391 tx_qual = cp_parser_tx_qualifier_opt (parser);
10392
10393 /* Parse optional exception specification. */
10394 exception_spec = cp_parser_exception_specification_opt (parser);
10395
10396 /* Parse optional trailing return type. */
10397 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10398 {
10399 cp_lexer_consume_token (parser->lexer);
10400 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10401 = cp_parser_trailing_type_id (parser);
10402 }
10403
10404 /* The function parameters must be in scope all the way until after the
10405 trailing-return-type in case of decltype. */
10406 pop_bindings_and_leave_scope ();
10407 }
10408 else if (template_param_list != NULL_TREE) // generate diagnostic
10409 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10410
10411 /* Create the function call operator.
10412
10413 Messing with declarators like this is no uglier than building up the
10414 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10415 other code. */
10416 {
10417 cp_decl_specifier_seq return_type_specs;
10418 cp_declarator* declarator;
10419 tree fco;
10420 int quals;
10421 void *p;
10422
10423 clear_decl_specs (&return_type_specs);
10424 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10425 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10426 else
10427 /* Maybe we will deduce the return type later. */
10428 return_type_specs.type = make_auto ();
10429
10430 if (lambda_specs.locations[ds_constexpr])
10431 {
10432 if (cxx_dialect >= cxx1z)
10433 return_type_specs.locations[ds_constexpr]
10434 = lambda_specs.locations[ds_constexpr];
10435 else
10436 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10437 "lambda only available with -std=c++1z or -std=gnu++1z");
10438 }
10439
10440 p = obstack_alloc (&declarator_obstack, 0);
10441
10442 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10443 sfk_none);
10444
10445 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10446 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10447 declarator = make_call_declarator (declarator, param_list, quals,
10448 VIRT_SPEC_UNSPECIFIED,
10449 REF_QUAL_NONE,
10450 tx_qual,
10451 exception_spec,
10452 /*late_return_type=*/NULL_TREE,
10453 /*requires_clause*/NULL_TREE);
10454 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10455
10456 fco = grokmethod (&return_type_specs,
10457 declarator,
10458 attributes);
10459 if (fco != error_mark_node)
10460 {
10461 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10462 DECL_ARTIFICIAL (fco) = 1;
10463 /* Give the object parameter a different name. */
10464 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10465 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10466 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10467 }
10468 if (template_param_list)
10469 {
10470 fco = finish_member_template_decl (fco);
10471 finish_template_decl (template_param_list);
10472 --parser->num_template_parameter_lists;
10473 }
10474 else if (parser->fully_implicit_function_template_p)
10475 fco = finish_fully_implicit_template (parser, fco);
10476
10477 finish_member_declaration (fco);
10478
10479 obstack_free (&declarator_obstack, p);
10480
10481 return (fco != error_mark_node);
10482 }
10483 }
10484
10485 /* Parse the body of a lambda expression, which is simply
10486
10487 compound-statement
10488
10489 but which requires special handling.
10490 LAMBDA_EXPR is the current representation of the lambda expression. */
10491
10492 static void
10493 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10494 {
10495 bool nested = (current_function_decl != NULL_TREE);
10496 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10497 if (nested)
10498 push_function_context ();
10499 else
10500 /* Still increment function_depth so that we don't GC in the
10501 middle of an expression. */
10502 ++function_depth;
10503 vec<tree> omp_privatization_save;
10504 save_omp_privatization_clauses (omp_privatization_save);
10505 /* Clear this in case we're in the middle of a default argument. */
10506 parser->local_variables_forbidden_p = false;
10507
10508 /* Finish the function call operator
10509 - class_specifier
10510 + late_parsing_for_member
10511 + function_definition_after_declarator
10512 + ctor_initializer_opt_and_function_body */
10513 {
10514 tree fco = lambda_function (lambda_expr);
10515 tree body;
10516 bool done = false;
10517 tree compound_stmt;
10518 tree cap;
10519
10520 /* Let the front end know that we are going to be defining this
10521 function. */
10522 start_preparsed_function (fco,
10523 NULL_TREE,
10524 SF_PRE_PARSED | SF_INCLASS_INLINE);
10525
10526 start_lambda_scope (fco);
10527 body = begin_function_body ();
10528
10529 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10530 goto out;
10531
10532 /* Push the proxies for any explicit captures. */
10533 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10534 cap = TREE_CHAIN (cap))
10535 build_capture_proxy (TREE_PURPOSE (cap));
10536
10537 compound_stmt = begin_compound_stmt (0);
10538
10539 /* 5.1.1.4 of the standard says:
10540 If a lambda-expression does not include a trailing-return-type, it
10541 is as if the trailing-return-type denotes the following type:
10542 * if the compound-statement is of the form
10543 { return attribute-specifier [opt] expression ; }
10544 the type of the returned expression after lvalue-to-rvalue
10545 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10546 (_conv.array_ 4.2), and function-to-pointer conversion
10547 (_conv.func_ 4.3);
10548 * otherwise, void. */
10549
10550 /* In a lambda that has neither a lambda-return-type-clause
10551 nor a deducible form, errors should be reported for return statements
10552 in the body. Since we used void as the placeholder return type, parsing
10553 the body as usual will give such desired behavior. */
10554 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10555 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10556 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10557 {
10558 tree expr = NULL_TREE;
10559 cp_id_kind idk = CP_ID_KIND_NONE;
10560
10561 /* Parse tentatively in case there's more after the initial return
10562 statement. */
10563 cp_parser_parse_tentatively (parser);
10564
10565 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10566
10567 expr = cp_parser_expression (parser, &idk);
10568
10569 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10570 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10571
10572 if (cp_parser_parse_definitely (parser))
10573 {
10574 if (!processing_template_decl)
10575 {
10576 tree type = lambda_return_type (expr);
10577 apply_deduced_return_type (fco, type);
10578 if (type == error_mark_node)
10579 expr = error_mark_node;
10580 }
10581
10582 /* Will get error here if type not deduced yet. */
10583 finish_return_stmt (expr);
10584
10585 done = true;
10586 }
10587 }
10588
10589 if (!done)
10590 {
10591 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10592 cp_parser_label_declaration (parser);
10593 cp_parser_statement_seq_opt (parser, NULL_TREE);
10594 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10595 }
10596
10597 finish_compound_stmt (compound_stmt);
10598
10599 out:
10600 finish_function_body (body);
10601 finish_lambda_scope ();
10602
10603 /* Finish the function and generate code for it if necessary. */
10604 tree fn = finish_function (/*inline*/2);
10605
10606 /* Only expand if the call op is not a template. */
10607 if (!DECL_TEMPLATE_INFO (fco))
10608 expand_or_defer_fn (fn);
10609 }
10610
10611 restore_omp_privatization_clauses (omp_privatization_save);
10612 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10613 if (nested)
10614 pop_function_context();
10615 else
10616 --function_depth;
10617 }
10618
10619 /* Statements [gram.stmt.stmt] */
10620
10621 /* Parse a statement.
10622
10623 statement:
10624 labeled-statement
10625 expression-statement
10626 compound-statement
10627 selection-statement
10628 iteration-statement
10629 jump-statement
10630 declaration-statement
10631 try-block
10632
10633 C++11:
10634
10635 statement:
10636 labeled-statement
10637 attribute-specifier-seq (opt) expression-statement
10638 attribute-specifier-seq (opt) compound-statement
10639 attribute-specifier-seq (opt) selection-statement
10640 attribute-specifier-seq (opt) iteration-statement
10641 attribute-specifier-seq (opt) jump-statement
10642 declaration-statement
10643 attribute-specifier-seq (opt) try-block
10644
10645 init-statement:
10646 expression-statement
10647 simple-declaration
10648
10649 TM Extension:
10650
10651 statement:
10652 atomic-statement
10653
10654 IN_COMPOUND is true when the statement is nested inside a
10655 cp_parser_compound_statement; this matters for certain pragmas.
10656
10657 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10658 is a (possibly labeled) if statement which is not enclosed in braces
10659 and has an else clause. This is used to implement -Wparentheses.
10660
10661 CHAIN is a vector of if-else-if conditions. */
10662
10663 static void
10664 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10665 bool in_compound, bool *if_p, vec<tree> *chain,
10666 location_t *loc_after_labels)
10667 {
10668 tree statement, std_attrs = NULL_TREE;
10669 cp_token *token;
10670 location_t statement_location, attrs_location;
10671
10672 restart:
10673 if (if_p != NULL)
10674 *if_p = false;
10675 /* There is no statement yet. */
10676 statement = NULL_TREE;
10677
10678 saved_token_sentinel saved_tokens (parser->lexer);
10679 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10680 if (c_dialect_objc ())
10681 /* In obj-c++, seeing '[[' might be the either the beginning of
10682 c++11 attributes, or a nested objc-message-expression. So
10683 let's parse the c++11 attributes tentatively. */
10684 cp_parser_parse_tentatively (parser);
10685 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10686 if (c_dialect_objc ())
10687 {
10688 if (!cp_parser_parse_definitely (parser))
10689 std_attrs = NULL_TREE;
10690 }
10691
10692 /* Peek at the next token. */
10693 token = cp_lexer_peek_token (parser->lexer);
10694 /* Remember the location of the first token in the statement. */
10695 statement_location = token->location;
10696 /* If this is a keyword, then that will often determine what kind of
10697 statement we have. */
10698 if (token->type == CPP_KEYWORD)
10699 {
10700 enum rid keyword = token->keyword;
10701
10702 switch (keyword)
10703 {
10704 case RID_CASE:
10705 case RID_DEFAULT:
10706 /* Looks like a labeled-statement with a case label.
10707 Parse the label, and then use tail recursion to parse
10708 the statement. */
10709 cp_parser_label_for_labeled_statement (parser, std_attrs);
10710 in_compound = false;
10711 goto restart;
10712
10713 case RID_IF:
10714 case RID_SWITCH:
10715 statement = cp_parser_selection_statement (parser, if_p, chain);
10716 break;
10717
10718 case RID_WHILE:
10719 case RID_DO:
10720 case RID_FOR:
10721 statement = cp_parser_iteration_statement (parser, if_p, false);
10722 break;
10723
10724 case RID_CILK_FOR:
10725 if (!flag_cilkplus)
10726 {
10727 error_at (cp_lexer_peek_token (parser->lexer)->location,
10728 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10729 cp_lexer_consume_token (parser->lexer);
10730 statement = error_mark_node;
10731 }
10732 else
10733 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10734 break;
10735
10736 case RID_BREAK:
10737 case RID_CONTINUE:
10738 case RID_RETURN:
10739 case RID_GOTO:
10740 statement = cp_parser_jump_statement (parser);
10741 break;
10742
10743 case RID_CILK_SYNC:
10744 cp_lexer_consume_token (parser->lexer);
10745 if (flag_cilkplus)
10746 {
10747 tree sync_expr = build_cilk_sync ();
10748 SET_EXPR_LOCATION (sync_expr,
10749 token->location);
10750 statement = finish_expr_stmt (sync_expr);
10751 }
10752 else
10753 {
10754 error_at (token->location, "-fcilkplus must be enabled to use"
10755 " %<_Cilk_sync%>");
10756 statement = error_mark_node;
10757 }
10758 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10759 break;
10760
10761 /* Objective-C++ exception-handling constructs. */
10762 case RID_AT_TRY:
10763 case RID_AT_CATCH:
10764 case RID_AT_FINALLY:
10765 case RID_AT_SYNCHRONIZED:
10766 case RID_AT_THROW:
10767 statement = cp_parser_objc_statement (parser);
10768 break;
10769
10770 case RID_TRY:
10771 statement = cp_parser_try_block (parser);
10772 break;
10773
10774 case RID_NAMESPACE:
10775 /* This must be a namespace alias definition. */
10776 cp_parser_declaration_statement (parser);
10777 return;
10778
10779 case RID_TRANSACTION_ATOMIC:
10780 case RID_TRANSACTION_RELAXED:
10781 case RID_SYNCHRONIZED:
10782 case RID_ATOMIC_NOEXCEPT:
10783 case RID_ATOMIC_CANCEL:
10784 statement = cp_parser_transaction (parser, token);
10785 break;
10786 case RID_TRANSACTION_CANCEL:
10787 statement = cp_parser_transaction_cancel (parser);
10788 break;
10789
10790 default:
10791 /* It might be a keyword like `int' that can start a
10792 declaration-statement. */
10793 break;
10794 }
10795 }
10796 else if (token->type == CPP_NAME)
10797 {
10798 /* If the next token is a `:', then we are looking at a
10799 labeled-statement. */
10800 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10801 if (token->type == CPP_COLON)
10802 {
10803 /* Looks like a labeled-statement with an ordinary label.
10804 Parse the label, and then use tail recursion to parse
10805 the statement. */
10806
10807 cp_parser_label_for_labeled_statement (parser, std_attrs);
10808 in_compound = false;
10809 goto restart;
10810 }
10811 }
10812 /* Anything that starts with a `{' must be a compound-statement. */
10813 else if (token->type == CPP_OPEN_BRACE)
10814 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10815 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10816 a statement all its own. */
10817 else if (token->type == CPP_PRAGMA)
10818 {
10819 /* Only certain OpenMP pragmas are attached to statements, and thus
10820 are considered statements themselves. All others are not. In
10821 the context of a compound, accept the pragma as a "statement" and
10822 return so that we can check for a close brace. Otherwise we
10823 require a real statement and must go back and read one. */
10824 if (in_compound)
10825 cp_parser_pragma (parser, pragma_compound, if_p);
10826 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10827 goto restart;
10828 return;
10829 }
10830 else if (token->type == CPP_EOF)
10831 {
10832 cp_parser_error (parser, "expected statement");
10833 return;
10834 }
10835
10836 /* Everything else must be a declaration-statement or an
10837 expression-statement. Try for the declaration-statement
10838 first, unless we are looking at a `;', in which case we know that
10839 we have an expression-statement. */
10840 if (!statement)
10841 {
10842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10843 {
10844 if (std_attrs != NULL_TREE)
10845 {
10846 /* Attributes should be parsed as part of the the
10847 declaration, so let's un-parse them. */
10848 saved_tokens.rollback();
10849 std_attrs = NULL_TREE;
10850 }
10851
10852 cp_parser_parse_tentatively (parser);
10853 /* Try to parse the declaration-statement. */
10854 cp_parser_declaration_statement (parser);
10855 /* If that worked, we're done. */
10856 if (cp_parser_parse_definitely (parser))
10857 return;
10858 }
10859 /* All preceding labels have been parsed at this point. */
10860 if (loc_after_labels != NULL)
10861 *loc_after_labels = statement_location;
10862
10863 /* Look for an expression-statement instead. */
10864 statement = cp_parser_expression_statement (parser, in_statement_expr);
10865
10866 /* Handle [[fallthrough]];. */
10867 if (attribute_fallthrough_p (std_attrs))
10868 {
10869 /* The next token after the fallthrough attribute is ';'. */
10870 if (statement == NULL_TREE)
10871 {
10872 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10873 statement = build_call_expr_internal_loc (statement_location,
10874 IFN_FALLTHROUGH,
10875 void_type_node, 0);
10876 finish_expr_stmt (statement);
10877 }
10878 else
10879 warning_at (statement_location, OPT_Wattributes,
10880 "%<fallthrough%> attribute not followed by %<;%>");
10881 std_attrs = NULL_TREE;
10882 }
10883 }
10884
10885 /* Set the line number for the statement. */
10886 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10887 SET_EXPR_LOCATION (statement, statement_location);
10888
10889 /* Allow "[[fallthrough]];", but warn otherwise. */
10890 if (std_attrs != NULL_TREE)
10891 warning_at (attrs_location,
10892 OPT_Wattributes,
10893 "attributes at the beginning of statement are ignored");
10894 }
10895
10896 /* Parse the label for a labeled-statement, i.e.
10897
10898 identifier :
10899 case constant-expression :
10900 default :
10901
10902 GNU Extension:
10903 case constant-expression ... constant-expression : statement
10904
10905 When a label is parsed without errors, the label is added to the
10906 parse tree by the finish_* functions, so this function doesn't
10907 have to return the label. */
10908
10909 static void
10910 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10911 {
10912 cp_token *token;
10913 tree label = NULL_TREE;
10914 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10915
10916 /* The next token should be an identifier. */
10917 token = cp_lexer_peek_token (parser->lexer);
10918 if (token->type != CPP_NAME
10919 && token->type != CPP_KEYWORD)
10920 {
10921 cp_parser_error (parser, "expected labeled-statement");
10922 return;
10923 }
10924
10925 /* Remember whether this case or a user-defined label is allowed to fall
10926 through to. */
10927 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10928
10929 parser->colon_corrects_to_scope_p = false;
10930 switch (token->keyword)
10931 {
10932 case RID_CASE:
10933 {
10934 tree expr, expr_hi;
10935 cp_token *ellipsis;
10936
10937 /* Consume the `case' token. */
10938 cp_lexer_consume_token (parser->lexer);
10939 /* Parse the constant-expression. */
10940 expr = cp_parser_constant_expression (parser);
10941 if (check_for_bare_parameter_packs (expr))
10942 expr = error_mark_node;
10943
10944 ellipsis = cp_lexer_peek_token (parser->lexer);
10945 if (ellipsis->type == CPP_ELLIPSIS)
10946 {
10947 /* Consume the `...' token. */
10948 cp_lexer_consume_token (parser->lexer);
10949 expr_hi = cp_parser_constant_expression (parser);
10950 if (check_for_bare_parameter_packs (expr_hi))
10951 expr_hi = error_mark_node;
10952
10953 /* We don't need to emit warnings here, as the common code
10954 will do this for us. */
10955 }
10956 else
10957 expr_hi = NULL_TREE;
10958
10959 if (parser->in_switch_statement_p)
10960 {
10961 tree l = finish_case_label (token->location, expr, expr_hi);
10962 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10963 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10964 }
10965 else
10966 error_at (token->location,
10967 "case label %qE not within a switch statement",
10968 expr);
10969 }
10970 break;
10971
10972 case RID_DEFAULT:
10973 /* Consume the `default' token. */
10974 cp_lexer_consume_token (parser->lexer);
10975
10976 if (parser->in_switch_statement_p)
10977 {
10978 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10979 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10980 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10981 }
10982 else
10983 error_at (token->location, "case label not within a switch statement");
10984 break;
10985
10986 default:
10987 /* Anything else must be an ordinary label. */
10988 label = finish_label_stmt (cp_parser_identifier (parser));
10989 if (label && TREE_CODE (label) == LABEL_DECL)
10990 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10991 break;
10992 }
10993
10994 /* Require the `:' token. */
10995 cp_parser_require (parser, CPP_COLON, RT_COLON);
10996
10997 /* An ordinary label may optionally be followed by attributes.
10998 However, this is only permitted if the attributes are then
10999 followed by a semicolon. This is because, for backward
11000 compatibility, when parsing
11001 lab: __attribute__ ((unused)) int i;
11002 we want the attribute to attach to "i", not "lab". */
11003 if (label != NULL_TREE
11004 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11005 {
11006 tree attrs;
11007 cp_parser_parse_tentatively (parser);
11008 attrs = cp_parser_gnu_attributes_opt (parser);
11009 if (attrs == NULL_TREE
11010 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11011 cp_parser_abort_tentative_parse (parser);
11012 else if (!cp_parser_parse_definitely (parser))
11013 ;
11014 else
11015 attributes = chainon (attributes, attrs);
11016 }
11017
11018 if (attributes != NULL_TREE)
11019 cplus_decl_attributes (&label, attributes, 0);
11020
11021 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11022 }
11023
11024 /* Parse an expression-statement.
11025
11026 expression-statement:
11027 expression [opt] ;
11028
11029 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11030 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11031 indicates whether this expression-statement is part of an
11032 expression statement. */
11033
11034 static tree
11035 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11036 {
11037 tree statement = NULL_TREE;
11038 cp_token *token = cp_lexer_peek_token (parser->lexer);
11039 location_t loc = token->location;
11040
11041 /* There might be attribute fallthrough. */
11042 tree attr = cp_parser_gnu_attributes_opt (parser);
11043
11044 /* If the next token is a ';', then there is no expression
11045 statement. */
11046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11047 {
11048 statement = cp_parser_expression (parser);
11049 if (statement == error_mark_node
11050 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11051 {
11052 cp_parser_skip_to_end_of_block_or_statement (parser);
11053 return error_mark_node;
11054 }
11055 }
11056
11057 /* Handle [[fallthrough]];. */
11058 if (attribute_fallthrough_p (attr))
11059 {
11060 /* The next token after the fallthrough attribute is ';'. */
11061 if (statement == NULL_TREE)
11062 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11063 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11064 void_type_node, 0);
11065 else
11066 warning_at (loc, OPT_Wattributes,
11067 "%<fallthrough%> attribute not followed by %<;%>");
11068 attr = NULL_TREE;
11069 }
11070
11071 /* Allow "[[fallthrough]];", but warn otherwise. */
11072 if (attr != NULL_TREE)
11073 warning_at (loc, OPT_Wattributes,
11074 "attributes at the beginning of statement are ignored");
11075
11076 /* Give a helpful message for "A<T>::type t;" and the like. */
11077 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11078 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11079 {
11080 if (TREE_CODE (statement) == SCOPE_REF)
11081 error_at (token->location, "need %<typename%> before %qE because "
11082 "%qT is a dependent scope",
11083 statement, TREE_OPERAND (statement, 0));
11084 else if (is_overloaded_fn (statement)
11085 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11086 {
11087 /* A::A a; */
11088 tree fn = get_first_fn (statement);
11089 error_at (token->location,
11090 "%<%T::%D%> names the constructor, not the type",
11091 DECL_CONTEXT (fn), DECL_NAME (fn));
11092 }
11093 }
11094
11095 /* Consume the final `;'. */
11096 cp_parser_consume_semicolon_at_end_of_statement (parser);
11097
11098 if (in_statement_expr
11099 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11100 /* This is the final expression statement of a statement
11101 expression. */
11102 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11103 else if (statement)
11104 statement = finish_expr_stmt (statement);
11105
11106 return statement;
11107 }
11108
11109 /* Parse a compound-statement.
11110
11111 compound-statement:
11112 { statement-seq [opt] }
11113
11114 GNU extension:
11115
11116 compound-statement:
11117 { label-declaration-seq [opt] statement-seq [opt] }
11118
11119 label-declaration-seq:
11120 label-declaration
11121 label-declaration-seq label-declaration
11122
11123 Returns a tree representing the statement. */
11124
11125 static tree
11126 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11127 int bcs_flags, bool function_body)
11128 {
11129 tree compound_stmt;
11130
11131 /* Consume the `{'. */
11132 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11133 return error_mark_node;
11134 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11135 && !function_body && cxx_dialect < cxx14)
11136 pedwarn (input_location, OPT_Wpedantic,
11137 "compound-statement in constexpr function");
11138 /* Begin the compound-statement. */
11139 compound_stmt = begin_compound_stmt (bcs_flags);
11140 /* If the next keyword is `__label__' we have a label declaration. */
11141 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11142 cp_parser_label_declaration (parser);
11143 /* Parse an (optional) statement-seq. */
11144 cp_parser_statement_seq_opt (parser, in_statement_expr);
11145 /* Finish the compound-statement. */
11146 finish_compound_stmt (compound_stmt);
11147 /* Consume the `}'. */
11148 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11149
11150 return compound_stmt;
11151 }
11152
11153 /* Parse an (optional) statement-seq.
11154
11155 statement-seq:
11156 statement
11157 statement-seq [opt] statement */
11158
11159 static void
11160 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11161 {
11162 /* Scan statements until there aren't any more. */
11163 while (true)
11164 {
11165 cp_token *token = cp_lexer_peek_token (parser->lexer);
11166
11167 /* If we are looking at a `}', then we have run out of
11168 statements; the same is true if we have reached the end
11169 of file, or have stumbled upon a stray '@end'. */
11170 if (token->type == CPP_CLOSE_BRACE
11171 || token->type == CPP_EOF
11172 || token->type == CPP_PRAGMA_EOL
11173 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11174 break;
11175
11176 /* If we are in a compound statement and find 'else' then
11177 something went wrong. */
11178 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11179 {
11180 if (parser->in_statement & IN_IF_STMT)
11181 break;
11182 else
11183 {
11184 token = cp_lexer_consume_token (parser->lexer);
11185 error_at (token->location, "%<else%> without a previous %<if%>");
11186 }
11187 }
11188
11189 /* Parse the statement. */
11190 cp_parser_statement (parser, in_statement_expr, true, NULL);
11191 }
11192 }
11193
11194 /* Return true if we're looking at (init; cond), false otherwise. */
11195
11196 static bool
11197 cp_parser_init_statement_p (cp_parser *parser)
11198 {
11199 /* Save tokens so that we can put them back. */
11200 cp_lexer_save_tokens (parser->lexer);
11201
11202 /* Look for ';' that is not nested in () or {}. */
11203 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11204 /*recovering=*/false,
11205 CPP_SEMICOLON,
11206 /*consume_paren=*/false);
11207
11208 /* Roll back the tokens we skipped. */
11209 cp_lexer_rollback_tokens (parser->lexer);
11210
11211 return ret == -1;
11212 }
11213
11214 /* Parse a selection-statement.
11215
11216 selection-statement:
11217 if ( init-statement [opt] condition ) statement
11218 if ( init-statement [opt] condition ) statement else statement
11219 switch ( init-statement [opt] condition ) statement
11220
11221 Returns the new IF_STMT or SWITCH_STMT.
11222
11223 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11224 is a (possibly labeled) if statement which is not enclosed in
11225 braces and has an else clause. This is used to implement
11226 -Wparentheses.
11227
11228 CHAIN is a vector of if-else-if conditions. This is used to implement
11229 -Wduplicated-cond. */
11230
11231 static tree
11232 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11233 vec<tree> *chain)
11234 {
11235 cp_token *token;
11236 enum rid keyword;
11237 token_indent_info guard_tinfo;
11238
11239 if (if_p != NULL)
11240 *if_p = false;
11241
11242 /* Peek at the next token. */
11243 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11244 guard_tinfo = get_token_indent_info (token);
11245
11246 /* See what kind of keyword it is. */
11247 keyword = token->keyword;
11248 switch (keyword)
11249 {
11250 case RID_IF:
11251 case RID_SWITCH:
11252 {
11253 tree statement;
11254 tree condition;
11255
11256 bool cx = false;
11257 if (keyword == RID_IF
11258 && cp_lexer_next_token_is_keyword (parser->lexer,
11259 RID_CONSTEXPR))
11260 {
11261 cx = true;
11262 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11263 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11264 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11265 "with -std=c++1z or -std=gnu++1z");
11266 }
11267
11268 /* Look for the `('. */
11269 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11270 {
11271 cp_parser_skip_to_end_of_statement (parser);
11272 return error_mark_node;
11273 }
11274
11275 /* Begin the selection-statement. */
11276 if (keyword == RID_IF)
11277 {
11278 statement = begin_if_stmt ();
11279 IF_STMT_CONSTEXPR_P (statement) = cx;
11280 }
11281 else
11282 statement = begin_switch_stmt ();
11283
11284 /* Parse the optional init-statement. */
11285 if (cp_parser_init_statement_p (parser))
11286 {
11287 tree decl;
11288 if (cxx_dialect < cxx1z)
11289 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11290 "init-statement in selection statements only available "
11291 "with -std=c++1z or -std=gnu++1z");
11292 cp_parser_init_statement (parser, &decl);
11293 }
11294
11295 /* Parse the condition. */
11296 condition = cp_parser_condition (parser);
11297 /* Look for the `)'. */
11298 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11299 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11300 /*consume_paren=*/true);
11301
11302 if (keyword == RID_IF)
11303 {
11304 bool nested_if;
11305 unsigned char in_statement;
11306
11307 /* Add the condition. */
11308 condition = finish_if_stmt_cond (condition, statement);
11309
11310 if (warn_duplicated_cond)
11311 warn_duplicated_cond_add_or_warn (token->location, condition,
11312 &chain);
11313
11314 /* Parse the then-clause. */
11315 in_statement = parser->in_statement;
11316 parser->in_statement |= IN_IF_STMT;
11317
11318 /* Outside a template, the non-selected branch of a constexpr
11319 if is a 'discarded statement', i.e. unevaluated. */
11320 bool was_discarded = in_discarded_stmt;
11321 bool discard_then = (cx && !processing_template_decl
11322 && integer_zerop (condition));
11323 if (discard_then)
11324 {
11325 in_discarded_stmt = true;
11326 ++c_inhibit_evaluation_warnings;
11327 }
11328
11329 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11330 guard_tinfo);
11331
11332 parser->in_statement = in_statement;
11333
11334 finish_then_clause (statement);
11335
11336 if (discard_then)
11337 {
11338 THEN_CLAUSE (statement) = NULL_TREE;
11339 in_discarded_stmt = was_discarded;
11340 --c_inhibit_evaluation_warnings;
11341 }
11342
11343 /* If the next token is `else', parse the else-clause. */
11344 if (cp_lexer_next_token_is_keyword (parser->lexer,
11345 RID_ELSE))
11346 {
11347 bool discard_else = (cx && !processing_template_decl
11348 && integer_nonzerop (condition));
11349 if (discard_else)
11350 {
11351 in_discarded_stmt = true;
11352 ++c_inhibit_evaluation_warnings;
11353 }
11354
11355 guard_tinfo
11356 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11357 /* Consume the `else' keyword. */
11358 cp_lexer_consume_token (parser->lexer);
11359 if (warn_duplicated_cond)
11360 {
11361 if (cp_lexer_next_token_is_keyword (parser->lexer,
11362 RID_IF)
11363 && chain == NULL)
11364 {
11365 /* We've got "if (COND) else if (COND2)". Start
11366 the condition chain and add COND as the first
11367 element. */
11368 chain = new vec<tree> ();
11369 if (!CONSTANT_CLASS_P (condition)
11370 && !TREE_SIDE_EFFECTS (condition))
11371 {
11372 /* Wrap it in a NOP_EXPR so that we can set the
11373 location of the condition. */
11374 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11375 condition);
11376 SET_EXPR_LOCATION (e, token->location);
11377 chain->safe_push (e);
11378 }
11379 }
11380 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11381 RID_IF))
11382 {
11383 /* This is if-else without subsequent if. Zap the
11384 condition chain; we would have already warned at
11385 this point. */
11386 delete chain;
11387 chain = NULL;
11388 }
11389 }
11390 begin_else_clause (statement);
11391 /* Parse the else-clause. */
11392 cp_parser_implicitly_scoped_statement (parser, NULL,
11393 guard_tinfo, chain);
11394
11395 finish_else_clause (statement);
11396
11397 /* If we are currently parsing a then-clause, then
11398 IF_P will not be NULL. We set it to true to
11399 indicate that this if statement has an else clause.
11400 This may trigger the Wparentheses warning below
11401 when we get back up to the parent if statement. */
11402 if (if_p != NULL)
11403 *if_p = true;
11404
11405 if (discard_else)
11406 {
11407 ELSE_CLAUSE (statement) = NULL_TREE;
11408 in_discarded_stmt = was_discarded;
11409 --c_inhibit_evaluation_warnings;
11410 }
11411 }
11412 else
11413 {
11414 /* This if statement does not have an else clause. If
11415 NESTED_IF is true, then the then-clause has an if
11416 statement which does have an else clause. We warn
11417 about the potential ambiguity. */
11418 if (nested_if)
11419 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11420 "suggest explicit braces to avoid ambiguous"
11421 " %<else%>");
11422 if (warn_duplicated_cond)
11423 {
11424 /* We don't need the condition chain anymore. */
11425 delete chain;
11426 chain = NULL;
11427 }
11428 }
11429
11430 /* Now we're all done with the if-statement. */
11431 finish_if_stmt (statement);
11432 }
11433 else
11434 {
11435 bool in_switch_statement_p;
11436 unsigned char in_statement;
11437
11438 /* Add the condition. */
11439 finish_switch_cond (condition, statement);
11440
11441 /* Parse the body of the switch-statement. */
11442 in_switch_statement_p = parser->in_switch_statement_p;
11443 in_statement = parser->in_statement;
11444 parser->in_switch_statement_p = true;
11445 parser->in_statement |= IN_SWITCH_STMT;
11446 cp_parser_implicitly_scoped_statement (parser, if_p,
11447 guard_tinfo);
11448 parser->in_switch_statement_p = in_switch_statement_p;
11449 parser->in_statement = in_statement;
11450
11451 /* Now we're all done with the switch-statement. */
11452 finish_switch_stmt (statement);
11453 }
11454
11455 return statement;
11456 }
11457 break;
11458
11459 default:
11460 cp_parser_error (parser, "expected selection-statement");
11461 return error_mark_node;
11462 }
11463 }
11464
11465 /* Parse a condition.
11466
11467 condition:
11468 expression
11469 type-specifier-seq declarator = initializer-clause
11470 type-specifier-seq declarator braced-init-list
11471
11472 GNU Extension:
11473
11474 condition:
11475 type-specifier-seq declarator asm-specification [opt]
11476 attributes [opt] = assignment-expression
11477
11478 Returns the expression that should be tested. */
11479
11480 static tree
11481 cp_parser_condition (cp_parser* parser)
11482 {
11483 cp_decl_specifier_seq type_specifiers;
11484 const char *saved_message;
11485 int declares_class_or_enum;
11486
11487 /* Try the declaration first. */
11488 cp_parser_parse_tentatively (parser);
11489 /* New types are not allowed in the type-specifier-seq for a
11490 condition. */
11491 saved_message = parser->type_definition_forbidden_message;
11492 parser->type_definition_forbidden_message
11493 = G_("types may not be defined in conditions");
11494 /* Parse the type-specifier-seq. */
11495 cp_parser_decl_specifier_seq (parser,
11496 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11497 &type_specifiers,
11498 &declares_class_or_enum);
11499 /* Restore the saved message. */
11500 parser->type_definition_forbidden_message = saved_message;
11501 /* If all is well, we might be looking at a declaration. */
11502 if (!cp_parser_error_occurred (parser))
11503 {
11504 tree decl;
11505 tree asm_specification;
11506 tree attributes;
11507 cp_declarator *declarator;
11508 tree initializer = NULL_TREE;
11509
11510 /* Parse the declarator. */
11511 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11512 /*ctor_dtor_or_conv_p=*/NULL,
11513 /*parenthesized_p=*/NULL,
11514 /*member_p=*/false,
11515 /*friend_p=*/false);
11516 /* Parse the attributes. */
11517 attributes = cp_parser_attributes_opt (parser);
11518 /* Parse the asm-specification. */
11519 asm_specification = cp_parser_asm_specification_opt (parser);
11520 /* If the next token is not an `=' or '{', then we might still be
11521 looking at an expression. For example:
11522
11523 if (A(a).x)
11524
11525 looks like a decl-specifier-seq and a declarator -- but then
11526 there is no `=', so this is an expression. */
11527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11528 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11529 cp_parser_simulate_error (parser);
11530
11531 /* If we did see an `=' or '{', then we are looking at a declaration
11532 for sure. */
11533 if (cp_parser_parse_definitely (parser))
11534 {
11535 tree pushed_scope;
11536 bool non_constant_p;
11537 int flags = LOOKUP_ONLYCONVERTING;
11538
11539 /* Create the declaration. */
11540 decl = start_decl (declarator, &type_specifiers,
11541 /*initialized_p=*/true,
11542 attributes, /*prefix_attributes=*/NULL_TREE,
11543 &pushed_scope);
11544
11545 /* Parse the initializer. */
11546 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11547 {
11548 initializer = cp_parser_braced_list (parser, &non_constant_p);
11549 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11550 flags = 0;
11551 }
11552 else
11553 {
11554 /* Consume the `='. */
11555 cp_parser_require (parser, CPP_EQ, RT_EQ);
11556 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11557 }
11558 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11559 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11560
11561 /* Process the initializer. */
11562 cp_finish_decl (decl,
11563 initializer, !non_constant_p,
11564 asm_specification,
11565 flags);
11566
11567 if (pushed_scope)
11568 pop_scope (pushed_scope);
11569
11570 return convert_from_reference (decl);
11571 }
11572 }
11573 /* If we didn't even get past the declarator successfully, we are
11574 definitely not looking at a declaration. */
11575 else
11576 cp_parser_abort_tentative_parse (parser);
11577
11578 /* Otherwise, we are looking at an expression. */
11579 return cp_parser_expression (parser);
11580 }
11581
11582 /* Parses a for-statement or range-for-statement until the closing ')',
11583 not included. */
11584
11585 static tree
11586 cp_parser_for (cp_parser *parser, bool ivdep)
11587 {
11588 tree init, scope, decl;
11589 bool is_range_for;
11590
11591 /* Begin the for-statement. */
11592 scope = begin_for_scope (&init);
11593
11594 /* Parse the initialization. */
11595 is_range_for = cp_parser_init_statement (parser, &decl);
11596
11597 if (is_range_for)
11598 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11599 else
11600 return cp_parser_c_for (parser, scope, init, ivdep);
11601 }
11602
11603 static tree
11604 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11605 {
11606 /* Normal for loop */
11607 tree condition = NULL_TREE;
11608 tree expression = NULL_TREE;
11609 tree stmt;
11610
11611 stmt = begin_for_stmt (scope, init);
11612 /* The init-statement has already been parsed in
11613 cp_parser_init_statement, so no work is needed here. */
11614 finish_init_stmt (stmt);
11615
11616 /* If there's a condition, process it. */
11617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11618 condition = cp_parser_condition (parser);
11619 else if (ivdep)
11620 {
11621 cp_parser_error (parser, "missing loop condition in loop with "
11622 "%<GCC ivdep%> pragma");
11623 condition = error_mark_node;
11624 }
11625 finish_for_cond (condition, stmt, ivdep);
11626 /* Look for the `;'. */
11627 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11628
11629 /* If there's an expression, process it. */
11630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11631 expression = cp_parser_expression (parser);
11632 finish_for_expr (expression, stmt);
11633
11634 return stmt;
11635 }
11636
11637 /* Tries to parse a range-based for-statement:
11638
11639 range-based-for:
11640 decl-specifier-seq declarator : expression
11641
11642 The decl-specifier-seq declarator and the `:' are already parsed by
11643 cp_parser_init_statement. If processing_template_decl it returns a
11644 newly created RANGE_FOR_STMT; if not, it is converted to a
11645 regular FOR_STMT. */
11646
11647 static tree
11648 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11649 bool ivdep)
11650 {
11651 tree stmt, range_expr;
11652 auto_vec <cxx_binding *, 16> bindings;
11653 auto_vec <tree, 16> names;
11654 tree decomp_first_name = NULL_TREE;
11655 unsigned int decomp_cnt = 0;
11656
11657 /* Get the range declaration momentarily out of the way so that
11658 the range expression doesn't clash with it. */
11659 if (range_decl != error_mark_node)
11660 {
11661 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11662 {
11663 tree v = DECL_VALUE_EXPR (range_decl);
11664 /* For decomposition declaration get all of the corresponding
11665 declarations out of the way. */
11666 if (TREE_CODE (v) == ARRAY_REF
11667 && VAR_P (TREE_OPERAND (v, 0))
11668 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11669 {
11670 tree d = range_decl;
11671 range_decl = TREE_OPERAND (v, 0);
11672 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11673 decomp_first_name = d;
11674 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11675 {
11676 tree name = DECL_NAME (d);
11677 names.safe_push (name);
11678 bindings.safe_push (IDENTIFIER_BINDING (name));
11679 IDENTIFIER_BINDING (name)
11680 = IDENTIFIER_BINDING (name)->previous;
11681 }
11682 }
11683 }
11684 if (names.is_empty ())
11685 {
11686 tree name = DECL_NAME (range_decl);
11687 names.safe_push (name);
11688 bindings.safe_push (IDENTIFIER_BINDING (name));
11689 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11690 }
11691 }
11692
11693 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11694 {
11695 bool expr_non_constant_p;
11696 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11697 }
11698 else
11699 range_expr = cp_parser_expression (parser);
11700
11701 /* Put the range declaration(s) back into scope. */
11702 for (unsigned int i = 0; i < names.length (); i++)
11703 {
11704 cxx_binding *binding = bindings[i];
11705 binding->previous = IDENTIFIER_BINDING (names[i]);
11706 IDENTIFIER_BINDING (names[i]) = binding;
11707 }
11708
11709 /* If in template, STMT is converted to a normal for-statement
11710 at instantiation. If not, it is done just ahead. */
11711 if (processing_template_decl)
11712 {
11713 if (check_for_bare_parameter_packs (range_expr))
11714 range_expr = error_mark_node;
11715 stmt = begin_range_for_stmt (scope, init);
11716 if (ivdep)
11717 RANGE_FOR_IVDEP (stmt) = 1;
11718 finish_range_for_decl (stmt, range_decl, range_expr);
11719 if (!type_dependent_expression_p (range_expr)
11720 /* do_auto_deduction doesn't mess with template init-lists. */
11721 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11722 do_range_for_auto_deduction (range_decl, range_expr);
11723 }
11724 else
11725 {
11726 stmt = begin_for_stmt (scope, init);
11727 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11728 decomp_first_name, decomp_cnt, ivdep);
11729 }
11730 return stmt;
11731 }
11732
11733 /* Subroutine of cp_convert_range_for: given the initializer expression,
11734 builds up the range temporary. */
11735
11736 static tree
11737 build_range_temp (tree range_expr)
11738 {
11739 tree range_type, range_temp;
11740
11741 /* Find out the type deduced by the declaration
11742 `auto &&__range = range_expr'. */
11743 range_type = cp_build_reference_type (make_auto (), true);
11744 range_type = do_auto_deduction (range_type, range_expr,
11745 type_uses_auto (range_type));
11746
11747 /* Create the __range variable. */
11748 range_temp = build_decl (input_location, VAR_DECL,
11749 get_identifier ("__for_range"), range_type);
11750 TREE_USED (range_temp) = 1;
11751 DECL_ARTIFICIAL (range_temp) = 1;
11752
11753 return range_temp;
11754 }
11755
11756 /* Used by cp_parser_range_for in template context: we aren't going to
11757 do a full conversion yet, but we still need to resolve auto in the
11758 type of the for-range-declaration if present. This is basically
11759 a shortcut version of cp_convert_range_for. */
11760
11761 static void
11762 do_range_for_auto_deduction (tree decl, tree range_expr)
11763 {
11764 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11765 if (auto_node)
11766 {
11767 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11768 range_temp = convert_from_reference (build_range_temp (range_expr));
11769 iter_type = (cp_parser_perform_range_for_lookup
11770 (range_temp, &begin_dummy, &end_dummy));
11771 if (iter_type)
11772 {
11773 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11774 iter_type);
11775 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11776 tf_warning_or_error);
11777 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11778 iter_decl, auto_node);
11779 }
11780 }
11781 }
11782
11783 /* Converts a range-based for-statement into a normal
11784 for-statement, as per the definition.
11785
11786 for (RANGE_DECL : RANGE_EXPR)
11787 BLOCK
11788
11789 should be equivalent to:
11790
11791 {
11792 auto &&__range = RANGE_EXPR;
11793 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11794 __begin != __end;
11795 ++__begin)
11796 {
11797 RANGE_DECL = *__begin;
11798 BLOCK
11799 }
11800 }
11801
11802 If RANGE_EXPR is an array:
11803 BEGIN_EXPR = __range
11804 END_EXPR = __range + ARRAY_SIZE(__range)
11805 Else if RANGE_EXPR has a member 'begin' or 'end':
11806 BEGIN_EXPR = __range.begin()
11807 END_EXPR = __range.end()
11808 Else:
11809 BEGIN_EXPR = begin(__range)
11810 END_EXPR = end(__range);
11811
11812 If __range has a member 'begin' but not 'end', or vice versa, we must
11813 still use the second alternative (it will surely fail, however).
11814 When calling begin()/end() in the third alternative we must use
11815 argument dependent lookup, but always considering 'std' as an associated
11816 namespace. */
11817
11818 tree
11819 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11820 tree decomp_first_name, unsigned int decomp_cnt,
11821 bool ivdep)
11822 {
11823 tree begin, end;
11824 tree iter_type, begin_expr, end_expr;
11825 tree condition, expression;
11826
11827 if (range_decl == error_mark_node || range_expr == error_mark_node)
11828 /* If an error happened previously do nothing or else a lot of
11829 unhelpful errors would be issued. */
11830 begin_expr = end_expr = iter_type = error_mark_node;
11831 else
11832 {
11833 tree range_temp;
11834
11835 if (VAR_P (range_expr)
11836 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11837 /* Can't bind a reference to an array of runtime bound. */
11838 range_temp = range_expr;
11839 else
11840 {
11841 range_temp = build_range_temp (range_expr);
11842 pushdecl (range_temp);
11843 cp_finish_decl (range_temp, range_expr,
11844 /*is_constant_init*/false, NULL_TREE,
11845 LOOKUP_ONLYCONVERTING);
11846 range_temp = convert_from_reference (range_temp);
11847 }
11848 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11849 &begin_expr, &end_expr);
11850 }
11851
11852 /* The new for initialization statement. */
11853 begin = build_decl (input_location, VAR_DECL,
11854 get_identifier ("__for_begin"), iter_type);
11855 TREE_USED (begin) = 1;
11856 DECL_ARTIFICIAL (begin) = 1;
11857 pushdecl (begin);
11858 cp_finish_decl (begin, begin_expr,
11859 /*is_constant_init*/false, NULL_TREE,
11860 LOOKUP_ONLYCONVERTING);
11861
11862 if (cxx_dialect >= cxx1z)
11863 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11864 end = build_decl (input_location, VAR_DECL,
11865 get_identifier ("__for_end"), iter_type);
11866 TREE_USED (end) = 1;
11867 DECL_ARTIFICIAL (end) = 1;
11868 pushdecl (end);
11869 cp_finish_decl (end, end_expr,
11870 /*is_constant_init*/false, NULL_TREE,
11871 LOOKUP_ONLYCONVERTING);
11872
11873 finish_init_stmt (statement);
11874
11875 /* The new for condition. */
11876 condition = build_x_binary_op (input_location, NE_EXPR,
11877 begin, ERROR_MARK,
11878 end, ERROR_MARK,
11879 NULL, tf_warning_or_error);
11880 finish_for_cond (condition, statement, ivdep);
11881
11882 /* The new increment expression. */
11883 expression = finish_unary_op_expr (input_location,
11884 PREINCREMENT_EXPR, begin,
11885 tf_warning_or_error);
11886 finish_for_expr (expression, statement);
11887
11888 /* The declaration is initialized with *__begin inside the loop body. */
11889 cp_finish_decl (range_decl,
11890 build_x_indirect_ref (input_location, begin, RO_NULL,
11891 tf_warning_or_error),
11892 /*is_constant_init*/false, NULL_TREE,
11893 LOOKUP_ONLYCONVERTING);
11894 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11895 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11896
11897 return statement;
11898 }
11899
11900 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11901 We need to solve both at the same time because the method used
11902 depends on the existence of members begin or end.
11903 Returns the type deduced for the iterator expression. */
11904
11905 static tree
11906 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11907 {
11908 if (error_operand_p (range))
11909 {
11910 *begin = *end = error_mark_node;
11911 return error_mark_node;
11912 }
11913
11914 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11915 {
11916 error ("range-based %<for%> expression of type %qT "
11917 "has incomplete type", TREE_TYPE (range));
11918 *begin = *end = error_mark_node;
11919 return error_mark_node;
11920 }
11921 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11922 {
11923 /* If RANGE is an array, we will use pointer arithmetic. */
11924 *begin = decay_conversion (range, tf_warning_or_error);
11925 *end = build_binary_op (input_location, PLUS_EXPR,
11926 range,
11927 array_type_nelts_top (TREE_TYPE (range)),
11928 0);
11929 return TREE_TYPE (*begin);
11930 }
11931 else
11932 {
11933 /* If it is not an array, we must do a bit of magic. */
11934 tree id_begin, id_end;
11935 tree member_begin, member_end;
11936
11937 *begin = *end = error_mark_node;
11938
11939 id_begin = get_identifier ("begin");
11940 id_end = get_identifier ("end");
11941 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11942 /*protect=*/2, /*want_type=*/false,
11943 tf_warning_or_error);
11944 member_end = lookup_member (TREE_TYPE (range), id_end,
11945 /*protect=*/2, /*want_type=*/false,
11946 tf_warning_or_error);
11947
11948 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11949 {
11950 /* Use the member functions. */
11951 if (member_begin != NULL_TREE)
11952 *begin = cp_parser_range_for_member_function (range, id_begin);
11953 else
11954 error ("range-based %<for%> expression of type %qT has an "
11955 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11956
11957 if (member_end != NULL_TREE)
11958 *end = cp_parser_range_for_member_function (range, id_end);
11959 else
11960 error ("range-based %<for%> expression of type %qT has a "
11961 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11962 }
11963 else
11964 {
11965 /* Use global functions with ADL. */
11966 vec<tree, va_gc> *vec;
11967 vec = make_tree_vector ();
11968
11969 vec_safe_push (vec, range);
11970
11971 member_begin = perform_koenig_lookup (id_begin, vec,
11972 tf_warning_or_error);
11973 *begin = finish_call_expr (member_begin, &vec, false, true,
11974 tf_warning_or_error);
11975 member_end = perform_koenig_lookup (id_end, vec,
11976 tf_warning_or_error);
11977 *end = finish_call_expr (member_end, &vec, false, true,
11978 tf_warning_or_error);
11979
11980 release_tree_vector (vec);
11981 }
11982
11983 /* Last common checks. */
11984 if (*begin == error_mark_node || *end == error_mark_node)
11985 {
11986 /* If one of the expressions is an error do no more checks. */
11987 *begin = *end = error_mark_node;
11988 return error_mark_node;
11989 }
11990 else if (type_dependent_expression_p (*begin)
11991 || type_dependent_expression_p (*end))
11992 /* Can happen, when, eg, in a template context, Koenig lookup
11993 can't resolve begin/end (c++/58503). */
11994 return NULL_TREE;
11995 else
11996 {
11997 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11998 /* The unqualified type of the __begin and __end temporaries should
11999 be the same, as required by the multiple auto declaration. */
12000 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12001 {
12002 if (cxx_dialect >= cxx1z
12003 && (build_x_binary_op (input_location, NE_EXPR,
12004 *begin, ERROR_MARK,
12005 *end, ERROR_MARK,
12006 NULL, tf_none)
12007 != error_mark_node))
12008 /* P0184R0 allows __begin and __end to have different types,
12009 but make sure they are comparable so we can give a better
12010 diagnostic. */;
12011 else
12012 error ("inconsistent begin/end types in range-based %<for%> "
12013 "statement: %qT and %qT",
12014 TREE_TYPE (*begin), TREE_TYPE (*end));
12015 }
12016 return iter_type;
12017 }
12018 }
12019 }
12020
12021 /* Helper function for cp_parser_perform_range_for_lookup.
12022 Builds a tree for RANGE.IDENTIFIER(). */
12023
12024 static tree
12025 cp_parser_range_for_member_function (tree range, tree identifier)
12026 {
12027 tree member, res;
12028 vec<tree, va_gc> *vec;
12029
12030 member = finish_class_member_access_expr (range, identifier,
12031 false, tf_warning_or_error);
12032 if (member == error_mark_node)
12033 return error_mark_node;
12034
12035 vec = make_tree_vector ();
12036 res = finish_call_expr (member, &vec,
12037 /*disallow_virtual=*/false,
12038 /*koenig_p=*/false,
12039 tf_warning_or_error);
12040 release_tree_vector (vec);
12041 return res;
12042 }
12043
12044 /* Parse an iteration-statement.
12045
12046 iteration-statement:
12047 while ( condition ) statement
12048 do statement while ( expression ) ;
12049 for ( init-statement condition [opt] ; expression [opt] )
12050 statement
12051
12052 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12053
12054 static tree
12055 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12056 {
12057 cp_token *token;
12058 enum rid keyword;
12059 tree statement;
12060 unsigned char in_statement;
12061 token_indent_info guard_tinfo;
12062
12063 /* Peek at the next token. */
12064 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
12065 if (!token)
12066 return error_mark_node;
12067
12068 guard_tinfo = get_token_indent_info (token);
12069
12070 /* Remember whether or not we are already within an iteration
12071 statement. */
12072 in_statement = parser->in_statement;
12073
12074 /* See what kind of keyword it is. */
12075 keyword = token->keyword;
12076 switch (keyword)
12077 {
12078 case RID_WHILE:
12079 {
12080 tree condition;
12081
12082 /* Begin the while-statement. */
12083 statement = begin_while_stmt ();
12084 /* Look for the `('. */
12085 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12086 /* Parse the condition. */
12087 condition = cp_parser_condition (parser);
12088 finish_while_stmt_cond (condition, statement, ivdep);
12089 /* Look for the `)'. */
12090 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12091 /* Parse the dependent statement. */
12092 parser->in_statement = IN_ITERATION_STMT;
12093 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12094 parser->in_statement = in_statement;
12095 /* We're done with the while-statement. */
12096 finish_while_stmt (statement);
12097 }
12098 break;
12099
12100 case RID_DO:
12101 {
12102 tree expression;
12103
12104 /* Begin the do-statement. */
12105 statement = begin_do_stmt ();
12106 /* Parse the body of the do-statement. */
12107 parser->in_statement = IN_ITERATION_STMT;
12108 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12109 parser->in_statement = in_statement;
12110 finish_do_body (statement);
12111 /* Look for the `while' keyword. */
12112 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12113 /* Look for the `('. */
12114 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12115 /* Parse the expression. */
12116 expression = cp_parser_expression (parser);
12117 /* We're done with the do-statement. */
12118 finish_do_stmt (expression, statement, ivdep);
12119 /* Look for the `)'. */
12120 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12121 /* Look for the `;'. */
12122 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12123 }
12124 break;
12125
12126 case RID_FOR:
12127 {
12128 /* Look for the `('. */
12129 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12130
12131 statement = cp_parser_for (parser, ivdep);
12132
12133 /* Look for the `)'. */
12134 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12135
12136 /* Parse the body of the for-statement. */
12137 parser->in_statement = IN_ITERATION_STMT;
12138 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12139 parser->in_statement = in_statement;
12140
12141 /* We're done with the for-statement. */
12142 finish_for_stmt (statement);
12143 }
12144 break;
12145
12146 default:
12147 cp_parser_error (parser, "expected iteration-statement");
12148 statement = error_mark_node;
12149 break;
12150 }
12151
12152 return statement;
12153 }
12154
12155 /* Parse a init-statement or the declarator of a range-based-for.
12156 Returns true if a range-based-for declaration is seen.
12157
12158 init-statement:
12159 expression-statement
12160 simple-declaration */
12161
12162 static bool
12163 cp_parser_init_statement (cp_parser* parser, tree *decl)
12164 {
12165 /* If the next token is a `;', then we have an empty
12166 expression-statement. Grammatically, this is also a
12167 simple-declaration, but an invalid one, because it does not
12168 declare anything. Therefore, if we did not handle this case
12169 specially, we would issue an error message about an invalid
12170 declaration. */
12171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12172 {
12173 bool is_range_for = false;
12174 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12175
12176 /* A colon is used in range-based for. */
12177 parser->colon_corrects_to_scope_p = false;
12178
12179 /* We're going to speculatively look for a declaration, falling back
12180 to an expression, if necessary. */
12181 cp_parser_parse_tentatively (parser);
12182 /* Parse the declaration. */
12183 cp_parser_simple_declaration (parser,
12184 /*function_definition_allowed_p=*/false,
12185 decl);
12186 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12187 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12188 {
12189 /* It is a range-for, consume the ':' */
12190 cp_lexer_consume_token (parser->lexer);
12191 is_range_for = true;
12192 if (cxx_dialect < cxx11)
12193 {
12194 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12195 "range-based %<for%> loops only available with "
12196 "-std=c++11 or -std=gnu++11");
12197 *decl = error_mark_node;
12198 }
12199 }
12200 else
12201 /* The ';' is not consumed yet because we told
12202 cp_parser_simple_declaration not to. */
12203 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12204
12205 if (cp_parser_parse_definitely (parser))
12206 return is_range_for;
12207 /* If the tentative parse failed, then we shall need to look for an
12208 expression-statement. */
12209 }
12210 /* If we are here, it is an expression-statement. */
12211 cp_parser_expression_statement (parser, NULL_TREE);
12212 return false;
12213 }
12214
12215 /* Parse a jump-statement.
12216
12217 jump-statement:
12218 break ;
12219 continue ;
12220 return expression [opt] ;
12221 return braced-init-list ;
12222 goto identifier ;
12223
12224 GNU extension:
12225
12226 jump-statement:
12227 goto * expression ;
12228
12229 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12230
12231 static tree
12232 cp_parser_jump_statement (cp_parser* parser)
12233 {
12234 tree statement = error_mark_node;
12235 cp_token *token;
12236 enum rid keyword;
12237 unsigned char in_statement;
12238
12239 /* Peek at the next token. */
12240 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12241 if (!token)
12242 return error_mark_node;
12243
12244 /* See what kind of keyword it is. */
12245 keyword = token->keyword;
12246 switch (keyword)
12247 {
12248 case RID_BREAK:
12249 in_statement = parser->in_statement & ~IN_IF_STMT;
12250 switch (in_statement)
12251 {
12252 case 0:
12253 error_at (token->location, "break statement not within loop or switch");
12254 break;
12255 default:
12256 gcc_assert ((in_statement & IN_SWITCH_STMT)
12257 || in_statement == IN_ITERATION_STMT);
12258 statement = finish_break_stmt ();
12259 if (in_statement == IN_ITERATION_STMT)
12260 break_maybe_infinite_loop ();
12261 break;
12262 case IN_OMP_BLOCK:
12263 error_at (token->location, "invalid exit from OpenMP structured block");
12264 break;
12265 case IN_OMP_FOR:
12266 error_at (token->location, "break statement used with OpenMP for loop");
12267 break;
12268 case IN_CILK_SIMD_FOR:
12269 error_at (token->location, "break statement used with Cilk Plus for loop");
12270 break;
12271 }
12272 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12273 break;
12274
12275 case RID_CONTINUE:
12276 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12277 {
12278 case 0:
12279 error_at (token->location, "continue statement not within a loop");
12280 break;
12281 case IN_CILK_SIMD_FOR:
12282 error_at (token->location,
12283 "continue statement within %<#pragma simd%> loop body");
12284 /* Fall through. */
12285 case IN_ITERATION_STMT:
12286 case IN_OMP_FOR:
12287 statement = finish_continue_stmt ();
12288 break;
12289 case IN_OMP_BLOCK:
12290 error_at (token->location, "invalid exit from OpenMP structured block");
12291 break;
12292 default:
12293 gcc_unreachable ();
12294 }
12295 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12296 break;
12297
12298 case RID_RETURN:
12299 {
12300 tree expr;
12301 bool expr_non_constant_p;
12302
12303 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12304 {
12305 cp_lexer_set_source_position (parser->lexer);
12306 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12307 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12308 }
12309 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12310 expr = cp_parser_expression (parser);
12311 else
12312 /* If the next token is a `;', then there is no
12313 expression. */
12314 expr = NULL_TREE;
12315 /* Build the return-statement. */
12316 if (current_function_auto_return_pattern && in_discarded_stmt)
12317 /* Don't deduce from a discarded return statement. */;
12318 else
12319 statement = finish_return_stmt (expr);
12320 /* Look for the final `;'. */
12321 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12322 }
12323 break;
12324
12325 case RID_GOTO:
12326 if (parser->in_function_body
12327 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12328 {
12329 error ("%<goto%> in %<constexpr%> function");
12330 cp_function_chain->invalid_constexpr = true;
12331 }
12332
12333 /* Create the goto-statement. */
12334 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12335 {
12336 /* Issue a warning about this use of a GNU extension. */
12337 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12338 /* Consume the '*' token. */
12339 cp_lexer_consume_token (parser->lexer);
12340 /* Parse the dependent expression. */
12341 finish_goto_stmt (cp_parser_expression (parser));
12342 }
12343 else
12344 finish_goto_stmt (cp_parser_identifier (parser));
12345 /* Look for the final `;'. */
12346 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12347 break;
12348
12349 default:
12350 cp_parser_error (parser, "expected jump-statement");
12351 break;
12352 }
12353
12354 return statement;
12355 }
12356
12357 /* Parse a declaration-statement.
12358
12359 declaration-statement:
12360 block-declaration */
12361
12362 static void
12363 cp_parser_declaration_statement (cp_parser* parser)
12364 {
12365 void *p;
12366
12367 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12368 p = obstack_alloc (&declarator_obstack, 0);
12369
12370 /* Parse the block-declaration. */
12371 cp_parser_block_declaration (parser, /*statement_p=*/true);
12372
12373 /* Free any declarators allocated. */
12374 obstack_free (&declarator_obstack, p);
12375 }
12376
12377 /* Some dependent statements (like `if (cond) statement'), are
12378 implicitly in their own scope. In other words, if the statement is
12379 a single statement (as opposed to a compound-statement), it is
12380 none-the-less treated as if it were enclosed in braces. Any
12381 declarations appearing in the dependent statement are out of scope
12382 after control passes that point. This function parses a statement,
12383 but ensures that is in its own scope, even if it is not a
12384 compound-statement.
12385
12386 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12387 is a (possibly labeled) if statement which is not enclosed in
12388 braces and has an else clause. This is used to implement
12389 -Wparentheses.
12390
12391 CHAIN is a vector of if-else-if conditions. This is used to implement
12392 -Wduplicated-cond.
12393
12394 Returns the new statement. */
12395
12396 static tree
12397 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12398 const token_indent_info &guard_tinfo,
12399 vec<tree> *chain)
12400 {
12401 tree statement;
12402 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12403 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12404 token_indent_info body_tinfo
12405 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12406
12407 if (if_p != NULL)
12408 *if_p = false;
12409
12410 /* Mark if () ; with a special NOP_EXPR. */
12411 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12412 {
12413 cp_lexer_consume_token (parser->lexer);
12414 statement = add_stmt (build_empty_stmt (body_loc));
12415
12416 if (guard_tinfo.keyword == RID_IF
12417 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12418 warning_at (body_loc, OPT_Wempty_body,
12419 "suggest braces around empty body in an %<if%> statement");
12420 else if (guard_tinfo.keyword == RID_ELSE)
12421 warning_at (body_loc, OPT_Wempty_body,
12422 "suggest braces around empty body in an %<else%> statement");
12423 }
12424 /* if a compound is opened, we simply parse the statement directly. */
12425 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12426 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12427 /* If the token is not a `{', then we must take special action. */
12428 else
12429 {
12430 /* Create a compound-statement. */
12431 statement = begin_compound_stmt (0);
12432 /* Parse the dependent-statement. */
12433 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12434 &body_loc_after_labels);
12435 /* Finish the dummy compound-statement. */
12436 finish_compound_stmt (statement);
12437 }
12438
12439 token_indent_info next_tinfo
12440 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12441 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12442
12443 if (body_loc_after_labels != UNKNOWN_LOCATION
12444 && next_tinfo.type != CPP_SEMICOLON)
12445 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12446 guard_tinfo.location, guard_tinfo.keyword);
12447
12448 /* Return the statement. */
12449 return statement;
12450 }
12451
12452 /* For some dependent statements (like `while (cond) statement'), we
12453 have already created a scope. Therefore, even if the dependent
12454 statement is a compound-statement, we do not want to create another
12455 scope. */
12456
12457 static void
12458 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12459 const token_indent_info &guard_tinfo)
12460 {
12461 /* If the token is a `{', then we must take special action. */
12462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12463 {
12464 token_indent_info body_tinfo
12465 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12466 location_t loc_after_labels = UNKNOWN_LOCATION;
12467
12468 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12469 &loc_after_labels);
12470 token_indent_info next_tinfo
12471 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12472 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12473
12474 if (loc_after_labels != UNKNOWN_LOCATION
12475 && next_tinfo.type != CPP_SEMICOLON)
12476 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12477 guard_tinfo.location,
12478 guard_tinfo.keyword);
12479 }
12480 else
12481 {
12482 /* Avoid calling cp_parser_compound_statement, so that we
12483 don't create a new scope. Do everything else by hand. */
12484 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12485 /* If the next keyword is `__label__' we have a label declaration. */
12486 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12487 cp_parser_label_declaration (parser);
12488 /* Parse an (optional) statement-seq. */
12489 cp_parser_statement_seq_opt (parser, NULL_TREE);
12490 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12491 }
12492 }
12493
12494 /* Declarations [gram.dcl.dcl] */
12495
12496 /* Parse an optional declaration-sequence.
12497
12498 declaration-seq:
12499 declaration
12500 declaration-seq declaration */
12501
12502 static void
12503 cp_parser_declaration_seq_opt (cp_parser* parser)
12504 {
12505 while (true)
12506 {
12507 cp_token *token;
12508
12509 token = cp_lexer_peek_token (parser->lexer);
12510
12511 if (token->type == CPP_CLOSE_BRACE
12512 || token->type == CPP_EOF
12513 || token->type == CPP_PRAGMA_EOL)
12514 break;
12515
12516 if (token->type == CPP_SEMICOLON)
12517 {
12518 /* A declaration consisting of a single semicolon is
12519 invalid. Allow it unless we're being pedantic. */
12520 cp_lexer_consume_token (parser->lexer);
12521 if (!in_system_header_at (input_location))
12522 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12523 continue;
12524 }
12525
12526 /* If we're entering or exiting a region that's implicitly
12527 extern "C", modify the lang context appropriately. */
12528 if (!parser->implicit_extern_c && token->implicit_extern_c)
12529 {
12530 push_lang_context (lang_name_c);
12531 parser->implicit_extern_c = true;
12532 }
12533 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12534 {
12535 pop_lang_context ();
12536 parser->implicit_extern_c = false;
12537 }
12538
12539 if (token->type == CPP_PRAGMA)
12540 {
12541 /* A top-level declaration can consist solely of a #pragma.
12542 A nested declaration cannot, so this is done here and not
12543 in cp_parser_declaration. (A #pragma at block scope is
12544 handled in cp_parser_statement.) */
12545 cp_parser_pragma (parser, pragma_external, NULL);
12546 continue;
12547 }
12548
12549 /* Parse the declaration itself. */
12550 cp_parser_declaration (parser);
12551 }
12552 }
12553
12554 /* Parse a declaration.
12555
12556 declaration:
12557 block-declaration
12558 function-definition
12559 template-declaration
12560 explicit-instantiation
12561 explicit-specialization
12562 linkage-specification
12563 namespace-definition
12564
12565 C++17:
12566 deduction-guide
12567
12568 GNU extension:
12569
12570 declaration:
12571 __extension__ declaration */
12572
12573 static void
12574 cp_parser_declaration (cp_parser* parser)
12575 {
12576 cp_token token1;
12577 cp_token token2;
12578 int saved_pedantic;
12579 void *p;
12580 tree attributes = NULL_TREE;
12581
12582 /* Check for the `__extension__' keyword. */
12583 if (cp_parser_extension_opt (parser, &saved_pedantic))
12584 {
12585 /* Parse the qualified declaration. */
12586 cp_parser_declaration (parser);
12587 /* Restore the PEDANTIC flag. */
12588 pedantic = saved_pedantic;
12589
12590 return;
12591 }
12592
12593 /* Try to figure out what kind of declaration is present. */
12594 token1 = *cp_lexer_peek_token (parser->lexer);
12595
12596 if (token1.type != CPP_EOF)
12597 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12598 else
12599 {
12600 token2.type = CPP_EOF;
12601 token2.keyword = RID_MAX;
12602 }
12603
12604 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12605 p = obstack_alloc (&declarator_obstack, 0);
12606
12607 /* If the next token is `extern' and the following token is a string
12608 literal, then we have a linkage specification. */
12609 if (token1.keyword == RID_EXTERN
12610 && cp_parser_is_pure_string_literal (&token2))
12611 cp_parser_linkage_specification (parser);
12612 /* If the next token is `template', then we have either a template
12613 declaration, an explicit instantiation, or an explicit
12614 specialization. */
12615 else if (token1.keyword == RID_TEMPLATE)
12616 {
12617 /* `template <>' indicates a template specialization. */
12618 if (token2.type == CPP_LESS
12619 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12620 cp_parser_explicit_specialization (parser);
12621 /* `template <' indicates a template declaration. */
12622 else if (token2.type == CPP_LESS)
12623 cp_parser_template_declaration (parser, /*member_p=*/false);
12624 /* Anything else must be an explicit instantiation. */
12625 else
12626 cp_parser_explicit_instantiation (parser);
12627 }
12628 /* If the next token is `export', then we have a template
12629 declaration. */
12630 else if (token1.keyword == RID_EXPORT)
12631 cp_parser_template_declaration (parser, /*member_p=*/false);
12632 /* If the next token is `extern', 'static' or 'inline' and the one
12633 after that is `template', we have a GNU extended explicit
12634 instantiation directive. */
12635 else if (cp_parser_allow_gnu_extensions_p (parser)
12636 && (token1.keyword == RID_EXTERN
12637 || token1.keyword == RID_STATIC
12638 || token1.keyword == RID_INLINE)
12639 && token2.keyword == RID_TEMPLATE)
12640 cp_parser_explicit_instantiation (parser);
12641 /* If the next token is `namespace', check for a named or unnamed
12642 namespace definition. */
12643 else if (token1.keyword == RID_NAMESPACE
12644 && (/* A named namespace definition. */
12645 (token2.type == CPP_NAME
12646 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12647 != CPP_EQ))
12648 || (token2.type == CPP_OPEN_SQUARE
12649 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12650 == CPP_OPEN_SQUARE)
12651 /* An unnamed namespace definition. */
12652 || token2.type == CPP_OPEN_BRACE
12653 || token2.keyword == RID_ATTRIBUTE))
12654 cp_parser_namespace_definition (parser);
12655 /* An inline (associated) namespace definition. */
12656 else if (token1.keyword == RID_INLINE
12657 && token2.keyword == RID_NAMESPACE)
12658 cp_parser_namespace_definition (parser);
12659 /* Objective-C++ declaration/definition. */
12660 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12661 cp_parser_objc_declaration (parser, NULL_TREE);
12662 else if (c_dialect_objc ()
12663 && token1.keyword == RID_ATTRIBUTE
12664 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12665 cp_parser_objc_declaration (parser, attributes);
12666 /* At this point we may have a template declared by a concept
12667 introduction. */
12668 else if (flag_concepts
12669 && cp_parser_template_declaration_after_export (parser,
12670 /*member_p=*/false))
12671 /* We did. */;
12672 else
12673 /* Try to parse a block-declaration, or a function-definition. */
12674 cp_parser_block_declaration (parser, /*statement_p=*/false);
12675
12676 /* Free any declarators allocated. */
12677 obstack_free (&declarator_obstack, p);
12678 }
12679
12680 /* Parse a block-declaration.
12681
12682 block-declaration:
12683 simple-declaration
12684 asm-definition
12685 namespace-alias-definition
12686 using-declaration
12687 using-directive
12688
12689 GNU Extension:
12690
12691 block-declaration:
12692 __extension__ block-declaration
12693
12694 C++0x Extension:
12695
12696 block-declaration:
12697 static_assert-declaration
12698
12699 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12700 part of a declaration-statement. */
12701
12702 static void
12703 cp_parser_block_declaration (cp_parser *parser,
12704 bool statement_p)
12705 {
12706 cp_token *token1;
12707 int saved_pedantic;
12708
12709 /* Check for the `__extension__' keyword. */
12710 if (cp_parser_extension_opt (parser, &saved_pedantic))
12711 {
12712 /* Parse the qualified declaration. */
12713 cp_parser_block_declaration (parser, statement_p);
12714 /* Restore the PEDANTIC flag. */
12715 pedantic = saved_pedantic;
12716
12717 return;
12718 }
12719
12720 /* Peek at the next token to figure out which kind of declaration is
12721 present. */
12722 token1 = cp_lexer_peek_token (parser->lexer);
12723
12724 /* If the next keyword is `asm', we have an asm-definition. */
12725 if (token1->keyword == RID_ASM)
12726 {
12727 if (statement_p)
12728 cp_parser_commit_to_tentative_parse (parser);
12729 cp_parser_asm_definition (parser);
12730 }
12731 /* If the next keyword is `namespace', we have a
12732 namespace-alias-definition. */
12733 else if (token1->keyword == RID_NAMESPACE)
12734 cp_parser_namespace_alias_definition (parser);
12735 /* If the next keyword is `using', we have a
12736 using-declaration, a using-directive, or an alias-declaration. */
12737 else if (token1->keyword == RID_USING)
12738 {
12739 cp_token *token2;
12740
12741 if (statement_p)
12742 cp_parser_commit_to_tentative_parse (parser);
12743 /* If the token after `using' is `namespace', then we have a
12744 using-directive. */
12745 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12746 if (token2->keyword == RID_NAMESPACE)
12747 cp_parser_using_directive (parser);
12748 /* If the second token after 'using' is '=', then we have an
12749 alias-declaration. */
12750 else if (cxx_dialect >= cxx11
12751 && token2->type == CPP_NAME
12752 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12753 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12754 cp_parser_alias_declaration (parser);
12755 /* Otherwise, it's a using-declaration. */
12756 else
12757 cp_parser_using_declaration (parser,
12758 /*access_declaration_p=*/false);
12759 }
12760 /* If the next keyword is `__label__' we have a misplaced label
12761 declaration. */
12762 else if (token1->keyword == RID_LABEL)
12763 {
12764 cp_lexer_consume_token (parser->lexer);
12765 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12766 cp_parser_skip_to_end_of_statement (parser);
12767 /* If the next token is now a `;', consume it. */
12768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12769 cp_lexer_consume_token (parser->lexer);
12770 }
12771 /* If the next token is `static_assert' we have a static assertion. */
12772 else if (token1->keyword == RID_STATIC_ASSERT)
12773 cp_parser_static_assert (parser, /*member_p=*/false);
12774 /* Anything else must be a simple-declaration. */
12775 else
12776 cp_parser_simple_declaration (parser, !statement_p,
12777 /*maybe_range_for_decl*/NULL);
12778 }
12779
12780 /* Parse a simple-declaration.
12781
12782 simple-declaration:
12783 decl-specifier-seq [opt] init-declarator-list [opt] ;
12784 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12785 brace-or-equal-initializer ;
12786
12787 init-declarator-list:
12788 init-declarator
12789 init-declarator-list , init-declarator
12790
12791 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12792 function-definition as a simple-declaration.
12793
12794 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12795 parsed declaration if it is an uninitialized single declarator not followed
12796 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12797 if present, will not be consumed. */
12798
12799 static void
12800 cp_parser_simple_declaration (cp_parser* parser,
12801 bool function_definition_allowed_p,
12802 tree *maybe_range_for_decl)
12803 {
12804 cp_decl_specifier_seq decl_specifiers;
12805 int declares_class_or_enum;
12806 bool saw_declarator;
12807 location_t comma_loc = UNKNOWN_LOCATION;
12808 location_t init_loc = UNKNOWN_LOCATION;
12809
12810 if (maybe_range_for_decl)
12811 *maybe_range_for_decl = NULL_TREE;
12812
12813 /* Defer access checks until we know what is being declared; the
12814 checks for names appearing in the decl-specifier-seq should be
12815 done as if we were in the scope of the thing being declared. */
12816 push_deferring_access_checks (dk_deferred);
12817
12818 /* Parse the decl-specifier-seq. We have to keep track of whether
12819 or not the decl-specifier-seq declares a named class or
12820 enumeration type, since that is the only case in which the
12821 init-declarator-list is allowed to be empty.
12822
12823 [dcl.dcl]
12824
12825 In a simple-declaration, the optional init-declarator-list can be
12826 omitted only when declaring a class or enumeration, that is when
12827 the decl-specifier-seq contains either a class-specifier, an
12828 elaborated-type-specifier, or an enum-specifier. */
12829 cp_parser_decl_specifier_seq (parser,
12830 CP_PARSER_FLAGS_OPTIONAL,
12831 &decl_specifiers,
12832 &declares_class_or_enum);
12833 /* We no longer need to defer access checks. */
12834 stop_deferring_access_checks ();
12835
12836 /* In a block scope, a valid declaration must always have a
12837 decl-specifier-seq. By not trying to parse declarators, we can
12838 resolve the declaration/expression ambiguity more quickly. */
12839 if (!function_definition_allowed_p
12840 && !decl_specifiers.any_specifiers_p)
12841 {
12842 cp_parser_error (parser, "expected declaration");
12843 goto done;
12844 }
12845
12846 /* If the next two tokens are both identifiers, the code is
12847 erroneous. The usual cause of this situation is code like:
12848
12849 T t;
12850
12851 where "T" should name a type -- but does not. */
12852 if (!decl_specifiers.any_type_specifiers_p
12853 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12854 {
12855 /* If parsing tentatively, we should commit; we really are
12856 looking at a declaration. */
12857 cp_parser_commit_to_tentative_parse (parser);
12858 /* Give up. */
12859 goto done;
12860 }
12861
12862 /* If we have seen at least one decl-specifier, and the next token
12863 is not a parenthesis, then we must be looking at a declaration.
12864 (After "int (" we might be looking at a functional cast.) */
12865 if (decl_specifiers.any_specifiers_p
12866 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12867 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12868 && !cp_parser_error_occurred (parser))
12869 cp_parser_commit_to_tentative_parse (parser);
12870
12871 /* Look for C++17 decomposition declaration. */
12872 for (size_t n = 1; ; n++)
12873 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12874 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12875 continue;
12876 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12877 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12878 && decl_specifiers.any_specifiers_p)
12879 {
12880 tree decl
12881 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12882 maybe_range_for_decl,
12883 &init_loc);
12884
12885 /* The next token should be either a `,' or a `;'. */
12886 cp_token *token = cp_lexer_peek_token (parser->lexer);
12887 /* If it's a `;', we are done. */
12888 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12889 goto finish;
12890 /* Anything else is an error. */
12891 else
12892 {
12893 /* If we have already issued an error message we don't need
12894 to issue another one. */
12895 if ((decl != error_mark_node
12896 && DECL_INITIAL (decl) != error_mark_node)
12897 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12898 cp_parser_error (parser, "expected %<,%> or %<;%>");
12899 /* Skip tokens until we reach the end of the statement. */
12900 cp_parser_skip_to_end_of_statement (parser);
12901 /* If the next token is now a `;', consume it. */
12902 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12903 cp_lexer_consume_token (parser->lexer);
12904 goto done;
12905 }
12906 }
12907 else
12908 break;
12909
12910 tree last_type;
12911 bool auto_specifier_p;
12912 /* NULL_TREE if both variable and function declaration are allowed,
12913 error_mark_node if function declaration are not allowed and
12914 a FUNCTION_DECL that should be diagnosed if it is followed by
12915 variable declarations. */
12916 tree auto_function_declaration;
12917
12918 last_type = NULL_TREE;
12919 auto_specifier_p
12920 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12921 auto_function_declaration = NULL_TREE;
12922
12923 /* Keep going until we hit the `;' at the end of the simple
12924 declaration. */
12925 saw_declarator = false;
12926 while (cp_lexer_next_token_is_not (parser->lexer,
12927 CPP_SEMICOLON))
12928 {
12929 cp_token *token;
12930 bool function_definition_p;
12931 tree decl;
12932 tree auto_result = NULL_TREE;
12933
12934 if (saw_declarator)
12935 {
12936 /* If we are processing next declarator, comma is expected */
12937 token = cp_lexer_peek_token (parser->lexer);
12938 gcc_assert (token->type == CPP_COMMA);
12939 cp_lexer_consume_token (parser->lexer);
12940 if (maybe_range_for_decl)
12941 {
12942 *maybe_range_for_decl = error_mark_node;
12943 if (comma_loc == UNKNOWN_LOCATION)
12944 comma_loc = token->location;
12945 }
12946 }
12947 else
12948 saw_declarator = true;
12949
12950 /* Parse the init-declarator. */
12951 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12952 /*checks=*/NULL,
12953 function_definition_allowed_p,
12954 /*member_p=*/false,
12955 declares_class_or_enum,
12956 &function_definition_p,
12957 maybe_range_for_decl,
12958 &init_loc,
12959 &auto_result);
12960 /* If an error occurred while parsing tentatively, exit quickly.
12961 (That usually happens when in the body of a function; each
12962 statement is treated as a declaration-statement until proven
12963 otherwise.) */
12964 if (cp_parser_error_occurred (parser))
12965 goto done;
12966
12967 if (auto_specifier_p && cxx_dialect >= cxx14)
12968 {
12969 /* If the init-declarator-list contains more than one
12970 init-declarator, they shall all form declarations of
12971 variables. */
12972 if (auto_function_declaration == NULL_TREE)
12973 auto_function_declaration
12974 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12975 else if (TREE_CODE (decl) == FUNCTION_DECL
12976 || auto_function_declaration != error_mark_node)
12977 {
12978 error_at (decl_specifiers.locations[ds_type_spec],
12979 "non-variable %qD in declaration with more than one "
12980 "declarator with placeholder type",
12981 TREE_CODE (decl) == FUNCTION_DECL
12982 ? decl : auto_function_declaration);
12983 auto_function_declaration = error_mark_node;
12984 }
12985 }
12986
12987 if (auto_result
12988 && (!processing_template_decl || !type_uses_auto (auto_result)))
12989 {
12990 if (last_type
12991 && last_type != error_mark_node
12992 && !same_type_p (auto_result, last_type))
12993 {
12994 /* If the list of declarators contains more than one declarator,
12995 the type of each declared variable is determined as described
12996 above. If the type deduced for the template parameter U is not
12997 the same in each deduction, the program is ill-formed. */
12998 error_at (decl_specifiers.locations[ds_type_spec],
12999 "inconsistent deduction for %qT: %qT and then %qT",
13000 decl_specifiers.type, last_type, auto_result);
13001 last_type = error_mark_node;
13002 }
13003 else
13004 last_type = auto_result;
13005 }
13006
13007 /* Handle function definitions specially. */
13008 if (function_definition_p)
13009 {
13010 /* If the next token is a `,', then we are probably
13011 processing something like:
13012
13013 void f() {}, *p;
13014
13015 which is erroneous. */
13016 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13017 {
13018 cp_token *token = cp_lexer_peek_token (parser->lexer);
13019 error_at (token->location,
13020 "mixing"
13021 " declarations and function-definitions is forbidden");
13022 }
13023 /* Otherwise, we're done with the list of declarators. */
13024 else
13025 {
13026 pop_deferring_access_checks ();
13027 return;
13028 }
13029 }
13030 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13031 *maybe_range_for_decl = decl;
13032 /* The next token should be either a `,' or a `;'. */
13033 token = cp_lexer_peek_token (parser->lexer);
13034 /* If it's a `,', there are more declarators to come. */
13035 if (token->type == CPP_COMMA)
13036 /* will be consumed next time around */;
13037 /* If it's a `;', we are done. */
13038 else if (token->type == CPP_SEMICOLON)
13039 break;
13040 else if (maybe_range_for_decl)
13041 {
13042 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13043 permerror (decl_specifiers.locations[ds_type_spec],
13044 "types may not be defined in a for-range-declaration");
13045 break;
13046 }
13047 /* Anything else is an error. */
13048 else
13049 {
13050 /* If we have already issued an error message we don't need
13051 to issue another one. */
13052 if ((decl != error_mark_node
13053 && DECL_INITIAL (decl) != error_mark_node)
13054 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13055 cp_parser_error (parser, "expected %<,%> or %<;%>");
13056 /* Skip tokens until we reach the end of the statement. */
13057 cp_parser_skip_to_end_of_statement (parser);
13058 /* If the next token is now a `;', consume it. */
13059 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13060 cp_lexer_consume_token (parser->lexer);
13061 goto done;
13062 }
13063 /* After the first time around, a function-definition is not
13064 allowed -- even if it was OK at first. For example:
13065
13066 int i, f() {}
13067
13068 is not valid. */
13069 function_definition_allowed_p = false;
13070 }
13071
13072 /* Issue an error message if no declarators are present, and the
13073 decl-specifier-seq does not itself declare a class or
13074 enumeration: [dcl.dcl]/3. */
13075 if (!saw_declarator)
13076 {
13077 if (cp_parser_declares_only_class_p (parser))
13078 {
13079 if (!declares_class_or_enum
13080 && decl_specifiers.type
13081 && OVERLOAD_TYPE_P (decl_specifiers.type))
13082 /* Ensure an error is issued anyway when finish_decltype_type,
13083 called via cp_parser_decl_specifier_seq, returns a class or
13084 an enumeration (c++/51786). */
13085 decl_specifiers.type = NULL_TREE;
13086 shadow_tag (&decl_specifiers);
13087 }
13088 /* Perform any deferred access checks. */
13089 perform_deferred_access_checks (tf_warning_or_error);
13090 }
13091
13092 /* Consume the `;'. */
13093 finish:
13094 if (!maybe_range_for_decl)
13095 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13096 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13097 {
13098 if (init_loc != UNKNOWN_LOCATION)
13099 error_at (init_loc, "initializer in range-based %<for%> loop");
13100 if (comma_loc != UNKNOWN_LOCATION)
13101 error_at (comma_loc,
13102 "multiple declarations in range-based %<for%> loop");
13103 }
13104
13105 done:
13106 pop_deferring_access_checks ();
13107 }
13108
13109 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13110 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13111 initializer ; */
13112
13113 static tree
13114 cp_parser_decomposition_declaration (cp_parser *parser,
13115 cp_decl_specifier_seq *decl_specifiers,
13116 tree *maybe_range_for_decl,
13117 location_t *init_loc)
13118 {
13119 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13120 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13121 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13122
13123 /* Parse the identifier-list. */
13124 auto_vec<cp_expr, 10> v;
13125 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13126 while (true)
13127 {
13128 cp_expr e = cp_parser_identifier (parser);
13129 if (e.get_value () == error_mark_node)
13130 break;
13131 v.safe_push (e);
13132 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13133 break;
13134 cp_lexer_consume_token (parser->lexer);
13135 }
13136
13137 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13138 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13139 {
13140 end_loc = UNKNOWN_LOCATION;
13141 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13142 false);
13143 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13144 cp_lexer_consume_token (parser->lexer);
13145 else
13146 {
13147 cp_parser_skip_to_end_of_statement (parser);
13148 return error_mark_node;
13149 }
13150 }
13151
13152 if (cxx_dialect < cxx1z)
13153 pedwarn (loc, 0, "decomposition declaration only available with "
13154 "-std=c++1z or -std=gnu++1z");
13155
13156 tree pushed_scope;
13157 cp_declarator *declarator = make_declarator (cdk_decomp);
13158 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13159 declarator->id_loc = loc;
13160 if (ref_qual != REF_QUAL_NONE)
13161 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13162 ref_qual == REF_QUAL_RVALUE,
13163 NULL_TREE);
13164 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13165 NULL_TREE, decl_specifiers->attributes,
13166 &pushed_scope);
13167 tree orig_decl = decl;
13168
13169 unsigned int i;
13170 cp_expr e;
13171 cp_decl_specifier_seq decl_specs;
13172 clear_decl_specs (&decl_specs);
13173 decl_specs.type = make_auto ();
13174 tree prev = decl;
13175 FOR_EACH_VEC_ELT (v, i, e)
13176 {
13177 if (i == 0)
13178 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13179 else
13180 declarator->u.id.unqualified_name = e.get_value ();
13181 declarator->id_loc = e.get_location ();
13182 tree elt_pushed_scope;
13183 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13184 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13185 if (decl2 == error_mark_node)
13186 decl = error_mark_node;
13187 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13188 {
13189 /* Ensure we've diagnosed redeclaration if we aren't creating
13190 a new VAR_DECL. */
13191 gcc_assert (errorcount);
13192 decl = error_mark_node;
13193 }
13194 else
13195 prev = decl2;
13196 if (elt_pushed_scope)
13197 pop_scope (elt_pushed_scope);
13198 }
13199
13200 if (v.is_empty ())
13201 {
13202 error_at (loc, "empty decomposition declaration");
13203 decl = error_mark_node;
13204 }
13205
13206 if (maybe_range_for_decl == NULL
13207 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13208 {
13209 bool non_constant_p = false, is_direct_init = false;
13210 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13211 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13212 &non_constant_p);
13213
13214 if (decl != error_mark_node)
13215 {
13216 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13217 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13218 cp_finish_decomp (decl, prev, v.length ());
13219 }
13220 }
13221 else if (decl != error_mark_node)
13222 {
13223 *maybe_range_for_decl = prev;
13224 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13225 the underlying DECL. */
13226 cp_finish_decomp (decl, prev, v.length ());
13227 }
13228
13229 if (pushed_scope)
13230 pop_scope (pushed_scope);
13231
13232 if (decl == error_mark_node && DECL_P (orig_decl))
13233 {
13234 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13235 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13236 }
13237
13238 return decl;
13239 }
13240
13241 /* Parse a decl-specifier-seq.
13242
13243 decl-specifier-seq:
13244 decl-specifier-seq [opt] decl-specifier
13245 decl-specifier attribute-specifier-seq [opt] (C++11)
13246
13247 decl-specifier:
13248 storage-class-specifier
13249 type-specifier
13250 function-specifier
13251 friend
13252 typedef
13253
13254 GNU Extension:
13255
13256 decl-specifier:
13257 attributes
13258
13259 Concepts Extension:
13260
13261 decl-specifier:
13262 concept
13263
13264 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13265
13266 The parser flags FLAGS is used to control type-specifier parsing.
13267
13268 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13269 flags:
13270
13271 1: one of the decl-specifiers is an elaborated-type-specifier
13272 (i.e., a type declaration)
13273 2: one of the decl-specifiers is an enum-specifier or a
13274 class-specifier (i.e., a type definition)
13275
13276 */
13277
13278 static void
13279 cp_parser_decl_specifier_seq (cp_parser* parser,
13280 cp_parser_flags flags,
13281 cp_decl_specifier_seq *decl_specs,
13282 int* declares_class_or_enum)
13283 {
13284 bool constructor_possible_p = !parser->in_declarator_p;
13285 bool found_decl_spec = false;
13286 cp_token *start_token = NULL;
13287 cp_decl_spec ds;
13288
13289 /* Clear DECL_SPECS. */
13290 clear_decl_specs (decl_specs);
13291
13292 /* Assume no class or enumeration type is declared. */
13293 *declares_class_or_enum = 0;
13294
13295 /* Keep reading specifiers until there are no more to read. */
13296 while (true)
13297 {
13298 bool constructor_p;
13299 cp_token *token;
13300 ds = ds_last;
13301
13302 /* Peek at the next token. */
13303 token = cp_lexer_peek_token (parser->lexer);
13304
13305 /* Save the first token of the decl spec list for error
13306 reporting. */
13307 if (!start_token)
13308 start_token = token;
13309 /* Handle attributes. */
13310 if (cp_next_tokens_can_be_attribute_p (parser))
13311 {
13312 /* Parse the attributes. */
13313 tree attrs = cp_parser_attributes_opt (parser);
13314
13315 /* In a sequence of declaration specifiers, c++11 attributes
13316 appertain to the type that precede them. In that case
13317 [dcl.spec]/1 says:
13318
13319 The attribute-specifier-seq affects the type only for
13320 the declaration it appears in, not other declarations
13321 involving the same type.
13322
13323 But for now let's force the user to position the
13324 attribute either at the beginning of the declaration or
13325 after the declarator-id, which would clearly mean that it
13326 applies to the declarator. */
13327 if (cxx11_attribute_p (attrs))
13328 {
13329 if (!found_decl_spec)
13330 /* The c++11 attribute is at the beginning of the
13331 declaration. It appertains to the entity being
13332 declared. */;
13333 else
13334 {
13335 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13336 {
13337 /* This is an attribute following a
13338 class-specifier. */
13339 if (decl_specs->type_definition_p)
13340 warn_misplaced_attr_for_class_type (token->location,
13341 decl_specs->type);
13342 attrs = NULL_TREE;
13343 }
13344 else
13345 {
13346 decl_specs->std_attributes
13347 = chainon (decl_specs->std_attributes,
13348 attrs);
13349 if (decl_specs->locations[ds_std_attribute] == 0)
13350 decl_specs->locations[ds_std_attribute] = token->location;
13351 }
13352 continue;
13353 }
13354 }
13355
13356 decl_specs->attributes
13357 = chainon (decl_specs->attributes,
13358 attrs);
13359 if (decl_specs->locations[ds_attribute] == 0)
13360 decl_specs->locations[ds_attribute] = token->location;
13361 continue;
13362 }
13363 /* Assume we will find a decl-specifier keyword. */
13364 found_decl_spec = true;
13365 /* If the next token is an appropriate keyword, we can simply
13366 add it to the list. */
13367 switch (token->keyword)
13368 {
13369 /* decl-specifier:
13370 friend
13371 constexpr */
13372 case RID_FRIEND:
13373 if (!at_class_scope_p ())
13374 {
13375 error_at (token->location, "%<friend%> used outside of class");
13376 cp_lexer_purge_token (parser->lexer);
13377 }
13378 else
13379 {
13380 ds = ds_friend;
13381 /* Consume the token. */
13382 cp_lexer_consume_token (parser->lexer);
13383 }
13384 break;
13385
13386 case RID_CONSTEXPR:
13387 ds = ds_constexpr;
13388 cp_lexer_consume_token (parser->lexer);
13389 break;
13390
13391 case RID_CONCEPT:
13392 ds = ds_concept;
13393 cp_lexer_consume_token (parser->lexer);
13394 break;
13395
13396 /* function-specifier:
13397 inline
13398 virtual
13399 explicit */
13400 case RID_INLINE:
13401 case RID_VIRTUAL:
13402 case RID_EXPLICIT:
13403 cp_parser_function_specifier_opt (parser, decl_specs);
13404 break;
13405
13406 /* decl-specifier:
13407 typedef */
13408 case RID_TYPEDEF:
13409 ds = ds_typedef;
13410 /* Consume the token. */
13411 cp_lexer_consume_token (parser->lexer);
13412 /* A constructor declarator cannot appear in a typedef. */
13413 constructor_possible_p = false;
13414 /* The "typedef" keyword can only occur in a declaration; we
13415 may as well commit at this point. */
13416 cp_parser_commit_to_tentative_parse (parser);
13417
13418 if (decl_specs->storage_class != sc_none)
13419 decl_specs->conflicting_specifiers_p = true;
13420 break;
13421
13422 /* storage-class-specifier:
13423 auto
13424 register
13425 static
13426 extern
13427 mutable
13428
13429 GNU Extension:
13430 thread */
13431 case RID_AUTO:
13432 if (cxx_dialect == cxx98)
13433 {
13434 /* Consume the token. */
13435 cp_lexer_consume_token (parser->lexer);
13436
13437 /* Complain about `auto' as a storage specifier, if
13438 we're complaining about C++0x compatibility. */
13439 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13440 " changes meaning in C++11; please remove it");
13441
13442 /* Set the storage class anyway. */
13443 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13444 token);
13445 }
13446 else
13447 /* C++0x auto type-specifier. */
13448 found_decl_spec = false;
13449 break;
13450
13451 case RID_REGISTER:
13452 case RID_STATIC:
13453 case RID_EXTERN:
13454 case RID_MUTABLE:
13455 /* Consume the token. */
13456 cp_lexer_consume_token (parser->lexer);
13457 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13458 token);
13459 break;
13460 case RID_THREAD:
13461 /* Consume the token. */
13462 ds = ds_thread;
13463 cp_lexer_consume_token (parser->lexer);
13464 break;
13465
13466 default:
13467 /* We did not yet find a decl-specifier yet. */
13468 found_decl_spec = false;
13469 break;
13470 }
13471
13472 if (found_decl_spec
13473 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13474 && token->keyword != RID_CONSTEXPR)
13475 error ("decl-specifier invalid in condition");
13476
13477 if (found_decl_spec
13478 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13479 && token->keyword != RID_MUTABLE
13480 && token->keyword != RID_CONSTEXPR)
13481 error_at (token->location, "%qD invalid in lambda",
13482 ridpointers[token->keyword]);
13483
13484 if (ds != ds_last)
13485 set_and_check_decl_spec_loc (decl_specs, ds, token);
13486
13487 /* Constructors are a special case. The `S' in `S()' is not a
13488 decl-specifier; it is the beginning of the declarator. */
13489 constructor_p
13490 = (!found_decl_spec
13491 && constructor_possible_p
13492 && (cp_parser_constructor_declarator_p
13493 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13494
13495 /* If we don't have a DECL_SPEC yet, then we must be looking at
13496 a type-specifier. */
13497 if (!found_decl_spec && !constructor_p)
13498 {
13499 int decl_spec_declares_class_or_enum;
13500 bool is_cv_qualifier;
13501 tree type_spec;
13502
13503 type_spec
13504 = cp_parser_type_specifier (parser, flags,
13505 decl_specs,
13506 /*is_declaration=*/true,
13507 &decl_spec_declares_class_or_enum,
13508 &is_cv_qualifier);
13509 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13510
13511 /* If this type-specifier referenced a user-defined type
13512 (a typedef, class-name, etc.), then we can't allow any
13513 more such type-specifiers henceforth.
13514
13515 [dcl.spec]
13516
13517 The longest sequence of decl-specifiers that could
13518 possibly be a type name is taken as the
13519 decl-specifier-seq of a declaration. The sequence shall
13520 be self-consistent as described below.
13521
13522 [dcl.type]
13523
13524 As a general rule, at most one type-specifier is allowed
13525 in the complete decl-specifier-seq of a declaration. The
13526 only exceptions are the following:
13527
13528 -- const or volatile can be combined with any other
13529 type-specifier.
13530
13531 -- signed or unsigned can be combined with char, long,
13532 short, or int.
13533
13534 -- ..
13535
13536 Example:
13537
13538 typedef char* Pc;
13539 void g (const int Pc);
13540
13541 Here, Pc is *not* part of the decl-specifier seq; it's
13542 the declarator. Therefore, once we see a type-specifier
13543 (other than a cv-qualifier), we forbid any additional
13544 user-defined types. We *do* still allow things like `int
13545 int' to be considered a decl-specifier-seq, and issue the
13546 error message later. */
13547 if (type_spec && !is_cv_qualifier)
13548 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13549 /* A constructor declarator cannot follow a type-specifier. */
13550 if (type_spec)
13551 {
13552 constructor_possible_p = false;
13553 found_decl_spec = true;
13554 if (!is_cv_qualifier)
13555 decl_specs->any_type_specifiers_p = true;
13556 }
13557 }
13558
13559 /* If we still do not have a DECL_SPEC, then there are no more
13560 decl-specifiers. */
13561 if (!found_decl_spec)
13562 break;
13563
13564 decl_specs->any_specifiers_p = true;
13565 /* After we see one decl-specifier, further decl-specifiers are
13566 always optional. */
13567 flags |= CP_PARSER_FLAGS_OPTIONAL;
13568 }
13569
13570 /* Don't allow a friend specifier with a class definition. */
13571 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13572 && (*declares_class_or_enum & 2))
13573 error_at (decl_specs->locations[ds_friend],
13574 "class definition may not be declared a friend");
13575 }
13576
13577 /* Parse an (optional) storage-class-specifier.
13578
13579 storage-class-specifier:
13580 auto
13581 register
13582 static
13583 extern
13584 mutable
13585
13586 GNU Extension:
13587
13588 storage-class-specifier:
13589 thread
13590
13591 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13592
13593 static tree
13594 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13595 {
13596 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13597 {
13598 case RID_AUTO:
13599 if (cxx_dialect != cxx98)
13600 return NULL_TREE;
13601 /* Fall through for C++98. */
13602 gcc_fallthrough ();
13603
13604 case RID_REGISTER:
13605 case RID_STATIC:
13606 case RID_EXTERN:
13607 case RID_MUTABLE:
13608 case RID_THREAD:
13609 /* Consume the token. */
13610 return cp_lexer_consume_token (parser->lexer)->u.value;
13611
13612 default:
13613 return NULL_TREE;
13614 }
13615 }
13616
13617 /* Parse an (optional) function-specifier.
13618
13619 function-specifier:
13620 inline
13621 virtual
13622 explicit
13623
13624 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13625 Updates DECL_SPECS, if it is non-NULL. */
13626
13627 static tree
13628 cp_parser_function_specifier_opt (cp_parser* parser,
13629 cp_decl_specifier_seq *decl_specs)
13630 {
13631 cp_token *token = cp_lexer_peek_token (parser->lexer);
13632 switch (token->keyword)
13633 {
13634 case RID_INLINE:
13635 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13636 break;
13637
13638 case RID_VIRTUAL:
13639 /* 14.5.2.3 [temp.mem]
13640
13641 A member function template shall not be virtual. */
13642 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13643 && current_class_type)
13644 error_at (token->location, "templates may not be %<virtual%>");
13645 else
13646 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13647 break;
13648
13649 case RID_EXPLICIT:
13650 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13651 break;
13652
13653 default:
13654 return NULL_TREE;
13655 }
13656
13657 /* Consume the token. */
13658 return cp_lexer_consume_token (parser->lexer)->u.value;
13659 }
13660
13661 /* Parse a linkage-specification.
13662
13663 linkage-specification:
13664 extern string-literal { declaration-seq [opt] }
13665 extern string-literal declaration */
13666
13667 static void
13668 cp_parser_linkage_specification (cp_parser* parser)
13669 {
13670 tree linkage;
13671
13672 /* Look for the `extern' keyword. */
13673 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13674
13675 /* Look for the string-literal. */
13676 linkage = cp_parser_string_literal (parser, false, false);
13677
13678 /* Transform the literal into an identifier. If the literal is a
13679 wide-character string, or contains embedded NULs, then we can't
13680 handle it as the user wants. */
13681 if (strlen (TREE_STRING_POINTER (linkage))
13682 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13683 {
13684 cp_parser_error (parser, "invalid linkage-specification");
13685 /* Assume C++ linkage. */
13686 linkage = lang_name_cplusplus;
13687 }
13688 else
13689 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13690
13691 /* We're now using the new linkage. */
13692 push_lang_context (linkage);
13693
13694 /* If the next token is a `{', then we're using the first
13695 production. */
13696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13697 {
13698 cp_ensure_no_omp_declare_simd (parser);
13699 cp_ensure_no_oacc_routine (parser);
13700
13701 /* Consume the `{' token. */
13702 cp_lexer_consume_token (parser->lexer);
13703 /* Parse the declarations. */
13704 cp_parser_declaration_seq_opt (parser);
13705 /* Look for the closing `}'. */
13706 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13707 }
13708 /* Otherwise, there's just one declaration. */
13709 else
13710 {
13711 bool saved_in_unbraced_linkage_specification_p;
13712
13713 saved_in_unbraced_linkage_specification_p
13714 = parser->in_unbraced_linkage_specification_p;
13715 parser->in_unbraced_linkage_specification_p = true;
13716 cp_parser_declaration (parser);
13717 parser->in_unbraced_linkage_specification_p
13718 = saved_in_unbraced_linkage_specification_p;
13719 }
13720
13721 /* We're done with the linkage-specification. */
13722 pop_lang_context ();
13723 }
13724
13725 /* Parse a static_assert-declaration.
13726
13727 static_assert-declaration:
13728 static_assert ( constant-expression , string-literal ) ;
13729 static_assert ( constant-expression ) ; (C++1Z)
13730
13731 If MEMBER_P, this static_assert is a class member. */
13732
13733 static void
13734 cp_parser_static_assert(cp_parser *parser, bool member_p)
13735 {
13736 tree condition;
13737 tree message;
13738 cp_token *token;
13739 location_t saved_loc;
13740 bool dummy;
13741
13742 /* Peek at the `static_assert' token so we can keep track of exactly
13743 where the static assertion started. */
13744 token = cp_lexer_peek_token (parser->lexer);
13745 saved_loc = token->location;
13746
13747 /* Look for the `static_assert' keyword. */
13748 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13749 RT_STATIC_ASSERT))
13750 return;
13751
13752 /* We know we are in a static assertion; commit to any tentative
13753 parse. */
13754 if (cp_parser_parsing_tentatively (parser))
13755 cp_parser_commit_to_tentative_parse (parser);
13756
13757 /* Parse the `(' starting the static assertion condition. */
13758 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13759
13760 /* Parse the constant-expression. Allow a non-constant expression
13761 here in order to give better diagnostics in finish_static_assert. */
13762 condition =
13763 cp_parser_constant_expression (parser,
13764 /*allow_non_constant_p=*/true,
13765 /*non_constant_p=*/&dummy);
13766
13767 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13768 {
13769 if (cxx_dialect < cxx1z)
13770 pedwarn (input_location, OPT_Wpedantic,
13771 "static_assert without a message "
13772 "only available with -std=c++1z or -std=gnu++1z");
13773 /* Eat the ')' */
13774 cp_lexer_consume_token (parser->lexer);
13775 message = build_string (1, "");
13776 TREE_TYPE (message) = char_array_type_node;
13777 fix_string_type (message);
13778 }
13779 else
13780 {
13781 /* Parse the separating `,'. */
13782 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13783
13784 /* Parse the string-literal message. */
13785 message = cp_parser_string_literal (parser,
13786 /*translate=*/false,
13787 /*wide_ok=*/true);
13788
13789 /* A `)' completes the static assertion. */
13790 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13791 cp_parser_skip_to_closing_parenthesis (parser,
13792 /*recovering=*/true,
13793 /*or_comma=*/false,
13794 /*consume_paren=*/true);
13795 }
13796
13797 /* A semicolon terminates the declaration. */
13798 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13799
13800 /* Complete the static assertion, which may mean either processing
13801 the static assert now or saving it for template instantiation. */
13802 finish_static_assert (condition, message, saved_loc, member_p);
13803 }
13804
13805 /* Parse the expression in decltype ( expression ). */
13806
13807 static tree
13808 cp_parser_decltype_expr (cp_parser *parser,
13809 bool &id_expression_or_member_access_p)
13810 {
13811 cp_token *id_expr_start_token;
13812 tree expr;
13813
13814 /* Since we're going to preserve any side-effects from this parse, set up a
13815 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13816 in the expression. */
13817 tentative_firewall firewall (parser);
13818
13819 /* First, try parsing an id-expression. */
13820 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13821 cp_parser_parse_tentatively (parser);
13822 expr = cp_parser_id_expression (parser,
13823 /*template_keyword_p=*/false,
13824 /*check_dependency_p=*/true,
13825 /*template_p=*/NULL,
13826 /*declarator_p=*/false,
13827 /*optional_p=*/false);
13828
13829 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13830 {
13831 bool non_integral_constant_expression_p = false;
13832 tree id_expression = expr;
13833 cp_id_kind idk;
13834 const char *error_msg;
13835
13836 if (identifier_p (expr))
13837 /* Lookup the name we got back from the id-expression. */
13838 expr = cp_parser_lookup_name_simple (parser, expr,
13839 id_expr_start_token->location);
13840
13841 if (expr
13842 && expr != error_mark_node
13843 && TREE_CODE (expr) != TYPE_DECL
13844 && (TREE_CODE (expr) != BIT_NOT_EXPR
13845 || !TYPE_P (TREE_OPERAND (expr, 0)))
13846 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13847 {
13848 /* Complete lookup of the id-expression. */
13849 expr = (finish_id_expression
13850 (id_expression, expr, parser->scope, &idk,
13851 /*integral_constant_expression_p=*/false,
13852 /*allow_non_integral_constant_expression_p=*/true,
13853 &non_integral_constant_expression_p,
13854 /*template_p=*/false,
13855 /*done=*/true,
13856 /*address_p=*/false,
13857 /*template_arg_p=*/false,
13858 &error_msg,
13859 id_expr_start_token->location));
13860
13861 if (expr == error_mark_node)
13862 /* We found an id-expression, but it was something that we
13863 should not have found. This is an error, not something
13864 we can recover from, so note that we found an
13865 id-expression and we'll recover as gracefully as
13866 possible. */
13867 id_expression_or_member_access_p = true;
13868 }
13869
13870 if (expr
13871 && expr != error_mark_node
13872 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13873 /* We have an id-expression. */
13874 id_expression_or_member_access_p = true;
13875 }
13876
13877 if (!id_expression_or_member_access_p)
13878 {
13879 /* Abort the id-expression parse. */
13880 cp_parser_abort_tentative_parse (parser);
13881
13882 /* Parsing tentatively, again. */
13883 cp_parser_parse_tentatively (parser);
13884
13885 /* Parse a class member access. */
13886 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13887 /*cast_p=*/false, /*decltype*/true,
13888 /*member_access_only_p=*/true, NULL);
13889
13890 if (expr
13891 && expr != error_mark_node
13892 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13893 /* We have an id-expression. */
13894 id_expression_or_member_access_p = true;
13895 }
13896
13897 if (id_expression_or_member_access_p)
13898 /* We have parsed the complete id-expression or member access. */
13899 cp_parser_parse_definitely (parser);
13900 else
13901 {
13902 /* Abort our attempt to parse an id-expression or member access
13903 expression. */
13904 cp_parser_abort_tentative_parse (parser);
13905
13906 /* Parse a full expression. */
13907 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13908 /*decltype_p=*/true);
13909 }
13910
13911 return expr;
13912 }
13913
13914 /* Parse a `decltype' type. Returns the type.
13915
13916 simple-type-specifier:
13917 decltype ( expression )
13918 C++14 proposal:
13919 decltype ( auto ) */
13920
13921 static tree
13922 cp_parser_decltype (cp_parser *parser)
13923 {
13924 tree expr;
13925 bool id_expression_or_member_access_p = false;
13926 const char *saved_message;
13927 bool saved_integral_constant_expression_p;
13928 bool saved_non_integral_constant_expression_p;
13929 bool saved_greater_than_is_operator_p;
13930 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13931
13932 if (start_token->type == CPP_DECLTYPE)
13933 {
13934 /* Already parsed. */
13935 cp_lexer_consume_token (parser->lexer);
13936 return saved_checks_value (start_token->u.tree_check_value);
13937 }
13938
13939 /* Look for the `decltype' token. */
13940 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13941 return error_mark_node;
13942
13943 /* Parse the opening `('. */
13944 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13945 return error_mark_node;
13946
13947 /* decltype (auto) */
13948 if (cxx_dialect >= cxx14
13949 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13950 {
13951 cp_lexer_consume_token (parser->lexer);
13952 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13953 return error_mark_node;
13954 expr = make_decltype_auto ();
13955 AUTO_IS_DECLTYPE (expr) = true;
13956 goto rewrite;
13957 }
13958
13959 /* Types cannot be defined in a `decltype' expression. Save away the
13960 old message. */
13961 saved_message = parser->type_definition_forbidden_message;
13962
13963 /* And create the new one. */
13964 parser->type_definition_forbidden_message
13965 = G_("types may not be defined in %<decltype%> expressions");
13966
13967 /* The restrictions on constant-expressions do not apply inside
13968 decltype expressions. */
13969 saved_integral_constant_expression_p
13970 = parser->integral_constant_expression_p;
13971 saved_non_integral_constant_expression_p
13972 = parser->non_integral_constant_expression_p;
13973 parser->integral_constant_expression_p = false;
13974
13975 /* Within a parenthesized expression, a `>' token is always
13976 the greater-than operator. */
13977 saved_greater_than_is_operator_p
13978 = parser->greater_than_is_operator_p;
13979 parser->greater_than_is_operator_p = true;
13980
13981 /* Do not actually evaluate the expression. */
13982 ++cp_unevaluated_operand;
13983
13984 /* Do not warn about problems with the expression. */
13985 ++c_inhibit_evaluation_warnings;
13986
13987 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13988
13989 /* Go back to evaluating expressions. */
13990 --cp_unevaluated_operand;
13991 --c_inhibit_evaluation_warnings;
13992
13993 /* The `>' token might be the end of a template-id or
13994 template-parameter-list now. */
13995 parser->greater_than_is_operator_p
13996 = saved_greater_than_is_operator_p;
13997
13998 /* Restore the old message and the integral constant expression
13999 flags. */
14000 parser->type_definition_forbidden_message = saved_message;
14001 parser->integral_constant_expression_p
14002 = saved_integral_constant_expression_p;
14003 parser->non_integral_constant_expression_p
14004 = saved_non_integral_constant_expression_p;
14005
14006 /* Parse to the closing `)'. */
14007 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14008 {
14009 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14010 /*consume_paren=*/true);
14011 return error_mark_node;
14012 }
14013
14014 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14015 tf_warning_or_error);
14016
14017 rewrite:
14018 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14019 it again. */
14020 start_token->type = CPP_DECLTYPE;
14021 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14022 start_token->u.tree_check_value->value = expr;
14023 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14024 start_token->keyword = RID_MAX;
14025 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14026
14027 return expr;
14028 }
14029
14030 /* Special member functions [gram.special] */
14031
14032 /* Parse a conversion-function-id.
14033
14034 conversion-function-id:
14035 operator conversion-type-id
14036
14037 Returns an IDENTIFIER_NODE representing the operator. */
14038
14039 static tree
14040 cp_parser_conversion_function_id (cp_parser* parser)
14041 {
14042 tree type;
14043 tree saved_scope;
14044 tree saved_qualifying_scope;
14045 tree saved_object_scope;
14046 tree pushed_scope = NULL_TREE;
14047
14048 /* Look for the `operator' token. */
14049 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14050 return error_mark_node;
14051 /* When we parse the conversion-type-id, the current scope will be
14052 reset. However, we need that information in able to look up the
14053 conversion function later, so we save it here. */
14054 saved_scope = parser->scope;
14055 saved_qualifying_scope = parser->qualifying_scope;
14056 saved_object_scope = parser->object_scope;
14057 /* We must enter the scope of the class so that the names of
14058 entities declared within the class are available in the
14059 conversion-type-id. For example, consider:
14060
14061 struct S {
14062 typedef int I;
14063 operator I();
14064 };
14065
14066 S::operator I() { ... }
14067
14068 In order to see that `I' is a type-name in the definition, we
14069 must be in the scope of `S'. */
14070 if (saved_scope)
14071 pushed_scope = push_scope (saved_scope);
14072 /* Parse the conversion-type-id. */
14073 type = cp_parser_conversion_type_id (parser);
14074 /* Leave the scope of the class, if any. */
14075 if (pushed_scope)
14076 pop_scope (pushed_scope);
14077 /* Restore the saved scope. */
14078 parser->scope = saved_scope;
14079 parser->qualifying_scope = saved_qualifying_scope;
14080 parser->object_scope = saved_object_scope;
14081 /* If the TYPE is invalid, indicate failure. */
14082 if (type == error_mark_node)
14083 return error_mark_node;
14084 return mangle_conv_op_name_for_type (type);
14085 }
14086
14087 /* Parse a conversion-type-id:
14088
14089 conversion-type-id:
14090 type-specifier-seq conversion-declarator [opt]
14091
14092 Returns the TYPE specified. */
14093
14094 static tree
14095 cp_parser_conversion_type_id (cp_parser* parser)
14096 {
14097 tree attributes;
14098 cp_decl_specifier_seq type_specifiers;
14099 cp_declarator *declarator;
14100 tree type_specified;
14101 const char *saved_message;
14102
14103 /* Parse the attributes. */
14104 attributes = cp_parser_attributes_opt (parser);
14105
14106 saved_message = parser->type_definition_forbidden_message;
14107 parser->type_definition_forbidden_message
14108 = G_("types may not be defined in a conversion-type-id");
14109
14110 /* Parse the type-specifiers. */
14111 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14112 /*is_trailing_return=*/false,
14113 &type_specifiers);
14114
14115 parser->type_definition_forbidden_message = saved_message;
14116
14117 /* If that didn't work, stop. */
14118 if (type_specifiers.type == error_mark_node)
14119 return error_mark_node;
14120 /* Parse the conversion-declarator. */
14121 declarator = cp_parser_conversion_declarator_opt (parser);
14122
14123 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14124 /*initialized=*/0, &attributes);
14125 if (attributes)
14126 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14127
14128 /* Don't give this error when parsing tentatively. This happens to
14129 work because we always parse this definitively once. */
14130 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14131 && type_uses_auto (type_specified))
14132 {
14133 if (cxx_dialect < cxx14)
14134 {
14135 error ("invalid use of %<auto%> in conversion operator");
14136 return error_mark_node;
14137 }
14138 else if (template_parm_scope_p ())
14139 warning (0, "use of %<auto%> in member template "
14140 "conversion operator can never be deduced");
14141 }
14142
14143 return type_specified;
14144 }
14145
14146 /* Parse an (optional) conversion-declarator.
14147
14148 conversion-declarator:
14149 ptr-operator conversion-declarator [opt]
14150
14151 */
14152
14153 static cp_declarator *
14154 cp_parser_conversion_declarator_opt (cp_parser* parser)
14155 {
14156 enum tree_code code;
14157 tree class_type, std_attributes = NULL_TREE;
14158 cp_cv_quals cv_quals;
14159
14160 /* We don't know if there's a ptr-operator next, or not. */
14161 cp_parser_parse_tentatively (parser);
14162 /* Try the ptr-operator. */
14163 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14164 &std_attributes);
14165 /* If it worked, look for more conversion-declarators. */
14166 if (cp_parser_parse_definitely (parser))
14167 {
14168 cp_declarator *declarator;
14169
14170 /* Parse another optional declarator. */
14171 declarator = cp_parser_conversion_declarator_opt (parser);
14172
14173 declarator = cp_parser_make_indirect_declarator
14174 (code, class_type, cv_quals, declarator, std_attributes);
14175
14176 return declarator;
14177 }
14178
14179 return NULL;
14180 }
14181
14182 /* Parse an (optional) ctor-initializer.
14183
14184 ctor-initializer:
14185 : mem-initializer-list
14186
14187 Returns TRUE iff the ctor-initializer was actually present. */
14188
14189 static bool
14190 cp_parser_ctor_initializer_opt (cp_parser* parser)
14191 {
14192 /* If the next token is not a `:', then there is no
14193 ctor-initializer. */
14194 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14195 {
14196 /* Do default initialization of any bases and members. */
14197 if (DECL_CONSTRUCTOR_P (current_function_decl))
14198 finish_mem_initializers (NULL_TREE);
14199
14200 return false;
14201 }
14202
14203 /* Consume the `:' token. */
14204 cp_lexer_consume_token (parser->lexer);
14205 /* And the mem-initializer-list. */
14206 cp_parser_mem_initializer_list (parser);
14207
14208 return true;
14209 }
14210
14211 /* Parse a mem-initializer-list.
14212
14213 mem-initializer-list:
14214 mem-initializer ... [opt]
14215 mem-initializer ... [opt] , mem-initializer-list */
14216
14217 static void
14218 cp_parser_mem_initializer_list (cp_parser* parser)
14219 {
14220 tree mem_initializer_list = NULL_TREE;
14221 tree target_ctor = error_mark_node;
14222 cp_token *token = cp_lexer_peek_token (parser->lexer);
14223
14224 /* Let the semantic analysis code know that we are starting the
14225 mem-initializer-list. */
14226 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14227 error_at (token->location,
14228 "only constructors take member initializers");
14229
14230 /* Loop through the list. */
14231 while (true)
14232 {
14233 tree mem_initializer;
14234
14235 token = cp_lexer_peek_token (parser->lexer);
14236 /* Parse the mem-initializer. */
14237 mem_initializer = cp_parser_mem_initializer (parser);
14238 /* If the next token is a `...', we're expanding member initializers. */
14239 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14240 {
14241 /* Consume the `...'. */
14242 cp_lexer_consume_token (parser->lexer);
14243
14244 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14245 can be expanded but members cannot. */
14246 if (mem_initializer != error_mark_node
14247 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14248 {
14249 error_at (token->location,
14250 "cannot expand initializer for member %qD",
14251 TREE_PURPOSE (mem_initializer));
14252 mem_initializer = error_mark_node;
14253 }
14254
14255 /* Construct the pack expansion type. */
14256 if (mem_initializer != error_mark_node)
14257 mem_initializer = make_pack_expansion (mem_initializer);
14258 }
14259 if (target_ctor != error_mark_node
14260 && mem_initializer != error_mark_node)
14261 {
14262 error ("mem-initializer for %qD follows constructor delegation",
14263 TREE_PURPOSE (mem_initializer));
14264 mem_initializer = error_mark_node;
14265 }
14266 /* Look for a target constructor. */
14267 if (mem_initializer != error_mark_node
14268 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14269 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14270 {
14271 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14272 if (mem_initializer_list)
14273 {
14274 error ("constructor delegation follows mem-initializer for %qD",
14275 TREE_PURPOSE (mem_initializer_list));
14276 mem_initializer = error_mark_node;
14277 }
14278 target_ctor = mem_initializer;
14279 }
14280 /* Add it to the list, unless it was erroneous. */
14281 if (mem_initializer != error_mark_node)
14282 {
14283 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14284 mem_initializer_list = mem_initializer;
14285 }
14286 /* If the next token is not a `,', we're done. */
14287 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14288 break;
14289 /* Consume the `,' token. */
14290 cp_lexer_consume_token (parser->lexer);
14291 }
14292
14293 /* Perform semantic analysis. */
14294 if (DECL_CONSTRUCTOR_P (current_function_decl))
14295 finish_mem_initializers (mem_initializer_list);
14296 }
14297
14298 /* Parse a mem-initializer.
14299
14300 mem-initializer:
14301 mem-initializer-id ( expression-list [opt] )
14302 mem-initializer-id braced-init-list
14303
14304 GNU extension:
14305
14306 mem-initializer:
14307 ( expression-list [opt] )
14308
14309 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14310 class) or FIELD_DECL (for a non-static data member) to initialize;
14311 the TREE_VALUE is the expression-list. An empty initialization
14312 list is represented by void_list_node. */
14313
14314 static tree
14315 cp_parser_mem_initializer (cp_parser* parser)
14316 {
14317 tree mem_initializer_id;
14318 tree expression_list;
14319 tree member;
14320 cp_token *token = cp_lexer_peek_token (parser->lexer);
14321
14322 /* Find out what is being initialized. */
14323 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14324 {
14325 permerror (token->location,
14326 "anachronistic old-style base class initializer");
14327 mem_initializer_id = NULL_TREE;
14328 }
14329 else
14330 {
14331 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14332 if (mem_initializer_id == error_mark_node)
14333 return mem_initializer_id;
14334 }
14335 member = expand_member_init (mem_initializer_id);
14336 if (member && !DECL_P (member))
14337 in_base_initializer = 1;
14338
14339 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14340 {
14341 bool expr_non_constant_p;
14342 cp_lexer_set_source_position (parser->lexer);
14343 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14344 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14345 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14346 expression_list = build_tree_list (NULL_TREE, expression_list);
14347 }
14348 else
14349 {
14350 vec<tree, va_gc> *vec;
14351 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14352 /*cast_p=*/false,
14353 /*allow_expansion_p=*/true,
14354 /*non_constant_p=*/NULL);
14355 if (vec == NULL)
14356 return error_mark_node;
14357 expression_list = build_tree_list_vec (vec);
14358 release_tree_vector (vec);
14359 }
14360
14361 if (expression_list == error_mark_node)
14362 return error_mark_node;
14363 if (!expression_list)
14364 expression_list = void_type_node;
14365
14366 in_base_initializer = 0;
14367
14368 return member ? build_tree_list (member, expression_list) : error_mark_node;
14369 }
14370
14371 /* Parse a mem-initializer-id.
14372
14373 mem-initializer-id:
14374 :: [opt] nested-name-specifier [opt] class-name
14375 decltype-specifier (C++11)
14376 identifier
14377
14378 Returns a TYPE indicating the class to be initialized for the first
14379 production (and the second in C++11). Returns an IDENTIFIER_NODE
14380 indicating the data member to be initialized for the last production. */
14381
14382 static tree
14383 cp_parser_mem_initializer_id (cp_parser* parser)
14384 {
14385 bool global_scope_p;
14386 bool nested_name_specifier_p;
14387 bool template_p = false;
14388 tree id;
14389
14390 cp_token *token = cp_lexer_peek_token (parser->lexer);
14391
14392 /* `typename' is not allowed in this context ([temp.res]). */
14393 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14394 {
14395 error_at (token->location,
14396 "keyword %<typename%> not allowed in this context (a qualified "
14397 "member initializer is implicitly a type)");
14398 cp_lexer_consume_token (parser->lexer);
14399 }
14400 /* Look for the optional `::' operator. */
14401 global_scope_p
14402 = (cp_parser_global_scope_opt (parser,
14403 /*current_scope_valid_p=*/false)
14404 != NULL_TREE);
14405 /* Look for the optional nested-name-specifier. The simplest way to
14406 implement:
14407
14408 [temp.res]
14409
14410 The keyword `typename' is not permitted in a base-specifier or
14411 mem-initializer; in these contexts a qualified name that
14412 depends on a template-parameter is implicitly assumed to be a
14413 type name.
14414
14415 is to assume that we have seen the `typename' keyword at this
14416 point. */
14417 nested_name_specifier_p
14418 = (cp_parser_nested_name_specifier_opt (parser,
14419 /*typename_keyword_p=*/true,
14420 /*check_dependency_p=*/true,
14421 /*type_p=*/true,
14422 /*is_declaration=*/true)
14423 != NULL_TREE);
14424 if (nested_name_specifier_p)
14425 template_p = cp_parser_optional_template_keyword (parser);
14426 /* If there is a `::' operator or a nested-name-specifier, then we
14427 are definitely looking for a class-name. */
14428 if (global_scope_p || nested_name_specifier_p)
14429 return cp_parser_class_name (parser,
14430 /*typename_keyword_p=*/true,
14431 /*template_keyword_p=*/template_p,
14432 typename_type,
14433 /*check_dependency_p=*/true,
14434 /*class_head_p=*/false,
14435 /*is_declaration=*/true);
14436 /* Otherwise, we could also be looking for an ordinary identifier. */
14437 cp_parser_parse_tentatively (parser);
14438 if (cp_lexer_next_token_is_decltype (parser->lexer))
14439 /* Try a decltype-specifier. */
14440 id = cp_parser_decltype (parser);
14441 else
14442 /* Otherwise, try a class-name. */
14443 id = cp_parser_class_name (parser,
14444 /*typename_keyword_p=*/true,
14445 /*template_keyword_p=*/false,
14446 none_type,
14447 /*check_dependency_p=*/true,
14448 /*class_head_p=*/false,
14449 /*is_declaration=*/true);
14450 /* If we found one, we're done. */
14451 if (cp_parser_parse_definitely (parser))
14452 return id;
14453 /* Otherwise, look for an ordinary identifier. */
14454 return cp_parser_identifier (parser);
14455 }
14456
14457 /* Overloading [gram.over] */
14458
14459 /* Parse an operator-function-id.
14460
14461 operator-function-id:
14462 operator operator
14463
14464 Returns an IDENTIFIER_NODE for the operator which is a
14465 human-readable spelling of the identifier, e.g., `operator +'. */
14466
14467 static cp_expr
14468 cp_parser_operator_function_id (cp_parser* parser)
14469 {
14470 /* Look for the `operator' keyword. */
14471 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14472 return error_mark_node;
14473 /* And then the name of the operator itself. */
14474 return cp_parser_operator (parser);
14475 }
14476
14477 /* Return an identifier node for a user-defined literal operator.
14478 The suffix identifier is chained to the operator name identifier. */
14479
14480 tree
14481 cp_literal_operator_id (const char* name)
14482 {
14483 tree identifier;
14484 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14485 + strlen (name) + 10);
14486 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14487 identifier = get_identifier (buffer);
14488
14489 return identifier;
14490 }
14491
14492 /* Parse an operator.
14493
14494 operator:
14495 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14496 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14497 || ++ -- , ->* -> () []
14498
14499 GNU Extensions:
14500
14501 operator:
14502 <? >? <?= >?=
14503
14504 Returns an IDENTIFIER_NODE for the operator which is a
14505 human-readable spelling of the identifier, e.g., `operator +'. */
14506
14507 static cp_expr
14508 cp_parser_operator (cp_parser* parser)
14509 {
14510 tree id = NULL_TREE;
14511 cp_token *token;
14512 bool utf8 = false;
14513
14514 /* Peek at the next token. */
14515 token = cp_lexer_peek_token (parser->lexer);
14516
14517 location_t start_loc = token->location;
14518
14519 /* Figure out which operator we have. */
14520 switch (token->type)
14521 {
14522 case CPP_KEYWORD:
14523 {
14524 enum tree_code op;
14525
14526 /* The keyword should be either `new' or `delete'. */
14527 if (token->keyword == RID_NEW)
14528 op = NEW_EXPR;
14529 else if (token->keyword == RID_DELETE)
14530 op = DELETE_EXPR;
14531 else
14532 break;
14533
14534 /* Consume the `new' or `delete' token. */
14535 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14536
14537 /* Peek at the next token. */
14538 token = cp_lexer_peek_token (parser->lexer);
14539 /* If it's a `[' token then this is the array variant of the
14540 operator. */
14541 if (token->type == CPP_OPEN_SQUARE)
14542 {
14543 /* Consume the `[' token. */
14544 cp_lexer_consume_token (parser->lexer);
14545 /* Look for the `]' token. */
14546 if (cp_token *close_token
14547 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14548 end_loc = close_token->location;
14549 id = cp_operator_id (op == NEW_EXPR
14550 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14551 }
14552 /* Otherwise, we have the non-array variant. */
14553 else
14554 id = cp_operator_id (op);
14555
14556 location_t loc = make_location (start_loc, start_loc, end_loc);
14557
14558 return cp_expr (id, loc);
14559 }
14560
14561 case CPP_PLUS:
14562 id = cp_operator_id (PLUS_EXPR);
14563 break;
14564
14565 case CPP_MINUS:
14566 id = cp_operator_id (MINUS_EXPR);
14567 break;
14568
14569 case CPP_MULT:
14570 id = cp_operator_id (MULT_EXPR);
14571 break;
14572
14573 case CPP_DIV:
14574 id = cp_operator_id (TRUNC_DIV_EXPR);
14575 break;
14576
14577 case CPP_MOD:
14578 id = cp_operator_id (TRUNC_MOD_EXPR);
14579 break;
14580
14581 case CPP_XOR:
14582 id = cp_operator_id (BIT_XOR_EXPR);
14583 break;
14584
14585 case CPP_AND:
14586 id = cp_operator_id (BIT_AND_EXPR);
14587 break;
14588
14589 case CPP_OR:
14590 id = cp_operator_id (BIT_IOR_EXPR);
14591 break;
14592
14593 case CPP_COMPL:
14594 id = cp_operator_id (BIT_NOT_EXPR);
14595 break;
14596
14597 case CPP_NOT:
14598 id = cp_operator_id (TRUTH_NOT_EXPR);
14599 break;
14600
14601 case CPP_EQ:
14602 id = cp_assignment_operator_id (NOP_EXPR);
14603 break;
14604
14605 case CPP_LESS:
14606 id = cp_operator_id (LT_EXPR);
14607 break;
14608
14609 case CPP_GREATER:
14610 id = cp_operator_id (GT_EXPR);
14611 break;
14612
14613 case CPP_PLUS_EQ:
14614 id = cp_assignment_operator_id (PLUS_EXPR);
14615 break;
14616
14617 case CPP_MINUS_EQ:
14618 id = cp_assignment_operator_id (MINUS_EXPR);
14619 break;
14620
14621 case CPP_MULT_EQ:
14622 id = cp_assignment_operator_id (MULT_EXPR);
14623 break;
14624
14625 case CPP_DIV_EQ:
14626 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14627 break;
14628
14629 case CPP_MOD_EQ:
14630 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14631 break;
14632
14633 case CPP_XOR_EQ:
14634 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14635 break;
14636
14637 case CPP_AND_EQ:
14638 id = cp_assignment_operator_id (BIT_AND_EXPR);
14639 break;
14640
14641 case CPP_OR_EQ:
14642 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14643 break;
14644
14645 case CPP_LSHIFT:
14646 id = cp_operator_id (LSHIFT_EXPR);
14647 break;
14648
14649 case CPP_RSHIFT:
14650 id = cp_operator_id (RSHIFT_EXPR);
14651 break;
14652
14653 case CPP_LSHIFT_EQ:
14654 id = cp_assignment_operator_id (LSHIFT_EXPR);
14655 break;
14656
14657 case CPP_RSHIFT_EQ:
14658 id = cp_assignment_operator_id (RSHIFT_EXPR);
14659 break;
14660
14661 case CPP_EQ_EQ:
14662 id = cp_operator_id (EQ_EXPR);
14663 break;
14664
14665 case CPP_NOT_EQ:
14666 id = cp_operator_id (NE_EXPR);
14667 break;
14668
14669 case CPP_LESS_EQ:
14670 id = cp_operator_id (LE_EXPR);
14671 break;
14672
14673 case CPP_GREATER_EQ:
14674 id = cp_operator_id (GE_EXPR);
14675 break;
14676
14677 case CPP_AND_AND:
14678 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14679 break;
14680
14681 case CPP_OR_OR:
14682 id = cp_operator_id (TRUTH_ORIF_EXPR);
14683 break;
14684
14685 case CPP_PLUS_PLUS:
14686 id = cp_operator_id (POSTINCREMENT_EXPR);
14687 break;
14688
14689 case CPP_MINUS_MINUS:
14690 id = cp_operator_id (PREDECREMENT_EXPR);
14691 break;
14692
14693 case CPP_COMMA:
14694 id = cp_operator_id (COMPOUND_EXPR);
14695 break;
14696
14697 case CPP_DEREF_STAR:
14698 id = cp_operator_id (MEMBER_REF);
14699 break;
14700
14701 case CPP_DEREF:
14702 id = cp_operator_id (COMPONENT_REF);
14703 break;
14704
14705 case CPP_OPEN_PAREN:
14706 /* Consume the `('. */
14707 cp_lexer_consume_token (parser->lexer);
14708 /* Look for the matching `)'. */
14709 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14710 return cp_operator_id (CALL_EXPR);
14711
14712 case CPP_OPEN_SQUARE:
14713 /* Consume the `['. */
14714 cp_lexer_consume_token (parser->lexer);
14715 /* Look for the matching `]'. */
14716 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14717 return cp_operator_id (ARRAY_REF);
14718
14719 case CPP_UTF8STRING:
14720 case CPP_UTF8STRING_USERDEF:
14721 utf8 = true;
14722 /* FALLTHRU */
14723 case CPP_STRING:
14724 case CPP_WSTRING:
14725 case CPP_STRING16:
14726 case CPP_STRING32:
14727 case CPP_STRING_USERDEF:
14728 case CPP_WSTRING_USERDEF:
14729 case CPP_STRING16_USERDEF:
14730 case CPP_STRING32_USERDEF:
14731 {
14732 tree str, string_tree;
14733 int sz, len;
14734
14735 if (cxx_dialect == cxx98)
14736 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14737
14738 /* Consume the string. */
14739 str = cp_parser_string_literal (parser, /*translate=*/true,
14740 /*wide_ok=*/true, /*lookup_udlit=*/false);
14741 if (str == error_mark_node)
14742 return error_mark_node;
14743 else if (TREE_CODE (str) == USERDEF_LITERAL)
14744 {
14745 string_tree = USERDEF_LITERAL_VALUE (str);
14746 id = USERDEF_LITERAL_SUFFIX_ID (str);
14747 }
14748 else
14749 {
14750 string_tree = str;
14751 /* Look for the suffix identifier. */
14752 token = cp_lexer_peek_token (parser->lexer);
14753 if (token->type == CPP_NAME)
14754 id = cp_parser_identifier (parser);
14755 else if (token->type == CPP_KEYWORD)
14756 {
14757 error ("unexpected keyword;"
14758 " remove space between quotes and suffix identifier");
14759 return error_mark_node;
14760 }
14761 else
14762 {
14763 error ("expected suffix identifier");
14764 return error_mark_node;
14765 }
14766 }
14767 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14768 (TREE_TYPE (TREE_TYPE (string_tree))));
14769 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14770 if (len != 0)
14771 {
14772 error ("expected empty string after %<operator%> keyword");
14773 return error_mark_node;
14774 }
14775 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14776 != char_type_node)
14777 {
14778 error ("invalid encoding prefix in literal operator");
14779 return error_mark_node;
14780 }
14781 if (id != error_mark_node)
14782 {
14783 const char *name = IDENTIFIER_POINTER (id);
14784 id = cp_literal_operator_id (name);
14785 }
14786 return id;
14787 }
14788
14789 default:
14790 /* Anything else is an error. */
14791 break;
14792 }
14793
14794 /* If we have selected an identifier, we need to consume the
14795 operator token. */
14796 if (id)
14797 cp_lexer_consume_token (parser->lexer);
14798 /* Otherwise, no valid operator name was present. */
14799 else
14800 {
14801 cp_parser_error (parser, "expected operator");
14802 id = error_mark_node;
14803 }
14804
14805 return cp_expr (id, start_loc);
14806 }
14807
14808 /* Parse a template-declaration.
14809
14810 template-declaration:
14811 export [opt] template < template-parameter-list > declaration
14812
14813 If MEMBER_P is TRUE, this template-declaration occurs within a
14814 class-specifier.
14815
14816 The grammar rule given by the standard isn't correct. What
14817 is really meant is:
14818
14819 template-declaration:
14820 export [opt] template-parameter-list-seq
14821 decl-specifier-seq [opt] init-declarator [opt] ;
14822 export [opt] template-parameter-list-seq
14823 function-definition
14824
14825 template-parameter-list-seq:
14826 template-parameter-list-seq [opt]
14827 template < template-parameter-list >
14828
14829 Concept Extensions:
14830
14831 template-parameter-list-seq:
14832 template < template-parameter-list > requires-clause [opt]
14833
14834 requires-clause:
14835 requires logical-or-expression */
14836
14837 static void
14838 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14839 {
14840 /* Check for `export'. */
14841 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14842 {
14843 /* Consume the `export' token. */
14844 cp_lexer_consume_token (parser->lexer);
14845 /* Warn that we do not support `export'. */
14846 warning (0, "keyword %<export%> not implemented, and will be ignored");
14847 }
14848
14849 cp_parser_template_declaration_after_export (parser, member_p);
14850 }
14851
14852 /* Parse a template-parameter-list.
14853
14854 template-parameter-list:
14855 template-parameter
14856 template-parameter-list , template-parameter
14857
14858 Returns a TREE_LIST. Each node represents a template parameter.
14859 The nodes are connected via their TREE_CHAINs. */
14860
14861 static tree
14862 cp_parser_template_parameter_list (cp_parser* parser)
14863 {
14864 tree parameter_list = NULL_TREE;
14865
14866 begin_template_parm_list ();
14867
14868 /* The loop below parses the template parms. We first need to know
14869 the total number of template parms to be able to compute proper
14870 canonical types of each dependent type. So after the loop, when
14871 we know the total number of template parms,
14872 end_template_parm_list computes the proper canonical types and
14873 fixes up the dependent types accordingly. */
14874 while (true)
14875 {
14876 tree parameter;
14877 bool is_non_type;
14878 bool is_parameter_pack;
14879 location_t parm_loc;
14880
14881 /* Parse the template-parameter. */
14882 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14883 parameter = cp_parser_template_parameter (parser,
14884 &is_non_type,
14885 &is_parameter_pack);
14886 /* Add it to the list. */
14887 if (parameter != error_mark_node)
14888 parameter_list = process_template_parm (parameter_list,
14889 parm_loc,
14890 parameter,
14891 is_non_type,
14892 is_parameter_pack);
14893 else
14894 {
14895 tree err_parm = build_tree_list (parameter, parameter);
14896 parameter_list = chainon (parameter_list, err_parm);
14897 }
14898
14899 /* If the next token is not a `,', we're done. */
14900 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14901 break;
14902 /* Otherwise, consume the `,' token. */
14903 cp_lexer_consume_token (parser->lexer);
14904 }
14905
14906 return end_template_parm_list (parameter_list);
14907 }
14908
14909 /* Parse a introduction-list.
14910
14911 introduction-list:
14912 introduced-parameter
14913 introduction-list , introduced-parameter
14914
14915 introduced-parameter:
14916 ...[opt] identifier
14917
14918 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14919 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14920 WILDCARD_DECL will also have DECL_NAME set and token location in
14921 DECL_SOURCE_LOCATION. */
14922
14923 static tree
14924 cp_parser_introduction_list (cp_parser *parser)
14925 {
14926 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14927
14928 while (true)
14929 {
14930 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14931 if (is_pack)
14932 cp_lexer_consume_token (parser->lexer);
14933
14934 /* Build placeholder. */
14935 tree parm = build_nt (WILDCARD_DECL);
14936 DECL_SOURCE_LOCATION (parm)
14937 = cp_lexer_peek_token (parser->lexer)->location;
14938 DECL_NAME (parm) = cp_parser_identifier (parser);
14939 WILDCARD_PACK_P (parm) = is_pack;
14940 vec_safe_push (introduction_vec, parm);
14941
14942 /* If the next token is not a `,', we're done. */
14943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14944 break;
14945 /* Otherwise, consume the `,' token. */
14946 cp_lexer_consume_token (parser->lexer);
14947 }
14948
14949 /* Convert the vec into a TREE_VEC. */
14950 tree introduction_list = make_tree_vec (introduction_vec->length ());
14951 unsigned int n;
14952 tree parm;
14953 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14954 TREE_VEC_ELT (introduction_list, n) = parm;
14955
14956 release_tree_vector (introduction_vec);
14957 return introduction_list;
14958 }
14959
14960 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14961 is an abstract declarator. */
14962
14963 static inline cp_declarator*
14964 get_id_declarator (cp_declarator *declarator)
14965 {
14966 cp_declarator *d = declarator;
14967 while (d && d->kind != cdk_id)
14968 d = d->declarator;
14969 return d;
14970 }
14971
14972 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14973 is an abstract declarator. */
14974
14975 static inline tree
14976 get_unqualified_id (cp_declarator *declarator)
14977 {
14978 declarator = get_id_declarator (declarator);
14979 if (declarator)
14980 return declarator->u.id.unqualified_name;
14981 else
14982 return NULL_TREE;
14983 }
14984
14985 /* Returns true if DECL represents a constrained-parameter. */
14986
14987 static inline bool
14988 is_constrained_parameter (tree decl)
14989 {
14990 return (decl
14991 && TREE_CODE (decl) == TYPE_DECL
14992 && CONSTRAINED_PARM_CONCEPT (decl)
14993 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14994 }
14995
14996 /* Returns true if PARM declares a constrained-parameter. */
14997
14998 static inline bool
14999 is_constrained_parameter (cp_parameter_declarator *parm)
15000 {
15001 return is_constrained_parameter (parm->decl_specifiers.type);
15002 }
15003
15004 /* Check that the type parameter is only a declarator-id, and that its
15005 type is not cv-qualified. */
15006
15007 bool
15008 cp_parser_check_constrained_type_parm (cp_parser *parser,
15009 cp_parameter_declarator *parm)
15010 {
15011 if (!parm->declarator)
15012 return true;
15013
15014 if (parm->declarator->kind != cdk_id)
15015 {
15016 cp_parser_error (parser, "invalid constrained type parameter");
15017 return false;
15018 }
15019
15020 /* Don't allow cv-qualified type parameters. */
15021 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15022 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15023 {
15024 cp_parser_error (parser, "cv-qualified type parameter");
15025 return false;
15026 }
15027
15028 return true;
15029 }
15030
15031 /* Finish parsing/processing a template type parameter and checking
15032 various restrictions. */
15033
15034 static inline tree
15035 cp_parser_constrained_type_template_parm (cp_parser *parser,
15036 tree id,
15037 cp_parameter_declarator* parmdecl)
15038 {
15039 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15040 return finish_template_type_parm (class_type_node, id);
15041 else
15042 return error_mark_node;
15043 }
15044
15045 static tree
15046 finish_constrained_template_template_parm (tree proto, tree id)
15047 {
15048 /* FIXME: This should probably be copied, and we may need to adjust
15049 the template parameter depths. */
15050 tree saved_parms = current_template_parms;
15051 begin_template_parm_list ();
15052 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15053 end_template_parm_list ();
15054
15055 tree parm = finish_template_template_parm (class_type_node, id);
15056 current_template_parms = saved_parms;
15057
15058 return parm;
15059 }
15060
15061 /* Finish parsing/processing a template template parameter by borrowing
15062 the template parameter list from the prototype parameter. */
15063
15064 static tree
15065 cp_parser_constrained_template_template_parm (cp_parser *parser,
15066 tree proto,
15067 tree id,
15068 cp_parameter_declarator *parmdecl)
15069 {
15070 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15071 return error_mark_node;
15072 return finish_constrained_template_template_parm (proto, id);
15073 }
15074
15075 /* Create a new non-type template parameter from the given PARM
15076 declarator. */
15077
15078 static tree
15079 constrained_non_type_template_parm (bool *is_non_type,
15080 cp_parameter_declarator *parm)
15081 {
15082 *is_non_type = true;
15083 cp_declarator *decl = parm->declarator;
15084 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15085 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15086 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15087 }
15088
15089 /* Build a constrained template parameter based on the PARMDECL
15090 declarator. The type of PARMDECL is the constrained type, which
15091 refers to the prototype template parameter that ultimately
15092 specifies the type of the declared parameter. */
15093
15094 static tree
15095 finish_constrained_parameter (cp_parser *parser,
15096 cp_parameter_declarator *parmdecl,
15097 bool *is_non_type,
15098 bool *is_parameter_pack)
15099 {
15100 tree decl = parmdecl->decl_specifiers.type;
15101 tree id = get_unqualified_id (parmdecl->declarator);
15102 tree def = parmdecl->default_argument;
15103 tree proto = DECL_INITIAL (decl);
15104
15105 /* A template parameter constrained by a variadic concept shall also
15106 be declared as a template parameter pack. */
15107 bool is_variadic = template_parameter_pack_p (proto);
15108 if (is_variadic && !*is_parameter_pack)
15109 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15110
15111 /* Build the parameter. Return an error if the declarator was invalid. */
15112 tree parm;
15113 if (TREE_CODE (proto) == TYPE_DECL)
15114 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15115 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15116 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15117 parmdecl);
15118 else
15119 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15120 if (parm == error_mark_node)
15121 return error_mark_node;
15122
15123 /* Finish the parameter decl and create a node attaching the
15124 default argument and constraint. */
15125 parm = build_tree_list (def, parm);
15126 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15127
15128 return parm;
15129 }
15130
15131 /* Returns true if the parsed type actually represents the declaration
15132 of a type template-parameter. */
15133
15134 static inline bool
15135 declares_constrained_type_template_parameter (tree type)
15136 {
15137 return (is_constrained_parameter (type)
15138 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15139 }
15140
15141
15142 /* Returns true if the parsed type actually represents the declaration of
15143 a template template-parameter. */
15144
15145 static bool
15146 declares_constrained_template_template_parameter (tree type)
15147 {
15148 return (is_constrained_parameter (type)
15149 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15150 }
15151
15152 /* Parse a default argument for a type template-parameter.
15153 Note that diagnostics are handled in cp_parser_template_parameter. */
15154
15155 static tree
15156 cp_parser_default_type_template_argument (cp_parser *parser)
15157 {
15158 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15159
15160 /* Consume the `=' token. */
15161 cp_lexer_consume_token (parser->lexer);
15162
15163 cp_token *token = cp_lexer_peek_token (parser->lexer);
15164
15165 /* Parse the default-argument. */
15166 push_deferring_access_checks (dk_no_deferred);
15167 tree default_argument = cp_parser_type_id (parser);
15168 pop_deferring_access_checks ();
15169
15170 if (flag_concepts && type_uses_auto (default_argument))
15171 {
15172 error_at (token->location,
15173 "invalid use of %<auto%> in default template argument");
15174 return error_mark_node;
15175 }
15176
15177 return default_argument;
15178 }
15179
15180 /* Parse a default argument for a template template-parameter. */
15181
15182 static tree
15183 cp_parser_default_template_template_argument (cp_parser *parser)
15184 {
15185 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15186
15187 bool is_template;
15188
15189 /* Consume the `='. */
15190 cp_lexer_consume_token (parser->lexer);
15191 /* Parse the id-expression. */
15192 push_deferring_access_checks (dk_no_deferred);
15193 /* save token before parsing the id-expression, for error
15194 reporting */
15195 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15196 tree default_argument
15197 = cp_parser_id_expression (parser,
15198 /*template_keyword_p=*/false,
15199 /*check_dependency_p=*/true,
15200 /*template_p=*/&is_template,
15201 /*declarator_p=*/false,
15202 /*optional_p=*/false);
15203 if (TREE_CODE (default_argument) == TYPE_DECL)
15204 /* If the id-expression was a template-id that refers to
15205 a template-class, we already have the declaration here,
15206 so no further lookup is needed. */
15207 ;
15208 else
15209 /* Look up the name. */
15210 default_argument
15211 = cp_parser_lookup_name (parser, default_argument,
15212 none_type,
15213 /*is_template=*/is_template,
15214 /*is_namespace=*/false,
15215 /*check_dependency=*/true,
15216 /*ambiguous_decls=*/NULL,
15217 token->location);
15218 /* See if the default argument is valid. */
15219 default_argument = check_template_template_default_arg (default_argument);
15220 pop_deferring_access_checks ();
15221 return default_argument;
15222 }
15223
15224 /* Parse a template-parameter.
15225
15226 template-parameter:
15227 type-parameter
15228 parameter-declaration
15229
15230 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15231 the parameter. The TREE_PURPOSE is the default value, if any.
15232 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15233 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15234 set to true iff this parameter is a parameter pack. */
15235
15236 static tree
15237 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15238 bool *is_parameter_pack)
15239 {
15240 cp_token *token;
15241 cp_parameter_declarator *parameter_declarator;
15242 tree parm;
15243
15244 /* Assume it is a type parameter or a template parameter. */
15245 *is_non_type = false;
15246 /* Assume it not a parameter pack. */
15247 *is_parameter_pack = false;
15248 /* Peek at the next token. */
15249 token = cp_lexer_peek_token (parser->lexer);
15250 /* If it is `template', we have a type-parameter. */
15251 if (token->keyword == RID_TEMPLATE)
15252 return cp_parser_type_parameter (parser, is_parameter_pack);
15253 /* If it is `class' or `typename' we do not know yet whether it is a
15254 type parameter or a non-type parameter. Consider:
15255
15256 template <typename T, typename T::X X> ...
15257
15258 or:
15259
15260 template <class C, class D*> ...
15261
15262 Here, the first parameter is a type parameter, and the second is
15263 a non-type parameter. We can tell by looking at the token after
15264 the identifier -- if it is a `,', `=', or `>' then we have a type
15265 parameter. */
15266 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15267 {
15268 /* Peek at the token after `class' or `typename'. */
15269 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15270 /* If it's an ellipsis, we have a template type parameter
15271 pack. */
15272 if (token->type == CPP_ELLIPSIS)
15273 return cp_parser_type_parameter (parser, is_parameter_pack);
15274 /* If it's an identifier, skip it. */
15275 if (token->type == CPP_NAME)
15276 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15277 /* Now, see if the token looks like the end of a template
15278 parameter. */
15279 if (token->type == CPP_COMMA
15280 || token->type == CPP_EQ
15281 || token->type == CPP_GREATER)
15282 return cp_parser_type_parameter (parser, is_parameter_pack);
15283 }
15284
15285 /* Otherwise, it is a non-type parameter or a constrained parameter.
15286
15287 [temp.param]
15288
15289 When parsing a default template-argument for a non-type
15290 template-parameter, the first non-nested `>' is taken as the end
15291 of the template parameter-list rather than a greater-than
15292 operator. */
15293 parameter_declarator
15294 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15295 /*parenthesized_p=*/NULL);
15296
15297 if (!parameter_declarator)
15298 return error_mark_node;
15299
15300 /* If the parameter declaration is marked as a parameter pack, set
15301 *IS_PARAMETER_PACK to notify the caller. */
15302 if (parameter_declarator->template_parameter_pack_p)
15303 *is_parameter_pack = true;
15304
15305 if (parameter_declarator->default_argument)
15306 {
15307 /* Can happen in some cases of erroneous input (c++/34892). */
15308 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15309 /* Consume the `...' for better error recovery. */
15310 cp_lexer_consume_token (parser->lexer);
15311 }
15312
15313 // The parameter may have been constrained.
15314 if (is_constrained_parameter (parameter_declarator))
15315 return finish_constrained_parameter (parser,
15316 parameter_declarator,
15317 is_non_type,
15318 is_parameter_pack);
15319
15320 // Now we're sure that the parameter is a non-type parameter.
15321 *is_non_type = true;
15322
15323 parm = grokdeclarator (parameter_declarator->declarator,
15324 &parameter_declarator->decl_specifiers,
15325 TPARM, /*initialized=*/0,
15326 /*attrlist=*/NULL);
15327 if (parm == error_mark_node)
15328 return error_mark_node;
15329
15330 return build_tree_list (parameter_declarator->default_argument, parm);
15331 }
15332
15333 /* Parse a type-parameter.
15334
15335 type-parameter:
15336 class identifier [opt]
15337 class identifier [opt] = type-id
15338 typename identifier [opt]
15339 typename identifier [opt] = type-id
15340 template < template-parameter-list > class identifier [opt]
15341 template < template-parameter-list > class identifier [opt]
15342 = id-expression
15343
15344 GNU Extension (variadic templates):
15345
15346 type-parameter:
15347 class ... identifier [opt]
15348 typename ... identifier [opt]
15349
15350 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15351 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15352 the declaration of the parameter.
15353
15354 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15355
15356 static tree
15357 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15358 {
15359 cp_token *token;
15360 tree parameter;
15361
15362 /* Look for a keyword to tell us what kind of parameter this is. */
15363 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15364 if (!token)
15365 return error_mark_node;
15366
15367 switch (token->keyword)
15368 {
15369 case RID_CLASS:
15370 case RID_TYPENAME:
15371 {
15372 tree identifier;
15373 tree default_argument;
15374
15375 /* If the next token is an ellipsis, we have a template
15376 argument pack. */
15377 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15378 {
15379 /* Consume the `...' token. */
15380 cp_lexer_consume_token (parser->lexer);
15381 maybe_warn_variadic_templates ();
15382
15383 *is_parameter_pack = true;
15384 }
15385
15386 /* If the next token is an identifier, then it names the
15387 parameter. */
15388 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15389 identifier = cp_parser_identifier (parser);
15390 else
15391 identifier = NULL_TREE;
15392
15393 /* Create the parameter. */
15394 parameter = finish_template_type_parm (class_type_node, identifier);
15395
15396 /* If the next token is an `=', we have a default argument. */
15397 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15398 {
15399 default_argument
15400 = cp_parser_default_type_template_argument (parser);
15401
15402 /* Template parameter packs cannot have default
15403 arguments. */
15404 if (*is_parameter_pack)
15405 {
15406 if (identifier)
15407 error_at (token->location,
15408 "template parameter pack %qD cannot have a "
15409 "default argument", identifier);
15410 else
15411 error_at (token->location,
15412 "template parameter packs cannot have "
15413 "default arguments");
15414 default_argument = NULL_TREE;
15415 }
15416 else if (check_for_bare_parameter_packs (default_argument))
15417 default_argument = error_mark_node;
15418 }
15419 else
15420 default_argument = NULL_TREE;
15421
15422 /* Create the combined representation of the parameter and the
15423 default argument. */
15424 parameter = build_tree_list (default_argument, parameter);
15425 }
15426 break;
15427
15428 case RID_TEMPLATE:
15429 {
15430 tree identifier;
15431 tree default_argument;
15432
15433 /* Look for the `<'. */
15434 cp_parser_require (parser, CPP_LESS, RT_LESS);
15435 /* Parse the template-parameter-list. */
15436 cp_parser_template_parameter_list (parser);
15437 /* Look for the `>'. */
15438 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15439
15440 // If template requirements are present, parse them.
15441 if (flag_concepts)
15442 {
15443 tree reqs = get_shorthand_constraints (current_template_parms);
15444 if (tree r = cp_parser_requires_clause_opt (parser))
15445 reqs = conjoin_constraints (reqs, normalize_expression (r));
15446 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15447 }
15448
15449 /* Look for the `class' or 'typename' keywords. */
15450 cp_parser_type_parameter_key (parser);
15451 /* If the next token is an ellipsis, we have a template
15452 argument pack. */
15453 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15454 {
15455 /* Consume the `...' token. */
15456 cp_lexer_consume_token (parser->lexer);
15457 maybe_warn_variadic_templates ();
15458
15459 *is_parameter_pack = true;
15460 }
15461 /* If the next token is an `=', then there is a
15462 default-argument. If the next token is a `>', we are at
15463 the end of the parameter-list. If the next token is a `,',
15464 then we are at the end of this parameter. */
15465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15466 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15467 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15468 {
15469 identifier = cp_parser_identifier (parser);
15470 /* Treat invalid names as if the parameter were nameless. */
15471 if (identifier == error_mark_node)
15472 identifier = NULL_TREE;
15473 }
15474 else
15475 identifier = NULL_TREE;
15476
15477 /* Create the template parameter. */
15478 parameter = finish_template_template_parm (class_type_node,
15479 identifier);
15480
15481 /* If the next token is an `=', then there is a
15482 default-argument. */
15483 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15484 {
15485 default_argument
15486 = cp_parser_default_template_template_argument (parser);
15487
15488 /* Template parameter packs cannot have default
15489 arguments. */
15490 if (*is_parameter_pack)
15491 {
15492 if (identifier)
15493 error_at (token->location,
15494 "template parameter pack %qD cannot "
15495 "have a default argument",
15496 identifier);
15497 else
15498 error_at (token->location, "template parameter packs cannot "
15499 "have default arguments");
15500 default_argument = NULL_TREE;
15501 }
15502 }
15503 else
15504 default_argument = NULL_TREE;
15505
15506 /* Create the combined representation of the parameter and the
15507 default argument. */
15508 parameter = build_tree_list (default_argument, parameter);
15509 }
15510 break;
15511
15512 default:
15513 gcc_unreachable ();
15514 break;
15515 }
15516
15517 return parameter;
15518 }
15519
15520 /* Parse a template-id.
15521
15522 template-id:
15523 template-name < template-argument-list [opt] >
15524
15525 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15526 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15527 returned. Otherwise, if the template-name names a function, or set
15528 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15529 names a class, returns a TYPE_DECL for the specialization.
15530
15531 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15532 uninstantiated templates. */
15533
15534 static tree
15535 cp_parser_template_id (cp_parser *parser,
15536 bool template_keyword_p,
15537 bool check_dependency_p,
15538 enum tag_types tag_type,
15539 bool is_declaration)
15540 {
15541 tree templ;
15542 tree arguments;
15543 tree template_id;
15544 cp_token_position start_of_id = 0;
15545 cp_token *next_token = NULL, *next_token_2 = NULL;
15546 bool is_identifier;
15547
15548 /* If the next token corresponds to a template-id, there is no need
15549 to reparse it. */
15550 cp_token *token = cp_lexer_peek_token (parser->lexer);
15551 if (token->type == CPP_TEMPLATE_ID)
15552 {
15553 cp_lexer_consume_token (parser->lexer);
15554 return saved_checks_value (token->u.tree_check_value);
15555 }
15556
15557 /* Avoid performing name lookup if there is no possibility of
15558 finding a template-id. */
15559 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15560 || (token->type == CPP_NAME
15561 && !cp_parser_nth_token_starts_template_argument_list_p
15562 (parser, 2)))
15563 {
15564 cp_parser_error (parser, "expected template-id");
15565 return error_mark_node;
15566 }
15567
15568 /* Remember where the template-id starts. */
15569 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15570 start_of_id = cp_lexer_token_position (parser->lexer, false);
15571
15572 push_deferring_access_checks (dk_deferred);
15573
15574 /* Parse the template-name. */
15575 is_identifier = false;
15576 templ = cp_parser_template_name (parser, template_keyword_p,
15577 check_dependency_p,
15578 is_declaration,
15579 tag_type,
15580 &is_identifier);
15581 if (templ == error_mark_node || is_identifier)
15582 {
15583 pop_deferring_access_checks ();
15584 return templ;
15585 }
15586
15587 /* Since we're going to preserve any side-effects from this parse, set up a
15588 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15589 in the template arguments. */
15590 tentative_firewall firewall (parser);
15591
15592 /* If we find the sequence `[:' after a template-name, it's probably
15593 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15594 parse correctly the argument list. */
15595 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15596 == CPP_OPEN_SQUARE)
15597 && next_token->flags & DIGRAPH
15598 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15599 == CPP_COLON)
15600 && !(next_token_2->flags & PREV_WHITE))
15601 {
15602 cp_parser_parse_tentatively (parser);
15603 /* Change `:' into `::'. */
15604 next_token_2->type = CPP_SCOPE;
15605 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15606 CPP_LESS. */
15607 cp_lexer_consume_token (parser->lexer);
15608
15609 /* Parse the arguments. */
15610 arguments = cp_parser_enclosed_template_argument_list (parser);
15611 if (!cp_parser_parse_definitely (parser))
15612 {
15613 /* If we couldn't parse an argument list, then we revert our changes
15614 and return simply an error. Maybe this is not a template-id
15615 after all. */
15616 next_token_2->type = CPP_COLON;
15617 cp_parser_error (parser, "expected %<<%>");
15618 pop_deferring_access_checks ();
15619 return error_mark_node;
15620 }
15621 /* Otherwise, emit an error about the invalid digraph, but continue
15622 parsing because we got our argument list. */
15623 if (permerror (next_token->location,
15624 "%<<::%> cannot begin a template-argument list"))
15625 {
15626 static bool hint = false;
15627 inform (next_token->location,
15628 "%<<:%> is an alternate spelling for %<[%>."
15629 " Insert whitespace between %<<%> and %<::%>");
15630 if (!hint && !flag_permissive)
15631 {
15632 inform (next_token->location, "(if you use %<-fpermissive%> "
15633 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15634 "accept your code)");
15635 hint = true;
15636 }
15637 }
15638 }
15639 else
15640 {
15641 /* Look for the `<' that starts the template-argument-list. */
15642 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15643 {
15644 pop_deferring_access_checks ();
15645 return error_mark_node;
15646 }
15647 /* Parse the arguments. */
15648 arguments = cp_parser_enclosed_template_argument_list (parser);
15649 }
15650
15651 /* Set the location to be of the form:
15652 template-name < template-argument-list [opt] >
15653 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15654 with caret == start at the start of the template-name,
15655 ranging until the closing '>'. */
15656 location_t finish_loc
15657 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15658 location_t combined_loc
15659 = make_location (token->location, token->location, finish_loc);
15660
15661 /* Build a representation of the specialization. */
15662 if (identifier_p (templ))
15663 template_id = build_min_nt_loc (combined_loc,
15664 TEMPLATE_ID_EXPR,
15665 templ, arguments);
15666 else if (DECL_TYPE_TEMPLATE_P (templ)
15667 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15668 {
15669 bool entering_scope;
15670 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15671 template (rather than some instantiation thereof) only if
15672 is not nested within some other construct. For example, in
15673 "template <typename T> void f(T) { A<T>::", A<T> is just an
15674 instantiation of A. */
15675 entering_scope = (template_parm_scope_p ()
15676 && cp_lexer_next_token_is (parser->lexer,
15677 CPP_SCOPE));
15678 template_id
15679 = finish_template_type (templ, arguments, entering_scope);
15680 }
15681 /* A template-like identifier may be a partial concept id. */
15682 else if (flag_concepts
15683 && (template_id = (cp_parser_maybe_partial_concept_id
15684 (parser, templ, arguments))))
15685 return template_id;
15686 else if (variable_template_p (templ))
15687 {
15688 template_id = lookup_template_variable (templ, arguments);
15689 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15690 SET_EXPR_LOCATION (template_id, combined_loc);
15691 }
15692 else
15693 {
15694 /* If it's not a class-template or a template-template, it should be
15695 a function-template. */
15696 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15697 || TREE_CODE (templ) == OVERLOAD
15698 || BASELINK_P (templ)));
15699
15700 template_id = lookup_template_function (templ, arguments);
15701 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15702 SET_EXPR_LOCATION (template_id, combined_loc);
15703 }
15704
15705 /* If parsing tentatively, replace the sequence of tokens that makes
15706 up the template-id with a CPP_TEMPLATE_ID token. That way,
15707 should we re-parse the token stream, we will not have to repeat
15708 the effort required to do the parse, nor will we issue duplicate
15709 error messages about problems during instantiation of the
15710 template. */
15711 if (start_of_id
15712 /* Don't do this if we had a parse error in a declarator; re-parsing
15713 might succeed if a name changes meaning (60361). */
15714 && !(cp_parser_error_occurred (parser)
15715 && cp_parser_parsing_tentatively (parser)
15716 && parser->in_declarator_p))
15717 {
15718 /* Reset the contents of the START_OF_ID token. */
15719 token->type = CPP_TEMPLATE_ID;
15720 token->location = combined_loc;
15721
15722 /* We must mark the lookup as kept, so we don't throw it away on
15723 the first parse. */
15724 if (is_overloaded_fn (template_id))
15725 lookup_keep (get_fns (template_id), true);
15726
15727 /* Retrieve any deferred checks. Do not pop this access checks yet
15728 so the memory will not be reclaimed during token replacing below. */
15729 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15730 token->u.tree_check_value->value = template_id;
15731 token->u.tree_check_value->checks = get_deferred_access_checks ();
15732 token->keyword = RID_MAX;
15733
15734 /* Purge all subsequent tokens. */
15735 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15736
15737 /* ??? Can we actually assume that, if template_id ==
15738 error_mark_node, we will have issued a diagnostic to the
15739 user, as opposed to simply marking the tentative parse as
15740 failed? */
15741 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15742 error_at (token->location, "parse error in template argument list");
15743 }
15744
15745 pop_to_parent_deferring_access_checks ();
15746 return template_id;
15747 }
15748
15749 /* Parse a template-name.
15750
15751 template-name:
15752 identifier
15753
15754 The standard should actually say:
15755
15756 template-name:
15757 identifier
15758 operator-function-id
15759
15760 A defect report has been filed about this issue.
15761
15762 A conversion-function-id cannot be a template name because they cannot
15763 be part of a template-id. In fact, looking at this code:
15764
15765 a.operator K<int>()
15766
15767 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15768 It is impossible to call a templated conversion-function-id with an
15769 explicit argument list, since the only allowed template parameter is
15770 the type to which it is converting.
15771
15772 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15773 `template' keyword, in a construction like:
15774
15775 T::template f<3>()
15776
15777 In that case `f' is taken to be a template-name, even though there
15778 is no way of knowing for sure.
15779
15780 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15781 name refers to a set of overloaded functions, at least one of which
15782 is a template, or an IDENTIFIER_NODE with the name of the template,
15783 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15784 names are looked up inside uninstantiated templates. */
15785
15786 static tree
15787 cp_parser_template_name (cp_parser* parser,
15788 bool template_keyword_p,
15789 bool check_dependency_p,
15790 bool is_declaration,
15791 enum tag_types tag_type,
15792 bool *is_identifier)
15793 {
15794 tree identifier;
15795 tree decl;
15796 cp_token *token = cp_lexer_peek_token (parser->lexer);
15797
15798 /* If the next token is `operator', then we have either an
15799 operator-function-id or a conversion-function-id. */
15800 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15801 {
15802 /* We don't know whether we're looking at an
15803 operator-function-id or a conversion-function-id. */
15804 cp_parser_parse_tentatively (parser);
15805 /* Try an operator-function-id. */
15806 identifier = cp_parser_operator_function_id (parser);
15807 /* If that didn't work, try a conversion-function-id. */
15808 if (!cp_parser_parse_definitely (parser))
15809 {
15810 cp_parser_error (parser, "expected template-name");
15811 return error_mark_node;
15812 }
15813 }
15814 /* Look for the identifier. */
15815 else
15816 identifier = cp_parser_identifier (parser);
15817
15818 /* If we didn't find an identifier, we don't have a template-id. */
15819 if (identifier == error_mark_node)
15820 return error_mark_node;
15821
15822 /* If the name immediately followed the `template' keyword, then it
15823 is a template-name. However, if the next token is not `<', then
15824 we do not treat it as a template-name, since it is not being used
15825 as part of a template-id. This enables us to handle constructs
15826 like:
15827
15828 template <typename T> struct S { S(); };
15829 template <typename T> S<T>::S();
15830
15831 correctly. We would treat `S' as a template -- if it were `S<T>'
15832 -- but we do not if there is no `<'. */
15833
15834 if (processing_template_decl
15835 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15836 {
15837 /* In a declaration, in a dependent context, we pretend that the
15838 "template" keyword was present in order to improve error
15839 recovery. For example, given:
15840
15841 template <typename T> void f(T::X<int>);
15842
15843 we want to treat "X<int>" as a template-id. */
15844 if (is_declaration
15845 && !template_keyword_p
15846 && parser->scope && TYPE_P (parser->scope)
15847 && check_dependency_p
15848 && dependent_scope_p (parser->scope)
15849 /* Do not do this for dtors (or ctors), since they never
15850 need the template keyword before their name. */
15851 && !constructor_name_p (identifier, parser->scope))
15852 {
15853 cp_token_position start = 0;
15854
15855 /* Explain what went wrong. */
15856 error_at (token->location, "non-template %qD used as template",
15857 identifier);
15858 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15859 parser->scope, identifier);
15860 /* If parsing tentatively, find the location of the "<" token. */
15861 if (cp_parser_simulate_error (parser))
15862 start = cp_lexer_token_position (parser->lexer, true);
15863 /* Parse the template arguments so that we can issue error
15864 messages about them. */
15865 cp_lexer_consume_token (parser->lexer);
15866 cp_parser_enclosed_template_argument_list (parser);
15867 /* Skip tokens until we find a good place from which to
15868 continue parsing. */
15869 cp_parser_skip_to_closing_parenthesis (parser,
15870 /*recovering=*/true,
15871 /*or_comma=*/true,
15872 /*consume_paren=*/false);
15873 /* If parsing tentatively, permanently remove the
15874 template argument list. That will prevent duplicate
15875 error messages from being issued about the missing
15876 "template" keyword. */
15877 if (start)
15878 cp_lexer_purge_tokens_after (parser->lexer, start);
15879 if (is_identifier)
15880 *is_identifier = true;
15881 parser->context->object_type = NULL_TREE;
15882 return identifier;
15883 }
15884
15885 /* If the "template" keyword is present, then there is generally
15886 no point in doing name-lookup, so we just return IDENTIFIER.
15887 But, if the qualifying scope is non-dependent then we can
15888 (and must) do name-lookup normally. */
15889 if (template_keyword_p)
15890 {
15891 tree scope = (parser->scope ? parser->scope
15892 : parser->context->object_type);
15893 if (scope && TYPE_P (scope)
15894 && (!CLASS_TYPE_P (scope)
15895 || (check_dependency_p && dependent_type_p (scope))))
15896 {
15897 /* We're optimizing away the call to cp_parser_lookup_name, but
15898 we still need to do this. */
15899 parser->context->object_type = NULL_TREE;
15900 return identifier;
15901 }
15902 }
15903 }
15904
15905 /* Look up the name. */
15906 decl = cp_parser_lookup_name (parser, identifier,
15907 tag_type,
15908 /*is_template=*/true,
15909 /*is_namespace=*/false,
15910 check_dependency_p,
15911 /*ambiguous_decls=*/NULL,
15912 token->location);
15913
15914 /* If the lookup failed and we got the 'template' keyword, believe it. */
15915 if (decl == error_mark_node && template_keyword_p
15916 && processing_template_decl)
15917 return identifier;
15918
15919 decl = strip_using_decl (decl);
15920
15921 /* If DECL is a template, then the name was a template-name. */
15922 if (TREE_CODE (decl) == TEMPLATE_DECL)
15923 {
15924 if (TREE_DEPRECATED (decl)
15925 && deprecated_state != DEPRECATED_SUPPRESS)
15926 warn_deprecated_use (decl, NULL_TREE);
15927 }
15928 else
15929 {
15930 /* The standard does not explicitly indicate whether a name that
15931 names a set of overloaded declarations, some of which are
15932 templates, is a template-name. However, such a name should
15933 be a template-name; otherwise, there is no way to form a
15934 template-id for the overloaded templates. */
15935 bool found = false;
15936
15937 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
15938 !found && iter; ++iter)
15939 if (TREE_CODE (*iter) == TEMPLATE_DECL)
15940 found = true;
15941
15942 if (!found)
15943 {
15944 /* The name does not name a template. */
15945 cp_parser_error (parser, "expected template-name");
15946 return error_mark_node;
15947 }
15948 }
15949
15950 /* If DECL is dependent, and refers to a function, then just return
15951 its name; we will look it up again during template instantiation. */
15952 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15953 {
15954 tree scope = ovl_scope (decl);
15955 if (TYPE_P (scope) && dependent_type_p (scope))
15956 return identifier;
15957 }
15958
15959 return decl;
15960 }
15961
15962 /* Parse a template-argument-list.
15963
15964 template-argument-list:
15965 template-argument ... [opt]
15966 template-argument-list , template-argument ... [opt]
15967
15968 Returns a TREE_VEC containing the arguments. */
15969
15970 static tree
15971 cp_parser_template_argument_list (cp_parser* parser)
15972 {
15973 tree fixed_args[10];
15974 unsigned n_args = 0;
15975 unsigned alloced = 10;
15976 tree *arg_ary = fixed_args;
15977 tree vec;
15978 bool saved_in_template_argument_list_p;
15979 bool saved_ice_p;
15980 bool saved_non_ice_p;
15981
15982 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15983 parser->in_template_argument_list_p = true;
15984 /* Even if the template-id appears in an integral
15985 constant-expression, the contents of the argument list do
15986 not. */
15987 saved_ice_p = parser->integral_constant_expression_p;
15988 parser->integral_constant_expression_p = false;
15989 saved_non_ice_p = parser->non_integral_constant_expression_p;
15990 parser->non_integral_constant_expression_p = false;
15991
15992 /* Parse the arguments. */
15993 do
15994 {
15995 tree argument;
15996
15997 if (n_args)
15998 /* Consume the comma. */
15999 cp_lexer_consume_token (parser->lexer);
16000
16001 /* Parse the template-argument. */
16002 argument = cp_parser_template_argument (parser);
16003
16004 /* If the next token is an ellipsis, we're expanding a template
16005 argument pack. */
16006 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16007 {
16008 if (argument == error_mark_node)
16009 {
16010 cp_token *token = cp_lexer_peek_token (parser->lexer);
16011 error_at (token->location,
16012 "expected parameter pack before %<...%>");
16013 }
16014 /* Consume the `...' token. */
16015 cp_lexer_consume_token (parser->lexer);
16016
16017 /* Make the argument into a TYPE_PACK_EXPANSION or
16018 EXPR_PACK_EXPANSION. */
16019 argument = make_pack_expansion (argument);
16020 }
16021
16022 if (n_args == alloced)
16023 {
16024 alloced *= 2;
16025
16026 if (arg_ary == fixed_args)
16027 {
16028 arg_ary = XNEWVEC (tree, alloced);
16029 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16030 }
16031 else
16032 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16033 }
16034 arg_ary[n_args++] = argument;
16035 }
16036 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16037
16038 vec = make_tree_vec (n_args);
16039
16040 while (n_args--)
16041 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16042
16043 if (arg_ary != fixed_args)
16044 free (arg_ary);
16045 parser->non_integral_constant_expression_p = saved_non_ice_p;
16046 parser->integral_constant_expression_p = saved_ice_p;
16047 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16048 if (CHECKING_P)
16049 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16050 return vec;
16051 }
16052
16053 /* Parse a template-argument.
16054
16055 template-argument:
16056 assignment-expression
16057 type-id
16058 id-expression
16059
16060 The representation is that of an assignment-expression, type-id, or
16061 id-expression -- except that the qualified id-expression is
16062 evaluated, so that the value returned is either a DECL or an
16063 OVERLOAD.
16064
16065 Although the standard says "assignment-expression", it forbids
16066 throw-expressions or assignments in the template argument.
16067 Therefore, we use "conditional-expression" instead. */
16068
16069 static tree
16070 cp_parser_template_argument (cp_parser* parser)
16071 {
16072 tree argument;
16073 bool template_p;
16074 bool address_p;
16075 bool maybe_type_id = false;
16076 cp_token *token = NULL, *argument_start_token = NULL;
16077 location_t loc = 0;
16078 cp_id_kind idk;
16079
16080 /* There's really no way to know what we're looking at, so we just
16081 try each alternative in order.
16082
16083 [temp.arg]
16084
16085 In a template-argument, an ambiguity between a type-id and an
16086 expression is resolved to a type-id, regardless of the form of
16087 the corresponding template-parameter.
16088
16089 Therefore, we try a type-id first. */
16090 cp_parser_parse_tentatively (parser);
16091 argument = cp_parser_template_type_arg (parser);
16092 /* If there was no error parsing the type-id but the next token is a
16093 '>>', our behavior depends on which dialect of C++ we're
16094 parsing. In C++98, we probably found a typo for '> >'. But there
16095 are type-id which are also valid expressions. For instance:
16096
16097 struct X { int operator >> (int); };
16098 template <int V> struct Foo {};
16099 Foo<X () >> 5> r;
16100
16101 Here 'X()' is a valid type-id of a function type, but the user just
16102 wanted to write the expression "X() >> 5". Thus, we remember that we
16103 found a valid type-id, but we still try to parse the argument as an
16104 expression to see what happens.
16105
16106 In C++0x, the '>>' will be considered two separate '>'
16107 tokens. */
16108 if (!cp_parser_error_occurred (parser)
16109 && cxx_dialect == cxx98
16110 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16111 {
16112 maybe_type_id = true;
16113 cp_parser_abort_tentative_parse (parser);
16114 }
16115 else
16116 {
16117 /* If the next token isn't a `,' or a `>', then this argument wasn't
16118 really finished. This means that the argument is not a valid
16119 type-id. */
16120 if (!cp_parser_next_token_ends_template_argument_p (parser))
16121 cp_parser_error (parser, "expected template-argument");
16122 /* If that worked, we're done. */
16123 if (cp_parser_parse_definitely (parser))
16124 return argument;
16125 }
16126 /* We're still not sure what the argument will be. */
16127 cp_parser_parse_tentatively (parser);
16128 /* Try a template. */
16129 argument_start_token = cp_lexer_peek_token (parser->lexer);
16130 argument = cp_parser_id_expression (parser,
16131 /*template_keyword_p=*/false,
16132 /*check_dependency_p=*/true,
16133 &template_p,
16134 /*declarator_p=*/false,
16135 /*optional_p=*/false);
16136 /* If the next token isn't a `,' or a `>', then this argument wasn't
16137 really finished. */
16138 if (!cp_parser_next_token_ends_template_argument_p (parser))
16139 cp_parser_error (parser, "expected template-argument");
16140 if (!cp_parser_error_occurred (parser))
16141 {
16142 /* Figure out what is being referred to. If the id-expression
16143 was for a class template specialization, then we will have a
16144 TYPE_DECL at this point. There is no need to do name lookup
16145 at this point in that case. */
16146 if (TREE_CODE (argument) != TYPE_DECL)
16147 argument = cp_parser_lookup_name (parser, argument,
16148 none_type,
16149 /*is_template=*/template_p,
16150 /*is_namespace=*/false,
16151 /*check_dependency=*/true,
16152 /*ambiguous_decls=*/NULL,
16153 argument_start_token->location);
16154 /* Handle a constrained-type-specifier for a non-type template
16155 parameter. */
16156 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16157 argument = decl;
16158 else if (TREE_CODE (argument) != TEMPLATE_DECL
16159 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16160 cp_parser_error (parser, "expected template-name");
16161 }
16162 if (cp_parser_parse_definitely (parser))
16163 {
16164 if (TREE_DEPRECATED (argument))
16165 warn_deprecated_use (argument, NULL_TREE);
16166 return argument;
16167 }
16168 /* It must be a non-type argument. In C++17 any constant-expression is
16169 allowed. */
16170 if (cxx_dialect > cxx14)
16171 goto general_expr;
16172
16173 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16174
16175 -- an integral constant-expression of integral or enumeration
16176 type; or
16177
16178 -- the name of a non-type template-parameter; or
16179
16180 -- the name of an object or function with external linkage...
16181
16182 -- the address of an object or function with external linkage...
16183
16184 -- a pointer to member... */
16185 /* Look for a non-type template parameter. */
16186 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16187 {
16188 cp_parser_parse_tentatively (parser);
16189 argument = cp_parser_primary_expression (parser,
16190 /*address_p=*/false,
16191 /*cast_p=*/false,
16192 /*template_arg_p=*/true,
16193 &idk);
16194 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16195 || !cp_parser_next_token_ends_template_argument_p (parser))
16196 cp_parser_simulate_error (parser);
16197 if (cp_parser_parse_definitely (parser))
16198 return argument;
16199 }
16200
16201 /* If the next token is "&", the argument must be the address of an
16202 object or function with external linkage. */
16203 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16204 if (address_p)
16205 {
16206 loc = cp_lexer_peek_token (parser->lexer)->location;
16207 cp_lexer_consume_token (parser->lexer);
16208 }
16209 /* See if we might have an id-expression. */
16210 token = cp_lexer_peek_token (parser->lexer);
16211 if (token->type == CPP_NAME
16212 || token->keyword == RID_OPERATOR
16213 || token->type == CPP_SCOPE
16214 || token->type == CPP_TEMPLATE_ID
16215 || token->type == CPP_NESTED_NAME_SPECIFIER)
16216 {
16217 cp_parser_parse_tentatively (parser);
16218 argument = cp_parser_primary_expression (parser,
16219 address_p,
16220 /*cast_p=*/false,
16221 /*template_arg_p=*/true,
16222 &idk);
16223 if (cp_parser_error_occurred (parser)
16224 || !cp_parser_next_token_ends_template_argument_p (parser))
16225 cp_parser_abort_tentative_parse (parser);
16226 else
16227 {
16228 tree probe;
16229
16230 if (INDIRECT_REF_P (argument))
16231 {
16232 /* Strip the dereference temporarily. */
16233 gcc_assert (REFERENCE_REF_P (argument));
16234 argument = TREE_OPERAND (argument, 0);
16235 }
16236
16237 /* If we're in a template, we represent a qualified-id referring
16238 to a static data member as a SCOPE_REF even if the scope isn't
16239 dependent so that we can check access control later. */
16240 probe = argument;
16241 if (TREE_CODE (probe) == SCOPE_REF)
16242 probe = TREE_OPERAND (probe, 1);
16243 if (VAR_P (probe))
16244 {
16245 /* A variable without external linkage might still be a
16246 valid constant-expression, so no error is issued here
16247 if the external-linkage check fails. */
16248 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16249 cp_parser_simulate_error (parser);
16250 }
16251 else if (is_overloaded_fn (argument))
16252 /* All overloaded functions are allowed; if the external
16253 linkage test does not pass, an error will be issued
16254 later. */
16255 ;
16256 else if (address_p
16257 && (TREE_CODE (argument) == OFFSET_REF
16258 || TREE_CODE (argument) == SCOPE_REF))
16259 /* A pointer-to-member. */
16260 ;
16261 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16262 ;
16263 else
16264 cp_parser_simulate_error (parser);
16265
16266 if (cp_parser_parse_definitely (parser))
16267 {
16268 if (address_p)
16269 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16270 tf_warning_or_error);
16271 else
16272 argument = convert_from_reference (argument);
16273 return argument;
16274 }
16275 }
16276 }
16277 /* If the argument started with "&", there are no other valid
16278 alternatives at this point. */
16279 if (address_p)
16280 {
16281 cp_parser_error (parser, "invalid non-type template argument");
16282 return error_mark_node;
16283 }
16284
16285 general_expr:
16286 /* If the argument wasn't successfully parsed as a type-id followed
16287 by '>>', the argument can only be a constant expression now.
16288 Otherwise, we try parsing the constant-expression tentatively,
16289 because the argument could really be a type-id. */
16290 if (maybe_type_id)
16291 cp_parser_parse_tentatively (parser);
16292
16293 if (cxx_dialect <= cxx14)
16294 argument = cp_parser_constant_expression (parser);
16295 else
16296 {
16297 /* With C++17 generalized non-type template arguments we need to handle
16298 lvalue constant expressions, too. */
16299 argument = cp_parser_assignment_expression (parser);
16300 require_potential_constant_expression (argument);
16301 }
16302
16303 if (!maybe_type_id)
16304 return argument;
16305 if (!cp_parser_next_token_ends_template_argument_p (parser))
16306 cp_parser_error (parser, "expected template-argument");
16307 if (cp_parser_parse_definitely (parser))
16308 return argument;
16309 /* We did our best to parse the argument as a non type-id, but that
16310 was the only alternative that matched (albeit with a '>' after
16311 it). We can assume it's just a typo from the user, and a
16312 diagnostic will then be issued. */
16313 return cp_parser_template_type_arg (parser);
16314 }
16315
16316 /* Parse an explicit-instantiation.
16317
16318 explicit-instantiation:
16319 template declaration
16320
16321 Although the standard says `declaration', what it really means is:
16322
16323 explicit-instantiation:
16324 template decl-specifier-seq [opt] declarator [opt] ;
16325
16326 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16327 supposed to be allowed. A defect report has been filed about this
16328 issue.
16329
16330 GNU Extension:
16331
16332 explicit-instantiation:
16333 storage-class-specifier template
16334 decl-specifier-seq [opt] declarator [opt] ;
16335 function-specifier template
16336 decl-specifier-seq [opt] declarator [opt] ; */
16337
16338 static void
16339 cp_parser_explicit_instantiation (cp_parser* parser)
16340 {
16341 int declares_class_or_enum;
16342 cp_decl_specifier_seq decl_specifiers;
16343 tree extension_specifier = NULL_TREE;
16344
16345 timevar_push (TV_TEMPLATE_INST);
16346
16347 /* Look for an (optional) storage-class-specifier or
16348 function-specifier. */
16349 if (cp_parser_allow_gnu_extensions_p (parser))
16350 {
16351 extension_specifier
16352 = cp_parser_storage_class_specifier_opt (parser);
16353 if (!extension_specifier)
16354 extension_specifier
16355 = cp_parser_function_specifier_opt (parser,
16356 /*decl_specs=*/NULL);
16357 }
16358
16359 /* Look for the `template' keyword. */
16360 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16361 /* Let the front end know that we are processing an explicit
16362 instantiation. */
16363 begin_explicit_instantiation ();
16364 /* [temp.explicit] says that we are supposed to ignore access
16365 control while processing explicit instantiation directives. */
16366 push_deferring_access_checks (dk_no_check);
16367 /* Parse a decl-specifier-seq. */
16368 cp_parser_decl_specifier_seq (parser,
16369 CP_PARSER_FLAGS_OPTIONAL,
16370 &decl_specifiers,
16371 &declares_class_or_enum);
16372 /* If there was exactly one decl-specifier, and it declared a class,
16373 and there's no declarator, then we have an explicit type
16374 instantiation. */
16375 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16376 {
16377 tree type;
16378
16379 type = check_tag_decl (&decl_specifiers,
16380 /*explicit_type_instantiation_p=*/true);
16381 /* Turn access control back on for names used during
16382 template instantiation. */
16383 pop_deferring_access_checks ();
16384 if (type)
16385 do_type_instantiation (type, extension_specifier,
16386 /*complain=*/tf_error);
16387 }
16388 else
16389 {
16390 cp_declarator *declarator;
16391 tree decl;
16392
16393 /* Parse the declarator. */
16394 declarator
16395 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16396 /*ctor_dtor_or_conv_p=*/NULL,
16397 /*parenthesized_p=*/NULL,
16398 /*member_p=*/false,
16399 /*friend_p=*/false);
16400 if (declares_class_or_enum & 2)
16401 cp_parser_check_for_definition_in_return_type (declarator,
16402 decl_specifiers.type,
16403 decl_specifiers.locations[ds_type_spec]);
16404 if (declarator != cp_error_declarator)
16405 {
16406 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16407 permerror (decl_specifiers.locations[ds_inline],
16408 "explicit instantiation shall not use"
16409 " %<inline%> specifier");
16410 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16411 permerror (decl_specifiers.locations[ds_constexpr],
16412 "explicit instantiation shall not use"
16413 " %<constexpr%> specifier");
16414
16415 decl = grokdeclarator (declarator, &decl_specifiers,
16416 NORMAL, 0, &decl_specifiers.attributes);
16417 /* Turn access control back on for names used during
16418 template instantiation. */
16419 pop_deferring_access_checks ();
16420 /* Do the explicit instantiation. */
16421 do_decl_instantiation (decl, extension_specifier);
16422 }
16423 else
16424 {
16425 pop_deferring_access_checks ();
16426 /* Skip the body of the explicit instantiation. */
16427 cp_parser_skip_to_end_of_statement (parser);
16428 }
16429 }
16430 /* We're done with the instantiation. */
16431 end_explicit_instantiation ();
16432
16433 cp_parser_consume_semicolon_at_end_of_statement (parser);
16434
16435 timevar_pop (TV_TEMPLATE_INST);
16436 }
16437
16438 /* Parse an explicit-specialization.
16439
16440 explicit-specialization:
16441 template < > declaration
16442
16443 Although the standard says `declaration', what it really means is:
16444
16445 explicit-specialization:
16446 template <> decl-specifier [opt] init-declarator [opt] ;
16447 template <> function-definition
16448 template <> explicit-specialization
16449 template <> template-declaration */
16450
16451 static void
16452 cp_parser_explicit_specialization (cp_parser* parser)
16453 {
16454 bool need_lang_pop;
16455 cp_token *token = cp_lexer_peek_token (parser->lexer);
16456
16457 /* Look for the `template' keyword. */
16458 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16459 /* Look for the `<'. */
16460 cp_parser_require (parser, CPP_LESS, RT_LESS);
16461 /* Look for the `>'. */
16462 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16463 /* We have processed another parameter list. */
16464 ++parser->num_template_parameter_lists;
16465 /* [temp]
16466
16467 A template ... explicit specialization ... shall not have C
16468 linkage. */
16469 if (current_lang_name == lang_name_c)
16470 {
16471 error_at (token->location, "template specialization with C linkage");
16472 /* Give it C++ linkage to avoid confusing other parts of the
16473 front end. */
16474 push_lang_context (lang_name_cplusplus);
16475 need_lang_pop = true;
16476 }
16477 else
16478 need_lang_pop = false;
16479 /* Let the front end know that we are beginning a specialization. */
16480 if (!begin_specialization ())
16481 {
16482 end_specialization ();
16483 return;
16484 }
16485
16486 /* If the next keyword is `template', we need to figure out whether
16487 or not we're looking a template-declaration. */
16488 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16489 {
16490 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16491 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16492 cp_parser_template_declaration_after_export (parser,
16493 /*member_p=*/false);
16494 else
16495 cp_parser_explicit_specialization (parser);
16496 }
16497 else
16498 /* Parse the dependent declaration. */
16499 cp_parser_single_declaration (parser,
16500 /*checks=*/NULL,
16501 /*member_p=*/false,
16502 /*explicit_specialization_p=*/true,
16503 /*friend_p=*/NULL);
16504 /* We're done with the specialization. */
16505 end_specialization ();
16506 /* For the erroneous case of a template with C linkage, we pushed an
16507 implicit C++ linkage scope; exit that scope now. */
16508 if (need_lang_pop)
16509 pop_lang_context ();
16510 /* We're done with this parameter list. */
16511 --parser->num_template_parameter_lists;
16512 }
16513
16514 /* Parse a type-specifier.
16515
16516 type-specifier:
16517 simple-type-specifier
16518 class-specifier
16519 enum-specifier
16520 elaborated-type-specifier
16521 cv-qualifier
16522
16523 GNU Extension:
16524
16525 type-specifier:
16526 __complex__
16527
16528 Returns a representation of the type-specifier. For a
16529 class-specifier, enum-specifier, or elaborated-type-specifier, a
16530 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16531
16532 The parser flags FLAGS is used to control type-specifier parsing.
16533
16534 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16535 in a decl-specifier-seq.
16536
16537 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16538 class-specifier, enum-specifier, or elaborated-type-specifier, then
16539 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16540 if a type is declared; 2 if it is defined. Otherwise, it is set to
16541 zero.
16542
16543 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16544 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16545 is set to FALSE. */
16546
16547 static tree
16548 cp_parser_type_specifier (cp_parser* parser,
16549 cp_parser_flags flags,
16550 cp_decl_specifier_seq *decl_specs,
16551 bool is_declaration,
16552 int* declares_class_or_enum,
16553 bool* is_cv_qualifier)
16554 {
16555 tree type_spec = NULL_TREE;
16556 cp_token *token;
16557 enum rid keyword;
16558 cp_decl_spec ds = ds_last;
16559
16560 /* Assume this type-specifier does not declare a new type. */
16561 if (declares_class_or_enum)
16562 *declares_class_or_enum = 0;
16563 /* And that it does not specify a cv-qualifier. */
16564 if (is_cv_qualifier)
16565 *is_cv_qualifier = false;
16566 /* Peek at the next token. */
16567 token = cp_lexer_peek_token (parser->lexer);
16568
16569 /* If we're looking at a keyword, we can use that to guide the
16570 production we choose. */
16571 keyword = token->keyword;
16572 switch (keyword)
16573 {
16574 case RID_ENUM:
16575 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16576 goto elaborated_type_specifier;
16577
16578 /* Look for the enum-specifier. */
16579 type_spec = cp_parser_enum_specifier (parser);
16580 /* If that worked, we're done. */
16581 if (type_spec)
16582 {
16583 if (declares_class_or_enum)
16584 *declares_class_or_enum = 2;
16585 if (decl_specs)
16586 cp_parser_set_decl_spec_type (decl_specs,
16587 type_spec,
16588 token,
16589 /*type_definition_p=*/true);
16590 return type_spec;
16591 }
16592 else
16593 goto elaborated_type_specifier;
16594
16595 /* Any of these indicate either a class-specifier, or an
16596 elaborated-type-specifier. */
16597 case RID_CLASS:
16598 case RID_STRUCT:
16599 case RID_UNION:
16600 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16601 goto elaborated_type_specifier;
16602
16603 /* Parse tentatively so that we can back up if we don't find a
16604 class-specifier. */
16605 cp_parser_parse_tentatively (parser);
16606 /* Look for the class-specifier. */
16607 type_spec = cp_parser_class_specifier (parser);
16608 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16609 /* If that worked, we're done. */
16610 if (cp_parser_parse_definitely (parser))
16611 {
16612 if (declares_class_or_enum)
16613 *declares_class_or_enum = 2;
16614 if (decl_specs)
16615 cp_parser_set_decl_spec_type (decl_specs,
16616 type_spec,
16617 token,
16618 /*type_definition_p=*/true);
16619 return type_spec;
16620 }
16621
16622 /* Fall through. */
16623 elaborated_type_specifier:
16624 /* We're declaring (not defining) a class or enum. */
16625 if (declares_class_or_enum)
16626 *declares_class_or_enum = 1;
16627
16628 /* Fall through. */
16629 case RID_TYPENAME:
16630 /* Look for an elaborated-type-specifier. */
16631 type_spec
16632 = (cp_parser_elaborated_type_specifier
16633 (parser,
16634 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16635 is_declaration));
16636 if (decl_specs)
16637 cp_parser_set_decl_spec_type (decl_specs,
16638 type_spec,
16639 token,
16640 /*type_definition_p=*/false);
16641 return type_spec;
16642
16643 case RID_CONST:
16644 ds = ds_const;
16645 if (is_cv_qualifier)
16646 *is_cv_qualifier = true;
16647 break;
16648
16649 case RID_VOLATILE:
16650 ds = ds_volatile;
16651 if (is_cv_qualifier)
16652 *is_cv_qualifier = true;
16653 break;
16654
16655 case RID_RESTRICT:
16656 ds = ds_restrict;
16657 if (is_cv_qualifier)
16658 *is_cv_qualifier = true;
16659 break;
16660
16661 case RID_COMPLEX:
16662 /* The `__complex__' keyword is a GNU extension. */
16663 ds = ds_complex;
16664 break;
16665
16666 default:
16667 break;
16668 }
16669
16670 /* Handle simple keywords. */
16671 if (ds != ds_last)
16672 {
16673 if (decl_specs)
16674 {
16675 set_and_check_decl_spec_loc (decl_specs, ds, token);
16676 decl_specs->any_specifiers_p = true;
16677 }
16678 return cp_lexer_consume_token (parser->lexer)->u.value;
16679 }
16680
16681 /* If we do not already have a type-specifier, assume we are looking
16682 at a simple-type-specifier. */
16683 type_spec = cp_parser_simple_type_specifier (parser,
16684 decl_specs,
16685 flags);
16686
16687 /* If we didn't find a type-specifier, and a type-specifier was not
16688 optional in this context, issue an error message. */
16689 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16690 {
16691 cp_parser_error (parser, "expected type specifier");
16692 return error_mark_node;
16693 }
16694
16695 return type_spec;
16696 }
16697
16698 /* Parse a simple-type-specifier.
16699
16700 simple-type-specifier:
16701 :: [opt] nested-name-specifier [opt] type-name
16702 :: [opt] nested-name-specifier template template-id
16703 char
16704 wchar_t
16705 bool
16706 short
16707 int
16708 long
16709 signed
16710 unsigned
16711 float
16712 double
16713 void
16714
16715 C++11 Extension:
16716
16717 simple-type-specifier:
16718 auto
16719 decltype ( expression )
16720 char16_t
16721 char32_t
16722 __underlying_type ( type-id )
16723
16724 C++17 extension:
16725
16726 nested-name-specifier(opt) template-name
16727
16728 GNU Extension:
16729
16730 simple-type-specifier:
16731 __int128
16732 __typeof__ unary-expression
16733 __typeof__ ( type-id )
16734 __typeof__ ( type-id ) { initializer-list , [opt] }
16735
16736 Concepts Extension:
16737
16738 simple-type-specifier:
16739 constrained-type-specifier
16740
16741 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16742 appropriately updated. */
16743
16744 static tree
16745 cp_parser_simple_type_specifier (cp_parser* parser,
16746 cp_decl_specifier_seq *decl_specs,
16747 cp_parser_flags flags)
16748 {
16749 tree type = NULL_TREE;
16750 cp_token *token;
16751 int idx;
16752
16753 /* Peek at the next token. */
16754 token = cp_lexer_peek_token (parser->lexer);
16755
16756 /* If we're looking at a keyword, things are easy. */
16757 switch (token->keyword)
16758 {
16759 case RID_CHAR:
16760 if (decl_specs)
16761 decl_specs->explicit_char_p = true;
16762 type = char_type_node;
16763 break;
16764 case RID_CHAR16:
16765 type = char16_type_node;
16766 break;
16767 case RID_CHAR32:
16768 type = char32_type_node;
16769 break;
16770 case RID_WCHAR:
16771 type = wchar_type_node;
16772 break;
16773 case RID_BOOL:
16774 type = boolean_type_node;
16775 break;
16776 case RID_SHORT:
16777 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16778 type = short_integer_type_node;
16779 break;
16780 case RID_INT:
16781 if (decl_specs)
16782 decl_specs->explicit_int_p = true;
16783 type = integer_type_node;
16784 break;
16785 case RID_INT_N_0:
16786 case RID_INT_N_1:
16787 case RID_INT_N_2:
16788 case RID_INT_N_3:
16789 idx = token->keyword - RID_INT_N_0;
16790 if (! int_n_enabled_p [idx])
16791 break;
16792 if (decl_specs)
16793 {
16794 decl_specs->explicit_intN_p = true;
16795 decl_specs->int_n_idx = idx;
16796 }
16797 type = int_n_trees [idx].signed_type;
16798 break;
16799 case RID_LONG:
16800 if (decl_specs)
16801 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16802 type = long_integer_type_node;
16803 break;
16804 case RID_SIGNED:
16805 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16806 type = integer_type_node;
16807 break;
16808 case RID_UNSIGNED:
16809 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16810 type = unsigned_type_node;
16811 break;
16812 case RID_FLOAT:
16813 type = float_type_node;
16814 break;
16815 case RID_DOUBLE:
16816 type = double_type_node;
16817 break;
16818 case RID_VOID:
16819 type = void_type_node;
16820 break;
16821
16822 case RID_AUTO:
16823 maybe_warn_cpp0x (CPP0X_AUTO);
16824 if (parser->auto_is_implicit_function_template_parm_p)
16825 {
16826 /* The 'auto' might be the placeholder return type for a function decl
16827 with trailing return type. */
16828 bool have_trailing_return_fn_decl = false;
16829
16830 cp_parser_parse_tentatively (parser);
16831 cp_lexer_consume_token (parser->lexer);
16832 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16833 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16834 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16835 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16836 {
16837 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16838 {
16839 cp_lexer_consume_token (parser->lexer);
16840 cp_parser_skip_to_closing_parenthesis (parser,
16841 /*recovering*/false,
16842 /*or_comma*/false,
16843 /*consume_paren*/true);
16844 continue;
16845 }
16846
16847 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16848 {
16849 have_trailing_return_fn_decl = true;
16850 break;
16851 }
16852
16853 cp_lexer_consume_token (parser->lexer);
16854 }
16855 cp_parser_abort_tentative_parse (parser);
16856
16857 if (have_trailing_return_fn_decl)
16858 {
16859 type = make_auto ();
16860 break;
16861 }
16862
16863 if (cxx_dialect >= cxx14)
16864 {
16865 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16866 type = TREE_TYPE (type);
16867 }
16868 else
16869 type = error_mark_node;
16870
16871 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16872 {
16873 if (cxx_dialect < cxx14)
16874 error_at (token->location,
16875 "use of %<auto%> in lambda parameter declaration "
16876 "only available with "
16877 "-std=c++14 or -std=gnu++14");
16878 }
16879 else if (cxx_dialect < cxx14)
16880 error_at (token->location,
16881 "use of %<auto%> in parameter declaration "
16882 "only available with "
16883 "-std=c++14 or -std=gnu++14");
16884 else if (!flag_concepts)
16885 pedwarn (token->location, OPT_Wpedantic,
16886 "ISO C++ forbids use of %<auto%> in parameter "
16887 "declaration");
16888 }
16889 else
16890 type = make_auto ();
16891 break;
16892
16893 case RID_DECLTYPE:
16894 /* Since DR 743, decltype can either be a simple-type-specifier by
16895 itself or begin a nested-name-specifier. Parsing it will replace
16896 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16897 handling below decide what to do. */
16898 cp_parser_decltype (parser);
16899 cp_lexer_set_token_position (parser->lexer, token);
16900 break;
16901
16902 case RID_TYPEOF:
16903 /* Consume the `typeof' token. */
16904 cp_lexer_consume_token (parser->lexer);
16905 /* Parse the operand to `typeof'. */
16906 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16907 /* If it is not already a TYPE, take its type. */
16908 if (!TYPE_P (type))
16909 type = finish_typeof (type);
16910
16911 if (decl_specs)
16912 cp_parser_set_decl_spec_type (decl_specs, type,
16913 token,
16914 /*type_definition_p=*/false);
16915
16916 return type;
16917
16918 case RID_UNDERLYING_TYPE:
16919 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16920 if (decl_specs)
16921 cp_parser_set_decl_spec_type (decl_specs, type,
16922 token,
16923 /*type_definition_p=*/false);
16924
16925 return type;
16926
16927 case RID_BASES:
16928 case RID_DIRECT_BASES:
16929 type = cp_parser_trait_expr (parser, token->keyword);
16930 if (decl_specs)
16931 cp_parser_set_decl_spec_type (decl_specs, type,
16932 token,
16933 /*type_definition_p=*/false);
16934 return type;
16935 default:
16936 break;
16937 }
16938
16939 /* If token is an already-parsed decltype not followed by ::,
16940 it's a simple-type-specifier. */
16941 if (token->type == CPP_DECLTYPE
16942 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16943 {
16944 type = saved_checks_value (token->u.tree_check_value);
16945 if (decl_specs)
16946 {
16947 cp_parser_set_decl_spec_type (decl_specs, type,
16948 token,
16949 /*type_definition_p=*/false);
16950 /* Remember that we are handling a decltype in order to
16951 implement the resolution of DR 1510 when the argument
16952 isn't instantiation dependent. */
16953 decl_specs->decltype_p = true;
16954 }
16955 cp_lexer_consume_token (parser->lexer);
16956 return type;
16957 }
16958
16959 /* If the type-specifier was for a built-in type, we're done. */
16960 if (type)
16961 {
16962 /* Record the type. */
16963 if (decl_specs
16964 && (token->keyword != RID_SIGNED
16965 && token->keyword != RID_UNSIGNED
16966 && token->keyword != RID_SHORT
16967 && token->keyword != RID_LONG))
16968 cp_parser_set_decl_spec_type (decl_specs,
16969 type,
16970 token,
16971 /*type_definition_p=*/false);
16972 if (decl_specs)
16973 decl_specs->any_specifiers_p = true;
16974
16975 /* Consume the token. */
16976 cp_lexer_consume_token (parser->lexer);
16977
16978 if (type == error_mark_node)
16979 return error_mark_node;
16980
16981 /* There is no valid C++ program where a non-template type is
16982 followed by a "<". That usually indicates that the user thought
16983 that the type was a template. */
16984 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16985 token->location);
16986
16987 return TYPE_NAME (type);
16988 }
16989
16990 /* The type-specifier must be a user-defined type. */
16991 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16992 {
16993 bool qualified_p;
16994 bool global_p;
16995
16996 /* Don't gobble tokens or issue error messages if this is an
16997 optional type-specifier. */
16998 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16999 cp_parser_parse_tentatively (parser);
17000
17001 token = cp_lexer_peek_token (parser->lexer);
17002
17003 /* Look for the optional `::' operator. */
17004 global_p
17005 = (cp_parser_global_scope_opt (parser,
17006 /*current_scope_valid_p=*/false)
17007 != NULL_TREE);
17008 /* Look for the nested-name specifier. */
17009 qualified_p
17010 = (cp_parser_nested_name_specifier_opt (parser,
17011 /*typename_keyword_p=*/false,
17012 /*check_dependency_p=*/true,
17013 /*type_p=*/false,
17014 /*is_declaration=*/false)
17015 != NULL_TREE);
17016 /* If we have seen a nested-name-specifier, and the next token
17017 is `template', then we are using the template-id production. */
17018 if (parser->scope
17019 && cp_parser_optional_template_keyword (parser))
17020 {
17021 /* Look for the template-id. */
17022 type = cp_parser_template_id (parser,
17023 /*template_keyword_p=*/true,
17024 /*check_dependency_p=*/true,
17025 none_type,
17026 /*is_declaration=*/false);
17027 /* If the template-id did not name a type, we are out of
17028 luck. */
17029 if (TREE_CODE (type) != TYPE_DECL)
17030 {
17031 cp_parser_error (parser, "expected template-id for type");
17032 type = NULL_TREE;
17033 }
17034 }
17035 /* Otherwise, look for a type-name. */
17036 else
17037 type = cp_parser_type_name (parser);
17038 /* Keep track of all name-lookups performed in class scopes. */
17039 if (type
17040 && !global_p
17041 && !qualified_p
17042 && TREE_CODE (type) == TYPE_DECL
17043 && identifier_p (DECL_NAME (type)))
17044 maybe_note_name_used_in_class (DECL_NAME (type), type);
17045 /* If it didn't work out, we don't have a TYPE. */
17046 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
17047 && !cp_parser_parse_definitely (parser))
17048 type = NULL_TREE;
17049 if (!type && cxx_dialect >= cxx1z)
17050 {
17051 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17052 cp_parser_parse_tentatively (parser);
17053
17054 cp_parser_global_scope_opt (parser,
17055 /*current_scope_valid_p=*/false);
17056 cp_parser_nested_name_specifier_opt (parser,
17057 /*typename_keyword_p=*/false,
17058 /*check_dependency_p=*/true,
17059 /*type_p=*/false,
17060 /*is_declaration=*/false);
17061 tree name = cp_parser_identifier (parser);
17062 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17063 && parser->scope != error_mark_node)
17064 {
17065 tree tmpl = cp_parser_lookup_name (parser, name,
17066 none_type,
17067 /*is_template=*/false,
17068 /*is_namespace=*/false,
17069 /*check_dependency=*/true,
17070 /*ambiguous_decls=*/NULL,
17071 token->location);
17072 if (tmpl && tmpl != error_mark_node
17073 && (DECL_CLASS_TEMPLATE_P (tmpl)
17074 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17075 type = make_template_placeholder (tmpl);
17076 else
17077 {
17078 type = error_mark_node;
17079 if (!cp_parser_simulate_error (parser))
17080 cp_parser_name_lookup_error (parser, name, tmpl,
17081 NLE_TYPE, token->location);
17082 }
17083 }
17084 else
17085 type = error_mark_node;
17086
17087 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17088 && !cp_parser_parse_definitely (parser))
17089 type = NULL_TREE;
17090 }
17091 if (type && decl_specs)
17092 cp_parser_set_decl_spec_type (decl_specs, type,
17093 token,
17094 /*type_definition_p=*/false);
17095 }
17096
17097 /* If we didn't get a type-name, issue an error message. */
17098 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17099 {
17100 cp_parser_error (parser, "expected type-name");
17101 return error_mark_node;
17102 }
17103
17104 if (type && type != error_mark_node)
17105 {
17106 /* See if TYPE is an Objective-C type, and if so, parse and
17107 accept any protocol references following it. Do this before
17108 the cp_parser_check_for_invalid_template_id() call, because
17109 Objective-C types can be followed by '<...>' which would
17110 enclose protocol names rather than template arguments, and so
17111 everything is fine. */
17112 if (c_dialect_objc () && !parser->scope
17113 && (objc_is_id (type) || objc_is_class_name (type)))
17114 {
17115 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17116 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17117
17118 /* Clobber the "unqualified" type previously entered into
17119 DECL_SPECS with the new, improved protocol-qualified version. */
17120 if (decl_specs)
17121 decl_specs->type = qual_type;
17122
17123 return qual_type;
17124 }
17125
17126 /* There is no valid C++ program where a non-template type is
17127 followed by a "<". That usually indicates that the user
17128 thought that the type was a template. */
17129 cp_parser_check_for_invalid_template_id (parser, type,
17130 none_type,
17131 token->location);
17132 }
17133
17134 return type;
17135 }
17136
17137 /* Parse a type-name.
17138
17139 type-name:
17140 class-name
17141 enum-name
17142 typedef-name
17143 simple-template-id [in c++0x]
17144
17145 enum-name:
17146 identifier
17147
17148 typedef-name:
17149 identifier
17150
17151 Concepts:
17152
17153 type-name:
17154 concept-name
17155 partial-concept-id
17156
17157 concept-name:
17158 identifier
17159
17160 Returns a TYPE_DECL for the type. */
17161
17162 static tree
17163 cp_parser_type_name (cp_parser* parser)
17164 {
17165 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17166 }
17167
17168 /* See above. */
17169 static tree
17170 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17171 {
17172 tree type_decl;
17173
17174 /* We can't know yet whether it is a class-name or not. */
17175 cp_parser_parse_tentatively (parser);
17176 /* Try a class-name. */
17177 type_decl = cp_parser_class_name (parser,
17178 typename_keyword_p,
17179 /*template_keyword_p=*/false,
17180 none_type,
17181 /*check_dependency_p=*/true,
17182 /*class_head_p=*/false,
17183 /*is_declaration=*/false);
17184 /* If it's not a class-name, keep looking. */
17185 if (!cp_parser_parse_definitely (parser))
17186 {
17187 if (cxx_dialect < cxx11)
17188 /* It must be a typedef-name or an enum-name. */
17189 return cp_parser_nonclass_name (parser);
17190
17191 cp_parser_parse_tentatively (parser);
17192 /* It is either a simple-template-id representing an
17193 instantiation of an alias template... */
17194 type_decl = cp_parser_template_id (parser,
17195 /*template_keyword_p=*/false,
17196 /*check_dependency_p=*/true,
17197 none_type,
17198 /*is_declaration=*/false);
17199 /* Note that this must be an instantiation of an alias template
17200 because [temp.names]/6 says:
17201
17202 A template-id that names an alias template specialization
17203 is a type-name.
17204
17205 Whereas [temp.names]/7 says:
17206
17207 A simple-template-id that names a class template
17208 specialization is a class-name.
17209
17210 With concepts, this could also be a partial-concept-id that
17211 declares a non-type template parameter. */
17212 if (type_decl != NULL_TREE
17213 && TREE_CODE (type_decl) == TYPE_DECL
17214 && TYPE_DECL_ALIAS_P (type_decl))
17215 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17216 else if (is_constrained_parameter (type_decl))
17217 /* Don't do anything. */ ;
17218 else
17219 cp_parser_simulate_error (parser);
17220
17221 if (!cp_parser_parse_definitely (parser))
17222 /* ... Or a typedef-name or an enum-name. */
17223 return cp_parser_nonclass_name (parser);
17224 }
17225
17226 return type_decl;
17227 }
17228
17229 /* Check if DECL and ARGS can form a constrained-type-specifier.
17230 If ARGS is non-null, we try to form a concept check of the
17231 form DECL<?, ARGS> where ? is a wildcard that matches any
17232 kind of template argument. If ARGS is NULL, then we try to
17233 form a concept check of the form DECL<?>. */
17234
17235 static tree
17236 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17237 tree decl, tree args)
17238 {
17239 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17240
17241 /* If we a constrained-type-specifier cannot be deduced. */
17242 if (parser->prevent_constrained_type_specifiers)
17243 return NULL_TREE;
17244
17245 /* A constrained type specifier can only be found in an
17246 overload set or as a reference to a template declaration.
17247
17248 FIXME: This might be masking a bug. It's possible that
17249 that the deduction below is causing template specializations
17250 to be formed with the wildcard as an argument. */
17251 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17252 return NULL_TREE;
17253
17254 /* Try to build a call expression that evaluates the
17255 concept. This can fail if the overload set refers
17256 only to non-templates. */
17257 tree placeholder = build_nt (WILDCARD_DECL);
17258 tree check = build_concept_check (decl, placeholder, args);
17259 if (check == error_mark_node)
17260 return NULL_TREE;
17261
17262 /* Deduce the checked constraint and the prototype parameter.
17263
17264 FIXME: In certain cases, failure to deduce should be a
17265 diagnosable error. */
17266 tree conc;
17267 tree proto;
17268 if (!deduce_constrained_parameter (check, conc, proto))
17269 return NULL_TREE;
17270
17271 /* In template parameter scope, this results in a constrained
17272 parameter. Return a descriptor of that parm. */
17273 if (processing_template_parmlist)
17274 return build_constrained_parameter (conc, proto, args);
17275
17276 /* In a parameter-declaration-clause, constrained-type
17277 specifiers result in invented template parameters. */
17278 if (parser->auto_is_implicit_function_template_parm_p)
17279 {
17280 tree x = build_constrained_parameter (conc, proto, args);
17281 return synthesize_implicit_template_parm (parser, x);
17282 }
17283 else
17284 {
17285 /* Otherwise, we're in a context where the constrained
17286 type name is deduced and the constraint applies
17287 after deduction. */
17288 return make_constrained_auto (conc, args);
17289 }
17290
17291 return NULL_TREE;
17292 }
17293
17294 /* If DECL refers to a concept, return a TYPE_DECL representing
17295 the result of using the constrained type specifier in the
17296 current context. DECL refers to a concept if
17297
17298 - it is an overload set containing a function concept taking a single
17299 type argument, or
17300
17301 - it is a variable concept taking a single type argument. */
17302
17303 static tree
17304 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17305 {
17306 if (flag_concepts
17307 && (TREE_CODE (decl) == OVERLOAD
17308 || BASELINK_P (decl)
17309 || variable_concept_p (decl)))
17310 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17311 else
17312 return NULL_TREE;
17313 }
17314
17315 /* Check if DECL and ARGS form a partial-concept-id. If so,
17316 assign ID to the resulting constrained placeholder.
17317
17318 Returns true if the partial-concept-id designates a placeholder
17319 and false otherwise. Note that *id is set to NULL_TREE in
17320 this case. */
17321
17322 static tree
17323 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17324 {
17325 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17326 }
17327
17328 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17329 or a concept-name.
17330
17331 enum-name:
17332 identifier
17333
17334 typedef-name:
17335 identifier
17336
17337 concept-name:
17338 identifier
17339
17340 Returns a TYPE_DECL for the type. */
17341
17342 static tree
17343 cp_parser_nonclass_name (cp_parser* parser)
17344 {
17345 tree type_decl;
17346 tree identifier;
17347
17348 cp_token *token = cp_lexer_peek_token (parser->lexer);
17349 identifier = cp_parser_identifier (parser);
17350 if (identifier == error_mark_node)
17351 return error_mark_node;
17352
17353 /* Look up the type-name. */
17354 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17355
17356 type_decl = strip_using_decl (type_decl);
17357
17358 /* If we found an overload set, then it may refer to a concept-name. */
17359 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17360 type_decl = decl;
17361
17362 if (TREE_CODE (type_decl) != TYPE_DECL
17363 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17364 {
17365 /* See if this is an Objective-C type. */
17366 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17367 tree type = objc_get_protocol_qualified_type (identifier, protos);
17368 if (type)
17369 type_decl = TYPE_NAME (type);
17370 }
17371
17372 /* Issue an error if we did not find a type-name. */
17373 if (TREE_CODE (type_decl) != TYPE_DECL
17374 /* In Objective-C, we have the complication that class names are
17375 normally type names and start declarations (eg, the
17376 "NSObject" in "NSObject *object;"), but can be used in an
17377 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17378 is an expression. So, a classname followed by a dot is not a
17379 valid type-name. */
17380 || (objc_is_class_name (TREE_TYPE (type_decl))
17381 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17382 {
17383 if (!cp_parser_simulate_error (parser))
17384 cp_parser_name_lookup_error (parser, identifier, type_decl,
17385 NLE_TYPE, token->location);
17386 return error_mark_node;
17387 }
17388 /* Remember that the name was used in the definition of the
17389 current class so that we can check later to see if the
17390 meaning would have been different after the class was
17391 entirely defined. */
17392 else if (type_decl != error_mark_node
17393 && !parser->scope)
17394 maybe_note_name_used_in_class (identifier, type_decl);
17395
17396 return type_decl;
17397 }
17398
17399 /* Parse an elaborated-type-specifier. Note that the grammar given
17400 here incorporates the resolution to DR68.
17401
17402 elaborated-type-specifier:
17403 class-key :: [opt] nested-name-specifier [opt] identifier
17404 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17405 enum-key :: [opt] nested-name-specifier [opt] identifier
17406 typename :: [opt] nested-name-specifier identifier
17407 typename :: [opt] nested-name-specifier template [opt]
17408 template-id
17409
17410 GNU extension:
17411
17412 elaborated-type-specifier:
17413 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17414 class-key attributes :: [opt] nested-name-specifier [opt]
17415 template [opt] template-id
17416 enum attributes :: [opt] nested-name-specifier [opt] identifier
17417
17418 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17419 declared `friend'. If IS_DECLARATION is TRUE, then this
17420 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17421 something is being declared.
17422
17423 Returns the TYPE specified. */
17424
17425 static tree
17426 cp_parser_elaborated_type_specifier (cp_parser* parser,
17427 bool is_friend,
17428 bool is_declaration)
17429 {
17430 enum tag_types tag_type;
17431 tree identifier;
17432 tree type = NULL_TREE;
17433 tree attributes = NULL_TREE;
17434 tree globalscope;
17435 cp_token *token = NULL;
17436
17437 /* See if we're looking at the `enum' keyword. */
17438 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17439 {
17440 /* Consume the `enum' token. */
17441 cp_lexer_consume_token (parser->lexer);
17442 /* Remember that it's an enumeration type. */
17443 tag_type = enum_type;
17444 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17445 enums) is used here. */
17446 cp_token *token = cp_lexer_peek_token (parser->lexer);
17447 if (cp_parser_is_keyword (token, RID_CLASS)
17448 || cp_parser_is_keyword (token, RID_STRUCT))
17449 {
17450 gcc_rich_location richloc (token->location);
17451 richloc.add_range (input_location, false);
17452 richloc.add_fixit_remove ();
17453 pedwarn_at_rich_loc (&richloc, 0, "elaborated-type-specifier for "
17454 "a scoped enum must not use the %qD keyword",
17455 token->u.value);
17456 /* Consume the `struct' or `class' and parse it anyway. */
17457 cp_lexer_consume_token (parser->lexer);
17458 }
17459 /* Parse the attributes. */
17460 attributes = cp_parser_attributes_opt (parser);
17461 }
17462 /* Or, it might be `typename'. */
17463 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17464 RID_TYPENAME))
17465 {
17466 /* Consume the `typename' token. */
17467 cp_lexer_consume_token (parser->lexer);
17468 /* Remember that it's a `typename' type. */
17469 tag_type = typename_type;
17470 }
17471 /* Otherwise it must be a class-key. */
17472 else
17473 {
17474 tag_type = cp_parser_class_key (parser);
17475 if (tag_type == none_type)
17476 return error_mark_node;
17477 /* Parse the attributes. */
17478 attributes = cp_parser_attributes_opt (parser);
17479 }
17480
17481 /* Look for the `::' operator. */
17482 globalscope = cp_parser_global_scope_opt (parser,
17483 /*current_scope_valid_p=*/false);
17484 /* Look for the nested-name-specifier. */
17485 tree nested_name_specifier;
17486 if (tag_type == typename_type && !globalscope)
17487 {
17488 nested_name_specifier
17489 = cp_parser_nested_name_specifier (parser,
17490 /*typename_keyword_p=*/true,
17491 /*check_dependency_p=*/true,
17492 /*type_p=*/true,
17493 is_declaration);
17494 if (!nested_name_specifier)
17495 return error_mark_node;
17496 }
17497 else
17498 /* Even though `typename' is not present, the proposed resolution
17499 to Core Issue 180 says that in `class A<T>::B', `B' should be
17500 considered a type-name, even if `A<T>' is dependent. */
17501 nested_name_specifier
17502 = cp_parser_nested_name_specifier_opt (parser,
17503 /*typename_keyword_p=*/true,
17504 /*check_dependency_p=*/true,
17505 /*type_p=*/true,
17506 is_declaration);
17507 /* For everything but enumeration types, consider a template-id.
17508 For an enumeration type, consider only a plain identifier. */
17509 if (tag_type != enum_type)
17510 {
17511 bool template_p = false;
17512 tree decl;
17513
17514 /* Allow the `template' keyword. */
17515 template_p = cp_parser_optional_template_keyword (parser);
17516 /* If we didn't see `template', we don't know if there's a
17517 template-id or not. */
17518 if (!template_p)
17519 cp_parser_parse_tentatively (parser);
17520 /* Parse the template-id. */
17521 token = cp_lexer_peek_token (parser->lexer);
17522 decl = cp_parser_template_id (parser, template_p,
17523 /*check_dependency_p=*/true,
17524 tag_type,
17525 is_declaration);
17526 /* If we didn't find a template-id, look for an ordinary
17527 identifier. */
17528 if (!template_p && !cp_parser_parse_definitely (parser))
17529 ;
17530 /* We can get here when cp_parser_template_id, called by
17531 cp_parser_class_name with tag_type == none_type, succeeds
17532 and caches a BASELINK. Then, when called again here,
17533 instead of failing and returning an error_mark_node
17534 returns it (see template/typename17.C in C++11).
17535 ??? Could we diagnose this earlier? */
17536 else if (tag_type == typename_type && BASELINK_P (decl))
17537 {
17538 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17539 type = error_mark_node;
17540 }
17541 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17542 in effect, then we must assume that, upon instantiation, the
17543 template will correspond to a class. */
17544 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17545 && tag_type == typename_type)
17546 type = make_typename_type (parser->scope, decl,
17547 typename_type,
17548 /*complain=*/tf_error);
17549 /* If the `typename' keyword is in effect and DECL is not a type
17550 decl, then type is non existent. */
17551 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17552 ;
17553 else if (TREE_CODE (decl) == TYPE_DECL)
17554 {
17555 type = check_elaborated_type_specifier (tag_type, decl,
17556 /*allow_template_p=*/true);
17557
17558 /* If the next token is a semicolon, this must be a specialization,
17559 instantiation, or friend declaration. Check the scope while we
17560 still know whether or not we had a nested-name-specifier. */
17561 if (type != error_mark_node
17562 && !nested_name_specifier && !is_friend
17563 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17564 check_unqualified_spec_or_inst (type, token->location);
17565 }
17566 else if (decl == error_mark_node)
17567 type = error_mark_node;
17568 }
17569
17570 if (!type)
17571 {
17572 token = cp_lexer_peek_token (parser->lexer);
17573 identifier = cp_parser_identifier (parser);
17574
17575 if (identifier == error_mark_node)
17576 {
17577 parser->scope = NULL_TREE;
17578 return error_mark_node;
17579 }
17580
17581 /* For a `typename', we needn't call xref_tag. */
17582 if (tag_type == typename_type
17583 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17584 return cp_parser_make_typename_type (parser, identifier,
17585 token->location);
17586
17587 /* Template parameter lists apply only if we are not within a
17588 function parameter list. */
17589 bool template_parm_lists_apply
17590 = parser->num_template_parameter_lists;
17591 if (template_parm_lists_apply)
17592 for (cp_binding_level *s = current_binding_level;
17593 s && s->kind != sk_template_parms;
17594 s = s->level_chain)
17595 if (s->kind == sk_function_parms)
17596 template_parm_lists_apply = false;
17597
17598 /* Look up a qualified name in the usual way. */
17599 if (parser->scope)
17600 {
17601 tree decl;
17602 tree ambiguous_decls;
17603
17604 decl = cp_parser_lookup_name (parser, identifier,
17605 tag_type,
17606 /*is_template=*/false,
17607 /*is_namespace=*/false,
17608 /*check_dependency=*/true,
17609 &ambiguous_decls,
17610 token->location);
17611
17612 /* If the lookup was ambiguous, an error will already have been
17613 issued. */
17614 if (ambiguous_decls)
17615 return error_mark_node;
17616
17617 /* If we are parsing friend declaration, DECL may be a
17618 TEMPLATE_DECL tree node here. However, we need to check
17619 whether this TEMPLATE_DECL results in valid code. Consider
17620 the following example:
17621
17622 namespace N {
17623 template <class T> class C {};
17624 }
17625 class X {
17626 template <class T> friend class N::C; // #1, valid code
17627 };
17628 template <class T> class Y {
17629 friend class N::C; // #2, invalid code
17630 };
17631
17632 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17633 name lookup of `N::C'. We see that friend declaration must
17634 be template for the code to be valid. Note that
17635 processing_template_decl does not work here since it is
17636 always 1 for the above two cases. */
17637
17638 decl = (cp_parser_maybe_treat_template_as_class
17639 (decl, /*tag_name_p=*/is_friend
17640 && template_parm_lists_apply));
17641
17642 if (TREE_CODE (decl) != TYPE_DECL)
17643 {
17644 cp_parser_diagnose_invalid_type_name (parser,
17645 identifier,
17646 token->location);
17647 return error_mark_node;
17648 }
17649
17650 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17651 {
17652 bool allow_template = (template_parm_lists_apply
17653 || DECL_SELF_REFERENCE_P (decl));
17654 type = check_elaborated_type_specifier (tag_type, decl,
17655 allow_template);
17656
17657 if (type == error_mark_node)
17658 return error_mark_node;
17659 }
17660
17661 /* Forward declarations of nested types, such as
17662
17663 class C1::C2;
17664 class C1::C2::C3;
17665
17666 are invalid unless all components preceding the final '::'
17667 are complete. If all enclosing types are complete, these
17668 declarations become merely pointless.
17669
17670 Invalid forward declarations of nested types are errors
17671 caught elsewhere in parsing. Those that are pointless arrive
17672 here. */
17673
17674 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17675 && !is_friend && !processing_explicit_instantiation)
17676 warning (0, "declaration %qD does not declare anything", decl);
17677
17678 type = TREE_TYPE (decl);
17679 }
17680 else
17681 {
17682 /* An elaborated-type-specifier sometimes introduces a new type and
17683 sometimes names an existing type. Normally, the rule is that it
17684 introduces a new type only if there is not an existing type of
17685 the same name already in scope. For example, given:
17686
17687 struct S {};
17688 void f() { struct S s; }
17689
17690 the `struct S' in the body of `f' is the same `struct S' as in
17691 the global scope; the existing definition is used. However, if
17692 there were no global declaration, this would introduce a new
17693 local class named `S'.
17694
17695 An exception to this rule applies to the following code:
17696
17697 namespace N { struct S; }
17698
17699 Here, the elaborated-type-specifier names a new type
17700 unconditionally; even if there is already an `S' in the
17701 containing scope this declaration names a new type.
17702 This exception only applies if the elaborated-type-specifier
17703 forms the complete declaration:
17704
17705 [class.name]
17706
17707 A declaration consisting solely of `class-key identifier ;' is
17708 either a redeclaration of the name in the current scope or a
17709 forward declaration of the identifier as a class name. It
17710 introduces the name into the current scope.
17711
17712 We are in this situation precisely when the next token is a `;'.
17713
17714 An exception to the exception is that a `friend' declaration does
17715 *not* name a new type; i.e., given:
17716
17717 struct S { friend struct T; };
17718
17719 `T' is not a new type in the scope of `S'.
17720
17721 Also, `new struct S' or `sizeof (struct S)' never results in the
17722 definition of a new type; a new type can only be declared in a
17723 declaration context. */
17724
17725 tag_scope ts;
17726 bool template_p;
17727
17728 if (is_friend)
17729 /* Friends have special name lookup rules. */
17730 ts = ts_within_enclosing_non_class;
17731 else if (is_declaration
17732 && cp_lexer_next_token_is (parser->lexer,
17733 CPP_SEMICOLON))
17734 /* This is a `class-key identifier ;' */
17735 ts = ts_current;
17736 else
17737 ts = ts_global;
17738
17739 template_p =
17740 (template_parm_lists_apply
17741 && (cp_parser_next_token_starts_class_definition_p (parser)
17742 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17743 /* An unqualified name was used to reference this type, so
17744 there were no qualifying templates. */
17745 if (template_parm_lists_apply
17746 && !cp_parser_check_template_parameters (parser,
17747 /*num_templates=*/0,
17748 token->location,
17749 /*declarator=*/NULL))
17750 return error_mark_node;
17751 type = xref_tag (tag_type, identifier, ts, template_p);
17752 }
17753 }
17754
17755 if (type == error_mark_node)
17756 return error_mark_node;
17757
17758 /* Allow attributes on forward declarations of classes. */
17759 if (attributes)
17760 {
17761 if (TREE_CODE (type) == TYPENAME_TYPE)
17762 warning (OPT_Wattributes,
17763 "attributes ignored on uninstantiated type");
17764 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17765 && ! processing_explicit_instantiation)
17766 warning (OPT_Wattributes,
17767 "attributes ignored on template instantiation");
17768 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17769 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17770 else
17771 warning (OPT_Wattributes,
17772 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17773 }
17774
17775 if (tag_type != enum_type)
17776 {
17777 /* Indicate whether this class was declared as a `class' or as a
17778 `struct'. */
17779 if (CLASS_TYPE_P (type))
17780 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17781 cp_parser_check_class_key (tag_type, type);
17782 }
17783
17784 /* A "<" cannot follow an elaborated type specifier. If that
17785 happens, the user was probably trying to form a template-id. */
17786 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17787 token->location);
17788
17789 return type;
17790 }
17791
17792 /* Parse an enum-specifier.
17793
17794 enum-specifier:
17795 enum-head { enumerator-list [opt] }
17796 enum-head { enumerator-list , } [C++0x]
17797
17798 enum-head:
17799 enum-key identifier [opt] enum-base [opt]
17800 enum-key nested-name-specifier identifier enum-base [opt]
17801
17802 enum-key:
17803 enum
17804 enum class [C++0x]
17805 enum struct [C++0x]
17806
17807 enum-base: [C++0x]
17808 : type-specifier-seq
17809
17810 opaque-enum-specifier:
17811 enum-key identifier enum-base [opt] ;
17812
17813 GNU Extensions:
17814 enum-key attributes[opt] identifier [opt] enum-base [opt]
17815 { enumerator-list [opt] }attributes[opt]
17816 enum-key attributes[opt] identifier [opt] enum-base [opt]
17817 { enumerator-list, }attributes[opt] [C++0x]
17818
17819 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17820 if the token stream isn't an enum-specifier after all. */
17821
17822 static tree
17823 cp_parser_enum_specifier (cp_parser* parser)
17824 {
17825 tree identifier;
17826 tree type = NULL_TREE;
17827 tree prev_scope;
17828 tree nested_name_specifier = NULL_TREE;
17829 tree attributes;
17830 bool scoped_enum_p = false;
17831 bool has_underlying_type = false;
17832 bool nested_being_defined = false;
17833 bool new_value_list = false;
17834 bool is_new_type = false;
17835 bool is_unnamed = false;
17836 tree underlying_type = NULL_TREE;
17837 cp_token *type_start_token = NULL;
17838 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17839
17840 parser->colon_corrects_to_scope_p = false;
17841
17842 /* Parse tentatively so that we can back up if we don't find a
17843 enum-specifier. */
17844 cp_parser_parse_tentatively (parser);
17845
17846 /* Caller guarantees that the current token is 'enum', an identifier
17847 possibly follows, and the token after that is an opening brace.
17848 If we don't have an identifier, fabricate an anonymous name for
17849 the enumeration being defined. */
17850 cp_lexer_consume_token (parser->lexer);
17851
17852 /* Parse the "class" or "struct", which indicates a scoped
17853 enumeration type in C++0x. */
17854 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17855 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17856 {
17857 if (cxx_dialect < cxx11)
17858 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17859
17860 /* Consume the `struct' or `class' token. */
17861 cp_lexer_consume_token (parser->lexer);
17862
17863 scoped_enum_p = true;
17864 }
17865
17866 attributes = cp_parser_attributes_opt (parser);
17867
17868 /* Clear the qualification. */
17869 parser->scope = NULL_TREE;
17870 parser->qualifying_scope = NULL_TREE;
17871 parser->object_scope = NULL_TREE;
17872
17873 /* Figure out in what scope the declaration is being placed. */
17874 prev_scope = current_scope ();
17875
17876 type_start_token = cp_lexer_peek_token (parser->lexer);
17877
17878 push_deferring_access_checks (dk_no_check);
17879 nested_name_specifier
17880 = cp_parser_nested_name_specifier_opt (parser,
17881 /*typename_keyword_p=*/true,
17882 /*check_dependency_p=*/false,
17883 /*type_p=*/false,
17884 /*is_declaration=*/false);
17885
17886 if (nested_name_specifier)
17887 {
17888 tree name;
17889
17890 identifier = cp_parser_identifier (parser);
17891 name = cp_parser_lookup_name (parser, identifier,
17892 enum_type,
17893 /*is_template=*/false,
17894 /*is_namespace=*/false,
17895 /*check_dependency=*/true,
17896 /*ambiguous_decls=*/NULL,
17897 input_location);
17898 if (name && name != error_mark_node)
17899 {
17900 type = TREE_TYPE (name);
17901 if (TREE_CODE (type) == TYPENAME_TYPE)
17902 {
17903 /* Are template enums allowed in ISO? */
17904 if (template_parm_scope_p ())
17905 pedwarn (type_start_token->location, OPT_Wpedantic,
17906 "%qD is an enumeration template", name);
17907 /* ignore a typename reference, for it will be solved by name
17908 in start_enum. */
17909 type = NULL_TREE;
17910 }
17911 }
17912 else if (nested_name_specifier == error_mark_node)
17913 /* We already issued an error. */;
17914 else
17915 {
17916 error_at (type_start_token->location,
17917 "%qD does not name an enumeration in %qT",
17918 identifier, nested_name_specifier);
17919 nested_name_specifier = error_mark_node;
17920 }
17921 }
17922 else
17923 {
17924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17925 identifier = cp_parser_identifier (parser);
17926 else
17927 {
17928 identifier = make_anon_name ();
17929 is_unnamed = true;
17930 if (scoped_enum_p)
17931 error_at (type_start_token->location,
17932 "unnamed scoped enum is not allowed");
17933 }
17934 }
17935 pop_deferring_access_checks ();
17936
17937 /* Check for the `:' that denotes a specified underlying type in C++0x.
17938 Note that a ':' could also indicate a bitfield width, however. */
17939 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17940 {
17941 cp_decl_specifier_seq type_specifiers;
17942
17943 /* Consume the `:'. */
17944 cp_lexer_consume_token (parser->lexer);
17945
17946 /* Parse the type-specifier-seq. */
17947 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17948 /*is_trailing_return=*/false,
17949 &type_specifiers);
17950
17951 /* At this point this is surely not elaborated type specifier. */
17952 if (!cp_parser_parse_definitely (parser))
17953 return NULL_TREE;
17954
17955 if (cxx_dialect < cxx11)
17956 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17957
17958 has_underlying_type = true;
17959
17960 /* If that didn't work, stop. */
17961 if (type_specifiers.type != error_mark_node)
17962 {
17963 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17964 /*initialized=*/0, NULL);
17965 if (underlying_type == error_mark_node
17966 || check_for_bare_parameter_packs (underlying_type))
17967 underlying_type = NULL_TREE;
17968 }
17969 }
17970
17971 /* Look for the `{' but don't consume it yet. */
17972 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17973 {
17974 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17975 {
17976 cp_parser_error (parser, "expected %<{%>");
17977 if (has_underlying_type)
17978 {
17979 type = NULL_TREE;
17980 goto out;
17981 }
17982 }
17983 /* An opaque-enum-specifier must have a ';' here. */
17984 if ((scoped_enum_p || underlying_type)
17985 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17986 {
17987 cp_parser_error (parser, "expected %<;%> or %<{%>");
17988 if (has_underlying_type)
17989 {
17990 type = NULL_TREE;
17991 goto out;
17992 }
17993 }
17994 }
17995
17996 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17997 return NULL_TREE;
17998
17999 if (nested_name_specifier)
18000 {
18001 if (CLASS_TYPE_P (nested_name_specifier))
18002 {
18003 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18004 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18005 push_scope (nested_name_specifier);
18006 }
18007 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18008 {
18009 push_nested_namespace (nested_name_specifier);
18010 }
18011 }
18012
18013 /* Issue an error message if type-definitions are forbidden here. */
18014 if (!cp_parser_check_type_definition (parser))
18015 type = error_mark_node;
18016 else
18017 /* Create the new type. We do this before consuming the opening
18018 brace so the enum will be recorded as being on the line of its
18019 tag (or the 'enum' keyword, if there is no tag). */
18020 type = start_enum (identifier, type, underlying_type,
18021 attributes, scoped_enum_p, &is_new_type);
18022
18023 /* If the next token is not '{' it is an opaque-enum-specifier or an
18024 elaborated-type-specifier. */
18025 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18026 {
18027 timevar_push (TV_PARSE_ENUM);
18028 if (nested_name_specifier
18029 && nested_name_specifier != error_mark_node)
18030 {
18031 /* The following catches invalid code such as:
18032 enum class S<int>::E { A, B, C }; */
18033 if (!processing_specialization
18034 && CLASS_TYPE_P (nested_name_specifier)
18035 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18036 error_at (type_start_token->location, "cannot add an enumerator "
18037 "list to a template instantiation");
18038
18039 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18040 {
18041 error_at (type_start_token->location,
18042 "%<%T::%E%> has not been declared",
18043 TYPE_CONTEXT (nested_name_specifier),
18044 nested_name_specifier);
18045 type = error_mark_node;
18046 }
18047 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18048 && !CLASS_TYPE_P (nested_name_specifier))
18049 {
18050 error_at (type_start_token->location, "nested name specifier "
18051 "%qT for enum declaration does not name a class "
18052 "or namespace", nested_name_specifier);
18053 type = error_mark_node;
18054 }
18055 /* If that scope does not contain the scope in which the
18056 class was originally declared, the program is invalid. */
18057 else if (prev_scope && !is_ancestor (prev_scope,
18058 nested_name_specifier))
18059 {
18060 if (at_namespace_scope_p ())
18061 error_at (type_start_token->location,
18062 "declaration of %qD in namespace %qD which does not "
18063 "enclose %qD",
18064 type, prev_scope, nested_name_specifier);
18065 else
18066 error_at (type_start_token->location,
18067 "declaration of %qD in %qD which does not "
18068 "enclose %qD",
18069 type, prev_scope, nested_name_specifier);
18070 type = error_mark_node;
18071 }
18072 /* If that scope is the scope where the declaration is being placed
18073 the program is invalid. */
18074 else if (CLASS_TYPE_P (nested_name_specifier)
18075 && CLASS_TYPE_P (prev_scope)
18076 && same_type_p (nested_name_specifier, prev_scope))
18077 {
18078 permerror (type_start_token->location,
18079 "extra qualification not allowed");
18080 nested_name_specifier = NULL_TREE;
18081 }
18082 }
18083
18084 if (scoped_enum_p)
18085 begin_scope (sk_scoped_enum, type);
18086
18087 /* Consume the opening brace. */
18088 cp_lexer_consume_token (parser->lexer);
18089
18090 if (type == error_mark_node)
18091 ; /* Nothing to add */
18092 else if (OPAQUE_ENUM_P (type)
18093 || (cxx_dialect > cxx98 && processing_specialization))
18094 {
18095 new_value_list = true;
18096 SET_OPAQUE_ENUM_P (type, false);
18097 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18098 }
18099 else
18100 {
18101 error_at (type_start_token->location,
18102 "multiple definition of %q#T", type);
18103 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18104 "previous definition here");
18105 type = error_mark_node;
18106 }
18107
18108 if (type == error_mark_node)
18109 cp_parser_skip_to_end_of_block_or_statement (parser);
18110 /* If the next token is not '}', then there are some enumerators. */
18111 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18112 {
18113 if (is_unnamed && !scoped_enum_p)
18114 pedwarn (type_start_token->location, OPT_Wpedantic,
18115 "ISO C++ forbids empty unnamed enum");
18116 }
18117 else
18118 cp_parser_enumerator_list (parser, type);
18119
18120 /* Consume the final '}'. */
18121 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18122
18123 if (scoped_enum_p)
18124 finish_scope ();
18125 timevar_pop (TV_PARSE_ENUM);
18126 }
18127 else
18128 {
18129 /* If a ';' follows, then it is an opaque-enum-specifier
18130 and additional restrictions apply. */
18131 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18132 {
18133 if (is_unnamed)
18134 error_at (type_start_token->location,
18135 "opaque-enum-specifier without name");
18136 else if (nested_name_specifier)
18137 error_at (type_start_token->location,
18138 "opaque-enum-specifier must use a simple identifier");
18139 }
18140 }
18141
18142 /* Look for trailing attributes to apply to this enumeration, and
18143 apply them if appropriate. */
18144 if (cp_parser_allow_gnu_extensions_p (parser))
18145 {
18146 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18147 cplus_decl_attributes (&type,
18148 trailing_attr,
18149 (int) ATTR_FLAG_TYPE_IN_PLACE);
18150 }
18151
18152 /* Finish up the enumeration. */
18153 if (type != error_mark_node)
18154 {
18155 if (new_value_list)
18156 finish_enum_value_list (type);
18157 if (is_new_type)
18158 finish_enum (type);
18159 }
18160
18161 if (nested_name_specifier)
18162 {
18163 if (CLASS_TYPE_P (nested_name_specifier))
18164 {
18165 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18166 pop_scope (nested_name_specifier);
18167 }
18168 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18169 {
18170 pop_nested_namespace (nested_name_specifier);
18171 }
18172 }
18173 out:
18174 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18175 return type;
18176 }
18177
18178 /* Parse an enumerator-list. The enumerators all have the indicated
18179 TYPE.
18180
18181 enumerator-list:
18182 enumerator-definition
18183 enumerator-list , enumerator-definition */
18184
18185 static void
18186 cp_parser_enumerator_list (cp_parser* parser, tree type)
18187 {
18188 while (true)
18189 {
18190 /* Parse an enumerator-definition. */
18191 cp_parser_enumerator_definition (parser, type);
18192
18193 /* If the next token is not a ',', we've reached the end of
18194 the list. */
18195 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18196 break;
18197 /* Otherwise, consume the `,' and keep going. */
18198 cp_lexer_consume_token (parser->lexer);
18199 /* If the next token is a `}', there is a trailing comma. */
18200 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18201 {
18202 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18203 pedwarn (input_location, OPT_Wpedantic,
18204 "comma at end of enumerator list");
18205 break;
18206 }
18207 }
18208 }
18209
18210 /* Parse an enumerator-definition. The enumerator has the indicated
18211 TYPE.
18212
18213 enumerator-definition:
18214 enumerator
18215 enumerator = constant-expression
18216
18217 enumerator:
18218 identifier
18219
18220 GNU Extensions:
18221
18222 enumerator-definition:
18223 enumerator attributes [opt]
18224 enumerator attributes [opt] = constant-expression */
18225
18226 static void
18227 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18228 {
18229 tree identifier;
18230 tree value;
18231 location_t loc;
18232
18233 /* Save the input location because we are interested in the location
18234 of the identifier and not the location of the explicit value. */
18235 loc = cp_lexer_peek_token (parser->lexer)->location;
18236
18237 /* Look for the identifier. */
18238 identifier = cp_parser_identifier (parser);
18239 if (identifier == error_mark_node)
18240 return;
18241
18242 /* Parse any specified attributes. */
18243 tree attrs = cp_parser_attributes_opt (parser);
18244
18245 /* If the next token is an '=', then there is an explicit value. */
18246 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18247 {
18248 /* Consume the `=' token. */
18249 cp_lexer_consume_token (parser->lexer);
18250 /* Parse the value. */
18251 value = cp_parser_constant_expression (parser);
18252 }
18253 else
18254 value = NULL_TREE;
18255
18256 /* If we are processing a template, make sure the initializer of the
18257 enumerator doesn't contain any bare template parameter pack. */
18258 if (check_for_bare_parameter_packs (value))
18259 value = error_mark_node;
18260
18261 /* Create the enumerator. */
18262 build_enumerator (identifier, value, type, attrs, loc);
18263 }
18264
18265 /* Parse a namespace-name.
18266
18267 namespace-name:
18268 original-namespace-name
18269 namespace-alias
18270
18271 Returns the NAMESPACE_DECL for the namespace. */
18272
18273 static tree
18274 cp_parser_namespace_name (cp_parser* parser)
18275 {
18276 tree identifier;
18277 tree namespace_decl;
18278
18279 cp_token *token = cp_lexer_peek_token (parser->lexer);
18280
18281 /* Get the name of the namespace. */
18282 identifier = cp_parser_identifier (parser);
18283 if (identifier == error_mark_node)
18284 return error_mark_node;
18285
18286 /* Look up the identifier in the currently active scope. Look only
18287 for namespaces, due to:
18288
18289 [basic.lookup.udir]
18290
18291 When looking up a namespace-name in a using-directive or alias
18292 definition, only namespace names are considered.
18293
18294 And:
18295
18296 [basic.lookup.qual]
18297
18298 During the lookup of a name preceding the :: scope resolution
18299 operator, object, function, and enumerator names are ignored.
18300
18301 (Note that cp_parser_qualifying_entity only calls this
18302 function if the token after the name is the scope resolution
18303 operator.) */
18304 namespace_decl = cp_parser_lookup_name (parser, identifier,
18305 none_type,
18306 /*is_template=*/false,
18307 /*is_namespace=*/true,
18308 /*check_dependency=*/true,
18309 /*ambiguous_decls=*/NULL,
18310 token->location);
18311 /* If it's not a namespace, issue an error. */
18312 if (namespace_decl == error_mark_node
18313 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18314 {
18315 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18316 error_at (token->location, "%qD is not a namespace-name", identifier);
18317 cp_parser_error (parser, "expected namespace-name");
18318 namespace_decl = error_mark_node;
18319 }
18320
18321 return namespace_decl;
18322 }
18323
18324 /* Parse a namespace-definition.
18325
18326 namespace-definition:
18327 named-namespace-definition
18328 unnamed-namespace-definition
18329
18330 named-namespace-definition:
18331 original-namespace-definition
18332 extension-namespace-definition
18333
18334 original-namespace-definition:
18335 namespace identifier { namespace-body }
18336
18337 extension-namespace-definition:
18338 namespace original-namespace-name { namespace-body }
18339
18340 unnamed-namespace-definition:
18341 namespace { namespace-body } */
18342
18343 static void
18344 cp_parser_namespace_definition (cp_parser* parser)
18345 {
18346 tree identifier;
18347 int nested_definition_count = 0;
18348
18349 cp_ensure_no_omp_declare_simd (parser);
18350 cp_ensure_no_oacc_routine (parser);
18351
18352 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18353
18354 if (is_inline)
18355 {
18356 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18357 cp_lexer_consume_token (parser->lexer);
18358 }
18359
18360 /* Look for the `namespace' keyword. */
18361 cp_token* token
18362 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18363
18364 /* Parse any specified attributes before the identifier. */
18365 tree attribs = cp_parser_attributes_opt (parser);
18366
18367 for (;;)
18368 {
18369 identifier = NULL_TREE;
18370
18371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18372 {
18373 identifier = cp_parser_identifier (parser);
18374
18375 /* Parse any attributes specified after the identifier. */
18376 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18377 }
18378
18379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18380 break;
18381
18382 if (!nested_definition_count && cxx_dialect < cxx1z)
18383 pedwarn (input_location, OPT_Wpedantic,
18384 "nested namespace definitions only available with "
18385 "-std=c++1z or -std=gnu++1z");
18386
18387 /* Nested namespace names can create new namespaces (unlike
18388 other qualified-ids). */
18389 if (int count = identifier ? push_namespace (identifier) : 0)
18390 nested_definition_count += count;
18391 else
18392 cp_parser_error (parser, "nested namespace name required");
18393 cp_lexer_consume_token (parser->lexer);
18394 }
18395
18396 if (nested_definition_count && !identifier)
18397 cp_parser_error (parser, "namespace name required");
18398
18399 if (nested_definition_count && attribs)
18400 error_at (token->location,
18401 "a nested namespace definition cannot have attributes");
18402 if (nested_definition_count && is_inline)
18403 error_at (token->location,
18404 "a nested namespace definition cannot be inline");
18405
18406 /* Start the namespace. */
18407 nested_definition_count += push_namespace (identifier, is_inline);
18408
18409 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18410
18411 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18412
18413 /* Look for the `{' to validate starting the namespace. */
18414 if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18415 {
18416 /* Parse the body of the namespace. */
18417 cp_parser_namespace_body (parser);
18418
18419 /* Look for the final `}'. */
18420 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18421 }
18422
18423 if (has_visibility)
18424 pop_visibility (1);
18425
18426 /* Pop the nested namespace definitions. */
18427 while (nested_definition_count--)
18428 pop_namespace ();
18429 }
18430
18431 /* Parse a namespace-body.
18432
18433 namespace-body:
18434 declaration-seq [opt] */
18435
18436 static void
18437 cp_parser_namespace_body (cp_parser* parser)
18438 {
18439 cp_parser_declaration_seq_opt (parser);
18440 }
18441
18442 /* Parse a namespace-alias-definition.
18443
18444 namespace-alias-definition:
18445 namespace identifier = qualified-namespace-specifier ; */
18446
18447 static void
18448 cp_parser_namespace_alias_definition (cp_parser* parser)
18449 {
18450 tree identifier;
18451 tree namespace_specifier;
18452
18453 cp_token *token = cp_lexer_peek_token (parser->lexer);
18454
18455 /* Look for the `namespace' keyword. */
18456 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18457 /* Look for the identifier. */
18458 identifier = cp_parser_identifier (parser);
18459 if (identifier == error_mark_node)
18460 return;
18461 /* Look for the `=' token. */
18462 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18463 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18464 {
18465 error_at (token->location, "%<namespace%> definition is not allowed here");
18466 /* Skip the definition. */
18467 cp_lexer_consume_token (parser->lexer);
18468 if (cp_parser_skip_to_closing_brace (parser))
18469 cp_lexer_consume_token (parser->lexer);
18470 return;
18471 }
18472 cp_parser_require (parser, CPP_EQ, RT_EQ);
18473 /* Look for the qualified-namespace-specifier. */
18474 namespace_specifier
18475 = cp_parser_qualified_namespace_specifier (parser);
18476 /* Look for the `;' token. */
18477 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18478
18479 /* Register the alias in the symbol table. */
18480 do_namespace_alias (identifier, namespace_specifier);
18481 }
18482
18483 /* Parse a qualified-namespace-specifier.
18484
18485 qualified-namespace-specifier:
18486 :: [opt] nested-name-specifier [opt] namespace-name
18487
18488 Returns a NAMESPACE_DECL corresponding to the specified
18489 namespace. */
18490
18491 static tree
18492 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18493 {
18494 /* Look for the optional `::'. */
18495 cp_parser_global_scope_opt (parser,
18496 /*current_scope_valid_p=*/false);
18497
18498 /* Look for the optional nested-name-specifier. */
18499 cp_parser_nested_name_specifier_opt (parser,
18500 /*typename_keyword_p=*/false,
18501 /*check_dependency_p=*/true,
18502 /*type_p=*/false,
18503 /*is_declaration=*/true);
18504
18505 return cp_parser_namespace_name (parser);
18506 }
18507
18508 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18509 access declaration.
18510
18511 using-declaration:
18512 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18513 using :: unqualified-id ;
18514
18515 access-declaration:
18516 qualified-id ;
18517
18518 */
18519
18520 static bool
18521 cp_parser_using_declaration (cp_parser* parser,
18522 bool access_declaration_p)
18523 {
18524 cp_token *token;
18525 bool typename_p = false;
18526 bool global_scope_p;
18527 tree decl;
18528 tree identifier;
18529 tree qscope;
18530 int oldcount = errorcount;
18531 cp_token *diag_token = NULL;
18532
18533 if (access_declaration_p)
18534 {
18535 diag_token = cp_lexer_peek_token (parser->lexer);
18536 cp_parser_parse_tentatively (parser);
18537 }
18538 else
18539 {
18540 /* Look for the `using' keyword. */
18541 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18542
18543 again:
18544 /* Peek at the next token. */
18545 token = cp_lexer_peek_token (parser->lexer);
18546 /* See if it's `typename'. */
18547 if (token->keyword == RID_TYPENAME)
18548 {
18549 /* Remember that we've seen it. */
18550 typename_p = true;
18551 /* Consume the `typename' token. */
18552 cp_lexer_consume_token (parser->lexer);
18553 }
18554 }
18555
18556 /* Look for the optional global scope qualification. */
18557 global_scope_p
18558 = (cp_parser_global_scope_opt (parser,
18559 /*current_scope_valid_p=*/false)
18560 != NULL_TREE);
18561
18562 /* If we saw `typename', or didn't see `::', then there must be a
18563 nested-name-specifier present. */
18564 if (typename_p || !global_scope_p)
18565 {
18566 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18567 /*check_dependency_p=*/true,
18568 /*type_p=*/false,
18569 /*is_declaration=*/true);
18570 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18571 {
18572 cp_parser_skip_to_end_of_block_or_statement (parser);
18573 return false;
18574 }
18575 }
18576 /* Otherwise, we could be in either of the two productions. In that
18577 case, treat the nested-name-specifier as optional. */
18578 else
18579 qscope = cp_parser_nested_name_specifier_opt (parser,
18580 /*typename_keyword_p=*/false,
18581 /*check_dependency_p=*/true,
18582 /*type_p=*/false,
18583 /*is_declaration=*/true);
18584 if (!qscope)
18585 qscope = global_namespace;
18586 else if (UNSCOPED_ENUM_P (qscope))
18587 qscope = CP_TYPE_CONTEXT (qscope);
18588
18589 if (access_declaration_p && cp_parser_error_occurred (parser))
18590 /* Something has already gone wrong; there's no need to parse
18591 further. Since an error has occurred, the return value of
18592 cp_parser_parse_definitely will be false, as required. */
18593 return cp_parser_parse_definitely (parser);
18594
18595 token = cp_lexer_peek_token (parser->lexer);
18596 /* Parse the unqualified-id. */
18597 identifier = cp_parser_unqualified_id (parser,
18598 /*template_keyword_p=*/false,
18599 /*check_dependency_p=*/true,
18600 /*declarator_p=*/true,
18601 /*optional_p=*/false);
18602
18603 if (access_declaration_p)
18604 {
18605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18606 cp_parser_simulate_error (parser);
18607 if (!cp_parser_parse_definitely (parser))
18608 return false;
18609 }
18610 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18611 {
18612 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18613 if (cxx_dialect < cxx1z
18614 && !in_system_header_at (ell->location))
18615 pedwarn (ell->location, 0,
18616 "pack expansion in using-declaration only available "
18617 "with -std=c++1z or -std=gnu++1z");
18618 qscope = make_pack_expansion (qscope);
18619 }
18620
18621 /* The function we call to handle a using-declaration is different
18622 depending on what scope we are in. */
18623 if (qscope == error_mark_node || identifier == error_mark_node)
18624 ;
18625 else if (!identifier_p (identifier)
18626 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18627 /* [namespace.udecl]
18628
18629 A using declaration shall not name a template-id. */
18630 error_at (token->location,
18631 "a template-id may not appear in a using-declaration");
18632 else
18633 {
18634 if (at_class_scope_p ())
18635 {
18636 /* Create the USING_DECL. */
18637 decl = do_class_using_decl (qscope, identifier);
18638
18639 if (decl && typename_p)
18640 USING_DECL_TYPENAME_P (decl) = 1;
18641
18642 if (check_for_bare_parameter_packs (decl))
18643 {
18644 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18645 return false;
18646 }
18647 else
18648 /* Add it to the list of members in this class. */
18649 finish_member_declaration (decl);
18650 }
18651 else
18652 {
18653 decl = cp_parser_lookup_name_simple (parser,
18654 identifier,
18655 token->location);
18656 if (decl == error_mark_node)
18657 cp_parser_name_lookup_error (parser, identifier,
18658 decl, NLE_NULL,
18659 token->location);
18660 else if (check_for_bare_parameter_packs (decl))
18661 {
18662 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18663 return false;
18664 }
18665 else if (!at_namespace_scope_p ())
18666 finish_local_using_decl (decl, qscope, identifier);
18667 else
18668 finish_namespace_using_decl (decl, qscope, identifier);
18669 }
18670 }
18671
18672 if (!access_declaration_p
18673 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18674 {
18675 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18676 if (cxx_dialect < cxx1z)
18677 pedwarn (comma->location, 0,
18678 "comma-separated list in using-declaration only available "
18679 "with -std=c++1z or -std=gnu++1z");
18680 goto again;
18681 }
18682
18683 /* Look for the final `;'. */
18684 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18685
18686 if (access_declaration_p && errorcount == oldcount)
18687 warning_at (diag_token->location, OPT_Wdeprecated,
18688 "access declarations are deprecated "
18689 "in favour of using-declarations; "
18690 "suggestion: add the %<using%> keyword");
18691
18692 return true;
18693 }
18694
18695 /* Parse an alias-declaration.
18696
18697 alias-declaration:
18698 using identifier attribute-specifier-seq [opt] = type-id */
18699
18700 static tree
18701 cp_parser_alias_declaration (cp_parser* parser)
18702 {
18703 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18704 location_t id_location;
18705 cp_declarator *declarator;
18706 cp_decl_specifier_seq decl_specs;
18707 bool member_p;
18708 const char *saved_message = NULL;
18709
18710 /* Look for the `using' keyword. */
18711 cp_token *using_token
18712 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18713 if (using_token == NULL)
18714 return error_mark_node;
18715
18716 id_location = cp_lexer_peek_token (parser->lexer)->location;
18717 id = cp_parser_identifier (parser);
18718 if (id == error_mark_node)
18719 return error_mark_node;
18720
18721 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18722 attributes = cp_parser_attributes_opt (parser);
18723 if (attributes == error_mark_node)
18724 return error_mark_node;
18725
18726 cp_parser_require (parser, CPP_EQ, RT_EQ);
18727
18728 if (cp_parser_error_occurred (parser))
18729 return error_mark_node;
18730
18731 cp_parser_commit_to_tentative_parse (parser);
18732
18733 /* Now we are going to parse the type-id of the declaration. */
18734
18735 /*
18736 [dcl.type]/3 says:
18737
18738 "A type-specifier-seq shall not define a class or enumeration
18739 unless it appears in the type-id of an alias-declaration (7.1.3) that
18740 is not the declaration of a template-declaration."
18741
18742 In other words, if we currently are in an alias template, the
18743 type-id should not define a type.
18744
18745 So let's set parser->type_definition_forbidden_message in that
18746 case; cp_parser_check_type_definition (called by
18747 cp_parser_class_specifier) will then emit an error if a type is
18748 defined in the type-id. */
18749 if (parser->num_template_parameter_lists)
18750 {
18751 saved_message = parser->type_definition_forbidden_message;
18752 parser->type_definition_forbidden_message =
18753 G_("types may not be defined in alias template declarations");
18754 }
18755
18756 type = cp_parser_type_id (parser);
18757
18758 /* Restore the error message if need be. */
18759 if (parser->num_template_parameter_lists)
18760 parser->type_definition_forbidden_message = saved_message;
18761
18762 if (type == error_mark_node
18763 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18764 {
18765 cp_parser_skip_to_end_of_block_or_statement (parser);
18766 return error_mark_node;
18767 }
18768
18769 /* A typedef-name can also be introduced by an alias-declaration. The
18770 identifier following the using keyword becomes a typedef-name. It has
18771 the same semantics as if it were introduced by the typedef
18772 specifier. In particular, it does not define a new type and it shall
18773 not appear in the type-id. */
18774
18775 clear_decl_specs (&decl_specs);
18776 decl_specs.type = type;
18777 if (attributes != NULL_TREE)
18778 {
18779 decl_specs.attributes = attributes;
18780 set_and_check_decl_spec_loc (&decl_specs,
18781 ds_attribute,
18782 attrs_token);
18783 }
18784 set_and_check_decl_spec_loc (&decl_specs,
18785 ds_typedef,
18786 using_token);
18787 set_and_check_decl_spec_loc (&decl_specs,
18788 ds_alias,
18789 using_token);
18790
18791 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18792 declarator->id_loc = id_location;
18793
18794 member_p = at_class_scope_p ();
18795 if (member_p)
18796 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18797 NULL_TREE, attributes);
18798 else
18799 decl = start_decl (declarator, &decl_specs, 0,
18800 attributes, NULL_TREE, &pushed_scope);
18801 if (decl == error_mark_node)
18802 return decl;
18803
18804 // Attach constraints to the alias declaration.
18805 if (flag_concepts && current_template_parms)
18806 {
18807 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18808 tree constr = build_constraints (reqs, NULL_TREE);
18809 set_constraints (decl, constr);
18810 }
18811
18812 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18813
18814 if (pushed_scope)
18815 pop_scope (pushed_scope);
18816
18817 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18818 added into the symbol table; otherwise, return the TYPE_DECL. */
18819 if (DECL_LANG_SPECIFIC (decl)
18820 && DECL_TEMPLATE_INFO (decl)
18821 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18822 {
18823 decl = DECL_TI_TEMPLATE (decl);
18824 if (member_p)
18825 check_member_template (decl);
18826 }
18827
18828 return decl;
18829 }
18830
18831 /* Parse a using-directive.
18832
18833 using-directive:
18834 using namespace :: [opt] nested-name-specifier [opt]
18835 namespace-name ; */
18836
18837 static void
18838 cp_parser_using_directive (cp_parser* parser)
18839 {
18840 tree namespace_decl;
18841 tree attribs;
18842
18843 /* Look for the `using' keyword. */
18844 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18845 /* And the `namespace' keyword. */
18846 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18847 /* Look for the optional `::' operator. */
18848 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18849 /* And the optional nested-name-specifier. */
18850 cp_parser_nested_name_specifier_opt (parser,
18851 /*typename_keyword_p=*/false,
18852 /*check_dependency_p=*/true,
18853 /*type_p=*/false,
18854 /*is_declaration=*/true);
18855 /* Get the namespace being used. */
18856 namespace_decl = cp_parser_namespace_name (parser);
18857 /* And any specified attributes. */
18858 attribs = cp_parser_attributes_opt (parser);
18859
18860 /* Update the symbol table. */
18861 if (namespace_bindings_p ())
18862 finish_namespace_using_directive (namespace_decl, attribs);
18863 else
18864 finish_local_using_directive (namespace_decl, attribs);
18865
18866 /* Look for the final `;'. */
18867 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18868 }
18869
18870 /* Parse an asm-definition.
18871
18872 asm-definition:
18873 asm ( string-literal ) ;
18874
18875 GNU Extension:
18876
18877 asm-definition:
18878 asm volatile [opt] ( string-literal ) ;
18879 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18880 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18881 : asm-operand-list [opt] ) ;
18882 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18883 : asm-operand-list [opt]
18884 : asm-clobber-list [opt] ) ;
18885 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18886 : asm-clobber-list [opt]
18887 : asm-goto-list ) ; */
18888
18889 static void
18890 cp_parser_asm_definition (cp_parser* parser)
18891 {
18892 tree string;
18893 tree outputs = NULL_TREE;
18894 tree inputs = NULL_TREE;
18895 tree clobbers = NULL_TREE;
18896 tree labels = NULL_TREE;
18897 tree asm_stmt;
18898 bool volatile_p = false;
18899 bool extended_p = false;
18900 bool invalid_inputs_p = false;
18901 bool invalid_outputs_p = false;
18902 bool goto_p = false;
18903 required_token missing = RT_NONE;
18904
18905 /* Look for the `asm' keyword. */
18906 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18907
18908 if (parser->in_function_body
18909 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18910 {
18911 error ("%<asm%> in %<constexpr%> function");
18912 cp_function_chain->invalid_constexpr = true;
18913 }
18914
18915 /* See if the next token is `volatile'. */
18916 if (cp_parser_allow_gnu_extensions_p (parser)
18917 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18918 {
18919 /* Remember that we saw the `volatile' keyword. */
18920 volatile_p = true;
18921 /* Consume the token. */
18922 cp_lexer_consume_token (parser->lexer);
18923 }
18924 if (cp_parser_allow_gnu_extensions_p (parser)
18925 && parser->in_function_body
18926 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18927 {
18928 /* Remember that we saw the `goto' keyword. */
18929 goto_p = true;
18930 /* Consume the token. */
18931 cp_lexer_consume_token (parser->lexer);
18932 }
18933 /* Look for the opening `('. */
18934 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18935 return;
18936 /* Look for the string. */
18937 string = cp_parser_string_literal (parser, false, false);
18938 if (string == error_mark_node)
18939 {
18940 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18941 /*consume_paren=*/true);
18942 return;
18943 }
18944
18945 /* If we're allowing GNU extensions, check for the extended assembly
18946 syntax. Unfortunately, the `:' tokens need not be separated by
18947 a space in C, and so, for compatibility, we tolerate that here
18948 too. Doing that means that we have to treat the `::' operator as
18949 two `:' tokens. */
18950 if (cp_parser_allow_gnu_extensions_p (parser)
18951 && parser->in_function_body
18952 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18953 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18954 {
18955 bool inputs_p = false;
18956 bool clobbers_p = false;
18957 bool labels_p = false;
18958
18959 /* The extended syntax was used. */
18960 extended_p = true;
18961
18962 /* Look for outputs. */
18963 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18964 {
18965 /* Consume the `:'. */
18966 cp_lexer_consume_token (parser->lexer);
18967 /* Parse the output-operands. */
18968 if (cp_lexer_next_token_is_not (parser->lexer,
18969 CPP_COLON)
18970 && cp_lexer_next_token_is_not (parser->lexer,
18971 CPP_SCOPE)
18972 && cp_lexer_next_token_is_not (parser->lexer,
18973 CPP_CLOSE_PAREN)
18974 && !goto_p)
18975 {
18976 outputs = cp_parser_asm_operand_list (parser);
18977 if (outputs == error_mark_node)
18978 invalid_outputs_p = true;
18979 }
18980 }
18981 /* If the next token is `::', there are no outputs, and the
18982 next token is the beginning of the inputs. */
18983 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18984 /* The inputs are coming next. */
18985 inputs_p = true;
18986
18987 /* Look for inputs. */
18988 if (inputs_p
18989 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18990 {
18991 /* Consume the `:' or `::'. */
18992 cp_lexer_consume_token (parser->lexer);
18993 /* Parse the output-operands. */
18994 if (cp_lexer_next_token_is_not (parser->lexer,
18995 CPP_COLON)
18996 && cp_lexer_next_token_is_not (parser->lexer,
18997 CPP_SCOPE)
18998 && cp_lexer_next_token_is_not (parser->lexer,
18999 CPP_CLOSE_PAREN))
19000 {
19001 inputs = cp_parser_asm_operand_list (parser);
19002 if (inputs == error_mark_node)
19003 invalid_inputs_p = true;
19004 }
19005 }
19006 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19007 /* The clobbers are coming next. */
19008 clobbers_p = true;
19009
19010 /* Look for clobbers. */
19011 if (clobbers_p
19012 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19013 {
19014 clobbers_p = true;
19015 /* Consume the `:' or `::'. */
19016 cp_lexer_consume_token (parser->lexer);
19017 /* Parse the clobbers. */
19018 if (cp_lexer_next_token_is_not (parser->lexer,
19019 CPP_COLON)
19020 && cp_lexer_next_token_is_not (parser->lexer,
19021 CPP_CLOSE_PAREN))
19022 clobbers = cp_parser_asm_clobber_list (parser);
19023 }
19024 else if (goto_p
19025 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19026 /* The labels are coming next. */
19027 labels_p = true;
19028
19029 /* Look for labels. */
19030 if (labels_p
19031 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19032 {
19033 labels_p = true;
19034 /* Consume the `:' or `::'. */
19035 cp_lexer_consume_token (parser->lexer);
19036 /* Parse the labels. */
19037 labels = cp_parser_asm_label_list (parser);
19038 }
19039
19040 if (goto_p && !labels_p)
19041 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19042 }
19043 else if (goto_p)
19044 missing = RT_COLON_SCOPE;
19045
19046 /* Look for the closing `)'. */
19047 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19048 missing ? missing : RT_CLOSE_PAREN))
19049 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19050 /*consume_paren=*/true);
19051 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19052
19053 if (!invalid_inputs_p && !invalid_outputs_p)
19054 {
19055 /* Create the ASM_EXPR. */
19056 if (parser->in_function_body)
19057 {
19058 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19059 inputs, clobbers, labels);
19060 /* If the extended syntax was not used, mark the ASM_EXPR. */
19061 if (!extended_p)
19062 {
19063 tree temp = asm_stmt;
19064 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19065 temp = TREE_OPERAND (temp, 0);
19066
19067 ASM_INPUT_P (temp) = 1;
19068 }
19069 }
19070 else
19071 symtab->finalize_toplevel_asm (string);
19072 }
19073 }
19074
19075 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19076 type that comes from the decl-specifier-seq. */
19077
19078 static tree
19079 strip_declarator_types (tree type, cp_declarator *declarator)
19080 {
19081 for (cp_declarator *d = declarator; d;)
19082 switch (d->kind)
19083 {
19084 case cdk_id:
19085 case cdk_decomp:
19086 case cdk_error:
19087 d = NULL;
19088 break;
19089
19090 default:
19091 if (TYPE_PTRMEMFUNC_P (type))
19092 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19093 type = TREE_TYPE (type);
19094 d = d->declarator;
19095 break;
19096 }
19097
19098 return type;
19099 }
19100
19101 /* Declarators [gram.dcl.decl] */
19102
19103 /* Parse an init-declarator.
19104
19105 init-declarator:
19106 declarator initializer [opt]
19107
19108 GNU Extension:
19109
19110 init-declarator:
19111 declarator asm-specification [opt] attributes [opt] initializer [opt]
19112
19113 function-definition:
19114 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19115 function-body
19116 decl-specifier-seq [opt] declarator function-try-block
19117
19118 GNU Extension:
19119
19120 function-definition:
19121 __extension__ function-definition
19122
19123 TM Extension:
19124
19125 function-definition:
19126 decl-specifier-seq [opt] declarator function-transaction-block
19127
19128 The DECL_SPECIFIERS apply to this declarator. Returns a
19129 representation of the entity declared. If MEMBER_P is TRUE, then
19130 this declarator appears in a class scope. The new DECL created by
19131 this declarator is returned.
19132
19133 The CHECKS are access checks that should be performed once we know
19134 what entity is being declared (and, therefore, what classes have
19135 befriended it).
19136
19137 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19138 for a function-definition here as well. If the declarator is a
19139 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19140 be TRUE upon return. By that point, the function-definition will
19141 have been completely parsed.
19142
19143 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19144 is FALSE.
19145
19146 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19147 parsed declaration if it is an uninitialized single declarator not followed
19148 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19149 if present, will not be consumed. If returned, this declarator will be
19150 created with SD_INITIALIZED but will not call cp_finish_decl.
19151
19152 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19153 and there is an initializer, the pointed location_t is set to the
19154 location of the '=' or `(', or '{' in C++11 token introducing the
19155 initializer. */
19156
19157 static tree
19158 cp_parser_init_declarator (cp_parser* parser,
19159 cp_decl_specifier_seq *decl_specifiers,
19160 vec<deferred_access_check, va_gc> *checks,
19161 bool function_definition_allowed_p,
19162 bool member_p,
19163 int declares_class_or_enum,
19164 bool* function_definition_p,
19165 tree* maybe_range_for_decl,
19166 location_t* init_loc,
19167 tree* auto_result)
19168 {
19169 cp_token *token = NULL, *asm_spec_start_token = NULL,
19170 *attributes_start_token = NULL;
19171 cp_declarator *declarator;
19172 tree prefix_attributes;
19173 tree attributes = NULL;
19174 tree asm_specification;
19175 tree initializer;
19176 tree decl = NULL_TREE;
19177 tree scope;
19178 int is_initialized;
19179 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19180 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19181 "(...)". */
19182 enum cpp_ttype initialization_kind;
19183 bool is_direct_init = false;
19184 bool is_non_constant_init;
19185 int ctor_dtor_or_conv_p;
19186 bool friend_p = cp_parser_friend_p (decl_specifiers);
19187 tree pushed_scope = NULL_TREE;
19188 bool range_for_decl_p = false;
19189 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19190 location_t tmp_init_loc = UNKNOWN_LOCATION;
19191
19192 /* Gather the attributes that were provided with the
19193 decl-specifiers. */
19194 prefix_attributes = decl_specifiers->attributes;
19195
19196 /* Assume that this is not the declarator for a function
19197 definition. */
19198 if (function_definition_p)
19199 *function_definition_p = false;
19200
19201 /* Default arguments are only permitted for function parameters. */
19202 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19203 parser->default_arg_ok_p = false;
19204
19205 /* Defer access checks while parsing the declarator; we cannot know
19206 what names are accessible until we know what is being
19207 declared. */
19208 resume_deferring_access_checks ();
19209
19210 token = cp_lexer_peek_token (parser->lexer);
19211
19212 /* Parse the declarator. */
19213 declarator
19214 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19215 &ctor_dtor_or_conv_p,
19216 /*parenthesized_p=*/NULL,
19217 member_p, friend_p);
19218 /* Gather up the deferred checks. */
19219 stop_deferring_access_checks ();
19220
19221 parser->default_arg_ok_p = saved_default_arg_ok_p;
19222
19223 /* If the DECLARATOR was erroneous, there's no need to go
19224 further. */
19225 if (declarator == cp_error_declarator)
19226 return error_mark_node;
19227
19228 /* Check that the number of template-parameter-lists is OK. */
19229 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19230 token->location))
19231 return error_mark_node;
19232
19233 if (declares_class_or_enum & 2)
19234 cp_parser_check_for_definition_in_return_type (declarator,
19235 decl_specifiers->type,
19236 decl_specifiers->locations[ds_type_spec]);
19237
19238 /* Figure out what scope the entity declared by the DECLARATOR is
19239 located in. `grokdeclarator' sometimes changes the scope, so
19240 we compute it now. */
19241 scope = get_scope_of_declarator (declarator);
19242
19243 /* Perform any lookups in the declared type which were thought to be
19244 dependent, but are not in the scope of the declarator. */
19245 decl_specifiers->type
19246 = maybe_update_decl_type (decl_specifiers->type, scope);
19247
19248 /* If we're allowing GNU extensions, look for an
19249 asm-specification. */
19250 if (cp_parser_allow_gnu_extensions_p (parser))
19251 {
19252 /* Look for an asm-specification. */
19253 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19254 asm_specification = cp_parser_asm_specification_opt (parser);
19255 }
19256 else
19257 asm_specification = NULL_TREE;
19258
19259 /* Look for attributes. */
19260 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19261 attributes = cp_parser_attributes_opt (parser);
19262
19263 /* Peek at the next token. */
19264 token = cp_lexer_peek_token (parser->lexer);
19265
19266 bool bogus_implicit_tmpl = false;
19267
19268 if (function_declarator_p (declarator))
19269 {
19270 /* Handle C++17 deduction guides. */
19271 if (!decl_specifiers->type
19272 && ctor_dtor_or_conv_p <= 0
19273 && cxx_dialect >= cxx1z)
19274 {
19275 cp_declarator *id = get_id_declarator (declarator);
19276 tree name = id->u.id.unqualified_name;
19277 parser->scope = id->u.id.qualifying_scope;
19278 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19279 if (tmpl
19280 && (DECL_CLASS_TEMPLATE_P (tmpl)
19281 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19282 {
19283 id->u.id.unqualified_name = dguide_name (tmpl);
19284 id->u.id.sfk = sfk_deduction_guide;
19285 ctor_dtor_or_conv_p = 1;
19286 }
19287 }
19288
19289 /* Check to see if the token indicates the start of a
19290 function-definition. */
19291 if (cp_parser_token_starts_function_definition_p (token))
19292 {
19293 if (!function_definition_allowed_p)
19294 {
19295 /* If a function-definition should not appear here, issue an
19296 error message. */
19297 cp_parser_error (parser,
19298 "a function-definition is not allowed here");
19299 return error_mark_node;
19300 }
19301
19302 location_t func_brace_location
19303 = cp_lexer_peek_token (parser->lexer)->location;
19304
19305 /* Neither attributes nor an asm-specification are allowed
19306 on a function-definition. */
19307 if (asm_specification)
19308 error_at (asm_spec_start_token->location,
19309 "an asm-specification is not allowed "
19310 "on a function-definition");
19311 if (attributes)
19312 error_at (attributes_start_token->location,
19313 "attributes are not allowed "
19314 "on a function-definition");
19315 /* This is a function-definition. */
19316 *function_definition_p = true;
19317
19318 /* Parse the function definition. */
19319 if (member_p)
19320 decl = cp_parser_save_member_function_body (parser,
19321 decl_specifiers,
19322 declarator,
19323 prefix_attributes);
19324 else
19325 decl =
19326 (cp_parser_function_definition_from_specifiers_and_declarator
19327 (parser, decl_specifiers, prefix_attributes, declarator));
19328
19329 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19330 {
19331 /* This is where the prologue starts... */
19332 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19333 = func_brace_location;
19334 }
19335
19336 return decl;
19337 }
19338 }
19339 else if (parser->fully_implicit_function_template_p)
19340 {
19341 /* A non-template declaration involving a function parameter list
19342 containing an implicit template parameter will be made into a
19343 template. If the resulting declaration is not going to be an
19344 actual function then finish the template scope here to prevent it.
19345 An error message will be issued once we have a decl to talk about.
19346
19347 FIXME probably we should do type deduction rather than create an
19348 implicit template, but the standard currently doesn't allow it. */
19349 bogus_implicit_tmpl = true;
19350 finish_fully_implicit_template (parser, NULL_TREE);
19351 }
19352
19353 /* [dcl.dcl]
19354
19355 Only in function declarations for constructors, destructors, type
19356 conversions, and deduction guides can the decl-specifier-seq be omitted.
19357
19358 We explicitly postpone this check past the point where we handle
19359 function-definitions because we tolerate function-definitions
19360 that are missing their return types in some modes. */
19361 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19362 {
19363 cp_parser_error (parser,
19364 "expected constructor, destructor, or type conversion");
19365 return error_mark_node;
19366 }
19367
19368 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19369 if (token->type == CPP_EQ
19370 || token->type == CPP_OPEN_PAREN
19371 || token->type == CPP_OPEN_BRACE)
19372 {
19373 is_initialized = SD_INITIALIZED;
19374 initialization_kind = token->type;
19375 if (maybe_range_for_decl)
19376 *maybe_range_for_decl = error_mark_node;
19377 tmp_init_loc = token->location;
19378 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19379 *init_loc = tmp_init_loc;
19380
19381 if (token->type == CPP_EQ
19382 && function_declarator_p (declarator))
19383 {
19384 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19385 if (t2->keyword == RID_DEFAULT)
19386 is_initialized = SD_DEFAULTED;
19387 else if (t2->keyword == RID_DELETE)
19388 is_initialized = SD_DELETED;
19389 }
19390 }
19391 else
19392 {
19393 /* If the init-declarator isn't initialized and isn't followed by a
19394 `,' or `;', it's not a valid init-declarator. */
19395 if (token->type != CPP_COMMA
19396 && token->type != CPP_SEMICOLON)
19397 {
19398 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19399 range_for_decl_p = true;
19400 else
19401 {
19402 if (!maybe_range_for_decl)
19403 cp_parser_error (parser, "expected initializer");
19404 return error_mark_node;
19405 }
19406 }
19407 is_initialized = SD_UNINITIALIZED;
19408 initialization_kind = CPP_EOF;
19409 }
19410
19411 /* Because start_decl has side-effects, we should only call it if we
19412 know we're going ahead. By this point, we know that we cannot
19413 possibly be looking at any other construct. */
19414 cp_parser_commit_to_tentative_parse (parser);
19415
19416 /* Enter the newly declared entry in the symbol table. If we're
19417 processing a declaration in a class-specifier, we wait until
19418 after processing the initializer. */
19419 if (!member_p)
19420 {
19421 if (parser->in_unbraced_linkage_specification_p)
19422 decl_specifiers->storage_class = sc_extern;
19423 decl = start_decl (declarator, decl_specifiers,
19424 range_for_decl_p? SD_INITIALIZED : is_initialized,
19425 attributes, prefix_attributes, &pushed_scope);
19426 cp_finalize_omp_declare_simd (parser, decl);
19427 cp_finalize_oacc_routine (parser, decl, false);
19428 /* Adjust location of decl if declarator->id_loc is more appropriate:
19429 set, and decl wasn't merged with another decl, in which case its
19430 location would be different from input_location, and more accurate. */
19431 if (DECL_P (decl)
19432 && declarator->id_loc != UNKNOWN_LOCATION
19433 && DECL_SOURCE_LOCATION (decl) == input_location)
19434 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19435 }
19436 else if (scope)
19437 /* Enter the SCOPE. That way unqualified names appearing in the
19438 initializer will be looked up in SCOPE. */
19439 pushed_scope = push_scope (scope);
19440
19441 /* Perform deferred access control checks, now that we know in which
19442 SCOPE the declared entity resides. */
19443 if (!member_p && decl)
19444 {
19445 tree saved_current_function_decl = NULL_TREE;
19446
19447 /* If the entity being declared is a function, pretend that we
19448 are in its scope. If it is a `friend', it may have access to
19449 things that would not otherwise be accessible. */
19450 if (TREE_CODE (decl) == FUNCTION_DECL)
19451 {
19452 saved_current_function_decl = current_function_decl;
19453 current_function_decl = decl;
19454 }
19455
19456 /* Perform access checks for template parameters. */
19457 cp_parser_perform_template_parameter_access_checks (checks);
19458
19459 /* Perform the access control checks for the declarator and the
19460 decl-specifiers. */
19461 perform_deferred_access_checks (tf_warning_or_error);
19462
19463 /* Restore the saved value. */
19464 if (TREE_CODE (decl) == FUNCTION_DECL)
19465 current_function_decl = saved_current_function_decl;
19466 }
19467
19468 /* Parse the initializer. */
19469 initializer = NULL_TREE;
19470 is_direct_init = false;
19471 is_non_constant_init = true;
19472 if (is_initialized)
19473 {
19474 if (function_declarator_p (declarator))
19475 {
19476 if (initialization_kind == CPP_EQ)
19477 initializer = cp_parser_pure_specifier (parser);
19478 else
19479 {
19480 /* If the declaration was erroneous, we don't really
19481 know what the user intended, so just silently
19482 consume the initializer. */
19483 if (decl != error_mark_node)
19484 error_at (tmp_init_loc, "initializer provided for function");
19485 cp_parser_skip_to_closing_parenthesis (parser,
19486 /*recovering=*/true,
19487 /*or_comma=*/false,
19488 /*consume_paren=*/true);
19489 }
19490 }
19491 else
19492 {
19493 /* We want to record the extra mangling scope for in-class
19494 initializers of class members and initializers of static data
19495 member templates. The former involves deferring
19496 parsing of the initializer until end of class as with default
19497 arguments. So right here we only handle the latter. */
19498 if (!member_p && processing_template_decl)
19499 start_lambda_scope (decl);
19500 initializer = cp_parser_initializer (parser,
19501 &is_direct_init,
19502 &is_non_constant_init);
19503 if (!member_p && processing_template_decl)
19504 finish_lambda_scope ();
19505 if (initializer == error_mark_node)
19506 cp_parser_skip_to_end_of_statement (parser);
19507 }
19508 }
19509
19510 /* The old parser allows attributes to appear after a parenthesized
19511 initializer. Mark Mitchell proposed removing this functionality
19512 on the GCC mailing lists on 2002-08-13. This parser accepts the
19513 attributes -- but ignores them. */
19514 if (cp_parser_allow_gnu_extensions_p (parser)
19515 && initialization_kind == CPP_OPEN_PAREN)
19516 if (cp_parser_attributes_opt (parser))
19517 warning (OPT_Wattributes,
19518 "attributes after parenthesized initializer ignored");
19519
19520 /* And now complain about a non-function implicit template. */
19521 if (bogus_implicit_tmpl && decl != error_mark_node)
19522 error_at (DECL_SOURCE_LOCATION (decl),
19523 "non-function %qD declared as implicit template", decl);
19524
19525 /* For an in-class declaration, use `grokfield' to create the
19526 declaration. */
19527 if (member_p)
19528 {
19529 if (pushed_scope)
19530 {
19531 pop_scope (pushed_scope);
19532 pushed_scope = NULL_TREE;
19533 }
19534 decl = grokfield (declarator, decl_specifiers,
19535 initializer, !is_non_constant_init,
19536 /*asmspec=*/NULL_TREE,
19537 chainon (attributes, prefix_attributes));
19538 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19539 cp_parser_save_default_args (parser, decl);
19540 cp_finalize_omp_declare_simd (parser, decl);
19541 cp_finalize_oacc_routine (parser, decl, false);
19542 }
19543
19544 /* Finish processing the declaration. But, skip member
19545 declarations. */
19546 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19547 {
19548 cp_finish_decl (decl,
19549 initializer, !is_non_constant_init,
19550 asm_specification,
19551 /* If the initializer is in parentheses, then this is
19552 a direct-initialization, which means that an
19553 `explicit' constructor is OK. Otherwise, an
19554 `explicit' constructor cannot be used. */
19555 ((is_direct_init || !is_initialized)
19556 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19557 }
19558 else if ((cxx_dialect != cxx98) && friend_p
19559 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19560 /* Core issue #226 (C++0x only): A default template-argument
19561 shall not be specified in a friend class template
19562 declaration. */
19563 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19564 /*is_partial=*/false, /*is_friend_decl=*/1);
19565
19566 if (!friend_p && pushed_scope)
19567 pop_scope (pushed_scope);
19568
19569 if (function_declarator_p (declarator)
19570 && parser->fully_implicit_function_template_p)
19571 {
19572 if (member_p)
19573 decl = finish_fully_implicit_template (parser, decl);
19574 else
19575 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19576 }
19577
19578 if (auto_result && is_initialized && decl_specifiers->type
19579 && type_uses_auto (decl_specifiers->type))
19580 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19581
19582 return decl;
19583 }
19584
19585 /* Parse a declarator.
19586
19587 declarator:
19588 direct-declarator
19589 ptr-operator declarator
19590
19591 abstract-declarator:
19592 ptr-operator abstract-declarator [opt]
19593 direct-abstract-declarator
19594
19595 GNU Extensions:
19596
19597 declarator:
19598 attributes [opt] direct-declarator
19599 attributes [opt] ptr-operator declarator
19600
19601 abstract-declarator:
19602 attributes [opt] ptr-operator abstract-declarator [opt]
19603 attributes [opt] direct-abstract-declarator
19604
19605 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19606 detect constructors, destructors, deduction guides, or conversion operators.
19607 It is set to -1 if the declarator is a name, and +1 if it is a
19608 function. Otherwise it is set to zero. Usually you just want to
19609 test for >0, but internally the negative value is used.
19610
19611 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19612 a decl-specifier-seq unless it declares a constructor, destructor,
19613 or conversion. It might seem that we could check this condition in
19614 semantic analysis, rather than parsing, but that makes it difficult
19615 to handle something like `f()'. We want to notice that there are
19616 no decl-specifiers, and therefore realize that this is an
19617 expression, not a declaration.)
19618
19619 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19620 the declarator is a direct-declarator of the form "(...)".
19621
19622 MEMBER_P is true iff this declarator is a member-declarator.
19623
19624 FRIEND_P is true iff this declarator is a friend. */
19625
19626 static cp_declarator *
19627 cp_parser_declarator (cp_parser* parser,
19628 cp_parser_declarator_kind dcl_kind,
19629 int* ctor_dtor_or_conv_p,
19630 bool* parenthesized_p,
19631 bool member_p, bool friend_p)
19632 {
19633 cp_declarator *declarator;
19634 enum tree_code code;
19635 cp_cv_quals cv_quals;
19636 tree class_type;
19637 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19638
19639 /* Assume this is not a constructor, destructor, or type-conversion
19640 operator. */
19641 if (ctor_dtor_or_conv_p)
19642 *ctor_dtor_or_conv_p = 0;
19643
19644 if (cp_parser_allow_gnu_extensions_p (parser))
19645 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19646
19647 /* Check for the ptr-operator production. */
19648 cp_parser_parse_tentatively (parser);
19649 /* Parse the ptr-operator. */
19650 code = cp_parser_ptr_operator (parser,
19651 &class_type,
19652 &cv_quals,
19653 &std_attributes);
19654
19655 /* If that worked, then we have a ptr-operator. */
19656 if (cp_parser_parse_definitely (parser))
19657 {
19658 /* If a ptr-operator was found, then this declarator was not
19659 parenthesized. */
19660 if (parenthesized_p)
19661 *parenthesized_p = true;
19662 /* The dependent declarator is optional if we are parsing an
19663 abstract-declarator. */
19664 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19665 cp_parser_parse_tentatively (parser);
19666
19667 /* Parse the dependent declarator. */
19668 declarator = cp_parser_declarator (parser, dcl_kind,
19669 /*ctor_dtor_or_conv_p=*/NULL,
19670 /*parenthesized_p=*/NULL,
19671 /*member_p=*/false,
19672 friend_p);
19673
19674 /* If we are parsing an abstract-declarator, we must handle the
19675 case where the dependent declarator is absent. */
19676 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19677 && !cp_parser_parse_definitely (parser))
19678 declarator = NULL;
19679
19680 declarator = cp_parser_make_indirect_declarator
19681 (code, class_type, cv_quals, declarator, std_attributes);
19682 }
19683 /* Everything else is a direct-declarator. */
19684 else
19685 {
19686 if (parenthesized_p)
19687 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19688 CPP_OPEN_PAREN);
19689 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19690 ctor_dtor_or_conv_p,
19691 member_p, friend_p);
19692 }
19693
19694 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19695 declarator->attributes = gnu_attributes;
19696 return declarator;
19697 }
19698
19699 /* Parse a direct-declarator or direct-abstract-declarator.
19700
19701 direct-declarator:
19702 declarator-id
19703 direct-declarator ( parameter-declaration-clause )
19704 cv-qualifier-seq [opt]
19705 ref-qualifier [opt]
19706 exception-specification [opt]
19707 direct-declarator [ constant-expression [opt] ]
19708 ( declarator )
19709
19710 direct-abstract-declarator:
19711 direct-abstract-declarator [opt]
19712 ( parameter-declaration-clause )
19713 cv-qualifier-seq [opt]
19714 ref-qualifier [opt]
19715 exception-specification [opt]
19716 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19717 ( abstract-declarator )
19718
19719 Returns a representation of the declarator. DCL_KIND is
19720 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19721 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19722 we are parsing a direct-declarator. It is
19723 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19724 of ambiguity we prefer an abstract declarator, as per
19725 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19726 as for cp_parser_declarator. */
19727
19728 static cp_declarator *
19729 cp_parser_direct_declarator (cp_parser* parser,
19730 cp_parser_declarator_kind dcl_kind,
19731 int* ctor_dtor_or_conv_p,
19732 bool member_p, bool friend_p)
19733 {
19734 cp_token *token;
19735 cp_declarator *declarator = NULL;
19736 tree scope = NULL_TREE;
19737 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19738 bool saved_in_declarator_p = parser->in_declarator_p;
19739 bool first = true;
19740 tree pushed_scope = NULL_TREE;
19741
19742 while (true)
19743 {
19744 /* Peek at the next token. */
19745 token = cp_lexer_peek_token (parser->lexer);
19746 if (token->type == CPP_OPEN_PAREN)
19747 {
19748 /* This is either a parameter-declaration-clause, or a
19749 parenthesized declarator. When we know we are parsing a
19750 named declarator, it must be a parenthesized declarator
19751 if FIRST is true. For instance, `(int)' is a
19752 parameter-declaration-clause, with an omitted
19753 direct-abstract-declarator. But `((*))', is a
19754 parenthesized abstract declarator. Finally, when T is a
19755 template parameter `(T)' is a
19756 parameter-declaration-clause, and not a parenthesized
19757 named declarator.
19758
19759 We first try and parse a parameter-declaration-clause,
19760 and then try a nested declarator (if FIRST is true).
19761
19762 It is not an error for it not to be a
19763 parameter-declaration-clause, even when FIRST is
19764 false. Consider,
19765
19766 int i (int);
19767 int i (3);
19768
19769 The first is the declaration of a function while the
19770 second is the definition of a variable, including its
19771 initializer.
19772
19773 Having seen only the parenthesis, we cannot know which of
19774 these two alternatives should be selected. Even more
19775 complex are examples like:
19776
19777 int i (int (a));
19778 int i (int (3));
19779
19780 The former is a function-declaration; the latter is a
19781 variable initialization.
19782
19783 Thus again, we try a parameter-declaration-clause, and if
19784 that fails, we back out and return. */
19785
19786 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19787 {
19788 tree params;
19789 bool is_declarator = false;
19790
19791 /* In a member-declarator, the only valid interpretation
19792 of a parenthesis is the start of a
19793 parameter-declaration-clause. (It is invalid to
19794 initialize a static data member with a parenthesized
19795 initializer; only the "=" form of initialization is
19796 permitted.) */
19797 if (!member_p)
19798 cp_parser_parse_tentatively (parser);
19799
19800 /* Consume the `('. */
19801 cp_lexer_consume_token (parser->lexer);
19802 if (first)
19803 {
19804 /* If this is going to be an abstract declarator, we're
19805 in a declarator and we can't have default args. */
19806 parser->default_arg_ok_p = false;
19807 parser->in_declarator_p = true;
19808 }
19809
19810 begin_scope (sk_function_parms, NULL_TREE);
19811
19812 /* Parse the parameter-declaration-clause. */
19813 params = cp_parser_parameter_declaration_clause (parser);
19814
19815 /* Consume the `)'. */
19816 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19817
19818 /* If all went well, parse the cv-qualifier-seq,
19819 ref-qualifier and the exception-specification. */
19820 if (member_p || cp_parser_parse_definitely (parser))
19821 {
19822 cp_cv_quals cv_quals;
19823 cp_virt_specifiers virt_specifiers;
19824 cp_ref_qualifier ref_qual;
19825 tree exception_specification;
19826 tree late_return;
19827 tree attrs;
19828 bool memfn = (member_p || (pushed_scope
19829 && CLASS_TYPE_P (pushed_scope)));
19830
19831 is_declarator = true;
19832
19833 if (ctor_dtor_or_conv_p)
19834 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19835 first = false;
19836
19837 /* Parse the cv-qualifier-seq. */
19838 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19839 /* Parse the ref-qualifier. */
19840 ref_qual = cp_parser_ref_qualifier_opt (parser);
19841 /* Parse the tx-qualifier. */
19842 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19843 /* And the exception-specification. */
19844 exception_specification
19845 = cp_parser_exception_specification_opt (parser);
19846
19847 attrs = cp_parser_std_attribute_spec_seq (parser);
19848
19849 /* In here, we handle cases where attribute is used after
19850 the function declaration. For example:
19851 void func (int x) __attribute__((vector(..))); */
19852 tree gnu_attrs = NULL_TREE;
19853 if (flag_cilkplus
19854 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19855 {
19856 cp_parser_parse_tentatively (parser);
19857 tree attr = cp_parser_gnu_attributes_opt (parser);
19858 if (cp_lexer_next_token_is_not (parser->lexer,
19859 CPP_SEMICOLON)
19860 && cp_lexer_next_token_is_not (parser->lexer,
19861 CPP_OPEN_BRACE))
19862 cp_parser_abort_tentative_parse (parser);
19863 else if (!cp_parser_parse_definitely (parser))
19864 ;
19865 else
19866 gnu_attrs = attr;
19867 }
19868 tree requires_clause = NULL_TREE;
19869 late_return = (cp_parser_late_return_type_opt
19870 (parser, declarator, requires_clause,
19871 memfn ? cv_quals : -1));
19872
19873 /* Parse the virt-specifier-seq. */
19874 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19875
19876 /* Create the function-declarator. */
19877 declarator = make_call_declarator (declarator,
19878 params,
19879 cv_quals,
19880 virt_specifiers,
19881 ref_qual,
19882 tx_qual,
19883 exception_specification,
19884 late_return,
19885 requires_clause);
19886 declarator->std_attributes = attrs;
19887 declarator->attributes = gnu_attrs;
19888 /* Any subsequent parameter lists are to do with
19889 return type, so are not those of the declared
19890 function. */
19891 parser->default_arg_ok_p = false;
19892 }
19893
19894 /* Remove the function parms from scope. */
19895 pop_bindings_and_leave_scope ();
19896
19897 if (is_declarator)
19898 /* Repeat the main loop. */
19899 continue;
19900 }
19901
19902 /* If this is the first, we can try a parenthesized
19903 declarator. */
19904 if (first)
19905 {
19906 bool saved_in_type_id_in_expr_p;
19907
19908 parser->default_arg_ok_p = saved_default_arg_ok_p;
19909 parser->in_declarator_p = saved_in_declarator_p;
19910
19911 /* Consume the `('. */
19912 cp_lexer_consume_token (parser->lexer);
19913 /* Parse the nested declarator. */
19914 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19915 parser->in_type_id_in_expr_p = true;
19916 declarator
19917 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19918 /*parenthesized_p=*/NULL,
19919 member_p, friend_p);
19920 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19921 first = false;
19922 /* Expect a `)'. */
19923 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19924 declarator = cp_error_declarator;
19925 if (declarator == cp_error_declarator)
19926 break;
19927
19928 goto handle_declarator;
19929 }
19930 /* Otherwise, we must be done. */
19931 else
19932 break;
19933 }
19934 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19935 && token->type == CPP_OPEN_SQUARE
19936 && !cp_next_tokens_can_be_attribute_p (parser))
19937 {
19938 /* Parse an array-declarator. */
19939 tree bounds, attrs;
19940
19941 if (ctor_dtor_or_conv_p)
19942 *ctor_dtor_or_conv_p = 0;
19943
19944 first = false;
19945 parser->default_arg_ok_p = false;
19946 parser->in_declarator_p = true;
19947 /* Consume the `['. */
19948 cp_lexer_consume_token (parser->lexer);
19949 /* Peek at the next token. */
19950 token = cp_lexer_peek_token (parser->lexer);
19951 /* If the next token is `]', then there is no
19952 constant-expression. */
19953 if (token->type != CPP_CLOSE_SQUARE)
19954 {
19955 bool non_constant_p;
19956 bounds
19957 = cp_parser_constant_expression (parser,
19958 /*allow_non_constant=*/true,
19959 &non_constant_p);
19960 if (!non_constant_p)
19961 /* OK */;
19962 else if (error_operand_p (bounds))
19963 /* Already gave an error. */;
19964 else if (!parser->in_function_body
19965 || current_binding_level->kind == sk_function_parms)
19966 {
19967 /* Normally, the array bound must be an integral constant
19968 expression. However, as an extension, we allow VLAs
19969 in function scopes as long as they aren't part of a
19970 parameter declaration. */
19971 cp_parser_error (parser,
19972 "array bound is not an integer constant");
19973 bounds = error_mark_node;
19974 }
19975 else if (processing_template_decl
19976 && !type_dependent_expression_p (bounds))
19977 {
19978 /* Remember this wasn't a constant-expression. */
19979 bounds = build_nop (TREE_TYPE (bounds), bounds);
19980 TREE_SIDE_EFFECTS (bounds) = 1;
19981 }
19982 }
19983 else
19984 bounds = NULL_TREE;
19985 /* Look for the closing `]'. */
19986 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19987 {
19988 declarator = cp_error_declarator;
19989 break;
19990 }
19991
19992 attrs = cp_parser_std_attribute_spec_seq (parser);
19993 declarator = make_array_declarator (declarator, bounds);
19994 declarator->std_attributes = attrs;
19995 }
19996 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19997 {
19998 {
19999 tree qualifying_scope;
20000 tree unqualified_name;
20001 tree attrs;
20002 special_function_kind sfk;
20003 bool abstract_ok;
20004 bool pack_expansion_p = false;
20005 cp_token *declarator_id_start_token;
20006
20007 /* Parse a declarator-id */
20008 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20009 if (abstract_ok)
20010 {
20011 cp_parser_parse_tentatively (parser);
20012
20013 /* If we see an ellipsis, we should be looking at a
20014 parameter pack. */
20015 if (token->type == CPP_ELLIPSIS)
20016 {
20017 /* Consume the `...' */
20018 cp_lexer_consume_token (parser->lexer);
20019
20020 pack_expansion_p = true;
20021 }
20022 }
20023
20024 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20025 unqualified_name
20026 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20027 qualifying_scope = parser->scope;
20028 if (abstract_ok)
20029 {
20030 bool okay = false;
20031
20032 if (!unqualified_name && pack_expansion_p)
20033 {
20034 /* Check whether an error occurred. */
20035 okay = !cp_parser_error_occurred (parser);
20036
20037 /* We already consumed the ellipsis to mark a
20038 parameter pack, but we have no way to report it,
20039 so abort the tentative parse. We will be exiting
20040 immediately anyway. */
20041 cp_parser_abort_tentative_parse (parser);
20042 }
20043 else
20044 okay = cp_parser_parse_definitely (parser);
20045
20046 if (!okay)
20047 unqualified_name = error_mark_node;
20048 else if (unqualified_name
20049 && (qualifying_scope
20050 || (!identifier_p (unqualified_name))))
20051 {
20052 cp_parser_error (parser, "expected unqualified-id");
20053 unqualified_name = error_mark_node;
20054 }
20055 }
20056
20057 if (!unqualified_name)
20058 return NULL;
20059 if (unqualified_name == error_mark_node)
20060 {
20061 declarator = cp_error_declarator;
20062 pack_expansion_p = false;
20063 declarator->parameter_pack_p = false;
20064 break;
20065 }
20066
20067 attrs = cp_parser_std_attribute_spec_seq (parser);
20068
20069 if (qualifying_scope && at_namespace_scope_p ()
20070 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20071 {
20072 /* In the declaration of a member of a template class
20073 outside of the class itself, the SCOPE will sometimes
20074 be a TYPENAME_TYPE. For example, given:
20075
20076 template <typename T>
20077 int S<T>::R::i = 3;
20078
20079 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20080 this context, we must resolve S<T>::R to an ordinary
20081 type, rather than a typename type.
20082
20083 The reason we normally avoid resolving TYPENAME_TYPEs
20084 is that a specialization of `S' might render
20085 `S<T>::R' not a type. However, if `S' is
20086 specialized, then this `i' will not be used, so there
20087 is no harm in resolving the types here. */
20088 tree type;
20089
20090 /* Resolve the TYPENAME_TYPE. */
20091 type = resolve_typename_type (qualifying_scope,
20092 /*only_current_p=*/false);
20093 /* If that failed, the declarator is invalid. */
20094 if (TREE_CODE (type) == TYPENAME_TYPE)
20095 {
20096 if (typedef_variant_p (type))
20097 error_at (declarator_id_start_token->location,
20098 "cannot define member of dependent typedef "
20099 "%qT", type);
20100 else
20101 error_at (declarator_id_start_token->location,
20102 "%<%T::%E%> is not a type",
20103 TYPE_CONTEXT (qualifying_scope),
20104 TYPE_IDENTIFIER (qualifying_scope));
20105 }
20106 qualifying_scope = type;
20107 }
20108
20109 sfk = sfk_none;
20110
20111 if (unqualified_name)
20112 {
20113 tree class_type;
20114
20115 if (qualifying_scope
20116 && CLASS_TYPE_P (qualifying_scope))
20117 class_type = qualifying_scope;
20118 else
20119 class_type = current_class_type;
20120
20121 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20122 {
20123 tree name_type = TREE_TYPE (unqualified_name);
20124
20125 if (!class_type || !same_type_p (name_type, class_type))
20126 {
20127 /* We do not attempt to print the declarator
20128 here because we do not have enough
20129 information about its original syntactic
20130 form. */
20131 cp_parser_error (parser, "invalid declarator");
20132 declarator = cp_error_declarator;
20133 break;
20134 }
20135 else if (qualifying_scope
20136 && CLASSTYPE_USE_TEMPLATE (name_type))
20137 {
20138 error_at (declarator_id_start_token->location,
20139 "invalid use of constructor as a template");
20140 inform (declarator_id_start_token->location,
20141 "use %<%T::%D%> instead of %<%T::%D%> to "
20142 "name the constructor in a qualified name",
20143 class_type,
20144 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20145 class_type, name_type);
20146 declarator = cp_error_declarator;
20147 break;
20148 }
20149 unqualified_name = constructor_name (class_type);
20150 }
20151
20152 if (class_type)
20153 {
20154 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20155 sfk = sfk_destructor;
20156 else if (identifier_p (unqualified_name)
20157 && IDENTIFIER_CONV_OP_P (unqualified_name))
20158 sfk = sfk_conversion;
20159 else if (/* There's no way to declare a constructor
20160 for an unnamed type, even if the type
20161 got a name for linkage purposes. */
20162 !TYPE_WAS_UNNAMED (class_type)
20163 /* Handle correctly (c++/19200):
20164
20165 struct S {
20166 struct T{};
20167 friend void S(T);
20168 };
20169
20170 and also:
20171
20172 namespace N {
20173 void S();
20174 }
20175
20176 struct S {
20177 friend void N::S();
20178 }; */
20179 && (!friend_p || class_type == qualifying_scope)
20180 && constructor_name_p (unqualified_name,
20181 class_type))
20182 sfk = sfk_constructor;
20183 else if (is_overloaded_fn (unqualified_name)
20184 && DECL_CONSTRUCTOR_P (get_first_fn
20185 (unqualified_name)))
20186 sfk = sfk_constructor;
20187
20188 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20189 *ctor_dtor_or_conv_p = -1;
20190 }
20191 }
20192 declarator = make_id_declarator (qualifying_scope,
20193 unqualified_name,
20194 sfk);
20195 declarator->std_attributes = attrs;
20196 declarator->id_loc = token->location;
20197 declarator->parameter_pack_p = pack_expansion_p;
20198
20199 if (pack_expansion_p)
20200 maybe_warn_variadic_templates ();
20201 }
20202
20203 handle_declarator:;
20204 scope = get_scope_of_declarator (declarator);
20205 if (scope)
20206 {
20207 /* Any names that appear after the declarator-id for a
20208 member are looked up in the containing scope. */
20209 if (at_function_scope_p ())
20210 {
20211 /* But declarations with qualified-ids can't appear in a
20212 function. */
20213 cp_parser_error (parser, "qualified-id in declaration");
20214 declarator = cp_error_declarator;
20215 break;
20216 }
20217 pushed_scope = push_scope (scope);
20218 }
20219 parser->in_declarator_p = true;
20220 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20221 || (declarator && declarator->kind == cdk_id))
20222 /* Default args are only allowed on function
20223 declarations. */
20224 parser->default_arg_ok_p = saved_default_arg_ok_p;
20225 else
20226 parser->default_arg_ok_p = false;
20227
20228 first = false;
20229 }
20230 /* We're done. */
20231 else
20232 break;
20233 }
20234
20235 /* For an abstract declarator, we might wind up with nothing at this
20236 point. That's an error; the declarator is not optional. */
20237 if (!declarator)
20238 cp_parser_error (parser, "expected declarator");
20239
20240 /* If we entered a scope, we must exit it now. */
20241 if (pushed_scope)
20242 pop_scope (pushed_scope);
20243
20244 parser->default_arg_ok_p = saved_default_arg_ok_p;
20245 parser->in_declarator_p = saved_in_declarator_p;
20246
20247 return declarator;
20248 }
20249
20250 /* Parse a ptr-operator.
20251
20252 ptr-operator:
20253 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20254 * cv-qualifier-seq [opt]
20255 &
20256 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20257 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20258
20259 GNU Extension:
20260
20261 ptr-operator:
20262 & cv-qualifier-seq [opt]
20263
20264 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20265 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20266 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20267 filled in with the TYPE containing the member. *CV_QUALS is
20268 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20269 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20270 Note that the tree codes returned by this function have nothing
20271 to do with the types of trees that will be eventually be created
20272 to represent the pointer or reference type being parsed. They are
20273 just constants with suggestive names. */
20274 static enum tree_code
20275 cp_parser_ptr_operator (cp_parser* parser,
20276 tree* type,
20277 cp_cv_quals *cv_quals,
20278 tree *attributes)
20279 {
20280 enum tree_code code = ERROR_MARK;
20281 cp_token *token;
20282 tree attrs = NULL_TREE;
20283
20284 /* Assume that it's not a pointer-to-member. */
20285 *type = NULL_TREE;
20286 /* And that there are no cv-qualifiers. */
20287 *cv_quals = TYPE_UNQUALIFIED;
20288
20289 /* Peek at the next token. */
20290 token = cp_lexer_peek_token (parser->lexer);
20291
20292 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20293 if (token->type == CPP_MULT)
20294 code = INDIRECT_REF;
20295 else if (token->type == CPP_AND)
20296 code = ADDR_EXPR;
20297 else if ((cxx_dialect != cxx98) &&
20298 token->type == CPP_AND_AND) /* C++0x only */
20299 code = NON_LVALUE_EXPR;
20300
20301 if (code != ERROR_MARK)
20302 {
20303 /* Consume the `*', `&' or `&&'. */
20304 cp_lexer_consume_token (parser->lexer);
20305
20306 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20307 `&', if we are allowing GNU extensions. (The only qualifier
20308 that can legally appear after `&' is `restrict', but that is
20309 enforced during semantic analysis. */
20310 if (code == INDIRECT_REF
20311 || cp_parser_allow_gnu_extensions_p (parser))
20312 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20313
20314 attrs = cp_parser_std_attribute_spec_seq (parser);
20315 if (attributes != NULL)
20316 *attributes = attrs;
20317 }
20318 else
20319 {
20320 /* Try the pointer-to-member case. */
20321 cp_parser_parse_tentatively (parser);
20322 /* Look for the optional `::' operator. */
20323 cp_parser_global_scope_opt (parser,
20324 /*current_scope_valid_p=*/false);
20325 /* Look for the nested-name specifier. */
20326 token = cp_lexer_peek_token (parser->lexer);
20327 cp_parser_nested_name_specifier (parser,
20328 /*typename_keyword_p=*/false,
20329 /*check_dependency_p=*/true,
20330 /*type_p=*/false,
20331 /*is_declaration=*/false);
20332 /* If we found it, and the next token is a `*', then we are
20333 indeed looking at a pointer-to-member operator. */
20334 if (!cp_parser_error_occurred (parser)
20335 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20336 {
20337 /* Indicate that the `*' operator was used. */
20338 code = INDIRECT_REF;
20339
20340 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20341 error_at (token->location, "%qD is a namespace", parser->scope);
20342 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20343 error_at (token->location, "cannot form pointer to member of "
20344 "non-class %q#T", parser->scope);
20345 else
20346 {
20347 /* The type of which the member is a member is given by the
20348 current SCOPE. */
20349 *type = parser->scope;
20350 /* The next name will not be qualified. */
20351 parser->scope = NULL_TREE;
20352 parser->qualifying_scope = NULL_TREE;
20353 parser->object_scope = NULL_TREE;
20354 /* Look for optional c++11 attributes. */
20355 attrs = cp_parser_std_attribute_spec_seq (parser);
20356 if (attributes != NULL)
20357 *attributes = attrs;
20358 /* Look for the optional cv-qualifier-seq. */
20359 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20360 }
20361 }
20362 /* If that didn't work we don't have a ptr-operator. */
20363 if (!cp_parser_parse_definitely (parser))
20364 cp_parser_error (parser, "expected ptr-operator");
20365 }
20366
20367 return code;
20368 }
20369
20370 /* Parse an (optional) cv-qualifier-seq.
20371
20372 cv-qualifier-seq:
20373 cv-qualifier cv-qualifier-seq [opt]
20374
20375 cv-qualifier:
20376 const
20377 volatile
20378
20379 GNU Extension:
20380
20381 cv-qualifier:
20382 __restrict__
20383
20384 Returns a bitmask representing the cv-qualifiers. */
20385
20386 static cp_cv_quals
20387 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20388 {
20389 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20390
20391 while (true)
20392 {
20393 cp_token *token;
20394 cp_cv_quals cv_qualifier;
20395
20396 /* Peek at the next token. */
20397 token = cp_lexer_peek_token (parser->lexer);
20398 /* See if it's a cv-qualifier. */
20399 switch (token->keyword)
20400 {
20401 case RID_CONST:
20402 cv_qualifier = TYPE_QUAL_CONST;
20403 break;
20404
20405 case RID_VOLATILE:
20406 cv_qualifier = TYPE_QUAL_VOLATILE;
20407 break;
20408
20409 case RID_RESTRICT:
20410 cv_qualifier = TYPE_QUAL_RESTRICT;
20411 break;
20412
20413 default:
20414 cv_qualifier = TYPE_UNQUALIFIED;
20415 break;
20416 }
20417
20418 if (!cv_qualifier)
20419 break;
20420
20421 if (cv_quals & cv_qualifier)
20422 {
20423 gcc_rich_location richloc (token->location);
20424 richloc.add_fixit_remove ();
20425 error_at_rich_loc (&richloc, "duplicate cv-qualifier");
20426 cp_lexer_purge_token (parser->lexer);
20427 }
20428 else
20429 {
20430 cp_lexer_consume_token (parser->lexer);
20431 cv_quals |= cv_qualifier;
20432 }
20433 }
20434
20435 return cv_quals;
20436 }
20437
20438 /* Parse an (optional) ref-qualifier
20439
20440 ref-qualifier:
20441 &
20442 &&
20443
20444 Returns cp_ref_qualifier representing ref-qualifier. */
20445
20446 static cp_ref_qualifier
20447 cp_parser_ref_qualifier_opt (cp_parser* parser)
20448 {
20449 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20450
20451 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20452 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20453 return ref_qual;
20454
20455 while (true)
20456 {
20457 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20458 cp_token *token = cp_lexer_peek_token (parser->lexer);
20459
20460 switch (token->type)
20461 {
20462 case CPP_AND:
20463 curr_ref_qual = REF_QUAL_LVALUE;
20464 break;
20465
20466 case CPP_AND_AND:
20467 curr_ref_qual = REF_QUAL_RVALUE;
20468 break;
20469
20470 default:
20471 curr_ref_qual = REF_QUAL_NONE;
20472 break;
20473 }
20474
20475 if (!curr_ref_qual)
20476 break;
20477 else if (ref_qual)
20478 {
20479 error_at (token->location, "multiple ref-qualifiers");
20480 cp_lexer_purge_token (parser->lexer);
20481 }
20482 else
20483 {
20484 ref_qual = curr_ref_qual;
20485 cp_lexer_consume_token (parser->lexer);
20486 }
20487 }
20488
20489 return ref_qual;
20490 }
20491
20492 /* Parse an optional tx-qualifier.
20493
20494 tx-qualifier:
20495 transaction_safe
20496 transaction_safe_dynamic */
20497
20498 static tree
20499 cp_parser_tx_qualifier_opt (cp_parser *parser)
20500 {
20501 cp_token *token = cp_lexer_peek_token (parser->lexer);
20502 if (token->type == CPP_NAME)
20503 {
20504 tree name = token->u.value;
20505 const char *p = IDENTIFIER_POINTER (name);
20506 const int len = strlen ("transaction_safe");
20507 if (!strncmp (p, "transaction_safe", len))
20508 {
20509 p += len;
20510 if (*p == '\0'
20511 || !strcmp (p, "_dynamic"))
20512 {
20513 cp_lexer_consume_token (parser->lexer);
20514 if (!flag_tm)
20515 {
20516 error ("%qE requires %<-fgnu-tm%>", name);
20517 return NULL_TREE;
20518 }
20519 else
20520 return name;
20521 }
20522 }
20523 }
20524 return NULL_TREE;
20525 }
20526
20527 /* Parse an (optional) virt-specifier-seq.
20528
20529 virt-specifier-seq:
20530 virt-specifier virt-specifier-seq [opt]
20531
20532 virt-specifier:
20533 override
20534 final
20535
20536 Returns a bitmask representing the virt-specifiers. */
20537
20538 static cp_virt_specifiers
20539 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20540 {
20541 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20542
20543 while (true)
20544 {
20545 cp_token *token;
20546 cp_virt_specifiers virt_specifier;
20547
20548 /* Peek at the next token. */
20549 token = cp_lexer_peek_token (parser->lexer);
20550 /* See if it's a virt-specifier-qualifier. */
20551 if (token->type != CPP_NAME)
20552 break;
20553 if (id_equal (token->u.value, "override"))
20554 {
20555 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20556 virt_specifier = VIRT_SPEC_OVERRIDE;
20557 }
20558 else if (id_equal (token->u.value, "final"))
20559 {
20560 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20561 virt_specifier = VIRT_SPEC_FINAL;
20562 }
20563 else if (id_equal (token->u.value, "__final"))
20564 {
20565 virt_specifier = VIRT_SPEC_FINAL;
20566 }
20567 else
20568 break;
20569
20570 if (virt_specifiers & virt_specifier)
20571 {
20572 gcc_rich_location richloc (token->location);
20573 richloc.add_fixit_remove ();
20574 error_at_rich_loc (&richloc, "duplicate virt-specifier");
20575 cp_lexer_purge_token (parser->lexer);
20576 }
20577 else
20578 {
20579 cp_lexer_consume_token (parser->lexer);
20580 virt_specifiers |= virt_specifier;
20581 }
20582 }
20583 return virt_specifiers;
20584 }
20585
20586 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20587 is in scope even though it isn't real. */
20588
20589 void
20590 inject_this_parameter (tree ctype, cp_cv_quals quals)
20591 {
20592 tree this_parm;
20593
20594 if (current_class_ptr)
20595 {
20596 /* We don't clear this between NSDMIs. Is it already what we want? */
20597 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20598 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20599 && cp_type_quals (type) == quals)
20600 return;
20601 }
20602
20603 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20604 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20605 current_class_ptr = NULL_TREE;
20606 current_class_ref
20607 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20608 current_class_ptr = this_parm;
20609 }
20610
20611 /* Return true iff our current scope is a non-static data member
20612 initializer. */
20613
20614 bool
20615 parsing_nsdmi (void)
20616 {
20617 /* We recognize NSDMI context by the context-less 'this' pointer set up
20618 by the function above. */
20619 if (current_class_ptr
20620 && TREE_CODE (current_class_ptr) == PARM_DECL
20621 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20622 return true;
20623 return false;
20624 }
20625
20626 /* Return true iff our current scope is a default capturing generic lambda
20627 defined within a template. FIXME: This is part of a workaround (see
20628 semantics.c) to handle building lambda closure types correctly in templates
20629 which we ultimately want to defer to instantiation time. */
20630
20631 bool
20632 parsing_default_capturing_generic_lambda_in_template (void)
20633 {
20634 if (!processing_template_decl || !current_class_type)
20635 return false;
20636
20637 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20638 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20639 return false;
20640
20641 tree callop = lambda_function (lam);
20642 if (!callop)
20643 return false;
20644
20645 return (DECL_TEMPLATE_INFO (callop)
20646 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20647 && ((current_nonlambda_class_type ()
20648 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20649 || ((current_nonlambda_function ()
20650 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20651 }
20652
20653 /* Parse a late-specified return type, if any. This is not a separate
20654 non-terminal, but part of a function declarator, which looks like
20655
20656 -> trailing-type-specifier-seq abstract-declarator(opt)
20657
20658 Returns the type indicated by the type-id.
20659
20660 In addition to this, parse any queued up #pragma omp declare simd
20661 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20662 #pragma acc routine clauses.
20663
20664 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20665 function. */
20666
20667 static tree
20668 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20669 tree& requires_clause, cp_cv_quals quals)
20670 {
20671 cp_token *token;
20672 tree type = NULL_TREE;
20673 bool declare_simd_p = (parser->omp_declare_simd
20674 && declarator
20675 && declarator->kind == cdk_id);
20676
20677 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20678 && declarator && declarator->kind == cdk_id);
20679
20680 bool oacc_routine_p = (parser->oacc_routine
20681 && declarator
20682 && declarator->kind == cdk_id);
20683
20684 /* Peek at the next token. */
20685 token = cp_lexer_peek_token (parser->lexer);
20686 /* A late-specified return type is indicated by an initial '->'. */
20687 if (token->type != CPP_DEREF
20688 && token->keyword != RID_REQUIRES
20689 && !(token->type == CPP_NAME
20690 && token->u.value == ridpointers[RID_REQUIRES])
20691 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20692 return NULL_TREE;
20693
20694 tree save_ccp = current_class_ptr;
20695 tree save_ccr = current_class_ref;
20696 if (quals >= 0)
20697 {
20698 /* DR 1207: 'this' is in scope in the trailing return type. */
20699 inject_this_parameter (current_class_type, quals);
20700 }
20701
20702 if (token->type == CPP_DEREF)
20703 {
20704 /* Consume the ->. */
20705 cp_lexer_consume_token (parser->lexer);
20706
20707 type = cp_parser_trailing_type_id (parser);
20708 }
20709
20710 /* Function declarations may be followed by a trailing
20711 requires-clause. */
20712 requires_clause = cp_parser_requires_clause_opt (parser);
20713
20714 if (cilk_simd_fn_vector_p)
20715 declarator->attributes
20716 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20717 declarator->attributes);
20718 if (declare_simd_p)
20719 declarator->attributes
20720 = cp_parser_late_parsing_omp_declare_simd (parser,
20721 declarator->attributes);
20722 if (oacc_routine_p)
20723 declarator->attributes
20724 = cp_parser_late_parsing_oacc_routine (parser,
20725 declarator->attributes);
20726
20727 if (quals >= 0)
20728 {
20729 current_class_ptr = save_ccp;
20730 current_class_ref = save_ccr;
20731 }
20732
20733 return type;
20734 }
20735
20736 /* Parse a declarator-id.
20737
20738 declarator-id:
20739 id-expression
20740 :: [opt] nested-name-specifier [opt] type-name
20741
20742 In the `id-expression' case, the value returned is as for
20743 cp_parser_id_expression if the id-expression was an unqualified-id.
20744 If the id-expression was a qualified-id, then a SCOPE_REF is
20745 returned. The first operand is the scope (either a NAMESPACE_DECL
20746 or TREE_TYPE), but the second is still just a representation of an
20747 unqualified-id. */
20748
20749 static tree
20750 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20751 {
20752 tree id;
20753 /* The expression must be an id-expression. Assume that qualified
20754 names are the names of types so that:
20755
20756 template <class T>
20757 int S<T>::R::i = 3;
20758
20759 will work; we must treat `S<T>::R' as the name of a type.
20760 Similarly, assume that qualified names are templates, where
20761 required, so that:
20762
20763 template <class T>
20764 int S<T>::R<T>::i = 3;
20765
20766 will work, too. */
20767 id = cp_parser_id_expression (parser,
20768 /*template_keyword_p=*/false,
20769 /*check_dependency_p=*/false,
20770 /*template_p=*/NULL,
20771 /*declarator_p=*/true,
20772 optional_p);
20773 if (id && BASELINK_P (id))
20774 id = BASELINK_FUNCTIONS (id);
20775 return id;
20776 }
20777
20778 /* Parse a type-id.
20779
20780 type-id:
20781 type-specifier-seq abstract-declarator [opt]
20782
20783 Returns the TYPE specified. */
20784
20785 static tree
20786 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20787 bool is_trailing_return)
20788 {
20789 cp_decl_specifier_seq type_specifier_seq;
20790 cp_declarator *abstract_declarator;
20791
20792 /* Parse the type-specifier-seq. */
20793 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20794 is_trailing_return,
20795 &type_specifier_seq);
20796 if (is_template_arg && type_specifier_seq.type
20797 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20798 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20799 /* A bare template name as a template argument is a template template
20800 argument, not a placeholder, so fail parsing it as a type argument. */
20801 {
20802 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20803 cp_parser_simulate_error (parser);
20804 return error_mark_node;
20805 }
20806 if (type_specifier_seq.type == error_mark_node)
20807 return error_mark_node;
20808
20809 /* There might or might not be an abstract declarator. */
20810 cp_parser_parse_tentatively (parser);
20811 /* Look for the declarator. */
20812 abstract_declarator
20813 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20814 /*parenthesized_p=*/NULL,
20815 /*member_p=*/false,
20816 /*friend_p=*/false);
20817 /* Check to see if there really was a declarator. */
20818 if (!cp_parser_parse_definitely (parser))
20819 abstract_declarator = NULL;
20820
20821 if (type_specifier_seq.type
20822 /* The concepts TS allows 'auto' as a type-id. */
20823 && (!flag_concepts || parser->in_type_id_in_expr_p)
20824 /* None of the valid uses of 'auto' in C++14 involve the type-id
20825 nonterminal, but it is valid in a trailing-return-type. */
20826 && !(cxx_dialect >= cxx14 && is_trailing_return))
20827 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20828 {
20829 /* A type-id with type 'auto' is only ok if the abstract declarator
20830 is a function declarator with a late-specified return type.
20831
20832 A type-id with 'auto' is also valid in a trailing-return-type
20833 in a compound-requirement. */
20834 if (abstract_declarator
20835 && abstract_declarator->kind == cdk_function
20836 && abstract_declarator->u.function.late_return_type)
20837 /* OK */;
20838 else if (parser->in_result_type_constraint_p)
20839 /* OK */;
20840 else
20841 {
20842 location_t loc = type_specifier_seq.locations[ds_type_spec];
20843 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20844 {
20845 error_at (loc, "missing template arguments after %qT",
20846 auto_node);
20847 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20848 tmpl);
20849 }
20850 else
20851 error_at (loc, "invalid use of %qT", auto_node);
20852 return error_mark_node;
20853 }
20854 }
20855
20856 return groktypename (&type_specifier_seq, abstract_declarator,
20857 is_template_arg);
20858 }
20859
20860 static tree
20861 cp_parser_type_id (cp_parser *parser)
20862 {
20863 return cp_parser_type_id_1 (parser, false, false);
20864 }
20865
20866 static tree
20867 cp_parser_template_type_arg (cp_parser *parser)
20868 {
20869 tree r;
20870 const char *saved_message = parser->type_definition_forbidden_message;
20871 parser->type_definition_forbidden_message
20872 = G_("types may not be defined in template arguments");
20873 r = cp_parser_type_id_1 (parser, true, false);
20874 parser->type_definition_forbidden_message = saved_message;
20875 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20876 {
20877 error ("invalid use of %<auto%> in template argument");
20878 r = error_mark_node;
20879 }
20880 return r;
20881 }
20882
20883 static tree
20884 cp_parser_trailing_type_id (cp_parser *parser)
20885 {
20886 return cp_parser_type_id_1 (parser, false, true);
20887 }
20888
20889 /* Parse a type-specifier-seq.
20890
20891 type-specifier-seq:
20892 type-specifier type-specifier-seq [opt]
20893
20894 GNU extension:
20895
20896 type-specifier-seq:
20897 attributes type-specifier-seq [opt]
20898
20899 If IS_DECLARATION is true, we are at the start of a "condition" or
20900 exception-declaration, so we might be followed by a declarator-id.
20901
20902 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20903 i.e. we've just seen "->".
20904
20905 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20906
20907 static void
20908 cp_parser_type_specifier_seq (cp_parser* parser,
20909 bool is_declaration,
20910 bool is_trailing_return,
20911 cp_decl_specifier_seq *type_specifier_seq)
20912 {
20913 bool seen_type_specifier = false;
20914 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20915 cp_token *start_token = NULL;
20916
20917 /* Clear the TYPE_SPECIFIER_SEQ. */
20918 clear_decl_specs (type_specifier_seq);
20919
20920 /* In the context of a trailing return type, enum E { } is an
20921 elaborated-type-specifier followed by a function-body, not an
20922 enum-specifier. */
20923 if (is_trailing_return)
20924 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20925
20926 /* Parse the type-specifiers and attributes. */
20927 while (true)
20928 {
20929 tree type_specifier;
20930 bool is_cv_qualifier;
20931
20932 /* Check for attributes first. */
20933 if (cp_next_tokens_can_be_attribute_p (parser))
20934 {
20935 type_specifier_seq->attributes =
20936 chainon (type_specifier_seq->attributes,
20937 cp_parser_attributes_opt (parser));
20938 continue;
20939 }
20940
20941 /* record the token of the beginning of the type specifier seq,
20942 for error reporting purposes*/
20943 if (!start_token)
20944 start_token = cp_lexer_peek_token (parser->lexer);
20945
20946 /* Look for the type-specifier. */
20947 type_specifier = cp_parser_type_specifier (parser,
20948 flags,
20949 type_specifier_seq,
20950 /*is_declaration=*/false,
20951 NULL,
20952 &is_cv_qualifier);
20953 if (!type_specifier)
20954 {
20955 /* If the first type-specifier could not be found, this is not a
20956 type-specifier-seq at all. */
20957 if (!seen_type_specifier)
20958 {
20959 /* Set in_declarator_p to avoid skipping to the semicolon. */
20960 int in_decl = parser->in_declarator_p;
20961 parser->in_declarator_p = true;
20962
20963 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20964 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20965 cp_parser_error (parser, "expected type-specifier");
20966
20967 parser->in_declarator_p = in_decl;
20968
20969 type_specifier_seq->type = error_mark_node;
20970 return;
20971 }
20972 /* If subsequent type-specifiers could not be found, the
20973 type-specifier-seq is complete. */
20974 break;
20975 }
20976
20977 seen_type_specifier = true;
20978 /* The standard says that a condition can be:
20979
20980 type-specifier-seq declarator = assignment-expression
20981
20982 However, given:
20983
20984 struct S {};
20985 if (int S = ...)
20986
20987 we should treat the "S" as a declarator, not as a
20988 type-specifier. The standard doesn't say that explicitly for
20989 type-specifier-seq, but it does say that for
20990 decl-specifier-seq in an ordinary declaration. Perhaps it
20991 would be clearer just to allow a decl-specifier-seq here, and
20992 then add a semantic restriction that if any decl-specifiers
20993 that are not type-specifiers appear, the program is invalid. */
20994 if (is_declaration && !is_cv_qualifier)
20995 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20996 }
20997 }
20998
20999 /* Return whether the function currently being declared has an associated
21000 template parameter list. */
21001
21002 static bool
21003 function_being_declared_is_template_p (cp_parser* parser)
21004 {
21005 if (!current_template_parms || processing_template_parmlist)
21006 return false;
21007
21008 if (parser->implicit_template_scope)
21009 return true;
21010
21011 if (at_class_scope_p ()
21012 && TYPE_BEING_DEFINED (current_class_type))
21013 return parser->num_template_parameter_lists != 0;
21014
21015 return ((int) parser->num_template_parameter_lists > template_class_depth
21016 (current_class_type));
21017 }
21018
21019 /* Parse a parameter-declaration-clause.
21020
21021 parameter-declaration-clause:
21022 parameter-declaration-list [opt] ... [opt]
21023 parameter-declaration-list , ...
21024
21025 Returns a representation for the parameter declarations. A return
21026 value of NULL indicates a parameter-declaration-clause consisting
21027 only of an ellipsis. */
21028
21029 static tree
21030 cp_parser_parameter_declaration_clause (cp_parser* parser)
21031 {
21032 tree parameters;
21033 cp_token *token;
21034 bool ellipsis_p;
21035 bool is_error;
21036
21037 struct cleanup {
21038 cp_parser* parser;
21039 int auto_is_implicit_function_template_parm_p;
21040 ~cleanup() {
21041 parser->auto_is_implicit_function_template_parm_p
21042 = auto_is_implicit_function_template_parm_p;
21043 }
21044 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21045
21046 (void) cleanup;
21047
21048 if (!processing_specialization
21049 && !processing_template_parmlist
21050 && !processing_explicit_instantiation)
21051 if (!current_function_decl
21052 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21053 parser->auto_is_implicit_function_template_parm_p = true;
21054
21055 /* Peek at the next token. */
21056 token = cp_lexer_peek_token (parser->lexer);
21057 /* Check for trivial parameter-declaration-clauses. */
21058 if (token->type == CPP_ELLIPSIS)
21059 {
21060 /* Consume the `...' token. */
21061 cp_lexer_consume_token (parser->lexer);
21062 return NULL_TREE;
21063 }
21064 else if (token->type == CPP_CLOSE_PAREN)
21065 /* There are no parameters. */
21066 {
21067 #ifndef NO_IMPLICIT_EXTERN_C
21068 if (in_system_header_at (input_location)
21069 && current_class_type == NULL
21070 && current_lang_name == lang_name_c)
21071 return NULL_TREE;
21072 else
21073 #endif
21074 return void_list_node;
21075 }
21076 /* Check for `(void)', too, which is a special case. */
21077 else if (token->keyword == RID_VOID
21078 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21079 == CPP_CLOSE_PAREN))
21080 {
21081 /* Consume the `void' token. */
21082 cp_lexer_consume_token (parser->lexer);
21083 /* There are no parameters. */
21084 return void_list_node;
21085 }
21086
21087 /* Parse the parameter-declaration-list. */
21088 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21089 /* If a parse error occurred while parsing the
21090 parameter-declaration-list, then the entire
21091 parameter-declaration-clause is erroneous. */
21092 if (is_error)
21093 return NULL;
21094
21095 /* Peek at the next token. */
21096 token = cp_lexer_peek_token (parser->lexer);
21097 /* If it's a `,', the clause should terminate with an ellipsis. */
21098 if (token->type == CPP_COMMA)
21099 {
21100 /* Consume the `,'. */
21101 cp_lexer_consume_token (parser->lexer);
21102 /* Expect an ellipsis. */
21103 ellipsis_p
21104 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21105 }
21106 /* It might also be `...' if the optional trailing `,' was
21107 omitted. */
21108 else if (token->type == CPP_ELLIPSIS)
21109 {
21110 /* Consume the `...' token. */
21111 cp_lexer_consume_token (parser->lexer);
21112 /* And remember that we saw it. */
21113 ellipsis_p = true;
21114 }
21115 else
21116 ellipsis_p = false;
21117
21118 /* Finish the parameter list. */
21119 if (!ellipsis_p)
21120 parameters = chainon (parameters, void_list_node);
21121
21122 return parameters;
21123 }
21124
21125 /* Parse a parameter-declaration-list.
21126
21127 parameter-declaration-list:
21128 parameter-declaration
21129 parameter-declaration-list , parameter-declaration
21130
21131 Returns a representation of the parameter-declaration-list, as for
21132 cp_parser_parameter_declaration_clause. However, the
21133 `void_list_node' is never appended to the list. Upon return,
21134 *IS_ERROR will be true iff an error occurred. */
21135
21136 static tree
21137 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21138 {
21139 tree parameters = NULL_TREE;
21140 tree *tail = &parameters;
21141 bool saved_in_unbraced_linkage_specification_p;
21142 int index = 0;
21143
21144 /* Assume all will go well. */
21145 *is_error = false;
21146 /* The special considerations that apply to a function within an
21147 unbraced linkage specifications do not apply to the parameters
21148 to the function. */
21149 saved_in_unbraced_linkage_specification_p
21150 = parser->in_unbraced_linkage_specification_p;
21151 parser->in_unbraced_linkage_specification_p = false;
21152
21153 /* Look for more parameters. */
21154 while (true)
21155 {
21156 cp_parameter_declarator *parameter;
21157 tree decl = error_mark_node;
21158 bool parenthesized_p = false;
21159 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21160 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21161 (current_template_parms)) : 0);
21162
21163 /* Parse the parameter. */
21164 parameter
21165 = cp_parser_parameter_declaration (parser,
21166 /*template_parm_p=*/false,
21167 &parenthesized_p);
21168
21169 /* We don't know yet if the enclosing context is deprecated, so wait
21170 and warn in grokparms if appropriate. */
21171 deprecated_state = DEPRECATED_SUPPRESS;
21172
21173 if (parameter)
21174 {
21175 /* If a function parameter pack was specified and an implicit template
21176 parameter was introduced during cp_parser_parameter_declaration,
21177 change any implicit parameters introduced into packs. */
21178 if (parser->implicit_template_parms
21179 && parameter->declarator
21180 && parameter->declarator->parameter_pack_p)
21181 {
21182 int latest_template_parm_idx = TREE_VEC_LENGTH
21183 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21184
21185 if (latest_template_parm_idx != template_parm_idx)
21186 parameter->decl_specifiers.type = convert_generic_types_to_packs
21187 (parameter->decl_specifiers.type,
21188 template_parm_idx, latest_template_parm_idx);
21189 }
21190
21191 decl = grokdeclarator (parameter->declarator,
21192 &parameter->decl_specifiers,
21193 PARM,
21194 parameter->default_argument != NULL_TREE,
21195 &parameter->decl_specifiers.attributes);
21196 }
21197
21198 deprecated_state = DEPRECATED_NORMAL;
21199
21200 /* If a parse error occurred parsing the parameter declaration,
21201 then the entire parameter-declaration-list is erroneous. */
21202 if (decl == error_mark_node)
21203 {
21204 *is_error = true;
21205 parameters = error_mark_node;
21206 break;
21207 }
21208
21209 if (parameter->decl_specifiers.attributes)
21210 cplus_decl_attributes (&decl,
21211 parameter->decl_specifiers.attributes,
21212 0);
21213 if (DECL_NAME (decl))
21214 decl = pushdecl (decl);
21215
21216 if (decl != error_mark_node)
21217 {
21218 retrofit_lang_decl (decl);
21219 DECL_PARM_INDEX (decl) = ++index;
21220 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21221 }
21222
21223 /* Add the new parameter to the list. */
21224 *tail = build_tree_list (parameter->default_argument, decl);
21225 tail = &TREE_CHAIN (*tail);
21226
21227 /* Peek at the next token. */
21228 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21229 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21230 /* These are for Objective-C++ */
21231 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21232 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21233 /* The parameter-declaration-list is complete. */
21234 break;
21235 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21236 {
21237 cp_token *token;
21238
21239 /* Peek at the next token. */
21240 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21241 /* If it's an ellipsis, then the list is complete. */
21242 if (token->type == CPP_ELLIPSIS)
21243 break;
21244 /* Otherwise, there must be more parameters. Consume the
21245 `,'. */
21246 cp_lexer_consume_token (parser->lexer);
21247 /* When parsing something like:
21248
21249 int i(float f, double d)
21250
21251 we can tell after seeing the declaration for "f" that we
21252 are not looking at an initialization of a variable "i",
21253 but rather at the declaration of a function "i".
21254
21255 Due to the fact that the parsing of template arguments
21256 (as specified to a template-id) requires backtracking we
21257 cannot use this technique when inside a template argument
21258 list. */
21259 if (!parser->in_template_argument_list_p
21260 && !parser->in_type_id_in_expr_p
21261 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21262 /* However, a parameter-declaration of the form
21263 "float(f)" (which is a valid declaration of a
21264 parameter "f") can also be interpreted as an
21265 expression (the conversion of "f" to "float"). */
21266 && !parenthesized_p)
21267 cp_parser_commit_to_tentative_parse (parser);
21268 }
21269 else
21270 {
21271 cp_parser_error (parser, "expected %<,%> or %<...%>");
21272 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21273 cp_parser_skip_to_closing_parenthesis (parser,
21274 /*recovering=*/true,
21275 /*or_comma=*/false,
21276 /*consume_paren=*/false);
21277 break;
21278 }
21279 }
21280
21281 parser->in_unbraced_linkage_specification_p
21282 = saved_in_unbraced_linkage_specification_p;
21283
21284 /* Reset implicit_template_scope if we are about to leave the function
21285 parameter list that introduced it. Note that for out-of-line member
21286 definitions, there will be one or more class scopes before we get to
21287 the template parameter scope. */
21288
21289 if (cp_binding_level *its = parser->implicit_template_scope)
21290 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21291 {
21292 while (maybe_its->kind == sk_class)
21293 maybe_its = maybe_its->level_chain;
21294 if (maybe_its == its)
21295 {
21296 parser->implicit_template_parms = 0;
21297 parser->implicit_template_scope = 0;
21298 }
21299 }
21300
21301 return parameters;
21302 }
21303
21304 /* Parse a parameter declaration.
21305
21306 parameter-declaration:
21307 decl-specifier-seq ... [opt] declarator
21308 decl-specifier-seq declarator = assignment-expression
21309 decl-specifier-seq ... [opt] abstract-declarator [opt]
21310 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21311
21312 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21313 declares a template parameter. (In that case, a non-nested `>'
21314 token encountered during the parsing of the assignment-expression
21315 is not interpreted as a greater-than operator.)
21316
21317 Returns a representation of the parameter, or NULL if an error
21318 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21319 true iff the declarator is of the form "(p)". */
21320
21321 static cp_parameter_declarator *
21322 cp_parser_parameter_declaration (cp_parser *parser,
21323 bool template_parm_p,
21324 bool *parenthesized_p)
21325 {
21326 int declares_class_or_enum;
21327 cp_decl_specifier_seq decl_specifiers;
21328 cp_declarator *declarator;
21329 tree default_argument;
21330 cp_token *token = NULL, *declarator_token_start = NULL;
21331 const char *saved_message;
21332 bool template_parameter_pack_p = false;
21333
21334 /* In a template parameter, `>' is not an operator.
21335
21336 [temp.param]
21337
21338 When parsing a default template-argument for a non-type
21339 template-parameter, the first non-nested `>' is taken as the end
21340 of the template parameter-list rather than a greater-than
21341 operator. */
21342
21343 /* Type definitions may not appear in parameter types. */
21344 saved_message = parser->type_definition_forbidden_message;
21345 parser->type_definition_forbidden_message
21346 = G_("types may not be defined in parameter types");
21347
21348 /* Parse the declaration-specifiers. */
21349 cp_parser_decl_specifier_seq (parser,
21350 CP_PARSER_FLAGS_NONE,
21351 &decl_specifiers,
21352 &declares_class_or_enum);
21353
21354 /* Complain about missing 'typename' or other invalid type names. */
21355 if (!decl_specifiers.any_type_specifiers_p
21356 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21357 decl_specifiers.type = error_mark_node;
21358
21359 /* If an error occurred, there's no reason to attempt to parse the
21360 rest of the declaration. */
21361 if (cp_parser_error_occurred (parser))
21362 {
21363 parser->type_definition_forbidden_message = saved_message;
21364 return NULL;
21365 }
21366
21367 /* Peek at the next token. */
21368 token = cp_lexer_peek_token (parser->lexer);
21369
21370 /* If the next token is a `)', `,', `=', `>', or `...', then there
21371 is no declarator. However, when variadic templates are enabled,
21372 there may be a declarator following `...'. */
21373 if (token->type == CPP_CLOSE_PAREN
21374 || token->type == CPP_COMMA
21375 || token->type == CPP_EQ
21376 || token->type == CPP_GREATER)
21377 {
21378 declarator = NULL;
21379 if (parenthesized_p)
21380 *parenthesized_p = false;
21381 }
21382 /* Otherwise, there should be a declarator. */
21383 else
21384 {
21385 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21386 parser->default_arg_ok_p = false;
21387
21388 /* After seeing a decl-specifier-seq, if the next token is not a
21389 "(", there is no possibility that the code is a valid
21390 expression. Therefore, if parsing tentatively, we commit at
21391 this point. */
21392 if (!parser->in_template_argument_list_p
21393 /* In an expression context, having seen:
21394
21395 (int((char ...
21396
21397 we cannot be sure whether we are looking at a
21398 function-type (taking a "char" as a parameter) or a cast
21399 of some object of type "char" to "int". */
21400 && !parser->in_type_id_in_expr_p
21401 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21402 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21403 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21404 cp_parser_commit_to_tentative_parse (parser);
21405 /* Parse the declarator. */
21406 declarator_token_start = token;
21407 declarator = cp_parser_declarator (parser,
21408 CP_PARSER_DECLARATOR_EITHER,
21409 /*ctor_dtor_or_conv_p=*/NULL,
21410 parenthesized_p,
21411 /*member_p=*/false,
21412 /*friend_p=*/false);
21413 parser->default_arg_ok_p = saved_default_arg_ok_p;
21414 /* After the declarator, allow more attributes. */
21415 decl_specifiers.attributes
21416 = chainon (decl_specifiers.attributes,
21417 cp_parser_attributes_opt (parser));
21418
21419 /* If the declarator is a template parameter pack, remember that and
21420 clear the flag in the declarator itself so we don't get errors
21421 from grokdeclarator. */
21422 if (template_parm_p && declarator && declarator->parameter_pack_p)
21423 {
21424 declarator->parameter_pack_p = false;
21425 template_parameter_pack_p = true;
21426 }
21427 }
21428
21429 /* If the next token is an ellipsis, and we have not seen a declarator
21430 name, and if either the type of the declarator contains parameter
21431 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21432 for, eg, abbreviated integral type names), then we actually have a
21433 parameter pack expansion expression. Otherwise, leave the ellipsis
21434 for a C-style variadic function. */
21435 token = cp_lexer_peek_token (parser->lexer);
21436 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21437 {
21438 tree type = decl_specifiers.type;
21439
21440 if (type && DECL_P (type))
21441 type = TREE_TYPE (type);
21442
21443 if (((type
21444 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21445 && (template_parm_p || uses_parameter_packs (type)))
21446 || (!type && template_parm_p))
21447 && declarator_can_be_parameter_pack (declarator))
21448 {
21449 /* Consume the `...'. */
21450 cp_lexer_consume_token (parser->lexer);
21451 maybe_warn_variadic_templates ();
21452
21453 /* Build a pack expansion type */
21454 if (template_parm_p)
21455 template_parameter_pack_p = true;
21456 else if (declarator)
21457 declarator->parameter_pack_p = true;
21458 else
21459 decl_specifiers.type = make_pack_expansion (type);
21460 }
21461 }
21462
21463 /* The restriction on defining new types applies only to the type
21464 of the parameter, not to the default argument. */
21465 parser->type_definition_forbidden_message = saved_message;
21466
21467 /* If the next token is `=', then process a default argument. */
21468 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21469 {
21470 tree type = decl_specifiers.type;
21471 token = cp_lexer_peek_token (parser->lexer);
21472 /* If we are defining a class, then the tokens that make up the
21473 default argument must be saved and processed later. */
21474 if (!template_parm_p && at_class_scope_p ()
21475 && TYPE_BEING_DEFINED (current_class_type)
21476 && !LAMBDA_TYPE_P (current_class_type))
21477 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21478
21479 // A constrained-type-specifier may declare a type template-parameter.
21480 else if (declares_constrained_type_template_parameter (type))
21481 default_argument
21482 = cp_parser_default_type_template_argument (parser);
21483
21484 // A constrained-type-specifier may declare a template-template-parameter.
21485 else if (declares_constrained_template_template_parameter (type))
21486 default_argument
21487 = cp_parser_default_template_template_argument (parser);
21488
21489 /* Outside of a class definition, we can just parse the
21490 assignment-expression. */
21491 else
21492 default_argument
21493 = cp_parser_default_argument (parser, template_parm_p);
21494
21495 if (!parser->default_arg_ok_p)
21496 {
21497 permerror (token->location,
21498 "default arguments are only "
21499 "permitted for function parameters");
21500 }
21501 else if ((declarator && declarator->parameter_pack_p)
21502 || template_parameter_pack_p
21503 || (decl_specifiers.type
21504 && PACK_EXPANSION_P (decl_specifiers.type)))
21505 {
21506 /* Find the name of the parameter pack. */
21507 cp_declarator *id_declarator = declarator;
21508 while (id_declarator && id_declarator->kind != cdk_id)
21509 id_declarator = id_declarator->declarator;
21510
21511 if (id_declarator && id_declarator->kind == cdk_id)
21512 error_at (declarator_token_start->location,
21513 template_parm_p
21514 ? G_("template parameter pack %qD "
21515 "cannot have a default argument")
21516 : G_("parameter pack %qD cannot have "
21517 "a default argument"),
21518 id_declarator->u.id.unqualified_name);
21519 else
21520 error_at (declarator_token_start->location,
21521 template_parm_p
21522 ? G_("template parameter pack cannot have "
21523 "a default argument")
21524 : G_("parameter pack cannot have a "
21525 "default argument"));
21526
21527 default_argument = NULL_TREE;
21528 }
21529 }
21530 else
21531 default_argument = NULL_TREE;
21532
21533 return make_parameter_declarator (&decl_specifiers,
21534 declarator,
21535 default_argument,
21536 template_parameter_pack_p);
21537 }
21538
21539 /* Parse a default argument and return it.
21540
21541 TEMPLATE_PARM_P is true if this is a default argument for a
21542 non-type template parameter. */
21543 static tree
21544 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21545 {
21546 tree default_argument = NULL_TREE;
21547 bool saved_greater_than_is_operator_p;
21548 bool saved_local_variables_forbidden_p;
21549 bool non_constant_p, is_direct_init;
21550
21551 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21552 set correctly. */
21553 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21554 parser->greater_than_is_operator_p = !template_parm_p;
21555 /* Local variable names (and the `this' keyword) may not
21556 appear in a default argument. */
21557 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21558 parser->local_variables_forbidden_p = true;
21559 /* Parse the assignment-expression. */
21560 if (template_parm_p)
21561 push_deferring_access_checks (dk_no_deferred);
21562 tree saved_class_ptr = NULL_TREE;
21563 tree saved_class_ref = NULL_TREE;
21564 /* The "this" pointer is not valid in a default argument. */
21565 if (cfun)
21566 {
21567 saved_class_ptr = current_class_ptr;
21568 cp_function_chain->x_current_class_ptr = NULL_TREE;
21569 saved_class_ref = current_class_ref;
21570 cp_function_chain->x_current_class_ref = NULL_TREE;
21571 }
21572 default_argument
21573 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21574 /* Restore the "this" pointer. */
21575 if (cfun)
21576 {
21577 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21578 cp_function_chain->x_current_class_ref = saved_class_ref;
21579 }
21580 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21581 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21582 if (template_parm_p)
21583 pop_deferring_access_checks ();
21584 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21585 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21586
21587 return default_argument;
21588 }
21589
21590 /* Parse a function-body.
21591
21592 function-body:
21593 compound_statement */
21594
21595 static void
21596 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21597 {
21598 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21599 ? BCS_TRY_BLOCK : BCS_NORMAL),
21600 true);
21601 }
21602
21603 /* Parse a ctor-initializer-opt followed by a function-body. Return
21604 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21605 is true we are parsing a function-try-block. */
21606
21607 static bool
21608 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21609 bool in_function_try_block)
21610 {
21611 tree body, list;
21612 bool ctor_initializer_p;
21613 const bool check_body_p =
21614 DECL_CONSTRUCTOR_P (current_function_decl)
21615 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21616 tree last = NULL;
21617
21618 /* Begin the function body. */
21619 body = begin_function_body ();
21620 /* Parse the optional ctor-initializer. */
21621 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21622
21623 /* If we're parsing a constexpr constructor definition, we need
21624 to check that the constructor body is indeed empty. However,
21625 before we get to cp_parser_function_body lot of junk has been
21626 generated, so we can't just check that we have an empty block.
21627 Rather we take a snapshot of the outermost block, and check whether
21628 cp_parser_function_body changed its state. */
21629 if (check_body_p)
21630 {
21631 list = cur_stmt_list;
21632 if (STATEMENT_LIST_TAIL (list))
21633 last = STATEMENT_LIST_TAIL (list)->stmt;
21634 }
21635 /* Parse the function-body. */
21636 cp_parser_function_body (parser, in_function_try_block);
21637 if (check_body_p)
21638 check_constexpr_ctor_body (last, list, /*complain=*/true);
21639 /* Finish the function body. */
21640 finish_function_body (body);
21641
21642 return ctor_initializer_p;
21643 }
21644
21645 /* Parse an initializer.
21646
21647 initializer:
21648 = initializer-clause
21649 ( expression-list )
21650
21651 Returns an expression representing the initializer. If no
21652 initializer is present, NULL_TREE is returned.
21653
21654 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21655 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21656 set to TRUE if there is no initializer present. If there is an
21657 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21658 is set to true; otherwise it is set to false. */
21659
21660 static tree
21661 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21662 bool* non_constant_p)
21663 {
21664 cp_token *token;
21665 tree init;
21666
21667 /* Peek at the next token. */
21668 token = cp_lexer_peek_token (parser->lexer);
21669
21670 /* Let our caller know whether or not this initializer was
21671 parenthesized. */
21672 *is_direct_init = (token->type != CPP_EQ);
21673 /* Assume that the initializer is constant. */
21674 *non_constant_p = false;
21675
21676 if (token->type == CPP_EQ)
21677 {
21678 /* Consume the `='. */
21679 cp_lexer_consume_token (parser->lexer);
21680 /* Parse the initializer-clause. */
21681 init = cp_parser_initializer_clause (parser, non_constant_p);
21682 }
21683 else if (token->type == CPP_OPEN_PAREN)
21684 {
21685 vec<tree, va_gc> *vec;
21686 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21687 /*cast_p=*/false,
21688 /*allow_expansion_p=*/true,
21689 non_constant_p);
21690 if (vec == NULL)
21691 return error_mark_node;
21692 init = build_tree_list_vec (vec);
21693 release_tree_vector (vec);
21694 }
21695 else if (token->type == CPP_OPEN_BRACE)
21696 {
21697 cp_lexer_set_source_position (parser->lexer);
21698 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21699 init = cp_parser_braced_list (parser, non_constant_p);
21700 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21701 }
21702 else
21703 {
21704 /* Anything else is an error. */
21705 cp_parser_error (parser, "expected initializer");
21706 init = error_mark_node;
21707 }
21708
21709 if (check_for_bare_parameter_packs (init))
21710 init = error_mark_node;
21711
21712 return init;
21713 }
21714
21715 /* Parse an initializer-clause.
21716
21717 initializer-clause:
21718 assignment-expression
21719 braced-init-list
21720
21721 Returns an expression representing the initializer.
21722
21723 If the `assignment-expression' production is used the value
21724 returned is simply a representation for the expression.
21725
21726 Otherwise, calls cp_parser_braced_list. */
21727
21728 static cp_expr
21729 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21730 {
21731 cp_expr initializer;
21732
21733 /* Assume the expression is constant. */
21734 *non_constant_p = false;
21735
21736 /* If it is not a `{', then we are looking at an
21737 assignment-expression. */
21738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21739 {
21740 initializer
21741 = cp_parser_constant_expression (parser,
21742 /*allow_non_constant_p=*/true,
21743 non_constant_p);
21744 }
21745 else
21746 initializer = cp_parser_braced_list (parser, non_constant_p);
21747
21748 return initializer;
21749 }
21750
21751 /* Parse a brace-enclosed initializer list.
21752
21753 braced-init-list:
21754 { initializer-list , [opt] }
21755 { }
21756
21757 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21758 the elements of the initializer-list (or NULL, if the last
21759 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21760 NULL_TREE. There is no way to detect whether or not the optional
21761 trailing `,' was provided. NON_CONSTANT_P is as for
21762 cp_parser_initializer. */
21763
21764 static cp_expr
21765 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21766 {
21767 tree initializer;
21768 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21769
21770 /* Consume the `{' token. */
21771 cp_lexer_consume_token (parser->lexer);
21772 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21773 initializer = make_node (CONSTRUCTOR);
21774 /* If it's not a `}', then there is a non-trivial initializer. */
21775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21776 {
21777 /* Parse the initializer list. */
21778 CONSTRUCTOR_ELTS (initializer)
21779 = cp_parser_initializer_list (parser, non_constant_p);
21780 /* A trailing `,' token is allowed. */
21781 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21782 cp_lexer_consume_token (parser->lexer);
21783 }
21784 else
21785 *non_constant_p = false;
21786 /* Now, there should be a trailing `}'. */
21787 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21788 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21789 TREE_TYPE (initializer) = init_list_type_node;
21790
21791 cp_expr result (initializer);
21792 /* Build a location of the form:
21793 { ... }
21794 ^~~~~~~
21795 with caret==start at the open brace, finish at the close brace. */
21796 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21797 result.set_location (combined_loc);
21798 return result;
21799 }
21800
21801 /* Consume tokens up to, and including, the next non-nested closing `]'.
21802 Returns true iff we found a closing `]'. */
21803
21804 static bool
21805 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21806 {
21807 unsigned square_depth = 0;
21808
21809 while (true)
21810 {
21811 cp_token * token = cp_lexer_peek_token (parser->lexer);
21812
21813 switch (token->type)
21814 {
21815 case CPP_EOF:
21816 case CPP_PRAGMA_EOL:
21817 /* If we've run out of tokens, then there is no closing `]'. */
21818 return false;
21819
21820 case CPP_OPEN_SQUARE:
21821 ++square_depth;
21822 break;
21823
21824 case CPP_CLOSE_SQUARE:
21825 if (!square_depth--)
21826 {
21827 cp_lexer_consume_token (parser->lexer);
21828 return true;
21829 }
21830 break;
21831
21832 default:
21833 break;
21834 }
21835
21836 /* Consume the token. */
21837 cp_lexer_consume_token (parser->lexer);
21838 }
21839 }
21840
21841 /* Return true if we are looking at an array-designator, false otherwise. */
21842
21843 static bool
21844 cp_parser_array_designator_p (cp_parser *parser)
21845 {
21846 /* Consume the `['. */
21847 cp_lexer_consume_token (parser->lexer);
21848
21849 cp_lexer_save_tokens (parser->lexer);
21850
21851 /* Skip tokens until the next token is a closing square bracket.
21852 If we find the closing `]', and the next token is a `=', then
21853 we are looking at an array designator. */
21854 bool array_designator_p
21855 = (cp_parser_skip_to_closing_square_bracket (parser)
21856 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21857
21858 /* Roll back the tokens we skipped. */
21859 cp_lexer_rollback_tokens (parser->lexer);
21860
21861 return array_designator_p;
21862 }
21863
21864 /* Parse an initializer-list.
21865
21866 initializer-list:
21867 initializer-clause ... [opt]
21868 initializer-list , initializer-clause ... [opt]
21869
21870 GNU Extension:
21871
21872 initializer-list:
21873 designation initializer-clause ...[opt]
21874 initializer-list , designation initializer-clause ...[opt]
21875
21876 designation:
21877 . identifier =
21878 identifier :
21879 [ constant-expression ] =
21880
21881 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21882 for the initializer. If the INDEX of the elt is non-NULL, it is the
21883 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21884 as for cp_parser_initializer. */
21885
21886 static vec<constructor_elt, va_gc> *
21887 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21888 {
21889 vec<constructor_elt, va_gc> *v = NULL;
21890
21891 /* Assume all of the expressions are constant. */
21892 *non_constant_p = false;
21893
21894 /* Parse the rest of the list. */
21895 while (true)
21896 {
21897 cp_token *token;
21898 tree designator;
21899 tree initializer;
21900 bool clause_non_constant_p;
21901
21902 /* If the next token is an identifier and the following one is a
21903 colon, we are looking at the GNU designated-initializer
21904 syntax. */
21905 if (cp_parser_allow_gnu_extensions_p (parser)
21906 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21907 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21908 {
21909 /* Warn the user that they are using an extension. */
21910 pedwarn (input_location, OPT_Wpedantic,
21911 "ISO C++ does not allow designated initializers");
21912 /* Consume the identifier. */
21913 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21914 /* Consume the `:'. */
21915 cp_lexer_consume_token (parser->lexer);
21916 }
21917 /* Also handle the C99 syntax, '. id ='. */
21918 else if (cp_parser_allow_gnu_extensions_p (parser)
21919 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21920 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21921 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21922 {
21923 /* Warn the user that they are using an extension. */
21924 pedwarn (input_location, OPT_Wpedantic,
21925 "ISO C++ does not allow C99 designated initializers");
21926 /* Consume the `.'. */
21927 cp_lexer_consume_token (parser->lexer);
21928 /* Consume the identifier. */
21929 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21930 /* Consume the `='. */
21931 cp_lexer_consume_token (parser->lexer);
21932 }
21933 /* Also handle C99 array designators, '[ const ] ='. */
21934 else if (cp_parser_allow_gnu_extensions_p (parser)
21935 && !c_dialect_objc ()
21936 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21937 {
21938 /* In C++11, [ could start a lambda-introducer. */
21939 bool non_const = false;
21940
21941 cp_parser_parse_tentatively (parser);
21942
21943 if (!cp_parser_array_designator_p (parser))
21944 {
21945 cp_parser_simulate_error (parser);
21946 designator = NULL_TREE;
21947 }
21948 else
21949 {
21950 designator = cp_parser_constant_expression (parser, true,
21951 &non_const);
21952 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21953 cp_parser_require (parser, CPP_EQ, RT_EQ);
21954 }
21955
21956 if (!cp_parser_parse_definitely (parser))
21957 designator = NULL_TREE;
21958 else if (non_const)
21959 require_potential_rvalue_constant_expression (designator);
21960 }
21961 else
21962 designator = NULL_TREE;
21963
21964 /* Parse the initializer. */
21965 initializer = cp_parser_initializer_clause (parser,
21966 &clause_non_constant_p);
21967 /* If any clause is non-constant, so is the entire initializer. */
21968 if (clause_non_constant_p)
21969 *non_constant_p = true;
21970
21971 /* If we have an ellipsis, this is an initializer pack
21972 expansion. */
21973 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21974 {
21975 /* Consume the `...'. */
21976 cp_lexer_consume_token (parser->lexer);
21977
21978 /* Turn the initializer into an initializer expansion. */
21979 initializer = make_pack_expansion (initializer);
21980 }
21981
21982 /* Add it to the vector. */
21983 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21984
21985 /* If the next token is not a comma, we have reached the end of
21986 the list. */
21987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21988 break;
21989
21990 /* Peek at the next token. */
21991 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21992 /* If the next token is a `}', then we're still done. An
21993 initializer-clause can have a trailing `,' after the
21994 initializer-list and before the closing `}'. */
21995 if (token->type == CPP_CLOSE_BRACE)
21996 break;
21997
21998 /* Consume the `,' token. */
21999 cp_lexer_consume_token (parser->lexer);
22000 }
22001
22002 return v;
22003 }
22004
22005 /* Classes [gram.class] */
22006
22007 /* Parse a class-name.
22008
22009 class-name:
22010 identifier
22011 template-id
22012
22013 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22014 to indicate that names looked up in dependent types should be
22015 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22016 keyword has been used to indicate that the name that appears next
22017 is a template. TAG_TYPE indicates the explicit tag given before
22018 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22019 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22020 is the class being defined in a class-head. If ENUM_OK is TRUE,
22021 enum-names are also accepted.
22022
22023 Returns the TYPE_DECL representing the class. */
22024
22025 static tree
22026 cp_parser_class_name (cp_parser *parser,
22027 bool typename_keyword_p,
22028 bool template_keyword_p,
22029 enum tag_types tag_type,
22030 bool check_dependency_p,
22031 bool class_head_p,
22032 bool is_declaration,
22033 bool enum_ok)
22034 {
22035 tree decl;
22036 tree scope;
22037 bool typename_p;
22038 cp_token *token;
22039 tree identifier = NULL_TREE;
22040
22041 /* All class-names start with an identifier. */
22042 token = cp_lexer_peek_token (parser->lexer);
22043 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22044 {
22045 cp_parser_error (parser, "expected class-name");
22046 return error_mark_node;
22047 }
22048
22049 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22050 to a template-id, so we save it here. */
22051 scope = parser->scope;
22052 if (scope == error_mark_node)
22053 return error_mark_node;
22054
22055 /* Any name names a type if we're following the `typename' keyword
22056 in a qualified name where the enclosing scope is type-dependent. */
22057 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22058 && dependent_type_p (scope));
22059 /* Handle the common case (an identifier, but not a template-id)
22060 efficiently. */
22061 if (token->type == CPP_NAME
22062 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22063 {
22064 cp_token *identifier_token;
22065 bool ambiguous_p;
22066
22067 /* Look for the identifier. */
22068 identifier_token = cp_lexer_peek_token (parser->lexer);
22069 ambiguous_p = identifier_token->error_reported;
22070 identifier = cp_parser_identifier (parser);
22071 /* If the next token isn't an identifier, we are certainly not
22072 looking at a class-name. */
22073 if (identifier == error_mark_node)
22074 decl = error_mark_node;
22075 /* If we know this is a type-name, there's no need to look it
22076 up. */
22077 else if (typename_p)
22078 decl = identifier;
22079 else
22080 {
22081 tree ambiguous_decls;
22082 /* If we already know that this lookup is ambiguous, then
22083 we've already issued an error message; there's no reason
22084 to check again. */
22085 if (ambiguous_p)
22086 {
22087 cp_parser_simulate_error (parser);
22088 return error_mark_node;
22089 }
22090 /* If the next token is a `::', then the name must be a type
22091 name.
22092
22093 [basic.lookup.qual]
22094
22095 During the lookup for a name preceding the :: scope
22096 resolution operator, object, function, and enumerator
22097 names are ignored. */
22098 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22099 tag_type = scope_type;
22100 /* Look up the name. */
22101 decl = cp_parser_lookup_name (parser, identifier,
22102 tag_type,
22103 /*is_template=*/false,
22104 /*is_namespace=*/false,
22105 check_dependency_p,
22106 &ambiguous_decls,
22107 identifier_token->location);
22108 if (ambiguous_decls)
22109 {
22110 if (cp_parser_parsing_tentatively (parser))
22111 cp_parser_simulate_error (parser);
22112 return error_mark_node;
22113 }
22114 }
22115 }
22116 else
22117 {
22118 /* Try a template-id. */
22119 decl = cp_parser_template_id (parser, template_keyword_p,
22120 check_dependency_p,
22121 tag_type,
22122 is_declaration);
22123 if (decl == error_mark_node)
22124 return error_mark_node;
22125 }
22126
22127 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22128
22129 /* If this is a typename, create a TYPENAME_TYPE. */
22130 if (typename_p && decl != error_mark_node)
22131 {
22132 decl = make_typename_type (scope, decl, typename_type,
22133 /*complain=*/tf_error);
22134 if (decl != error_mark_node)
22135 decl = TYPE_NAME (decl);
22136 }
22137
22138 decl = strip_using_decl (decl);
22139
22140 /* Check to see that it is really the name of a class. */
22141 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22142 && identifier_p (TREE_OPERAND (decl, 0))
22143 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22144 /* Situations like this:
22145
22146 template <typename T> struct A {
22147 typename T::template X<int>::I i;
22148 };
22149
22150 are problematic. Is `T::template X<int>' a class-name? The
22151 standard does not seem to be definitive, but there is no other
22152 valid interpretation of the following `::'. Therefore, those
22153 names are considered class-names. */
22154 {
22155 decl = make_typename_type (scope, decl, tag_type, tf_error);
22156 if (decl != error_mark_node)
22157 decl = TYPE_NAME (decl);
22158 }
22159 else if (TREE_CODE (decl) != TYPE_DECL
22160 || TREE_TYPE (decl) == error_mark_node
22161 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22162 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22163 /* In Objective-C 2.0, a classname followed by '.' starts a
22164 dot-syntax expression, and it's not a type-name. */
22165 || (c_dialect_objc ()
22166 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22167 && objc_is_class_name (decl)))
22168 decl = error_mark_node;
22169
22170 if (decl == error_mark_node)
22171 cp_parser_error (parser, "expected class-name");
22172 else if (identifier && !parser->scope)
22173 maybe_note_name_used_in_class (identifier, decl);
22174
22175 return decl;
22176 }
22177
22178 /* Parse a class-specifier.
22179
22180 class-specifier:
22181 class-head { member-specification [opt] }
22182
22183 Returns the TREE_TYPE representing the class. */
22184
22185 static tree
22186 cp_parser_class_specifier_1 (cp_parser* parser)
22187 {
22188 tree type;
22189 tree attributes = NULL_TREE;
22190 bool nested_name_specifier_p;
22191 unsigned saved_num_template_parameter_lists;
22192 bool saved_in_function_body;
22193 unsigned char in_statement;
22194 bool in_switch_statement_p;
22195 bool saved_in_unbraced_linkage_specification_p;
22196 tree old_scope = NULL_TREE;
22197 tree scope = NULL_TREE;
22198 cp_token *closing_brace;
22199
22200 push_deferring_access_checks (dk_no_deferred);
22201
22202 /* Parse the class-head. */
22203 type = cp_parser_class_head (parser,
22204 &nested_name_specifier_p);
22205 /* If the class-head was a semantic disaster, skip the entire body
22206 of the class. */
22207 if (!type)
22208 {
22209 cp_parser_skip_to_end_of_block_or_statement (parser);
22210 pop_deferring_access_checks ();
22211 return error_mark_node;
22212 }
22213
22214 /* Look for the `{'. */
22215 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22216 {
22217 pop_deferring_access_checks ();
22218 return error_mark_node;
22219 }
22220
22221 cp_ensure_no_omp_declare_simd (parser);
22222 cp_ensure_no_oacc_routine (parser);
22223
22224 /* Issue an error message if type-definitions are forbidden here. */
22225 cp_parser_check_type_definition (parser);
22226 /* Remember that we are defining one more class. */
22227 ++parser->num_classes_being_defined;
22228 /* Inside the class, surrounding template-parameter-lists do not
22229 apply. */
22230 saved_num_template_parameter_lists
22231 = parser->num_template_parameter_lists;
22232 parser->num_template_parameter_lists = 0;
22233 /* We are not in a function body. */
22234 saved_in_function_body = parser->in_function_body;
22235 parser->in_function_body = false;
22236 /* Or in a loop. */
22237 in_statement = parser->in_statement;
22238 parser->in_statement = 0;
22239 /* Or in a switch. */
22240 in_switch_statement_p = parser->in_switch_statement_p;
22241 parser->in_switch_statement_p = false;
22242 /* We are not immediately inside an extern "lang" block. */
22243 saved_in_unbraced_linkage_specification_p
22244 = parser->in_unbraced_linkage_specification_p;
22245 parser->in_unbraced_linkage_specification_p = false;
22246
22247 // Associate constraints with the type.
22248 if (flag_concepts)
22249 type = associate_classtype_constraints (type);
22250
22251 /* Start the class. */
22252 if (nested_name_specifier_p)
22253 {
22254 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22255 old_scope = push_inner_scope (scope);
22256 }
22257 type = begin_class_definition (type);
22258
22259 if (type == error_mark_node)
22260 /* If the type is erroneous, skip the entire body of the class. */
22261 cp_parser_skip_to_closing_brace (parser);
22262 else
22263 /* Parse the member-specification. */
22264 cp_parser_member_specification_opt (parser);
22265
22266 /* Look for the trailing `}'. */
22267 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22268 /* Look for trailing attributes to apply to this class. */
22269 if (cp_parser_allow_gnu_extensions_p (parser))
22270 attributes = cp_parser_gnu_attributes_opt (parser);
22271 if (type != error_mark_node)
22272 type = finish_struct (type, attributes);
22273 if (nested_name_specifier_p)
22274 pop_inner_scope (old_scope, scope);
22275
22276 /* We've finished a type definition. Check for the common syntax
22277 error of forgetting a semicolon after the definition. We need to
22278 be careful, as we can't just check for not-a-semicolon and be done
22279 with it; the user might have typed:
22280
22281 class X { } c = ...;
22282 class X { } *p = ...;
22283
22284 and so forth. Instead, enumerate all the possible tokens that
22285 might follow this production; if we don't see one of them, then
22286 complain and silently insert the semicolon. */
22287 {
22288 cp_token *token = cp_lexer_peek_token (parser->lexer);
22289 bool want_semicolon = true;
22290
22291 if (cp_next_tokens_can_be_std_attribute_p (parser))
22292 /* Don't try to parse c++11 attributes here. As per the
22293 grammar, that should be a task for
22294 cp_parser_decl_specifier_seq. */
22295 want_semicolon = false;
22296
22297 switch (token->type)
22298 {
22299 case CPP_NAME:
22300 case CPP_SEMICOLON:
22301 case CPP_MULT:
22302 case CPP_AND:
22303 case CPP_OPEN_PAREN:
22304 case CPP_CLOSE_PAREN:
22305 case CPP_COMMA:
22306 want_semicolon = false;
22307 break;
22308
22309 /* While it's legal for type qualifiers and storage class
22310 specifiers to follow type definitions in the grammar, only
22311 compiler testsuites contain code like that. Assume that if
22312 we see such code, then what we're really seeing is a case
22313 like:
22314
22315 class X { }
22316 const <type> var = ...;
22317
22318 or
22319
22320 class Y { }
22321 static <type> func (...) ...
22322
22323 i.e. the qualifier or specifier applies to the next
22324 declaration. To do so, however, we need to look ahead one
22325 more token to see if *that* token is a type specifier.
22326
22327 This code could be improved to handle:
22328
22329 class Z { }
22330 static const <type> var = ...; */
22331 case CPP_KEYWORD:
22332 if (keyword_is_decl_specifier (token->keyword))
22333 {
22334 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22335
22336 /* Handling user-defined types here would be nice, but very
22337 tricky. */
22338 want_semicolon
22339 = (lookahead->type == CPP_KEYWORD
22340 && keyword_begins_type_specifier (lookahead->keyword));
22341 }
22342 break;
22343 default:
22344 break;
22345 }
22346
22347 /* If we don't have a type, then something is very wrong and we
22348 shouldn't try to do anything clever. Likewise for not seeing the
22349 closing brace. */
22350 if (closing_brace && TYPE_P (type) && want_semicolon)
22351 {
22352 /* Locate the closing brace. */
22353 cp_token_position prev
22354 = cp_lexer_previous_token_position (parser->lexer);
22355 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22356 location_t loc = prev_token->location;
22357
22358 /* We want to suggest insertion of a ';' immediately *after* the
22359 closing brace, so, if we can, offset the location by 1 column. */
22360 location_t next_loc = loc;
22361 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22362 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22363
22364 rich_location richloc (line_table, next_loc);
22365
22366 /* If we successfully offset the location, suggest the fix-it. */
22367 if (next_loc != loc)
22368 richloc.add_fixit_insert_before (next_loc, ";");
22369
22370 if (CLASSTYPE_DECLARED_CLASS (type))
22371 error_at_rich_loc (&richloc,
22372 "expected %<;%> after class definition");
22373 else if (TREE_CODE (type) == RECORD_TYPE)
22374 error_at_rich_loc (&richloc,
22375 "expected %<;%> after struct definition");
22376 else if (TREE_CODE (type) == UNION_TYPE)
22377 error_at_rich_loc (&richloc,
22378 "expected %<;%> after union definition");
22379 else
22380 gcc_unreachable ();
22381
22382 /* Unget one token and smash it to look as though we encountered
22383 a semicolon in the input stream. */
22384 cp_lexer_set_token_position (parser->lexer, prev);
22385 token = cp_lexer_peek_token (parser->lexer);
22386 token->type = CPP_SEMICOLON;
22387 token->keyword = RID_MAX;
22388 }
22389 }
22390
22391 /* If this class is not itself within the scope of another class,
22392 then we need to parse the bodies of all of the queued function
22393 definitions. Note that the queued functions defined in a class
22394 are not always processed immediately following the
22395 class-specifier for that class. Consider:
22396
22397 struct A {
22398 struct B { void f() { sizeof (A); } };
22399 };
22400
22401 If `f' were processed before the processing of `A' were
22402 completed, there would be no way to compute the size of `A'.
22403 Note that the nesting we are interested in here is lexical --
22404 not the semantic nesting given by TYPE_CONTEXT. In particular,
22405 for:
22406
22407 struct A { struct B; };
22408 struct A::B { void f() { } };
22409
22410 there is no need to delay the parsing of `A::B::f'. */
22411 if (--parser->num_classes_being_defined == 0)
22412 {
22413 tree decl;
22414 tree class_type = NULL_TREE;
22415 tree pushed_scope = NULL_TREE;
22416 unsigned ix;
22417 cp_default_arg_entry *e;
22418 tree save_ccp, save_ccr;
22419
22420 /* In a first pass, parse default arguments to the functions.
22421 Then, in a second pass, parse the bodies of the functions.
22422 This two-phased approach handles cases like:
22423
22424 struct S {
22425 void f() { g(); }
22426 void g(int i = 3);
22427 };
22428
22429 */
22430 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22431 {
22432 decl = e->decl;
22433 /* If there are default arguments that have not yet been processed,
22434 take care of them now. */
22435 if (class_type != e->class_type)
22436 {
22437 if (pushed_scope)
22438 pop_scope (pushed_scope);
22439 class_type = e->class_type;
22440 pushed_scope = push_scope (class_type);
22441 }
22442 /* Make sure that any template parameters are in scope. */
22443 maybe_begin_member_template_processing (decl);
22444 /* Parse the default argument expressions. */
22445 cp_parser_late_parsing_default_args (parser, decl);
22446 /* Remove any template parameters from the symbol table. */
22447 maybe_end_member_template_processing ();
22448 }
22449 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22450 /* Now parse any NSDMIs. */
22451 save_ccp = current_class_ptr;
22452 save_ccr = current_class_ref;
22453 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22454 {
22455 if (class_type != DECL_CONTEXT (decl))
22456 {
22457 if (pushed_scope)
22458 pop_scope (pushed_scope);
22459 class_type = DECL_CONTEXT (decl);
22460 pushed_scope = push_scope (class_type);
22461 }
22462 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22463 cp_parser_late_parsing_nsdmi (parser, decl);
22464 }
22465 vec_safe_truncate (unparsed_nsdmis, 0);
22466 current_class_ptr = save_ccp;
22467 current_class_ref = save_ccr;
22468 if (pushed_scope)
22469 pop_scope (pushed_scope);
22470
22471 /* Now do some post-NSDMI bookkeeping. */
22472 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22473 after_nsdmi_defaulted_late_checks (class_type);
22474 vec_safe_truncate (unparsed_classes, 0);
22475 after_nsdmi_defaulted_late_checks (type);
22476
22477 /* Now parse the body of the functions. */
22478 if (flag_openmp)
22479 {
22480 /* OpenMP UDRs need to be parsed before all other functions. */
22481 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22482 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22483 cp_parser_late_parsing_for_member (parser, decl);
22484 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22485 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22486 cp_parser_late_parsing_for_member (parser, decl);
22487 }
22488 else
22489 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22490 cp_parser_late_parsing_for_member (parser, decl);
22491 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22492 }
22493 else
22494 vec_safe_push (unparsed_classes, type);
22495
22496 /* Put back any saved access checks. */
22497 pop_deferring_access_checks ();
22498
22499 /* Restore saved state. */
22500 parser->in_switch_statement_p = in_switch_statement_p;
22501 parser->in_statement = in_statement;
22502 parser->in_function_body = saved_in_function_body;
22503 parser->num_template_parameter_lists
22504 = saved_num_template_parameter_lists;
22505 parser->in_unbraced_linkage_specification_p
22506 = saved_in_unbraced_linkage_specification_p;
22507
22508 return type;
22509 }
22510
22511 static tree
22512 cp_parser_class_specifier (cp_parser* parser)
22513 {
22514 tree ret;
22515 timevar_push (TV_PARSE_STRUCT);
22516 ret = cp_parser_class_specifier_1 (parser);
22517 timevar_pop (TV_PARSE_STRUCT);
22518 return ret;
22519 }
22520
22521 /* Parse a class-head.
22522
22523 class-head:
22524 class-key identifier [opt] base-clause [opt]
22525 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22526 class-key nested-name-specifier [opt] template-id
22527 base-clause [opt]
22528
22529 class-virt-specifier:
22530 final
22531
22532 GNU Extensions:
22533 class-key attributes identifier [opt] base-clause [opt]
22534 class-key attributes nested-name-specifier identifier base-clause [opt]
22535 class-key attributes nested-name-specifier [opt] template-id
22536 base-clause [opt]
22537
22538 Upon return BASES is initialized to the list of base classes (or
22539 NULL, if there are none) in the same form returned by
22540 cp_parser_base_clause.
22541
22542 Returns the TYPE of the indicated class. Sets
22543 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22544 involving a nested-name-specifier was used, and FALSE otherwise.
22545
22546 Returns error_mark_node if this is not a class-head.
22547
22548 Returns NULL_TREE if the class-head is syntactically valid, but
22549 semantically invalid in a way that means we should skip the entire
22550 body of the class. */
22551
22552 static tree
22553 cp_parser_class_head (cp_parser* parser,
22554 bool* nested_name_specifier_p)
22555 {
22556 tree nested_name_specifier;
22557 enum tag_types class_key;
22558 tree id = NULL_TREE;
22559 tree type = NULL_TREE;
22560 tree attributes;
22561 tree bases;
22562 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22563 bool template_id_p = false;
22564 bool qualified_p = false;
22565 bool invalid_nested_name_p = false;
22566 bool invalid_explicit_specialization_p = false;
22567 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22568 tree pushed_scope = NULL_TREE;
22569 unsigned num_templates;
22570 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22571 /* Assume no nested-name-specifier will be present. */
22572 *nested_name_specifier_p = false;
22573 /* Assume no template parameter lists will be used in defining the
22574 type. */
22575 num_templates = 0;
22576 parser->colon_corrects_to_scope_p = false;
22577
22578 /* Look for the class-key. */
22579 class_key = cp_parser_class_key (parser);
22580 if (class_key == none_type)
22581 return error_mark_node;
22582
22583 location_t class_head_start_location = input_location;
22584
22585 /* Parse the attributes. */
22586 attributes = cp_parser_attributes_opt (parser);
22587
22588 /* If the next token is `::', that is invalid -- but sometimes
22589 people do try to write:
22590
22591 struct ::S {};
22592
22593 Handle this gracefully by accepting the extra qualifier, and then
22594 issuing an error about it later if this really is a
22595 class-head. If it turns out just to be an elaborated type
22596 specifier, remain silent. */
22597 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22598 qualified_p = true;
22599
22600 push_deferring_access_checks (dk_no_check);
22601
22602 /* Determine the name of the class. Begin by looking for an
22603 optional nested-name-specifier. */
22604 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22605 nested_name_specifier
22606 = cp_parser_nested_name_specifier_opt (parser,
22607 /*typename_keyword_p=*/false,
22608 /*check_dependency_p=*/false,
22609 /*type_p=*/true,
22610 /*is_declaration=*/false);
22611 /* If there was a nested-name-specifier, then there *must* be an
22612 identifier. */
22613
22614 cp_token *bad_template_keyword = NULL;
22615
22616 if (nested_name_specifier)
22617 {
22618 type_start_token = cp_lexer_peek_token (parser->lexer);
22619 /* Although the grammar says `identifier', it really means
22620 `class-name' or `template-name'. You are only allowed to
22621 define a class that has already been declared with this
22622 syntax.
22623
22624 The proposed resolution for Core Issue 180 says that wherever
22625 you see `class T::X' you should treat `X' as a type-name.
22626
22627 It is OK to define an inaccessible class; for example:
22628
22629 class A { class B; };
22630 class A::B {};
22631
22632 We do not know if we will see a class-name, or a
22633 template-name. We look for a class-name first, in case the
22634 class-name is a template-id; if we looked for the
22635 template-name first we would stop after the template-name. */
22636 cp_parser_parse_tentatively (parser);
22637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22638 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22639 type = cp_parser_class_name (parser,
22640 /*typename_keyword_p=*/false,
22641 /*template_keyword_p=*/false,
22642 class_type,
22643 /*check_dependency_p=*/false,
22644 /*class_head_p=*/true,
22645 /*is_declaration=*/false);
22646 /* If that didn't work, ignore the nested-name-specifier. */
22647 if (!cp_parser_parse_definitely (parser))
22648 {
22649 invalid_nested_name_p = true;
22650 type_start_token = cp_lexer_peek_token (parser->lexer);
22651 id = cp_parser_identifier (parser);
22652 if (id == error_mark_node)
22653 id = NULL_TREE;
22654 }
22655 /* If we could not find a corresponding TYPE, treat this
22656 declaration like an unqualified declaration. */
22657 if (type == error_mark_node)
22658 nested_name_specifier = NULL_TREE;
22659 /* Otherwise, count the number of templates used in TYPE and its
22660 containing scopes. */
22661 else
22662 {
22663 tree scope;
22664
22665 for (scope = TREE_TYPE (type);
22666 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22667 scope = get_containing_scope (scope))
22668 if (TYPE_P (scope)
22669 && CLASS_TYPE_P (scope)
22670 && CLASSTYPE_TEMPLATE_INFO (scope)
22671 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22672 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22673 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22674 ++num_templates;
22675 }
22676 }
22677 /* Otherwise, the identifier is optional. */
22678 else
22679 {
22680 /* We don't know whether what comes next is a template-id,
22681 an identifier, or nothing at all. */
22682 cp_parser_parse_tentatively (parser);
22683 /* Check for a template-id. */
22684 type_start_token = cp_lexer_peek_token (parser->lexer);
22685 id = cp_parser_template_id (parser,
22686 /*template_keyword_p=*/false,
22687 /*check_dependency_p=*/true,
22688 class_key,
22689 /*is_declaration=*/true);
22690 /* If that didn't work, it could still be an identifier. */
22691 if (!cp_parser_parse_definitely (parser))
22692 {
22693 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22694 {
22695 type_start_token = cp_lexer_peek_token (parser->lexer);
22696 id = cp_parser_identifier (parser);
22697 }
22698 else
22699 id = NULL_TREE;
22700 }
22701 else
22702 {
22703 template_id_p = true;
22704 ++num_templates;
22705 }
22706 }
22707
22708 pop_deferring_access_checks ();
22709
22710 if (id)
22711 {
22712 cp_parser_check_for_invalid_template_id (parser, id,
22713 class_key,
22714 type_start_token->location);
22715 }
22716 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22717
22718 /* If it's not a `:' or a `{' then we can't really be looking at a
22719 class-head, since a class-head only appears as part of a
22720 class-specifier. We have to detect this situation before calling
22721 xref_tag, since that has irreversible side-effects. */
22722 if (!cp_parser_next_token_starts_class_definition_p (parser))
22723 {
22724 cp_parser_error (parser, "expected %<{%> or %<:%>");
22725 type = error_mark_node;
22726 goto out;
22727 }
22728
22729 /* At this point, we're going ahead with the class-specifier, even
22730 if some other problem occurs. */
22731 cp_parser_commit_to_tentative_parse (parser);
22732 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22733 {
22734 cp_parser_error (parser,
22735 "cannot specify %<override%> for a class");
22736 type = error_mark_node;
22737 goto out;
22738 }
22739 /* Issue the error about the overly-qualified name now. */
22740 if (qualified_p)
22741 {
22742 cp_parser_error (parser,
22743 "global qualification of class name is invalid");
22744 type = error_mark_node;
22745 goto out;
22746 }
22747 else if (invalid_nested_name_p)
22748 {
22749 cp_parser_error (parser,
22750 "qualified name does not name a class");
22751 type = error_mark_node;
22752 goto out;
22753 }
22754 else if (nested_name_specifier)
22755 {
22756 tree scope;
22757
22758 if (bad_template_keyword)
22759 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22760 keyword template shall not appear at the top level. */
22761 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22762 "keyword %<template%> not allowed in class-head-name");
22763
22764 /* Reject typedef-names in class heads. */
22765 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22766 {
22767 error_at (type_start_token->location,
22768 "invalid class name in declaration of %qD",
22769 type);
22770 type = NULL_TREE;
22771 goto done;
22772 }
22773
22774 /* Figure out in what scope the declaration is being placed. */
22775 scope = current_scope ();
22776 /* If that scope does not contain the scope in which the
22777 class was originally declared, the program is invalid. */
22778 if (scope && !is_ancestor (scope, nested_name_specifier))
22779 {
22780 if (at_namespace_scope_p ())
22781 error_at (type_start_token->location,
22782 "declaration of %qD in namespace %qD which does not "
22783 "enclose %qD",
22784 type, scope, nested_name_specifier);
22785 else
22786 error_at (type_start_token->location,
22787 "declaration of %qD in %qD which does not enclose %qD",
22788 type, scope, nested_name_specifier);
22789 type = NULL_TREE;
22790 goto done;
22791 }
22792 /* [dcl.meaning]
22793
22794 A declarator-id shall not be qualified except for the
22795 definition of a ... nested class outside of its class
22796 ... [or] the definition or explicit instantiation of a
22797 class member of a namespace outside of its namespace. */
22798 if (scope == nested_name_specifier)
22799 {
22800 permerror (nested_name_specifier_token_start->location,
22801 "extra qualification not allowed");
22802 nested_name_specifier = NULL_TREE;
22803 num_templates = 0;
22804 }
22805 }
22806 /* An explicit-specialization must be preceded by "template <>". If
22807 it is not, try to recover gracefully. */
22808 if (at_namespace_scope_p ()
22809 && parser->num_template_parameter_lists == 0
22810 && !processing_template_parmlist
22811 && template_id_p)
22812 {
22813 /* Build a location of this form:
22814 struct typename <ARGS>
22815 ^~~~~~~~~~~~~~~~~~~~~~
22816 with caret==start at the start token, and
22817 finishing at the end of the type. */
22818 location_t reported_loc
22819 = make_location (class_head_start_location,
22820 class_head_start_location,
22821 get_finish (type_start_token->location));
22822 rich_location richloc (line_table, reported_loc);
22823 richloc.add_fixit_insert_before (class_head_start_location,
22824 "template <> ");
22825 error_at_rich_loc
22826 (&richloc,
22827 "an explicit specialization must be preceded by %<template <>%>");
22828 invalid_explicit_specialization_p = true;
22829 /* Take the same action that would have been taken by
22830 cp_parser_explicit_specialization. */
22831 ++parser->num_template_parameter_lists;
22832 begin_specialization ();
22833 }
22834 /* There must be no "return" statements between this point and the
22835 end of this function; set "type "to the correct return value and
22836 use "goto done;" to return. */
22837 /* Make sure that the right number of template parameters were
22838 present. */
22839 if (!cp_parser_check_template_parameters (parser, num_templates,
22840 type_start_token->location,
22841 /*declarator=*/NULL))
22842 {
22843 /* If something went wrong, there is no point in even trying to
22844 process the class-definition. */
22845 type = NULL_TREE;
22846 goto done;
22847 }
22848
22849 /* Look up the type. */
22850 if (template_id_p)
22851 {
22852 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22853 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22854 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22855 {
22856 error_at (type_start_token->location,
22857 "function template %qD redeclared as a class template", id);
22858 type = error_mark_node;
22859 }
22860 else
22861 {
22862 type = TREE_TYPE (id);
22863 type = maybe_process_partial_specialization (type);
22864
22865 /* Check the scope while we still know whether or not we had a
22866 nested-name-specifier. */
22867 if (type != error_mark_node)
22868 check_unqualified_spec_or_inst (type, type_start_token->location);
22869 }
22870 if (nested_name_specifier)
22871 pushed_scope = push_scope (nested_name_specifier);
22872 }
22873 else if (nested_name_specifier)
22874 {
22875 tree class_type;
22876
22877 /* Given:
22878
22879 template <typename T> struct S { struct T };
22880 template <typename T> struct S<T>::T { };
22881
22882 we will get a TYPENAME_TYPE when processing the definition of
22883 `S::T'. We need to resolve it to the actual type before we
22884 try to define it. */
22885 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22886 {
22887 class_type = resolve_typename_type (TREE_TYPE (type),
22888 /*only_current_p=*/false);
22889 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22890 type = TYPE_NAME (class_type);
22891 else
22892 {
22893 cp_parser_error (parser, "could not resolve typename type");
22894 type = error_mark_node;
22895 }
22896 }
22897
22898 if (maybe_process_partial_specialization (TREE_TYPE (type))
22899 == error_mark_node)
22900 {
22901 type = NULL_TREE;
22902 goto done;
22903 }
22904
22905 class_type = current_class_type;
22906 /* Enter the scope indicated by the nested-name-specifier. */
22907 pushed_scope = push_scope (nested_name_specifier);
22908 /* Get the canonical version of this type. */
22909 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22910 /* Call push_template_decl if it seems like we should be defining a
22911 template either from the template headers or the type we're
22912 defining, so that we diagnose both extra and missing headers. */
22913 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22914 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22915 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22916 {
22917 type = push_template_decl (type);
22918 if (type == error_mark_node)
22919 {
22920 type = NULL_TREE;
22921 goto done;
22922 }
22923 }
22924
22925 type = TREE_TYPE (type);
22926 *nested_name_specifier_p = true;
22927 }
22928 else /* The name is not a nested name. */
22929 {
22930 /* If the class was unnamed, create a dummy name. */
22931 if (!id)
22932 id = make_anon_name ();
22933 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22934 ? ts_within_enclosing_non_class
22935 : ts_current);
22936 type = xref_tag (class_key, id, tag_scope,
22937 parser->num_template_parameter_lists);
22938 }
22939
22940 /* Indicate whether this class was declared as a `class' or as a
22941 `struct'. */
22942 if (TREE_CODE (type) == RECORD_TYPE)
22943 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22944 cp_parser_check_class_key (class_key, type);
22945
22946 /* If this type was already complete, and we see another definition,
22947 that's an error. */
22948 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22949 {
22950 error_at (type_start_token->location, "redefinition of %q#T",
22951 type);
22952 inform (location_of (type), "previous definition of %q#T",
22953 type);
22954 type = NULL_TREE;
22955 goto done;
22956 }
22957 else if (type == error_mark_node)
22958 type = NULL_TREE;
22959
22960 if (type)
22961 {
22962 /* Apply attributes now, before any use of the class as a template
22963 argument in its base list. */
22964 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22965 fixup_attribute_variants (type);
22966 }
22967
22968 /* We will have entered the scope containing the class; the names of
22969 base classes should be looked up in that context. For example:
22970
22971 struct A { struct B {}; struct C; };
22972 struct A::C : B {};
22973
22974 is valid. */
22975
22976 /* Get the list of base-classes, if there is one. */
22977 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22978 {
22979 /* PR59482: enter the class scope so that base-specifiers are looked
22980 up correctly. */
22981 if (type)
22982 pushclass (type);
22983 bases = cp_parser_base_clause (parser);
22984 /* PR59482: get out of the previously pushed class scope so that the
22985 subsequent pops pop the right thing. */
22986 if (type)
22987 popclass ();
22988 }
22989 else
22990 bases = NULL_TREE;
22991
22992 /* If we're really defining a class, process the base classes.
22993 If they're invalid, fail. */
22994 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22995 xref_basetypes (type, bases);
22996
22997 done:
22998 /* Leave the scope given by the nested-name-specifier. We will
22999 enter the class scope itself while processing the members. */
23000 if (pushed_scope)
23001 pop_scope (pushed_scope);
23002
23003 if (invalid_explicit_specialization_p)
23004 {
23005 end_specialization ();
23006 --parser->num_template_parameter_lists;
23007 }
23008
23009 if (type)
23010 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23011 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23012 CLASSTYPE_FINAL (type) = 1;
23013 out:
23014 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23015 return type;
23016 }
23017
23018 /* Parse a class-key.
23019
23020 class-key:
23021 class
23022 struct
23023 union
23024
23025 Returns the kind of class-key specified, or none_type to indicate
23026 error. */
23027
23028 static enum tag_types
23029 cp_parser_class_key (cp_parser* parser)
23030 {
23031 cp_token *token;
23032 enum tag_types tag_type;
23033
23034 /* Look for the class-key. */
23035 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23036 if (!token)
23037 return none_type;
23038
23039 /* Check to see if the TOKEN is a class-key. */
23040 tag_type = cp_parser_token_is_class_key (token);
23041 if (!tag_type)
23042 cp_parser_error (parser, "expected class-key");
23043 return tag_type;
23044 }
23045
23046 /* Parse a type-parameter-key.
23047
23048 type-parameter-key:
23049 class
23050 typename
23051 */
23052
23053 static void
23054 cp_parser_type_parameter_key (cp_parser* parser)
23055 {
23056 /* Look for the type-parameter-key. */
23057 enum tag_types tag_type = none_type;
23058 cp_token *token = cp_lexer_peek_token (parser->lexer);
23059 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23060 {
23061 cp_lexer_consume_token (parser->lexer);
23062 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
23063 /* typename is not allowed in a template template parameter
23064 by the standard until C++1Z. */
23065 pedwarn (token->location, OPT_Wpedantic,
23066 "ISO C++ forbids typename key in template template parameter;"
23067 " use -std=c++1z or -std=gnu++1z");
23068 }
23069 else
23070 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23071
23072 return;
23073 }
23074
23075 /* Parse an (optional) member-specification.
23076
23077 member-specification:
23078 member-declaration member-specification [opt]
23079 access-specifier : member-specification [opt] */
23080
23081 static void
23082 cp_parser_member_specification_opt (cp_parser* parser)
23083 {
23084 while (true)
23085 {
23086 cp_token *token;
23087 enum rid keyword;
23088
23089 /* Peek at the next token. */
23090 token = cp_lexer_peek_token (parser->lexer);
23091 /* If it's a `}', or EOF then we've seen all the members. */
23092 if (token->type == CPP_CLOSE_BRACE
23093 || token->type == CPP_EOF
23094 || token->type == CPP_PRAGMA_EOL)
23095 break;
23096
23097 /* See if this token is a keyword. */
23098 keyword = token->keyword;
23099 switch (keyword)
23100 {
23101 case RID_PUBLIC:
23102 case RID_PROTECTED:
23103 case RID_PRIVATE:
23104 /* Consume the access-specifier. */
23105 cp_lexer_consume_token (parser->lexer);
23106 /* Remember which access-specifier is active. */
23107 current_access_specifier = token->u.value;
23108 /* Look for the `:'. */
23109 cp_parser_require (parser, CPP_COLON, RT_COLON);
23110 break;
23111
23112 default:
23113 /* Accept #pragmas at class scope. */
23114 if (token->type == CPP_PRAGMA)
23115 {
23116 cp_parser_pragma (parser, pragma_member, NULL);
23117 break;
23118 }
23119
23120 /* Otherwise, the next construction must be a
23121 member-declaration. */
23122 cp_parser_member_declaration (parser);
23123 }
23124 }
23125 }
23126
23127 /* Parse a member-declaration.
23128
23129 member-declaration:
23130 decl-specifier-seq [opt] member-declarator-list [opt] ;
23131 function-definition ; [opt]
23132 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23133 using-declaration
23134 template-declaration
23135 alias-declaration
23136
23137 member-declarator-list:
23138 member-declarator
23139 member-declarator-list , member-declarator
23140
23141 member-declarator:
23142 declarator pure-specifier [opt]
23143 declarator constant-initializer [opt]
23144 identifier [opt] : constant-expression
23145
23146 GNU Extensions:
23147
23148 member-declaration:
23149 __extension__ member-declaration
23150
23151 member-declarator:
23152 declarator attributes [opt] pure-specifier [opt]
23153 declarator attributes [opt] constant-initializer [opt]
23154 identifier [opt] attributes [opt] : constant-expression
23155
23156 C++0x Extensions:
23157
23158 member-declaration:
23159 static_assert-declaration */
23160
23161 static void
23162 cp_parser_member_declaration (cp_parser* parser)
23163 {
23164 cp_decl_specifier_seq decl_specifiers;
23165 tree prefix_attributes;
23166 tree decl;
23167 int declares_class_or_enum;
23168 bool friend_p;
23169 cp_token *token = NULL;
23170 cp_token *decl_spec_token_start = NULL;
23171 cp_token *initializer_token_start = NULL;
23172 int saved_pedantic;
23173 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23174
23175 /* Check for the `__extension__' keyword. */
23176 if (cp_parser_extension_opt (parser, &saved_pedantic))
23177 {
23178 /* Recurse. */
23179 cp_parser_member_declaration (parser);
23180 /* Restore the old value of the PEDANTIC flag. */
23181 pedantic = saved_pedantic;
23182
23183 return;
23184 }
23185
23186 /* Check for a template-declaration. */
23187 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23188 {
23189 /* An explicit specialization here is an error condition, and we
23190 expect the specialization handler to detect and report this. */
23191 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23192 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23193 cp_parser_explicit_specialization (parser);
23194 else
23195 cp_parser_template_declaration (parser, /*member_p=*/true);
23196
23197 return;
23198 }
23199 /* Check for a template introduction. */
23200 else if (cp_parser_template_declaration_after_export (parser, true))
23201 return;
23202
23203 /* Check for a using-declaration. */
23204 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23205 {
23206 if (cxx_dialect < cxx11)
23207 {
23208 /* Parse the using-declaration. */
23209 cp_parser_using_declaration (parser,
23210 /*access_declaration_p=*/false);
23211 return;
23212 }
23213 else
23214 {
23215 tree decl;
23216 bool alias_decl_expected;
23217 cp_parser_parse_tentatively (parser);
23218 decl = cp_parser_alias_declaration (parser);
23219 /* Note that if we actually see the '=' token after the
23220 identifier, cp_parser_alias_declaration commits the
23221 tentative parse. In that case, we really expect an
23222 alias-declaration. Otherwise, we expect a using
23223 declaration. */
23224 alias_decl_expected =
23225 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23226 cp_parser_parse_definitely (parser);
23227
23228 if (alias_decl_expected)
23229 finish_member_declaration (decl);
23230 else
23231 cp_parser_using_declaration (parser,
23232 /*access_declaration_p=*/false);
23233 return;
23234 }
23235 }
23236
23237 /* Check for @defs. */
23238 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23239 {
23240 tree ivar, member;
23241 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23242 ivar = ivar_chains;
23243 while (ivar)
23244 {
23245 member = ivar;
23246 ivar = TREE_CHAIN (member);
23247 TREE_CHAIN (member) = NULL_TREE;
23248 finish_member_declaration (member);
23249 }
23250 return;
23251 }
23252
23253 /* If the next token is `static_assert' we have a static assertion. */
23254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23255 {
23256 cp_parser_static_assert (parser, /*member_p=*/true);
23257 return;
23258 }
23259
23260 parser->colon_corrects_to_scope_p = false;
23261
23262 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23263 goto out;
23264
23265 /* Parse the decl-specifier-seq. */
23266 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23267 cp_parser_decl_specifier_seq (parser,
23268 CP_PARSER_FLAGS_OPTIONAL,
23269 &decl_specifiers,
23270 &declares_class_or_enum);
23271 /* Check for an invalid type-name. */
23272 if (!decl_specifiers.any_type_specifiers_p
23273 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23274 goto out;
23275 /* If there is no declarator, then the decl-specifier-seq should
23276 specify a type. */
23277 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23278 {
23279 /* If there was no decl-specifier-seq, and the next token is a
23280 `;', then we have something like:
23281
23282 struct S { ; };
23283
23284 [class.mem]
23285
23286 Each member-declaration shall declare at least one member
23287 name of the class. */
23288 if (!decl_specifiers.any_specifiers_p)
23289 {
23290 cp_token *token = cp_lexer_peek_token (parser->lexer);
23291 if (!in_system_header_at (token->location))
23292 {
23293 gcc_rich_location richloc (token->location);
23294 richloc.add_fixit_remove ();
23295 pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, "extra %<;%>");
23296 }
23297 }
23298 else
23299 {
23300 tree type;
23301
23302 /* See if this declaration is a friend. */
23303 friend_p = cp_parser_friend_p (&decl_specifiers);
23304 /* If there were decl-specifiers, check to see if there was
23305 a class-declaration. */
23306 type = check_tag_decl (&decl_specifiers,
23307 /*explicit_type_instantiation_p=*/false);
23308 /* Nested classes have already been added to the class, but
23309 a `friend' needs to be explicitly registered. */
23310 if (friend_p)
23311 {
23312 /* If the `friend' keyword was present, the friend must
23313 be introduced with a class-key. */
23314 if (!declares_class_or_enum && cxx_dialect < cxx11)
23315 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23316 "in C++03 a class-key must be used "
23317 "when declaring a friend");
23318 /* In this case:
23319
23320 template <typename T> struct A {
23321 friend struct A<T>::B;
23322 };
23323
23324 A<T>::B will be represented by a TYPENAME_TYPE, and
23325 therefore not recognized by check_tag_decl. */
23326 if (!type)
23327 {
23328 type = decl_specifiers.type;
23329 if (type && TREE_CODE (type) == TYPE_DECL)
23330 type = TREE_TYPE (type);
23331 }
23332 if (!type || !TYPE_P (type))
23333 error_at (decl_spec_token_start->location,
23334 "friend declaration does not name a class or "
23335 "function");
23336 else
23337 make_friend_class (current_class_type, type,
23338 /*complain=*/true);
23339 }
23340 /* If there is no TYPE, an error message will already have
23341 been issued. */
23342 else if (!type || type == error_mark_node)
23343 ;
23344 /* An anonymous aggregate has to be handled specially; such
23345 a declaration really declares a data member (with a
23346 particular type), as opposed to a nested class. */
23347 else if (ANON_AGGR_TYPE_P (type))
23348 {
23349 /* C++11 9.5/6. */
23350 if (decl_specifiers.storage_class != sc_none)
23351 error_at (decl_spec_token_start->location,
23352 "a storage class on an anonymous aggregate "
23353 "in class scope is not allowed");
23354
23355 /* Remove constructors and such from TYPE, now that we
23356 know it is an anonymous aggregate. */
23357 fixup_anonymous_aggr (type);
23358 /* And make the corresponding data member. */
23359 decl = build_decl (decl_spec_token_start->location,
23360 FIELD_DECL, NULL_TREE, type);
23361 /* Add it to the class. */
23362 finish_member_declaration (decl);
23363 }
23364 else
23365 cp_parser_check_access_in_redeclaration
23366 (TYPE_NAME (type),
23367 decl_spec_token_start->location);
23368 }
23369 }
23370 else
23371 {
23372 bool assume_semicolon = false;
23373
23374 /* Clear attributes from the decl_specifiers but keep them
23375 around as prefix attributes that apply them to the entity
23376 being declared. */
23377 prefix_attributes = decl_specifiers.attributes;
23378 decl_specifiers.attributes = NULL_TREE;
23379
23380 /* See if these declarations will be friends. */
23381 friend_p = cp_parser_friend_p (&decl_specifiers);
23382
23383 /* Keep going until we hit the `;' at the end of the
23384 declaration. */
23385 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23386 {
23387 tree attributes = NULL_TREE;
23388 tree first_attribute;
23389
23390 /* Peek at the next token. */
23391 token = cp_lexer_peek_token (parser->lexer);
23392
23393 /* Check for a bitfield declaration. */
23394 if (token->type == CPP_COLON
23395 || (token->type == CPP_NAME
23396 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23397 == CPP_COLON))
23398 {
23399 tree identifier;
23400 tree width;
23401
23402 /* Get the name of the bitfield. Note that we cannot just
23403 check TOKEN here because it may have been invalidated by
23404 the call to cp_lexer_peek_nth_token above. */
23405 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23406 identifier = cp_parser_identifier (parser);
23407 else
23408 identifier = NULL_TREE;
23409
23410 /* Consume the `:' token. */
23411 cp_lexer_consume_token (parser->lexer);
23412 /* Get the width of the bitfield. */
23413 width
23414 = cp_parser_constant_expression (parser);
23415
23416 /* Look for attributes that apply to the bitfield. */
23417 attributes = cp_parser_attributes_opt (parser);
23418 /* Remember which attributes are prefix attributes and
23419 which are not. */
23420 first_attribute = attributes;
23421 /* Combine the attributes. */
23422 attributes = chainon (prefix_attributes, attributes);
23423
23424 /* Create the bitfield declaration. */
23425 decl = grokbitfield (identifier
23426 ? make_id_declarator (NULL_TREE,
23427 identifier,
23428 sfk_none)
23429 : NULL,
23430 &decl_specifiers,
23431 width,
23432 attributes);
23433 }
23434 else
23435 {
23436 cp_declarator *declarator;
23437 tree initializer;
23438 tree asm_specification;
23439 int ctor_dtor_or_conv_p;
23440
23441 /* Parse the declarator. */
23442 declarator
23443 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23444 &ctor_dtor_or_conv_p,
23445 /*parenthesized_p=*/NULL,
23446 /*member_p=*/true,
23447 friend_p);
23448
23449 /* If something went wrong parsing the declarator, make sure
23450 that we at least consume some tokens. */
23451 if (declarator == cp_error_declarator)
23452 {
23453 /* Skip to the end of the statement. */
23454 cp_parser_skip_to_end_of_statement (parser);
23455 /* If the next token is not a semicolon, that is
23456 probably because we just skipped over the body of
23457 a function. So, we consume a semicolon if
23458 present, but do not issue an error message if it
23459 is not present. */
23460 if (cp_lexer_next_token_is (parser->lexer,
23461 CPP_SEMICOLON))
23462 cp_lexer_consume_token (parser->lexer);
23463 goto out;
23464 }
23465
23466 if (declares_class_or_enum & 2)
23467 cp_parser_check_for_definition_in_return_type
23468 (declarator, decl_specifiers.type,
23469 decl_specifiers.locations[ds_type_spec]);
23470
23471 /* Look for an asm-specification. */
23472 asm_specification = cp_parser_asm_specification_opt (parser);
23473 /* Look for attributes that apply to the declaration. */
23474 attributes = cp_parser_attributes_opt (parser);
23475 /* Remember which attributes are prefix attributes and
23476 which are not. */
23477 first_attribute = attributes;
23478 /* Combine the attributes. */
23479 attributes = chainon (prefix_attributes, attributes);
23480
23481 /* If it's an `=', then we have a constant-initializer or a
23482 pure-specifier. It is not correct to parse the
23483 initializer before registering the member declaration
23484 since the member declaration should be in scope while
23485 its initializer is processed. However, the rest of the
23486 front end does not yet provide an interface that allows
23487 us to handle this correctly. */
23488 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23489 {
23490 /* In [class.mem]:
23491
23492 A pure-specifier shall be used only in the declaration of
23493 a virtual function.
23494
23495 A member-declarator can contain a constant-initializer
23496 only if it declares a static member of integral or
23497 enumeration type.
23498
23499 Therefore, if the DECLARATOR is for a function, we look
23500 for a pure-specifier; otherwise, we look for a
23501 constant-initializer. When we call `grokfield', it will
23502 perform more stringent semantics checks. */
23503 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23504 if (function_declarator_p (declarator)
23505 || (decl_specifiers.type
23506 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23507 && declarator->kind == cdk_id
23508 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23509 == FUNCTION_TYPE)))
23510 initializer = cp_parser_pure_specifier (parser);
23511 else if (decl_specifiers.storage_class != sc_static)
23512 initializer = cp_parser_save_nsdmi (parser);
23513 else if (cxx_dialect >= cxx11)
23514 {
23515 bool nonconst;
23516 /* Don't require a constant rvalue in C++11, since we
23517 might want a reference constant. We'll enforce
23518 constancy later. */
23519 cp_lexer_consume_token (parser->lexer);
23520 /* Parse the initializer. */
23521 initializer = cp_parser_initializer_clause (parser,
23522 &nonconst);
23523 }
23524 else
23525 /* Parse the initializer. */
23526 initializer = cp_parser_constant_initializer (parser);
23527 }
23528 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23529 && !function_declarator_p (declarator))
23530 {
23531 bool x;
23532 if (decl_specifiers.storage_class != sc_static)
23533 initializer = cp_parser_save_nsdmi (parser);
23534 else
23535 initializer = cp_parser_initializer (parser, &x, &x);
23536 }
23537 /* Otherwise, there is no initializer. */
23538 else
23539 initializer = NULL_TREE;
23540
23541 /* See if we are probably looking at a function
23542 definition. We are certainly not looking at a
23543 member-declarator. Calling `grokfield' has
23544 side-effects, so we must not do it unless we are sure
23545 that we are looking at a member-declarator. */
23546 if (cp_parser_token_starts_function_definition_p
23547 (cp_lexer_peek_token (parser->lexer)))
23548 {
23549 /* The grammar does not allow a pure-specifier to be
23550 used when a member function is defined. (It is
23551 possible that this fact is an oversight in the
23552 standard, since a pure function may be defined
23553 outside of the class-specifier. */
23554 if (initializer && initializer_token_start)
23555 error_at (initializer_token_start->location,
23556 "pure-specifier on function-definition");
23557 decl = cp_parser_save_member_function_body (parser,
23558 &decl_specifiers,
23559 declarator,
23560 attributes);
23561 if (parser->fully_implicit_function_template_p)
23562 decl = finish_fully_implicit_template (parser, decl);
23563 /* If the member was not a friend, declare it here. */
23564 if (!friend_p)
23565 finish_member_declaration (decl);
23566 /* Peek at the next token. */
23567 token = cp_lexer_peek_token (parser->lexer);
23568 /* If the next token is a semicolon, consume it. */
23569 if (token->type == CPP_SEMICOLON)
23570 {
23571 location_t semicolon_loc
23572 = cp_lexer_consume_token (parser->lexer)->location;
23573 gcc_rich_location richloc (semicolon_loc);
23574 richloc.add_fixit_remove ();
23575 warning_at_rich_loc (&richloc, OPT_Wextra_semi,
23576 "extra %<;%> after in-class "
23577 "function definition");
23578 }
23579 goto out;
23580 }
23581 else
23582 if (declarator->kind == cdk_function)
23583 declarator->id_loc = token->location;
23584 /* Create the declaration. */
23585 decl = grokfield (declarator, &decl_specifiers,
23586 initializer, /*init_const_expr_p=*/true,
23587 asm_specification, attributes);
23588 if (parser->fully_implicit_function_template_p)
23589 {
23590 if (friend_p)
23591 finish_fully_implicit_template (parser, 0);
23592 else
23593 decl = finish_fully_implicit_template (parser, decl);
23594 }
23595 }
23596
23597 cp_finalize_omp_declare_simd (parser, decl);
23598 cp_finalize_oacc_routine (parser, decl, false);
23599
23600 /* Reset PREFIX_ATTRIBUTES. */
23601 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23602 attributes = TREE_CHAIN (attributes);
23603 if (attributes)
23604 TREE_CHAIN (attributes) = NULL_TREE;
23605
23606 /* If there is any qualification still in effect, clear it
23607 now; we will be starting fresh with the next declarator. */
23608 parser->scope = NULL_TREE;
23609 parser->qualifying_scope = NULL_TREE;
23610 parser->object_scope = NULL_TREE;
23611 /* If it's a `,', then there are more declarators. */
23612 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23613 {
23614 cp_lexer_consume_token (parser->lexer);
23615 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23616 {
23617 cp_token *token = cp_lexer_previous_token (parser->lexer);
23618 gcc_rich_location richloc (token->location);
23619 richloc.add_fixit_remove ();
23620 error_at_rich_loc (&richloc, "stray %<,%> at end of "
23621 "member declaration");
23622 }
23623 }
23624 /* If the next token isn't a `;', then we have a parse error. */
23625 else if (cp_lexer_next_token_is_not (parser->lexer,
23626 CPP_SEMICOLON))
23627 {
23628 /* The next token might be a ways away from where the
23629 actual semicolon is missing. Find the previous token
23630 and use that for our error position. */
23631 cp_token *token = cp_lexer_previous_token (parser->lexer);
23632 gcc_rich_location richloc (token->location);
23633 richloc.add_fixit_insert_after (";");
23634 error_at_rich_loc (&richloc, "expected %<;%> at end of "
23635 "member declaration");
23636
23637 /* Assume that the user meant to provide a semicolon. If
23638 we were to cp_parser_skip_to_end_of_statement, we might
23639 skip to a semicolon inside a member function definition
23640 and issue nonsensical error messages. */
23641 assume_semicolon = true;
23642 }
23643
23644 if (decl)
23645 {
23646 /* Add DECL to the list of members. */
23647 if (!friend_p
23648 /* Explicitly include, eg, NSDMIs, for better error
23649 recovery (c++/58650). */
23650 || !DECL_DECLARES_FUNCTION_P (decl))
23651 finish_member_declaration (decl);
23652
23653 if (TREE_CODE (decl) == FUNCTION_DECL)
23654 cp_parser_save_default_args (parser, decl);
23655 else if (TREE_CODE (decl) == FIELD_DECL
23656 && !DECL_C_BIT_FIELD (decl)
23657 && DECL_INITIAL (decl))
23658 /* Add DECL to the queue of NSDMI to be parsed later. */
23659 vec_safe_push (unparsed_nsdmis, decl);
23660 }
23661
23662 if (assume_semicolon)
23663 goto out;
23664 }
23665 }
23666
23667 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23668 out:
23669 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23670 }
23671
23672 /* Parse a pure-specifier.
23673
23674 pure-specifier:
23675 = 0
23676
23677 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23678 Otherwise, ERROR_MARK_NODE is returned. */
23679
23680 static tree
23681 cp_parser_pure_specifier (cp_parser* parser)
23682 {
23683 cp_token *token;
23684
23685 /* Look for the `=' token. */
23686 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23687 return error_mark_node;
23688 /* Look for the `0' token. */
23689 token = cp_lexer_peek_token (parser->lexer);
23690
23691 if (token->type == CPP_EOF
23692 || token->type == CPP_PRAGMA_EOL)
23693 return error_mark_node;
23694
23695 cp_lexer_consume_token (parser->lexer);
23696
23697 /* Accept = default or = delete in c++0x mode. */
23698 if (token->keyword == RID_DEFAULT
23699 || token->keyword == RID_DELETE)
23700 {
23701 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23702 return token->u.value;
23703 }
23704
23705 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23706 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23707 {
23708 cp_parser_error (parser,
23709 "invalid pure specifier (only %<= 0%> is allowed)");
23710 cp_parser_skip_to_end_of_statement (parser);
23711 return error_mark_node;
23712 }
23713 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23714 {
23715 error_at (token->location, "templates may not be %<virtual%>");
23716 return error_mark_node;
23717 }
23718
23719 return integer_zero_node;
23720 }
23721
23722 /* Parse a constant-initializer.
23723
23724 constant-initializer:
23725 = constant-expression
23726
23727 Returns a representation of the constant-expression. */
23728
23729 static tree
23730 cp_parser_constant_initializer (cp_parser* parser)
23731 {
23732 /* Look for the `=' token. */
23733 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23734 return error_mark_node;
23735
23736 /* It is invalid to write:
23737
23738 struct S { static const int i = { 7 }; };
23739
23740 */
23741 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23742 {
23743 cp_parser_error (parser,
23744 "a brace-enclosed initializer is not allowed here");
23745 /* Consume the opening brace. */
23746 cp_lexer_consume_token (parser->lexer);
23747 /* Skip the initializer. */
23748 cp_parser_skip_to_closing_brace (parser);
23749 /* Look for the trailing `}'. */
23750 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23751
23752 return error_mark_node;
23753 }
23754
23755 return cp_parser_constant_expression (parser);
23756 }
23757
23758 /* Derived classes [gram.class.derived] */
23759
23760 /* Parse a base-clause.
23761
23762 base-clause:
23763 : base-specifier-list
23764
23765 base-specifier-list:
23766 base-specifier ... [opt]
23767 base-specifier-list , base-specifier ... [opt]
23768
23769 Returns a TREE_LIST representing the base-classes, in the order in
23770 which they were declared. The representation of each node is as
23771 described by cp_parser_base_specifier.
23772
23773 In the case that no bases are specified, this function will return
23774 NULL_TREE, not ERROR_MARK_NODE. */
23775
23776 static tree
23777 cp_parser_base_clause (cp_parser* parser)
23778 {
23779 tree bases = NULL_TREE;
23780
23781 /* Look for the `:' that begins the list. */
23782 cp_parser_require (parser, CPP_COLON, RT_COLON);
23783
23784 /* Scan the base-specifier-list. */
23785 while (true)
23786 {
23787 cp_token *token;
23788 tree base;
23789 bool pack_expansion_p = false;
23790
23791 /* Look for the base-specifier. */
23792 base = cp_parser_base_specifier (parser);
23793 /* Look for the (optional) ellipsis. */
23794 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23795 {
23796 /* Consume the `...'. */
23797 cp_lexer_consume_token (parser->lexer);
23798
23799 pack_expansion_p = true;
23800 }
23801
23802 /* Add BASE to the front of the list. */
23803 if (base && base != error_mark_node)
23804 {
23805 if (pack_expansion_p)
23806 /* Make this a pack expansion type. */
23807 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23808
23809 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23810 {
23811 TREE_CHAIN (base) = bases;
23812 bases = base;
23813 }
23814 }
23815 /* Peek at the next token. */
23816 token = cp_lexer_peek_token (parser->lexer);
23817 /* If it's not a comma, then the list is complete. */
23818 if (token->type != CPP_COMMA)
23819 break;
23820 /* Consume the `,'. */
23821 cp_lexer_consume_token (parser->lexer);
23822 }
23823
23824 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23825 base class had a qualified name. However, the next name that
23826 appears is certainly not qualified. */
23827 parser->scope = NULL_TREE;
23828 parser->qualifying_scope = NULL_TREE;
23829 parser->object_scope = NULL_TREE;
23830
23831 return nreverse (bases);
23832 }
23833
23834 /* Parse a base-specifier.
23835
23836 base-specifier:
23837 :: [opt] nested-name-specifier [opt] class-name
23838 virtual access-specifier [opt] :: [opt] nested-name-specifier
23839 [opt] class-name
23840 access-specifier virtual [opt] :: [opt] nested-name-specifier
23841 [opt] class-name
23842
23843 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23844 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23845 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23846 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23847
23848 static tree
23849 cp_parser_base_specifier (cp_parser* parser)
23850 {
23851 cp_token *token;
23852 bool done = false;
23853 bool virtual_p = false;
23854 bool duplicate_virtual_error_issued_p = false;
23855 bool duplicate_access_error_issued_p = false;
23856 bool class_scope_p, template_p;
23857 tree access = access_default_node;
23858 tree type;
23859
23860 /* Process the optional `virtual' and `access-specifier'. */
23861 while (!done)
23862 {
23863 /* Peek at the next token. */
23864 token = cp_lexer_peek_token (parser->lexer);
23865 /* Process `virtual'. */
23866 switch (token->keyword)
23867 {
23868 case RID_VIRTUAL:
23869 /* If `virtual' appears more than once, issue an error. */
23870 if (virtual_p && !duplicate_virtual_error_issued_p)
23871 {
23872 cp_parser_error (parser,
23873 "%<virtual%> specified more than once in base-specifier");
23874 duplicate_virtual_error_issued_p = true;
23875 }
23876
23877 virtual_p = true;
23878
23879 /* Consume the `virtual' token. */
23880 cp_lexer_consume_token (parser->lexer);
23881
23882 break;
23883
23884 case RID_PUBLIC:
23885 case RID_PROTECTED:
23886 case RID_PRIVATE:
23887 /* If more than one access specifier appears, issue an
23888 error. */
23889 if (access != access_default_node
23890 && !duplicate_access_error_issued_p)
23891 {
23892 cp_parser_error (parser,
23893 "more than one access specifier in base-specifier");
23894 duplicate_access_error_issued_p = true;
23895 }
23896
23897 access = ridpointers[(int) token->keyword];
23898
23899 /* Consume the access-specifier. */
23900 cp_lexer_consume_token (parser->lexer);
23901
23902 break;
23903
23904 default:
23905 done = true;
23906 break;
23907 }
23908 }
23909 /* It is not uncommon to see programs mechanically, erroneously, use
23910 the 'typename' keyword to denote (dependent) qualified types
23911 as base classes. */
23912 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23913 {
23914 token = cp_lexer_peek_token (parser->lexer);
23915 if (!processing_template_decl)
23916 error_at (token->location,
23917 "keyword %<typename%> not allowed outside of templates");
23918 else
23919 error_at (token->location,
23920 "keyword %<typename%> not allowed in this context "
23921 "(the base class is implicitly a type)");
23922 cp_lexer_consume_token (parser->lexer);
23923 }
23924
23925 /* Look for the optional `::' operator. */
23926 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23927 /* Look for the nested-name-specifier. The simplest way to
23928 implement:
23929
23930 [temp.res]
23931
23932 The keyword `typename' is not permitted in a base-specifier or
23933 mem-initializer; in these contexts a qualified name that
23934 depends on a template-parameter is implicitly assumed to be a
23935 type name.
23936
23937 is to pretend that we have seen the `typename' keyword at this
23938 point. */
23939 cp_parser_nested_name_specifier_opt (parser,
23940 /*typename_keyword_p=*/true,
23941 /*check_dependency_p=*/true,
23942 /*type_p=*/true,
23943 /*is_declaration=*/true);
23944 /* If the base class is given by a qualified name, assume that names
23945 we see are type names or templates, as appropriate. */
23946 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23947 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23948
23949 if (!parser->scope
23950 && cp_lexer_next_token_is_decltype (parser->lexer))
23951 /* DR 950 allows decltype as a base-specifier. */
23952 type = cp_parser_decltype (parser);
23953 else
23954 {
23955 /* Otherwise, look for the class-name. */
23956 type = cp_parser_class_name (parser,
23957 class_scope_p,
23958 template_p,
23959 typename_type,
23960 /*check_dependency_p=*/true,
23961 /*class_head_p=*/false,
23962 /*is_declaration=*/true);
23963 type = TREE_TYPE (type);
23964 }
23965
23966 if (type == error_mark_node)
23967 return error_mark_node;
23968
23969 return finish_base_specifier (type, access, virtual_p);
23970 }
23971
23972 /* Exception handling [gram.exception] */
23973
23974 /* Parse an (optional) noexcept-specification.
23975
23976 noexcept-specification:
23977 noexcept ( constant-expression ) [opt]
23978
23979 If no noexcept-specification is present, returns NULL_TREE.
23980 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23981 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23982 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23983 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23984 in which case a boolean condition is returned instead. */
23985
23986 static tree
23987 cp_parser_noexcept_specification_opt (cp_parser* parser,
23988 bool require_constexpr,
23989 bool* consumed_expr,
23990 bool return_cond)
23991 {
23992 cp_token *token;
23993 const char *saved_message;
23994
23995 /* Peek at the next token. */
23996 token = cp_lexer_peek_token (parser->lexer);
23997
23998 /* Is it a noexcept-specification? */
23999 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24000 {
24001 tree expr;
24002 cp_lexer_consume_token (parser->lexer);
24003
24004 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24005 {
24006 cp_lexer_consume_token (parser->lexer);
24007
24008 if (require_constexpr)
24009 {
24010 /* Types may not be defined in an exception-specification. */
24011 saved_message = parser->type_definition_forbidden_message;
24012 parser->type_definition_forbidden_message
24013 = G_("types may not be defined in an exception-specification");
24014
24015 expr = cp_parser_constant_expression (parser);
24016
24017 /* Restore the saved message. */
24018 parser->type_definition_forbidden_message = saved_message;
24019 }
24020 else
24021 {
24022 expr = cp_parser_expression (parser);
24023 *consumed_expr = true;
24024 }
24025
24026 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24027 }
24028 else
24029 {
24030 expr = boolean_true_node;
24031 if (!require_constexpr)
24032 *consumed_expr = false;
24033 }
24034
24035 /* We cannot build a noexcept-spec right away because this will check
24036 that expr is a constexpr. */
24037 if (!return_cond)
24038 return build_noexcept_spec (expr, tf_warning_or_error);
24039 else
24040 return expr;
24041 }
24042 else
24043 return NULL_TREE;
24044 }
24045
24046 /* Parse an (optional) exception-specification.
24047
24048 exception-specification:
24049 throw ( type-id-list [opt] )
24050
24051 Returns a TREE_LIST representing the exception-specification. The
24052 TREE_VALUE of each node is a type. */
24053
24054 static tree
24055 cp_parser_exception_specification_opt (cp_parser* parser)
24056 {
24057 cp_token *token;
24058 tree type_id_list;
24059 const char *saved_message;
24060
24061 /* Peek at the next token. */
24062 token = cp_lexer_peek_token (parser->lexer);
24063
24064 /* Is it a noexcept-specification? */
24065 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24066 false);
24067 if (type_id_list != NULL_TREE)
24068 return type_id_list;
24069
24070 /* If it's not `throw', then there's no exception-specification. */
24071 if (!cp_parser_is_keyword (token, RID_THROW))
24072 return NULL_TREE;
24073
24074 location_t loc = token->location;
24075
24076 /* Consume the `throw'. */
24077 cp_lexer_consume_token (parser->lexer);
24078
24079 /* Look for the `('. */
24080 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24081
24082 /* Peek at the next token. */
24083 token = cp_lexer_peek_token (parser->lexer);
24084 /* If it's not a `)', then there is a type-id-list. */
24085 if (token->type != CPP_CLOSE_PAREN)
24086 {
24087 /* Types may not be defined in an exception-specification. */
24088 saved_message = parser->type_definition_forbidden_message;
24089 parser->type_definition_forbidden_message
24090 = G_("types may not be defined in an exception-specification");
24091 /* Parse the type-id-list. */
24092 type_id_list = cp_parser_type_id_list (parser);
24093 /* Restore the saved message. */
24094 parser->type_definition_forbidden_message = saved_message;
24095
24096 if (cxx_dialect >= cxx1z)
24097 {
24098 error_at (loc, "ISO C++1z does not allow dynamic exception "
24099 "specifications");
24100 type_id_list = NULL_TREE;
24101 }
24102 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24103 warning_at (loc, OPT_Wdeprecated,
24104 "dynamic exception specifications are deprecated in "
24105 "C++11");
24106 }
24107 /* In C++17, throw() is equivalent to noexcept (true). throw()
24108 is deprecated in C++11 and above as well, but is still widely used,
24109 so don't warn about it yet. */
24110 else if (cxx_dialect >= cxx1z)
24111 type_id_list = noexcept_true_spec;
24112 else
24113 type_id_list = empty_except_spec;
24114
24115 /* Look for the `)'. */
24116 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24117
24118 return type_id_list;
24119 }
24120
24121 /* Parse an (optional) type-id-list.
24122
24123 type-id-list:
24124 type-id ... [opt]
24125 type-id-list , type-id ... [opt]
24126
24127 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24128 in the order that the types were presented. */
24129
24130 static tree
24131 cp_parser_type_id_list (cp_parser* parser)
24132 {
24133 tree types = NULL_TREE;
24134
24135 while (true)
24136 {
24137 cp_token *token;
24138 tree type;
24139
24140 token = cp_lexer_peek_token (parser->lexer);
24141
24142 /* Get the next type-id. */
24143 type = cp_parser_type_id (parser);
24144 /* Check for invalid 'auto'. */
24145 if (flag_concepts && type_uses_auto (type))
24146 {
24147 error_at (token->location,
24148 "invalid use of %<auto%> in exception-specification");
24149 type = error_mark_node;
24150 }
24151 /* Parse the optional ellipsis. */
24152 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24153 {
24154 /* Consume the `...'. */
24155 cp_lexer_consume_token (parser->lexer);
24156
24157 /* Turn the type into a pack expansion expression. */
24158 type = make_pack_expansion (type);
24159 }
24160 /* Add it to the list. */
24161 types = add_exception_specifier (types, type, /*complain=*/1);
24162 /* Peek at the next token. */
24163 token = cp_lexer_peek_token (parser->lexer);
24164 /* If it is not a `,', we are done. */
24165 if (token->type != CPP_COMMA)
24166 break;
24167 /* Consume the `,'. */
24168 cp_lexer_consume_token (parser->lexer);
24169 }
24170
24171 return nreverse (types);
24172 }
24173
24174 /* Parse a try-block.
24175
24176 try-block:
24177 try compound-statement handler-seq */
24178
24179 static tree
24180 cp_parser_try_block (cp_parser* parser)
24181 {
24182 tree try_block;
24183
24184 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24185 if (parser->in_function_body
24186 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24187 error ("%<try%> in %<constexpr%> function");
24188
24189 try_block = begin_try_block ();
24190 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24191 finish_try_block (try_block);
24192 cp_parser_handler_seq (parser);
24193 finish_handler_sequence (try_block);
24194
24195 return try_block;
24196 }
24197
24198 /* Parse a function-try-block.
24199
24200 function-try-block:
24201 try ctor-initializer [opt] function-body handler-seq */
24202
24203 static bool
24204 cp_parser_function_try_block (cp_parser* parser)
24205 {
24206 tree compound_stmt;
24207 tree try_block;
24208 bool ctor_initializer_p;
24209
24210 /* Look for the `try' keyword. */
24211 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24212 return false;
24213 /* Let the rest of the front end know where we are. */
24214 try_block = begin_function_try_block (&compound_stmt);
24215 /* Parse the function-body. */
24216 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24217 (parser, /*in_function_try_block=*/true);
24218 /* We're done with the `try' part. */
24219 finish_function_try_block (try_block);
24220 /* Parse the handlers. */
24221 cp_parser_handler_seq (parser);
24222 /* We're done with the handlers. */
24223 finish_function_handler_sequence (try_block, compound_stmt);
24224
24225 return ctor_initializer_p;
24226 }
24227
24228 /* Parse a handler-seq.
24229
24230 handler-seq:
24231 handler handler-seq [opt] */
24232
24233 static void
24234 cp_parser_handler_seq (cp_parser* parser)
24235 {
24236 while (true)
24237 {
24238 cp_token *token;
24239
24240 /* Parse the handler. */
24241 cp_parser_handler (parser);
24242 /* Peek at the next token. */
24243 token = cp_lexer_peek_token (parser->lexer);
24244 /* If it's not `catch' then there are no more handlers. */
24245 if (!cp_parser_is_keyword (token, RID_CATCH))
24246 break;
24247 }
24248 }
24249
24250 /* Parse a handler.
24251
24252 handler:
24253 catch ( exception-declaration ) compound-statement */
24254
24255 static void
24256 cp_parser_handler (cp_parser* parser)
24257 {
24258 tree handler;
24259 tree declaration;
24260
24261 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24262 handler = begin_handler ();
24263 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24264 declaration = cp_parser_exception_declaration (parser);
24265 finish_handler_parms (declaration, handler);
24266 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24267 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24268 finish_handler (handler);
24269 }
24270
24271 /* Parse an exception-declaration.
24272
24273 exception-declaration:
24274 type-specifier-seq declarator
24275 type-specifier-seq abstract-declarator
24276 type-specifier-seq
24277 ...
24278
24279 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24280 ellipsis variant is used. */
24281
24282 static tree
24283 cp_parser_exception_declaration (cp_parser* parser)
24284 {
24285 cp_decl_specifier_seq type_specifiers;
24286 cp_declarator *declarator;
24287 const char *saved_message;
24288
24289 /* If it's an ellipsis, it's easy to handle. */
24290 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24291 {
24292 /* Consume the `...' token. */
24293 cp_lexer_consume_token (parser->lexer);
24294 return NULL_TREE;
24295 }
24296
24297 /* Types may not be defined in exception-declarations. */
24298 saved_message = parser->type_definition_forbidden_message;
24299 parser->type_definition_forbidden_message
24300 = G_("types may not be defined in exception-declarations");
24301
24302 /* Parse the type-specifier-seq. */
24303 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24304 /*is_trailing_return=*/false,
24305 &type_specifiers);
24306 /* If it's a `)', then there is no declarator. */
24307 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24308 declarator = NULL;
24309 else
24310 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24311 /*ctor_dtor_or_conv_p=*/NULL,
24312 /*parenthesized_p=*/NULL,
24313 /*member_p=*/false,
24314 /*friend_p=*/false);
24315
24316 /* Restore the saved message. */
24317 parser->type_definition_forbidden_message = saved_message;
24318
24319 if (!type_specifiers.any_specifiers_p)
24320 return error_mark_node;
24321
24322 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24323 }
24324
24325 /* Parse a throw-expression.
24326
24327 throw-expression:
24328 throw assignment-expression [opt]
24329
24330 Returns a THROW_EXPR representing the throw-expression. */
24331
24332 static tree
24333 cp_parser_throw_expression (cp_parser* parser)
24334 {
24335 tree expression;
24336 cp_token* token;
24337
24338 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24339 token = cp_lexer_peek_token (parser->lexer);
24340 /* Figure out whether or not there is an assignment-expression
24341 following the "throw" keyword. */
24342 if (token->type == CPP_COMMA
24343 || token->type == CPP_SEMICOLON
24344 || token->type == CPP_CLOSE_PAREN
24345 || token->type == CPP_CLOSE_SQUARE
24346 || token->type == CPP_CLOSE_BRACE
24347 || token->type == CPP_COLON)
24348 expression = NULL_TREE;
24349 else
24350 expression = cp_parser_assignment_expression (parser);
24351
24352 return build_throw (expression);
24353 }
24354
24355 /* GNU Extensions */
24356
24357 /* Parse an (optional) asm-specification.
24358
24359 asm-specification:
24360 asm ( string-literal )
24361
24362 If the asm-specification is present, returns a STRING_CST
24363 corresponding to the string-literal. Otherwise, returns
24364 NULL_TREE. */
24365
24366 static tree
24367 cp_parser_asm_specification_opt (cp_parser* parser)
24368 {
24369 cp_token *token;
24370 tree asm_specification;
24371
24372 /* Peek at the next token. */
24373 token = cp_lexer_peek_token (parser->lexer);
24374 /* If the next token isn't the `asm' keyword, then there's no
24375 asm-specification. */
24376 if (!cp_parser_is_keyword (token, RID_ASM))
24377 return NULL_TREE;
24378
24379 /* Consume the `asm' token. */
24380 cp_lexer_consume_token (parser->lexer);
24381 /* Look for the `('. */
24382 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24383
24384 /* Look for the string-literal. */
24385 asm_specification = cp_parser_string_literal (parser, false, false);
24386
24387 /* Look for the `)'. */
24388 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24389
24390 return asm_specification;
24391 }
24392
24393 /* Parse an asm-operand-list.
24394
24395 asm-operand-list:
24396 asm-operand
24397 asm-operand-list , asm-operand
24398
24399 asm-operand:
24400 string-literal ( expression )
24401 [ string-literal ] string-literal ( expression )
24402
24403 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24404 each node is the expression. The TREE_PURPOSE is itself a
24405 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24406 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24407 is a STRING_CST for the string literal before the parenthesis. Returns
24408 ERROR_MARK_NODE if any of the operands are invalid. */
24409
24410 static tree
24411 cp_parser_asm_operand_list (cp_parser* parser)
24412 {
24413 tree asm_operands = NULL_TREE;
24414 bool invalid_operands = false;
24415
24416 while (true)
24417 {
24418 tree string_literal;
24419 tree expression;
24420 tree name;
24421
24422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24423 {
24424 /* Consume the `[' token. */
24425 cp_lexer_consume_token (parser->lexer);
24426 /* Read the operand name. */
24427 name = cp_parser_identifier (parser);
24428 if (name != error_mark_node)
24429 name = build_string (IDENTIFIER_LENGTH (name),
24430 IDENTIFIER_POINTER (name));
24431 /* Look for the closing `]'. */
24432 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24433 }
24434 else
24435 name = NULL_TREE;
24436 /* Look for the string-literal. */
24437 string_literal = cp_parser_string_literal (parser, false, false);
24438
24439 /* Look for the `('. */
24440 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24441 /* Parse the expression. */
24442 expression = cp_parser_expression (parser);
24443 /* Look for the `)'. */
24444 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24445
24446 if (name == error_mark_node
24447 || string_literal == error_mark_node
24448 || expression == error_mark_node)
24449 invalid_operands = true;
24450
24451 /* Add this operand to the list. */
24452 asm_operands = tree_cons (build_tree_list (name, string_literal),
24453 expression,
24454 asm_operands);
24455 /* If the next token is not a `,', there are no more
24456 operands. */
24457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24458 break;
24459 /* Consume the `,'. */
24460 cp_lexer_consume_token (parser->lexer);
24461 }
24462
24463 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24464 }
24465
24466 /* Parse an asm-clobber-list.
24467
24468 asm-clobber-list:
24469 string-literal
24470 asm-clobber-list , string-literal
24471
24472 Returns a TREE_LIST, indicating the clobbers in the order that they
24473 appeared. The TREE_VALUE of each node is a STRING_CST. */
24474
24475 static tree
24476 cp_parser_asm_clobber_list (cp_parser* parser)
24477 {
24478 tree clobbers = NULL_TREE;
24479
24480 while (true)
24481 {
24482 tree string_literal;
24483
24484 /* Look for the string literal. */
24485 string_literal = cp_parser_string_literal (parser, false, false);
24486 /* Add it to the list. */
24487 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24488 /* If the next token is not a `,', then the list is
24489 complete. */
24490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24491 break;
24492 /* Consume the `,' token. */
24493 cp_lexer_consume_token (parser->lexer);
24494 }
24495
24496 return clobbers;
24497 }
24498
24499 /* Parse an asm-label-list.
24500
24501 asm-label-list:
24502 identifier
24503 asm-label-list , identifier
24504
24505 Returns a TREE_LIST, indicating the labels in the order that they
24506 appeared. The TREE_VALUE of each node is a label. */
24507
24508 static tree
24509 cp_parser_asm_label_list (cp_parser* parser)
24510 {
24511 tree labels = NULL_TREE;
24512
24513 while (true)
24514 {
24515 tree identifier, label, name;
24516
24517 /* Look for the identifier. */
24518 identifier = cp_parser_identifier (parser);
24519 if (!error_operand_p (identifier))
24520 {
24521 label = lookup_label (identifier);
24522 if (TREE_CODE (label) == LABEL_DECL)
24523 {
24524 TREE_USED (label) = 1;
24525 check_goto (label);
24526 name = build_string (IDENTIFIER_LENGTH (identifier),
24527 IDENTIFIER_POINTER (identifier));
24528 labels = tree_cons (name, label, labels);
24529 }
24530 }
24531 /* If the next token is not a `,', then the list is
24532 complete. */
24533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24534 break;
24535 /* Consume the `,' token. */
24536 cp_lexer_consume_token (parser->lexer);
24537 }
24538
24539 return nreverse (labels);
24540 }
24541
24542 /* Return TRUE iff the next tokens in the stream are possibly the
24543 beginning of a GNU extension attribute. */
24544
24545 static bool
24546 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24547 {
24548 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24549 }
24550
24551 /* Return TRUE iff the next tokens in the stream are possibly the
24552 beginning of a standard C++-11 attribute specifier. */
24553
24554 static bool
24555 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24556 {
24557 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24558 }
24559
24560 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24561 beginning of a standard C++-11 attribute specifier. */
24562
24563 static bool
24564 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24565 {
24566 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24567
24568 return (cxx_dialect >= cxx11
24569 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24570 || (token->type == CPP_OPEN_SQUARE
24571 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24572 && token->type == CPP_OPEN_SQUARE)));
24573 }
24574
24575 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24576 beginning of a GNU extension attribute. */
24577
24578 static bool
24579 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24580 {
24581 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24582
24583 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24584 }
24585
24586 /* Return true iff the next tokens can be the beginning of either a
24587 GNU attribute list, or a standard C++11 attribute sequence. */
24588
24589 static bool
24590 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24591 {
24592 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24593 || cp_next_tokens_can_be_std_attribute_p (parser));
24594 }
24595
24596 /* Return true iff the next Nth tokens can be the beginning of either
24597 a GNU attribute list, or a standard C++11 attribute sequence. */
24598
24599 static bool
24600 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24601 {
24602 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24603 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24604 }
24605
24606 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24607 of GNU attributes, or return NULL. */
24608
24609 static tree
24610 cp_parser_attributes_opt (cp_parser *parser)
24611 {
24612 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24613 return cp_parser_gnu_attributes_opt (parser);
24614 return cp_parser_std_attribute_spec_seq (parser);
24615 }
24616
24617 #define CILK_SIMD_FN_CLAUSE_MASK \
24618 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24619 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24620 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24621 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24622 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24623
24624 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24625 vector [(<clauses>)] */
24626
24627 static void
24628 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24629 {
24630 bool first_p = parser->cilk_simd_fn_info == NULL;
24631 cp_token *token = v_token;
24632 if (first_p)
24633 {
24634 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24635 parser->cilk_simd_fn_info->error_seen = false;
24636 parser->cilk_simd_fn_info->fndecl_seen = false;
24637 parser->cilk_simd_fn_info->tokens = vNULL;
24638 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24639 }
24640 int paren_scope = 0;
24641 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24642 {
24643 cp_lexer_consume_token (parser->lexer);
24644 v_token = cp_lexer_peek_token (parser->lexer);
24645 paren_scope++;
24646 }
24647 while (paren_scope > 0)
24648 {
24649 token = cp_lexer_peek_token (parser->lexer);
24650 if (token->type == CPP_OPEN_PAREN)
24651 paren_scope++;
24652 else if (token->type == CPP_CLOSE_PAREN)
24653 paren_scope--;
24654 /* Do not push the last ')' */
24655 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24656 cp_lexer_consume_token (parser->lexer);
24657 }
24658
24659 token->type = CPP_PRAGMA_EOL;
24660 parser->lexer->next_token = token;
24661 cp_lexer_consume_token (parser->lexer);
24662
24663 struct cp_token_cache *cp
24664 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24665 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24666 }
24667
24668 /* Parse an (optional) series of attributes.
24669
24670 attributes:
24671 attributes attribute
24672
24673 attribute:
24674 __attribute__ (( attribute-list [opt] ))
24675
24676 The return value is as for cp_parser_gnu_attribute_list. */
24677
24678 static tree
24679 cp_parser_gnu_attributes_opt (cp_parser* parser)
24680 {
24681 tree attributes = NULL_TREE;
24682
24683 while (true)
24684 {
24685 cp_token *token;
24686 tree attribute_list;
24687 bool ok = true;
24688
24689 /* Peek at the next token. */
24690 token = cp_lexer_peek_token (parser->lexer);
24691 /* If it's not `__attribute__', then we're done. */
24692 if (token->keyword != RID_ATTRIBUTE)
24693 break;
24694
24695 /* Consume the `__attribute__' keyword. */
24696 cp_lexer_consume_token (parser->lexer);
24697 /* Look for the two `(' tokens. */
24698 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24699 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24700
24701 /* Peek at the next token. */
24702 token = cp_lexer_peek_token (parser->lexer);
24703 if (token->type != CPP_CLOSE_PAREN)
24704 /* Parse the attribute-list. */
24705 attribute_list = cp_parser_gnu_attribute_list (parser);
24706 else
24707 /* If the next token is a `)', then there is no attribute
24708 list. */
24709 attribute_list = NULL;
24710
24711 /* Look for the two `)' tokens. */
24712 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24713 ok = false;
24714 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24715 ok = false;
24716 if (!ok)
24717 cp_parser_skip_to_end_of_statement (parser);
24718
24719 /* Add these new attributes to the list. */
24720 attributes = chainon (attributes, attribute_list);
24721 }
24722
24723 return attributes;
24724 }
24725
24726 /* Parse a GNU attribute-list.
24727
24728 attribute-list:
24729 attribute
24730 attribute-list , attribute
24731
24732 attribute:
24733 identifier
24734 identifier ( identifier )
24735 identifier ( identifier , expression-list )
24736 identifier ( expression-list )
24737
24738 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24739 to an attribute. The TREE_PURPOSE of each node is the identifier
24740 indicating which attribute is in use. The TREE_VALUE represents
24741 the arguments, if any. */
24742
24743 static tree
24744 cp_parser_gnu_attribute_list (cp_parser* parser)
24745 {
24746 tree attribute_list = NULL_TREE;
24747 bool save_translate_strings_p = parser->translate_strings_p;
24748
24749 parser->translate_strings_p = false;
24750 while (true)
24751 {
24752 cp_token *token;
24753 tree identifier;
24754 tree attribute;
24755
24756 /* Look for the identifier. We also allow keywords here; for
24757 example `__attribute__ ((const))' is legal. */
24758 token = cp_lexer_peek_token (parser->lexer);
24759 if (token->type == CPP_NAME
24760 || token->type == CPP_KEYWORD)
24761 {
24762 tree arguments = NULL_TREE;
24763
24764 /* Consume the token, but save it since we need it for the
24765 SIMD enabled function parsing. */
24766 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24767
24768 /* Save away the identifier that indicates which attribute
24769 this is. */
24770 identifier = (token->type == CPP_KEYWORD)
24771 /* For keywords, use the canonical spelling, not the
24772 parsed identifier. */
24773 ? ridpointers[(int) token->keyword]
24774 : id_token->u.value;
24775
24776 attribute = build_tree_list (identifier, NULL_TREE);
24777
24778 /* Peek at the next token. */
24779 token = cp_lexer_peek_token (parser->lexer);
24780 /* If it's an `(', then parse the attribute arguments. */
24781 if (token->type == CPP_OPEN_PAREN)
24782 {
24783 vec<tree, va_gc> *vec;
24784 int attr_flag = (attribute_takes_identifier_p (identifier)
24785 ? id_attr : normal_attr);
24786 if (is_cilkplus_vector_p (identifier))
24787 {
24788 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24789 continue;
24790 }
24791 else
24792 vec = cp_parser_parenthesized_expression_list
24793 (parser, attr_flag, /*cast_p=*/false,
24794 /*allow_expansion_p=*/false,
24795 /*non_constant_p=*/NULL);
24796 if (vec == NULL)
24797 arguments = error_mark_node;
24798 else
24799 {
24800 arguments = build_tree_list_vec (vec);
24801 release_tree_vector (vec);
24802 }
24803 /* Save the arguments away. */
24804 TREE_VALUE (attribute) = arguments;
24805 }
24806 else if (is_cilkplus_vector_p (identifier))
24807 {
24808 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24809 continue;
24810 }
24811
24812 if (arguments != error_mark_node)
24813 {
24814 /* Add this attribute to the list. */
24815 TREE_CHAIN (attribute) = attribute_list;
24816 attribute_list = attribute;
24817 }
24818
24819 token = cp_lexer_peek_token (parser->lexer);
24820 }
24821 /* Now, look for more attributes. If the next token isn't a
24822 `,', we're done. */
24823 if (token->type != CPP_COMMA)
24824 break;
24825
24826 /* Consume the comma and keep going. */
24827 cp_lexer_consume_token (parser->lexer);
24828 }
24829 parser->translate_strings_p = save_translate_strings_p;
24830
24831 /* We built up the list in reverse order. */
24832 return nreverse (attribute_list);
24833 }
24834
24835 /* Parse a standard C++11 attribute.
24836
24837 The returned representation is a TREE_LIST which TREE_PURPOSE is
24838 the scoped name of the attribute, and the TREE_VALUE is its
24839 arguments list.
24840
24841 Note that the scoped name of the attribute is itself a TREE_LIST
24842 which TREE_PURPOSE is the namespace of the attribute, and
24843 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24844 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24845 and which TREE_PURPOSE is directly the attribute name.
24846
24847 Clients of the attribute code should use get_attribute_namespace
24848 and get_attribute_name to get the actual namespace and name of
24849 attributes, regardless of their being GNU or C++11 attributes.
24850
24851 attribute:
24852 attribute-token attribute-argument-clause [opt]
24853
24854 attribute-token:
24855 identifier
24856 attribute-scoped-token
24857
24858 attribute-scoped-token:
24859 attribute-namespace :: identifier
24860
24861 attribute-namespace:
24862 identifier
24863
24864 attribute-argument-clause:
24865 ( balanced-token-seq )
24866
24867 balanced-token-seq:
24868 balanced-token [opt]
24869 balanced-token-seq balanced-token
24870
24871 balanced-token:
24872 ( balanced-token-seq )
24873 [ balanced-token-seq ]
24874 { balanced-token-seq }. */
24875
24876 static tree
24877 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24878 {
24879 tree attribute, attr_id = NULL_TREE, arguments;
24880 cp_token *token;
24881
24882 /* First, parse name of the attribute, a.k.a attribute-token. */
24883
24884 token = cp_lexer_peek_token (parser->lexer);
24885 if (token->type == CPP_NAME)
24886 attr_id = token->u.value;
24887 else if (token->type == CPP_KEYWORD)
24888 attr_id = ridpointers[(int) token->keyword];
24889 else if (token->flags & NAMED_OP)
24890 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24891
24892 if (attr_id == NULL_TREE)
24893 return NULL_TREE;
24894
24895 cp_lexer_consume_token (parser->lexer);
24896
24897 token = cp_lexer_peek_token (parser->lexer);
24898 if (token->type == CPP_SCOPE)
24899 {
24900 /* We are seeing a scoped attribute token. */
24901
24902 cp_lexer_consume_token (parser->lexer);
24903 if (attr_ns)
24904 error_at (token->location, "attribute using prefix used together "
24905 "with scoped attribute token");
24906 attr_ns = attr_id;
24907
24908 token = cp_lexer_consume_token (parser->lexer);
24909 if (token->type == CPP_NAME)
24910 attr_id = token->u.value;
24911 else if (token->type == CPP_KEYWORD)
24912 attr_id = ridpointers[(int) token->keyword];
24913 else if (token->flags & NAMED_OP)
24914 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24915 else
24916 {
24917 error_at (token->location,
24918 "expected an identifier for the attribute name");
24919 return error_mark_node;
24920 }
24921 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24922 NULL_TREE);
24923 token = cp_lexer_peek_token (parser->lexer);
24924 }
24925 else if (attr_ns)
24926 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24927 NULL_TREE);
24928 else
24929 {
24930 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24931 NULL_TREE);
24932 /* C++11 noreturn attribute is equivalent to GNU's. */
24933 if (is_attribute_p ("noreturn", attr_id))
24934 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24935 /* C++14 deprecated attribute is equivalent to GNU's. */
24936 else if (is_attribute_p ("deprecated", attr_id))
24937 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24938 /* C++17 fallthrough attribute is equivalent to GNU's. */
24939 else if (is_attribute_p ("fallthrough", attr_id))
24940 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24941 /* Transactional Memory TS optimize_for_synchronized attribute is
24942 equivalent to GNU transaction_callable. */
24943 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24944 TREE_PURPOSE (attribute)
24945 = get_identifier ("transaction_callable");
24946 /* Transactional Memory attributes are GNU attributes. */
24947 else if (tm_attr_to_mask (attr_id))
24948 TREE_PURPOSE (attribute) = attr_id;
24949 }
24950
24951 /* Now parse the optional argument clause of the attribute. */
24952
24953 if (token->type != CPP_OPEN_PAREN)
24954 return attribute;
24955
24956 {
24957 vec<tree, va_gc> *vec;
24958 int attr_flag = normal_attr;
24959
24960 if (attr_ns == get_identifier ("gnu")
24961 && attribute_takes_identifier_p (attr_id))
24962 /* A GNU attribute that takes an identifier in parameter. */
24963 attr_flag = id_attr;
24964
24965 vec = cp_parser_parenthesized_expression_list
24966 (parser, attr_flag, /*cast_p=*/false,
24967 /*allow_expansion_p=*/true,
24968 /*non_constant_p=*/NULL);
24969 if (vec == NULL)
24970 arguments = error_mark_node;
24971 else
24972 {
24973 arguments = build_tree_list_vec (vec);
24974 release_tree_vector (vec);
24975 }
24976
24977 if (arguments == error_mark_node)
24978 attribute = error_mark_node;
24979 else
24980 TREE_VALUE (attribute) = arguments;
24981 }
24982
24983 return attribute;
24984 }
24985
24986 /* Check that the attribute ATTRIBUTE appears at most once in the
24987 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24988 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24989 isn't implemented yet in GCC. */
24990
24991 static void
24992 cp_parser_check_std_attribute (tree attributes, tree attribute)
24993 {
24994 if (attributes)
24995 {
24996 tree name = get_attribute_name (attribute);
24997 if (is_attribute_p ("noreturn", name)
24998 && lookup_attribute ("noreturn", attributes))
24999 error ("attribute %<noreturn%> can appear at most once "
25000 "in an attribute-list");
25001 else if (is_attribute_p ("deprecated", name)
25002 && lookup_attribute ("deprecated", attributes))
25003 error ("attribute %<deprecated%> can appear at most once "
25004 "in an attribute-list");
25005 }
25006 }
25007
25008 /* Parse a list of standard C++-11 attributes.
25009
25010 attribute-list:
25011 attribute [opt]
25012 attribute-list , attribute[opt]
25013 attribute ...
25014 attribute-list , attribute ...
25015 */
25016
25017 static tree
25018 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25019 {
25020 tree attributes = NULL_TREE, attribute = NULL_TREE;
25021 cp_token *token = NULL;
25022
25023 while (true)
25024 {
25025 attribute = cp_parser_std_attribute (parser, attr_ns);
25026 if (attribute == error_mark_node)
25027 break;
25028 if (attribute != NULL_TREE)
25029 {
25030 cp_parser_check_std_attribute (attributes, attribute);
25031 TREE_CHAIN (attribute) = attributes;
25032 attributes = attribute;
25033 }
25034 token = cp_lexer_peek_token (parser->lexer);
25035 if (token->type == CPP_ELLIPSIS)
25036 {
25037 cp_lexer_consume_token (parser->lexer);
25038 if (attribute == NULL_TREE)
25039 error_at (token->location,
25040 "expected attribute before %<...%>");
25041 else
25042 {
25043 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25044 if (pack == error_mark_node)
25045 return error_mark_node;
25046 TREE_VALUE (attribute) = pack;
25047 }
25048 token = cp_lexer_peek_token (parser->lexer);
25049 }
25050 if (token->type != CPP_COMMA)
25051 break;
25052 cp_lexer_consume_token (parser->lexer);
25053 }
25054 attributes = nreverse (attributes);
25055 return attributes;
25056 }
25057
25058 /* Parse a standard C++-11 attribute specifier.
25059
25060 attribute-specifier:
25061 [ [ attribute-using-prefix [opt] attribute-list ] ]
25062 alignment-specifier
25063
25064 attribute-using-prefix:
25065 using attribute-namespace :
25066
25067 alignment-specifier:
25068 alignas ( type-id ... [opt] )
25069 alignas ( alignment-expression ... [opt] ). */
25070
25071 static tree
25072 cp_parser_std_attribute_spec (cp_parser *parser)
25073 {
25074 tree attributes = NULL_TREE;
25075 cp_token *token = cp_lexer_peek_token (parser->lexer);
25076
25077 if (token->type == CPP_OPEN_SQUARE
25078 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25079 {
25080 tree attr_ns = NULL_TREE;
25081
25082 cp_lexer_consume_token (parser->lexer);
25083 cp_lexer_consume_token (parser->lexer);
25084
25085 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25086 {
25087 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25088 if (token->type == CPP_NAME)
25089 attr_ns = token->u.value;
25090 else if (token->type == CPP_KEYWORD)
25091 attr_ns = ridpointers[(int) token->keyword];
25092 else if (token->flags & NAMED_OP)
25093 attr_ns = get_identifier (cpp_type2name (token->type,
25094 token->flags));
25095 if (attr_ns
25096 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25097 {
25098 if (cxx_dialect < cxx1z
25099 && !in_system_header_at (input_location))
25100 pedwarn (input_location, 0,
25101 "attribute using prefix only available "
25102 "with -std=c++1z or -std=gnu++1z");
25103
25104 cp_lexer_consume_token (parser->lexer);
25105 cp_lexer_consume_token (parser->lexer);
25106 cp_lexer_consume_token (parser->lexer);
25107 }
25108 else
25109 attr_ns = NULL_TREE;
25110 }
25111
25112 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25113
25114 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25115 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25116 cp_parser_skip_to_end_of_statement (parser);
25117 else
25118 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25119 when we are sure that we have actually parsed them. */
25120 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25121 }
25122 else
25123 {
25124 tree alignas_expr;
25125
25126 /* Look for an alignment-specifier. */
25127
25128 token = cp_lexer_peek_token (parser->lexer);
25129
25130 if (token->type != CPP_KEYWORD
25131 || token->keyword != RID_ALIGNAS)
25132 return NULL_TREE;
25133
25134 cp_lexer_consume_token (parser->lexer);
25135 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25136
25137 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
25138 {
25139 cp_parser_error (parser, "expected %<(%>");
25140 return error_mark_node;
25141 }
25142
25143 cp_parser_parse_tentatively (parser);
25144 alignas_expr = cp_parser_type_id (parser);
25145
25146 if (!cp_parser_parse_definitely (parser))
25147 {
25148 alignas_expr = cp_parser_assignment_expression (parser);
25149 if (alignas_expr == error_mark_node)
25150 cp_parser_skip_to_end_of_statement (parser);
25151 if (alignas_expr == NULL_TREE
25152 || alignas_expr == error_mark_node)
25153 return alignas_expr;
25154 }
25155
25156 alignas_expr = cxx_alignas_expr (alignas_expr);
25157 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25158
25159 /* Handle alignas (pack...). */
25160 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25161 {
25162 cp_lexer_consume_token (parser->lexer);
25163 alignas_expr = make_pack_expansion (alignas_expr);
25164 }
25165
25166 /* Something went wrong, so don't build the attribute. */
25167 if (alignas_expr == error_mark_node)
25168 return error_mark_node;
25169
25170 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
25171 {
25172 cp_parser_error (parser, "expected %<)%>");
25173 return error_mark_node;
25174 }
25175
25176 /* Build the C++-11 representation of an 'aligned'
25177 attribute. */
25178 attributes =
25179 build_tree_list (build_tree_list (get_identifier ("gnu"),
25180 get_identifier ("aligned")),
25181 alignas_expr);
25182 }
25183
25184 return attributes;
25185 }
25186
25187 /* Parse a standard C++-11 attribute-specifier-seq.
25188
25189 attribute-specifier-seq:
25190 attribute-specifier-seq [opt] attribute-specifier
25191 */
25192
25193 static tree
25194 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25195 {
25196 tree attr_specs = NULL_TREE;
25197 tree attr_last = NULL_TREE;
25198
25199 while (true)
25200 {
25201 tree attr_spec = cp_parser_std_attribute_spec (parser);
25202 if (attr_spec == NULL_TREE)
25203 break;
25204 if (attr_spec == error_mark_node)
25205 return error_mark_node;
25206
25207 if (attr_last)
25208 TREE_CHAIN (attr_last) = attr_spec;
25209 else
25210 attr_specs = attr_last = attr_spec;
25211 attr_last = tree_last (attr_last);
25212 }
25213
25214 return attr_specs;
25215 }
25216
25217 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25218 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25219 current value of the PEDANTIC flag, regardless of whether or not
25220 the `__extension__' keyword is present. The caller is responsible
25221 for restoring the value of the PEDANTIC flag. */
25222
25223 static bool
25224 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25225 {
25226 /* Save the old value of the PEDANTIC flag. */
25227 *saved_pedantic = pedantic;
25228
25229 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25230 {
25231 /* Consume the `__extension__' token. */
25232 cp_lexer_consume_token (parser->lexer);
25233 /* We're not being pedantic while the `__extension__' keyword is
25234 in effect. */
25235 pedantic = 0;
25236
25237 return true;
25238 }
25239
25240 return false;
25241 }
25242
25243 /* Parse a label declaration.
25244
25245 label-declaration:
25246 __label__ label-declarator-seq ;
25247
25248 label-declarator-seq:
25249 identifier , label-declarator-seq
25250 identifier */
25251
25252 static void
25253 cp_parser_label_declaration (cp_parser* parser)
25254 {
25255 /* Look for the `__label__' keyword. */
25256 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25257
25258 while (true)
25259 {
25260 tree identifier;
25261
25262 /* Look for an identifier. */
25263 identifier = cp_parser_identifier (parser);
25264 /* If we failed, stop. */
25265 if (identifier == error_mark_node)
25266 break;
25267 /* Declare it as a label. */
25268 finish_label_decl (identifier);
25269 /* If the next token is a `;', stop. */
25270 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25271 break;
25272 /* Look for the `,' separating the label declarations. */
25273 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25274 }
25275
25276 /* Look for the final `;'. */
25277 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25278 }
25279
25280 // -------------------------------------------------------------------------- //
25281 // Requires Clause
25282
25283 // Parse a requires clause.
25284 //
25285 // requires-clause:
25286 // 'requires' logical-or-expression
25287 //
25288 // The required logical-or-expression must be a constant expression. Note
25289 // that we don't check that the expression is constepxr here. We defer until
25290 // we analyze constraints and then, we only check atomic constraints.
25291 static tree
25292 cp_parser_requires_clause (cp_parser *parser)
25293 {
25294 // Parse the requires clause so that it is not automatically folded.
25295 ++processing_template_decl;
25296 tree expr = cp_parser_binary_expression (parser, false, false,
25297 PREC_NOT_OPERATOR, NULL);
25298 if (check_for_bare_parameter_packs (expr))
25299 expr = error_mark_node;
25300 --processing_template_decl;
25301 return expr;
25302 }
25303
25304 // Optionally parse a requires clause:
25305 static tree
25306 cp_parser_requires_clause_opt (cp_parser *parser)
25307 {
25308 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25309 if (tok->keyword != RID_REQUIRES)
25310 {
25311 if (!flag_concepts && tok->type == CPP_NAME
25312 && tok->u.value == ridpointers[RID_REQUIRES])
25313 {
25314 error_at (cp_lexer_peek_token (parser->lexer)->location,
25315 "%<requires%> only available with -fconcepts");
25316 /* Parse and discard the requires-clause. */
25317 cp_lexer_consume_token (parser->lexer);
25318 cp_parser_requires_clause (parser);
25319 }
25320 return NULL_TREE;
25321 }
25322 cp_lexer_consume_token (parser->lexer);
25323 return cp_parser_requires_clause (parser);
25324 }
25325
25326
25327 /*---------------------------------------------------------------------------
25328 Requires expressions
25329 ---------------------------------------------------------------------------*/
25330
25331 /* Parse a requires expression
25332
25333 requirement-expression:
25334 'requires' requirement-parameter-list [opt] requirement-body */
25335 static tree
25336 cp_parser_requires_expression (cp_parser *parser)
25337 {
25338 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25339 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25340
25341 /* A requires-expression shall appear only within a concept
25342 definition or a requires-clause.
25343
25344 TODO: Implement this diagnostic correctly. */
25345 if (!processing_template_decl)
25346 {
25347 error_at (loc, "a requires expression cannot appear outside a template");
25348 cp_parser_skip_to_end_of_statement (parser);
25349 return error_mark_node;
25350 }
25351
25352 tree parms, reqs;
25353 {
25354 /* Local parameters are delared as variables within the scope
25355 of the expression. They are not visible past the end of
25356 the expression. Expressions within the requires-expression
25357 are unevaluated. */
25358 struct scope_sentinel
25359 {
25360 scope_sentinel ()
25361 {
25362 ++cp_unevaluated_operand;
25363 begin_scope (sk_block, NULL_TREE);
25364 }
25365
25366 ~scope_sentinel ()
25367 {
25368 pop_bindings_and_leave_scope ();
25369 --cp_unevaluated_operand;
25370 }
25371 } s;
25372
25373 /* Parse the optional parameter list. */
25374 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25375 {
25376 parms = cp_parser_requirement_parameter_list (parser);
25377 if (parms == error_mark_node)
25378 return error_mark_node;
25379 }
25380 else
25381 parms = NULL_TREE;
25382
25383 /* Parse the requirement body. */
25384 reqs = cp_parser_requirement_body (parser);
25385 if (reqs == error_mark_node)
25386 return error_mark_node;
25387 }
25388
25389 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25390 the parm chain. */
25391 grokparms (parms, &parms);
25392 return finish_requires_expr (parms, reqs);
25393 }
25394
25395 /* Parse a parameterized requirement.
25396
25397 requirement-parameter-list:
25398 '(' parameter-declaration-clause ')' */
25399 static tree
25400 cp_parser_requirement_parameter_list (cp_parser *parser)
25401 {
25402 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25403 return error_mark_node;
25404
25405 tree parms = cp_parser_parameter_declaration_clause (parser);
25406
25407 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25408 return error_mark_node;
25409
25410 return parms;
25411 }
25412
25413 /* Parse the body of a requirement.
25414
25415 requirement-body:
25416 '{' requirement-list '}' */
25417 static tree
25418 cp_parser_requirement_body (cp_parser *parser)
25419 {
25420 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25421 return error_mark_node;
25422
25423 tree reqs = cp_parser_requirement_list (parser);
25424
25425 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25426 return error_mark_node;
25427
25428 return reqs;
25429 }
25430
25431 /* Parse a list of requirements.
25432
25433 requirement-list:
25434 requirement
25435 requirement-list ';' requirement[opt] */
25436 static tree
25437 cp_parser_requirement_list (cp_parser *parser)
25438 {
25439 tree result = NULL_TREE;
25440 while (true)
25441 {
25442 tree req = cp_parser_requirement (parser);
25443 if (req == error_mark_node)
25444 return error_mark_node;
25445
25446 result = tree_cons (NULL_TREE, req, result);
25447
25448 /* If we see a semi-colon, consume it. */
25449 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25450 cp_lexer_consume_token (parser->lexer);
25451
25452 /* Stop processing at the end of the list. */
25453 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25454 break;
25455 }
25456
25457 /* Reverse the order of requirements so they are analyzed in
25458 declaration order. */
25459 return nreverse (result);
25460 }
25461
25462 /* Parse a syntactic requirement or type requirement.
25463
25464 requirement:
25465 simple-requirement
25466 compound-requirement
25467 type-requirement
25468 nested-requirement */
25469 static tree
25470 cp_parser_requirement (cp_parser *parser)
25471 {
25472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25473 return cp_parser_compound_requirement (parser);
25474 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25475 return cp_parser_type_requirement (parser);
25476 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25477 return cp_parser_nested_requirement (parser);
25478 else
25479 return cp_parser_simple_requirement (parser);
25480 }
25481
25482 /* Parse a simple requirement.
25483
25484 simple-requirement:
25485 expression ';' */
25486 static tree
25487 cp_parser_simple_requirement (cp_parser *parser)
25488 {
25489 tree expr = cp_parser_expression (parser, NULL, false, false);
25490 if (!expr || expr == error_mark_node)
25491 return error_mark_node;
25492
25493 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25494 return error_mark_node;
25495
25496 return finish_simple_requirement (expr);
25497 }
25498
25499 /* Parse a type requirement
25500
25501 type-requirement
25502 nested-name-specifier [opt] required-type-name ';'
25503
25504 required-type-name:
25505 type-name
25506 'template' [opt] simple-template-id */
25507 static tree
25508 cp_parser_type_requirement (cp_parser *parser)
25509 {
25510 cp_lexer_consume_token (parser->lexer);
25511
25512 // Save the scope before parsing name specifiers.
25513 tree saved_scope = parser->scope;
25514 tree saved_object_scope = parser->object_scope;
25515 tree saved_qualifying_scope = parser->qualifying_scope;
25516 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25517 cp_parser_nested_name_specifier_opt (parser,
25518 /*typename_keyword_p=*/true,
25519 /*check_dependency_p=*/false,
25520 /*type_p=*/true,
25521 /*is_declaration=*/false);
25522
25523 tree type;
25524 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25525 {
25526 cp_lexer_consume_token (parser->lexer);
25527 type = cp_parser_template_id (parser,
25528 /*template_keyword_p=*/true,
25529 /*check_dependency=*/false,
25530 /*tag_type=*/none_type,
25531 /*is_declaration=*/false);
25532 type = make_typename_type (parser->scope, type, typename_type,
25533 /*complain=*/tf_error);
25534 }
25535 else
25536 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25537
25538 if (TREE_CODE (type) == TYPE_DECL)
25539 type = TREE_TYPE (type);
25540
25541 parser->scope = saved_scope;
25542 parser->object_scope = saved_object_scope;
25543 parser->qualifying_scope = saved_qualifying_scope;
25544
25545 if (type == error_mark_node)
25546 cp_parser_skip_to_end_of_statement (parser);
25547
25548 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25549 return error_mark_node;
25550 if (type == error_mark_node)
25551 return error_mark_node;
25552
25553 return finish_type_requirement (type);
25554 }
25555
25556 /* Parse a compound requirement
25557
25558 compound-requirement:
25559 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25560 static tree
25561 cp_parser_compound_requirement (cp_parser *parser)
25562 {
25563 /* Parse an expression enclosed in '{ }'s. */
25564 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25565 return error_mark_node;
25566
25567 tree expr = cp_parser_expression (parser, NULL, false, false);
25568 if (!expr || expr == error_mark_node)
25569 return error_mark_node;
25570
25571 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25572 return error_mark_node;
25573
25574 /* Parse the optional noexcept. */
25575 bool noexcept_p = false;
25576 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25577 {
25578 cp_lexer_consume_token (parser->lexer);
25579 noexcept_p = true;
25580 }
25581
25582 /* Parse the optional trailing return type. */
25583 tree type = NULL_TREE;
25584 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25585 {
25586 cp_lexer_consume_token (parser->lexer);
25587 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25588 parser->in_result_type_constraint_p = true;
25589 type = cp_parser_trailing_type_id (parser);
25590 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25591 if (type == error_mark_node)
25592 return error_mark_node;
25593 }
25594
25595 return finish_compound_requirement (expr, type, noexcept_p);
25596 }
25597
25598 /* Parse a nested requirement. This is the same as a requires clause.
25599
25600 nested-requirement:
25601 requires-clause */
25602 static tree
25603 cp_parser_nested_requirement (cp_parser *parser)
25604 {
25605 cp_lexer_consume_token (parser->lexer);
25606 tree req = cp_parser_requires_clause (parser);
25607 if (req == error_mark_node)
25608 return error_mark_node;
25609 return finish_nested_requirement (req);
25610 }
25611
25612 /* Support Functions */
25613
25614 /* Return the appropriate prefer_type argument for lookup_name_real based on
25615 tag_type and template_mem_access. */
25616
25617 static inline int
25618 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25619 {
25620 /* DR 141: When looking in the current enclosing context for a template-name
25621 after -> or ., only consider class templates. */
25622 if (template_mem_access)
25623 return 2;
25624 switch (tag_type)
25625 {
25626 case none_type: return 0; // No preference.
25627 case scope_type: return 1; // Type or namespace.
25628 default: return 2; // Type only.
25629 }
25630 }
25631
25632 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25633 NAME should have one of the representations used for an
25634 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25635 is returned. If PARSER->SCOPE is a dependent type, then a
25636 SCOPE_REF is returned.
25637
25638 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25639 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25640 was formed. Abstractly, such entities should not be passed to this
25641 function, because they do not need to be looked up, but it is
25642 simpler to check for this special case here, rather than at the
25643 call-sites.
25644
25645 In cases not explicitly covered above, this function returns a
25646 DECL, OVERLOAD, or baselink representing the result of the lookup.
25647 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25648 is returned.
25649
25650 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25651 (e.g., "struct") that was used. In that case bindings that do not
25652 refer to types are ignored.
25653
25654 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25655 ignored.
25656
25657 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25658 are ignored.
25659
25660 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25661 types.
25662
25663 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25664 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25665 NULL_TREE otherwise. */
25666
25667 static cp_expr
25668 cp_parser_lookup_name (cp_parser *parser, tree name,
25669 enum tag_types tag_type,
25670 bool is_template,
25671 bool is_namespace,
25672 bool check_dependency,
25673 tree *ambiguous_decls,
25674 location_t name_location)
25675 {
25676 tree decl;
25677 tree object_type = parser->context->object_type;
25678
25679 /* Assume that the lookup will be unambiguous. */
25680 if (ambiguous_decls)
25681 *ambiguous_decls = NULL_TREE;
25682
25683 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25684 no longer valid. Note that if we are parsing tentatively, and
25685 the parse fails, OBJECT_TYPE will be automatically restored. */
25686 parser->context->object_type = NULL_TREE;
25687
25688 if (name == error_mark_node)
25689 return error_mark_node;
25690
25691 /* A template-id has already been resolved; there is no lookup to
25692 do. */
25693 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25694 return name;
25695 if (BASELINK_P (name))
25696 {
25697 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25698 == TEMPLATE_ID_EXPR);
25699 return name;
25700 }
25701
25702 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25703 it should already have been checked to make sure that the name
25704 used matches the type being destroyed. */
25705 if (TREE_CODE (name) == BIT_NOT_EXPR)
25706 {
25707 tree type;
25708
25709 /* Figure out to which type this destructor applies. */
25710 if (parser->scope)
25711 type = parser->scope;
25712 else if (object_type)
25713 type = object_type;
25714 else
25715 type = current_class_type;
25716 /* If that's not a class type, there is no destructor. */
25717 if (!type || !CLASS_TYPE_P (type))
25718 return error_mark_node;
25719
25720 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25721 lazily_declare_fn (sfk_destructor, type);
25722
25723 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25724 return dtor;
25725
25726 return error_mark_node;
25727 }
25728
25729 /* By this point, the NAME should be an ordinary identifier. If
25730 the id-expression was a qualified name, the qualifying scope is
25731 stored in PARSER->SCOPE at this point. */
25732 gcc_assert (identifier_p (name));
25733
25734 /* Perform the lookup. */
25735 if (parser->scope)
25736 {
25737 bool dependent_p;
25738
25739 if (parser->scope == error_mark_node)
25740 return error_mark_node;
25741
25742 /* If the SCOPE is dependent, the lookup must be deferred until
25743 the template is instantiated -- unless we are explicitly
25744 looking up names in uninstantiated templates. Even then, we
25745 cannot look up the name if the scope is not a class type; it
25746 might, for example, be a template type parameter. */
25747 dependent_p = (TYPE_P (parser->scope)
25748 && dependent_scope_p (parser->scope));
25749 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25750 && dependent_p)
25751 /* Defer lookup. */
25752 decl = error_mark_node;
25753 else
25754 {
25755 tree pushed_scope = NULL_TREE;
25756
25757 /* If PARSER->SCOPE is a dependent type, then it must be a
25758 class type, and we must not be checking dependencies;
25759 otherwise, we would have processed this lookup above. So
25760 that PARSER->SCOPE is not considered a dependent base by
25761 lookup_member, we must enter the scope here. */
25762 if (dependent_p)
25763 pushed_scope = push_scope (parser->scope);
25764
25765 /* If the PARSER->SCOPE is a template specialization, it
25766 may be instantiated during name lookup. In that case,
25767 errors may be issued. Even if we rollback the current
25768 tentative parse, those errors are valid. */
25769 decl = lookup_qualified_name (parser->scope, name,
25770 prefer_type_arg (tag_type),
25771 /*complain=*/true);
25772
25773 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25774 lookup result and the nested-name-specifier nominates a class C:
25775 * if the name specified after the nested-name-specifier, when
25776 looked up in C, is the injected-class-name of C (Clause 9), or
25777 * if the name specified after the nested-name-specifier is the
25778 same as the identifier or the simple-template-id's template-
25779 name in the last component of the nested-name-specifier,
25780 the name is instead considered to name the constructor of
25781 class C. [ Note: for example, the constructor is not an
25782 acceptable lookup result in an elaborated-type-specifier so
25783 the constructor would not be used in place of the
25784 injected-class-name. --end note ] Such a constructor name
25785 shall be used only in the declarator-id of a declaration that
25786 names a constructor or in a using-declaration. */
25787 if (tag_type == none_type
25788 && DECL_SELF_REFERENCE_P (decl)
25789 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25790 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25791 prefer_type_arg (tag_type),
25792 /*complain=*/true);
25793
25794 /* If we have a single function from a using decl, pull it out. */
25795 if (TREE_CODE (decl) == OVERLOAD
25796 && !really_overloaded_fn (decl))
25797 decl = OVL_FUNCTION (decl);
25798
25799 if (pushed_scope)
25800 pop_scope (pushed_scope);
25801 }
25802
25803 /* If the scope is a dependent type and either we deferred lookup or
25804 we did lookup but didn't find the name, rememeber the name. */
25805 if (decl == error_mark_node && TYPE_P (parser->scope)
25806 && dependent_type_p (parser->scope))
25807 {
25808 if (tag_type)
25809 {
25810 tree type;
25811
25812 /* The resolution to Core Issue 180 says that `struct
25813 A::B' should be considered a type-name, even if `A'
25814 is dependent. */
25815 type = make_typename_type (parser->scope, name, tag_type,
25816 /*complain=*/tf_error);
25817 if (type != error_mark_node)
25818 decl = TYPE_NAME (type);
25819 }
25820 else if (is_template
25821 && (cp_parser_next_token_ends_template_argument_p (parser)
25822 || cp_lexer_next_token_is (parser->lexer,
25823 CPP_CLOSE_PAREN)))
25824 decl = make_unbound_class_template (parser->scope,
25825 name, NULL_TREE,
25826 /*complain=*/tf_error);
25827 else
25828 decl = build_qualified_name (/*type=*/NULL_TREE,
25829 parser->scope, name,
25830 is_template);
25831 }
25832 parser->qualifying_scope = parser->scope;
25833 parser->object_scope = NULL_TREE;
25834 }
25835 else if (object_type)
25836 {
25837 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25838 OBJECT_TYPE is not a class. */
25839 if (CLASS_TYPE_P (object_type))
25840 /* If the OBJECT_TYPE is a template specialization, it may
25841 be instantiated during name lookup. In that case, errors
25842 may be issued. Even if we rollback the current tentative
25843 parse, those errors are valid. */
25844 decl = lookup_member (object_type,
25845 name,
25846 /*protect=*/0,
25847 prefer_type_arg (tag_type),
25848 tf_warning_or_error);
25849 else
25850 decl = NULL_TREE;
25851
25852 if (!decl)
25853 {
25854 /* Look it up in the enclosing context. */
25855 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25856 /*nonclass=*/0,
25857 /*block_p=*/true, is_namespace, 0);
25858 /* DR 141 says when looking for a template-name after -> or ., only
25859 consider class templates. */
25860 if (decl && is_template && !DECL_TYPE_TEMPLATE_P (decl))
25861 {
25862 tree d = decl;
25863 if (is_overloaded_fn (d))
25864 d = get_first_fn (d);
25865 if (DECL_P (d) && !DECL_CLASS_SCOPE_P (d))
25866 decl = NULL_TREE;
25867 }
25868 }
25869 if (object_type == unknown_type_node)
25870 /* The object is type-dependent, so we can't look anything up; we used
25871 this to get the DR 141 behavior. */
25872 object_type = NULL_TREE;
25873 parser->object_scope = object_type;
25874 parser->qualifying_scope = NULL_TREE;
25875 }
25876 else
25877 {
25878 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25879 /*nonclass=*/0,
25880 /*block_p=*/true, is_namespace, 0);
25881 parser->qualifying_scope = NULL_TREE;
25882 parser->object_scope = NULL_TREE;
25883 }
25884
25885 /* If the lookup failed, let our caller know. */
25886 if (!decl || decl == error_mark_node)
25887 return error_mark_node;
25888
25889 /* Pull out the template from an injected-class-name (or multiple). */
25890 if (is_template)
25891 decl = maybe_get_template_decl_from_type_decl (decl);
25892
25893 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25894 if (TREE_CODE (decl) == TREE_LIST)
25895 {
25896 if (ambiguous_decls)
25897 *ambiguous_decls = decl;
25898 /* The error message we have to print is too complicated for
25899 cp_parser_error, so we incorporate its actions directly. */
25900 if (!cp_parser_simulate_error (parser))
25901 {
25902 error_at (name_location, "reference to %qD is ambiguous",
25903 name);
25904 print_candidates (decl);
25905 }
25906 return error_mark_node;
25907 }
25908
25909 gcc_assert (DECL_P (decl)
25910 || TREE_CODE (decl) == OVERLOAD
25911 || TREE_CODE (decl) == SCOPE_REF
25912 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25913 || BASELINK_P (decl));
25914
25915 /* If we have resolved the name of a member declaration, check to
25916 see if the declaration is accessible. When the name resolves to
25917 set of overloaded functions, accessibility is checked when
25918 overload resolution is done.
25919
25920 During an explicit instantiation, access is not checked at all,
25921 as per [temp.explicit]. */
25922 if (DECL_P (decl))
25923 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25924
25925 maybe_record_typedef_use (decl);
25926
25927 return cp_expr (decl, name_location);
25928 }
25929
25930 /* Like cp_parser_lookup_name, but for use in the typical case where
25931 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25932 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25933
25934 static tree
25935 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25936 {
25937 return cp_parser_lookup_name (parser, name,
25938 none_type,
25939 /*is_template=*/false,
25940 /*is_namespace=*/false,
25941 /*check_dependency=*/true,
25942 /*ambiguous_decls=*/NULL,
25943 location);
25944 }
25945
25946 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25947 the current context, return the TYPE_DECL. If TAG_NAME_P is
25948 true, the DECL indicates the class being defined in a class-head,
25949 or declared in an elaborated-type-specifier.
25950
25951 Otherwise, return DECL. */
25952
25953 static tree
25954 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25955 {
25956 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25957 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25958
25959 struct A {
25960 template <typename T> struct B;
25961 };
25962
25963 template <typename T> struct A::B {};
25964
25965 Similarly, in an elaborated-type-specifier:
25966
25967 namespace N { struct X{}; }
25968
25969 struct A {
25970 template <typename T> friend struct N::X;
25971 };
25972
25973 However, if the DECL refers to a class type, and we are in
25974 the scope of the class, then the name lookup automatically
25975 finds the TYPE_DECL created by build_self_reference rather
25976 than a TEMPLATE_DECL. For example, in:
25977
25978 template <class T> struct S {
25979 S s;
25980 };
25981
25982 there is no need to handle such case. */
25983
25984 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25985 return DECL_TEMPLATE_RESULT (decl);
25986
25987 return decl;
25988 }
25989
25990 /* If too many, or too few, template-parameter lists apply to the
25991 declarator, issue an error message. Returns TRUE if all went well,
25992 and FALSE otherwise. */
25993
25994 static bool
25995 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25996 cp_declarator *declarator,
25997 location_t declarator_location)
25998 {
25999 switch (declarator->kind)
26000 {
26001 case cdk_id:
26002 {
26003 unsigned num_templates = 0;
26004 tree scope = declarator->u.id.qualifying_scope;
26005
26006 if (scope)
26007 num_templates = num_template_headers_for_class (scope);
26008 else if (TREE_CODE (declarator->u.id.unqualified_name)
26009 == TEMPLATE_ID_EXPR)
26010 /* If the DECLARATOR has the form `X<y>' then it uses one
26011 additional level of template parameters. */
26012 ++num_templates;
26013
26014 return cp_parser_check_template_parameters
26015 (parser, num_templates, declarator_location, declarator);
26016 }
26017
26018 case cdk_function:
26019 case cdk_array:
26020 case cdk_pointer:
26021 case cdk_reference:
26022 case cdk_ptrmem:
26023 return (cp_parser_check_declarator_template_parameters
26024 (parser, declarator->declarator, declarator_location));
26025
26026 case cdk_decomp:
26027 case cdk_error:
26028 return true;
26029
26030 default:
26031 gcc_unreachable ();
26032 }
26033 return false;
26034 }
26035
26036 /* NUM_TEMPLATES were used in the current declaration. If that is
26037 invalid, return FALSE and issue an error messages. Otherwise,
26038 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26039 declarator and we can print more accurate diagnostics. */
26040
26041 static bool
26042 cp_parser_check_template_parameters (cp_parser* parser,
26043 unsigned num_templates,
26044 location_t location,
26045 cp_declarator *declarator)
26046 {
26047 /* If there are the same number of template classes and parameter
26048 lists, that's OK. */
26049 if (parser->num_template_parameter_lists == num_templates)
26050 return true;
26051 /* If there are more, but only one more, then we are referring to a
26052 member template. That's OK too. */
26053 if (parser->num_template_parameter_lists == num_templates + 1)
26054 return true;
26055 /* If there are more template classes than parameter lists, we have
26056 something like:
26057
26058 template <class T> void S<T>::R<T>::f (); */
26059 if (parser->num_template_parameter_lists < num_templates)
26060 {
26061 if (declarator && !current_function_decl)
26062 error_at (location, "specializing member %<%T::%E%> "
26063 "requires %<template<>%> syntax",
26064 declarator->u.id.qualifying_scope,
26065 declarator->u.id.unqualified_name);
26066 else if (declarator)
26067 error_at (location, "invalid declaration of %<%T::%E%>",
26068 declarator->u.id.qualifying_scope,
26069 declarator->u.id.unqualified_name);
26070 else
26071 error_at (location, "too few template-parameter-lists");
26072 return false;
26073 }
26074 /* Otherwise, there are too many template parameter lists. We have
26075 something like:
26076
26077 template <class T> template <class U> void S::f(); */
26078 error_at (location, "too many template-parameter-lists");
26079 return false;
26080 }
26081
26082 /* Parse an optional `::' token indicating that the following name is
26083 from the global namespace. If so, PARSER->SCOPE is set to the
26084 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26085 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26086 Returns the new value of PARSER->SCOPE, if the `::' token is
26087 present, and NULL_TREE otherwise. */
26088
26089 static tree
26090 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26091 {
26092 cp_token *token;
26093
26094 /* Peek at the next token. */
26095 token = cp_lexer_peek_token (parser->lexer);
26096 /* If we're looking at a `::' token then we're starting from the
26097 global namespace, not our current location. */
26098 if (token->type == CPP_SCOPE)
26099 {
26100 /* Consume the `::' token. */
26101 cp_lexer_consume_token (parser->lexer);
26102 /* Set the SCOPE so that we know where to start the lookup. */
26103 parser->scope = global_namespace;
26104 parser->qualifying_scope = global_namespace;
26105 parser->object_scope = NULL_TREE;
26106
26107 return parser->scope;
26108 }
26109 else if (!current_scope_valid_p)
26110 {
26111 parser->scope = NULL_TREE;
26112 parser->qualifying_scope = NULL_TREE;
26113 parser->object_scope = NULL_TREE;
26114 }
26115
26116 return NULL_TREE;
26117 }
26118
26119 /* Returns TRUE if the upcoming token sequence is the start of a
26120 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26121 declarator is preceded by the `friend' specifier. */
26122
26123 static bool
26124 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26125 {
26126 bool constructor_p;
26127 bool outside_class_specifier_p;
26128 tree nested_name_specifier;
26129 cp_token *next_token;
26130
26131 /* The common case is that this is not a constructor declarator, so
26132 try to avoid doing lots of work if at all possible. It's not
26133 valid declare a constructor at function scope. */
26134 if (parser->in_function_body)
26135 return false;
26136 /* And only certain tokens can begin a constructor declarator. */
26137 next_token = cp_lexer_peek_token (parser->lexer);
26138 if (next_token->type != CPP_NAME
26139 && next_token->type != CPP_SCOPE
26140 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26141 && next_token->type != CPP_TEMPLATE_ID)
26142 return false;
26143
26144 /* Parse tentatively; we are going to roll back all of the tokens
26145 consumed here. */
26146 cp_parser_parse_tentatively (parser);
26147 /* Assume that we are looking at a constructor declarator. */
26148 constructor_p = true;
26149
26150 /* Look for the optional `::' operator. */
26151 cp_parser_global_scope_opt (parser,
26152 /*current_scope_valid_p=*/false);
26153 /* Look for the nested-name-specifier. */
26154 nested_name_specifier
26155 = (cp_parser_nested_name_specifier_opt (parser,
26156 /*typename_keyword_p=*/false,
26157 /*check_dependency_p=*/false,
26158 /*type_p=*/false,
26159 /*is_declaration=*/false));
26160
26161 outside_class_specifier_p = (!at_class_scope_p ()
26162 || !TYPE_BEING_DEFINED (current_class_type)
26163 || friend_p);
26164
26165 /* Outside of a class-specifier, there must be a
26166 nested-name-specifier. Except in C++17 mode, where we
26167 might be declaring a guiding declaration. */
26168 if (!nested_name_specifier && outside_class_specifier_p
26169 && cxx_dialect < cxx1z)
26170 constructor_p = false;
26171 else if (nested_name_specifier == error_mark_node)
26172 constructor_p = false;
26173
26174 /* If we have a class scope, this is easy; DR 147 says that S::S always
26175 names the constructor, and no other qualified name could. */
26176 if (constructor_p && nested_name_specifier
26177 && CLASS_TYPE_P (nested_name_specifier))
26178 {
26179 tree id = cp_parser_unqualified_id (parser,
26180 /*template_keyword_p=*/false,
26181 /*check_dependency_p=*/false,
26182 /*declarator_p=*/true,
26183 /*optional_p=*/false);
26184 if (is_overloaded_fn (id))
26185 id = DECL_NAME (get_first_fn (id));
26186 if (!constructor_name_p (id, nested_name_specifier))
26187 constructor_p = false;
26188 }
26189 /* If we still think that this might be a constructor-declarator,
26190 look for a class-name. */
26191 else if (constructor_p)
26192 {
26193 /* If we have:
26194
26195 template <typename T> struct S {
26196 S();
26197 };
26198
26199 we must recognize that the nested `S' names a class. */
26200 if (cxx_dialect >= cxx1z)
26201 cp_parser_parse_tentatively (parser);
26202
26203 tree type_decl;
26204 type_decl = cp_parser_class_name (parser,
26205 /*typename_keyword_p=*/false,
26206 /*template_keyword_p=*/false,
26207 none_type,
26208 /*check_dependency_p=*/false,
26209 /*class_head_p=*/false,
26210 /*is_declaration=*/false);
26211
26212 if (cxx_dialect >= cxx1z
26213 && !cp_parser_parse_definitely (parser))
26214 {
26215 type_decl = NULL_TREE;
26216 tree tmpl = cp_parser_template_name (parser,
26217 /*template_keyword*/false,
26218 /*check_dependency_p*/false,
26219 /*is_declaration*/false,
26220 none_type,
26221 /*is_identifier*/NULL);
26222 if (DECL_CLASS_TEMPLATE_P (tmpl)
26223 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26224 /* It's a deduction guide, return true. */;
26225 else
26226 cp_parser_simulate_error (parser);
26227 }
26228
26229 /* If there was no class-name, then this is not a constructor.
26230 Otherwise, if we are in a class-specifier and we aren't
26231 handling a friend declaration, check that its type matches
26232 current_class_type (c++/38313). Note: error_mark_node
26233 is left alone for error recovery purposes. */
26234 constructor_p = (!cp_parser_error_occurred (parser)
26235 && (outside_class_specifier_p
26236 || type_decl == NULL_TREE
26237 || type_decl == error_mark_node
26238 || same_type_p (current_class_type,
26239 TREE_TYPE (type_decl))));
26240
26241 /* If we're still considering a constructor, we have to see a `(',
26242 to begin the parameter-declaration-clause, followed by either a
26243 `)', an `...', or a decl-specifier. We need to check for a
26244 type-specifier to avoid being fooled into thinking that:
26245
26246 S (f) (int);
26247
26248 is a constructor. (It is actually a function named `f' that
26249 takes one parameter (of type `int') and returns a value of type
26250 `S'. */
26251 if (constructor_p
26252 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26253 constructor_p = false;
26254
26255 if (constructor_p
26256 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26257 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26258 /* A parameter declaration begins with a decl-specifier,
26259 which is either the "attribute" keyword, a storage class
26260 specifier, or (usually) a type-specifier. */
26261 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26262 {
26263 tree type;
26264 tree pushed_scope = NULL_TREE;
26265 unsigned saved_num_template_parameter_lists;
26266
26267 /* Names appearing in the type-specifier should be looked up
26268 in the scope of the class. */
26269 if (current_class_type)
26270 type = NULL_TREE;
26271 else if (type_decl)
26272 {
26273 type = TREE_TYPE (type_decl);
26274 if (TREE_CODE (type) == TYPENAME_TYPE)
26275 {
26276 type = resolve_typename_type (type,
26277 /*only_current_p=*/false);
26278 if (TREE_CODE (type) == TYPENAME_TYPE)
26279 {
26280 cp_parser_abort_tentative_parse (parser);
26281 return false;
26282 }
26283 }
26284 pushed_scope = push_scope (type);
26285 }
26286
26287 /* Inside the constructor parameter list, surrounding
26288 template-parameter-lists do not apply. */
26289 saved_num_template_parameter_lists
26290 = parser->num_template_parameter_lists;
26291 parser->num_template_parameter_lists = 0;
26292
26293 /* Look for the type-specifier. */
26294 cp_parser_type_specifier (parser,
26295 CP_PARSER_FLAGS_NONE,
26296 /*decl_specs=*/NULL,
26297 /*is_declarator=*/true,
26298 /*declares_class_or_enum=*/NULL,
26299 /*is_cv_qualifier=*/NULL);
26300
26301 parser->num_template_parameter_lists
26302 = saved_num_template_parameter_lists;
26303
26304 /* Leave the scope of the class. */
26305 if (pushed_scope)
26306 pop_scope (pushed_scope);
26307
26308 constructor_p = !cp_parser_error_occurred (parser);
26309 }
26310 }
26311
26312 /* We did not really want to consume any tokens. */
26313 cp_parser_abort_tentative_parse (parser);
26314
26315 return constructor_p;
26316 }
26317
26318 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26319 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26320 they must be performed once we are in the scope of the function.
26321
26322 Returns the function defined. */
26323
26324 static tree
26325 cp_parser_function_definition_from_specifiers_and_declarator
26326 (cp_parser* parser,
26327 cp_decl_specifier_seq *decl_specifiers,
26328 tree attributes,
26329 const cp_declarator *declarator)
26330 {
26331 tree fn;
26332 bool success_p;
26333
26334 /* Begin the function-definition. */
26335 success_p = start_function (decl_specifiers, declarator, attributes);
26336
26337 /* The things we're about to see are not directly qualified by any
26338 template headers we've seen thus far. */
26339 reset_specialization ();
26340
26341 /* If there were names looked up in the decl-specifier-seq that we
26342 did not check, check them now. We must wait until we are in the
26343 scope of the function to perform the checks, since the function
26344 might be a friend. */
26345 perform_deferred_access_checks (tf_warning_or_error);
26346
26347 if (success_p)
26348 {
26349 cp_finalize_omp_declare_simd (parser, current_function_decl);
26350 parser->omp_declare_simd = NULL;
26351 cp_finalize_oacc_routine (parser, current_function_decl, true);
26352 parser->oacc_routine = NULL;
26353 }
26354
26355 if (!success_p)
26356 {
26357 /* Skip the entire function. */
26358 cp_parser_skip_to_end_of_block_or_statement (parser);
26359 fn = error_mark_node;
26360 }
26361 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26362 {
26363 /* Seen already, skip it. An error message has already been output. */
26364 cp_parser_skip_to_end_of_block_or_statement (parser);
26365 fn = current_function_decl;
26366 current_function_decl = NULL_TREE;
26367 /* If this is a function from a class, pop the nested class. */
26368 if (current_class_name)
26369 pop_nested_class ();
26370 }
26371 else
26372 {
26373 timevar_id_t tv;
26374 if (DECL_DECLARED_INLINE_P (current_function_decl))
26375 tv = TV_PARSE_INLINE;
26376 else
26377 tv = TV_PARSE_FUNC;
26378 timevar_push (tv);
26379 fn = cp_parser_function_definition_after_declarator (parser,
26380 /*inline_p=*/false);
26381 timevar_pop (tv);
26382 }
26383
26384 return fn;
26385 }
26386
26387 /* Parse the part of a function-definition that follows the
26388 declarator. INLINE_P is TRUE iff this function is an inline
26389 function defined within a class-specifier.
26390
26391 Returns the function defined. */
26392
26393 static tree
26394 cp_parser_function_definition_after_declarator (cp_parser* parser,
26395 bool inline_p)
26396 {
26397 tree fn;
26398 bool ctor_initializer_p = false;
26399 bool saved_in_unbraced_linkage_specification_p;
26400 bool saved_in_function_body;
26401 unsigned saved_num_template_parameter_lists;
26402 cp_token *token;
26403 bool fully_implicit_function_template_p
26404 = parser->fully_implicit_function_template_p;
26405 parser->fully_implicit_function_template_p = false;
26406 tree implicit_template_parms
26407 = parser->implicit_template_parms;
26408 parser->implicit_template_parms = 0;
26409 cp_binding_level* implicit_template_scope
26410 = parser->implicit_template_scope;
26411 parser->implicit_template_scope = 0;
26412
26413 saved_in_function_body = parser->in_function_body;
26414 parser->in_function_body = true;
26415 /* If the next token is `return', then the code may be trying to
26416 make use of the "named return value" extension that G++ used to
26417 support. */
26418 token = cp_lexer_peek_token (parser->lexer);
26419 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26420 {
26421 /* Consume the `return' keyword. */
26422 cp_lexer_consume_token (parser->lexer);
26423 /* Look for the identifier that indicates what value is to be
26424 returned. */
26425 cp_parser_identifier (parser);
26426 /* Issue an error message. */
26427 error_at (token->location,
26428 "named return values are no longer supported");
26429 /* Skip tokens until we reach the start of the function body. */
26430 while (true)
26431 {
26432 cp_token *token = cp_lexer_peek_token (parser->lexer);
26433 if (token->type == CPP_OPEN_BRACE
26434 || token->type == CPP_EOF
26435 || token->type == CPP_PRAGMA_EOL)
26436 break;
26437 cp_lexer_consume_token (parser->lexer);
26438 }
26439 }
26440 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26441 anything declared inside `f'. */
26442 saved_in_unbraced_linkage_specification_p
26443 = parser->in_unbraced_linkage_specification_p;
26444 parser->in_unbraced_linkage_specification_p = false;
26445 /* Inside the function, surrounding template-parameter-lists do not
26446 apply. */
26447 saved_num_template_parameter_lists
26448 = parser->num_template_parameter_lists;
26449 parser->num_template_parameter_lists = 0;
26450
26451 start_lambda_scope (current_function_decl);
26452
26453 /* If the next token is `try', `__transaction_atomic', or
26454 `__transaction_relaxed`, then we are looking at either function-try-block
26455 or function-transaction-block. Note that all of these include the
26456 function-body. */
26457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26458 ctor_initializer_p = cp_parser_function_transaction (parser,
26459 RID_TRANSACTION_ATOMIC);
26460 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26461 RID_TRANSACTION_RELAXED))
26462 ctor_initializer_p = cp_parser_function_transaction (parser,
26463 RID_TRANSACTION_RELAXED);
26464 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26465 ctor_initializer_p = cp_parser_function_try_block (parser);
26466 else
26467 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26468 (parser, /*in_function_try_block=*/false);
26469
26470 finish_lambda_scope ();
26471
26472 /* Finish the function. */
26473 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26474 (inline_p ? 2 : 0));
26475 /* Generate code for it, if necessary. */
26476 expand_or_defer_fn (fn);
26477 /* Restore the saved values. */
26478 parser->in_unbraced_linkage_specification_p
26479 = saved_in_unbraced_linkage_specification_p;
26480 parser->num_template_parameter_lists
26481 = saved_num_template_parameter_lists;
26482 parser->in_function_body = saved_in_function_body;
26483
26484 parser->fully_implicit_function_template_p
26485 = fully_implicit_function_template_p;
26486 parser->implicit_template_parms
26487 = implicit_template_parms;
26488 parser->implicit_template_scope
26489 = implicit_template_scope;
26490
26491 if (parser->fully_implicit_function_template_p)
26492 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26493
26494 return fn;
26495 }
26496
26497 /* Parse a template-declaration body (following argument list). */
26498
26499 static void
26500 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26501 tree parameter_list,
26502 bool member_p)
26503 {
26504 tree decl = NULL_TREE;
26505 bool friend_p = false;
26506
26507 /* We just processed one more parameter list. */
26508 ++parser->num_template_parameter_lists;
26509
26510 /* Get the deferred access checks from the parameter list. These
26511 will be checked once we know what is being declared, as for a
26512 member template the checks must be performed in the scope of the
26513 class containing the member. */
26514 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26515
26516 /* Tentatively parse for a new template parameter list, which can either be
26517 the template keyword or a template introduction. */
26518 if (cp_parser_template_declaration_after_export (parser, member_p))
26519 /* OK */;
26520 else if (cxx_dialect >= cxx11
26521 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26522 decl = cp_parser_alias_declaration (parser);
26523 else
26524 {
26525 /* There are no access checks when parsing a template, as we do not
26526 know if a specialization will be a friend. */
26527 push_deferring_access_checks (dk_no_check);
26528 cp_token *token = cp_lexer_peek_token (parser->lexer);
26529 decl = cp_parser_single_declaration (parser,
26530 checks,
26531 member_p,
26532 /*explicit_specialization_p=*/false,
26533 &friend_p);
26534 pop_deferring_access_checks ();
26535
26536 /* If this is a member template declaration, let the front
26537 end know. */
26538 if (member_p && !friend_p && decl)
26539 {
26540 if (TREE_CODE (decl) == TYPE_DECL)
26541 cp_parser_check_access_in_redeclaration (decl, token->location);
26542
26543 decl = finish_member_template_decl (decl);
26544 }
26545 else if (friend_p && decl
26546 && DECL_DECLARES_TYPE_P (decl))
26547 make_friend_class (current_class_type, TREE_TYPE (decl),
26548 /*complain=*/true);
26549 }
26550 /* We are done with the current parameter list. */
26551 --parser->num_template_parameter_lists;
26552
26553 pop_deferring_access_checks ();
26554
26555 /* Finish up. */
26556 finish_template_decl (parameter_list);
26557
26558 /* Check the template arguments for a literal operator template. */
26559 if (decl
26560 && DECL_DECLARES_FUNCTION_P (decl)
26561 && UDLIT_OPER_P (DECL_NAME (decl)))
26562 {
26563 bool ok = true;
26564 if (parameter_list == NULL_TREE)
26565 ok = false;
26566 else
26567 {
26568 int num_parms = TREE_VEC_LENGTH (parameter_list);
26569 if (num_parms == 1)
26570 {
26571 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26572 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26573 if (TREE_TYPE (parm) != char_type_node
26574 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26575 ok = false;
26576 }
26577 else if (num_parms == 2 && cxx_dialect >= cxx14)
26578 {
26579 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26580 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26581 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26582 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26583 if (parm == error_mark_node
26584 || TREE_TYPE (parm) != TREE_TYPE (type)
26585 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26586 ok = false;
26587 }
26588 else
26589 ok = false;
26590 }
26591 if (!ok)
26592 {
26593 if (cxx_dialect >= cxx14)
26594 error ("literal operator template %qD has invalid parameter list."
26595 " Expected non-type template argument pack <char...>"
26596 " or <typename CharT, CharT...>",
26597 decl);
26598 else
26599 error ("literal operator template %qD has invalid parameter list."
26600 " Expected non-type template argument pack <char...>",
26601 decl);
26602 }
26603 }
26604
26605 /* Register member declarations. */
26606 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26607 finish_member_declaration (decl);
26608 /* If DECL is a function template, we must return to parse it later.
26609 (Even though there is no definition, there might be default
26610 arguments that need handling.) */
26611 if (member_p && decl
26612 && DECL_DECLARES_FUNCTION_P (decl))
26613 vec_safe_push (unparsed_funs_with_definitions, decl);
26614 }
26615
26616 /* Parse a template introduction header for a template-declaration. Returns
26617 false if tentative parse fails. */
26618
26619 static bool
26620 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26621 {
26622 cp_parser_parse_tentatively (parser);
26623
26624 tree saved_scope = parser->scope;
26625 tree saved_object_scope = parser->object_scope;
26626 tree saved_qualifying_scope = parser->qualifying_scope;
26627
26628 /* Look for the optional `::' operator. */
26629 cp_parser_global_scope_opt (parser,
26630 /*current_scope_valid_p=*/false);
26631 /* Look for the nested-name-specifier. */
26632 cp_parser_nested_name_specifier_opt (parser,
26633 /*typename_keyword_p=*/false,
26634 /*check_dependency_p=*/true,
26635 /*type_p=*/false,
26636 /*is_declaration=*/false);
26637
26638 cp_token *token = cp_lexer_peek_token (parser->lexer);
26639 tree concept_name = cp_parser_identifier (parser);
26640
26641 /* Look up the concept for which we will be matching
26642 template parameters. */
26643 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26644 token->location);
26645 parser->scope = saved_scope;
26646 parser->object_scope = saved_object_scope;
26647 parser->qualifying_scope = saved_qualifying_scope;
26648
26649 if (concept_name == error_mark_node)
26650 cp_parser_simulate_error (parser);
26651
26652 /* Look for opening brace for introduction. */
26653 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26654
26655 if (!cp_parser_parse_definitely (parser))
26656 return false;
26657
26658 push_deferring_access_checks (dk_deferred);
26659
26660 /* Build vector of placeholder parameters and grab
26661 matching identifiers. */
26662 tree introduction_list = cp_parser_introduction_list (parser);
26663
26664 /* The introduction-list shall not be empty. */
26665 int nargs = TREE_VEC_LENGTH (introduction_list);
26666 if (nargs == 0)
26667 {
26668 error ("empty introduction-list");
26669 return true;
26670 }
26671
26672 /* Look for closing brace for introduction. */
26673 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26674 return true;
26675
26676 if (tmpl_decl == error_mark_node)
26677 {
26678 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26679 token->location);
26680 return true;
26681 }
26682
26683 /* Build and associate the constraint. */
26684 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26685 if (parms && parms != error_mark_node)
26686 {
26687 cp_parser_template_declaration_after_parameters (parser, parms,
26688 member_p);
26689 return true;
26690 }
26691
26692 error_at (token->location, "no matching concept for template-introduction");
26693 return true;
26694 }
26695
26696 /* Parse a normal template-declaration following the template keyword. */
26697
26698 static void
26699 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26700 {
26701 tree parameter_list;
26702 bool need_lang_pop;
26703 location_t location = input_location;
26704
26705 /* Look for the `<' token. */
26706 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26707 return;
26708 if (at_class_scope_p () && current_function_decl)
26709 {
26710 /* 14.5.2.2 [temp.mem]
26711
26712 A local class shall not have member templates. */
26713 error_at (location,
26714 "invalid declaration of member template in local class");
26715 cp_parser_skip_to_end_of_block_or_statement (parser);
26716 return;
26717 }
26718 /* [temp]
26719
26720 A template ... shall not have C linkage. */
26721 if (current_lang_name == lang_name_c)
26722 {
26723 error_at (location, "template with C linkage");
26724 /* Give it C++ linkage to avoid confusing other parts of the
26725 front end. */
26726 push_lang_context (lang_name_cplusplus);
26727 need_lang_pop = true;
26728 }
26729 else
26730 need_lang_pop = false;
26731
26732 /* We cannot perform access checks on the template parameter
26733 declarations until we know what is being declared, just as we
26734 cannot check the decl-specifier list. */
26735 push_deferring_access_checks (dk_deferred);
26736
26737 /* If the next token is `>', then we have an invalid
26738 specialization. Rather than complain about an invalid template
26739 parameter, issue an error message here. */
26740 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26741 {
26742 cp_parser_error (parser, "invalid explicit specialization");
26743 begin_specialization ();
26744 parameter_list = NULL_TREE;
26745 }
26746 else
26747 {
26748 /* Parse the template parameters. */
26749 parameter_list = cp_parser_template_parameter_list (parser);
26750 }
26751
26752 /* Look for the `>'. */
26753 cp_parser_skip_to_end_of_template_parameter_list (parser);
26754
26755 /* Manage template requirements */
26756 if (flag_concepts)
26757 {
26758 tree reqs = get_shorthand_constraints (current_template_parms);
26759 if (tree r = cp_parser_requires_clause_opt (parser))
26760 reqs = conjoin_constraints (reqs, normalize_expression (r));
26761 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26762 }
26763
26764 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26765 member_p);
26766
26767 /* For the erroneous case of a template with C linkage, we pushed an
26768 implicit C++ linkage scope; exit that scope now. */
26769 if (need_lang_pop)
26770 pop_lang_context ();
26771 }
26772
26773 /* Parse a template-declaration, assuming that the `export' (and
26774 `extern') keywords, if present, has already been scanned. MEMBER_P
26775 is as for cp_parser_template_declaration. */
26776
26777 static bool
26778 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26779 {
26780 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26781 {
26782 cp_lexer_consume_token (parser->lexer);
26783 cp_parser_explicit_template_declaration (parser, member_p);
26784 return true;
26785 }
26786 else if (flag_concepts)
26787 return cp_parser_template_introduction (parser, member_p);
26788
26789 return false;
26790 }
26791
26792 /* Perform the deferred access checks from a template-parameter-list.
26793 CHECKS is a TREE_LIST of access checks, as returned by
26794 get_deferred_access_checks. */
26795
26796 static void
26797 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26798 {
26799 ++processing_template_parmlist;
26800 perform_access_checks (checks, tf_warning_or_error);
26801 --processing_template_parmlist;
26802 }
26803
26804 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26805 `function-definition' sequence that follows a template header.
26806 If MEMBER_P is true, this declaration appears in a class scope.
26807
26808 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26809 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26810
26811 static tree
26812 cp_parser_single_declaration (cp_parser* parser,
26813 vec<deferred_access_check, va_gc> *checks,
26814 bool member_p,
26815 bool explicit_specialization_p,
26816 bool* friend_p)
26817 {
26818 int declares_class_or_enum;
26819 tree decl = NULL_TREE;
26820 cp_decl_specifier_seq decl_specifiers;
26821 bool function_definition_p = false;
26822 cp_token *decl_spec_token_start;
26823
26824 /* This function is only used when processing a template
26825 declaration. */
26826 gcc_assert (innermost_scope_kind () == sk_template_parms
26827 || innermost_scope_kind () == sk_template_spec);
26828
26829 /* Defer access checks until we know what is being declared. */
26830 push_deferring_access_checks (dk_deferred);
26831
26832 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26833 alternative. */
26834 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26835 cp_parser_decl_specifier_seq (parser,
26836 CP_PARSER_FLAGS_OPTIONAL,
26837 &decl_specifiers,
26838 &declares_class_or_enum);
26839 if (friend_p)
26840 *friend_p = cp_parser_friend_p (&decl_specifiers);
26841
26842 /* There are no template typedefs. */
26843 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26844 {
26845 error_at (decl_spec_token_start->location,
26846 "template declaration of %<typedef%>");
26847 decl = error_mark_node;
26848 }
26849
26850 /* Gather up the access checks that occurred the
26851 decl-specifier-seq. */
26852 stop_deferring_access_checks ();
26853
26854 /* Check for the declaration of a template class. */
26855 if (declares_class_or_enum)
26856 {
26857 if (cp_parser_declares_only_class_p (parser)
26858 || (declares_class_or_enum & 2))
26859 {
26860 // If this is a declaration, but not a definition, associate
26861 // any constraints with the type declaration. Constraints
26862 // are associated with definitions in cp_parser_class_specifier.
26863 if (declares_class_or_enum == 1)
26864 associate_classtype_constraints (decl_specifiers.type);
26865
26866 decl = shadow_tag (&decl_specifiers);
26867
26868 /* In this case:
26869
26870 struct C {
26871 friend template <typename T> struct A<T>::B;
26872 };
26873
26874 A<T>::B will be represented by a TYPENAME_TYPE, and
26875 therefore not recognized by shadow_tag. */
26876 if (friend_p && *friend_p
26877 && !decl
26878 && decl_specifiers.type
26879 && TYPE_P (decl_specifiers.type))
26880 decl = decl_specifiers.type;
26881
26882 if (decl && decl != error_mark_node)
26883 decl = TYPE_NAME (decl);
26884 else
26885 decl = error_mark_node;
26886
26887 /* Perform access checks for template parameters. */
26888 cp_parser_perform_template_parameter_access_checks (checks);
26889
26890 /* Give a helpful diagnostic for
26891 template <class T> struct A { } a;
26892 if we aren't already recovering from an error. */
26893 if (!cp_parser_declares_only_class_p (parser)
26894 && !seen_error ())
26895 {
26896 error_at (cp_lexer_peek_token (parser->lexer)->location,
26897 "a class template declaration must not declare "
26898 "anything else");
26899 cp_parser_skip_to_end_of_block_or_statement (parser);
26900 goto out;
26901 }
26902 }
26903 }
26904
26905 /* Complain about missing 'typename' or other invalid type names. */
26906 if (!decl_specifiers.any_type_specifiers_p
26907 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26908 {
26909 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26910 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26911 the rest of this declaration. */
26912 decl = error_mark_node;
26913 goto out;
26914 }
26915
26916 /* If it's not a template class, try for a template function. If
26917 the next token is a `;', then this declaration does not declare
26918 anything. But, if there were errors in the decl-specifiers, then
26919 the error might well have come from an attempted class-specifier.
26920 In that case, there's no need to warn about a missing declarator. */
26921 if (!decl
26922 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26923 || decl_specifiers.type != error_mark_node))
26924 {
26925 decl = cp_parser_init_declarator (parser,
26926 &decl_specifiers,
26927 checks,
26928 /*function_definition_allowed_p=*/true,
26929 member_p,
26930 declares_class_or_enum,
26931 &function_definition_p,
26932 NULL, NULL, NULL);
26933
26934 /* 7.1.1-1 [dcl.stc]
26935
26936 A storage-class-specifier shall not be specified in an explicit
26937 specialization... */
26938 if (decl
26939 && explicit_specialization_p
26940 && decl_specifiers.storage_class != sc_none)
26941 {
26942 error_at (decl_spec_token_start->location,
26943 "explicit template specialization cannot have a storage class");
26944 decl = error_mark_node;
26945 }
26946
26947 if (decl && VAR_P (decl))
26948 check_template_variable (decl);
26949 }
26950
26951 /* Look for a trailing `;' after the declaration. */
26952 if (!function_definition_p
26953 && (decl == error_mark_node
26954 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26955 cp_parser_skip_to_end_of_block_or_statement (parser);
26956
26957 out:
26958 pop_deferring_access_checks ();
26959
26960 /* Clear any current qualification; whatever comes next is the start
26961 of something new. */
26962 parser->scope = NULL_TREE;
26963 parser->qualifying_scope = NULL_TREE;
26964 parser->object_scope = NULL_TREE;
26965
26966 return decl;
26967 }
26968
26969 /* Parse a cast-expression that is not the operand of a unary "&". */
26970
26971 static cp_expr
26972 cp_parser_simple_cast_expression (cp_parser *parser)
26973 {
26974 return cp_parser_cast_expression (parser, /*address_p=*/false,
26975 /*cast_p=*/false, /*decltype*/false, NULL);
26976 }
26977
26978 /* Parse a functional cast to TYPE. Returns an expression
26979 representing the cast. */
26980
26981 static cp_expr
26982 cp_parser_functional_cast (cp_parser* parser, tree type)
26983 {
26984 vec<tree, va_gc> *vec;
26985 tree expression_list;
26986 cp_expr cast;
26987 bool nonconst_p;
26988
26989 location_t start_loc = input_location;
26990
26991 if (!type)
26992 type = error_mark_node;
26993
26994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26995 {
26996 cp_lexer_set_source_position (parser->lexer);
26997 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26998 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26999 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27000 if (TREE_CODE (type) == TYPE_DECL)
27001 type = TREE_TYPE (type);
27002
27003 cast = finish_compound_literal (type, expression_list,
27004 tf_warning_or_error, fcl_functional);
27005 /* Create a location of the form:
27006 type_name{i, f}
27007 ^~~~~~~~~~~~~~~
27008 with caret == start at the start of the type name,
27009 finishing at the closing brace. */
27010 location_t finish_loc
27011 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27012 location_t combined_loc = make_location (start_loc, start_loc,
27013 finish_loc);
27014 cast.set_location (combined_loc);
27015 return cast;
27016 }
27017
27018
27019 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27020 /*cast_p=*/true,
27021 /*allow_expansion_p=*/true,
27022 /*non_constant_p=*/NULL);
27023 if (vec == NULL)
27024 expression_list = error_mark_node;
27025 else
27026 {
27027 expression_list = build_tree_list_vec (vec);
27028 release_tree_vector (vec);
27029 }
27030
27031 cast = build_functional_cast (type, expression_list,
27032 tf_warning_or_error);
27033 /* [expr.const]/1: In an integral constant expression "only type
27034 conversions to integral or enumeration type can be used". */
27035 if (TREE_CODE (type) == TYPE_DECL)
27036 type = TREE_TYPE (type);
27037 if (cast != error_mark_node
27038 && !cast_valid_in_integral_constant_expression_p (type)
27039 && cp_parser_non_integral_constant_expression (parser,
27040 NIC_CONSTRUCTOR))
27041 return error_mark_node;
27042
27043 /* Create a location of the form:
27044 float(i)
27045 ^~~~~~~~
27046 with caret == start at the start of the type name,
27047 finishing at the closing paren. */
27048 location_t finish_loc
27049 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27050 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27051 cast.set_location (combined_loc);
27052 return cast;
27053 }
27054
27055 /* Save the tokens that make up the body of a member function defined
27056 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27057 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27058 specifiers applied to the declaration. Returns the FUNCTION_DECL
27059 for the member function. */
27060
27061 static tree
27062 cp_parser_save_member_function_body (cp_parser* parser,
27063 cp_decl_specifier_seq *decl_specifiers,
27064 cp_declarator *declarator,
27065 tree attributes)
27066 {
27067 cp_token *first;
27068 cp_token *last;
27069 tree fn;
27070 bool function_try_block = false;
27071
27072 /* Create the FUNCTION_DECL. */
27073 fn = grokmethod (decl_specifiers, declarator, attributes);
27074 cp_finalize_omp_declare_simd (parser, fn);
27075 cp_finalize_oacc_routine (parser, fn, true);
27076 /* If something went badly wrong, bail out now. */
27077 if (fn == error_mark_node)
27078 {
27079 /* If there's a function-body, skip it. */
27080 if (cp_parser_token_starts_function_definition_p
27081 (cp_lexer_peek_token (parser->lexer)))
27082 cp_parser_skip_to_end_of_block_or_statement (parser);
27083 return error_mark_node;
27084 }
27085
27086 /* Remember it, if there default args to post process. */
27087 cp_parser_save_default_args (parser, fn);
27088
27089 /* Save away the tokens that make up the body of the
27090 function. */
27091 first = parser->lexer->next_token;
27092
27093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27094 cp_lexer_consume_token (parser->lexer);
27095 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27096 RID_TRANSACTION_ATOMIC))
27097 {
27098 cp_lexer_consume_token (parser->lexer);
27099 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27101 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27102 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27103 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27104 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27105 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27106 {
27107 cp_lexer_consume_token (parser->lexer);
27108 cp_lexer_consume_token (parser->lexer);
27109 cp_lexer_consume_token (parser->lexer);
27110 cp_lexer_consume_token (parser->lexer);
27111 cp_lexer_consume_token (parser->lexer);
27112 }
27113 else
27114 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27115 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27116 {
27117 cp_lexer_consume_token (parser->lexer);
27118 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27119 break;
27120 }
27121 }
27122
27123 /* Handle function try blocks. */
27124 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27125 {
27126 cp_lexer_consume_token (parser->lexer);
27127 function_try_block = true;
27128 }
27129 /* We can have braced-init-list mem-initializers before the fn body. */
27130 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27131 {
27132 cp_lexer_consume_token (parser->lexer);
27133 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27134 {
27135 /* cache_group will stop after an un-nested { } pair, too. */
27136 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27137 break;
27138
27139 /* variadic mem-inits have ... after the ')'. */
27140 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27141 cp_lexer_consume_token (parser->lexer);
27142 }
27143 }
27144 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27145 /* Handle function try blocks. */
27146 if (function_try_block)
27147 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27148 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27149 last = parser->lexer->next_token;
27150
27151 /* Save away the inline definition; we will process it when the
27152 class is complete. */
27153 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27154 DECL_PENDING_INLINE_P (fn) = 1;
27155
27156 /* We need to know that this was defined in the class, so that
27157 friend templates are handled correctly. */
27158 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27159
27160 /* Add FN to the queue of functions to be parsed later. */
27161 vec_safe_push (unparsed_funs_with_definitions, fn);
27162
27163 return fn;
27164 }
27165
27166 /* Save the tokens that make up the in-class initializer for a non-static
27167 data member. Returns a DEFAULT_ARG. */
27168
27169 static tree
27170 cp_parser_save_nsdmi (cp_parser* parser)
27171 {
27172 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27173 }
27174
27175 /* Parse a template-argument-list, as well as the trailing ">" (but
27176 not the opening "<"). See cp_parser_template_argument_list for the
27177 return value. */
27178
27179 static tree
27180 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27181 {
27182 tree arguments;
27183 tree saved_scope;
27184 tree saved_qualifying_scope;
27185 tree saved_object_scope;
27186 bool saved_greater_than_is_operator_p;
27187 int saved_unevaluated_operand;
27188 int saved_inhibit_evaluation_warnings;
27189
27190 /* [temp.names]
27191
27192 When parsing a template-id, the first non-nested `>' is taken as
27193 the end of the template-argument-list rather than a greater-than
27194 operator. */
27195 saved_greater_than_is_operator_p
27196 = parser->greater_than_is_operator_p;
27197 parser->greater_than_is_operator_p = false;
27198 /* Parsing the argument list may modify SCOPE, so we save it
27199 here. */
27200 saved_scope = parser->scope;
27201 saved_qualifying_scope = parser->qualifying_scope;
27202 saved_object_scope = parser->object_scope;
27203 /* We need to evaluate the template arguments, even though this
27204 template-id may be nested within a "sizeof". */
27205 saved_unevaluated_operand = cp_unevaluated_operand;
27206 cp_unevaluated_operand = 0;
27207 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27208 c_inhibit_evaluation_warnings = 0;
27209 /* Parse the template-argument-list itself. */
27210 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27211 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27212 arguments = NULL_TREE;
27213 else
27214 arguments = cp_parser_template_argument_list (parser);
27215 /* Look for the `>' that ends the template-argument-list. If we find
27216 a '>>' instead, it's probably just a typo. */
27217 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27218 {
27219 if (cxx_dialect != cxx98)
27220 {
27221 /* In C++0x, a `>>' in a template argument list or cast
27222 expression is considered to be two separate `>'
27223 tokens. So, change the current token to a `>', but don't
27224 consume it: it will be consumed later when the outer
27225 template argument list (or cast expression) is parsed.
27226 Note that this replacement of `>' for `>>' is necessary
27227 even if we are parsing tentatively: in the tentative
27228 case, after calling
27229 cp_parser_enclosed_template_argument_list we will always
27230 throw away all of the template arguments and the first
27231 closing `>', either because the template argument list
27232 was erroneous or because we are replacing those tokens
27233 with a CPP_TEMPLATE_ID token. The second `>' (which will
27234 not have been thrown away) is needed either to close an
27235 outer template argument list or to complete a new-style
27236 cast. */
27237 cp_token *token = cp_lexer_peek_token (parser->lexer);
27238 token->type = CPP_GREATER;
27239 }
27240 else if (!saved_greater_than_is_operator_p)
27241 {
27242 /* If we're in a nested template argument list, the '>>' has
27243 to be a typo for '> >'. We emit the error message, but we
27244 continue parsing and we push a '>' as next token, so that
27245 the argument list will be parsed correctly. Note that the
27246 global source location is still on the token before the
27247 '>>', so we need to say explicitly where we want it. */
27248 cp_token *token = cp_lexer_peek_token (parser->lexer);
27249 gcc_rich_location richloc (token->location);
27250 richloc.add_fixit_replace ("> >");
27251 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27252 "within a nested template argument list");
27253
27254 token->type = CPP_GREATER;
27255 }
27256 else
27257 {
27258 /* If this is not a nested template argument list, the '>>'
27259 is a typo for '>'. Emit an error message and continue.
27260 Same deal about the token location, but here we can get it
27261 right by consuming the '>>' before issuing the diagnostic. */
27262 cp_token *token = cp_lexer_consume_token (parser->lexer);
27263 error_at (token->location,
27264 "spurious %<>>%>, use %<>%> to terminate "
27265 "a template argument list");
27266 }
27267 }
27268 else
27269 cp_parser_skip_to_end_of_template_parameter_list (parser);
27270 /* The `>' token might be a greater-than operator again now. */
27271 parser->greater_than_is_operator_p
27272 = saved_greater_than_is_operator_p;
27273 /* Restore the SAVED_SCOPE. */
27274 parser->scope = saved_scope;
27275 parser->qualifying_scope = saved_qualifying_scope;
27276 parser->object_scope = saved_object_scope;
27277 cp_unevaluated_operand = saved_unevaluated_operand;
27278 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27279
27280 return arguments;
27281 }
27282
27283 /* MEMBER_FUNCTION is a member function, or a friend. If default
27284 arguments, or the body of the function have not yet been parsed,
27285 parse them now. */
27286
27287 static void
27288 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27289 {
27290 timevar_push (TV_PARSE_INMETH);
27291 /* If this member is a template, get the underlying
27292 FUNCTION_DECL. */
27293 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27294 member_function = DECL_TEMPLATE_RESULT (member_function);
27295
27296 /* There should not be any class definitions in progress at this
27297 point; the bodies of members are only parsed outside of all class
27298 definitions. */
27299 gcc_assert (parser->num_classes_being_defined == 0);
27300 /* While we're parsing the member functions we might encounter more
27301 classes. We want to handle them right away, but we don't want
27302 them getting mixed up with functions that are currently in the
27303 queue. */
27304 push_unparsed_function_queues (parser);
27305
27306 /* Make sure that any template parameters are in scope. */
27307 maybe_begin_member_template_processing (member_function);
27308
27309 /* If the body of the function has not yet been parsed, parse it
27310 now. */
27311 if (DECL_PENDING_INLINE_P (member_function))
27312 {
27313 tree function_scope;
27314 cp_token_cache *tokens;
27315
27316 /* The function is no longer pending; we are processing it. */
27317 tokens = DECL_PENDING_INLINE_INFO (member_function);
27318 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27319 DECL_PENDING_INLINE_P (member_function) = 0;
27320
27321 /* If this is a local class, enter the scope of the containing
27322 function. */
27323 function_scope = current_function_decl;
27324 if (function_scope)
27325 push_function_context ();
27326
27327 /* Push the body of the function onto the lexer stack. */
27328 cp_parser_push_lexer_for_tokens (parser, tokens);
27329
27330 /* Let the front end know that we going to be defining this
27331 function. */
27332 start_preparsed_function (member_function, NULL_TREE,
27333 SF_PRE_PARSED | SF_INCLASS_INLINE);
27334
27335 /* Don't do access checking if it is a templated function. */
27336 if (processing_template_decl)
27337 push_deferring_access_checks (dk_no_check);
27338
27339 /* #pragma omp declare reduction needs special parsing. */
27340 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27341 {
27342 parser->lexer->in_pragma = true;
27343 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27344 finish_function (/*inline*/2);
27345 cp_check_omp_declare_reduction (member_function);
27346 }
27347 else
27348 /* Now, parse the body of the function. */
27349 cp_parser_function_definition_after_declarator (parser,
27350 /*inline_p=*/true);
27351
27352 if (processing_template_decl)
27353 pop_deferring_access_checks ();
27354
27355 /* Leave the scope of the containing function. */
27356 if (function_scope)
27357 pop_function_context ();
27358 cp_parser_pop_lexer (parser);
27359 }
27360
27361 /* Remove any template parameters from the symbol table. */
27362 maybe_end_member_template_processing ();
27363
27364 /* Restore the queue. */
27365 pop_unparsed_function_queues (parser);
27366 timevar_pop (TV_PARSE_INMETH);
27367 }
27368
27369 /* If DECL contains any default args, remember it on the unparsed
27370 functions queue. */
27371
27372 static void
27373 cp_parser_save_default_args (cp_parser* parser, tree decl)
27374 {
27375 tree probe;
27376
27377 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27378 probe;
27379 probe = TREE_CHAIN (probe))
27380 if (TREE_PURPOSE (probe))
27381 {
27382 cp_default_arg_entry entry = {current_class_type, decl};
27383 vec_safe_push (unparsed_funs_with_default_args, entry);
27384 break;
27385 }
27386 }
27387
27388 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27389 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27390 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27391 from the parameter-type-list. */
27392
27393 static tree
27394 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27395 tree default_arg, tree parmtype)
27396 {
27397 cp_token_cache *tokens;
27398 tree parsed_arg;
27399 bool dummy;
27400
27401 if (default_arg == error_mark_node)
27402 return error_mark_node;
27403
27404 /* Push the saved tokens for the default argument onto the parser's
27405 lexer stack. */
27406 tokens = DEFARG_TOKENS (default_arg);
27407 cp_parser_push_lexer_for_tokens (parser, tokens);
27408
27409 start_lambda_scope (decl);
27410
27411 /* Parse the default argument. */
27412 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27413 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27414 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27415
27416 finish_lambda_scope ();
27417
27418 if (parsed_arg == error_mark_node)
27419 cp_parser_skip_to_end_of_statement (parser);
27420
27421 if (!processing_template_decl)
27422 {
27423 /* In a non-template class, check conversions now. In a template,
27424 we'll wait and instantiate these as needed. */
27425 if (TREE_CODE (decl) == PARM_DECL)
27426 parsed_arg = check_default_argument (parmtype, parsed_arg,
27427 tf_warning_or_error);
27428 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27429 parsed_arg = error_mark_node;
27430 else
27431 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27432 }
27433
27434 /* If the token stream has not been completely used up, then
27435 there was extra junk after the end of the default
27436 argument. */
27437 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27438 {
27439 if (TREE_CODE (decl) == PARM_DECL)
27440 cp_parser_error (parser, "expected %<,%>");
27441 else
27442 cp_parser_error (parser, "expected %<;%>");
27443 }
27444
27445 /* Revert to the main lexer. */
27446 cp_parser_pop_lexer (parser);
27447
27448 return parsed_arg;
27449 }
27450
27451 /* FIELD is a non-static data member with an initializer which we saved for
27452 later; parse it now. */
27453
27454 static void
27455 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27456 {
27457 tree def;
27458
27459 maybe_begin_member_template_processing (field);
27460
27461 push_unparsed_function_queues (parser);
27462 def = cp_parser_late_parse_one_default_arg (parser, field,
27463 DECL_INITIAL (field),
27464 NULL_TREE);
27465 pop_unparsed_function_queues (parser);
27466
27467 maybe_end_member_template_processing ();
27468
27469 DECL_INITIAL (field) = def;
27470 }
27471
27472 /* FN is a FUNCTION_DECL which may contains a parameter with an
27473 unparsed DEFAULT_ARG. Parse the default args now. This function
27474 assumes that the current scope is the scope in which the default
27475 argument should be processed. */
27476
27477 static void
27478 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27479 {
27480 bool saved_local_variables_forbidden_p;
27481 tree parm, parmdecl;
27482
27483 /* While we're parsing the default args, we might (due to the
27484 statement expression extension) encounter more classes. We want
27485 to handle them right away, but we don't want them getting mixed
27486 up with default args that are currently in the queue. */
27487 push_unparsed_function_queues (parser);
27488
27489 /* Local variable names (and the `this' keyword) may not appear
27490 in a default argument. */
27491 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27492 parser->local_variables_forbidden_p = true;
27493
27494 push_defarg_context (fn);
27495
27496 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27497 parmdecl = DECL_ARGUMENTS (fn);
27498 parm && parm != void_list_node;
27499 parm = TREE_CHAIN (parm),
27500 parmdecl = DECL_CHAIN (parmdecl))
27501 {
27502 tree default_arg = TREE_PURPOSE (parm);
27503 tree parsed_arg;
27504 vec<tree, va_gc> *insts;
27505 tree copy;
27506 unsigned ix;
27507
27508 if (!default_arg)
27509 continue;
27510
27511 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27512 /* This can happen for a friend declaration for a function
27513 already declared with default arguments. */
27514 continue;
27515
27516 parsed_arg
27517 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27518 default_arg,
27519 TREE_VALUE (parm));
27520 if (parsed_arg == error_mark_node)
27521 {
27522 continue;
27523 }
27524
27525 TREE_PURPOSE (parm) = parsed_arg;
27526
27527 /* Update any instantiations we've already created. */
27528 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27529 vec_safe_iterate (insts, ix, &copy); ix++)
27530 TREE_PURPOSE (copy) = parsed_arg;
27531 }
27532
27533 pop_defarg_context ();
27534
27535 /* Make sure no default arg is missing. */
27536 check_default_args (fn);
27537
27538 /* Restore the state of local_variables_forbidden_p. */
27539 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27540
27541 /* Restore the queue. */
27542 pop_unparsed_function_queues (parser);
27543 }
27544
27545 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27546
27547 sizeof ... ( identifier )
27548
27549 where the 'sizeof' token has already been consumed. */
27550
27551 static tree
27552 cp_parser_sizeof_pack (cp_parser *parser)
27553 {
27554 /* Consume the `...'. */
27555 cp_lexer_consume_token (parser->lexer);
27556 maybe_warn_variadic_templates ();
27557
27558 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27559 if (paren)
27560 cp_lexer_consume_token (parser->lexer);
27561 else
27562 permerror (cp_lexer_peek_token (parser->lexer)->location,
27563 "%<sizeof...%> argument must be surrounded by parentheses");
27564
27565 cp_token *token = cp_lexer_peek_token (parser->lexer);
27566 tree name = cp_parser_identifier (parser);
27567 if (name == error_mark_node)
27568 return error_mark_node;
27569 /* The name is not qualified. */
27570 parser->scope = NULL_TREE;
27571 parser->qualifying_scope = NULL_TREE;
27572 parser->object_scope = NULL_TREE;
27573 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27574 if (expr == error_mark_node)
27575 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27576 token->location);
27577 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27578 expr = TREE_TYPE (expr);
27579 else if (TREE_CODE (expr) == CONST_DECL)
27580 expr = DECL_INITIAL (expr);
27581 expr = make_pack_expansion (expr);
27582 PACK_EXPANSION_SIZEOF_P (expr) = true;
27583
27584 if (paren)
27585 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27586
27587 return expr;
27588 }
27589
27590 /* Parse the operand of `sizeof' (or a similar operator). Returns
27591 either a TYPE or an expression, depending on the form of the
27592 input. The KEYWORD indicates which kind of expression we have
27593 encountered. */
27594
27595 static tree
27596 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27597 {
27598 tree expr = NULL_TREE;
27599 const char *saved_message;
27600 char *tmp;
27601 bool saved_integral_constant_expression_p;
27602 bool saved_non_integral_constant_expression_p;
27603
27604 /* If it's a `...', then we are computing the length of a parameter
27605 pack. */
27606 if (keyword == RID_SIZEOF
27607 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27608 return cp_parser_sizeof_pack (parser);
27609
27610 /* Types cannot be defined in a `sizeof' expression. Save away the
27611 old message. */
27612 saved_message = parser->type_definition_forbidden_message;
27613 /* And create the new one. */
27614 tmp = concat ("types may not be defined in %<",
27615 IDENTIFIER_POINTER (ridpointers[keyword]),
27616 "%> expressions", NULL);
27617 parser->type_definition_forbidden_message = tmp;
27618
27619 /* The restrictions on constant-expressions do not apply inside
27620 sizeof expressions. */
27621 saved_integral_constant_expression_p
27622 = parser->integral_constant_expression_p;
27623 saved_non_integral_constant_expression_p
27624 = parser->non_integral_constant_expression_p;
27625 parser->integral_constant_expression_p = false;
27626
27627 /* Do not actually evaluate the expression. */
27628 ++cp_unevaluated_operand;
27629 ++c_inhibit_evaluation_warnings;
27630 /* If it's a `(', then we might be looking at the type-id
27631 construction. */
27632 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27633 {
27634 tree type = NULL_TREE;
27635
27636 /* We can't be sure yet whether we're looking at a type-id or an
27637 expression. */
27638 cp_parser_parse_tentatively (parser);
27639 /* Note: as a GNU Extension, compound literals are considered
27640 postfix-expressions as they are in C99, so they are valid
27641 arguments to sizeof. See comment in cp_parser_cast_expression
27642 for details. */
27643 if (cp_parser_compound_literal_p (parser))
27644 cp_parser_simulate_error (parser);
27645 else
27646 {
27647 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27648 parser->in_type_id_in_expr_p = true;
27649 /* Look for the type-id. */
27650 type = cp_parser_type_id (parser);
27651 /* Look for the closing `)'. */
27652 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27653 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27654 }
27655
27656 /* If all went well, then we're done. */
27657 if (cp_parser_parse_definitely (parser))
27658 {
27659 cp_decl_specifier_seq decl_specs;
27660
27661 /* Build a trivial decl-specifier-seq. */
27662 clear_decl_specs (&decl_specs);
27663 decl_specs.type = type;
27664
27665 /* Call grokdeclarator to figure out what type this is. */
27666 expr = grokdeclarator (NULL,
27667 &decl_specs,
27668 TYPENAME,
27669 /*initialized=*/0,
27670 /*attrlist=*/NULL);
27671 }
27672 }
27673
27674 /* If the type-id production did not work out, then we must be
27675 looking at the unary-expression production. */
27676 if (!expr)
27677 expr = cp_parser_unary_expression (parser);
27678
27679 /* Go back to evaluating expressions. */
27680 --cp_unevaluated_operand;
27681 --c_inhibit_evaluation_warnings;
27682
27683 /* Free the message we created. */
27684 free (tmp);
27685 /* And restore the old one. */
27686 parser->type_definition_forbidden_message = saved_message;
27687 parser->integral_constant_expression_p
27688 = saved_integral_constant_expression_p;
27689 parser->non_integral_constant_expression_p
27690 = saved_non_integral_constant_expression_p;
27691
27692 return expr;
27693 }
27694
27695 /* If the current declaration has no declarator, return true. */
27696
27697 static bool
27698 cp_parser_declares_only_class_p (cp_parser *parser)
27699 {
27700 /* If the next token is a `;' or a `,' then there is no
27701 declarator. */
27702 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27703 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27704 }
27705
27706 /* Update the DECL_SPECS to reflect the storage class indicated by
27707 KEYWORD. */
27708
27709 static void
27710 cp_parser_set_storage_class (cp_parser *parser,
27711 cp_decl_specifier_seq *decl_specs,
27712 enum rid keyword,
27713 cp_token *token)
27714 {
27715 cp_storage_class storage_class;
27716
27717 if (parser->in_unbraced_linkage_specification_p)
27718 {
27719 error_at (token->location, "invalid use of %qD in linkage specification",
27720 ridpointers[keyword]);
27721 return;
27722 }
27723 else if (decl_specs->storage_class != sc_none)
27724 {
27725 decl_specs->conflicting_specifiers_p = true;
27726 return;
27727 }
27728
27729 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27730 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27731 && decl_specs->gnu_thread_keyword_p)
27732 {
27733 pedwarn (decl_specs->locations[ds_thread], 0,
27734 "%<__thread%> before %qD", ridpointers[keyword]);
27735 }
27736
27737 switch (keyword)
27738 {
27739 case RID_AUTO:
27740 storage_class = sc_auto;
27741 break;
27742 case RID_REGISTER:
27743 storage_class = sc_register;
27744 break;
27745 case RID_STATIC:
27746 storage_class = sc_static;
27747 break;
27748 case RID_EXTERN:
27749 storage_class = sc_extern;
27750 break;
27751 case RID_MUTABLE:
27752 storage_class = sc_mutable;
27753 break;
27754 default:
27755 gcc_unreachable ();
27756 }
27757 decl_specs->storage_class = storage_class;
27758 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27759
27760 /* A storage class specifier cannot be applied alongside a typedef
27761 specifier. If there is a typedef specifier present then set
27762 conflicting_specifiers_p which will trigger an error later
27763 on in grokdeclarator. */
27764 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27765 decl_specs->conflicting_specifiers_p = true;
27766 }
27767
27768 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27769 is true, the type is a class or enum definition. */
27770
27771 static void
27772 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27773 tree type_spec,
27774 cp_token *token,
27775 bool type_definition_p)
27776 {
27777 decl_specs->any_specifiers_p = true;
27778
27779 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27780 (with, for example, in "typedef int wchar_t;") we remember that
27781 this is what happened. In system headers, we ignore these
27782 declarations so that G++ can work with system headers that are not
27783 C++-safe. */
27784 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27785 && !type_definition_p
27786 && (type_spec == boolean_type_node
27787 || type_spec == char16_type_node
27788 || type_spec == char32_type_node
27789 || type_spec == wchar_type_node)
27790 && (decl_specs->type
27791 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27792 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27793 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27794 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27795 {
27796 decl_specs->redefined_builtin_type = type_spec;
27797 set_and_check_decl_spec_loc (decl_specs,
27798 ds_redefined_builtin_type_spec,
27799 token);
27800 if (!decl_specs->type)
27801 {
27802 decl_specs->type = type_spec;
27803 decl_specs->type_definition_p = false;
27804 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27805 }
27806 }
27807 else if (decl_specs->type)
27808 decl_specs->multiple_types_p = true;
27809 else
27810 {
27811 decl_specs->type = type_spec;
27812 decl_specs->type_definition_p = type_definition_p;
27813 decl_specs->redefined_builtin_type = NULL_TREE;
27814 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27815 }
27816 }
27817
27818 /* True iff TOKEN is the GNU keyword __thread. */
27819
27820 static bool
27821 token_is__thread (cp_token *token)
27822 {
27823 gcc_assert (token->keyword == RID_THREAD);
27824 return id_equal (token->u.value, "__thread");
27825 }
27826
27827 /* Set the location for a declarator specifier and check if it is
27828 duplicated.
27829
27830 DECL_SPECS is the sequence of declarator specifiers onto which to
27831 set the location.
27832
27833 DS is the single declarator specifier to set which location is to
27834 be set onto the existing sequence of declarators.
27835
27836 LOCATION is the location for the declarator specifier to
27837 consider. */
27838
27839 static void
27840 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27841 cp_decl_spec ds, cp_token *token)
27842 {
27843 gcc_assert (ds < ds_last);
27844
27845 if (decl_specs == NULL)
27846 return;
27847
27848 source_location location = token->location;
27849
27850 if (decl_specs->locations[ds] == 0)
27851 {
27852 decl_specs->locations[ds] = location;
27853 if (ds == ds_thread)
27854 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27855 }
27856 else
27857 {
27858 if (ds == ds_long)
27859 {
27860 if (decl_specs->locations[ds_long_long] != 0)
27861 error_at (location,
27862 "%<long long long%> is too long for GCC");
27863 else
27864 {
27865 decl_specs->locations[ds_long_long] = location;
27866 pedwarn_cxx98 (location,
27867 OPT_Wlong_long,
27868 "ISO C++ 1998 does not support %<long long%>");
27869 }
27870 }
27871 else if (ds == ds_thread)
27872 {
27873 bool gnu = token_is__thread (token);
27874 if (gnu != decl_specs->gnu_thread_keyword_p)
27875 error_at (location,
27876 "both %<__thread%> and %<thread_local%> specified");
27877 else
27878 {
27879 gcc_rich_location richloc (location);
27880 richloc.add_fixit_remove ();
27881 error_at_rich_loc (&richloc, "duplicate %qD", token->u.value);
27882 }
27883 }
27884 else
27885 {
27886 static const char *const decl_spec_names[] = {
27887 "signed",
27888 "unsigned",
27889 "short",
27890 "long",
27891 "const",
27892 "volatile",
27893 "restrict",
27894 "inline",
27895 "virtual",
27896 "explicit",
27897 "friend",
27898 "typedef",
27899 "using",
27900 "constexpr",
27901 "__complex"
27902 };
27903 gcc_rich_location richloc (location);
27904 richloc.add_fixit_remove ();
27905 error_at_rich_loc (&richloc, "duplicate %qs", decl_spec_names[ds]);
27906 }
27907 }
27908 }
27909
27910 /* Return true iff the declarator specifier DS is present in the
27911 sequence of declarator specifiers DECL_SPECS. */
27912
27913 bool
27914 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27915 cp_decl_spec ds)
27916 {
27917 gcc_assert (ds < ds_last);
27918
27919 if (decl_specs == NULL)
27920 return false;
27921
27922 return decl_specs->locations[ds] != 0;
27923 }
27924
27925 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27926 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27927
27928 static bool
27929 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27930 {
27931 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27932 }
27933
27934 /* Issue an error message indicating that TOKEN_DESC was expected.
27935 If KEYWORD is true, it indicated this function is called by
27936 cp_parser_require_keword and the required token can only be
27937 a indicated keyword. */
27938
27939 static void
27940 cp_parser_required_error (cp_parser *parser,
27941 required_token token_desc,
27942 bool keyword)
27943 {
27944 switch (token_desc)
27945 {
27946 case RT_NEW:
27947 cp_parser_error (parser, "expected %<new%>");
27948 return;
27949 case RT_DELETE:
27950 cp_parser_error (parser, "expected %<delete%>");
27951 return;
27952 case RT_RETURN:
27953 cp_parser_error (parser, "expected %<return%>");
27954 return;
27955 case RT_WHILE:
27956 cp_parser_error (parser, "expected %<while%>");
27957 return;
27958 case RT_EXTERN:
27959 cp_parser_error (parser, "expected %<extern%>");
27960 return;
27961 case RT_STATIC_ASSERT:
27962 cp_parser_error (parser, "expected %<static_assert%>");
27963 return;
27964 case RT_DECLTYPE:
27965 cp_parser_error (parser, "expected %<decltype%>");
27966 return;
27967 case RT_OPERATOR:
27968 cp_parser_error (parser, "expected %<operator%>");
27969 return;
27970 case RT_CLASS:
27971 cp_parser_error (parser, "expected %<class%>");
27972 return;
27973 case RT_TEMPLATE:
27974 cp_parser_error (parser, "expected %<template%>");
27975 return;
27976 case RT_NAMESPACE:
27977 cp_parser_error (parser, "expected %<namespace%>");
27978 return;
27979 case RT_USING:
27980 cp_parser_error (parser, "expected %<using%>");
27981 return;
27982 case RT_ASM:
27983 cp_parser_error (parser, "expected %<asm%>");
27984 return;
27985 case RT_TRY:
27986 cp_parser_error (parser, "expected %<try%>");
27987 return;
27988 case RT_CATCH:
27989 cp_parser_error (parser, "expected %<catch%>");
27990 return;
27991 case RT_THROW:
27992 cp_parser_error (parser, "expected %<throw%>");
27993 return;
27994 case RT_LABEL:
27995 cp_parser_error (parser, "expected %<__label__%>");
27996 return;
27997 case RT_AT_TRY:
27998 cp_parser_error (parser, "expected %<@try%>");
27999 return;
28000 case RT_AT_SYNCHRONIZED:
28001 cp_parser_error (parser, "expected %<@synchronized%>");
28002 return;
28003 case RT_AT_THROW:
28004 cp_parser_error (parser, "expected %<@throw%>");
28005 return;
28006 case RT_TRANSACTION_ATOMIC:
28007 cp_parser_error (parser, "expected %<__transaction_atomic%>");
28008 return;
28009 case RT_TRANSACTION_RELAXED:
28010 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
28011 return;
28012 default:
28013 break;
28014 }
28015 if (!keyword)
28016 {
28017 switch (token_desc)
28018 {
28019 case RT_SEMICOLON:
28020 cp_parser_error (parser, "expected %<;%>");
28021 return;
28022 case RT_OPEN_PAREN:
28023 cp_parser_error (parser, "expected %<(%>");
28024 return;
28025 case RT_CLOSE_BRACE:
28026 cp_parser_error (parser, "expected %<}%>");
28027 return;
28028 case RT_OPEN_BRACE:
28029 cp_parser_error (parser, "expected %<{%>");
28030 return;
28031 case RT_CLOSE_SQUARE:
28032 cp_parser_error (parser, "expected %<]%>");
28033 return;
28034 case RT_OPEN_SQUARE:
28035 cp_parser_error (parser, "expected %<[%>");
28036 return;
28037 case RT_COMMA:
28038 cp_parser_error (parser, "expected %<,%>");
28039 return;
28040 case RT_SCOPE:
28041 cp_parser_error (parser, "expected %<::%>");
28042 return;
28043 case RT_LESS:
28044 cp_parser_error (parser, "expected %<<%>");
28045 return;
28046 case RT_GREATER:
28047 cp_parser_error (parser, "expected %<>%>");
28048 return;
28049 case RT_EQ:
28050 cp_parser_error (parser, "expected %<=%>");
28051 return;
28052 case RT_ELLIPSIS:
28053 cp_parser_error (parser, "expected %<...%>");
28054 return;
28055 case RT_MULT:
28056 cp_parser_error (parser, "expected %<*%>");
28057 return;
28058 case RT_COMPL:
28059 cp_parser_error (parser, "expected %<~%>");
28060 return;
28061 case RT_COLON:
28062 cp_parser_error (parser, "expected %<:%>");
28063 return;
28064 case RT_COLON_SCOPE:
28065 cp_parser_error (parser, "expected %<:%> or %<::%>");
28066 return;
28067 case RT_CLOSE_PAREN:
28068 cp_parser_error (parser, "expected %<)%>");
28069 return;
28070 case RT_COMMA_CLOSE_PAREN:
28071 cp_parser_error (parser, "expected %<,%> or %<)%>");
28072 return;
28073 case RT_PRAGMA_EOL:
28074 cp_parser_error (parser, "expected end of line");
28075 return;
28076 case RT_NAME:
28077 cp_parser_error (parser, "expected identifier");
28078 return;
28079 case RT_SELECT:
28080 cp_parser_error (parser, "expected selection-statement");
28081 return;
28082 case RT_INTERATION:
28083 cp_parser_error (parser, "expected iteration-statement");
28084 return;
28085 case RT_JUMP:
28086 cp_parser_error (parser, "expected jump-statement");
28087 return;
28088 case RT_CLASS_KEY:
28089 cp_parser_error (parser, "expected class-key");
28090 return;
28091 case RT_CLASS_TYPENAME_TEMPLATE:
28092 cp_parser_error (parser,
28093 "expected %<class%>, %<typename%>, or %<template%>");
28094 return;
28095 default:
28096 gcc_unreachable ();
28097 }
28098 }
28099 else
28100 gcc_unreachable ();
28101 }
28102
28103
28104
28105 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28106 issue an error message indicating that TOKEN_DESC was expected.
28107
28108 Returns the token consumed, if the token had the appropriate type.
28109 Otherwise, returns NULL. */
28110
28111 static cp_token *
28112 cp_parser_require (cp_parser* parser,
28113 enum cpp_ttype type,
28114 required_token token_desc)
28115 {
28116 if (cp_lexer_next_token_is (parser->lexer, type))
28117 return cp_lexer_consume_token (parser->lexer);
28118 else
28119 {
28120 /* Output the MESSAGE -- unless we're parsing tentatively. */
28121 if (!cp_parser_simulate_error (parser))
28122 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
28123 return NULL;
28124 }
28125 }
28126
28127 /* An error message is produced if the next token is not '>'.
28128 All further tokens are skipped until the desired token is
28129 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28130
28131 static void
28132 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28133 {
28134 /* Current level of '< ... >'. */
28135 unsigned level = 0;
28136 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28137 unsigned nesting_depth = 0;
28138
28139 /* Are we ready, yet? If not, issue error message. */
28140 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28141 return;
28142
28143 /* Skip tokens until the desired token is found. */
28144 while (true)
28145 {
28146 /* Peek at the next token. */
28147 switch (cp_lexer_peek_token (parser->lexer)->type)
28148 {
28149 case CPP_LESS:
28150 if (!nesting_depth)
28151 ++level;
28152 break;
28153
28154 case CPP_RSHIFT:
28155 if (cxx_dialect == cxx98)
28156 /* C++0x views the `>>' operator as two `>' tokens, but
28157 C++98 does not. */
28158 break;
28159 else if (!nesting_depth && level-- == 0)
28160 {
28161 /* We've hit a `>>' where the first `>' closes the
28162 template argument list, and the second `>' is
28163 spurious. Just consume the `>>' and stop; we've
28164 already produced at least one error. */
28165 cp_lexer_consume_token (parser->lexer);
28166 return;
28167 }
28168 /* Fall through for C++0x, so we handle the second `>' in
28169 the `>>'. */
28170 gcc_fallthrough ();
28171
28172 case CPP_GREATER:
28173 if (!nesting_depth && level-- == 0)
28174 {
28175 /* We've reached the token we want, consume it and stop. */
28176 cp_lexer_consume_token (parser->lexer);
28177 return;
28178 }
28179 break;
28180
28181 case CPP_OPEN_PAREN:
28182 case CPP_OPEN_SQUARE:
28183 ++nesting_depth;
28184 break;
28185
28186 case CPP_CLOSE_PAREN:
28187 case CPP_CLOSE_SQUARE:
28188 if (nesting_depth-- == 0)
28189 return;
28190 break;
28191
28192 case CPP_EOF:
28193 case CPP_PRAGMA_EOL:
28194 case CPP_SEMICOLON:
28195 case CPP_OPEN_BRACE:
28196 case CPP_CLOSE_BRACE:
28197 /* The '>' was probably forgotten, don't look further. */
28198 return;
28199
28200 default:
28201 break;
28202 }
28203
28204 /* Consume this token. */
28205 cp_lexer_consume_token (parser->lexer);
28206 }
28207 }
28208
28209 /* If the next token is the indicated keyword, consume it. Otherwise,
28210 issue an error message indicating that TOKEN_DESC was expected.
28211
28212 Returns the token consumed, if the token had the appropriate type.
28213 Otherwise, returns NULL. */
28214
28215 static cp_token *
28216 cp_parser_require_keyword (cp_parser* parser,
28217 enum rid keyword,
28218 required_token token_desc)
28219 {
28220 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28221
28222 if (token && token->keyword != keyword)
28223 {
28224 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28225 return NULL;
28226 }
28227
28228 return token;
28229 }
28230
28231 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28232 function-definition. */
28233
28234 static bool
28235 cp_parser_token_starts_function_definition_p (cp_token* token)
28236 {
28237 return (/* An ordinary function-body begins with an `{'. */
28238 token->type == CPP_OPEN_BRACE
28239 /* A ctor-initializer begins with a `:'. */
28240 || token->type == CPP_COLON
28241 /* A function-try-block begins with `try'. */
28242 || token->keyword == RID_TRY
28243 /* A function-transaction-block begins with `__transaction_atomic'
28244 or `__transaction_relaxed'. */
28245 || token->keyword == RID_TRANSACTION_ATOMIC
28246 || token->keyword == RID_TRANSACTION_RELAXED
28247 /* The named return value extension begins with `return'. */
28248 || token->keyword == RID_RETURN);
28249 }
28250
28251 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28252 definition. */
28253
28254 static bool
28255 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28256 {
28257 cp_token *token;
28258
28259 token = cp_lexer_peek_token (parser->lexer);
28260 return (token->type == CPP_OPEN_BRACE
28261 || (token->type == CPP_COLON
28262 && !parser->colon_doesnt_start_class_def_p));
28263 }
28264
28265 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28266 C++0x) ending a template-argument. */
28267
28268 static bool
28269 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28270 {
28271 cp_token *token;
28272
28273 token = cp_lexer_peek_token (parser->lexer);
28274 return (token->type == CPP_COMMA
28275 || token->type == CPP_GREATER
28276 || token->type == CPP_ELLIPSIS
28277 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28278 }
28279
28280 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28281 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28282
28283 static bool
28284 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28285 size_t n)
28286 {
28287 cp_token *token;
28288
28289 token = cp_lexer_peek_nth_token (parser->lexer, n);
28290 if (token->type == CPP_LESS)
28291 return true;
28292 /* Check for the sequence `<::' in the original code. It would be lexed as
28293 `[:', where `[' is a digraph, and there is no whitespace before
28294 `:'. */
28295 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28296 {
28297 cp_token *token2;
28298 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28299 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28300 return true;
28301 }
28302 return false;
28303 }
28304
28305 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28306 or none_type otherwise. */
28307
28308 static enum tag_types
28309 cp_parser_token_is_class_key (cp_token* token)
28310 {
28311 switch (token->keyword)
28312 {
28313 case RID_CLASS:
28314 return class_type;
28315 case RID_STRUCT:
28316 return record_type;
28317 case RID_UNION:
28318 return union_type;
28319
28320 default:
28321 return none_type;
28322 }
28323 }
28324
28325 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28326 or none_type otherwise or if the token is null. */
28327
28328 static enum tag_types
28329 cp_parser_token_is_type_parameter_key (cp_token* token)
28330 {
28331 if (!token)
28332 return none_type;
28333
28334 switch (token->keyword)
28335 {
28336 case RID_CLASS:
28337 return class_type;
28338 case RID_TYPENAME:
28339 return typename_type;
28340
28341 default:
28342 return none_type;
28343 }
28344 }
28345
28346 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28347
28348 static void
28349 cp_parser_check_class_key (enum tag_types class_key, tree type)
28350 {
28351 if (type == error_mark_node)
28352 return;
28353 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28354 {
28355 if (permerror (input_location, "%qs tag used in naming %q#T",
28356 class_key == union_type ? "union"
28357 : class_key == record_type ? "struct" : "class",
28358 type))
28359 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28360 "%q#T was previously declared here", type);
28361 }
28362 }
28363
28364 /* Issue an error message if DECL is redeclared with different
28365 access than its original declaration [class.access.spec/3].
28366 This applies to nested classes, nested class templates and
28367 enumerations [class.mem/1]. */
28368
28369 static void
28370 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28371 {
28372 if (!decl
28373 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28374 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28375 return;
28376
28377 if ((TREE_PRIVATE (decl)
28378 != (current_access_specifier == access_private_node))
28379 || (TREE_PROTECTED (decl)
28380 != (current_access_specifier == access_protected_node)))
28381 error_at (location, "%qD redeclared with different access", decl);
28382 }
28383
28384 /* Look for the `template' keyword, as a syntactic disambiguator.
28385 Return TRUE iff it is present, in which case it will be
28386 consumed. */
28387
28388 static bool
28389 cp_parser_optional_template_keyword (cp_parser *parser)
28390 {
28391 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28392 {
28393 /* In C++98 the `template' keyword can only be used within templates;
28394 outside templates the parser can always figure out what is a
28395 template and what is not. In C++11, per the resolution of DR 468,
28396 `template' is allowed in cases where it is not strictly necessary. */
28397 if (!processing_template_decl
28398 && pedantic && cxx_dialect == cxx98)
28399 {
28400 cp_token *token = cp_lexer_peek_token (parser->lexer);
28401 pedwarn (token->location, OPT_Wpedantic,
28402 "in C++98 %<template%> (as a disambiguator) is only "
28403 "allowed within templates");
28404 /* If this part of the token stream is rescanned, the same
28405 error message would be generated. So, we purge the token
28406 from the stream. */
28407 cp_lexer_purge_token (parser->lexer);
28408 return false;
28409 }
28410 else
28411 {
28412 /* Consume the `template' keyword. */
28413 cp_lexer_consume_token (parser->lexer);
28414 return true;
28415 }
28416 }
28417 return false;
28418 }
28419
28420 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28421 set PARSER->SCOPE, and perform other related actions. */
28422
28423 static void
28424 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28425 {
28426 struct tree_check *check_value;
28427
28428 /* Get the stored value. */
28429 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28430 /* Set the scope from the stored value. */
28431 parser->scope = saved_checks_value (check_value);
28432 parser->qualifying_scope = check_value->qualifying_scope;
28433 parser->object_scope = NULL_TREE;
28434 }
28435
28436 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28437 encounter the end of a block before what we were looking for. */
28438
28439 static bool
28440 cp_parser_cache_group (cp_parser *parser,
28441 enum cpp_ttype end,
28442 unsigned depth)
28443 {
28444 while (true)
28445 {
28446 cp_token *token = cp_lexer_peek_token (parser->lexer);
28447
28448 /* Abort a parenthesized expression if we encounter a semicolon. */
28449 if ((end == CPP_CLOSE_PAREN || depth == 0)
28450 && token->type == CPP_SEMICOLON)
28451 return true;
28452 /* If we've reached the end of the file, stop. */
28453 if (token->type == CPP_EOF
28454 || (end != CPP_PRAGMA_EOL
28455 && token->type == CPP_PRAGMA_EOL))
28456 return true;
28457 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28458 /* We've hit the end of an enclosing block, so there's been some
28459 kind of syntax error. */
28460 return true;
28461
28462 /* Consume the token. */
28463 cp_lexer_consume_token (parser->lexer);
28464 /* See if it starts a new group. */
28465 if (token->type == CPP_OPEN_BRACE)
28466 {
28467 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28468 /* In theory this should probably check end == '}', but
28469 cp_parser_save_member_function_body needs it to exit
28470 after either '}' or ')' when called with ')'. */
28471 if (depth == 0)
28472 return false;
28473 }
28474 else if (token->type == CPP_OPEN_PAREN)
28475 {
28476 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28477 if (depth == 0 && end == CPP_CLOSE_PAREN)
28478 return false;
28479 }
28480 else if (token->type == CPP_PRAGMA)
28481 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28482 else if (token->type == end)
28483 return false;
28484 }
28485 }
28486
28487 /* Like above, for caching a default argument or NSDMI. Both of these are
28488 terminated by a non-nested comma, but it can be unclear whether or not a
28489 comma is nested in a template argument list unless we do more parsing.
28490 In order to handle this ambiguity, when we encounter a ',' after a '<'
28491 we try to parse what follows as a parameter-declaration-list (in the
28492 case of a default argument) or a member-declarator (in the case of an
28493 NSDMI). If that succeeds, then we stop caching. */
28494
28495 static tree
28496 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28497 {
28498 unsigned depth = 0;
28499 int maybe_template_id = 0;
28500 cp_token *first_token;
28501 cp_token *token;
28502 tree default_argument;
28503
28504 /* Add tokens until we have processed the entire default
28505 argument. We add the range [first_token, token). */
28506 first_token = cp_lexer_peek_token (parser->lexer);
28507 if (first_token->type == CPP_OPEN_BRACE)
28508 {
28509 /* For list-initialization, this is straightforward. */
28510 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28511 token = cp_lexer_peek_token (parser->lexer);
28512 }
28513 else while (true)
28514 {
28515 bool done = false;
28516
28517 /* Peek at the next token. */
28518 token = cp_lexer_peek_token (parser->lexer);
28519 /* What we do depends on what token we have. */
28520 switch (token->type)
28521 {
28522 /* In valid code, a default argument must be
28523 immediately followed by a `,' `)', or `...'. */
28524 case CPP_COMMA:
28525 if (depth == 0 && maybe_template_id)
28526 {
28527 /* If we've seen a '<', we might be in a
28528 template-argument-list. Until Core issue 325 is
28529 resolved, we don't know how this situation ought
28530 to be handled, so try to DTRT. We check whether
28531 what comes after the comma is a valid parameter
28532 declaration list. If it is, then the comma ends
28533 the default argument; otherwise the default
28534 argument continues. */
28535 bool error = false;
28536 cp_token *peek;
28537
28538 /* Set ITALP so cp_parser_parameter_declaration_list
28539 doesn't decide to commit to this parse. */
28540 bool saved_italp = parser->in_template_argument_list_p;
28541 parser->in_template_argument_list_p = true;
28542
28543 cp_parser_parse_tentatively (parser);
28544
28545 if (nsdmi)
28546 {
28547 /* Parse declarators until we reach a non-comma or
28548 somthing that cannot be an initializer.
28549 Just checking whether we're looking at a single
28550 declarator is insufficient. Consider:
28551 int var = tuple<T,U>::x;
28552 The template parameter 'U' looks exactly like a
28553 declarator. */
28554 do
28555 {
28556 int ctor_dtor_or_conv_p;
28557 cp_lexer_consume_token (parser->lexer);
28558 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28559 &ctor_dtor_or_conv_p,
28560 /*parenthesized_p=*/NULL,
28561 /*member_p=*/true,
28562 /*friend_p=*/false);
28563 peek = cp_lexer_peek_token (parser->lexer);
28564 if (cp_parser_error_occurred (parser))
28565 break;
28566 }
28567 while (peek->type == CPP_COMMA);
28568 /* If we met an '=' or ';' then the original comma
28569 was the end of the NSDMI. Otherwise assume
28570 we're still in the NSDMI. */
28571 error = (peek->type != CPP_EQ
28572 && peek->type != CPP_SEMICOLON);
28573 }
28574 else
28575 {
28576 cp_lexer_consume_token (parser->lexer);
28577 begin_scope (sk_function_parms, NULL_TREE);
28578 cp_parser_parameter_declaration_list (parser, &error);
28579 pop_bindings_and_leave_scope ();
28580 }
28581 if (!cp_parser_error_occurred (parser) && !error)
28582 done = true;
28583 cp_parser_abort_tentative_parse (parser);
28584
28585 parser->in_template_argument_list_p = saved_italp;
28586 break;
28587 }
28588 /* FALLTHRU */
28589 case CPP_CLOSE_PAREN:
28590 case CPP_ELLIPSIS:
28591 /* If we run into a non-nested `;', `}', or `]',
28592 then the code is invalid -- but the default
28593 argument is certainly over. */
28594 case CPP_SEMICOLON:
28595 case CPP_CLOSE_BRACE:
28596 case CPP_CLOSE_SQUARE:
28597 if (depth == 0
28598 /* Handle correctly int n = sizeof ... ( p ); */
28599 && token->type != CPP_ELLIPSIS)
28600 done = true;
28601 /* Update DEPTH, if necessary. */
28602 else if (token->type == CPP_CLOSE_PAREN
28603 || token->type == CPP_CLOSE_BRACE
28604 || token->type == CPP_CLOSE_SQUARE)
28605 --depth;
28606 break;
28607
28608 case CPP_OPEN_PAREN:
28609 case CPP_OPEN_SQUARE:
28610 case CPP_OPEN_BRACE:
28611 ++depth;
28612 break;
28613
28614 case CPP_LESS:
28615 if (depth == 0)
28616 /* This might be the comparison operator, or it might
28617 start a template argument list. */
28618 ++maybe_template_id;
28619 break;
28620
28621 case CPP_RSHIFT:
28622 if (cxx_dialect == cxx98)
28623 break;
28624 /* Fall through for C++0x, which treats the `>>'
28625 operator like two `>' tokens in certain
28626 cases. */
28627 gcc_fallthrough ();
28628
28629 case CPP_GREATER:
28630 if (depth == 0)
28631 {
28632 /* This might be an operator, or it might close a
28633 template argument list. But if a previous '<'
28634 started a template argument list, this will have
28635 closed it, so we can't be in one anymore. */
28636 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28637 if (maybe_template_id < 0)
28638 maybe_template_id = 0;
28639 }
28640 break;
28641
28642 /* If we run out of tokens, issue an error message. */
28643 case CPP_EOF:
28644 case CPP_PRAGMA_EOL:
28645 error_at (token->location, "file ends in default argument");
28646 return error_mark_node;
28647
28648 case CPP_NAME:
28649 case CPP_SCOPE:
28650 /* In these cases, we should look for template-ids.
28651 For example, if the default argument is
28652 `X<int, double>()', we need to do name lookup to
28653 figure out whether or not `X' is a template; if
28654 so, the `,' does not end the default argument.
28655
28656 That is not yet done. */
28657 break;
28658
28659 default:
28660 break;
28661 }
28662
28663 /* If we've reached the end, stop. */
28664 if (done)
28665 break;
28666
28667 /* Add the token to the token block. */
28668 token = cp_lexer_consume_token (parser->lexer);
28669 }
28670
28671 /* Create a DEFAULT_ARG to represent the unparsed default
28672 argument. */
28673 default_argument = make_node (DEFAULT_ARG);
28674 DEFARG_TOKENS (default_argument)
28675 = cp_token_cache_new (first_token, token);
28676 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28677
28678 return default_argument;
28679 }
28680
28681 /* Begin parsing tentatively. We always save tokens while parsing
28682 tentatively so that if the tentative parsing fails we can restore the
28683 tokens. */
28684
28685 static void
28686 cp_parser_parse_tentatively (cp_parser* parser)
28687 {
28688 /* Enter a new parsing context. */
28689 parser->context = cp_parser_context_new (parser->context);
28690 /* Begin saving tokens. */
28691 cp_lexer_save_tokens (parser->lexer);
28692 /* In order to avoid repetitive access control error messages,
28693 access checks are queued up until we are no longer parsing
28694 tentatively. */
28695 push_deferring_access_checks (dk_deferred);
28696 }
28697
28698 /* Commit to the currently active tentative parse. */
28699
28700 static void
28701 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28702 {
28703 cp_parser_context *context;
28704 cp_lexer *lexer;
28705
28706 /* Mark all of the levels as committed. */
28707 lexer = parser->lexer;
28708 for (context = parser->context; context->next; context = context->next)
28709 {
28710 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28711 break;
28712 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28713 while (!cp_lexer_saving_tokens (lexer))
28714 lexer = lexer->next;
28715 cp_lexer_commit_tokens (lexer);
28716 }
28717 }
28718
28719 /* Commit to the topmost currently active tentative parse.
28720
28721 Note that this function shouldn't be called when there are
28722 irreversible side-effects while in a tentative state. For
28723 example, we shouldn't create a permanent entry in the symbol
28724 table, or issue an error message that might not apply if the
28725 tentative parse is aborted. */
28726
28727 static void
28728 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28729 {
28730 cp_parser_context *context = parser->context;
28731 cp_lexer *lexer = parser->lexer;
28732
28733 if (context)
28734 {
28735 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28736 return;
28737 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28738
28739 while (!cp_lexer_saving_tokens (lexer))
28740 lexer = lexer->next;
28741 cp_lexer_commit_tokens (lexer);
28742 }
28743 }
28744
28745 /* Abort the currently active tentative parse. All consumed tokens
28746 will be rolled back, and no diagnostics will be issued. */
28747
28748 static void
28749 cp_parser_abort_tentative_parse (cp_parser* parser)
28750 {
28751 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28752 || errorcount > 0);
28753 cp_parser_simulate_error (parser);
28754 /* Now, pretend that we want to see if the construct was
28755 successfully parsed. */
28756 cp_parser_parse_definitely (parser);
28757 }
28758
28759 /* Stop parsing tentatively. If a parse error has occurred, restore the
28760 token stream. Otherwise, commit to the tokens we have consumed.
28761 Returns true if no error occurred; false otherwise. */
28762
28763 static bool
28764 cp_parser_parse_definitely (cp_parser* parser)
28765 {
28766 bool error_occurred;
28767 cp_parser_context *context;
28768
28769 /* Remember whether or not an error occurred, since we are about to
28770 destroy that information. */
28771 error_occurred = cp_parser_error_occurred (parser);
28772 /* Remove the topmost context from the stack. */
28773 context = parser->context;
28774 parser->context = context->next;
28775 /* If no parse errors occurred, commit to the tentative parse. */
28776 if (!error_occurred)
28777 {
28778 /* Commit to the tokens read tentatively, unless that was
28779 already done. */
28780 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28781 cp_lexer_commit_tokens (parser->lexer);
28782
28783 pop_to_parent_deferring_access_checks ();
28784 }
28785 /* Otherwise, if errors occurred, roll back our state so that things
28786 are just as they were before we began the tentative parse. */
28787 else
28788 {
28789 cp_lexer_rollback_tokens (parser->lexer);
28790 pop_deferring_access_checks ();
28791 }
28792 /* Add the context to the front of the free list. */
28793 context->next = cp_parser_context_free_list;
28794 cp_parser_context_free_list = context;
28795
28796 return !error_occurred;
28797 }
28798
28799 /* Returns true if we are parsing tentatively and are not committed to
28800 this tentative parse. */
28801
28802 static bool
28803 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28804 {
28805 return (cp_parser_parsing_tentatively (parser)
28806 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28807 }
28808
28809 /* Returns nonzero iff an error has occurred during the most recent
28810 tentative parse. */
28811
28812 static bool
28813 cp_parser_error_occurred (cp_parser* parser)
28814 {
28815 return (cp_parser_parsing_tentatively (parser)
28816 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28817 }
28818
28819 /* Returns nonzero if GNU extensions are allowed. */
28820
28821 static bool
28822 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28823 {
28824 return parser->allow_gnu_extensions_p;
28825 }
28826 \f
28827 /* Objective-C++ Productions */
28828
28829
28830 /* Parse an Objective-C expression, which feeds into a primary-expression
28831 above.
28832
28833 objc-expression:
28834 objc-message-expression
28835 objc-string-literal
28836 objc-encode-expression
28837 objc-protocol-expression
28838 objc-selector-expression
28839
28840 Returns a tree representation of the expression. */
28841
28842 static cp_expr
28843 cp_parser_objc_expression (cp_parser* parser)
28844 {
28845 /* Try to figure out what kind of declaration is present. */
28846 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28847
28848 switch (kwd->type)
28849 {
28850 case CPP_OPEN_SQUARE:
28851 return cp_parser_objc_message_expression (parser);
28852
28853 case CPP_OBJC_STRING:
28854 kwd = cp_lexer_consume_token (parser->lexer);
28855 return objc_build_string_object (kwd->u.value);
28856
28857 case CPP_KEYWORD:
28858 switch (kwd->keyword)
28859 {
28860 case RID_AT_ENCODE:
28861 return cp_parser_objc_encode_expression (parser);
28862
28863 case RID_AT_PROTOCOL:
28864 return cp_parser_objc_protocol_expression (parser);
28865
28866 case RID_AT_SELECTOR:
28867 return cp_parser_objc_selector_expression (parser);
28868
28869 default:
28870 break;
28871 }
28872 default:
28873 error_at (kwd->location,
28874 "misplaced %<@%D%> Objective-C++ construct",
28875 kwd->u.value);
28876 cp_parser_skip_to_end_of_block_or_statement (parser);
28877 }
28878
28879 return error_mark_node;
28880 }
28881
28882 /* Parse an Objective-C message expression.
28883
28884 objc-message-expression:
28885 [ objc-message-receiver objc-message-args ]
28886
28887 Returns a representation of an Objective-C message. */
28888
28889 static tree
28890 cp_parser_objc_message_expression (cp_parser* parser)
28891 {
28892 tree receiver, messageargs;
28893
28894 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28895 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28896 receiver = cp_parser_objc_message_receiver (parser);
28897 messageargs = cp_parser_objc_message_args (parser);
28898 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28899 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28900
28901 tree result = objc_build_message_expr (receiver, messageargs);
28902
28903 /* Construct a location e.g.
28904 [self func1:5]
28905 ^~~~~~~~~~~~~~
28906 ranging from the '[' to the ']', with the caret at the start. */
28907 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28908 protected_set_expr_location (result, combined_loc);
28909
28910 return result;
28911 }
28912
28913 /* Parse an objc-message-receiver.
28914
28915 objc-message-receiver:
28916 expression
28917 simple-type-specifier
28918
28919 Returns a representation of the type or expression. */
28920
28921 static tree
28922 cp_parser_objc_message_receiver (cp_parser* parser)
28923 {
28924 tree rcv;
28925
28926 /* An Objective-C message receiver may be either (1) a type
28927 or (2) an expression. */
28928 cp_parser_parse_tentatively (parser);
28929 rcv = cp_parser_expression (parser);
28930
28931 /* If that worked out, fine. */
28932 if (cp_parser_parse_definitely (parser))
28933 return rcv;
28934
28935 cp_parser_parse_tentatively (parser);
28936 rcv = cp_parser_simple_type_specifier (parser,
28937 /*decl_specs=*/NULL,
28938 CP_PARSER_FLAGS_NONE);
28939
28940 if (cp_parser_parse_definitely (parser))
28941 return objc_get_class_reference (rcv);
28942
28943 cp_parser_error (parser, "objective-c++ message receiver expected");
28944 return error_mark_node;
28945 }
28946
28947 /* Parse the arguments and selectors comprising an Objective-C message.
28948
28949 objc-message-args:
28950 objc-selector
28951 objc-selector-args
28952 objc-selector-args , objc-comma-args
28953
28954 objc-selector-args:
28955 objc-selector [opt] : assignment-expression
28956 objc-selector-args objc-selector [opt] : assignment-expression
28957
28958 objc-comma-args:
28959 assignment-expression
28960 objc-comma-args , assignment-expression
28961
28962 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28963 selector arguments and TREE_VALUE containing a list of comma
28964 arguments. */
28965
28966 static tree
28967 cp_parser_objc_message_args (cp_parser* parser)
28968 {
28969 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28970 bool maybe_unary_selector_p = true;
28971 cp_token *token = cp_lexer_peek_token (parser->lexer);
28972
28973 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28974 {
28975 tree selector = NULL_TREE, arg;
28976
28977 if (token->type != CPP_COLON)
28978 selector = cp_parser_objc_selector (parser);
28979
28980 /* Detect if we have a unary selector. */
28981 if (maybe_unary_selector_p
28982 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28983 return build_tree_list (selector, NULL_TREE);
28984
28985 maybe_unary_selector_p = false;
28986 cp_parser_require (parser, CPP_COLON, RT_COLON);
28987 arg = cp_parser_assignment_expression (parser);
28988
28989 sel_args
28990 = chainon (sel_args,
28991 build_tree_list (selector, arg));
28992
28993 token = cp_lexer_peek_token (parser->lexer);
28994 }
28995
28996 /* Handle non-selector arguments, if any. */
28997 while (token->type == CPP_COMMA)
28998 {
28999 tree arg;
29000
29001 cp_lexer_consume_token (parser->lexer);
29002 arg = cp_parser_assignment_expression (parser);
29003
29004 addl_args
29005 = chainon (addl_args,
29006 build_tree_list (NULL_TREE, arg));
29007
29008 token = cp_lexer_peek_token (parser->lexer);
29009 }
29010
29011 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29012 {
29013 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29014 return build_tree_list (error_mark_node, error_mark_node);
29015 }
29016
29017 return build_tree_list (sel_args, addl_args);
29018 }
29019
29020 /* Parse an Objective-C encode expression.
29021
29022 objc-encode-expression:
29023 @encode objc-typename
29024
29025 Returns an encoded representation of the type argument. */
29026
29027 static cp_expr
29028 cp_parser_objc_encode_expression (cp_parser* parser)
29029 {
29030 tree type;
29031 cp_token *token;
29032 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29033
29034 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29035 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29036 token = cp_lexer_peek_token (parser->lexer);
29037 type = complete_type (cp_parser_type_id (parser));
29038 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29039
29040 if (!type)
29041 {
29042 error_at (token->location,
29043 "%<@encode%> must specify a type as an argument");
29044 return error_mark_node;
29045 }
29046
29047 /* This happens if we find @encode(T) (where T is a template
29048 typename or something dependent on a template typename) when
29049 parsing a template. In that case, we can't compile it
29050 immediately, but we rather create an AT_ENCODE_EXPR which will
29051 need to be instantiated when the template is used.
29052 */
29053 if (dependent_type_p (type))
29054 {
29055 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29056 TREE_READONLY (value) = 1;
29057 return value;
29058 }
29059
29060
29061 /* Build a location of the form:
29062 @encode(int)
29063 ^~~~~~~~~~~~
29064 with caret==start at the @ token, finishing at the close paren. */
29065 location_t combined_loc
29066 = make_location (start_loc, start_loc,
29067 cp_lexer_previous_token (parser->lexer)->location);
29068
29069 return cp_expr (objc_build_encode_expr (type), combined_loc);
29070 }
29071
29072 /* Parse an Objective-C @defs expression. */
29073
29074 static tree
29075 cp_parser_objc_defs_expression (cp_parser *parser)
29076 {
29077 tree name;
29078
29079 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29080 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29081 name = cp_parser_identifier (parser);
29082 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29083
29084 return objc_get_class_ivars (name);
29085 }
29086
29087 /* Parse an Objective-C protocol expression.
29088
29089 objc-protocol-expression:
29090 @protocol ( identifier )
29091
29092 Returns a representation of the protocol expression. */
29093
29094 static tree
29095 cp_parser_objc_protocol_expression (cp_parser* parser)
29096 {
29097 tree proto;
29098 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29099
29100 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29101 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29102 proto = cp_parser_identifier (parser);
29103 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29104
29105 /* Build a location of the form:
29106 @protocol(prot)
29107 ^~~~~~~~~~~~~~~
29108 with caret==start at the @ token, finishing at the close paren. */
29109 location_t combined_loc
29110 = make_location (start_loc, start_loc,
29111 cp_lexer_previous_token (parser->lexer)->location);
29112 tree result = objc_build_protocol_expr (proto);
29113 protected_set_expr_location (result, combined_loc);
29114 return result;
29115 }
29116
29117 /* Parse an Objective-C selector expression.
29118
29119 objc-selector-expression:
29120 @selector ( objc-method-signature )
29121
29122 objc-method-signature:
29123 objc-selector
29124 objc-selector-seq
29125
29126 objc-selector-seq:
29127 objc-selector :
29128 objc-selector-seq objc-selector :
29129
29130 Returns a representation of the method selector. */
29131
29132 static tree
29133 cp_parser_objc_selector_expression (cp_parser* parser)
29134 {
29135 tree sel_seq = NULL_TREE;
29136 bool maybe_unary_selector_p = true;
29137 cp_token *token;
29138 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29139
29140 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29141 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29142 token = cp_lexer_peek_token (parser->lexer);
29143
29144 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29145 || token->type == CPP_SCOPE)
29146 {
29147 tree selector = NULL_TREE;
29148
29149 if (token->type != CPP_COLON
29150 || token->type == CPP_SCOPE)
29151 selector = cp_parser_objc_selector (parser);
29152
29153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29154 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29155 {
29156 /* Detect if we have a unary selector. */
29157 if (maybe_unary_selector_p)
29158 {
29159 sel_seq = selector;
29160 goto finish_selector;
29161 }
29162 else
29163 {
29164 cp_parser_error (parser, "expected %<:%>");
29165 }
29166 }
29167 maybe_unary_selector_p = false;
29168 token = cp_lexer_consume_token (parser->lexer);
29169
29170 if (token->type == CPP_SCOPE)
29171 {
29172 sel_seq
29173 = chainon (sel_seq,
29174 build_tree_list (selector, NULL_TREE));
29175 sel_seq
29176 = chainon (sel_seq,
29177 build_tree_list (NULL_TREE, NULL_TREE));
29178 }
29179 else
29180 sel_seq
29181 = chainon (sel_seq,
29182 build_tree_list (selector, NULL_TREE));
29183
29184 token = cp_lexer_peek_token (parser->lexer);
29185 }
29186
29187 finish_selector:
29188 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29189
29190
29191 /* Build a location of the form:
29192 @selector(func)
29193 ^~~~~~~~~~~~~~~
29194 with caret==start at the @ token, finishing at the close paren. */
29195 location_t combined_loc
29196 = make_location (loc, loc,
29197 cp_lexer_previous_token (parser->lexer)->location);
29198 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29199 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29200 protected_set_expr_location (result, combined_loc);
29201 return result;
29202 }
29203
29204 /* Parse a list of identifiers.
29205
29206 objc-identifier-list:
29207 identifier
29208 objc-identifier-list , identifier
29209
29210 Returns a TREE_LIST of identifier nodes. */
29211
29212 static tree
29213 cp_parser_objc_identifier_list (cp_parser* parser)
29214 {
29215 tree identifier;
29216 tree list;
29217 cp_token *sep;
29218
29219 identifier = cp_parser_identifier (parser);
29220 if (identifier == error_mark_node)
29221 return error_mark_node;
29222
29223 list = build_tree_list (NULL_TREE, identifier);
29224 sep = cp_lexer_peek_token (parser->lexer);
29225
29226 while (sep->type == CPP_COMMA)
29227 {
29228 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29229 identifier = cp_parser_identifier (parser);
29230 if (identifier == error_mark_node)
29231 return list;
29232
29233 list = chainon (list, build_tree_list (NULL_TREE,
29234 identifier));
29235 sep = cp_lexer_peek_token (parser->lexer);
29236 }
29237
29238 return list;
29239 }
29240
29241 /* Parse an Objective-C alias declaration.
29242
29243 objc-alias-declaration:
29244 @compatibility_alias identifier identifier ;
29245
29246 This function registers the alias mapping with the Objective-C front end.
29247 It returns nothing. */
29248
29249 static void
29250 cp_parser_objc_alias_declaration (cp_parser* parser)
29251 {
29252 tree alias, orig;
29253
29254 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29255 alias = cp_parser_identifier (parser);
29256 orig = cp_parser_identifier (parser);
29257 objc_declare_alias (alias, orig);
29258 cp_parser_consume_semicolon_at_end_of_statement (parser);
29259 }
29260
29261 /* Parse an Objective-C class forward-declaration.
29262
29263 objc-class-declaration:
29264 @class objc-identifier-list ;
29265
29266 The function registers the forward declarations with the Objective-C
29267 front end. It returns nothing. */
29268
29269 static void
29270 cp_parser_objc_class_declaration (cp_parser* parser)
29271 {
29272 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29273 while (true)
29274 {
29275 tree id;
29276
29277 id = cp_parser_identifier (parser);
29278 if (id == error_mark_node)
29279 break;
29280
29281 objc_declare_class (id);
29282
29283 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29284 cp_lexer_consume_token (parser->lexer);
29285 else
29286 break;
29287 }
29288 cp_parser_consume_semicolon_at_end_of_statement (parser);
29289 }
29290
29291 /* Parse a list of Objective-C protocol references.
29292
29293 objc-protocol-refs-opt:
29294 objc-protocol-refs [opt]
29295
29296 objc-protocol-refs:
29297 < objc-identifier-list >
29298
29299 Returns a TREE_LIST of identifiers, if any. */
29300
29301 static tree
29302 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29303 {
29304 tree protorefs = NULL_TREE;
29305
29306 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29307 {
29308 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29309 protorefs = cp_parser_objc_identifier_list (parser);
29310 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29311 }
29312
29313 return protorefs;
29314 }
29315
29316 /* Parse a Objective-C visibility specification. */
29317
29318 static void
29319 cp_parser_objc_visibility_spec (cp_parser* parser)
29320 {
29321 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29322
29323 switch (vis->keyword)
29324 {
29325 case RID_AT_PRIVATE:
29326 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29327 break;
29328 case RID_AT_PROTECTED:
29329 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29330 break;
29331 case RID_AT_PUBLIC:
29332 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29333 break;
29334 case RID_AT_PACKAGE:
29335 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29336 break;
29337 default:
29338 return;
29339 }
29340
29341 /* Eat '@private'/'@protected'/'@public'. */
29342 cp_lexer_consume_token (parser->lexer);
29343 }
29344
29345 /* Parse an Objective-C method type. Return 'true' if it is a class
29346 (+) method, and 'false' if it is an instance (-) method. */
29347
29348 static inline bool
29349 cp_parser_objc_method_type (cp_parser* parser)
29350 {
29351 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29352 return true;
29353 else
29354 return false;
29355 }
29356
29357 /* Parse an Objective-C protocol qualifier. */
29358
29359 static tree
29360 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29361 {
29362 tree quals = NULL_TREE, node;
29363 cp_token *token = cp_lexer_peek_token (parser->lexer);
29364
29365 node = token->u.value;
29366
29367 while (node && identifier_p (node)
29368 && (node == ridpointers [(int) RID_IN]
29369 || node == ridpointers [(int) RID_OUT]
29370 || node == ridpointers [(int) RID_INOUT]
29371 || node == ridpointers [(int) RID_BYCOPY]
29372 || node == ridpointers [(int) RID_BYREF]
29373 || node == ridpointers [(int) RID_ONEWAY]))
29374 {
29375 quals = tree_cons (NULL_TREE, node, quals);
29376 cp_lexer_consume_token (parser->lexer);
29377 token = cp_lexer_peek_token (parser->lexer);
29378 node = token->u.value;
29379 }
29380
29381 return quals;
29382 }
29383
29384 /* Parse an Objective-C typename. */
29385
29386 static tree
29387 cp_parser_objc_typename (cp_parser* parser)
29388 {
29389 tree type_name = NULL_TREE;
29390
29391 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29392 {
29393 tree proto_quals, cp_type = NULL_TREE;
29394
29395 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29396 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29397
29398 /* An ObjC type name may consist of just protocol qualifiers, in which
29399 case the type shall default to 'id'. */
29400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29401 {
29402 cp_type = cp_parser_type_id (parser);
29403
29404 /* If the type could not be parsed, an error has already
29405 been produced. For error recovery, behave as if it had
29406 not been specified, which will use the default type
29407 'id'. */
29408 if (cp_type == error_mark_node)
29409 {
29410 cp_type = NULL_TREE;
29411 /* We need to skip to the closing parenthesis as
29412 cp_parser_type_id() does not seem to do it for
29413 us. */
29414 cp_parser_skip_to_closing_parenthesis (parser,
29415 /*recovering=*/true,
29416 /*or_comma=*/false,
29417 /*consume_paren=*/false);
29418 }
29419 }
29420
29421 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29422 type_name = build_tree_list (proto_quals, cp_type);
29423 }
29424
29425 return type_name;
29426 }
29427
29428 /* Check to see if TYPE refers to an Objective-C selector name. */
29429
29430 static bool
29431 cp_parser_objc_selector_p (enum cpp_ttype type)
29432 {
29433 return (type == CPP_NAME || type == CPP_KEYWORD
29434 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29435 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29436 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29437 || type == CPP_XOR || type == CPP_XOR_EQ);
29438 }
29439
29440 /* Parse an Objective-C selector. */
29441
29442 static tree
29443 cp_parser_objc_selector (cp_parser* parser)
29444 {
29445 cp_token *token = cp_lexer_consume_token (parser->lexer);
29446
29447 if (!cp_parser_objc_selector_p (token->type))
29448 {
29449 error_at (token->location, "invalid Objective-C++ selector name");
29450 return error_mark_node;
29451 }
29452
29453 /* C++ operator names are allowed to appear in ObjC selectors. */
29454 switch (token->type)
29455 {
29456 case CPP_AND_AND: return get_identifier ("and");
29457 case CPP_AND_EQ: return get_identifier ("and_eq");
29458 case CPP_AND: return get_identifier ("bitand");
29459 case CPP_OR: return get_identifier ("bitor");
29460 case CPP_COMPL: return get_identifier ("compl");
29461 case CPP_NOT: return get_identifier ("not");
29462 case CPP_NOT_EQ: return get_identifier ("not_eq");
29463 case CPP_OR_OR: return get_identifier ("or");
29464 case CPP_OR_EQ: return get_identifier ("or_eq");
29465 case CPP_XOR: return get_identifier ("xor");
29466 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29467 default: return token->u.value;
29468 }
29469 }
29470
29471 /* Parse an Objective-C params list. */
29472
29473 static tree
29474 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29475 {
29476 tree params = NULL_TREE;
29477 bool maybe_unary_selector_p = true;
29478 cp_token *token = cp_lexer_peek_token (parser->lexer);
29479
29480 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29481 {
29482 tree selector = NULL_TREE, type_name, identifier;
29483 tree parm_attr = NULL_TREE;
29484
29485 if (token->keyword == RID_ATTRIBUTE)
29486 break;
29487
29488 if (token->type != CPP_COLON)
29489 selector = cp_parser_objc_selector (parser);
29490
29491 /* Detect if we have a unary selector. */
29492 if (maybe_unary_selector_p
29493 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29494 {
29495 params = selector; /* Might be followed by attributes. */
29496 break;
29497 }
29498
29499 maybe_unary_selector_p = false;
29500 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29501 {
29502 /* Something went quite wrong. There should be a colon
29503 here, but there is not. Stop parsing parameters. */
29504 break;
29505 }
29506 type_name = cp_parser_objc_typename (parser);
29507 /* New ObjC allows attributes on parameters too. */
29508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29509 parm_attr = cp_parser_attributes_opt (parser);
29510 identifier = cp_parser_identifier (parser);
29511
29512 params
29513 = chainon (params,
29514 objc_build_keyword_decl (selector,
29515 type_name,
29516 identifier,
29517 parm_attr));
29518
29519 token = cp_lexer_peek_token (parser->lexer);
29520 }
29521
29522 if (params == NULL_TREE)
29523 {
29524 cp_parser_error (parser, "objective-c++ method declaration is expected");
29525 return error_mark_node;
29526 }
29527
29528 /* We allow tail attributes for the method. */
29529 if (token->keyword == RID_ATTRIBUTE)
29530 {
29531 *attributes = cp_parser_attributes_opt (parser);
29532 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29533 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29534 return params;
29535 cp_parser_error (parser,
29536 "method attributes must be specified at the end");
29537 return error_mark_node;
29538 }
29539
29540 if (params == NULL_TREE)
29541 {
29542 cp_parser_error (parser, "objective-c++ method declaration is expected");
29543 return error_mark_node;
29544 }
29545 return params;
29546 }
29547
29548 /* Parse the non-keyword Objective-C params. */
29549
29550 static tree
29551 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29552 tree* attributes)
29553 {
29554 tree params = make_node (TREE_LIST);
29555 cp_token *token = cp_lexer_peek_token (parser->lexer);
29556 *ellipsisp = false; /* Initially, assume no ellipsis. */
29557
29558 while (token->type == CPP_COMMA)
29559 {
29560 cp_parameter_declarator *parmdecl;
29561 tree parm;
29562
29563 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29564 token = cp_lexer_peek_token (parser->lexer);
29565
29566 if (token->type == CPP_ELLIPSIS)
29567 {
29568 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29569 *ellipsisp = true;
29570 token = cp_lexer_peek_token (parser->lexer);
29571 break;
29572 }
29573
29574 /* TODO: parse attributes for tail parameters. */
29575 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29576 parm = grokdeclarator (parmdecl->declarator,
29577 &parmdecl->decl_specifiers,
29578 PARM, /*initialized=*/0,
29579 /*attrlist=*/NULL);
29580
29581 chainon (params, build_tree_list (NULL_TREE, parm));
29582 token = cp_lexer_peek_token (parser->lexer);
29583 }
29584
29585 /* We allow tail attributes for the method. */
29586 if (token->keyword == RID_ATTRIBUTE)
29587 {
29588 if (*attributes == NULL_TREE)
29589 {
29590 *attributes = cp_parser_attributes_opt (parser);
29591 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29592 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29593 return params;
29594 }
29595 else
29596 /* We have an error, but parse the attributes, so that we can
29597 carry on. */
29598 *attributes = cp_parser_attributes_opt (parser);
29599
29600 cp_parser_error (parser,
29601 "method attributes must be specified at the end");
29602 return error_mark_node;
29603 }
29604
29605 return params;
29606 }
29607
29608 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29609
29610 static void
29611 cp_parser_objc_interstitial_code (cp_parser* parser)
29612 {
29613 cp_token *token = cp_lexer_peek_token (parser->lexer);
29614
29615 /* If the next token is `extern' and the following token is a string
29616 literal, then we have a linkage specification. */
29617 if (token->keyword == RID_EXTERN
29618 && cp_parser_is_pure_string_literal
29619 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29620 cp_parser_linkage_specification (parser);
29621 /* Handle #pragma, if any. */
29622 else if (token->type == CPP_PRAGMA)
29623 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29624 /* Allow stray semicolons. */
29625 else if (token->type == CPP_SEMICOLON)
29626 cp_lexer_consume_token (parser->lexer);
29627 /* Mark methods as optional or required, when building protocols. */
29628 else if (token->keyword == RID_AT_OPTIONAL)
29629 {
29630 cp_lexer_consume_token (parser->lexer);
29631 objc_set_method_opt (true);
29632 }
29633 else if (token->keyword == RID_AT_REQUIRED)
29634 {
29635 cp_lexer_consume_token (parser->lexer);
29636 objc_set_method_opt (false);
29637 }
29638 else if (token->keyword == RID_NAMESPACE)
29639 cp_parser_namespace_definition (parser);
29640 /* Other stray characters must generate errors. */
29641 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29642 {
29643 cp_lexer_consume_token (parser->lexer);
29644 error ("stray %qs between Objective-C++ methods",
29645 token->type == CPP_OPEN_BRACE ? "{" : "}");
29646 }
29647 /* Finally, try to parse a block-declaration, or a function-definition. */
29648 else
29649 cp_parser_block_declaration (parser, /*statement_p=*/false);
29650 }
29651
29652 /* Parse a method signature. */
29653
29654 static tree
29655 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29656 {
29657 tree rettype, kwdparms, optparms;
29658 bool ellipsis = false;
29659 bool is_class_method;
29660
29661 is_class_method = cp_parser_objc_method_type (parser);
29662 rettype = cp_parser_objc_typename (parser);
29663 *attributes = NULL_TREE;
29664 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29665 if (kwdparms == error_mark_node)
29666 return error_mark_node;
29667 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29668 if (optparms == error_mark_node)
29669 return error_mark_node;
29670
29671 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29672 }
29673
29674 static bool
29675 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29676 {
29677 tree tattr;
29678 cp_lexer_save_tokens (parser->lexer);
29679 tattr = cp_parser_attributes_opt (parser);
29680 gcc_assert (tattr) ;
29681
29682 /* If the attributes are followed by a method introducer, this is not allowed.
29683 Dump the attributes and flag the situation. */
29684 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29685 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29686 return true;
29687
29688 /* Otherwise, the attributes introduce some interstitial code, possibly so
29689 rewind to allow that check. */
29690 cp_lexer_rollback_tokens (parser->lexer);
29691 return false;
29692 }
29693
29694 /* Parse an Objective-C method prototype list. */
29695
29696 static void
29697 cp_parser_objc_method_prototype_list (cp_parser* parser)
29698 {
29699 cp_token *token = cp_lexer_peek_token (parser->lexer);
29700
29701 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29702 {
29703 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29704 {
29705 tree attributes, sig;
29706 bool is_class_method;
29707 if (token->type == CPP_PLUS)
29708 is_class_method = true;
29709 else
29710 is_class_method = false;
29711 sig = cp_parser_objc_method_signature (parser, &attributes);
29712 if (sig == error_mark_node)
29713 {
29714 cp_parser_skip_to_end_of_block_or_statement (parser);
29715 token = cp_lexer_peek_token (parser->lexer);
29716 continue;
29717 }
29718 objc_add_method_declaration (is_class_method, sig, attributes);
29719 cp_parser_consume_semicolon_at_end_of_statement (parser);
29720 }
29721 else if (token->keyword == RID_AT_PROPERTY)
29722 cp_parser_objc_at_property_declaration (parser);
29723 else if (token->keyword == RID_ATTRIBUTE
29724 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29725 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29726 OPT_Wattributes,
29727 "prefix attributes are ignored for methods");
29728 else
29729 /* Allow for interspersed non-ObjC++ code. */
29730 cp_parser_objc_interstitial_code (parser);
29731
29732 token = cp_lexer_peek_token (parser->lexer);
29733 }
29734
29735 if (token->type != CPP_EOF)
29736 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29737 else
29738 cp_parser_error (parser, "expected %<@end%>");
29739
29740 objc_finish_interface ();
29741 }
29742
29743 /* Parse an Objective-C method definition list. */
29744
29745 static void
29746 cp_parser_objc_method_definition_list (cp_parser* parser)
29747 {
29748 cp_token *token = cp_lexer_peek_token (parser->lexer);
29749
29750 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29751 {
29752 tree meth;
29753
29754 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29755 {
29756 cp_token *ptk;
29757 tree sig, attribute;
29758 bool is_class_method;
29759 if (token->type == CPP_PLUS)
29760 is_class_method = true;
29761 else
29762 is_class_method = false;
29763 push_deferring_access_checks (dk_deferred);
29764 sig = cp_parser_objc_method_signature (parser, &attribute);
29765 if (sig == error_mark_node)
29766 {
29767 cp_parser_skip_to_end_of_block_or_statement (parser);
29768 token = cp_lexer_peek_token (parser->lexer);
29769 continue;
29770 }
29771 objc_start_method_definition (is_class_method, sig, attribute,
29772 NULL_TREE);
29773
29774 /* For historical reasons, we accept an optional semicolon. */
29775 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29776 cp_lexer_consume_token (parser->lexer);
29777
29778 ptk = cp_lexer_peek_token (parser->lexer);
29779 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29780 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29781 {
29782 perform_deferred_access_checks (tf_warning_or_error);
29783 stop_deferring_access_checks ();
29784 meth = cp_parser_function_definition_after_declarator (parser,
29785 false);
29786 pop_deferring_access_checks ();
29787 objc_finish_method_definition (meth);
29788 }
29789 }
29790 /* The following case will be removed once @synthesize is
29791 completely implemented. */
29792 else if (token->keyword == RID_AT_PROPERTY)
29793 cp_parser_objc_at_property_declaration (parser);
29794 else if (token->keyword == RID_AT_SYNTHESIZE)
29795 cp_parser_objc_at_synthesize_declaration (parser);
29796 else if (token->keyword == RID_AT_DYNAMIC)
29797 cp_parser_objc_at_dynamic_declaration (parser);
29798 else if (token->keyword == RID_ATTRIBUTE
29799 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29800 warning_at (token->location, OPT_Wattributes,
29801 "prefix attributes are ignored for methods");
29802 else
29803 /* Allow for interspersed non-ObjC++ code. */
29804 cp_parser_objc_interstitial_code (parser);
29805
29806 token = cp_lexer_peek_token (parser->lexer);
29807 }
29808
29809 if (token->type != CPP_EOF)
29810 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29811 else
29812 cp_parser_error (parser, "expected %<@end%>");
29813
29814 objc_finish_implementation ();
29815 }
29816
29817 /* Parse Objective-C ivars. */
29818
29819 static void
29820 cp_parser_objc_class_ivars (cp_parser* parser)
29821 {
29822 cp_token *token = cp_lexer_peek_token (parser->lexer);
29823
29824 if (token->type != CPP_OPEN_BRACE)
29825 return; /* No ivars specified. */
29826
29827 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29828 token = cp_lexer_peek_token (parser->lexer);
29829
29830 while (token->type != CPP_CLOSE_BRACE
29831 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29832 {
29833 cp_decl_specifier_seq declspecs;
29834 int decl_class_or_enum_p;
29835 tree prefix_attributes;
29836
29837 cp_parser_objc_visibility_spec (parser);
29838
29839 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29840 break;
29841
29842 cp_parser_decl_specifier_seq (parser,
29843 CP_PARSER_FLAGS_OPTIONAL,
29844 &declspecs,
29845 &decl_class_or_enum_p);
29846
29847 /* auto, register, static, extern, mutable. */
29848 if (declspecs.storage_class != sc_none)
29849 {
29850 cp_parser_error (parser, "invalid type for instance variable");
29851 declspecs.storage_class = sc_none;
29852 }
29853
29854 /* thread_local. */
29855 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29856 {
29857 cp_parser_error (parser, "invalid type for instance variable");
29858 declspecs.locations[ds_thread] = 0;
29859 }
29860
29861 /* typedef. */
29862 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29863 {
29864 cp_parser_error (parser, "invalid type for instance variable");
29865 declspecs.locations[ds_typedef] = 0;
29866 }
29867
29868 prefix_attributes = declspecs.attributes;
29869 declspecs.attributes = NULL_TREE;
29870
29871 /* Keep going until we hit the `;' at the end of the
29872 declaration. */
29873 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29874 {
29875 tree width = NULL_TREE, attributes, first_attribute, decl;
29876 cp_declarator *declarator = NULL;
29877 int ctor_dtor_or_conv_p;
29878
29879 /* Check for a (possibly unnamed) bitfield declaration. */
29880 token = cp_lexer_peek_token (parser->lexer);
29881 if (token->type == CPP_COLON)
29882 goto eat_colon;
29883
29884 if (token->type == CPP_NAME
29885 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29886 == CPP_COLON))
29887 {
29888 /* Get the name of the bitfield. */
29889 declarator = make_id_declarator (NULL_TREE,
29890 cp_parser_identifier (parser),
29891 sfk_none);
29892
29893 eat_colon:
29894 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29895 /* Get the width of the bitfield. */
29896 width
29897 = cp_parser_constant_expression (parser);
29898 }
29899 else
29900 {
29901 /* Parse the declarator. */
29902 declarator
29903 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29904 &ctor_dtor_or_conv_p,
29905 /*parenthesized_p=*/NULL,
29906 /*member_p=*/false,
29907 /*friend_p=*/false);
29908 }
29909
29910 /* Look for attributes that apply to the ivar. */
29911 attributes = cp_parser_attributes_opt (parser);
29912 /* Remember which attributes are prefix attributes and
29913 which are not. */
29914 first_attribute = attributes;
29915 /* Combine the attributes. */
29916 attributes = chainon (prefix_attributes, attributes);
29917
29918 if (width)
29919 /* Create the bitfield declaration. */
29920 decl = grokbitfield (declarator, &declspecs,
29921 width,
29922 attributes);
29923 else
29924 decl = grokfield (declarator, &declspecs,
29925 NULL_TREE, /*init_const_expr_p=*/false,
29926 NULL_TREE, attributes);
29927
29928 /* Add the instance variable. */
29929 if (decl != error_mark_node && decl != NULL_TREE)
29930 objc_add_instance_variable (decl);
29931
29932 /* Reset PREFIX_ATTRIBUTES. */
29933 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29934 attributes = TREE_CHAIN (attributes);
29935 if (attributes)
29936 TREE_CHAIN (attributes) = NULL_TREE;
29937
29938 token = cp_lexer_peek_token (parser->lexer);
29939
29940 if (token->type == CPP_COMMA)
29941 {
29942 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29943 continue;
29944 }
29945 break;
29946 }
29947
29948 cp_parser_consume_semicolon_at_end_of_statement (parser);
29949 token = cp_lexer_peek_token (parser->lexer);
29950 }
29951
29952 if (token->keyword == RID_AT_END)
29953 cp_parser_error (parser, "expected %<}%>");
29954
29955 /* Do not consume the RID_AT_END, so it will be read again as terminating
29956 the @interface of @implementation. */
29957 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29958 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29959
29960 /* For historical reasons, we accept an optional semicolon. */
29961 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29962 cp_lexer_consume_token (parser->lexer);
29963 }
29964
29965 /* Parse an Objective-C protocol declaration. */
29966
29967 static void
29968 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29969 {
29970 tree proto, protorefs;
29971 cp_token *tok;
29972
29973 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29974 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29975 {
29976 tok = cp_lexer_peek_token (parser->lexer);
29977 error_at (tok->location, "identifier expected after %<@protocol%>");
29978 cp_parser_consume_semicolon_at_end_of_statement (parser);
29979 return;
29980 }
29981
29982 /* See if we have a forward declaration or a definition. */
29983 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29984
29985 /* Try a forward declaration first. */
29986 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29987 {
29988 while (true)
29989 {
29990 tree id;
29991
29992 id = cp_parser_identifier (parser);
29993 if (id == error_mark_node)
29994 break;
29995
29996 objc_declare_protocol (id, attributes);
29997
29998 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29999 cp_lexer_consume_token (parser->lexer);
30000 else
30001 break;
30002 }
30003 cp_parser_consume_semicolon_at_end_of_statement (parser);
30004 }
30005
30006 /* Ok, we got a full-fledged definition (or at least should). */
30007 else
30008 {
30009 proto = cp_parser_identifier (parser);
30010 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30011 objc_start_protocol (proto, protorefs, attributes);
30012 cp_parser_objc_method_prototype_list (parser);
30013 }
30014 }
30015
30016 /* Parse an Objective-C superclass or category. */
30017
30018 static void
30019 cp_parser_objc_superclass_or_category (cp_parser *parser,
30020 bool iface_p,
30021 tree *super,
30022 tree *categ, bool *is_class_extension)
30023 {
30024 cp_token *next = cp_lexer_peek_token (parser->lexer);
30025
30026 *super = *categ = NULL_TREE;
30027 *is_class_extension = false;
30028 if (next->type == CPP_COLON)
30029 {
30030 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30031 *super = cp_parser_identifier (parser);
30032 }
30033 else if (next->type == CPP_OPEN_PAREN)
30034 {
30035 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
30036
30037 /* If there is no category name, and this is an @interface, we
30038 have a class extension. */
30039 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30040 {
30041 *categ = NULL_TREE;
30042 *is_class_extension = true;
30043 }
30044 else
30045 *categ = cp_parser_identifier (parser);
30046
30047 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30048 }
30049 }
30050
30051 /* Parse an Objective-C class interface. */
30052
30053 static void
30054 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30055 {
30056 tree name, super, categ, protos;
30057 bool is_class_extension;
30058
30059 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30060 name = cp_parser_identifier (parser);
30061 if (name == error_mark_node)
30062 {
30063 /* It's hard to recover because even if valid @interface stuff
30064 is to follow, we can't compile it (or validate it) if we
30065 don't even know which class it refers to. Let's assume this
30066 was a stray '@interface' token in the stream and skip it.
30067 */
30068 return;
30069 }
30070 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30071 &is_class_extension);
30072 protos = cp_parser_objc_protocol_refs_opt (parser);
30073
30074 /* We have either a class or a category on our hands. */
30075 if (categ || is_class_extension)
30076 objc_start_category_interface (name, categ, protos, attributes);
30077 else
30078 {
30079 objc_start_class_interface (name, super, protos, attributes);
30080 /* Handle instance variable declarations, if any. */
30081 cp_parser_objc_class_ivars (parser);
30082 objc_continue_interface ();
30083 }
30084
30085 cp_parser_objc_method_prototype_list (parser);
30086 }
30087
30088 /* Parse an Objective-C class implementation. */
30089
30090 static void
30091 cp_parser_objc_class_implementation (cp_parser* parser)
30092 {
30093 tree name, super, categ;
30094 bool is_class_extension;
30095
30096 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30097 name = cp_parser_identifier (parser);
30098 if (name == error_mark_node)
30099 {
30100 /* It's hard to recover because even if valid @implementation
30101 stuff is to follow, we can't compile it (or validate it) if
30102 we don't even know which class it refers to. Let's assume
30103 this was a stray '@implementation' token in the stream and
30104 skip it.
30105 */
30106 return;
30107 }
30108 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30109 &is_class_extension);
30110
30111 /* We have either a class or a category on our hands. */
30112 if (categ)
30113 objc_start_category_implementation (name, categ);
30114 else
30115 {
30116 objc_start_class_implementation (name, super);
30117 /* Handle instance variable declarations, if any. */
30118 cp_parser_objc_class_ivars (parser);
30119 objc_continue_implementation ();
30120 }
30121
30122 cp_parser_objc_method_definition_list (parser);
30123 }
30124
30125 /* Consume the @end token and finish off the implementation. */
30126
30127 static void
30128 cp_parser_objc_end_implementation (cp_parser* parser)
30129 {
30130 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30131 objc_finish_implementation ();
30132 }
30133
30134 /* Parse an Objective-C declaration. */
30135
30136 static void
30137 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30138 {
30139 /* Try to figure out what kind of declaration is present. */
30140 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30141
30142 if (attributes)
30143 switch (kwd->keyword)
30144 {
30145 case RID_AT_ALIAS:
30146 case RID_AT_CLASS:
30147 case RID_AT_END:
30148 error_at (kwd->location, "attributes may not be specified before"
30149 " the %<@%D%> Objective-C++ keyword",
30150 kwd->u.value);
30151 attributes = NULL;
30152 break;
30153 case RID_AT_IMPLEMENTATION:
30154 warning_at (kwd->location, OPT_Wattributes,
30155 "prefix attributes are ignored before %<@%D%>",
30156 kwd->u.value);
30157 attributes = NULL;
30158 default:
30159 break;
30160 }
30161
30162 switch (kwd->keyword)
30163 {
30164 case RID_AT_ALIAS:
30165 cp_parser_objc_alias_declaration (parser);
30166 break;
30167 case RID_AT_CLASS:
30168 cp_parser_objc_class_declaration (parser);
30169 break;
30170 case RID_AT_PROTOCOL:
30171 cp_parser_objc_protocol_declaration (parser, attributes);
30172 break;
30173 case RID_AT_INTERFACE:
30174 cp_parser_objc_class_interface (parser, attributes);
30175 break;
30176 case RID_AT_IMPLEMENTATION:
30177 cp_parser_objc_class_implementation (parser);
30178 break;
30179 case RID_AT_END:
30180 cp_parser_objc_end_implementation (parser);
30181 break;
30182 default:
30183 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30184 kwd->u.value);
30185 cp_parser_skip_to_end_of_block_or_statement (parser);
30186 }
30187 }
30188
30189 /* Parse an Objective-C try-catch-finally statement.
30190
30191 objc-try-catch-finally-stmt:
30192 @try compound-statement objc-catch-clause-seq [opt]
30193 objc-finally-clause [opt]
30194
30195 objc-catch-clause-seq:
30196 objc-catch-clause objc-catch-clause-seq [opt]
30197
30198 objc-catch-clause:
30199 @catch ( objc-exception-declaration ) compound-statement
30200
30201 objc-finally-clause:
30202 @finally compound-statement
30203
30204 objc-exception-declaration:
30205 parameter-declaration
30206 '...'
30207
30208 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30209
30210 Returns NULL_TREE.
30211
30212 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30213 for C. Keep them in sync. */
30214
30215 static tree
30216 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30217 {
30218 location_t location;
30219 tree stmt;
30220
30221 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30222 location = cp_lexer_peek_token (parser->lexer)->location;
30223 objc_maybe_warn_exceptions (location);
30224 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30225 node, lest it get absorbed into the surrounding block. */
30226 stmt = push_stmt_list ();
30227 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30228 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30229
30230 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30231 {
30232 cp_parameter_declarator *parm;
30233 tree parameter_declaration = error_mark_node;
30234 bool seen_open_paren = false;
30235
30236 cp_lexer_consume_token (parser->lexer);
30237 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30238 seen_open_paren = true;
30239 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30240 {
30241 /* We have "@catch (...)" (where the '...' are literally
30242 what is in the code). Skip the '...'.
30243 parameter_declaration is set to NULL_TREE, and
30244 objc_being_catch_clauses() knows that that means
30245 '...'. */
30246 cp_lexer_consume_token (parser->lexer);
30247 parameter_declaration = NULL_TREE;
30248 }
30249 else
30250 {
30251 /* We have "@catch (NSException *exception)" or something
30252 like that. Parse the parameter declaration. */
30253 parm = cp_parser_parameter_declaration (parser, false, NULL);
30254 if (parm == NULL)
30255 parameter_declaration = error_mark_node;
30256 else
30257 parameter_declaration = grokdeclarator (parm->declarator,
30258 &parm->decl_specifiers,
30259 PARM, /*initialized=*/0,
30260 /*attrlist=*/NULL);
30261 }
30262 if (seen_open_paren)
30263 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30264 else
30265 {
30266 /* If there was no open parenthesis, we are recovering from
30267 an error, and we are trying to figure out what mistake
30268 the user has made. */
30269
30270 /* If there is an immediate closing parenthesis, the user
30271 probably forgot the opening one (ie, they typed "@catch
30272 NSException *e)". Parse the closing parenthesis and keep
30273 going. */
30274 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30275 cp_lexer_consume_token (parser->lexer);
30276
30277 /* If these is no immediate closing parenthesis, the user
30278 probably doesn't know that parenthesis are required at
30279 all (ie, they typed "@catch NSException *e"). So, just
30280 forget about the closing parenthesis and keep going. */
30281 }
30282 objc_begin_catch_clause (parameter_declaration);
30283 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30284 objc_finish_catch_clause ();
30285 }
30286 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30287 {
30288 cp_lexer_consume_token (parser->lexer);
30289 location = cp_lexer_peek_token (parser->lexer)->location;
30290 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30291 node, lest it get absorbed into the surrounding block. */
30292 stmt = push_stmt_list ();
30293 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30294 objc_build_finally_clause (location, pop_stmt_list (stmt));
30295 }
30296
30297 return objc_finish_try_stmt ();
30298 }
30299
30300 /* Parse an Objective-C synchronized statement.
30301
30302 objc-synchronized-stmt:
30303 @synchronized ( expression ) compound-statement
30304
30305 Returns NULL_TREE. */
30306
30307 static tree
30308 cp_parser_objc_synchronized_statement (cp_parser *parser)
30309 {
30310 location_t location;
30311 tree lock, stmt;
30312
30313 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30314
30315 location = cp_lexer_peek_token (parser->lexer)->location;
30316 objc_maybe_warn_exceptions (location);
30317 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30318 lock = cp_parser_expression (parser);
30319 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30320
30321 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30322 node, lest it get absorbed into the surrounding block. */
30323 stmt = push_stmt_list ();
30324 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30325
30326 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30327 }
30328
30329 /* Parse an Objective-C throw statement.
30330
30331 objc-throw-stmt:
30332 @throw assignment-expression [opt] ;
30333
30334 Returns a constructed '@throw' statement. */
30335
30336 static tree
30337 cp_parser_objc_throw_statement (cp_parser *parser)
30338 {
30339 tree expr = NULL_TREE;
30340 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30341
30342 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30343
30344 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30345 expr = cp_parser_expression (parser);
30346
30347 cp_parser_consume_semicolon_at_end_of_statement (parser);
30348
30349 return objc_build_throw_stmt (loc, expr);
30350 }
30351
30352 /* Parse an Objective-C statement. */
30353
30354 static tree
30355 cp_parser_objc_statement (cp_parser * parser)
30356 {
30357 /* Try to figure out what kind of declaration is present. */
30358 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30359
30360 switch (kwd->keyword)
30361 {
30362 case RID_AT_TRY:
30363 return cp_parser_objc_try_catch_finally_statement (parser);
30364 case RID_AT_SYNCHRONIZED:
30365 return cp_parser_objc_synchronized_statement (parser);
30366 case RID_AT_THROW:
30367 return cp_parser_objc_throw_statement (parser);
30368 default:
30369 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30370 kwd->u.value);
30371 cp_parser_skip_to_end_of_block_or_statement (parser);
30372 }
30373
30374 return error_mark_node;
30375 }
30376
30377 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30378 look ahead to see if an objc keyword follows the attributes. This
30379 is to detect the use of prefix attributes on ObjC @interface and
30380 @protocol. */
30381
30382 static bool
30383 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30384 {
30385 cp_lexer_save_tokens (parser->lexer);
30386 *attrib = cp_parser_attributes_opt (parser);
30387 gcc_assert (*attrib);
30388 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30389 {
30390 cp_lexer_commit_tokens (parser->lexer);
30391 return true;
30392 }
30393 cp_lexer_rollback_tokens (parser->lexer);
30394 return false;
30395 }
30396
30397 /* This routine is a minimal replacement for
30398 c_parser_struct_declaration () used when parsing the list of
30399 types/names or ObjC++ properties. For example, when parsing the
30400 code
30401
30402 @property (readonly) int a, b, c;
30403
30404 this function is responsible for parsing "int a, int b, int c" and
30405 returning the declarations as CHAIN of DECLs.
30406
30407 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30408 similar parsing. */
30409 static tree
30410 cp_parser_objc_struct_declaration (cp_parser *parser)
30411 {
30412 tree decls = NULL_TREE;
30413 cp_decl_specifier_seq declspecs;
30414 int decl_class_or_enum_p;
30415 tree prefix_attributes;
30416
30417 cp_parser_decl_specifier_seq (parser,
30418 CP_PARSER_FLAGS_NONE,
30419 &declspecs,
30420 &decl_class_or_enum_p);
30421
30422 if (declspecs.type == error_mark_node)
30423 return error_mark_node;
30424
30425 /* auto, register, static, extern, mutable. */
30426 if (declspecs.storage_class != sc_none)
30427 {
30428 cp_parser_error (parser, "invalid type for property");
30429 declspecs.storage_class = sc_none;
30430 }
30431
30432 /* thread_local. */
30433 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30434 {
30435 cp_parser_error (parser, "invalid type for property");
30436 declspecs.locations[ds_thread] = 0;
30437 }
30438
30439 /* typedef. */
30440 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30441 {
30442 cp_parser_error (parser, "invalid type for property");
30443 declspecs.locations[ds_typedef] = 0;
30444 }
30445
30446 prefix_attributes = declspecs.attributes;
30447 declspecs.attributes = NULL_TREE;
30448
30449 /* Keep going until we hit the `;' at the end of the declaration. */
30450 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30451 {
30452 tree attributes, first_attribute, decl;
30453 cp_declarator *declarator;
30454 cp_token *token;
30455
30456 /* Parse the declarator. */
30457 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30458 NULL, NULL, false, false);
30459
30460 /* Look for attributes that apply to the ivar. */
30461 attributes = cp_parser_attributes_opt (parser);
30462 /* Remember which attributes are prefix attributes and
30463 which are not. */
30464 first_attribute = attributes;
30465 /* Combine the attributes. */
30466 attributes = chainon (prefix_attributes, attributes);
30467
30468 decl = grokfield (declarator, &declspecs,
30469 NULL_TREE, /*init_const_expr_p=*/false,
30470 NULL_TREE, attributes);
30471
30472 if (decl == error_mark_node || decl == NULL_TREE)
30473 return error_mark_node;
30474
30475 /* Reset PREFIX_ATTRIBUTES. */
30476 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30477 attributes = TREE_CHAIN (attributes);
30478 if (attributes)
30479 TREE_CHAIN (attributes) = NULL_TREE;
30480
30481 DECL_CHAIN (decl) = decls;
30482 decls = decl;
30483
30484 token = cp_lexer_peek_token (parser->lexer);
30485 if (token->type == CPP_COMMA)
30486 {
30487 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30488 continue;
30489 }
30490 else
30491 break;
30492 }
30493 return decls;
30494 }
30495
30496 /* Parse an Objective-C @property declaration. The syntax is:
30497
30498 objc-property-declaration:
30499 '@property' objc-property-attributes[opt] struct-declaration ;
30500
30501 objc-property-attributes:
30502 '(' objc-property-attribute-list ')'
30503
30504 objc-property-attribute-list:
30505 objc-property-attribute
30506 objc-property-attribute-list, objc-property-attribute
30507
30508 objc-property-attribute
30509 'getter' = identifier
30510 'setter' = identifier
30511 'readonly'
30512 'readwrite'
30513 'assign'
30514 'retain'
30515 'copy'
30516 'nonatomic'
30517
30518 For example:
30519 @property NSString *name;
30520 @property (readonly) id object;
30521 @property (retain, nonatomic, getter=getTheName) id name;
30522 @property int a, b, c;
30523
30524 PS: This function is identical to
30525 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30526 static void
30527 cp_parser_objc_at_property_declaration (cp_parser *parser)
30528 {
30529 /* The following variables hold the attributes of the properties as
30530 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30531 seen. When we see an attribute, we set them to 'true' (if they
30532 are boolean properties) or to the identifier (if they have an
30533 argument, ie, for getter and setter). Note that here we only
30534 parse the list of attributes, check the syntax and accumulate the
30535 attributes that we find. objc_add_property_declaration() will
30536 then process the information. */
30537 bool property_assign = false;
30538 bool property_copy = false;
30539 tree property_getter_ident = NULL_TREE;
30540 bool property_nonatomic = false;
30541 bool property_readonly = false;
30542 bool property_readwrite = false;
30543 bool property_retain = false;
30544 tree property_setter_ident = NULL_TREE;
30545
30546 /* 'properties' is the list of properties that we read. Usually a
30547 single one, but maybe more (eg, in "@property int a, b, c;" there
30548 are three). */
30549 tree properties;
30550 location_t loc;
30551
30552 loc = cp_lexer_peek_token (parser->lexer)->location;
30553
30554 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30555
30556 /* Parse the optional attribute list... */
30557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30558 {
30559 /* Eat the '('. */
30560 cp_lexer_consume_token (parser->lexer);
30561
30562 while (true)
30563 {
30564 bool syntax_error = false;
30565 cp_token *token = cp_lexer_peek_token (parser->lexer);
30566 enum rid keyword;
30567
30568 if (token->type != CPP_NAME)
30569 {
30570 cp_parser_error (parser, "expected identifier");
30571 break;
30572 }
30573 keyword = C_RID_CODE (token->u.value);
30574 cp_lexer_consume_token (parser->lexer);
30575 switch (keyword)
30576 {
30577 case RID_ASSIGN: property_assign = true; break;
30578 case RID_COPY: property_copy = true; break;
30579 case RID_NONATOMIC: property_nonatomic = true; break;
30580 case RID_READONLY: property_readonly = true; break;
30581 case RID_READWRITE: property_readwrite = true; break;
30582 case RID_RETAIN: property_retain = true; break;
30583
30584 case RID_GETTER:
30585 case RID_SETTER:
30586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30587 {
30588 if (keyword == RID_GETTER)
30589 cp_parser_error (parser,
30590 "missing %<=%> (after %<getter%> attribute)");
30591 else
30592 cp_parser_error (parser,
30593 "missing %<=%> (after %<setter%> attribute)");
30594 syntax_error = true;
30595 break;
30596 }
30597 cp_lexer_consume_token (parser->lexer); /* eat the = */
30598 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30599 {
30600 cp_parser_error (parser, "expected identifier");
30601 syntax_error = true;
30602 break;
30603 }
30604 if (keyword == RID_SETTER)
30605 {
30606 if (property_setter_ident != NULL_TREE)
30607 {
30608 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30609 cp_lexer_consume_token (parser->lexer);
30610 }
30611 else
30612 property_setter_ident = cp_parser_objc_selector (parser);
30613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30614 cp_parser_error (parser, "setter name must terminate with %<:%>");
30615 else
30616 cp_lexer_consume_token (parser->lexer);
30617 }
30618 else
30619 {
30620 if (property_getter_ident != NULL_TREE)
30621 {
30622 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30623 cp_lexer_consume_token (parser->lexer);
30624 }
30625 else
30626 property_getter_ident = cp_parser_objc_selector (parser);
30627 }
30628 break;
30629 default:
30630 cp_parser_error (parser, "unknown property attribute");
30631 syntax_error = true;
30632 break;
30633 }
30634
30635 if (syntax_error)
30636 break;
30637
30638 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30639 cp_lexer_consume_token (parser->lexer);
30640 else
30641 break;
30642 }
30643
30644 /* FIXME: "@property (setter, assign);" will generate a spurious
30645 "error: expected ‘)’ before ‘,’ token". This is because
30646 cp_parser_require, unlike the C counterpart, will produce an
30647 error even if we are in error recovery. */
30648 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30649 {
30650 cp_parser_skip_to_closing_parenthesis (parser,
30651 /*recovering=*/true,
30652 /*or_comma=*/false,
30653 /*consume_paren=*/true);
30654 }
30655 }
30656
30657 /* ... and the property declaration(s). */
30658 properties = cp_parser_objc_struct_declaration (parser);
30659
30660 if (properties == error_mark_node)
30661 {
30662 cp_parser_skip_to_end_of_statement (parser);
30663 /* If the next token is now a `;', consume it. */
30664 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30665 cp_lexer_consume_token (parser->lexer);
30666 return;
30667 }
30668
30669 if (properties == NULL_TREE)
30670 cp_parser_error (parser, "expected identifier");
30671 else
30672 {
30673 /* Comma-separated properties are chained together in
30674 reverse order; add them one by one. */
30675 properties = nreverse (properties);
30676
30677 for (; properties; properties = TREE_CHAIN (properties))
30678 objc_add_property_declaration (loc, copy_node (properties),
30679 property_readonly, property_readwrite,
30680 property_assign, property_retain,
30681 property_copy, property_nonatomic,
30682 property_getter_ident, property_setter_ident);
30683 }
30684
30685 cp_parser_consume_semicolon_at_end_of_statement (parser);
30686 }
30687
30688 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30689
30690 objc-synthesize-declaration:
30691 @synthesize objc-synthesize-identifier-list ;
30692
30693 objc-synthesize-identifier-list:
30694 objc-synthesize-identifier
30695 objc-synthesize-identifier-list, objc-synthesize-identifier
30696
30697 objc-synthesize-identifier
30698 identifier
30699 identifier = identifier
30700
30701 For example:
30702 @synthesize MyProperty;
30703 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30704
30705 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30706 for C. Keep them in sync.
30707 */
30708 static void
30709 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30710 {
30711 tree list = NULL_TREE;
30712 location_t loc;
30713 loc = cp_lexer_peek_token (parser->lexer)->location;
30714
30715 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30716 while (true)
30717 {
30718 tree property, ivar;
30719 property = cp_parser_identifier (parser);
30720 if (property == error_mark_node)
30721 {
30722 cp_parser_consume_semicolon_at_end_of_statement (parser);
30723 return;
30724 }
30725 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30726 {
30727 cp_lexer_consume_token (parser->lexer);
30728 ivar = cp_parser_identifier (parser);
30729 if (ivar == error_mark_node)
30730 {
30731 cp_parser_consume_semicolon_at_end_of_statement (parser);
30732 return;
30733 }
30734 }
30735 else
30736 ivar = NULL_TREE;
30737 list = chainon (list, build_tree_list (ivar, property));
30738 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30739 cp_lexer_consume_token (parser->lexer);
30740 else
30741 break;
30742 }
30743 cp_parser_consume_semicolon_at_end_of_statement (parser);
30744 objc_add_synthesize_declaration (loc, list);
30745 }
30746
30747 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30748
30749 objc-dynamic-declaration:
30750 @dynamic identifier-list ;
30751
30752 For example:
30753 @dynamic MyProperty;
30754 @dynamic MyProperty, AnotherProperty;
30755
30756 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30757 for C. Keep them in sync.
30758 */
30759 static void
30760 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30761 {
30762 tree list = NULL_TREE;
30763 location_t loc;
30764 loc = cp_lexer_peek_token (parser->lexer)->location;
30765
30766 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30767 while (true)
30768 {
30769 tree property;
30770 property = cp_parser_identifier (parser);
30771 if (property == error_mark_node)
30772 {
30773 cp_parser_consume_semicolon_at_end_of_statement (parser);
30774 return;
30775 }
30776 list = chainon (list, build_tree_list (NULL, property));
30777 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30778 cp_lexer_consume_token (parser->lexer);
30779 else
30780 break;
30781 }
30782 cp_parser_consume_semicolon_at_end_of_statement (parser);
30783 objc_add_dynamic_declaration (loc, list);
30784 }
30785
30786 \f
30787 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30788
30789 /* Returns name of the next clause.
30790 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30791 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30792 returned and the token is consumed. */
30793
30794 static pragma_omp_clause
30795 cp_parser_omp_clause_name (cp_parser *parser)
30796 {
30797 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30798
30799 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30800 result = PRAGMA_OACC_CLAUSE_AUTO;
30801 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30802 result = PRAGMA_OMP_CLAUSE_IF;
30803 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30804 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30805 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30806 result = PRAGMA_OACC_CLAUSE_DELETE;
30807 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30808 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30809 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30810 result = PRAGMA_OMP_CLAUSE_FOR;
30811 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30812 {
30813 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30814 const char *p = IDENTIFIER_POINTER (id);
30815
30816 switch (p[0])
30817 {
30818 case 'a':
30819 if (!strcmp ("aligned", p))
30820 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30821 else if (!strcmp ("async", p))
30822 result = PRAGMA_OACC_CLAUSE_ASYNC;
30823 break;
30824 case 'c':
30825 if (!strcmp ("collapse", p))
30826 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30827 else if (!strcmp ("copy", p))
30828 result = PRAGMA_OACC_CLAUSE_COPY;
30829 else if (!strcmp ("copyin", p))
30830 result = PRAGMA_OMP_CLAUSE_COPYIN;
30831 else if (!strcmp ("copyout", p))
30832 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30833 else if (!strcmp ("copyprivate", p))
30834 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30835 else if (!strcmp ("create", p))
30836 result = PRAGMA_OACC_CLAUSE_CREATE;
30837 break;
30838 case 'd':
30839 if (!strcmp ("defaultmap", p))
30840 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30841 else if (!strcmp ("depend", p))
30842 result = PRAGMA_OMP_CLAUSE_DEPEND;
30843 else if (!strcmp ("device", p))
30844 result = PRAGMA_OMP_CLAUSE_DEVICE;
30845 else if (!strcmp ("deviceptr", p))
30846 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30847 else if (!strcmp ("device_resident", p))
30848 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30849 else if (!strcmp ("dist_schedule", p))
30850 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30851 break;
30852 case 'f':
30853 if (!strcmp ("final", p))
30854 result = PRAGMA_OMP_CLAUSE_FINAL;
30855 else if (!strcmp ("firstprivate", p))
30856 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30857 else if (!strcmp ("from", p))
30858 result = PRAGMA_OMP_CLAUSE_FROM;
30859 break;
30860 case 'g':
30861 if (!strcmp ("gang", p))
30862 result = PRAGMA_OACC_CLAUSE_GANG;
30863 else if (!strcmp ("grainsize", p))
30864 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30865 break;
30866 case 'h':
30867 if (!strcmp ("hint", p))
30868 result = PRAGMA_OMP_CLAUSE_HINT;
30869 else if (!strcmp ("host", p))
30870 result = PRAGMA_OACC_CLAUSE_HOST;
30871 break;
30872 case 'i':
30873 if (!strcmp ("inbranch", p))
30874 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30875 else if (!strcmp ("independent", p))
30876 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30877 else if (!strcmp ("is_device_ptr", p))
30878 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30879 break;
30880 case 'l':
30881 if (!strcmp ("lastprivate", p))
30882 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30883 else if (!strcmp ("linear", p))
30884 result = PRAGMA_OMP_CLAUSE_LINEAR;
30885 else if (!strcmp ("link", p))
30886 result = PRAGMA_OMP_CLAUSE_LINK;
30887 break;
30888 case 'm':
30889 if (!strcmp ("map", p))
30890 result = PRAGMA_OMP_CLAUSE_MAP;
30891 else if (!strcmp ("mergeable", p))
30892 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30893 else if (flag_cilkplus && !strcmp ("mask", p))
30894 result = PRAGMA_CILK_CLAUSE_MASK;
30895 break;
30896 case 'n':
30897 if (!strcmp ("nogroup", p))
30898 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30899 else if (!strcmp ("notinbranch", p))
30900 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30901 else if (!strcmp ("nowait", p))
30902 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30903 else if (flag_cilkplus && !strcmp ("nomask", p))
30904 result = PRAGMA_CILK_CLAUSE_NOMASK;
30905 else if (!strcmp ("num_gangs", p))
30906 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30907 else if (!strcmp ("num_tasks", p))
30908 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30909 else if (!strcmp ("num_teams", p))
30910 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30911 else if (!strcmp ("num_threads", p))
30912 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30913 else if (!strcmp ("num_workers", p))
30914 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30915 break;
30916 case 'o':
30917 if (!strcmp ("ordered", p))
30918 result = PRAGMA_OMP_CLAUSE_ORDERED;
30919 break;
30920 case 'p':
30921 if (!strcmp ("parallel", p))
30922 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30923 else if (!strcmp ("present", p))
30924 result = PRAGMA_OACC_CLAUSE_PRESENT;
30925 else if (!strcmp ("present_or_copy", p)
30926 || !strcmp ("pcopy", p))
30927 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30928 else if (!strcmp ("present_or_copyin", p)
30929 || !strcmp ("pcopyin", p))
30930 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30931 else if (!strcmp ("present_or_copyout", p)
30932 || !strcmp ("pcopyout", p))
30933 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30934 else if (!strcmp ("present_or_create", p)
30935 || !strcmp ("pcreate", p))
30936 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30937 else if (!strcmp ("priority", p))
30938 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30939 else if (!strcmp ("proc_bind", p))
30940 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30941 break;
30942 case 'r':
30943 if (!strcmp ("reduction", p))
30944 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30945 break;
30946 case 's':
30947 if (!strcmp ("safelen", p))
30948 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30949 else if (!strcmp ("schedule", p))
30950 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30951 else if (!strcmp ("sections", p))
30952 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30953 else if (!strcmp ("self", p))
30954 result = PRAGMA_OACC_CLAUSE_SELF;
30955 else if (!strcmp ("seq", p))
30956 result = PRAGMA_OACC_CLAUSE_SEQ;
30957 else if (!strcmp ("shared", p))
30958 result = PRAGMA_OMP_CLAUSE_SHARED;
30959 else if (!strcmp ("simd", p))
30960 result = PRAGMA_OMP_CLAUSE_SIMD;
30961 else if (!strcmp ("simdlen", p))
30962 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30963 break;
30964 case 't':
30965 if (!strcmp ("taskgroup", p))
30966 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30967 else if (!strcmp ("thread_limit", p))
30968 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30969 else if (!strcmp ("threads", p))
30970 result = PRAGMA_OMP_CLAUSE_THREADS;
30971 else if (!strcmp ("tile", p))
30972 result = PRAGMA_OACC_CLAUSE_TILE;
30973 else if (!strcmp ("to", p))
30974 result = PRAGMA_OMP_CLAUSE_TO;
30975 break;
30976 case 'u':
30977 if (!strcmp ("uniform", p))
30978 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30979 else if (!strcmp ("untied", p))
30980 result = PRAGMA_OMP_CLAUSE_UNTIED;
30981 else if (!strcmp ("use_device", p))
30982 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30983 else if (!strcmp ("use_device_ptr", p))
30984 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30985 break;
30986 case 'v':
30987 if (!strcmp ("vector", p))
30988 result = PRAGMA_OACC_CLAUSE_VECTOR;
30989 else if (!strcmp ("vector_length", p))
30990 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30991 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30992 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30993 break;
30994 case 'w':
30995 if (!strcmp ("wait", p))
30996 result = PRAGMA_OACC_CLAUSE_WAIT;
30997 else if (!strcmp ("worker", p))
30998 result = PRAGMA_OACC_CLAUSE_WORKER;
30999 break;
31000 }
31001 }
31002
31003 if (result != PRAGMA_OMP_CLAUSE_NONE)
31004 cp_lexer_consume_token (parser->lexer);
31005
31006 return result;
31007 }
31008
31009 /* Validate that a clause of the given type does not already exist. */
31010
31011 static void
31012 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31013 const char *name, location_t location)
31014 {
31015 tree c;
31016
31017 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31018 if (OMP_CLAUSE_CODE (c) == code)
31019 {
31020 error_at (location, "too many %qs clauses", name);
31021 break;
31022 }
31023 }
31024
31025 /* OpenMP 2.5:
31026 variable-list:
31027 identifier
31028 variable-list , identifier
31029
31030 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31031 colon). An opening parenthesis will have been consumed by the caller.
31032
31033 If KIND is nonzero, create the appropriate node and install the decl
31034 in OMP_CLAUSE_DECL and add the node to the head of the list.
31035
31036 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31037 return the list created.
31038
31039 COLON can be NULL if only closing parenthesis should end the list,
31040 or pointer to bool which will receive false if the list is terminated
31041 by closing parenthesis or true if the list is terminated by colon. */
31042
31043 static tree
31044 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31045 tree list, bool *colon)
31046 {
31047 cp_token *token;
31048 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31049 if (colon)
31050 {
31051 parser->colon_corrects_to_scope_p = false;
31052 *colon = false;
31053 }
31054 while (1)
31055 {
31056 tree name, decl;
31057
31058 token = cp_lexer_peek_token (parser->lexer);
31059 if (kind != 0
31060 && current_class_ptr
31061 && cp_parser_is_keyword (token, RID_THIS))
31062 {
31063 decl = finish_this_expr ();
31064 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31065 || CONVERT_EXPR_P (decl))
31066 decl = TREE_OPERAND (decl, 0);
31067 cp_lexer_consume_token (parser->lexer);
31068 }
31069 else
31070 {
31071 name = cp_parser_id_expression (parser, /*template_p=*/false,
31072 /*check_dependency_p=*/true,
31073 /*template_p=*/NULL,
31074 /*declarator_p=*/false,
31075 /*optional_p=*/false);
31076 if (name == error_mark_node)
31077 goto skip_comma;
31078
31079 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31080 if (decl == error_mark_node)
31081 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31082 token->location);
31083 }
31084 if (decl == error_mark_node)
31085 ;
31086 else if (kind != 0)
31087 {
31088 switch (kind)
31089 {
31090 case OMP_CLAUSE__CACHE_:
31091 /* The OpenACC cache directive explicitly only allows "array
31092 elements or subarrays". */
31093 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31094 {
31095 error_at (token->location, "expected %<[%>");
31096 decl = error_mark_node;
31097 break;
31098 }
31099 /* FALLTHROUGH. */
31100 case OMP_CLAUSE_MAP:
31101 case OMP_CLAUSE_FROM:
31102 case OMP_CLAUSE_TO:
31103 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31104 {
31105 location_t loc
31106 = cp_lexer_peek_token (parser->lexer)->location;
31107 cp_id_kind idk = CP_ID_KIND_NONE;
31108 cp_lexer_consume_token (parser->lexer);
31109 decl = convert_from_reference (decl);
31110 decl
31111 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31112 decl, false,
31113 &idk, loc);
31114 }
31115 /* FALLTHROUGH. */
31116 case OMP_CLAUSE_DEPEND:
31117 case OMP_CLAUSE_REDUCTION:
31118 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31119 {
31120 tree low_bound = NULL_TREE, length = NULL_TREE;
31121
31122 parser->colon_corrects_to_scope_p = false;
31123 cp_lexer_consume_token (parser->lexer);
31124 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31125 low_bound = cp_parser_expression (parser);
31126 if (!colon)
31127 parser->colon_corrects_to_scope_p
31128 = saved_colon_corrects_to_scope_p;
31129 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31130 length = integer_one_node;
31131 else
31132 {
31133 /* Look for `:'. */
31134 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31135 goto skip_comma;
31136 if (!cp_lexer_next_token_is (parser->lexer,
31137 CPP_CLOSE_SQUARE))
31138 length = cp_parser_expression (parser);
31139 }
31140 /* Look for the closing `]'. */
31141 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31142 RT_CLOSE_SQUARE))
31143 goto skip_comma;
31144
31145 decl = tree_cons (low_bound, length, decl);
31146 }
31147 break;
31148 default:
31149 break;
31150 }
31151
31152 tree u = build_omp_clause (token->location, kind);
31153 OMP_CLAUSE_DECL (u) = decl;
31154 OMP_CLAUSE_CHAIN (u) = list;
31155 list = u;
31156 }
31157 else
31158 list = tree_cons (decl, NULL_TREE, list);
31159
31160 get_comma:
31161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31162 break;
31163 cp_lexer_consume_token (parser->lexer);
31164 }
31165
31166 if (colon)
31167 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31168
31169 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31170 {
31171 *colon = true;
31172 cp_parser_require (parser, CPP_COLON, RT_COLON);
31173 return list;
31174 }
31175
31176 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31177 {
31178 int ending;
31179
31180 /* Try to resync to an unnested comma. Copied from
31181 cp_parser_parenthesized_expression_list. */
31182 skip_comma:
31183 if (colon)
31184 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31185 ending = cp_parser_skip_to_closing_parenthesis (parser,
31186 /*recovering=*/true,
31187 /*or_comma=*/true,
31188 /*consume_paren=*/true);
31189 if (ending < 0)
31190 goto get_comma;
31191 }
31192
31193 return list;
31194 }
31195
31196 /* Similarly, but expect leading and trailing parenthesis. This is a very
31197 common case for omp clauses. */
31198
31199 static tree
31200 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31201 {
31202 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31203 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31204 return list;
31205 }
31206
31207 /* OpenACC 2.0:
31208 copy ( variable-list )
31209 copyin ( variable-list )
31210 copyout ( variable-list )
31211 create ( variable-list )
31212 delete ( variable-list )
31213 present ( variable-list )
31214 present_or_copy ( variable-list )
31215 pcopy ( variable-list )
31216 present_or_copyin ( variable-list )
31217 pcopyin ( variable-list )
31218 present_or_copyout ( variable-list )
31219 pcopyout ( variable-list )
31220 present_or_create ( variable-list )
31221 pcreate ( variable-list ) */
31222
31223 static tree
31224 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31225 tree list)
31226 {
31227 enum gomp_map_kind kind;
31228 switch (c_kind)
31229 {
31230 case PRAGMA_OACC_CLAUSE_COPY:
31231 kind = GOMP_MAP_FORCE_TOFROM;
31232 break;
31233 case PRAGMA_OACC_CLAUSE_COPYIN:
31234 kind = GOMP_MAP_FORCE_TO;
31235 break;
31236 case PRAGMA_OACC_CLAUSE_COPYOUT:
31237 kind = GOMP_MAP_FORCE_FROM;
31238 break;
31239 case PRAGMA_OACC_CLAUSE_CREATE:
31240 kind = GOMP_MAP_FORCE_ALLOC;
31241 break;
31242 case PRAGMA_OACC_CLAUSE_DELETE:
31243 kind = GOMP_MAP_DELETE;
31244 break;
31245 case PRAGMA_OACC_CLAUSE_DEVICE:
31246 kind = GOMP_MAP_FORCE_TO;
31247 break;
31248 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31249 kind = GOMP_MAP_DEVICE_RESIDENT;
31250 break;
31251 case PRAGMA_OACC_CLAUSE_HOST:
31252 case PRAGMA_OACC_CLAUSE_SELF:
31253 kind = GOMP_MAP_FORCE_FROM;
31254 break;
31255 case PRAGMA_OACC_CLAUSE_LINK:
31256 kind = GOMP_MAP_LINK;
31257 break;
31258 case PRAGMA_OACC_CLAUSE_PRESENT:
31259 kind = GOMP_MAP_FORCE_PRESENT;
31260 break;
31261 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31262 kind = GOMP_MAP_TOFROM;
31263 break;
31264 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31265 kind = GOMP_MAP_TO;
31266 break;
31267 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31268 kind = GOMP_MAP_FROM;
31269 break;
31270 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31271 kind = GOMP_MAP_ALLOC;
31272 break;
31273 default:
31274 gcc_unreachable ();
31275 }
31276 tree nl, c;
31277 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31278
31279 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31280 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31281
31282 return nl;
31283 }
31284
31285 /* OpenACC 2.0:
31286 deviceptr ( variable-list ) */
31287
31288 static tree
31289 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31290 {
31291 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31292 tree vars, t;
31293
31294 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31295 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31296 variable-list must only allow for pointer variables. */
31297 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31298 for (t = vars; t; t = TREE_CHAIN (t))
31299 {
31300 tree v = TREE_PURPOSE (t);
31301 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31302 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31303 OMP_CLAUSE_DECL (u) = v;
31304 OMP_CLAUSE_CHAIN (u) = list;
31305 list = u;
31306 }
31307
31308 return list;
31309 }
31310
31311 /* OpenACC 2.0:
31312 auto
31313 independent
31314 nohost
31315 seq */
31316
31317 static tree
31318 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31319 enum omp_clause_code code,
31320 tree list, location_t location)
31321 {
31322 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31323 tree c = build_omp_clause (location, code);
31324 OMP_CLAUSE_CHAIN (c) = list;
31325 return c;
31326 }
31327
31328 /* OpenACC:
31329 num_gangs ( expression )
31330 num_workers ( expression )
31331 vector_length ( expression ) */
31332
31333 static tree
31334 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31335 const char *str, tree list)
31336 {
31337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31338
31339 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31340 return list;
31341
31342 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31343
31344 if (t == error_mark_node
31345 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31346 {
31347 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31348 /*or_comma=*/false,
31349 /*consume_paren=*/true);
31350 return list;
31351 }
31352
31353 check_no_duplicate_clause (list, code, str, loc);
31354
31355 tree c = build_omp_clause (loc, code);
31356 OMP_CLAUSE_OPERAND (c, 0) = t;
31357 OMP_CLAUSE_CHAIN (c) = list;
31358 return c;
31359 }
31360
31361 /* OpenACC:
31362
31363 gang [( gang-arg-list )]
31364 worker [( [num:] int-expr )]
31365 vector [( [length:] int-expr )]
31366
31367 where gang-arg is one of:
31368
31369 [num:] int-expr
31370 static: size-expr
31371
31372 and size-expr may be:
31373
31374 *
31375 int-expr
31376 */
31377
31378 static tree
31379 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31380 const char *str, tree list)
31381 {
31382 const char *id = "num";
31383 cp_lexer *lexer = parser->lexer;
31384 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31385 location_t loc = cp_lexer_peek_token (lexer)->location;
31386
31387 if (kind == OMP_CLAUSE_VECTOR)
31388 id = "length";
31389
31390 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31391 {
31392 cp_lexer_consume_token (lexer);
31393
31394 do
31395 {
31396 cp_token *next = cp_lexer_peek_token (lexer);
31397 int idx = 0;
31398
31399 /* Gang static argument. */
31400 if (kind == OMP_CLAUSE_GANG
31401 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31402 {
31403 cp_lexer_consume_token (lexer);
31404
31405 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31406 goto cleanup_error;
31407
31408 idx = 1;
31409 if (ops[idx] != NULL)
31410 {
31411 cp_parser_error (parser, "too many %<static%> arguments");
31412 goto cleanup_error;
31413 }
31414
31415 /* Check for the '*' argument. */
31416 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31417 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31418 || cp_lexer_nth_token_is (parser->lexer, 2,
31419 CPP_CLOSE_PAREN)))
31420 {
31421 cp_lexer_consume_token (lexer);
31422 ops[idx] = integer_minus_one_node;
31423
31424 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31425 {
31426 cp_lexer_consume_token (lexer);
31427 continue;
31428 }
31429 else break;
31430 }
31431 }
31432 /* Worker num: argument and vector length: arguments. */
31433 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31434 && id_equal (next->u.value, id)
31435 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31436 {
31437 cp_lexer_consume_token (lexer); /* id */
31438 cp_lexer_consume_token (lexer); /* ':' */
31439 }
31440
31441 /* Now collect the actual argument. */
31442 if (ops[idx] != NULL_TREE)
31443 {
31444 cp_parser_error (parser, "unexpected argument");
31445 goto cleanup_error;
31446 }
31447
31448 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31449 false);
31450 if (expr == error_mark_node)
31451 goto cleanup_error;
31452
31453 mark_exp_read (expr);
31454 ops[idx] = expr;
31455
31456 if (kind == OMP_CLAUSE_GANG
31457 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31458 {
31459 cp_lexer_consume_token (lexer);
31460 continue;
31461 }
31462 break;
31463 }
31464 while (1);
31465
31466 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31467 goto cleanup_error;
31468 }
31469
31470 check_no_duplicate_clause (list, kind, str, loc);
31471
31472 c = build_omp_clause (loc, kind);
31473
31474 if (ops[1])
31475 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31476
31477 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31478 OMP_CLAUSE_CHAIN (c) = list;
31479
31480 return c;
31481
31482 cleanup_error:
31483 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31484 return list;
31485 }
31486
31487 /* OpenACC 2.0:
31488 tile ( size-expr-list ) */
31489
31490 static tree
31491 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31492 {
31493 tree c, expr = error_mark_node;
31494 tree tile = NULL_TREE;
31495
31496 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31497 so, but the spec authors never considered such a case and have
31498 differing opinions on what it might mean, including 'not
31499 allowed'.) */
31500 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31501 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31502 clause_loc);
31503
31504 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31505 return list;
31506
31507 do
31508 {
31509 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31510 return list;
31511
31512 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31513 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31514 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31515 {
31516 cp_lexer_consume_token (parser->lexer);
31517 expr = integer_zero_node;
31518 }
31519 else
31520 expr = cp_parser_constant_expression (parser);
31521
31522 tile = tree_cons (NULL_TREE, expr, tile);
31523 }
31524 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31525
31526 /* Consume the trailing ')'. */
31527 cp_lexer_consume_token (parser->lexer);
31528
31529 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31530 tile = nreverse (tile);
31531 OMP_CLAUSE_TILE_LIST (c) = tile;
31532 OMP_CLAUSE_CHAIN (c) = list;
31533 return c;
31534 }
31535
31536 /* OpenACC 2.0
31537 Parse wait clause or directive parameters. */
31538
31539 static tree
31540 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31541 {
31542 vec<tree, va_gc> *args;
31543 tree t, args_tree;
31544
31545 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31546 /*cast_p=*/false,
31547 /*allow_expansion_p=*/true,
31548 /*non_constant_p=*/NULL);
31549
31550 if (args == NULL || args->length () == 0)
31551 {
31552 cp_parser_error (parser, "expected integer expression before ')'");
31553 if (args != NULL)
31554 release_tree_vector (args);
31555 return list;
31556 }
31557
31558 args_tree = build_tree_list_vec (args);
31559
31560 release_tree_vector (args);
31561
31562 for (t = args_tree; t; t = TREE_CHAIN (t))
31563 {
31564 tree targ = TREE_VALUE (t);
31565
31566 if (targ != error_mark_node)
31567 {
31568 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31569 error ("%<wait%> expression must be integral");
31570 else
31571 {
31572 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31573
31574 mark_rvalue_use (targ);
31575 OMP_CLAUSE_DECL (c) = targ;
31576 OMP_CLAUSE_CHAIN (c) = list;
31577 list = c;
31578 }
31579 }
31580 }
31581
31582 return list;
31583 }
31584
31585 /* OpenACC:
31586 wait ( int-expr-list ) */
31587
31588 static tree
31589 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31590 {
31591 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31592
31593 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31594 return list;
31595
31596 list = cp_parser_oacc_wait_list (parser, location, list);
31597
31598 return list;
31599 }
31600
31601 /* OpenMP 3.0:
31602 collapse ( constant-expression ) */
31603
31604 static tree
31605 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31606 {
31607 tree c, num;
31608 location_t loc;
31609 HOST_WIDE_INT n;
31610
31611 loc = cp_lexer_peek_token (parser->lexer)->location;
31612 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31613 return list;
31614
31615 num = cp_parser_constant_expression (parser);
31616
31617 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31618 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31619 /*or_comma=*/false,
31620 /*consume_paren=*/true);
31621
31622 if (num == error_mark_node)
31623 return list;
31624 num = fold_non_dependent_expr (num);
31625 if (!tree_fits_shwi_p (num)
31626 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31627 || (n = tree_to_shwi (num)) <= 0
31628 || (int) n != n)
31629 {
31630 error_at (loc, "collapse argument needs positive constant integer expression");
31631 return list;
31632 }
31633
31634 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31635 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31636 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31637 OMP_CLAUSE_CHAIN (c) = list;
31638 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31639
31640 return c;
31641 }
31642
31643 /* OpenMP 2.5:
31644 default ( none | shared )
31645
31646 OpenACC:
31647 default ( none | present ) */
31648
31649 static tree
31650 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31651 location_t location, bool is_oacc)
31652 {
31653 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31654 tree c;
31655
31656 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31657 return list;
31658 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31659 {
31660 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31661 const char *p = IDENTIFIER_POINTER (id);
31662
31663 switch (p[0])
31664 {
31665 case 'n':
31666 if (strcmp ("none", p) != 0)
31667 goto invalid_kind;
31668 kind = OMP_CLAUSE_DEFAULT_NONE;
31669 break;
31670
31671 case 'p':
31672 if (strcmp ("present", p) != 0 || !is_oacc)
31673 goto invalid_kind;
31674 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31675 break;
31676
31677 case 's':
31678 if (strcmp ("shared", p) != 0 || is_oacc)
31679 goto invalid_kind;
31680 kind = OMP_CLAUSE_DEFAULT_SHARED;
31681 break;
31682
31683 default:
31684 goto invalid_kind;
31685 }
31686
31687 cp_lexer_consume_token (parser->lexer);
31688 }
31689 else
31690 {
31691 invalid_kind:
31692 if (is_oacc)
31693 cp_parser_error (parser, "expected %<none%> or %<present%>");
31694 else
31695 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31696 }
31697
31698 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31699 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31700 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31701 /*or_comma=*/false,
31702 /*consume_paren=*/true);
31703
31704 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31705 return list;
31706
31707 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31708 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31709 OMP_CLAUSE_CHAIN (c) = list;
31710 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31711
31712 return c;
31713 }
31714
31715 /* OpenMP 3.1:
31716 final ( expression ) */
31717
31718 static tree
31719 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31720 {
31721 tree t, c;
31722
31723 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31724 return list;
31725
31726 t = cp_parser_condition (parser);
31727
31728 if (t == error_mark_node
31729 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31730 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31731 /*or_comma=*/false,
31732 /*consume_paren=*/true);
31733
31734 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31735
31736 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31737 OMP_CLAUSE_FINAL_EXPR (c) = t;
31738 OMP_CLAUSE_CHAIN (c) = list;
31739
31740 return c;
31741 }
31742
31743 /* OpenMP 2.5:
31744 if ( expression )
31745
31746 OpenMP 4.5:
31747 if ( directive-name-modifier : expression )
31748
31749 directive-name-modifier:
31750 parallel | task | taskloop | target data | target | target update
31751 | target enter data | target exit data */
31752
31753 static tree
31754 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31755 bool is_omp)
31756 {
31757 tree t, c;
31758 enum tree_code if_modifier = ERROR_MARK;
31759
31760 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31761 return list;
31762
31763 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31764 {
31765 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31766 const char *p = IDENTIFIER_POINTER (id);
31767 int n = 2;
31768
31769 if (strcmp ("parallel", p) == 0)
31770 if_modifier = OMP_PARALLEL;
31771 else if (strcmp ("task", p) == 0)
31772 if_modifier = OMP_TASK;
31773 else if (strcmp ("taskloop", p) == 0)
31774 if_modifier = OMP_TASKLOOP;
31775 else if (strcmp ("target", p) == 0)
31776 {
31777 if_modifier = OMP_TARGET;
31778 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31779 {
31780 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31781 p = IDENTIFIER_POINTER (id);
31782 if (strcmp ("data", p) == 0)
31783 if_modifier = OMP_TARGET_DATA;
31784 else if (strcmp ("update", p) == 0)
31785 if_modifier = OMP_TARGET_UPDATE;
31786 else if (strcmp ("enter", p) == 0)
31787 if_modifier = OMP_TARGET_ENTER_DATA;
31788 else if (strcmp ("exit", p) == 0)
31789 if_modifier = OMP_TARGET_EXIT_DATA;
31790 if (if_modifier != OMP_TARGET)
31791 n = 3;
31792 else
31793 {
31794 location_t loc
31795 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31796 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31797 "or %<exit%>");
31798 if_modifier = ERROR_MARK;
31799 }
31800 if (if_modifier == OMP_TARGET_ENTER_DATA
31801 || if_modifier == OMP_TARGET_EXIT_DATA)
31802 {
31803 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31804 {
31805 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31806 p = IDENTIFIER_POINTER (id);
31807 if (strcmp ("data", p) == 0)
31808 n = 4;
31809 }
31810 if (n != 4)
31811 {
31812 location_t loc
31813 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31814 error_at (loc, "expected %<data%>");
31815 if_modifier = ERROR_MARK;
31816 }
31817 }
31818 }
31819 }
31820 if (if_modifier != ERROR_MARK)
31821 {
31822 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31823 {
31824 while (n-- > 0)
31825 cp_lexer_consume_token (parser->lexer);
31826 }
31827 else
31828 {
31829 if (n > 2)
31830 {
31831 location_t loc
31832 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31833 error_at (loc, "expected %<:%>");
31834 }
31835 if_modifier = ERROR_MARK;
31836 }
31837 }
31838 }
31839
31840 t = cp_parser_condition (parser);
31841
31842 if (t == error_mark_node
31843 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31844 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31845 /*or_comma=*/false,
31846 /*consume_paren=*/true);
31847
31848 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31849 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31850 {
31851 if (if_modifier != ERROR_MARK
31852 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31853 {
31854 const char *p = NULL;
31855 switch (if_modifier)
31856 {
31857 case OMP_PARALLEL: p = "parallel"; break;
31858 case OMP_TASK: p = "task"; break;
31859 case OMP_TASKLOOP: p = "taskloop"; break;
31860 case OMP_TARGET_DATA: p = "target data"; break;
31861 case OMP_TARGET: p = "target"; break;
31862 case OMP_TARGET_UPDATE: p = "target update"; break;
31863 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31864 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31865 default: gcc_unreachable ();
31866 }
31867 error_at (location, "too many %<if%> clauses with %qs modifier",
31868 p);
31869 return list;
31870 }
31871 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31872 {
31873 if (!is_omp)
31874 error_at (location, "too many %<if%> clauses");
31875 else
31876 error_at (location, "too many %<if%> clauses without modifier");
31877 return list;
31878 }
31879 else if (if_modifier == ERROR_MARK
31880 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31881 {
31882 error_at (location, "if any %<if%> clause has modifier, then all "
31883 "%<if%> clauses have to use modifier");
31884 return list;
31885 }
31886 }
31887
31888 c = build_omp_clause (location, OMP_CLAUSE_IF);
31889 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31890 OMP_CLAUSE_IF_EXPR (c) = t;
31891 OMP_CLAUSE_CHAIN (c) = list;
31892
31893 return c;
31894 }
31895
31896 /* OpenMP 3.1:
31897 mergeable */
31898
31899 static tree
31900 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31901 tree list, location_t location)
31902 {
31903 tree c;
31904
31905 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31906 location);
31907
31908 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31909 OMP_CLAUSE_CHAIN (c) = list;
31910 return c;
31911 }
31912
31913 /* OpenMP 2.5:
31914 nowait */
31915
31916 static tree
31917 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31918 tree list, location_t location)
31919 {
31920 tree c;
31921
31922 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31923
31924 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31925 OMP_CLAUSE_CHAIN (c) = list;
31926 return c;
31927 }
31928
31929 /* OpenMP 2.5:
31930 num_threads ( expression ) */
31931
31932 static tree
31933 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31934 location_t location)
31935 {
31936 tree t, c;
31937
31938 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31939 return list;
31940
31941 t = cp_parser_expression (parser);
31942
31943 if (t == error_mark_node
31944 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31945 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31946 /*or_comma=*/false,
31947 /*consume_paren=*/true);
31948
31949 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31950 "num_threads", location);
31951
31952 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31953 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31954 OMP_CLAUSE_CHAIN (c) = list;
31955
31956 return c;
31957 }
31958
31959 /* OpenMP 4.5:
31960 num_tasks ( expression ) */
31961
31962 static tree
31963 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31964 location_t location)
31965 {
31966 tree t, c;
31967
31968 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31969 return list;
31970
31971 t = cp_parser_expression (parser);
31972
31973 if (t == error_mark_node
31974 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31975 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31976 /*or_comma=*/false,
31977 /*consume_paren=*/true);
31978
31979 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31980 "num_tasks", location);
31981
31982 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31983 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31984 OMP_CLAUSE_CHAIN (c) = list;
31985
31986 return c;
31987 }
31988
31989 /* OpenMP 4.5:
31990 grainsize ( expression ) */
31991
31992 static tree
31993 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31994 location_t location)
31995 {
31996 tree t, c;
31997
31998 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31999 return list;
32000
32001 t = cp_parser_expression (parser);
32002
32003 if (t == error_mark_node
32004 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32005 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32006 /*or_comma=*/false,
32007 /*consume_paren=*/true);
32008
32009 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32010 "grainsize", location);
32011
32012 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32013 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32014 OMP_CLAUSE_CHAIN (c) = list;
32015
32016 return c;
32017 }
32018
32019 /* OpenMP 4.5:
32020 priority ( expression ) */
32021
32022 static tree
32023 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32024 location_t location)
32025 {
32026 tree t, c;
32027
32028 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32029 return list;
32030
32031 t = cp_parser_expression (parser);
32032
32033 if (t == error_mark_node
32034 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32035 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32036 /*or_comma=*/false,
32037 /*consume_paren=*/true);
32038
32039 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32040 "priority", location);
32041
32042 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32043 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32044 OMP_CLAUSE_CHAIN (c) = list;
32045
32046 return c;
32047 }
32048
32049 /* OpenMP 4.5:
32050 hint ( expression ) */
32051
32052 static tree
32053 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32054 location_t location)
32055 {
32056 tree t, c;
32057
32058 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32059 return list;
32060
32061 t = cp_parser_expression (parser);
32062
32063 if (t == error_mark_node
32064 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32065 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32066 /*or_comma=*/false,
32067 /*consume_paren=*/true);
32068
32069 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32070
32071 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32072 OMP_CLAUSE_HINT_EXPR (c) = t;
32073 OMP_CLAUSE_CHAIN (c) = list;
32074
32075 return c;
32076 }
32077
32078 /* OpenMP 4.5:
32079 defaultmap ( tofrom : scalar ) */
32080
32081 static tree
32082 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32083 location_t location)
32084 {
32085 tree c, id;
32086 const char *p;
32087
32088 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32089 return list;
32090
32091 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32092 {
32093 cp_parser_error (parser, "expected %<tofrom%>");
32094 goto out_err;
32095 }
32096 id = cp_lexer_peek_token (parser->lexer)->u.value;
32097 p = IDENTIFIER_POINTER (id);
32098 if (strcmp (p, "tofrom") != 0)
32099 {
32100 cp_parser_error (parser, "expected %<tofrom%>");
32101 goto out_err;
32102 }
32103 cp_lexer_consume_token (parser->lexer);
32104 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32105 goto out_err;
32106
32107 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32108 {
32109 cp_parser_error (parser, "expected %<scalar%>");
32110 goto out_err;
32111 }
32112 id = cp_lexer_peek_token (parser->lexer)->u.value;
32113 p = IDENTIFIER_POINTER (id);
32114 if (strcmp (p, "scalar") != 0)
32115 {
32116 cp_parser_error (parser, "expected %<scalar%>");
32117 goto out_err;
32118 }
32119 cp_lexer_consume_token (parser->lexer);
32120 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32121 goto out_err;
32122
32123 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32124 location);
32125
32126 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32127 OMP_CLAUSE_CHAIN (c) = list;
32128 return c;
32129
32130 out_err:
32131 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32132 /*or_comma=*/false,
32133 /*consume_paren=*/true);
32134 return list;
32135 }
32136
32137 /* OpenMP 2.5:
32138 ordered
32139
32140 OpenMP 4.5:
32141 ordered ( constant-expression ) */
32142
32143 static tree
32144 cp_parser_omp_clause_ordered (cp_parser *parser,
32145 tree list, location_t location)
32146 {
32147 tree c, num = NULL_TREE;
32148 HOST_WIDE_INT n;
32149
32150 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32151 "ordered", location);
32152
32153 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32154 {
32155 cp_lexer_consume_token (parser->lexer);
32156
32157 num = cp_parser_constant_expression (parser);
32158
32159 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32160 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32161 /*or_comma=*/false,
32162 /*consume_paren=*/true);
32163
32164 if (num == error_mark_node)
32165 return list;
32166 num = fold_non_dependent_expr (num);
32167 if (!tree_fits_shwi_p (num)
32168 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32169 || (n = tree_to_shwi (num)) <= 0
32170 || (int) n != n)
32171 {
32172 error_at (location,
32173 "ordered argument needs positive constant integer "
32174 "expression");
32175 return list;
32176 }
32177 }
32178
32179 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32180 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32181 OMP_CLAUSE_CHAIN (c) = list;
32182 return c;
32183 }
32184
32185 /* OpenMP 2.5:
32186 reduction ( reduction-operator : variable-list )
32187
32188 reduction-operator:
32189 One of: + * - & ^ | && ||
32190
32191 OpenMP 3.1:
32192
32193 reduction-operator:
32194 One of: + * - & ^ | && || min max
32195
32196 OpenMP 4.0:
32197
32198 reduction-operator:
32199 One of: + * - & ^ | && ||
32200 id-expression */
32201
32202 static tree
32203 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32204 {
32205 enum tree_code code = ERROR_MARK;
32206 tree nlist, c, id = NULL_TREE;
32207
32208 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32209 return list;
32210
32211 switch (cp_lexer_peek_token (parser->lexer)->type)
32212 {
32213 case CPP_PLUS: code = PLUS_EXPR; break;
32214 case CPP_MULT: code = MULT_EXPR; break;
32215 case CPP_MINUS: code = MINUS_EXPR; break;
32216 case CPP_AND: code = BIT_AND_EXPR; break;
32217 case CPP_XOR: code = BIT_XOR_EXPR; break;
32218 case CPP_OR: code = BIT_IOR_EXPR; break;
32219 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32220 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32221 default: break;
32222 }
32223
32224 if (code != ERROR_MARK)
32225 cp_lexer_consume_token (parser->lexer);
32226 else
32227 {
32228 bool saved_colon_corrects_to_scope_p;
32229 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32230 parser->colon_corrects_to_scope_p = false;
32231 id = cp_parser_id_expression (parser, /*template_p=*/false,
32232 /*check_dependency_p=*/true,
32233 /*template_p=*/NULL,
32234 /*declarator_p=*/false,
32235 /*optional_p=*/false);
32236 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32237 if (identifier_p (id))
32238 {
32239 const char *p = IDENTIFIER_POINTER (id);
32240
32241 if (strcmp (p, "min") == 0)
32242 code = MIN_EXPR;
32243 else if (strcmp (p, "max") == 0)
32244 code = MAX_EXPR;
32245 else if (id == cp_operator_id (PLUS_EXPR))
32246 code = PLUS_EXPR;
32247 else if (id == cp_operator_id (MULT_EXPR))
32248 code = MULT_EXPR;
32249 else if (id == cp_operator_id (MINUS_EXPR))
32250 code = MINUS_EXPR;
32251 else if (id == cp_operator_id (BIT_AND_EXPR))
32252 code = BIT_AND_EXPR;
32253 else if (id == cp_operator_id (BIT_IOR_EXPR))
32254 code = BIT_IOR_EXPR;
32255 else if (id == cp_operator_id (BIT_XOR_EXPR))
32256 code = BIT_XOR_EXPR;
32257 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32258 code = TRUTH_ANDIF_EXPR;
32259 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32260 code = TRUTH_ORIF_EXPR;
32261 id = omp_reduction_id (code, id, NULL_TREE);
32262 tree scope = parser->scope;
32263 if (scope)
32264 id = build_qualified_name (NULL_TREE, scope, id, false);
32265 parser->scope = NULL_TREE;
32266 parser->qualifying_scope = NULL_TREE;
32267 parser->object_scope = NULL_TREE;
32268 }
32269 else
32270 {
32271 error ("invalid reduction-identifier");
32272 resync_fail:
32273 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32274 /*or_comma=*/false,
32275 /*consume_paren=*/true);
32276 return list;
32277 }
32278 }
32279
32280 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32281 goto resync_fail;
32282
32283 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32284 NULL);
32285 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32286 {
32287 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32288 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32289 }
32290
32291 return nlist;
32292 }
32293
32294 /* OpenMP 2.5:
32295 schedule ( schedule-kind )
32296 schedule ( schedule-kind , expression )
32297
32298 schedule-kind:
32299 static | dynamic | guided | runtime | auto
32300
32301 OpenMP 4.5:
32302 schedule ( schedule-modifier : schedule-kind )
32303 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32304
32305 schedule-modifier:
32306 simd
32307 monotonic
32308 nonmonotonic */
32309
32310 static tree
32311 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32312 {
32313 tree c, t;
32314 int modifiers = 0, nmodifiers = 0;
32315
32316 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32317 return list;
32318
32319 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32320
32321 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32322 {
32323 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32324 const char *p = IDENTIFIER_POINTER (id);
32325 if (strcmp ("simd", p) == 0)
32326 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32327 else if (strcmp ("monotonic", p) == 0)
32328 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32329 else if (strcmp ("nonmonotonic", p) == 0)
32330 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32331 else
32332 break;
32333 cp_lexer_consume_token (parser->lexer);
32334 if (nmodifiers++ == 0
32335 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32336 cp_lexer_consume_token (parser->lexer);
32337 else
32338 {
32339 cp_parser_require (parser, CPP_COLON, RT_COLON);
32340 break;
32341 }
32342 }
32343
32344 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32345 {
32346 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32347 const char *p = IDENTIFIER_POINTER (id);
32348
32349 switch (p[0])
32350 {
32351 case 'd':
32352 if (strcmp ("dynamic", p) != 0)
32353 goto invalid_kind;
32354 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32355 break;
32356
32357 case 'g':
32358 if (strcmp ("guided", p) != 0)
32359 goto invalid_kind;
32360 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32361 break;
32362
32363 case 'r':
32364 if (strcmp ("runtime", p) != 0)
32365 goto invalid_kind;
32366 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32367 break;
32368
32369 default:
32370 goto invalid_kind;
32371 }
32372 }
32373 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32374 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32375 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32376 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32377 else
32378 goto invalid_kind;
32379 cp_lexer_consume_token (parser->lexer);
32380
32381 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32382 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32383 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32384 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32385 {
32386 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32387 "specified");
32388 modifiers = 0;
32389 }
32390
32391 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32392 {
32393 cp_token *token;
32394 cp_lexer_consume_token (parser->lexer);
32395
32396 token = cp_lexer_peek_token (parser->lexer);
32397 t = cp_parser_assignment_expression (parser);
32398
32399 if (t == error_mark_node)
32400 goto resync_fail;
32401 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32402 error_at (token->location, "schedule %<runtime%> does not take "
32403 "a %<chunk_size%> parameter");
32404 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32405 error_at (token->location, "schedule %<auto%> does not take "
32406 "a %<chunk_size%> parameter");
32407 else
32408 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32409
32410 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32411 goto resync_fail;
32412 }
32413 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32414 goto resync_fail;
32415
32416 OMP_CLAUSE_SCHEDULE_KIND (c)
32417 = (enum omp_clause_schedule_kind)
32418 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32419
32420 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32421 OMP_CLAUSE_CHAIN (c) = list;
32422 return c;
32423
32424 invalid_kind:
32425 cp_parser_error (parser, "invalid schedule kind");
32426 resync_fail:
32427 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32428 /*or_comma=*/false,
32429 /*consume_paren=*/true);
32430 return list;
32431 }
32432
32433 /* OpenMP 3.0:
32434 untied */
32435
32436 static tree
32437 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32438 tree list, location_t location)
32439 {
32440 tree c;
32441
32442 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32443
32444 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32445 OMP_CLAUSE_CHAIN (c) = list;
32446 return c;
32447 }
32448
32449 /* OpenMP 4.0:
32450 inbranch
32451 notinbranch */
32452
32453 static tree
32454 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32455 tree list, location_t location)
32456 {
32457 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32458 tree c = build_omp_clause (location, code);
32459 OMP_CLAUSE_CHAIN (c) = list;
32460 return c;
32461 }
32462
32463 /* OpenMP 4.0:
32464 parallel
32465 for
32466 sections
32467 taskgroup */
32468
32469 static tree
32470 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32471 enum omp_clause_code code,
32472 tree list, location_t location)
32473 {
32474 tree c = build_omp_clause (location, code);
32475 OMP_CLAUSE_CHAIN (c) = list;
32476 return c;
32477 }
32478
32479 /* OpenMP 4.5:
32480 nogroup */
32481
32482 static tree
32483 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32484 tree list, location_t location)
32485 {
32486 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32487 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32488 OMP_CLAUSE_CHAIN (c) = list;
32489 return c;
32490 }
32491
32492 /* OpenMP 4.5:
32493 simd
32494 threads */
32495
32496 static tree
32497 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32498 enum omp_clause_code code,
32499 tree list, location_t location)
32500 {
32501 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32502 tree c = build_omp_clause (location, code);
32503 OMP_CLAUSE_CHAIN (c) = list;
32504 return c;
32505 }
32506
32507 /* OpenMP 4.0:
32508 num_teams ( expression ) */
32509
32510 static tree
32511 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32512 location_t location)
32513 {
32514 tree t, c;
32515
32516 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32517 return list;
32518
32519 t = cp_parser_expression (parser);
32520
32521 if (t == error_mark_node
32522 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32523 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32524 /*or_comma=*/false,
32525 /*consume_paren=*/true);
32526
32527 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32528 "num_teams", location);
32529
32530 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32531 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32532 OMP_CLAUSE_CHAIN (c) = list;
32533
32534 return c;
32535 }
32536
32537 /* OpenMP 4.0:
32538 thread_limit ( expression ) */
32539
32540 static tree
32541 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32542 location_t location)
32543 {
32544 tree t, c;
32545
32546 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32547 return list;
32548
32549 t = cp_parser_expression (parser);
32550
32551 if (t == error_mark_node
32552 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32553 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32554 /*or_comma=*/false,
32555 /*consume_paren=*/true);
32556
32557 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32558 "thread_limit", location);
32559
32560 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32561 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32562 OMP_CLAUSE_CHAIN (c) = list;
32563
32564 return c;
32565 }
32566
32567 /* OpenMP 4.0:
32568 aligned ( variable-list )
32569 aligned ( variable-list : constant-expression ) */
32570
32571 static tree
32572 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32573 {
32574 tree nlist, c, alignment = NULL_TREE;
32575 bool colon;
32576
32577 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32578 return list;
32579
32580 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32581 &colon);
32582
32583 if (colon)
32584 {
32585 alignment = cp_parser_constant_expression (parser);
32586
32587 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32588 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32589 /*or_comma=*/false,
32590 /*consume_paren=*/true);
32591
32592 if (alignment == error_mark_node)
32593 alignment = NULL_TREE;
32594 }
32595
32596 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32597 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32598
32599 return nlist;
32600 }
32601
32602 /* OpenMP 4.0:
32603 linear ( variable-list )
32604 linear ( variable-list : expression )
32605
32606 OpenMP 4.5:
32607 linear ( modifier ( variable-list ) )
32608 linear ( modifier ( variable-list ) : expression ) */
32609
32610 static tree
32611 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32612 bool is_cilk_simd_fn, bool declare_simd)
32613 {
32614 tree nlist, c, step = integer_one_node;
32615 bool colon;
32616 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32617
32618 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32619 return list;
32620
32621 if (!is_cilk_simd_fn
32622 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32623 {
32624 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32625 const char *p = IDENTIFIER_POINTER (id);
32626
32627 if (strcmp ("ref", p) == 0)
32628 kind = OMP_CLAUSE_LINEAR_REF;
32629 else if (strcmp ("val", p) == 0)
32630 kind = OMP_CLAUSE_LINEAR_VAL;
32631 else if (strcmp ("uval", p) == 0)
32632 kind = OMP_CLAUSE_LINEAR_UVAL;
32633 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32634 cp_lexer_consume_token (parser->lexer);
32635 else
32636 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32637 }
32638
32639 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32640 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32641 &colon);
32642 else
32643 {
32644 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32645 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32646 if (colon)
32647 cp_parser_require (parser, CPP_COLON, RT_COLON);
32648 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32649 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32650 /*or_comma=*/false,
32651 /*consume_paren=*/true);
32652 }
32653
32654 if (colon)
32655 {
32656 step = NULL_TREE;
32657 if (declare_simd
32658 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32659 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32660 {
32661 cp_token *token = cp_lexer_peek_token (parser->lexer);
32662 cp_parser_parse_tentatively (parser);
32663 step = cp_parser_id_expression (parser, /*template_p=*/false,
32664 /*check_dependency_p=*/true,
32665 /*template_p=*/NULL,
32666 /*declarator_p=*/false,
32667 /*optional_p=*/false);
32668 if (step != error_mark_node)
32669 step = cp_parser_lookup_name_simple (parser, step, token->location);
32670 if (step == error_mark_node)
32671 {
32672 step = NULL_TREE;
32673 cp_parser_abort_tentative_parse (parser);
32674 }
32675 else if (!cp_parser_parse_definitely (parser))
32676 step = NULL_TREE;
32677 }
32678 if (!step)
32679 step = cp_parser_expression (parser);
32680
32681 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32682 {
32683 sorry ("using parameters for %<linear%> step is not supported yet");
32684 step = integer_one_node;
32685 }
32686 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32687 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32688 /*or_comma=*/false,
32689 /*consume_paren=*/true);
32690
32691 if (step == error_mark_node)
32692 return list;
32693 }
32694
32695 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32696 {
32697 OMP_CLAUSE_LINEAR_STEP (c) = step;
32698 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32699 }
32700
32701 return nlist;
32702 }
32703
32704 /* OpenMP 4.0:
32705 safelen ( constant-expression ) */
32706
32707 static tree
32708 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32709 location_t location)
32710 {
32711 tree t, c;
32712
32713 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32714 return list;
32715
32716 t = cp_parser_constant_expression (parser);
32717
32718 if (t == error_mark_node
32719 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32720 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32721 /*or_comma=*/false,
32722 /*consume_paren=*/true);
32723
32724 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32725
32726 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32727 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32728 OMP_CLAUSE_CHAIN (c) = list;
32729
32730 return c;
32731 }
32732
32733 /* OpenMP 4.0:
32734 simdlen ( constant-expression ) */
32735
32736 static tree
32737 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32738 location_t location)
32739 {
32740 tree t, c;
32741
32742 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32743 return list;
32744
32745 t = cp_parser_constant_expression (parser);
32746
32747 if (t == error_mark_node
32748 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32749 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32750 /*or_comma=*/false,
32751 /*consume_paren=*/true);
32752
32753 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32754
32755 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32756 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32757 OMP_CLAUSE_CHAIN (c) = list;
32758
32759 return c;
32760 }
32761
32762 /* OpenMP 4.5:
32763 vec:
32764 identifier [+/- integer]
32765 vec , identifier [+/- integer]
32766 */
32767
32768 static tree
32769 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32770 tree list)
32771 {
32772 tree vec = NULL;
32773
32774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32775 {
32776 cp_parser_error (parser, "expected identifier");
32777 return list;
32778 }
32779
32780 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32781 {
32782 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32783 tree t, identifier = cp_parser_identifier (parser);
32784 tree addend = NULL;
32785
32786 if (identifier == error_mark_node)
32787 t = error_mark_node;
32788 else
32789 {
32790 t = cp_parser_lookup_name_simple
32791 (parser, identifier,
32792 cp_lexer_peek_token (parser->lexer)->location);
32793 if (t == error_mark_node)
32794 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32795 id_loc);
32796 }
32797
32798 bool neg = false;
32799 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32800 neg = true;
32801 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32802 {
32803 addend = integer_zero_node;
32804 goto add_to_vector;
32805 }
32806 cp_lexer_consume_token (parser->lexer);
32807
32808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32809 {
32810 cp_parser_error (parser, "expected integer");
32811 return list;
32812 }
32813
32814 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32815 if (TREE_CODE (addend) != INTEGER_CST)
32816 {
32817 cp_parser_error (parser, "expected integer");
32818 return list;
32819 }
32820 cp_lexer_consume_token (parser->lexer);
32821
32822 add_to_vector:
32823 if (t != error_mark_node)
32824 {
32825 vec = tree_cons (addend, t, vec);
32826 if (neg)
32827 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32828 }
32829
32830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32831 break;
32832
32833 cp_lexer_consume_token (parser->lexer);
32834 }
32835
32836 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32837 {
32838 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32839 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32840 OMP_CLAUSE_DECL (u) = nreverse (vec);
32841 OMP_CLAUSE_CHAIN (u) = list;
32842 return u;
32843 }
32844 return list;
32845 }
32846
32847 /* OpenMP 4.0:
32848 depend ( depend-kind : variable-list )
32849
32850 depend-kind:
32851 in | out | inout
32852
32853 OpenMP 4.5:
32854 depend ( source )
32855
32856 depend ( sink : vec ) */
32857
32858 static tree
32859 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32860 {
32861 tree nlist, c;
32862 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32863
32864 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32865 return list;
32866
32867 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32868 {
32869 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32870 const char *p = IDENTIFIER_POINTER (id);
32871
32872 if (strcmp ("in", p) == 0)
32873 kind = OMP_CLAUSE_DEPEND_IN;
32874 else if (strcmp ("inout", p) == 0)
32875 kind = OMP_CLAUSE_DEPEND_INOUT;
32876 else if (strcmp ("out", p) == 0)
32877 kind = OMP_CLAUSE_DEPEND_OUT;
32878 else if (strcmp ("source", p) == 0)
32879 kind = OMP_CLAUSE_DEPEND_SOURCE;
32880 else if (strcmp ("sink", p) == 0)
32881 kind = OMP_CLAUSE_DEPEND_SINK;
32882 else
32883 goto invalid_kind;
32884 }
32885 else
32886 goto invalid_kind;
32887
32888 cp_lexer_consume_token (parser->lexer);
32889
32890 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32891 {
32892 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32893 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32894 OMP_CLAUSE_DECL (c) = NULL_TREE;
32895 OMP_CLAUSE_CHAIN (c) = list;
32896 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32897 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32898 /*or_comma=*/false,
32899 /*consume_paren=*/true);
32900 return c;
32901 }
32902
32903 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32904 goto resync_fail;
32905
32906 if (kind == OMP_CLAUSE_DEPEND_SINK)
32907 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32908 else
32909 {
32910 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32911 list, NULL);
32912
32913 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32914 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32915 }
32916 return nlist;
32917
32918 invalid_kind:
32919 cp_parser_error (parser, "invalid depend kind");
32920 resync_fail:
32921 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32922 /*or_comma=*/false,
32923 /*consume_paren=*/true);
32924 return list;
32925 }
32926
32927 /* OpenMP 4.0:
32928 map ( map-kind : variable-list )
32929 map ( variable-list )
32930
32931 map-kind:
32932 alloc | to | from | tofrom
32933
32934 OpenMP 4.5:
32935 map-kind:
32936 alloc | to | from | tofrom | release | delete
32937
32938 map ( always [,] map-kind: variable-list ) */
32939
32940 static tree
32941 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32942 {
32943 tree nlist, c;
32944 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32945 bool always = false;
32946
32947 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32948 return list;
32949
32950 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32951 {
32952 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32953 const char *p = IDENTIFIER_POINTER (id);
32954
32955 if (strcmp ("always", p) == 0)
32956 {
32957 int nth = 2;
32958 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32959 nth++;
32960 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32961 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32962 == RID_DELETE))
32963 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32964 == CPP_COLON))
32965 {
32966 always = true;
32967 cp_lexer_consume_token (parser->lexer);
32968 if (nth == 3)
32969 cp_lexer_consume_token (parser->lexer);
32970 }
32971 }
32972 }
32973
32974 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32975 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32976 {
32977 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32978 const char *p = IDENTIFIER_POINTER (id);
32979
32980 if (strcmp ("alloc", p) == 0)
32981 kind = GOMP_MAP_ALLOC;
32982 else if (strcmp ("to", p) == 0)
32983 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32984 else if (strcmp ("from", p) == 0)
32985 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32986 else if (strcmp ("tofrom", p) == 0)
32987 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32988 else if (strcmp ("release", p) == 0)
32989 kind = GOMP_MAP_RELEASE;
32990 else
32991 {
32992 cp_parser_error (parser, "invalid map kind");
32993 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32994 /*or_comma=*/false,
32995 /*consume_paren=*/true);
32996 return list;
32997 }
32998 cp_lexer_consume_token (parser->lexer);
32999 cp_lexer_consume_token (parser->lexer);
33000 }
33001 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33002 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33003 {
33004 kind = GOMP_MAP_DELETE;
33005 cp_lexer_consume_token (parser->lexer);
33006 cp_lexer_consume_token (parser->lexer);
33007 }
33008
33009 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33010 NULL);
33011
33012 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33013 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33014
33015 return nlist;
33016 }
33017
33018 /* OpenMP 4.0:
33019 device ( expression ) */
33020
33021 static tree
33022 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33023 location_t location)
33024 {
33025 tree t, c;
33026
33027 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33028 return list;
33029
33030 t = cp_parser_expression (parser);
33031
33032 if (t == error_mark_node
33033 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33034 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33035 /*or_comma=*/false,
33036 /*consume_paren=*/true);
33037
33038 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33039 "device", location);
33040
33041 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33042 OMP_CLAUSE_DEVICE_ID (c) = t;
33043 OMP_CLAUSE_CHAIN (c) = list;
33044
33045 return c;
33046 }
33047
33048 /* OpenMP 4.0:
33049 dist_schedule ( static )
33050 dist_schedule ( static , expression ) */
33051
33052 static tree
33053 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33054 location_t location)
33055 {
33056 tree c, t;
33057
33058 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33059 return list;
33060
33061 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33062
33063 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33064 goto invalid_kind;
33065 cp_lexer_consume_token (parser->lexer);
33066
33067 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33068 {
33069 cp_lexer_consume_token (parser->lexer);
33070
33071 t = cp_parser_assignment_expression (parser);
33072
33073 if (t == error_mark_node)
33074 goto resync_fail;
33075 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33076
33077 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33078 goto resync_fail;
33079 }
33080 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33081 goto resync_fail;
33082
33083 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33084 location);
33085 OMP_CLAUSE_CHAIN (c) = list;
33086 return c;
33087
33088 invalid_kind:
33089 cp_parser_error (parser, "invalid dist_schedule kind");
33090 resync_fail:
33091 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33092 /*or_comma=*/false,
33093 /*consume_paren=*/true);
33094 return list;
33095 }
33096
33097 /* OpenMP 4.0:
33098 proc_bind ( proc-bind-kind )
33099
33100 proc-bind-kind:
33101 master | close | spread */
33102
33103 static tree
33104 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33105 location_t location)
33106 {
33107 tree c;
33108 enum omp_clause_proc_bind_kind kind;
33109
33110 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33111 return list;
33112
33113 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33114 {
33115 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33116 const char *p = IDENTIFIER_POINTER (id);
33117
33118 if (strcmp ("master", p) == 0)
33119 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33120 else if (strcmp ("close", p) == 0)
33121 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33122 else if (strcmp ("spread", p) == 0)
33123 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33124 else
33125 goto invalid_kind;
33126 }
33127 else
33128 goto invalid_kind;
33129
33130 cp_lexer_consume_token (parser->lexer);
33131 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33132 goto resync_fail;
33133
33134 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33135 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33136 location);
33137 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33138 OMP_CLAUSE_CHAIN (c) = list;
33139 return c;
33140
33141 invalid_kind:
33142 cp_parser_error (parser, "invalid depend kind");
33143 resync_fail:
33144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33145 /*or_comma=*/false,
33146 /*consume_paren=*/true);
33147 return list;
33148 }
33149
33150 /* OpenACC:
33151 async [( int-expr )] */
33152
33153 static tree
33154 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33155 {
33156 tree c, t;
33157 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33158
33159 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33160
33161 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33162 {
33163 cp_lexer_consume_token (parser->lexer);
33164
33165 t = cp_parser_expression (parser);
33166 if (t == error_mark_node
33167 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33168 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33169 /*or_comma=*/false,
33170 /*consume_paren=*/true);
33171 }
33172
33173 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33174
33175 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33176 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33177 OMP_CLAUSE_CHAIN (c) = list;
33178 list = c;
33179
33180 return list;
33181 }
33182
33183 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33184 is a bitmask in MASK. Return the list of clauses found. */
33185
33186 static tree
33187 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33188 const char *where, cp_token *pragma_tok,
33189 bool finish_p = true)
33190 {
33191 tree clauses = NULL;
33192 bool first = true;
33193
33194 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33195 {
33196 location_t here;
33197 pragma_omp_clause c_kind;
33198 omp_clause_code code;
33199 const char *c_name;
33200 tree prev = clauses;
33201
33202 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33203 cp_lexer_consume_token (parser->lexer);
33204
33205 here = cp_lexer_peek_token (parser->lexer)->location;
33206 c_kind = cp_parser_omp_clause_name (parser);
33207
33208 switch (c_kind)
33209 {
33210 case PRAGMA_OACC_CLAUSE_ASYNC:
33211 clauses = cp_parser_oacc_clause_async (parser, clauses);
33212 c_name = "async";
33213 break;
33214 case PRAGMA_OACC_CLAUSE_AUTO:
33215 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33216 clauses, here);
33217 c_name = "auto";
33218 break;
33219 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33220 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33221 c_name = "collapse";
33222 break;
33223 case PRAGMA_OACC_CLAUSE_COPY:
33224 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33225 c_name = "copy";
33226 break;
33227 case PRAGMA_OACC_CLAUSE_COPYIN:
33228 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33229 c_name = "copyin";
33230 break;
33231 case PRAGMA_OACC_CLAUSE_COPYOUT:
33232 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33233 c_name = "copyout";
33234 break;
33235 case PRAGMA_OACC_CLAUSE_CREATE:
33236 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33237 c_name = "create";
33238 break;
33239 case PRAGMA_OACC_CLAUSE_DELETE:
33240 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33241 c_name = "delete";
33242 break;
33243 case PRAGMA_OMP_CLAUSE_DEFAULT:
33244 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33245 c_name = "default";
33246 break;
33247 case PRAGMA_OACC_CLAUSE_DEVICE:
33248 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33249 c_name = "device";
33250 break;
33251 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33252 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33253 c_name = "deviceptr";
33254 break;
33255 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33256 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33257 c_name = "device_resident";
33258 break;
33259 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33260 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33261 clauses);
33262 c_name = "firstprivate";
33263 break;
33264 case PRAGMA_OACC_CLAUSE_GANG:
33265 c_name = "gang";
33266 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33267 c_name, clauses);
33268 break;
33269 case PRAGMA_OACC_CLAUSE_HOST:
33270 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33271 c_name = "host";
33272 break;
33273 case PRAGMA_OACC_CLAUSE_IF:
33274 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33275 c_name = "if";
33276 break;
33277 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33278 clauses = cp_parser_oacc_simple_clause (parser,
33279 OMP_CLAUSE_INDEPENDENT,
33280 clauses, here);
33281 c_name = "independent";
33282 break;
33283 case PRAGMA_OACC_CLAUSE_LINK:
33284 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33285 c_name = "link";
33286 break;
33287 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33288 code = OMP_CLAUSE_NUM_GANGS;
33289 c_name = "num_gangs";
33290 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33291 clauses);
33292 break;
33293 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33294 c_name = "num_workers";
33295 code = OMP_CLAUSE_NUM_WORKERS;
33296 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33297 clauses);
33298 break;
33299 case PRAGMA_OACC_CLAUSE_PRESENT:
33300 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33301 c_name = "present";
33302 break;
33303 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33304 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33305 c_name = "present_or_copy";
33306 break;
33307 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33308 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33309 c_name = "present_or_copyin";
33310 break;
33311 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33312 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33313 c_name = "present_or_copyout";
33314 break;
33315 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33316 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33317 c_name = "present_or_create";
33318 break;
33319 case PRAGMA_OACC_CLAUSE_PRIVATE:
33320 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33321 clauses);
33322 c_name = "private";
33323 break;
33324 case PRAGMA_OACC_CLAUSE_REDUCTION:
33325 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33326 c_name = "reduction";
33327 break;
33328 case PRAGMA_OACC_CLAUSE_SELF:
33329 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33330 c_name = "self";
33331 break;
33332 case PRAGMA_OACC_CLAUSE_SEQ:
33333 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33334 clauses, here);
33335 c_name = "seq";
33336 break;
33337 case PRAGMA_OACC_CLAUSE_TILE:
33338 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33339 c_name = "tile";
33340 break;
33341 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33342 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33343 clauses);
33344 c_name = "use_device";
33345 break;
33346 case PRAGMA_OACC_CLAUSE_VECTOR:
33347 c_name = "vector";
33348 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33349 c_name, clauses);
33350 break;
33351 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33352 c_name = "vector_length";
33353 code = OMP_CLAUSE_VECTOR_LENGTH;
33354 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33355 clauses);
33356 break;
33357 case PRAGMA_OACC_CLAUSE_WAIT:
33358 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33359 c_name = "wait";
33360 break;
33361 case PRAGMA_OACC_CLAUSE_WORKER:
33362 c_name = "worker";
33363 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33364 c_name, clauses);
33365 break;
33366 default:
33367 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33368 goto saw_error;
33369 }
33370
33371 first = false;
33372
33373 if (((mask >> c_kind) & 1) == 0)
33374 {
33375 /* Remove the invalid clause(s) from the list to avoid
33376 confusing the rest of the compiler. */
33377 clauses = prev;
33378 error_at (here, "%qs is not valid for %qs", c_name, where);
33379 }
33380 }
33381
33382 saw_error:
33383 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33384
33385 if (finish_p)
33386 return finish_omp_clauses (clauses, C_ORT_ACC);
33387
33388 return clauses;
33389 }
33390
33391 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33392 is a bitmask in MASK. Return the list of clauses found; the result
33393 of clause default goes in *pdefault. */
33394
33395 static tree
33396 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33397 const char *where, cp_token *pragma_tok,
33398 bool finish_p = true)
33399 {
33400 tree clauses = NULL;
33401 bool first = true;
33402 cp_token *token = NULL;
33403
33404 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33405 {
33406 pragma_omp_clause c_kind;
33407 const char *c_name;
33408 tree prev = clauses;
33409
33410 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33411 cp_lexer_consume_token (parser->lexer);
33412
33413 token = cp_lexer_peek_token (parser->lexer);
33414 c_kind = cp_parser_omp_clause_name (parser);
33415
33416 switch (c_kind)
33417 {
33418 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33419 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33420 token->location);
33421 c_name = "collapse";
33422 break;
33423 case PRAGMA_OMP_CLAUSE_COPYIN:
33424 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33425 c_name = "copyin";
33426 break;
33427 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33428 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33429 clauses);
33430 c_name = "copyprivate";
33431 break;
33432 case PRAGMA_OMP_CLAUSE_DEFAULT:
33433 clauses = cp_parser_omp_clause_default (parser, clauses,
33434 token->location, false);
33435 c_name = "default";
33436 break;
33437 case PRAGMA_OMP_CLAUSE_FINAL:
33438 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33439 c_name = "final";
33440 break;
33441 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33442 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33443 clauses);
33444 c_name = "firstprivate";
33445 break;
33446 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33447 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33448 token->location);
33449 c_name = "grainsize";
33450 break;
33451 case PRAGMA_OMP_CLAUSE_HINT:
33452 clauses = cp_parser_omp_clause_hint (parser, clauses,
33453 token->location);
33454 c_name = "hint";
33455 break;
33456 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33457 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33458 token->location);
33459 c_name = "defaultmap";
33460 break;
33461 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33462 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33463 clauses);
33464 c_name = "use_device_ptr";
33465 break;
33466 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33467 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33468 clauses);
33469 c_name = "is_device_ptr";
33470 break;
33471 case PRAGMA_OMP_CLAUSE_IF:
33472 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33473 true);
33474 c_name = "if";
33475 break;
33476 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33477 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33478 clauses);
33479 c_name = "lastprivate";
33480 break;
33481 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33482 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33483 token->location);
33484 c_name = "mergeable";
33485 break;
33486 case PRAGMA_OMP_CLAUSE_NOWAIT:
33487 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33488 c_name = "nowait";
33489 break;
33490 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33491 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33492 token->location);
33493 c_name = "num_tasks";
33494 break;
33495 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33496 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33497 token->location);
33498 c_name = "num_threads";
33499 break;
33500 case PRAGMA_OMP_CLAUSE_ORDERED:
33501 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33502 token->location);
33503 c_name = "ordered";
33504 break;
33505 case PRAGMA_OMP_CLAUSE_PRIORITY:
33506 clauses = cp_parser_omp_clause_priority (parser, clauses,
33507 token->location);
33508 c_name = "priority";
33509 break;
33510 case PRAGMA_OMP_CLAUSE_PRIVATE:
33511 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33512 clauses);
33513 c_name = "private";
33514 break;
33515 case PRAGMA_OMP_CLAUSE_REDUCTION:
33516 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33517 c_name = "reduction";
33518 break;
33519 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33520 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33521 token->location);
33522 c_name = "schedule";
33523 break;
33524 case PRAGMA_OMP_CLAUSE_SHARED:
33525 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33526 clauses);
33527 c_name = "shared";
33528 break;
33529 case PRAGMA_OMP_CLAUSE_UNTIED:
33530 clauses = cp_parser_omp_clause_untied (parser, clauses,
33531 token->location);
33532 c_name = "untied";
33533 break;
33534 case PRAGMA_OMP_CLAUSE_INBRANCH:
33535 case PRAGMA_CILK_CLAUSE_MASK:
33536 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33537 clauses, token->location);
33538 c_name = "inbranch";
33539 break;
33540 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33541 case PRAGMA_CILK_CLAUSE_NOMASK:
33542 clauses = cp_parser_omp_clause_branch (parser,
33543 OMP_CLAUSE_NOTINBRANCH,
33544 clauses, token->location);
33545 c_name = "notinbranch";
33546 break;
33547 case PRAGMA_OMP_CLAUSE_PARALLEL:
33548 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33549 clauses, token->location);
33550 c_name = "parallel";
33551 if (!first)
33552 {
33553 clause_not_first:
33554 error_at (token->location, "%qs must be the first clause of %qs",
33555 c_name, where);
33556 clauses = prev;
33557 }
33558 break;
33559 case PRAGMA_OMP_CLAUSE_FOR:
33560 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33561 clauses, token->location);
33562 c_name = "for";
33563 if (!first)
33564 goto clause_not_first;
33565 break;
33566 case PRAGMA_OMP_CLAUSE_SECTIONS:
33567 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33568 clauses, token->location);
33569 c_name = "sections";
33570 if (!first)
33571 goto clause_not_first;
33572 break;
33573 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33574 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33575 clauses, token->location);
33576 c_name = "taskgroup";
33577 if (!first)
33578 goto clause_not_first;
33579 break;
33580 case PRAGMA_OMP_CLAUSE_LINK:
33581 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33582 c_name = "to";
33583 break;
33584 case PRAGMA_OMP_CLAUSE_TO:
33585 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33586 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33587 clauses);
33588 else
33589 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33590 c_name = "to";
33591 break;
33592 case PRAGMA_OMP_CLAUSE_FROM:
33593 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33594 c_name = "from";
33595 break;
33596 case PRAGMA_OMP_CLAUSE_UNIFORM:
33597 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33598 clauses);
33599 c_name = "uniform";
33600 break;
33601 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33602 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33603 token->location);
33604 c_name = "num_teams";
33605 break;
33606 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33607 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33608 token->location);
33609 c_name = "thread_limit";
33610 break;
33611 case PRAGMA_OMP_CLAUSE_ALIGNED:
33612 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33613 c_name = "aligned";
33614 break;
33615 case PRAGMA_OMP_CLAUSE_LINEAR:
33616 {
33617 bool cilk_simd_fn = false, declare_simd = false;
33618 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33619 cilk_simd_fn = true;
33620 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33621 declare_simd = true;
33622 clauses = cp_parser_omp_clause_linear (parser, clauses,
33623 cilk_simd_fn, declare_simd);
33624 }
33625 c_name = "linear";
33626 break;
33627 case PRAGMA_OMP_CLAUSE_DEPEND:
33628 clauses = cp_parser_omp_clause_depend (parser, clauses,
33629 token->location);
33630 c_name = "depend";
33631 break;
33632 case PRAGMA_OMP_CLAUSE_MAP:
33633 clauses = cp_parser_omp_clause_map (parser, clauses);
33634 c_name = "map";
33635 break;
33636 case PRAGMA_OMP_CLAUSE_DEVICE:
33637 clauses = cp_parser_omp_clause_device (parser, clauses,
33638 token->location);
33639 c_name = "device";
33640 break;
33641 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33642 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33643 token->location);
33644 c_name = "dist_schedule";
33645 break;
33646 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33647 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33648 token->location);
33649 c_name = "proc_bind";
33650 break;
33651 case PRAGMA_OMP_CLAUSE_SAFELEN:
33652 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33653 token->location);
33654 c_name = "safelen";
33655 break;
33656 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33657 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33658 token->location);
33659 c_name = "simdlen";
33660 break;
33661 case PRAGMA_OMP_CLAUSE_NOGROUP:
33662 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33663 token->location);
33664 c_name = "nogroup";
33665 break;
33666 case PRAGMA_OMP_CLAUSE_THREADS:
33667 clauses
33668 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33669 clauses, token->location);
33670 c_name = "threads";
33671 break;
33672 case PRAGMA_OMP_CLAUSE_SIMD:
33673 clauses
33674 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33675 clauses, token->location);
33676 c_name = "simd";
33677 break;
33678 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33679 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33680 c_name = "simdlen";
33681 break;
33682 default:
33683 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33684 goto saw_error;
33685 }
33686
33687 first = false;
33688
33689 if (((mask >> c_kind) & 1) == 0)
33690 {
33691 /* Remove the invalid clause(s) from the list to avoid
33692 confusing the rest of the compiler. */
33693 clauses = prev;
33694 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33695 }
33696 }
33697 saw_error:
33698 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33699 no reason to skip to the end. */
33700 if (!(flag_cilkplus && pragma_tok == NULL))
33701 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33702 if (finish_p)
33703 {
33704 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33705 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33706 else
33707 return finish_omp_clauses (clauses, C_ORT_OMP);
33708 }
33709 return clauses;
33710 }
33711
33712 /* OpenMP 2.5:
33713 structured-block:
33714 statement
33715
33716 In practice, we're also interested in adding the statement to an
33717 outer node. So it is convenient if we work around the fact that
33718 cp_parser_statement calls add_stmt. */
33719
33720 static unsigned
33721 cp_parser_begin_omp_structured_block (cp_parser *parser)
33722 {
33723 unsigned save = parser->in_statement;
33724
33725 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33726 This preserves the "not within loop or switch" style error messages
33727 for nonsense cases like
33728 void foo() {
33729 #pragma omp single
33730 break;
33731 }
33732 */
33733 if (parser->in_statement)
33734 parser->in_statement = IN_OMP_BLOCK;
33735
33736 return save;
33737 }
33738
33739 static void
33740 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33741 {
33742 parser->in_statement = save;
33743 }
33744
33745 static tree
33746 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33747 {
33748 tree stmt = begin_omp_structured_block ();
33749 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33750
33751 cp_parser_statement (parser, NULL_TREE, false, if_p);
33752
33753 cp_parser_end_omp_structured_block (parser, save);
33754 return finish_omp_structured_block (stmt);
33755 }
33756
33757 /* OpenMP 2.5:
33758 # pragma omp atomic new-line
33759 expression-stmt
33760
33761 expression-stmt:
33762 x binop= expr | x++ | ++x | x-- | --x
33763 binop:
33764 +, *, -, /, &, ^, |, <<, >>
33765
33766 where x is an lvalue expression with scalar type.
33767
33768 OpenMP 3.1:
33769 # pragma omp atomic new-line
33770 update-stmt
33771
33772 # pragma omp atomic read new-line
33773 read-stmt
33774
33775 # pragma omp atomic write new-line
33776 write-stmt
33777
33778 # pragma omp atomic update new-line
33779 update-stmt
33780
33781 # pragma omp atomic capture new-line
33782 capture-stmt
33783
33784 # pragma omp atomic capture new-line
33785 capture-block
33786
33787 read-stmt:
33788 v = x
33789 write-stmt:
33790 x = expr
33791 update-stmt:
33792 expression-stmt | x = x binop expr
33793 capture-stmt:
33794 v = expression-stmt
33795 capture-block:
33796 { v = x; update-stmt; } | { update-stmt; v = x; }
33797
33798 OpenMP 4.0:
33799 update-stmt:
33800 expression-stmt | x = x binop expr | x = expr binop x
33801 capture-stmt:
33802 v = update-stmt
33803 capture-block:
33804 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33805
33806 where x and v are lvalue expressions with scalar type. */
33807
33808 static void
33809 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33810 {
33811 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33812 tree rhs1 = NULL_TREE, orig_lhs;
33813 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33814 bool structured_block = false;
33815 bool seq_cst = false;
33816
33817 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33818 {
33819 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33820 const char *p = IDENTIFIER_POINTER (id);
33821
33822 if (!strcmp (p, "seq_cst"))
33823 {
33824 seq_cst = true;
33825 cp_lexer_consume_token (parser->lexer);
33826 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33827 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33828 cp_lexer_consume_token (parser->lexer);
33829 }
33830 }
33831 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33832 {
33833 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33834 const char *p = IDENTIFIER_POINTER (id);
33835
33836 if (!strcmp (p, "read"))
33837 code = OMP_ATOMIC_READ;
33838 else if (!strcmp (p, "write"))
33839 code = NOP_EXPR;
33840 else if (!strcmp (p, "update"))
33841 code = OMP_ATOMIC;
33842 else if (!strcmp (p, "capture"))
33843 code = OMP_ATOMIC_CAPTURE_NEW;
33844 else
33845 p = NULL;
33846 if (p)
33847 cp_lexer_consume_token (parser->lexer);
33848 }
33849 if (!seq_cst)
33850 {
33851 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33852 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33853 cp_lexer_consume_token (parser->lexer);
33854
33855 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33856 {
33857 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33858 const char *p = IDENTIFIER_POINTER (id);
33859
33860 if (!strcmp (p, "seq_cst"))
33861 {
33862 seq_cst = true;
33863 cp_lexer_consume_token (parser->lexer);
33864 }
33865 }
33866 }
33867 cp_parser_require_pragma_eol (parser, pragma_tok);
33868
33869 switch (code)
33870 {
33871 case OMP_ATOMIC_READ:
33872 case NOP_EXPR: /* atomic write */
33873 v = cp_parser_unary_expression (parser);
33874 if (v == error_mark_node)
33875 goto saw_error;
33876 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33877 goto saw_error;
33878 if (code == NOP_EXPR)
33879 lhs = cp_parser_expression (parser);
33880 else
33881 lhs = cp_parser_unary_expression (parser);
33882 if (lhs == error_mark_node)
33883 goto saw_error;
33884 if (code == NOP_EXPR)
33885 {
33886 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33887 opcode. */
33888 code = OMP_ATOMIC;
33889 rhs = lhs;
33890 lhs = v;
33891 v = NULL_TREE;
33892 }
33893 goto done;
33894 case OMP_ATOMIC_CAPTURE_NEW:
33895 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33896 {
33897 cp_lexer_consume_token (parser->lexer);
33898 structured_block = true;
33899 }
33900 else
33901 {
33902 v = cp_parser_unary_expression (parser);
33903 if (v == error_mark_node)
33904 goto saw_error;
33905 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33906 goto saw_error;
33907 }
33908 default:
33909 break;
33910 }
33911
33912 restart:
33913 lhs = cp_parser_unary_expression (parser);
33914 orig_lhs = lhs;
33915 switch (TREE_CODE (lhs))
33916 {
33917 case ERROR_MARK:
33918 goto saw_error;
33919
33920 case POSTINCREMENT_EXPR:
33921 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33922 code = OMP_ATOMIC_CAPTURE_OLD;
33923 /* FALLTHROUGH */
33924 case PREINCREMENT_EXPR:
33925 lhs = TREE_OPERAND (lhs, 0);
33926 opcode = PLUS_EXPR;
33927 rhs = integer_one_node;
33928 break;
33929
33930 case POSTDECREMENT_EXPR:
33931 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33932 code = OMP_ATOMIC_CAPTURE_OLD;
33933 /* FALLTHROUGH */
33934 case PREDECREMENT_EXPR:
33935 lhs = TREE_OPERAND (lhs, 0);
33936 opcode = MINUS_EXPR;
33937 rhs = integer_one_node;
33938 break;
33939
33940 case COMPOUND_EXPR:
33941 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33942 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33943 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33944 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33945 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33946 (TREE_OPERAND (lhs, 1), 0), 0)))
33947 == BOOLEAN_TYPE)
33948 /* Undo effects of boolean_increment for post {in,de}crement. */
33949 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33950 /* FALLTHRU */
33951 case MODIFY_EXPR:
33952 if (TREE_CODE (lhs) == MODIFY_EXPR
33953 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33954 {
33955 /* Undo effects of boolean_increment. */
33956 if (integer_onep (TREE_OPERAND (lhs, 1)))
33957 {
33958 /* This is pre or post increment. */
33959 rhs = TREE_OPERAND (lhs, 1);
33960 lhs = TREE_OPERAND (lhs, 0);
33961 opcode = NOP_EXPR;
33962 if (code == OMP_ATOMIC_CAPTURE_NEW
33963 && !structured_block
33964 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33965 code = OMP_ATOMIC_CAPTURE_OLD;
33966 break;
33967 }
33968 }
33969 /* FALLTHRU */
33970 default:
33971 switch (cp_lexer_peek_token (parser->lexer)->type)
33972 {
33973 case CPP_MULT_EQ:
33974 opcode = MULT_EXPR;
33975 break;
33976 case CPP_DIV_EQ:
33977 opcode = TRUNC_DIV_EXPR;
33978 break;
33979 case CPP_PLUS_EQ:
33980 opcode = PLUS_EXPR;
33981 break;
33982 case CPP_MINUS_EQ:
33983 opcode = MINUS_EXPR;
33984 break;
33985 case CPP_LSHIFT_EQ:
33986 opcode = LSHIFT_EXPR;
33987 break;
33988 case CPP_RSHIFT_EQ:
33989 opcode = RSHIFT_EXPR;
33990 break;
33991 case CPP_AND_EQ:
33992 opcode = BIT_AND_EXPR;
33993 break;
33994 case CPP_OR_EQ:
33995 opcode = BIT_IOR_EXPR;
33996 break;
33997 case CPP_XOR_EQ:
33998 opcode = BIT_XOR_EXPR;
33999 break;
34000 case CPP_EQ:
34001 enum cp_parser_prec oprec;
34002 cp_token *token;
34003 cp_lexer_consume_token (parser->lexer);
34004 cp_parser_parse_tentatively (parser);
34005 rhs1 = cp_parser_simple_cast_expression (parser);
34006 if (rhs1 == error_mark_node)
34007 {
34008 cp_parser_abort_tentative_parse (parser);
34009 cp_parser_simple_cast_expression (parser);
34010 goto saw_error;
34011 }
34012 token = cp_lexer_peek_token (parser->lexer);
34013 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34014 {
34015 cp_parser_abort_tentative_parse (parser);
34016 cp_parser_parse_tentatively (parser);
34017 rhs = cp_parser_binary_expression (parser, false, true,
34018 PREC_NOT_OPERATOR, NULL);
34019 if (rhs == error_mark_node)
34020 {
34021 cp_parser_abort_tentative_parse (parser);
34022 cp_parser_binary_expression (parser, false, true,
34023 PREC_NOT_OPERATOR, NULL);
34024 goto saw_error;
34025 }
34026 switch (TREE_CODE (rhs))
34027 {
34028 case MULT_EXPR:
34029 case TRUNC_DIV_EXPR:
34030 case RDIV_EXPR:
34031 case PLUS_EXPR:
34032 case MINUS_EXPR:
34033 case LSHIFT_EXPR:
34034 case RSHIFT_EXPR:
34035 case BIT_AND_EXPR:
34036 case BIT_IOR_EXPR:
34037 case BIT_XOR_EXPR:
34038 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34039 {
34040 if (cp_parser_parse_definitely (parser))
34041 {
34042 opcode = TREE_CODE (rhs);
34043 rhs1 = TREE_OPERAND (rhs, 0);
34044 rhs = TREE_OPERAND (rhs, 1);
34045 goto stmt_done;
34046 }
34047 else
34048 goto saw_error;
34049 }
34050 break;
34051 default:
34052 break;
34053 }
34054 cp_parser_abort_tentative_parse (parser);
34055 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34056 {
34057 rhs = cp_parser_expression (parser);
34058 if (rhs == error_mark_node)
34059 goto saw_error;
34060 opcode = NOP_EXPR;
34061 rhs1 = NULL_TREE;
34062 goto stmt_done;
34063 }
34064 cp_parser_error (parser,
34065 "invalid form of %<#pragma omp atomic%>");
34066 goto saw_error;
34067 }
34068 if (!cp_parser_parse_definitely (parser))
34069 goto saw_error;
34070 switch (token->type)
34071 {
34072 case CPP_SEMICOLON:
34073 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34074 {
34075 code = OMP_ATOMIC_CAPTURE_OLD;
34076 v = lhs;
34077 lhs = NULL_TREE;
34078 lhs1 = rhs1;
34079 rhs1 = NULL_TREE;
34080 cp_lexer_consume_token (parser->lexer);
34081 goto restart;
34082 }
34083 else if (structured_block)
34084 {
34085 opcode = NOP_EXPR;
34086 rhs = rhs1;
34087 rhs1 = NULL_TREE;
34088 goto stmt_done;
34089 }
34090 cp_parser_error (parser,
34091 "invalid form of %<#pragma omp atomic%>");
34092 goto saw_error;
34093 case CPP_MULT:
34094 opcode = MULT_EXPR;
34095 break;
34096 case CPP_DIV:
34097 opcode = TRUNC_DIV_EXPR;
34098 break;
34099 case CPP_PLUS:
34100 opcode = PLUS_EXPR;
34101 break;
34102 case CPP_MINUS:
34103 opcode = MINUS_EXPR;
34104 break;
34105 case CPP_LSHIFT:
34106 opcode = LSHIFT_EXPR;
34107 break;
34108 case CPP_RSHIFT:
34109 opcode = RSHIFT_EXPR;
34110 break;
34111 case CPP_AND:
34112 opcode = BIT_AND_EXPR;
34113 break;
34114 case CPP_OR:
34115 opcode = BIT_IOR_EXPR;
34116 break;
34117 case CPP_XOR:
34118 opcode = BIT_XOR_EXPR;
34119 break;
34120 default:
34121 cp_parser_error (parser,
34122 "invalid operator for %<#pragma omp atomic%>");
34123 goto saw_error;
34124 }
34125 oprec = TOKEN_PRECEDENCE (token);
34126 gcc_assert (oprec != PREC_NOT_OPERATOR);
34127 if (commutative_tree_code (opcode))
34128 oprec = (enum cp_parser_prec) (oprec - 1);
34129 cp_lexer_consume_token (parser->lexer);
34130 rhs = cp_parser_binary_expression (parser, false, false,
34131 oprec, NULL);
34132 if (rhs == error_mark_node)
34133 goto saw_error;
34134 goto stmt_done;
34135 /* FALLTHROUGH */
34136 default:
34137 cp_parser_error (parser,
34138 "invalid operator for %<#pragma omp atomic%>");
34139 goto saw_error;
34140 }
34141 cp_lexer_consume_token (parser->lexer);
34142
34143 rhs = cp_parser_expression (parser);
34144 if (rhs == error_mark_node)
34145 goto saw_error;
34146 break;
34147 }
34148 stmt_done:
34149 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34150 {
34151 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34152 goto saw_error;
34153 v = cp_parser_unary_expression (parser);
34154 if (v == error_mark_node)
34155 goto saw_error;
34156 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34157 goto saw_error;
34158 lhs1 = cp_parser_unary_expression (parser);
34159 if (lhs1 == error_mark_node)
34160 goto saw_error;
34161 }
34162 if (structured_block)
34163 {
34164 cp_parser_consume_semicolon_at_end_of_statement (parser);
34165 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34166 }
34167 done:
34168 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34169 if (!structured_block)
34170 cp_parser_consume_semicolon_at_end_of_statement (parser);
34171 return;
34172
34173 saw_error:
34174 cp_parser_skip_to_end_of_block_or_statement (parser);
34175 if (structured_block)
34176 {
34177 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34178 cp_lexer_consume_token (parser->lexer);
34179 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34180 {
34181 cp_parser_skip_to_end_of_block_or_statement (parser);
34182 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34183 cp_lexer_consume_token (parser->lexer);
34184 }
34185 }
34186 }
34187
34188
34189 /* OpenMP 2.5:
34190 # pragma omp barrier new-line */
34191
34192 static void
34193 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34194 {
34195 cp_parser_require_pragma_eol (parser, pragma_tok);
34196 finish_omp_barrier ();
34197 }
34198
34199 /* OpenMP 2.5:
34200 # pragma omp critical [(name)] new-line
34201 structured-block
34202
34203 OpenMP 4.5:
34204 # pragma omp critical [(name) [hint(expression)]] new-line
34205 structured-block */
34206
34207 #define OMP_CRITICAL_CLAUSE_MASK \
34208 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34209
34210 static tree
34211 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34212 {
34213 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34214
34215 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34216 {
34217 cp_lexer_consume_token (parser->lexer);
34218
34219 name = cp_parser_identifier (parser);
34220
34221 if (name == error_mark_node
34222 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34223 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34224 /*or_comma=*/false,
34225 /*consume_paren=*/true);
34226 if (name == error_mark_node)
34227 name = NULL;
34228
34229 clauses = cp_parser_omp_all_clauses (parser,
34230 OMP_CRITICAL_CLAUSE_MASK,
34231 "#pragma omp critical", pragma_tok);
34232 }
34233 else
34234 cp_parser_require_pragma_eol (parser, pragma_tok);
34235
34236 stmt = cp_parser_omp_structured_block (parser, if_p);
34237 return c_finish_omp_critical (input_location, stmt, name, clauses);
34238 }
34239
34240 /* OpenMP 2.5:
34241 # pragma omp flush flush-vars[opt] new-line
34242
34243 flush-vars:
34244 ( variable-list ) */
34245
34246 static void
34247 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34248 {
34249 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34250 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34251 cp_parser_require_pragma_eol (parser, pragma_tok);
34252
34253 finish_omp_flush ();
34254 }
34255
34256 /* Helper function, to parse omp for increment expression. */
34257
34258 static tree
34259 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34260 {
34261 tree cond = cp_parser_binary_expression (parser, false, true,
34262 PREC_NOT_OPERATOR, NULL);
34263 if (cond == error_mark_node
34264 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34265 {
34266 cp_parser_skip_to_end_of_statement (parser);
34267 return error_mark_node;
34268 }
34269
34270 switch (TREE_CODE (cond))
34271 {
34272 case GT_EXPR:
34273 case GE_EXPR:
34274 case LT_EXPR:
34275 case LE_EXPR:
34276 break;
34277 case NE_EXPR:
34278 if (code == CILK_SIMD || code == CILK_FOR)
34279 break;
34280 /* Fall through: OpenMP disallows NE_EXPR. */
34281 gcc_fallthrough ();
34282 default:
34283 return error_mark_node;
34284 }
34285
34286 /* If decl is an iterator, preserve LHS and RHS of the relational
34287 expr until finish_omp_for. */
34288 if (decl
34289 && (type_dependent_expression_p (decl)
34290 || CLASS_TYPE_P (TREE_TYPE (decl))))
34291 return cond;
34292
34293 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34294 TREE_CODE (cond),
34295 TREE_OPERAND (cond, 0), ERROR_MARK,
34296 TREE_OPERAND (cond, 1), ERROR_MARK,
34297 /*overload=*/NULL, tf_warning_or_error);
34298 }
34299
34300 /* Helper function, to parse omp for increment expression. */
34301
34302 static tree
34303 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34304 {
34305 cp_token *token = cp_lexer_peek_token (parser->lexer);
34306 enum tree_code op;
34307 tree lhs, rhs;
34308 cp_id_kind idk;
34309 bool decl_first;
34310
34311 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34312 {
34313 op = (token->type == CPP_PLUS_PLUS
34314 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34315 cp_lexer_consume_token (parser->lexer);
34316 lhs = cp_parser_simple_cast_expression (parser);
34317 if (lhs != decl
34318 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34319 return error_mark_node;
34320 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34321 }
34322
34323 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34324 if (lhs != decl
34325 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34326 return error_mark_node;
34327
34328 token = cp_lexer_peek_token (parser->lexer);
34329 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34330 {
34331 op = (token->type == CPP_PLUS_PLUS
34332 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34333 cp_lexer_consume_token (parser->lexer);
34334 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34335 }
34336
34337 op = cp_parser_assignment_operator_opt (parser);
34338 if (op == ERROR_MARK)
34339 return error_mark_node;
34340
34341 if (op != NOP_EXPR)
34342 {
34343 rhs = cp_parser_assignment_expression (parser);
34344 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34345 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34346 }
34347
34348 lhs = cp_parser_binary_expression (parser, false, false,
34349 PREC_ADDITIVE_EXPRESSION, NULL);
34350 token = cp_lexer_peek_token (parser->lexer);
34351 decl_first = (lhs == decl
34352 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34353 if (decl_first)
34354 lhs = NULL_TREE;
34355 if (token->type != CPP_PLUS
34356 && token->type != CPP_MINUS)
34357 return error_mark_node;
34358
34359 do
34360 {
34361 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34362 cp_lexer_consume_token (parser->lexer);
34363 rhs = cp_parser_binary_expression (parser, false, false,
34364 PREC_ADDITIVE_EXPRESSION, NULL);
34365 token = cp_lexer_peek_token (parser->lexer);
34366 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34367 {
34368 if (lhs == NULL_TREE)
34369 {
34370 if (op == PLUS_EXPR)
34371 lhs = rhs;
34372 else
34373 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34374 tf_warning_or_error);
34375 }
34376 else
34377 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34378 ERROR_MARK, NULL, tf_warning_or_error);
34379 }
34380 }
34381 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34382
34383 if (!decl_first)
34384 {
34385 if ((rhs != decl
34386 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34387 || op == MINUS_EXPR)
34388 return error_mark_node;
34389 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34390 }
34391 else
34392 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34393
34394 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34395 }
34396
34397 /* Parse the initialization statement of either an OpenMP for loop or
34398 a Cilk Plus for loop.
34399
34400 Return true if the resulting construct should have an
34401 OMP_CLAUSE_PRIVATE added to it. */
34402
34403 static tree
34404 cp_parser_omp_for_loop_init (cp_parser *parser,
34405 enum tree_code code,
34406 tree &this_pre_body,
34407 vec<tree, va_gc> *for_block,
34408 tree &init,
34409 tree &orig_init,
34410 tree &decl,
34411 tree &real_decl)
34412 {
34413 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34414 return NULL_TREE;
34415
34416 tree add_private_clause = NULL_TREE;
34417
34418 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34419
34420 init-expr:
34421 var = lb
34422 integer-type var = lb
34423 random-access-iterator-type var = lb
34424 pointer-type var = lb
34425 */
34426 cp_decl_specifier_seq type_specifiers;
34427
34428 /* First, try to parse as an initialized declaration. See
34429 cp_parser_condition, from whence the bulk of this is copied. */
34430
34431 cp_parser_parse_tentatively (parser);
34432 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34433 /*is_trailing_return=*/false,
34434 &type_specifiers);
34435 if (cp_parser_parse_definitely (parser))
34436 {
34437 /* If parsing a type specifier seq succeeded, then this
34438 MUST be a initialized declaration. */
34439 tree asm_specification, attributes;
34440 cp_declarator *declarator;
34441
34442 declarator = cp_parser_declarator (parser,
34443 CP_PARSER_DECLARATOR_NAMED,
34444 /*ctor_dtor_or_conv_p=*/NULL,
34445 /*parenthesized_p=*/NULL,
34446 /*member_p=*/false,
34447 /*friend_p=*/false);
34448 attributes = cp_parser_attributes_opt (parser);
34449 asm_specification = cp_parser_asm_specification_opt (parser);
34450
34451 if (declarator == cp_error_declarator)
34452 cp_parser_skip_to_end_of_statement (parser);
34453
34454 else
34455 {
34456 tree pushed_scope, auto_node;
34457
34458 decl = start_decl (declarator, &type_specifiers,
34459 SD_INITIALIZED, attributes,
34460 /*prefix_attributes=*/NULL_TREE,
34461 &pushed_scope);
34462
34463 auto_node = type_uses_auto (TREE_TYPE (decl));
34464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34465 {
34466 if (cp_lexer_next_token_is (parser->lexer,
34467 CPP_OPEN_PAREN))
34468 {
34469 if (code != CILK_SIMD && code != CILK_FOR)
34470 error ("parenthesized initialization is not allowed in "
34471 "OpenMP %<for%> loop");
34472 else
34473 error ("parenthesized initialization is "
34474 "not allowed in for-loop");
34475 }
34476 else
34477 /* Trigger an error. */
34478 cp_parser_require (parser, CPP_EQ, RT_EQ);
34479
34480 init = error_mark_node;
34481 cp_parser_skip_to_end_of_statement (parser);
34482 }
34483 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34484 || type_dependent_expression_p (decl)
34485 || auto_node)
34486 {
34487 bool is_direct_init, is_non_constant_init;
34488
34489 init = cp_parser_initializer (parser,
34490 &is_direct_init,
34491 &is_non_constant_init);
34492
34493 if (auto_node)
34494 {
34495 TREE_TYPE (decl)
34496 = do_auto_deduction (TREE_TYPE (decl), init,
34497 auto_node);
34498
34499 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34500 && !type_dependent_expression_p (decl))
34501 goto non_class;
34502 }
34503
34504 cp_finish_decl (decl, init, !is_non_constant_init,
34505 asm_specification,
34506 LOOKUP_ONLYCONVERTING);
34507 orig_init = init;
34508 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34509 {
34510 vec_safe_push (for_block, this_pre_body);
34511 init = NULL_TREE;
34512 }
34513 else
34514 {
34515 init = pop_stmt_list (this_pre_body);
34516 if (init && TREE_CODE (init) == STATEMENT_LIST)
34517 {
34518 tree_stmt_iterator i = tsi_start (init);
34519 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34520 while (!tsi_end_p (i))
34521 {
34522 tree t = tsi_stmt (i);
34523 if (TREE_CODE (t) == DECL_EXPR
34524 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34525 {
34526 tsi_delink (&i);
34527 vec_safe_push (for_block, t);
34528 continue;
34529 }
34530 break;
34531 }
34532 if (tsi_one_before_end_p (i))
34533 {
34534 tree t = tsi_stmt (i);
34535 tsi_delink (&i);
34536 free_stmt_list (init);
34537 init = t;
34538 }
34539 }
34540 }
34541 this_pre_body = NULL_TREE;
34542 }
34543 else
34544 {
34545 /* Consume '='. */
34546 cp_lexer_consume_token (parser->lexer);
34547 init = cp_parser_assignment_expression (parser);
34548
34549 non_class:
34550 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34551 init = error_mark_node;
34552 else
34553 cp_finish_decl (decl, NULL_TREE,
34554 /*init_const_expr_p=*/false,
34555 asm_specification,
34556 LOOKUP_ONLYCONVERTING);
34557 }
34558
34559 if (pushed_scope)
34560 pop_scope (pushed_scope);
34561 }
34562 }
34563 else
34564 {
34565 cp_id_kind idk;
34566 /* If parsing a type specifier sequence failed, then
34567 this MUST be a simple expression. */
34568 if (code == CILK_FOR)
34569 error ("%<_Cilk_for%> allows expression instead of declaration only "
34570 "in C, not in C++");
34571 cp_parser_parse_tentatively (parser);
34572 decl = cp_parser_primary_expression (parser, false, false,
34573 false, &idk);
34574 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34575 if (!cp_parser_error_occurred (parser)
34576 && decl
34577 && (TREE_CODE (decl) == COMPONENT_REF
34578 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34579 {
34580 cp_parser_abort_tentative_parse (parser);
34581 cp_parser_parse_tentatively (parser);
34582 cp_token *token = cp_lexer_peek_token (parser->lexer);
34583 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34584 /*check_dependency_p=*/true,
34585 /*template_p=*/NULL,
34586 /*declarator_p=*/false,
34587 /*optional_p=*/false);
34588 if (name != error_mark_node
34589 && last_tok == cp_lexer_peek_token (parser->lexer))
34590 {
34591 decl = cp_parser_lookup_name_simple (parser, name,
34592 token->location);
34593 if (TREE_CODE (decl) == FIELD_DECL)
34594 add_private_clause = omp_privatize_field (decl, false);
34595 }
34596 cp_parser_abort_tentative_parse (parser);
34597 cp_parser_parse_tentatively (parser);
34598 decl = cp_parser_primary_expression (parser, false, false,
34599 false, &idk);
34600 }
34601 if (!cp_parser_error_occurred (parser)
34602 && decl
34603 && DECL_P (decl)
34604 && CLASS_TYPE_P (TREE_TYPE (decl)))
34605 {
34606 tree rhs;
34607
34608 cp_parser_parse_definitely (parser);
34609 cp_parser_require (parser, CPP_EQ, RT_EQ);
34610 rhs = cp_parser_assignment_expression (parser);
34611 orig_init = rhs;
34612 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34613 decl, NOP_EXPR,
34614 rhs,
34615 tf_warning_or_error));
34616 if (!add_private_clause)
34617 add_private_clause = decl;
34618 }
34619 else
34620 {
34621 decl = NULL;
34622 cp_parser_abort_tentative_parse (parser);
34623 init = cp_parser_expression (parser);
34624 if (init)
34625 {
34626 if (TREE_CODE (init) == MODIFY_EXPR
34627 || TREE_CODE (init) == MODOP_EXPR)
34628 real_decl = TREE_OPERAND (init, 0);
34629 }
34630 }
34631 }
34632 return add_private_clause;
34633 }
34634
34635 /* Parse the restricted form of the for statement allowed by OpenMP. */
34636
34637 static tree
34638 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34639 tree *cclauses, bool *if_p)
34640 {
34641 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34642 tree real_decl, initv, condv, incrv, declv;
34643 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34644 location_t loc_first;
34645 bool collapse_err = false;
34646 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34647 vec<tree, va_gc> *for_block = make_tree_vector ();
34648 auto_vec<tree, 4> orig_inits;
34649 bool tiling = false;
34650
34651 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34652 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34653 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34654 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34655 {
34656 tiling = true;
34657 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34658 }
34659 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34660 && OMP_CLAUSE_ORDERED_EXPR (cl))
34661 {
34662 ordered_cl = cl;
34663 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34664 }
34665
34666 if (ordered && ordered < collapse)
34667 {
34668 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34669 "%<ordered%> clause parameter is less than %<collapse%>");
34670 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34671 = build_int_cst (NULL_TREE, collapse);
34672 ordered = collapse;
34673 }
34674 if (ordered)
34675 {
34676 for (tree *pc = &clauses; *pc; )
34677 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34678 {
34679 error_at (OMP_CLAUSE_LOCATION (*pc),
34680 "%<linear%> clause may not be specified together "
34681 "with %<ordered%> clause with a parameter");
34682 *pc = OMP_CLAUSE_CHAIN (*pc);
34683 }
34684 else
34685 pc = &OMP_CLAUSE_CHAIN (*pc);
34686 }
34687
34688 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34689 count = ordered ? ordered : collapse;
34690
34691 declv = make_tree_vec (count);
34692 initv = make_tree_vec (count);
34693 condv = make_tree_vec (count);
34694 incrv = make_tree_vec (count);
34695
34696 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34697
34698 for (i = 0; i < count; i++)
34699 {
34700 int bracecount = 0;
34701 tree add_private_clause = NULL_TREE;
34702 location_t loc;
34703
34704 if (code != CILK_FOR
34705 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34706 {
34707 if (!collapse_err)
34708 cp_parser_error (parser, "for statement expected");
34709 return NULL;
34710 }
34711 if (code == CILK_FOR
34712 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34713 {
34714 if (!collapse_err)
34715 cp_parser_error (parser, "_Cilk_for statement expected");
34716 return NULL;
34717 }
34718 loc = cp_lexer_consume_token (parser->lexer)->location;
34719
34720 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34721 return NULL;
34722
34723 init = orig_init = decl = real_decl = NULL;
34724 this_pre_body = push_stmt_list ();
34725
34726 add_private_clause
34727 = cp_parser_omp_for_loop_init (parser, code,
34728 this_pre_body, for_block,
34729 init, orig_init, decl, real_decl);
34730
34731 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34732 if (this_pre_body)
34733 {
34734 this_pre_body = pop_stmt_list (this_pre_body);
34735 if (pre_body)
34736 {
34737 tree t = pre_body;
34738 pre_body = push_stmt_list ();
34739 add_stmt (t);
34740 add_stmt (this_pre_body);
34741 pre_body = pop_stmt_list (pre_body);
34742 }
34743 else
34744 pre_body = this_pre_body;
34745 }
34746
34747 if (decl)
34748 real_decl = decl;
34749 if (cclauses != NULL
34750 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34751 && real_decl != NULL_TREE)
34752 {
34753 tree *c;
34754 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34755 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34756 && OMP_CLAUSE_DECL (*c) == real_decl)
34757 {
34758 error_at (loc, "iteration variable %qD"
34759 " should not be firstprivate", real_decl);
34760 *c = OMP_CLAUSE_CHAIN (*c);
34761 }
34762 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34763 && OMP_CLAUSE_DECL (*c) == real_decl)
34764 {
34765 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34766 tree l = *c;
34767 *c = OMP_CLAUSE_CHAIN (*c);
34768 if (code == OMP_SIMD)
34769 {
34770 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34771 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34772 }
34773 else
34774 {
34775 OMP_CLAUSE_CHAIN (l) = clauses;
34776 clauses = l;
34777 }
34778 add_private_clause = NULL_TREE;
34779 }
34780 else
34781 {
34782 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34783 && OMP_CLAUSE_DECL (*c) == real_decl)
34784 add_private_clause = NULL_TREE;
34785 c = &OMP_CLAUSE_CHAIN (*c);
34786 }
34787 }
34788
34789 if (add_private_clause)
34790 {
34791 tree c;
34792 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34793 {
34794 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34795 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34796 && OMP_CLAUSE_DECL (c) == decl)
34797 break;
34798 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34799 && OMP_CLAUSE_DECL (c) == decl)
34800 error_at (loc, "iteration variable %qD "
34801 "should not be firstprivate",
34802 decl);
34803 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34804 && OMP_CLAUSE_DECL (c) == decl)
34805 error_at (loc, "iteration variable %qD should not be reduction",
34806 decl);
34807 }
34808 if (c == NULL)
34809 {
34810 if (code != OMP_SIMD)
34811 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34812 else if (collapse == 1)
34813 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34814 else
34815 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34816 OMP_CLAUSE_DECL (c) = add_private_clause;
34817 c = finish_omp_clauses (c, C_ORT_OMP);
34818 if (c)
34819 {
34820 OMP_CLAUSE_CHAIN (c) = clauses;
34821 clauses = c;
34822 /* For linear, signal that we need to fill up
34823 the so far unknown linear step. */
34824 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34825 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34826 }
34827 }
34828 }
34829
34830 cond = NULL;
34831 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34832 cond = cp_parser_omp_for_cond (parser, decl, code);
34833 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34834
34835 incr = NULL;
34836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34837 {
34838 /* If decl is an iterator, preserve the operator on decl
34839 until finish_omp_for. */
34840 if (real_decl
34841 && ((processing_template_decl
34842 && (TREE_TYPE (real_decl) == NULL_TREE
34843 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34844 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34845 incr = cp_parser_omp_for_incr (parser, real_decl);
34846 else
34847 incr = cp_parser_expression (parser);
34848 if (!EXPR_HAS_LOCATION (incr))
34849 protected_set_expr_location (incr, input_location);
34850 }
34851
34852 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34853 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34854 /*or_comma=*/false,
34855 /*consume_paren=*/true);
34856
34857 TREE_VEC_ELT (declv, i) = decl;
34858 TREE_VEC_ELT (initv, i) = init;
34859 TREE_VEC_ELT (condv, i) = cond;
34860 TREE_VEC_ELT (incrv, i) = incr;
34861 if (orig_init)
34862 {
34863 orig_inits.safe_grow_cleared (i + 1);
34864 orig_inits[i] = orig_init;
34865 }
34866
34867 if (i == count - 1)
34868 break;
34869
34870 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34871 in between the collapsed for loops to be still considered perfectly
34872 nested. Hopefully the final version clarifies this.
34873 For now handle (multiple) {'s and empty statements. */
34874 cp_parser_parse_tentatively (parser);
34875 for (;;)
34876 {
34877 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34878 break;
34879 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34880 {
34881 cp_lexer_consume_token (parser->lexer);
34882 bracecount++;
34883 }
34884 else if (bracecount
34885 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34886 cp_lexer_consume_token (parser->lexer);
34887 else
34888 {
34889 loc = cp_lexer_peek_token (parser->lexer)->location;
34890 error_at (loc, "not enough for loops to collapse");
34891 collapse_err = true;
34892 cp_parser_abort_tentative_parse (parser);
34893 declv = NULL_TREE;
34894 break;
34895 }
34896 }
34897
34898 if (declv)
34899 {
34900 cp_parser_parse_definitely (parser);
34901 nbraces += bracecount;
34902 }
34903 }
34904
34905 if (nbraces)
34906 if_p = NULL;
34907
34908 /* Note that we saved the original contents of this flag when we entered
34909 the structured block, and so we don't need to re-save it here. */
34910 if (code == CILK_SIMD || code == CILK_FOR)
34911 parser->in_statement = IN_CILK_SIMD_FOR;
34912 else
34913 parser->in_statement = IN_OMP_FOR;
34914
34915 /* Note that the grammar doesn't call for a structured block here,
34916 though the loop as a whole is a structured block. */
34917 body = push_stmt_list ();
34918 cp_parser_statement (parser, NULL_TREE, false, if_p);
34919 body = pop_stmt_list (body);
34920
34921 if (declv == NULL_TREE)
34922 ret = NULL_TREE;
34923 else
34924 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34925 body, pre_body, &orig_inits, clauses);
34926
34927 while (nbraces)
34928 {
34929 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34930 {
34931 cp_lexer_consume_token (parser->lexer);
34932 nbraces--;
34933 }
34934 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34935 cp_lexer_consume_token (parser->lexer);
34936 else
34937 {
34938 if (!collapse_err)
34939 {
34940 error_at (cp_lexer_peek_token (parser->lexer)->location,
34941 "collapsed loops not perfectly nested");
34942 }
34943 collapse_err = true;
34944 cp_parser_statement_seq_opt (parser, NULL);
34945 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34946 break;
34947 }
34948 }
34949
34950 while (!for_block->is_empty ())
34951 {
34952 tree t = for_block->pop ();
34953 if (TREE_CODE (t) == STATEMENT_LIST)
34954 add_stmt (pop_stmt_list (t));
34955 else
34956 add_stmt (t);
34957 }
34958 release_tree_vector (for_block);
34959
34960 return ret;
34961 }
34962
34963 /* Helper function for OpenMP parsing, split clauses and call
34964 finish_omp_clauses on each of the set of clauses afterwards. */
34965
34966 static void
34967 cp_omp_split_clauses (location_t loc, enum tree_code code,
34968 omp_clause_mask mask, tree clauses, tree *cclauses)
34969 {
34970 int i;
34971 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34972 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34973 if (cclauses[i])
34974 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34975 }
34976
34977 /* OpenMP 4.0:
34978 #pragma omp simd simd-clause[optseq] new-line
34979 for-loop */
34980
34981 #define OMP_SIMD_CLAUSE_MASK \
34982 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34990
34991 static tree
34992 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34993 char *p_name, omp_clause_mask mask, tree *cclauses,
34994 bool *if_p)
34995 {
34996 tree clauses, sb, ret;
34997 unsigned int save;
34998 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34999
35000 strcat (p_name, " simd");
35001 mask |= OMP_SIMD_CLAUSE_MASK;
35002
35003 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35004 cclauses == NULL);
35005 if (cclauses)
35006 {
35007 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35008 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35009 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35010 OMP_CLAUSE_ORDERED);
35011 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35012 {
35013 error_at (OMP_CLAUSE_LOCATION (c),
35014 "%<ordered%> clause with parameter may not be specified "
35015 "on %qs construct", p_name);
35016 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35017 }
35018 }
35019
35020 sb = begin_omp_structured_block ();
35021 save = cp_parser_begin_omp_structured_block (parser);
35022
35023 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35024
35025 cp_parser_end_omp_structured_block (parser, save);
35026 add_stmt (finish_omp_structured_block (sb));
35027
35028 return ret;
35029 }
35030
35031 /* OpenMP 2.5:
35032 #pragma omp for for-clause[optseq] new-line
35033 for-loop
35034
35035 OpenMP 4.0:
35036 #pragma omp for simd for-simd-clause[optseq] new-line
35037 for-loop */
35038
35039 #define OMP_FOR_CLAUSE_MASK \
35040 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35049
35050 static tree
35051 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35052 char *p_name, omp_clause_mask mask, tree *cclauses,
35053 bool *if_p)
35054 {
35055 tree clauses, sb, ret;
35056 unsigned int save;
35057 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35058
35059 strcat (p_name, " for");
35060 mask |= OMP_FOR_CLAUSE_MASK;
35061 /* parallel for{, simd} disallows nowait clause, but for
35062 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35063 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35064 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35065 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35066 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35067 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35068
35069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35070 {
35071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35072 const char *p = IDENTIFIER_POINTER (id);
35073
35074 if (strcmp (p, "simd") == 0)
35075 {
35076 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35077 if (cclauses == NULL)
35078 cclauses = cclauses_buf;
35079
35080 cp_lexer_consume_token (parser->lexer);
35081 if (!flag_openmp) /* flag_openmp_simd */
35082 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35083 cclauses, if_p);
35084 sb = begin_omp_structured_block ();
35085 save = cp_parser_begin_omp_structured_block (parser);
35086 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35087 cclauses, if_p);
35088 cp_parser_end_omp_structured_block (parser, save);
35089 tree body = finish_omp_structured_block (sb);
35090 if (ret == NULL)
35091 return ret;
35092 ret = make_node (OMP_FOR);
35093 TREE_TYPE (ret) = void_type_node;
35094 OMP_FOR_BODY (ret) = body;
35095 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35096 SET_EXPR_LOCATION (ret, loc);
35097 add_stmt (ret);
35098 return ret;
35099 }
35100 }
35101 if (!flag_openmp) /* flag_openmp_simd */
35102 {
35103 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35104 return NULL_TREE;
35105 }
35106
35107 /* Composite distribute parallel for disallows linear clause. */
35108 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35109 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35110
35111 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35112 cclauses == NULL);
35113 if (cclauses)
35114 {
35115 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35116 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35117 }
35118
35119 sb = begin_omp_structured_block ();
35120 save = cp_parser_begin_omp_structured_block (parser);
35121
35122 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35123
35124 cp_parser_end_omp_structured_block (parser, save);
35125 add_stmt (finish_omp_structured_block (sb));
35126
35127 return ret;
35128 }
35129
35130 /* OpenMP 2.5:
35131 # pragma omp master new-line
35132 structured-block */
35133
35134 static tree
35135 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35136 {
35137 cp_parser_require_pragma_eol (parser, pragma_tok);
35138 return c_finish_omp_master (input_location,
35139 cp_parser_omp_structured_block (parser, if_p));
35140 }
35141
35142 /* OpenMP 2.5:
35143 # pragma omp ordered new-line
35144 structured-block
35145
35146 OpenMP 4.5:
35147 # pragma omp ordered ordered-clauses new-line
35148 structured-block */
35149
35150 #define OMP_ORDERED_CLAUSE_MASK \
35151 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35153
35154 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35155 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35156
35157 static bool
35158 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35159 enum pragma_context context, bool *if_p)
35160 {
35161 location_t loc = pragma_tok->location;
35162
35163 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35164 {
35165 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35166 const char *p = IDENTIFIER_POINTER (id);
35167
35168 if (strcmp (p, "depend") == 0)
35169 {
35170 if (context == pragma_stmt)
35171 {
35172 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35173 "%<depend%> clause may only be used in compound "
35174 "statements");
35175 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35176 return false;
35177 }
35178 tree clauses
35179 = cp_parser_omp_all_clauses (parser,
35180 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35181 "#pragma omp ordered", pragma_tok);
35182 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35183 return false;
35184 }
35185 }
35186
35187 tree clauses
35188 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35189 "#pragma omp ordered", pragma_tok);
35190 c_finish_omp_ordered (loc, clauses,
35191 cp_parser_omp_structured_block (parser, if_p));
35192 return true;
35193 }
35194
35195 /* OpenMP 2.5:
35196
35197 section-scope:
35198 { section-sequence }
35199
35200 section-sequence:
35201 section-directive[opt] structured-block
35202 section-sequence section-directive structured-block */
35203
35204 static tree
35205 cp_parser_omp_sections_scope (cp_parser *parser)
35206 {
35207 tree stmt, substmt;
35208 bool error_suppress = false;
35209 cp_token *tok;
35210
35211 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
35212 return NULL_TREE;
35213
35214 stmt = push_stmt_list ();
35215
35216 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35217 != PRAGMA_OMP_SECTION)
35218 {
35219 substmt = cp_parser_omp_structured_block (parser, NULL);
35220 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35221 add_stmt (substmt);
35222 }
35223
35224 while (1)
35225 {
35226 tok = cp_lexer_peek_token (parser->lexer);
35227 if (tok->type == CPP_CLOSE_BRACE)
35228 break;
35229 if (tok->type == CPP_EOF)
35230 break;
35231
35232 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35233 {
35234 cp_lexer_consume_token (parser->lexer);
35235 cp_parser_require_pragma_eol (parser, tok);
35236 error_suppress = false;
35237 }
35238 else if (!error_suppress)
35239 {
35240 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35241 error_suppress = true;
35242 }
35243
35244 substmt = cp_parser_omp_structured_block (parser, NULL);
35245 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35246 add_stmt (substmt);
35247 }
35248 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35249
35250 substmt = pop_stmt_list (stmt);
35251
35252 stmt = make_node (OMP_SECTIONS);
35253 TREE_TYPE (stmt) = void_type_node;
35254 OMP_SECTIONS_BODY (stmt) = substmt;
35255
35256 add_stmt (stmt);
35257 return stmt;
35258 }
35259
35260 /* OpenMP 2.5:
35261 # pragma omp sections sections-clause[optseq] newline
35262 sections-scope */
35263
35264 #define OMP_SECTIONS_CLAUSE_MASK \
35265 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35270
35271 static tree
35272 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35273 char *p_name, omp_clause_mask mask, tree *cclauses)
35274 {
35275 tree clauses, ret;
35276 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35277
35278 strcat (p_name, " sections");
35279 mask |= OMP_SECTIONS_CLAUSE_MASK;
35280 if (cclauses)
35281 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35282
35283 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35284 cclauses == NULL);
35285 if (cclauses)
35286 {
35287 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35288 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35289 }
35290
35291 ret = cp_parser_omp_sections_scope (parser);
35292 if (ret)
35293 OMP_SECTIONS_CLAUSES (ret) = clauses;
35294
35295 return ret;
35296 }
35297
35298 /* OpenMP 2.5:
35299 # pragma omp parallel parallel-clause[optseq] new-line
35300 structured-block
35301 # pragma omp parallel for parallel-for-clause[optseq] new-line
35302 structured-block
35303 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35304 structured-block
35305
35306 OpenMP 4.0:
35307 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35308 structured-block */
35309
35310 #define OMP_PARALLEL_CLAUSE_MASK \
35311 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35320
35321 static tree
35322 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35323 char *p_name, omp_clause_mask mask, tree *cclauses,
35324 bool *if_p)
35325 {
35326 tree stmt, clauses, block;
35327 unsigned int save;
35328 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35329
35330 strcat (p_name, " parallel");
35331 mask |= OMP_PARALLEL_CLAUSE_MASK;
35332 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35333 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35334 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35335 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35336
35337 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35338 {
35339 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35340 if (cclauses == NULL)
35341 cclauses = cclauses_buf;
35342
35343 cp_lexer_consume_token (parser->lexer);
35344 if (!flag_openmp) /* flag_openmp_simd */
35345 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35346 if_p);
35347 block = begin_omp_parallel ();
35348 save = cp_parser_begin_omp_structured_block (parser);
35349 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35350 if_p);
35351 cp_parser_end_omp_structured_block (parser, save);
35352 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35353 block);
35354 if (ret == NULL_TREE)
35355 return ret;
35356 OMP_PARALLEL_COMBINED (stmt) = 1;
35357 return stmt;
35358 }
35359 /* When combined with distribute, parallel has to be followed by for.
35360 #pragma omp target parallel is allowed though. */
35361 else if (cclauses
35362 && (mask & (OMP_CLAUSE_MASK_1
35363 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35364 {
35365 error_at (loc, "expected %<for%> after %qs", p_name);
35366 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35367 return NULL_TREE;
35368 }
35369 else if (!flag_openmp) /* flag_openmp_simd */
35370 {
35371 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35372 return NULL_TREE;
35373 }
35374 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35375 {
35376 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35377 const char *p = IDENTIFIER_POINTER (id);
35378 if (strcmp (p, "sections") == 0)
35379 {
35380 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35381 cclauses = cclauses_buf;
35382
35383 cp_lexer_consume_token (parser->lexer);
35384 block = begin_omp_parallel ();
35385 save = cp_parser_begin_omp_structured_block (parser);
35386 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35387 cp_parser_end_omp_structured_block (parser, save);
35388 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35389 block);
35390 OMP_PARALLEL_COMBINED (stmt) = 1;
35391 return stmt;
35392 }
35393 }
35394
35395 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35396 cclauses == NULL);
35397 if (cclauses)
35398 {
35399 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35400 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35401 }
35402
35403 block = begin_omp_parallel ();
35404 save = cp_parser_begin_omp_structured_block (parser);
35405 cp_parser_statement (parser, NULL_TREE, false, if_p);
35406 cp_parser_end_omp_structured_block (parser, save);
35407 stmt = finish_omp_parallel (clauses, block);
35408 return stmt;
35409 }
35410
35411 /* OpenMP 2.5:
35412 # pragma omp single single-clause[optseq] new-line
35413 structured-block */
35414
35415 #define OMP_SINGLE_CLAUSE_MASK \
35416 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35420
35421 static tree
35422 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35423 {
35424 tree stmt = make_node (OMP_SINGLE);
35425 TREE_TYPE (stmt) = void_type_node;
35426
35427 OMP_SINGLE_CLAUSES (stmt)
35428 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35429 "#pragma omp single", pragma_tok);
35430 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35431
35432 return add_stmt (stmt);
35433 }
35434
35435 /* OpenMP 3.0:
35436 # pragma omp task task-clause[optseq] new-line
35437 structured-block */
35438
35439 #define OMP_TASK_CLAUSE_MASK \
35440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35450
35451 static tree
35452 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35453 {
35454 tree clauses, block;
35455 unsigned int save;
35456
35457 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35458 "#pragma omp task", pragma_tok);
35459 block = begin_omp_task ();
35460 save = cp_parser_begin_omp_structured_block (parser);
35461 cp_parser_statement (parser, NULL_TREE, false, if_p);
35462 cp_parser_end_omp_structured_block (parser, save);
35463 return finish_omp_task (clauses, block);
35464 }
35465
35466 /* OpenMP 3.0:
35467 # pragma omp taskwait new-line */
35468
35469 static void
35470 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35471 {
35472 cp_parser_require_pragma_eol (parser, pragma_tok);
35473 finish_omp_taskwait ();
35474 }
35475
35476 /* OpenMP 3.1:
35477 # pragma omp taskyield new-line */
35478
35479 static void
35480 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35481 {
35482 cp_parser_require_pragma_eol (parser, pragma_tok);
35483 finish_omp_taskyield ();
35484 }
35485
35486 /* OpenMP 4.0:
35487 # pragma omp taskgroup new-line
35488 structured-block */
35489
35490 static tree
35491 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35492 {
35493 cp_parser_require_pragma_eol (parser, pragma_tok);
35494 return c_finish_omp_taskgroup (input_location,
35495 cp_parser_omp_structured_block (parser,
35496 if_p));
35497 }
35498
35499
35500 /* OpenMP 2.5:
35501 # pragma omp threadprivate (variable-list) */
35502
35503 static void
35504 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35505 {
35506 tree vars;
35507
35508 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35509 cp_parser_require_pragma_eol (parser, pragma_tok);
35510
35511 finish_omp_threadprivate (vars);
35512 }
35513
35514 /* OpenMP 4.0:
35515 # pragma omp cancel cancel-clause[optseq] new-line */
35516
35517 #define OMP_CANCEL_CLAUSE_MASK \
35518 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35523
35524 static void
35525 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35526 {
35527 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35528 "#pragma omp cancel", pragma_tok);
35529 finish_omp_cancel (clauses);
35530 }
35531
35532 /* OpenMP 4.0:
35533 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35534
35535 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35536 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35540
35541 static void
35542 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35543 enum pragma_context context)
35544 {
35545 tree clauses;
35546 bool point_seen = false;
35547
35548 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35549 {
35550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35551 const char *p = IDENTIFIER_POINTER (id);
35552
35553 if (strcmp (p, "point") == 0)
35554 {
35555 cp_lexer_consume_token (parser->lexer);
35556 point_seen = true;
35557 }
35558 }
35559 if (!point_seen)
35560 {
35561 cp_parser_error (parser, "expected %<point%>");
35562 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35563 return;
35564 }
35565
35566 if (context != pragma_compound)
35567 {
35568 if (context == pragma_stmt)
35569 error_at (pragma_tok->location,
35570 "%<#pragma %s%> may only be used in compound statements",
35571 "omp cancellation point");
35572 else
35573 cp_parser_error (parser, "expected declaration specifiers");
35574 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35575 return;
35576 }
35577
35578 clauses = cp_parser_omp_all_clauses (parser,
35579 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35580 "#pragma omp cancellation point",
35581 pragma_tok);
35582 finish_omp_cancellation_point (clauses);
35583 }
35584
35585 /* OpenMP 4.0:
35586 #pragma omp distribute distribute-clause[optseq] new-line
35587 for-loop */
35588
35589 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35590 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35595
35596 static tree
35597 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35598 char *p_name, omp_clause_mask mask, tree *cclauses,
35599 bool *if_p)
35600 {
35601 tree clauses, sb, ret;
35602 unsigned int save;
35603 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35604
35605 strcat (p_name, " distribute");
35606 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35607
35608 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35609 {
35610 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35611 const char *p = IDENTIFIER_POINTER (id);
35612 bool simd = false;
35613 bool parallel = false;
35614
35615 if (strcmp (p, "simd") == 0)
35616 simd = true;
35617 else
35618 parallel = strcmp (p, "parallel") == 0;
35619 if (parallel || simd)
35620 {
35621 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35622 if (cclauses == NULL)
35623 cclauses = cclauses_buf;
35624 cp_lexer_consume_token (parser->lexer);
35625 if (!flag_openmp) /* flag_openmp_simd */
35626 {
35627 if (simd)
35628 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35629 cclauses, if_p);
35630 else
35631 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35632 cclauses, if_p);
35633 }
35634 sb = begin_omp_structured_block ();
35635 save = cp_parser_begin_omp_structured_block (parser);
35636 if (simd)
35637 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35638 cclauses, if_p);
35639 else
35640 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35641 cclauses, if_p);
35642 cp_parser_end_omp_structured_block (parser, save);
35643 tree body = finish_omp_structured_block (sb);
35644 if (ret == NULL)
35645 return ret;
35646 ret = make_node (OMP_DISTRIBUTE);
35647 TREE_TYPE (ret) = void_type_node;
35648 OMP_FOR_BODY (ret) = body;
35649 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35650 SET_EXPR_LOCATION (ret, loc);
35651 add_stmt (ret);
35652 return ret;
35653 }
35654 }
35655 if (!flag_openmp) /* flag_openmp_simd */
35656 {
35657 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35658 return NULL_TREE;
35659 }
35660
35661 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35662 cclauses == NULL);
35663 if (cclauses)
35664 {
35665 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35666 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35667 }
35668
35669 sb = begin_omp_structured_block ();
35670 save = cp_parser_begin_omp_structured_block (parser);
35671
35672 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35673
35674 cp_parser_end_omp_structured_block (parser, save);
35675 add_stmt (finish_omp_structured_block (sb));
35676
35677 return ret;
35678 }
35679
35680 /* OpenMP 4.0:
35681 # pragma omp teams teams-clause[optseq] new-line
35682 structured-block */
35683
35684 #define OMP_TEAMS_CLAUSE_MASK \
35685 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35692
35693 static tree
35694 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35695 char *p_name, omp_clause_mask mask, tree *cclauses,
35696 bool *if_p)
35697 {
35698 tree clauses, sb, ret;
35699 unsigned int save;
35700 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35701
35702 strcat (p_name, " teams");
35703 mask |= OMP_TEAMS_CLAUSE_MASK;
35704
35705 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35706 {
35707 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35708 const char *p = IDENTIFIER_POINTER (id);
35709 if (strcmp (p, "distribute") == 0)
35710 {
35711 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35712 if (cclauses == NULL)
35713 cclauses = cclauses_buf;
35714
35715 cp_lexer_consume_token (parser->lexer);
35716 if (!flag_openmp) /* flag_openmp_simd */
35717 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35718 cclauses, if_p);
35719 sb = begin_omp_structured_block ();
35720 save = cp_parser_begin_omp_structured_block (parser);
35721 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35722 cclauses, if_p);
35723 cp_parser_end_omp_structured_block (parser, save);
35724 tree body = finish_omp_structured_block (sb);
35725 if (ret == NULL)
35726 return ret;
35727 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35728 ret = make_node (OMP_TEAMS);
35729 TREE_TYPE (ret) = void_type_node;
35730 OMP_TEAMS_CLAUSES (ret) = clauses;
35731 OMP_TEAMS_BODY (ret) = body;
35732 OMP_TEAMS_COMBINED (ret) = 1;
35733 SET_EXPR_LOCATION (ret, loc);
35734 return add_stmt (ret);
35735 }
35736 }
35737 if (!flag_openmp) /* flag_openmp_simd */
35738 {
35739 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35740 return NULL_TREE;
35741 }
35742
35743 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35744 cclauses == NULL);
35745 if (cclauses)
35746 {
35747 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35748 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35749 }
35750
35751 tree stmt = make_node (OMP_TEAMS);
35752 TREE_TYPE (stmt) = void_type_node;
35753 OMP_TEAMS_CLAUSES (stmt) = clauses;
35754 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35755 SET_EXPR_LOCATION (stmt, loc);
35756
35757 return add_stmt (stmt);
35758 }
35759
35760 /* OpenMP 4.0:
35761 # pragma omp target data target-data-clause[optseq] new-line
35762 structured-block */
35763
35764 #define OMP_TARGET_DATA_CLAUSE_MASK \
35765 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35769
35770 static tree
35771 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35772 {
35773 tree clauses
35774 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35775 "#pragma omp target data", pragma_tok);
35776 int map_seen = 0;
35777 for (tree *pc = &clauses; *pc;)
35778 {
35779 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35780 switch (OMP_CLAUSE_MAP_KIND (*pc))
35781 {
35782 case GOMP_MAP_TO:
35783 case GOMP_MAP_ALWAYS_TO:
35784 case GOMP_MAP_FROM:
35785 case GOMP_MAP_ALWAYS_FROM:
35786 case GOMP_MAP_TOFROM:
35787 case GOMP_MAP_ALWAYS_TOFROM:
35788 case GOMP_MAP_ALLOC:
35789 map_seen = 3;
35790 break;
35791 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35792 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35793 case GOMP_MAP_ALWAYS_POINTER:
35794 break;
35795 default:
35796 map_seen |= 1;
35797 error_at (OMP_CLAUSE_LOCATION (*pc),
35798 "%<#pragma omp target data%> with map-type other "
35799 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35800 "on %<map%> clause");
35801 *pc = OMP_CLAUSE_CHAIN (*pc);
35802 continue;
35803 }
35804 pc = &OMP_CLAUSE_CHAIN (*pc);
35805 }
35806
35807 if (map_seen != 3)
35808 {
35809 if (map_seen == 0)
35810 error_at (pragma_tok->location,
35811 "%<#pragma omp target data%> must contain at least "
35812 "one %<map%> clause");
35813 return NULL_TREE;
35814 }
35815
35816 tree stmt = make_node (OMP_TARGET_DATA);
35817 TREE_TYPE (stmt) = void_type_node;
35818 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35819
35820 keep_next_level (true);
35821 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35822
35823 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35824 return add_stmt (stmt);
35825 }
35826
35827 /* OpenMP 4.5:
35828 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35829 structured-block */
35830
35831 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35832 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35837
35838 static tree
35839 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35840 enum pragma_context context)
35841 {
35842 bool data_seen = false;
35843 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35844 {
35845 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35846 const char *p = IDENTIFIER_POINTER (id);
35847
35848 if (strcmp (p, "data") == 0)
35849 {
35850 cp_lexer_consume_token (parser->lexer);
35851 data_seen = true;
35852 }
35853 }
35854 if (!data_seen)
35855 {
35856 cp_parser_error (parser, "expected %<data%>");
35857 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35858 return NULL_TREE;
35859 }
35860
35861 if (context == pragma_stmt)
35862 {
35863 error_at (pragma_tok->location,
35864 "%<#pragma %s%> may only be used in compound statements",
35865 "omp target enter data");
35866 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35867 return NULL_TREE;
35868 }
35869
35870 tree clauses
35871 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35872 "#pragma omp target enter data", pragma_tok);
35873 int map_seen = 0;
35874 for (tree *pc = &clauses; *pc;)
35875 {
35876 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35877 switch (OMP_CLAUSE_MAP_KIND (*pc))
35878 {
35879 case GOMP_MAP_TO:
35880 case GOMP_MAP_ALWAYS_TO:
35881 case GOMP_MAP_ALLOC:
35882 map_seen = 3;
35883 break;
35884 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35885 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35886 case GOMP_MAP_ALWAYS_POINTER:
35887 break;
35888 default:
35889 map_seen |= 1;
35890 error_at (OMP_CLAUSE_LOCATION (*pc),
35891 "%<#pragma omp target enter data%> with map-type other "
35892 "than %<to%> or %<alloc%> on %<map%> clause");
35893 *pc = OMP_CLAUSE_CHAIN (*pc);
35894 continue;
35895 }
35896 pc = &OMP_CLAUSE_CHAIN (*pc);
35897 }
35898
35899 if (map_seen != 3)
35900 {
35901 if (map_seen == 0)
35902 error_at (pragma_tok->location,
35903 "%<#pragma omp target enter data%> must contain at least "
35904 "one %<map%> clause");
35905 return NULL_TREE;
35906 }
35907
35908 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35909 TREE_TYPE (stmt) = void_type_node;
35910 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35911 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35912 return add_stmt (stmt);
35913 }
35914
35915 /* OpenMP 4.5:
35916 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35917 structured-block */
35918
35919 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35925
35926 static tree
35927 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35928 enum pragma_context context)
35929 {
35930 bool data_seen = false;
35931 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35932 {
35933 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35934 const char *p = IDENTIFIER_POINTER (id);
35935
35936 if (strcmp (p, "data") == 0)
35937 {
35938 cp_lexer_consume_token (parser->lexer);
35939 data_seen = true;
35940 }
35941 }
35942 if (!data_seen)
35943 {
35944 cp_parser_error (parser, "expected %<data%>");
35945 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35946 return NULL_TREE;
35947 }
35948
35949 if (context == pragma_stmt)
35950 {
35951 error_at (pragma_tok->location,
35952 "%<#pragma %s%> may only be used in compound statements",
35953 "omp target exit data");
35954 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35955 return NULL_TREE;
35956 }
35957
35958 tree clauses
35959 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35960 "#pragma omp target exit data", pragma_tok);
35961 int map_seen = 0;
35962 for (tree *pc = &clauses; *pc;)
35963 {
35964 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35965 switch (OMP_CLAUSE_MAP_KIND (*pc))
35966 {
35967 case GOMP_MAP_FROM:
35968 case GOMP_MAP_ALWAYS_FROM:
35969 case GOMP_MAP_RELEASE:
35970 case GOMP_MAP_DELETE:
35971 map_seen = 3;
35972 break;
35973 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35974 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35975 case GOMP_MAP_ALWAYS_POINTER:
35976 break;
35977 default:
35978 map_seen |= 1;
35979 error_at (OMP_CLAUSE_LOCATION (*pc),
35980 "%<#pragma omp target exit data%> with map-type other "
35981 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35982 " clause");
35983 *pc = OMP_CLAUSE_CHAIN (*pc);
35984 continue;
35985 }
35986 pc = &OMP_CLAUSE_CHAIN (*pc);
35987 }
35988
35989 if (map_seen != 3)
35990 {
35991 if (map_seen == 0)
35992 error_at (pragma_tok->location,
35993 "%<#pragma omp target exit data%> must contain at least "
35994 "one %<map%> clause");
35995 return NULL_TREE;
35996 }
35997
35998 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35999 TREE_TYPE (stmt) = void_type_node;
36000 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36001 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36002 return add_stmt (stmt);
36003 }
36004
36005 /* OpenMP 4.0:
36006 # pragma omp target update target-update-clause[optseq] new-line */
36007
36008 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36009 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36015
36016 static bool
36017 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36018 enum pragma_context context)
36019 {
36020 if (context == pragma_stmt)
36021 {
36022 error_at (pragma_tok->location,
36023 "%<#pragma %s%> may only be used in compound statements",
36024 "omp target update");
36025 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36026 return false;
36027 }
36028
36029 tree clauses
36030 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36031 "#pragma omp target update", pragma_tok);
36032 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36033 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36034 {
36035 error_at (pragma_tok->location,
36036 "%<#pragma omp target update%> must contain at least one "
36037 "%<from%> or %<to%> clauses");
36038 return false;
36039 }
36040
36041 tree stmt = make_node (OMP_TARGET_UPDATE);
36042 TREE_TYPE (stmt) = void_type_node;
36043 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36044 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36045 add_stmt (stmt);
36046 return false;
36047 }
36048
36049 /* OpenMP 4.0:
36050 # pragma omp target target-clause[optseq] new-line
36051 structured-block */
36052
36053 #define OMP_TARGET_CLAUSE_MASK \
36054 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36063
36064 static bool
36065 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36066 enum pragma_context context, bool *if_p)
36067 {
36068 tree *pc = NULL, stmt;
36069
36070 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36071 {
36072 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36073 const char *p = IDENTIFIER_POINTER (id);
36074 enum tree_code ccode = ERROR_MARK;
36075
36076 if (strcmp (p, "teams") == 0)
36077 ccode = OMP_TEAMS;
36078 else if (strcmp (p, "parallel") == 0)
36079 ccode = OMP_PARALLEL;
36080 else if (strcmp (p, "simd") == 0)
36081 ccode = OMP_SIMD;
36082 if (ccode != ERROR_MARK)
36083 {
36084 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36085 char p_name[sizeof ("#pragma omp target teams distribute "
36086 "parallel for simd")];
36087
36088 cp_lexer_consume_token (parser->lexer);
36089 strcpy (p_name, "#pragma omp target");
36090 if (!flag_openmp) /* flag_openmp_simd */
36091 {
36092 tree stmt;
36093 switch (ccode)
36094 {
36095 case OMP_TEAMS:
36096 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36097 OMP_TARGET_CLAUSE_MASK,
36098 cclauses, if_p);
36099 break;
36100 case OMP_PARALLEL:
36101 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36102 OMP_TARGET_CLAUSE_MASK,
36103 cclauses, if_p);
36104 break;
36105 case OMP_SIMD:
36106 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36107 OMP_TARGET_CLAUSE_MASK,
36108 cclauses, if_p);
36109 break;
36110 default:
36111 gcc_unreachable ();
36112 }
36113 return stmt != NULL_TREE;
36114 }
36115 keep_next_level (true);
36116 tree sb = begin_omp_structured_block (), ret;
36117 unsigned save = cp_parser_begin_omp_structured_block (parser);
36118 switch (ccode)
36119 {
36120 case OMP_TEAMS:
36121 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36122 OMP_TARGET_CLAUSE_MASK, cclauses,
36123 if_p);
36124 break;
36125 case OMP_PARALLEL:
36126 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36127 OMP_TARGET_CLAUSE_MASK, cclauses,
36128 if_p);
36129 break;
36130 case OMP_SIMD:
36131 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36132 OMP_TARGET_CLAUSE_MASK, cclauses,
36133 if_p);
36134 break;
36135 default:
36136 gcc_unreachable ();
36137 }
36138 cp_parser_end_omp_structured_block (parser, save);
36139 tree body = finish_omp_structured_block (sb);
36140 if (ret == NULL_TREE)
36141 return false;
36142 if (ccode == OMP_TEAMS && !processing_template_decl)
36143 {
36144 /* For combined target teams, ensure the num_teams and
36145 thread_limit clause expressions are evaluated on the host,
36146 before entering the target construct. */
36147 tree c;
36148 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36149 c; c = OMP_CLAUSE_CHAIN (c))
36150 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36151 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36152 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36153 {
36154 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36155 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36156 if (expr == error_mark_node)
36157 continue;
36158 tree tmp = TARGET_EXPR_SLOT (expr);
36159 add_stmt (expr);
36160 OMP_CLAUSE_OPERAND (c, 0) = expr;
36161 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36162 OMP_CLAUSE_FIRSTPRIVATE);
36163 OMP_CLAUSE_DECL (tc) = tmp;
36164 OMP_CLAUSE_CHAIN (tc)
36165 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36166 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36167 }
36168 }
36169 tree stmt = make_node (OMP_TARGET);
36170 TREE_TYPE (stmt) = void_type_node;
36171 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36172 OMP_TARGET_BODY (stmt) = body;
36173 OMP_TARGET_COMBINED (stmt) = 1;
36174 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36175 add_stmt (stmt);
36176 pc = &OMP_TARGET_CLAUSES (stmt);
36177 goto check_clauses;
36178 }
36179 else if (!flag_openmp) /* flag_openmp_simd */
36180 {
36181 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36182 return false;
36183 }
36184 else if (strcmp (p, "data") == 0)
36185 {
36186 cp_lexer_consume_token (parser->lexer);
36187 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36188 return true;
36189 }
36190 else if (strcmp (p, "enter") == 0)
36191 {
36192 cp_lexer_consume_token (parser->lexer);
36193 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36194 return false;
36195 }
36196 else if (strcmp (p, "exit") == 0)
36197 {
36198 cp_lexer_consume_token (parser->lexer);
36199 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36200 return false;
36201 }
36202 else if (strcmp (p, "update") == 0)
36203 {
36204 cp_lexer_consume_token (parser->lexer);
36205 return cp_parser_omp_target_update (parser, pragma_tok, context);
36206 }
36207 }
36208 if (!flag_openmp) /* flag_openmp_simd */
36209 {
36210 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36211 return false;
36212 }
36213
36214 stmt = make_node (OMP_TARGET);
36215 TREE_TYPE (stmt) = void_type_node;
36216
36217 OMP_TARGET_CLAUSES (stmt)
36218 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36219 "#pragma omp target", pragma_tok);
36220 pc = &OMP_TARGET_CLAUSES (stmt);
36221 keep_next_level (true);
36222 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36223
36224 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36225 add_stmt (stmt);
36226
36227 check_clauses:
36228 while (*pc)
36229 {
36230 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36231 switch (OMP_CLAUSE_MAP_KIND (*pc))
36232 {
36233 case GOMP_MAP_TO:
36234 case GOMP_MAP_ALWAYS_TO:
36235 case GOMP_MAP_FROM:
36236 case GOMP_MAP_ALWAYS_FROM:
36237 case GOMP_MAP_TOFROM:
36238 case GOMP_MAP_ALWAYS_TOFROM:
36239 case GOMP_MAP_ALLOC:
36240 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36241 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36242 case GOMP_MAP_ALWAYS_POINTER:
36243 break;
36244 default:
36245 error_at (OMP_CLAUSE_LOCATION (*pc),
36246 "%<#pragma omp target%> with map-type other "
36247 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36248 "on %<map%> clause");
36249 *pc = OMP_CLAUSE_CHAIN (*pc);
36250 continue;
36251 }
36252 pc = &OMP_CLAUSE_CHAIN (*pc);
36253 }
36254 return true;
36255 }
36256
36257 /* OpenACC 2.0:
36258 # pragma acc cache (variable-list) new-line
36259 */
36260
36261 static tree
36262 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36263 {
36264 tree stmt, clauses;
36265
36266 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36267 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36268
36269 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36270
36271 stmt = make_node (OACC_CACHE);
36272 TREE_TYPE (stmt) = void_type_node;
36273 OACC_CACHE_CLAUSES (stmt) = clauses;
36274 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36275 add_stmt (stmt);
36276
36277 return stmt;
36278 }
36279
36280 /* OpenACC 2.0:
36281 # pragma acc data oacc-data-clause[optseq] new-line
36282 structured-block */
36283
36284 #define OACC_DATA_CLAUSE_MASK \
36285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36296
36297 static tree
36298 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36299 {
36300 tree stmt, clauses, block;
36301 unsigned int save;
36302
36303 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36304 "#pragma acc data", pragma_tok);
36305
36306 block = begin_omp_parallel ();
36307 save = cp_parser_begin_omp_structured_block (parser);
36308 cp_parser_statement (parser, NULL_TREE, false, if_p);
36309 cp_parser_end_omp_structured_block (parser, save);
36310 stmt = finish_oacc_data (clauses, block);
36311 return stmt;
36312 }
36313
36314 /* OpenACC 2.0:
36315 # pragma acc host_data <clauses> new-line
36316 structured-block */
36317
36318 #define OACC_HOST_DATA_CLAUSE_MASK \
36319 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36320
36321 static tree
36322 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36323 {
36324 tree stmt, clauses, block;
36325 unsigned int save;
36326
36327 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36328 "#pragma acc host_data", pragma_tok);
36329
36330 block = begin_omp_parallel ();
36331 save = cp_parser_begin_omp_structured_block (parser);
36332 cp_parser_statement (parser, NULL_TREE, false, if_p);
36333 cp_parser_end_omp_structured_block (parser, save);
36334 stmt = finish_oacc_host_data (clauses, block);
36335 return stmt;
36336 }
36337
36338 /* OpenACC 2.0:
36339 # pragma acc declare oacc-data-clause[optseq] new-line
36340 */
36341
36342 #define OACC_DECLARE_CLAUSE_MASK \
36343 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36355
36356 static tree
36357 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36358 {
36359 tree clauses, stmt;
36360 bool error = false;
36361
36362 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36363 "#pragma acc declare", pragma_tok, true);
36364
36365
36366 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36367 {
36368 error_at (pragma_tok->location,
36369 "no valid clauses specified in %<#pragma acc declare%>");
36370 return NULL_TREE;
36371 }
36372
36373 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36374 {
36375 location_t loc = OMP_CLAUSE_LOCATION (t);
36376 tree decl = OMP_CLAUSE_DECL (t);
36377 if (!DECL_P (decl))
36378 {
36379 error_at (loc, "array section in %<#pragma acc declare%>");
36380 error = true;
36381 continue;
36382 }
36383 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36384 switch (OMP_CLAUSE_MAP_KIND (t))
36385 {
36386 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36387 case GOMP_MAP_FORCE_ALLOC:
36388 case GOMP_MAP_FORCE_TO:
36389 case GOMP_MAP_FORCE_DEVICEPTR:
36390 case GOMP_MAP_DEVICE_RESIDENT:
36391 break;
36392
36393 case GOMP_MAP_LINK:
36394 if (!global_bindings_p ()
36395 && (TREE_STATIC (decl)
36396 || !DECL_EXTERNAL (decl)))
36397 {
36398 error_at (loc,
36399 "%qD must be a global variable in "
36400 "%<#pragma acc declare link%>",
36401 decl);
36402 error = true;
36403 continue;
36404 }
36405 break;
36406
36407 default:
36408 if (global_bindings_p ())
36409 {
36410 error_at (loc, "invalid OpenACC clause at file scope");
36411 error = true;
36412 continue;
36413 }
36414 if (DECL_EXTERNAL (decl))
36415 {
36416 error_at (loc,
36417 "invalid use of %<extern%> variable %qD "
36418 "in %<#pragma acc declare%>", decl);
36419 error = true;
36420 continue;
36421 }
36422 else if (TREE_PUBLIC (decl))
36423 {
36424 error_at (loc,
36425 "invalid use of %<global%> variable %qD "
36426 "in %<#pragma acc declare%>", decl);
36427 error = true;
36428 continue;
36429 }
36430 break;
36431 }
36432
36433 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36434 || lookup_attribute ("omp declare target link",
36435 DECL_ATTRIBUTES (decl)))
36436 {
36437 error_at (loc, "variable %qD used more than once with "
36438 "%<#pragma acc declare%>", decl);
36439 error = true;
36440 continue;
36441 }
36442
36443 if (!error)
36444 {
36445 tree id;
36446
36447 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36448 id = get_identifier ("omp declare target link");
36449 else
36450 id = get_identifier ("omp declare target");
36451
36452 DECL_ATTRIBUTES (decl)
36453 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36454 if (global_bindings_p ())
36455 {
36456 symtab_node *node = symtab_node::get (decl);
36457 if (node != NULL)
36458 {
36459 node->offloadable = 1;
36460 if (ENABLE_OFFLOADING)
36461 {
36462 g->have_offload = true;
36463 if (is_a <varpool_node *> (node))
36464 vec_safe_push (offload_vars, decl);
36465 }
36466 }
36467 }
36468 }
36469 }
36470
36471 if (error || global_bindings_p ())
36472 return NULL_TREE;
36473
36474 stmt = make_node (OACC_DECLARE);
36475 TREE_TYPE (stmt) = void_type_node;
36476 OACC_DECLARE_CLAUSES (stmt) = clauses;
36477 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36478
36479 add_stmt (stmt);
36480
36481 return NULL_TREE;
36482 }
36483
36484 /* OpenACC 2.0:
36485 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36486
36487 or
36488
36489 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36490
36491 LOC is the location of the #pragma token.
36492 */
36493
36494 #define OACC_ENTER_DATA_CLAUSE_MASK \
36495 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36502
36503 #define OACC_EXIT_DATA_CLAUSE_MASK \
36504 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36509
36510 static tree
36511 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36512 bool enter)
36513 {
36514 location_t loc = pragma_tok->location;
36515 tree stmt, clauses;
36516 const char *p = "";
36517
36518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36519 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36520
36521 if (strcmp (p, "data") != 0)
36522 {
36523 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36524 enter ? "enter" : "exit");
36525 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36526 return NULL_TREE;
36527 }
36528
36529 cp_lexer_consume_token (parser->lexer);
36530
36531 if (enter)
36532 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36533 "#pragma acc enter data", pragma_tok);
36534 else
36535 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36536 "#pragma acc exit data", pragma_tok);
36537
36538 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36539 {
36540 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36541 enter ? "enter" : "exit");
36542 return NULL_TREE;
36543 }
36544
36545 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36546 TREE_TYPE (stmt) = void_type_node;
36547 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36548 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36549 add_stmt (stmt);
36550 return stmt;
36551 }
36552
36553 /* OpenACC 2.0:
36554 # pragma acc loop oacc-loop-clause[optseq] new-line
36555 structured-block */
36556
36557 #define OACC_LOOP_CLAUSE_MASK \
36558 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36568
36569 static tree
36570 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36571 omp_clause_mask mask, tree *cclauses, bool *if_p)
36572 {
36573 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36574
36575 strcat (p_name, " loop");
36576 mask |= OACC_LOOP_CLAUSE_MASK;
36577
36578 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36579 cclauses == NULL);
36580 if (cclauses)
36581 {
36582 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36583 if (*cclauses)
36584 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36585 if (clauses)
36586 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36587 }
36588
36589 tree block = begin_omp_structured_block ();
36590 int save = cp_parser_begin_omp_structured_block (parser);
36591 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36592 cp_parser_end_omp_structured_block (parser, save);
36593 add_stmt (finish_omp_structured_block (block));
36594
36595 return stmt;
36596 }
36597
36598 /* OpenACC 2.0:
36599 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36600 structured-block
36601
36602 or
36603
36604 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36605 structured-block
36606 */
36607
36608 #define OACC_KERNELS_CLAUSE_MASK \
36609 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36626
36627 #define OACC_PARALLEL_CLAUSE_MASK \
36628 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36648
36649 static tree
36650 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36651 char *p_name, bool *if_p)
36652 {
36653 omp_clause_mask mask;
36654 enum tree_code code;
36655 switch (cp_parser_pragma_kind (pragma_tok))
36656 {
36657 case PRAGMA_OACC_KERNELS:
36658 strcat (p_name, " kernels");
36659 mask = OACC_KERNELS_CLAUSE_MASK;
36660 code = OACC_KERNELS;
36661 break;
36662 case PRAGMA_OACC_PARALLEL:
36663 strcat (p_name, " parallel");
36664 mask = OACC_PARALLEL_CLAUSE_MASK;
36665 code = OACC_PARALLEL;
36666 break;
36667 default:
36668 gcc_unreachable ();
36669 }
36670
36671 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36672 {
36673 const char *p
36674 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36675 if (strcmp (p, "loop") == 0)
36676 {
36677 cp_lexer_consume_token (parser->lexer);
36678 tree block = begin_omp_parallel ();
36679 tree clauses;
36680 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36681 if_p);
36682 return finish_omp_construct (code, block, clauses);
36683 }
36684 }
36685
36686 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36687
36688 tree block = begin_omp_parallel ();
36689 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36690 cp_parser_statement (parser, NULL_TREE, false, if_p);
36691 cp_parser_end_omp_structured_block (parser, save);
36692 return finish_omp_construct (code, block, clauses);
36693 }
36694
36695 /* OpenACC 2.0:
36696 # pragma acc update oacc-update-clause[optseq] new-line
36697 */
36698
36699 #define OACC_UPDATE_CLAUSE_MASK \
36700 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36706
36707 static tree
36708 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36709 {
36710 tree stmt, clauses;
36711
36712 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36713 "#pragma acc update", pragma_tok);
36714
36715 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36716 {
36717 error_at (pragma_tok->location,
36718 "%<#pragma acc update%> must contain at least one "
36719 "%<device%> or %<host%> or %<self%> clause");
36720 return NULL_TREE;
36721 }
36722
36723 stmt = make_node (OACC_UPDATE);
36724 TREE_TYPE (stmt) = void_type_node;
36725 OACC_UPDATE_CLAUSES (stmt) = clauses;
36726 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36727 add_stmt (stmt);
36728 return stmt;
36729 }
36730
36731 /* OpenACC 2.0:
36732 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36733
36734 LOC is the location of the #pragma token.
36735 */
36736
36737 #define OACC_WAIT_CLAUSE_MASK \
36738 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36739
36740 static tree
36741 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36742 {
36743 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36744 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36745
36746 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36747 list = cp_parser_oacc_wait_list (parser, loc, list);
36748
36749 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36750 "#pragma acc wait", pragma_tok);
36751
36752 stmt = c_finish_oacc_wait (loc, list, clauses);
36753 stmt = finish_expr_stmt (stmt);
36754
36755 return stmt;
36756 }
36757
36758 /* OpenMP 4.0:
36759 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36760
36761 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36762 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36768
36769 static void
36770 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36771 enum pragma_context context)
36772 {
36773 bool first_p = parser->omp_declare_simd == NULL;
36774 cp_omp_declare_simd_data data;
36775 if (first_p)
36776 {
36777 data.error_seen = false;
36778 data.fndecl_seen = false;
36779 data.tokens = vNULL;
36780 data.clauses = NULL_TREE;
36781 /* It is safe to take the address of a local variable; it will only be
36782 used while this scope is live. */
36783 parser->omp_declare_simd = &data;
36784 }
36785
36786 /* Store away all pragma tokens. */
36787 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36788 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36789 cp_lexer_consume_token (parser->lexer);
36790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36791 parser->omp_declare_simd->error_seen = true;
36792 cp_parser_require_pragma_eol (parser, pragma_tok);
36793 struct cp_token_cache *cp
36794 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36795 parser->omp_declare_simd->tokens.safe_push (cp);
36796
36797 if (first_p)
36798 {
36799 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36800 cp_parser_pragma (parser, context, NULL);
36801 switch (context)
36802 {
36803 case pragma_external:
36804 cp_parser_declaration (parser);
36805 break;
36806 case pragma_member:
36807 cp_parser_member_declaration (parser);
36808 break;
36809 case pragma_objc_icode:
36810 cp_parser_block_declaration (parser, /*statement_p=*/false);
36811 break;
36812 default:
36813 cp_parser_declaration_statement (parser);
36814 break;
36815 }
36816 if (parser->omp_declare_simd
36817 && !parser->omp_declare_simd->error_seen
36818 && !parser->omp_declare_simd->fndecl_seen)
36819 error_at (pragma_tok->location,
36820 "%<#pragma omp declare simd%> not immediately followed by "
36821 "function declaration or definition");
36822 data.tokens.release ();
36823 parser->omp_declare_simd = NULL;
36824 }
36825 }
36826
36827 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36828 This function is modelled similar to the late parsing of omp declare
36829 simd. */
36830
36831 static tree
36832 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36833 {
36834 struct cp_token_cache *ce;
36835 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36836 int ii = 0;
36837
36838 if (parser->omp_declare_simd != NULL
36839 || lookup_attribute ("simd", attrs))
36840 {
36841 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36842 "used in the same function marked as a Cilk Plus SIMD-enabled "
36843 "function");
36844 parser->cilk_simd_fn_info->tokens.release ();
36845 XDELETE (parser->cilk_simd_fn_info);
36846 parser->cilk_simd_fn_info = NULL;
36847 return attrs;
36848 }
36849 if (!info->error_seen && info->fndecl_seen)
36850 {
36851 error ("vector attribute not immediately followed by a single function"
36852 " declaration or definition");
36853 info->error_seen = true;
36854 }
36855 if (info->error_seen)
36856 return attrs;
36857
36858 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36859 {
36860 tree c, cl;
36861
36862 cp_parser_push_lexer_for_tokens (parser, ce);
36863 parser->lexer->in_pragma = true;
36864 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36865 "SIMD-enabled functions attribute",
36866 NULL);
36867 cp_parser_pop_lexer (parser);
36868 if (cl)
36869 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36870
36871 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36872 TREE_CHAIN (c) = attrs;
36873 attrs = c;
36874
36875 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36876 TREE_CHAIN (c) = attrs;
36877 if (processing_template_decl)
36878 ATTR_IS_DEPENDENT (c) = 1;
36879 attrs = c;
36880 }
36881 info->fndecl_seen = true;
36882 parser->cilk_simd_fn_info->tokens.release ();
36883 XDELETE (parser->cilk_simd_fn_info);
36884 parser->cilk_simd_fn_info = NULL;
36885 return attrs;
36886 }
36887
36888 /* Finalize #pragma omp declare simd clauses after direct declarator has
36889 been parsed, and put that into "omp declare simd" attribute. */
36890
36891 static tree
36892 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36893 {
36894 struct cp_token_cache *ce;
36895 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36896 int i;
36897
36898 if (!data->error_seen && data->fndecl_seen)
36899 {
36900 error ("%<#pragma omp declare simd%> not immediately followed by "
36901 "a single function declaration or definition");
36902 data->error_seen = true;
36903 }
36904 if (data->error_seen)
36905 return attrs;
36906
36907 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36908 {
36909 tree c, cl;
36910
36911 cp_parser_push_lexer_for_tokens (parser, ce);
36912 parser->lexer->in_pragma = true;
36913 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36914 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36915 cp_lexer_consume_token (parser->lexer);
36916 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36917 "#pragma omp declare simd", pragma_tok);
36918 cp_parser_pop_lexer (parser);
36919 if (cl)
36920 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36921 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36922 TREE_CHAIN (c) = attrs;
36923 if (processing_template_decl)
36924 ATTR_IS_DEPENDENT (c) = 1;
36925 attrs = c;
36926 }
36927
36928 data->fndecl_seen = true;
36929 return attrs;
36930 }
36931
36932
36933 /* OpenMP 4.0:
36934 # pragma omp declare target new-line
36935 declarations and definitions
36936 # pragma omp end declare target new-line
36937
36938 OpenMP 4.5:
36939 # pragma omp declare target ( extended-list ) new-line
36940
36941 # pragma omp declare target declare-target-clauses[seq] new-line */
36942
36943 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36944 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36946
36947 static void
36948 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36949 {
36950 tree clauses = NULL_TREE;
36951 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36952 clauses
36953 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36954 "#pragma omp declare target", pragma_tok);
36955 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36956 {
36957 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36958 clauses);
36959 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36960 cp_parser_require_pragma_eol (parser, pragma_tok);
36961 }
36962 else
36963 {
36964 cp_parser_require_pragma_eol (parser, pragma_tok);
36965 scope_chain->omp_declare_target_attribute++;
36966 return;
36967 }
36968 if (scope_chain->omp_declare_target_attribute)
36969 error_at (pragma_tok->location,
36970 "%<#pragma omp declare target%> with clauses in between "
36971 "%<#pragma omp declare target%> without clauses and "
36972 "%<#pragma omp end declare target%>");
36973 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36974 {
36975 tree t = OMP_CLAUSE_DECL (c), id;
36976 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36977 tree at2 = lookup_attribute ("omp declare target link",
36978 DECL_ATTRIBUTES (t));
36979 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36980 {
36981 id = get_identifier ("omp declare target link");
36982 std::swap (at1, at2);
36983 }
36984 else
36985 id = get_identifier ("omp declare target");
36986 if (at2)
36987 {
36988 error_at (OMP_CLAUSE_LOCATION (c),
36989 "%qD specified both in declare target %<link%> and %<to%>"
36990 " clauses", t);
36991 continue;
36992 }
36993 if (!at1)
36994 {
36995 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36996 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36997 continue;
36998
36999 symtab_node *node = symtab_node::get (t);
37000 if (node != NULL)
37001 {
37002 node->offloadable = 1;
37003 if (ENABLE_OFFLOADING)
37004 {
37005 g->have_offload = true;
37006 if (is_a <varpool_node *> (node))
37007 vec_safe_push (offload_vars, t);
37008 }
37009 }
37010 }
37011 }
37012 }
37013
37014 static void
37015 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37016 {
37017 const char *p = "";
37018 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37019 {
37020 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37021 p = IDENTIFIER_POINTER (id);
37022 }
37023 if (strcmp (p, "declare") == 0)
37024 {
37025 cp_lexer_consume_token (parser->lexer);
37026 p = "";
37027 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37028 {
37029 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37030 p = IDENTIFIER_POINTER (id);
37031 }
37032 if (strcmp (p, "target") == 0)
37033 cp_lexer_consume_token (parser->lexer);
37034 else
37035 {
37036 cp_parser_error (parser, "expected %<target%>");
37037 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37038 return;
37039 }
37040 }
37041 else
37042 {
37043 cp_parser_error (parser, "expected %<declare%>");
37044 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37045 return;
37046 }
37047 cp_parser_require_pragma_eol (parser, pragma_tok);
37048 if (!scope_chain->omp_declare_target_attribute)
37049 error_at (pragma_tok->location,
37050 "%<#pragma omp end declare target%> without corresponding "
37051 "%<#pragma omp declare target%>");
37052 else
37053 scope_chain->omp_declare_target_attribute--;
37054 }
37055
37056 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37057 expression and optional initializer clause of
37058 #pragma omp declare reduction. We store the expression(s) as
37059 either 3, 6 or 7 special statements inside of the artificial function's
37060 body. The first two statements are DECL_EXPRs for the artificial
37061 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37062 expression that uses those variables.
37063 If there was any INITIALIZER clause, this is followed by further statements,
37064 the fourth and fifth statements are DECL_EXPRs for the artificial
37065 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37066 constructor variant (first token after open paren is not omp_priv),
37067 then the sixth statement is a statement with the function call expression
37068 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37069 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37070 to initialize the OMP_PRIV artificial variable and there is seventh
37071 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37072
37073 static bool
37074 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37075 {
37076 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37077 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37078 type = TREE_TYPE (type);
37079 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37080 DECL_ARTIFICIAL (omp_out) = 1;
37081 pushdecl (omp_out);
37082 add_decl_expr (omp_out);
37083 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37084 DECL_ARTIFICIAL (omp_in) = 1;
37085 pushdecl (omp_in);
37086 add_decl_expr (omp_in);
37087 tree combiner;
37088 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37089
37090 keep_next_level (true);
37091 tree block = begin_omp_structured_block ();
37092 combiner = cp_parser_expression (parser);
37093 finish_expr_stmt (combiner);
37094 block = finish_omp_structured_block (block);
37095 add_stmt (block);
37096
37097 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37098 return false;
37099
37100 const char *p = "";
37101 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37102 {
37103 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37104 p = IDENTIFIER_POINTER (id);
37105 }
37106
37107 if (strcmp (p, "initializer") == 0)
37108 {
37109 cp_lexer_consume_token (parser->lexer);
37110 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37111 return false;
37112
37113 p = "";
37114 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37115 {
37116 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37117 p = IDENTIFIER_POINTER (id);
37118 }
37119
37120 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37121 DECL_ARTIFICIAL (omp_priv) = 1;
37122 pushdecl (omp_priv);
37123 add_decl_expr (omp_priv);
37124 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37125 DECL_ARTIFICIAL (omp_orig) = 1;
37126 pushdecl (omp_orig);
37127 add_decl_expr (omp_orig);
37128
37129 keep_next_level (true);
37130 block = begin_omp_structured_block ();
37131
37132 bool ctor = false;
37133 if (strcmp (p, "omp_priv") == 0)
37134 {
37135 bool is_direct_init, is_non_constant_init;
37136 ctor = true;
37137 cp_lexer_consume_token (parser->lexer);
37138 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37139 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37140 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37141 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37142 == CPP_CLOSE_PAREN
37143 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37144 == CPP_CLOSE_PAREN))
37145 {
37146 finish_omp_structured_block (block);
37147 error ("invalid initializer clause");
37148 return false;
37149 }
37150 initializer = cp_parser_initializer (parser, &is_direct_init,
37151 &is_non_constant_init);
37152 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37153 NULL_TREE, LOOKUP_ONLYCONVERTING);
37154 }
37155 else
37156 {
37157 cp_parser_parse_tentatively (parser);
37158 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37159 /*check_dependency_p=*/true,
37160 /*template_p=*/NULL,
37161 /*declarator_p=*/false,
37162 /*optional_p=*/false);
37163 vec<tree, va_gc> *args;
37164 if (fn_name == error_mark_node
37165 || cp_parser_error_occurred (parser)
37166 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37167 || ((args = cp_parser_parenthesized_expression_list
37168 (parser, non_attr, /*cast_p=*/false,
37169 /*allow_expansion_p=*/true,
37170 /*non_constant_p=*/NULL)),
37171 cp_parser_error_occurred (parser)))
37172 {
37173 finish_omp_structured_block (block);
37174 cp_parser_abort_tentative_parse (parser);
37175 cp_parser_error (parser, "expected id-expression (arguments)");
37176 return false;
37177 }
37178 unsigned int i;
37179 tree arg;
37180 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37181 if (arg == omp_priv
37182 || (TREE_CODE (arg) == ADDR_EXPR
37183 && TREE_OPERAND (arg, 0) == omp_priv))
37184 break;
37185 cp_parser_abort_tentative_parse (parser);
37186 if (arg == NULL_TREE)
37187 error ("one of the initializer call arguments should be %<omp_priv%>"
37188 " or %<&omp_priv%>");
37189 initializer = cp_parser_postfix_expression (parser, false, false, false,
37190 false, NULL);
37191 finish_expr_stmt (initializer);
37192 }
37193
37194 block = finish_omp_structured_block (block);
37195 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37196 add_stmt (block);
37197
37198 if (ctor)
37199 add_decl_expr (omp_orig);
37200
37201 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37202 return false;
37203 }
37204
37205 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37206 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
37207
37208 return true;
37209 }
37210
37211 /* OpenMP 4.0
37212 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37213 initializer-clause[opt] new-line
37214
37215 initializer-clause:
37216 initializer (omp_priv initializer)
37217 initializer (function-name (argument-list)) */
37218
37219 static void
37220 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37221 enum pragma_context)
37222 {
37223 auto_vec<tree> types;
37224 enum tree_code reduc_code = ERROR_MARK;
37225 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37226 unsigned int i;
37227 cp_token *first_token;
37228 cp_token_cache *cp;
37229 int errs;
37230 void *p;
37231
37232 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37233 p = obstack_alloc (&declarator_obstack, 0);
37234
37235 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37236 goto fail;
37237
37238 switch (cp_lexer_peek_token (parser->lexer)->type)
37239 {
37240 case CPP_PLUS:
37241 reduc_code = PLUS_EXPR;
37242 break;
37243 case CPP_MULT:
37244 reduc_code = MULT_EXPR;
37245 break;
37246 case CPP_MINUS:
37247 reduc_code = MINUS_EXPR;
37248 break;
37249 case CPP_AND:
37250 reduc_code = BIT_AND_EXPR;
37251 break;
37252 case CPP_XOR:
37253 reduc_code = BIT_XOR_EXPR;
37254 break;
37255 case CPP_OR:
37256 reduc_code = BIT_IOR_EXPR;
37257 break;
37258 case CPP_AND_AND:
37259 reduc_code = TRUTH_ANDIF_EXPR;
37260 break;
37261 case CPP_OR_OR:
37262 reduc_code = TRUTH_ORIF_EXPR;
37263 break;
37264 case CPP_NAME:
37265 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37266 break;
37267 default:
37268 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37269 "%<|%>, %<&&%>, %<||%> or identifier");
37270 goto fail;
37271 }
37272
37273 if (reduc_code != ERROR_MARK)
37274 cp_lexer_consume_token (parser->lexer);
37275
37276 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37277 if (reduc_id == error_mark_node)
37278 goto fail;
37279
37280 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37281 goto fail;
37282
37283 /* Types may not be defined in declare reduction type list. */
37284 const char *saved_message;
37285 saved_message = parser->type_definition_forbidden_message;
37286 parser->type_definition_forbidden_message
37287 = G_("types may not be defined in declare reduction type list");
37288 bool saved_colon_corrects_to_scope_p;
37289 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37290 parser->colon_corrects_to_scope_p = false;
37291 bool saved_colon_doesnt_start_class_def_p;
37292 saved_colon_doesnt_start_class_def_p
37293 = parser->colon_doesnt_start_class_def_p;
37294 parser->colon_doesnt_start_class_def_p = true;
37295
37296 while (true)
37297 {
37298 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37299 type = cp_parser_type_id (parser);
37300 if (type == error_mark_node)
37301 ;
37302 else if (ARITHMETIC_TYPE_P (type)
37303 && (orig_reduc_id == NULL_TREE
37304 || (TREE_CODE (type) != COMPLEX_TYPE
37305 && (id_equal (orig_reduc_id, "min")
37306 || id_equal (orig_reduc_id, "max")))))
37307 error_at (loc, "predeclared arithmetic type %qT in "
37308 "%<#pragma omp declare reduction%>", type);
37309 else if (TREE_CODE (type) == FUNCTION_TYPE
37310 || TREE_CODE (type) == METHOD_TYPE
37311 || TREE_CODE (type) == ARRAY_TYPE)
37312 error_at (loc, "function or array type %qT in "
37313 "%<#pragma omp declare reduction%>", type);
37314 else if (TREE_CODE (type) == REFERENCE_TYPE)
37315 error_at (loc, "reference type %qT in "
37316 "%<#pragma omp declare reduction%>", type);
37317 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37318 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37319 "%<#pragma omp declare reduction%>", type);
37320 else
37321 types.safe_push (type);
37322
37323 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37324 cp_lexer_consume_token (parser->lexer);
37325 else
37326 break;
37327 }
37328
37329 /* Restore the saved message. */
37330 parser->type_definition_forbidden_message = saved_message;
37331 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37332 parser->colon_doesnt_start_class_def_p
37333 = saved_colon_doesnt_start_class_def_p;
37334
37335 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37336 || types.is_empty ())
37337 {
37338 fail:
37339 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37340 goto done;
37341 }
37342
37343 first_token = cp_lexer_peek_token (parser->lexer);
37344 cp = NULL;
37345 errs = errorcount;
37346 FOR_EACH_VEC_ELT (types, i, type)
37347 {
37348 tree fntype
37349 = build_function_type_list (void_type_node,
37350 cp_build_reference_type (type, false),
37351 NULL_TREE);
37352 tree this_reduc_id = reduc_id;
37353 if (!dependent_type_p (type))
37354 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37355 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37356 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37357 DECL_ARTIFICIAL (fndecl) = 1;
37358 DECL_EXTERNAL (fndecl) = 1;
37359 DECL_DECLARED_INLINE_P (fndecl) = 1;
37360 DECL_IGNORED_P (fndecl) = 1;
37361 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37362 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37363 DECL_ATTRIBUTES (fndecl)
37364 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37365 DECL_ATTRIBUTES (fndecl));
37366 if (processing_template_decl)
37367 fndecl = push_template_decl (fndecl);
37368 bool block_scope = false;
37369 tree block = NULL_TREE;
37370 if (current_function_decl)
37371 {
37372 block_scope = true;
37373 DECL_CONTEXT (fndecl) = global_namespace;
37374 if (!processing_template_decl)
37375 pushdecl (fndecl);
37376 }
37377 else if (current_class_type)
37378 {
37379 if (cp == NULL)
37380 {
37381 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37382 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37383 cp_lexer_consume_token (parser->lexer);
37384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37385 goto fail;
37386 cp = cp_token_cache_new (first_token,
37387 cp_lexer_peek_nth_token (parser->lexer,
37388 2));
37389 }
37390 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37391 finish_member_declaration (fndecl);
37392 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37393 DECL_PENDING_INLINE_P (fndecl) = 1;
37394 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37395 continue;
37396 }
37397 else
37398 {
37399 DECL_CONTEXT (fndecl) = current_namespace;
37400 pushdecl (fndecl);
37401 }
37402 if (!block_scope)
37403 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37404 else
37405 block = begin_omp_structured_block ();
37406 if (cp)
37407 {
37408 cp_parser_push_lexer_for_tokens (parser, cp);
37409 parser->lexer->in_pragma = true;
37410 }
37411 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37412 {
37413 if (!block_scope)
37414 finish_function (0);
37415 else
37416 DECL_CONTEXT (fndecl) = current_function_decl;
37417 if (cp)
37418 cp_parser_pop_lexer (parser);
37419 goto fail;
37420 }
37421 if (cp)
37422 cp_parser_pop_lexer (parser);
37423 if (!block_scope)
37424 finish_function (0);
37425 else
37426 {
37427 DECL_CONTEXT (fndecl) = current_function_decl;
37428 block = finish_omp_structured_block (block);
37429 if (TREE_CODE (block) == BIND_EXPR)
37430 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37431 else if (TREE_CODE (block) == STATEMENT_LIST)
37432 DECL_SAVED_TREE (fndecl) = block;
37433 if (processing_template_decl)
37434 add_decl_expr (fndecl);
37435 }
37436 cp_check_omp_declare_reduction (fndecl);
37437 if (cp == NULL && types.length () > 1)
37438 cp = cp_token_cache_new (first_token,
37439 cp_lexer_peek_nth_token (parser->lexer, 2));
37440 if (errs != errorcount)
37441 break;
37442 }
37443
37444 cp_parser_require_pragma_eol (parser, pragma_tok);
37445
37446 done:
37447 /* Free any declarators allocated. */
37448 obstack_free (&declarator_obstack, p);
37449 }
37450
37451 /* OpenMP 4.0
37452 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37453 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37454 initializer-clause[opt] new-line
37455 #pragma omp declare target new-line */
37456
37457 static void
37458 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37459 enum pragma_context context)
37460 {
37461 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37462 {
37463 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37464 const char *p = IDENTIFIER_POINTER (id);
37465
37466 if (strcmp (p, "simd") == 0)
37467 {
37468 cp_lexer_consume_token (parser->lexer);
37469 cp_parser_omp_declare_simd (parser, pragma_tok,
37470 context);
37471 return;
37472 }
37473 cp_ensure_no_omp_declare_simd (parser);
37474 if (strcmp (p, "reduction") == 0)
37475 {
37476 cp_lexer_consume_token (parser->lexer);
37477 cp_parser_omp_declare_reduction (parser, pragma_tok,
37478 context);
37479 return;
37480 }
37481 if (!flag_openmp) /* flag_openmp_simd */
37482 {
37483 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37484 return;
37485 }
37486 if (strcmp (p, "target") == 0)
37487 {
37488 cp_lexer_consume_token (parser->lexer);
37489 cp_parser_omp_declare_target (parser, pragma_tok);
37490 return;
37491 }
37492 }
37493 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37494 "or %<target%>");
37495 cp_parser_require_pragma_eol (parser, pragma_tok);
37496 }
37497
37498 /* OpenMP 4.5:
37499 #pragma omp taskloop taskloop-clause[optseq] new-line
37500 for-loop
37501
37502 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37503 for-loop */
37504
37505 #define OMP_TASKLOOP_CLAUSE_MASK \
37506 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37520
37521 static tree
37522 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37523 char *p_name, omp_clause_mask mask, tree *cclauses,
37524 bool *if_p)
37525 {
37526 tree clauses, sb, ret;
37527 unsigned int save;
37528 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37529
37530 strcat (p_name, " taskloop");
37531 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37532
37533 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37534 {
37535 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37536 const char *p = IDENTIFIER_POINTER (id);
37537
37538 if (strcmp (p, "simd") == 0)
37539 {
37540 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37541 if (cclauses == NULL)
37542 cclauses = cclauses_buf;
37543
37544 cp_lexer_consume_token (parser->lexer);
37545 if (!flag_openmp) /* flag_openmp_simd */
37546 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37547 cclauses, if_p);
37548 sb = begin_omp_structured_block ();
37549 save = cp_parser_begin_omp_structured_block (parser);
37550 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37551 cclauses, if_p);
37552 cp_parser_end_omp_structured_block (parser, save);
37553 tree body = finish_omp_structured_block (sb);
37554 if (ret == NULL)
37555 return ret;
37556 ret = make_node (OMP_TASKLOOP);
37557 TREE_TYPE (ret) = void_type_node;
37558 OMP_FOR_BODY (ret) = body;
37559 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37560 SET_EXPR_LOCATION (ret, loc);
37561 add_stmt (ret);
37562 return ret;
37563 }
37564 }
37565 if (!flag_openmp) /* flag_openmp_simd */
37566 {
37567 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37568 return NULL_TREE;
37569 }
37570
37571 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37572 cclauses == NULL);
37573 if (cclauses)
37574 {
37575 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37576 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37577 }
37578
37579 sb = begin_omp_structured_block ();
37580 save = cp_parser_begin_omp_structured_block (parser);
37581
37582 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37583 if_p);
37584
37585 cp_parser_end_omp_structured_block (parser, save);
37586 add_stmt (finish_omp_structured_block (sb));
37587
37588 return ret;
37589 }
37590
37591
37592 /* OpenACC 2.0:
37593 # pragma acc routine oacc-routine-clause[optseq] new-line
37594 function-definition
37595
37596 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37597 */
37598
37599 #define OACC_ROUTINE_CLAUSE_MASK \
37600 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37604
37605
37606 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37607 component, which must resolve to a declared namespace-scope
37608 function. The clauses are either processed directly (for a named
37609 function), or defered until the immediatley following declaration
37610 is parsed. */
37611
37612 static void
37613 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37614 enum pragma_context context)
37615 {
37616 gcc_checking_assert (context == pragma_external);
37617 /* The checking for "another pragma following this one" in the "no optional
37618 '( name )'" case makes sure that we dont re-enter. */
37619 gcc_checking_assert (parser->oacc_routine == NULL);
37620
37621 cp_oacc_routine_data data;
37622 data.error_seen = false;
37623 data.fndecl_seen = false;
37624 data.tokens = vNULL;
37625 data.clauses = NULL_TREE;
37626 data.loc = pragma_tok->location;
37627 /* It is safe to take the address of a local variable; it will only be
37628 used while this scope is live. */
37629 parser->oacc_routine = &data;
37630
37631 /* Look for optional '( name )'. */
37632 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37633 {
37634 cp_lexer_consume_token (parser->lexer); /* '(' */
37635
37636 /* We parse the name as an id-expression. If it resolves to
37637 anything other than a non-overloaded function at namespace
37638 scope, it's an error. */
37639 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37640 tree name = cp_parser_id_expression (parser,
37641 /*template_keyword_p=*/false,
37642 /*check_dependency_p=*/false,
37643 /*template_p=*/NULL,
37644 /*declarator_p=*/false,
37645 /*optional_p=*/false);
37646 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37647 if (name != error_mark_node && decl == error_mark_node)
37648 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37649
37650 if (decl == error_mark_node
37651 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37652 {
37653 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37654 parser->oacc_routine = NULL;
37655 return;
37656 }
37657
37658 data.clauses
37659 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37660 "#pragma acc routine",
37661 cp_lexer_peek_token (parser->lexer));
37662
37663 if (decl && is_overloaded_fn (decl)
37664 && (TREE_CODE (decl) != FUNCTION_DECL
37665 || DECL_FUNCTION_TEMPLATE_P (decl)))
37666 {
37667 error_at (name_loc,
37668 "%<#pragma acc routine%> names a set of overloads");
37669 parser->oacc_routine = NULL;
37670 return;
37671 }
37672
37673 /* Perhaps we should use the same rule as declarations in different
37674 namespaces? */
37675 if (!DECL_NAMESPACE_SCOPE_P (decl))
37676 {
37677 error_at (name_loc,
37678 "%qD does not refer to a namespace scope function", decl);
37679 parser->oacc_routine = NULL;
37680 return;
37681 }
37682
37683 if (TREE_CODE (decl) != FUNCTION_DECL)
37684 {
37685 error_at (name_loc, "%qD does not refer to a function", decl);
37686 parser->oacc_routine = NULL;
37687 return;
37688 }
37689
37690 cp_finalize_oacc_routine (parser, decl, false);
37691 parser->oacc_routine = NULL;
37692 }
37693 else /* No optional '( name )'. */
37694 {
37695 /* Store away all pragma tokens. */
37696 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37697 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37698 cp_lexer_consume_token (parser->lexer);
37699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37700 parser->oacc_routine->error_seen = true;
37701 cp_parser_require_pragma_eol (parser, pragma_tok);
37702 struct cp_token_cache *cp
37703 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37704 parser->oacc_routine->tokens.safe_push (cp);
37705
37706 /* Emit a helpful diagnostic if there's another pragma following this
37707 one. */
37708 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37709 {
37710 cp_ensure_no_oacc_routine (parser);
37711 data.tokens.release ();
37712 /* ..., and then just keep going. */
37713 return;
37714 }
37715
37716 /* We only have to consider the pragma_external case here. */
37717 cp_parser_declaration (parser);
37718 if (parser->oacc_routine
37719 && !parser->oacc_routine->fndecl_seen)
37720 cp_ensure_no_oacc_routine (parser);
37721 else
37722 parser->oacc_routine = NULL;
37723 data.tokens.release ();
37724 }
37725 }
37726
37727 /* Finalize #pragma acc routine clauses after direct declarator has
37728 been parsed. */
37729
37730 static tree
37731 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37732 {
37733 struct cp_token_cache *ce;
37734 cp_oacc_routine_data *data = parser->oacc_routine;
37735
37736 if (!data->error_seen && data->fndecl_seen)
37737 {
37738 error_at (data->loc,
37739 "%<#pragma acc routine%> not immediately followed by "
37740 "a single function declaration or definition");
37741 data->error_seen = true;
37742 }
37743 if (data->error_seen)
37744 return attrs;
37745
37746 gcc_checking_assert (data->tokens.length () == 1);
37747 ce = data->tokens[0];
37748
37749 cp_parser_push_lexer_for_tokens (parser, ce);
37750 parser->lexer->in_pragma = true;
37751 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37752
37753 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37754 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37755 parser->oacc_routine->clauses
37756 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37757 "#pragma acc routine", pragma_tok);
37758 cp_parser_pop_lexer (parser);
37759 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37760 fndecl_seen. */
37761
37762 return attrs;
37763 }
37764
37765 /* Apply any saved OpenACC routine clauses to a just-parsed
37766 declaration. */
37767
37768 static void
37769 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37770 {
37771 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37772 {
37773 /* Keep going if we're in error reporting mode. */
37774 if (parser->oacc_routine->error_seen
37775 || fndecl == error_mark_node)
37776 return;
37777
37778 if (parser->oacc_routine->fndecl_seen)
37779 {
37780 error_at (parser->oacc_routine->loc,
37781 "%<#pragma acc routine%> not immediately followed by"
37782 " a single function declaration or definition");
37783 parser->oacc_routine = NULL;
37784 return;
37785 }
37786 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37787 {
37788 cp_ensure_no_oacc_routine (parser);
37789 return;
37790 }
37791
37792 if (oacc_get_fn_attrib (fndecl))
37793 {
37794 error_at (parser->oacc_routine->loc,
37795 "%<#pragma acc routine%> already applied to %qD", fndecl);
37796 parser->oacc_routine = NULL;
37797 return;
37798 }
37799
37800 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37801 {
37802 error_at (parser->oacc_routine->loc,
37803 TREE_USED (fndecl)
37804 ? G_("%<#pragma acc routine%> must be applied before use")
37805 : G_("%<#pragma acc routine%> must be applied before "
37806 "definition"));
37807 parser->oacc_routine = NULL;
37808 return;
37809 }
37810
37811 /* Process the routine's dimension clauses. */
37812 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37813 oacc_replace_fn_attrib (fndecl, dims);
37814
37815 /* Add an "omp declare target" attribute. */
37816 DECL_ATTRIBUTES (fndecl)
37817 = tree_cons (get_identifier ("omp declare target"),
37818 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37819
37820 /* Don't unset parser->oacc_routine here: we may still need it to
37821 diagnose wrong usage. But, remember that we've used this "#pragma acc
37822 routine". */
37823 parser->oacc_routine->fndecl_seen = true;
37824 }
37825 }
37826
37827 /* Main entry point to OpenMP statement pragmas. */
37828
37829 static void
37830 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37831 {
37832 tree stmt;
37833 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37834 omp_clause_mask mask (0);
37835
37836 switch (cp_parser_pragma_kind (pragma_tok))
37837 {
37838 case PRAGMA_OACC_ATOMIC:
37839 cp_parser_omp_atomic (parser, pragma_tok);
37840 return;
37841 case PRAGMA_OACC_CACHE:
37842 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37843 break;
37844 case PRAGMA_OACC_DATA:
37845 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37846 break;
37847 case PRAGMA_OACC_ENTER_DATA:
37848 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37849 break;
37850 case PRAGMA_OACC_EXIT_DATA:
37851 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37852 break;
37853 case PRAGMA_OACC_HOST_DATA:
37854 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37855 break;
37856 case PRAGMA_OACC_KERNELS:
37857 case PRAGMA_OACC_PARALLEL:
37858 strcpy (p_name, "#pragma acc");
37859 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37860 if_p);
37861 break;
37862 case PRAGMA_OACC_LOOP:
37863 strcpy (p_name, "#pragma acc");
37864 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37865 if_p);
37866 break;
37867 case PRAGMA_OACC_UPDATE:
37868 stmt = cp_parser_oacc_update (parser, pragma_tok);
37869 break;
37870 case PRAGMA_OACC_WAIT:
37871 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37872 break;
37873 case PRAGMA_OMP_ATOMIC:
37874 cp_parser_omp_atomic (parser, pragma_tok);
37875 return;
37876 case PRAGMA_OMP_CRITICAL:
37877 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37878 break;
37879 case PRAGMA_OMP_DISTRIBUTE:
37880 strcpy (p_name, "#pragma omp");
37881 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37882 if_p);
37883 break;
37884 case PRAGMA_OMP_FOR:
37885 strcpy (p_name, "#pragma omp");
37886 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37887 if_p);
37888 break;
37889 case PRAGMA_OMP_MASTER:
37890 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37891 break;
37892 case PRAGMA_OMP_PARALLEL:
37893 strcpy (p_name, "#pragma omp");
37894 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37895 if_p);
37896 break;
37897 case PRAGMA_OMP_SECTIONS:
37898 strcpy (p_name, "#pragma omp");
37899 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37900 break;
37901 case PRAGMA_OMP_SIMD:
37902 strcpy (p_name, "#pragma omp");
37903 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37904 if_p);
37905 break;
37906 case PRAGMA_OMP_SINGLE:
37907 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37908 break;
37909 case PRAGMA_OMP_TASK:
37910 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37911 break;
37912 case PRAGMA_OMP_TASKGROUP:
37913 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37914 break;
37915 case PRAGMA_OMP_TASKLOOP:
37916 strcpy (p_name, "#pragma omp");
37917 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37918 if_p);
37919 break;
37920 case PRAGMA_OMP_TEAMS:
37921 strcpy (p_name, "#pragma omp");
37922 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37923 if_p);
37924 break;
37925 default:
37926 gcc_unreachable ();
37927 }
37928
37929 protected_set_expr_location (stmt, pragma_tok->location);
37930 }
37931 \f
37932 /* Transactional Memory parsing routines. */
37933
37934 /* Parse a transaction attribute.
37935
37936 txn-attribute:
37937 attribute
37938 [ [ identifier ] ]
37939
37940 We use this instead of cp_parser_attributes_opt for transactions to avoid
37941 the pedwarn in C++98 mode. */
37942
37943 static tree
37944 cp_parser_txn_attribute_opt (cp_parser *parser)
37945 {
37946 cp_token *token;
37947 tree attr_name, attr = NULL;
37948
37949 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37950 return cp_parser_attributes_opt (parser);
37951
37952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37953 return NULL_TREE;
37954 cp_lexer_consume_token (parser->lexer);
37955 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37956 goto error1;
37957
37958 token = cp_lexer_peek_token (parser->lexer);
37959 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37960 {
37961 token = cp_lexer_consume_token (parser->lexer);
37962
37963 attr_name = (token->type == CPP_KEYWORD
37964 /* For keywords, use the canonical spelling,
37965 not the parsed identifier. */
37966 ? ridpointers[(int) token->keyword]
37967 : token->u.value);
37968 attr = build_tree_list (attr_name, NULL_TREE);
37969 }
37970 else
37971 cp_parser_error (parser, "expected identifier");
37972
37973 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37974 error1:
37975 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37976 return attr;
37977 }
37978
37979 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37980
37981 transaction-statement:
37982 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37983 compound-statement
37984 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37985 */
37986
37987 static tree
37988 cp_parser_transaction (cp_parser *parser, cp_token *token)
37989 {
37990 unsigned char old_in = parser->in_transaction;
37991 unsigned char this_in = 1, new_in;
37992 enum rid keyword = token->keyword;
37993 tree stmt, attrs, noex;
37994
37995 cp_lexer_consume_token (parser->lexer);
37996
37997 if (keyword == RID_TRANSACTION_RELAXED
37998 || keyword == RID_SYNCHRONIZED)
37999 this_in |= TM_STMT_ATTR_RELAXED;
38000 else
38001 {
38002 attrs = cp_parser_txn_attribute_opt (parser);
38003 if (attrs)
38004 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38005 }
38006
38007 /* Parse a noexcept specification. */
38008 if (keyword == RID_ATOMIC_NOEXCEPT)
38009 noex = boolean_true_node;
38010 else if (keyword == RID_ATOMIC_CANCEL)
38011 {
38012 /* cancel-and-throw is unimplemented. */
38013 sorry ("atomic_cancel");
38014 noex = NULL_TREE;
38015 }
38016 else
38017 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38018
38019 /* Keep track if we're in the lexical scope of an outer transaction. */
38020 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38021
38022 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38023
38024 parser->in_transaction = new_in;
38025 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38026 parser->in_transaction = old_in;
38027
38028 finish_transaction_stmt (stmt, NULL, this_in, noex);
38029
38030 return stmt;
38031 }
38032
38033 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38034
38035 transaction-expression:
38036 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38037 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38038 */
38039
38040 static tree
38041 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38042 {
38043 unsigned char old_in = parser->in_transaction;
38044 unsigned char this_in = 1;
38045 cp_token *token;
38046 tree expr, noex;
38047 bool noex_expr;
38048 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38049
38050 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38051 || keyword == RID_TRANSACTION_RELAXED);
38052
38053 if (!flag_tm)
38054 error_at (loc,
38055 keyword == RID_TRANSACTION_RELAXED
38056 ? G_("%<__transaction_relaxed%> without transactional memory "
38057 "support enabled")
38058 : G_("%<__transaction_atomic%> without transactional memory "
38059 "support enabled"));
38060
38061 token = cp_parser_require_keyword (parser, keyword,
38062 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38063 : RT_TRANSACTION_RELAXED));
38064 gcc_assert (token != NULL);
38065
38066 if (keyword == RID_TRANSACTION_RELAXED)
38067 this_in |= TM_STMT_ATTR_RELAXED;
38068
38069 /* Set this early. This might mean that we allow transaction_cancel in
38070 an expression that we find out later actually has to be a constexpr.
38071 However, we expect that cxx_constant_value will be able to deal with
38072 this; also, if the noexcept has no constexpr, then what we parse next
38073 really is a transaction's body. */
38074 parser->in_transaction = this_in;
38075
38076 /* Parse a noexcept specification. */
38077 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38078 true);
38079
38080 if (!noex || !noex_expr
38081 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38082 {
38083 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
38084
38085 expr = cp_parser_expression (parser);
38086 expr = finish_parenthesized_expr (expr);
38087
38088 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
38089 }
38090 else
38091 {
38092 /* The only expression that is available got parsed for the noexcept
38093 already. noexcept is true then. */
38094 expr = noex;
38095 noex = boolean_true_node;
38096 }
38097
38098 expr = build_transaction_expr (token->location, expr, this_in, noex);
38099 parser->in_transaction = old_in;
38100
38101 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38102 return error_mark_node;
38103
38104 return (flag_tm ? expr : error_mark_node);
38105 }
38106
38107 /* Parse a function-transaction-block.
38108
38109 function-transaction-block:
38110 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38111 function-body
38112 __transaction_atomic txn-attribute[opt] function-try-block
38113 __transaction_relaxed ctor-initializer[opt] function-body
38114 __transaction_relaxed function-try-block
38115 */
38116
38117 static bool
38118 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38119 {
38120 unsigned char old_in = parser->in_transaction;
38121 unsigned char new_in = 1;
38122 tree compound_stmt, stmt, attrs;
38123 bool ctor_initializer_p;
38124 cp_token *token;
38125
38126 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38127 || keyword == RID_TRANSACTION_RELAXED);
38128 token = cp_parser_require_keyword (parser, keyword,
38129 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38130 : RT_TRANSACTION_RELAXED));
38131 gcc_assert (token != NULL);
38132
38133 if (keyword == RID_TRANSACTION_RELAXED)
38134 new_in |= TM_STMT_ATTR_RELAXED;
38135 else
38136 {
38137 attrs = cp_parser_txn_attribute_opt (parser);
38138 if (attrs)
38139 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38140 }
38141
38142 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38143
38144 parser->in_transaction = new_in;
38145
38146 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38147 ctor_initializer_p = cp_parser_function_try_block (parser);
38148 else
38149 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
38150 (parser, /*in_function_try_block=*/false);
38151
38152 parser->in_transaction = old_in;
38153
38154 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38155
38156 return ctor_initializer_p;
38157 }
38158
38159 /* Parse a __transaction_cancel statement.
38160
38161 cancel-statement:
38162 __transaction_cancel txn-attribute[opt] ;
38163 __transaction_cancel txn-attribute[opt] throw-expression ;
38164
38165 ??? Cancel and throw is not yet implemented. */
38166
38167 static tree
38168 cp_parser_transaction_cancel (cp_parser *parser)
38169 {
38170 cp_token *token;
38171 bool is_outer = false;
38172 tree stmt, attrs;
38173
38174 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38175 RT_TRANSACTION_CANCEL);
38176 gcc_assert (token != NULL);
38177
38178 attrs = cp_parser_txn_attribute_opt (parser);
38179 if (attrs)
38180 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38181
38182 /* ??? Parse cancel-and-throw here. */
38183
38184 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38185
38186 if (!flag_tm)
38187 {
38188 error_at (token->location, "%<__transaction_cancel%> without "
38189 "transactional memory support enabled");
38190 return error_mark_node;
38191 }
38192 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38193 {
38194 error_at (token->location, "%<__transaction_cancel%> within a "
38195 "%<__transaction_relaxed%>");
38196 return error_mark_node;
38197 }
38198 else if (is_outer)
38199 {
38200 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38201 && !is_tm_may_cancel_outer (current_function_decl))
38202 {
38203 error_at (token->location, "outer %<__transaction_cancel%> not "
38204 "within outer %<__transaction_atomic%>");
38205 error_at (token->location,
38206 " or a %<transaction_may_cancel_outer%> function");
38207 return error_mark_node;
38208 }
38209 }
38210 else if (parser->in_transaction == 0)
38211 {
38212 error_at (token->location, "%<__transaction_cancel%> not within "
38213 "%<__transaction_atomic%>");
38214 return error_mark_node;
38215 }
38216
38217 stmt = build_tm_abort_call (token->location, is_outer);
38218 add_stmt (stmt);
38219
38220 return stmt;
38221 }
38222 \f
38223 /* The parser. */
38224
38225 static GTY (()) cp_parser *the_parser;
38226
38227 \f
38228 /* Special handling for the first token or line in the file. The first
38229 thing in the file might be #pragma GCC pch_preprocess, which loads a
38230 PCH file, which is a GC collection point. So we need to handle this
38231 first pragma without benefit of an existing lexer structure.
38232
38233 Always returns one token to the caller in *FIRST_TOKEN. This is
38234 either the true first token of the file, or the first token after
38235 the initial pragma. */
38236
38237 static void
38238 cp_parser_initial_pragma (cp_token *first_token)
38239 {
38240 tree name = NULL;
38241
38242 cp_lexer_get_preprocessor_token (NULL, first_token);
38243 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38244 return;
38245
38246 cp_lexer_get_preprocessor_token (NULL, first_token);
38247 if (first_token->type == CPP_STRING)
38248 {
38249 name = first_token->u.value;
38250
38251 cp_lexer_get_preprocessor_token (NULL, first_token);
38252 if (first_token->type != CPP_PRAGMA_EOL)
38253 error_at (first_token->location,
38254 "junk at end of %<#pragma GCC pch_preprocess%>");
38255 }
38256 else
38257 error_at (first_token->location, "expected string literal");
38258
38259 /* Skip to the end of the pragma. */
38260 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38261 cp_lexer_get_preprocessor_token (NULL, first_token);
38262
38263 /* Now actually load the PCH file. */
38264 if (name)
38265 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38266
38267 /* Read one more token to return to our caller. We have to do this
38268 after reading the PCH file in, since its pointers have to be
38269 live. */
38270 cp_lexer_get_preprocessor_token (NULL, first_token);
38271 }
38272
38273 /* Parses the grainsize pragma for the _Cilk_for statement.
38274 Syntax:
38275 #pragma cilk grainsize = <VALUE>. */
38276
38277 static void
38278 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38279 {
38280 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38281 {
38282 tree exp = cp_parser_binary_expression (parser, false, false,
38283 PREC_NOT_OPERATOR, NULL);
38284 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38285 if (!exp || exp == error_mark_node)
38286 {
38287 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38288 return;
38289 }
38290
38291 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38292 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38293 cp_parser_cilk_for (parser, exp, if_p);
38294 else
38295 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38296 "%<#pragma cilk grainsize%> is not followed by "
38297 "%<_Cilk_for%>");
38298 return;
38299 }
38300 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38301 }
38302
38303 /* Normal parsing of a pragma token. Here we can (and must) use the
38304 regular lexer. */
38305
38306 static bool
38307 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38308 {
38309 cp_token *pragma_tok;
38310 unsigned int id;
38311 tree stmt;
38312 bool ret;
38313
38314 pragma_tok = cp_lexer_consume_token (parser->lexer);
38315 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38316 parser->lexer->in_pragma = true;
38317
38318 id = cp_parser_pragma_kind (pragma_tok);
38319 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38320 cp_ensure_no_omp_declare_simd (parser);
38321 switch (id)
38322 {
38323 case PRAGMA_GCC_PCH_PREPROCESS:
38324 error_at (pragma_tok->location,
38325 "%<#pragma GCC pch_preprocess%> must be first");
38326 break;
38327
38328 case PRAGMA_OMP_BARRIER:
38329 switch (context)
38330 {
38331 case pragma_compound:
38332 cp_parser_omp_barrier (parser, pragma_tok);
38333 return false;
38334 case pragma_stmt:
38335 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38336 "used in compound statements", "omp barrier");
38337 break;
38338 default:
38339 goto bad_stmt;
38340 }
38341 break;
38342
38343 case PRAGMA_OMP_FLUSH:
38344 switch (context)
38345 {
38346 case pragma_compound:
38347 cp_parser_omp_flush (parser, pragma_tok);
38348 return false;
38349 case pragma_stmt:
38350 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38351 "used in compound statements", "omp flush");
38352 break;
38353 default:
38354 goto bad_stmt;
38355 }
38356 break;
38357
38358 case PRAGMA_OMP_TASKWAIT:
38359 switch (context)
38360 {
38361 case pragma_compound:
38362 cp_parser_omp_taskwait (parser, pragma_tok);
38363 return false;
38364 case pragma_stmt:
38365 error_at (pragma_tok->location,
38366 "%<#pragma %s%> may only be used in compound statements",
38367 "omp taskwait");
38368 break;
38369 default:
38370 goto bad_stmt;
38371 }
38372 break;
38373
38374 case PRAGMA_OMP_TASKYIELD:
38375 switch (context)
38376 {
38377 case pragma_compound:
38378 cp_parser_omp_taskyield (parser, pragma_tok);
38379 return false;
38380 case pragma_stmt:
38381 error_at (pragma_tok->location,
38382 "%<#pragma %s%> may only be used in compound statements",
38383 "omp taskyield");
38384 break;
38385 default:
38386 goto bad_stmt;
38387 }
38388 break;
38389
38390 case PRAGMA_OMP_CANCEL:
38391 switch (context)
38392 {
38393 case pragma_compound:
38394 cp_parser_omp_cancel (parser, pragma_tok);
38395 return false;
38396 case pragma_stmt:
38397 error_at (pragma_tok->location,
38398 "%<#pragma %s%> may only be used in compound statements",
38399 "omp cancel");
38400 break;
38401 default:
38402 goto bad_stmt;
38403 }
38404 break;
38405
38406 case PRAGMA_OMP_CANCELLATION_POINT:
38407 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38408 return false;
38409
38410 case PRAGMA_OMP_THREADPRIVATE:
38411 cp_parser_omp_threadprivate (parser, pragma_tok);
38412 return false;
38413
38414 case PRAGMA_OMP_DECLARE:
38415 cp_parser_omp_declare (parser, pragma_tok, context);
38416 return false;
38417
38418 case PRAGMA_OACC_DECLARE:
38419 cp_parser_oacc_declare (parser, pragma_tok);
38420 return false;
38421
38422 case PRAGMA_OACC_ENTER_DATA:
38423 if (context == pragma_stmt)
38424 {
38425 error_at (pragma_tok->location,
38426 "%<#pragma %s%> may only be used in compound statements",
38427 "acc enter data");
38428 break;
38429 }
38430 else if (context != pragma_compound)
38431 goto bad_stmt;
38432 cp_parser_omp_construct (parser, pragma_tok, if_p);
38433 return true;
38434
38435 case PRAGMA_OACC_EXIT_DATA:
38436 if (context == pragma_stmt)
38437 {
38438 error_at (pragma_tok->location,
38439 "%<#pragma %s%> may only be used in compound statements",
38440 "acc exit data");
38441 break;
38442 }
38443 else if (context != pragma_compound)
38444 goto bad_stmt;
38445 cp_parser_omp_construct (parser, pragma_tok, if_p);
38446 return true;
38447
38448 case PRAGMA_OACC_ROUTINE:
38449 if (context != pragma_external)
38450 {
38451 error_at (pragma_tok->location,
38452 "%<#pragma acc routine%> must be at file scope");
38453 break;
38454 }
38455 cp_parser_oacc_routine (parser, pragma_tok, context);
38456 return false;
38457
38458 case PRAGMA_OACC_UPDATE:
38459 if (context == pragma_stmt)
38460 {
38461 error_at (pragma_tok->location,
38462 "%<#pragma %s%> may only be used in compound statements",
38463 "acc update");
38464 break;
38465 }
38466 else if (context != pragma_compound)
38467 goto bad_stmt;
38468 cp_parser_omp_construct (parser, pragma_tok, if_p);
38469 return true;
38470
38471 case PRAGMA_OACC_WAIT:
38472 if (context == pragma_stmt)
38473 {
38474 error_at (pragma_tok->location,
38475 "%<#pragma %s%> may only be used in compound statements",
38476 "acc wait");
38477 break;
38478 }
38479 else if (context != pragma_compound)
38480 goto bad_stmt;
38481 cp_parser_omp_construct (parser, pragma_tok, if_p);
38482 return true;
38483
38484 case PRAGMA_OACC_ATOMIC:
38485 case PRAGMA_OACC_CACHE:
38486 case PRAGMA_OACC_DATA:
38487 case PRAGMA_OACC_HOST_DATA:
38488 case PRAGMA_OACC_KERNELS:
38489 case PRAGMA_OACC_PARALLEL:
38490 case PRAGMA_OACC_LOOP:
38491 case PRAGMA_OMP_ATOMIC:
38492 case PRAGMA_OMP_CRITICAL:
38493 case PRAGMA_OMP_DISTRIBUTE:
38494 case PRAGMA_OMP_FOR:
38495 case PRAGMA_OMP_MASTER:
38496 case PRAGMA_OMP_PARALLEL:
38497 case PRAGMA_OMP_SECTIONS:
38498 case PRAGMA_OMP_SIMD:
38499 case PRAGMA_OMP_SINGLE:
38500 case PRAGMA_OMP_TASK:
38501 case PRAGMA_OMP_TASKGROUP:
38502 case PRAGMA_OMP_TASKLOOP:
38503 case PRAGMA_OMP_TEAMS:
38504 if (context != pragma_stmt && context != pragma_compound)
38505 goto bad_stmt;
38506 stmt = push_omp_privatization_clauses (false);
38507 cp_parser_omp_construct (parser, pragma_tok, if_p);
38508 pop_omp_privatization_clauses (stmt);
38509 return true;
38510
38511 case PRAGMA_OMP_ORDERED:
38512 if (context != pragma_stmt && context != pragma_compound)
38513 goto bad_stmt;
38514 stmt = push_omp_privatization_clauses (false);
38515 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38516 pop_omp_privatization_clauses (stmt);
38517 return ret;
38518
38519 case PRAGMA_OMP_TARGET:
38520 if (context != pragma_stmt && context != pragma_compound)
38521 goto bad_stmt;
38522 stmt = push_omp_privatization_clauses (false);
38523 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38524 pop_omp_privatization_clauses (stmt);
38525 return ret;
38526
38527 case PRAGMA_OMP_END_DECLARE_TARGET:
38528 cp_parser_omp_end_declare_target (parser, pragma_tok);
38529 return false;
38530
38531 case PRAGMA_OMP_SECTION:
38532 error_at (pragma_tok->location,
38533 "%<#pragma omp section%> may only be used in "
38534 "%<#pragma omp sections%> construct");
38535 break;
38536
38537 case PRAGMA_IVDEP:
38538 {
38539 if (context == pragma_external)
38540 {
38541 error_at (pragma_tok->location,
38542 "%<#pragma GCC ivdep%> must be inside a function");
38543 break;
38544 }
38545 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38546 cp_token *tok;
38547 tok = cp_lexer_peek_token (the_parser->lexer);
38548 if (tok->type != CPP_KEYWORD
38549 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38550 && tok->keyword != RID_DO))
38551 {
38552 cp_parser_error (parser, "for, while or do statement expected");
38553 return false;
38554 }
38555 cp_parser_iteration_statement (parser, if_p, true);
38556 return true;
38557 }
38558
38559 case PRAGMA_CILK_SIMD:
38560 if (context == pragma_external)
38561 {
38562 error_at (pragma_tok->location,
38563 "%<#pragma simd%> must be inside a function");
38564 break;
38565 }
38566 stmt = push_omp_privatization_clauses (false);
38567 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38568 pop_omp_privatization_clauses (stmt);
38569 return true;
38570
38571 case PRAGMA_CILK_GRAINSIZE:
38572 if (context == pragma_external)
38573 {
38574 error_at (pragma_tok->location,
38575 "%<#pragma cilk grainsize%> must be inside a function");
38576 break;
38577 }
38578
38579 /* Ignore the pragma if Cilk Plus is not enabled. */
38580 if (flag_cilkplus)
38581 {
38582 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38583 return true;
38584 }
38585 else
38586 {
38587 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38588 "%<#pragma cilk grainsize%>");
38589 break;
38590 }
38591
38592 default:
38593 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38594 c_invoke_pragma_handler (id);
38595 break;
38596
38597 bad_stmt:
38598 cp_parser_error (parser, "expected declaration specifiers");
38599 break;
38600 }
38601
38602 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38603 return false;
38604 }
38605
38606 /* The interface the pragma parsers have to the lexer. */
38607
38608 enum cpp_ttype
38609 pragma_lex (tree *value, location_t *loc)
38610 {
38611 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38612 enum cpp_ttype ret = tok->type;
38613
38614 *value = tok->u.value;
38615 if (loc)
38616 *loc = tok->location;
38617
38618 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38619 ret = CPP_EOF;
38620 else if (ret == CPP_STRING)
38621 *value = cp_parser_string_literal (the_parser, false, false);
38622 else
38623 {
38624 if (ret == CPP_KEYWORD)
38625 ret = CPP_NAME;
38626 cp_lexer_consume_token (the_parser->lexer);
38627 }
38628
38629 return ret;
38630 }
38631
38632 \f
38633 /* External interface. */
38634
38635 /* Parse one entire translation unit. */
38636
38637 void
38638 c_parse_file (void)
38639 {
38640 static bool already_called = false;
38641
38642 if (already_called)
38643 fatal_error (input_location,
38644 "inter-module optimizations not implemented for C++");
38645 already_called = true;
38646
38647 the_parser = cp_parser_new ();
38648 push_deferring_access_checks (flag_access_control
38649 ? dk_no_deferred : dk_no_check);
38650 cp_parser_translation_unit (the_parser);
38651 the_parser = NULL;
38652 }
38653
38654 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38655 vectorlength clause:
38656 Syntax:
38657 vectorlength ( constant-expression ) */
38658
38659 static tree
38660 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38661 bool is_simd_fn)
38662 {
38663 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38664 tree expr;
38665 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38666 safelen clause. Thus, vectorlength is represented as OMP 4.0
38667 safelen. For SIMD-enabled function it is represented by OMP 4.0
38668 simdlen. */
38669 if (!is_simd_fn)
38670 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38671 loc);
38672 else
38673 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38674 loc);
38675
38676 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38677 return error_mark_node;
38678
38679 expr = cp_parser_constant_expression (parser);
38680 expr = maybe_constant_value (expr);
38681
38682 /* If expr == error_mark_node, then don't emit any errors nor
38683 create a clause. if any of the above functions returns
38684 error mark node then they would have emitted an error message. */
38685 if (expr == error_mark_node)
38686 ;
38687 else if (!TREE_TYPE (expr)
38688 || !TREE_CONSTANT (expr)
38689 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38690 error_at (loc, "vectorlength must be an integer constant");
38691 else if (TREE_CONSTANT (expr)
38692 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38693 error_at (loc, "vectorlength must be a power of 2");
38694 else
38695 {
38696 tree c;
38697 if (!is_simd_fn)
38698 {
38699 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38700 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38701 OMP_CLAUSE_CHAIN (c) = clauses;
38702 clauses = c;
38703 }
38704 else
38705 {
38706 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38707 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38708 OMP_CLAUSE_CHAIN (c) = clauses;
38709 clauses = c;
38710 }
38711 }
38712
38713 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38714 return error_mark_node;
38715 return clauses;
38716 }
38717
38718 /* Handles the Cilk Plus #pragma simd linear clause.
38719 Syntax:
38720 linear ( simd-linear-variable-list )
38721
38722 simd-linear-variable-list:
38723 simd-linear-variable
38724 simd-linear-variable-list , simd-linear-variable
38725
38726 simd-linear-variable:
38727 id-expression
38728 id-expression : simd-linear-step
38729
38730 simd-linear-step:
38731 conditional-expression */
38732
38733 static tree
38734 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38735 {
38736 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38737
38738 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38739 return clauses;
38740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38741 {
38742 cp_parser_error (parser, "expected identifier");
38743 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38744 return error_mark_node;
38745 }
38746
38747 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38748 parser->colon_corrects_to_scope_p = false;
38749 while (1)
38750 {
38751 cp_token *token = cp_lexer_peek_token (parser->lexer);
38752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38753 {
38754 cp_parser_error (parser, "expected variable-name");
38755 clauses = error_mark_node;
38756 break;
38757 }
38758
38759 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38760 false, false);
38761 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38762 token->location);
38763 if (decl == error_mark_node)
38764 {
38765 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38766 token->location);
38767 clauses = error_mark_node;
38768 }
38769 else
38770 {
38771 tree e = NULL_TREE;
38772 tree step_size = integer_one_node;
38773
38774 /* If present, parse the linear step. Otherwise, assume the default
38775 value of 1. */
38776 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38777 {
38778 cp_lexer_consume_token (parser->lexer);
38779
38780 e = cp_parser_assignment_expression (parser);
38781 e = maybe_constant_value (e);
38782
38783 if (e == error_mark_node)
38784 {
38785 /* If an error has occurred, then the whole pragma is
38786 considered ill-formed. Thus, no reason to keep
38787 parsing. */
38788 clauses = error_mark_node;
38789 break;
38790 }
38791 else if (type_dependent_expression_p (e)
38792 || value_dependent_expression_p (e)
38793 || (TREE_TYPE (e)
38794 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38795 && (TREE_CONSTANT (e)
38796 || DECL_P (e))))
38797 step_size = e;
38798 else
38799 cp_parser_error (parser,
38800 "step size must be an integer constant "
38801 "expression or an integer variable");
38802 }
38803
38804 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38805 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38806 OMP_CLAUSE_DECL (l) = decl;
38807 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38808 OMP_CLAUSE_CHAIN (l) = clauses;
38809 clauses = l;
38810 }
38811 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38812 cp_lexer_consume_token (parser->lexer);
38813 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38814 break;
38815 else
38816 {
38817 error_at (cp_lexer_peek_token (parser->lexer)->location,
38818 "expected %<,%> or %<)%> after %qE", decl);
38819 clauses = error_mark_node;
38820 break;
38821 }
38822 }
38823 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38824 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38825 return clauses;
38826 }
38827
38828 /* Returns the name of the next clause. If the clause is not
38829 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38830 token is not consumed. Otherwise, the appropriate enum from the
38831 pragma_simd_clause is returned and the token is consumed. */
38832
38833 static pragma_omp_clause
38834 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38835 {
38836 pragma_omp_clause clause_type;
38837 cp_token *token = cp_lexer_peek_token (parser->lexer);
38838
38839 if (token->keyword == RID_PRIVATE)
38840 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38841 else if (!token->u.value || token->type != CPP_NAME)
38842 return PRAGMA_CILK_CLAUSE_NONE;
38843 else if (id_equal (token->u.value, "vectorlength"))
38844 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38845 else if (id_equal (token->u.value, "linear"))
38846 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38847 else if (id_equal (token->u.value, "firstprivate"))
38848 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38849 else if (id_equal (token->u.value, "lastprivate"))
38850 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38851 else if (id_equal (token->u.value, "reduction"))
38852 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38853 else
38854 return PRAGMA_CILK_CLAUSE_NONE;
38855
38856 cp_lexer_consume_token (parser->lexer);
38857 return clause_type;
38858 }
38859
38860 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38861
38862 static tree
38863 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38864 {
38865 tree clauses = NULL_TREE;
38866
38867 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38868 && clauses != error_mark_node)
38869 {
38870 pragma_omp_clause c_kind;
38871 c_kind = cp_parser_cilk_simd_clause_name (parser);
38872 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38873 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38874 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38875 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38876 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38877 /* Use the OpenMP 4.0 equivalent function. */
38878 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38879 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38880 /* Use the OpenMP 4.0 equivalent function. */
38881 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38882 clauses);
38883 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38884 /* Use the OMP 4.0 equivalent function. */
38885 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38886 clauses);
38887 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38888 /* Use the OMP 4.0 equivalent function. */
38889 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38890 else
38891 {
38892 clauses = error_mark_node;
38893 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38894 break;
38895 }
38896 }
38897
38898 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38899
38900 if (clauses == error_mark_node)
38901 return error_mark_node;
38902 else
38903 return finish_omp_clauses (clauses, C_ORT_CILK);
38904 }
38905
38906 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38907
38908 static void
38909 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38910 {
38911 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38912
38913 if (clauses == error_mark_node)
38914 return;
38915
38916 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38917 {
38918 error_at (cp_lexer_peek_token (parser->lexer)->location,
38919 "for statement expected");
38920 return;
38921 }
38922
38923 tree sb = begin_omp_structured_block ();
38924 int save = cp_parser_begin_omp_structured_block (parser);
38925 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38926 if (ret)
38927 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38928 cp_parser_end_omp_structured_block (parser, save);
38929 add_stmt (finish_omp_structured_block (sb));
38930 }
38931
38932 /* Main entry-point for parsing Cilk Plus _Cilk_for
38933 loops. The return value is error_mark_node
38934 when errors happen and CILK_FOR tree on success. */
38935
38936 static tree
38937 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38938 {
38939 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38940 gcc_unreachable ();
38941
38942 tree sb = begin_omp_structured_block ();
38943 int save = cp_parser_begin_omp_structured_block (parser);
38944
38945 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38946 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38947 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38948 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38949
38950 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38951 if (ret)
38952 cpp_validate_cilk_plus_loop (ret);
38953 else
38954 ret = error_mark_node;
38955
38956 cp_parser_end_omp_structured_block (parser, save);
38957 add_stmt (finish_omp_structured_block (sb));
38958 return ret;
38959 }
38960
38961 /* Create an identifier for a generic parameter type (a synthesized
38962 template parameter implied by `auto' or a concept identifier). */
38963
38964 static GTY(()) int generic_parm_count;
38965 static tree
38966 make_generic_type_name ()
38967 {
38968 char buf[32];
38969 sprintf (buf, "auto:%d", ++generic_parm_count);
38970 return get_identifier (buf);
38971 }
38972
38973 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38974 (creating a new template parameter list if necessary). Returns the newly
38975 created template type parm. */
38976
38977 static tree
38978 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38979 {
38980 gcc_assert (current_binding_level->kind == sk_function_parms);
38981
38982 /* Before committing to modifying any scope, if we're in an
38983 implicit template scope, and we're trying to synthesize a
38984 constrained parameter, try to find a previous parameter with
38985 the same name. This is the same-type rule for abbreviated
38986 function templates.
38987
38988 NOTE: We can generate implicit parameters when tentatively
38989 parsing a nested name specifier, only to reject that parse
38990 later. However, matching the same template-id as part of a
38991 direct-declarator should generate an identical template
38992 parameter, so this rule will merge them. */
38993 if (parser->implicit_template_scope && constr)
38994 {
38995 tree t = parser->implicit_template_parms;
38996 while (t)
38997 {
38998 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38999 {
39000 tree d = TREE_VALUE (t);
39001 if (TREE_CODE (d) == PARM_DECL)
39002 /* Return the TEMPLATE_PARM_INDEX. */
39003 d = DECL_INITIAL (d);
39004 return d;
39005 }
39006 t = TREE_CHAIN (t);
39007 }
39008 }
39009
39010 /* We are either continuing a function template that already contains implicit
39011 template parameters, creating a new fully-implicit function template, or
39012 extending an existing explicit function template with implicit template
39013 parameters. */
39014
39015 cp_binding_level *const entry_scope = current_binding_level;
39016
39017 bool become_template = false;
39018 cp_binding_level *parent_scope = 0;
39019
39020 if (parser->implicit_template_scope)
39021 {
39022 gcc_assert (parser->implicit_template_parms);
39023
39024 current_binding_level = parser->implicit_template_scope;
39025 }
39026 else
39027 {
39028 /* Roll back to the existing template parameter scope (in the case of
39029 extending an explicit function template) or introduce a new template
39030 parameter scope ahead of the function parameter scope (or class scope
39031 in the case of out-of-line member definitions). The function scope is
39032 added back after template parameter synthesis below. */
39033
39034 cp_binding_level *scope = entry_scope;
39035
39036 while (scope->kind == sk_function_parms)
39037 {
39038 parent_scope = scope;
39039 scope = scope->level_chain;
39040 }
39041 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39042 {
39043 /* If not defining a class, then any class scope is a scope level in
39044 an out-of-line member definition. In this case simply wind back
39045 beyond the first such scope to inject the template parameter list.
39046 Otherwise wind back to the class being defined. The latter can
39047 occur in class member friend declarations such as:
39048
39049 class A {
39050 void foo (auto);
39051 };
39052 class B {
39053 friend void A::foo (auto);
39054 };
39055
39056 The template parameter list synthesized for the friend declaration
39057 must be injected in the scope of 'B'. This can also occur in
39058 erroneous cases such as:
39059
39060 struct A {
39061 struct B {
39062 void foo (auto);
39063 };
39064 void B::foo (auto) {}
39065 };
39066
39067 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39068 but, nevertheless, the template parameter list synthesized for the
39069 declarator should be injected into the scope of 'A' as if the
39070 ill-formed template was specified explicitly. */
39071
39072 while (scope->kind == sk_class && !scope->defining_class_p)
39073 {
39074 parent_scope = scope;
39075 scope = scope->level_chain;
39076 }
39077 }
39078
39079 current_binding_level = scope;
39080
39081 if (scope->kind != sk_template_parms
39082 || !function_being_declared_is_template_p (parser))
39083 {
39084 /* Introduce a new template parameter list for implicit template
39085 parameters. */
39086
39087 become_template = true;
39088
39089 parser->implicit_template_scope
39090 = begin_scope (sk_template_parms, NULL);
39091
39092 ++processing_template_decl;
39093
39094 parser->fully_implicit_function_template_p = true;
39095 ++parser->num_template_parameter_lists;
39096 }
39097 else
39098 {
39099 /* Synthesize implicit template parameters at the end of the explicit
39100 template parameter list. */
39101
39102 gcc_assert (current_template_parms);
39103
39104 parser->implicit_template_scope = scope;
39105
39106 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39107 parser->implicit_template_parms
39108 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39109 }
39110 }
39111
39112 /* Synthesize a new template parameter and track the current template
39113 parameter chain with implicit_template_parms. */
39114
39115 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39116 tree synth_id = make_generic_type_name ();
39117 tree synth_tmpl_parm;
39118 bool non_type = false;
39119
39120 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39121 synth_tmpl_parm
39122 = finish_template_type_parm (class_type_node, synth_id);
39123 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39124 synth_tmpl_parm
39125 = finish_constrained_template_template_parm (proto, synth_id);
39126 else
39127 {
39128 synth_tmpl_parm = copy_decl (proto);
39129 DECL_NAME (synth_tmpl_parm) = synth_id;
39130 non_type = true;
39131 }
39132
39133 // Attach the constraint to the parm before processing.
39134 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39135 TREE_TYPE (node) = constr;
39136 tree new_parm
39137 = process_template_parm (parser->implicit_template_parms,
39138 input_location,
39139 node,
39140 /*non_type=*/non_type,
39141 /*param_pack=*/false);
39142
39143 // Chain the new parameter to the list of implicit parameters.
39144 if (parser->implicit_template_parms)
39145 parser->implicit_template_parms
39146 = TREE_CHAIN (parser->implicit_template_parms);
39147 else
39148 parser->implicit_template_parms = new_parm;
39149
39150 tree new_decl = get_local_decls ();
39151 if (non_type)
39152 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39153 new_decl = DECL_INITIAL (new_decl);
39154
39155 /* If creating a fully implicit function template, start the new implicit
39156 template parameter list with this synthesized type, otherwise grow the
39157 current template parameter list. */
39158
39159 if (become_template)
39160 {
39161 parent_scope->level_chain = current_binding_level;
39162
39163 tree new_parms = make_tree_vec (1);
39164 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39165 current_template_parms = tree_cons (size_int (processing_template_decl),
39166 new_parms, current_template_parms);
39167 }
39168 else
39169 {
39170 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39171 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39172 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39173 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39174 }
39175
39176 // If the new parameter was constrained, we need to add that to the
39177 // constraints in the template parameter list.
39178 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39179 {
39180 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39181 reqs = conjoin_constraints (reqs, req);
39182 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39183 }
39184
39185 current_binding_level = entry_scope;
39186
39187 return new_decl;
39188 }
39189
39190 /* Finish the declaration of a fully implicit function template. Such a
39191 template has no explicit template parameter list so has not been through the
39192 normal template head and tail processing. synthesize_implicit_template_parm
39193 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39194 provided if the declaration is a class member such that its template
39195 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39196 form is returned. Otherwise NULL_TREE is returned. */
39197
39198 static tree
39199 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39200 {
39201 gcc_assert (parser->fully_implicit_function_template_p);
39202
39203 if (member_decl_opt && member_decl_opt != error_mark_node
39204 && DECL_VIRTUAL_P (member_decl_opt))
39205 {
39206 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39207 "implicit templates may not be %<virtual%>");
39208 DECL_VIRTUAL_P (member_decl_opt) = false;
39209 }
39210
39211 if (member_decl_opt)
39212 member_decl_opt = finish_member_template_decl (member_decl_opt);
39213 end_template_decl ();
39214
39215 parser->fully_implicit_function_template_p = false;
39216 --parser->num_template_parameter_lists;
39217
39218 return member_decl_opt;
39219 }
39220
39221 #include "gt-cp-parser.h"