C++: fix-it hint for missing "typename" (PR c++/63392)
[gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
47
48 \f
49 /* The lexer. */
50
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
53
54 static cp_token eof_token =
55 {
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
57 };
58
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116 };
117
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128 };
129
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
153
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
175
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 };
185
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
188
189 class type_id_in_expr_sentinel
190 {
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
200 };
201
202 /* Prototypes. */
203
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
246
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
249
250 static void cp_parser_initial_pragma
251 (cp_token *);
252
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
257
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
261
262 /* Variables. */
263
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
266
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
270
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
276
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
281 {
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
285
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
287
288 if (buffer == NULL)
289 return;
290
291 if (num == 0)
292 num = buffer->length ();
293
294 if (start_token == NULL)
295 start_token = buffer->address ();
296
297 if (start_token > buffer->address ())
298 {
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
301 }
302
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
306 {
307 if (token == start_token)
308 do_print = true;
309
310 if (!do_print)
311 continue;
312
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
316
317 cp_lexer_print_token (file, token);
318
319 if (token == curr_token)
320 fprintf (file, "]]");
321
322 switch (token->type)
323 {
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
330
331 default:
332 fputc (' ', file);
333 }
334 }
335
336 if (i == num && i < buffer->length ())
337 {
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
340 }
341
342 fprintf (file, "\n");
343 }
344
345
346 /* Dump all tokens in BUFFER to stderr. */
347
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
350 {
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
352 }
353
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
356 {
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
358 }
359
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
362 {
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
367 }
368
369
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
372
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
375 {
376 if (t)
377 {
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
380 }
381 }
382
383
384 /* Dump parser context C to FILE. */
385
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
388 {
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
393 }
394
395
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
397
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
400 {
401 unsigned i;
402 cp_parser_context *c;
403
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
406 {
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
409 }
410 }
411
412
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
414
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
417 {
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
420 }
421
422
423 /* Print an unparsed function entry UF to FILE. */
424
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
427 {
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
431
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
436 {
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
442 }
443
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
447 {
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
450 }
451 fprintf (file, "\n");
452
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
456 {
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
459 }
460 fprintf (file, "\n");
461 }
462
463
464 /* Print the stack of unparsed member functions S to FILE. */
465
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
469 {
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
472
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
475 {
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
478 }
479 }
480
481
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
484
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
487 {
488 cp_token *next_token, *first_token, *start_token;
489
490 if (file == NULL)
491 file = stderr;
492
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
500 }
501
502
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
505
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
508 {
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
512
513 if (file == NULL)
514 file = stderr;
515
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
587 }
588
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
591 {
592 cp_debug_parser (stderr, &ref);
593 }
594
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
597 {
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
602 }
603
604 /* Allocate memory for a new lexer object and return it. */
605
606 static cp_lexer *
607 cp_lexer_alloc (void)
608 {
609 cp_lexer *lexer;
610
611 c_common_no_more_pch ();
612
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
615
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
618
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
620
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
623
624 return lexer;
625 }
626
627
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
630
631 static cp_lexer *
632 cp_lexer_new_main (void)
633 {
634 cp_lexer *lexer;
635 cp_token token;
636
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
641
642 lexer = cp_lexer_alloc ();
643
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
646
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
649 {
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
652 }
653
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
660
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
664
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
667 }
668
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
671
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 {
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
683
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
685
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
688
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
691 }
692
693 /* Frees all resources associated with LEXER. */
694
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
697 {
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
701 }
702
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
707
708 #define LEXER_DEBUGGING_ENABLED_P false
709
710 /* Returns nonzero if debugging information should be output. */
711
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
714 {
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
717
718 return lexer->debugging_p;
719 }
720
721
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724 {
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
726
727 return lexer->next_token - previous_p;
728 }
729
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 {
733 return pos;
734 }
735
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738 {
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
740 }
741
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
744 {
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
749 }
750
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
753 {
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
755
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
758 {
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
761 }
762
763 return cp_lexer_token_at (lexer, tp);
764 }
765
766 /* nonzero if we are presently saving tokens. */
767
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
770 {
771 return lexer->saved_tokens.length () != 0;
772 }
773
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
778
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
781 {
782 static int is_extern_c = 0;
783
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
791
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
798
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
801 {
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
803 {
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
808 }
809 else
810 {
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
814 {
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
820
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
824 }
825
826 token->keyword = RID_MAX;
827 }
828 }
829 else if (token->type == CPP_AT_NAME)
830 {
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
834 {
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
842 */
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
852 }
853 }
854 }
855
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
859 {
860 if (token->type != CPP_EOF)
861 {
862 input_location = token->location;
863 }
864 }
865
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
869 {
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
872 }
873
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
876
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
879 {
880 if (cp_lexer_debugging_p (lexer))
881 {
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
885 }
886 return lexer->next_token;
887 }
888
889 /* Return true if the next token has the indicated TYPE. */
890
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893 {
894 return cp_lexer_peek_token (lexer)->type == type;
895 }
896
897 /* Return true if the next token does not have the indicated TYPE. */
898
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901 {
902 return !cp_lexer_next_token_is (lexer, type);
903 }
904
905 /* Return true if the next token is the indicated KEYWORD. */
906
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909 {
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
911 }
912
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915 {
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
917 }
918
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921 {
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
923 }
924
925 /* Return true if the next token is not the indicated KEYWORD. */
926
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
929 {
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
931 }
932
933 /* Return true if KEYWORD can start a decl-specifier. */
934
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
937 {
938 switch (keyword)
939 {
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
977
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
984 }
985 }
986
987 /* Return true if the next token is a keyword for a decl-specifier. */
988
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
991 {
992 cp_token *token;
993
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
996 }
997
998 /* Returns TRUE iff the token T begins a decltype type. */
999
1000 static bool
1001 token_is_decltype (cp_token *t)
1002 {
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1005 }
1006
1007 /* Returns TRUE iff the next token begins a decltype type. */
1008
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1011 {
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1014 }
1015
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1018
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1021 {
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1027 {
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1033 }
1034 /* Return the stored value. */
1035 return check_value->value;
1036 }
1037
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1043
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1046 {
1047 cp_token *token;
1048
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1051
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1055
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1060 {
1061 ++token;
1062 if (token == lexer->last_token)
1063 {
1064 token = &eof_token;
1065 break;
1066 }
1067
1068 if (!token->purged_p)
1069 --n;
1070 }
1071
1072 if (cp_lexer_debugging_p (lexer))
1073 {
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1076 }
1077
1078 return token;
1079 }
1080
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1083
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1086 {
1087 cp_token *token = lexer->next_token;
1088
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1091
1092 do
1093 {
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1096 {
1097 lexer->next_token = &eof_token;
1098 break;
1099 }
1100
1101 }
1102 while (lexer->next_token->purged_p);
1103
1104 cp_lexer_set_source_position_from_token (token);
1105
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1108 {
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1112 }
1113
1114 return token;
1115 }
1116
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1120
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1123 {
1124 cp_token *tok = lexer->next_token;
1125
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1131
1132 do
1133 {
1134 tok++;
1135 if (tok == lexer->last_token)
1136 {
1137 tok = &eof_token;
1138 break;
1139 }
1140 }
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1143 }
1144
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1148
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1151 {
1152 cp_token *peek = lexer->next_token;
1153
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1156
1157 gcc_assert (tok < peek);
1158
1159 for ( tok += 1; tok != peek; tok += 1)
1160 {
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1165 }
1166 }
1167
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1170
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1173 {
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1177
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1179 }
1180
1181 /* Commit to the portion of the token stream most recently saved. */
1182
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1185 {
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1189
1190 lexer->saved_tokens.pop ();
1191 }
1192
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1195
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1198 {
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1202
1203 lexer->next_token = lexer->saved_tokens.pop ();
1204 }
1205
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1210
1211 struct saved_token_sentinel
1212 {
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1217 {
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1220 }
1221 void rollback ()
1222 {
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1225 }
1226 ~saved_token_sentinel()
1227 {
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1231 }
1232 };
1233
1234 /* Print a representation of the TOKEN on the STREAM. */
1235
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1238 {
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1252 };
1253
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1256 {
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1266
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1274
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1278
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1287 }
1288 }
1289
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1292 {
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1295 }
1296
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1299 {
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1304 }
1305
1306
1307 /* Start emitting debugging information. */
1308
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1311 {
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1315
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1318 }
1319
1320 /* Stop emitting debugging information. */
1321
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1324 {
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1328
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1331 }
1332
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1334
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1337 {
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1342 }
1343
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1346
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1349 {
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1351 {
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1355 }
1356 }
1357
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1360
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1363 {
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1365 {
1366 if (fndecl == error_mark_node)
1367 {
1368 parser->omp_declare_simd = NULL;
1369 return;
1370 }
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1372 {
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1375 }
1376 }
1377 }
1378
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1381
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1384 {
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1386 {
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1391 }
1392 }
1393 \f
1394 /* Decl-specifiers. */
1395
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1397
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1400 {
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1402 }
1403
1404 /* Declarators. */
1405
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1410
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1421
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1424
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1428
1429 /* Alloc BYTES from the declarator memory pool. */
1430
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1433 {
1434 return obstack_alloc (&declarator_obstack, bytes);
1435 }
1436
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1439
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1442 {
1443 cp_declarator *declarator;
1444
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1453
1454 return declarator;
1455 }
1456
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1462
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1466 {
1467 cp_declarator *declarator;
1468
1469 /* It is valid to write:
1470
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1474
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1481
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1485
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1490
1491 return declarator;
1492 }
1493
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1498
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1502 {
1503 cp_declarator *declarator;
1504
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1510 {
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1514 }
1515 else
1516 declarator->parameter_pack_p = false;
1517
1518 declarator->std_attributes = attributes;
1519
1520 return declarator;
1521 }
1522
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1526
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1530 {
1531 cp_declarator *declarator;
1532
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1538 {
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1542 }
1543 else
1544 declarator->parameter_pack_p = false;
1545
1546 declarator->std_attributes = attributes;
1547
1548 return declarator;
1549 }
1550
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1554
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1559 {
1560 cp_declarator *declarator;
1561
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1566
1567 if (pointee)
1568 {
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1571 }
1572 else
1573 declarator->parameter_pack_p = false;
1574
1575 declarator->std_attributes = attributes;
1576
1577 return declarator;
1578 }
1579
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1584
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1595 {
1596 cp_declarator *declarator;
1597
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1609 {
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1613 }
1614 else
1615 declarator->parameter_pack_p = false;
1616
1617 return declarator;
1618 }
1619
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1622
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1625 {
1626 cp_declarator *declarator;
1627
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1632 {
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1636 }
1637 else
1638 declarator->parameter_pack_p = false;
1639
1640 return declarator;
1641 }
1642
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1647 {
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1651
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1658 {
1659 switch ((int)declarator->kind)
1660 {
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1666
1667 case cdk_error:
1668 return true;
1669
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1673 }
1674 }
1675
1676 return !found;
1677 }
1678
1679 cp_parameter_declarator *no_parameters;
1680
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1683
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1690 {
1691 cp_parameter_declarator *parameter;
1692
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1704
1705 return parameter;
1706 }
1707
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1709
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1712 {
1713 while (declarator)
1714 {
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1723 }
1724 return false;
1725 }
1726
1727 /* The parser. */
1728
1729 /* Overview
1730 --------
1731
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1739
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1743
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1752
1753 Methodology
1754 -----------
1755
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1772
1773 Future Improvements
1774 -------------------
1775
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1781
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1784
1785 enum
1786 {
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1803 };
1804
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1808
1809 /* The different kinds of declarators we want to parse. */
1810
1811 enum cp_parser_declarator_kind
1812 {
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1819 };
1820
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1824
1825 enum cp_parser_prec
1826 {
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1840 };
1841
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1844
1845 struct cp_parser_binary_operations_map_node
1846 {
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1853 };
1854
1855 struct cp_parser_expression_stack_entry
1856 {
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1869 };
1870
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1876
1877 /* Prototypes. */
1878
1879 /* Constructors and destructors. */
1880
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1883
1884 /* Class variables. */
1885
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1887
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1891
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1895
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1899
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1902
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1905
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1910
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1913
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1915
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1917
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1919
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1921
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1923 };
1924
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1929
1930 /* Constructors and destructors. */
1931
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1934
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1937 {
1938 cp_parser_context *context;
1939
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1942 {
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1947 }
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1950
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1956 {
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1962 }
1963
1964 return context;
1965 }
1966
1967 /* Managing the unparsed function queues. */
1968
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1977
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1980 {
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1983 }
1984
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1987 {
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1990 }
1991
1992 /* Prototypes. */
1993
1994 /* Constructors and destructors. */
1995
1996 static cp_parser *cp_parser_new
1997 (void);
1998
1999 /* Routines to parse various constructs.
2000
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2010
2011 /* Lexical conventions [gram.lex] */
2012
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2023
2024 /* Basic concepts [gram.basic] */
2025
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2028
2029 /* Expressions [gram.expr] */
2030
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2051 bool = false);
2052 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2053 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2054 static void cp_parser_pseudo_destructor_name
2055 (cp_parser *, tree, tree *, tree *);
2056 static cp_expr cp_parser_unary_expression
2057 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2058 static enum tree_code cp_parser_unary_operator
2059 (cp_token *);
2060 static tree cp_parser_new_expression
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_placement
2063 (cp_parser *);
2064 static tree cp_parser_new_type_id
2065 (cp_parser *, tree *);
2066 static cp_declarator *cp_parser_new_declarator_opt
2067 (cp_parser *);
2068 static cp_declarator *cp_parser_direct_new_declarator
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_initializer
2071 (cp_parser *);
2072 static tree cp_parser_delete_expression
2073 (cp_parser *);
2074 static cp_expr cp_parser_cast_expression
2075 (cp_parser *, bool, bool, bool, cp_id_kind *);
2076 static cp_expr cp_parser_binary_expression
2077 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2078 static tree cp_parser_question_colon_clause
2079 (cp_parser *, cp_expr);
2080 static cp_expr cp_parser_assignment_expression
2081 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2082 static enum tree_code cp_parser_assignment_operator_opt
2083 (cp_parser *);
2084 static cp_expr cp_parser_expression
2085 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2086 static cp_expr cp_parser_constant_expression
2087 (cp_parser *, bool = false, bool * = NULL, bool = false);
2088 static cp_expr cp_parser_builtin_offsetof
2089 (cp_parser *);
2090 static cp_expr cp_parser_lambda_expression
2091 (cp_parser *);
2092 static void cp_parser_lambda_introducer
2093 (cp_parser *, tree);
2094 static bool cp_parser_lambda_declarator_opt
2095 (cp_parser *, tree);
2096 static void cp_parser_lambda_body
2097 (cp_parser *, tree);
2098
2099 /* Statements [gram.stmt.stmt] */
2100
2101 static void cp_parser_statement
2102 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2103 static void cp_parser_label_for_labeled_statement
2104 (cp_parser *, tree);
2105 static tree cp_parser_expression_statement
2106 (cp_parser *, tree);
2107 static tree cp_parser_compound_statement
2108 (cp_parser *, tree, int, bool);
2109 static void cp_parser_statement_seq_opt
2110 (cp_parser *, tree);
2111 static tree cp_parser_selection_statement
2112 (cp_parser *, bool *, vec<tree> *);
2113 static tree cp_parser_condition
2114 (cp_parser *);
2115 static tree cp_parser_iteration_statement
2116 (cp_parser *, bool *, bool, unsigned short);
2117 static bool cp_parser_init_statement
2118 (cp_parser *, tree *decl);
2119 static tree cp_parser_for
2120 (cp_parser *, bool, unsigned short);
2121 static tree cp_parser_c_for
2122 (cp_parser *, tree, tree, bool, unsigned short);
2123 static tree cp_parser_range_for
2124 (cp_parser *, tree, tree, tree, bool, unsigned short);
2125 static void do_range_for_auto_deduction
2126 (tree, tree);
2127 static tree cp_parser_perform_range_for_lookup
2128 (tree, tree *, tree *);
2129 static tree cp_parser_range_for_member_function
2130 (tree, tree);
2131 static tree cp_parser_jump_statement
2132 (cp_parser *);
2133 static void cp_parser_declaration_statement
2134 (cp_parser *);
2135
2136 static tree cp_parser_implicitly_scoped_statement
2137 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2138 static void cp_parser_already_scoped_statement
2139 (cp_parser *, bool *, const token_indent_info &);
2140
2141 /* Declarations [gram.dcl.dcl] */
2142
2143 static void cp_parser_declaration_seq_opt
2144 (cp_parser *);
2145 static void cp_parser_declaration
2146 (cp_parser *);
2147 static void cp_parser_block_declaration
2148 (cp_parser *, bool);
2149 static void cp_parser_simple_declaration
2150 (cp_parser *, bool, tree *);
2151 static void cp_parser_decl_specifier_seq
2152 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2153 static tree cp_parser_storage_class_specifier_opt
2154 (cp_parser *);
2155 static tree cp_parser_function_specifier_opt
2156 (cp_parser *, cp_decl_specifier_seq *);
2157 static tree cp_parser_type_specifier
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2159 int *, bool *);
2160 static tree cp_parser_simple_type_specifier
2161 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2162 static tree cp_parser_type_name
2163 (cp_parser *, bool);
2164 static tree cp_parser_type_name
2165 (cp_parser *);
2166 static tree cp_parser_nonclass_name
2167 (cp_parser* parser);
2168 static tree cp_parser_elaborated_type_specifier
2169 (cp_parser *, bool, bool);
2170 static tree cp_parser_enum_specifier
2171 (cp_parser *);
2172 static void cp_parser_enumerator_list
2173 (cp_parser *, tree);
2174 static void cp_parser_enumerator_definition
2175 (cp_parser *, tree);
2176 static tree cp_parser_namespace_name
2177 (cp_parser *);
2178 static void cp_parser_namespace_definition
2179 (cp_parser *);
2180 static void cp_parser_namespace_body
2181 (cp_parser *);
2182 static tree cp_parser_qualified_namespace_specifier
2183 (cp_parser *);
2184 static void cp_parser_namespace_alias_definition
2185 (cp_parser *);
2186 static bool cp_parser_using_declaration
2187 (cp_parser *, bool);
2188 static void cp_parser_using_directive
2189 (cp_parser *);
2190 static tree cp_parser_alias_declaration
2191 (cp_parser *);
2192 static void cp_parser_asm_definition
2193 (cp_parser *);
2194 static void cp_parser_linkage_specification
2195 (cp_parser *);
2196 static void cp_parser_static_assert
2197 (cp_parser *, bool);
2198 static tree cp_parser_decltype
2199 (cp_parser *);
2200 static tree cp_parser_decomposition_declaration
2201 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2202
2203 /* Declarators [gram.dcl.decl] */
2204
2205 static tree cp_parser_init_declarator
2206 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2207 bool, bool, int, bool *, tree *, location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2210 static cp_declarator *cp_parser_direct_declarator
2211 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2212 static enum tree_code cp_parser_ptr_operator
2213 (cp_parser *, tree *, cp_cv_quals *, tree *);
2214 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2215 (cp_parser *);
2216 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2217 (cp_parser *);
2218 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2219 (cp_parser *);
2220 static tree cp_parser_tx_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_late_return_type_opt
2223 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2224 static tree cp_parser_declarator_id
2225 (cp_parser *, bool);
2226 static tree cp_parser_type_id
2227 (cp_parser *);
2228 static tree cp_parser_template_type_arg
2229 (cp_parser *);
2230 static tree cp_parser_trailing_type_id (cp_parser *);
2231 static tree cp_parser_type_id_1
2232 (cp_parser *, bool, bool);
2233 static void cp_parser_type_specifier_seq
2234 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2235 static tree cp_parser_parameter_declaration_clause
2236 (cp_parser *);
2237 static tree cp_parser_parameter_declaration_list
2238 (cp_parser *);
2239 static cp_parameter_declarator *cp_parser_parameter_declaration
2240 (cp_parser *, bool, bool *);
2241 static tree cp_parser_default_argument
2242 (cp_parser *, bool);
2243 static void cp_parser_function_body
2244 (cp_parser *, bool);
2245 static tree cp_parser_initializer
2246 (cp_parser *, bool *, bool *, bool = false);
2247 static cp_expr cp_parser_initializer_clause
2248 (cp_parser *, bool *);
2249 static cp_expr cp_parser_braced_list
2250 (cp_parser*, bool*);
2251 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2252 (cp_parser *, bool *);
2253
2254 static void cp_parser_ctor_initializer_opt_and_function_body
2255 (cp_parser *, bool);
2256
2257 static tree cp_parser_late_parsing_omp_declare_simd
2258 (cp_parser *, tree);
2259
2260 static tree cp_parser_late_parsing_oacc_routine
2261 (cp_parser *, tree);
2262
2263 static tree synthesize_implicit_template_parm
2264 (cp_parser *, tree);
2265 static tree finish_fully_implicit_template
2266 (cp_parser *, tree);
2267 static void abort_fully_implicit_template
2268 (cp_parser *);
2269
2270 /* Classes [gram.class] */
2271
2272 static tree cp_parser_class_name
2273 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2274 static tree cp_parser_class_specifier
2275 (cp_parser *);
2276 static tree cp_parser_class_head
2277 (cp_parser *, bool *);
2278 static enum tag_types cp_parser_class_key
2279 (cp_parser *);
2280 static void cp_parser_type_parameter_key
2281 (cp_parser* parser);
2282 static void cp_parser_member_specification_opt
2283 (cp_parser *);
2284 static void cp_parser_member_declaration
2285 (cp_parser *);
2286 static tree cp_parser_pure_specifier
2287 (cp_parser *);
2288 static tree cp_parser_constant_initializer
2289 (cp_parser *);
2290
2291 /* Derived classes [gram.class.derived] */
2292
2293 static tree cp_parser_base_clause
2294 (cp_parser *);
2295 static tree cp_parser_base_specifier
2296 (cp_parser *);
2297
2298 /* Special member functions [gram.special] */
2299
2300 static tree cp_parser_conversion_function_id
2301 (cp_parser *);
2302 static tree cp_parser_conversion_type_id
2303 (cp_parser *);
2304 static cp_declarator *cp_parser_conversion_declarator_opt
2305 (cp_parser *);
2306 static void cp_parser_ctor_initializer_opt
2307 (cp_parser *);
2308 static void cp_parser_mem_initializer_list
2309 (cp_parser *);
2310 static tree cp_parser_mem_initializer
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer_id
2313 (cp_parser *);
2314
2315 /* Overloading [gram.over] */
2316
2317 static cp_expr cp_parser_operator_function_id
2318 (cp_parser *);
2319 static cp_expr cp_parser_operator
2320 (cp_parser *);
2321
2322 /* Templates [gram.temp] */
2323
2324 static void cp_parser_template_declaration
2325 (cp_parser *, bool);
2326 static tree cp_parser_template_parameter_list
2327 (cp_parser *);
2328 static tree cp_parser_template_parameter
2329 (cp_parser *, bool *, bool *);
2330 static tree cp_parser_type_parameter
2331 (cp_parser *, bool *);
2332 static tree cp_parser_template_id
2333 (cp_parser *, bool, bool, enum tag_types, bool);
2334 static tree cp_parser_template_name
2335 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2336 static tree cp_parser_template_argument_list
2337 (cp_parser *);
2338 static tree cp_parser_template_argument
2339 (cp_parser *);
2340 static void cp_parser_explicit_instantiation
2341 (cp_parser *);
2342 static void cp_parser_explicit_specialization
2343 (cp_parser *);
2344
2345 /* Exception handling [gram.exception] */
2346
2347 static tree cp_parser_try_block
2348 (cp_parser *);
2349 static void cp_parser_function_try_block
2350 (cp_parser *);
2351 static void cp_parser_handler_seq
2352 (cp_parser *);
2353 static void cp_parser_handler
2354 (cp_parser *);
2355 static tree cp_parser_exception_declaration
2356 (cp_parser *);
2357 static tree cp_parser_throw_expression
2358 (cp_parser *);
2359 static tree cp_parser_exception_specification_opt
2360 (cp_parser *);
2361 static tree cp_parser_type_id_list
2362 (cp_parser *);
2363
2364 /* GNU Extensions */
2365
2366 static tree cp_parser_asm_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_asm_operand_list
2369 (cp_parser *);
2370 static tree cp_parser_asm_clobber_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_label_list
2373 (cp_parser *);
2374 static bool cp_next_tokens_can_be_attribute_p
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_gnu_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_std_attribute_p
2379 (cp_parser *);
2380 static bool cp_nth_tokens_can_be_std_attribute_p
2381 (cp_parser *, size_t);
2382 static bool cp_nth_tokens_can_be_gnu_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_attribute_p
2385 (cp_parser *, size_t);
2386 static tree cp_parser_attributes_opt
2387 (cp_parser *);
2388 static tree cp_parser_gnu_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attribute_list
2391 (cp_parser *);
2392 static tree cp_parser_std_attribute
2393 (cp_parser *, tree);
2394 static tree cp_parser_std_attribute_spec
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute_spec_seq
2397 (cp_parser *);
2398 static size_t cp_parser_skip_attributes_opt
2399 (cp_parser *, size_t);
2400 static bool cp_parser_extension_opt
2401 (cp_parser *, int *);
2402 static void cp_parser_label_declaration
2403 (cp_parser *);
2404
2405 /* Concept Extensions */
2406
2407 static tree cp_parser_requires_clause
2408 (cp_parser *);
2409 static tree cp_parser_requires_clause_opt
2410 (cp_parser *);
2411 static tree cp_parser_requires_expression
2412 (cp_parser *);
2413 static tree cp_parser_requirement_parameter_list
2414 (cp_parser *);
2415 static tree cp_parser_requirement_body
2416 (cp_parser *);
2417 static tree cp_parser_requirement_list
2418 (cp_parser *);
2419 static tree cp_parser_requirement
2420 (cp_parser *);
2421 static tree cp_parser_simple_requirement
2422 (cp_parser *);
2423 static tree cp_parser_compound_requirement
2424 (cp_parser *);
2425 static tree cp_parser_type_requirement
2426 (cp_parser *);
2427 static tree cp_parser_nested_requirement
2428 (cp_parser *);
2429
2430 /* Transactional Memory Extensions */
2431
2432 static tree cp_parser_transaction
2433 (cp_parser *, cp_token *);
2434 static tree cp_parser_transaction_expression
2435 (cp_parser *, enum rid);
2436 static void cp_parser_function_transaction
2437 (cp_parser *, enum rid);
2438 static tree cp_parser_transaction_cancel
2439 (cp_parser *);
2440
2441 enum pragma_context {
2442 pragma_external,
2443 pragma_member,
2444 pragma_objc_icode,
2445 pragma_stmt,
2446 pragma_compound
2447 };
2448 static bool cp_parser_pragma
2449 (cp_parser *, enum pragma_context, bool *);
2450
2451 /* Objective-C++ Productions */
2452
2453 static tree cp_parser_objc_message_receiver
2454 (cp_parser *);
2455 static tree cp_parser_objc_message_args
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_encode_expression
2460 (cp_parser *);
2461 static tree cp_parser_objc_defs_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_protocol_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_selector_expression
2466 (cp_parser *);
2467 static cp_expr cp_parser_objc_expression
2468 (cp_parser *);
2469 static bool cp_parser_objc_selector_p
2470 (enum cpp_ttype);
2471 static tree cp_parser_objc_selector
2472 (cp_parser *);
2473 static tree cp_parser_objc_protocol_refs_opt
2474 (cp_parser *);
2475 static void cp_parser_objc_declaration
2476 (cp_parser *, tree);
2477 static tree cp_parser_objc_statement
2478 (cp_parser *);
2479 static bool cp_parser_objc_valid_prefix_attributes
2480 (cp_parser *, tree *);
2481 static void cp_parser_objc_at_property_declaration
2482 (cp_parser *) ;
2483 static void cp_parser_objc_at_synthesize_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_dynamic_declaration
2486 (cp_parser *) ;
2487 static tree cp_parser_objc_struct_declaration
2488 (cp_parser *) ;
2489
2490 /* Utility Routines */
2491
2492 static cp_expr cp_parser_lookup_name
2493 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2494 static tree cp_parser_lookup_name_simple
2495 (cp_parser *, tree, location_t);
2496 static tree cp_parser_maybe_treat_template_as_class
2497 (tree, bool);
2498 static bool cp_parser_check_declarator_template_parameters
2499 (cp_parser *, cp_declarator *, location_t);
2500 static bool cp_parser_check_template_parameters
2501 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2502 static cp_expr cp_parser_simple_cast_expression
2503 (cp_parser *);
2504 static tree cp_parser_global_scope_opt
2505 (cp_parser *, bool);
2506 static bool cp_parser_constructor_declarator_p
2507 (cp_parser *, bool);
2508 static tree cp_parser_function_definition_from_specifiers_and_declarator
2509 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2510 static tree cp_parser_function_definition_after_declarator
2511 (cp_parser *, bool);
2512 static bool cp_parser_template_declaration_after_export
2513 (cp_parser *, bool);
2514 static void cp_parser_perform_template_parameter_access_checks
2515 (vec<deferred_access_check, va_gc> *);
2516 static tree cp_parser_single_declaration
2517 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2518 static cp_expr cp_parser_functional_cast
2519 (cp_parser *, tree);
2520 static tree cp_parser_save_member_function_body
2521 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2522 static tree cp_parser_save_nsdmi
2523 (cp_parser *);
2524 static tree cp_parser_enclosed_template_argument_list
2525 (cp_parser *);
2526 static void cp_parser_save_default_args
2527 (cp_parser *, tree);
2528 static void cp_parser_late_parsing_for_member
2529 (cp_parser *, tree);
2530 static tree cp_parser_late_parse_one_default_arg
2531 (cp_parser *, tree, tree, tree);
2532 static void cp_parser_late_parsing_nsdmi
2533 (cp_parser *, tree);
2534 static void cp_parser_late_parsing_default_args
2535 (cp_parser *, tree);
2536 static tree cp_parser_sizeof_operand
2537 (cp_parser *, enum rid);
2538 static cp_expr cp_parser_trait_expr
2539 (cp_parser *, enum rid);
2540 static bool cp_parser_declares_only_class_p
2541 (cp_parser *);
2542 static void cp_parser_set_storage_class
2543 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2544 static void cp_parser_set_decl_spec_type
2545 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2546 static void set_and_check_decl_spec_loc
2547 (cp_decl_specifier_seq *decl_specs,
2548 cp_decl_spec ds, cp_token *);
2549 static bool cp_parser_friend_p
2550 (const cp_decl_specifier_seq *);
2551 static void cp_parser_required_error
2552 (cp_parser *, required_token, bool, location_t);
2553 static cp_token *cp_parser_require
2554 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2555 static cp_token *cp_parser_require_keyword
2556 (cp_parser *, enum rid, required_token);
2557 static bool cp_parser_token_starts_function_definition_p
2558 (cp_token *);
2559 static bool cp_parser_next_token_starts_class_definition_p
2560 (cp_parser *);
2561 static bool cp_parser_next_token_ends_template_argument_p
2562 (cp_parser *);
2563 static bool cp_parser_nth_token_starts_template_argument_list_p
2564 (cp_parser *, size_t);
2565 static enum tag_types cp_parser_token_is_class_key
2566 (cp_token *);
2567 static enum tag_types cp_parser_token_is_type_parameter_key
2568 (cp_token *);
2569 static void cp_parser_check_class_key
2570 (enum tag_types, tree type);
2571 static void cp_parser_check_access_in_redeclaration
2572 (tree type, location_t location);
2573 static bool cp_parser_optional_template_keyword
2574 (cp_parser *);
2575 static void cp_parser_pre_parsed_nested_name_specifier
2576 (cp_parser *);
2577 static bool cp_parser_cache_group
2578 (cp_parser *, enum cpp_ttype, unsigned);
2579 static tree cp_parser_cache_defarg
2580 (cp_parser *parser, bool nsdmi);
2581 static void cp_parser_parse_tentatively
2582 (cp_parser *);
2583 static void cp_parser_commit_to_tentative_parse
2584 (cp_parser *);
2585 static void cp_parser_commit_to_topmost_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_abort_tentative_parse
2588 (cp_parser *);
2589 static bool cp_parser_parse_definitely
2590 (cp_parser *);
2591 static inline bool cp_parser_parsing_tentatively
2592 (cp_parser *);
2593 static bool cp_parser_uncommitted_to_tentative_parse_p
2594 (cp_parser *);
2595 static void cp_parser_error
2596 (cp_parser *, const char *);
2597 static void cp_parser_name_lookup_error
2598 (cp_parser *, tree, tree, name_lookup_error, location_t);
2599 static bool cp_parser_simulate_error
2600 (cp_parser *);
2601 static bool cp_parser_check_type_definition
2602 (cp_parser *);
2603 static void cp_parser_check_for_definition_in_return_type
2604 (cp_declarator *, tree, location_t type_location);
2605 static void cp_parser_check_for_invalid_template_id
2606 (cp_parser *, tree, enum tag_types, location_t location);
2607 static bool cp_parser_non_integral_constant_expression
2608 (cp_parser *, non_integral_constant);
2609 static void cp_parser_diagnose_invalid_type_name
2610 (cp_parser *, tree, location_t);
2611 static bool cp_parser_parse_and_diagnose_invalid_type_name
2612 (cp_parser *);
2613 static int cp_parser_skip_to_closing_parenthesis
2614 (cp_parser *, bool, bool, bool);
2615 static void cp_parser_skip_to_end_of_statement
2616 (cp_parser *);
2617 static void cp_parser_consume_semicolon_at_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_skip_to_end_of_block_or_statement
2620 (cp_parser *);
2621 static bool cp_parser_skip_to_closing_brace
2622 (cp_parser *);
2623 static void cp_parser_skip_to_end_of_template_parameter_list
2624 (cp_parser *);
2625 static void cp_parser_skip_to_pragma_eol
2626 (cp_parser*, cp_token *);
2627 static bool cp_parser_error_occurred
2628 (cp_parser *);
2629 static bool cp_parser_allow_gnu_extensions_p
2630 (cp_parser *);
2631 static bool cp_parser_is_pure_string_literal
2632 (cp_token *);
2633 static bool cp_parser_is_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_keyword
2636 (cp_token *, enum rid);
2637 static tree cp_parser_make_typename_type
2638 (cp_parser *, tree, location_t location);
2639 static cp_declarator * cp_parser_make_indirect_declarator
2640 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2641 static bool cp_parser_compound_literal_p
2642 (cp_parser *);
2643 static bool cp_parser_array_designator_p
2644 (cp_parser *);
2645 static bool cp_parser_init_statement_p
2646 (cp_parser *);
2647 static bool cp_parser_skip_to_closing_square_bracket
2648 (cp_parser *);
2649
2650 /* Concept-related syntactic transformations */
2651
2652 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2653 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2654
2655 // -------------------------------------------------------------------------- //
2656 // Unevaluated Operand Guard
2657 //
2658 // Implementation of an RAII helper for unevaluated operand parsing.
2659 cp_unevaluated::cp_unevaluated ()
2660 {
2661 ++cp_unevaluated_operand;
2662 ++c_inhibit_evaluation_warnings;
2663 }
2664
2665 cp_unevaluated::~cp_unevaluated ()
2666 {
2667 --c_inhibit_evaluation_warnings;
2668 --cp_unevaluated_operand;
2669 }
2670
2671 // -------------------------------------------------------------------------- //
2672 // Tentative Parsing
2673
2674 /* Returns nonzero if we are parsing tentatively. */
2675
2676 static inline bool
2677 cp_parser_parsing_tentatively (cp_parser* parser)
2678 {
2679 return parser->context->next != NULL;
2680 }
2681
2682 /* Returns nonzero if TOKEN is a string literal. */
2683
2684 static bool
2685 cp_parser_is_pure_string_literal (cp_token* token)
2686 {
2687 return (token->type == CPP_STRING ||
2688 token->type == CPP_STRING16 ||
2689 token->type == CPP_STRING32 ||
2690 token->type == CPP_WSTRING ||
2691 token->type == CPP_UTF8STRING);
2692 }
2693
2694 /* Returns nonzero if TOKEN is a string literal
2695 of a user-defined string literal. */
2696
2697 static bool
2698 cp_parser_is_string_literal (cp_token* token)
2699 {
2700 return (cp_parser_is_pure_string_literal (token) ||
2701 token->type == CPP_STRING_USERDEF ||
2702 token->type == CPP_STRING16_USERDEF ||
2703 token->type == CPP_STRING32_USERDEF ||
2704 token->type == CPP_WSTRING_USERDEF ||
2705 token->type == CPP_UTF8STRING_USERDEF);
2706 }
2707
2708 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2709
2710 static bool
2711 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2712 {
2713 return token->keyword == keyword;
2714 }
2715
2716 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2717 PRAGMA_NONE. */
2718
2719 static enum pragma_kind
2720 cp_parser_pragma_kind (cp_token *token)
2721 {
2722 if (token->type != CPP_PRAGMA)
2723 return PRAGMA_NONE;
2724 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2725 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2726 }
2727
2728 /* Helper function for cp_parser_error.
2729 Having peeked a token of kind TOK1_KIND that might signify
2730 a conflict marker, peek successor tokens to determine
2731 if we actually do have a conflict marker.
2732 Specifically, we consider a run of 7 '<', '=' or '>' characters
2733 at the start of a line as a conflict marker.
2734 These come through the lexer as three pairs and a single,
2735 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2736 If it returns true, *OUT_LOC is written to with the location/range
2737 of the marker. */
2738
2739 static bool
2740 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2741 location_t *out_loc)
2742 {
2743 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2744 if (token2->type != tok1_kind)
2745 return false;
2746 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2747 if (token3->type != tok1_kind)
2748 return false;
2749 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2750 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2751 return false;
2752
2753 /* It must be at the start of the line. */
2754 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2755 if (LOCATION_COLUMN (start_loc) != 1)
2756 return false;
2757
2758 /* We have a conflict marker. Construct a location of the form:
2759 <<<<<<<
2760 ^~~~~~~
2761 with start == caret, finishing at the end of the marker. */
2762 location_t finish_loc = get_finish (token4->location);
2763 *out_loc = make_location (start_loc, start_loc, finish_loc);
2764
2765 return true;
2766 }
2767
2768 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2769 RT_CLOSE_PAREN. */
2770
2771 static const char *
2772 get_matching_symbol (required_token token_desc)
2773 {
2774 switch (token_desc)
2775 {
2776 default:
2777 gcc_unreachable ();
2778 return "";
2779 case RT_CLOSE_BRACE:
2780 return "{";
2781 case RT_CLOSE_PAREN:
2782 return "(";
2783 }
2784 }
2785
2786 /* Attempt to convert TOKEN_DESC from a required_token to an
2787 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2788
2789 static enum cpp_ttype
2790 get_required_cpp_ttype (required_token token_desc)
2791 {
2792 switch (token_desc)
2793 {
2794 case RT_SEMICOLON:
2795 return CPP_SEMICOLON;
2796 case RT_OPEN_PAREN:
2797 return CPP_OPEN_PAREN;
2798 case RT_CLOSE_BRACE:
2799 return CPP_CLOSE_BRACE;
2800 case RT_OPEN_BRACE:
2801 return CPP_OPEN_BRACE;
2802 case RT_CLOSE_SQUARE:
2803 return CPP_CLOSE_SQUARE;
2804 case RT_OPEN_SQUARE:
2805 return CPP_OPEN_SQUARE;
2806 case RT_COMMA:
2807 return CPP_COMMA;
2808 case RT_COLON:
2809 return CPP_COLON;
2810 case RT_CLOSE_PAREN:
2811 return CPP_CLOSE_PAREN;
2812
2813 default:
2814 /* Use CPP_EOF as a "no completions possible" code. */
2815 return CPP_EOF;
2816 }
2817 }
2818
2819
2820 /* Subroutine of cp_parser_error and cp_parser_required_error.
2821
2822 Issue a diagnostic of the form
2823 FILE:LINE: MESSAGE before TOKEN
2824 where TOKEN is the next token in the input stream. MESSAGE
2825 (specified by the caller) is usually of the form "expected
2826 OTHER-TOKEN".
2827
2828 This bypasses the check for tentative passing, and potentially
2829 adds material needed by cp_parser_required_error.
2830
2831 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2832 suggesting insertion of the missing token.
2833
2834 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2835 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2836 location. */
2837
2838 static void
2839 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2840 required_token missing_token_desc,
2841 location_t matching_location)
2842 {
2843 cp_token *token = cp_lexer_peek_token (parser->lexer);
2844 /* This diagnostic makes more sense if it is tagged to the line
2845 of the token we just peeked at. */
2846 cp_lexer_set_source_position_from_token (token);
2847
2848 if (token->type == CPP_PRAGMA)
2849 {
2850 error_at (token->location,
2851 "%<#pragma%> is not allowed here");
2852 cp_parser_skip_to_pragma_eol (parser, token);
2853 return;
2854 }
2855
2856 /* If this is actually a conflict marker, report it as such. */
2857 if (token->type == CPP_LSHIFT
2858 || token->type == CPP_RSHIFT
2859 || token->type == CPP_EQ_EQ)
2860 {
2861 location_t loc;
2862 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2863 {
2864 error_at (loc, "version control conflict marker in file");
2865 expanded_location token_exploc = expand_location (token->location);
2866 /* Consume tokens until the end of the source line. */
2867 while (1)
2868 {
2869 cp_lexer_consume_token (parser->lexer);
2870 cp_token *next = cp_lexer_peek_token (parser->lexer);
2871 if (next == NULL)
2872 break;
2873 expanded_location next_exploc = expand_location (next->location);
2874 if (next_exploc.file != token_exploc.file)
2875 break;
2876 if (next_exploc.line != token_exploc.line)
2877 break;
2878 }
2879 return;
2880 }
2881 }
2882
2883 gcc_rich_location richloc (input_location);
2884
2885 bool added_matching_location = false;
2886
2887 if (missing_token_desc != RT_NONE)
2888 {
2889 /* Potentially supply a fix-it hint, suggesting to add the
2890 missing token immediately after the *previous* token.
2891 This may move the primary location within richloc. */
2892 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2893 location_t prev_token_loc
2894 = cp_lexer_previous_token (parser->lexer)->location;
2895 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2896
2897 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2898 Attempt to consolidate diagnostics by printing it as a
2899 secondary range within the main diagnostic. */
2900 if (matching_location != UNKNOWN_LOCATION)
2901 added_matching_location
2902 = richloc.add_location_if_nearby (matching_location);
2903 }
2904
2905 /* Actually emit the error. */
2906 c_parse_error (gmsgid,
2907 /* Because c_parser_error does not understand
2908 CPP_KEYWORD, keywords are treated like
2909 identifiers. */
2910 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2911 token->u.value, token->flags, &richloc);
2912
2913 if (missing_token_desc != RT_NONE)
2914 {
2915 /* If we weren't able to consolidate matching_location, then
2916 print it as a secondary diagnostic. */
2917 if (matching_location != UNKNOWN_LOCATION
2918 && !added_matching_location)
2919 inform (matching_location, "to match this %qs",
2920 get_matching_symbol (missing_token_desc));
2921 }
2922 }
2923
2924 /* If not parsing tentatively, issue a diagnostic of the form
2925 FILE:LINE: MESSAGE before TOKEN
2926 where TOKEN is the next token in the input stream. MESSAGE
2927 (specified by the caller) is usually of the form "expected
2928 OTHER-TOKEN". */
2929
2930 static void
2931 cp_parser_error (cp_parser* parser, const char* gmsgid)
2932 {
2933 if (!cp_parser_simulate_error (parser))
2934 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2935 }
2936
2937 /* Issue an error about name-lookup failing. NAME is the
2938 IDENTIFIER_NODE DECL is the result of
2939 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2940 the thing that we hoped to find. */
2941
2942 static void
2943 cp_parser_name_lookup_error (cp_parser* parser,
2944 tree name,
2945 tree decl,
2946 name_lookup_error desired,
2947 location_t location)
2948 {
2949 /* If name lookup completely failed, tell the user that NAME was not
2950 declared. */
2951 if (decl == error_mark_node)
2952 {
2953 if (parser->scope && parser->scope != global_namespace)
2954 error_at (location, "%<%E::%E%> has not been declared",
2955 parser->scope, name);
2956 else if (parser->scope == global_namespace)
2957 error_at (location, "%<::%E%> has not been declared", name);
2958 else if (parser->object_scope
2959 && !CLASS_TYPE_P (parser->object_scope))
2960 error_at (location, "request for member %qE in non-class type %qT",
2961 name, parser->object_scope);
2962 else if (parser->object_scope)
2963 error_at (location, "%<%T::%E%> has not been declared",
2964 parser->object_scope, name);
2965 else
2966 error_at (location, "%qE has not been declared", name);
2967 }
2968 else if (parser->scope && parser->scope != global_namespace)
2969 {
2970 switch (desired)
2971 {
2972 case NLE_TYPE:
2973 error_at (location, "%<%E::%E%> is not a type",
2974 parser->scope, name);
2975 break;
2976 case NLE_CXX98:
2977 error_at (location, "%<%E::%E%> is not a class or namespace",
2978 parser->scope, name);
2979 break;
2980 case NLE_NOT_CXX98:
2981 error_at (location,
2982 "%<%E::%E%> is not a class, namespace, or enumeration",
2983 parser->scope, name);
2984 break;
2985 default:
2986 gcc_unreachable ();
2987
2988 }
2989 }
2990 else if (parser->scope == global_namespace)
2991 {
2992 switch (desired)
2993 {
2994 case NLE_TYPE:
2995 error_at (location, "%<::%E%> is not a type", name);
2996 break;
2997 case NLE_CXX98:
2998 error_at (location, "%<::%E%> is not a class or namespace", name);
2999 break;
3000 case NLE_NOT_CXX98:
3001 error_at (location,
3002 "%<::%E%> is not a class, namespace, or enumeration",
3003 name);
3004 break;
3005 default:
3006 gcc_unreachable ();
3007 }
3008 }
3009 else
3010 {
3011 switch (desired)
3012 {
3013 case NLE_TYPE:
3014 error_at (location, "%qE is not a type", name);
3015 break;
3016 case NLE_CXX98:
3017 error_at (location, "%qE is not a class or namespace", name);
3018 break;
3019 case NLE_NOT_CXX98:
3020 error_at (location,
3021 "%qE is not a class, namespace, or enumeration", name);
3022 break;
3023 default:
3024 gcc_unreachable ();
3025 }
3026 }
3027 }
3028
3029 /* If we are parsing tentatively, remember that an error has occurred
3030 during this tentative parse. Returns true if the error was
3031 simulated; false if a message should be issued by the caller. */
3032
3033 static bool
3034 cp_parser_simulate_error (cp_parser* parser)
3035 {
3036 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3037 {
3038 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3039 return true;
3040 }
3041 return false;
3042 }
3043
3044 /* This function is called when a type is defined. If type
3045 definitions are forbidden at this point, an error message is
3046 issued. */
3047
3048 static bool
3049 cp_parser_check_type_definition (cp_parser* parser)
3050 {
3051 /* If types are forbidden here, issue a message. */
3052 if (parser->type_definition_forbidden_message)
3053 {
3054 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3055 in the message need to be interpreted. */
3056 error (parser->type_definition_forbidden_message);
3057 return false;
3058 }
3059 return true;
3060 }
3061
3062 /* This function is called when the DECLARATOR is processed. The TYPE
3063 was a type defined in the decl-specifiers. If it is invalid to
3064 define a type in the decl-specifiers for DECLARATOR, an error is
3065 issued. TYPE_LOCATION is the location of TYPE and is used
3066 for error reporting. */
3067
3068 static void
3069 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3070 tree type, location_t type_location)
3071 {
3072 /* [dcl.fct] forbids type definitions in return types.
3073 Unfortunately, it's not easy to know whether or not we are
3074 processing a return type until after the fact. */
3075 while (declarator
3076 && (declarator->kind == cdk_pointer
3077 || declarator->kind == cdk_reference
3078 || declarator->kind == cdk_ptrmem))
3079 declarator = declarator->declarator;
3080 if (declarator
3081 && declarator->kind == cdk_function)
3082 {
3083 error_at (type_location,
3084 "new types may not be defined in a return type");
3085 inform (type_location,
3086 "(perhaps a semicolon is missing after the definition of %qT)",
3087 type);
3088 }
3089 }
3090
3091 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3092 "<" in any valid C++ program. If the next token is indeed "<",
3093 issue a message warning the user about what appears to be an
3094 invalid attempt to form a template-id. LOCATION is the location
3095 of the type-specifier (TYPE) */
3096
3097 static void
3098 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3099 tree type,
3100 enum tag_types tag_type,
3101 location_t location)
3102 {
3103 cp_token_position start = 0;
3104
3105 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3106 {
3107 if (TREE_CODE (type) == TYPE_DECL)
3108 type = TREE_TYPE (type);
3109 if (TYPE_P (type) && !template_placeholder_p (type))
3110 error_at (location, "%qT is not a template", type);
3111 else if (identifier_p (type))
3112 {
3113 if (tag_type != none_type)
3114 error_at (location, "%qE is not a class template", type);
3115 else
3116 error_at (location, "%qE is not a template", type);
3117 }
3118 else
3119 error_at (location, "invalid template-id");
3120 /* Remember the location of the invalid "<". */
3121 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3122 start = cp_lexer_token_position (parser->lexer, true);
3123 /* Consume the "<". */
3124 cp_lexer_consume_token (parser->lexer);
3125 /* Parse the template arguments. */
3126 cp_parser_enclosed_template_argument_list (parser);
3127 /* Permanently remove the invalid template arguments so that
3128 this error message is not issued again. */
3129 if (start)
3130 cp_lexer_purge_tokens_after (parser->lexer, start);
3131 }
3132 }
3133
3134 /* If parsing an integral constant-expression, issue an error message
3135 about the fact that THING appeared and return true. Otherwise,
3136 return false. In either case, set
3137 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3138
3139 static bool
3140 cp_parser_non_integral_constant_expression (cp_parser *parser,
3141 non_integral_constant thing)
3142 {
3143 parser->non_integral_constant_expression_p = true;
3144 if (parser->integral_constant_expression_p)
3145 {
3146 if (!parser->allow_non_integral_constant_expression_p)
3147 {
3148 const char *msg = NULL;
3149 switch (thing)
3150 {
3151 case NIC_FLOAT:
3152 pedwarn (input_location, OPT_Wpedantic,
3153 "ISO C++ forbids using a floating-point literal "
3154 "in a constant-expression");
3155 return true;
3156 case NIC_CAST:
3157 error ("a cast to a type other than an integral or "
3158 "enumeration type cannot appear in a "
3159 "constant-expression");
3160 return true;
3161 case NIC_TYPEID:
3162 error ("%<typeid%> operator "
3163 "cannot appear in a constant-expression");
3164 return true;
3165 case NIC_NCC:
3166 error ("non-constant compound literals "
3167 "cannot appear in a constant-expression");
3168 return true;
3169 case NIC_FUNC_CALL:
3170 error ("a function call "
3171 "cannot appear in a constant-expression");
3172 return true;
3173 case NIC_INC:
3174 error ("an increment "
3175 "cannot appear in a constant-expression");
3176 return true;
3177 case NIC_DEC:
3178 error ("an decrement "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_ARRAY_REF:
3182 error ("an array reference "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_ADDR_LABEL:
3186 error ("the address of a label "
3187 "cannot appear in a constant-expression");
3188 return true;
3189 case NIC_OVERLOADED:
3190 error ("calls to overloaded operators "
3191 "cannot appear in a constant-expression");
3192 return true;
3193 case NIC_ASSIGNMENT:
3194 error ("an assignment cannot appear in a constant-expression");
3195 return true;
3196 case NIC_COMMA:
3197 error ("a comma operator "
3198 "cannot appear in a constant-expression");
3199 return true;
3200 case NIC_CONSTRUCTOR:
3201 error ("a call to a constructor "
3202 "cannot appear in a constant-expression");
3203 return true;
3204 case NIC_TRANSACTION:
3205 error ("a transaction expression "
3206 "cannot appear in a constant-expression");
3207 return true;
3208 case NIC_THIS:
3209 msg = "this";
3210 break;
3211 case NIC_FUNC_NAME:
3212 msg = "__FUNCTION__";
3213 break;
3214 case NIC_PRETTY_FUNC:
3215 msg = "__PRETTY_FUNCTION__";
3216 break;
3217 case NIC_C99_FUNC:
3218 msg = "__func__";
3219 break;
3220 case NIC_VA_ARG:
3221 msg = "va_arg";
3222 break;
3223 case NIC_ARROW:
3224 msg = "->";
3225 break;
3226 case NIC_POINT:
3227 msg = ".";
3228 break;
3229 case NIC_STAR:
3230 msg = "*";
3231 break;
3232 case NIC_ADDR:
3233 msg = "&";
3234 break;
3235 case NIC_PREINCREMENT:
3236 msg = "++";
3237 break;
3238 case NIC_PREDECREMENT:
3239 msg = "--";
3240 break;
3241 case NIC_NEW:
3242 msg = "new";
3243 break;
3244 case NIC_DEL:
3245 msg = "delete";
3246 break;
3247 default:
3248 gcc_unreachable ();
3249 }
3250 if (msg)
3251 error ("%qs cannot appear in a constant-expression", msg);
3252 return true;
3253 }
3254 }
3255 return false;
3256 }
3257
3258 /* Emit a diagnostic for an invalid type name. This function commits
3259 to the current active tentative parse, if any. (Otherwise, the
3260 problematic construct might be encountered again later, resulting
3261 in duplicate error messages.) LOCATION is the location of ID. */
3262
3263 static void
3264 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3265 location_t location)
3266 {
3267 tree decl, ambiguous_decls;
3268 cp_parser_commit_to_tentative_parse (parser);
3269 /* Try to lookup the identifier. */
3270 decl = cp_parser_lookup_name (parser, id, none_type,
3271 /*is_template=*/false,
3272 /*is_namespace=*/false,
3273 /*check_dependency=*/true,
3274 &ambiguous_decls, location);
3275 if (ambiguous_decls)
3276 /* If the lookup was ambiguous, an error will already have
3277 been issued. */
3278 return;
3279 /* If the lookup found a template-name, it means that the user forgot
3280 to specify an argument list. Emit a useful error message. */
3281 if (DECL_TYPE_TEMPLATE_P (decl))
3282 {
3283 auto_diagnostic_group d;
3284 error_at (location,
3285 "invalid use of template-name %qE without an argument list",
3286 decl);
3287 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3288 inform (location, "class template argument deduction is only available "
3289 "with -std=c++17 or -std=gnu++17");
3290 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3291 }
3292 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3293 error_at (location, "invalid use of destructor %qD as a type", id);
3294 else if (TREE_CODE (decl) == TYPE_DECL)
3295 /* Something like 'unsigned A a;' */
3296 error_at (location, "invalid combination of multiple type-specifiers");
3297 else if (!parser->scope)
3298 {
3299 /* Issue an error message. */
3300 auto_diagnostic_group d;
3301 name_hint hint;
3302 if (TREE_CODE (id) == IDENTIFIER_NODE)
3303 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3304 if (hint)
3305 {
3306 gcc_rich_location richloc (location);
3307 richloc.add_fixit_replace (hint.suggestion ());
3308 error_at (&richloc,
3309 "%qE does not name a type; did you mean %qs?",
3310 id, hint.suggestion ());
3311 }
3312 else
3313 error_at (location, "%qE does not name a type", id);
3314 /* If we're in a template class, it's possible that the user was
3315 referring to a type from a base class. For example:
3316
3317 template <typename T> struct A { typedef T X; };
3318 template <typename T> struct B : public A<T> { X x; };
3319
3320 The user should have said "typename A<T>::X". */
3321 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3322 inform (location, "C++11 %<constexpr%> only available with "
3323 "-std=c++11 or -std=gnu++11");
3324 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3325 inform (location, "C++11 %<noexcept%> only available with "
3326 "-std=c++11 or -std=gnu++11");
3327 else if (cxx_dialect < cxx11
3328 && TREE_CODE (id) == IDENTIFIER_NODE
3329 && id_equal (id, "thread_local"))
3330 inform (location, "C++11 %<thread_local%> only available with "
3331 "-std=c++11 or -std=gnu++11");
3332 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3333 inform (location, "%<concept%> only available with -fconcepts");
3334 else if (processing_template_decl && current_class_type
3335 && TYPE_BINFO (current_class_type))
3336 {
3337 tree b;
3338
3339 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3340 b;
3341 b = TREE_CHAIN (b))
3342 {
3343 tree base_type = BINFO_TYPE (b);
3344 if (CLASS_TYPE_P (base_type)
3345 && dependent_type_p (base_type))
3346 {
3347 tree field;
3348 /* Go from a particular instantiation of the
3349 template (which will have an empty TYPE_FIELDs),
3350 to the main version. */
3351 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3352 for (field = TYPE_FIELDS (base_type);
3353 field;
3354 field = DECL_CHAIN (field))
3355 if (TREE_CODE (field) == TYPE_DECL
3356 && DECL_NAME (field) == id)
3357 {
3358 inform (location,
3359 "(perhaps %<typename %T::%E%> was intended)",
3360 BINFO_TYPE (b), id);
3361 break;
3362 }
3363 if (field)
3364 break;
3365 }
3366 }
3367 }
3368 }
3369 /* Here we diagnose qualified-ids where the scope is actually correct,
3370 but the identifier does not resolve to a valid type name. */
3371 else if (parser->scope != error_mark_node)
3372 {
3373 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3374 {
3375 auto_diagnostic_group d;
3376 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3377 error_at (location_of (id),
3378 "%qE in namespace %qE does not name a template type",
3379 id, parser->scope);
3380 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3381 error_at (location_of (id),
3382 "%qE in namespace %qE does not name a template type",
3383 TREE_OPERAND (id, 0), parser->scope);
3384 else
3385 error_at (location_of (id),
3386 "%qE in namespace %qE does not name a type",
3387 id, parser->scope);
3388 if (DECL_P (decl))
3389 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3390 else if (decl == error_mark_node)
3391 suggest_alternative_in_explicit_scope (location, id,
3392 parser->scope);
3393 }
3394 else if (CLASS_TYPE_P (parser->scope)
3395 && constructor_name_p (id, parser->scope))
3396 {
3397 /* A<T>::A<T>() */
3398 auto_diagnostic_group d;
3399 error_at (location, "%<%T::%E%> names the constructor, not"
3400 " the type", parser->scope, id);
3401 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3402 error_at (location, "and %qT has no template constructors",
3403 parser->scope);
3404 }
3405 else if (TYPE_P (parser->scope)
3406 && dependent_scope_p (parser->scope))
3407 {
3408 gcc_rich_location richloc (location);
3409 richloc.add_fixit_insert_before ("typename ");
3410 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3411 error_at (&richloc,
3412 "need %<typename%> before %<%T::%D::%E%> because "
3413 "%<%T::%D%> is a dependent scope",
3414 TYPE_CONTEXT (parser->scope),
3415 TYPENAME_TYPE_FULLNAME (parser->scope),
3416 id,
3417 TYPE_CONTEXT (parser->scope),
3418 TYPENAME_TYPE_FULLNAME (parser->scope));
3419 else
3420 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3421 "%qT is a dependent scope",
3422 parser->scope, id, parser->scope);
3423 }
3424 else if (TYPE_P (parser->scope))
3425 {
3426 auto_diagnostic_group d;
3427 if (!COMPLETE_TYPE_P (parser->scope))
3428 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3429 parser->scope);
3430 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3431 error_at (location_of (id),
3432 "%qE in %q#T does not name a template type",
3433 id, parser->scope);
3434 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3435 error_at (location_of (id),
3436 "%qE in %q#T does not name a template type",
3437 TREE_OPERAND (id, 0), parser->scope);
3438 else
3439 error_at (location_of (id),
3440 "%qE in %q#T does not name a type",
3441 id, parser->scope);
3442 if (DECL_P (decl))
3443 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3444 }
3445 else
3446 gcc_unreachable ();
3447 }
3448 }
3449
3450 /* Check for a common situation where a type-name should be present,
3451 but is not, and issue a sensible error message. Returns true if an
3452 invalid type-name was detected.
3453
3454 The situation handled by this function are variable declarations of the
3455 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3456 Usually, `ID' should name a type, but if we got here it means that it
3457 does not. We try to emit the best possible error message depending on
3458 how exactly the id-expression looks like. */
3459
3460 static bool
3461 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3462 {
3463 tree id;
3464 cp_token *token = cp_lexer_peek_token (parser->lexer);
3465
3466 /* Avoid duplicate error about ambiguous lookup. */
3467 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3468 {
3469 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3470 if (next->type == CPP_NAME && next->error_reported)
3471 goto out;
3472 }
3473
3474 cp_parser_parse_tentatively (parser);
3475 id = cp_parser_id_expression (parser,
3476 /*template_keyword_p=*/false,
3477 /*check_dependency_p=*/true,
3478 /*template_p=*/NULL,
3479 /*declarator_p=*/false,
3480 /*optional_p=*/false);
3481 /* If the next token is a (, this is a function with no explicit return
3482 type, i.e. constructor, destructor or conversion op. */
3483 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3484 || TREE_CODE (id) == TYPE_DECL)
3485 {
3486 cp_parser_abort_tentative_parse (parser);
3487 return false;
3488 }
3489 if (!cp_parser_parse_definitely (parser))
3490 return false;
3491
3492 /* Emit a diagnostic for the invalid type. */
3493 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3494 out:
3495 /* If we aren't in the middle of a declarator (i.e. in a
3496 parameter-declaration-clause), skip to the end of the declaration;
3497 there's no point in trying to process it. */
3498 if (!parser->in_declarator_p)
3499 cp_parser_skip_to_end_of_block_or_statement (parser);
3500 return true;
3501 }
3502
3503 /* Consume tokens up to, and including, the next non-nested closing `)'.
3504 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3505 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3506 found an unnested token of that type. */
3507
3508 static int
3509 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3510 bool recovering,
3511 cpp_ttype or_ttype,
3512 bool consume_paren)
3513 {
3514 unsigned paren_depth = 0;
3515 unsigned brace_depth = 0;
3516 unsigned square_depth = 0;
3517 unsigned condop_depth = 0;
3518
3519 if (recovering && or_ttype == CPP_EOF
3520 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3521 return 0;
3522
3523 while (true)
3524 {
3525 cp_token * token = cp_lexer_peek_token (parser->lexer);
3526
3527 /* Have we found what we're looking for before the closing paren? */
3528 if (token->type == or_ttype && or_ttype != CPP_EOF
3529 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3530 return -1;
3531
3532 switch (token->type)
3533 {
3534 case CPP_EOF:
3535 case CPP_PRAGMA_EOL:
3536 /* If we've run out of tokens, then there is no closing `)'. */
3537 return 0;
3538
3539 /* This is good for lambda expression capture-lists. */
3540 case CPP_OPEN_SQUARE:
3541 ++square_depth;
3542 break;
3543 case CPP_CLOSE_SQUARE:
3544 if (!square_depth--)
3545 return 0;
3546 break;
3547
3548 case CPP_SEMICOLON:
3549 /* This matches the processing in skip_to_end_of_statement. */
3550 if (!brace_depth)
3551 return 0;
3552 break;
3553
3554 case CPP_OPEN_BRACE:
3555 ++brace_depth;
3556 break;
3557 case CPP_CLOSE_BRACE:
3558 if (!brace_depth--)
3559 return 0;
3560 break;
3561
3562 case CPP_OPEN_PAREN:
3563 if (!brace_depth)
3564 ++paren_depth;
3565 break;
3566
3567 case CPP_CLOSE_PAREN:
3568 if (!brace_depth && !paren_depth--)
3569 {
3570 if (consume_paren)
3571 cp_lexer_consume_token (parser->lexer);
3572 return 1;
3573 }
3574 break;
3575
3576 case CPP_QUERY:
3577 if (!brace_depth && !paren_depth && !square_depth)
3578 ++condop_depth;
3579 break;
3580
3581 case CPP_COLON:
3582 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3583 condop_depth--;
3584 break;
3585
3586 default:
3587 break;
3588 }
3589
3590 /* Consume the token. */
3591 cp_lexer_consume_token (parser->lexer);
3592 }
3593 }
3594
3595 /* Consume tokens up to, and including, the next non-nested closing `)'.
3596 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3597 are doing error recovery. Returns -1 if OR_COMMA is true and we
3598 found an unnested token of that type. */
3599
3600 static int
3601 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3602 bool recovering,
3603 bool or_comma,
3604 bool consume_paren)
3605 {
3606 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3607 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3608 ttype, consume_paren);
3609 }
3610
3611 /* Consume tokens until we reach the end of the current statement.
3612 Normally, that will be just before consuming a `;'. However, if a
3613 non-nested `}' comes first, then we stop before consuming that. */
3614
3615 static void
3616 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3617 {
3618 unsigned nesting_depth = 0;
3619
3620 /* Unwind generic function template scope if necessary. */
3621 if (parser->fully_implicit_function_template_p)
3622 abort_fully_implicit_template (parser);
3623
3624 while (true)
3625 {
3626 cp_token *token = cp_lexer_peek_token (parser->lexer);
3627
3628 switch (token->type)
3629 {
3630 case CPP_EOF:
3631 case CPP_PRAGMA_EOL:
3632 /* If we've run out of tokens, stop. */
3633 return;
3634
3635 case CPP_SEMICOLON:
3636 /* If the next token is a `;', we have reached the end of the
3637 statement. */
3638 if (!nesting_depth)
3639 return;
3640 break;
3641
3642 case CPP_CLOSE_BRACE:
3643 /* If this is a non-nested '}', stop before consuming it.
3644 That way, when confronted with something like:
3645
3646 { 3 + }
3647
3648 we stop before consuming the closing '}', even though we
3649 have not yet reached a `;'. */
3650 if (nesting_depth == 0)
3651 return;
3652
3653 /* If it is the closing '}' for a block that we have
3654 scanned, stop -- but only after consuming the token.
3655 That way given:
3656
3657 void f g () { ... }
3658 typedef int I;
3659
3660 we will stop after the body of the erroneously declared
3661 function, but before consuming the following `typedef'
3662 declaration. */
3663 if (--nesting_depth == 0)
3664 {
3665 cp_lexer_consume_token (parser->lexer);
3666 return;
3667 }
3668 break;
3669
3670 case CPP_OPEN_BRACE:
3671 ++nesting_depth;
3672 break;
3673
3674 default:
3675 break;
3676 }
3677
3678 /* Consume the token. */
3679 cp_lexer_consume_token (parser->lexer);
3680 }
3681 }
3682
3683 /* This function is called at the end of a statement or declaration.
3684 If the next token is a semicolon, it is consumed; otherwise, error
3685 recovery is attempted. */
3686
3687 static void
3688 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3689 {
3690 /* Look for the trailing `;'. */
3691 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3692 {
3693 /* If there is additional (erroneous) input, skip to the end of
3694 the statement. */
3695 cp_parser_skip_to_end_of_statement (parser);
3696 /* If the next token is now a `;', consume it. */
3697 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3698 cp_lexer_consume_token (parser->lexer);
3699 }
3700 }
3701
3702 /* Skip tokens until we have consumed an entire block, or until we
3703 have consumed a non-nested `;'. */
3704
3705 static void
3706 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3707 {
3708 int nesting_depth = 0;
3709
3710 /* Unwind generic function template scope if necessary. */
3711 if (parser->fully_implicit_function_template_p)
3712 abort_fully_implicit_template (parser);
3713
3714 while (nesting_depth >= 0)
3715 {
3716 cp_token *token = cp_lexer_peek_token (parser->lexer);
3717
3718 switch (token->type)
3719 {
3720 case CPP_EOF:
3721 case CPP_PRAGMA_EOL:
3722 /* If we've run out of tokens, stop. */
3723 return;
3724
3725 case CPP_SEMICOLON:
3726 /* Stop if this is an unnested ';'. */
3727 if (!nesting_depth)
3728 nesting_depth = -1;
3729 break;
3730
3731 case CPP_CLOSE_BRACE:
3732 /* Stop if this is an unnested '}', or closes the outermost
3733 nesting level. */
3734 nesting_depth--;
3735 if (nesting_depth < 0)
3736 return;
3737 if (!nesting_depth)
3738 nesting_depth = -1;
3739 break;
3740
3741 case CPP_OPEN_BRACE:
3742 /* Nest. */
3743 nesting_depth++;
3744 break;
3745
3746 default:
3747 break;
3748 }
3749
3750 /* Consume the token. */
3751 cp_lexer_consume_token (parser->lexer);
3752 }
3753 }
3754
3755 /* Skip tokens until a non-nested closing curly brace is the next
3756 token, or there are no more tokens. Return true in the first case,
3757 false otherwise. */
3758
3759 static bool
3760 cp_parser_skip_to_closing_brace (cp_parser *parser)
3761 {
3762 unsigned nesting_depth = 0;
3763
3764 while (true)
3765 {
3766 cp_token *token = cp_lexer_peek_token (parser->lexer);
3767
3768 switch (token->type)
3769 {
3770 case CPP_EOF:
3771 case CPP_PRAGMA_EOL:
3772 /* If we've run out of tokens, stop. */
3773 return false;
3774
3775 case CPP_CLOSE_BRACE:
3776 /* If the next token is a non-nested `}', then we have reached
3777 the end of the current block. */
3778 if (nesting_depth-- == 0)
3779 return true;
3780 break;
3781
3782 case CPP_OPEN_BRACE:
3783 /* If it the next token is a `{', then we are entering a new
3784 block. Consume the entire block. */
3785 ++nesting_depth;
3786 break;
3787
3788 default:
3789 break;
3790 }
3791
3792 /* Consume the token. */
3793 cp_lexer_consume_token (parser->lexer);
3794 }
3795 }
3796
3797 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3798 parameter is the PRAGMA token, allowing us to purge the entire pragma
3799 sequence. */
3800
3801 static void
3802 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3803 {
3804 cp_token *token;
3805
3806 parser->lexer->in_pragma = false;
3807
3808 do
3809 token = cp_lexer_consume_token (parser->lexer);
3810 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3811
3812 /* Ensure that the pragma is not parsed again. */
3813 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3814 }
3815
3816 /* Require pragma end of line, resyncing with it as necessary. The
3817 arguments are as for cp_parser_skip_to_pragma_eol. */
3818
3819 static void
3820 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3821 {
3822 parser->lexer->in_pragma = false;
3823 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3824 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3825 }
3826
3827 /* This is a simple wrapper around make_typename_type. When the id is
3828 an unresolved identifier node, we can provide a superior diagnostic
3829 using cp_parser_diagnose_invalid_type_name. */
3830
3831 static tree
3832 cp_parser_make_typename_type (cp_parser *parser, tree id,
3833 location_t id_location)
3834 {
3835 tree result;
3836 if (identifier_p (id))
3837 {
3838 result = make_typename_type (parser->scope, id, typename_type,
3839 /*complain=*/tf_none);
3840 if (result == error_mark_node)
3841 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3842 return result;
3843 }
3844 return make_typename_type (parser->scope, id, typename_type, tf_error);
3845 }
3846
3847 /* This is a wrapper around the
3848 make_{pointer,ptrmem,reference}_declarator functions that decides
3849 which one to call based on the CODE and CLASS_TYPE arguments. The
3850 CODE argument should be one of the values returned by
3851 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3852 appertain to the pointer or reference. */
3853
3854 static cp_declarator *
3855 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3856 cp_cv_quals cv_qualifiers,
3857 cp_declarator *target,
3858 tree attributes)
3859 {
3860 if (code == ERROR_MARK || target == cp_error_declarator)
3861 return cp_error_declarator;
3862
3863 if (code == INDIRECT_REF)
3864 if (class_type == NULL_TREE)
3865 return make_pointer_declarator (cv_qualifiers, target, attributes);
3866 else
3867 return make_ptrmem_declarator (cv_qualifiers, class_type,
3868 target, attributes);
3869 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3870 return make_reference_declarator (cv_qualifiers, target,
3871 false, attributes);
3872 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3873 return make_reference_declarator (cv_qualifiers, target,
3874 true, attributes);
3875 gcc_unreachable ();
3876 }
3877
3878 /* Create a new C++ parser. */
3879
3880 static cp_parser *
3881 cp_parser_new (void)
3882 {
3883 cp_parser *parser;
3884 cp_lexer *lexer;
3885 unsigned i;
3886
3887 /* cp_lexer_new_main is called before doing GC allocation because
3888 cp_lexer_new_main might load a PCH file. */
3889 lexer = cp_lexer_new_main ();
3890
3891 /* Initialize the binops_by_token so that we can get the tree
3892 directly from the token. */
3893 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3894 binops_by_token[binops[i].token_type] = binops[i];
3895
3896 parser = ggc_cleared_alloc<cp_parser> ();
3897 parser->lexer = lexer;
3898 parser->context = cp_parser_context_new (NULL);
3899
3900 /* For now, we always accept GNU extensions. */
3901 parser->allow_gnu_extensions_p = 1;
3902
3903 /* The `>' token is a greater-than operator, not the end of a
3904 template-id. */
3905 parser->greater_than_is_operator_p = true;
3906
3907 parser->default_arg_ok_p = true;
3908
3909 /* We are not parsing a constant-expression. */
3910 parser->integral_constant_expression_p = false;
3911 parser->allow_non_integral_constant_expression_p = false;
3912 parser->non_integral_constant_expression_p = false;
3913
3914 /* Local variable names are not forbidden. */
3915 parser->local_variables_forbidden_p = false;
3916
3917 /* We are not processing an `extern "C"' declaration. */
3918 parser->in_unbraced_linkage_specification_p = false;
3919
3920 /* We are not processing a declarator. */
3921 parser->in_declarator_p = false;
3922
3923 /* We are not processing a template-argument-list. */
3924 parser->in_template_argument_list_p = false;
3925
3926 /* We are not in an iteration statement. */
3927 parser->in_statement = 0;
3928
3929 /* We are not in a switch statement. */
3930 parser->in_switch_statement_p = false;
3931
3932 /* We are not parsing a type-id inside an expression. */
3933 parser->in_type_id_in_expr_p = false;
3934
3935 /* Declarations aren't implicitly extern "C". */
3936 parser->implicit_extern_c = false;
3937
3938 /* String literals should be translated to the execution character set. */
3939 parser->translate_strings_p = true;
3940
3941 /* We are not parsing a function body. */
3942 parser->in_function_body = false;
3943
3944 /* We can correct until told otherwise. */
3945 parser->colon_corrects_to_scope_p = true;
3946
3947 /* The unparsed function queue is empty. */
3948 push_unparsed_function_queues (parser);
3949
3950 /* There are no classes being defined. */
3951 parser->num_classes_being_defined = 0;
3952
3953 /* No template parameters apply. */
3954 parser->num_template_parameter_lists = 0;
3955
3956 /* Special parsing data structures. */
3957 parser->omp_declare_simd = NULL;
3958 parser->oacc_routine = NULL;
3959
3960 /* Not declaring an implicit function template. */
3961 parser->auto_is_implicit_function_template_parm_p = false;
3962 parser->fully_implicit_function_template_p = false;
3963 parser->implicit_template_parms = 0;
3964 parser->implicit_template_scope = 0;
3965
3966 /* Allow constrained-type-specifiers. */
3967 parser->prevent_constrained_type_specifiers = 0;
3968
3969 /* We haven't yet seen an 'extern "C"'. */
3970 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3971
3972 return parser;
3973 }
3974
3975 /* Create a cp_lexer structure which will emit the tokens in CACHE
3976 and push it onto the parser's lexer stack. This is used for delayed
3977 parsing of in-class method bodies and default arguments, and should
3978 not be confused with tentative parsing. */
3979 static void
3980 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3981 {
3982 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3983 lexer->next = parser->lexer;
3984 parser->lexer = lexer;
3985
3986 /* Move the current source position to that of the first token in the
3987 new lexer. */
3988 cp_lexer_set_source_position_from_token (lexer->next_token);
3989 }
3990
3991 /* Pop the top lexer off the parser stack. This is never used for the
3992 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3993 static void
3994 cp_parser_pop_lexer (cp_parser *parser)
3995 {
3996 cp_lexer *lexer = parser->lexer;
3997 parser->lexer = lexer->next;
3998 cp_lexer_destroy (lexer);
3999
4000 /* Put the current source position back where it was before this
4001 lexer was pushed. */
4002 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4003 }
4004
4005 /* Lexical conventions [gram.lex] */
4006
4007 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4008 identifier. */
4009
4010 static cp_expr
4011 cp_parser_identifier (cp_parser* parser)
4012 {
4013 cp_token *token;
4014
4015 /* Look for the identifier. */
4016 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4017 /* Return the value. */
4018 if (token)
4019 return cp_expr (token->u.value, token->location);
4020 else
4021 return error_mark_node;
4022 }
4023
4024 /* Parse a sequence of adjacent string constants. Returns a
4025 TREE_STRING representing the combined, nul-terminated string
4026 constant. If TRANSLATE is true, translate the string to the
4027 execution character set. If WIDE_OK is true, a wide string is
4028 invalid here.
4029
4030 C++98 [lex.string] says that if a narrow string literal token is
4031 adjacent to a wide string literal token, the behavior is undefined.
4032 However, C99 6.4.5p4 says that this results in a wide string literal.
4033 We follow C99 here, for consistency with the C front end.
4034
4035 This code is largely lifted from lex_string() in c-lex.c.
4036
4037 FUTURE: ObjC++ will need to handle @-strings here. */
4038 static cp_expr
4039 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4040 bool lookup_udlit = true)
4041 {
4042 tree value;
4043 size_t count;
4044 struct obstack str_ob;
4045 cpp_string str, istr, *strs;
4046 cp_token *tok;
4047 enum cpp_ttype type, curr_type;
4048 int have_suffix_p = 0;
4049 tree string_tree;
4050 tree suffix_id = NULL_TREE;
4051 bool curr_tok_is_userdef_p = false;
4052
4053 tok = cp_lexer_peek_token (parser->lexer);
4054 if (!cp_parser_is_string_literal (tok))
4055 {
4056 cp_parser_error (parser, "expected string-literal");
4057 return error_mark_node;
4058 }
4059
4060 location_t loc = tok->location;
4061
4062 if (cpp_userdef_string_p (tok->type))
4063 {
4064 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4065 curr_type = cpp_userdef_string_remove_type (tok->type);
4066 curr_tok_is_userdef_p = true;
4067 }
4068 else
4069 {
4070 string_tree = tok->u.value;
4071 curr_type = tok->type;
4072 }
4073 type = curr_type;
4074
4075 /* Try to avoid the overhead of creating and destroying an obstack
4076 for the common case of just one string. */
4077 if (!cp_parser_is_string_literal
4078 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4079 {
4080 cp_lexer_consume_token (parser->lexer);
4081
4082 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4083 str.len = TREE_STRING_LENGTH (string_tree);
4084 count = 1;
4085
4086 if (curr_tok_is_userdef_p)
4087 {
4088 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4089 have_suffix_p = 1;
4090 curr_type = cpp_userdef_string_remove_type (tok->type);
4091 }
4092 else
4093 curr_type = tok->type;
4094
4095 strs = &str;
4096 }
4097 else
4098 {
4099 location_t last_tok_loc = tok->location;
4100 gcc_obstack_init (&str_ob);
4101 count = 0;
4102
4103 do
4104 {
4105 cp_lexer_consume_token (parser->lexer);
4106 count++;
4107 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4108 str.len = TREE_STRING_LENGTH (string_tree);
4109
4110 if (curr_tok_is_userdef_p)
4111 {
4112 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4113 if (have_suffix_p == 0)
4114 {
4115 suffix_id = curr_suffix_id;
4116 have_suffix_p = 1;
4117 }
4118 else if (have_suffix_p == 1
4119 && curr_suffix_id != suffix_id)
4120 {
4121 error ("inconsistent user-defined literal suffixes"
4122 " %qD and %qD in string literal",
4123 suffix_id, curr_suffix_id);
4124 have_suffix_p = -1;
4125 }
4126 curr_type = cpp_userdef_string_remove_type (tok->type);
4127 }
4128 else
4129 curr_type = tok->type;
4130
4131 if (type != curr_type)
4132 {
4133 if (type == CPP_STRING)
4134 type = curr_type;
4135 else if (curr_type != CPP_STRING)
4136 {
4137 rich_location rich_loc (line_table, tok->location);
4138 rich_loc.add_range (last_tok_loc);
4139 error_at (&rich_loc,
4140 "unsupported non-standard concatenation "
4141 "of string literals");
4142 }
4143 }
4144
4145 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4146
4147 last_tok_loc = tok->location;
4148
4149 tok = cp_lexer_peek_token (parser->lexer);
4150 if (cpp_userdef_string_p (tok->type))
4151 {
4152 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4153 curr_type = cpp_userdef_string_remove_type (tok->type);
4154 curr_tok_is_userdef_p = true;
4155 }
4156 else
4157 {
4158 string_tree = tok->u.value;
4159 curr_type = tok->type;
4160 curr_tok_is_userdef_p = false;
4161 }
4162 }
4163 while (cp_parser_is_string_literal (tok));
4164
4165 /* A string literal built by concatenation has its caret=start at
4166 the start of the initial string, and its finish at the finish of
4167 the final string literal. */
4168 loc = make_location (loc, loc, get_finish (last_tok_loc));
4169
4170 strs = (cpp_string *) obstack_finish (&str_ob);
4171 }
4172
4173 if (type != CPP_STRING && !wide_ok)
4174 {
4175 cp_parser_error (parser, "a wide string is invalid in this context");
4176 type = CPP_STRING;
4177 }
4178
4179 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4180 (parse_in, strs, count, &istr, type))
4181 {
4182 value = build_string (istr.len, (const char *)istr.text);
4183 free (CONST_CAST (unsigned char *, istr.text));
4184
4185 switch (type)
4186 {
4187 default:
4188 case CPP_STRING:
4189 case CPP_UTF8STRING:
4190 TREE_TYPE (value) = char_array_type_node;
4191 break;
4192 case CPP_STRING16:
4193 TREE_TYPE (value) = char16_array_type_node;
4194 break;
4195 case CPP_STRING32:
4196 TREE_TYPE (value) = char32_array_type_node;
4197 break;
4198 case CPP_WSTRING:
4199 TREE_TYPE (value) = wchar_array_type_node;
4200 break;
4201 }
4202
4203 value = fix_string_type (value);
4204
4205 if (have_suffix_p)
4206 {
4207 tree literal = build_userdef_literal (suffix_id, value,
4208 OT_NONE, NULL_TREE);
4209 if (lookup_udlit)
4210 value = cp_parser_userdef_string_literal (literal);
4211 else
4212 value = literal;
4213 }
4214 }
4215 else
4216 /* cpp_interpret_string has issued an error. */
4217 value = error_mark_node;
4218
4219 if (count > 1)
4220 obstack_free (&str_ob, 0);
4221
4222 return cp_expr (value, loc);
4223 }
4224
4225 /* Look up a literal operator with the name and the exact arguments. */
4226
4227 static tree
4228 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4229 {
4230 tree decl;
4231 decl = lookup_name (name);
4232 if (!decl || !is_overloaded_fn (decl))
4233 return error_mark_node;
4234
4235 for (lkp_iterator iter (decl); iter; ++iter)
4236 {
4237 unsigned int ix;
4238 bool found = true;
4239 tree fn = *iter;
4240 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4241 if (parmtypes != NULL_TREE)
4242 {
4243 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4244 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4245 {
4246 tree tparm = TREE_VALUE (parmtypes);
4247 tree targ = TREE_TYPE ((*args)[ix]);
4248 bool ptr = TYPE_PTR_P (tparm);
4249 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4250 if ((ptr || arr || !same_type_p (tparm, targ))
4251 && (!ptr || !arr
4252 || !same_type_p (TREE_TYPE (tparm),
4253 TREE_TYPE (targ))))
4254 found = false;
4255 }
4256 if (found
4257 && ix == vec_safe_length (args)
4258 /* May be this should be sufficient_parms_p instead,
4259 depending on how exactly should user-defined literals
4260 work in presence of default arguments on the literal
4261 operator parameters. */
4262 && parmtypes == void_list_node)
4263 return decl;
4264 }
4265 }
4266
4267 return error_mark_node;
4268 }
4269
4270 /* Parse a user-defined char constant. Returns a call to a user-defined
4271 literal operator taking the character as an argument. */
4272
4273 static cp_expr
4274 cp_parser_userdef_char_literal (cp_parser *parser)
4275 {
4276 cp_token *token = cp_lexer_consume_token (parser->lexer);
4277 tree literal = token->u.value;
4278 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4279 tree value = USERDEF_LITERAL_VALUE (literal);
4280 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4281 tree decl, result;
4282
4283 /* Build up a call to the user-defined operator */
4284 /* Lookup the name we got back from the id-expression. */
4285 vec<tree, va_gc> *args = make_tree_vector ();
4286 vec_safe_push (args, value);
4287 decl = lookup_literal_operator (name, args);
4288 if (!decl || decl == error_mark_node)
4289 {
4290 error ("unable to find character literal operator %qD with %qT argument",
4291 name, TREE_TYPE (value));
4292 release_tree_vector (args);
4293 return error_mark_node;
4294 }
4295 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4296 release_tree_vector (args);
4297 return result;
4298 }
4299
4300 /* A subroutine of cp_parser_userdef_numeric_literal to
4301 create a char... template parameter pack from a string node. */
4302
4303 static tree
4304 make_char_string_pack (tree value)
4305 {
4306 tree charvec;
4307 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4308 const char *str = TREE_STRING_POINTER (value);
4309 int i, len = TREE_STRING_LENGTH (value) - 1;
4310 tree argvec = make_tree_vec (1);
4311
4312 /* Fill in CHARVEC with all of the parameters. */
4313 charvec = make_tree_vec (len);
4314 for (i = 0; i < len; ++i)
4315 {
4316 unsigned char s[3] = { '\'', str[i], '\'' };
4317 cpp_string in = { 3, s };
4318 cpp_string out = { 0, 0 };
4319 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4320 return NULL_TREE;
4321 gcc_assert (out.len == 2);
4322 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4323 out.text[0]);
4324 }
4325
4326 /* Build the argument packs. */
4327 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4328
4329 TREE_VEC_ELT (argvec, 0) = argpack;
4330
4331 return argvec;
4332 }
4333
4334 /* A subroutine of cp_parser_userdef_numeric_literal to
4335 create a char... template parameter pack from a string node. */
4336
4337 static tree
4338 make_string_pack (tree value)
4339 {
4340 tree charvec;
4341 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4342 const unsigned char *str
4343 = (const unsigned char *) TREE_STRING_POINTER (value);
4344 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4345 int len = TREE_STRING_LENGTH (value) / sz - 1;
4346 tree argvec = make_tree_vec (2);
4347
4348 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4349 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4350
4351 /* First template parm is character type. */
4352 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4353
4354 /* Fill in CHARVEC with all of the parameters. */
4355 charvec = make_tree_vec (len);
4356 for (int i = 0; i < len; ++i)
4357 TREE_VEC_ELT (charvec, i)
4358 = double_int_to_tree (str_char_type_node,
4359 double_int::from_buffer (str + i * sz, sz));
4360
4361 /* Build the argument packs. */
4362 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4363
4364 TREE_VEC_ELT (argvec, 1) = argpack;
4365
4366 return argvec;
4367 }
4368
4369 /* Parse a user-defined numeric constant. returns a call to a user-defined
4370 literal operator. */
4371
4372 static cp_expr
4373 cp_parser_userdef_numeric_literal (cp_parser *parser)
4374 {
4375 cp_token *token = cp_lexer_consume_token (parser->lexer);
4376 tree literal = token->u.value;
4377 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4378 tree value = USERDEF_LITERAL_VALUE (literal);
4379 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4380 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4381 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4382 tree decl, result;
4383 vec<tree, va_gc> *args;
4384
4385 /* Look for a literal operator taking the exact type of numeric argument
4386 as the literal value. */
4387 args = make_tree_vector ();
4388 vec_safe_push (args, value);
4389 decl = lookup_literal_operator (name, args);
4390 if (decl && decl != error_mark_node)
4391 {
4392 result = finish_call_expr (decl, &args, false, true,
4393 tf_warning_or_error);
4394
4395 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4396 {
4397 warning_at (token->location, OPT_Woverflow,
4398 "integer literal exceeds range of %qT type",
4399 long_long_unsigned_type_node);
4400 }
4401 else
4402 {
4403 if (overflow > 0)
4404 warning_at (token->location, OPT_Woverflow,
4405 "floating literal exceeds range of %qT type",
4406 long_double_type_node);
4407 else if (overflow < 0)
4408 warning_at (token->location, OPT_Woverflow,
4409 "floating literal truncated to zero");
4410 }
4411
4412 release_tree_vector (args);
4413 return result;
4414 }
4415 release_tree_vector (args);
4416
4417 /* If the numeric argument didn't work, look for a raw literal
4418 operator taking a const char* argument consisting of the number
4419 in string format. */
4420 args = make_tree_vector ();
4421 vec_safe_push (args, num_string);
4422 decl = lookup_literal_operator (name, args);
4423 if (decl && decl != error_mark_node)
4424 {
4425 result = finish_call_expr (decl, &args, false, true,
4426 tf_warning_or_error);
4427 release_tree_vector (args);
4428 return result;
4429 }
4430 release_tree_vector (args);
4431
4432 /* If the raw literal didn't work, look for a non-type template
4433 function with parameter pack char.... Call the function with
4434 template parameter characters representing the number. */
4435 args = make_tree_vector ();
4436 decl = lookup_literal_operator (name, args);
4437 if (decl && decl != error_mark_node)
4438 {
4439 tree tmpl_args = make_char_string_pack (num_string);
4440 if (tmpl_args == NULL_TREE)
4441 {
4442 error ("failed to translate literal to execution character set %qT",
4443 num_string);
4444 return error_mark_node;
4445 }
4446 decl = lookup_template_function (decl, tmpl_args);
4447 result = finish_call_expr (decl, &args, false, true,
4448 tf_warning_or_error);
4449 release_tree_vector (args);
4450 return result;
4451 }
4452
4453 release_tree_vector (args);
4454
4455 /* In C++14 the standard library defines complex number suffixes that
4456 conflict with GNU extensions. Prefer them if <complex> is #included. */
4457 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4458 bool i14 = (cxx_dialect > cxx11
4459 && (id_equal (suffix_id, "i")
4460 || id_equal (suffix_id, "if")
4461 || id_equal (suffix_id, "il")));
4462 diagnostic_t kind = DK_ERROR;
4463 int opt = 0;
4464
4465 if (i14 && ext)
4466 {
4467 tree cxlit = lookup_qualified_name (std_node,
4468 get_identifier ("complex_literals"),
4469 0, false, false);
4470 if (cxlit == error_mark_node)
4471 {
4472 /* No <complex>, so pedwarn and use GNU semantics. */
4473 kind = DK_PEDWARN;
4474 opt = OPT_Wpedantic;
4475 }
4476 }
4477
4478 bool complained
4479 = emit_diagnostic (kind, input_location, opt,
4480 "unable to find numeric literal operator %qD", name);
4481
4482 if (!complained)
4483 /* Don't inform either. */;
4484 else if (i14)
4485 {
4486 inform (token->location, "add %<using namespace std::complex_literals%> "
4487 "(from <complex>) to enable the C++14 user-defined literal "
4488 "suffixes");
4489 if (ext)
4490 inform (token->location, "or use %<j%> instead of %<i%> for the "
4491 "GNU built-in suffix");
4492 }
4493 else if (!ext)
4494 inform (token->location, "use -fext-numeric-literals "
4495 "to enable more built-in suffixes");
4496
4497 if (kind == DK_ERROR)
4498 value = error_mark_node;
4499 else
4500 {
4501 /* Use the built-in semantics. */
4502 tree type;
4503 if (id_equal (suffix_id, "i"))
4504 {
4505 if (TREE_CODE (value) == INTEGER_CST)
4506 type = integer_type_node;
4507 else
4508 type = double_type_node;
4509 }
4510 else if (id_equal (suffix_id, "if"))
4511 type = float_type_node;
4512 else /* if (id_equal (suffix_id, "il")) */
4513 type = long_double_type_node;
4514
4515 value = build_complex (build_complex_type (type),
4516 fold_convert (type, integer_zero_node),
4517 fold_convert (type, value));
4518 }
4519
4520 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4521 /* Avoid repeated diagnostics. */
4522 token->u.value = value;
4523 return value;
4524 }
4525
4526 /* Parse a user-defined string constant. Returns a call to a user-defined
4527 literal operator taking a character pointer and the length of the string
4528 as arguments. */
4529
4530 static tree
4531 cp_parser_userdef_string_literal (tree literal)
4532 {
4533 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4534 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4535 tree value = USERDEF_LITERAL_VALUE (literal);
4536 int len = TREE_STRING_LENGTH (value)
4537 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4538 tree decl, result;
4539 vec<tree, va_gc> *args;
4540
4541 /* Build up a call to the user-defined operator. */
4542 /* Lookup the name we got back from the id-expression. */
4543 args = make_tree_vector ();
4544 vec_safe_push (args, value);
4545 vec_safe_push (args, build_int_cst (size_type_node, len));
4546 decl = lookup_literal_operator (name, args);
4547
4548 if (decl && decl != error_mark_node)
4549 {
4550 result = finish_call_expr (decl, &args, false, true,
4551 tf_warning_or_error);
4552 release_tree_vector (args);
4553 return result;
4554 }
4555 release_tree_vector (args);
4556
4557 /* Look for a template function with typename parameter CharT
4558 and parameter pack CharT... Call the function with
4559 template parameter characters representing the string. */
4560 args = make_tree_vector ();
4561 decl = lookup_literal_operator (name, args);
4562 if (decl && decl != error_mark_node)
4563 {
4564 tree tmpl_args = make_string_pack (value);
4565 decl = lookup_template_function (decl, tmpl_args);
4566 result = finish_call_expr (decl, &args, false, true,
4567 tf_warning_or_error);
4568 release_tree_vector (args);
4569 return result;
4570 }
4571 release_tree_vector (args);
4572
4573 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4574 name, TREE_TYPE (value), size_type_node);
4575 return error_mark_node;
4576 }
4577
4578
4579 /* Basic concepts [gram.basic] */
4580
4581 /* Parse a translation-unit.
4582
4583 translation-unit:
4584 declaration-seq [opt]
4585
4586 Returns TRUE if all went well. */
4587
4588 static bool
4589 cp_parser_translation_unit (cp_parser* parser)
4590 {
4591 /* The address of the first non-permanent object on the declarator
4592 obstack. */
4593 static void *declarator_obstack_base;
4594
4595 bool success;
4596
4597 /* Create the declarator obstack, if necessary. */
4598 if (!cp_error_declarator)
4599 {
4600 gcc_obstack_init (&declarator_obstack);
4601 /* Create the error declarator. */
4602 cp_error_declarator = make_declarator (cdk_error);
4603 /* Create the empty parameter list. */
4604 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4605 UNKNOWN_LOCATION);
4606 /* Remember where the base of the declarator obstack lies. */
4607 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4608 }
4609
4610 cp_parser_declaration_seq_opt (parser);
4611
4612 /* If there are no tokens left then all went well. */
4613 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4614 {
4615 /* Get rid of the token array; we don't need it any more. */
4616 cp_lexer_destroy (parser->lexer);
4617 parser->lexer = NULL;
4618
4619 /* This file might have been a context that's implicitly extern
4620 "C". If so, pop the lang context. (Only relevant for PCH.) */
4621 if (parser->implicit_extern_c)
4622 {
4623 pop_lang_context ();
4624 parser->implicit_extern_c = false;
4625 }
4626
4627 /* Finish up. */
4628 finish_translation_unit ();
4629
4630 success = true;
4631 }
4632 else
4633 {
4634 cp_parser_error (parser, "expected declaration");
4635 success = false;
4636 }
4637
4638 /* Make sure the declarator obstack was fully cleaned up. */
4639 gcc_assert (obstack_next_free (&declarator_obstack)
4640 == declarator_obstack_base);
4641
4642 /* All went well. */
4643 return success;
4644 }
4645
4646 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4647 decltype context. */
4648
4649 static inline tsubst_flags_t
4650 complain_flags (bool decltype_p)
4651 {
4652 tsubst_flags_t complain = tf_warning_or_error;
4653 if (decltype_p)
4654 complain |= tf_decltype;
4655 return complain;
4656 }
4657
4658 /* We're about to parse a collection of statements. If we're currently
4659 parsing tentatively, set up a firewall so that any nested
4660 cp_parser_commit_to_tentative_parse won't affect the current context. */
4661
4662 static cp_token_position
4663 cp_parser_start_tentative_firewall (cp_parser *parser)
4664 {
4665 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4666 return 0;
4667
4668 cp_parser_parse_tentatively (parser);
4669 cp_parser_commit_to_topmost_tentative_parse (parser);
4670 return cp_lexer_token_position (parser->lexer, false);
4671 }
4672
4673 /* We've finished parsing the collection of statements. Wrap up the
4674 firewall and replace the relevant tokens with the parsed form. */
4675
4676 static void
4677 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4678 tree expr)
4679 {
4680 if (!start)
4681 return;
4682
4683 /* Finish the firewall level. */
4684 cp_parser_parse_definitely (parser);
4685 /* And remember the result of the parse for when we try again. */
4686 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4687 token->type = CPP_PREPARSED_EXPR;
4688 token->u.value = expr;
4689 token->keyword = RID_MAX;
4690 cp_lexer_purge_tokens_after (parser->lexer, start);
4691 }
4692
4693 /* Like the above functions, but let the user modify the tokens. Used by
4694 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4695 later parses, so it makes sense to localize the effects of
4696 cp_parser_commit_to_tentative_parse. */
4697
4698 struct tentative_firewall
4699 {
4700 cp_parser *parser;
4701 bool set;
4702
4703 tentative_firewall (cp_parser *p): parser(p)
4704 {
4705 /* If we're currently parsing tentatively, start a committed level as a
4706 firewall and then an inner tentative parse. */
4707 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4708 {
4709 cp_parser_parse_tentatively (parser);
4710 cp_parser_commit_to_topmost_tentative_parse (parser);
4711 cp_parser_parse_tentatively (parser);
4712 }
4713 }
4714
4715 ~tentative_firewall()
4716 {
4717 if (set)
4718 {
4719 /* Finish the inner tentative parse and the firewall, propagating any
4720 uncommitted error state to the outer tentative parse. */
4721 bool err = cp_parser_error_occurred (parser);
4722 cp_parser_parse_definitely (parser);
4723 cp_parser_parse_definitely (parser);
4724 if (err)
4725 cp_parser_simulate_error (parser);
4726 }
4727 }
4728 };
4729
4730 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4731 This class is for tracking such a matching pair of symbols.
4732 In particular, it tracks the location of the first token,
4733 so that if the second token is missing, we can highlight the
4734 location of the first token when notifying the user about the
4735 problem. */
4736
4737 template <typename traits_t>
4738 class token_pair
4739 {
4740 public:
4741 /* token_pair's ctor. */
4742 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4743
4744 /* If the next token is the opening symbol for this pair, consume it and
4745 return true.
4746 Otherwise, issue an error and return false.
4747 In either case, record the location of the opening token. */
4748
4749 bool require_open (cp_parser *parser)
4750 {
4751 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4752 return cp_parser_require (parser, traits_t::open_token_type,
4753 traits_t::required_token_open);
4754 }
4755
4756 /* Consume the next token from PARSER, recording its location as
4757 that of the opening token within the pair. */
4758
4759 cp_token * consume_open (cp_parser *parser)
4760 {
4761 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4762 gcc_assert (tok->type == traits_t::open_token_type);
4763 m_open_loc = tok->location;
4764 return tok;
4765 }
4766
4767 /* If the next token is the closing symbol for this pair, consume it
4768 and return it.
4769 Otherwise, issue an error, highlighting the location of the
4770 corresponding opening token, and return NULL. */
4771
4772 cp_token *require_close (cp_parser *parser) const
4773 {
4774 return cp_parser_require (parser, traits_t::close_token_type,
4775 traits_t::required_token_close,
4776 m_open_loc);
4777 }
4778
4779 private:
4780 location_t m_open_loc;
4781 };
4782
4783 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4784
4785 struct matching_paren_traits
4786 {
4787 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4788 static const enum required_token required_token_open = RT_OPEN_PAREN;
4789 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4790 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4791 };
4792
4793 /* "matching_parens" is a token_pair<T> class for tracking matching
4794 pairs of parentheses. */
4795
4796 typedef token_pair<matching_paren_traits> matching_parens;
4797
4798 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4799
4800 struct matching_brace_traits
4801 {
4802 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4803 static const enum required_token required_token_open = RT_OPEN_BRACE;
4804 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4805 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4806 };
4807
4808 /* "matching_braces" is a token_pair<T> class for tracking matching
4809 pairs of braces. */
4810
4811 typedef token_pair<matching_brace_traits> matching_braces;
4812
4813
4814 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4815 enclosing parentheses. */
4816
4817 static cp_expr
4818 cp_parser_statement_expr (cp_parser *parser)
4819 {
4820 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4821
4822 /* Consume the '('. */
4823 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4824 matching_parens parens;
4825 parens.consume_open (parser);
4826 /* Start the statement-expression. */
4827 tree expr = begin_stmt_expr ();
4828 /* Parse the compound-statement. */
4829 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4830 /* Finish up. */
4831 expr = finish_stmt_expr (expr, false);
4832 /* Consume the ')'. */
4833 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4834 if (!parens.require_close (parser))
4835 cp_parser_skip_to_end_of_statement (parser);
4836
4837 cp_parser_end_tentative_firewall (parser, start, expr);
4838 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4839 return cp_expr (expr, combined_loc);
4840 }
4841
4842 /* Expressions [gram.expr] */
4843
4844 /* Parse a fold-operator.
4845
4846 fold-operator:
4847 - * / % ^ & | = < > << >>
4848 = -= *= /= %= ^= &= |= <<= >>=
4849 == != <= >= && || , .* ->*
4850
4851 This returns the tree code corresponding to the matched operator
4852 as an int. When the current token matches a compound assignment
4853 opertor, the resulting tree code is the negative value of the
4854 non-assignment operator. */
4855
4856 static int
4857 cp_parser_fold_operator (cp_token *token)
4858 {
4859 switch (token->type)
4860 {
4861 case CPP_PLUS: return PLUS_EXPR;
4862 case CPP_MINUS: return MINUS_EXPR;
4863 case CPP_MULT: return MULT_EXPR;
4864 case CPP_DIV: return TRUNC_DIV_EXPR;
4865 case CPP_MOD: return TRUNC_MOD_EXPR;
4866 case CPP_XOR: return BIT_XOR_EXPR;
4867 case CPP_AND: return BIT_AND_EXPR;
4868 case CPP_OR: return BIT_IOR_EXPR;
4869 case CPP_LSHIFT: return LSHIFT_EXPR;
4870 case CPP_RSHIFT: return RSHIFT_EXPR;
4871
4872 case CPP_EQ: return -NOP_EXPR;
4873 case CPP_PLUS_EQ: return -PLUS_EXPR;
4874 case CPP_MINUS_EQ: return -MINUS_EXPR;
4875 case CPP_MULT_EQ: return -MULT_EXPR;
4876 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4877 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4878 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4879 case CPP_AND_EQ: return -BIT_AND_EXPR;
4880 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4881 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4882 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4883
4884 case CPP_EQ_EQ: return EQ_EXPR;
4885 case CPP_NOT_EQ: return NE_EXPR;
4886 case CPP_LESS: return LT_EXPR;
4887 case CPP_GREATER: return GT_EXPR;
4888 case CPP_LESS_EQ: return LE_EXPR;
4889 case CPP_GREATER_EQ: return GE_EXPR;
4890
4891 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4892 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4893
4894 case CPP_COMMA: return COMPOUND_EXPR;
4895
4896 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4897 case CPP_DEREF_STAR: return MEMBER_REF;
4898
4899 default: return ERROR_MARK;
4900 }
4901 }
4902
4903 /* Returns true if CODE indicates a binary expression, which is not allowed in
4904 the LHS of a fold-expression. More codes will need to be added to use this
4905 function in other contexts. */
4906
4907 static bool
4908 is_binary_op (tree_code code)
4909 {
4910 switch (code)
4911 {
4912 case PLUS_EXPR:
4913 case POINTER_PLUS_EXPR:
4914 case MINUS_EXPR:
4915 case MULT_EXPR:
4916 case TRUNC_DIV_EXPR:
4917 case TRUNC_MOD_EXPR:
4918 case BIT_XOR_EXPR:
4919 case BIT_AND_EXPR:
4920 case BIT_IOR_EXPR:
4921 case LSHIFT_EXPR:
4922 case RSHIFT_EXPR:
4923
4924 case MODOP_EXPR:
4925
4926 case EQ_EXPR:
4927 case NE_EXPR:
4928 case LE_EXPR:
4929 case GE_EXPR:
4930 case LT_EXPR:
4931 case GT_EXPR:
4932
4933 case TRUTH_ANDIF_EXPR:
4934 case TRUTH_ORIF_EXPR:
4935
4936 case COMPOUND_EXPR:
4937
4938 case DOTSTAR_EXPR:
4939 case MEMBER_REF:
4940 return true;
4941
4942 default:
4943 return false;
4944 }
4945 }
4946
4947 /* If the next token is a suitable fold operator, consume it and return as
4948 the function above. */
4949
4950 static int
4951 cp_parser_fold_operator (cp_parser *parser)
4952 {
4953 cp_token* token = cp_lexer_peek_token (parser->lexer);
4954 int code = cp_parser_fold_operator (token);
4955 if (code != ERROR_MARK)
4956 cp_lexer_consume_token (parser->lexer);
4957 return code;
4958 }
4959
4960 /* Parse a fold-expression.
4961
4962 fold-expression:
4963 ( ... folding-operator cast-expression)
4964 ( cast-expression folding-operator ... )
4965 ( cast-expression folding operator ... folding-operator cast-expression)
4966
4967 Note that the '(' and ')' are matched in primary expression. */
4968
4969 static cp_expr
4970 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4971 {
4972 cp_id_kind pidk;
4973
4974 // Left fold.
4975 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4976 {
4977 cp_lexer_consume_token (parser->lexer);
4978 int op = cp_parser_fold_operator (parser);
4979 if (op == ERROR_MARK)
4980 {
4981 cp_parser_error (parser, "expected binary operator");
4982 return error_mark_node;
4983 }
4984
4985 tree expr = cp_parser_cast_expression (parser, false, false,
4986 false, &pidk);
4987 if (expr == error_mark_node)
4988 return error_mark_node;
4989 return finish_left_unary_fold_expr (expr, op);
4990 }
4991
4992 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4993 int op = cp_parser_fold_operator (parser);
4994 if (op == ERROR_MARK)
4995 {
4996 cp_parser_error (parser, "expected binary operator");
4997 return error_mark_node;
4998 }
4999
5000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5001 {
5002 cp_parser_error (parser, "expected ...");
5003 return error_mark_node;
5004 }
5005 cp_lexer_consume_token (parser->lexer);
5006
5007 /* The operands of a fold-expression are cast-expressions, so binary or
5008 conditional expressions are not allowed. We check this here to avoid
5009 tentative parsing. */
5010 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5011 /* OK, the expression was parenthesized. */;
5012 else if (is_binary_op (TREE_CODE (expr1)))
5013 error_at (location_of (expr1),
5014 "binary expression in operand of fold-expression");
5015 else if (TREE_CODE (expr1) == COND_EXPR
5016 || (REFERENCE_REF_P (expr1)
5017 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5018 error_at (location_of (expr1),
5019 "conditional expression in operand of fold-expression");
5020
5021 // Right fold.
5022 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5023 return finish_right_unary_fold_expr (expr1, op);
5024
5025 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5026 {
5027 cp_parser_error (parser, "mismatched operator in fold-expression");
5028 return error_mark_node;
5029 }
5030 cp_lexer_consume_token (parser->lexer);
5031
5032 // Binary left or right fold.
5033 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5034 if (expr2 == error_mark_node)
5035 return error_mark_node;
5036 return finish_binary_fold_expr (expr1, expr2, op);
5037 }
5038
5039 /* Parse a primary-expression.
5040
5041 primary-expression:
5042 literal
5043 this
5044 ( expression )
5045 id-expression
5046 lambda-expression (C++11)
5047
5048 GNU Extensions:
5049
5050 primary-expression:
5051 ( compound-statement )
5052 __builtin_va_arg ( assignment-expression , type-id )
5053 __builtin_offsetof ( type-id , offsetof-expression )
5054
5055 C++ Extensions:
5056 __has_nothrow_assign ( type-id )
5057 __has_nothrow_constructor ( type-id )
5058 __has_nothrow_copy ( type-id )
5059 __has_trivial_assign ( type-id )
5060 __has_trivial_constructor ( type-id )
5061 __has_trivial_copy ( type-id )
5062 __has_trivial_destructor ( type-id )
5063 __has_virtual_destructor ( type-id )
5064 __is_abstract ( type-id )
5065 __is_base_of ( type-id , type-id )
5066 __is_class ( type-id )
5067 __is_empty ( type-id )
5068 __is_enum ( type-id )
5069 __is_final ( type-id )
5070 __is_literal_type ( type-id )
5071 __is_pod ( type-id )
5072 __is_polymorphic ( type-id )
5073 __is_std_layout ( type-id )
5074 __is_trivial ( type-id )
5075 __is_union ( type-id )
5076
5077 Objective-C++ Extension:
5078
5079 primary-expression:
5080 objc-expression
5081
5082 literal:
5083 __null
5084
5085 ADDRESS_P is true iff this expression was immediately preceded by
5086 "&" and therefore might denote a pointer-to-member. CAST_P is true
5087 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5088 true iff this expression is a template argument.
5089
5090 Returns a representation of the expression. Upon return, *IDK
5091 indicates what kind of id-expression (if any) was present. */
5092
5093 static cp_expr
5094 cp_parser_primary_expression (cp_parser *parser,
5095 bool address_p,
5096 bool cast_p,
5097 bool template_arg_p,
5098 bool decltype_p,
5099 cp_id_kind *idk)
5100 {
5101 cp_token *token = NULL;
5102
5103 /* Assume the primary expression is not an id-expression. */
5104 *idk = CP_ID_KIND_NONE;
5105
5106 /* Peek at the next token. */
5107 token = cp_lexer_peek_token (parser->lexer);
5108 switch ((int) token->type)
5109 {
5110 /* literal:
5111 integer-literal
5112 character-literal
5113 floating-literal
5114 string-literal
5115 boolean-literal
5116 pointer-literal
5117 user-defined-literal */
5118 case CPP_CHAR:
5119 case CPP_CHAR16:
5120 case CPP_CHAR32:
5121 case CPP_WCHAR:
5122 case CPP_UTF8CHAR:
5123 case CPP_NUMBER:
5124 case CPP_PREPARSED_EXPR:
5125 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5126 return cp_parser_userdef_numeric_literal (parser);
5127 token = cp_lexer_consume_token (parser->lexer);
5128 if (TREE_CODE (token->u.value) == FIXED_CST)
5129 {
5130 error_at (token->location,
5131 "fixed-point types not supported in C++");
5132 return error_mark_node;
5133 }
5134 /* Floating-point literals are only allowed in an integral
5135 constant expression if they are cast to an integral or
5136 enumeration type. */
5137 if (TREE_CODE (token->u.value) == REAL_CST
5138 && parser->integral_constant_expression_p
5139 && pedantic)
5140 {
5141 /* CAST_P will be set even in invalid code like "int(2.7 +
5142 ...)". Therefore, we have to check that the next token
5143 is sure to end the cast. */
5144 if (cast_p)
5145 {
5146 cp_token *next_token;
5147
5148 next_token = cp_lexer_peek_token (parser->lexer);
5149 if (/* The comma at the end of an
5150 enumerator-definition. */
5151 next_token->type != CPP_COMMA
5152 /* The curly brace at the end of an enum-specifier. */
5153 && next_token->type != CPP_CLOSE_BRACE
5154 /* The end of a statement. */
5155 && next_token->type != CPP_SEMICOLON
5156 /* The end of the cast-expression. */
5157 && next_token->type != CPP_CLOSE_PAREN
5158 /* The end of an array bound. */
5159 && next_token->type != CPP_CLOSE_SQUARE
5160 /* The closing ">" in a template-argument-list. */
5161 && (next_token->type != CPP_GREATER
5162 || parser->greater_than_is_operator_p)
5163 /* C++0x only: A ">>" treated like two ">" tokens,
5164 in a template-argument-list. */
5165 && (next_token->type != CPP_RSHIFT
5166 || (cxx_dialect == cxx98)
5167 || parser->greater_than_is_operator_p))
5168 cast_p = false;
5169 }
5170
5171 /* If we are within a cast, then the constraint that the
5172 cast is to an integral or enumeration type will be
5173 checked at that point. If we are not within a cast, then
5174 this code is invalid. */
5175 if (!cast_p)
5176 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5177 }
5178 return cp_expr (token->u.value, token->location);
5179
5180 case CPP_CHAR_USERDEF:
5181 case CPP_CHAR16_USERDEF:
5182 case CPP_CHAR32_USERDEF:
5183 case CPP_WCHAR_USERDEF:
5184 case CPP_UTF8CHAR_USERDEF:
5185 return cp_parser_userdef_char_literal (parser);
5186
5187 case CPP_STRING:
5188 case CPP_STRING16:
5189 case CPP_STRING32:
5190 case CPP_WSTRING:
5191 case CPP_UTF8STRING:
5192 case CPP_STRING_USERDEF:
5193 case CPP_STRING16_USERDEF:
5194 case CPP_STRING32_USERDEF:
5195 case CPP_WSTRING_USERDEF:
5196 case CPP_UTF8STRING_USERDEF:
5197 /* ??? Should wide strings be allowed when parser->translate_strings_p
5198 is false (i.e. in attributes)? If not, we can kill the third
5199 argument to cp_parser_string_literal. */
5200 return cp_parser_string_literal (parser,
5201 parser->translate_strings_p,
5202 true);
5203
5204 case CPP_OPEN_PAREN:
5205 /* If we see `( { ' then we are looking at the beginning of
5206 a GNU statement-expression. */
5207 if (cp_parser_allow_gnu_extensions_p (parser)
5208 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5209 {
5210 /* Statement-expressions are not allowed by the standard. */
5211 pedwarn (token->location, OPT_Wpedantic,
5212 "ISO C++ forbids braced-groups within expressions");
5213
5214 /* And they're not allowed outside of a function-body; you
5215 cannot, for example, write:
5216
5217 int i = ({ int j = 3; j + 1; });
5218
5219 at class or namespace scope. */
5220 if (!parser->in_function_body
5221 || parser->in_template_argument_list_p)
5222 {
5223 error_at (token->location,
5224 "statement-expressions are not allowed outside "
5225 "functions nor in template-argument lists");
5226 cp_parser_skip_to_end_of_block_or_statement (parser);
5227 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5228 cp_lexer_consume_token (parser->lexer);
5229 return error_mark_node;
5230 }
5231 else
5232 return cp_parser_statement_expr (parser);
5233 }
5234 /* Otherwise it's a normal parenthesized expression. */
5235 {
5236 cp_expr expr;
5237 bool saved_greater_than_is_operator_p;
5238
5239 location_t open_paren_loc = token->location;
5240
5241 /* Consume the `('. */
5242 matching_parens parens;
5243 parens.consume_open (parser);
5244 /* Within a parenthesized expression, a `>' token is always
5245 the greater-than operator. */
5246 saved_greater_than_is_operator_p
5247 = parser->greater_than_is_operator_p;
5248 parser->greater_than_is_operator_p = true;
5249
5250 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5251 /* Left fold expression. */
5252 expr = NULL_TREE;
5253 else
5254 /* Parse the parenthesized expression. */
5255 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5256
5257 token = cp_lexer_peek_token (parser->lexer);
5258 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5259 {
5260 expr = cp_parser_fold_expression (parser, expr);
5261 if (expr != error_mark_node
5262 && cxx_dialect < cxx17
5263 && !in_system_header_at (input_location))
5264 pedwarn (input_location, 0, "fold-expressions only available "
5265 "with -std=c++17 or -std=gnu++17");
5266 }
5267 else
5268 /* Let the front end know that this expression was
5269 enclosed in parentheses. This matters in case, for
5270 example, the expression is of the form `A::B', since
5271 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5272 not. */
5273 expr = finish_parenthesized_expr (expr);
5274
5275 /* DR 705: Wrapping an unqualified name in parentheses
5276 suppresses arg-dependent lookup. We want to pass back
5277 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5278 (c++/37862), but none of the others. */
5279 if (*idk != CP_ID_KIND_QUALIFIED)
5280 *idk = CP_ID_KIND_NONE;
5281
5282 /* The `>' token might be the end of a template-id or
5283 template-parameter-list now. */
5284 parser->greater_than_is_operator_p
5285 = saved_greater_than_is_operator_p;
5286
5287 /* Consume the `)'. */
5288 token = cp_lexer_peek_token (parser->lexer);
5289 location_t close_paren_loc = token->location;
5290 expr.set_range (open_paren_loc, close_paren_loc);
5291 if (!parens.require_close (parser)
5292 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5293 cp_parser_skip_to_end_of_statement (parser);
5294
5295 return expr;
5296 }
5297
5298 case CPP_OPEN_SQUARE:
5299 {
5300 if (c_dialect_objc ())
5301 {
5302 /* We might have an Objective-C++ message. */
5303 cp_parser_parse_tentatively (parser);
5304 tree msg = cp_parser_objc_message_expression (parser);
5305 /* If that works out, we're done ... */
5306 if (cp_parser_parse_definitely (parser))
5307 return msg;
5308 /* ... else, fall though to see if it's a lambda. */
5309 }
5310 cp_expr lam = cp_parser_lambda_expression (parser);
5311 /* Don't warn about a failed tentative parse. */
5312 if (cp_parser_error_occurred (parser))
5313 return error_mark_node;
5314 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5315 return lam;
5316 }
5317
5318 case CPP_OBJC_STRING:
5319 if (c_dialect_objc ())
5320 /* We have an Objective-C++ string literal. */
5321 return cp_parser_objc_expression (parser);
5322 cp_parser_error (parser, "expected primary-expression");
5323 return error_mark_node;
5324
5325 case CPP_KEYWORD:
5326 switch (token->keyword)
5327 {
5328 /* These two are the boolean literals. */
5329 case RID_TRUE:
5330 cp_lexer_consume_token (parser->lexer);
5331 return cp_expr (boolean_true_node, token->location);
5332 case RID_FALSE:
5333 cp_lexer_consume_token (parser->lexer);
5334 return cp_expr (boolean_false_node, token->location);
5335
5336 /* The `__null' literal. */
5337 case RID_NULL:
5338 cp_lexer_consume_token (parser->lexer);
5339 return cp_expr (null_node, token->location);
5340
5341 /* The `nullptr' literal. */
5342 case RID_NULLPTR:
5343 cp_lexer_consume_token (parser->lexer);
5344 return cp_expr (nullptr_node, token->location);
5345
5346 /* Recognize the `this' keyword. */
5347 case RID_THIS:
5348 cp_lexer_consume_token (parser->lexer);
5349 if (parser->local_variables_forbidden_p)
5350 {
5351 error_at (token->location,
5352 "%<this%> may not be used in this context");
5353 return error_mark_node;
5354 }
5355 /* Pointers cannot appear in constant-expressions. */
5356 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5357 return error_mark_node;
5358 return cp_expr (finish_this_expr (), token->location);
5359
5360 /* The `operator' keyword can be the beginning of an
5361 id-expression. */
5362 case RID_OPERATOR:
5363 goto id_expression;
5364
5365 case RID_FUNCTION_NAME:
5366 case RID_PRETTY_FUNCTION_NAME:
5367 case RID_C99_FUNCTION_NAME:
5368 {
5369 non_integral_constant name;
5370
5371 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5372 __func__ are the names of variables -- but they are
5373 treated specially. Therefore, they are handled here,
5374 rather than relying on the generic id-expression logic
5375 below. Grammatically, these names are id-expressions.
5376
5377 Consume the token. */
5378 token = cp_lexer_consume_token (parser->lexer);
5379
5380 switch (token->keyword)
5381 {
5382 case RID_FUNCTION_NAME:
5383 name = NIC_FUNC_NAME;
5384 break;
5385 case RID_PRETTY_FUNCTION_NAME:
5386 name = NIC_PRETTY_FUNC;
5387 break;
5388 case RID_C99_FUNCTION_NAME:
5389 name = NIC_C99_FUNC;
5390 break;
5391 default:
5392 gcc_unreachable ();
5393 }
5394
5395 if (cp_parser_non_integral_constant_expression (parser, name))
5396 return error_mark_node;
5397
5398 /* Look up the name. */
5399 return finish_fname (token->u.value);
5400 }
5401
5402 case RID_VA_ARG:
5403 {
5404 tree expression;
5405 tree type;
5406 source_location type_location;
5407 location_t start_loc
5408 = cp_lexer_peek_token (parser->lexer)->location;
5409 /* The `__builtin_va_arg' construct is used to handle
5410 `va_arg'. Consume the `__builtin_va_arg' token. */
5411 cp_lexer_consume_token (parser->lexer);
5412 /* Look for the opening `('. */
5413 matching_parens parens;
5414 parens.require_open (parser);
5415 /* Now, parse the assignment-expression. */
5416 expression = cp_parser_assignment_expression (parser);
5417 /* Look for the `,'. */
5418 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5419 type_location = cp_lexer_peek_token (parser->lexer)->location;
5420 /* Parse the type-id. */
5421 {
5422 type_id_in_expr_sentinel s (parser);
5423 type = cp_parser_type_id (parser);
5424 }
5425 /* Look for the closing `)'. */
5426 location_t finish_loc
5427 = cp_lexer_peek_token (parser->lexer)->location;
5428 parens.require_close (parser);
5429 /* Using `va_arg' in a constant-expression is not
5430 allowed. */
5431 if (cp_parser_non_integral_constant_expression (parser,
5432 NIC_VA_ARG))
5433 return error_mark_node;
5434 /* Construct a location of the form:
5435 __builtin_va_arg (v, int)
5436 ~~~~~~~~~~~~~~~~~~~~~^~~~
5437 with the caret at the type, ranging from the start of the
5438 "__builtin_va_arg" token to the close paren. */
5439 location_t combined_loc
5440 = make_location (type_location, start_loc, finish_loc);
5441 return build_x_va_arg (combined_loc, expression, type);
5442 }
5443
5444 case RID_OFFSETOF:
5445 return cp_parser_builtin_offsetof (parser);
5446
5447 case RID_HAS_NOTHROW_ASSIGN:
5448 case RID_HAS_NOTHROW_CONSTRUCTOR:
5449 case RID_HAS_NOTHROW_COPY:
5450 case RID_HAS_TRIVIAL_ASSIGN:
5451 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5452 case RID_HAS_TRIVIAL_COPY:
5453 case RID_HAS_TRIVIAL_DESTRUCTOR:
5454 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5455 case RID_HAS_VIRTUAL_DESTRUCTOR:
5456 case RID_IS_ABSTRACT:
5457 case RID_IS_AGGREGATE:
5458 case RID_IS_BASE_OF:
5459 case RID_IS_CLASS:
5460 case RID_IS_EMPTY:
5461 case RID_IS_ENUM:
5462 case RID_IS_FINAL:
5463 case RID_IS_LITERAL_TYPE:
5464 case RID_IS_POD:
5465 case RID_IS_POLYMORPHIC:
5466 case RID_IS_SAME_AS:
5467 case RID_IS_STD_LAYOUT:
5468 case RID_IS_TRIVIAL:
5469 case RID_IS_TRIVIALLY_ASSIGNABLE:
5470 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5471 case RID_IS_TRIVIALLY_COPYABLE:
5472 case RID_IS_UNION:
5473 case RID_IS_ASSIGNABLE:
5474 case RID_IS_CONSTRUCTIBLE:
5475 return cp_parser_trait_expr (parser, token->keyword);
5476
5477 // C++ concepts
5478 case RID_REQUIRES:
5479 return cp_parser_requires_expression (parser);
5480
5481 /* Objective-C++ expressions. */
5482 case RID_AT_ENCODE:
5483 case RID_AT_PROTOCOL:
5484 case RID_AT_SELECTOR:
5485 return cp_parser_objc_expression (parser);
5486
5487 case RID_TEMPLATE:
5488 if (parser->in_function_body
5489 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5490 == CPP_LESS))
5491 {
5492 error_at (token->location,
5493 "a template declaration cannot appear at block scope");
5494 cp_parser_skip_to_end_of_block_or_statement (parser);
5495 return error_mark_node;
5496 }
5497 /* FALLTHRU */
5498 default:
5499 cp_parser_error (parser, "expected primary-expression");
5500 return error_mark_node;
5501 }
5502
5503 /* An id-expression can start with either an identifier, a
5504 `::' as the beginning of a qualified-id, or the "operator"
5505 keyword. */
5506 case CPP_NAME:
5507 case CPP_SCOPE:
5508 case CPP_TEMPLATE_ID:
5509 case CPP_NESTED_NAME_SPECIFIER:
5510 {
5511 id_expression:
5512 cp_expr id_expression;
5513 cp_expr decl;
5514 const char *error_msg;
5515 bool template_p;
5516 bool done;
5517 cp_token *id_expr_token;
5518
5519 /* Parse the id-expression. */
5520 id_expression
5521 = cp_parser_id_expression (parser,
5522 /*template_keyword_p=*/false,
5523 /*check_dependency_p=*/true,
5524 &template_p,
5525 /*declarator_p=*/false,
5526 /*optional_p=*/false);
5527 if (id_expression == error_mark_node)
5528 return error_mark_node;
5529 id_expr_token = token;
5530 token = cp_lexer_peek_token (parser->lexer);
5531 done = (token->type != CPP_OPEN_SQUARE
5532 && token->type != CPP_OPEN_PAREN
5533 && token->type != CPP_DOT
5534 && token->type != CPP_DEREF
5535 && token->type != CPP_PLUS_PLUS
5536 && token->type != CPP_MINUS_MINUS);
5537 /* If we have a template-id, then no further lookup is
5538 required. If the template-id was for a template-class, we
5539 will sometimes have a TYPE_DECL at this point. */
5540 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5541 || TREE_CODE (id_expression) == TYPE_DECL)
5542 decl = id_expression;
5543 /* Look up the name. */
5544 else
5545 {
5546 tree ambiguous_decls;
5547
5548 /* If we already know that this lookup is ambiguous, then
5549 we've already issued an error message; there's no reason
5550 to check again. */
5551 if (id_expr_token->type == CPP_NAME
5552 && id_expr_token->error_reported)
5553 {
5554 cp_parser_simulate_error (parser);
5555 return error_mark_node;
5556 }
5557
5558 decl = cp_parser_lookup_name (parser, id_expression,
5559 none_type,
5560 template_p,
5561 /*is_namespace=*/false,
5562 /*check_dependency=*/true,
5563 &ambiguous_decls,
5564 id_expr_token->location);
5565 /* If the lookup was ambiguous, an error will already have
5566 been issued. */
5567 if (ambiguous_decls)
5568 return error_mark_node;
5569
5570 /* In Objective-C++, we may have an Objective-C 2.0
5571 dot-syntax for classes here. */
5572 if (c_dialect_objc ()
5573 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5574 && TREE_CODE (decl) == TYPE_DECL
5575 && objc_is_class_name (decl))
5576 {
5577 tree component;
5578 cp_lexer_consume_token (parser->lexer);
5579 component = cp_parser_identifier (parser);
5580 if (component == error_mark_node)
5581 return error_mark_node;
5582
5583 tree result = objc_build_class_component_ref (id_expression,
5584 component);
5585 /* Build a location of the form:
5586 expr.component
5587 ~~~~~^~~~~~~~~
5588 with caret at the start of the component name (at
5589 input_location), ranging from the start of the id_expression
5590 to the end of the component name. */
5591 location_t combined_loc
5592 = make_location (input_location, id_expression.get_start (),
5593 get_finish (input_location));
5594 protected_set_expr_location (result, combined_loc);
5595 return result;
5596 }
5597
5598 /* In Objective-C++, an instance variable (ivar) may be preferred
5599 to whatever cp_parser_lookup_name() found.
5600 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5601 rest of c-family, we have to do a little extra work to preserve
5602 any location information in cp_expr "decl". Given that
5603 objc_lookup_ivar is implemented in "c-family" and "objc", we
5604 have a trip through the pure "tree" type, rather than cp_expr.
5605 Naively copying it back to "decl" would implicitly give the
5606 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5607 store an EXPR_LOCATION. Hence we only update "decl" (and
5608 hence its location_t) if we get back a different tree node. */
5609 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5610 id_expression);
5611 if (decl_tree != decl.get_value ())
5612 decl = cp_expr (decl_tree);
5613
5614 /* If name lookup gives us a SCOPE_REF, then the
5615 qualifying scope was dependent. */
5616 if (TREE_CODE (decl) == SCOPE_REF)
5617 {
5618 /* At this point, we do not know if DECL is a valid
5619 integral constant expression. We assume that it is
5620 in fact such an expression, so that code like:
5621
5622 template <int N> struct A {
5623 int a[B<N>::i];
5624 };
5625
5626 is accepted. At template-instantiation time, we
5627 will check that B<N>::i is actually a constant. */
5628 return decl;
5629 }
5630 /* Check to see if DECL is a local variable in a context
5631 where that is forbidden. */
5632 if (parser->local_variables_forbidden_p
5633 && local_variable_p (decl))
5634 {
5635 error_at (id_expr_token->location,
5636 "local variable %qD may not appear in this context",
5637 decl.get_value ());
5638 return error_mark_node;
5639 }
5640 }
5641
5642 if (processing_template_decl)
5643 if (tree fns = maybe_get_fns (decl))
5644 /* It's too difficult to mark ths in all the places where
5645 we know for sure we need to keep the lookup, so do it
5646 now. The cost is extra GC to recycle the lookups
5647 resolved at parse time. */
5648 lookup_keep (fns);
5649
5650 decl = (finish_id_expression
5651 (id_expression, decl, parser->scope,
5652 idk,
5653 parser->integral_constant_expression_p,
5654 parser->allow_non_integral_constant_expression_p,
5655 &parser->non_integral_constant_expression_p,
5656 template_p, done, address_p,
5657 template_arg_p,
5658 &error_msg,
5659 id_expression.get_location ()));
5660 if (error_msg)
5661 cp_parser_error (parser, error_msg);
5662 decl.set_location (id_expr_token->location);
5663 return decl;
5664 }
5665
5666 /* Anything else is an error. */
5667 default:
5668 cp_parser_error (parser, "expected primary-expression");
5669 return error_mark_node;
5670 }
5671 }
5672
5673 static inline cp_expr
5674 cp_parser_primary_expression (cp_parser *parser,
5675 bool address_p,
5676 bool cast_p,
5677 bool template_arg_p,
5678 cp_id_kind *idk)
5679 {
5680 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5681 /*decltype*/false, idk);
5682 }
5683
5684 /* Parse an id-expression.
5685
5686 id-expression:
5687 unqualified-id
5688 qualified-id
5689
5690 qualified-id:
5691 :: [opt] nested-name-specifier template [opt] unqualified-id
5692 :: identifier
5693 :: operator-function-id
5694 :: template-id
5695
5696 Return a representation of the unqualified portion of the
5697 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5698 a `::' or nested-name-specifier.
5699
5700 Often, if the id-expression was a qualified-id, the caller will
5701 want to make a SCOPE_REF to represent the qualified-id. This
5702 function does not do this in order to avoid wastefully creating
5703 SCOPE_REFs when they are not required.
5704
5705 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5706 `template' keyword.
5707
5708 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5709 uninstantiated templates.
5710
5711 If *TEMPLATE_P is non-NULL, it is set to true iff the
5712 `template' keyword is used to explicitly indicate that the entity
5713 named is a template.
5714
5715 If DECLARATOR_P is true, the id-expression is appearing as part of
5716 a declarator, rather than as part of an expression. */
5717
5718 static cp_expr
5719 cp_parser_id_expression (cp_parser *parser,
5720 bool template_keyword_p,
5721 bool check_dependency_p,
5722 bool *template_p,
5723 bool declarator_p,
5724 bool optional_p)
5725 {
5726 bool global_scope_p;
5727 bool nested_name_specifier_p;
5728
5729 /* Assume the `template' keyword was not used. */
5730 if (template_p)
5731 *template_p = template_keyword_p;
5732
5733 /* Look for the optional `::' operator. */
5734 global_scope_p
5735 = (!template_keyword_p
5736 && (cp_parser_global_scope_opt (parser,
5737 /*current_scope_valid_p=*/false)
5738 != NULL_TREE));
5739
5740 /* Look for the optional nested-name-specifier. */
5741 nested_name_specifier_p
5742 = (cp_parser_nested_name_specifier_opt (parser,
5743 /*typename_keyword_p=*/false,
5744 check_dependency_p,
5745 /*type_p=*/false,
5746 declarator_p,
5747 template_keyword_p)
5748 != NULL_TREE);
5749
5750 /* If there is a nested-name-specifier, then we are looking at
5751 the first qualified-id production. */
5752 if (nested_name_specifier_p)
5753 {
5754 tree saved_scope;
5755 tree saved_object_scope;
5756 tree saved_qualifying_scope;
5757 cp_expr unqualified_id;
5758 bool is_template;
5759
5760 /* See if the next token is the `template' keyword. */
5761 if (!template_p)
5762 template_p = &is_template;
5763 *template_p = cp_parser_optional_template_keyword (parser);
5764 /* Name lookup we do during the processing of the
5765 unqualified-id might obliterate SCOPE. */
5766 saved_scope = parser->scope;
5767 saved_object_scope = parser->object_scope;
5768 saved_qualifying_scope = parser->qualifying_scope;
5769 /* Process the final unqualified-id. */
5770 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5771 check_dependency_p,
5772 declarator_p,
5773 /*optional_p=*/false);
5774 /* Restore the SAVED_SCOPE for our caller. */
5775 parser->scope = saved_scope;
5776 parser->object_scope = saved_object_scope;
5777 parser->qualifying_scope = saved_qualifying_scope;
5778
5779 return unqualified_id;
5780 }
5781 /* Otherwise, if we are in global scope, then we are looking at one
5782 of the other qualified-id productions. */
5783 else if (global_scope_p)
5784 {
5785 cp_token *token;
5786 tree id;
5787
5788 /* Peek at the next token. */
5789 token = cp_lexer_peek_token (parser->lexer);
5790
5791 /* If it's an identifier, and the next token is not a "<", then
5792 we can avoid the template-id case. This is an optimization
5793 for this common case. */
5794 if (token->type == CPP_NAME
5795 && !cp_parser_nth_token_starts_template_argument_list_p
5796 (parser, 2))
5797 return cp_parser_identifier (parser);
5798
5799 cp_parser_parse_tentatively (parser);
5800 /* Try a template-id. */
5801 id = cp_parser_template_id (parser,
5802 /*template_keyword_p=*/false,
5803 /*check_dependency_p=*/true,
5804 none_type,
5805 declarator_p);
5806 /* If that worked, we're done. */
5807 if (cp_parser_parse_definitely (parser))
5808 return id;
5809
5810 /* Peek at the next token. (Changes in the token buffer may
5811 have invalidated the pointer obtained above.) */
5812 token = cp_lexer_peek_token (parser->lexer);
5813
5814 switch (token->type)
5815 {
5816 case CPP_NAME:
5817 return cp_parser_identifier (parser);
5818
5819 case CPP_KEYWORD:
5820 if (token->keyword == RID_OPERATOR)
5821 return cp_parser_operator_function_id (parser);
5822 /* Fall through. */
5823
5824 default:
5825 cp_parser_error (parser, "expected id-expression");
5826 return error_mark_node;
5827 }
5828 }
5829 else
5830 return cp_parser_unqualified_id (parser, template_keyword_p,
5831 /*check_dependency_p=*/true,
5832 declarator_p,
5833 optional_p);
5834 }
5835
5836 /* Parse an unqualified-id.
5837
5838 unqualified-id:
5839 identifier
5840 operator-function-id
5841 conversion-function-id
5842 ~ class-name
5843 template-id
5844
5845 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5846 keyword, in a construct like `A::template ...'.
5847
5848 Returns a representation of unqualified-id. For the `identifier'
5849 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5850 production a BIT_NOT_EXPR is returned; the operand of the
5851 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5852 other productions, see the documentation accompanying the
5853 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5854 names are looked up in uninstantiated templates. If DECLARATOR_P
5855 is true, the unqualified-id is appearing as part of a declarator,
5856 rather than as part of an expression. */
5857
5858 static cp_expr
5859 cp_parser_unqualified_id (cp_parser* parser,
5860 bool template_keyword_p,
5861 bool check_dependency_p,
5862 bool declarator_p,
5863 bool optional_p)
5864 {
5865 cp_token *token;
5866
5867 /* Peek at the next token. */
5868 token = cp_lexer_peek_token (parser->lexer);
5869
5870 switch ((int) token->type)
5871 {
5872 case CPP_NAME:
5873 {
5874 tree id;
5875
5876 /* We don't know yet whether or not this will be a
5877 template-id. */
5878 cp_parser_parse_tentatively (parser);
5879 /* Try a template-id. */
5880 id = cp_parser_template_id (parser, template_keyword_p,
5881 check_dependency_p,
5882 none_type,
5883 declarator_p);
5884 /* If it worked, we're done. */
5885 if (cp_parser_parse_definitely (parser))
5886 return id;
5887 /* Otherwise, it's an ordinary identifier. */
5888 return cp_parser_identifier (parser);
5889 }
5890
5891 case CPP_TEMPLATE_ID:
5892 return cp_parser_template_id (parser, template_keyword_p,
5893 check_dependency_p,
5894 none_type,
5895 declarator_p);
5896
5897 case CPP_COMPL:
5898 {
5899 tree type_decl;
5900 tree qualifying_scope;
5901 tree object_scope;
5902 tree scope;
5903 bool done;
5904
5905 /* Consume the `~' token. */
5906 cp_lexer_consume_token (parser->lexer);
5907 /* Parse the class-name. The standard, as written, seems to
5908 say that:
5909
5910 template <typename T> struct S { ~S (); };
5911 template <typename T> S<T>::~S() {}
5912
5913 is invalid, since `~' must be followed by a class-name, but
5914 `S<T>' is dependent, and so not known to be a class.
5915 That's not right; we need to look in uninstantiated
5916 templates. A further complication arises from:
5917
5918 template <typename T> void f(T t) {
5919 t.T::~T();
5920 }
5921
5922 Here, it is not possible to look up `T' in the scope of `T'
5923 itself. We must look in both the current scope, and the
5924 scope of the containing complete expression.
5925
5926 Yet another issue is:
5927
5928 struct S {
5929 int S;
5930 ~S();
5931 };
5932
5933 S::~S() {}
5934
5935 The standard does not seem to say that the `S' in `~S'
5936 should refer to the type `S' and not the data member
5937 `S::S'. */
5938
5939 /* DR 244 says that we look up the name after the "~" in the
5940 same scope as we looked up the qualifying name. That idea
5941 isn't fully worked out; it's more complicated than that. */
5942 scope = parser->scope;
5943 object_scope = parser->object_scope;
5944 qualifying_scope = parser->qualifying_scope;
5945
5946 /* Check for invalid scopes. */
5947 if (scope == error_mark_node)
5948 {
5949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5950 cp_lexer_consume_token (parser->lexer);
5951 return error_mark_node;
5952 }
5953 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5954 {
5955 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5956 error_at (token->location,
5957 "scope %qT before %<~%> is not a class-name",
5958 scope);
5959 cp_parser_simulate_error (parser);
5960 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5961 cp_lexer_consume_token (parser->lexer);
5962 return error_mark_node;
5963 }
5964 gcc_assert (!scope || TYPE_P (scope));
5965
5966 /* If the name is of the form "X::~X" it's OK even if X is a
5967 typedef. */
5968 token = cp_lexer_peek_token (parser->lexer);
5969 if (scope
5970 && token->type == CPP_NAME
5971 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5972 != CPP_LESS)
5973 && (token->u.value == TYPE_IDENTIFIER (scope)
5974 || (CLASS_TYPE_P (scope)
5975 && constructor_name_p (token->u.value, scope))))
5976 {
5977 cp_lexer_consume_token (parser->lexer);
5978 return build_nt (BIT_NOT_EXPR, scope);
5979 }
5980
5981 /* ~auto means the destructor of whatever the object is. */
5982 if (cp_parser_is_keyword (token, RID_AUTO))
5983 {
5984 if (cxx_dialect < cxx14)
5985 pedwarn (input_location, 0,
5986 "%<~auto%> only available with "
5987 "-std=c++14 or -std=gnu++14");
5988 cp_lexer_consume_token (parser->lexer);
5989 return build_nt (BIT_NOT_EXPR, make_auto ());
5990 }
5991
5992 /* If there was an explicit qualification (S::~T), first look
5993 in the scope given by the qualification (i.e., S).
5994
5995 Note: in the calls to cp_parser_class_name below we pass
5996 typename_type so that lookup finds the injected-class-name
5997 rather than the constructor. */
5998 done = false;
5999 type_decl = NULL_TREE;
6000 if (scope)
6001 {
6002 cp_parser_parse_tentatively (parser);
6003 type_decl = cp_parser_class_name (parser,
6004 /*typename_keyword_p=*/false,
6005 /*template_keyword_p=*/false,
6006 typename_type,
6007 /*check_dependency=*/false,
6008 /*class_head_p=*/false,
6009 declarator_p);
6010 if (cp_parser_parse_definitely (parser))
6011 done = true;
6012 }
6013 /* In "N::S::~S", look in "N" as well. */
6014 if (!done && scope && qualifying_scope)
6015 {
6016 cp_parser_parse_tentatively (parser);
6017 parser->scope = qualifying_scope;
6018 parser->object_scope = NULL_TREE;
6019 parser->qualifying_scope = NULL_TREE;
6020 type_decl
6021 = cp_parser_class_name (parser,
6022 /*typename_keyword_p=*/false,
6023 /*template_keyword_p=*/false,
6024 typename_type,
6025 /*check_dependency=*/false,
6026 /*class_head_p=*/false,
6027 declarator_p);
6028 if (cp_parser_parse_definitely (parser))
6029 done = true;
6030 }
6031 /* In "p->S::~T", look in the scope given by "*p" as well. */
6032 else if (!done && object_scope)
6033 {
6034 cp_parser_parse_tentatively (parser);
6035 parser->scope = object_scope;
6036 parser->object_scope = NULL_TREE;
6037 parser->qualifying_scope = NULL_TREE;
6038 type_decl
6039 = cp_parser_class_name (parser,
6040 /*typename_keyword_p=*/false,
6041 /*template_keyword_p=*/false,
6042 typename_type,
6043 /*check_dependency=*/false,
6044 /*class_head_p=*/false,
6045 declarator_p);
6046 if (cp_parser_parse_definitely (parser))
6047 done = true;
6048 }
6049 /* Look in the surrounding context. */
6050 if (!done)
6051 {
6052 parser->scope = NULL_TREE;
6053 parser->object_scope = NULL_TREE;
6054 parser->qualifying_scope = NULL_TREE;
6055 if (processing_template_decl)
6056 cp_parser_parse_tentatively (parser);
6057 type_decl
6058 = cp_parser_class_name (parser,
6059 /*typename_keyword_p=*/false,
6060 /*template_keyword_p=*/false,
6061 typename_type,
6062 /*check_dependency=*/false,
6063 /*class_head_p=*/false,
6064 declarator_p);
6065 if (processing_template_decl
6066 && ! cp_parser_parse_definitely (parser))
6067 {
6068 /* We couldn't find a type with this name. If we're parsing
6069 tentatively, fail and try something else. */
6070 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6071 {
6072 cp_parser_simulate_error (parser);
6073 return error_mark_node;
6074 }
6075 /* Otherwise, accept it and check for a match at instantiation
6076 time. */
6077 type_decl = cp_parser_identifier (parser);
6078 if (type_decl != error_mark_node)
6079 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6080 return type_decl;
6081 }
6082 }
6083 /* If an error occurred, assume that the name of the
6084 destructor is the same as the name of the qualifying
6085 class. That allows us to keep parsing after running
6086 into ill-formed destructor names. */
6087 if (type_decl == error_mark_node && scope)
6088 return build_nt (BIT_NOT_EXPR, scope);
6089 else if (type_decl == error_mark_node)
6090 return error_mark_node;
6091
6092 /* Check that destructor name and scope match. */
6093 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6094 {
6095 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6096 error_at (token->location,
6097 "declaration of %<~%T%> as member of %qT",
6098 type_decl, scope);
6099 cp_parser_simulate_error (parser);
6100 return error_mark_node;
6101 }
6102
6103 /* [class.dtor]
6104
6105 A typedef-name that names a class shall not be used as the
6106 identifier in the declarator for a destructor declaration. */
6107 if (declarator_p
6108 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6109 && !DECL_SELF_REFERENCE_P (type_decl)
6110 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6111 error_at (token->location,
6112 "typedef-name %qD used as destructor declarator",
6113 type_decl);
6114
6115 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6116 }
6117
6118 case CPP_KEYWORD:
6119 if (token->keyword == RID_OPERATOR)
6120 {
6121 cp_expr id;
6122
6123 /* This could be a template-id, so we try that first. */
6124 cp_parser_parse_tentatively (parser);
6125 /* Try a template-id. */
6126 id = cp_parser_template_id (parser, template_keyword_p,
6127 /*check_dependency_p=*/true,
6128 none_type,
6129 declarator_p);
6130 /* If that worked, we're done. */
6131 if (cp_parser_parse_definitely (parser))
6132 return id;
6133 /* We still don't know whether we're looking at an
6134 operator-function-id or a conversion-function-id. */
6135 cp_parser_parse_tentatively (parser);
6136 /* Try an operator-function-id. */
6137 id = cp_parser_operator_function_id (parser);
6138 /* If that didn't work, try a conversion-function-id. */
6139 if (!cp_parser_parse_definitely (parser))
6140 id = cp_parser_conversion_function_id (parser);
6141
6142 return id;
6143 }
6144 /* Fall through. */
6145
6146 default:
6147 if (optional_p)
6148 return NULL_TREE;
6149 cp_parser_error (parser, "expected unqualified-id");
6150 return error_mark_node;
6151 }
6152 }
6153
6154 /* Parse an (optional) nested-name-specifier.
6155
6156 nested-name-specifier: [C++98]
6157 class-or-namespace-name :: nested-name-specifier [opt]
6158 class-or-namespace-name :: template nested-name-specifier [opt]
6159
6160 nested-name-specifier: [C++0x]
6161 type-name ::
6162 namespace-name ::
6163 nested-name-specifier identifier ::
6164 nested-name-specifier template [opt] simple-template-id ::
6165
6166 PARSER->SCOPE should be set appropriately before this function is
6167 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6168 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6169 in name lookups.
6170
6171 Sets PARSER->SCOPE to the class (TYPE) or namespace
6172 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6173 it unchanged if there is no nested-name-specifier. Returns the new
6174 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6175
6176 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6177 part of a declaration and/or decl-specifier. */
6178
6179 static tree
6180 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6181 bool typename_keyword_p,
6182 bool check_dependency_p,
6183 bool type_p,
6184 bool is_declaration,
6185 bool template_keyword_p /* = false */)
6186 {
6187 bool success = false;
6188 cp_token_position start = 0;
6189 cp_token *token;
6190
6191 /* Remember where the nested-name-specifier starts. */
6192 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6193 {
6194 start = cp_lexer_token_position (parser->lexer, false);
6195 push_deferring_access_checks (dk_deferred);
6196 }
6197
6198 while (true)
6199 {
6200 tree new_scope;
6201 tree old_scope;
6202 tree saved_qualifying_scope;
6203
6204 /* Spot cases that cannot be the beginning of a
6205 nested-name-specifier. */
6206 token = cp_lexer_peek_token (parser->lexer);
6207
6208 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6209 the already parsed nested-name-specifier. */
6210 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6211 {
6212 /* Grab the nested-name-specifier and continue the loop. */
6213 cp_parser_pre_parsed_nested_name_specifier (parser);
6214 /* If we originally encountered this nested-name-specifier
6215 with IS_DECLARATION set to false, we will not have
6216 resolved TYPENAME_TYPEs, so we must do so here. */
6217 if (is_declaration
6218 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6219 {
6220 new_scope = resolve_typename_type (parser->scope,
6221 /*only_current_p=*/false);
6222 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6223 parser->scope = new_scope;
6224 }
6225 success = true;
6226 continue;
6227 }
6228
6229 /* Spot cases that cannot be the beginning of a
6230 nested-name-specifier. On the second and subsequent times
6231 through the loop, we look for the `template' keyword. */
6232 if (success && token->keyword == RID_TEMPLATE)
6233 ;
6234 /* A template-id can start a nested-name-specifier. */
6235 else if (token->type == CPP_TEMPLATE_ID)
6236 ;
6237 /* DR 743: decltype can be used in a nested-name-specifier. */
6238 else if (token_is_decltype (token))
6239 ;
6240 else
6241 {
6242 /* If the next token is not an identifier, then it is
6243 definitely not a type-name or namespace-name. */
6244 if (token->type != CPP_NAME)
6245 break;
6246 /* If the following token is neither a `<' (to begin a
6247 template-id), nor a `::', then we are not looking at a
6248 nested-name-specifier. */
6249 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6250
6251 if (token->type == CPP_COLON
6252 && parser->colon_corrects_to_scope_p
6253 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6254 {
6255 gcc_rich_location richloc (token->location);
6256 richloc.add_fixit_replace ("::");
6257 error_at (&richloc,
6258 "found %<:%> in nested-name-specifier, "
6259 "expected %<::%>");
6260 token->type = CPP_SCOPE;
6261 }
6262
6263 if (token->type != CPP_SCOPE
6264 && !cp_parser_nth_token_starts_template_argument_list_p
6265 (parser, 2))
6266 break;
6267 }
6268
6269 /* The nested-name-specifier is optional, so we parse
6270 tentatively. */
6271 cp_parser_parse_tentatively (parser);
6272
6273 /* Look for the optional `template' keyword, if this isn't the
6274 first time through the loop. */
6275 if (success)
6276 template_keyword_p = cp_parser_optional_template_keyword (parser);
6277
6278 /* Save the old scope since the name lookup we are about to do
6279 might destroy it. */
6280 old_scope = parser->scope;
6281 saved_qualifying_scope = parser->qualifying_scope;
6282 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6283 look up names in "X<T>::I" in order to determine that "Y" is
6284 a template. So, if we have a typename at this point, we make
6285 an effort to look through it. */
6286 if (is_declaration
6287 && !typename_keyword_p
6288 && parser->scope
6289 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6290 parser->scope = resolve_typename_type (parser->scope,
6291 /*only_current_p=*/false);
6292 /* Parse the qualifying entity. */
6293 new_scope
6294 = cp_parser_qualifying_entity (parser,
6295 typename_keyword_p,
6296 template_keyword_p,
6297 check_dependency_p,
6298 type_p,
6299 is_declaration);
6300 /* Look for the `::' token. */
6301 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6302
6303 /* If we found what we wanted, we keep going; otherwise, we're
6304 done. */
6305 if (!cp_parser_parse_definitely (parser))
6306 {
6307 bool error_p = false;
6308
6309 /* Restore the OLD_SCOPE since it was valid before the
6310 failed attempt at finding the last
6311 class-or-namespace-name. */
6312 parser->scope = old_scope;
6313 parser->qualifying_scope = saved_qualifying_scope;
6314
6315 /* If the next token is a decltype, and the one after that is a
6316 `::', then the decltype has failed to resolve to a class or
6317 enumeration type. Give this error even when parsing
6318 tentatively since it can't possibly be valid--and we're going
6319 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6320 won't get another chance.*/
6321 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6322 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6323 == CPP_SCOPE))
6324 {
6325 token = cp_lexer_consume_token (parser->lexer);
6326 error_at (token->location, "decltype evaluates to %qT, "
6327 "which is not a class or enumeration type",
6328 token->u.tree_check_value->value);
6329 parser->scope = error_mark_node;
6330 error_p = true;
6331 /* As below. */
6332 success = true;
6333 cp_lexer_consume_token (parser->lexer);
6334 }
6335
6336 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6337 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6338 {
6339 /* If we have a non-type template-id followed by ::, it can't
6340 possibly be valid. */
6341 token = cp_lexer_peek_token (parser->lexer);
6342 tree tid = token->u.tree_check_value->value;
6343 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6344 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6345 {
6346 tree tmpl = NULL_TREE;
6347 if (is_overloaded_fn (tid))
6348 {
6349 tree fns = get_fns (tid);
6350 if (OVL_SINGLE_P (fns))
6351 tmpl = OVL_FIRST (fns);
6352 error_at (token->location, "function template-id %qD "
6353 "in nested-name-specifier", tid);
6354 }
6355 else
6356 {
6357 /* Variable template. */
6358 tmpl = TREE_OPERAND (tid, 0);
6359 gcc_assert (variable_template_p (tmpl));
6360 error_at (token->location, "variable template-id %qD "
6361 "in nested-name-specifier", tid);
6362 }
6363 if (tmpl)
6364 inform (DECL_SOURCE_LOCATION (tmpl),
6365 "%qD declared here", tmpl);
6366
6367 parser->scope = error_mark_node;
6368 error_p = true;
6369 /* As below. */
6370 success = true;
6371 cp_lexer_consume_token (parser->lexer);
6372 cp_lexer_consume_token (parser->lexer);
6373 }
6374 }
6375
6376 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6377 break;
6378 /* If the next token is an identifier, and the one after
6379 that is a `::', then any valid interpretation would have
6380 found a class-or-namespace-name. */
6381 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6382 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6383 == CPP_SCOPE)
6384 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6385 != CPP_COMPL))
6386 {
6387 token = cp_lexer_consume_token (parser->lexer);
6388 if (!error_p)
6389 {
6390 if (!token->error_reported)
6391 {
6392 tree decl;
6393 tree ambiguous_decls;
6394
6395 decl = cp_parser_lookup_name (parser, token->u.value,
6396 none_type,
6397 /*is_template=*/false,
6398 /*is_namespace=*/false,
6399 /*check_dependency=*/true,
6400 &ambiguous_decls,
6401 token->location);
6402 if (TREE_CODE (decl) == TEMPLATE_DECL)
6403 error_at (token->location,
6404 "%qD used without template arguments",
6405 decl);
6406 else if (ambiguous_decls)
6407 {
6408 // cp_parser_lookup_name has the same diagnostic,
6409 // thus make sure to emit it at most once.
6410 if (cp_parser_uncommitted_to_tentative_parse_p
6411 (parser))
6412 {
6413 error_at (token->location,
6414 "reference to %qD is ambiguous",
6415 token->u.value);
6416 print_candidates (ambiguous_decls);
6417 }
6418 decl = error_mark_node;
6419 }
6420 else
6421 {
6422 if (cxx_dialect != cxx98)
6423 cp_parser_name_lookup_error
6424 (parser, token->u.value, decl, NLE_NOT_CXX98,
6425 token->location);
6426 else
6427 cp_parser_name_lookup_error
6428 (parser, token->u.value, decl, NLE_CXX98,
6429 token->location);
6430 }
6431 }
6432 parser->scope = error_mark_node;
6433 error_p = true;
6434 /* Treat this as a successful nested-name-specifier
6435 due to:
6436
6437 [basic.lookup.qual]
6438
6439 If the name found is not a class-name (clause
6440 _class_) or namespace-name (_namespace.def_), the
6441 program is ill-formed. */
6442 success = true;
6443 }
6444 cp_lexer_consume_token (parser->lexer);
6445 }
6446 break;
6447 }
6448 /* We've found one valid nested-name-specifier. */
6449 success = true;
6450 /* Name lookup always gives us a DECL. */
6451 if (TREE_CODE (new_scope) == TYPE_DECL)
6452 new_scope = TREE_TYPE (new_scope);
6453 /* Uses of "template" must be followed by actual templates. */
6454 if (template_keyword_p
6455 && !(CLASS_TYPE_P (new_scope)
6456 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6457 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6458 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6459 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6460 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6461 == TEMPLATE_ID_EXPR)))
6462 permerror (input_location, TYPE_P (new_scope)
6463 ? G_("%qT is not a template")
6464 : G_("%qD is not a template"),
6465 new_scope);
6466 /* If it is a class scope, try to complete it; we are about to
6467 be looking up names inside the class. */
6468 if (TYPE_P (new_scope)
6469 /* Since checking types for dependency can be expensive,
6470 avoid doing it if the type is already complete. */
6471 && !COMPLETE_TYPE_P (new_scope)
6472 /* Do not try to complete dependent types. */
6473 && !dependent_type_p (new_scope))
6474 {
6475 new_scope = complete_type (new_scope);
6476 /* If it is a typedef to current class, use the current
6477 class instead, as the typedef won't have any names inside
6478 it yet. */
6479 if (!COMPLETE_TYPE_P (new_scope)
6480 && currently_open_class (new_scope))
6481 new_scope = TYPE_MAIN_VARIANT (new_scope);
6482 }
6483 /* Make sure we look in the right scope the next time through
6484 the loop. */
6485 parser->scope = new_scope;
6486 }
6487
6488 /* If parsing tentatively, replace the sequence of tokens that makes
6489 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6490 token. That way, should we re-parse the token stream, we will
6491 not have to repeat the effort required to do the parse, nor will
6492 we issue duplicate error messages. */
6493 if (success && start)
6494 {
6495 cp_token *token;
6496
6497 token = cp_lexer_token_at (parser->lexer, start);
6498 /* Reset the contents of the START token. */
6499 token->type = CPP_NESTED_NAME_SPECIFIER;
6500 /* Retrieve any deferred checks. Do not pop this access checks yet
6501 so the memory will not be reclaimed during token replacing below. */
6502 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6503 token->u.tree_check_value->value = parser->scope;
6504 token->u.tree_check_value->checks = get_deferred_access_checks ();
6505 token->u.tree_check_value->qualifying_scope =
6506 parser->qualifying_scope;
6507 token->keyword = RID_MAX;
6508
6509 /* Purge all subsequent tokens. */
6510 cp_lexer_purge_tokens_after (parser->lexer, start);
6511 }
6512
6513 if (start)
6514 pop_to_parent_deferring_access_checks ();
6515
6516 return success ? parser->scope : NULL_TREE;
6517 }
6518
6519 /* Parse a nested-name-specifier. See
6520 cp_parser_nested_name_specifier_opt for details. This function
6521 behaves identically, except that it will an issue an error if no
6522 nested-name-specifier is present. */
6523
6524 static tree
6525 cp_parser_nested_name_specifier (cp_parser *parser,
6526 bool typename_keyword_p,
6527 bool check_dependency_p,
6528 bool type_p,
6529 bool is_declaration)
6530 {
6531 tree scope;
6532
6533 /* Look for the nested-name-specifier. */
6534 scope = cp_parser_nested_name_specifier_opt (parser,
6535 typename_keyword_p,
6536 check_dependency_p,
6537 type_p,
6538 is_declaration);
6539 /* If it was not present, issue an error message. */
6540 if (!scope)
6541 {
6542 cp_parser_error (parser, "expected nested-name-specifier");
6543 parser->scope = NULL_TREE;
6544 }
6545
6546 return scope;
6547 }
6548
6549 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6550 this is either a class-name or a namespace-name (which corresponds
6551 to the class-or-namespace-name production in the grammar). For
6552 C++0x, it can also be a type-name that refers to an enumeration
6553 type or a simple-template-id.
6554
6555 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6556 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6557 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6558 TYPE_P is TRUE iff the next name should be taken as a class-name,
6559 even the same name is declared to be another entity in the same
6560 scope.
6561
6562 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6563 specified by the class-or-namespace-name. If neither is found the
6564 ERROR_MARK_NODE is returned. */
6565
6566 static tree
6567 cp_parser_qualifying_entity (cp_parser *parser,
6568 bool typename_keyword_p,
6569 bool template_keyword_p,
6570 bool check_dependency_p,
6571 bool type_p,
6572 bool is_declaration)
6573 {
6574 tree saved_scope;
6575 tree saved_qualifying_scope;
6576 tree saved_object_scope;
6577 tree scope;
6578 bool only_class_p;
6579 bool successful_parse_p;
6580
6581 /* DR 743: decltype can appear in a nested-name-specifier. */
6582 if (cp_lexer_next_token_is_decltype (parser->lexer))
6583 {
6584 scope = cp_parser_decltype (parser);
6585 if (TREE_CODE (scope) != ENUMERAL_TYPE
6586 && !MAYBE_CLASS_TYPE_P (scope))
6587 {
6588 cp_parser_simulate_error (parser);
6589 return error_mark_node;
6590 }
6591 if (TYPE_NAME (scope))
6592 scope = TYPE_NAME (scope);
6593 return scope;
6594 }
6595
6596 /* Before we try to parse the class-name, we must save away the
6597 current PARSER->SCOPE since cp_parser_class_name will destroy
6598 it. */
6599 saved_scope = parser->scope;
6600 saved_qualifying_scope = parser->qualifying_scope;
6601 saved_object_scope = parser->object_scope;
6602 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6603 there is no need to look for a namespace-name. */
6604 only_class_p = template_keyword_p
6605 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6606 if (!only_class_p)
6607 cp_parser_parse_tentatively (parser);
6608 scope = cp_parser_class_name (parser,
6609 typename_keyword_p,
6610 template_keyword_p,
6611 type_p ? class_type : none_type,
6612 check_dependency_p,
6613 /*class_head_p=*/false,
6614 is_declaration,
6615 /*enum_ok=*/cxx_dialect > cxx98);
6616 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6617 /* If that didn't work, try for a namespace-name. */
6618 if (!only_class_p && !successful_parse_p)
6619 {
6620 /* Restore the saved scope. */
6621 parser->scope = saved_scope;
6622 parser->qualifying_scope = saved_qualifying_scope;
6623 parser->object_scope = saved_object_scope;
6624 /* If we are not looking at an identifier followed by the scope
6625 resolution operator, then this is not part of a
6626 nested-name-specifier. (Note that this function is only used
6627 to parse the components of a nested-name-specifier.) */
6628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6629 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6630 return error_mark_node;
6631 scope = cp_parser_namespace_name (parser);
6632 }
6633
6634 return scope;
6635 }
6636
6637 /* Return true if we are looking at a compound-literal, false otherwise. */
6638
6639 static bool
6640 cp_parser_compound_literal_p (cp_parser *parser)
6641 {
6642 cp_lexer_save_tokens (parser->lexer);
6643
6644 /* Skip tokens until the next token is a closing parenthesis.
6645 If we find the closing `)', and the next token is a `{', then
6646 we are looking at a compound-literal. */
6647 bool compound_literal_p
6648 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6649 /*consume_paren=*/true)
6650 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6651
6652 /* Roll back the tokens we skipped. */
6653 cp_lexer_rollback_tokens (parser->lexer);
6654
6655 return compound_literal_p;
6656 }
6657
6658 /* Return true if EXPR is the integer constant zero or a complex constant
6659 of zero, without any folding, but ignoring location wrappers. */
6660
6661 bool
6662 literal_integer_zerop (const_tree expr)
6663 {
6664 return (location_wrapper_p (expr)
6665 && integer_zerop (TREE_OPERAND (expr, 0)));
6666 }
6667
6668 /* Parse a postfix-expression.
6669
6670 postfix-expression:
6671 primary-expression
6672 postfix-expression [ expression ]
6673 postfix-expression ( expression-list [opt] )
6674 simple-type-specifier ( expression-list [opt] )
6675 typename :: [opt] nested-name-specifier identifier
6676 ( expression-list [opt] )
6677 typename :: [opt] nested-name-specifier template [opt] template-id
6678 ( expression-list [opt] )
6679 postfix-expression . template [opt] id-expression
6680 postfix-expression -> template [opt] id-expression
6681 postfix-expression . pseudo-destructor-name
6682 postfix-expression -> pseudo-destructor-name
6683 postfix-expression ++
6684 postfix-expression --
6685 dynamic_cast < type-id > ( expression )
6686 static_cast < type-id > ( expression )
6687 reinterpret_cast < type-id > ( expression )
6688 const_cast < type-id > ( expression )
6689 typeid ( expression )
6690 typeid ( type-id )
6691
6692 GNU Extension:
6693
6694 postfix-expression:
6695 ( type-id ) { initializer-list , [opt] }
6696
6697 This extension is a GNU version of the C99 compound-literal
6698 construct. (The C99 grammar uses `type-name' instead of `type-id',
6699 but they are essentially the same concept.)
6700
6701 If ADDRESS_P is true, the postfix expression is the operand of the
6702 `&' operator. CAST_P is true if this expression is the target of a
6703 cast.
6704
6705 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6706 class member access expressions [expr.ref].
6707
6708 Returns a representation of the expression. */
6709
6710 static cp_expr
6711 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6712 bool member_access_only_p, bool decltype_p,
6713 cp_id_kind * pidk_return)
6714 {
6715 cp_token *token;
6716 location_t loc;
6717 enum rid keyword;
6718 cp_id_kind idk = CP_ID_KIND_NONE;
6719 cp_expr postfix_expression = NULL_TREE;
6720 bool is_member_access = false;
6721
6722 /* Peek at the next token. */
6723 token = cp_lexer_peek_token (parser->lexer);
6724 loc = token->location;
6725 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6726
6727 /* Some of the productions are determined by keywords. */
6728 keyword = token->keyword;
6729 switch (keyword)
6730 {
6731 case RID_DYNCAST:
6732 case RID_STATCAST:
6733 case RID_REINTCAST:
6734 case RID_CONSTCAST:
6735 {
6736 tree type;
6737 cp_expr expression;
6738 const char *saved_message;
6739 bool saved_in_type_id_in_expr_p;
6740
6741 /* All of these can be handled in the same way from the point
6742 of view of parsing. Begin by consuming the token
6743 identifying the cast. */
6744 cp_lexer_consume_token (parser->lexer);
6745
6746 /* New types cannot be defined in the cast. */
6747 saved_message = parser->type_definition_forbidden_message;
6748 parser->type_definition_forbidden_message
6749 = G_("types may not be defined in casts");
6750
6751 /* Look for the opening `<'. */
6752 cp_parser_require (parser, CPP_LESS, RT_LESS);
6753 /* Parse the type to which we are casting. */
6754 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6755 parser->in_type_id_in_expr_p = true;
6756 type = cp_parser_type_id (parser);
6757 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6758 /* Look for the closing `>'. */
6759 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6760 /* Restore the old message. */
6761 parser->type_definition_forbidden_message = saved_message;
6762
6763 bool saved_greater_than_is_operator_p
6764 = parser->greater_than_is_operator_p;
6765 parser->greater_than_is_operator_p = true;
6766
6767 /* And the expression which is being cast. */
6768 matching_parens parens;
6769 parens.require_open (parser);
6770 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6771 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6772 RT_CLOSE_PAREN);
6773 location_t end_loc = close_paren ?
6774 close_paren->location : UNKNOWN_LOCATION;
6775
6776 parser->greater_than_is_operator_p
6777 = saved_greater_than_is_operator_p;
6778
6779 /* Only type conversions to integral or enumeration types
6780 can be used in constant-expressions. */
6781 if (!cast_valid_in_integral_constant_expression_p (type)
6782 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6783 {
6784 postfix_expression = error_mark_node;
6785 break;
6786 }
6787
6788 switch (keyword)
6789 {
6790 case RID_DYNCAST:
6791 postfix_expression
6792 = build_dynamic_cast (type, expression, tf_warning_or_error);
6793 break;
6794 case RID_STATCAST:
6795 postfix_expression
6796 = build_static_cast (type, expression, tf_warning_or_error);
6797 break;
6798 case RID_REINTCAST:
6799 postfix_expression
6800 = build_reinterpret_cast (type, expression,
6801 tf_warning_or_error);
6802 break;
6803 case RID_CONSTCAST:
6804 postfix_expression
6805 = build_const_cast (type, expression, tf_warning_or_error);
6806 break;
6807 default:
6808 gcc_unreachable ();
6809 }
6810
6811 /* Construct a location e.g. :
6812 reinterpret_cast <int *> (expr)
6813 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6814 ranging from the start of the "*_cast" token to the final closing
6815 paren, with the caret at the start. */
6816 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6817 postfix_expression.set_location (cp_cast_loc);
6818 }
6819 break;
6820
6821 case RID_TYPEID:
6822 {
6823 tree type;
6824 const char *saved_message;
6825 bool saved_in_type_id_in_expr_p;
6826
6827 /* Consume the `typeid' token. */
6828 cp_lexer_consume_token (parser->lexer);
6829 /* Look for the `(' token. */
6830 matching_parens parens;
6831 parens.require_open (parser);
6832 /* Types cannot be defined in a `typeid' expression. */
6833 saved_message = parser->type_definition_forbidden_message;
6834 parser->type_definition_forbidden_message
6835 = G_("types may not be defined in a %<typeid%> expression");
6836 /* We can't be sure yet whether we're looking at a type-id or an
6837 expression. */
6838 cp_parser_parse_tentatively (parser);
6839 /* Try a type-id first. */
6840 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6841 parser->in_type_id_in_expr_p = true;
6842 type = cp_parser_type_id (parser);
6843 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6844 /* Look for the `)' token. Otherwise, we can't be sure that
6845 we're not looking at an expression: consider `typeid (int
6846 (3))', for example. */
6847 cp_token *close_paren = parens.require_close (parser);
6848 /* If all went well, simply lookup the type-id. */
6849 if (cp_parser_parse_definitely (parser))
6850 postfix_expression = get_typeid (type, tf_warning_or_error);
6851 /* Otherwise, fall back to the expression variant. */
6852 else
6853 {
6854 tree expression;
6855
6856 /* Look for an expression. */
6857 expression = cp_parser_expression (parser, & idk);
6858 /* Compute its typeid. */
6859 postfix_expression = build_typeid (expression, tf_warning_or_error);
6860 /* Look for the `)' token. */
6861 close_paren = parens.require_close (parser);
6862 }
6863 /* Restore the saved message. */
6864 parser->type_definition_forbidden_message = saved_message;
6865 /* `typeid' may not appear in an integral constant expression. */
6866 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6867 postfix_expression = error_mark_node;
6868
6869 /* Construct a location e.g. :
6870 typeid (expr)
6871 ^~~~~~~~~~~~~
6872 ranging from the start of the "typeid" token to the final closing
6873 paren, with the caret at the start. */
6874 if (close_paren)
6875 {
6876 location_t typeid_loc
6877 = make_location (start_loc, start_loc, close_paren->location);
6878 postfix_expression.set_location (typeid_loc);
6879 postfix_expression.maybe_add_location_wrapper ();
6880 }
6881 }
6882 break;
6883
6884 case RID_TYPENAME:
6885 {
6886 tree type;
6887 /* The syntax permitted here is the same permitted for an
6888 elaborated-type-specifier. */
6889 ++parser->prevent_constrained_type_specifiers;
6890 type = cp_parser_elaborated_type_specifier (parser,
6891 /*is_friend=*/false,
6892 /*is_declaration=*/false);
6893 --parser->prevent_constrained_type_specifiers;
6894 postfix_expression = cp_parser_functional_cast (parser, type);
6895 }
6896 break;
6897
6898 case RID_ADDRESSOF:
6899 case RID_BUILTIN_SHUFFLE:
6900 case RID_BUILTIN_LAUNDER:
6901 {
6902 vec<tree, va_gc> *vec;
6903 unsigned int i;
6904 tree p;
6905
6906 cp_lexer_consume_token (parser->lexer);
6907 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6908 /*cast_p=*/false, /*allow_expansion_p=*/true,
6909 /*non_constant_p=*/NULL);
6910 if (vec == NULL)
6911 {
6912 postfix_expression = error_mark_node;
6913 break;
6914 }
6915
6916 FOR_EACH_VEC_ELT (*vec, i, p)
6917 mark_exp_read (p);
6918
6919 switch (keyword)
6920 {
6921 case RID_ADDRESSOF:
6922 if (vec->length () == 1)
6923 postfix_expression
6924 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6925 else
6926 {
6927 error_at (loc, "wrong number of arguments to "
6928 "%<__builtin_addressof%>");
6929 postfix_expression = error_mark_node;
6930 }
6931 break;
6932
6933 case RID_BUILTIN_LAUNDER:
6934 if (vec->length () == 1)
6935 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6936 tf_warning_or_error);
6937 else
6938 {
6939 error_at (loc, "wrong number of arguments to "
6940 "%<__builtin_launder%>");
6941 postfix_expression = error_mark_node;
6942 }
6943 break;
6944
6945 case RID_BUILTIN_SHUFFLE:
6946 if (vec->length () == 2)
6947 postfix_expression
6948 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6949 (*vec)[1], tf_warning_or_error);
6950 else if (vec->length () == 3)
6951 postfix_expression
6952 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6953 (*vec)[2], tf_warning_or_error);
6954 else
6955 {
6956 error_at (loc, "wrong number of arguments to "
6957 "%<__builtin_shuffle%>");
6958 postfix_expression = error_mark_node;
6959 }
6960 break;
6961
6962 default:
6963 gcc_unreachable ();
6964 }
6965 break;
6966 }
6967
6968 default:
6969 {
6970 tree type;
6971
6972 /* If the next thing is a simple-type-specifier, we may be
6973 looking at a functional cast. We could also be looking at
6974 an id-expression. So, we try the functional cast, and if
6975 that doesn't work we fall back to the primary-expression. */
6976 cp_parser_parse_tentatively (parser);
6977 /* Look for the simple-type-specifier. */
6978 ++parser->prevent_constrained_type_specifiers;
6979 type = cp_parser_simple_type_specifier (parser,
6980 /*decl_specs=*/NULL,
6981 CP_PARSER_FLAGS_NONE);
6982 --parser->prevent_constrained_type_specifiers;
6983 /* Parse the cast itself. */
6984 if (!cp_parser_error_occurred (parser))
6985 postfix_expression
6986 = cp_parser_functional_cast (parser, type);
6987 /* If that worked, we're done. */
6988 if (cp_parser_parse_definitely (parser))
6989 break;
6990
6991 /* If the functional-cast didn't work out, try a
6992 compound-literal. */
6993 if (cp_parser_allow_gnu_extensions_p (parser)
6994 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6995 {
6996 cp_expr initializer = NULL_TREE;
6997
6998 cp_parser_parse_tentatively (parser);
6999
7000 matching_parens parens;
7001 parens.consume_open (parser);
7002
7003 /* Avoid calling cp_parser_type_id pointlessly, see comment
7004 in cp_parser_cast_expression about c++/29234. */
7005 if (!cp_parser_compound_literal_p (parser))
7006 cp_parser_simulate_error (parser);
7007 else
7008 {
7009 /* Parse the type. */
7010 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7011 parser->in_type_id_in_expr_p = true;
7012 type = cp_parser_type_id (parser);
7013 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7014 parens.require_close (parser);
7015 }
7016
7017 /* If things aren't going well, there's no need to
7018 keep going. */
7019 if (!cp_parser_error_occurred (parser))
7020 {
7021 bool non_constant_p;
7022 /* Parse the brace-enclosed initializer list. */
7023 initializer = cp_parser_braced_list (parser,
7024 &non_constant_p);
7025 }
7026 /* If that worked, we're definitely looking at a
7027 compound-literal expression. */
7028 if (cp_parser_parse_definitely (parser))
7029 {
7030 /* Warn the user that a compound literal is not
7031 allowed in standard C++. */
7032 pedwarn (input_location, OPT_Wpedantic,
7033 "ISO C++ forbids compound-literals");
7034 /* For simplicity, we disallow compound literals in
7035 constant-expressions. We could
7036 allow compound literals of integer type, whose
7037 initializer was a constant, in constant
7038 expressions. Permitting that usage, as a further
7039 extension, would not change the meaning of any
7040 currently accepted programs. (Of course, as
7041 compound literals are not part of ISO C++, the
7042 standard has nothing to say.) */
7043 if (cp_parser_non_integral_constant_expression (parser,
7044 NIC_NCC))
7045 {
7046 postfix_expression = error_mark_node;
7047 break;
7048 }
7049 /* Form the representation of the compound-literal. */
7050 postfix_expression
7051 = finish_compound_literal (type, initializer,
7052 tf_warning_or_error, fcl_c99);
7053 postfix_expression.set_location (initializer.get_location ());
7054 break;
7055 }
7056 }
7057
7058 /* It must be a primary-expression. */
7059 postfix_expression
7060 = cp_parser_primary_expression (parser, address_p, cast_p,
7061 /*template_arg_p=*/false,
7062 decltype_p,
7063 &idk);
7064 }
7065 break;
7066 }
7067
7068 /* Note that we don't need to worry about calling build_cplus_new on a
7069 class-valued CALL_EXPR in decltype when it isn't the end of the
7070 postfix-expression; unary_complex_lvalue will take care of that for
7071 all these cases. */
7072
7073 /* Keep looping until the postfix-expression is complete. */
7074 while (true)
7075 {
7076 if (idk == CP_ID_KIND_UNQUALIFIED
7077 && identifier_p (postfix_expression)
7078 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7079 /* It is not a Koenig lookup function call. */
7080 postfix_expression
7081 = unqualified_name_lookup_error (postfix_expression);
7082
7083 /* Peek at the next token. */
7084 token = cp_lexer_peek_token (parser->lexer);
7085
7086 switch (token->type)
7087 {
7088 case CPP_OPEN_SQUARE:
7089 if (cp_next_tokens_can_be_std_attribute_p (parser))
7090 {
7091 cp_parser_error (parser,
7092 "two consecutive %<[%> shall "
7093 "only introduce an attribute");
7094 return error_mark_node;
7095 }
7096 postfix_expression
7097 = cp_parser_postfix_open_square_expression (parser,
7098 postfix_expression,
7099 false,
7100 decltype_p);
7101 postfix_expression.set_range (start_loc,
7102 postfix_expression.get_location ());
7103
7104 idk = CP_ID_KIND_NONE;
7105 is_member_access = false;
7106 break;
7107
7108 case CPP_OPEN_PAREN:
7109 /* postfix-expression ( expression-list [opt] ) */
7110 {
7111 bool koenig_p;
7112 bool is_builtin_constant_p;
7113 bool saved_integral_constant_expression_p = false;
7114 bool saved_non_integral_constant_expression_p = false;
7115 tsubst_flags_t complain = complain_flags (decltype_p);
7116 vec<tree, va_gc> *args;
7117 location_t close_paren_loc = UNKNOWN_LOCATION;
7118
7119 is_member_access = false;
7120
7121 is_builtin_constant_p
7122 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7123 if (is_builtin_constant_p)
7124 {
7125 /* The whole point of __builtin_constant_p is to allow
7126 non-constant expressions to appear as arguments. */
7127 saved_integral_constant_expression_p
7128 = parser->integral_constant_expression_p;
7129 saved_non_integral_constant_expression_p
7130 = parser->non_integral_constant_expression_p;
7131 parser->integral_constant_expression_p = false;
7132 }
7133 args = (cp_parser_parenthesized_expression_list
7134 (parser, non_attr,
7135 /*cast_p=*/false, /*allow_expansion_p=*/true,
7136 /*non_constant_p=*/NULL,
7137 /*close_paren_loc=*/&close_paren_loc,
7138 /*wrap_locations_p=*/true));
7139 if (is_builtin_constant_p)
7140 {
7141 parser->integral_constant_expression_p
7142 = saved_integral_constant_expression_p;
7143 parser->non_integral_constant_expression_p
7144 = saved_non_integral_constant_expression_p;
7145 }
7146
7147 if (args == NULL)
7148 {
7149 postfix_expression = error_mark_node;
7150 break;
7151 }
7152
7153 /* Function calls are not permitted in
7154 constant-expressions. */
7155 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7156 && cp_parser_non_integral_constant_expression (parser,
7157 NIC_FUNC_CALL))
7158 {
7159 postfix_expression = error_mark_node;
7160 release_tree_vector (args);
7161 break;
7162 }
7163
7164 koenig_p = false;
7165 if (idk == CP_ID_KIND_UNQUALIFIED
7166 || idk == CP_ID_KIND_TEMPLATE_ID)
7167 {
7168 if (identifier_p (postfix_expression))
7169 {
7170 if (!args->is_empty ())
7171 {
7172 koenig_p = true;
7173 if (!any_type_dependent_arguments_p (args))
7174 postfix_expression
7175 = perform_koenig_lookup (postfix_expression, args,
7176 complain);
7177 }
7178 else
7179 postfix_expression
7180 = unqualified_fn_lookup_error (postfix_expression);
7181 }
7182 /* We do not perform argument-dependent lookup if
7183 normal lookup finds a non-function, in accordance
7184 with the expected resolution of DR 218. */
7185 else if (!args->is_empty ()
7186 && is_overloaded_fn (postfix_expression))
7187 {
7188 tree fn = get_first_fn (postfix_expression);
7189 fn = STRIP_TEMPLATE (fn);
7190
7191 /* Do not do argument dependent lookup if regular
7192 lookup finds a member function or a block-scope
7193 function declaration. [basic.lookup.argdep]/3 */
7194 if (!DECL_FUNCTION_MEMBER_P (fn)
7195 && !DECL_LOCAL_FUNCTION_P (fn))
7196 {
7197 koenig_p = true;
7198 if (!any_type_dependent_arguments_p (args))
7199 postfix_expression
7200 = perform_koenig_lookup (postfix_expression, args,
7201 complain);
7202 }
7203 }
7204 }
7205
7206 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7207 {
7208 tree instance = TREE_OPERAND (postfix_expression, 0);
7209 tree fn = TREE_OPERAND (postfix_expression, 1);
7210
7211 if (processing_template_decl
7212 && (type_dependent_object_expression_p (instance)
7213 || (!BASELINK_P (fn)
7214 && TREE_CODE (fn) != FIELD_DECL)
7215 || type_dependent_expression_p (fn)
7216 || any_type_dependent_arguments_p (args)))
7217 {
7218 maybe_generic_this_capture (instance, fn);
7219 postfix_expression
7220 = build_min_nt_call_vec (postfix_expression, args);
7221 release_tree_vector (args);
7222 break;
7223 }
7224
7225 if (BASELINK_P (fn))
7226 {
7227 postfix_expression
7228 = (build_new_method_call
7229 (instance, fn, &args, NULL_TREE,
7230 (idk == CP_ID_KIND_QUALIFIED
7231 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7232 : LOOKUP_NORMAL),
7233 /*fn_p=*/NULL,
7234 complain));
7235 }
7236 else
7237 postfix_expression
7238 = finish_call_expr (postfix_expression, &args,
7239 /*disallow_virtual=*/false,
7240 /*koenig_p=*/false,
7241 complain);
7242 }
7243 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7244 || TREE_CODE (postfix_expression) == MEMBER_REF
7245 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7246 postfix_expression = (build_offset_ref_call_from_tree
7247 (postfix_expression, &args,
7248 complain));
7249 else if (idk == CP_ID_KIND_QUALIFIED)
7250 /* A call to a static class member, or a namespace-scope
7251 function. */
7252 postfix_expression
7253 = finish_call_expr (postfix_expression, &args,
7254 /*disallow_virtual=*/true,
7255 koenig_p,
7256 complain);
7257 else
7258 /* All other function calls. */
7259 postfix_expression
7260 = finish_call_expr (postfix_expression, &args,
7261 /*disallow_virtual=*/false,
7262 koenig_p,
7263 complain);
7264
7265 if (close_paren_loc != UNKNOWN_LOCATION)
7266 {
7267 location_t combined_loc = make_location (token->location,
7268 start_loc,
7269 close_paren_loc);
7270 postfix_expression.set_location (combined_loc);
7271 }
7272
7273 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7274 idk = CP_ID_KIND_NONE;
7275
7276 release_tree_vector (args);
7277 }
7278 break;
7279
7280 case CPP_DOT:
7281 case CPP_DEREF:
7282 /* postfix-expression . template [opt] id-expression
7283 postfix-expression . pseudo-destructor-name
7284 postfix-expression -> template [opt] id-expression
7285 postfix-expression -> pseudo-destructor-name */
7286
7287 /* Consume the `.' or `->' operator. */
7288 cp_lexer_consume_token (parser->lexer);
7289
7290 postfix_expression
7291 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7292 postfix_expression,
7293 false, &idk, loc);
7294
7295 is_member_access = true;
7296 break;
7297
7298 case CPP_PLUS_PLUS:
7299 /* postfix-expression ++ */
7300 /* Consume the `++' token. */
7301 cp_lexer_consume_token (parser->lexer);
7302 /* Generate a representation for the complete expression. */
7303 postfix_expression
7304 = finish_increment_expr (postfix_expression,
7305 POSTINCREMENT_EXPR);
7306 /* Increments may not appear in constant-expressions. */
7307 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7308 postfix_expression = error_mark_node;
7309 idk = CP_ID_KIND_NONE;
7310 is_member_access = false;
7311 break;
7312
7313 case CPP_MINUS_MINUS:
7314 /* postfix-expression -- */
7315 /* Consume the `--' token. */
7316 cp_lexer_consume_token (parser->lexer);
7317 /* Generate a representation for the complete expression. */
7318 postfix_expression
7319 = finish_increment_expr (postfix_expression,
7320 POSTDECREMENT_EXPR);
7321 /* Decrements may not appear in constant-expressions. */
7322 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7323 postfix_expression = error_mark_node;
7324 idk = CP_ID_KIND_NONE;
7325 is_member_access = false;
7326 break;
7327
7328 default:
7329 if (pidk_return != NULL)
7330 * pidk_return = idk;
7331 if (member_access_only_p)
7332 return is_member_access
7333 ? postfix_expression
7334 : cp_expr (error_mark_node);
7335 else
7336 return postfix_expression;
7337 }
7338 }
7339
7340 /* We should never get here. */
7341 gcc_unreachable ();
7342 return error_mark_node;
7343 }
7344
7345 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7346 by cp_parser_builtin_offsetof. We're looking for
7347
7348 postfix-expression [ expression ]
7349 postfix-expression [ braced-init-list ] (C++11)
7350
7351 FOR_OFFSETOF is set if we're being called in that context, which
7352 changes how we deal with integer constant expressions. */
7353
7354 static tree
7355 cp_parser_postfix_open_square_expression (cp_parser *parser,
7356 tree postfix_expression,
7357 bool for_offsetof,
7358 bool decltype_p)
7359 {
7360 tree index = NULL_TREE;
7361 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7362 bool saved_greater_than_is_operator_p;
7363
7364 /* Consume the `[' token. */
7365 cp_lexer_consume_token (parser->lexer);
7366
7367 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7368 parser->greater_than_is_operator_p = true;
7369
7370 /* Parse the index expression. */
7371 /* ??? For offsetof, there is a question of what to allow here. If
7372 offsetof is not being used in an integral constant expression context,
7373 then we *could* get the right answer by computing the value at runtime.
7374 If we are in an integral constant expression context, then we might
7375 could accept any constant expression; hard to say without analysis.
7376 Rather than open the barn door too wide right away, allow only integer
7377 constant expressions here. */
7378 if (for_offsetof)
7379 index = cp_parser_constant_expression (parser);
7380 else
7381 {
7382 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7383 {
7384 bool expr_nonconst_p;
7385 cp_lexer_set_source_position (parser->lexer);
7386 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7387 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7388 }
7389 else
7390 index = cp_parser_expression (parser);
7391 }
7392
7393 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7394
7395 /* Look for the closing `]'. */
7396 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7397
7398 /* Build the ARRAY_REF. */
7399 postfix_expression = grok_array_decl (loc, postfix_expression,
7400 index, decltype_p);
7401
7402 /* When not doing offsetof, array references are not permitted in
7403 constant-expressions. */
7404 if (!for_offsetof
7405 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7406 postfix_expression = error_mark_node;
7407
7408 return postfix_expression;
7409 }
7410
7411 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7412 dereference of incomplete type, returns true if error_mark_node should
7413 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7414 and *DEPENDENT_P. */
7415
7416 bool
7417 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7418 bool *dependent_p)
7419 {
7420 /* In a template, be permissive by treating an object expression
7421 of incomplete type as dependent (after a pedwarn). */
7422 diagnostic_t kind = (processing_template_decl
7423 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7424
7425 switch (TREE_CODE (*postfix_expression))
7426 {
7427 case CAST_EXPR:
7428 case REINTERPRET_CAST_EXPR:
7429 case CONST_CAST_EXPR:
7430 case STATIC_CAST_EXPR:
7431 case DYNAMIC_CAST_EXPR:
7432 case IMPLICIT_CONV_EXPR:
7433 case VIEW_CONVERT_EXPR:
7434 case NON_LVALUE_EXPR:
7435 kind = DK_ERROR;
7436 break;
7437 case OVERLOAD:
7438 /* Don't emit any diagnostic for OVERLOADs. */
7439 kind = DK_IGNORED;
7440 break;
7441 default:
7442 /* Avoid clobbering e.g. DECLs. */
7443 if (!EXPR_P (*postfix_expression))
7444 kind = DK_ERROR;
7445 break;
7446 }
7447
7448 if (kind == DK_IGNORED)
7449 return false;
7450
7451 location_t exploc = location_of (*postfix_expression);
7452 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7453 if (!MAYBE_CLASS_TYPE_P (*scope))
7454 return true;
7455 if (kind == DK_ERROR)
7456 *scope = *postfix_expression = error_mark_node;
7457 else if (processing_template_decl)
7458 {
7459 *dependent_p = true;
7460 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7461 }
7462 return false;
7463 }
7464
7465 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7466 by cp_parser_builtin_offsetof. We're looking for
7467
7468 postfix-expression . template [opt] id-expression
7469 postfix-expression . pseudo-destructor-name
7470 postfix-expression -> template [opt] id-expression
7471 postfix-expression -> pseudo-destructor-name
7472
7473 FOR_OFFSETOF is set if we're being called in that context. That sorta
7474 limits what of the above we'll actually accept, but nevermind.
7475 TOKEN_TYPE is the "." or "->" token, which will already have been
7476 removed from the stream. */
7477
7478 static tree
7479 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7480 enum cpp_ttype token_type,
7481 cp_expr postfix_expression,
7482 bool for_offsetof, cp_id_kind *idk,
7483 location_t location)
7484 {
7485 tree name;
7486 bool dependent_p;
7487 bool pseudo_destructor_p;
7488 tree scope = NULL_TREE;
7489 location_t start_loc = postfix_expression.get_start ();
7490
7491 /* If this is a `->' operator, dereference the pointer. */
7492 if (token_type == CPP_DEREF)
7493 postfix_expression = build_x_arrow (location, postfix_expression,
7494 tf_warning_or_error);
7495 /* Check to see whether or not the expression is type-dependent and
7496 not the current instantiation. */
7497 dependent_p = type_dependent_object_expression_p (postfix_expression);
7498 /* The identifier following the `->' or `.' is not qualified. */
7499 parser->scope = NULL_TREE;
7500 parser->qualifying_scope = NULL_TREE;
7501 parser->object_scope = NULL_TREE;
7502 *idk = CP_ID_KIND_NONE;
7503
7504 /* Enter the scope corresponding to the type of the object
7505 given by the POSTFIX_EXPRESSION. */
7506 if (!dependent_p)
7507 {
7508 scope = TREE_TYPE (postfix_expression);
7509 /* According to the standard, no expression should ever have
7510 reference type. Unfortunately, we do not currently match
7511 the standard in this respect in that our internal representation
7512 of an expression may have reference type even when the standard
7513 says it does not. Therefore, we have to manually obtain the
7514 underlying type here. */
7515 scope = non_reference (scope);
7516 /* The type of the POSTFIX_EXPRESSION must be complete. */
7517 /* Unlike the object expression in other contexts, *this is not
7518 required to be of complete type for purposes of class member
7519 access (5.2.5) outside the member function body. */
7520 if (postfix_expression != current_class_ref
7521 && scope != error_mark_node
7522 && !currently_open_class (scope))
7523 {
7524 scope = complete_type (scope);
7525 if (!COMPLETE_TYPE_P (scope)
7526 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7527 &dependent_p))
7528 return error_mark_node;
7529 }
7530
7531 if (!dependent_p)
7532 {
7533 /* Let the name lookup machinery know that we are processing a
7534 class member access expression. */
7535 parser->context->object_type = scope;
7536 /* If something went wrong, we want to be able to discern that case,
7537 as opposed to the case where there was no SCOPE due to the type
7538 of expression being dependent. */
7539 if (!scope)
7540 scope = error_mark_node;
7541 /* If the SCOPE was erroneous, make the various semantic analysis
7542 functions exit quickly -- and without issuing additional error
7543 messages. */
7544 if (scope == error_mark_node)
7545 postfix_expression = error_mark_node;
7546 }
7547 }
7548
7549 if (dependent_p)
7550 /* Tell cp_parser_lookup_name that there was an object, even though it's
7551 type-dependent. */
7552 parser->context->object_type = unknown_type_node;
7553
7554 /* Assume this expression is not a pseudo-destructor access. */
7555 pseudo_destructor_p = false;
7556
7557 /* If the SCOPE is a scalar type, then, if this is a valid program,
7558 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7559 is type dependent, it can be pseudo-destructor-name or something else.
7560 Try to parse it as pseudo-destructor-name first. */
7561 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7562 {
7563 tree s;
7564 tree type;
7565
7566 cp_parser_parse_tentatively (parser);
7567 /* Parse the pseudo-destructor-name. */
7568 s = NULL_TREE;
7569 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7570 &s, &type);
7571 if (dependent_p
7572 && (cp_parser_error_occurred (parser)
7573 || !SCALAR_TYPE_P (type)))
7574 cp_parser_abort_tentative_parse (parser);
7575 else if (cp_parser_parse_definitely (parser))
7576 {
7577 pseudo_destructor_p = true;
7578 postfix_expression
7579 = finish_pseudo_destructor_expr (postfix_expression,
7580 s, type, location);
7581 }
7582 }
7583
7584 if (!pseudo_destructor_p)
7585 {
7586 /* If the SCOPE is not a scalar type, we are looking at an
7587 ordinary class member access expression, rather than a
7588 pseudo-destructor-name. */
7589 bool template_p;
7590 cp_token *token = cp_lexer_peek_token (parser->lexer);
7591 /* Parse the id-expression. */
7592 name = (cp_parser_id_expression
7593 (parser,
7594 cp_parser_optional_template_keyword (parser),
7595 /*check_dependency_p=*/true,
7596 &template_p,
7597 /*declarator_p=*/false,
7598 /*optional_p=*/false));
7599 /* In general, build a SCOPE_REF if the member name is qualified.
7600 However, if the name was not dependent and has already been
7601 resolved; there is no need to build the SCOPE_REF. For example;
7602
7603 struct X { void f(); };
7604 template <typename T> void f(T* t) { t->X::f(); }
7605
7606 Even though "t" is dependent, "X::f" is not and has been resolved
7607 to a BASELINK; there is no need to include scope information. */
7608
7609 /* But we do need to remember that there was an explicit scope for
7610 virtual function calls. */
7611 if (parser->scope)
7612 *idk = CP_ID_KIND_QUALIFIED;
7613
7614 /* If the name is a template-id that names a type, we will get a
7615 TYPE_DECL here. That is invalid code. */
7616 if (TREE_CODE (name) == TYPE_DECL)
7617 {
7618 error_at (token->location, "invalid use of %qD", name);
7619 postfix_expression = error_mark_node;
7620 }
7621 else
7622 {
7623 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7624 {
7625 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7626 {
7627 error_at (token->location, "%<%D::%D%> is not a class member",
7628 parser->scope, name);
7629 postfix_expression = error_mark_node;
7630 }
7631 else
7632 name = build_qualified_name (/*type=*/NULL_TREE,
7633 parser->scope,
7634 name,
7635 template_p);
7636 parser->scope = NULL_TREE;
7637 parser->qualifying_scope = NULL_TREE;
7638 parser->object_scope = NULL_TREE;
7639 }
7640 if (parser->scope && name && BASELINK_P (name))
7641 adjust_result_of_qualified_name_lookup
7642 (name, parser->scope, scope);
7643 postfix_expression
7644 = finish_class_member_access_expr (postfix_expression, name,
7645 template_p,
7646 tf_warning_or_error);
7647 /* Build a location e.g.:
7648 ptr->access_expr
7649 ~~~^~~~~~~~~~~~~
7650 where the caret is at the deref token, ranging from
7651 the start of postfix_expression to the end of the access expr. */
7652 location_t end_loc
7653 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7654 location_t combined_loc
7655 = make_location (input_location, start_loc, end_loc);
7656 protected_set_expr_location (postfix_expression, combined_loc);
7657 }
7658 }
7659
7660 /* We no longer need to look up names in the scope of the object on
7661 the left-hand side of the `.' or `->' operator. */
7662 parser->context->object_type = NULL_TREE;
7663
7664 /* Outside of offsetof, these operators may not appear in
7665 constant-expressions. */
7666 if (!for_offsetof
7667 && (cp_parser_non_integral_constant_expression
7668 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7669 postfix_expression = error_mark_node;
7670
7671 return postfix_expression;
7672 }
7673
7674 /* Parse a parenthesized expression-list.
7675
7676 expression-list:
7677 assignment-expression
7678 expression-list, assignment-expression
7679
7680 attribute-list:
7681 expression-list
7682 identifier
7683 identifier, expression-list
7684
7685 CAST_P is true if this expression is the target of a cast.
7686
7687 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7688 argument pack.
7689
7690 WRAP_LOCATIONS_P is true if expressions within this list for which
7691 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7692 their source locations.
7693
7694 Returns a vector of trees. Each element is a representation of an
7695 assignment-expression. NULL is returned if the ( and or ) are
7696 missing. An empty, but allocated, vector is returned on no
7697 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7698 if we are parsing an attribute list for an attribute that wants a
7699 plain identifier argument, normal_attr for an attribute that wants
7700 an expression, or non_attr if we aren't parsing an attribute list. If
7701 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7702 not all of the expressions in the list were constant.
7703 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7704 will be written to with the location of the closing parenthesis. If
7705 an error occurs, it may or may not be written to. */
7706
7707 static vec<tree, va_gc> *
7708 cp_parser_parenthesized_expression_list (cp_parser* parser,
7709 int is_attribute_list,
7710 bool cast_p,
7711 bool allow_expansion_p,
7712 bool *non_constant_p,
7713 location_t *close_paren_loc,
7714 bool wrap_locations_p)
7715 {
7716 vec<tree, va_gc> *expression_list;
7717 bool fold_expr_p = is_attribute_list != non_attr;
7718 tree identifier = NULL_TREE;
7719 bool saved_greater_than_is_operator_p;
7720
7721 /* Assume all the expressions will be constant. */
7722 if (non_constant_p)
7723 *non_constant_p = false;
7724
7725 matching_parens parens;
7726 if (!parens.require_open (parser))
7727 return NULL;
7728
7729 expression_list = make_tree_vector ();
7730
7731 /* Within a parenthesized expression, a `>' token is always
7732 the greater-than operator. */
7733 saved_greater_than_is_operator_p
7734 = parser->greater_than_is_operator_p;
7735 parser->greater_than_is_operator_p = true;
7736
7737 cp_expr expr (NULL_TREE);
7738
7739 /* Consume expressions until there are no more. */
7740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7741 while (true)
7742 {
7743 /* At the beginning of attribute lists, check to see if the
7744 next token is an identifier. */
7745 if (is_attribute_list == id_attr
7746 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7747 {
7748 cp_token *token;
7749
7750 /* Consume the identifier. */
7751 token = cp_lexer_consume_token (parser->lexer);
7752 /* Save the identifier. */
7753 identifier = token->u.value;
7754 }
7755 else
7756 {
7757 bool expr_non_constant_p;
7758
7759 /* Parse the next assignment-expression. */
7760 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7761 {
7762 /* A braced-init-list. */
7763 cp_lexer_set_source_position (parser->lexer);
7764 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7765 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7766 if (non_constant_p && expr_non_constant_p)
7767 *non_constant_p = true;
7768 }
7769 else if (non_constant_p)
7770 {
7771 expr = (cp_parser_constant_expression
7772 (parser, /*allow_non_constant_p=*/true,
7773 &expr_non_constant_p));
7774 if (expr_non_constant_p)
7775 *non_constant_p = true;
7776 }
7777 else
7778 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7779 cast_p);
7780
7781 if (fold_expr_p)
7782 expr = instantiate_non_dependent_expr (expr);
7783
7784 /* If we have an ellipsis, then this is an expression
7785 expansion. */
7786 if (allow_expansion_p
7787 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7788 {
7789 /* Consume the `...'. */
7790 cp_lexer_consume_token (parser->lexer);
7791
7792 /* Build the argument pack. */
7793 expr = make_pack_expansion (expr);
7794 }
7795
7796 if (wrap_locations_p)
7797 expr.maybe_add_location_wrapper ();
7798
7799 /* Add it to the list. We add error_mark_node
7800 expressions to the list, so that we can still tell if
7801 the correct form for a parenthesized expression-list
7802 is found. That gives better errors. */
7803 vec_safe_push (expression_list, expr.get_value ());
7804
7805 if (expr == error_mark_node)
7806 goto skip_comma;
7807 }
7808
7809 /* After the first item, attribute lists look the same as
7810 expression lists. */
7811 is_attribute_list = non_attr;
7812
7813 get_comma:;
7814 /* If the next token isn't a `,', then we are done. */
7815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7816 break;
7817
7818 /* Otherwise, consume the `,' and keep going. */
7819 cp_lexer_consume_token (parser->lexer);
7820 }
7821
7822 if (close_paren_loc)
7823 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7824
7825 if (!parens.require_close (parser))
7826 {
7827 int ending;
7828
7829 skip_comma:;
7830 /* We try and resync to an unnested comma, as that will give the
7831 user better diagnostics. */
7832 ending = cp_parser_skip_to_closing_parenthesis (parser,
7833 /*recovering=*/true,
7834 /*or_comma=*/true,
7835 /*consume_paren=*/true);
7836 if (ending < 0)
7837 goto get_comma;
7838 if (!ending)
7839 {
7840 parser->greater_than_is_operator_p
7841 = saved_greater_than_is_operator_p;
7842 return NULL;
7843 }
7844 }
7845
7846 parser->greater_than_is_operator_p
7847 = saved_greater_than_is_operator_p;
7848
7849 if (identifier)
7850 vec_safe_insert (expression_list, 0, identifier);
7851
7852 return expression_list;
7853 }
7854
7855 /* Parse a pseudo-destructor-name.
7856
7857 pseudo-destructor-name:
7858 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7859 :: [opt] nested-name-specifier template template-id :: ~ type-name
7860 :: [opt] nested-name-specifier [opt] ~ type-name
7861
7862 If either of the first two productions is used, sets *SCOPE to the
7863 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7864 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7865 or ERROR_MARK_NODE if the parse fails. */
7866
7867 static void
7868 cp_parser_pseudo_destructor_name (cp_parser* parser,
7869 tree object,
7870 tree* scope,
7871 tree* type)
7872 {
7873 bool nested_name_specifier_p;
7874
7875 /* Handle ~auto. */
7876 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7877 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7878 && !type_dependent_expression_p (object))
7879 {
7880 if (cxx_dialect < cxx14)
7881 pedwarn (input_location, 0,
7882 "%<~auto%> only available with "
7883 "-std=c++14 or -std=gnu++14");
7884 cp_lexer_consume_token (parser->lexer);
7885 cp_lexer_consume_token (parser->lexer);
7886 *scope = NULL_TREE;
7887 *type = TREE_TYPE (object);
7888 return;
7889 }
7890
7891 /* Assume that things will not work out. */
7892 *type = error_mark_node;
7893
7894 /* Look for the optional `::' operator. */
7895 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7896 /* Look for the optional nested-name-specifier. */
7897 nested_name_specifier_p
7898 = (cp_parser_nested_name_specifier_opt (parser,
7899 /*typename_keyword_p=*/false,
7900 /*check_dependency_p=*/true,
7901 /*type_p=*/false,
7902 /*is_declaration=*/false)
7903 != NULL_TREE);
7904 /* Now, if we saw a nested-name-specifier, we might be doing the
7905 second production. */
7906 if (nested_name_specifier_p
7907 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7908 {
7909 /* Consume the `template' keyword. */
7910 cp_lexer_consume_token (parser->lexer);
7911 /* Parse the template-id. */
7912 cp_parser_template_id (parser,
7913 /*template_keyword_p=*/true,
7914 /*check_dependency_p=*/false,
7915 class_type,
7916 /*is_declaration=*/true);
7917 /* Look for the `::' token. */
7918 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7919 }
7920 /* If the next token is not a `~', then there might be some
7921 additional qualification. */
7922 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7923 {
7924 /* At this point, we're looking for "type-name :: ~". The type-name
7925 must not be a class-name, since this is a pseudo-destructor. So,
7926 it must be either an enum-name, or a typedef-name -- both of which
7927 are just identifiers. So, we peek ahead to check that the "::"
7928 and "~" tokens are present; if they are not, then we can avoid
7929 calling type_name. */
7930 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7931 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7932 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7933 {
7934 cp_parser_error (parser, "non-scalar type");
7935 return;
7936 }
7937
7938 /* Look for the type-name. */
7939 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7940 if (*scope == error_mark_node)
7941 return;
7942
7943 /* Look for the `::' token. */
7944 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7945 }
7946 else
7947 *scope = NULL_TREE;
7948
7949 /* Look for the `~'. */
7950 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7951
7952 /* Once we see the ~, this has to be a pseudo-destructor. */
7953 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7954 cp_parser_commit_to_topmost_tentative_parse (parser);
7955
7956 /* Look for the type-name again. We are not responsible for
7957 checking that it matches the first type-name. */
7958 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7959 }
7960
7961 /* Parse a unary-expression.
7962
7963 unary-expression:
7964 postfix-expression
7965 ++ cast-expression
7966 -- cast-expression
7967 unary-operator cast-expression
7968 sizeof unary-expression
7969 sizeof ( type-id )
7970 alignof ( type-id ) [C++0x]
7971 new-expression
7972 delete-expression
7973
7974 GNU Extensions:
7975
7976 unary-expression:
7977 __extension__ cast-expression
7978 __alignof__ unary-expression
7979 __alignof__ ( type-id )
7980 alignof unary-expression [C++0x]
7981 __real__ cast-expression
7982 __imag__ cast-expression
7983 && identifier
7984 sizeof ( type-id ) { initializer-list , [opt] }
7985 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7986 __alignof__ ( type-id ) { initializer-list , [opt] }
7987
7988 ADDRESS_P is true iff the unary-expression is appearing as the
7989 operand of the `&' operator. CAST_P is true if this expression is
7990 the target of a cast.
7991
7992 Returns a representation of the expression. */
7993
7994 static cp_expr
7995 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7996 bool address_p, bool cast_p, bool decltype_p)
7997 {
7998 cp_token *token;
7999 enum tree_code unary_operator;
8000
8001 /* Peek at the next token. */
8002 token = cp_lexer_peek_token (parser->lexer);
8003 /* Some keywords give away the kind of expression. */
8004 if (token->type == CPP_KEYWORD)
8005 {
8006 enum rid keyword = token->keyword;
8007
8008 switch (keyword)
8009 {
8010 case RID_ALIGNOF:
8011 case RID_SIZEOF:
8012 {
8013 tree operand, ret;
8014 enum tree_code op;
8015 location_t start_loc = token->location;
8016
8017 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8018 bool std_alignof = id_equal (token->u.value, "alignof");
8019
8020 /* Consume the token. */
8021 cp_lexer_consume_token (parser->lexer);
8022 /* Parse the operand. */
8023 operand = cp_parser_sizeof_operand (parser, keyword);
8024
8025 if (TYPE_P (operand))
8026 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8027 true);
8028 else
8029 {
8030 /* ISO C++ defines alignof only with types, not with
8031 expressions. So pedwarn if alignof is used with a non-
8032 type expression. However, __alignof__ is ok. */
8033 if (std_alignof)
8034 pedwarn (token->location, OPT_Wpedantic,
8035 "ISO C++ does not allow %<alignof%> "
8036 "with a non-type");
8037
8038 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8039 }
8040 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8041 SIZEOF_EXPR with the original operand. */
8042 if (op == SIZEOF_EXPR && ret != error_mark_node)
8043 {
8044 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8045 {
8046 if (!processing_template_decl && TYPE_P (operand))
8047 {
8048 ret = build_min (SIZEOF_EXPR, size_type_node,
8049 build1 (NOP_EXPR, operand,
8050 error_mark_node));
8051 SIZEOF_EXPR_TYPE_P (ret) = 1;
8052 }
8053 else
8054 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8055 TREE_SIDE_EFFECTS (ret) = 0;
8056 TREE_READONLY (ret) = 1;
8057 }
8058 }
8059
8060 /* Construct a location e.g. :
8061 alignof (expr)
8062 ^~~~~~~~~~~~~~
8063 with start == caret at the start of the "alignof"/"sizeof"
8064 token, with the endpoint at the final closing paren. */
8065 location_t finish_loc
8066 = cp_lexer_previous_token (parser->lexer)->location;
8067 location_t compound_loc
8068 = make_location (start_loc, start_loc, finish_loc);
8069
8070 cp_expr ret_expr (ret);
8071 ret_expr.set_location (compound_loc);
8072 ret_expr = ret_expr.maybe_add_location_wrapper ();
8073 return ret_expr;
8074 }
8075
8076 case RID_NEW:
8077 return cp_parser_new_expression (parser);
8078
8079 case RID_DELETE:
8080 return cp_parser_delete_expression (parser);
8081
8082 case RID_EXTENSION:
8083 {
8084 /* The saved value of the PEDANTIC flag. */
8085 int saved_pedantic;
8086 tree expr;
8087
8088 /* Save away the PEDANTIC flag. */
8089 cp_parser_extension_opt (parser, &saved_pedantic);
8090 /* Parse the cast-expression. */
8091 expr = cp_parser_simple_cast_expression (parser);
8092 /* Restore the PEDANTIC flag. */
8093 pedantic = saved_pedantic;
8094
8095 return expr;
8096 }
8097
8098 case RID_REALPART:
8099 case RID_IMAGPART:
8100 {
8101 tree expression;
8102
8103 /* Consume the `__real__' or `__imag__' token. */
8104 cp_lexer_consume_token (parser->lexer);
8105 /* Parse the cast-expression. */
8106 expression = cp_parser_simple_cast_expression (parser);
8107 /* Create the complete representation. */
8108 return build_x_unary_op (token->location,
8109 (keyword == RID_REALPART
8110 ? REALPART_EXPR : IMAGPART_EXPR),
8111 expression,
8112 tf_warning_or_error);
8113 }
8114 break;
8115
8116 case RID_TRANSACTION_ATOMIC:
8117 case RID_TRANSACTION_RELAXED:
8118 return cp_parser_transaction_expression (parser, keyword);
8119
8120 case RID_NOEXCEPT:
8121 {
8122 tree expr;
8123 const char *saved_message;
8124 bool saved_integral_constant_expression_p;
8125 bool saved_non_integral_constant_expression_p;
8126 bool saved_greater_than_is_operator_p;
8127
8128 location_t start_loc = token->location;
8129
8130 cp_lexer_consume_token (parser->lexer);
8131 matching_parens parens;
8132 parens.require_open (parser);
8133
8134 saved_message = parser->type_definition_forbidden_message;
8135 parser->type_definition_forbidden_message
8136 = G_("types may not be defined in %<noexcept%> expressions");
8137
8138 saved_integral_constant_expression_p
8139 = parser->integral_constant_expression_p;
8140 saved_non_integral_constant_expression_p
8141 = parser->non_integral_constant_expression_p;
8142 parser->integral_constant_expression_p = false;
8143
8144 saved_greater_than_is_operator_p
8145 = parser->greater_than_is_operator_p;
8146 parser->greater_than_is_operator_p = true;
8147
8148 ++cp_unevaluated_operand;
8149 ++c_inhibit_evaluation_warnings;
8150 ++cp_noexcept_operand;
8151 expr = cp_parser_expression (parser);
8152 --cp_noexcept_operand;
8153 --c_inhibit_evaluation_warnings;
8154 --cp_unevaluated_operand;
8155
8156 parser->greater_than_is_operator_p
8157 = saved_greater_than_is_operator_p;
8158
8159 parser->integral_constant_expression_p
8160 = saved_integral_constant_expression_p;
8161 parser->non_integral_constant_expression_p
8162 = saved_non_integral_constant_expression_p;
8163
8164 parser->type_definition_forbidden_message = saved_message;
8165
8166 location_t finish_loc
8167 = cp_lexer_peek_token (parser->lexer)->location;
8168 parens.require_close (parser);
8169
8170 /* Construct a location of the form:
8171 noexcept (expr)
8172 ^~~~~~~~~~~~~~~
8173 with start == caret, finishing at the close-paren. */
8174 location_t noexcept_loc
8175 = make_location (start_loc, start_loc, finish_loc);
8176
8177 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8178 noexcept_loc);
8179 }
8180
8181 default:
8182 break;
8183 }
8184 }
8185
8186 /* Look for the `:: new' and `:: delete', which also signal the
8187 beginning of a new-expression, or delete-expression,
8188 respectively. If the next token is `::', then it might be one of
8189 these. */
8190 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8191 {
8192 enum rid keyword;
8193
8194 /* See if the token after the `::' is one of the keywords in
8195 which we're interested. */
8196 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8197 /* If it's `new', we have a new-expression. */
8198 if (keyword == RID_NEW)
8199 return cp_parser_new_expression (parser);
8200 /* Similarly, for `delete'. */
8201 else if (keyword == RID_DELETE)
8202 return cp_parser_delete_expression (parser);
8203 }
8204
8205 /* Look for a unary operator. */
8206 unary_operator = cp_parser_unary_operator (token);
8207 /* The `++' and `--' operators can be handled similarly, even though
8208 they are not technically unary-operators in the grammar. */
8209 if (unary_operator == ERROR_MARK)
8210 {
8211 if (token->type == CPP_PLUS_PLUS)
8212 unary_operator = PREINCREMENT_EXPR;
8213 else if (token->type == CPP_MINUS_MINUS)
8214 unary_operator = PREDECREMENT_EXPR;
8215 /* Handle the GNU address-of-label extension. */
8216 else if (cp_parser_allow_gnu_extensions_p (parser)
8217 && token->type == CPP_AND_AND)
8218 {
8219 tree identifier;
8220 tree expression;
8221 location_t start_loc = token->location;
8222
8223 /* Consume the '&&' token. */
8224 cp_lexer_consume_token (parser->lexer);
8225 /* Look for the identifier. */
8226 location_t finish_loc
8227 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8228 identifier = cp_parser_identifier (parser);
8229 /* Construct a location of the form:
8230 &&label
8231 ^~~~~~~
8232 with caret==start at the "&&", finish at the end of the label. */
8233 location_t combined_loc
8234 = make_location (start_loc, start_loc, finish_loc);
8235 /* Create an expression representing the address. */
8236 expression = finish_label_address_expr (identifier, combined_loc);
8237 if (cp_parser_non_integral_constant_expression (parser,
8238 NIC_ADDR_LABEL))
8239 expression = error_mark_node;
8240 return expression;
8241 }
8242 }
8243 if (unary_operator != ERROR_MARK)
8244 {
8245 cp_expr cast_expression;
8246 cp_expr expression = error_mark_node;
8247 non_integral_constant non_constant_p = NIC_NONE;
8248 location_t loc = token->location;
8249 tsubst_flags_t complain = complain_flags (decltype_p);
8250
8251 /* Consume the operator token. */
8252 token = cp_lexer_consume_token (parser->lexer);
8253 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8254
8255 /* Parse the cast-expression. */
8256 cast_expression
8257 = cp_parser_cast_expression (parser,
8258 unary_operator == ADDR_EXPR,
8259 /*cast_p=*/false,
8260 /*decltype*/false,
8261 pidk);
8262
8263 /* Make a location:
8264 OP_TOKEN CAST_EXPRESSION
8265 ^~~~~~~~~~~~~~~~~~~~~~~~~
8266 with start==caret at the operator token, and
8267 extending to the end of the cast_expression. */
8268 loc = make_location (loc, loc, cast_expression.get_finish ());
8269
8270 /* Now, build an appropriate representation. */
8271 switch (unary_operator)
8272 {
8273 case INDIRECT_REF:
8274 non_constant_p = NIC_STAR;
8275 expression = build_x_indirect_ref (loc, cast_expression,
8276 RO_UNARY_STAR,
8277 complain);
8278 /* TODO: build_x_indirect_ref does not always honor the
8279 location, so ensure it is set. */
8280 expression.set_location (loc);
8281 break;
8282
8283 case ADDR_EXPR:
8284 non_constant_p = NIC_ADDR;
8285 /* Fall through. */
8286 case BIT_NOT_EXPR:
8287 expression = build_x_unary_op (loc, unary_operator,
8288 cast_expression,
8289 complain);
8290 /* TODO: build_x_unary_op does not always honor the location,
8291 so ensure it is set. */
8292 expression.set_location (loc);
8293 break;
8294
8295 case PREINCREMENT_EXPR:
8296 case PREDECREMENT_EXPR:
8297 non_constant_p = unary_operator == PREINCREMENT_EXPR
8298 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8299 /* Fall through. */
8300 case NEGATE_EXPR:
8301 /* Immediately fold negation of a constant, unless the constant is 0
8302 (since -0 == 0) or it would overflow. */
8303 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8304 && CONSTANT_CLASS_P (cast_expression)
8305 && !integer_zerop (cast_expression)
8306 && !TREE_OVERFLOW (cast_expression))
8307 {
8308 tree folded = fold_build1 (unary_operator,
8309 TREE_TYPE (cast_expression),
8310 cast_expression);
8311 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8312 {
8313 expression = cp_expr (folded, loc);
8314 break;
8315 }
8316 }
8317 /* Fall through. */
8318 case UNARY_PLUS_EXPR:
8319 case TRUTH_NOT_EXPR:
8320 expression = finish_unary_op_expr (loc, unary_operator,
8321 cast_expression, complain);
8322 break;
8323
8324 default:
8325 gcc_unreachable ();
8326 }
8327
8328 if (non_constant_p != NIC_NONE
8329 && cp_parser_non_integral_constant_expression (parser,
8330 non_constant_p))
8331 expression = error_mark_node;
8332
8333 return expression;
8334 }
8335
8336 return cp_parser_postfix_expression (parser, address_p, cast_p,
8337 /*member_access_only_p=*/false,
8338 decltype_p,
8339 pidk);
8340 }
8341
8342 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8343 unary-operator, the corresponding tree code is returned. */
8344
8345 static enum tree_code
8346 cp_parser_unary_operator (cp_token* token)
8347 {
8348 switch (token->type)
8349 {
8350 case CPP_MULT:
8351 return INDIRECT_REF;
8352
8353 case CPP_AND:
8354 return ADDR_EXPR;
8355
8356 case CPP_PLUS:
8357 return UNARY_PLUS_EXPR;
8358
8359 case CPP_MINUS:
8360 return NEGATE_EXPR;
8361
8362 case CPP_NOT:
8363 return TRUTH_NOT_EXPR;
8364
8365 case CPP_COMPL:
8366 return BIT_NOT_EXPR;
8367
8368 default:
8369 return ERROR_MARK;
8370 }
8371 }
8372
8373 /* Parse a new-expression.
8374
8375 new-expression:
8376 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8377 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8378
8379 Returns a representation of the expression. */
8380
8381 static tree
8382 cp_parser_new_expression (cp_parser* parser)
8383 {
8384 bool global_scope_p;
8385 vec<tree, va_gc> *placement;
8386 tree type;
8387 vec<tree, va_gc> *initializer;
8388 tree nelts = NULL_TREE;
8389 tree ret;
8390
8391 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8392
8393 /* Look for the optional `::' operator. */
8394 global_scope_p
8395 = (cp_parser_global_scope_opt (parser,
8396 /*current_scope_valid_p=*/false)
8397 != NULL_TREE);
8398 /* Look for the `new' operator. */
8399 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8400 /* There's no easy way to tell a new-placement from the
8401 `( type-id )' construct. */
8402 cp_parser_parse_tentatively (parser);
8403 /* Look for a new-placement. */
8404 placement = cp_parser_new_placement (parser);
8405 /* If that didn't work out, there's no new-placement. */
8406 if (!cp_parser_parse_definitely (parser))
8407 {
8408 if (placement != NULL)
8409 release_tree_vector (placement);
8410 placement = NULL;
8411 }
8412
8413 /* If the next token is a `(', then we have a parenthesized
8414 type-id. */
8415 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8416 {
8417 cp_token *token;
8418 const char *saved_message = parser->type_definition_forbidden_message;
8419
8420 /* Consume the `('. */
8421 matching_parens parens;
8422 parens.consume_open (parser);
8423
8424 /* Parse the type-id. */
8425 parser->type_definition_forbidden_message
8426 = G_("types may not be defined in a new-expression");
8427 {
8428 type_id_in_expr_sentinel s (parser);
8429 type = cp_parser_type_id (parser);
8430 }
8431 parser->type_definition_forbidden_message = saved_message;
8432
8433 /* Look for the closing `)'. */
8434 parens.require_close (parser);
8435 token = cp_lexer_peek_token (parser->lexer);
8436 /* There should not be a direct-new-declarator in this production,
8437 but GCC used to allowed this, so we check and emit a sensible error
8438 message for this case. */
8439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8440 {
8441 error_at (token->location,
8442 "array bound forbidden after parenthesized type-id");
8443 inform (token->location,
8444 "try removing the parentheses around the type-id");
8445 cp_parser_direct_new_declarator (parser);
8446 }
8447 }
8448 /* Otherwise, there must be a new-type-id. */
8449 else
8450 type = cp_parser_new_type_id (parser, &nelts);
8451
8452 /* If the next token is a `(' or '{', then we have a new-initializer. */
8453 cp_token *token = cp_lexer_peek_token (parser->lexer);
8454 if (token->type == CPP_OPEN_PAREN
8455 || token->type == CPP_OPEN_BRACE)
8456 initializer = cp_parser_new_initializer (parser);
8457 else
8458 initializer = NULL;
8459
8460 /* A new-expression may not appear in an integral constant
8461 expression. */
8462 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8463 ret = error_mark_node;
8464 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8465 of a new-type-id or type-id of a new-expression, the new-expression shall
8466 contain a new-initializer of the form ( assignment-expression )".
8467 Additionally, consistently with the spirit of DR 1467, we want to accept
8468 'new auto { 2 }' too. */
8469 else if ((ret = type_uses_auto (type))
8470 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8471 && (vec_safe_length (initializer) != 1
8472 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8473 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8474 {
8475 error_at (token->location,
8476 "initialization of new-expression for type %<auto%> "
8477 "requires exactly one element");
8478 ret = error_mark_node;
8479 }
8480 else
8481 {
8482 /* Construct a location e.g.:
8483 ptr = new int[100]
8484 ^~~~~~~~~~~~
8485 with caret == start at the start of the "new" token, and the end
8486 at the end of the final token we consumed. */
8487 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8488 location_t end_loc = get_finish (end_tok->location);
8489 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8490
8491 /* Create a representation of the new-expression. */
8492 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8493 tf_warning_or_error);
8494 protected_set_expr_location (ret, combined_loc);
8495 }
8496
8497 if (placement != NULL)
8498 release_tree_vector (placement);
8499 if (initializer != NULL)
8500 release_tree_vector (initializer);
8501
8502 return ret;
8503 }
8504
8505 /* Parse a new-placement.
8506
8507 new-placement:
8508 ( expression-list )
8509
8510 Returns the same representation as for an expression-list. */
8511
8512 static vec<tree, va_gc> *
8513 cp_parser_new_placement (cp_parser* parser)
8514 {
8515 vec<tree, va_gc> *expression_list;
8516
8517 /* Parse the expression-list. */
8518 expression_list = (cp_parser_parenthesized_expression_list
8519 (parser, non_attr, /*cast_p=*/false,
8520 /*allow_expansion_p=*/true,
8521 /*non_constant_p=*/NULL));
8522
8523 if (expression_list && expression_list->is_empty ())
8524 error ("expected expression-list or type-id");
8525
8526 return expression_list;
8527 }
8528
8529 /* Parse a new-type-id.
8530
8531 new-type-id:
8532 type-specifier-seq new-declarator [opt]
8533
8534 Returns the TYPE allocated. If the new-type-id indicates an array
8535 type, *NELTS is set to the number of elements in the last array
8536 bound; the TYPE will not include the last array bound. */
8537
8538 static tree
8539 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8540 {
8541 cp_decl_specifier_seq type_specifier_seq;
8542 cp_declarator *new_declarator;
8543 cp_declarator *declarator;
8544 cp_declarator *outer_declarator;
8545 const char *saved_message;
8546
8547 /* The type-specifier sequence must not contain type definitions.
8548 (It cannot contain declarations of new types either, but if they
8549 are not definitions we will catch that because they are not
8550 complete.) */
8551 saved_message = parser->type_definition_forbidden_message;
8552 parser->type_definition_forbidden_message
8553 = G_("types may not be defined in a new-type-id");
8554 /* Parse the type-specifier-seq. */
8555 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8556 /*is_trailing_return=*/false,
8557 &type_specifier_seq);
8558 /* Restore the old message. */
8559 parser->type_definition_forbidden_message = saved_message;
8560
8561 if (type_specifier_seq.type == error_mark_node)
8562 return error_mark_node;
8563
8564 /* Parse the new-declarator. */
8565 new_declarator = cp_parser_new_declarator_opt (parser);
8566
8567 /* Determine the number of elements in the last array dimension, if
8568 any. */
8569 *nelts = NULL_TREE;
8570 /* Skip down to the last array dimension. */
8571 declarator = new_declarator;
8572 outer_declarator = NULL;
8573 while (declarator && (declarator->kind == cdk_pointer
8574 || declarator->kind == cdk_ptrmem))
8575 {
8576 outer_declarator = declarator;
8577 declarator = declarator->declarator;
8578 }
8579 while (declarator
8580 && declarator->kind == cdk_array
8581 && declarator->declarator
8582 && declarator->declarator->kind == cdk_array)
8583 {
8584 outer_declarator = declarator;
8585 declarator = declarator->declarator;
8586 }
8587
8588 if (declarator && declarator->kind == cdk_array)
8589 {
8590 *nelts = declarator->u.array.bounds;
8591 if (*nelts == error_mark_node)
8592 *nelts = integer_one_node;
8593
8594 if (outer_declarator)
8595 outer_declarator->declarator = declarator->declarator;
8596 else
8597 new_declarator = NULL;
8598 }
8599
8600 return groktypename (&type_specifier_seq, new_declarator, false);
8601 }
8602
8603 /* Parse an (optional) new-declarator.
8604
8605 new-declarator:
8606 ptr-operator new-declarator [opt]
8607 direct-new-declarator
8608
8609 Returns the declarator. */
8610
8611 static cp_declarator *
8612 cp_parser_new_declarator_opt (cp_parser* parser)
8613 {
8614 enum tree_code code;
8615 tree type, std_attributes = NULL_TREE;
8616 cp_cv_quals cv_quals;
8617
8618 /* We don't know if there's a ptr-operator next, or not. */
8619 cp_parser_parse_tentatively (parser);
8620 /* Look for a ptr-operator. */
8621 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8622 /* If that worked, look for more new-declarators. */
8623 if (cp_parser_parse_definitely (parser))
8624 {
8625 cp_declarator *declarator;
8626
8627 /* Parse another optional declarator. */
8628 declarator = cp_parser_new_declarator_opt (parser);
8629
8630 declarator = cp_parser_make_indirect_declarator
8631 (code, type, cv_quals, declarator, std_attributes);
8632
8633 return declarator;
8634 }
8635
8636 /* If the next token is a `[', there is a direct-new-declarator. */
8637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8638 return cp_parser_direct_new_declarator (parser);
8639
8640 return NULL;
8641 }
8642
8643 /* Parse a direct-new-declarator.
8644
8645 direct-new-declarator:
8646 [ expression ]
8647 direct-new-declarator [constant-expression]
8648
8649 */
8650
8651 static cp_declarator *
8652 cp_parser_direct_new_declarator (cp_parser* parser)
8653 {
8654 cp_declarator *declarator = NULL;
8655
8656 while (true)
8657 {
8658 tree expression;
8659 cp_token *token;
8660
8661 /* Look for the opening `['. */
8662 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8663
8664 token = cp_lexer_peek_token (parser->lexer);
8665 expression = cp_parser_expression (parser);
8666 /* The standard requires that the expression have integral
8667 type. DR 74 adds enumeration types. We believe that the
8668 real intent is that these expressions be handled like the
8669 expression in a `switch' condition, which also allows
8670 classes with a single conversion to integral or
8671 enumeration type. */
8672 if (!processing_template_decl)
8673 {
8674 expression
8675 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8676 expression,
8677 /*complain=*/true);
8678 if (!expression)
8679 {
8680 error_at (token->location,
8681 "expression in new-declarator must have integral "
8682 "or enumeration type");
8683 expression = error_mark_node;
8684 }
8685 }
8686
8687 /* Look for the closing `]'. */
8688 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8689
8690 /* Add this bound to the declarator. */
8691 declarator = make_array_declarator (declarator, expression);
8692
8693 /* If the next token is not a `[', then there are no more
8694 bounds. */
8695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8696 break;
8697 }
8698
8699 return declarator;
8700 }
8701
8702 /* Parse a new-initializer.
8703
8704 new-initializer:
8705 ( expression-list [opt] )
8706 braced-init-list
8707
8708 Returns a representation of the expression-list. */
8709
8710 static vec<tree, va_gc> *
8711 cp_parser_new_initializer (cp_parser* parser)
8712 {
8713 vec<tree, va_gc> *expression_list;
8714
8715 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8716 {
8717 tree t;
8718 bool expr_non_constant_p;
8719 cp_lexer_set_source_position (parser->lexer);
8720 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8721 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8722 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8723 expression_list = make_tree_vector_single (t);
8724 }
8725 else
8726 expression_list = (cp_parser_parenthesized_expression_list
8727 (parser, non_attr, /*cast_p=*/false,
8728 /*allow_expansion_p=*/true,
8729 /*non_constant_p=*/NULL));
8730
8731 return expression_list;
8732 }
8733
8734 /* Parse a delete-expression.
8735
8736 delete-expression:
8737 :: [opt] delete cast-expression
8738 :: [opt] delete [ ] cast-expression
8739
8740 Returns a representation of the expression. */
8741
8742 static tree
8743 cp_parser_delete_expression (cp_parser* parser)
8744 {
8745 bool global_scope_p;
8746 bool array_p;
8747 tree expression;
8748
8749 /* Look for the optional `::' operator. */
8750 global_scope_p
8751 = (cp_parser_global_scope_opt (parser,
8752 /*current_scope_valid_p=*/false)
8753 != NULL_TREE);
8754 /* Look for the `delete' keyword. */
8755 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8756 /* See if the array syntax is in use. */
8757 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8758 {
8759 /* Consume the `[' token. */
8760 cp_lexer_consume_token (parser->lexer);
8761 /* Look for the `]' token. */
8762 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8763 /* Remember that this is the `[]' construct. */
8764 array_p = true;
8765 }
8766 else
8767 array_p = false;
8768
8769 /* Parse the cast-expression. */
8770 expression = cp_parser_simple_cast_expression (parser);
8771
8772 /* A delete-expression may not appear in an integral constant
8773 expression. */
8774 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8775 return error_mark_node;
8776
8777 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8778 tf_warning_or_error);
8779 }
8780
8781 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8782 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8783 0 otherwise. */
8784
8785 static int
8786 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8787 {
8788 cp_token *token = cp_lexer_peek_token (parser->lexer);
8789 switch (token->type)
8790 {
8791 case CPP_COMMA:
8792 case CPP_SEMICOLON:
8793 case CPP_QUERY:
8794 case CPP_COLON:
8795 case CPP_CLOSE_SQUARE:
8796 case CPP_CLOSE_PAREN:
8797 case CPP_CLOSE_BRACE:
8798 case CPP_OPEN_BRACE:
8799 case CPP_DOT:
8800 case CPP_DOT_STAR:
8801 case CPP_DEREF:
8802 case CPP_DEREF_STAR:
8803 case CPP_DIV:
8804 case CPP_MOD:
8805 case CPP_LSHIFT:
8806 case CPP_RSHIFT:
8807 case CPP_LESS:
8808 case CPP_GREATER:
8809 case CPP_LESS_EQ:
8810 case CPP_GREATER_EQ:
8811 case CPP_EQ_EQ:
8812 case CPP_NOT_EQ:
8813 case CPP_EQ:
8814 case CPP_MULT_EQ:
8815 case CPP_DIV_EQ:
8816 case CPP_MOD_EQ:
8817 case CPP_PLUS_EQ:
8818 case CPP_MINUS_EQ:
8819 case CPP_RSHIFT_EQ:
8820 case CPP_LSHIFT_EQ:
8821 case CPP_AND_EQ:
8822 case CPP_XOR_EQ:
8823 case CPP_OR_EQ:
8824 case CPP_XOR:
8825 case CPP_OR:
8826 case CPP_OR_OR:
8827 case CPP_EOF:
8828 case CPP_ELLIPSIS:
8829 return 0;
8830
8831 case CPP_OPEN_PAREN:
8832 /* In ((type ()) () the last () isn't a valid cast-expression,
8833 so the whole must be parsed as postfix-expression. */
8834 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8835 != CPP_CLOSE_PAREN;
8836
8837 case CPP_OPEN_SQUARE:
8838 /* '[' may start a primary-expression in obj-c++ and in C++11,
8839 as a lambda-expression, eg, '(void)[]{}'. */
8840 if (cxx_dialect >= cxx11)
8841 return -1;
8842 return c_dialect_objc ();
8843
8844 case CPP_PLUS_PLUS:
8845 case CPP_MINUS_MINUS:
8846 /* '++' and '--' may or may not start a cast-expression:
8847
8848 struct T { void operator++(int); };
8849 void f() { (T())++; }
8850
8851 vs
8852
8853 int a;
8854 (int)++a; */
8855 return -1;
8856
8857 default:
8858 return 1;
8859 }
8860 }
8861
8862 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8863 in the order: const_cast, static_cast, reinterpret_cast.
8864
8865 Don't suggest dynamic_cast.
8866
8867 Return the first legal cast kind found, or NULL otherwise. */
8868
8869 static const char *
8870 get_cast_suggestion (tree dst_type, tree orig_expr)
8871 {
8872 tree trial;
8873
8874 /* Reuse the parser logic by attempting to build the various kinds of
8875 cast, with "complain" disabled.
8876 Identify the first such cast that is valid. */
8877
8878 /* Don't attempt to run such logic within template processing. */
8879 if (processing_template_decl)
8880 return NULL;
8881
8882 /* First try const_cast. */
8883 trial = build_const_cast (dst_type, orig_expr, tf_none);
8884 if (trial != error_mark_node)
8885 return "const_cast";
8886
8887 /* If that fails, try static_cast. */
8888 trial = build_static_cast (dst_type, orig_expr, tf_none);
8889 if (trial != error_mark_node)
8890 return "static_cast";
8891
8892 /* Finally, try reinterpret_cast. */
8893 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8894 if (trial != error_mark_node)
8895 return "reinterpret_cast";
8896
8897 /* No such cast possible. */
8898 return NULL;
8899 }
8900
8901 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8902 suggesting how to convert a C-style cast of the form:
8903
8904 (DST_TYPE)ORIG_EXPR
8905
8906 to a C++-style cast.
8907
8908 The primary range of RICHLOC is asssumed to be that of the original
8909 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8910 of the parens in the C-style cast. */
8911
8912 static void
8913 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8914 location_t close_paren_loc, tree orig_expr,
8915 tree dst_type)
8916 {
8917 /* This function is non-trivial, so bail out now if the warning isn't
8918 going to be emitted. */
8919 if (!warn_old_style_cast)
8920 return;
8921
8922 /* Try to find a legal C++ cast, trying them in order:
8923 const_cast, static_cast, reinterpret_cast. */
8924 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8925 if (!cast_suggestion)
8926 return;
8927
8928 /* Replace the open paren with "CAST_SUGGESTION<". */
8929 pretty_printer pp;
8930 pp_printf (&pp, "%s<", cast_suggestion);
8931 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8932
8933 /* Replace the close paren with "> (". */
8934 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8935
8936 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8937 rich_loc->add_fixit_insert_after (")");
8938 }
8939
8940
8941 /* Parse a cast-expression.
8942
8943 cast-expression:
8944 unary-expression
8945 ( type-id ) cast-expression
8946
8947 ADDRESS_P is true iff the unary-expression is appearing as the
8948 operand of the `&' operator. CAST_P is true if this expression is
8949 the target of a cast.
8950
8951 Returns a representation of the expression. */
8952
8953 static cp_expr
8954 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8955 bool decltype_p, cp_id_kind * pidk)
8956 {
8957 /* If it's a `(', then we might be looking at a cast. */
8958 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8959 {
8960 tree type = NULL_TREE;
8961 cp_expr expr (NULL_TREE);
8962 int cast_expression = 0;
8963 const char *saved_message;
8964
8965 /* There's no way to know yet whether or not this is a cast.
8966 For example, `(int (3))' is a unary-expression, while `(int)
8967 3' is a cast. So, we resort to parsing tentatively. */
8968 cp_parser_parse_tentatively (parser);
8969 /* Types may not be defined in a cast. */
8970 saved_message = parser->type_definition_forbidden_message;
8971 parser->type_definition_forbidden_message
8972 = G_("types may not be defined in casts");
8973 /* Consume the `('. */
8974 matching_parens parens;
8975 cp_token *open_paren = parens.consume_open (parser);
8976 location_t open_paren_loc = open_paren->location;
8977 location_t close_paren_loc = UNKNOWN_LOCATION;
8978
8979 /* A very tricky bit is that `(struct S) { 3 }' is a
8980 compound-literal (which we permit in C++ as an extension).
8981 But, that construct is not a cast-expression -- it is a
8982 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8983 is legal; if the compound-literal were a cast-expression,
8984 you'd need an extra set of parentheses.) But, if we parse
8985 the type-id, and it happens to be a class-specifier, then we
8986 will commit to the parse at that point, because we cannot
8987 undo the action that is done when creating a new class. So,
8988 then we cannot back up and do a postfix-expression.
8989
8990 Another tricky case is the following (c++/29234):
8991
8992 struct S { void operator () (); };
8993
8994 void foo ()
8995 {
8996 ( S()() );
8997 }
8998
8999 As a type-id we parse the parenthesized S()() as a function
9000 returning a function, groktypename complains and we cannot
9001 back up in this case either.
9002
9003 Therefore, we scan ahead to the closing `)', and check to see
9004 if the tokens after the `)' can start a cast-expression. Otherwise
9005 we are dealing with an unary-expression, a postfix-expression
9006 or something else.
9007
9008 Yet another tricky case, in C++11, is the following (c++/54891):
9009
9010 (void)[]{};
9011
9012 The issue is that usually, besides the case of lambda-expressions,
9013 the parenthesized type-id cannot be followed by '[', and, eg, we
9014 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9015 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9016 we don't commit, we try a cast-expression, then an unary-expression.
9017
9018 Save tokens so that we can put them back. */
9019 cp_lexer_save_tokens (parser->lexer);
9020
9021 /* We may be looking at a cast-expression. */
9022 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9023 /*consume_paren=*/true))
9024 cast_expression
9025 = cp_parser_tokens_start_cast_expression (parser);
9026
9027 /* Roll back the tokens we skipped. */
9028 cp_lexer_rollback_tokens (parser->lexer);
9029 /* If we aren't looking at a cast-expression, simulate an error so
9030 that the call to cp_parser_error_occurred below returns true. */
9031 if (!cast_expression)
9032 cp_parser_simulate_error (parser);
9033 else
9034 {
9035 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9036 parser->in_type_id_in_expr_p = true;
9037 /* Look for the type-id. */
9038 type = cp_parser_type_id (parser);
9039 /* Look for the closing `)'. */
9040 cp_token *close_paren = parens.require_close (parser);
9041 if (close_paren)
9042 close_paren_loc = close_paren->location;
9043 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9044 }
9045
9046 /* Restore the saved message. */
9047 parser->type_definition_forbidden_message = saved_message;
9048
9049 /* At this point this can only be either a cast or a
9050 parenthesized ctor such as `(T ())' that looks like a cast to
9051 function returning T. */
9052 if (!cp_parser_error_occurred (parser))
9053 {
9054 /* Only commit if the cast-expression doesn't start with
9055 '++', '--', or '[' in C++11. */
9056 if (cast_expression > 0)
9057 cp_parser_commit_to_topmost_tentative_parse (parser);
9058
9059 expr = cp_parser_cast_expression (parser,
9060 /*address_p=*/false,
9061 /*cast_p=*/true,
9062 /*decltype_p=*/false,
9063 pidk);
9064
9065 if (cp_parser_parse_definitely (parser))
9066 {
9067 /* Warn about old-style casts, if so requested. */
9068 if (warn_old_style_cast
9069 && !in_system_header_at (input_location)
9070 && !VOID_TYPE_P (type)
9071 && current_lang_name != lang_name_c)
9072 {
9073 gcc_rich_location rich_loc (input_location);
9074 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9075 expr, type);
9076 warning_at (&rich_loc, OPT_Wold_style_cast,
9077 "use of old-style cast to %q#T", type);
9078 }
9079
9080 /* Only type conversions to integral or enumeration types
9081 can be used in constant-expressions. */
9082 if (!cast_valid_in_integral_constant_expression_p (type)
9083 && cp_parser_non_integral_constant_expression (parser,
9084 NIC_CAST))
9085 return error_mark_node;
9086
9087 /* Perform the cast. */
9088 /* Make a location:
9089 (TYPE) EXPR
9090 ^~~~~~~~~~~
9091 with start==caret at the open paren, extending to the
9092 end of "expr". */
9093 location_t cast_loc = make_location (open_paren_loc,
9094 open_paren_loc,
9095 expr.get_finish ());
9096 expr = build_c_cast (cast_loc, type, expr);
9097 return expr;
9098 }
9099 }
9100 else
9101 cp_parser_abort_tentative_parse (parser);
9102 }
9103
9104 /* If we get here, then it's not a cast, so it must be a
9105 unary-expression. */
9106 return cp_parser_unary_expression (parser, pidk, address_p,
9107 cast_p, decltype_p);
9108 }
9109
9110 /* Parse a binary expression of the general form:
9111
9112 pm-expression:
9113 cast-expression
9114 pm-expression .* cast-expression
9115 pm-expression ->* cast-expression
9116
9117 multiplicative-expression:
9118 pm-expression
9119 multiplicative-expression * pm-expression
9120 multiplicative-expression / pm-expression
9121 multiplicative-expression % pm-expression
9122
9123 additive-expression:
9124 multiplicative-expression
9125 additive-expression + multiplicative-expression
9126 additive-expression - multiplicative-expression
9127
9128 shift-expression:
9129 additive-expression
9130 shift-expression << additive-expression
9131 shift-expression >> additive-expression
9132
9133 relational-expression:
9134 shift-expression
9135 relational-expression < shift-expression
9136 relational-expression > shift-expression
9137 relational-expression <= shift-expression
9138 relational-expression >= shift-expression
9139
9140 GNU Extension:
9141
9142 relational-expression:
9143 relational-expression <? shift-expression
9144 relational-expression >? shift-expression
9145
9146 equality-expression:
9147 relational-expression
9148 equality-expression == relational-expression
9149 equality-expression != relational-expression
9150
9151 and-expression:
9152 equality-expression
9153 and-expression & equality-expression
9154
9155 exclusive-or-expression:
9156 and-expression
9157 exclusive-or-expression ^ and-expression
9158
9159 inclusive-or-expression:
9160 exclusive-or-expression
9161 inclusive-or-expression | exclusive-or-expression
9162
9163 logical-and-expression:
9164 inclusive-or-expression
9165 logical-and-expression && inclusive-or-expression
9166
9167 logical-or-expression:
9168 logical-and-expression
9169 logical-or-expression || logical-and-expression
9170
9171 All these are implemented with a single function like:
9172
9173 binary-expression:
9174 simple-cast-expression
9175 binary-expression <token> binary-expression
9176
9177 CAST_P is true if this expression is the target of a cast.
9178
9179 The binops_by_token map is used to get the tree codes for each <token> type.
9180 binary-expressions are associated according to a precedence table. */
9181
9182 #define TOKEN_PRECEDENCE(token) \
9183 (((token->type == CPP_GREATER \
9184 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9185 && !parser->greater_than_is_operator_p) \
9186 ? PREC_NOT_OPERATOR \
9187 : binops_by_token[token->type].prec)
9188
9189 static cp_expr
9190 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9191 bool no_toplevel_fold_p,
9192 bool decltype_p,
9193 enum cp_parser_prec prec,
9194 cp_id_kind * pidk)
9195 {
9196 cp_parser_expression_stack stack;
9197 cp_parser_expression_stack_entry *sp = &stack[0];
9198 cp_parser_expression_stack_entry current;
9199 cp_expr rhs;
9200 cp_token *token;
9201 enum tree_code rhs_type;
9202 enum cp_parser_prec new_prec, lookahead_prec;
9203 tree overload;
9204
9205 /* Parse the first expression. */
9206 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9207 ? TRUTH_NOT_EXPR : ERROR_MARK);
9208 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9209 cast_p, decltype_p, pidk);
9210 current.prec = prec;
9211
9212 if (cp_parser_error_occurred (parser))
9213 return error_mark_node;
9214
9215 for (;;)
9216 {
9217 /* Get an operator token. */
9218 token = cp_lexer_peek_token (parser->lexer);
9219
9220 if (warn_cxx11_compat
9221 && token->type == CPP_RSHIFT
9222 && !parser->greater_than_is_operator_p)
9223 {
9224 if (warning_at (token->location, OPT_Wc__11_compat,
9225 "%<>>%> operator is treated"
9226 " as two right angle brackets in C++11"))
9227 inform (token->location,
9228 "suggest parentheses around %<>>%> expression");
9229 }
9230
9231 new_prec = TOKEN_PRECEDENCE (token);
9232 if (new_prec != PREC_NOT_OPERATOR
9233 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9234 /* This is a fold-expression; handle it later. */
9235 new_prec = PREC_NOT_OPERATOR;
9236
9237 /* Popping an entry off the stack means we completed a subexpression:
9238 - either we found a token which is not an operator (`>' where it is not
9239 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9240 will happen repeatedly;
9241 - or, we found an operator which has lower priority. This is the case
9242 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9243 parsing `3 * 4'. */
9244 if (new_prec <= current.prec)
9245 {
9246 if (sp == stack)
9247 break;
9248 else
9249 goto pop;
9250 }
9251
9252 get_rhs:
9253 current.tree_type = binops_by_token[token->type].tree_type;
9254 current.loc = token->location;
9255
9256 /* We used the operator token. */
9257 cp_lexer_consume_token (parser->lexer);
9258
9259 /* For "false && x" or "true || x", x will never be executed;
9260 disable warnings while evaluating it. */
9261 if (current.tree_type == TRUTH_ANDIF_EXPR)
9262 c_inhibit_evaluation_warnings +=
9263 cp_fully_fold (current.lhs) == truthvalue_false_node;
9264 else if (current.tree_type == TRUTH_ORIF_EXPR)
9265 c_inhibit_evaluation_warnings +=
9266 cp_fully_fold (current.lhs) == truthvalue_true_node;
9267
9268 /* Extract another operand. It may be the RHS of this expression
9269 or the LHS of a new, higher priority expression. */
9270 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9271 ? TRUTH_NOT_EXPR : ERROR_MARK);
9272 rhs = cp_parser_simple_cast_expression (parser);
9273
9274 /* Get another operator token. Look up its precedence to avoid
9275 building a useless (immediately popped) stack entry for common
9276 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9277 token = cp_lexer_peek_token (parser->lexer);
9278 lookahead_prec = TOKEN_PRECEDENCE (token);
9279 if (lookahead_prec != PREC_NOT_OPERATOR
9280 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9281 lookahead_prec = PREC_NOT_OPERATOR;
9282 if (lookahead_prec > new_prec)
9283 {
9284 /* ... and prepare to parse the RHS of the new, higher priority
9285 expression. Since precedence levels on the stack are
9286 monotonically increasing, we do not have to care about
9287 stack overflows. */
9288 *sp = current;
9289 ++sp;
9290 current.lhs = rhs;
9291 current.lhs_type = rhs_type;
9292 current.prec = new_prec;
9293 new_prec = lookahead_prec;
9294 goto get_rhs;
9295
9296 pop:
9297 lookahead_prec = new_prec;
9298 /* If the stack is not empty, we have parsed into LHS the right side
9299 (`4' in the example above) of an expression we had suspended.
9300 We can use the information on the stack to recover the LHS (`3')
9301 from the stack together with the tree code (`MULT_EXPR'), and
9302 the precedence of the higher level subexpression
9303 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9304 which will be used to actually build the additive expression. */
9305 rhs = current.lhs;
9306 rhs_type = current.lhs_type;
9307 --sp;
9308 current = *sp;
9309 }
9310
9311 /* Undo the disabling of warnings done above. */
9312 if (current.tree_type == TRUTH_ANDIF_EXPR)
9313 c_inhibit_evaluation_warnings -=
9314 cp_fully_fold (current.lhs) == truthvalue_false_node;
9315 else if (current.tree_type == TRUTH_ORIF_EXPR)
9316 c_inhibit_evaluation_warnings -=
9317 cp_fully_fold (current.lhs) == truthvalue_true_node;
9318
9319 if (warn_logical_not_paren
9320 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9321 && current.lhs_type == TRUTH_NOT_EXPR
9322 /* Avoid warning for !!x == y. */
9323 && (TREE_CODE (current.lhs) != NE_EXPR
9324 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9325 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9326 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9327 /* Avoid warning for !b == y where b is boolean. */
9328 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9329 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9330 != BOOLEAN_TYPE))))
9331 /* Avoid warning for !!b == y where b is boolean. */
9332 && (!DECL_P (current.lhs)
9333 || TREE_TYPE (current.lhs) == NULL_TREE
9334 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9335 warn_logical_not_parentheses (current.loc, current.tree_type,
9336 current.lhs, maybe_constant_value (rhs));
9337
9338 overload = NULL;
9339
9340 location_t combined_loc = make_location (current.loc,
9341 current.lhs.get_start (),
9342 rhs.get_finish ());
9343
9344 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9345 ERROR_MARK for everything that is not a binary expression.
9346 This makes warn_about_parentheses miss some warnings that
9347 involve unary operators. For unary expressions we should
9348 pass the correct tree_code unless the unary expression was
9349 surrounded by parentheses.
9350 */
9351 if (no_toplevel_fold_p
9352 && lookahead_prec <= current.prec
9353 && sp == stack)
9354 {
9355 if (current.lhs == error_mark_node || rhs == error_mark_node)
9356 current.lhs = error_mark_node;
9357 else
9358 {
9359 current.lhs
9360 = build_min (current.tree_type,
9361 TREE_CODE_CLASS (current.tree_type)
9362 == tcc_comparison
9363 ? boolean_type_node : TREE_TYPE (current.lhs),
9364 current.lhs.get_value (), rhs.get_value ());
9365 SET_EXPR_LOCATION (current.lhs, combined_loc);
9366 }
9367 }
9368 else
9369 {
9370 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9371 current.lhs, current.lhs_type,
9372 rhs, rhs_type, &overload,
9373 complain_flags (decltype_p));
9374 /* TODO: build_x_binary_op doesn't always honor the location. */
9375 current.lhs.set_location (combined_loc);
9376 }
9377 current.lhs_type = current.tree_type;
9378
9379 /* If the binary operator required the use of an overloaded operator,
9380 then this expression cannot be an integral constant-expression.
9381 An overloaded operator can be used even if both operands are
9382 otherwise permissible in an integral constant-expression if at
9383 least one of the operands is of enumeration type. */
9384
9385 if (overload
9386 && cp_parser_non_integral_constant_expression (parser,
9387 NIC_OVERLOADED))
9388 return error_mark_node;
9389 }
9390
9391 return current.lhs;
9392 }
9393
9394 static cp_expr
9395 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9396 bool no_toplevel_fold_p,
9397 enum cp_parser_prec prec,
9398 cp_id_kind * pidk)
9399 {
9400 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9401 /*decltype*/false, prec, pidk);
9402 }
9403
9404 /* Parse the `? expression : assignment-expression' part of a
9405 conditional-expression. The LOGICAL_OR_EXPR is the
9406 logical-or-expression that started the conditional-expression.
9407 Returns a representation of the entire conditional-expression.
9408
9409 This routine is used by cp_parser_assignment_expression.
9410
9411 ? expression : assignment-expression
9412
9413 GNU Extensions:
9414
9415 ? : assignment-expression */
9416
9417 static tree
9418 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9419 {
9420 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9421 cp_expr assignment_expr;
9422 struct cp_token *token;
9423 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9424
9425 /* Consume the `?' token. */
9426 cp_lexer_consume_token (parser->lexer);
9427 token = cp_lexer_peek_token (parser->lexer);
9428 if (cp_parser_allow_gnu_extensions_p (parser)
9429 && token->type == CPP_COLON)
9430 {
9431 pedwarn (token->location, OPT_Wpedantic,
9432 "ISO C++ does not allow ?: with omitted middle operand");
9433 /* Implicit true clause. */
9434 expr = NULL_TREE;
9435 c_inhibit_evaluation_warnings +=
9436 folded_logical_or_expr == truthvalue_true_node;
9437 warn_for_omitted_condop (token->location, logical_or_expr);
9438 }
9439 else
9440 {
9441 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9442 parser->colon_corrects_to_scope_p = false;
9443 /* Parse the expression. */
9444 c_inhibit_evaluation_warnings +=
9445 folded_logical_or_expr == truthvalue_false_node;
9446 expr = cp_parser_expression (parser);
9447 c_inhibit_evaluation_warnings +=
9448 ((folded_logical_or_expr == truthvalue_true_node)
9449 - (folded_logical_or_expr == truthvalue_false_node));
9450 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9451 }
9452
9453 /* The next token should be a `:'. */
9454 cp_parser_require (parser, CPP_COLON, RT_COLON);
9455 /* Parse the assignment-expression. */
9456 assignment_expr = cp_parser_assignment_expression (parser);
9457 c_inhibit_evaluation_warnings -=
9458 folded_logical_or_expr == truthvalue_true_node;
9459
9460 /* Make a location:
9461 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9462 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9463 with the caret at the "?", ranging from the start of
9464 the logical_or_expr to the end of the assignment_expr. */
9465 loc = make_location (loc,
9466 logical_or_expr.get_start (),
9467 assignment_expr.get_finish ());
9468
9469 /* Build the conditional-expression. */
9470 return build_x_conditional_expr (loc, logical_or_expr,
9471 expr,
9472 assignment_expr,
9473 tf_warning_or_error);
9474 }
9475
9476 /* Parse an assignment-expression.
9477
9478 assignment-expression:
9479 conditional-expression
9480 logical-or-expression assignment-operator assignment_expression
9481 throw-expression
9482
9483 CAST_P is true if this expression is the target of a cast.
9484 DECLTYPE_P is true if this expression is the operand of decltype.
9485
9486 Returns a representation for the expression. */
9487
9488 static cp_expr
9489 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9490 bool cast_p, bool decltype_p)
9491 {
9492 cp_expr expr;
9493
9494 /* If the next token is the `throw' keyword, then we're looking at
9495 a throw-expression. */
9496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9497 expr = cp_parser_throw_expression (parser);
9498 /* Otherwise, it must be that we are looking at a
9499 logical-or-expression. */
9500 else
9501 {
9502 /* Parse the binary expressions (logical-or-expression). */
9503 expr = cp_parser_binary_expression (parser, cast_p, false,
9504 decltype_p,
9505 PREC_NOT_OPERATOR, pidk);
9506 /* If the next token is a `?' then we're actually looking at a
9507 conditional-expression. */
9508 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9509 return cp_parser_question_colon_clause (parser, expr);
9510 else
9511 {
9512 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9513
9514 /* If it's an assignment-operator, we're using the second
9515 production. */
9516 enum tree_code assignment_operator
9517 = cp_parser_assignment_operator_opt (parser);
9518 if (assignment_operator != ERROR_MARK)
9519 {
9520 bool non_constant_p;
9521
9522 /* Parse the right-hand side of the assignment. */
9523 cp_expr rhs = cp_parser_initializer_clause (parser,
9524 &non_constant_p);
9525
9526 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9527 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9528
9529 /* An assignment may not appear in a
9530 constant-expression. */
9531 if (cp_parser_non_integral_constant_expression (parser,
9532 NIC_ASSIGNMENT))
9533 return error_mark_node;
9534 /* Build the assignment expression. Its default
9535 location:
9536 LHS = RHS
9537 ~~~~^~~~~
9538 is the location of the '=' token as the
9539 caret, ranging from the start of the lhs to the
9540 end of the rhs. */
9541 loc = make_location (loc,
9542 expr.get_start (),
9543 rhs.get_finish ());
9544 expr = build_x_modify_expr (loc, expr,
9545 assignment_operator,
9546 rhs,
9547 complain_flags (decltype_p));
9548 /* TODO: build_x_modify_expr doesn't honor the location,
9549 so we must set it here. */
9550 expr.set_location (loc);
9551 }
9552 }
9553 }
9554
9555 return expr;
9556 }
9557
9558 /* Parse an (optional) assignment-operator.
9559
9560 assignment-operator: one of
9561 = *= /= %= += -= >>= <<= &= ^= |=
9562
9563 GNU Extension:
9564
9565 assignment-operator: one of
9566 <?= >?=
9567
9568 If the next token is an assignment operator, the corresponding tree
9569 code is returned, and the token is consumed. For example, for
9570 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9571 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9572 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9573 operator, ERROR_MARK is returned. */
9574
9575 static enum tree_code
9576 cp_parser_assignment_operator_opt (cp_parser* parser)
9577 {
9578 enum tree_code op;
9579 cp_token *token;
9580
9581 /* Peek at the next token. */
9582 token = cp_lexer_peek_token (parser->lexer);
9583
9584 switch (token->type)
9585 {
9586 case CPP_EQ:
9587 op = NOP_EXPR;
9588 break;
9589
9590 case CPP_MULT_EQ:
9591 op = MULT_EXPR;
9592 break;
9593
9594 case CPP_DIV_EQ:
9595 op = TRUNC_DIV_EXPR;
9596 break;
9597
9598 case CPP_MOD_EQ:
9599 op = TRUNC_MOD_EXPR;
9600 break;
9601
9602 case CPP_PLUS_EQ:
9603 op = PLUS_EXPR;
9604 break;
9605
9606 case CPP_MINUS_EQ:
9607 op = MINUS_EXPR;
9608 break;
9609
9610 case CPP_RSHIFT_EQ:
9611 op = RSHIFT_EXPR;
9612 break;
9613
9614 case CPP_LSHIFT_EQ:
9615 op = LSHIFT_EXPR;
9616 break;
9617
9618 case CPP_AND_EQ:
9619 op = BIT_AND_EXPR;
9620 break;
9621
9622 case CPP_XOR_EQ:
9623 op = BIT_XOR_EXPR;
9624 break;
9625
9626 case CPP_OR_EQ:
9627 op = BIT_IOR_EXPR;
9628 break;
9629
9630 default:
9631 /* Nothing else is an assignment operator. */
9632 op = ERROR_MARK;
9633 }
9634
9635 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9636 if (op != ERROR_MARK
9637 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9638 op = ERROR_MARK;
9639
9640 /* If it was an assignment operator, consume it. */
9641 if (op != ERROR_MARK)
9642 cp_lexer_consume_token (parser->lexer);
9643
9644 return op;
9645 }
9646
9647 /* Parse an expression.
9648
9649 expression:
9650 assignment-expression
9651 expression , assignment-expression
9652
9653 CAST_P is true if this expression is the target of a cast.
9654 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9655 except possibly parenthesized or on the RHS of a comma (N3276).
9656
9657 Returns a representation of the expression. */
9658
9659 static cp_expr
9660 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9661 bool cast_p, bool decltype_p)
9662 {
9663 cp_expr expression = NULL_TREE;
9664 location_t loc = UNKNOWN_LOCATION;
9665
9666 while (true)
9667 {
9668 cp_expr assignment_expression;
9669
9670 /* Parse the next assignment-expression. */
9671 assignment_expression
9672 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9673
9674 /* We don't create a temporary for a call that is the immediate operand
9675 of decltype or on the RHS of a comma. But when we see a comma, we
9676 need to create a temporary for a call on the LHS. */
9677 if (decltype_p && !processing_template_decl
9678 && TREE_CODE (assignment_expression) == CALL_EXPR
9679 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9680 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9681 assignment_expression
9682 = build_cplus_new (TREE_TYPE (assignment_expression),
9683 assignment_expression, tf_warning_or_error);
9684
9685 /* If this is the first assignment-expression, we can just
9686 save it away. */
9687 if (!expression)
9688 expression = assignment_expression;
9689 else
9690 {
9691 /* Create a location with caret at the comma, ranging
9692 from the start of the LHS to the end of the RHS. */
9693 loc = make_location (loc,
9694 expression.get_start (),
9695 assignment_expression.get_finish ());
9696 expression = build_x_compound_expr (loc, expression,
9697 assignment_expression,
9698 complain_flags (decltype_p));
9699 expression.set_location (loc);
9700 }
9701 /* If the next token is not a comma, or we're in a fold-expression, then
9702 we are done with the expression. */
9703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9704 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9705 break;
9706 /* Consume the `,'. */
9707 loc = cp_lexer_peek_token (parser->lexer)->location;
9708 cp_lexer_consume_token (parser->lexer);
9709 /* A comma operator cannot appear in a constant-expression. */
9710 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9711 expression = error_mark_node;
9712 }
9713
9714 return expression;
9715 }
9716
9717 /* Parse a constant-expression.
9718
9719 constant-expression:
9720 conditional-expression
9721
9722 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9723 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9724 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9725 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9726 only parse a conditional-expression, otherwise parse an
9727 assignment-expression. See below for rationale. */
9728
9729 static cp_expr
9730 cp_parser_constant_expression (cp_parser* parser,
9731 bool allow_non_constant_p,
9732 bool *non_constant_p,
9733 bool strict_p)
9734 {
9735 bool saved_integral_constant_expression_p;
9736 bool saved_allow_non_integral_constant_expression_p;
9737 bool saved_non_integral_constant_expression_p;
9738 cp_expr expression;
9739
9740 /* It might seem that we could simply parse the
9741 conditional-expression, and then check to see if it were
9742 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9743 one that the compiler can figure out is constant, possibly after
9744 doing some simplifications or optimizations. The standard has a
9745 precise definition of constant-expression, and we must honor
9746 that, even though it is somewhat more restrictive.
9747
9748 For example:
9749
9750 int i[(2, 3)];
9751
9752 is not a legal declaration, because `(2, 3)' is not a
9753 constant-expression. The `,' operator is forbidden in a
9754 constant-expression. However, GCC's constant-folding machinery
9755 will fold this operation to an INTEGER_CST for `3'. */
9756
9757 /* Save the old settings. */
9758 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9759 saved_allow_non_integral_constant_expression_p
9760 = parser->allow_non_integral_constant_expression_p;
9761 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9762 /* We are now parsing a constant-expression. */
9763 parser->integral_constant_expression_p = true;
9764 parser->allow_non_integral_constant_expression_p
9765 = (allow_non_constant_p || cxx_dialect >= cxx11);
9766 parser->non_integral_constant_expression_p = false;
9767 /* Although the grammar says "conditional-expression", when not STRICT_P,
9768 we parse an "assignment-expression", which also permits
9769 "throw-expression" and the use of assignment operators. In the case
9770 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9771 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9772 actually essential that we look for an assignment-expression.
9773 For example, cp_parser_initializer_clauses uses this function to
9774 determine whether a particular assignment-expression is in fact
9775 constant. */
9776 if (strict_p)
9777 {
9778 /* Parse the binary expressions (logical-or-expression). */
9779 expression = cp_parser_binary_expression (parser, false, false, false,
9780 PREC_NOT_OPERATOR, NULL);
9781 /* If the next token is a `?' then we're actually looking at
9782 a conditional-expression; otherwise we're done. */
9783 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9784 expression = cp_parser_question_colon_clause (parser, expression);
9785 }
9786 else
9787 expression = cp_parser_assignment_expression (parser);
9788 /* Restore the old settings. */
9789 parser->integral_constant_expression_p
9790 = saved_integral_constant_expression_p;
9791 parser->allow_non_integral_constant_expression_p
9792 = saved_allow_non_integral_constant_expression_p;
9793 if (cxx_dialect >= cxx11)
9794 {
9795 /* Require an rvalue constant expression here; that's what our
9796 callers expect. Reference constant expressions are handled
9797 separately in e.g. cp_parser_template_argument. */
9798 tree decay = expression;
9799 if (TREE_TYPE (expression)
9800 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9801 decay = build_address (expression);
9802 bool is_const = potential_rvalue_constant_expression (decay);
9803 parser->non_integral_constant_expression_p = !is_const;
9804 if (!is_const && !allow_non_constant_p)
9805 require_potential_rvalue_constant_expression (decay);
9806 }
9807 if (allow_non_constant_p)
9808 *non_constant_p = parser->non_integral_constant_expression_p;
9809 parser->non_integral_constant_expression_p
9810 = saved_non_integral_constant_expression_p;
9811
9812 return expression;
9813 }
9814
9815 /* Parse __builtin_offsetof.
9816
9817 offsetof-expression:
9818 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9819
9820 offsetof-member-designator:
9821 id-expression
9822 | offsetof-member-designator "." id-expression
9823 | offsetof-member-designator "[" expression "]"
9824 | offsetof-member-designator "->" id-expression */
9825
9826 static cp_expr
9827 cp_parser_builtin_offsetof (cp_parser *parser)
9828 {
9829 int save_ice_p, save_non_ice_p;
9830 tree type;
9831 cp_expr expr;
9832 cp_id_kind dummy;
9833 cp_token *token;
9834 location_t finish_loc;
9835
9836 /* We're about to accept non-integral-constant things, but will
9837 definitely yield an integral constant expression. Save and
9838 restore these values around our local parsing. */
9839 save_ice_p = parser->integral_constant_expression_p;
9840 save_non_ice_p = parser->non_integral_constant_expression_p;
9841
9842 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9843
9844 /* Consume the "__builtin_offsetof" token. */
9845 cp_lexer_consume_token (parser->lexer);
9846 /* Consume the opening `('. */
9847 matching_parens parens;
9848 parens.require_open (parser);
9849 /* Parse the type-id. */
9850 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9851 {
9852 const char *saved_message = parser->type_definition_forbidden_message;
9853 parser->type_definition_forbidden_message
9854 = G_("types may not be defined within __builtin_offsetof");
9855 type = cp_parser_type_id (parser);
9856 parser->type_definition_forbidden_message = saved_message;
9857 }
9858 /* Look for the `,'. */
9859 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9860 token = cp_lexer_peek_token (parser->lexer);
9861
9862 /* Build the (type *)null that begins the traditional offsetof macro. */
9863 tree object_ptr
9864 = build_static_cast (build_pointer_type (type), null_pointer_node,
9865 tf_warning_or_error);
9866
9867 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9868 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9869 true, &dummy, token->location);
9870 while (true)
9871 {
9872 token = cp_lexer_peek_token (parser->lexer);
9873 switch (token->type)
9874 {
9875 case CPP_OPEN_SQUARE:
9876 /* offsetof-member-designator "[" expression "]" */
9877 expr = cp_parser_postfix_open_square_expression (parser, expr,
9878 true, false);
9879 break;
9880
9881 case CPP_DEREF:
9882 /* offsetof-member-designator "->" identifier */
9883 expr = grok_array_decl (token->location, expr,
9884 integer_zero_node, false);
9885 /* FALLTHRU */
9886
9887 case CPP_DOT:
9888 /* offsetof-member-designator "." identifier */
9889 cp_lexer_consume_token (parser->lexer);
9890 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9891 expr, true, &dummy,
9892 token->location);
9893 break;
9894
9895 case CPP_CLOSE_PAREN:
9896 /* Consume the ")" token. */
9897 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9898 cp_lexer_consume_token (parser->lexer);
9899 goto success;
9900
9901 default:
9902 /* Error. We know the following require will fail, but
9903 that gives the proper error message. */
9904 parens.require_close (parser);
9905 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9906 expr = error_mark_node;
9907 goto failure;
9908 }
9909 }
9910
9911 success:
9912 /* Make a location of the form:
9913 __builtin_offsetof (struct s, f)
9914 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9915 with caret at the type-id, ranging from the start of the
9916 "_builtin_offsetof" token to the close paren. */
9917 loc = make_location (loc, start_loc, finish_loc);
9918 /* The result will be an INTEGER_CST, so we need to explicitly
9919 preserve the location. */
9920 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9921
9922 failure:
9923 parser->integral_constant_expression_p = save_ice_p;
9924 parser->non_integral_constant_expression_p = save_non_ice_p;
9925
9926 expr = expr.maybe_add_location_wrapper ();
9927 return expr;
9928 }
9929
9930 /* Parse a trait expression.
9931
9932 Returns a representation of the expression, the underlying type
9933 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9934
9935 static cp_expr
9936 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9937 {
9938 cp_trait_kind kind;
9939 tree type1, type2 = NULL_TREE;
9940 bool binary = false;
9941 bool variadic = false;
9942
9943 switch (keyword)
9944 {
9945 case RID_HAS_NOTHROW_ASSIGN:
9946 kind = CPTK_HAS_NOTHROW_ASSIGN;
9947 break;
9948 case RID_HAS_NOTHROW_CONSTRUCTOR:
9949 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9950 break;
9951 case RID_HAS_NOTHROW_COPY:
9952 kind = CPTK_HAS_NOTHROW_COPY;
9953 break;
9954 case RID_HAS_TRIVIAL_ASSIGN:
9955 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9956 break;
9957 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9958 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9959 break;
9960 case RID_HAS_TRIVIAL_COPY:
9961 kind = CPTK_HAS_TRIVIAL_COPY;
9962 break;
9963 case RID_HAS_TRIVIAL_DESTRUCTOR:
9964 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9965 break;
9966 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9967 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9968 break;
9969 case RID_HAS_VIRTUAL_DESTRUCTOR:
9970 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9971 break;
9972 case RID_IS_ABSTRACT:
9973 kind = CPTK_IS_ABSTRACT;
9974 break;
9975 case RID_IS_AGGREGATE:
9976 kind = CPTK_IS_AGGREGATE;
9977 break;
9978 case RID_IS_BASE_OF:
9979 kind = CPTK_IS_BASE_OF;
9980 binary = true;
9981 break;
9982 case RID_IS_CLASS:
9983 kind = CPTK_IS_CLASS;
9984 break;
9985 case RID_IS_EMPTY:
9986 kind = CPTK_IS_EMPTY;
9987 break;
9988 case RID_IS_ENUM:
9989 kind = CPTK_IS_ENUM;
9990 break;
9991 case RID_IS_FINAL:
9992 kind = CPTK_IS_FINAL;
9993 break;
9994 case RID_IS_LITERAL_TYPE:
9995 kind = CPTK_IS_LITERAL_TYPE;
9996 break;
9997 case RID_IS_POD:
9998 kind = CPTK_IS_POD;
9999 break;
10000 case RID_IS_POLYMORPHIC:
10001 kind = CPTK_IS_POLYMORPHIC;
10002 break;
10003 case RID_IS_SAME_AS:
10004 kind = CPTK_IS_SAME_AS;
10005 binary = true;
10006 break;
10007 case RID_IS_STD_LAYOUT:
10008 kind = CPTK_IS_STD_LAYOUT;
10009 break;
10010 case RID_IS_TRIVIAL:
10011 kind = CPTK_IS_TRIVIAL;
10012 break;
10013 case RID_IS_TRIVIALLY_ASSIGNABLE:
10014 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10015 binary = true;
10016 break;
10017 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10018 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10019 variadic = true;
10020 break;
10021 case RID_IS_TRIVIALLY_COPYABLE:
10022 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10023 break;
10024 case RID_IS_UNION:
10025 kind = CPTK_IS_UNION;
10026 break;
10027 case RID_UNDERLYING_TYPE:
10028 kind = CPTK_UNDERLYING_TYPE;
10029 break;
10030 case RID_BASES:
10031 kind = CPTK_BASES;
10032 break;
10033 case RID_DIRECT_BASES:
10034 kind = CPTK_DIRECT_BASES;
10035 break;
10036 case RID_IS_ASSIGNABLE:
10037 kind = CPTK_IS_ASSIGNABLE;
10038 binary = true;
10039 break;
10040 case RID_IS_CONSTRUCTIBLE:
10041 kind = CPTK_IS_CONSTRUCTIBLE;
10042 variadic = true;
10043 break;
10044 default:
10045 gcc_unreachable ();
10046 }
10047
10048 /* Get location of initial token. */
10049 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10050
10051 /* Consume the token. */
10052 cp_lexer_consume_token (parser->lexer);
10053
10054 matching_parens parens;
10055 parens.require_open (parser);
10056
10057 {
10058 type_id_in_expr_sentinel s (parser);
10059 type1 = cp_parser_type_id (parser);
10060 }
10061
10062 if (type1 == error_mark_node)
10063 return error_mark_node;
10064
10065 if (binary)
10066 {
10067 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10068
10069 {
10070 type_id_in_expr_sentinel s (parser);
10071 type2 = cp_parser_type_id (parser);
10072 }
10073
10074 if (type2 == error_mark_node)
10075 return error_mark_node;
10076 }
10077 else if (variadic)
10078 {
10079 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10080 {
10081 cp_lexer_consume_token (parser->lexer);
10082 tree elt = cp_parser_type_id (parser);
10083 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10084 {
10085 cp_lexer_consume_token (parser->lexer);
10086 elt = make_pack_expansion (elt);
10087 }
10088 if (elt == error_mark_node)
10089 return error_mark_node;
10090 type2 = tree_cons (NULL_TREE, elt, type2);
10091 }
10092 }
10093
10094 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10095 parens.require_close (parser);
10096
10097 /* Construct a location of the form:
10098 __is_trivially_copyable(_Tp)
10099 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10100 with start == caret, finishing at the close-paren. */
10101 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10102
10103 /* Complete the trait expression, which may mean either processing
10104 the trait expr now or saving it for template instantiation. */
10105 switch (kind)
10106 {
10107 case CPTK_UNDERLYING_TYPE:
10108 return cp_expr (finish_underlying_type (type1), trait_loc);
10109 case CPTK_BASES:
10110 return cp_expr (finish_bases (type1, false), trait_loc);
10111 case CPTK_DIRECT_BASES:
10112 return cp_expr (finish_bases (type1, true), trait_loc);
10113 default:
10114 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10115 }
10116 }
10117
10118 /* Parse a lambda expression.
10119
10120 lambda-expression:
10121 lambda-introducer lambda-declarator [opt] compound-statement
10122
10123 Returns a representation of the expression. */
10124
10125 static cp_expr
10126 cp_parser_lambda_expression (cp_parser* parser)
10127 {
10128 tree lambda_expr = build_lambda_expr ();
10129 tree type;
10130 bool ok = true;
10131 cp_token *token = cp_lexer_peek_token (parser->lexer);
10132 cp_token_position start = 0;
10133
10134 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10135
10136 if (cp_unevaluated_operand)
10137 {
10138 if (!token->error_reported)
10139 {
10140 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10141 "lambda-expression in unevaluated context");
10142 token->error_reported = true;
10143 }
10144 ok = false;
10145 }
10146 else if (parser->in_template_argument_list_p)
10147 {
10148 if (!token->error_reported)
10149 {
10150 error_at (token->location, "lambda-expression in template-argument");
10151 token->error_reported = true;
10152 }
10153 ok = false;
10154 }
10155
10156 /* We may be in the middle of deferred access check. Disable
10157 it now. */
10158 push_deferring_access_checks (dk_no_deferred);
10159
10160 cp_parser_lambda_introducer (parser, lambda_expr);
10161
10162 type = begin_lambda_type (lambda_expr);
10163 if (type == error_mark_node)
10164 return error_mark_node;
10165
10166 record_lambda_scope (lambda_expr);
10167
10168 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10169 determine_visibility (TYPE_NAME (type));
10170
10171 /* Now that we've started the type, add the capture fields for any
10172 explicit captures. */
10173 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10174
10175 {
10176 /* Inside the class, surrounding template-parameter-lists do not apply. */
10177 unsigned int saved_num_template_parameter_lists
10178 = parser->num_template_parameter_lists;
10179 unsigned char in_statement = parser->in_statement;
10180 bool in_switch_statement_p = parser->in_switch_statement_p;
10181 bool fully_implicit_function_template_p
10182 = parser->fully_implicit_function_template_p;
10183 tree implicit_template_parms = parser->implicit_template_parms;
10184 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10185 bool auto_is_implicit_function_template_parm_p
10186 = parser->auto_is_implicit_function_template_parm_p;
10187
10188 parser->num_template_parameter_lists = 0;
10189 parser->in_statement = 0;
10190 parser->in_switch_statement_p = false;
10191 parser->fully_implicit_function_template_p = false;
10192 parser->implicit_template_parms = 0;
10193 parser->implicit_template_scope = 0;
10194 parser->auto_is_implicit_function_template_parm_p = false;
10195
10196 /* By virtue of defining a local class, a lambda expression has access to
10197 the private variables of enclosing classes. */
10198
10199 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10200
10201 if (ok && cp_parser_error_occurred (parser))
10202 ok = false;
10203
10204 if (ok)
10205 {
10206 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10207 && cp_parser_start_tentative_firewall (parser))
10208 start = token;
10209 cp_parser_lambda_body (parser, lambda_expr);
10210 }
10211 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10212 {
10213 if (cp_parser_skip_to_closing_brace (parser))
10214 cp_lexer_consume_token (parser->lexer);
10215 }
10216
10217 /* The capture list was built up in reverse order; fix that now. */
10218 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10219 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10220
10221 if (ok)
10222 maybe_add_lambda_conv_op (type);
10223
10224 type = finish_struct (type, /*attributes=*/NULL_TREE);
10225
10226 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10227 parser->in_statement = in_statement;
10228 parser->in_switch_statement_p = in_switch_statement_p;
10229 parser->fully_implicit_function_template_p
10230 = fully_implicit_function_template_p;
10231 parser->implicit_template_parms = implicit_template_parms;
10232 parser->implicit_template_scope = implicit_template_scope;
10233 parser->auto_is_implicit_function_template_parm_p
10234 = auto_is_implicit_function_template_parm_p;
10235 }
10236
10237 /* This field is only used during parsing of the lambda. */
10238 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10239
10240 /* This lambda shouldn't have any proxies left at this point. */
10241 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10242 /* And now that we're done, push proxies for an enclosing lambda. */
10243 insert_pending_capture_proxies ();
10244
10245 /* Update the lambda expression to a range. */
10246 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10247 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10248 token->location,
10249 end_tok->location);
10250
10251 if (ok)
10252 lambda_expr = build_lambda_object (lambda_expr);
10253 else
10254 lambda_expr = error_mark_node;
10255
10256 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10257
10258 pop_deferring_access_checks ();
10259
10260 return lambda_expr;
10261 }
10262
10263 /* Parse the beginning of a lambda expression.
10264
10265 lambda-introducer:
10266 [ lambda-capture [opt] ]
10267
10268 LAMBDA_EXPR is the current representation of the lambda expression. */
10269
10270 static void
10271 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10272 {
10273 /* Need commas after the first capture. */
10274 bool first = true;
10275
10276 /* Eat the leading `['. */
10277 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10278
10279 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10280 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10281 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10282 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10283 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10284 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10285
10286 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10287 {
10288 cp_lexer_consume_token (parser->lexer);
10289 first = false;
10290
10291 if (!(at_function_scope_p () || parsing_nsdmi ()))
10292 error ("non-local lambda expression cannot have a capture-default");
10293 }
10294
10295 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10296 {
10297 cp_token* capture_token;
10298 tree capture_id;
10299 tree capture_init_expr;
10300 cp_id_kind idk = CP_ID_KIND_NONE;
10301 bool explicit_init_p = false;
10302
10303 enum capture_kind_type
10304 {
10305 BY_COPY,
10306 BY_REFERENCE
10307 };
10308 enum capture_kind_type capture_kind = BY_COPY;
10309
10310 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10311 {
10312 error ("expected end of capture-list");
10313 return;
10314 }
10315
10316 if (first)
10317 first = false;
10318 else
10319 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10320
10321 /* Possibly capture `this'. */
10322 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10323 {
10324 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10325 if (cxx_dialect < cxx2a
10326 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10327 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10328 "with by-copy capture default");
10329 cp_lexer_consume_token (parser->lexer);
10330 add_capture (lambda_expr,
10331 /*id=*/this_identifier,
10332 /*initializer=*/finish_this_expr (),
10333 /*by_reference_p=*/true,
10334 explicit_init_p);
10335 continue;
10336 }
10337
10338 /* Possibly capture `*this'. */
10339 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10340 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10341 {
10342 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10343 if (cxx_dialect < cxx17)
10344 pedwarn (loc, 0, "%<*this%> capture only available with "
10345 "-std=c++17 or -std=gnu++17");
10346 cp_lexer_consume_token (parser->lexer);
10347 cp_lexer_consume_token (parser->lexer);
10348 add_capture (lambda_expr,
10349 /*id=*/this_identifier,
10350 /*initializer=*/finish_this_expr (),
10351 /*by_reference_p=*/false,
10352 explicit_init_p);
10353 continue;
10354 }
10355
10356 /* Remember whether we want to capture as a reference or not. */
10357 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10358 {
10359 capture_kind = BY_REFERENCE;
10360 cp_lexer_consume_token (parser->lexer);
10361 }
10362
10363 /* Get the identifier. */
10364 capture_token = cp_lexer_peek_token (parser->lexer);
10365 capture_id = cp_parser_identifier (parser);
10366
10367 if (capture_id == error_mark_node)
10368 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10369 delimiters, but I modified this to stop on unnested ']' as well. It
10370 was already changed to stop on unnested '}', so the
10371 "closing_parenthesis" name is no more misleading with my change. */
10372 {
10373 cp_parser_skip_to_closing_parenthesis (parser,
10374 /*recovering=*/true,
10375 /*or_comma=*/true,
10376 /*consume_paren=*/true);
10377 break;
10378 }
10379
10380 /* Find the initializer for this capture. */
10381 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10382 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10383 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10384 {
10385 bool direct, non_constant;
10386 /* An explicit initializer exists. */
10387 if (cxx_dialect < cxx14)
10388 pedwarn (input_location, 0,
10389 "lambda capture initializers "
10390 "only available with -std=c++14 or -std=gnu++14");
10391 capture_init_expr = cp_parser_initializer (parser, &direct,
10392 &non_constant, true);
10393 explicit_init_p = true;
10394 if (capture_init_expr == NULL_TREE)
10395 {
10396 error ("empty initializer for lambda init-capture");
10397 capture_init_expr = error_mark_node;
10398 }
10399 }
10400 else
10401 {
10402 const char* error_msg;
10403
10404 /* Turn the identifier into an id-expression. */
10405 capture_init_expr
10406 = cp_parser_lookup_name_simple (parser, capture_id,
10407 capture_token->location);
10408
10409 if (capture_init_expr == error_mark_node)
10410 {
10411 unqualified_name_lookup_error (capture_id);
10412 continue;
10413 }
10414 else if (!VAR_P (capture_init_expr)
10415 && TREE_CODE (capture_init_expr) != PARM_DECL)
10416 {
10417 error_at (capture_token->location,
10418 "capture of non-variable %qE",
10419 capture_init_expr);
10420 if (DECL_P (capture_init_expr))
10421 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10422 "%q#D declared here", capture_init_expr);
10423 continue;
10424 }
10425 if (VAR_P (capture_init_expr)
10426 && decl_storage_duration (capture_init_expr) != dk_auto)
10427 {
10428 if (pedwarn (capture_token->location, 0, "capture of variable "
10429 "%qD with non-automatic storage duration",
10430 capture_init_expr))
10431 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10432 "%q#D declared here", capture_init_expr);
10433 continue;
10434 }
10435
10436 capture_init_expr
10437 = finish_id_expression
10438 (capture_id,
10439 capture_init_expr,
10440 parser->scope,
10441 &idk,
10442 /*integral_constant_expression_p=*/false,
10443 /*allow_non_integral_constant_expression_p=*/false,
10444 /*non_integral_constant_expression_p=*/NULL,
10445 /*template_p=*/false,
10446 /*done=*/true,
10447 /*address_p=*/false,
10448 /*template_arg_p=*/false,
10449 &error_msg,
10450 capture_token->location);
10451
10452 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10453 {
10454 cp_lexer_consume_token (parser->lexer);
10455 capture_init_expr = make_pack_expansion (capture_init_expr);
10456 }
10457 }
10458
10459 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10460 && !explicit_init_p)
10461 {
10462 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10463 && capture_kind == BY_COPY)
10464 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10465 "of %qD redundant with by-copy capture default",
10466 capture_id);
10467 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10468 && capture_kind == BY_REFERENCE)
10469 pedwarn (capture_token->location, 0, "explicit by-reference "
10470 "capture of %qD redundant with by-reference capture "
10471 "default", capture_id);
10472 }
10473
10474 add_capture (lambda_expr,
10475 capture_id,
10476 capture_init_expr,
10477 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10478 explicit_init_p);
10479
10480 /* If there is any qualification still in effect, clear it
10481 now; we will be starting fresh with the next capture. */
10482 parser->scope = NULL_TREE;
10483 parser->qualifying_scope = NULL_TREE;
10484 parser->object_scope = NULL_TREE;
10485 }
10486
10487 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10488 }
10489
10490 /* Parse the (optional) middle of a lambda expression.
10491
10492 lambda-declarator:
10493 < template-parameter-list [opt] >
10494 ( parameter-declaration-clause [opt] )
10495 attribute-specifier [opt]
10496 decl-specifier-seq [opt]
10497 exception-specification [opt]
10498 lambda-return-type-clause [opt]
10499
10500 LAMBDA_EXPR is the current representation of the lambda expression. */
10501
10502 static bool
10503 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10504 {
10505 /* 5.1.1.4 of the standard says:
10506 If a lambda-expression does not include a lambda-declarator, it is as if
10507 the lambda-declarator were ().
10508 This means an empty parameter list, no attributes, and no exception
10509 specification. */
10510 tree param_list = void_list_node;
10511 tree attributes = NULL_TREE;
10512 tree exception_spec = NULL_TREE;
10513 tree template_param_list = NULL_TREE;
10514 tree tx_qual = NULL_TREE;
10515 tree return_type = NULL_TREE;
10516 cp_decl_specifier_seq lambda_specs;
10517 clear_decl_specs (&lambda_specs);
10518
10519 /* The template-parameter-list is optional, but must begin with
10520 an opening angle if present. */
10521 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10522 {
10523 if (cxx_dialect < cxx14)
10524 pedwarn (parser->lexer->next_token->location, 0,
10525 "lambda templates are only available with "
10526 "-std=c++14 or -std=gnu++14");
10527 else if (cxx_dialect < cxx2a)
10528 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10529 "lambda templates are only available with "
10530 "-std=c++2a or -std=gnu++2a");
10531
10532 cp_lexer_consume_token (parser->lexer);
10533
10534 template_param_list = cp_parser_template_parameter_list (parser);
10535
10536 cp_parser_skip_to_end_of_template_parameter_list (parser);
10537
10538 /* We just processed one more parameter list. */
10539 ++parser->num_template_parameter_lists;
10540 }
10541
10542 /* The parameter-declaration-clause is optional (unless
10543 template-parameter-list was given), but must begin with an
10544 opening parenthesis if present. */
10545 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10546 {
10547 matching_parens parens;
10548 parens.consume_open (parser);
10549
10550 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10551
10552 /* Parse parameters. */
10553 param_list = cp_parser_parameter_declaration_clause (parser);
10554
10555 /* Default arguments shall not be specified in the
10556 parameter-declaration-clause of a lambda-declarator. */
10557 if (cxx_dialect < cxx14)
10558 for (tree t = param_list; t; t = TREE_CHAIN (t))
10559 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10560 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10561 "default argument specified for lambda parameter");
10562
10563 parens.require_close (parser);
10564
10565 attributes = cp_parser_attributes_opt (parser);
10566
10567 /* In the decl-specifier-seq of the lambda-declarator, each
10568 decl-specifier shall either be mutable or constexpr. */
10569 int declares_class_or_enum;
10570 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10571 cp_parser_decl_specifier_seq (parser,
10572 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10573 &lambda_specs, &declares_class_or_enum);
10574 if (lambda_specs.storage_class == sc_mutable)
10575 {
10576 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10577 if (lambda_specs.conflicting_specifiers_p)
10578 error_at (lambda_specs.locations[ds_storage_class],
10579 "duplicate %<mutable%>");
10580 }
10581
10582 tx_qual = cp_parser_tx_qualifier_opt (parser);
10583
10584 /* Parse optional exception specification. */
10585 exception_spec = cp_parser_exception_specification_opt (parser);
10586
10587 /* Parse optional trailing return type. */
10588 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10589 {
10590 cp_lexer_consume_token (parser->lexer);
10591 return_type = cp_parser_trailing_type_id (parser);
10592 }
10593
10594 /* The function parameters must be in scope all the way until after the
10595 trailing-return-type in case of decltype. */
10596 pop_bindings_and_leave_scope ();
10597 }
10598 else if (template_param_list != NULL_TREE) // generate diagnostic
10599 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10600
10601 /* Create the function call operator.
10602
10603 Messing with declarators like this is no uglier than building up the
10604 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10605 other code. */
10606 {
10607 cp_decl_specifier_seq return_type_specs;
10608 cp_declarator* declarator;
10609 tree fco;
10610 int quals;
10611 void *p;
10612
10613 clear_decl_specs (&return_type_specs);
10614 return_type_specs.type = make_auto ();
10615
10616 if (lambda_specs.locations[ds_constexpr])
10617 {
10618 if (cxx_dialect >= cxx17)
10619 return_type_specs.locations[ds_constexpr]
10620 = lambda_specs.locations[ds_constexpr];
10621 else
10622 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10623 "lambda only available with -std=c++17 or -std=gnu++17");
10624 }
10625
10626 p = obstack_alloc (&declarator_obstack, 0);
10627
10628 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10629
10630 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10631 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10632 declarator = make_call_declarator (declarator, param_list, quals,
10633 VIRT_SPEC_UNSPECIFIED,
10634 REF_QUAL_NONE,
10635 tx_qual,
10636 exception_spec,
10637 /*late_return_type=*/NULL_TREE,
10638 /*requires_clause*/NULL_TREE);
10639 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10640 if (return_type)
10641 declarator->u.function.late_return_type = return_type;
10642
10643 fco = grokmethod (&return_type_specs,
10644 declarator,
10645 attributes);
10646 if (fco != error_mark_node)
10647 {
10648 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10649 DECL_ARTIFICIAL (fco) = 1;
10650 /* Give the object parameter a different name. */
10651 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10652 }
10653 if (template_param_list)
10654 {
10655 fco = finish_member_template_decl (fco);
10656 finish_template_decl (template_param_list);
10657 --parser->num_template_parameter_lists;
10658 }
10659 else if (parser->fully_implicit_function_template_p)
10660 fco = finish_fully_implicit_template (parser, fco);
10661
10662 finish_member_declaration (fco);
10663
10664 obstack_free (&declarator_obstack, p);
10665
10666 return (fco != error_mark_node);
10667 }
10668 }
10669
10670 /* Parse the body of a lambda expression, which is simply
10671
10672 compound-statement
10673
10674 but which requires special handling.
10675 LAMBDA_EXPR is the current representation of the lambda expression. */
10676
10677 static void
10678 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10679 {
10680 bool nested = (current_function_decl != NULL_TREE);
10681 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10682 bool in_function_body = parser->in_function_body;
10683
10684 if (nested)
10685 push_function_context ();
10686 else
10687 /* Still increment function_depth so that we don't GC in the
10688 middle of an expression. */
10689 ++function_depth;
10690
10691 vec<tree> omp_privatization_save;
10692 save_omp_privatization_clauses (omp_privatization_save);
10693 /* Clear this in case we're in the middle of a default argument. */
10694 parser->local_variables_forbidden_p = false;
10695 parser->in_function_body = true;
10696
10697 {
10698 local_specialization_stack s (lss_copy);
10699 tree fco = lambda_function (lambda_expr);
10700 tree body = start_lambda_function (fco, lambda_expr);
10701 matching_braces braces;
10702
10703 if (braces.require_open (parser))
10704 {
10705 tree compound_stmt = begin_compound_stmt (0);
10706
10707 /* Originally C++11 required us to peek for 'return expr'; and
10708 process it specially here to deduce the return type. N3638
10709 removed the need for that. */
10710
10711 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10712 cp_parser_label_declaration (parser);
10713 cp_parser_statement_seq_opt (parser, NULL_TREE);
10714 braces.require_close (parser);
10715
10716 finish_compound_stmt (compound_stmt);
10717 }
10718
10719 finish_lambda_function (body);
10720 }
10721
10722 restore_omp_privatization_clauses (omp_privatization_save);
10723 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10724 parser->in_function_body = in_function_body;
10725 if (nested)
10726 pop_function_context();
10727 else
10728 --function_depth;
10729 }
10730
10731 /* Statements [gram.stmt.stmt] */
10732
10733 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10734
10735 static void
10736 add_debug_begin_stmt (location_t loc)
10737 {
10738 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10739 return;
10740 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10741 /* A concept is never expanded normally. */
10742 return;
10743
10744 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10745 SET_EXPR_LOCATION (stmt, loc);
10746 add_stmt (stmt);
10747 }
10748
10749 /* Parse a statement.
10750
10751 statement:
10752 labeled-statement
10753 expression-statement
10754 compound-statement
10755 selection-statement
10756 iteration-statement
10757 jump-statement
10758 declaration-statement
10759 try-block
10760
10761 C++11:
10762
10763 statement:
10764 labeled-statement
10765 attribute-specifier-seq (opt) expression-statement
10766 attribute-specifier-seq (opt) compound-statement
10767 attribute-specifier-seq (opt) selection-statement
10768 attribute-specifier-seq (opt) iteration-statement
10769 attribute-specifier-seq (opt) jump-statement
10770 declaration-statement
10771 attribute-specifier-seq (opt) try-block
10772
10773 init-statement:
10774 expression-statement
10775 simple-declaration
10776
10777 TM Extension:
10778
10779 statement:
10780 atomic-statement
10781
10782 IN_COMPOUND is true when the statement is nested inside a
10783 cp_parser_compound_statement; this matters for certain pragmas.
10784
10785 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10786 is a (possibly labeled) if statement which is not enclosed in braces
10787 and has an else clause. This is used to implement -Wparentheses.
10788
10789 CHAIN is a vector of if-else-if conditions. */
10790
10791 static void
10792 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10793 bool in_compound, bool *if_p, vec<tree> *chain,
10794 location_t *loc_after_labels)
10795 {
10796 tree statement, std_attrs = NULL_TREE;
10797 cp_token *token;
10798 location_t statement_location, attrs_location;
10799
10800 restart:
10801 if (if_p != NULL)
10802 *if_p = false;
10803 /* There is no statement yet. */
10804 statement = NULL_TREE;
10805
10806 saved_token_sentinel saved_tokens (parser->lexer);
10807 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10808 if (c_dialect_objc ())
10809 /* In obj-c++, seeing '[[' might be the either the beginning of
10810 c++11 attributes, or a nested objc-message-expression. So
10811 let's parse the c++11 attributes tentatively. */
10812 cp_parser_parse_tentatively (parser);
10813 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10814 if (c_dialect_objc ())
10815 {
10816 if (!cp_parser_parse_definitely (parser))
10817 std_attrs = NULL_TREE;
10818 }
10819
10820 /* Peek at the next token. */
10821 token = cp_lexer_peek_token (parser->lexer);
10822 /* Remember the location of the first token in the statement. */
10823 statement_location = token->location;
10824 add_debug_begin_stmt (statement_location);
10825 /* If this is a keyword, then that will often determine what kind of
10826 statement we have. */
10827 if (token->type == CPP_KEYWORD)
10828 {
10829 enum rid keyword = token->keyword;
10830
10831 switch (keyword)
10832 {
10833 case RID_CASE:
10834 case RID_DEFAULT:
10835 /* Looks like a labeled-statement with a case label.
10836 Parse the label, and then use tail recursion to parse
10837 the statement. */
10838 cp_parser_label_for_labeled_statement (parser, std_attrs);
10839 in_compound = false;
10840 goto restart;
10841
10842 case RID_IF:
10843 case RID_SWITCH:
10844 statement = cp_parser_selection_statement (parser, if_p, chain);
10845 break;
10846
10847 case RID_WHILE:
10848 case RID_DO:
10849 case RID_FOR:
10850 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10851 break;
10852
10853 case RID_BREAK:
10854 case RID_CONTINUE:
10855 case RID_RETURN:
10856 case RID_GOTO:
10857 statement = cp_parser_jump_statement (parser);
10858 break;
10859
10860 /* Objective-C++ exception-handling constructs. */
10861 case RID_AT_TRY:
10862 case RID_AT_CATCH:
10863 case RID_AT_FINALLY:
10864 case RID_AT_SYNCHRONIZED:
10865 case RID_AT_THROW:
10866 statement = cp_parser_objc_statement (parser);
10867 break;
10868
10869 case RID_TRY:
10870 statement = cp_parser_try_block (parser);
10871 break;
10872
10873 case RID_NAMESPACE:
10874 /* This must be a namespace alias definition. */
10875 cp_parser_declaration_statement (parser);
10876 return;
10877
10878 case RID_TRANSACTION_ATOMIC:
10879 case RID_TRANSACTION_RELAXED:
10880 case RID_SYNCHRONIZED:
10881 case RID_ATOMIC_NOEXCEPT:
10882 case RID_ATOMIC_CANCEL:
10883 statement = cp_parser_transaction (parser, token);
10884 break;
10885 case RID_TRANSACTION_CANCEL:
10886 statement = cp_parser_transaction_cancel (parser);
10887 break;
10888
10889 default:
10890 /* It might be a keyword like `int' that can start a
10891 declaration-statement. */
10892 break;
10893 }
10894 }
10895 else if (token->type == CPP_NAME)
10896 {
10897 /* If the next token is a `:', then we are looking at a
10898 labeled-statement. */
10899 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10900 if (token->type == CPP_COLON)
10901 {
10902 /* Looks like a labeled-statement with an ordinary label.
10903 Parse the label, and then use tail recursion to parse
10904 the statement. */
10905
10906 cp_parser_label_for_labeled_statement (parser, std_attrs);
10907 in_compound = false;
10908 goto restart;
10909 }
10910 }
10911 /* Anything that starts with a `{' must be a compound-statement. */
10912 else if (token->type == CPP_OPEN_BRACE)
10913 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10914 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10915 a statement all its own. */
10916 else if (token->type == CPP_PRAGMA)
10917 {
10918 /* Only certain OpenMP pragmas are attached to statements, and thus
10919 are considered statements themselves. All others are not. In
10920 the context of a compound, accept the pragma as a "statement" and
10921 return so that we can check for a close brace. Otherwise we
10922 require a real statement and must go back and read one. */
10923 if (in_compound)
10924 cp_parser_pragma (parser, pragma_compound, if_p);
10925 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10926 goto restart;
10927 return;
10928 }
10929 else if (token->type == CPP_EOF)
10930 {
10931 cp_parser_error (parser, "expected statement");
10932 return;
10933 }
10934
10935 /* Everything else must be a declaration-statement or an
10936 expression-statement. Try for the declaration-statement
10937 first, unless we are looking at a `;', in which case we know that
10938 we have an expression-statement. */
10939 if (!statement)
10940 {
10941 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10942 {
10943 if (std_attrs != NULL_TREE)
10944 {
10945 /* Attributes should be parsed as part of the the
10946 declaration, so let's un-parse them. */
10947 saved_tokens.rollback();
10948 std_attrs = NULL_TREE;
10949 }
10950
10951 cp_parser_parse_tentatively (parser);
10952 /* Try to parse the declaration-statement. */
10953 cp_parser_declaration_statement (parser);
10954 /* If that worked, we're done. */
10955 if (cp_parser_parse_definitely (parser))
10956 return;
10957 }
10958 /* All preceding labels have been parsed at this point. */
10959 if (loc_after_labels != NULL)
10960 *loc_after_labels = statement_location;
10961
10962 /* Look for an expression-statement instead. */
10963 statement = cp_parser_expression_statement (parser, in_statement_expr);
10964
10965 /* Handle [[fallthrough]];. */
10966 if (attribute_fallthrough_p (std_attrs))
10967 {
10968 /* The next token after the fallthrough attribute is ';'. */
10969 if (statement == NULL_TREE)
10970 {
10971 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10972 statement = build_call_expr_internal_loc (statement_location,
10973 IFN_FALLTHROUGH,
10974 void_type_node, 0);
10975 finish_expr_stmt (statement);
10976 }
10977 else
10978 warning_at (statement_location, OPT_Wattributes,
10979 "%<fallthrough%> attribute not followed by %<;%>");
10980 std_attrs = NULL_TREE;
10981 }
10982 }
10983
10984 /* Set the line number for the statement. */
10985 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10986 SET_EXPR_LOCATION (statement, statement_location);
10987
10988 /* Allow "[[fallthrough]];", but warn otherwise. */
10989 if (std_attrs != NULL_TREE)
10990 warning_at (attrs_location,
10991 OPT_Wattributes,
10992 "attributes at the beginning of statement are ignored");
10993 }
10994
10995 /* Append ATTR to attribute list ATTRS. */
10996
10997 static tree
10998 attr_chainon (tree attrs, tree attr)
10999 {
11000 if (attrs == error_mark_node)
11001 return error_mark_node;
11002 if (attr == error_mark_node)
11003 return error_mark_node;
11004 return chainon (attrs, attr);
11005 }
11006
11007 /* Parse the label for a labeled-statement, i.e.
11008
11009 identifier :
11010 case constant-expression :
11011 default :
11012
11013 GNU Extension:
11014 case constant-expression ... constant-expression : statement
11015
11016 When a label is parsed without errors, the label is added to the
11017 parse tree by the finish_* functions, so this function doesn't
11018 have to return the label. */
11019
11020 static void
11021 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11022 {
11023 cp_token *token;
11024 tree label = NULL_TREE;
11025 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11026
11027 /* The next token should be an identifier. */
11028 token = cp_lexer_peek_token (parser->lexer);
11029 if (token->type != CPP_NAME
11030 && token->type != CPP_KEYWORD)
11031 {
11032 cp_parser_error (parser, "expected labeled-statement");
11033 return;
11034 }
11035
11036 /* Remember whether this case or a user-defined label is allowed to fall
11037 through to. */
11038 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11039
11040 parser->colon_corrects_to_scope_p = false;
11041 switch (token->keyword)
11042 {
11043 case RID_CASE:
11044 {
11045 tree expr, expr_hi;
11046 cp_token *ellipsis;
11047
11048 /* Consume the `case' token. */
11049 cp_lexer_consume_token (parser->lexer);
11050 /* Parse the constant-expression. */
11051 expr = cp_parser_constant_expression (parser);
11052 if (check_for_bare_parameter_packs (expr))
11053 expr = error_mark_node;
11054
11055 ellipsis = cp_lexer_peek_token (parser->lexer);
11056 if (ellipsis->type == CPP_ELLIPSIS)
11057 {
11058 /* Consume the `...' token. */
11059 cp_lexer_consume_token (parser->lexer);
11060 expr_hi = cp_parser_constant_expression (parser);
11061 if (check_for_bare_parameter_packs (expr_hi))
11062 expr_hi = error_mark_node;
11063
11064 /* We don't need to emit warnings here, as the common code
11065 will do this for us. */
11066 }
11067 else
11068 expr_hi = NULL_TREE;
11069
11070 if (parser->in_switch_statement_p)
11071 {
11072 tree l = finish_case_label (token->location, expr, expr_hi);
11073 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11074 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11075 }
11076 else
11077 error_at (token->location,
11078 "case label %qE not within a switch statement",
11079 expr);
11080 }
11081 break;
11082
11083 case RID_DEFAULT:
11084 /* Consume the `default' token. */
11085 cp_lexer_consume_token (parser->lexer);
11086
11087 if (parser->in_switch_statement_p)
11088 {
11089 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11090 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11091 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11092 }
11093 else
11094 error_at (token->location, "case label not within a switch statement");
11095 break;
11096
11097 default:
11098 /* Anything else must be an ordinary label. */
11099 label = finish_label_stmt (cp_parser_identifier (parser));
11100 if (label && TREE_CODE (label) == LABEL_DECL)
11101 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11102 break;
11103 }
11104
11105 /* Require the `:' token. */
11106 cp_parser_require (parser, CPP_COLON, RT_COLON);
11107
11108 /* An ordinary label may optionally be followed by attributes.
11109 However, this is only permitted if the attributes are then
11110 followed by a semicolon. This is because, for backward
11111 compatibility, when parsing
11112 lab: __attribute__ ((unused)) int i;
11113 we want the attribute to attach to "i", not "lab". */
11114 if (label != NULL_TREE
11115 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11116 {
11117 tree attrs;
11118 cp_parser_parse_tentatively (parser);
11119 attrs = cp_parser_gnu_attributes_opt (parser);
11120 if (attrs == NULL_TREE
11121 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11122 cp_parser_abort_tentative_parse (parser);
11123 else if (!cp_parser_parse_definitely (parser))
11124 ;
11125 else
11126 attributes = attr_chainon (attributes, attrs);
11127 }
11128
11129 if (attributes != NULL_TREE)
11130 cplus_decl_attributes (&label, attributes, 0);
11131
11132 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11133 }
11134
11135 /* Parse an expression-statement.
11136
11137 expression-statement:
11138 expression [opt] ;
11139
11140 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11141 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11142 indicates whether this expression-statement is part of an
11143 expression statement. */
11144
11145 static tree
11146 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11147 {
11148 tree statement = NULL_TREE;
11149 cp_token *token = cp_lexer_peek_token (parser->lexer);
11150 location_t loc = token->location;
11151
11152 /* There might be attribute fallthrough. */
11153 tree attr = cp_parser_gnu_attributes_opt (parser);
11154
11155 /* If the next token is a ';', then there is no expression
11156 statement. */
11157 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11158 {
11159 statement = cp_parser_expression (parser);
11160 if (statement == error_mark_node
11161 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11162 {
11163 cp_parser_skip_to_end_of_block_or_statement (parser);
11164 return error_mark_node;
11165 }
11166 }
11167
11168 /* Handle [[fallthrough]];. */
11169 if (attribute_fallthrough_p (attr))
11170 {
11171 /* The next token after the fallthrough attribute is ';'. */
11172 if (statement == NULL_TREE)
11173 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11174 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11175 void_type_node, 0);
11176 else
11177 warning_at (loc, OPT_Wattributes,
11178 "%<fallthrough%> attribute not followed by %<;%>");
11179 attr = NULL_TREE;
11180 }
11181
11182 /* Allow "[[fallthrough]];", but warn otherwise. */
11183 if (attr != NULL_TREE)
11184 warning_at (loc, OPT_Wattributes,
11185 "attributes at the beginning of statement are ignored");
11186
11187 /* Give a helpful message for "A<T>::type t;" and the like. */
11188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11189 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11190 {
11191 if (TREE_CODE (statement) == SCOPE_REF)
11192 error_at (token->location, "need %<typename%> before %qE because "
11193 "%qT is a dependent scope",
11194 statement, TREE_OPERAND (statement, 0));
11195 else if (is_overloaded_fn (statement)
11196 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11197 {
11198 /* A::A a; */
11199 tree fn = get_first_fn (statement);
11200 error_at (token->location,
11201 "%<%T::%D%> names the constructor, not the type",
11202 DECL_CONTEXT (fn), DECL_NAME (fn));
11203 }
11204 }
11205
11206 /* Consume the final `;'. */
11207 cp_parser_consume_semicolon_at_end_of_statement (parser);
11208
11209 if (in_statement_expr
11210 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11211 /* This is the final expression statement of a statement
11212 expression. */
11213 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11214 else if (statement)
11215 statement = finish_expr_stmt (statement);
11216
11217 return statement;
11218 }
11219
11220 /* Parse a compound-statement.
11221
11222 compound-statement:
11223 { statement-seq [opt] }
11224
11225 GNU extension:
11226
11227 compound-statement:
11228 { label-declaration-seq [opt] statement-seq [opt] }
11229
11230 label-declaration-seq:
11231 label-declaration
11232 label-declaration-seq label-declaration
11233
11234 Returns a tree representing the statement. */
11235
11236 static tree
11237 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11238 int bcs_flags, bool function_body)
11239 {
11240 tree compound_stmt;
11241 matching_braces braces;
11242
11243 /* Consume the `{'. */
11244 if (!braces.require_open (parser))
11245 return error_mark_node;
11246 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11247 && !function_body && cxx_dialect < cxx14)
11248 pedwarn (input_location, OPT_Wpedantic,
11249 "compound-statement in %<constexpr%> function");
11250 /* Begin the compound-statement. */
11251 compound_stmt = begin_compound_stmt (bcs_flags);
11252 /* If the next keyword is `__label__' we have a label declaration. */
11253 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11254 cp_parser_label_declaration (parser);
11255 /* Parse an (optional) statement-seq. */
11256 cp_parser_statement_seq_opt (parser, in_statement_expr);
11257 /* Finish the compound-statement. */
11258 finish_compound_stmt (compound_stmt);
11259 /* Consume the `}'. */
11260 braces.require_close (parser);
11261
11262 return compound_stmt;
11263 }
11264
11265 /* Parse an (optional) statement-seq.
11266
11267 statement-seq:
11268 statement
11269 statement-seq [opt] statement */
11270
11271 static void
11272 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11273 {
11274 /* Scan statements until there aren't any more. */
11275 while (true)
11276 {
11277 cp_token *token = cp_lexer_peek_token (parser->lexer);
11278
11279 /* If we are looking at a `}', then we have run out of
11280 statements; the same is true if we have reached the end
11281 of file, or have stumbled upon a stray '@end'. */
11282 if (token->type == CPP_CLOSE_BRACE
11283 || token->type == CPP_EOF
11284 || token->type == CPP_PRAGMA_EOL
11285 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11286 break;
11287
11288 /* If we are in a compound statement and find 'else' then
11289 something went wrong. */
11290 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11291 {
11292 if (parser->in_statement & IN_IF_STMT)
11293 break;
11294 else
11295 {
11296 token = cp_lexer_consume_token (parser->lexer);
11297 error_at (token->location, "%<else%> without a previous %<if%>");
11298 }
11299 }
11300
11301 /* Parse the statement. */
11302 cp_parser_statement (parser, in_statement_expr, true, NULL);
11303 }
11304 }
11305
11306 /* Return true if this is the C++20 version of range-based-for with
11307 init-statement. */
11308
11309 static bool
11310 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11311 {
11312 bool r = false;
11313
11314 /* Save tokens so that we can put them back. */
11315 cp_lexer_save_tokens (parser->lexer);
11316
11317 /* There has to be an unnested ; followed by an unnested :. */
11318 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11319 /*recovering=*/false,
11320 CPP_SEMICOLON,
11321 /*consume_paren=*/false) != -1)
11322 goto out;
11323
11324 /* We found the semicolon, eat it now. */
11325 cp_lexer_consume_token (parser->lexer);
11326
11327 /* Now look for ':' that is not nested in () or {}. */
11328 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11329 /*recovering=*/false,
11330 CPP_COLON,
11331 /*consume_paren=*/false) == -1);
11332
11333 out:
11334 /* Roll back the tokens we skipped. */
11335 cp_lexer_rollback_tokens (parser->lexer);
11336
11337 return r;
11338 }
11339
11340 /* Return true if we're looking at (init; cond), false otherwise. */
11341
11342 static bool
11343 cp_parser_init_statement_p (cp_parser *parser)
11344 {
11345 /* Save tokens so that we can put them back. */
11346 cp_lexer_save_tokens (parser->lexer);
11347
11348 /* Look for ';' that is not nested in () or {}. */
11349 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11350 /*recovering=*/false,
11351 CPP_SEMICOLON,
11352 /*consume_paren=*/false);
11353
11354 /* Roll back the tokens we skipped. */
11355 cp_lexer_rollback_tokens (parser->lexer);
11356
11357 return ret == -1;
11358 }
11359
11360 /* Parse a selection-statement.
11361
11362 selection-statement:
11363 if ( init-statement [opt] condition ) statement
11364 if ( init-statement [opt] condition ) statement else statement
11365 switch ( init-statement [opt] condition ) statement
11366
11367 Returns the new IF_STMT or SWITCH_STMT.
11368
11369 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11370 is a (possibly labeled) if statement which is not enclosed in
11371 braces and has an else clause. This is used to implement
11372 -Wparentheses.
11373
11374 CHAIN is a vector of if-else-if conditions. This is used to implement
11375 -Wduplicated-cond. */
11376
11377 static tree
11378 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11379 vec<tree> *chain)
11380 {
11381 cp_token *token;
11382 enum rid keyword;
11383 token_indent_info guard_tinfo;
11384
11385 if (if_p != NULL)
11386 *if_p = false;
11387
11388 /* Peek at the next token. */
11389 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11390 guard_tinfo = get_token_indent_info (token);
11391
11392 /* See what kind of keyword it is. */
11393 keyword = token->keyword;
11394 switch (keyword)
11395 {
11396 case RID_IF:
11397 case RID_SWITCH:
11398 {
11399 tree statement;
11400 tree condition;
11401
11402 bool cx = false;
11403 if (keyword == RID_IF
11404 && cp_lexer_next_token_is_keyword (parser->lexer,
11405 RID_CONSTEXPR))
11406 {
11407 cx = true;
11408 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11409 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11410 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11411 "with -std=c++17 or -std=gnu++17");
11412 }
11413
11414 /* Look for the `('. */
11415 matching_parens parens;
11416 if (!parens.require_open (parser))
11417 {
11418 cp_parser_skip_to_end_of_statement (parser);
11419 return error_mark_node;
11420 }
11421
11422 /* Begin the selection-statement. */
11423 if (keyword == RID_IF)
11424 {
11425 statement = begin_if_stmt ();
11426 IF_STMT_CONSTEXPR_P (statement) = cx;
11427 }
11428 else
11429 statement = begin_switch_stmt ();
11430
11431 /* Parse the optional init-statement. */
11432 if (cp_parser_init_statement_p (parser))
11433 {
11434 tree decl;
11435 if (cxx_dialect < cxx17)
11436 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11437 "init-statement in selection statements only available "
11438 "with -std=c++17 or -std=gnu++17");
11439 cp_parser_init_statement (parser, &decl);
11440 }
11441
11442 /* Parse the condition. */
11443 condition = cp_parser_condition (parser);
11444 /* Look for the `)'. */
11445 if (!parens.require_close (parser))
11446 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11447 /*consume_paren=*/true);
11448
11449 if (keyword == RID_IF)
11450 {
11451 bool nested_if;
11452 unsigned char in_statement;
11453
11454 /* Add the condition. */
11455 condition = finish_if_stmt_cond (condition, statement);
11456
11457 if (warn_duplicated_cond)
11458 warn_duplicated_cond_add_or_warn (token->location, condition,
11459 &chain);
11460
11461 /* Parse the then-clause. */
11462 in_statement = parser->in_statement;
11463 parser->in_statement |= IN_IF_STMT;
11464
11465 /* Outside a template, the non-selected branch of a constexpr
11466 if is a 'discarded statement', i.e. unevaluated. */
11467 bool was_discarded = in_discarded_stmt;
11468 bool discard_then = (cx && !processing_template_decl
11469 && integer_zerop (condition));
11470 if (discard_then)
11471 {
11472 in_discarded_stmt = true;
11473 ++c_inhibit_evaluation_warnings;
11474 }
11475
11476 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11477 guard_tinfo);
11478
11479 parser->in_statement = in_statement;
11480
11481 finish_then_clause (statement);
11482
11483 if (discard_then)
11484 {
11485 THEN_CLAUSE (statement) = NULL_TREE;
11486 in_discarded_stmt = was_discarded;
11487 --c_inhibit_evaluation_warnings;
11488 }
11489
11490 /* If the next token is `else', parse the else-clause. */
11491 if (cp_lexer_next_token_is_keyword (parser->lexer,
11492 RID_ELSE))
11493 {
11494 bool discard_else = (cx && !processing_template_decl
11495 && integer_nonzerop (condition));
11496 if (discard_else)
11497 {
11498 in_discarded_stmt = true;
11499 ++c_inhibit_evaluation_warnings;
11500 }
11501
11502 guard_tinfo
11503 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11504 /* Consume the `else' keyword. */
11505 cp_lexer_consume_token (parser->lexer);
11506 if (warn_duplicated_cond)
11507 {
11508 if (cp_lexer_next_token_is_keyword (parser->lexer,
11509 RID_IF)
11510 && chain == NULL)
11511 {
11512 /* We've got "if (COND) else if (COND2)". Start
11513 the condition chain and add COND as the first
11514 element. */
11515 chain = new vec<tree> ();
11516 if (!CONSTANT_CLASS_P (condition)
11517 && !TREE_SIDE_EFFECTS (condition))
11518 {
11519 /* Wrap it in a NOP_EXPR so that we can set the
11520 location of the condition. */
11521 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11522 condition);
11523 SET_EXPR_LOCATION (e, token->location);
11524 chain->safe_push (e);
11525 }
11526 }
11527 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11528 RID_IF))
11529 {
11530 /* This is if-else without subsequent if. Zap the
11531 condition chain; we would have already warned at
11532 this point. */
11533 delete chain;
11534 chain = NULL;
11535 }
11536 }
11537 begin_else_clause (statement);
11538 /* Parse the else-clause. */
11539 cp_parser_implicitly_scoped_statement (parser, NULL,
11540 guard_tinfo, chain);
11541
11542 finish_else_clause (statement);
11543
11544 /* If we are currently parsing a then-clause, then
11545 IF_P will not be NULL. We set it to true to
11546 indicate that this if statement has an else clause.
11547 This may trigger the Wparentheses warning below
11548 when we get back up to the parent if statement. */
11549 if (if_p != NULL)
11550 *if_p = true;
11551
11552 if (discard_else)
11553 {
11554 ELSE_CLAUSE (statement) = NULL_TREE;
11555 in_discarded_stmt = was_discarded;
11556 --c_inhibit_evaluation_warnings;
11557 }
11558 }
11559 else
11560 {
11561 /* This if statement does not have an else clause. If
11562 NESTED_IF is true, then the then-clause has an if
11563 statement which does have an else clause. We warn
11564 about the potential ambiguity. */
11565 if (nested_if)
11566 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11567 "suggest explicit braces to avoid ambiguous"
11568 " %<else%>");
11569 if (warn_duplicated_cond)
11570 {
11571 /* We don't need the condition chain anymore. */
11572 delete chain;
11573 chain = NULL;
11574 }
11575 }
11576
11577 /* Now we're all done with the if-statement. */
11578 finish_if_stmt (statement);
11579 }
11580 else
11581 {
11582 bool in_switch_statement_p;
11583 unsigned char in_statement;
11584
11585 /* Add the condition. */
11586 finish_switch_cond (condition, statement);
11587
11588 /* Parse the body of the switch-statement. */
11589 in_switch_statement_p = parser->in_switch_statement_p;
11590 in_statement = parser->in_statement;
11591 parser->in_switch_statement_p = true;
11592 parser->in_statement |= IN_SWITCH_STMT;
11593 cp_parser_implicitly_scoped_statement (parser, if_p,
11594 guard_tinfo);
11595 parser->in_switch_statement_p = in_switch_statement_p;
11596 parser->in_statement = in_statement;
11597
11598 /* Now we're all done with the switch-statement. */
11599 finish_switch_stmt (statement);
11600 }
11601
11602 return statement;
11603 }
11604 break;
11605
11606 default:
11607 cp_parser_error (parser, "expected selection-statement");
11608 return error_mark_node;
11609 }
11610 }
11611
11612 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11613 If we have seen at least one decl-specifier, and the next token
11614 is not a parenthesis, then we must be looking at a declaration.
11615 (After "int (" we might be looking at a functional cast.) */
11616
11617 static void
11618 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11619 bool any_specifiers_p)
11620 {
11621 if (any_specifiers_p
11622 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11623 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11624 && !cp_parser_error_occurred (parser))
11625 cp_parser_commit_to_tentative_parse (parser);
11626 }
11627
11628 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11629 The declarator shall not specify a function or an array. Returns
11630 TRUE if the declarator is valid, FALSE otherwise. */
11631
11632 static bool
11633 cp_parser_check_condition_declarator (cp_parser* parser,
11634 cp_declarator *declarator,
11635 location_t loc)
11636 {
11637 if (declarator == cp_error_declarator
11638 || function_declarator_p (declarator)
11639 || declarator->kind == cdk_array)
11640 {
11641 if (declarator == cp_error_declarator)
11642 /* Already complained. */;
11643 else if (declarator->kind == cdk_array)
11644 error_at (loc, "condition declares an array");
11645 else
11646 error_at (loc, "condition declares a function");
11647 if (parser->fully_implicit_function_template_p)
11648 abort_fully_implicit_template (parser);
11649 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11650 /*or_comma=*/false,
11651 /*consume_paren=*/false);
11652 return false;
11653 }
11654 else
11655 return true;
11656 }
11657
11658 /* Parse a condition.
11659
11660 condition:
11661 expression
11662 type-specifier-seq declarator = initializer-clause
11663 type-specifier-seq declarator braced-init-list
11664
11665 GNU Extension:
11666
11667 condition:
11668 type-specifier-seq declarator asm-specification [opt]
11669 attributes [opt] = assignment-expression
11670
11671 Returns the expression that should be tested. */
11672
11673 static tree
11674 cp_parser_condition (cp_parser* parser)
11675 {
11676 cp_decl_specifier_seq type_specifiers;
11677 const char *saved_message;
11678 int declares_class_or_enum;
11679
11680 /* Try the declaration first. */
11681 cp_parser_parse_tentatively (parser);
11682 /* New types are not allowed in the type-specifier-seq for a
11683 condition. */
11684 saved_message = parser->type_definition_forbidden_message;
11685 parser->type_definition_forbidden_message
11686 = G_("types may not be defined in conditions");
11687 /* Parse the type-specifier-seq. */
11688 cp_parser_decl_specifier_seq (parser,
11689 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11690 &type_specifiers,
11691 &declares_class_or_enum);
11692 /* Restore the saved message. */
11693 parser->type_definition_forbidden_message = saved_message;
11694
11695 cp_parser_maybe_commit_to_declaration (parser,
11696 type_specifiers.any_specifiers_p);
11697
11698 /* If all is well, we might be looking at a declaration. */
11699 if (!cp_parser_error_occurred (parser))
11700 {
11701 tree decl;
11702 tree asm_specification;
11703 tree attributes;
11704 cp_declarator *declarator;
11705 tree initializer = NULL_TREE;
11706 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11707
11708 /* Parse the declarator. */
11709 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11710 /*ctor_dtor_or_conv_p=*/NULL,
11711 /*parenthesized_p=*/NULL,
11712 /*member_p=*/false,
11713 /*friend_p=*/false);
11714 /* Parse the attributes. */
11715 attributes = cp_parser_attributes_opt (parser);
11716 /* Parse the asm-specification. */
11717 asm_specification = cp_parser_asm_specification_opt (parser);
11718 /* If the next token is not an `=' or '{', then we might still be
11719 looking at an expression. For example:
11720
11721 if (A(a).x)
11722
11723 looks like a decl-specifier-seq and a declarator -- but then
11724 there is no `=', so this is an expression. */
11725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11726 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11727 cp_parser_simulate_error (parser);
11728
11729 /* If we did see an `=' or '{', then we are looking at a declaration
11730 for sure. */
11731 if (cp_parser_parse_definitely (parser))
11732 {
11733 tree pushed_scope;
11734 bool non_constant_p = false;
11735 int flags = LOOKUP_ONLYCONVERTING;
11736
11737 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11738 return error_mark_node;
11739
11740 /* Create the declaration. */
11741 decl = start_decl (declarator, &type_specifiers,
11742 /*initialized_p=*/true,
11743 attributes, /*prefix_attributes=*/NULL_TREE,
11744 &pushed_scope);
11745
11746 /* Parse the initializer. */
11747 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11748 {
11749 initializer = cp_parser_braced_list (parser, &non_constant_p);
11750 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11751 flags = 0;
11752 }
11753 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11754 {
11755 /* Consume the `='. */
11756 cp_lexer_consume_token (parser->lexer);
11757 initializer = cp_parser_initializer_clause (parser,
11758 &non_constant_p);
11759 }
11760 else
11761 {
11762 cp_parser_error (parser, "expected initializer");
11763 initializer = error_mark_node;
11764 }
11765 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11766 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11767
11768 /* Process the initializer. */
11769 cp_finish_decl (decl,
11770 initializer, !non_constant_p,
11771 asm_specification,
11772 flags);
11773
11774 if (pushed_scope)
11775 pop_scope (pushed_scope);
11776
11777 return convert_from_reference (decl);
11778 }
11779 }
11780 /* If we didn't even get past the declarator successfully, we are
11781 definitely not looking at a declaration. */
11782 else
11783 cp_parser_abort_tentative_parse (parser);
11784
11785 /* Otherwise, we are looking at an expression. */
11786 return cp_parser_expression (parser);
11787 }
11788
11789 /* Parses a for-statement or range-for-statement until the closing ')',
11790 not included. */
11791
11792 static tree
11793 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11794 {
11795 tree init, scope, decl;
11796 bool is_range_for;
11797
11798 /* Begin the for-statement. */
11799 scope = begin_for_scope (&init);
11800
11801 /* Parse the initialization. */
11802 is_range_for = cp_parser_init_statement (parser, &decl);
11803
11804 if (is_range_for)
11805 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll);
11806 else
11807 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11808 }
11809
11810 static tree
11811 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11812 unsigned short unroll)
11813 {
11814 /* Normal for loop */
11815 tree condition = NULL_TREE;
11816 tree expression = NULL_TREE;
11817 tree stmt;
11818
11819 stmt = begin_for_stmt (scope, init);
11820 /* The init-statement has already been parsed in
11821 cp_parser_init_statement, so no work is needed here. */
11822 finish_init_stmt (stmt);
11823
11824 /* If there's a condition, process it. */
11825 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11826 condition = cp_parser_condition (parser);
11827 else if (ivdep)
11828 {
11829 cp_parser_error (parser, "missing loop condition in loop with "
11830 "%<GCC ivdep%> pragma");
11831 condition = error_mark_node;
11832 }
11833 else if (unroll)
11834 {
11835 cp_parser_error (parser, "missing loop condition in loop with "
11836 "%<GCC unroll%> pragma");
11837 condition = error_mark_node;
11838 }
11839 finish_for_cond (condition, stmt, ivdep, unroll);
11840 /* Look for the `;'. */
11841 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11842
11843 /* If there's an expression, process it. */
11844 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11845 expression = cp_parser_expression (parser);
11846 finish_for_expr (expression, stmt);
11847
11848 return stmt;
11849 }
11850
11851 /* Tries to parse a range-based for-statement:
11852
11853 range-based-for:
11854 decl-specifier-seq declarator : expression
11855
11856 The decl-specifier-seq declarator and the `:' are already parsed by
11857 cp_parser_init_statement. If processing_template_decl it returns a
11858 newly created RANGE_FOR_STMT; if not, it is converted to a
11859 regular FOR_STMT. */
11860
11861 static tree
11862 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11863 bool ivdep, unsigned short unroll)
11864 {
11865 tree stmt, range_expr;
11866 auto_vec <cxx_binding *, 16> bindings;
11867 auto_vec <tree, 16> names;
11868 tree decomp_first_name = NULL_TREE;
11869 unsigned int decomp_cnt = 0;
11870
11871 /* Get the range declaration momentarily out of the way so that
11872 the range expression doesn't clash with it. */
11873 if (range_decl != error_mark_node)
11874 {
11875 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11876 {
11877 tree v = DECL_VALUE_EXPR (range_decl);
11878 /* For decomposition declaration get all of the corresponding
11879 declarations out of the way. */
11880 if (TREE_CODE (v) == ARRAY_REF
11881 && VAR_P (TREE_OPERAND (v, 0))
11882 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11883 {
11884 tree d = range_decl;
11885 range_decl = TREE_OPERAND (v, 0);
11886 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11887 decomp_first_name = d;
11888 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11889 {
11890 tree name = DECL_NAME (d);
11891 names.safe_push (name);
11892 bindings.safe_push (IDENTIFIER_BINDING (name));
11893 IDENTIFIER_BINDING (name)
11894 = IDENTIFIER_BINDING (name)->previous;
11895 }
11896 }
11897 }
11898 if (names.is_empty ())
11899 {
11900 tree name = DECL_NAME (range_decl);
11901 names.safe_push (name);
11902 bindings.safe_push (IDENTIFIER_BINDING (name));
11903 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11904 }
11905 }
11906
11907 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11908 {
11909 bool expr_non_constant_p;
11910 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11911 }
11912 else
11913 range_expr = cp_parser_expression (parser);
11914
11915 /* Put the range declaration(s) back into scope. */
11916 for (unsigned int i = 0; i < names.length (); i++)
11917 {
11918 cxx_binding *binding = bindings[i];
11919 binding->previous = IDENTIFIER_BINDING (names[i]);
11920 IDENTIFIER_BINDING (names[i]) = binding;
11921 }
11922
11923 /* If in template, STMT is converted to a normal for-statement
11924 at instantiation. If not, it is done just ahead. */
11925 if (processing_template_decl)
11926 {
11927 if (check_for_bare_parameter_packs (range_expr))
11928 range_expr = error_mark_node;
11929 stmt = begin_range_for_stmt (scope, init);
11930 if (ivdep)
11931 RANGE_FOR_IVDEP (stmt) = 1;
11932 if (unroll)
11933 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
11934 finish_range_for_decl (stmt, range_decl, range_expr);
11935 if (!type_dependent_expression_p (range_expr)
11936 /* do_auto_deduction doesn't mess with template init-lists. */
11937 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11938 do_range_for_auto_deduction (range_decl, range_expr);
11939 }
11940 else
11941 {
11942 stmt = begin_for_stmt (scope, init);
11943 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11944 decomp_first_name, decomp_cnt, ivdep,
11945 unroll);
11946 }
11947 return stmt;
11948 }
11949
11950 /* Subroutine of cp_convert_range_for: given the initializer expression,
11951 builds up the range temporary. */
11952
11953 static tree
11954 build_range_temp (tree range_expr)
11955 {
11956 tree range_type, range_temp;
11957
11958 /* Find out the type deduced by the declaration
11959 `auto &&__range = range_expr'. */
11960 range_type = cp_build_reference_type (make_auto (), true);
11961 range_type = do_auto_deduction (range_type, range_expr,
11962 type_uses_auto (range_type));
11963
11964 /* Create the __range variable. */
11965 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
11966 range_type);
11967 TREE_USED (range_temp) = 1;
11968 DECL_ARTIFICIAL (range_temp) = 1;
11969
11970 return range_temp;
11971 }
11972
11973 /* Used by cp_parser_range_for in template context: we aren't going to
11974 do a full conversion yet, but we still need to resolve auto in the
11975 type of the for-range-declaration if present. This is basically
11976 a shortcut version of cp_convert_range_for. */
11977
11978 static void
11979 do_range_for_auto_deduction (tree decl, tree range_expr)
11980 {
11981 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11982 if (auto_node)
11983 {
11984 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11985 range_temp = convert_from_reference (build_range_temp (range_expr));
11986 iter_type = (cp_parser_perform_range_for_lookup
11987 (range_temp, &begin_dummy, &end_dummy));
11988 if (iter_type)
11989 {
11990 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11991 iter_type);
11992 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11993 RO_UNARY_STAR,
11994 tf_warning_or_error);
11995 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11996 iter_decl, auto_node);
11997 }
11998 }
11999 }
12000
12001 /* Converts a range-based for-statement into a normal
12002 for-statement, as per the definition.
12003
12004 for (RANGE_DECL : RANGE_EXPR)
12005 BLOCK
12006
12007 should be equivalent to:
12008
12009 {
12010 auto &&__range = RANGE_EXPR;
12011 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12012 __begin != __end;
12013 ++__begin)
12014 {
12015 RANGE_DECL = *__begin;
12016 BLOCK
12017 }
12018 }
12019
12020 If RANGE_EXPR is an array:
12021 BEGIN_EXPR = __range
12022 END_EXPR = __range + ARRAY_SIZE(__range)
12023 Else if RANGE_EXPR has a member 'begin' or 'end':
12024 BEGIN_EXPR = __range.begin()
12025 END_EXPR = __range.end()
12026 Else:
12027 BEGIN_EXPR = begin(__range)
12028 END_EXPR = end(__range);
12029
12030 If __range has a member 'begin' but not 'end', or vice versa, we must
12031 still use the second alternative (it will surely fail, however).
12032 When calling begin()/end() in the third alternative we must use
12033 argument dependent lookup, but always considering 'std' as an associated
12034 namespace. */
12035
12036 tree
12037 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12038 tree decomp_first_name, unsigned int decomp_cnt,
12039 bool ivdep, unsigned short unroll)
12040 {
12041 tree begin, end;
12042 tree iter_type, begin_expr, end_expr;
12043 tree condition, expression;
12044
12045 range_expr = mark_lvalue_use (range_expr);
12046
12047 if (range_decl == error_mark_node || range_expr == error_mark_node)
12048 /* If an error happened previously do nothing or else a lot of
12049 unhelpful errors would be issued. */
12050 begin_expr = end_expr = iter_type = error_mark_node;
12051 else
12052 {
12053 tree range_temp;
12054
12055 if (VAR_P (range_expr)
12056 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12057 /* Can't bind a reference to an array of runtime bound. */
12058 range_temp = range_expr;
12059 else
12060 {
12061 range_temp = build_range_temp (range_expr);
12062 pushdecl (range_temp);
12063 cp_finish_decl (range_temp, range_expr,
12064 /*is_constant_init*/false, NULL_TREE,
12065 LOOKUP_ONLYCONVERTING);
12066 range_temp = convert_from_reference (range_temp);
12067 }
12068 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12069 &begin_expr, &end_expr);
12070 }
12071
12072 /* The new for initialization statement. */
12073 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12074 iter_type);
12075 TREE_USED (begin) = 1;
12076 DECL_ARTIFICIAL (begin) = 1;
12077 pushdecl (begin);
12078 cp_finish_decl (begin, begin_expr,
12079 /*is_constant_init*/false, NULL_TREE,
12080 LOOKUP_ONLYCONVERTING);
12081
12082 if (cxx_dialect >= cxx17)
12083 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12084 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12085 TREE_USED (end) = 1;
12086 DECL_ARTIFICIAL (end) = 1;
12087 pushdecl (end);
12088 cp_finish_decl (end, end_expr,
12089 /*is_constant_init*/false, NULL_TREE,
12090 LOOKUP_ONLYCONVERTING);
12091
12092 finish_init_stmt (statement);
12093
12094 /* The new for condition. */
12095 condition = build_x_binary_op (input_location, NE_EXPR,
12096 begin, ERROR_MARK,
12097 end, ERROR_MARK,
12098 NULL, tf_warning_or_error);
12099 finish_for_cond (condition, statement, ivdep, unroll);
12100
12101 /* The new increment expression. */
12102 expression = finish_unary_op_expr (input_location,
12103 PREINCREMENT_EXPR, begin,
12104 tf_warning_or_error);
12105 finish_for_expr (expression, statement);
12106
12107 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12108 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12109
12110 /* The declaration is initialized with *__begin inside the loop body. */
12111 cp_finish_decl (range_decl,
12112 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12113 tf_warning_or_error),
12114 /*is_constant_init*/false, NULL_TREE,
12115 LOOKUP_ONLYCONVERTING);
12116 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12117 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12118
12119 return statement;
12120 }
12121
12122 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12123 We need to solve both at the same time because the method used
12124 depends on the existence of members begin or end.
12125 Returns the type deduced for the iterator expression. */
12126
12127 static tree
12128 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12129 {
12130 if (error_operand_p (range))
12131 {
12132 *begin = *end = error_mark_node;
12133 return error_mark_node;
12134 }
12135
12136 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12137 {
12138 error ("range-based %<for%> expression of type %qT "
12139 "has incomplete type", TREE_TYPE (range));
12140 *begin = *end = error_mark_node;
12141 return error_mark_node;
12142 }
12143 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12144 {
12145 /* If RANGE is an array, we will use pointer arithmetic. */
12146 *begin = decay_conversion (range, tf_warning_or_error);
12147 *end = build_binary_op (input_location, PLUS_EXPR,
12148 range,
12149 array_type_nelts_top (TREE_TYPE (range)),
12150 false);
12151 return TREE_TYPE (*begin);
12152 }
12153 else
12154 {
12155 /* If it is not an array, we must do a bit of magic. */
12156 tree id_begin, id_end;
12157 tree member_begin, member_end;
12158
12159 *begin = *end = error_mark_node;
12160
12161 id_begin = get_identifier ("begin");
12162 id_end = get_identifier ("end");
12163 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12164 /*protect=*/2, /*want_type=*/false,
12165 tf_warning_or_error);
12166 member_end = lookup_member (TREE_TYPE (range), id_end,
12167 /*protect=*/2, /*want_type=*/false,
12168 tf_warning_or_error);
12169
12170 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12171 {
12172 /* Use the member functions. */
12173 *begin = cp_parser_range_for_member_function (range, id_begin);
12174 *end = cp_parser_range_for_member_function (range, id_end);
12175 }
12176 else
12177 {
12178 /* Use global functions with ADL. */
12179 vec<tree, va_gc> *vec;
12180 vec = make_tree_vector ();
12181
12182 vec_safe_push (vec, range);
12183
12184 member_begin = perform_koenig_lookup (id_begin, vec,
12185 tf_warning_or_error);
12186 *begin = finish_call_expr (member_begin, &vec, false, true,
12187 tf_warning_or_error);
12188 member_end = perform_koenig_lookup (id_end, vec,
12189 tf_warning_or_error);
12190 *end = finish_call_expr (member_end, &vec, false, true,
12191 tf_warning_or_error);
12192
12193 release_tree_vector (vec);
12194 }
12195
12196 /* Last common checks. */
12197 if (*begin == error_mark_node || *end == error_mark_node)
12198 {
12199 /* If one of the expressions is an error do no more checks. */
12200 *begin = *end = error_mark_node;
12201 return error_mark_node;
12202 }
12203 else if (type_dependent_expression_p (*begin)
12204 || type_dependent_expression_p (*end))
12205 /* Can happen, when, eg, in a template context, Koenig lookup
12206 can't resolve begin/end (c++/58503). */
12207 return NULL_TREE;
12208 else
12209 {
12210 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12211 /* The unqualified type of the __begin and __end temporaries should
12212 be the same, as required by the multiple auto declaration. */
12213 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12214 {
12215 if (cxx_dialect >= cxx17
12216 && (build_x_binary_op (input_location, NE_EXPR,
12217 *begin, ERROR_MARK,
12218 *end, ERROR_MARK,
12219 NULL, tf_none)
12220 != error_mark_node))
12221 /* P0184R0 allows __begin and __end to have different types,
12222 but make sure they are comparable so we can give a better
12223 diagnostic. */;
12224 else
12225 error ("inconsistent begin/end types in range-based %<for%> "
12226 "statement: %qT and %qT",
12227 TREE_TYPE (*begin), TREE_TYPE (*end));
12228 }
12229 return iter_type;
12230 }
12231 }
12232 }
12233
12234 /* Helper function for cp_parser_perform_range_for_lookup.
12235 Builds a tree for RANGE.IDENTIFIER(). */
12236
12237 static tree
12238 cp_parser_range_for_member_function (tree range, tree identifier)
12239 {
12240 tree member, res;
12241 vec<tree, va_gc> *vec;
12242
12243 member = finish_class_member_access_expr (range, identifier,
12244 false, tf_warning_or_error);
12245 if (member == error_mark_node)
12246 return error_mark_node;
12247
12248 vec = make_tree_vector ();
12249 res = finish_call_expr (member, &vec,
12250 /*disallow_virtual=*/false,
12251 /*koenig_p=*/false,
12252 tf_warning_or_error);
12253 release_tree_vector (vec);
12254 return res;
12255 }
12256
12257 /* Parse an iteration-statement.
12258
12259 iteration-statement:
12260 while ( condition ) statement
12261 do statement while ( expression ) ;
12262 for ( init-statement condition [opt] ; expression [opt] )
12263 statement
12264
12265 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12266
12267 static tree
12268 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12269 unsigned short unroll)
12270 {
12271 cp_token *token;
12272 enum rid keyword;
12273 tree statement;
12274 unsigned char in_statement;
12275 token_indent_info guard_tinfo;
12276
12277 /* Peek at the next token. */
12278 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12279 if (!token)
12280 return error_mark_node;
12281
12282 guard_tinfo = get_token_indent_info (token);
12283
12284 /* Remember whether or not we are already within an iteration
12285 statement. */
12286 in_statement = parser->in_statement;
12287
12288 /* See what kind of keyword it is. */
12289 keyword = token->keyword;
12290 switch (keyword)
12291 {
12292 case RID_WHILE:
12293 {
12294 tree condition;
12295
12296 /* Begin the while-statement. */
12297 statement = begin_while_stmt ();
12298 /* Look for the `('. */
12299 matching_parens parens;
12300 parens.require_open (parser);
12301 /* Parse the condition. */
12302 condition = cp_parser_condition (parser);
12303 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12304 /* Look for the `)'. */
12305 parens.require_close (parser);
12306 /* Parse the dependent statement. */
12307 parser->in_statement = IN_ITERATION_STMT;
12308 bool prev = note_iteration_stmt_body_start ();
12309 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12310 note_iteration_stmt_body_end (prev);
12311 parser->in_statement = in_statement;
12312 /* We're done with the while-statement. */
12313 finish_while_stmt (statement);
12314 }
12315 break;
12316
12317 case RID_DO:
12318 {
12319 tree expression;
12320
12321 /* Begin the do-statement. */
12322 statement = begin_do_stmt ();
12323 /* Parse the body of the do-statement. */
12324 parser->in_statement = IN_ITERATION_STMT;
12325 bool prev = note_iteration_stmt_body_start ();
12326 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12327 note_iteration_stmt_body_end (prev);
12328 parser->in_statement = in_statement;
12329 finish_do_body (statement);
12330 /* Look for the `while' keyword. */
12331 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12332 /* Look for the `('. */
12333 matching_parens parens;
12334 parens.require_open (parser);
12335 /* Parse the expression. */
12336 expression = cp_parser_expression (parser);
12337 /* We're done with the do-statement. */
12338 finish_do_stmt (expression, statement, ivdep, unroll);
12339 /* Look for the `)'. */
12340 parens.require_close (parser);
12341 /* Look for the `;'. */
12342 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12343 }
12344 break;
12345
12346 case RID_FOR:
12347 {
12348 /* Look for the `('. */
12349 matching_parens parens;
12350 parens.require_open (parser);
12351
12352 statement = cp_parser_for (parser, ivdep, unroll);
12353
12354 /* Look for the `)'. */
12355 parens.require_close (parser);
12356
12357 /* Parse the body of the for-statement. */
12358 parser->in_statement = IN_ITERATION_STMT;
12359 bool prev = note_iteration_stmt_body_start ();
12360 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12361 note_iteration_stmt_body_end (prev);
12362 parser->in_statement = in_statement;
12363
12364 /* We're done with the for-statement. */
12365 finish_for_stmt (statement);
12366 }
12367 break;
12368
12369 default:
12370 cp_parser_error (parser, "expected iteration-statement");
12371 statement = error_mark_node;
12372 break;
12373 }
12374
12375 return statement;
12376 }
12377
12378 /* Parse a init-statement or the declarator of a range-based-for.
12379 Returns true if a range-based-for declaration is seen.
12380
12381 init-statement:
12382 expression-statement
12383 simple-declaration */
12384
12385 static bool
12386 cp_parser_init_statement (cp_parser *parser, tree *decl)
12387 {
12388 /* If the next token is a `;', then we have an empty
12389 expression-statement. Grammatically, this is also a
12390 simple-declaration, but an invalid one, because it does not
12391 declare anything. Therefore, if we did not handle this case
12392 specially, we would issue an error message about an invalid
12393 declaration. */
12394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12395 {
12396 bool is_range_for = false;
12397 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12398
12399 /* Try to parse the init-statement. */
12400 if (cp_parser_range_based_for_with_init_p (parser))
12401 {
12402 tree dummy;
12403 cp_parser_parse_tentatively (parser);
12404 /* Parse the declaration. */
12405 cp_parser_simple_declaration (parser,
12406 /*function_definition_allowed_p=*/false,
12407 &dummy);
12408 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12409 if (!cp_parser_parse_definitely (parser))
12410 /* That didn't work, try to parse it as an expression-statement. */
12411 cp_parser_expression_statement (parser, NULL_TREE);
12412
12413 if (cxx_dialect < cxx2a)
12414 {
12415 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12416 "range-based %<for%> loops with initializer only "
12417 "available with -std=c++2a or -std=gnu++2a");
12418 *decl = error_mark_node;
12419 }
12420 }
12421
12422 /* A colon is used in range-based for. */
12423 parser->colon_corrects_to_scope_p = false;
12424
12425 /* We're going to speculatively look for a declaration, falling back
12426 to an expression, if necessary. */
12427 cp_parser_parse_tentatively (parser);
12428 /* Parse the declaration. */
12429 cp_parser_simple_declaration (parser,
12430 /*function_definition_allowed_p=*/false,
12431 decl);
12432 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12433 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12434 {
12435 /* It is a range-for, consume the ':'. */
12436 cp_lexer_consume_token (parser->lexer);
12437 is_range_for = true;
12438 if (cxx_dialect < cxx11)
12439 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12440 "range-based %<for%> loops only available with "
12441 "-std=c++11 or -std=gnu++11");
12442 }
12443 else
12444 /* The ';' is not consumed yet because we told
12445 cp_parser_simple_declaration not to. */
12446 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12447
12448 if (cp_parser_parse_definitely (parser))
12449 return is_range_for;
12450 /* If the tentative parse failed, then we shall need to look for an
12451 expression-statement. */
12452 }
12453 /* If we are here, it is an expression-statement. */
12454 cp_parser_expression_statement (parser, NULL_TREE);
12455 return false;
12456 }
12457
12458 /* Parse a jump-statement.
12459
12460 jump-statement:
12461 break ;
12462 continue ;
12463 return expression [opt] ;
12464 return braced-init-list ;
12465 goto identifier ;
12466
12467 GNU extension:
12468
12469 jump-statement:
12470 goto * expression ;
12471
12472 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12473
12474 static tree
12475 cp_parser_jump_statement (cp_parser* parser)
12476 {
12477 tree statement = error_mark_node;
12478 cp_token *token;
12479 enum rid keyword;
12480 unsigned char in_statement;
12481
12482 /* Peek at the next token. */
12483 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12484 if (!token)
12485 return error_mark_node;
12486
12487 /* See what kind of keyword it is. */
12488 keyword = token->keyword;
12489 switch (keyword)
12490 {
12491 case RID_BREAK:
12492 in_statement = parser->in_statement & ~IN_IF_STMT;
12493 switch (in_statement)
12494 {
12495 case 0:
12496 error_at (token->location, "break statement not within loop or switch");
12497 break;
12498 default:
12499 gcc_assert ((in_statement & IN_SWITCH_STMT)
12500 || in_statement == IN_ITERATION_STMT);
12501 statement = finish_break_stmt ();
12502 if (in_statement == IN_ITERATION_STMT)
12503 break_maybe_infinite_loop ();
12504 break;
12505 case IN_OMP_BLOCK:
12506 error_at (token->location, "invalid exit from OpenMP structured block");
12507 break;
12508 case IN_OMP_FOR:
12509 error_at (token->location, "break statement used with OpenMP for loop");
12510 break;
12511 }
12512 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12513 break;
12514
12515 case RID_CONTINUE:
12516 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12517 {
12518 case 0:
12519 error_at (token->location, "continue statement not within a loop");
12520 break;
12521 /* Fall through. */
12522 case IN_ITERATION_STMT:
12523 case IN_OMP_FOR:
12524 statement = finish_continue_stmt ();
12525 break;
12526 case IN_OMP_BLOCK:
12527 error_at (token->location, "invalid exit from OpenMP structured block");
12528 break;
12529 default:
12530 gcc_unreachable ();
12531 }
12532 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12533 break;
12534
12535 case RID_RETURN:
12536 {
12537 tree expr;
12538 bool expr_non_constant_p;
12539
12540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12541 {
12542 cp_lexer_set_source_position (parser->lexer);
12543 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12544 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12545 }
12546 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12547 expr = cp_parser_expression (parser);
12548 else
12549 /* If the next token is a `;', then there is no
12550 expression. */
12551 expr = NULL_TREE;
12552 /* Build the return-statement. */
12553 if (current_function_auto_return_pattern && in_discarded_stmt)
12554 /* Don't deduce from a discarded return statement. */;
12555 else
12556 statement = finish_return_stmt (expr);
12557 /* Look for the final `;'. */
12558 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12559 }
12560 break;
12561
12562 case RID_GOTO:
12563 if (parser->in_function_body
12564 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12565 {
12566 error ("%<goto%> in %<constexpr%> function");
12567 cp_function_chain->invalid_constexpr = true;
12568 }
12569
12570 /* Create the goto-statement. */
12571 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12572 {
12573 /* Issue a warning about this use of a GNU extension. */
12574 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12575 /* Consume the '*' token. */
12576 cp_lexer_consume_token (parser->lexer);
12577 /* Parse the dependent expression. */
12578 finish_goto_stmt (cp_parser_expression (parser));
12579 }
12580 else
12581 finish_goto_stmt (cp_parser_identifier (parser));
12582 /* Look for the final `;'. */
12583 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12584 break;
12585
12586 default:
12587 cp_parser_error (parser, "expected jump-statement");
12588 break;
12589 }
12590
12591 return statement;
12592 }
12593
12594 /* Parse a declaration-statement.
12595
12596 declaration-statement:
12597 block-declaration */
12598
12599 static void
12600 cp_parser_declaration_statement (cp_parser* parser)
12601 {
12602 void *p;
12603
12604 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12605 p = obstack_alloc (&declarator_obstack, 0);
12606
12607 /* Parse the block-declaration. */
12608 cp_parser_block_declaration (parser, /*statement_p=*/true);
12609
12610 /* Free any declarators allocated. */
12611 obstack_free (&declarator_obstack, p);
12612 }
12613
12614 /* Some dependent statements (like `if (cond) statement'), are
12615 implicitly in their own scope. In other words, if the statement is
12616 a single statement (as opposed to a compound-statement), it is
12617 none-the-less treated as if it were enclosed in braces. Any
12618 declarations appearing in the dependent statement are out of scope
12619 after control passes that point. This function parses a statement,
12620 but ensures that is in its own scope, even if it is not a
12621 compound-statement.
12622
12623 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12624 is a (possibly labeled) if statement which is not enclosed in
12625 braces and has an else clause. This is used to implement
12626 -Wparentheses.
12627
12628 CHAIN is a vector of if-else-if conditions. This is used to implement
12629 -Wduplicated-cond.
12630
12631 Returns the new statement. */
12632
12633 static tree
12634 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12635 const token_indent_info &guard_tinfo,
12636 vec<tree> *chain)
12637 {
12638 tree statement;
12639 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12640 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12641 token_indent_info body_tinfo
12642 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12643
12644 if (if_p != NULL)
12645 *if_p = false;
12646
12647 /* Mark if () ; with a special NOP_EXPR. */
12648 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12649 {
12650 cp_lexer_consume_token (parser->lexer);
12651 statement = add_stmt (build_empty_stmt (body_loc));
12652
12653 if (guard_tinfo.keyword == RID_IF
12654 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12655 warning_at (body_loc, OPT_Wempty_body,
12656 "suggest braces around empty body in an %<if%> statement");
12657 else if (guard_tinfo.keyword == RID_ELSE)
12658 warning_at (body_loc, OPT_Wempty_body,
12659 "suggest braces around empty body in an %<else%> statement");
12660 }
12661 /* if a compound is opened, we simply parse the statement directly. */
12662 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12663 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12664 /* If the token is not a `{', then we must take special action. */
12665 else
12666 {
12667 /* Create a compound-statement. */
12668 statement = begin_compound_stmt (0);
12669 /* Parse the dependent-statement. */
12670 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12671 &body_loc_after_labels);
12672 /* Finish the dummy compound-statement. */
12673 finish_compound_stmt (statement);
12674 }
12675
12676 token_indent_info next_tinfo
12677 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12678 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12679
12680 if (body_loc_after_labels != UNKNOWN_LOCATION
12681 && next_tinfo.type != CPP_SEMICOLON)
12682 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12683 guard_tinfo.location, guard_tinfo.keyword);
12684
12685 /* Return the statement. */
12686 return statement;
12687 }
12688
12689 /* For some dependent statements (like `while (cond) statement'), we
12690 have already created a scope. Therefore, even if the dependent
12691 statement is a compound-statement, we do not want to create another
12692 scope. */
12693
12694 static void
12695 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12696 const token_indent_info &guard_tinfo)
12697 {
12698 /* If the token is a `{', then we must take special action. */
12699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12700 {
12701 token_indent_info body_tinfo
12702 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12703 location_t loc_after_labels = UNKNOWN_LOCATION;
12704
12705 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12706 &loc_after_labels);
12707 token_indent_info next_tinfo
12708 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12709 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12710
12711 if (loc_after_labels != UNKNOWN_LOCATION
12712 && next_tinfo.type != CPP_SEMICOLON)
12713 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12714 guard_tinfo.location,
12715 guard_tinfo.keyword);
12716 }
12717 else
12718 {
12719 /* Avoid calling cp_parser_compound_statement, so that we
12720 don't create a new scope. Do everything else by hand. */
12721 matching_braces braces;
12722 braces.require_open (parser);
12723 /* If the next keyword is `__label__' we have a label declaration. */
12724 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12725 cp_parser_label_declaration (parser);
12726 /* Parse an (optional) statement-seq. */
12727 cp_parser_statement_seq_opt (parser, NULL_TREE);
12728 braces.require_close (parser);
12729 }
12730 }
12731
12732 /* Declarations [gram.dcl.dcl] */
12733
12734 /* Parse an optional declaration-sequence.
12735
12736 declaration-seq:
12737 declaration
12738 declaration-seq declaration */
12739
12740 static void
12741 cp_parser_declaration_seq_opt (cp_parser* parser)
12742 {
12743 while (true)
12744 {
12745 cp_token *token;
12746
12747 token = cp_lexer_peek_token (parser->lexer);
12748
12749 if (token->type == CPP_CLOSE_BRACE
12750 || token->type == CPP_EOF
12751 || token->type == CPP_PRAGMA_EOL)
12752 break;
12753
12754 if (token->type == CPP_SEMICOLON)
12755 {
12756 /* A declaration consisting of a single semicolon is
12757 invalid. Allow it unless we're being pedantic. */
12758 cp_lexer_consume_token (parser->lexer);
12759 if (!in_system_header_at (input_location))
12760 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12761 continue;
12762 }
12763
12764 /* If we're entering or exiting a region that's implicitly
12765 extern "C", modify the lang context appropriately. */
12766 if (!parser->implicit_extern_c && token->implicit_extern_c)
12767 {
12768 push_lang_context (lang_name_c);
12769 parser->implicit_extern_c = true;
12770 }
12771 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12772 {
12773 pop_lang_context ();
12774 parser->implicit_extern_c = false;
12775 }
12776
12777 if (token->type == CPP_PRAGMA)
12778 {
12779 /* A top-level declaration can consist solely of a #pragma.
12780 A nested declaration cannot, so this is done here and not
12781 in cp_parser_declaration. (A #pragma at block scope is
12782 handled in cp_parser_statement.) */
12783 cp_parser_pragma (parser, pragma_external, NULL);
12784 continue;
12785 }
12786
12787 /* Parse the declaration itself. */
12788 cp_parser_declaration (parser);
12789 }
12790 }
12791
12792 /* Parse a declaration.
12793
12794 declaration:
12795 block-declaration
12796 function-definition
12797 template-declaration
12798 explicit-instantiation
12799 explicit-specialization
12800 linkage-specification
12801 namespace-definition
12802
12803 C++17:
12804 deduction-guide
12805
12806 GNU extension:
12807
12808 declaration:
12809 __extension__ declaration */
12810
12811 static void
12812 cp_parser_declaration (cp_parser* parser)
12813 {
12814 cp_token token1;
12815 cp_token token2;
12816 int saved_pedantic;
12817 void *p;
12818 tree attributes = NULL_TREE;
12819
12820 /* Check for the `__extension__' keyword. */
12821 if (cp_parser_extension_opt (parser, &saved_pedantic))
12822 {
12823 /* Parse the qualified declaration. */
12824 cp_parser_declaration (parser);
12825 /* Restore the PEDANTIC flag. */
12826 pedantic = saved_pedantic;
12827
12828 return;
12829 }
12830
12831 /* Try to figure out what kind of declaration is present. */
12832 token1 = *cp_lexer_peek_token (parser->lexer);
12833
12834 if (token1.type != CPP_EOF)
12835 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12836 else
12837 {
12838 token2.type = CPP_EOF;
12839 token2.keyword = RID_MAX;
12840 }
12841
12842 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12843 p = obstack_alloc (&declarator_obstack, 0);
12844
12845 /* If the next token is `extern' and the following token is a string
12846 literal, then we have a linkage specification. */
12847 if (token1.keyword == RID_EXTERN
12848 && cp_parser_is_pure_string_literal (&token2))
12849 cp_parser_linkage_specification (parser);
12850 /* If the next token is `template', then we have either a template
12851 declaration, an explicit instantiation, or an explicit
12852 specialization. */
12853 else if (token1.keyword == RID_TEMPLATE)
12854 {
12855 /* `template <>' indicates a template specialization. */
12856 if (token2.type == CPP_LESS
12857 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12858 cp_parser_explicit_specialization (parser);
12859 /* `template <' indicates a template declaration. */
12860 else if (token2.type == CPP_LESS)
12861 cp_parser_template_declaration (parser, /*member_p=*/false);
12862 /* Anything else must be an explicit instantiation. */
12863 else
12864 cp_parser_explicit_instantiation (parser);
12865 }
12866 /* If the next token is `export', then we have a template
12867 declaration. */
12868 else if (token1.keyword == RID_EXPORT)
12869 cp_parser_template_declaration (parser, /*member_p=*/false);
12870 /* If the next token is `extern', 'static' or 'inline' and the one
12871 after that is `template', we have a GNU extended explicit
12872 instantiation directive. */
12873 else if (cp_parser_allow_gnu_extensions_p (parser)
12874 && (token1.keyword == RID_EXTERN
12875 || token1.keyword == RID_STATIC
12876 || token1.keyword == RID_INLINE)
12877 && token2.keyword == RID_TEMPLATE)
12878 cp_parser_explicit_instantiation (parser);
12879 /* If the next token is `namespace', check for a named or unnamed
12880 namespace definition. */
12881 else if (token1.keyword == RID_NAMESPACE
12882 && (/* A named namespace definition. */
12883 (token2.type == CPP_NAME
12884 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12885 != CPP_EQ))
12886 || (token2.type == CPP_OPEN_SQUARE
12887 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12888 == CPP_OPEN_SQUARE)
12889 /* An unnamed namespace definition. */
12890 || token2.type == CPP_OPEN_BRACE
12891 || token2.keyword == RID_ATTRIBUTE))
12892 cp_parser_namespace_definition (parser);
12893 /* An inline (associated) namespace definition. */
12894 else if (token1.keyword == RID_INLINE
12895 && token2.keyword == RID_NAMESPACE)
12896 cp_parser_namespace_definition (parser);
12897 /* Objective-C++ declaration/definition. */
12898 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12899 cp_parser_objc_declaration (parser, NULL_TREE);
12900 else if (c_dialect_objc ()
12901 && token1.keyword == RID_ATTRIBUTE
12902 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12903 cp_parser_objc_declaration (parser, attributes);
12904 /* At this point we may have a template declared by a concept
12905 introduction. */
12906 else if (flag_concepts
12907 && cp_parser_template_declaration_after_export (parser,
12908 /*member_p=*/false))
12909 /* We did. */;
12910 else
12911 /* Try to parse a block-declaration, or a function-definition. */
12912 cp_parser_block_declaration (parser, /*statement_p=*/false);
12913
12914 /* Free any declarators allocated. */
12915 obstack_free (&declarator_obstack, p);
12916 }
12917
12918 /* Parse a block-declaration.
12919
12920 block-declaration:
12921 simple-declaration
12922 asm-definition
12923 namespace-alias-definition
12924 using-declaration
12925 using-directive
12926
12927 GNU Extension:
12928
12929 block-declaration:
12930 __extension__ block-declaration
12931
12932 C++0x Extension:
12933
12934 block-declaration:
12935 static_assert-declaration
12936
12937 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12938 part of a declaration-statement. */
12939
12940 static void
12941 cp_parser_block_declaration (cp_parser *parser,
12942 bool statement_p)
12943 {
12944 cp_token *token1;
12945 int saved_pedantic;
12946
12947 /* Check for the `__extension__' keyword. */
12948 if (cp_parser_extension_opt (parser, &saved_pedantic))
12949 {
12950 /* Parse the qualified declaration. */
12951 cp_parser_block_declaration (parser, statement_p);
12952 /* Restore the PEDANTIC flag. */
12953 pedantic = saved_pedantic;
12954
12955 return;
12956 }
12957
12958 /* Peek at the next token to figure out which kind of declaration is
12959 present. */
12960 token1 = cp_lexer_peek_token (parser->lexer);
12961
12962 /* If the next keyword is `asm', we have an asm-definition. */
12963 if (token1->keyword == RID_ASM)
12964 {
12965 if (statement_p)
12966 cp_parser_commit_to_tentative_parse (parser);
12967 cp_parser_asm_definition (parser);
12968 }
12969 /* If the next keyword is `namespace', we have a
12970 namespace-alias-definition. */
12971 else if (token1->keyword == RID_NAMESPACE)
12972 cp_parser_namespace_alias_definition (parser);
12973 /* If the next keyword is `using', we have a
12974 using-declaration, a using-directive, or an alias-declaration. */
12975 else if (token1->keyword == RID_USING)
12976 {
12977 cp_token *token2;
12978
12979 if (statement_p)
12980 cp_parser_commit_to_tentative_parse (parser);
12981 /* If the token after `using' is `namespace', then we have a
12982 using-directive. */
12983 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12984 if (token2->keyword == RID_NAMESPACE)
12985 cp_parser_using_directive (parser);
12986 /* If the second token after 'using' is '=', then we have an
12987 alias-declaration. */
12988 else if (cxx_dialect >= cxx11
12989 && token2->type == CPP_NAME
12990 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12991 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12992 cp_parser_alias_declaration (parser);
12993 /* Otherwise, it's a using-declaration. */
12994 else
12995 cp_parser_using_declaration (parser,
12996 /*access_declaration_p=*/false);
12997 }
12998 /* If the next keyword is `__label__' we have a misplaced label
12999 declaration. */
13000 else if (token1->keyword == RID_LABEL)
13001 {
13002 cp_lexer_consume_token (parser->lexer);
13003 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13004 cp_parser_skip_to_end_of_statement (parser);
13005 /* If the next token is now a `;', consume it. */
13006 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13007 cp_lexer_consume_token (parser->lexer);
13008 }
13009 /* If the next token is `static_assert' we have a static assertion. */
13010 else if (token1->keyword == RID_STATIC_ASSERT)
13011 cp_parser_static_assert (parser, /*member_p=*/false);
13012 /* Anything else must be a simple-declaration. */
13013 else
13014 cp_parser_simple_declaration (parser, !statement_p,
13015 /*maybe_range_for_decl*/NULL);
13016 }
13017
13018 /* Parse a simple-declaration.
13019
13020 simple-declaration:
13021 decl-specifier-seq [opt] init-declarator-list [opt] ;
13022 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13023 brace-or-equal-initializer ;
13024
13025 init-declarator-list:
13026 init-declarator
13027 init-declarator-list , init-declarator
13028
13029 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13030 function-definition as a simple-declaration.
13031
13032 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13033 parsed declaration if it is an uninitialized single declarator not followed
13034 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13035 if present, will not be consumed. */
13036
13037 static void
13038 cp_parser_simple_declaration (cp_parser* parser,
13039 bool function_definition_allowed_p,
13040 tree *maybe_range_for_decl)
13041 {
13042 cp_decl_specifier_seq decl_specifiers;
13043 int declares_class_or_enum;
13044 bool saw_declarator;
13045 location_t comma_loc = UNKNOWN_LOCATION;
13046 location_t init_loc = UNKNOWN_LOCATION;
13047
13048 if (maybe_range_for_decl)
13049 *maybe_range_for_decl = NULL_TREE;
13050
13051 /* Defer access checks until we know what is being declared; the
13052 checks for names appearing in the decl-specifier-seq should be
13053 done as if we were in the scope of the thing being declared. */
13054 push_deferring_access_checks (dk_deferred);
13055
13056 /* Parse the decl-specifier-seq. We have to keep track of whether
13057 or not the decl-specifier-seq declares a named class or
13058 enumeration type, since that is the only case in which the
13059 init-declarator-list is allowed to be empty.
13060
13061 [dcl.dcl]
13062
13063 In a simple-declaration, the optional init-declarator-list can be
13064 omitted only when declaring a class or enumeration, that is when
13065 the decl-specifier-seq contains either a class-specifier, an
13066 elaborated-type-specifier, or an enum-specifier. */
13067 cp_parser_decl_specifier_seq (parser,
13068 CP_PARSER_FLAGS_OPTIONAL,
13069 &decl_specifiers,
13070 &declares_class_or_enum);
13071 /* We no longer need to defer access checks. */
13072 stop_deferring_access_checks ();
13073
13074 /* In a block scope, a valid declaration must always have a
13075 decl-specifier-seq. By not trying to parse declarators, we can
13076 resolve the declaration/expression ambiguity more quickly. */
13077 if (!function_definition_allowed_p
13078 && !decl_specifiers.any_specifiers_p)
13079 {
13080 cp_parser_error (parser, "expected declaration");
13081 goto done;
13082 }
13083
13084 /* If the next two tokens are both identifiers, the code is
13085 erroneous. The usual cause of this situation is code like:
13086
13087 T t;
13088
13089 where "T" should name a type -- but does not. */
13090 if (!decl_specifiers.any_type_specifiers_p
13091 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13092 {
13093 /* If parsing tentatively, we should commit; we really are
13094 looking at a declaration. */
13095 cp_parser_commit_to_tentative_parse (parser);
13096 /* Give up. */
13097 goto done;
13098 }
13099
13100 cp_parser_maybe_commit_to_declaration (parser,
13101 decl_specifiers.any_specifiers_p);
13102
13103 /* Look for C++17 decomposition declaration. */
13104 for (size_t n = 1; ; n++)
13105 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13106 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13107 continue;
13108 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13109 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13110 && decl_specifiers.any_specifiers_p)
13111 {
13112 tree decl
13113 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13114 maybe_range_for_decl,
13115 &init_loc);
13116
13117 /* The next token should be either a `,' or a `;'. */
13118 cp_token *token = cp_lexer_peek_token (parser->lexer);
13119 /* If it's a `;', we are done. */
13120 if (token->type == CPP_SEMICOLON)
13121 goto finish;
13122 else if (maybe_range_for_decl)
13123 {
13124 if (*maybe_range_for_decl == NULL_TREE)
13125 *maybe_range_for_decl = error_mark_node;
13126 goto finish;
13127 }
13128 /* Anything else is an error. */
13129 else
13130 {
13131 /* If we have already issued an error message we don't need
13132 to issue another one. */
13133 if ((decl != error_mark_node
13134 && DECL_INITIAL (decl) != error_mark_node)
13135 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13136 cp_parser_error (parser, "expected %<,%> or %<;%>");
13137 /* Skip tokens until we reach the end of the statement. */
13138 cp_parser_skip_to_end_of_statement (parser);
13139 /* If the next token is now a `;', consume it. */
13140 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13141 cp_lexer_consume_token (parser->lexer);
13142 goto done;
13143 }
13144 }
13145 else
13146 break;
13147
13148 tree last_type;
13149 bool auto_specifier_p;
13150 /* NULL_TREE if both variable and function declaration are allowed,
13151 error_mark_node if function declaration are not allowed and
13152 a FUNCTION_DECL that should be diagnosed if it is followed by
13153 variable declarations. */
13154 tree auto_function_declaration;
13155
13156 last_type = NULL_TREE;
13157 auto_specifier_p
13158 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13159 auto_function_declaration = NULL_TREE;
13160
13161 /* Keep going until we hit the `;' at the end of the simple
13162 declaration. */
13163 saw_declarator = false;
13164 while (cp_lexer_next_token_is_not (parser->lexer,
13165 CPP_SEMICOLON))
13166 {
13167 cp_token *token;
13168 bool function_definition_p;
13169 tree decl;
13170 tree auto_result = NULL_TREE;
13171
13172 if (saw_declarator)
13173 {
13174 /* If we are processing next declarator, comma is expected */
13175 token = cp_lexer_peek_token (parser->lexer);
13176 gcc_assert (token->type == CPP_COMMA);
13177 cp_lexer_consume_token (parser->lexer);
13178 if (maybe_range_for_decl)
13179 {
13180 *maybe_range_for_decl = error_mark_node;
13181 if (comma_loc == UNKNOWN_LOCATION)
13182 comma_loc = token->location;
13183 }
13184 }
13185 else
13186 saw_declarator = true;
13187
13188 /* Parse the init-declarator. */
13189 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13190 /*checks=*/NULL,
13191 function_definition_allowed_p,
13192 /*member_p=*/false,
13193 declares_class_or_enum,
13194 &function_definition_p,
13195 maybe_range_for_decl,
13196 &init_loc,
13197 &auto_result);
13198 /* If an error occurred while parsing tentatively, exit quickly.
13199 (That usually happens when in the body of a function; each
13200 statement is treated as a declaration-statement until proven
13201 otherwise.) */
13202 if (cp_parser_error_occurred (parser))
13203 goto done;
13204
13205 if (auto_specifier_p && cxx_dialect >= cxx14)
13206 {
13207 /* If the init-declarator-list contains more than one
13208 init-declarator, they shall all form declarations of
13209 variables. */
13210 if (auto_function_declaration == NULL_TREE)
13211 auto_function_declaration
13212 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13213 else if (TREE_CODE (decl) == FUNCTION_DECL
13214 || auto_function_declaration != error_mark_node)
13215 {
13216 error_at (decl_specifiers.locations[ds_type_spec],
13217 "non-variable %qD in declaration with more than one "
13218 "declarator with placeholder type",
13219 TREE_CODE (decl) == FUNCTION_DECL
13220 ? decl : auto_function_declaration);
13221 auto_function_declaration = error_mark_node;
13222 }
13223 }
13224
13225 if (auto_result
13226 && (!processing_template_decl || !type_uses_auto (auto_result)))
13227 {
13228 if (last_type
13229 && last_type != error_mark_node
13230 && !same_type_p (auto_result, last_type))
13231 {
13232 /* If the list of declarators contains more than one declarator,
13233 the type of each declared variable is determined as described
13234 above. If the type deduced for the template parameter U is not
13235 the same in each deduction, the program is ill-formed. */
13236 error_at (decl_specifiers.locations[ds_type_spec],
13237 "inconsistent deduction for %qT: %qT and then %qT",
13238 decl_specifiers.type, last_type, auto_result);
13239 last_type = error_mark_node;
13240 }
13241 else
13242 last_type = auto_result;
13243 }
13244
13245 /* Handle function definitions specially. */
13246 if (function_definition_p)
13247 {
13248 /* If the next token is a `,', then we are probably
13249 processing something like:
13250
13251 void f() {}, *p;
13252
13253 which is erroneous. */
13254 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13255 {
13256 cp_token *token = cp_lexer_peek_token (parser->lexer);
13257 error_at (token->location,
13258 "mixing"
13259 " declarations and function-definitions is forbidden");
13260 }
13261 /* Otherwise, we're done with the list of declarators. */
13262 else
13263 {
13264 pop_deferring_access_checks ();
13265 return;
13266 }
13267 }
13268 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13269 *maybe_range_for_decl = decl;
13270 /* The next token should be either a `,' or a `;'. */
13271 token = cp_lexer_peek_token (parser->lexer);
13272 /* If it's a `,', there are more declarators to come. */
13273 if (token->type == CPP_COMMA)
13274 /* will be consumed next time around */;
13275 /* If it's a `;', we are done. */
13276 else if (token->type == CPP_SEMICOLON)
13277 break;
13278 else if (maybe_range_for_decl)
13279 {
13280 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13281 permerror (decl_specifiers.locations[ds_type_spec],
13282 "types may not be defined in a for-range-declaration");
13283 break;
13284 }
13285 /* Anything else is an error. */
13286 else
13287 {
13288 /* If we have already issued an error message we don't need
13289 to issue another one. */
13290 if ((decl != error_mark_node
13291 && DECL_INITIAL (decl) != error_mark_node)
13292 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13293 cp_parser_error (parser, "expected %<,%> or %<;%>");
13294 /* Skip tokens until we reach the end of the statement. */
13295 cp_parser_skip_to_end_of_statement (parser);
13296 /* If the next token is now a `;', consume it. */
13297 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13298 cp_lexer_consume_token (parser->lexer);
13299 goto done;
13300 }
13301 /* After the first time around, a function-definition is not
13302 allowed -- even if it was OK at first. For example:
13303
13304 int i, f() {}
13305
13306 is not valid. */
13307 function_definition_allowed_p = false;
13308 }
13309
13310 /* Issue an error message if no declarators are present, and the
13311 decl-specifier-seq does not itself declare a class or
13312 enumeration: [dcl.dcl]/3. */
13313 if (!saw_declarator)
13314 {
13315 if (cp_parser_declares_only_class_p (parser))
13316 {
13317 if (!declares_class_or_enum
13318 && decl_specifiers.type
13319 && OVERLOAD_TYPE_P (decl_specifiers.type))
13320 /* Ensure an error is issued anyway when finish_decltype_type,
13321 called via cp_parser_decl_specifier_seq, returns a class or
13322 an enumeration (c++/51786). */
13323 decl_specifiers.type = NULL_TREE;
13324 shadow_tag (&decl_specifiers);
13325 }
13326 /* Perform any deferred access checks. */
13327 perform_deferred_access_checks (tf_warning_or_error);
13328 }
13329
13330 /* Consume the `;'. */
13331 finish:
13332 if (!maybe_range_for_decl)
13333 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13334 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13335 {
13336 if (init_loc != UNKNOWN_LOCATION)
13337 error_at (init_loc, "initializer in range-based %<for%> loop");
13338 if (comma_loc != UNKNOWN_LOCATION)
13339 error_at (comma_loc,
13340 "multiple declarations in range-based %<for%> loop");
13341 }
13342
13343 done:
13344 pop_deferring_access_checks ();
13345 }
13346
13347 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13348 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13349 initializer ; */
13350
13351 static tree
13352 cp_parser_decomposition_declaration (cp_parser *parser,
13353 cp_decl_specifier_seq *decl_specifiers,
13354 tree *maybe_range_for_decl,
13355 location_t *init_loc)
13356 {
13357 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13358 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13359 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13360
13361 /* Parse the identifier-list. */
13362 auto_vec<cp_expr, 10> v;
13363 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13364 while (true)
13365 {
13366 cp_expr e = cp_parser_identifier (parser);
13367 if (e.get_value () == error_mark_node)
13368 break;
13369 v.safe_push (e);
13370 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13371 break;
13372 cp_lexer_consume_token (parser->lexer);
13373 }
13374
13375 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13376 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13377 {
13378 end_loc = UNKNOWN_LOCATION;
13379 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13380 false);
13381 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13382 cp_lexer_consume_token (parser->lexer);
13383 else
13384 {
13385 cp_parser_skip_to_end_of_statement (parser);
13386 return error_mark_node;
13387 }
13388 }
13389
13390 if (cxx_dialect < cxx17)
13391 pedwarn (loc, 0, "structured bindings only available with "
13392 "-std=c++17 or -std=gnu++17");
13393
13394 tree pushed_scope;
13395 cp_declarator *declarator = make_declarator (cdk_decomp);
13396 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13397 declarator->id_loc = loc;
13398 if (ref_qual != REF_QUAL_NONE)
13399 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13400 ref_qual == REF_QUAL_RVALUE,
13401 NULL_TREE);
13402 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13403 NULL_TREE, decl_specifiers->attributes,
13404 &pushed_scope);
13405 tree orig_decl = decl;
13406
13407 unsigned int i;
13408 cp_expr e;
13409 cp_decl_specifier_seq decl_specs;
13410 clear_decl_specs (&decl_specs);
13411 decl_specs.type = make_auto ();
13412 tree prev = decl;
13413 FOR_EACH_VEC_ELT (v, i, e)
13414 {
13415 if (i == 0)
13416 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13417 else
13418 declarator->u.id.unqualified_name = e.get_value ();
13419 declarator->id_loc = e.get_location ();
13420 tree elt_pushed_scope;
13421 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13422 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13423 if (decl2 == error_mark_node)
13424 decl = error_mark_node;
13425 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13426 {
13427 /* Ensure we've diagnosed redeclaration if we aren't creating
13428 a new VAR_DECL. */
13429 gcc_assert (errorcount);
13430 decl = error_mark_node;
13431 }
13432 else
13433 prev = decl2;
13434 if (elt_pushed_scope)
13435 pop_scope (elt_pushed_scope);
13436 }
13437
13438 if (v.is_empty ())
13439 {
13440 error_at (loc, "empty structured binding declaration");
13441 decl = error_mark_node;
13442 }
13443
13444 if (maybe_range_for_decl == NULL
13445 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13446 {
13447 bool non_constant_p = false, is_direct_init = false;
13448 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13449 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13450 &non_constant_p);
13451 if (initializer == NULL_TREE
13452 || (TREE_CODE (initializer) == TREE_LIST
13453 && TREE_CHAIN (initializer))
13454 || (is_direct_init
13455 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13456 && CONSTRUCTOR_NELTS (initializer) != 1))
13457 {
13458 error_at (loc, "invalid initializer for structured binding "
13459 "declaration");
13460 initializer = error_mark_node;
13461 }
13462
13463 if (decl != error_mark_node)
13464 {
13465 cp_maybe_mangle_decomp (decl, prev, v.length ());
13466 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13467 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13468 cp_finish_decomp (decl, prev, v.length ());
13469 }
13470 }
13471 else if (decl != error_mark_node)
13472 {
13473 *maybe_range_for_decl = prev;
13474 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13475 the underlying DECL. */
13476 cp_finish_decomp (decl, prev, v.length ());
13477 }
13478
13479 if (pushed_scope)
13480 pop_scope (pushed_scope);
13481
13482 if (decl == error_mark_node && DECL_P (orig_decl))
13483 {
13484 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13485 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13486 }
13487
13488 return decl;
13489 }
13490
13491 /* Parse a decl-specifier-seq.
13492
13493 decl-specifier-seq:
13494 decl-specifier-seq [opt] decl-specifier
13495 decl-specifier attribute-specifier-seq [opt] (C++11)
13496
13497 decl-specifier:
13498 storage-class-specifier
13499 type-specifier
13500 function-specifier
13501 friend
13502 typedef
13503
13504 GNU Extension:
13505
13506 decl-specifier:
13507 attributes
13508
13509 Concepts Extension:
13510
13511 decl-specifier:
13512 concept
13513
13514 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13515
13516 The parser flags FLAGS is used to control type-specifier parsing.
13517
13518 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13519 flags:
13520
13521 1: one of the decl-specifiers is an elaborated-type-specifier
13522 (i.e., a type declaration)
13523 2: one of the decl-specifiers is an enum-specifier or a
13524 class-specifier (i.e., a type definition)
13525
13526 */
13527
13528 static void
13529 cp_parser_decl_specifier_seq (cp_parser* parser,
13530 cp_parser_flags flags,
13531 cp_decl_specifier_seq *decl_specs,
13532 int* declares_class_or_enum)
13533 {
13534 bool constructor_possible_p = !parser->in_declarator_p;
13535 bool found_decl_spec = false;
13536 cp_token *start_token = NULL;
13537 cp_decl_spec ds;
13538
13539 /* Clear DECL_SPECS. */
13540 clear_decl_specs (decl_specs);
13541
13542 /* Assume no class or enumeration type is declared. */
13543 *declares_class_or_enum = 0;
13544
13545 /* Keep reading specifiers until there are no more to read. */
13546 while (true)
13547 {
13548 bool constructor_p;
13549 cp_token *token;
13550 ds = ds_last;
13551
13552 /* Peek at the next token. */
13553 token = cp_lexer_peek_token (parser->lexer);
13554
13555 /* Save the first token of the decl spec list for error
13556 reporting. */
13557 if (!start_token)
13558 start_token = token;
13559 /* Handle attributes. */
13560 if (cp_next_tokens_can_be_attribute_p (parser))
13561 {
13562 /* Parse the attributes. */
13563 tree attrs = cp_parser_attributes_opt (parser);
13564
13565 /* In a sequence of declaration specifiers, c++11 attributes
13566 appertain to the type that precede them. In that case
13567 [dcl.spec]/1 says:
13568
13569 The attribute-specifier-seq affects the type only for
13570 the declaration it appears in, not other declarations
13571 involving the same type.
13572
13573 But for now let's force the user to position the
13574 attribute either at the beginning of the declaration or
13575 after the declarator-id, which would clearly mean that it
13576 applies to the declarator. */
13577 if (cxx11_attribute_p (attrs))
13578 {
13579 if (!found_decl_spec)
13580 /* The c++11 attribute is at the beginning of the
13581 declaration. It appertains to the entity being
13582 declared. */;
13583 else
13584 {
13585 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13586 {
13587 /* This is an attribute following a
13588 class-specifier. */
13589 if (decl_specs->type_definition_p)
13590 warn_misplaced_attr_for_class_type (token->location,
13591 decl_specs->type);
13592 attrs = NULL_TREE;
13593 }
13594 else
13595 {
13596 decl_specs->std_attributes
13597 = attr_chainon (decl_specs->std_attributes, attrs);
13598 if (decl_specs->locations[ds_std_attribute] == 0)
13599 decl_specs->locations[ds_std_attribute] = token->location;
13600 }
13601 continue;
13602 }
13603 }
13604
13605 decl_specs->attributes
13606 = attr_chainon (decl_specs->attributes, attrs);
13607 if (decl_specs->locations[ds_attribute] == 0)
13608 decl_specs->locations[ds_attribute] = token->location;
13609 continue;
13610 }
13611 /* Assume we will find a decl-specifier keyword. */
13612 found_decl_spec = true;
13613 /* If the next token is an appropriate keyword, we can simply
13614 add it to the list. */
13615 switch (token->keyword)
13616 {
13617 /* decl-specifier:
13618 friend
13619 constexpr */
13620 case RID_FRIEND:
13621 if (!at_class_scope_p ())
13622 {
13623 gcc_rich_location richloc (token->location);
13624 richloc.add_fixit_remove ();
13625 error_at (&richloc, "%<friend%> used outside of class");
13626 cp_lexer_purge_token (parser->lexer);
13627 }
13628 else
13629 {
13630 ds = ds_friend;
13631 /* Consume the token. */
13632 cp_lexer_consume_token (parser->lexer);
13633 }
13634 break;
13635
13636 case RID_CONSTEXPR:
13637 ds = ds_constexpr;
13638 cp_lexer_consume_token (parser->lexer);
13639 break;
13640
13641 case RID_CONCEPT:
13642 ds = ds_concept;
13643 cp_lexer_consume_token (parser->lexer);
13644 break;
13645
13646 /* function-specifier:
13647 inline
13648 virtual
13649 explicit */
13650 case RID_INLINE:
13651 case RID_VIRTUAL:
13652 case RID_EXPLICIT:
13653 cp_parser_function_specifier_opt (parser, decl_specs);
13654 break;
13655
13656 /* decl-specifier:
13657 typedef */
13658 case RID_TYPEDEF:
13659 ds = ds_typedef;
13660 /* Consume the token. */
13661 cp_lexer_consume_token (parser->lexer);
13662 /* A constructor declarator cannot appear in a typedef. */
13663 constructor_possible_p = false;
13664 /* The "typedef" keyword can only occur in a declaration; we
13665 may as well commit at this point. */
13666 cp_parser_commit_to_tentative_parse (parser);
13667
13668 if (decl_specs->storage_class != sc_none)
13669 decl_specs->conflicting_specifiers_p = true;
13670 break;
13671
13672 /* storage-class-specifier:
13673 auto
13674 register
13675 static
13676 extern
13677 mutable
13678
13679 GNU Extension:
13680 thread */
13681 case RID_AUTO:
13682 if (cxx_dialect == cxx98)
13683 {
13684 /* Consume the token. */
13685 cp_lexer_consume_token (parser->lexer);
13686
13687 /* Complain about `auto' as a storage specifier, if
13688 we're complaining about C++0x compatibility. */
13689 gcc_rich_location richloc (token->location);
13690 richloc.add_fixit_remove ();
13691 warning_at (&richloc, OPT_Wc__11_compat,
13692 "%<auto%> changes meaning in C++11; "
13693 "please remove it");
13694
13695 /* Set the storage class anyway. */
13696 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13697 token);
13698 }
13699 else
13700 /* C++0x auto type-specifier. */
13701 found_decl_spec = false;
13702 break;
13703
13704 case RID_REGISTER:
13705 case RID_STATIC:
13706 case RID_EXTERN:
13707 case RID_MUTABLE:
13708 /* Consume the token. */
13709 cp_lexer_consume_token (parser->lexer);
13710 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13711 token);
13712 break;
13713 case RID_THREAD:
13714 /* Consume the token. */
13715 ds = ds_thread;
13716 cp_lexer_consume_token (parser->lexer);
13717 break;
13718
13719 default:
13720 /* We did not yet find a decl-specifier yet. */
13721 found_decl_spec = false;
13722 break;
13723 }
13724
13725 if (found_decl_spec
13726 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13727 && token->keyword != RID_CONSTEXPR)
13728 error ("decl-specifier invalid in condition");
13729
13730 if (found_decl_spec
13731 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13732 && token->keyword != RID_MUTABLE
13733 && token->keyword != RID_CONSTEXPR)
13734 error_at (token->location, "%qD invalid in lambda",
13735 ridpointers[token->keyword]);
13736
13737 if (ds != ds_last)
13738 set_and_check_decl_spec_loc (decl_specs, ds, token);
13739
13740 /* Constructors are a special case. The `S' in `S()' is not a
13741 decl-specifier; it is the beginning of the declarator. */
13742 constructor_p
13743 = (!found_decl_spec
13744 && constructor_possible_p
13745 && (cp_parser_constructor_declarator_p
13746 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13747
13748 /* If we don't have a DECL_SPEC yet, then we must be looking at
13749 a type-specifier. */
13750 if (!found_decl_spec && !constructor_p)
13751 {
13752 int decl_spec_declares_class_or_enum;
13753 bool is_cv_qualifier;
13754 tree type_spec;
13755
13756 type_spec
13757 = cp_parser_type_specifier (parser, flags,
13758 decl_specs,
13759 /*is_declaration=*/true,
13760 &decl_spec_declares_class_or_enum,
13761 &is_cv_qualifier);
13762 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13763
13764 /* If this type-specifier referenced a user-defined type
13765 (a typedef, class-name, etc.), then we can't allow any
13766 more such type-specifiers henceforth.
13767
13768 [dcl.spec]
13769
13770 The longest sequence of decl-specifiers that could
13771 possibly be a type name is taken as the
13772 decl-specifier-seq of a declaration. The sequence shall
13773 be self-consistent as described below.
13774
13775 [dcl.type]
13776
13777 As a general rule, at most one type-specifier is allowed
13778 in the complete decl-specifier-seq of a declaration. The
13779 only exceptions are the following:
13780
13781 -- const or volatile can be combined with any other
13782 type-specifier.
13783
13784 -- signed or unsigned can be combined with char, long,
13785 short, or int.
13786
13787 -- ..
13788
13789 Example:
13790
13791 typedef char* Pc;
13792 void g (const int Pc);
13793
13794 Here, Pc is *not* part of the decl-specifier seq; it's
13795 the declarator. Therefore, once we see a type-specifier
13796 (other than a cv-qualifier), we forbid any additional
13797 user-defined types. We *do* still allow things like `int
13798 int' to be considered a decl-specifier-seq, and issue the
13799 error message later. */
13800 if (type_spec && !is_cv_qualifier)
13801 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13802 /* A constructor declarator cannot follow a type-specifier. */
13803 if (type_spec)
13804 {
13805 constructor_possible_p = false;
13806 found_decl_spec = true;
13807 if (!is_cv_qualifier)
13808 decl_specs->any_type_specifiers_p = true;
13809
13810 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13811 error_at (token->location, "type-specifier invalid in lambda");
13812 }
13813 }
13814
13815 /* If we still do not have a DECL_SPEC, then there are no more
13816 decl-specifiers. */
13817 if (!found_decl_spec)
13818 break;
13819
13820 decl_specs->any_specifiers_p = true;
13821 /* After we see one decl-specifier, further decl-specifiers are
13822 always optional. */
13823 flags |= CP_PARSER_FLAGS_OPTIONAL;
13824 }
13825
13826 /* Don't allow a friend specifier with a class definition. */
13827 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13828 && (*declares_class_or_enum & 2))
13829 error_at (decl_specs->locations[ds_friend],
13830 "class definition may not be declared a friend");
13831 }
13832
13833 /* Parse an (optional) storage-class-specifier.
13834
13835 storage-class-specifier:
13836 auto
13837 register
13838 static
13839 extern
13840 mutable
13841
13842 GNU Extension:
13843
13844 storage-class-specifier:
13845 thread
13846
13847 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13848
13849 static tree
13850 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13851 {
13852 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13853 {
13854 case RID_AUTO:
13855 if (cxx_dialect != cxx98)
13856 return NULL_TREE;
13857 /* Fall through for C++98. */
13858 gcc_fallthrough ();
13859
13860 case RID_REGISTER:
13861 case RID_STATIC:
13862 case RID_EXTERN:
13863 case RID_MUTABLE:
13864 case RID_THREAD:
13865 /* Consume the token. */
13866 return cp_lexer_consume_token (parser->lexer)->u.value;
13867
13868 default:
13869 return NULL_TREE;
13870 }
13871 }
13872
13873 /* Parse an (optional) function-specifier.
13874
13875 function-specifier:
13876 inline
13877 virtual
13878 explicit
13879
13880 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13881 Updates DECL_SPECS, if it is non-NULL. */
13882
13883 static tree
13884 cp_parser_function_specifier_opt (cp_parser* parser,
13885 cp_decl_specifier_seq *decl_specs)
13886 {
13887 cp_token *token = cp_lexer_peek_token (parser->lexer);
13888 switch (token->keyword)
13889 {
13890 case RID_INLINE:
13891 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13892 break;
13893
13894 case RID_VIRTUAL:
13895 /* 14.5.2.3 [temp.mem]
13896
13897 A member function template shall not be virtual. */
13898 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13899 && current_class_type)
13900 error_at (token->location, "templates may not be %<virtual%>");
13901 else
13902 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13903 break;
13904
13905 case RID_EXPLICIT:
13906 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13907 break;
13908
13909 default:
13910 return NULL_TREE;
13911 }
13912
13913 /* Consume the token. */
13914 return cp_lexer_consume_token (parser->lexer)->u.value;
13915 }
13916
13917 /* Parse a linkage-specification.
13918
13919 linkage-specification:
13920 extern string-literal { declaration-seq [opt] }
13921 extern string-literal declaration */
13922
13923 static void
13924 cp_parser_linkage_specification (cp_parser* parser)
13925 {
13926 tree linkage;
13927
13928 /* Look for the `extern' keyword. */
13929 cp_token *extern_token
13930 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13931
13932 /* Look for the string-literal. */
13933 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13934 linkage = cp_parser_string_literal (parser, false, false);
13935
13936 /* Transform the literal into an identifier. If the literal is a
13937 wide-character string, or contains embedded NULs, then we can't
13938 handle it as the user wants. */
13939 if (strlen (TREE_STRING_POINTER (linkage))
13940 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13941 {
13942 cp_parser_error (parser, "invalid linkage-specification");
13943 /* Assume C++ linkage. */
13944 linkage = lang_name_cplusplus;
13945 }
13946 else
13947 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13948
13949 /* We're now using the new linkage. */
13950 push_lang_context (linkage);
13951
13952 /* Preserve the location of the the innermost linkage specification,
13953 tracking the locations of nested specifications via a local. */
13954 location_t saved_location
13955 = parser->innermost_linkage_specification_location;
13956 /* Construct a location ranging from the start of the "extern" to
13957 the end of the string-literal, with the caret at the start, e.g.:
13958 extern "C" {
13959 ^~~~~~~~~~
13960 */
13961 parser->innermost_linkage_specification_location
13962 = make_location (extern_token->location,
13963 extern_token->location,
13964 get_finish (string_token->location));
13965
13966 /* If the next token is a `{', then we're using the first
13967 production. */
13968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13969 {
13970 cp_ensure_no_omp_declare_simd (parser);
13971 cp_ensure_no_oacc_routine (parser);
13972
13973 /* Consume the `{' token. */
13974 matching_braces braces;
13975 braces.consume_open (parser)->location;
13976 /* Parse the declarations. */
13977 cp_parser_declaration_seq_opt (parser);
13978 /* Look for the closing `}'. */
13979 braces.require_close (parser);
13980 }
13981 /* Otherwise, there's just one declaration. */
13982 else
13983 {
13984 bool saved_in_unbraced_linkage_specification_p;
13985
13986 saved_in_unbraced_linkage_specification_p
13987 = parser->in_unbraced_linkage_specification_p;
13988 parser->in_unbraced_linkage_specification_p = true;
13989 cp_parser_declaration (parser);
13990 parser->in_unbraced_linkage_specification_p
13991 = saved_in_unbraced_linkage_specification_p;
13992 }
13993
13994 /* We're done with the linkage-specification. */
13995 pop_lang_context ();
13996
13997 /* Restore location of parent linkage specification, if any. */
13998 parser->innermost_linkage_specification_location = saved_location;
13999 }
14000
14001 /* Parse a static_assert-declaration.
14002
14003 static_assert-declaration:
14004 static_assert ( constant-expression , string-literal ) ;
14005 static_assert ( constant-expression ) ; (C++17)
14006
14007 If MEMBER_P, this static_assert is a class member. */
14008
14009 static void
14010 cp_parser_static_assert(cp_parser *parser, bool member_p)
14011 {
14012 cp_expr condition;
14013 location_t token_loc;
14014 tree message;
14015 bool dummy;
14016
14017 /* Peek at the `static_assert' token so we can keep track of exactly
14018 where the static assertion started. */
14019 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14020
14021 /* Look for the `static_assert' keyword. */
14022 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14023 RT_STATIC_ASSERT))
14024 return;
14025
14026 /* We know we are in a static assertion; commit to any tentative
14027 parse. */
14028 if (cp_parser_parsing_tentatively (parser))
14029 cp_parser_commit_to_tentative_parse (parser);
14030
14031 /* Parse the `(' starting the static assertion condition. */
14032 matching_parens parens;
14033 parens.require_open (parser);
14034
14035 /* Parse the constant-expression. Allow a non-constant expression
14036 here in order to give better diagnostics in finish_static_assert. */
14037 condition =
14038 cp_parser_constant_expression (parser,
14039 /*allow_non_constant_p=*/true,
14040 /*non_constant_p=*/&dummy);
14041
14042 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14043 {
14044 if (cxx_dialect < cxx17)
14045 pedwarn (input_location, OPT_Wpedantic,
14046 "static_assert without a message "
14047 "only available with -std=c++17 or -std=gnu++17");
14048 /* Eat the ')' */
14049 cp_lexer_consume_token (parser->lexer);
14050 message = build_string (1, "");
14051 TREE_TYPE (message) = char_array_type_node;
14052 fix_string_type (message);
14053 }
14054 else
14055 {
14056 /* Parse the separating `,'. */
14057 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14058
14059 /* Parse the string-literal message. */
14060 message = cp_parser_string_literal (parser,
14061 /*translate=*/false,
14062 /*wide_ok=*/true);
14063
14064 /* A `)' completes the static assertion. */
14065 if (!parens.require_close (parser))
14066 cp_parser_skip_to_closing_parenthesis (parser,
14067 /*recovering=*/true,
14068 /*or_comma=*/false,
14069 /*consume_paren=*/true);
14070 }
14071
14072 /* A semicolon terminates the declaration. */
14073 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14074
14075 /* Get the location for the static assertion. Use that of the
14076 condition if available, otherwise, use that of the "static_assert"
14077 token. */
14078 location_t assert_loc = condition.get_location ();
14079 if (assert_loc == UNKNOWN_LOCATION)
14080 assert_loc = token_loc;
14081
14082 /* Complete the static assertion, which may mean either processing
14083 the static assert now or saving it for template instantiation. */
14084 finish_static_assert (condition, message, assert_loc, member_p);
14085 }
14086
14087 /* Parse the expression in decltype ( expression ). */
14088
14089 static tree
14090 cp_parser_decltype_expr (cp_parser *parser,
14091 bool &id_expression_or_member_access_p)
14092 {
14093 cp_token *id_expr_start_token;
14094 tree expr;
14095
14096 /* Since we're going to preserve any side-effects from this parse, set up a
14097 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14098 in the expression. */
14099 tentative_firewall firewall (parser);
14100
14101 /* First, try parsing an id-expression. */
14102 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14103 cp_parser_parse_tentatively (parser);
14104 expr = cp_parser_id_expression (parser,
14105 /*template_keyword_p=*/false,
14106 /*check_dependency_p=*/true,
14107 /*template_p=*/NULL,
14108 /*declarator_p=*/false,
14109 /*optional_p=*/false);
14110
14111 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14112 {
14113 bool non_integral_constant_expression_p = false;
14114 tree id_expression = expr;
14115 cp_id_kind idk;
14116 const char *error_msg;
14117
14118 if (identifier_p (expr))
14119 /* Lookup the name we got back from the id-expression. */
14120 expr = cp_parser_lookup_name_simple (parser, expr,
14121 id_expr_start_token->location);
14122
14123 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14124 /* A template without args is not a complete id-expression. */
14125 expr = error_mark_node;
14126
14127 if (expr
14128 && expr != error_mark_node
14129 && TREE_CODE (expr) != TYPE_DECL
14130 && (TREE_CODE (expr) != BIT_NOT_EXPR
14131 || !TYPE_P (TREE_OPERAND (expr, 0)))
14132 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14133 {
14134 /* Complete lookup of the id-expression. */
14135 expr = (finish_id_expression
14136 (id_expression, expr, parser->scope, &idk,
14137 /*integral_constant_expression_p=*/false,
14138 /*allow_non_integral_constant_expression_p=*/true,
14139 &non_integral_constant_expression_p,
14140 /*template_p=*/false,
14141 /*done=*/true,
14142 /*address_p=*/false,
14143 /*template_arg_p=*/false,
14144 &error_msg,
14145 id_expr_start_token->location));
14146
14147 if (expr == error_mark_node)
14148 /* We found an id-expression, but it was something that we
14149 should not have found. This is an error, not something
14150 we can recover from, so note that we found an
14151 id-expression and we'll recover as gracefully as
14152 possible. */
14153 id_expression_or_member_access_p = true;
14154 }
14155
14156 if (expr
14157 && expr != error_mark_node
14158 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14159 /* We have an id-expression. */
14160 id_expression_or_member_access_p = true;
14161 }
14162
14163 if (!id_expression_or_member_access_p)
14164 {
14165 /* Abort the id-expression parse. */
14166 cp_parser_abort_tentative_parse (parser);
14167
14168 /* Parsing tentatively, again. */
14169 cp_parser_parse_tentatively (parser);
14170
14171 /* Parse a class member access. */
14172 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14173 /*cast_p=*/false, /*decltype*/true,
14174 /*member_access_only_p=*/true, NULL);
14175
14176 if (expr
14177 && expr != error_mark_node
14178 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14179 /* We have an id-expression. */
14180 id_expression_or_member_access_p = true;
14181 }
14182
14183 if (id_expression_or_member_access_p)
14184 /* We have parsed the complete id-expression or member access. */
14185 cp_parser_parse_definitely (parser);
14186 else
14187 {
14188 /* Abort our attempt to parse an id-expression or member access
14189 expression. */
14190 cp_parser_abort_tentative_parse (parser);
14191
14192 /* Commit to the tentative_firewall so we get syntax errors. */
14193 cp_parser_commit_to_tentative_parse (parser);
14194
14195 /* Parse a full expression. */
14196 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14197 /*decltype_p=*/true);
14198 }
14199
14200 return expr;
14201 }
14202
14203 /* Parse a `decltype' type. Returns the type.
14204
14205 simple-type-specifier:
14206 decltype ( expression )
14207 C++14 proposal:
14208 decltype ( auto ) */
14209
14210 static tree
14211 cp_parser_decltype (cp_parser *parser)
14212 {
14213 bool id_expression_or_member_access_p = false;
14214 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14215
14216 if (start_token->type == CPP_DECLTYPE)
14217 {
14218 /* Already parsed. */
14219 cp_lexer_consume_token (parser->lexer);
14220 return saved_checks_value (start_token->u.tree_check_value);
14221 }
14222
14223 /* Look for the `decltype' token. */
14224 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14225 return error_mark_node;
14226
14227 /* Parse the opening `('. */
14228 matching_parens parens;
14229 if (!parens.require_open (parser))
14230 return error_mark_node;
14231
14232 push_deferring_access_checks (dk_deferred);
14233
14234 tree expr = NULL_TREE;
14235
14236 if (cxx_dialect >= cxx14
14237 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14238 /* decltype (auto) */
14239 cp_lexer_consume_token (parser->lexer);
14240 else
14241 {
14242 /* decltype (expression) */
14243
14244 /* Types cannot be defined in a `decltype' expression. Save away the
14245 old message and set the new one. */
14246 const char *saved_message = parser->type_definition_forbidden_message;
14247 parser->type_definition_forbidden_message
14248 = G_("types may not be defined in %<decltype%> expressions");
14249
14250 /* The restrictions on constant-expressions do not apply inside
14251 decltype expressions. */
14252 bool saved_integral_constant_expression_p
14253 = parser->integral_constant_expression_p;
14254 bool saved_non_integral_constant_expression_p
14255 = parser->non_integral_constant_expression_p;
14256 parser->integral_constant_expression_p = false;
14257
14258 /* Within a parenthesized expression, a `>' token is always
14259 the greater-than operator. */
14260 bool saved_greater_than_is_operator_p
14261 = parser->greater_than_is_operator_p;
14262 parser->greater_than_is_operator_p = true;
14263
14264 /* Do not actually evaluate the expression. */
14265 ++cp_unevaluated_operand;
14266
14267 /* Do not warn about problems with the expression. */
14268 ++c_inhibit_evaluation_warnings;
14269
14270 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14271
14272 /* Go back to evaluating expressions. */
14273 --cp_unevaluated_operand;
14274 --c_inhibit_evaluation_warnings;
14275
14276 /* The `>' token might be the end of a template-id or
14277 template-parameter-list now. */
14278 parser->greater_than_is_operator_p
14279 = saved_greater_than_is_operator_p;
14280
14281 /* Restore the old message and the integral constant expression
14282 flags. */
14283 parser->type_definition_forbidden_message = saved_message;
14284 parser->integral_constant_expression_p
14285 = saved_integral_constant_expression_p;
14286 parser->non_integral_constant_expression_p
14287 = saved_non_integral_constant_expression_p;
14288 }
14289
14290 /* Parse to the closing `)'. */
14291 if (!parens.require_close (parser))
14292 {
14293 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14294 /*consume_paren=*/true);
14295 pop_deferring_access_checks ();
14296 return error_mark_node;
14297 }
14298
14299 if (!expr)
14300 {
14301 /* Build auto. */
14302 expr = make_decltype_auto ();
14303 AUTO_IS_DECLTYPE (expr) = true;
14304 }
14305 else
14306 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14307 tf_warning_or_error);
14308
14309 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14310 it again. */
14311 start_token->type = CPP_DECLTYPE;
14312 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14313 start_token->u.tree_check_value->value = expr;
14314 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14315 start_token->keyword = RID_MAX;
14316 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14317
14318 pop_to_parent_deferring_access_checks ();
14319
14320 return expr;
14321 }
14322
14323 /* Special member functions [gram.special] */
14324
14325 /* Parse a conversion-function-id.
14326
14327 conversion-function-id:
14328 operator conversion-type-id
14329
14330 Returns an IDENTIFIER_NODE representing the operator. */
14331
14332 static tree
14333 cp_parser_conversion_function_id (cp_parser* parser)
14334 {
14335 tree type;
14336 tree saved_scope;
14337 tree saved_qualifying_scope;
14338 tree saved_object_scope;
14339 tree pushed_scope = NULL_TREE;
14340
14341 /* Look for the `operator' token. */
14342 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14343 return error_mark_node;
14344 /* When we parse the conversion-type-id, the current scope will be
14345 reset. However, we need that information in able to look up the
14346 conversion function later, so we save it here. */
14347 saved_scope = parser->scope;
14348 saved_qualifying_scope = parser->qualifying_scope;
14349 saved_object_scope = parser->object_scope;
14350 /* We must enter the scope of the class so that the names of
14351 entities declared within the class are available in the
14352 conversion-type-id. For example, consider:
14353
14354 struct S {
14355 typedef int I;
14356 operator I();
14357 };
14358
14359 S::operator I() { ... }
14360
14361 In order to see that `I' is a type-name in the definition, we
14362 must be in the scope of `S'. */
14363 if (saved_scope)
14364 pushed_scope = push_scope (saved_scope);
14365 /* Parse the conversion-type-id. */
14366 type = cp_parser_conversion_type_id (parser);
14367 /* Leave the scope of the class, if any. */
14368 if (pushed_scope)
14369 pop_scope (pushed_scope);
14370 /* Restore the saved scope. */
14371 parser->scope = saved_scope;
14372 parser->qualifying_scope = saved_qualifying_scope;
14373 parser->object_scope = saved_object_scope;
14374 /* If the TYPE is invalid, indicate failure. */
14375 if (type == error_mark_node)
14376 return error_mark_node;
14377 return make_conv_op_name (type);
14378 }
14379
14380 /* Parse a conversion-type-id:
14381
14382 conversion-type-id:
14383 type-specifier-seq conversion-declarator [opt]
14384
14385 Returns the TYPE specified. */
14386
14387 static tree
14388 cp_parser_conversion_type_id (cp_parser* parser)
14389 {
14390 tree attributes;
14391 cp_decl_specifier_seq type_specifiers;
14392 cp_declarator *declarator;
14393 tree type_specified;
14394 const char *saved_message;
14395
14396 /* Parse the attributes. */
14397 attributes = cp_parser_attributes_opt (parser);
14398
14399 saved_message = parser->type_definition_forbidden_message;
14400 parser->type_definition_forbidden_message
14401 = G_("types may not be defined in a conversion-type-id");
14402
14403 /* Parse the type-specifiers. */
14404 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14405 /*is_trailing_return=*/false,
14406 &type_specifiers);
14407
14408 parser->type_definition_forbidden_message = saved_message;
14409
14410 /* If that didn't work, stop. */
14411 if (type_specifiers.type == error_mark_node)
14412 return error_mark_node;
14413 /* Parse the conversion-declarator. */
14414 declarator = cp_parser_conversion_declarator_opt (parser);
14415
14416 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14417 /*initialized=*/0, &attributes);
14418 if (attributes)
14419 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14420
14421 /* Don't give this error when parsing tentatively. This happens to
14422 work because we always parse this definitively once. */
14423 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14424 && type_uses_auto (type_specified))
14425 {
14426 if (cxx_dialect < cxx14)
14427 {
14428 error ("invalid use of %<auto%> in conversion operator");
14429 return error_mark_node;
14430 }
14431 else if (template_parm_scope_p ())
14432 warning (0, "use of %<auto%> in member template "
14433 "conversion operator can never be deduced");
14434 }
14435
14436 return type_specified;
14437 }
14438
14439 /* Parse an (optional) conversion-declarator.
14440
14441 conversion-declarator:
14442 ptr-operator conversion-declarator [opt]
14443
14444 */
14445
14446 static cp_declarator *
14447 cp_parser_conversion_declarator_opt (cp_parser* parser)
14448 {
14449 enum tree_code code;
14450 tree class_type, std_attributes = NULL_TREE;
14451 cp_cv_quals cv_quals;
14452
14453 /* We don't know if there's a ptr-operator next, or not. */
14454 cp_parser_parse_tentatively (parser);
14455 /* Try the ptr-operator. */
14456 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14457 &std_attributes);
14458 /* If it worked, look for more conversion-declarators. */
14459 if (cp_parser_parse_definitely (parser))
14460 {
14461 cp_declarator *declarator;
14462
14463 /* Parse another optional declarator. */
14464 declarator = cp_parser_conversion_declarator_opt (parser);
14465
14466 declarator = cp_parser_make_indirect_declarator
14467 (code, class_type, cv_quals, declarator, std_attributes);
14468
14469 return declarator;
14470 }
14471
14472 return NULL;
14473 }
14474
14475 /* Parse an (optional) ctor-initializer.
14476
14477 ctor-initializer:
14478 : mem-initializer-list */
14479
14480 static void
14481 cp_parser_ctor_initializer_opt (cp_parser* parser)
14482 {
14483 /* If the next token is not a `:', then there is no
14484 ctor-initializer. */
14485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14486 {
14487 /* Do default initialization of any bases and members. */
14488 if (DECL_CONSTRUCTOR_P (current_function_decl))
14489 finish_mem_initializers (NULL_TREE);
14490 return;
14491 }
14492
14493 /* Consume the `:' token. */
14494 cp_lexer_consume_token (parser->lexer);
14495 /* And the mem-initializer-list. */
14496 cp_parser_mem_initializer_list (parser);
14497 }
14498
14499 /* Parse a mem-initializer-list.
14500
14501 mem-initializer-list:
14502 mem-initializer ... [opt]
14503 mem-initializer ... [opt] , mem-initializer-list */
14504
14505 static void
14506 cp_parser_mem_initializer_list (cp_parser* parser)
14507 {
14508 tree mem_initializer_list = NULL_TREE;
14509 tree target_ctor = error_mark_node;
14510 cp_token *token = cp_lexer_peek_token (parser->lexer);
14511
14512 /* Let the semantic analysis code know that we are starting the
14513 mem-initializer-list. */
14514 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14515 error_at (token->location,
14516 "only constructors take member initializers");
14517
14518 /* Loop through the list. */
14519 while (true)
14520 {
14521 tree mem_initializer;
14522
14523 token = cp_lexer_peek_token (parser->lexer);
14524 /* Parse the mem-initializer. */
14525 mem_initializer = cp_parser_mem_initializer (parser);
14526 /* If the next token is a `...', we're expanding member initializers. */
14527 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14528 if (ellipsis
14529 || (mem_initializer != error_mark_node
14530 && check_for_bare_parameter_packs (TREE_PURPOSE
14531 (mem_initializer))))
14532 {
14533 /* Consume the `...'. */
14534 if (ellipsis)
14535 cp_lexer_consume_token (parser->lexer);
14536
14537 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14538 can be expanded but members cannot. */
14539 if (mem_initializer != error_mark_node
14540 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14541 {
14542 error_at (token->location,
14543 "cannot expand initializer for member %qD",
14544 TREE_PURPOSE (mem_initializer));
14545 mem_initializer = error_mark_node;
14546 }
14547
14548 /* Construct the pack expansion type. */
14549 if (mem_initializer != error_mark_node)
14550 mem_initializer = make_pack_expansion (mem_initializer);
14551 }
14552 if (target_ctor != error_mark_node
14553 && mem_initializer != error_mark_node)
14554 {
14555 error ("mem-initializer for %qD follows constructor delegation",
14556 TREE_PURPOSE (mem_initializer));
14557 mem_initializer = error_mark_node;
14558 }
14559 /* Look for a target constructor. */
14560 if (mem_initializer != error_mark_node
14561 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14562 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14563 {
14564 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14565 if (mem_initializer_list)
14566 {
14567 error ("constructor delegation follows mem-initializer for %qD",
14568 TREE_PURPOSE (mem_initializer_list));
14569 mem_initializer = error_mark_node;
14570 }
14571 target_ctor = mem_initializer;
14572 }
14573 /* Add it to the list, unless it was erroneous. */
14574 if (mem_initializer != error_mark_node)
14575 {
14576 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14577 mem_initializer_list = mem_initializer;
14578 }
14579 /* If the next token is not a `,', we're done. */
14580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14581 break;
14582 /* Consume the `,' token. */
14583 cp_lexer_consume_token (parser->lexer);
14584 }
14585
14586 /* Perform semantic analysis. */
14587 if (DECL_CONSTRUCTOR_P (current_function_decl))
14588 finish_mem_initializers (mem_initializer_list);
14589 }
14590
14591 /* Parse a mem-initializer.
14592
14593 mem-initializer:
14594 mem-initializer-id ( expression-list [opt] )
14595 mem-initializer-id braced-init-list
14596
14597 GNU extension:
14598
14599 mem-initializer:
14600 ( expression-list [opt] )
14601
14602 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14603 class) or FIELD_DECL (for a non-static data member) to initialize;
14604 the TREE_VALUE is the expression-list. An empty initialization
14605 list is represented by void_list_node. */
14606
14607 static tree
14608 cp_parser_mem_initializer (cp_parser* parser)
14609 {
14610 tree mem_initializer_id;
14611 tree expression_list;
14612 tree member;
14613 cp_token *token = cp_lexer_peek_token (parser->lexer);
14614
14615 /* Find out what is being initialized. */
14616 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14617 {
14618 permerror (token->location,
14619 "anachronistic old-style base class initializer");
14620 mem_initializer_id = NULL_TREE;
14621 }
14622 else
14623 {
14624 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14625 if (mem_initializer_id == error_mark_node)
14626 return mem_initializer_id;
14627 }
14628 member = expand_member_init (mem_initializer_id);
14629 if (member && !DECL_P (member))
14630 in_base_initializer = 1;
14631
14632 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14633 {
14634 bool expr_non_constant_p;
14635 cp_lexer_set_source_position (parser->lexer);
14636 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14637 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14638 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14639 expression_list = build_tree_list (NULL_TREE, expression_list);
14640 }
14641 else
14642 {
14643 vec<tree, va_gc> *vec;
14644 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14645 /*cast_p=*/false,
14646 /*allow_expansion_p=*/true,
14647 /*non_constant_p=*/NULL);
14648 if (vec == NULL)
14649 return error_mark_node;
14650 expression_list = build_tree_list_vec (vec);
14651 release_tree_vector (vec);
14652 }
14653
14654 if (expression_list == error_mark_node)
14655 return error_mark_node;
14656 if (!expression_list)
14657 expression_list = void_type_node;
14658
14659 in_base_initializer = 0;
14660
14661 return member ? build_tree_list (member, expression_list) : error_mark_node;
14662 }
14663
14664 /* Parse a mem-initializer-id.
14665
14666 mem-initializer-id:
14667 :: [opt] nested-name-specifier [opt] class-name
14668 decltype-specifier (C++11)
14669 identifier
14670
14671 Returns a TYPE indicating the class to be initialized for the first
14672 production (and the second in C++11). Returns an IDENTIFIER_NODE
14673 indicating the data member to be initialized for the last production. */
14674
14675 static tree
14676 cp_parser_mem_initializer_id (cp_parser* parser)
14677 {
14678 bool global_scope_p;
14679 bool nested_name_specifier_p;
14680 bool template_p = false;
14681 tree id;
14682
14683 cp_token *token = cp_lexer_peek_token (parser->lexer);
14684
14685 /* `typename' is not allowed in this context ([temp.res]). */
14686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14687 {
14688 error_at (token->location,
14689 "keyword %<typename%> not allowed in this context (a qualified "
14690 "member initializer is implicitly a type)");
14691 cp_lexer_consume_token (parser->lexer);
14692 }
14693 /* Look for the optional `::' operator. */
14694 global_scope_p
14695 = (cp_parser_global_scope_opt (parser,
14696 /*current_scope_valid_p=*/false)
14697 != NULL_TREE);
14698 /* Look for the optional nested-name-specifier. The simplest way to
14699 implement:
14700
14701 [temp.res]
14702
14703 The keyword `typename' is not permitted in a base-specifier or
14704 mem-initializer; in these contexts a qualified name that
14705 depends on a template-parameter is implicitly assumed to be a
14706 type name.
14707
14708 is to assume that we have seen the `typename' keyword at this
14709 point. */
14710 nested_name_specifier_p
14711 = (cp_parser_nested_name_specifier_opt (parser,
14712 /*typename_keyword_p=*/true,
14713 /*check_dependency_p=*/true,
14714 /*type_p=*/true,
14715 /*is_declaration=*/true)
14716 != NULL_TREE);
14717 if (nested_name_specifier_p)
14718 template_p = cp_parser_optional_template_keyword (parser);
14719 /* If there is a `::' operator or a nested-name-specifier, then we
14720 are definitely looking for a class-name. */
14721 if (global_scope_p || nested_name_specifier_p)
14722 return cp_parser_class_name (parser,
14723 /*typename_keyword_p=*/true,
14724 /*template_keyword_p=*/template_p,
14725 typename_type,
14726 /*check_dependency_p=*/true,
14727 /*class_head_p=*/false,
14728 /*is_declaration=*/true);
14729 /* Otherwise, we could also be looking for an ordinary identifier. */
14730 cp_parser_parse_tentatively (parser);
14731 if (cp_lexer_next_token_is_decltype (parser->lexer))
14732 /* Try a decltype-specifier. */
14733 id = cp_parser_decltype (parser);
14734 else
14735 /* Otherwise, try a class-name. */
14736 id = cp_parser_class_name (parser,
14737 /*typename_keyword_p=*/true,
14738 /*template_keyword_p=*/false,
14739 none_type,
14740 /*check_dependency_p=*/true,
14741 /*class_head_p=*/false,
14742 /*is_declaration=*/true);
14743 /* If we found one, we're done. */
14744 if (cp_parser_parse_definitely (parser))
14745 return id;
14746 /* Otherwise, look for an ordinary identifier. */
14747 return cp_parser_identifier (parser);
14748 }
14749
14750 /* Overloading [gram.over] */
14751
14752 /* Parse an operator-function-id.
14753
14754 operator-function-id:
14755 operator operator
14756
14757 Returns an IDENTIFIER_NODE for the operator which is a
14758 human-readable spelling of the identifier, e.g., `operator +'. */
14759
14760 static cp_expr
14761 cp_parser_operator_function_id (cp_parser* parser)
14762 {
14763 /* Look for the `operator' keyword. */
14764 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14765 return error_mark_node;
14766 /* And then the name of the operator itself. */
14767 return cp_parser_operator (parser);
14768 }
14769
14770 /* Return an identifier node for a user-defined literal operator.
14771 The suffix identifier is chained to the operator name identifier. */
14772
14773 tree
14774 cp_literal_operator_id (const char* name)
14775 {
14776 tree identifier;
14777 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14778 + strlen (name) + 10);
14779 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14780 identifier = get_identifier (buffer);
14781
14782 return identifier;
14783 }
14784
14785 /* Parse an operator.
14786
14787 operator:
14788 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14789 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14790 || ++ -- , ->* -> () []
14791
14792 GNU Extensions:
14793
14794 operator:
14795 <? >? <?= >?=
14796
14797 Returns an IDENTIFIER_NODE for the operator which is a
14798 human-readable spelling of the identifier, e.g., `operator +'. */
14799
14800 static cp_expr
14801 cp_parser_operator (cp_parser* parser)
14802 {
14803 tree id = NULL_TREE;
14804 cp_token *token;
14805 bool utf8 = false;
14806
14807 /* Peek at the next token. */
14808 token = cp_lexer_peek_token (parser->lexer);
14809
14810 location_t start_loc = token->location;
14811
14812 /* Figure out which operator we have. */
14813 enum tree_code op = ERROR_MARK;
14814 bool assop = false;
14815 bool consumed = false;
14816 switch (token->type)
14817 {
14818 case CPP_KEYWORD:
14819 {
14820 /* The keyword should be either `new' or `delete'. */
14821 if (token->keyword == RID_NEW)
14822 op = NEW_EXPR;
14823 else if (token->keyword == RID_DELETE)
14824 op = DELETE_EXPR;
14825 else
14826 break;
14827
14828 /* Consume the `new' or `delete' token. */
14829 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14830
14831 /* Peek at the next token. */
14832 token = cp_lexer_peek_token (parser->lexer);
14833 /* If it's a `[' token then this is the array variant of the
14834 operator. */
14835 if (token->type == CPP_OPEN_SQUARE)
14836 {
14837 /* Consume the `[' token. */
14838 cp_lexer_consume_token (parser->lexer);
14839 /* Look for the `]' token. */
14840 if (cp_token *close_token
14841 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14842 end_loc = close_token->location;
14843 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14844 }
14845 start_loc = make_location (start_loc, start_loc, end_loc);
14846 consumed = true;
14847 break;
14848 }
14849
14850 case CPP_PLUS:
14851 op = PLUS_EXPR;
14852 break;
14853
14854 case CPP_MINUS:
14855 op = MINUS_EXPR;
14856 break;
14857
14858 case CPP_MULT:
14859 op = MULT_EXPR;
14860 break;
14861
14862 case CPP_DIV:
14863 op = TRUNC_DIV_EXPR;
14864 break;
14865
14866 case CPP_MOD:
14867 op = TRUNC_MOD_EXPR;
14868 break;
14869
14870 case CPP_XOR:
14871 op = BIT_XOR_EXPR;
14872 break;
14873
14874 case CPP_AND:
14875 op = BIT_AND_EXPR;
14876 break;
14877
14878 case CPP_OR:
14879 op = BIT_IOR_EXPR;
14880 break;
14881
14882 case CPP_COMPL:
14883 op = BIT_NOT_EXPR;
14884 break;
14885
14886 case CPP_NOT:
14887 op = TRUTH_NOT_EXPR;
14888 break;
14889
14890 case CPP_EQ:
14891 assop = true;
14892 op = NOP_EXPR;
14893 break;
14894
14895 case CPP_LESS:
14896 op = LT_EXPR;
14897 break;
14898
14899 case CPP_GREATER:
14900 op = GT_EXPR;
14901 break;
14902
14903 case CPP_PLUS_EQ:
14904 assop = true;
14905 op = PLUS_EXPR;
14906 break;
14907
14908 case CPP_MINUS_EQ:
14909 assop = true;
14910 op = MINUS_EXPR;
14911 break;
14912
14913 case CPP_MULT_EQ:
14914 assop = true;
14915 op = MULT_EXPR;
14916 break;
14917
14918 case CPP_DIV_EQ:
14919 assop = true;
14920 op = TRUNC_DIV_EXPR;
14921 break;
14922
14923 case CPP_MOD_EQ:
14924 assop = true;
14925 op = TRUNC_MOD_EXPR;
14926 break;
14927
14928 case CPP_XOR_EQ:
14929 assop = true;
14930 op = BIT_XOR_EXPR;
14931 break;
14932
14933 case CPP_AND_EQ:
14934 assop = true;
14935 op = BIT_AND_EXPR;
14936 break;
14937
14938 case CPP_OR_EQ:
14939 assop = true;
14940 op = BIT_IOR_EXPR;
14941 break;
14942
14943 case CPP_LSHIFT:
14944 op = LSHIFT_EXPR;
14945 break;
14946
14947 case CPP_RSHIFT:
14948 op = RSHIFT_EXPR;
14949 break;
14950
14951 case CPP_LSHIFT_EQ:
14952 assop = true;
14953 op = LSHIFT_EXPR;
14954 break;
14955
14956 case CPP_RSHIFT_EQ:
14957 assop = true;
14958 op = RSHIFT_EXPR;
14959 break;
14960
14961 case CPP_EQ_EQ:
14962 op = EQ_EXPR;
14963 break;
14964
14965 case CPP_NOT_EQ:
14966 op = NE_EXPR;
14967 break;
14968
14969 case CPP_LESS_EQ:
14970 op = LE_EXPR;
14971 break;
14972
14973 case CPP_GREATER_EQ:
14974 op = GE_EXPR;
14975 break;
14976
14977 case CPP_AND_AND:
14978 op = TRUTH_ANDIF_EXPR;
14979 break;
14980
14981 case CPP_OR_OR:
14982 op = TRUTH_ORIF_EXPR;
14983 break;
14984
14985 case CPP_PLUS_PLUS:
14986 op = POSTINCREMENT_EXPR;
14987 break;
14988
14989 case CPP_MINUS_MINUS:
14990 op = PREDECREMENT_EXPR;
14991 break;
14992
14993 case CPP_COMMA:
14994 op = COMPOUND_EXPR;
14995 break;
14996
14997 case CPP_DEREF_STAR:
14998 op = MEMBER_REF;
14999 break;
15000
15001 case CPP_DEREF:
15002 op = COMPONENT_REF;
15003 break;
15004
15005 case CPP_OPEN_PAREN:
15006 {
15007 /* Consume the `('. */
15008 matching_parens parens;
15009 parens.consume_open (parser);
15010 /* Look for the matching `)'. */
15011 parens.require_close (parser);
15012 op = CALL_EXPR;
15013 consumed = true;
15014 break;
15015 }
15016
15017 case CPP_OPEN_SQUARE:
15018 /* Consume the `['. */
15019 cp_lexer_consume_token (parser->lexer);
15020 /* Look for the matching `]'. */
15021 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15022 op = ARRAY_REF;
15023 consumed = true;
15024 break;
15025
15026 case CPP_UTF8STRING:
15027 case CPP_UTF8STRING_USERDEF:
15028 utf8 = true;
15029 /* FALLTHRU */
15030 case CPP_STRING:
15031 case CPP_WSTRING:
15032 case CPP_STRING16:
15033 case CPP_STRING32:
15034 case CPP_STRING_USERDEF:
15035 case CPP_WSTRING_USERDEF:
15036 case CPP_STRING16_USERDEF:
15037 case CPP_STRING32_USERDEF:
15038 {
15039 tree str, string_tree;
15040 int sz, len;
15041
15042 if (cxx_dialect == cxx98)
15043 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15044
15045 /* Consume the string. */
15046 str = cp_parser_string_literal (parser, /*translate=*/true,
15047 /*wide_ok=*/true, /*lookup_udlit=*/false);
15048 if (str == error_mark_node)
15049 return error_mark_node;
15050 else if (TREE_CODE (str) == USERDEF_LITERAL)
15051 {
15052 string_tree = USERDEF_LITERAL_VALUE (str);
15053 id = USERDEF_LITERAL_SUFFIX_ID (str);
15054 }
15055 else
15056 {
15057 string_tree = str;
15058 /* Look for the suffix identifier. */
15059 token = cp_lexer_peek_token (parser->lexer);
15060 if (token->type == CPP_NAME)
15061 id = cp_parser_identifier (parser);
15062 else if (token->type == CPP_KEYWORD)
15063 {
15064 error ("unexpected keyword;"
15065 " remove space between quotes and suffix identifier");
15066 return error_mark_node;
15067 }
15068 else
15069 {
15070 error ("expected suffix identifier");
15071 return error_mark_node;
15072 }
15073 }
15074 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15075 (TREE_TYPE (TREE_TYPE (string_tree))));
15076 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15077 if (len != 0)
15078 {
15079 error ("expected empty string after %<operator%> keyword");
15080 return error_mark_node;
15081 }
15082 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15083 != char_type_node)
15084 {
15085 error ("invalid encoding prefix in literal operator");
15086 return error_mark_node;
15087 }
15088 if (id != error_mark_node)
15089 {
15090 const char *name = IDENTIFIER_POINTER (id);
15091 id = cp_literal_operator_id (name);
15092 }
15093 return id;
15094 }
15095
15096 default:
15097 /* Anything else is an error. */
15098 break;
15099 }
15100
15101 /* If we have selected an identifier, we need to consume the
15102 operator token. */
15103 if (op != ERROR_MARK)
15104 {
15105 id = ovl_op_identifier (assop, op);
15106 if (!consumed)
15107 cp_lexer_consume_token (parser->lexer);
15108 }
15109 /* Otherwise, no valid operator name was present. */
15110 else
15111 {
15112 cp_parser_error (parser, "expected operator");
15113 id = error_mark_node;
15114 }
15115
15116 return cp_expr (id, start_loc);
15117 }
15118
15119 /* Parse a template-declaration.
15120
15121 template-declaration:
15122 export [opt] template < template-parameter-list > declaration
15123
15124 If MEMBER_P is TRUE, this template-declaration occurs within a
15125 class-specifier.
15126
15127 The grammar rule given by the standard isn't correct. What
15128 is really meant is:
15129
15130 template-declaration:
15131 export [opt] template-parameter-list-seq
15132 decl-specifier-seq [opt] init-declarator [opt] ;
15133 export [opt] template-parameter-list-seq
15134 function-definition
15135
15136 template-parameter-list-seq:
15137 template-parameter-list-seq [opt]
15138 template < template-parameter-list >
15139
15140 Concept Extensions:
15141
15142 template-parameter-list-seq:
15143 template < template-parameter-list > requires-clause [opt]
15144
15145 requires-clause:
15146 requires logical-or-expression */
15147
15148 static void
15149 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15150 {
15151 /* Check for `export'. */
15152 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15153 {
15154 /* Consume the `export' token. */
15155 cp_lexer_consume_token (parser->lexer);
15156 /* Warn that we do not support `export'. */
15157 warning (0, "keyword %<export%> not implemented, and will be ignored");
15158 }
15159
15160 cp_parser_template_declaration_after_export (parser, member_p);
15161 }
15162
15163 /* Parse a template-parameter-list.
15164
15165 template-parameter-list:
15166 template-parameter
15167 template-parameter-list , template-parameter
15168
15169 Returns a TREE_LIST. Each node represents a template parameter.
15170 The nodes are connected via their TREE_CHAINs. */
15171
15172 static tree
15173 cp_parser_template_parameter_list (cp_parser* parser)
15174 {
15175 tree parameter_list = NULL_TREE;
15176
15177 begin_template_parm_list ();
15178
15179 /* The loop below parses the template parms. We first need to know
15180 the total number of template parms to be able to compute proper
15181 canonical types of each dependent type. So after the loop, when
15182 we know the total number of template parms,
15183 end_template_parm_list computes the proper canonical types and
15184 fixes up the dependent types accordingly. */
15185 while (true)
15186 {
15187 tree parameter;
15188 bool is_non_type;
15189 bool is_parameter_pack;
15190 location_t parm_loc;
15191
15192 /* Parse the template-parameter. */
15193 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15194 parameter = cp_parser_template_parameter (parser,
15195 &is_non_type,
15196 &is_parameter_pack);
15197 /* Add it to the list. */
15198 if (parameter != error_mark_node)
15199 parameter_list = process_template_parm (parameter_list,
15200 parm_loc,
15201 parameter,
15202 is_non_type,
15203 is_parameter_pack);
15204 else
15205 {
15206 tree err_parm = build_tree_list (parameter, parameter);
15207 parameter_list = chainon (parameter_list, err_parm);
15208 }
15209
15210 /* If the next token is not a `,', we're done. */
15211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15212 break;
15213 /* Otherwise, consume the `,' token. */
15214 cp_lexer_consume_token (parser->lexer);
15215 }
15216
15217 return end_template_parm_list (parameter_list);
15218 }
15219
15220 /* Parse a introduction-list.
15221
15222 introduction-list:
15223 introduced-parameter
15224 introduction-list , introduced-parameter
15225
15226 introduced-parameter:
15227 ...[opt] identifier
15228
15229 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15230 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15231 WILDCARD_DECL will also have DECL_NAME set and token location in
15232 DECL_SOURCE_LOCATION. */
15233
15234 static tree
15235 cp_parser_introduction_list (cp_parser *parser)
15236 {
15237 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15238
15239 while (true)
15240 {
15241 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15242 if (is_pack)
15243 cp_lexer_consume_token (parser->lexer);
15244
15245 /* Build placeholder. */
15246 tree parm = build_nt (WILDCARD_DECL);
15247 DECL_SOURCE_LOCATION (parm)
15248 = cp_lexer_peek_token (parser->lexer)->location;
15249 DECL_NAME (parm) = cp_parser_identifier (parser);
15250 WILDCARD_PACK_P (parm) = is_pack;
15251 vec_safe_push (introduction_vec, parm);
15252
15253 /* If the next token is not a `,', we're done. */
15254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15255 break;
15256 /* Otherwise, consume the `,' token. */
15257 cp_lexer_consume_token (parser->lexer);
15258 }
15259
15260 /* Convert the vec into a TREE_VEC. */
15261 tree introduction_list = make_tree_vec (introduction_vec->length ());
15262 unsigned int n;
15263 tree parm;
15264 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15265 TREE_VEC_ELT (introduction_list, n) = parm;
15266
15267 release_tree_vector (introduction_vec);
15268 return introduction_list;
15269 }
15270
15271 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15272 is an abstract declarator. */
15273
15274 static inline cp_declarator*
15275 get_id_declarator (cp_declarator *declarator)
15276 {
15277 cp_declarator *d = declarator;
15278 while (d && d->kind != cdk_id)
15279 d = d->declarator;
15280 return d;
15281 }
15282
15283 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15284 is an abstract declarator. */
15285
15286 static inline tree
15287 get_unqualified_id (cp_declarator *declarator)
15288 {
15289 declarator = get_id_declarator (declarator);
15290 if (declarator)
15291 return declarator->u.id.unqualified_name;
15292 else
15293 return NULL_TREE;
15294 }
15295
15296 /* Returns true if DECL represents a constrained-parameter. */
15297
15298 static inline bool
15299 is_constrained_parameter (tree decl)
15300 {
15301 return (decl
15302 && TREE_CODE (decl) == TYPE_DECL
15303 && CONSTRAINED_PARM_CONCEPT (decl)
15304 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15305 }
15306
15307 /* Returns true if PARM declares a constrained-parameter. */
15308
15309 static inline bool
15310 is_constrained_parameter (cp_parameter_declarator *parm)
15311 {
15312 return is_constrained_parameter (parm->decl_specifiers.type);
15313 }
15314
15315 /* Check that the type parameter is only a declarator-id, and that its
15316 type is not cv-qualified. */
15317
15318 bool
15319 cp_parser_check_constrained_type_parm (cp_parser *parser,
15320 cp_parameter_declarator *parm)
15321 {
15322 if (!parm->declarator)
15323 return true;
15324
15325 if (parm->declarator->kind != cdk_id)
15326 {
15327 cp_parser_error (parser, "invalid constrained type parameter");
15328 return false;
15329 }
15330
15331 /* Don't allow cv-qualified type parameters. */
15332 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15333 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15334 {
15335 cp_parser_error (parser, "cv-qualified type parameter");
15336 return false;
15337 }
15338
15339 return true;
15340 }
15341
15342 /* Finish parsing/processing a template type parameter and checking
15343 various restrictions. */
15344
15345 static inline tree
15346 cp_parser_constrained_type_template_parm (cp_parser *parser,
15347 tree id,
15348 cp_parameter_declarator* parmdecl)
15349 {
15350 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15351 return finish_template_type_parm (class_type_node, id);
15352 else
15353 return error_mark_node;
15354 }
15355
15356 static tree
15357 finish_constrained_template_template_parm (tree proto, tree id)
15358 {
15359 /* FIXME: This should probably be copied, and we may need to adjust
15360 the template parameter depths. */
15361 tree saved_parms = current_template_parms;
15362 begin_template_parm_list ();
15363 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15364 end_template_parm_list ();
15365
15366 tree parm = finish_template_template_parm (class_type_node, id);
15367 current_template_parms = saved_parms;
15368
15369 return parm;
15370 }
15371
15372 /* Finish parsing/processing a template template parameter by borrowing
15373 the template parameter list from the prototype parameter. */
15374
15375 static tree
15376 cp_parser_constrained_template_template_parm (cp_parser *parser,
15377 tree proto,
15378 tree id,
15379 cp_parameter_declarator *parmdecl)
15380 {
15381 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15382 return error_mark_node;
15383 return finish_constrained_template_template_parm (proto, id);
15384 }
15385
15386 /* Create a new non-type template parameter from the given PARM
15387 declarator. */
15388
15389 static tree
15390 constrained_non_type_template_parm (bool *is_non_type,
15391 cp_parameter_declarator *parm)
15392 {
15393 *is_non_type = true;
15394 cp_declarator *decl = parm->declarator;
15395 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15396 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15397 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15398 }
15399
15400 /* Build a constrained template parameter based on the PARMDECL
15401 declarator. The type of PARMDECL is the constrained type, which
15402 refers to the prototype template parameter that ultimately
15403 specifies the type of the declared parameter. */
15404
15405 static tree
15406 finish_constrained_parameter (cp_parser *parser,
15407 cp_parameter_declarator *parmdecl,
15408 bool *is_non_type,
15409 bool *is_parameter_pack)
15410 {
15411 tree decl = parmdecl->decl_specifiers.type;
15412 tree id = get_unqualified_id (parmdecl->declarator);
15413 tree def = parmdecl->default_argument;
15414 tree proto = DECL_INITIAL (decl);
15415
15416 /* A template parameter constrained by a variadic concept shall also
15417 be declared as a template parameter pack. */
15418 bool is_variadic = template_parameter_pack_p (proto);
15419 if (is_variadic && !*is_parameter_pack)
15420 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15421
15422 /* Build the parameter. Return an error if the declarator was invalid. */
15423 tree parm;
15424 if (TREE_CODE (proto) == TYPE_DECL)
15425 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15426 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15427 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15428 parmdecl);
15429 else
15430 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15431 if (parm == error_mark_node)
15432 return error_mark_node;
15433
15434 /* Finish the parameter decl and create a node attaching the
15435 default argument and constraint. */
15436 parm = build_tree_list (def, parm);
15437 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15438
15439 return parm;
15440 }
15441
15442 /* Returns true if the parsed type actually represents the declaration
15443 of a type template-parameter. */
15444
15445 static inline bool
15446 declares_constrained_type_template_parameter (tree type)
15447 {
15448 return (is_constrained_parameter (type)
15449 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15450 }
15451
15452
15453 /* Returns true if the parsed type actually represents the declaration of
15454 a template template-parameter. */
15455
15456 static bool
15457 declares_constrained_template_template_parameter (tree type)
15458 {
15459 return (is_constrained_parameter (type)
15460 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15461 }
15462
15463 /* Parse a default argument for a type template-parameter.
15464 Note that diagnostics are handled in cp_parser_template_parameter. */
15465
15466 static tree
15467 cp_parser_default_type_template_argument (cp_parser *parser)
15468 {
15469 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15470
15471 /* Consume the `=' token. */
15472 cp_lexer_consume_token (parser->lexer);
15473
15474 cp_token *token = cp_lexer_peek_token (parser->lexer);
15475
15476 /* Parse the default-argument. */
15477 push_deferring_access_checks (dk_no_deferred);
15478 tree default_argument = cp_parser_type_id (parser);
15479 pop_deferring_access_checks ();
15480
15481 if (flag_concepts && type_uses_auto (default_argument))
15482 {
15483 error_at (token->location,
15484 "invalid use of %<auto%> in default template argument");
15485 return error_mark_node;
15486 }
15487
15488 return default_argument;
15489 }
15490
15491 /* Parse a default argument for a template template-parameter. */
15492
15493 static tree
15494 cp_parser_default_template_template_argument (cp_parser *parser)
15495 {
15496 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15497
15498 bool is_template;
15499
15500 /* Consume the `='. */
15501 cp_lexer_consume_token (parser->lexer);
15502 /* Parse the id-expression. */
15503 push_deferring_access_checks (dk_no_deferred);
15504 /* save token before parsing the id-expression, for error
15505 reporting */
15506 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15507 tree default_argument
15508 = cp_parser_id_expression (parser,
15509 /*template_keyword_p=*/false,
15510 /*check_dependency_p=*/true,
15511 /*template_p=*/&is_template,
15512 /*declarator_p=*/false,
15513 /*optional_p=*/false);
15514 if (TREE_CODE (default_argument) == TYPE_DECL)
15515 /* If the id-expression was a template-id that refers to
15516 a template-class, we already have the declaration here,
15517 so no further lookup is needed. */
15518 ;
15519 else
15520 /* Look up the name. */
15521 default_argument
15522 = cp_parser_lookup_name (parser, default_argument,
15523 none_type,
15524 /*is_template=*/is_template,
15525 /*is_namespace=*/false,
15526 /*check_dependency=*/true,
15527 /*ambiguous_decls=*/NULL,
15528 token->location);
15529 /* See if the default argument is valid. */
15530 default_argument = check_template_template_default_arg (default_argument);
15531 pop_deferring_access_checks ();
15532 return default_argument;
15533 }
15534
15535 /* Parse a template-parameter.
15536
15537 template-parameter:
15538 type-parameter
15539 parameter-declaration
15540
15541 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15542 the parameter. The TREE_PURPOSE is the default value, if any.
15543 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15544 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15545 set to true iff this parameter is a parameter pack. */
15546
15547 static tree
15548 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15549 bool *is_parameter_pack)
15550 {
15551 cp_token *token;
15552 cp_parameter_declarator *parameter_declarator;
15553 tree parm;
15554
15555 /* Assume it is a type parameter or a template parameter. */
15556 *is_non_type = false;
15557 /* Assume it not a parameter pack. */
15558 *is_parameter_pack = false;
15559 /* Peek at the next token. */
15560 token = cp_lexer_peek_token (parser->lexer);
15561 /* If it is `template', we have a type-parameter. */
15562 if (token->keyword == RID_TEMPLATE)
15563 return cp_parser_type_parameter (parser, is_parameter_pack);
15564 /* If it is `class' or `typename' we do not know yet whether it is a
15565 type parameter or a non-type parameter. Consider:
15566
15567 template <typename T, typename T::X X> ...
15568
15569 or:
15570
15571 template <class C, class D*> ...
15572
15573 Here, the first parameter is a type parameter, and the second is
15574 a non-type parameter. We can tell by looking at the token after
15575 the identifier -- if it is a `,', `=', or `>' then we have a type
15576 parameter. */
15577 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15578 {
15579 /* Peek at the token after `class' or `typename'. */
15580 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15581 /* If it's an ellipsis, we have a template type parameter
15582 pack. */
15583 if (token->type == CPP_ELLIPSIS)
15584 return cp_parser_type_parameter (parser, is_parameter_pack);
15585 /* If it's an identifier, skip it. */
15586 if (token->type == CPP_NAME)
15587 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15588 /* Now, see if the token looks like the end of a template
15589 parameter. */
15590 if (token->type == CPP_COMMA
15591 || token->type == CPP_EQ
15592 || token->type == CPP_GREATER)
15593 return cp_parser_type_parameter (parser, is_parameter_pack);
15594 }
15595
15596 /* Otherwise, it is a non-type parameter or a constrained parameter.
15597
15598 [temp.param]
15599
15600 When parsing a default template-argument for a non-type
15601 template-parameter, the first non-nested `>' is taken as the end
15602 of the template parameter-list rather than a greater-than
15603 operator. */
15604 parameter_declarator
15605 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15606 /*parenthesized_p=*/NULL);
15607
15608 if (!parameter_declarator)
15609 return error_mark_node;
15610
15611 /* If the parameter declaration is marked as a parameter pack, set
15612 *IS_PARAMETER_PACK to notify the caller. */
15613 if (parameter_declarator->template_parameter_pack_p)
15614 *is_parameter_pack = true;
15615
15616 if (parameter_declarator->default_argument)
15617 {
15618 /* Can happen in some cases of erroneous input (c++/34892). */
15619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15620 /* Consume the `...' for better error recovery. */
15621 cp_lexer_consume_token (parser->lexer);
15622 }
15623
15624 // The parameter may have been constrained.
15625 if (is_constrained_parameter (parameter_declarator))
15626 return finish_constrained_parameter (parser,
15627 parameter_declarator,
15628 is_non_type,
15629 is_parameter_pack);
15630
15631 // Now we're sure that the parameter is a non-type parameter.
15632 *is_non_type = true;
15633
15634 parm = grokdeclarator (parameter_declarator->declarator,
15635 &parameter_declarator->decl_specifiers,
15636 TPARM, /*initialized=*/0,
15637 /*attrlist=*/NULL);
15638 if (parm == error_mark_node)
15639 return error_mark_node;
15640
15641 return build_tree_list (parameter_declarator->default_argument, parm);
15642 }
15643
15644 /* Parse a type-parameter.
15645
15646 type-parameter:
15647 class identifier [opt]
15648 class identifier [opt] = type-id
15649 typename identifier [opt]
15650 typename identifier [opt] = type-id
15651 template < template-parameter-list > class identifier [opt]
15652 template < template-parameter-list > class identifier [opt]
15653 = id-expression
15654
15655 GNU Extension (variadic templates):
15656
15657 type-parameter:
15658 class ... identifier [opt]
15659 typename ... identifier [opt]
15660
15661 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15662 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15663 the declaration of the parameter.
15664
15665 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15666
15667 static tree
15668 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15669 {
15670 cp_token *token;
15671 tree parameter;
15672
15673 /* Look for a keyword to tell us what kind of parameter this is. */
15674 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15675 if (!token)
15676 return error_mark_node;
15677
15678 switch (token->keyword)
15679 {
15680 case RID_CLASS:
15681 case RID_TYPENAME:
15682 {
15683 tree identifier;
15684 tree default_argument;
15685
15686 /* If the next token is an ellipsis, we have a template
15687 argument pack. */
15688 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15689 {
15690 /* Consume the `...' token. */
15691 cp_lexer_consume_token (parser->lexer);
15692 maybe_warn_variadic_templates ();
15693
15694 *is_parameter_pack = true;
15695 }
15696
15697 /* If the next token is an identifier, then it names the
15698 parameter. */
15699 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15700 identifier = cp_parser_identifier (parser);
15701 else
15702 identifier = NULL_TREE;
15703
15704 /* Create the parameter. */
15705 parameter = finish_template_type_parm (class_type_node, identifier);
15706
15707 /* If the next token is an `=', we have a default argument. */
15708 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15709 {
15710 default_argument
15711 = cp_parser_default_type_template_argument (parser);
15712
15713 /* Template parameter packs cannot have default
15714 arguments. */
15715 if (*is_parameter_pack)
15716 {
15717 if (identifier)
15718 error_at (token->location,
15719 "template parameter pack %qD cannot have a "
15720 "default argument", identifier);
15721 else
15722 error_at (token->location,
15723 "template parameter packs cannot have "
15724 "default arguments");
15725 default_argument = NULL_TREE;
15726 }
15727 else if (check_for_bare_parameter_packs (default_argument))
15728 default_argument = error_mark_node;
15729 }
15730 else
15731 default_argument = NULL_TREE;
15732
15733 /* Create the combined representation of the parameter and the
15734 default argument. */
15735 parameter = build_tree_list (default_argument, parameter);
15736 }
15737 break;
15738
15739 case RID_TEMPLATE:
15740 {
15741 tree identifier;
15742 tree default_argument;
15743
15744 /* Look for the `<'. */
15745 cp_parser_require (parser, CPP_LESS, RT_LESS);
15746 /* Parse the template-parameter-list. */
15747 cp_parser_template_parameter_list (parser);
15748 /* Look for the `>'. */
15749 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15750
15751 // If template requirements are present, parse them.
15752 if (flag_concepts)
15753 {
15754 tree reqs = get_shorthand_constraints (current_template_parms);
15755 if (tree r = cp_parser_requires_clause_opt (parser))
15756 reqs = conjoin_constraints (reqs, normalize_expression (r));
15757 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15758 }
15759
15760 /* Look for the `class' or 'typename' keywords. */
15761 cp_parser_type_parameter_key (parser);
15762 /* If the next token is an ellipsis, we have a template
15763 argument pack. */
15764 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15765 {
15766 /* Consume the `...' token. */
15767 cp_lexer_consume_token (parser->lexer);
15768 maybe_warn_variadic_templates ();
15769
15770 *is_parameter_pack = true;
15771 }
15772 /* If the next token is an `=', then there is a
15773 default-argument. If the next token is a `>', we are at
15774 the end of the parameter-list. If the next token is a `,',
15775 then we are at the end of this parameter. */
15776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15777 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15778 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15779 {
15780 identifier = cp_parser_identifier (parser);
15781 /* Treat invalid names as if the parameter were nameless. */
15782 if (identifier == error_mark_node)
15783 identifier = NULL_TREE;
15784 }
15785 else
15786 identifier = NULL_TREE;
15787
15788 /* Create the template parameter. */
15789 parameter = finish_template_template_parm (class_type_node,
15790 identifier);
15791
15792 /* If the next token is an `=', then there is a
15793 default-argument. */
15794 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15795 {
15796 default_argument
15797 = cp_parser_default_template_template_argument (parser);
15798
15799 /* Template parameter packs cannot have default
15800 arguments. */
15801 if (*is_parameter_pack)
15802 {
15803 if (identifier)
15804 error_at (token->location,
15805 "template parameter pack %qD cannot "
15806 "have a default argument",
15807 identifier);
15808 else
15809 error_at (token->location, "template parameter packs cannot "
15810 "have default arguments");
15811 default_argument = NULL_TREE;
15812 }
15813 }
15814 else
15815 default_argument = NULL_TREE;
15816
15817 /* Create the combined representation of the parameter and the
15818 default argument. */
15819 parameter = build_tree_list (default_argument, parameter);
15820 }
15821 break;
15822
15823 default:
15824 gcc_unreachable ();
15825 break;
15826 }
15827
15828 return parameter;
15829 }
15830
15831 /* Parse a template-id.
15832
15833 template-id:
15834 template-name < template-argument-list [opt] >
15835
15836 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15837 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15838 returned. Otherwise, if the template-name names a function, or set
15839 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15840 names a class, returns a TYPE_DECL for the specialization.
15841
15842 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15843 uninstantiated templates. */
15844
15845 static tree
15846 cp_parser_template_id (cp_parser *parser,
15847 bool template_keyword_p,
15848 bool check_dependency_p,
15849 enum tag_types tag_type,
15850 bool is_declaration)
15851 {
15852 tree templ;
15853 tree arguments;
15854 tree template_id;
15855 cp_token_position start_of_id = 0;
15856 cp_token *next_token = NULL, *next_token_2 = NULL;
15857 bool is_identifier;
15858
15859 /* If the next token corresponds to a template-id, there is no need
15860 to reparse it. */
15861 cp_token *token = cp_lexer_peek_token (parser->lexer);
15862 if (token->type == CPP_TEMPLATE_ID)
15863 {
15864 cp_lexer_consume_token (parser->lexer);
15865 return saved_checks_value (token->u.tree_check_value);
15866 }
15867
15868 /* Avoid performing name lookup if there is no possibility of
15869 finding a template-id. */
15870 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15871 || (token->type == CPP_NAME
15872 && !cp_parser_nth_token_starts_template_argument_list_p
15873 (parser, 2)))
15874 {
15875 cp_parser_error (parser, "expected template-id");
15876 return error_mark_node;
15877 }
15878
15879 /* Remember where the template-id starts. */
15880 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15881 start_of_id = cp_lexer_token_position (parser->lexer, false);
15882
15883 push_deferring_access_checks (dk_deferred);
15884
15885 /* Parse the template-name. */
15886 is_identifier = false;
15887 templ = cp_parser_template_name (parser, template_keyword_p,
15888 check_dependency_p,
15889 is_declaration,
15890 tag_type,
15891 &is_identifier);
15892 if (templ == error_mark_node || is_identifier)
15893 {
15894 pop_deferring_access_checks ();
15895 return templ;
15896 }
15897
15898 /* Since we're going to preserve any side-effects from this parse, set up a
15899 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15900 in the template arguments. */
15901 tentative_firewall firewall (parser);
15902
15903 /* If we find the sequence `[:' after a template-name, it's probably
15904 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15905 parse correctly the argument list. */
15906 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15907 == CPP_OPEN_SQUARE)
15908 && next_token->flags & DIGRAPH
15909 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15910 == CPP_COLON)
15911 && !(next_token_2->flags & PREV_WHITE))
15912 {
15913 cp_parser_parse_tentatively (parser);
15914 /* Change `:' into `::'. */
15915 next_token_2->type = CPP_SCOPE;
15916 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15917 CPP_LESS. */
15918 cp_lexer_consume_token (parser->lexer);
15919
15920 /* Parse the arguments. */
15921 arguments = cp_parser_enclosed_template_argument_list (parser);
15922 if (!cp_parser_parse_definitely (parser))
15923 {
15924 /* If we couldn't parse an argument list, then we revert our changes
15925 and return simply an error. Maybe this is not a template-id
15926 after all. */
15927 next_token_2->type = CPP_COLON;
15928 cp_parser_error (parser, "expected %<<%>");
15929 pop_deferring_access_checks ();
15930 return error_mark_node;
15931 }
15932 /* Otherwise, emit an error about the invalid digraph, but continue
15933 parsing because we got our argument list. */
15934 if (permerror (next_token->location,
15935 "%<<::%> cannot begin a template-argument list"))
15936 {
15937 static bool hint = false;
15938 inform (next_token->location,
15939 "%<<:%> is an alternate spelling for %<[%>."
15940 " Insert whitespace between %<<%> and %<::%>");
15941 if (!hint && !flag_permissive)
15942 {
15943 inform (next_token->location, "(if you use %<-fpermissive%> "
15944 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15945 "accept your code)");
15946 hint = true;
15947 }
15948 }
15949 }
15950 else
15951 {
15952 /* Look for the `<' that starts the template-argument-list. */
15953 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15954 {
15955 pop_deferring_access_checks ();
15956 return error_mark_node;
15957 }
15958 /* Parse the arguments. */
15959 arguments = cp_parser_enclosed_template_argument_list (parser);
15960 }
15961
15962 /* Set the location to be of the form:
15963 template-name < template-argument-list [opt] >
15964 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15965 with caret == start at the start of the template-name,
15966 ranging until the closing '>'. */
15967 location_t finish_loc
15968 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15969 location_t combined_loc
15970 = make_location (token->location, token->location, finish_loc);
15971
15972 /* Check for concepts autos where they don't belong. We could
15973 identify types in some cases of idnetifier TEMPL, looking ahead
15974 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
15975 types. We reject them in functions, but if what we have is an
15976 identifier, even with none_type we can't conclude it's NOT a
15977 type, we have to wait for template substitution. */
15978 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
15979 template_id = error_mark_node;
15980 /* Build a representation of the specialization. */
15981 else if (identifier_p (templ))
15982 template_id = build_min_nt_loc (combined_loc,
15983 TEMPLATE_ID_EXPR,
15984 templ, arguments);
15985 else if (DECL_TYPE_TEMPLATE_P (templ)
15986 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15987 {
15988 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15989 template (rather than some instantiation thereof) only if
15990 is not nested within some other construct. For example, in
15991 "template <typename T> void f(T) { A<T>::", A<T> is just an
15992 instantiation of A. */
15993 bool entering_scope
15994 = (template_parm_scope_p ()
15995 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
15996 template_id
15997 = finish_template_type (templ, arguments, entering_scope);
15998 }
15999 /* A template-like identifier may be a partial concept id. */
16000 else if (flag_concepts
16001 && (template_id = (cp_parser_maybe_partial_concept_id
16002 (parser, templ, arguments))))
16003 return template_id;
16004 else if (variable_template_p (templ))
16005 {
16006 template_id = lookup_template_variable (templ, arguments);
16007 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16008 SET_EXPR_LOCATION (template_id, combined_loc);
16009 }
16010 else
16011 {
16012 /* If it's not a class-template or a template-template, it should be
16013 a function-template. */
16014 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16015 || TREE_CODE (templ) == OVERLOAD
16016 || BASELINK_P (templ)));
16017
16018 template_id = lookup_template_function (templ, arguments);
16019 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16020 SET_EXPR_LOCATION (template_id, combined_loc);
16021 }
16022
16023 /* If parsing tentatively, replace the sequence of tokens that makes
16024 up the template-id with a CPP_TEMPLATE_ID token. That way,
16025 should we re-parse the token stream, we will not have to repeat
16026 the effort required to do the parse, nor will we issue duplicate
16027 error messages about problems during instantiation of the
16028 template. */
16029 if (start_of_id
16030 /* Don't do this if we had a parse error in a declarator; re-parsing
16031 might succeed if a name changes meaning (60361). */
16032 && !(cp_parser_error_occurred (parser)
16033 && cp_parser_parsing_tentatively (parser)
16034 && parser->in_declarator_p))
16035 {
16036 /* Reset the contents of the START_OF_ID token. */
16037 token->type = CPP_TEMPLATE_ID;
16038 token->location = combined_loc;
16039
16040 /* Retrieve any deferred checks. Do not pop this access checks yet
16041 so the memory will not be reclaimed during token replacing below. */
16042 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16043 token->u.tree_check_value->value = template_id;
16044 token->u.tree_check_value->checks = get_deferred_access_checks ();
16045 token->keyword = RID_MAX;
16046
16047 /* Purge all subsequent tokens. */
16048 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16049
16050 /* ??? Can we actually assume that, if template_id ==
16051 error_mark_node, we will have issued a diagnostic to the
16052 user, as opposed to simply marking the tentative parse as
16053 failed? */
16054 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16055 error_at (token->location, "parse error in template argument list");
16056 }
16057
16058 pop_to_parent_deferring_access_checks ();
16059 return template_id;
16060 }
16061
16062 /* Parse a template-name.
16063
16064 template-name:
16065 identifier
16066
16067 The standard should actually say:
16068
16069 template-name:
16070 identifier
16071 operator-function-id
16072
16073 A defect report has been filed about this issue.
16074
16075 A conversion-function-id cannot be a template name because they cannot
16076 be part of a template-id. In fact, looking at this code:
16077
16078 a.operator K<int>()
16079
16080 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16081 It is impossible to call a templated conversion-function-id with an
16082 explicit argument list, since the only allowed template parameter is
16083 the type to which it is converting.
16084
16085 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16086 `template' keyword, in a construction like:
16087
16088 T::template f<3>()
16089
16090 In that case `f' is taken to be a template-name, even though there
16091 is no way of knowing for sure.
16092
16093 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16094 name refers to a set of overloaded functions, at least one of which
16095 is a template, or an IDENTIFIER_NODE with the name of the template,
16096 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16097 names are looked up inside uninstantiated templates. */
16098
16099 static tree
16100 cp_parser_template_name (cp_parser* parser,
16101 bool template_keyword_p,
16102 bool check_dependency_p,
16103 bool is_declaration,
16104 enum tag_types tag_type,
16105 bool *is_identifier)
16106 {
16107 tree identifier;
16108 tree decl;
16109 cp_token *token = cp_lexer_peek_token (parser->lexer);
16110
16111 /* If the next token is `operator', then we have either an
16112 operator-function-id or a conversion-function-id. */
16113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16114 {
16115 /* We don't know whether we're looking at an
16116 operator-function-id or a conversion-function-id. */
16117 cp_parser_parse_tentatively (parser);
16118 /* Try an operator-function-id. */
16119 identifier = cp_parser_operator_function_id (parser);
16120 /* If that didn't work, try a conversion-function-id. */
16121 if (!cp_parser_parse_definitely (parser))
16122 {
16123 cp_parser_error (parser, "expected template-name");
16124 return error_mark_node;
16125 }
16126 }
16127 /* Look for the identifier. */
16128 else
16129 identifier = cp_parser_identifier (parser);
16130
16131 /* If we didn't find an identifier, we don't have a template-id. */
16132 if (identifier == error_mark_node)
16133 return error_mark_node;
16134
16135 /* If the name immediately followed the `template' keyword, then it
16136 is a template-name. However, if the next token is not `<', then
16137 we do not treat it as a template-name, since it is not being used
16138 as part of a template-id. This enables us to handle constructs
16139 like:
16140
16141 template <typename T> struct S { S(); };
16142 template <typename T> S<T>::S();
16143
16144 correctly. We would treat `S' as a template -- if it were `S<T>'
16145 -- but we do not if there is no `<'. */
16146
16147 if (processing_template_decl
16148 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16149 {
16150 /* In a declaration, in a dependent context, we pretend that the
16151 "template" keyword was present in order to improve error
16152 recovery. For example, given:
16153
16154 template <typename T> void f(T::X<int>);
16155
16156 we want to treat "X<int>" as a template-id. */
16157 if (is_declaration
16158 && !template_keyword_p
16159 && parser->scope && TYPE_P (parser->scope)
16160 && check_dependency_p
16161 && dependent_scope_p (parser->scope)
16162 /* Do not do this for dtors (or ctors), since they never
16163 need the template keyword before their name. */
16164 && !constructor_name_p (identifier, parser->scope))
16165 {
16166 cp_token_position start = 0;
16167
16168 /* Explain what went wrong. */
16169 error_at (token->location, "non-template %qD used as template",
16170 identifier);
16171 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16172 parser->scope, identifier);
16173 /* If parsing tentatively, find the location of the "<" token. */
16174 if (cp_parser_simulate_error (parser))
16175 start = cp_lexer_token_position (parser->lexer, true);
16176 /* Parse the template arguments so that we can issue error
16177 messages about them. */
16178 cp_lexer_consume_token (parser->lexer);
16179 cp_parser_enclosed_template_argument_list (parser);
16180 /* Skip tokens until we find a good place from which to
16181 continue parsing. */
16182 cp_parser_skip_to_closing_parenthesis (parser,
16183 /*recovering=*/true,
16184 /*or_comma=*/true,
16185 /*consume_paren=*/false);
16186 /* If parsing tentatively, permanently remove the
16187 template argument list. That will prevent duplicate
16188 error messages from being issued about the missing
16189 "template" keyword. */
16190 if (start)
16191 cp_lexer_purge_tokens_after (parser->lexer, start);
16192 if (is_identifier)
16193 *is_identifier = true;
16194 parser->context->object_type = NULL_TREE;
16195 return identifier;
16196 }
16197
16198 /* If the "template" keyword is present, then there is generally
16199 no point in doing name-lookup, so we just return IDENTIFIER.
16200 But, if the qualifying scope is non-dependent then we can
16201 (and must) do name-lookup normally. */
16202 if (template_keyword_p)
16203 {
16204 tree scope = (parser->scope ? parser->scope
16205 : parser->context->object_type);
16206 if (scope && TYPE_P (scope)
16207 && (!CLASS_TYPE_P (scope)
16208 || (check_dependency_p && dependent_type_p (scope))))
16209 {
16210 /* We're optimizing away the call to cp_parser_lookup_name, but
16211 we still need to do this. */
16212 parser->context->object_type = NULL_TREE;
16213 return identifier;
16214 }
16215 }
16216 }
16217
16218 /* Look up the name. */
16219 decl = cp_parser_lookup_name (parser, identifier,
16220 tag_type,
16221 /*is_template=*/true,
16222 /*is_namespace=*/false,
16223 check_dependency_p,
16224 /*ambiguous_decls=*/NULL,
16225 token->location);
16226
16227 decl = strip_using_decl (decl);
16228
16229 /* If DECL is a template, then the name was a template-name. */
16230 if (TREE_CODE (decl) == TEMPLATE_DECL)
16231 {
16232 if (TREE_DEPRECATED (decl)
16233 && deprecated_state != DEPRECATED_SUPPRESS)
16234 warn_deprecated_use (decl, NULL_TREE);
16235 }
16236 else
16237 {
16238 /* The standard does not explicitly indicate whether a name that
16239 names a set of overloaded declarations, some of which are
16240 templates, is a template-name. However, such a name should
16241 be a template-name; otherwise, there is no way to form a
16242 template-id for the overloaded templates. */
16243 bool found = false;
16244
16245 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16246 !found && iter; ++iter)
16247 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16248 found = true;
16249
16250 if (!found)
16251 {
16252 /* The name does not name a template. */
16253 cp_parser_error (parser, "expected template-name");
16254 return error_mark_node;
16255 }
16256 }
16257
16258 /* If DECL is dependent, and refers to a function, then just return
16259 its name; we will look it up again during template instantiation. */
16260 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16261 {
16262 tree scope = ovl_scope (decl);
16263 if (TYPE_P (scope) && dependent_type_p (scope))
16264 return identifier;
16265 }
16266
16267 return decl;
16268 }
16269
16270 /* Parse a template-argument-list.
16271
16272 template-argument-list:
16273 template-argument ... [opt]
16274 template-argument-list , template-argument ... [opt]
16275
16276 Returns a TREE_VEC containing the arguments. */
16277
16278 static tree
16279 cp_parser_template_argument_list (cp_parser* parser)
16280 {
16281 tree fixed_args[10];
16282 unsigned n_args = 0;
16283 unsigned alloced = 10;
16284 tree *arg_ary = fixed_args;
16285 tree vec;
16286 bool saved_in_template_argument_list_p;
16287 bool saved_ice_p;
16288 bool saved_non_ice_p;
16289
16290 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16291 parser->in_template_argument_list_p = true;
16292 /* Even if the template-id appears in an integral
16293 constant-expression, the contents of the argument list do
16294 not. */
16295 saved_ice_p = parser->integral_constant_expression_p;
16296 parser->integral_constant_expression_p = false;
16297 saved_non_ice_p = parser->non_integral_constant_expression_p;
16298 parser->non_integral_constant_expression_p = false;
16299
16300 /* Parse the arguments. */
16301 do
16302 {
16303 tree argument;
16304
16305 if (n_args)
16306 /* Consume the comma. */
16307 cp_lexer_consume_token (parser->lexer);
16308
16309 /* Parse the template-argument. */
16310 argument = cp_parser_template_argument (parser);
16311
16312 /* If the next token is an ellipsis, we're expanding a template
16313 argument pack. */
16314 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16315 {
16316 if (argument == error_mark_node)
16317 {
16318 cp_token *token = cp_lexer_peek_token (parser->lexer);
16319 error_at (token->location,
16320 "expected parameter pack before %<...%>");
16321 }
16322 /* Consume the `...' token. */
16323 cp_lexer_consume_token (parser->lexer);
16324
16325 /* Make the argument into a TYPE_PACK_EXPANSION or
16326 EXPR_PACK_EXPANSION. */
16327 argument = make_pack_expansion (argument);
16328 }
16329
16330 if (n_args == alloced)
16331 {
16332 alloced *= 2;
16333
16334 if (arg_ary == fixed_args)
16335 {
16336 arg_ary = XNEWVEC (tree, alloced);
16337 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16338 }
16339 else
16340 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16341 }
16342 arg_ary[n_args++] = argument;
16343 }
16344 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16345
16346 vec = make_tree_vec (n_args);
16347
16348 while (n_args--)
16349 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16350
16351 if (arg_ary != fixed_args)
16352 free (arg_ary);
16353 parser->non_integral_constant_expression_p = saved_non_ice_p;
16354 parser->integral_constant_expression_p = saved_ice_p;
16355 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16356 if (CHECKING_P)
16357 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16358 return vec;
16359 }
16360
16361 /* Parse a template-argument.
16362
16363 template-argument:
16364 assignment-expression
16365 type-id
16366 id-expression
16367
16368 The representation is that of an assignment-expression, type-id, or
16369 id-expression -- except that the qualified id-expression is
16370 evaluated, so that the value returned is either a DECL or an
16371 OVERLOAD.
16372
16373 Although the standard says "assignment-expression", it forbids
16374 throw-expressions or assignments in the template argument.
16375 Therefore, we use "conditional-expression" instead. */
16376
16377 static tree
16378 cp_parser_template_argument (cp_parser* parser)
16379 {
16380 tree argument;
16381 bool template_p;
16382 bool address_p;
16383 bool maybe_type_id = false;
16384 cp_token *token = NULL, *argument_start_token = NULL;
16385 location_t loc = 0;
16386 cp_id_kind idk;
16387
16388 /* There's really no way to know what we're looking at, so we just
16389 try each alternative in order.
16390
16391 [temp.arg]
16392
16393 In a template-argument, an ambiguity between a type-id and an
16394 expression is resolved to a type-id, regardless of the form of
16395 the corresponding template-parameter.
16396
16397 Therefore, we try a type-id first. */
16398 cp_parser_parse_tentatively (parser);
16399 argument = cp_parser_template_type_arg (parser);
16400 /* If there was no error parsing the type-id but the next token is a
16401 '>>', our behavior depends on which dialect of C++ we're
16402 parsing. In C++98, we probably found a typo for '> >'. But there
16403 are type-id which are also valid expressions. For instance:
16404
16405 struct X { int operator >> (int); };
16406 template <int V> struct Foo {};
16407 Foo<X () >> 5> r;
16408
16409 Here 'X()' is a valid type-id of a function type, but the user just
16410 wanted to write the expression "X() >> 5". Thus, we remember that we
16411 found a valid type-id, but we still try to parse the argument as an
16412 expression to see what happens.
16413
16414 In C++0x, the '>>' will be considered two separate '>'
16415 tokens. */
16416 if (!cp_parser_error_occurred (parser)
16417 && cxx_dialect == cxx98
16418 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16419 {
16420 maybe_type_id = true;
16421 cp_parser_abort_tentative_parse (parser);
16422 }
16423 else
16424 {
16425 /* If the next token isn't a `,' or a `>', then this argument wasn't
16426 really finished. This means that the argument is not a valid
16427 type-id. */
16428 if (!cp_parser_next_token_ends_template_argument_p (parser))
16429 cp_parser_error (parser, "expected template-argument");
16430 /* If that worked, we're done. */
16431 if (cp_parser_parse_definitely (parser))
16432 return argument;
16433 }
16434 /* We're still not sure what the argument will be. */
16435 cp_parser_parse_tentatively (parser);
16436 /* Try a template. */
16437 argument_start_token = cp_lexer_peek_token (parser->lexer);
16438 argument = cp_parser_id_expression (parser,
16439 /*template_keyword_p=*/false,
16440 /*check_dependency_p=*/true,
16441 &template_p,
16442 /*declarator_p=*/false,
16443 /*optional_p=*/false);
16444 /* If the next token isn't a `,' or a `>', then this argument wasn't
16445 really finished. */
16446 if (!cp_parser_next_token_ends_template_argument_p (parser))
16447 cp_parser_error (parser, "expected template-argument");
16448 if (!cp_parser_error_occurred (parser))
16449 {
16450 /* Figure out what is being referred to. If the id-expression
16451 was for a class template specialization, then we will have a
16452 TYPE_DECL at this point. There is no need to do name lookup
16453 at this point in that case. */
16454 if (TREE_CODE (argument) != TYPE_DECL)
16455 argument = cp_parser_lookup_name (parser, argument,
16456 none_type,
16457 /*is_template=*/template_p,
16458 /*is_namespace=*/false,
16459 /*check_dependency=*/true,
16460 /*ambiguous_decls=*/NULL,
16461 argument_start_token->location);
16462 /* Handle a constrained-type-specifier for a non-type template
16463 parameter. */
16464 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16465 argument = decl;
16466 else if (TREE_CODE (argument) != TEMPLATE_DECL
16467 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16468 cp_parser_error (parser, "expected template-name");
16469 }
16470 if (cp_parser_parse_definitely (parser))
16471 {
16472 if (TREE_DEPRECATED (argument))
16473 warn_deprecated_use (argument, NULL_TREE);
16474 return argument;
16475 }
16476 /* It must be a non-type argument. In C++17 any constant-expression is
16477 allowed. */
16478 if (cxx_dialect > cxx14)
16479 goto general_expr;
16480
16481 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16482
16483 -- an integral constant-expression of integral or enumeration
16484 type; or
16485
16486 -- the name of a non-type template-parameter; or
16487
16488 -- the name of an object or function with external linkage...
16489
16490 -- the address of an object or function with external linkage...
16491
16492 -- a pointer to member... */
16493 /* Look for a non-type template parameter. */
16494 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16495 {
16496 cp_parser_parse_tentatively (parser);
16497 argument = cp_parser_primary_expression (parser,
16498 /*address_p=*/false,
16499 /*cast_p=*/false,
16500 /*template_arg_p=*/true,
16501 &idk);
16502 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16503 || !cp_parser_next_token_ends_template_argument_p (parser))
16504 cp_parser_simulate_error (parser);
16505 if (cp_parser_parse_definitely (parser))
16506 return argument;
16507 }
16508
16509 /* If the next token is "&", the argument must be the address of an
16510 object or function with external linkage. */
16511 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16512 if (address_p)
16513 {
16514 loc = cp_lexer_peek_token (parser->lexer)->location;
16515 cp_lexer_consume_token (parser->lexer);
16516 }
16517 /* See if we might have an id-expression. */
16518 token = cp_lexer_peek_token (parser->lexer);
16519 if (token->type == CPP_NAME
16520 || token->keyword == RID_OPERATOR
16521 || token->type == CPP_SCOPE
16522 || token->type == CPP_TEMPLATE_ID
16523 || token->type == CPP_NESTED_NAME_SPECIFIER)
16524 {
16525 cp_parser_parse_tentatively (parser);
16526 argument = cp_parser_primary_expression (parser,
16527 address_p,
16528 /*cast_p=*/false,
16529 /*template_arg_p=*/true,
16530 &idk);
16531 if (cp_parser_error_occurred (parser)
16532 || !cp_parser_next_token_ends_template_argument_p (parser))
16533 cp_parser_abort_tentative_parse (parser);
16534 else
16535 {
16536 tree probe;
16537
16538 if (INDIRECT_REF_P (argument))
16539 {
16540 /* Strip the dereference temporarily. */
16541 gcc_assert (REFERENCE_REF_P (argument));
16542 argument = TREE_OPERAND (argument, 0);
16543 }
16544
16545 /* If we're in a template, we represent a qualified-id referring
16546 to a static data member as a SCOPE_REF even if the scope isn't
16547 dependent so that we can check access control later. */
16548 probe = argument;
16549 if (TREE_CODE (probe) == SCOPE_REF)
16550 probe = TREE_OPERAND (probe, 1);
16551 if (VAR_P (probe))
16552 {
16553 /* A variable without external linkage might still be a
16554 valid constant-expression, so no error is issued here
16555 if the external-linkage check fails. */
16556 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16557 cp_parser_simulate_error (parser);
16558 }
16559 else if (is_overloaded_fn (argument))
16560 /* All overloaded functions are allowed; if the external
16561 linkage test does not pass, an error will be issued
16562 later. */
16563 ;
16564 else if (address_p
16565 && (TREE_CODE (argument) == OFFSET_REF
16566 || TREE_CODE (argument) == SCOPE_REF))
16567 /* A pointer-to-member. */
16568 ;
16569 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16570 ;
16571 else
16572 cp_parser_simulate_error (parser);
16573
16574 if (cp_parser_parse_definitely (parser))
16575 {
16576 if (address_p)
16577 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16578 tf_warning_or_error);
16579 else
16580 argument = convert_from_reference (argument);
16581 return argument;
16582 }
16583 }
16584 }
16585 /* If the argument started with "&", there are no other valid
16586 alternatives at this point. */
16587 if (address_p)
16588 {
16589 cp_parser_error (parser, "invalid non-type template argument");
16590 return error_mark_node;
16591 }
16592
16593 general_expr:
16594 /* If the argument wasn't successfully parsed as a type-id followed
16595 by '>>', the argument can only be a constant expression now.
16596 Otherwise, we try parsing the constant-expression tentatively,
16597 because the argument could really be a type-id. */
16598 if (maybe_type_id)
16599 cp_parser_parse_tentatively (parser);
16600
16601 if (cxx_dialect <= cxx14)
16602 argument = cp_parser_constant_expression (parser);
16603 else
16604 {
16605 /* With C++17 generalized non-type template arguments we need to handle
16606 lvalue constant expressions, too. */
16607 argument = cp_parser_assignment_expression (parser);
16608 require_potential_constant_expression (argument);
16609 }
16610
16611 if (!maybe_type_id)
16612 return argument;
16613 if (!cp_parser_next_token_ends_template_argument_p (parser))
16614 cp_parser_error (parser, "expected template-argument");
16615 if (cp_parser_parse_definitely (parser))
16616 return argument;
16617 /* We did our best to parse the argument as a non type-id, but that
16618 was the only alternative that matched (albeit with a '>' after
16619 it). We can assume it's just a typo from the user, and a
16620 diagnostic will then be issued. */
16621 return cp_parser_template_type_arg (parser);
16622 }
16623
16624 /* Parse an explicit-instantiation.
16625
16626 explicit-instantiation:
16627 template declaration
16628
16629 Although the standard says `declaration', what it really means is:
16630
16631 explicit-instantiation:
16632 template decl-specifier-seq [opt] declarator [opt] ;
16633
16634 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16635 supposed to be allowed. A defect report has been filed about this
16636 issue.
16637
16638 GNU Extension:
16639
16640 explicit-instantiation:
16641 storage-class-specifier template
16642 decl-specifier-seq [opt] declarator [opt] ;
16643 function-specifier template
16644 decl-specifier-seq [opt] declarator [opt] ; */
16645
16646 static void
16647 cp_parser_explicit_instantiation (cp_parser* parser)
16648 {
16649 int declares_class_or_enum;
16650 cp_decl_specifier_seq decl_specifiers;
16651 tree extension_specifier = NULL_TREE;
16652
16653 timevar_push (TV_TEMPLATE_INST);
16654
16655 /* Look for an (optional) storage-class-specifier or
16656 function-specifier. */
16657 if (cp_parser_allow_gnu_extensions_p (parser))
16658 {
16659 extension_specifier
16660 = cp_parser_storage_class_specifier_opt (parser);
16661 if (!extension_specifier)
16662 extension_specifier
16663 = cp_parser_function_specifier_opt (parser,
16664 /*decl_specs=*/NULL);
16665 }
16666
16667 /* Look for the `template' keyword. */
16668 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16669 /* Let the front end know that we are processing an explicit
16670 instantiation. */
16671 begin_explicit_instantiation ();
16672 /* [temp.explicit] says that we are supposed to ignore access
16673 control while processing explicit instantiation directives. */
16674 push_deferring_access_checks (dk_no_check);
16675 /* Parse a decl-specifier-seq. */
16676 cp_parser_decl_specifier_seq (parser,
16677 CP_PARSER_FLAGS_OPTIONAL,
16678 &decl_specifiers,
16679 &declares_class_or_enum);
16680 /* If there was exactly one decl-specifier, and it declared a class,
16681 and there's no declarator, then we have an explicit type
16682 instantiation. */
16683 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16684 {
16685 tree type;
16686
16687 type = check_tag_decl (&decl_specifiers,
16688 /*explicit_type_instantiation_p=*/true);
16689 /* Turn access control back on for names used during
16690 template instantiation. */
16691 pop_deferring_access_checks ();
16692 if (type)
16693 do_type_instantiation (type, extension_specifier,
16694 /*complain=*/tf_error);
16695 }
16696 else
16697 {
16698 cp_declarator *declarator;
16699 tree decl;
16700
16701 /* Parse the declarator. */
16702 declarator
16703 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16704 /*ctor_dtor_or_conv_p=*/NULL,
16705 /*parenthesized_p=*/NULL,
16706 /*member_p=*/false,
16707 /*friend_p=*/false);
16708 if (declares_class_or_enum & 2)
16709 cp_parser_check_for_definition_in_return_type (declarator,
16710 decl_specifiers.type,
16711 decl_specifiers.locations[ds_type_spec]);
16712 if (declarator != cp_error_declarator)
16713 {
16714 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16715 permerror (decl_specifiers.locations[ds_inline],
16716 "explicit instantiation shall not use"
16717 " %<inline%> specifier");
16718 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16719 permerror (decl_specifiers.locations[ds_constexpr],
16720 "explicit instantiation shall not use"
16721 " %<constexpr%> specifier");
16722
16723 decl = grokdeclarator (declarator, &decl_specifiers,
16724 NORMAL, 0, &decl_specifiers.attributes);
16725 /* Turn access control back on for names used during
16726 template instantiation. */
16727 pop_deferring_access_checks ();
16728 /* Do the explicit instantiation. */
16729 do_decl_instantiation (decl, extension_specifier);
16730 }
16731 else
16732 {
16733 pop_deferring_access_checks ();
16734 /* Skip the body of the explicit instantiation. */
16735 cp_parser_skip_to_end_of_statement (parser);
16736 }
16737 }
16738 /* We're done with the instantiation. */
16739 end_explicit_instantiation ();
16740
16741 cp_parser_consume_semicolon_at_end_of_statement (parser);
16742
16743 timevar_pop (TV_TEMPLATE_INST);
16744 }
16745
16746 /* Parse an explicit-specialization.
16747
16748 explicit-specialization:
16749 template < > declaration
16750
16751 Although the standard says `declaration', what it really means is:
16752
16753 explicit-specialization:
16754 template <> decl-specifier [opt] init-declarator [opt] ;
16755 template <> function-definition
16756 template <> explicit-specialization
16757 template <> template-declaration */
16758
16759 static void
16760 cp_parser_explicit_specialization (cp_parser* parser)
16761 {
16762 bool need_lang_pop;
16763 cp_token *token = cp_lexer_peek_token (parser->lexer);
16764
16765 /* Look for the `template' keyword. */
16766 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16767 /* Look for the `<'. */
16768 cp_parser_require (parser, CPP_LESS, RT_LESS);
16769 /* Look for the `>'. */
16770 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16771 /* We have processed another parameter list. */
16772 ++parser->num_template_parameter_lists;
16773 /* [temp]
16774
16775 A template ... explicit specialization ... shall not have C
16776 linkage. */
16777 if (current_lang_name == lang_name_c)
16778 {
16779 error_at (token->location, "template specialization with C linkage");
16780 maybe_show_extern_c_location ();
16781 /* Give it C++ linkage to avoid confusing other parts of the
16782 front end. */
16783 push_lang_context (lang_name_cplusplus);
16784 need_lang_pop = true;
16785 }
16786 else
16787 need_lang_pop = false;
16788 /* Let the front end know that we are beginning a specialization. */
16789 if (!begin_specialization ())
16790 {
16791 end_specialization ();
16792 return;
16793 }
16794
16795 /* If the next keyword is `template', we need to figure out whether
16796 or not we're looking a template-declaration. */
16797 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16798 {
16799 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16800 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16801 cp_parser_template_declaration_after_export (parser,
16802 /*member_p=*/false);
16803 else
16804 cp_parser_explicit_specialization (parser);
16805 }
16806 else
16807 /* Parse the dependent declaration. */
16808 cp_parser_single_declaration (parser,
16809 /*checks=*/NULL,
16810 /*member_p=*/false,
16811 /*explicit_specialization_p=*/true,
16812 /*friend_p=*/NULL);
16813 /* We're done with the specialization. */
16814 end_specialization ();
16815 /* For the erroneous case of a template with C linkage, we pushed an
16816 implicit C++ linkage scope; exit that scope now. */
16817 if (need_lang_pop)
16818 pop_lang_context ();
16819 /* We're done with this parameter list. */
16820 --parser->num_template_parameter_lists;
16821 }
16822
16823 /* Parse a type-specifier.
16824
16825 type-specifier:
16826 simple-type-specifier
16827 class-specifier
16828 enum-specifier
16829 elaborated-type-specifier
16830 cv-qualifier
16831
16832 GNU Extension:
16833
16834 type-specifier:
16835 __complex__
16836
16837 Returns a representation of the type-specifier. For a
16838 class-specifier, enum-specifier, or elaborated-type-specifier, a
16839 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16840
16841 The parser flags FLAGS is used to control type-specifier parsing.
16842
16843 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16844 in a decl-specifier-seq.
16845
16846 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16847 class-specifier, enum-specifier, or elaborated-type-specifier, then
16848 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16849 if a type is declared; 2 if it is defined. Otherwise, it is set to
16850 zero.
16851
16852 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16853 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16854 is set to FALSE. */
16855
16856 static tree
16857 cp_parser_type_specifier (cp_parser* parser,
16858 cp_parser_flags flags,
16859 cp_decl_specifier_seq *decl_specs,
16860 bool is_declaration,
16861 int* declares_class_or_enum,
16862 bool* is_cv_qualifier)
16863 {
16864 tree type_spec = NULL_TREE;
16865 cp_token *token;
16866 enum rid keyword;
16867 cp_decl_spec ds = ds_last;
16868
16869 /* Assume this type-specifier does not declare a new type. */
16870 if (declares_class_or_enum)
16871 *declares_class_or_enum = 0;
16872 /* And that it does not specify a cv-qualifier. */
16873 if (is_cv_qualifier)
16874 *is_cv_qualifier = false;
16875 /* Peek at the next token. */
16876 token = cp_lexer_peek_token (parser->lexer);
16877
16878 /* If we're looking at a keyword, we can use that to guide the
16879 production we choose. */
16880 keyword = token->keyword;
16881 switch (keyword)
16882 {
16883 case RID_ENUM:
16884 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16885 goto elaborated_type_specifier;
16886
16887 /* Look for the enum-specifier. */
16888 type_spec = cp_parser_enum_specifier (parser);
16889 /* If that worked, we're done. */
16890 if (type_spec)
16891 {
16892 if (declares_class_or_enum)
16893 *declares_class_or_enum = 2;
16894 if (decl_specs)
16895 cp_parser_set_decl_spec_type (decl_specs,
16896 type_spec,
16897 token,
16898 /*type_definition_p=*/true);
16899 return type_spec;
16900 }
16901 else
16902 goto elaborated_type_specifier;
16903
16904 /* Any of these indicate either a class-specifier, or an
16905 elaborated-type-specifier. */
16906 case RID_CLASS:
16907 case RID_STRUCT:
16908 case RID_UNION:
16909 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16910 goto elaborated_type_specifier;
16911
16912 /* Parse tentatively so that we can back up if we don't find a
16913 class-specifier. */
16914 cp_parser_parse_tentatively (parser);
16915 /* Look for the class-specifier. */
16916 type_spec = cp_parser_class_specifier (parser);
16917 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16918 /* If that worked, we're done. */
16919 if (cp_parser_parse_definitely (parser))
16920 {
16921 if (declares_class_or_enum)
16922 *declares_class_or_enum = 2;
16923 if (decl_specs)
16924 cp_parser_set_decl_spec_type (decl_specs,
16925 type_spec,
16926 token,
16927 /*type_definition_p=*/true);
16928 return type_spec;
16929 }
16930
16931 /* Fall through. */
16932 elaborated_type_specifier:
16933 /* We're declaring (not defining) a class or enum. */
16934 if (declares_class_or_enum)
16935 *declares_class_or_enum = 1;
16936
16937 /* Fall through. */
16938 case RID_TYPENAME:
16939 /* Look for an elaborated-type-specifier. */
16940 type_spec
16941 = (cp_parser_elaborated_type_specifier
16942 (parser,
16943 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16944 is_declaration));
16945 if (decl_specs)
16946 cp_parser_set_decl_spec_type (decl_specs,
16947 type_spec,
16948 token,
16949 /*type_definition_p=*/false);
16950 return type_spec;
16951
16952 case RID_CONST:
16953 ds = ds_const;
16954 if (is_cv_qualifier)
16955 *is_cv_qualifier = true;
16956 break;
16957
16958 case RID_VOLATILE:
16959 ds = ds_volatile;
16960 if (is_cv_qualifier)
16961 *is_cv_qualifier = true;
16962 break;
16963
16964 case RID_RESTRICT:
16965 ds = ds_restrict;
16966 if (is_cv_qualifier)
16967 *is_cv_qualifier = true;
16968 break;
16969
16970 case RID_COMPLEX:
16971 /* The `__complex__' keyword is a GNU extension. */
16972 ds = ds_complex;
16973 break;
16974
16975 default:
16976 break;
16977 }
16978
16979 /* Handle simple keywords. */
16980 if (ds != ds_last)
16981 {
16982 if (decl_specs)
16983 {
16984 set_and_check_decl_spec_loc (decl_specs, ds, token);
16985 decl_specs->any_specifiers_p = true;
16986 }
16987 return cp_lexer_consume_token (parser->lexer)->u.value;
16988 }
16989
16990 /* If we do not already have a type-specifier, assume we are looking
16991 at a simple-type-specifier. */
16992 type_spec = cp_parser_simple_type_specifier (parser,
16993 decl_specs,
16994 flags);
16995
16996 /* If we didn't find a type-specifier, and a type-specifier was not
16997 optional in this context, issue an error message. */
16998 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16999 {
17000 cp_parser_error (parser, "expected type specifier");
17001 return error_mark_node;
17002 }
17003
17004 return type_spec;
17005 }
17006
17007 /* Parse a simple-type-specifier.
17008
17009 simple-type-specifier:
17010 :: [opt] nested-name-specifier [opt] type-name
17011 :: [opt] nested-name-specifier template template-id
17012 char
17013 wchar_t
17014 bool
17015 short
17016 int
17017 long
17018 signed
17019 unsigned
17020 float
17021 double
17022 void
17023
17024 C++11 Extension:
17025
17026 simple-type-specifier:
17027 auto
17028 decltype ( expression )
17029 char16_t
17030 char32_t
17031 __underlying_type ( type-id )
17032
17033 C++17 extension:
17034
17035 nested-name-specifier(opt) template-name
17036
17037 GNU Extension:
17038
17039 simple-type-specifier:
17040 __int128
17041 __typeof__ unary-expression
17042 __typeof__ ( type-id )
17043 __typeof__ ( type-id ) { initializer-list , [opt] }
17044
17045 Concepts Extension:
17046
17047 simple-type-specifier:
17048 constrained-type-specifier
17049
17050 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17051 appropriately updated. */
17052
17053 static tree
17054 cp_parser_simple_type_specifier (cp_parser* parser,
17055 cp_decl_specifier_seq *decl_specs,
17056 cp_parser_flags flags)
17057 {
17058 tree type = NULL_TREE;
17059 cp_token *token;
17060 int idx;
17061
17062 /* Peek at the next token. */
17063 token = cp_lexer_peek_token (parser->lexer);
17064
17065 /* If we're looking at a keyword, things are easy. */
17066 switch (token->keyword)
17067 {
17068 case RID_CHAR:
17069 if (decl_specs)
17070 decl_specs->explicit_char_p = true;
17071 type = char_type_node;
17072 break;
17073 case RID_CHAR16:
17074 type = char16_type_node;
17075 break;
17076 case RID_CHAR32:
17077 type = char32_type_node;
17078 break;
17079 case RID_WCHAR:
17080 type = wchar_type_node;
17081 break;
17082 case RID_BOOL:
17083 type = boolean_type_node;
17084 break;
17085 case RID_SHORT:
17086 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17087 type = short_integer_type_node;
17088 break;
17089 case RID_INT:
17090 if (decl_specs)
17091 decl_specs->explicit_int_p = true;
17092 type = integer_type_node;
17093 break;
17094 case RID_INT_N_0:
17095 case RID_INT_N_1:
17096 case RID_INT_N_2:
17097 case RID_INT_N_3:
17098 idx = token->keyword - RID_INT_N_0;
17099 if (! int_n_enabled_p [idx])
17100 break;
17101 if (decl_specs)
17102 {
17103 decl_specs->explicit_intN_p = true;
17104 decl_specs->int_n_idx = idx;
17105 }
17106 type = int_n_trees [idx].signed_type;
17107 break;
17108 case RID_LONG:
17109 if (decl_specs)
17110 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17111 type = long_integer_type_node;
17112 break;
17113 case RID_SIGNED:
17114 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17115 type = integer_type_node;
17116 break;
17117 case RID_UNSIGNED:
17118 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17119 type = unsigned_type_node;
17120 break;
17121 case RID_FLOAT:
17122 type = float_type_node;
17123 break;
17124 case RID_DOUBLE:
17125 type = double_type_node;
17126 break;
17127 case RID_VOID:
17128 type = void_type_node;
17129 break;
17130
17131 case RID_AUTO:
17132 maybe_warn_cpp0x (CPP0X_AUTO);
17133 if (parser->auto_is_implicit_function_template_parm_p)
17134 {
17135 /* The 'auto' might be the placeholder return type for a function decl
17136 with trailing return type. */
17137 bool have_trailing_return_fn_decl = false;
17138
17139 cp_parser_parse_tentatively (parser);
17140 cp_lexer_consume_token (parser->lexer);
17141 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17142 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17143 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17144 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17145 {
17146 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17147 {
17148 cp_lexer_consume_token (parser->lexer);
17149 cp_parser_skip_to_closing_parenthesis (parser,
17150 /*recovering*/false,
17151 /*or_comma*/false,
17152 /*consume_paren*/true);
17153 continue;
17154 }
17155
17156 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17157 {
17158 have_trailing_return_fn_decl = true;
17159 break;
17160 }
17161
17162 cp_lexer_consume_token (parser->lexer);
17163 }
17164 cp_parser_abort_tentative_parse (parser);
17165
17166 if (have_trailing_return_fn_decl)
17167 {
17168 type = make_auto ();
17169 break;
17170 }
17171
17172 if (cxx_dialect >= cxx14)
17173 {
17174 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17175 type = TREE_TYPE (type);
17176 }
17177 else
17178 type = error_mark_node;
17179
17180 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17181 {
17182 if (cxx_dialect < cxx14)
17183 error_at (token->location,
17184 "use of %<auto%> in lambda parameter declaration "
17185 "only available with "
17186 "-std=c++14 or -std=gnu++14");
17187 }
17188 else if (cxx_dialect < cxx14)
17189 error_at (token->location,
17190 "use of %<auto%> in parameter declaration "
17191 "only available with "
17192 "-std=c++14 or -std=gnu++14");
17193 else if (!flag_concepts)
17194 pedwarn (token->location, 0,
17195 "use of %<auto%> in parameter declaration "
17196 "only available with -fconcepts");
17197 }
17198 else
17199 type = make_auto ();
17200 break;
17201
17202 case RID_DECLTYPE:
17203 /* Since DR 743, decltype can either be a simple-type-specifier by
17204 itself or begin a nested-name-specifier. Parsing it will replace
17205 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17206 handling below decide what to do. */
17207 cp_parser_decltype (parser);
17208 cp_lexer_set_token_position (parser->lexer, token);
17209 break;
17210
17211 case RID_TYPEOF:
17212 /* Consume the `typeof' token. */
17213 cp_lexer_consume_token (parser->lexer);
17214 /* Parse the operand to `typeof'. */
17215 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17216 /* If it is not already a TYPE, take its type. */
17217 if (!TYPE_P (type))
17218 type = finish_typeof (type);
17219
17220 if (decl_specs)
17221 cp_parser_set_decl_spec_type (decl_specs, type,
17222 token,
17223 /*type_definition_p=*/false);
17224
17225 return type;
17226
17227 case RID_UNDERLYING_TYPE:
17228 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17229 if (decl_specs)
17230 cp_parser_set_decl_spec_type (decl_specs, type,
17231 token,
17232 /*type_definition_p=*/false);
17233
17234 return type;
17235
17236 case RID_BASES:
17237 case RID_DIRECT_BASES:
17238 type = cp_parser_trait_expr (parser, token->keyword);
17239 if (decl_specs)
17240 cp_parser_set_decl_spec_type (decl_specs, type,
17241 token,
17242 /*type_definition_p=*/false);
17243 return type;
17244 default:
17245 break;
17246 }
17247
17248 /* If token is an already-parsed decltype not followed by ::,
17249 it's a simple-type-specifier. */
17250 if (token->type == CPP_DECLTYPE
17251 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17252 {
17253 type = saved_checks_value (token->u.tree_check_value);
17254 if (decl_specs)
17255 {
17256 cp_parser_set_decl_spec_type (decl_specs, type,
17257 token,
17258 /*type_definition_p=*/false);
17259 /* Remember that we are handling a decltype in order to
17260 implement the resolution of DR 1510 when the argument
17261 isn't instantiation dependent. */
17262 decl_specs->decltype_p = true;
17263 }
17264 cp_lexer_consume_token (parser->lexer);
17265 return type;
17266 }
17267
17268 /* If the type-specifier was for a built-in type, we're done. */
17269 if (type)
17270 {
17271 /* Record the type. */
17272 if (decl_specs
17273 && (token->keyword != RID_SIGNED
17274 && token->keyword != RID_UNSIGNED
17275 && token->keyword != RID_SHORT
17276 && token->keyword != RID_LONG))
17277 cp_parser_set_decl_spec_type (decl_specs,
17278 type,
17279 token,
17280 /*type_definition_p=*/false);
17281 if (decl_specs)
17282 decl_specs->any_specifiers_p = true;
17283
17284 /* Consume the token. */
17285 cp_lexer_consume_token (parser->lexer);
17286
17287 if (type == error_mark_node)
17288 return error_mark_node;
17289
17290 /* There is no valid C++ program where a non-template type is
17291 followed by a "<". That usually indicates that the user thought
17292 that the type was a template. */
17293 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17294 token->location);
17295
17296 return TYPE_NAME (type);
17297 }
17298
17299 /* The type-specifier must be a user-defined type. */
17300 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17301 {
17302 bool qualified_p;
17303 bool global_p;
17304
17305 /* Don't gobble tokens or issue error messages if this is an
17306 optional type-specifier. */
17307 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17308 cp_parser_parse_tentatively (parser);
17309
17310 token = cp_lexer_peek_token (parser->lexer);
17311
17312 /* Look for the optional `::' operator. */
17313 global_p
17314 = (cp_parser_global_scope_opt (parser,
17315 /*current_scope_valid_p=*/false)
17316 != NULL_TREE);
17317 /* Look for the nested-name specifier. */
17318 qualified_p
17319 = (cp_parser_nested_name_specifier_opt (parser,
17320 /*typename_keyword_p=*/false,
17321 /*check_dependency_p=*/true,
17322 /*type_p=*/false,
17323 /*is_declaration=*/false)
17324 != NULL_TREE);
17325 /* If we have seen a nested-name-specifier, and the next token
17326 is `template', then we are using the template-id production. */
17327 if (parser->scope
17328 && cp_parser_optional_template_keyword (parser))
17329 {
17330 /* Look for the template-id. */
17331 type = cp_parser_template_id (parser,
17332 /*template_keyword_p=*/true,
17333 /*check_dependency_p=*/true,
17334 none_type,
17335 /*is_declaration=*/false);
17336 /* If the template-id did not name a type, we are out of
17337 luck. */
17338 if (TREE_CODE (type) != TYPE_DECL)
17339 {
17340 cp_parser_error (parser, "expected template-id for type");
17341 type = NULL_TREE;
17342 }
17343 }
17344 /* Otherwise, look for a type-name. */
17345 else
17346 type = cp_parser_type_name (parser);
17347 /* Keep track of all name-lookups performed in class scopes. */
17348 if (type
17349 && !global_p
17350 && !qualified_p
17351 && TREE_CODE (type) == TYPE_DECL
17352 && identifier_p (DECL_NAME (type)))
17353 maybe_note_name_used_in_class (DECL_NAME (type), type);
17354 /* If it didn't work out, we don't have a TYPE. */
17355 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17356 && !cp_parser_parse_definitely (parser))
17357 type = NULL_TREE;
17358 if (!type && cxx_dialect >= cxx17)
17359 {
17360 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17361 cp_parser_parse_tentatively (parser);
17362
17363 cp_parser_global_scope_opt (parser,
17364 /*current_scope_valid_p=*/false);
17365 cp_parser_nested_name_specifier_opt (parser,
17366 /*typename_keyword_p=*/false,
17367 /*check_dependency_p=*/true,
17368 /*type_p=*/false,
17369 /*is_declaration=*/false);
17370 tree name = cp_parser_identifier (parser);
17371 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17372 && parser->scope != error_mark_node)
17373 {
17374 tree tmpl = cp_parser_lookup_name (parser, name,
17375 none_type,
17376 /*is_template=*/false,
17377 /*is_namespace=*/false,
17378 /*check_dependency=*/true,
17379 /*ambiguous_decls=*/NULL,
17380 token->location);
17381 if (tmpl && tmpl != error_mark_node
17382 && (DECL_CLASS_TEMPLATE_P (tmpl)
17383 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17384 type = make_template_placeholder (tmpl);
17385 else
17386 {
17387 type = error_mark_node;
17388 if (!cp_parser_simulate_error (parser))
17389 cp_parser_name_lookup_error (parser, name, tmpl,
17390 NLE_TYPE, token->location);
17391 }
17392 }
17393 else
17394 type = error_mark_node;
17395
17396 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17397 && !cp_parser_parse_definitely (parser))
17398 type = NULL_TREE;
17399 }
17400 if (type && decl_specs)
17401 cp_parser_set_decl_spec_type (decl_specs, type,
17402 token,
17403 /*type_definition_p=*/false);
17404 }
17405
17406 /* If we didn't get a type-name, issue an error message. */
17407 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17408 {
17409 cp_parser_error (parser, "expected type-name");
17410 return error_mark_node;
17411 }
17412
17413 if (type && type != error_mark_node)
17414 {
17415 /* See if TYPE is an Objective-C type, and if so, parse and
17416 accept any protocol references following it. Do this before
17417 the cp_parser_check_for_invalid_template_id() call, because
17418 Objective-C types can be followed by '<...>' which would
17419 enclose protocol names rather than template arguments, and so
17420 everything is fine. */
17421 if (c_dialect_objc () && !parser->scope
17422 && (objc_is_id (type) || objc_is_class_name (type)))
17423 {
17424 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17425 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17426
17427 /* Clobber the "unqualified" type previously entered into
17428 DECL_SPECS with the new, improved protocol-qualified version. */
17429 if (decl_specs)
17430 decl_specs->type = qual_type;
17431
17432 return qual_type;
17433 }
17434
17435 /* There is no valid C++ program where a non-template type is
17436 followed by a "<". That usually indicates that the user
17437 thought that the type was a template. */
17438 cp_parser_check_for_invalid_template_id (parser, type,
17439 none_type,
17440 token->location);
17441 }
17442
17443 return type;
17444 }
17445
17446 /* Parse a type-name.
17447
17448 type-name:
17449 class-name
17450 enum-name
17451 typedef-name
17452 simple-template-id [in c++0x]
17453
17454 enum-name:
17455 identifier
17456
17457 typedef-name:
17458 identifier
17459
17460 Concepts:
17461
17462 type-name:
17463 concept-name
17464 partial-concept-id
17465
17466 concept-name:
17467 identifier
17468
17469 Returns a TYPE_DECL for the type. */
17470
17471 static tree
17472 cp_parser_type_name (cp_parser* parser)
17473 {
17474 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17475 }
17476
17477 /* See above. */
17478 static tree
17479 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17480 {
17481 tree type_decl;
17482
17483 /* We can't know yet whether it is a class-name or not. */
17484 cp_parser_parse_tentatively (parser);
17485 /* Try a class-name. */
17486 type_decl = cp_parser_class_name (parser,
17487 typename_keyword_p,
17488 /*template_keyword_p=*/false,
17489 none_type,
17490 /*check_dependency_p=*/true,
17491 /*class_head_p=*/false,
17492 /*is_declaration=*/false);
17493 /* If it's not a class-name, keep looking. */
17494 if (!cp_parser_parse_definitely (parser))
17495 {
17496 if (cxx_dialect < cxx11)
17497 /* It must be a typedef-name or an enum-name. */
17498 return cp_parser_nonclass_name (parser);
17499
17500 cp_parser_parse_tentatively (parser);
17501 /* It is either a simple-template-id representing an
17502 instantiation of an alias template... */
17503 type_decl = cp_parser_template_id (parser,
17504 /*template_keyword_p=*/false,
17505 /*check_dependency_p=*/true,
17506 none_type,
17507 /*is_declaration=*/false);
17508 /* Note that this must be an instantiation of an alias template
17509 because [temp.names]/6 says:
17510
17511 A template-id that names an alias template specialization
17512 is a type-name.
17513
17514 Whereas [temp.names]/7 says:
17515
17516 A simple-template-id that names a class template
17517 specialization is a class-name.
17518
17519 With concepts, this could also be a partial-concept-id that
17520 declares a non-type template parameter. */
17521 if (type_decl != NULL_TREE
17522 && TREE_CODE (type_decl) == TYPE_DECL
17523 && TYPE_DECL_ALIAS_P (type_decl))
17524 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17525 else if (is_constrained_parameter (type_decl))
17526 /* Don't do anything. */ ;
17527 else
17528 cp_parser_simulate_error (parser);
17529
17530 if (!cp_parser_parse_definitely (parser))
17531 /* ... Or a typedef-name or an enum-name. */
17532 return cp_parser_nonclass_name (parser);
17533 }
17534
17535 return type_decl;
17536 }
17537
17538 /* Check if DECL and ARGS can form a constrained-type-specifier.
17539 If ARGS is non-null, we try to form a concept check of the
17540 form DECL<?, ARGS> where ? is a wildcard that matches any
17541 kind of template argument. If ARGS is NULL, then we try to
17542 form a concept check of the form DECL<?>. */
17543
17544 static tree
17545 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17546 tree decl, tree args)
17547 {
17548 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17549
17550 /* If we a constrained-type-specifier cannot be deduced. */
17551 if (parser->prevent_constrained_type_specifiers)
17552 return NULL_TREE;
17553
17554 /* A constrained type specifier can only be found in an
17555 overload set or as a reference to a template declaration.
17556
17557 FIXME: This might be masking a bug. It's possible that
17558 that the deduction below is causing template specializations
17559 to be formed with the wildcard as an argument. */
17560 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17561 return NULL_TREE;
17562
17563 /* Try to build a call expression that evaluates the
17564 concept. This can fail if the overload set refers
17565 only to non-templates. */
17566 tree placeholder = build_nt (WILDCARD_DECL);
17567 tree check = build_concept_check (decl, placeholder, args);
17568 if (check == error_mark_node)
17569 return NULL_TREE;
17570
17571 /* Deduce the checked constraint and the prototype parameter.
17572
17573 FIXME: In certain cases, failure to deduce should be a
17574 diagnosable error. */
17575 tree conc;
17576 tree proto;
17577 if (!deduce_constrained_parameter (check, conc, proto))
17578 return NULL_TREE;
17579
17580 /* In template parameter scope, this results in a constrained
17581 parameter. Return a descriptor of that parm. */
17582 if (processing_template_parmlist)
17583 return build_constrained_parameter (conc, proto, args);
17584
17585 /* In a parameter-declaration-clause, constrained-type
17586 specifiers result in invented template parameters. */
17587 if (parser->auto_is_implicit_function_template_parm_p)
17588 {
17589 tree x = build_constrained_parameter (conc, proto, args);
17590 return synthesize_implicit_template_parm (parser, x);
17591 }
17592 else
17593 {
17594 /* Otherwise, we're in a context where the constrained
17595 type name is deduced and the constraint applies
17596 after deduction. */
17597 return make_constrained_auto (conc, args);
17598 }
17599
17600 return NULL_TREE;
17601 }
17602
17603 /* If DECL refers to a concept, return a TYPE_DECL representing
17604 the result of using the constrained type specifier in the
17605 current context. DECL refers to a concept if
17606
17607 - it is an overload set containing a function concept taking a single
17608 type argument, or
17609
17610 - it is a variable concept taking a single type argument. */
17611
17612 static tree
17613 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17614 {
17615 if (flag_concepts
17616 && (TREE_CODE (decl) == OVERLOAD
17617 || BASELINK_P (decl)
17618 || variable_concept_p (decl)))
17619 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17620 else
17621 return NULL_TREE;
17622 }
17623
17624 /* Check if DECL and ARGS form a partial-concept-id. If so,
17625 assign ID to the resulting constrained placeholder.
17626
17627 Returns true if the partial-concept-id designates a placeholder
17628 and false otherwise. Note that *id is set to NULL_TREE in
17629 this case. */
17630
17631 static tree
17632 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17633 {
17634 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17635 }
17636
17637 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17638 or a concept-name.
17639
17640 enum-name:
17641 identifier
17642
17643 typedef-name:
17644 identifier
17645
17646 concept-name:
17647 identifier
17648
17649 Returns a TYPE_DECL for the type. */
17650
17651 static tree
17652 cp_parser_nonclass_name (cp_parser* parser)
17653 {
17654 tree type_decl;
17655 tree identifier;
17656
17657 cp_token *token = cp_lexer_peek_token (parser->lexer);
17658 identifier = cp_parser_identifier (parser);
17659 if (identifier == error_mark_node)
17660 return error_mark_node;
17661
17662 /* Look up the type-name. */
17663 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17664
17665 type_decl = strip_using_decl (type_decl);
17666
17667 /* If we found an overload set, then it may refer to a concept-name. */
17668 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17669 type_decl = decl;
17670
17671 if (TREE_CODE (type_decl) != TYPE_DECL
17672 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17673 {
17674 /* See if this is an Objective-C type. */
17675 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17676 tree type = objc_get_protocol_qualified_type (identifier, protos);
17677 if (type)
17678 type_decl = TYPE_NAME (type);
17679 }
17680
17681 /* Issue an error if we did not find a type-name. */
17682 if (TREE_CODE (type_decl) != TYPE_DECL
17683 /* In Objective-C, we have the complication that class names are
17684 normally type names and start declarations (eg, the
17685 "NSObject" in "NSObject *object;"), but can be used in an
17686 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17687 is an expression. So, a classname followed by a dot is not a
17688 valid type-name. */
17689 || (objc_is_class_name (TREE_TYPE (type_decl))
17690 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17691 {
17692 if (!cp_parser_simulate_error (parser))
17693 cp_parser_name_lookup_error (parser, identifier, type_decl,
17694 NLE_TYPE, token->location);
17695 return error_mark_node;
17696 }
17697 /* Remember that the name was used in the definition of the
17698 current class so that we can check later to see if the
17699 meaning would have been different after the class was
17700 entirely defined. */
17701 else if (type_decl != error_mark_node
17702 && !parser->scope)
17703 maybe_note_name_used_in_class (identifier, type_decl);
17704
17705 return type_decl;
17706 }
17707
17708 /* Parse an elaborated-type-specifier. Note that the grammar given
17709 here incorporates the resolution to DR68.
17710
17711 elaborated-type-specifier:
17712 class-key :: [opt] nested-name-specifier [opt] identifier
17713 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17714 enum-key :: [opt] nested-name-specifier [opt] identifier
17715 typename :: [opt] nested-name-specifier identifier
17716 typename :: [opt] nested-name-specifier template [opt]
17717 template-id
17718
17719 GNU extension:
17720
17721 elaborated-type-specifier:
17722 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17723 class-key attributes :: [opt] nested-name-specifier [opt]
17724 template [opt] template-id
17725 enum attributes :: [opt] nested-name-specifier [opt] identifier
17726
17727 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17728 declared `friend'. If IS_DECLARATION is TRUE, then this
17729 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17730 something is being declared.
17731
17732 Returns the TYPE specified. */
17733
17734 static tree
17735 cp_parser_elaborated_type_specifier (cp_parser* parser,
17736 bool is_friend,
17737 bool is_declaration)
17738 {
17739 enum tag_types tag_type;
17740 tree identifier;
17741 tree type = NULL_TREE;
17742 tree attributes = NULL_TREE;
17743 tree globalscope;
17744 cp_token *token = NULL;
17745
17746 /* See if we're looking at the `enum' keyword. */
17747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17748 {
17749 /* Consume the `enum' token. */
17750 cp_lexer_consume_token (parser->lexer);
17751 /* Remember that it's an enumeration type. */
17752 tag_type = enum_type;
17753 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17754 enums) is used here. */
17755 cp_token *token = cp_lexer_peek_token (parser->lexer);
17756 if (cp_parser_is_keyword (token, RID_CLASS)
17757 || cp_parser_is_keyword (token, RID_STRUCT))
17758 {
17759 gcc_rich_location richloc (token->location);
17760 richloc.add_range (input_location);
17761 richloc.add_fixit_remove ();
17762 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17763 "a scoped enum must not use the %qD keyword",
17764 token->u.value);
17765 /* Consume the `struct' or `class' and parse it anyway. */
17766 cp_lexer_consume_token (parser->lexer);
17767 }
17768 /* Parse the attributes. */
17769 attributes = cp_parser_attributes_opt (parser);
17770 }
17771 /* Or, it might be `typename'. */
17772 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17773 RID_TYPENAME))
17774 {
17775 /* Consume the `typename' token. */
17776 cp_lexer_consume_token (parser->lexer);
17777 /* Remember that it's a `typename' type. */
17778 tag_type = typename_type;
17779 }
17780 /* Otherwise it must be a class-key. */
17781 else
17782 {
17783 tag_type = cp_parser_class_key (parser);
17784 if (tag_type == none_type)
17785 return error_mark_node;
17786 /* Parse the attributes. */
17787 attributes = cp_parser_attributes_opt (parser);
17788 }
17789
17790 /* Look for the `::' operator. */
17791 globalscope = cp_parser_global_scope_opt (parser,
17792 /*current_scope_valid_p=*/false);
17793 /* Look for the nested-name-specifier. */
17794 tree nested_name_specifier;
17795 if (tag_type == typename_type && !globalscope)
17796 {
17797 nested_name_specifier
17798 = cp_parser_nested_name_specifier (parser,
17799 /*typename_keyword_p=*/true,
17800 /*check_dependency_p=*/true,
17801 /*type_p=*/true,
17802 is_declaration);
17803 if (!nested_name_specifier)
17804 return error_mark_node;
17805 }
17806 else
17807 /* Even though `typename' is not present, the proposed resolution
17808 to Core Issue 180 says that in `class A<T>::B', `B' should be
17809 considered a type-name, even if `A<T>' is dependent. */
17810 nested_name_specifier
17811 = cp_parser_nested_name_specifier_opt (parser,
17812 /*typename_keyword_p=*/true,
17813 /*check_dependency_p=*/true,
17814 /*type_p=*/true,
17815 is_declaration);
17816 /* For everything but enumeration types, consider a template-id.
17817 For an enumeration type, consider only a plain identifier. */
17818 if (tag_type != enum_type)
17819 {
17820 bool template_p = false;
17821 tree decl;
17822
17823 /* Allow the `template' keyword. */
17824 template_p = cp_parser_optional_template_keyword (parser);
17825 /* If we didn't see `template', we don't know if there's a
17826 template-id or not. */
17827 if (!template_p)
17828 cp_parser_parse_tentatively (parser);
17829 /* Parse the template-id. */
17830 token = cp_lexer_peek_token (parser->lexer);
17831 decl = cp_parser_template_id (parser, template_p,
17832 /*check_dependency_p=*/true,
17833 tag_type,
17834 is_declaration);
17835 /* If we didn't find a template-id, look for an ordinary
17836 identifier. */
17837 if (!template_p && !cp_parser_parse_definitely (parser))
17838 ;
17839 /* We can get here when cp_parser_template_id, called by
17840 cp_parser_class_name with tag_type == none_type, succeeds
17841 and caches a BASELINK. Then, when called again here,
17842 instead of failing and returning an error_mark_node
17843 returns it (see template/typename17.C in C++11).
17844 ??? Could we diagnose this earlier? */
17845 else if (tag_type == typename_type && BASELINK_P (decl))
17846 {
17847 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17848 type = error_mark_node;
17849 }
17850 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17851 in effect, then we must assume that, upon instantiation, the
17852 template will correspond to a class. */
17853 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17854 && tag_type == typename_type)
17855 type = make_typename_type (parser->scope, decl,
17856 typename_type,
17857 /*complain=*/tf_error);
17858 /* If the `typename' keyword is in effect and DECL is not a type
17859 decl, then type is non existent. */
17860 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17861 ;
17862 else if (TREE_CODE (decl) == TYPE_DECL)
17863 {
17864 type = check_elaborated_type_specifier (tag_type, decl,
17865 /*allow_template_p=*/true);
17866
17867 /* If the next token is a semicolon, this must be a specialization,
17868 instantiation, or friend declaration. Check the scope while we
17869 still know whether or not we had a nested-name-specifier. */
17870 if (type != error_mark_node
17871 && !nested_name_specifier && !is_friend
17872 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17873 check_unqualified_spec_or_inst (type, token->location);
17874 }
17875 else if (decl == error_mark_node)
17876 type = error_mark_node;
17877 }
17878
17879 if (!type)
17880 {
17881 token = cp_lexer_peek_token (parser->lexer);
17882 identifier = cp_parser_identifier (parser);
17883
17884 if (identifier == error_mark_node)
17885 {
17886 parser->scope = NULL_TREE;
17887 return error_mark_node;
17888 }
17889
17890 /* For a `typename', we needn't call xref_tag. */
17891 if (tag_type == typename_type
17892 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17893 return cp_parser_make_typename_type (parser, identifier,
17894 token->location);
17895
17896 /* Template parameter lists apply only if we are not within a
17897 function parameter list. */
17898 bool template_parm_lists_apply
17899 = parser->num_template_parameter_lists;
17900 if (template_parm_lists_apply)
17901 for (cp_binding_level *s = current_binding_level;
17902 s && s->kind != sk_template_parms;
17903 s = s->level_chain)
17904 if (s->kind == sk_function_parms)
17905 template_parm_lists_apply = false;
17906
17907 /* Look up a qualified name in the usual way. */
17908 if (parser->scope)
17909 {
17910 tree decl;
17911 tree ambiguous_decls;
17912
17913 decl = cp_parser_lookup_name (parser, identifier,
17914 tag_type,
17915 /*is_template=*/false,
17916 /*is_namespace=*/false,
17917 /*check_dependency=*/true,
17918 &ambiguous_decls,
17919 token->location);
17920
17921 /* If the lookup was ambiguous, an error will already have been
17922 issued. */
17923 if (ambiguous_decls)
17924 return error_mark_node;
17925
17926 /* If we are parsing friend declaration, DECL may be a
17927 TEMPLATE_DECL tree node here. However, we need to check
17928 whether this TEMPLATE_DECL results in valid code. Consider
17929 the following example:
17930
17931 namespace N {
17932 template <class T> class C {};
17933 }
17934 class X {
17935 template <class T> friend class N::C; // #1, valid code
17936 };
17937 template <class T> class Y {
17938 friend class N::C; // #2, invalid code
17939 };
17940
17941 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17942 name lookup of `N::C'. We see that friend declaration must
17943 be template for the code to be valid. Note that
17944 processing_template_decl does not work here since it is
17945 always 1 for the above two cases. */
17946
17947 decl = (cp_parser_maybe_treat_template_as_class
17948 (decl, /*tag_name_p=*/is_friend
17949 && template_parm_lists_apply));
17950
17951 if (TREE_CODE (decl) != TYPE_DECL)
17952 {
17953 cp_parser_diagnose_invalid_type_name (parser,
17954 identifier,
17955 token->location);
17956 return error_mark_node;
17957 }
17958
17959 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17960 {
17961 bool allow_template = (template_parm_lists_apply
17962 || DECL_SELF_REFERENCE_P (decl));
17963 type = check_elaborated_type_specifier (tag_type, decl,
17964 allow_template);
17965
17966 if (type == error_mark_node)
17967 return error_mark_node;
17968 }
17969
17970 /* Forward declarations of nested types, such as
17971
17972 class C1::C2;
17973 class C1::C2::C3;
17974
17975 are invalid unless all components preceding the final '::'
17976 are complete. If all enclosing types are complete, these
17977 declarations become merely pointless.
17978
17979 Invalid forward declarations of nested types are errors
17980 caught elsewhere in parsing. Those that are pointless arrive
17981 here. */
17982
17983 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17984 && !is_friend && !processing_explicit_instantiation)
17985 warning (0, "declaration %qD does not declare anything", decl);
17986
17987 type = TREE_TYPE (decl);
17988 }
17989 else
17990 {
17991 /* An elaborated-type-specifier sometimes introduces a new type and
17992 sometimes names an existing type. Normally, the rule is that it
17993 introduces a new type only if there is not an existing type of
17994 the same name already in scope. For example, given:
17995
17996 struct S {};
17997 void f() { struct S s; }
17998
17999 the `struct S' in the body of `f' is the same `struct S' as in
18000 the global scope; the existing definition is used. However, if
18001 there were no global declaration, this would introduce a new
18002 local class named `S'.
18003
18004 An exception to this rule applies to the following code:
18005
18006 namespace N { struct S; }
18007
18008 Here, the elaborated-type-specifier names a new type
18009 unconditionally; even if there is already an `S' in the
18010 containing scope this declaration names a new type.
18011 This exception only applies if the elaborated-type-specifier
18012 forms the complete declaration:
18013
18014 [class.name]
18015
18016 A declaration consisting solely of `class-key identifier ;' is
18017 either a redeclaration of the name in the current scope or a
18018 forward declaration of the identifier as a class name. It
18019 introduces the name into the current scope.
18020
18021 We are in this situation precisely when the next token is a `;'.
18022
18023 An exception to the exception is that a `friend' declaration does
18024 *not* name a new type; i.e., given:
18025
18026 struct S { friend struct T; };
18027
18028 `T' is not a new type in the scope of `S'.
18029
18030 Also, `new struct S' or `sizeof (struct S)' never results in the
18031 definition of a new type; a new type can only be declared in a
18032 declaration context. */
18033
18034 tag_scope ts;
18035 bool template_p;
18036
18037 if (is_friend)
18038 /* Friends have special name lookup rules. */
18039 ts = ts_within_enclosing_non_class;
18040 else if (is_declaration
18041 && cp_lexer_next_token_is (parser->lexer,
18042 CPP_SEMICOLON))
18043 /* This is a `class-key identifier ;' */
18044 ts = ts_current;
18045 else
18046 ts = ts_global;
18047
18048 template_p =
18049 (template_parm_lists_apply
18050 && (cp_parser_next_token_starts_class_definition_p (parser)
18051 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18052 /* An unqualified name was used to reference this type, so
18053 there were no qualifying templates. */
18054 if (template_parm_lists_apply
18055 && !cp_parser_check_template_parameters (parser,
18056 /*num_templates=*/0,
18057 /*template_id*/false,
18058 token->location,
18059 /*declarator=*/NULL))
18060 return error_mark_node;
18061 type = xref_tag (tag_type, identifier, ts, template_p);
18062 }
18063 }
18064
18065 if (type == error_mark_node)
18066 return error_mark_node;
18067
18068 /* Allow attributes on forward declarations of classes. */
18069 if (attributes)
18070 {
18071 if (TREE_CODE (type) == TYPENAME_TYPE)
18072 warning (OPT_Wattributes,
18073 "attributes ignored on uninstantiated type");
18074 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18075 && ! processing_explicit_instantiation)
18076 warning (OPT_Wattributes,
18077 "attributes ignored on template instantiation");
18078 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18079 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18080 else
18081 warning (OPT_Wattributes,
18082 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18083 }
18084
18085 if (tag_type != enum_type)
18086 {
18087 /* Indicate whether this class was declared as a `class' or as a
18088 `struct'. */
18089 if (CLASS_TYPE_P (type))
18090 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18091 cp_parser_check_class_key (tag_type, type);
18092 }
18093
18094 /* A "<" cannot follow an elaborated type specifier. If that
18095 happens, the user was probably trying to form a template-id. */
18096 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18097 token->location);
18098
18099 return type;
18100 }
18101
18102 /* Parse an enum-specifier.
18103
18104 enum-specifier:
18105 enum-head { enumerator-list [opt] }
18106 enum-head { enumerator-list , } [C++0x]
18107
18108 enum-head:
18109 enum-key identifier [opt] enum-base [opt]
18110 enum-key nested-name-specifier identifier enum-base [opt]
18111
18112 enum-key:
18113 enum
18114 enum class [C++0x]
18115 enum struct [C++0x]
18116
18117 enum-base: [C++0x]
18118 : type-specifier-seq
18119
18120 opaque-enum-specifier:
18121 enum-key identifier enum-base [opt] ;
18122
18123 GNU Extensions:
18124 enum-key attributes[opt] identifier [opt] enum-base [opt]
18125 { enumerator-list [opt] }attributes[opt]
18126 enum-key attributes[opt] identifier [opt] enum-base [opt]
18127 { enumerator-list, }attributes[opt] [C++0x]
18128
18129 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18130 if the token stream isn't an enum-specifier after all. */
18131
18132 static tree
18133 cp_parser_enum_specifier (cp_parser* parser)
18134 {
18135 tree identifier;
18136 tree type = NULL_TREE;
18137 tree prev_scope;
18138 tree nested_name_specifier = NULL_TREE;
18139 tree attributes;
18140 bool scoped_enum_p = false;
18141 bool has_underlying_type = false;
18142 bool nested_being_defined = false;
18143 bool new_value_list = false;
18144 bool is_new_type = false;
18145 bool is_unnamed = false;
18146 tree underlying_type = NULL_TREE;
18147 cp_token *type_start_token = NULL;
18148 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18149
18150 parser->colon_corrects_to_scope_p = false;
18151
18152 /* Parse tentatively so that we can back up if we don't find a
18153 enum-specifier. */
18154 cp_parser_parse_tentatively (parser);
18155
18156 /* Caller guarantees that the current token is 'enum', an identifier
18157 possibly follows, and the token after that is an opening brace.
18158 If we don't have an identifier, fabricate an anonymous name for
18159 the enumeration being defined. */
18160 cp_lexer_consume_token (parser->lexer);
18161
18162 /* Parse the "class" or "struct", which indicates a scoped
18163 enumeration type in C++0x. */
18164 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18165 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18166 {
18167 if (cxx_dialect < cxx11)
18168 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18169
18170 /* Consume the `struct' or `class' token. */
18171 cp_lexer_consume_token (parser->lexer);
18172
18173 scoped_enum_p = true;
18174 }
18175
18176 attributes = cp_parser_attributes_opt (parser);
18177
18178 /* Clear the qualification. */
18179 parser->scope = NULL_TREE;
18180 parser->qualifying_scope = NULL_TREE;
18181 parser->object_scope = NULL_TREE;
18182
18183 /* Figure out in what scope the declaration is being placed. */
18184 prev_scope = current_scope ();
18185
18186 type_start_token = cp_lexer_peek_token (parser->lexer);
18187
18188 push_deferring_access_checks (dk_no_check);
18189 nested_name_specifier
18190 = cp_parser_nested_name_specifier_opt (parser,
18191 /*typename_keyword_p=*/true,
18192 /*check_dependency_p=*/false,
18193 /*type_p=*/false,
18194 /*is_declaration=*/false);
18195
18196 if (nested_name_specifier)
18197 {
18198 tree name;
18199
18200 identifier = cp_parser_identifier (parser);
18201 name = cp_parser_lookup_name (parser, identifier,
18202 enum_type,
18203 /*is_template=*/false,
18204 /*is_namespace=*/false,
18205 /*check_dependency=*/true,
18206 /*ambiguous_decls=*/NULL,
18207 input_location);
18208 if (name && name != error_mark_node)
18209 {
18210 type = TREE_TYPE (name);
18211 if (TREE_CODE (type) == TYPENAME_TYPE)
18212 {
18213 /* Are template enums allowed in ISO? */
18214 if (template_parm_scope_p ())
18215 pedwarn (type_start_token->location, OPT_Wpedantic,
18216 "%qD is an enumeration template", name);
18217 /* ignore a typename reference, for it will be solved by name
18218 in start_enum. */
18219 type = NULL_TREE;
18220 }
18221 }
18222 else if (nested_name_specifier == error_mark_node)
18223 /* We already issued an error. */;
18224 else
18225 {
18226 error_at (type_start_token->location,
18227 "%qD does not name an enumeration in %qT",
18228 identifier, nested_name_specifier);
18229 nested_name_specifier = error_mark_node;
18230 }
18231 }
18232 else
18233 {
18234 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18235 identifier = cp_parser_identifier (parser);
18236 else
18237 {
18238 identifier = make_anon_name ();
18239 is_unnamed = true;
18240 if (scoped_enum_p)
18241 error_at (type_start_token->location,
18242 "unnamed scoped enum is not allowed");
18243 }
18244 }
18245 pop_deferring_access_checks ();
18246
18247 /* Check for the `:' that denotes a specified underlying type in C++0x.
18248 Note that a ':' could also indicate a bitfield width, however. */
18249 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18250 {
18251 cp_decl_specifier_seq type_specifiers;
18252
18253 /* Consume the `:'. */
18254 cp_lexer_consume_token (parser->lexer);
18255
18256 /* Parse the type-specifier-seq. */
18257 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18258 /*is_trailing_return=*/false,
18259 &type_specifiers);
18260
18261 /* At this point this is surely not elaborated type specifier. */
18262 if (!cp_parser_parse_definitely (parser))
18263 return NULL_TREE;
18264
18265 if (cxx_dialect < cxx11)
18266 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18267
18268 has_underlying_type = true;
18269
18270 /* If that didn't work, stop. */
18271 if (type_specifiers.type != error_mark_node)
18272 {
18273 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18274 /*initialized=*/0, NULL);
18275 if (underlying_type == error_mark_node
18276 || check_for_bare_parameter_packs (underlying_type))
18277 underlying_type = NULL_TREE;
18278 }
18279 }
18280
18281 /* Look for the `{' but don't consume it yet. */
18282 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18283 {
18284 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18285 {
18286 cp_parser_error (parser, "expected %<{%>");
18287 if (has_underlying_type)
18288 {
18289 type = NULL_TREE;
18290 goto out;
18291 }
18292 }
18293 /* An opaque-enum-specifier must have a ';' here. */
18294 if ((scoped_enum_p || underlying_type)
18295 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18296 {
18297 cp_parser_error (parser, "expected %<;%> or %<{%>");
18298 if (has_underlying_type)
18299 {
18300 type = NULL_TREE;
18301 goto out;
18302 }
18303 }
18304 }
18305
18306 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18307 return NULL_TREE;
18308
18309 if (nested_name_specifier)
18310 {
18311 if (CLASS_TYPE_P (nested_name_specifier))
18312 {
18313 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18314 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18315 push_scope (nested_name_specifier);
18316 }
18317 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18318 {
18319 push_nested_namespace (nested_name_specifier);
18320 }
18321 }
18322
18323 /* Issue an error message if type-definitions are forbidden here. */
18324 if (!cp_parser_check_type_definition (parser))
18325 type = error_mark_node;
18326 else
18327 /* Create the new type. We do this before consuming the opening
18328 brace so the enum will be recorded as being on the line of its
18329 tag (or the 'enum' keyword, if there is no tag). */
18330 type = start_enum (identifier, type, underlying_type,
18331 attributes, scoped_enum_p, &is_new_type);
18332
18333 /* If the next token is not '{' it is an opaque-enum-specifier or an
18334 elaborated-type-specifier. */
18335 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18336 {
18337 timevar_push (TV_PARSE_ENUM);
18338 if (nested_name_specifier
18339 && nested_name_specifier != error_mark_node)
18340 {
18341 /* The following catches invalid code such as:
18342 enum class S<int>::E { A, B, C }; */
18343 if (!processing_specialization
18344 && CLASS_TYPE_P (nested_name_specifier)
18345 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18346 error_at (type_start_token->location, "cannot add an enumerator "
18347 "list to a template instantiation");
18348
18349 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18350 {
18351 error_at (type_start_token->location,
18352 "%<%T::%E%> has not been declared",
18353 TYPE_CONTEXT (nested_name_specifier),
18354 nested_name_specifier);
18355 type = error_mark_node;
18356 }
18357 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18358 && !CLASS_TYPE_P (nested_name_specifier))
18359 {
18360 error_at (type_start_token->location, "nested name specifier "
18361 "%qT for enum declaration does not name a class "
18362 "or namespace", nested_name_specifier);
18363 type = error_mark_node;
18364 }
18365 /* If that scope does not contain the scope in which the
18366 class was originally declared, the program is invalid. */
18367 else if (prev_scope && !is_ancestor (prev_scope,
18368 nested_name_specifier))
18369 {
18370 if (at_namespace_scope_p ())
18371 error_at (type_start_token->location,
18372 "declaration of %qD in namespace %qD which does not "
18373 "enclose %qD",
18374 type, prev_scope, nested_name_specifier);
18375 else
18376 error_at (type_start_token->location,
18377 "declaration of %qD in %qD which does not "
18378 "enclose %qD",
18379 type, prev_scope, nested_name_specifier);
18380 type = error_mark_node;
18381 }
18382 /* If that scope is the scope where the declaration is being placed
18383 the program is invalid. */
18384 else if (CLASS_TYPE_P (nested_name_specifier)
18385 && CLASS_TYPE_P (prev_scope)
18386 && same_type_p (nested_name_specifier, prev_scope))
18387 {
18388 permerror (type_start_token->location,
18389 "extra qualification not allowed");
18390 nested_name_specifier = NULL_TREE;
18391 }
18392 }
18393
18394 if (scoped_enum_p)
18395 begin_scope (sk_scoped_enum, type);
18396
18397 /* Consume the opening brace. */
18398 matching_braces braces;
18399 braces.consume_open (parser);
18400
18401 if (type == error_mark_node)
18402 ; /* Nothing to add */
18403 else if (OPAQUE_ENUM_P (type)
18404 || (cxx_dialect > cxx98 && processing_specialization))
18405 {
18406 new_value_list = true;
18407 SET_OPAQUE_ENUM_P (type, false);
18408 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18409 }
18410 else
18411 {
18412 error_at (type_start_token->location,
18413 "multiple definition of %q#T", type);
18414 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18415 "previous definition here");
18416 type = error_mark_node;
18417 }
18418
18419 if (type == error_mark_node)
18420 cp_parser_skip_to_end_of_block_or_statement (parser);
18421 /* If the next token is not '}', then there are some enumerators. */
18422 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18423 {
18424 if (is_unnamed && !scoped_enum_p)
18425 pedwarn (type_start_token->location, OPT_Wpedantic,
18426 "ISO C++ forbids empty unnamed enum");
18427 }
18428 else
18429 cp_parser_enumerator_list (parser, type);
18430
18431 /* Consume the final '}'. */
18432 braces.require_close (parser);
18433
18434 if (scoped_enum_p)
18435 finish_scope ();
18436 timevar_pop (TV_PARSE_ENUM);
18437 }
18438 else
18439 {
18440 /* If a ';' follows, then it is an opaque-enum-specifier
18441 and additional restrictions apply. */
18442 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18443 {
18444 if (is_unnamed)
18445 error_at (type_start_token->location,
18446 "opaque-enum-specifier without name");
18447 else if (nested_name_specifier)
18448 error_at (type_start_token->location,
18449 "opaque-enum-specifier must use a simple identifier");
18450 }
18451 }
18452
18453 /* Look for trailing attributes to apply to this enumeration, and
18454 apply them if appropriate. */
18455 if (cp_parser_allow_gnu_extensions_p (parser))
18456 {
18457 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18458 cplus_decl_attributes (&type,
18459 trailing_attr,
18460 (int) ATTR_FLAG_TYPE_IN_PLACE);
18461 }
18462
18463 /* Finish up the enumeration. */
18464 if (type != error_mark_node)
18465 {
18466 if (new_value_list)
18467 finish_enum_value_list (type);
18468 if (is_new_type)
18469 finish_enum (type);
18470 }
18471
18472 if (nested_name_specifier)
18473 {
18474 if (CLASS_TYPE_P (nested_name_specifier))
18475 {
18476 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18477 pop_scope (nested_name_specifier);
18478 }
18479 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18480 {
18481 pop_nested_namespace (nested_name_specifier);
18482 }
18483 }
18484 out:
18485 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18486 return type;
18487 }
18488
18489 /* Parse an enumerator-list. The enumerators all have the indicated
18490 TYPE.
18491
18492 enumerator-list:
18493 enumerator-definition
18494 enumerator-list , enumerator-definition */
18495
18496 static void
18497 cp_parser_enumerator_list (cp_parser* parser, tree type)
18498 {
18499 while (true)
18500 {
18501 /* Parse an enumerator-definition. */
18502 cp_parser_enumerator_definition (parser, type);
18503
18504 /* If the next token is not a ',', we've reached the end of
18505 the list. */
18506 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18507 break;
18508 /* Otherwise, consume the `,' and keep going. */
18509 cp_lexer_consume_token (parser->lexer);
18510 /* If the next token is a `}', there is a trailing comma. */
18511 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18512 {
18513 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18514 pedwarn (input_location, OPT_Wpedantic,
18515 "comma at end of enumerator list");
18516 break;
18517 }
18518 }
18519 }
18520
18521 /* Parse an enumerator-definition. The enumerator has the indicated
18522 TYPE.
18523
18524 enumerator-definition:
18525 enumerator
18526 enumerator = constant-expression
18527
18528 enumerator:
18529 identifier
18530
18531 GNU Extensions:
18532
18533 enumerator-definition:
18534 enumerator attributes [opt]
18535 enumerator attributes [opt] = constant-expression */
18536
18537 static void
18538 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18539 {
18540 tree identifier;
18541 tree value;
18542 location_t loc;
18543
18544 /* Save the input location because we are interested in the location
18545 of the identifier and not the location of the explicit value. */
18546 loc = cp_lexer_peek_token (parser->lexer)->location;
18547
18548 /* Look for the identifier. */
18549 identifier = cp_parser_identifier (parser);
18550 if (identifier == error_mark_node)
18551 return;
18552
18553 /* Parse any specified attributes. */
18554 tree attrs = cp_parser_attributes_opt (parser);
18555
18556 /* If the next token is an '=', then there is an explicit value. */
18557 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18558 {
18559 /* Consume the `=' token. */
18560 cp_lexer_consume_token (parser->lexer);
18561 /* Parse the value. */
18562 value = cp_parser_constant_expression (parser);
18563 }
18564 else
18565 value = NULL_TREE;
18566
18567 /* If we are processing a template, make sure the initializer of the
18568 enumerator doesn't contain any bare template parameter pack. */
18569 if (check_for_bare_parameter_packs (value))
18570 value = error_mark_node;
18571
18572 /* Create the enumerator. */
18573 build_enumerator (identifier, value, type, attrs, loc);
18574 }
18575
18576 /* Parse a namespace-name.
18577
18578 namespace-name:
18579 original-namespace-name
18580 namespace-alias
18581
18582 Returns the NAMESPACE_DECL for the namespace. */
18583
18584 static tree
18585 cp_parser_namespace_name (cp_parser* parser)
18586 {
18587 tree identifier;
18588 tree namespace_decl;
18589
18590 cp_token *token = cp_lexer_peek_token (parser->lexer);
18591
18592 /* Get the name of the namespace. */
18593 identifier = cp_parser_identifier (parser);
18594 if (identifier == error_mark_node)
18595 return error_mark_node;
18596
18597 /* Look up the identifier in the currently active scope. Look only
18598 for namespaces, due to:
18599
18600 [basic.lookup.udir]
18601
18602 When looking up a namespace-name in a using-directive or alias
18603 definition, only namespace names are considered.
18604
18605 And:
18606
18607 [basic.lookup.qual]
18608
18609 During the lookup of a name preceding the :: scope resolution
18610 operator, object, function, and enumerator names are ignored.
18611
18612 (Note that cp_parser_qualifying_entity only calls this
18613 function if the token after the name is the scope resolution
18614 operator.) */
18615 namespace_decl = cp_parser_lookup_name (parser, identifier,
18616 none_type,
18617 /*is_template=*/false,
18618 /*is_namespace=*/true,
18619 /*check_dependency=*/true,
18620 /*ambiguous_decls=*/NULL,
18621 token->location);
18622 /* If it's not a namespace, issue an error. */
18623 if (namespace_decl == error_mark_node
18624 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18625 {
18626 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18627 {
18628 auto_diagnostic_group d;
18629 error_at (token->location, "%qD is not a namespace-name", identifier);
18630 if (namespace_decl == error_mark_node
18631 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18632 suggest_alternative_in_explicit_scope (token->location, identifier,
18633 parser->scope);
18634 }
18635 cp_parser_error (parser, "expected namespace-name");
18636 namespace_decl = error_mark_node;
18637 }
18638
18639 return namespace_decl;
18640 }
18641
18642 /* Parse a namespace-definition.
18643
18644 namespace-definition:
18645 named-namespace-definition
18646 unnamed-namespace-definition
18647
18648 named-namespace-definition:
18649 original-namespace-definition
18650 extension-namespace-definition
18651
18652 original-namespace-definition:
18653 namespace identifier { namespace-body }
18654
18655 extension-namespace-definition:
18656 namespace original-namespace-name { namespace-body }
18657
18658 unnamed-namespace-definition:
18659 namespace { namespace-body } */
18660
18661 static void
18662 cp_parser_namespace_definition (cp_parser* parser)
18663 {
18664 tree identifier;
18665 int nested_definition_count = 0;
18666
18667 cp_ensure_no_omp_declare_simd (parser);
18668 cp_ensure_no_oacc_routine (parser);
18669
18670 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18671
18672 if (is_inline)
18673 {
18674 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18675 cp_lexer_consume_token (parser->lexer);
18676 }
18677
18678 /* Look for the `namespace' keyword. */
18679 cp_token* token
18680 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18681
18682 /* Parse any specified attributes before the identifier. */
18683 tree attribs = cp_parser_attributes_opt (parser);
18684
18685 for (;;)
18686 {
18687 identifier = NULL_TREE;
18688
18689 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18690 {
18691 identifier = cp_parser_identifier (parser);
18692
18693 if (cp_next_tokens_can_be_std_attribute_p (parser))
18694 pedwarn (input_location, OPT_Wpedantic,
18695 "standard attributes on namespaces must precede "
18696 "the namespace name");
18697
18698 /* Parse any attributes specified after the identifier. */
18699 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18700 }
18701
18702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18703 break;
18704
18705 if (!nested_definition_count && cxx_dialect < cxx17)
18706 pedwarn (input_location, OPT_Wpedantic,
18707 "nested namespace definitions only available with "
18708 "-std=c++17 or -std=gnu++17");
18709
18710 /* Nested namespace names can create new namespaces (unlike
18711 other qualified-ids). */
18712 if (int count = identifier ? push_namespace (identifier) : 0)
18713 nested_definition_count += count;
18714 else
18715 cp_parser_error (parser, "nested namespace name required");
18716 cp_lexer_consume_token (parser->lexer);
18717 }
18718
18719 if (nested_definition_count && !identifier)
18720 cp_parser_error (parser, "namespace name required");
18721
18722 if (nested_definition_count && attribs)
18723 error_at (token->location,
18724 "a nested namespace definition cannot have attributes");
18725 if (nested_definition_count && is_inline)
18726 error_at (token->location,
18727 "a nested namespace definition cannot be inline");
18728
18729 /* Start the namespace. */
18730 nested_definition_count += push_namespace (identifier, is_inline);
18731
18732 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18733
18734 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18735
18736 /* Look for the `{' to validate starting the namespace. */
18737 matching_braces braces;
18738 if (braces.require_open (parser))
18739 {
18740 /* Parse the body of the namespace. */
18741 cp_parser_namespace_body (parser);
18742
18743 /* Look for the final `}'. */
18744 braces.require_close (parser);
18745 }
18746
18747 if (has_visibility)
18748 pop_visibility (1);
18749
18750 /* Pop the nested namespace definitions. */
18751 while (nested_definition_count--)
18752 pop_namespace ();
18753 }
18754
18755 /* Parse a namespace-body.
18756
18757 namespace-body:
18758 declaration-seq [opt] */
18759
18760 static void
18761 cp_parser_namespace_body (cp_parser* parser)
18762 {
18763 cp_parser_declaration_seq_opt (parser);
18764 }
18765
18766 /* Parse a namespace-alias-definition.
18767
18768 namespace-alias-definition:
18769 namespace identifier = qualified-namespace-specifier ; */
18770
18771 static void
18772 cp_parser_namespace_alias_definition (cp_parser* parser)
18773 {
18774 tree identifier;
18775 tree namespace_specifier;
18776
18777 cp_token *token = cp_lexer_peek_token (parser->lexer);
18778
18779 /* Look for the `namespace' keyword. */
18780 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18781 /* Look for the identifier. */
18782 identifier = cp_parser_identifier (parser);
18783 if (identifier == error_mark_node)
18784 return;
18785 /* Look for the `=' token. */
18786 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18787 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18788 {
18789 error_at (token->location, "%<namespace%> definition is not allowed here");
18790 /* Skip the definition. */
18791 cp_lexer_consume_token (parser->lexer);
18792 if (cp_parser_skip_to_closing_brace (parser))
18793 cp_lexer_consume_token (parser->lexer);
18794 return;
18795 }
18796 cp_parser_require (parser, CPP_EQ, RT_EQ);
18797 /* Look for the qualified-namespace-specifier. */
18798 namespace_specifier
18799 = cp_parser_qualified_namespace_specifier (parser);
18800 /* Look for the `;' token. */
18801 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18802
18803 /* Register the alias in the symbol table. */
18804 do_namespace_alias (identifier, namespace_specifier);
18805 }
18806
18807 /* Parse a qualified-namespace-specifier.
18808
18809 qualified-namespace-specifier:
18810 :: [opt] nested-name-specifier [opt] namespace-name
18811
18812 Returns a NAMESPACE_DECL corresponding to the specified
18813 namespace. */
18814
18815 static tree
18816 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18817 {
18818 /* Look for the optional `::'. */
18819 cp_parser_global_scope_opt (parser,
18820 /*current_scope_valid_p=*/false);
18821
18822 /* Look for the optional nested-name-specifier. */
18823 cp_parser_nested_name_specifier_opt (parser,
18824 /*typename_keyword_p=*/false,
18825 /*check_dependency_p=*/true,
18826 /*type_p=*/false,
18827 /*is_declaration=*/true);
18828
18829 return cp_parser_namespace_name (parser);
18830 }
18831
18832 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18833 access declaration.
18834
18835 using-declaration:
18836 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18837 using :: unqualified-id ;
18838
18839 access-declaration:
18840 qualified-id ;
18841
18842 */
18843
18844 static bool
18845 cp_parser_using_declaration (cp_parser* parser,
18846 bool access_declaration_p)
18847 {
18848 cp_token *token;
18849 bool typename_p = false;
18850 bool global_scope_p;
18851 tree decl;
18852 tree identifier;
18853 tree qscope;
18854 int oldcount = errorcount;
18855 cp_token *diag_token = NULL;
18856
18857 if (access_declaration_p)
18858 {
18859 diag_token = cp_lexer_peek_token (parser->lexer);
18860 cp_parser_parse_tentatively (parser);
18861 }
18862 else
18863 {
18864 /* Look for the `using' keyword. */
18865 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18866
18867 again:
18868 /* Peek at the next token. */
18869 token = cp_lexer_peek_token (parser->lexer);
18870 /* See if it's `typename'. */
18871 if (token->keyword == RID_TYPENAME)
18872 {
18873 /* Remember that we've seen it. */
18874 typename_p = true;
18875 /* Consume the `typename' token. */
18876 cp_lexer_consume_token (parser->lexer);
18877 }
18878 }
18879
18880 /* Look for the optional global scope qualification. */
18881 global_scope_p
18882 = (cp_parser_global_scope_opt (parser,
18883 /*current_scope_valid_p=*/false)
18884 != NULL_TREE);
18885
18886 /* If we saw `typename', or didn't see `::', then there must be a
18887 nested-name-specifier present. */
18888 if (typename_p || !global_scope_p)
18889 {
18890 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18891 /*check_dependency_p=*/true,
18892 /*type_p=*/false,
18893 /*is_declaration=*/true);
18894 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18895 {
18896 cp_parser_skip_to_end_of_block_or_statement (parser);
18897 return false;
18898 }
18899 }
18900 /* Otherwise, we could be in either of the two productions. In that
18901 case, treat the nested-name-specifier as optional. */
18902 else
18903 qscope = cp_parser_nested_name_specifier_opt (parser,
18904 /*typename_keyword_p=*/false,
18905 /*check_dependency_p=*/true,
18906 /*type_p=*/false,
18907 /*is_declaration=*/true);
18908 if (!qscope)
18909 qscope = global_namespace;
18910 else if (UNSCOPED_ENUM_P (qscope))
18911 qscope = CP_TYPE_CONTEXT (qscope);
18912
18913 if (access_declaration_p && cp_parser_error_occurred (parser))
18914 /* Something has already gone wrong; there's no need to parse
18915 further. Since an error has occurred, the return value of
18916 cp_parser_parse_definitely will be false, as required. */
18917 return cp_parser_parse_definitely (parser);
18918
18919 token = cp_lexer_peek_token (parser->lexer);
18920 /* Parse the unqualified-id. */
18921 identifier = cp_parser_unqualified_id (parser,
18922 /*template_keyword_p=*/false,
18923 /*check_dependency_p=*/true,
18924 /*declarator_p=*/true,
18925 /*optional_p=*/false);
18926
18927 if (access_declaration_p)
18928 {
18929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18930 cp_parser_simulate_error (parser);
18931 if (!cp_parser_parse_definitely (parser))
18932 return false;
18933 }
18934 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18935 {
18936 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18937 if (cxx_dialect < cxx17
18938 && !in_system_header_at (ell->location))
18939 pedwarn (ell->location, 0,
18940 "pack expansion in using-declaration only available "
18941 "with -std=c++17 or -std=gnu++17");
18942 qscope = make_pack_expansion (qscope);
18943 }
18944
18945 /* The function we call to handle a using-declaration is different
18946 depending on what scope we are in. */
18947 if (qscope == error_mark_node || identifier == error_mark_node)
18948 ;
18949 else if (!identifier_p (identifier)
18950 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18951 /* [namespace.udecl]
18952
18953 A using declaration shall not name a template-id. */
18954 error_at (token->location,
18955 "a template-id may not appear in a using-declaration");
18956 else
18957 {
18958 if (at_class_scope_p ())
18959 {
18960 /* Create the USING_DECL. */
18961 decl = do_class_using_decl (qscope, identifier);
18962
18963 if (decl && typename_p)
18964 USING_DECL_TYPENAME_P (decl) = 1;
18965
18966 if (check_for_bare_parameter_packs (decl))
18967 {
18968 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18969 return false;
18970 }
18971 else
18972 /* Add it to the list of members in this class. */
18973 finish_member_declaration (decl);
18974 }
18975 else
18976 {
18977 decl = cp_parser_lookup_name_simple (parser,
18978 identifier,
18979 token->location);
18980 if (decl == error_mark_node)
18981 cp_parser_name_lookup_error (parser, identifier,
18982 decl, NLE_NULL,
18983 token->location);
18984 else if (check_for_bare_parameter_packs (decl))
18985 {
18986 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18987 return false;
18988 }
18989 else if (!at_namespace_scope_p ())
18990 finish_local_using_decl (decl, qscope, identifier);
18991 else
18992 finish_namespace_using_decl (decl, qscope, identifier);
18993 }
18994 }
18995
18996 if (!access_declaration_p
18997 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18998 {
18999 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19000 if (cxx_dialect < cxx17)
19001 pedwarn (comma->location, 0,
19002 "comma-separated list in using-declaration only available "
19003 "with -std=c++17 or -std=gnu++17");
19004 goto again;
19005 }
19006
19007 /* Look for the final `;'. */
19008 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19009
19010 if (access_declaration_p && errorcount == oldcount)
19011 warning_at (diag_token->location, OPT_Wdeprecated,
19012 "access declarations are deprecated "
19013 "in favour of using-declarations; "
19014 "suggestion: add the %<using%> keyword");
19015
19016 return true;
19017 }
19018
19019 /* Parse an alias-declaration.
19020
19021 alias-declaration:
19022 using identifier attribute-specifier-seq [opt] = type-id */
19023
19024 static tree
19025 cp_parser_alias_declaration (cp_parser* parser)
19026 {
19027 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19028 location_t id_location;
19029 cp_declarator *declarator;
19030 cp_decl_specifier_seq decl_specs;
19031 bool member_p;
19032 const char *saved_message = NULL;
19033
19034 /* Look for the `using' keyword. */
19035 cp_token *using_token
19036 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19037 if (using_token == NULL)
19038 return error_mark_node;
19039
19040 id_location = cp_lexer_peek_token (parser->lexer)->location;
19041 id = cp_parser_identifier (parser);
19042 if (id == error_mark_node)
19043 return error_mark_node;
19044
19045 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19046 attributes = cp_parser_attributes_opt (parser);
19047 if (attributes == error_mark_node)
19048 return error_mark_node;
19049
19050 cp_parser_require (parser, CPP_EQ, RT_EQ);
19051
19052 if (cp_parser_error_occurred (parser))
19053 return error_mark_node;
19054
19055 cp_parser_commit_to_tentative_parse (parser);
19056
19057 /* Now we are going to parse the type-id of the declaration. */
19058
19059 /*
19060 [dcl.type]/3 says:
19061
19062 "A type-specifier-seq shall not define a class or enumeration
19063 unless it appears in the type-id of an alias-declaration (7.1.3) that
19064 is not the declaration of a template-declaration."
19065
19066 In other words, if we currently are in an alias template, the
19067 type-id should not define a type.
19068
19069 So let's set parser->type_definition_forbidden_message in that
19070 case; cp_parser_check_type_definition (called by
19071 cp_parser_class_specifier) will then emit an error if a type is
19072 defined in the type-id. */
19073 if (parser->num_template_parameter_lists)
19074 {
19075 saved_message = parser->type_definition_forbidden_message;
19076 parser->type_definition_forbidden_message =
19077 G_("types may not be defined in alias template declarations");
19078 }
19079
19080 type = cp_parser_type_id (parser);
19081
19082 /* Restore the error message if need be. */
19083 if (parser->num_template_parameter_lists)
19084 parser->type_definition_forbidden_message = saved_message;
19085
19086 if (type == error_mark_node
19087 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19088 {
19089 cp_parser_skip_to_end_of_block_or_statement (parser);
19090 return error_mark_node;
19091 }
19092
19093 /* A typedef-name can also be introduced by an alias-declaration. The
19094 identifier following the using keyword becomes a typedef-name. It has
19095 the same semantics as if it were introduced by the typedef
19096 specifier. In particular, it does not define a new type and it shall
19097 not appear in the type-id. */
19098
19099 clear_decl_specs (&decl_specs);
19100 decl_specs.type = type;
19101 if (attributes != NULL_TREE)
19102 {
19103 decl_specs.attributes = attributes;
19104 set_and_check_decl_spec_loc (&decl_specs,
19105 ds_attribute,
19106 attrs_token);
19107 }
19108 set_and_check_decl_spec_loc (&decl_specs,
19109 ds_typedef,
19110 using_token);
19111 set_and_check_decl_spec_loc (&decl_specs,
19112 ds_alias,
19113 using_token);
19114
19115 if (parser->num_template_parameter_lists
19116 && !cp_parser_check_template_parameters (parser,
19117 /*num_templates=*/0,
19118 /*template_id*/false,
19119 id_location,
19120 /*declarator=*/NULL))
19121 return error_mark_node;
19122
19123 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19124 declarator->id_loc = id_location;
19125
19126 member_p = at_class_scope_p ();
19127 if (member_p)
19128 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19129 NULL_TREE, attributes);
19130 else
19131 decl = start_decl (declarator, &decl_specs, 0,
19132 attributes, NULL_TREE, &pushed_scope);
19133 if (decl == error_mark_node)
19134 return decl;
19135
19136 // Attach constraints to the alias declaration.
19137 if (flag_concepts && current_template_parms)
19138 {
19139 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19140 tree constr = build_constraints (reqs, NULL_TREE);
19141 set_constraints (decl, constr);
19142 }
19143
19144 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19145
19146 if (pushed_scope)
19147 pop_scope (pushed_scope);
19148
19149 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19150 added into the symbol table; otherwise, return the TYPE_DECL. */
19151 if (DECL_LANG_SPECIFIC (decl)
19152 && DECL_TEMPLATE_INFO (decl)
19153 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19154 {
19155 decl = DECL_TI_TEMPLATE (decl);
19156 if (member_p)
19157 check_member_template (decl);
19158 }
19159
19160 return decl;
19161 }
19162
19163 /* Parse a using-directive.
19164
19165 using-directive:
19166 using namespace :: [opt] nested-name-specifier [opt]
19167 namespace-name ; */
19168
19169 static void
19170 cp_parser_using_directive (cp_parser* parser)
19171 {
19172 tree namespace_decl;
19173 tree attribs;
19174
19175 /* Look for the `using' keyword. */
19176 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19177 /* And the `namespace' keyword. */
19178 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19179 /* Look for the optional `::' operator. */
19180 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19181 /* And the optional nested-name-specifier. */
19182 cp_parser_nested_name_specifier_opt (parser,
19183 /*typename_keyword_p=*/false,
19184 /*check_dependency_p=*/true,
19185 /*type_p=*/false,
19186 /*is_declaration=*/true);
19187 /* Get the namespace being used. */
19188 namespace_decl = cp_parser_namespace_name (parser);
19189 /* And any specified attributes. */
19190 attribs = cp_parser_attributes_opt (parser);
19191
19192 /* Update the symbol table. */
19193 if (namespace_bindings_p ())
19194 finish_namespace_using_directive (namespace_decl, attribs);
19195 else
19196 finish_local_using_directive (namespace_decl, attribs);
19197
19198 /* Look for the final `;'. */
19199 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19200 }
19201
19202 /* Parse an asm-definition.
19203
19204 asm-definition:
19205 asm ( string-literal ) ;
19206
19207 GNU Extension:
19208
19209 asm-definition:
19210 asm volatile [opt] ( string-literal ) ;
19211 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19212 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19213 : asm-operand-list [opt] ) ;
19214 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19215 : asm-operand-list [opt]
19216 : asm-clobber-list [opt] ) ;
19217 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19218 : asm-clobber-list [opt]
19219 : asm-goto-list ) ; */
19220
19221 static void
19222 cp_parser_asm_definition (cp_parser* parser)
19223 {
19224 tree string;
19225 tree outputs = NULL_TREE;
19226 tree inputs = NULL_TREE;
19227 tree clobbers = NULL_TREE;
19228 tree labels = NULL_TREE;
19229 tree asm_stmt;
19230 bool volatile_p = false;
19231 bool extended_p = false;
19232 bool invalid_inputs_p = false;
19233 bool invalid_outputs_p = false;
19234 bool goto_p = false;
19235 required_token missing = RT_NONE;
19236
19237 /* Look for the `asm' keyword. */
19238 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19239
19240 if (parser->in_function_body
19241 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19242 {
19243 error ("%<asm%> in %<constexpr%> function");
19244 cp_function_chain->invalid_constexpr = true;
19245 }
19246
19247 /* See if the next token is `volatile'. */
19248 if (cp_parser_allow_gnu_extensions_p (parser)
19249 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19250 {
19251 /* Remember that we saw the `volatile' keyword. */
19252 volatile_p = true;
19253 /* Consume the token. */
19254 cp_lexer_consume_token (parser->lexer);
19255 }
19256 if (cp_parser_allow_gnu_extensions_p (parser)
19257 && parser->in_function_body
19258 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19259 {
19260 /* Remember that we saw the `goto' keyword. */
19261 goto_p = true;
19262 /* Consume the token. */
19263 cp_lexer_consume_token (parser->lexer);
19264 }
19265 /* Look for the opening `('. */
19266 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19267 return;
19268 /* Look for the string. */
19269 string = cp_parser_string_literal (parser, false, false);
19270 if (string == error_mark_node)
19271 {
19272 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19273 /*consume_paren=*/true);
19274 return;
19275 }
19276
19277 /* If we're allowing GNU extensions, check for the extended assembly
19278 syntax. Unfortunately, the `:' tokens need not be separated by
19279 a space in C, and so, for compatibility, we tolerate that here
19280 too. Doing that means that we have to treat the `::' operator as
19281 two `:' tokens. */
19282 if (cp_parser_allow_gnu_extensions_p (parser)
19283 && parser->in_function_body
19284 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19285 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19286 {
19287 bool inputs_p = false;
19288 bool clobbers_p = false;
19289 bool labels_p = false;
19290
19291 /* The extended syntax was used. */
19292 extended_p = true;
19293
19294 /* Look for outputs. */
19295 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19296 {
19297 /* Consume the `:'. */
19298 cp_lexer_consume_token (parser->lexer);
19299 /* Parse the output-operands. */
19300 if (cp_lexer_next_token_is_not (parser->lexer,
19301 CPP_COLON)
19302 && cp_lexer_next_token_is_not (parser->lexer,
19303 CPP_SCOPE)
19304 && cp_lexer_next_token_is_not (parser->lexer,
19305 CPP_CLOSE_PAREN)
19306 && !goto_p)
19307 {
19308 outputs = cp_parser_asm_operand_list (parser);
19309 if (outputs == error_mark_node)
19310 invalid_outputs_p = true;
19311 }
19312 }
19313 /* If the next token is `::', there are no outputs, and the
19314 next token is the beginning of the inputs. */
19315 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19316 /* The inputs are coming next. */
19317 inputs_p = true;
19318
19319 /* Look for inputs. */
19320 if (inputs_p
19321 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19322 {
19323 /* Consume the `:' or `::'. */
19324 cp_lexer_consume_token (parser->lexer);
19325 /* Parse the output-operands. */
19326 if (cp_lexer_next_token_is_not (parser->lexer,
19327 CPP_COLON)
19328 && cp_lexer_next_token_is_not (parser->lexer,
19329 CPP_SCOPE)
19330 && cp_lexer_next_token_is_not (parser->lexer,
19331 CPP_CLOSE_PAREN))
19332 {
19333 inputs = cp_parser_asm_operand_list (parser);
19334 if (inputs == error_mark_node)
19335 invalid_inputs_p = true;
19336 }
19337 }
19338 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19339 /* The clobbers are coming next. */
19340 clobbers_p = true;
19341
19342 /* Look for clobbers. */
19343 if (clobbers_p
19344 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19345 {
19346 clobbers_p = true;
19347 /* Consume the `:' or `::'. */
19348 cp_lexer_consume_token (parser->lexer);
19349 /* Parse the clobbers. */
19350 if (cp_lexer_next_token_is_not (parser->lexer,
19351 CPP_COLON)
19352 && cp_lexer_next_token_is_not (parser->lexer,
19353 CPP_CLOSE_PAREN))
19354 clobbers = cp_parser_asm_clobber_list (parser);
19355 }
19356 else if (goto_p
19357 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19358 /* The labels are coming next. */
19359 labels_p = true;
19360
19361 /* Look for labels. */
19362 if (labels_p
19363 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19364 {
19365 labels_p = true;
19366 /* Consume the `:' or `::'. */
19367 cp_lexer_consume_token (parser->lexer);
19368 /* Parse the labels. */
19369 labels = cp_parser_asm_label_list (parser);
19370 }
19371
19372 if (goto_p && !labels_p)
19373 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19374 }
19375 else if (goto_p)
19376 missing = RT_COLON_SCOPE;
19377
19378 /* Look for the closing `)'. */
19379 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19380 missing ? missing : RT_CLOSE_PAREN))
19381 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19382 /*consume_paren=*/true);
19383 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19384
19385 if (!invalid_inputs_p && !invalid_outputs_p)
19386 {
19387 /* Create the ASM_EXPR. */
19388 if (parser->in_function_body)
19389 {
19390 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19391 inputs, clobbers, labels);
19392 /* If the extended syntax was not used, mark the ASM_EXPR. */
19393 if (!extended_p)
19394 {
19395 tree temp = asm_stmt;
19396 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19397 temp = TREE_OPERAND (temp, 0);
19398
19399 ASM_INPUT_P (temp) = 1;
19400 }
19401 }
19402 else
19403 symtab->finalize_toplevel_asm (string);
19404 }
19405 }
19406
19407 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19408 type that comes from the decl-specifier-seq. */
19409
19410 static tree
19411 strip_declarator_types (tree type, cp_declarator *declarator)
19412 {
19413 for (cp_declarator *d = declarator; d;)
19414 switch (d->kind)
19415 {
19416 case cdk_id:
19417 case cdk_decomp:
19418 case cdk_error:
19419 d = NULL;
19420 break;
19421
19422 default:
19423 if (TYPE_PTRMEMFUNC_P (type))
19424 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19425 type = TREE_TYPE (type);
19426 d = d->declarator;
19427 break;
19428 }
19429
19430 return type;
19431 }
19432
19433 /* Declarators [gram.dcl.decl] */
19434
19435 /* Parse an init-declarator.
19436
19437 init-declarator:
19438 declarator initializer [opt]
19439
19440 GNU Extension:
19441
19442 init-declarator:
19443 declarator asm-specification [opt] attributes [opt] initializer [opt]
19444
19445 function-definition:
19446 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19447 function-body
19448 decl-specifier-seq [opt] declarator function-try-block
19449
19450 GNU Extension:
19451
19452 function-definition:
19453 __extension__ function-definition
19454
19455 TM Extension:
19456
19457 function-definition:
19458 decl-specifier-seq [opt] declarator function-transaction-block
19459
19460 The DECL_SPECIFIERS apply to this declarator. Returns a
19461 representation of the entity declared. If MEMBER_P is TRUE, then
19462 this declarator appears in a class scope. The new DECL created by
19463 this declarator is returned.
19464
19465 The CHECKS are access checks that should be performed once we know
19466 what entity is being declared (and, therefore, what classes have
19467 befriended it).
19468
19469 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19470 for a function-definition here as well. If the declarator is a
19471 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19472 be TRUE upon return. By that point, the function-definition will
19473 have been completely parsed.
19474
19475 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19476 is FALSE.
19477
19478 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19479 parsed declaration if it is an uninitialized single declarator not followed
19480 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19481 if present, will not be consumed. If returned, this declarator will be
19482 created with SD_INITIALIZED but will not call cp_finish_decl.
19483
19484 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19485 and there is an initializer, the pointed location_t is set to the
19486 location of the '=' or `(', or '{' in C++11 token introducing the
19487 initializer. */
19488
19489 static tree
19490 cp_parser_init_declarator (cp_parser* parser,
19491 cp_decl_specifier_seq *decl_specifiers,
19492 vec<deferred_access_check, va_gc> *checks,
19493 bool function_definition_allowed_p,
19494 bool member_p,
19495 int declares_class_or_enum,
19496 bool* function_definition_p,
19497 tree* maybe_range_for_decl,
19498 location_t* init_loc,
19499 tree* auto_result)
19500 {
19501 cp_token *token = NULL, *asm_spec_start_token = NULL,
19502 *attributes_start_token = NULL;
19503 cp_declarator *declarator;
19504 tree prefix_attributes;
19505 tree attributes = NULL;
19506 tree asm_specification;
19507 tree initializer;
19508 tree decl = NULL_TREE;
19509 tree scope;
19510 int is_initialized;
19511 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19512 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19513 "(...)". */
19514 enum cpp_ttype initialization_kind;
19515 bool is_direct_init = false;
19516 bool is_non_constant_init;
19517 int ctor_dtor_or_conv_p;
19518 bool friend_p = cp_parser_friend_p (decl_specifiers);
19519 tree pushed_scope = NULL_TREE;
19520 bool range_for_decl_p = false;
19521 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19522 location_t tmp_init_loc = UNKNOWN_LOCATION;
19523
19524 /* Gather the attributes that were provided with the
19525 decl-specifiers. */
19526 prefix_attributes = decl_specifiers->attributes;
19527
19528 /* Assume that this is not the declarator for a function
19529 definition. */
19530 if (function_definition_p)
19531 *function_definition_p = false;
19532
19533 /* Default arguments are only permitted for function parameters. */
19534 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19535 parser->default_arg_ok_p = false;
19536
19537 /* Defer access checks while parsing the declarator; we cannot know
19538 what names are accessible until we know what is being
19539 declared. */
19540 resume_deferring_access_checks ();
19541
19542 token = cp_lexer_peek_token (parser->lexer);
19543
19544 /* Parse the declarator. */
19545 declarator
19546 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19547 &ctor_dtor_or_conv_p,
19548 /*parenthesized_p=*/NULL,
19549 member_p, friend_p);
19550 /* Gather up the deferred checks. */
19551 stop_deferring_access_checks ();
19552
19553 parser->default_arg_ok_p = saved_default_arg_ok_p;
19554
19555 /* If the DECLARATOR was erroneous, there's no need to go
19556 further. */
19557 if (declarator == cp_error_declarator)
19558 return error_mark_node;
19559
19560 /* Check that the number of template-parameter-lists is OK. */
19561 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19562 token->location))
19563 return error_mark_node;
19564
19565 if (declares_class_or_enum & 2)
19566 cp_parser_check_for_definition_in_return_type (declarator,
19567 decl_specifiers->type,
19568 decl_specifiers->locations[ds_type_spec]);
19569
19570 /* Figure out what scope the entity declared by the DECLARATOR is
19571 located in. `grokdeclarator' sometimes changes the scope, so
19572 we compute it now. */
19573 scope = get_scope_of_declarator (declarator);
19574
19575 /* Perform any lookups in the declared type which were thought to be
19576 dependent, but are not in the scope of the declarator. */
19577 decl_specifiers->type
19578 = maybe_update_decl_type (decl_specifiers->type, scope);
19579
19580 /* If we're allowing GNU extensions, look for an
19581 asm-specification. */
19582 if (cp_parser_allow_gnu_extensions_p (parser))
19583 {
19584 /* Look for an asm-specification. */
19585 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19586 asm_specification = cp_parser_asm_specification_opt (parser);
19587 }
19588 else
19589 asm_specification = NULL_TREE;
19590
19591 /* Look for attributes. */
19592 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19593 attributes = cp_parser_attributes_opt (parser);
19594
19595 /* Peek at the next token. */
19596 token = cp_lexer_peek_token (parser->lexer);
19597
19598 bool bogus_implicit_tmpl = false;
19599
19600 if (function_declarator_p (declarator))
19601 {
19602 /* Handle C++17 deduction guides. */
19603 if (!decl_specifiers->type
19604 && ctor_dtor_or_conv_p <= 0
19605 && cxx_dialect >= cxx17)
19606 {
19607 cp_declarator *id = get_id_declarator (declarator);
19608 tree name = id->u.id.unqualified_name;
19609 parser->scope = id->u.id.qualifying_scope;
19610 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19611 if (tmpl
19612 && (DECL_CLASS_TEMPLATE_P (tmpl)
19613 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19614 {
19615 id->u.id.unqualified_name = dguide_name (tmpl);
19616 id->u.id.sfk = sfk_deduction_guide;
19617 ctor_dtor_or_conv_p = 1;
19618 }
19619 }
19620
19621 /* Check to see if the token indicates the start of a
19622 function-definition. */
19623 if (cp_parser_token_starts_function_definition_p (token))
19624 {
19625 if (!function_definition_allowed_p)
19626 {
19627 /* If a function-definition should not appear here, issue an
19628 error message. */
19629 cp_parser_error (parser,
19630 "a function-definition is not allowed here");
19631 return error_mark_node;
19632 }
19633
19634 location_t func_brace_location
19635 = cp_lexer_peek_token (parser->lexer)->location;
19636
19637 /* Neither attributes nor an asm-specification are allowed
19638 on a function-definition. */
19639 if (asm_specification)
19640 error_at (asm_spec_start_token->location,
19641 "an asm-specification is not allowed "
19642 "on a function-definition");
19643 if (attributes)
19644 error_at (attributes_start_token->location,
19645 "attributes are not allowed "
19646 "on a function-definition");
19647 /* This is a function-definition. */
19648 *function_definition_p = true;
19649
19650 /* Parse the function definition. */
19651 if (member_p)
19652 decl = cp_parser_save_member_function_body (parser,
19653 decl_specifiers,
19654 declarator,
19655 prefix_attributes);
19656 else
19657 decl =
19658 (cp_parser_function_definition_from_specifiers_and_declarator
19659 (parser, decl_specifiers, prefix_attributes, declarator));
19660
19661 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19662 {
19663 /* This is where the prologue starts... */
19664 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19665 = func_brace_location;
19666 }
19667
19668 return decl;
19669 }
19670 }
19671 else if (parser->fully_implicit_function_template_p)
19672 {
19673 /* A non-template declaration involving a function parameter list
19674 containing an implicit template parameter will be made into a
19675 template. If the resulting declaration is not going to be an
19676 actual function then finish the template scope here to prevent it.
19677 An error message will be issued once we have a decl to talk about.
19678
19679 FIXME probably we should do type deduction rather than create an
19680 implicit template, but the standard currently doesn't allow it. */
19681 bogus_implicit_tmpl = true;
19682 finish_fully_implicit_template (parser, NULL_TREE);
19683 }
19684
19685 /* [dcl.dcl]
19686
19687 Only in function declarations for constructors, destructors, type
19688 conversions, and deduction guides can the decl-specifier-seq be omitted.
19689
19690 We explicitly postpone this check past the point where we handle
19691 function-definitions because we tolerate function-definitions
19692 that are missing their return types in some modes. */
19693 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19694 {
19695 cp_parser_error (parser,
19696 "expected constructor, destructor, or type conversion");
19697 return error_mark_node;
19698 }
19699
19700 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19701 if (token->type == CPP_EQ
19702 || token->type == CPP_OPEN_PAREN
19703 || token->type == CPP_OPEN_BRACE)
19704 {
19705 is_initialized = SD_INITIALIZED;
19706 initialization_kind = token->type;
19707 if (maybe_range_for_decl)
19708 *maybe_range_for_decl = error_mark_node;
19709 tmp_init_loc = token->location;
19710 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19711 *init_loc = tmp_init_loc;
19712
19713 if (token->type == CPP_EQ
19714 && function_declarator_p (declarator))
19715 {
19716 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19717 if (t2->keyword == RID_DEFAULT)
19718 is_initialized = SD_DEFAULTED;
19719 else if (t2->keyword == RID_DELETE)
19720 is_initialized = SD_DELETED;
19721 }
19722 }
19723 else
19724 {
19725 /* If the init-declarator isn't initialized and isn't followed by a
19726 `,' or `;', it's not a valid init-declarator. */
19727 if (token->type != CPP_COMMA
19728 && token->type != CPP_SEMICOLON)
19729 {
19730 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19731 range_for_decl_p = true;
19732 else
19733 {
19734 if (!maybe_range_for_decl)
19735 cp_parser_error (parser, "expected initializer");
19736 return error_mark_node;
19737 }
19738 }
19739 is_initialized = SD_UNINITIALIZED;
19740 initialization_kind = CPP_EOF;
19741 }
19742
19743 /* Because start_decl has side-effects, we should only call it if we
19744 know we're going ahead. By this point, we know that we cannot
19745 possibly be looking at any other construct. */
19746 cp_parser_commit_to_tentative_parse (parser);
19747
19748 /* Enter the newly declared entry in the symbol table. If we're
19749 processing a declaration in a class-specifier, we wait until
19750 after processing the initializer. */
19751 if (!member_p)
19752 {
19753 if (parser->in_unbraced_linkage_specification_p)
19754 decl_specifiers->storage_class = sc_extern;
19755 decl = start_decl (declarator, decl_specifiers,
19756 range_for_decl_p? SD_INITIALIZED : is_initialized,
19757 attributes, prefix_attributes, &pushed_scope);
19758 cp_finalize_omp_declare_simd (parser, decl);
19759 cp_finalize_oacc_routine (parser, decl, false);
19760 /* Adjust location of decl if declarator->id_loc is more appropriate:
19761 set, and decl wasn't merged with another decl, in which case its
19762 location would be different from input_location, and more accurate. */
19763 if (DECL_P (decl)
19764 && declarator->id_loc != UNKNOWN_LOCATION
19765 && DECL_SOURCE_LOCATION (decl) == input_location)
19766 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19767 }
19768 else if (scope)
19769 /* Enter the SCOPE. That way unqualified names appearing in the
19770 initializer will be looked up in SCOPE. */
19771 pushed_scope = push_scope (scope);
19772
19773 /* Perform deferred access control checks, now that we know in which
19774 SCOPE the declared entity resides. */
19775 if (!member_p && decl)
19776 {
19777 tree saved_current_function_decl = NULL_TREE;
19778
19779 /* If the entity being declared is a function, pretend that we
19780 are in its scope. If it is a `friend', it may have access to
19781 things that would not otherwise be accessible. */
19782 if (TREE_CODE (decl) == FUNCTION_DECL)
19783 {
19784 saved_current_function_decl = current_function_decl;
19785 current_function_decl = decl;
19786 }
19787
19788 /* Perform access checks for template parameters. */
19789 cp_parser_perform_template_parameter_access_checks (checks);
19790
19791 /* Perform the access control checks for the declarator and the
19792 decl-specifiers. */
19793 perform_deferred_access_checks (tf_warning_or_error);
19794
19795 /* Restore the saved value. */
19796 if (TREE_CODE (decl) == FUNCTION_DECL)
19797 current_function_decl = saved_current_function_decl;
19798 }
19799
19800 /* Parse the initializer. */
19801 initializer = NULL_TREE;
19802 is_direct_init = false;
19803 is_non_constant_init = true;
19804 if (is_initialized)
19805 {
19806 if (function_declarator_p (declarator))
19807 {
19808 if (initialization_kind == CPP_EQ)
19809 initializer = cp_parser_pure_specifier (parser);
19810 else
19811 {
19812 /* If the declaration was erroneous, we don't really
19813 know what the user intended, so just silently
19814 consume the initializer. */
19815 if (decl != error_mark_node)
19816 error_at (tmp_init_loc, "initializer provided for function");
19817 cp_parser_skip_to_closing_parenthesis (parser,
19818 /*recovering=*/true,
19819 /*or_comma=*/false,
19820 /*consume_paren=*/true);
19821 }
19822 }
19823 else
19824 {
19825 /* We want to record the extra mangling scope for in-class
19826 initializers of class members and initializers of static data
19827 member templates. The former involves deferring
19828 parsing of the initializer until end of class as with default
19829 arguments. So right here we only handle the latter. */
19830 if (!member_p && processing_template_decl && decl != error_mark_node)
19831 start_lambda_scope (decl);
19832 initializer = cp_parser_initializer (parser,
19833 &is_direct_init,
19834 &is_non_constant_init);
19835 if (!member_p && processing_template_decl && decl != error_mark_node)
19836 finish_lambda_scope ();
19837 if (initializer == error_mark_node)
19838 cp_parser_skip_to_end_of_statement (parser);
19839 }
19840 }
19841
19842 /* The old parser allows attributes to appear after a parenthesized
19843 initializer. Mark Mitchell proposed removing this functionality
19844 on the GCC mailing lists on 2002-08-13. This parser accepts the
19845 attributes -- but ignores them. Made a permerror in GCC 8. */
19846 if (cp_parser_allow_gnu_extensions_p (parser)
19847 && initialization_kind == CPP_OPEN_PAREN
19848 && cp_parser_attributes_opt (parser)
19849 && permerror (input_location,
19850 "attributes after parenthesized initializer ignored"))
19851 {
19852 static bool hint;
19853 if (flag_permissive && !hint)
19854 {
19855 hint = true;
19856 inform (input_location,
19857 "this flexibility is deprecated and will be removed");
19858 }
19859 }
19860
19861 /* And now complain about a non-function implicit template. */
19862 if (bogus_implicit_tmpl && decl != error_mark_node)
19863 error_at (DECL_SOURCE_LOCATION (decl),
19864 "non-function %qD declared as implicit template", decl);
19865
19866 /* For an in-class declaration, use `grokfield' to create the
19867 declaration. */
19868 if (member_p)
19869 {
19870 if (pushed_scope)
19871 {
19872 pop_scope (pushed_scope);
19873 pushed_scope = NULL_TREE;
19874 }
19875 decl = grokfield (declarator, decl_specifiers,
19876 initializer, !is_non_constant_init,
19877 /*asmspec=*/NULL_TREE,
19878 attr_chainon (attributes, prefix_attributes));
19879 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19880 cp_parser_save_default_args (parser, decl);
19881 cp_finalize_omp_declare_simd (parser, decl);
19882 cp_finalize_oacc_routine (parser, decl, false);
19883 }
19884
19885 /* Finish processing the declaration. But, skip member
19886 declarations. */
19887 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19888 {
19889 cp_finish_decl (decl,
19890 initializer, !is_non_constant_init,
19891 asm_specification,
19892 /* If the initializer is in parentheses, then this is
19893 a direct-initialization, which means that an
19894 `explicit' constructor is OK. Otherwise, an
19895 `explicit' constructor cannot be used. */
19896 ((is_direct_init || !is_initialized)
19897 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19898 }
19899 else if ((cxx_dialect != cxx98) && friend_p
19900 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19901 /* Core issue #226 (C++0x only): A default template-argument
19902 shall not be specified in a friend class template
19903 declaration. */
19904 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19905 /*is_partial=*/false, /*is_friend_decl=*/1);
19906
19907 if (!friend_p && pushed_scope)
19908 pop_scope (pushed_scope);
19909
19910 if (function_declarator_p (declarator)
19911 && parser->fully_implicit_function_template_p)
19912 {
19913 if (member_p)
19914 decl = finish_fully_implicit_template (parser, decl);
19915 else
19916 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19917 }
19918
19919 if (auto_result && is_initialized && decl_specifiers->type
19920 && type_uses_auto (decl_specifiers->type))
19921 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19922
19923 return decl;
19924 }
19925
19926 /* Parse a declarator.
19927
19928 declarator:
19929 direct-declarator
19930 ptr-operator declarator
19931
19932 abstract-declarator:
19933 ptr-operator abstract-declarator [opt]
19934 direct-abstract-declarator
19935
19936 GNU Extensions:
19937
19938 declarator:
19939 attributes [opt] direct-declarator
19940 attributes [opt] ptr-operator declarator
19941
19942 abstract-declarator:
19943 attributes [opt] ptr-operator abstract-declarator [opt]
19944 attributes [opt] direct-abstract-declarator
19945
19946 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19947 detect constructors, destructors, deduction guides, or conversion operators.
19948 It is set to -1 if the declarator is a name, and +1 if it is a
19949 function. Otherwise it is set to zero. Usually you just want to
19950 test for >0, but internally the negative value is used.
19951
19952 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19953 a decl-specifier-seq unless it declares a constructor, destructor,
19954 or conversion. It might seem that we could check this condition in
19955 semantic analysis, rather than parsing, but that makes it difficult
19956 to handle something like `f()'. We want to notice that there are
19957 no decl-specifiers, and therefore realize that this is an
19958 expression, not a declaration.)
19959
19960 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19961 the declarator is a direct-declarator of the form "(...)".
19962
19963 MEMBER_P is true iff this declarator is a member-declarator.
19964
19965 FRIEND_P is true iff this declarator is a friend. */
19966
19967 static cp_declarator *
19968 cp_parser_declarator (cp_parser* parser,
19969 cp_parser_declarator_kind dcl_kind,
19970 int* ctor_dtor_or_conv_p,
19971 bool* parenthesized_p,
19972 bool member_p, bool friend_p)
19973 {
19974 cp_declarator *declarator;
19975 enum tree_code code;
19976 cp_cv_quals cv_quals;
19977 tree class_type;
19978 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19979
19980 /* Assume this is not a constructor, destructor, or type-conversion
19981 operator. */
19982 if (ctor_dtor_or_conv_p)
19983 *ctor_dtor_or_conv_p = 0;
19984
19985 if (cp_parser_allow_gnu_extensions_p (parser))
19986 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19987
19988 /* Check for the ptr-operator production. */
19989 cp_parser_parse_tentatively (parser);
19990 /* Parse the ptr-operator. */
19991 code = cp_parser_ptr_operator (parser,
19992 &class_type,
19993 &cv_quals,
19994 &std_attributes);
19995
19996 /* If that worked, then we have a ptr-operator. */
19997 if (cp_parser_parse_definitely (parser))
19998 {
19999 /* If a ptr-operator was found, then this declarator was not
20000 parenthesized. */
20001 if (parenthesized_p)
20002 *parenthesized_p = true;
20003 /* The dependent declarator is optional if we are parsing an
20004 abstract-declarator. */
20005 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20006 cp_parser_parse_tentatively (parser);
20007
20008 /* Parse the dependent declarator. */
20009 declarator = cp_parser_declarator (parser, dcl_kind,
20010 /*ctor_dtor_or_conv_p=*/NULL,
20011 /*parenthesized_p=*/NULL,
20012 /*member_p=*/false,
20013 friend_p);
20014
20015 /* If we are parsing an abstract-declarator, we must handle the
20016 case where the dependent declarator is absent. */
20017 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20018 && !cp_parser_parse_definitely (parser))
20019 declarator = NULL;
20020
20021 declarator = cp_parser_make_indirect_declarator
20022 (code, class_type, cv_quals, declarator, std_attributes);
20023 }
20024 /* Everything else is a direct-declarator. */
20025 else
20026 {
20027 if (parenthesized_p)
20028 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20029 CPP_OPEN_PAREN);
20030 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20031 ctor_dtor_or_conv_p,
20032 member_p, friend_p);
20033 }
20034
20035 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20036 declarator->attributes = gnu_attributes;
20037 return declarator;
20038 }
20039
20040 /* Parse a direct-declarator or direct-abstract-declarator.
20041
20042 direct-declarator:
20043 declarator-id
20044 direct-declarator ( parameter-declaration-clause )
20045 cv-qualifier-seq [opt]
20046 ref-qualifier [opt]
20047 exception-specification [opt]
20048 direct-declarator [ constant-expression [opt] ]
20049 ( declarator )
20050
20051 direct-abstract-declarator:
20052 direct-abstract-declarator [opt]
20053 ( parameter-declaration-clause )
20054 cv-qualifier-seq [opt]
20055 ref-qualifier [opt]
20056 exception-specification [opt]
20057 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20058 ( abstract-declarator )
20059
20060 Returns a representation of the declarator. DCL_KIND is
20061 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20062 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20063 we are parsing a direct-declarator. It is
20064 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20065 of ambiguity we prefer an abstract declarator, as per
20066 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20067 as for cp_parser_declarator. */
20068
20069 static cp_declarator *
20070 cp_parser_direct_declarator (cp_parser* parser,
20071 cp_parser_declarator_kind dcl_kind,
20072 int* ctor_dtor_or_conv_p,
20073 bool member_p, bool friend_p)
20074 {
20075 cp_token *token;
20076 cp_declarator *declarator = NULL;
20077 tree scope = NULL_TREE;
20078 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20079 bool saved_in_declarator_p = parser->in_declarator_p;
20080 bool first = true;
20081 tree pushed_scope = NULL_TREE;
20082 cp_token *open_paren = NULL, *close_paren = NULL;
20083
20084 while (true)
20085 {
20086 /* Peek at the next token. */
20087 token = cp_lexer_peek_token (parser->lexer);
20088 if (token->type == CPP_OPEN_PAREN)
20089 {
20090 /* This is either a parameter-declaration-clause, or a
20091 parenthesized declarator. When we know we are parsing a
20092 named declarator, it must be a parenthesized declarator
20093 if FIRST is true. For instance, `(int)' is a
20094 parameter-declaration-clause, with an omitted
20095 direct-abstract-declarator. But `((*))', is a
20096 parenthesized abstract declarator. Finally, when T is a
20097 template parameter `(T)' is a
20098 parameter-declaration-clause, and not a parenthesized
20099 named declarator.
20100
20101 We first try and parse a parameter-declaration-clause,
20102 and then try a nested declarator (if FIRST is true).
20103
20104 It is not an error for it not to be a
20105 parameter-declaration-clause, even when FIRST is
20106 false. Consider,
20107
20108 int i (int);
20109 int i (3);
20110
20111 The first is the declaration of a function while the
20112 second is the definition of a variable, including its
20113 initializer.
20114
20115 Having seen only the parenthesis, we cannot know which of
20116 these two alternatives should be selected. Even more
20117 complex are examples like:
20118
20119 int i (int (a));
20120 int i (int (3));
20121
20122 The former is a function-declaration; the latter is a
20123 variable initialization.
20124
20125 Thus again, we try a parameter-declaration-clause, and if
20126 that fails, we back out and return. */
20127
20128 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20129 {
20130 tree params;
20131 bool is_declarator = false;
20132
20133 open_paren = NULL;
20134
20135 /* In a member-declarator, the only valid interpretation
20136 of a parenthesis is the start of a
20137 parameter-declaration-clause. (It is invalid to
20138 initialize a static data member with a parenthesized
20139 initializer; only the "=" form of initialization is
20140 permitted.) */
20141 if (!member_p)
20142 cp_parser_parse_tentatively (parser);
20143
20144 /* Consume the `('. */
20145 matching_parens parens;
20146 parens.consume_open (parser);
20147 if (first)
20148 {
20149 /* If this is going to be an abstract declarator, we're
20150 in a declarator and we can't have default args. */
20151 parser->default_arg_ok_p = false;
20152 parser->in_declarator_p = true;
20153 }
20154
20155 begin_scope (sk_function_parms, NULL_TREE);
20156
20157 /* Parse the parameter-declaration-clause. */
20158 params = cp_parser_parameter_declaration_clause (parser);
20159
20160 /* Consume the `)'. */
20161 parens.require_close (parser);
20162
20163 /* If all went well, parse the cv-qualifier-seq,
20164 ref-qualifier and the exception-specification. */
20165 if (member_p || cp_parser_parse_definitely (parser))
20166 {
20167 cp_cv_quals cv_quals;
20168 cp_virt_specifiers virt_specifiers;
20169 cp_ref_qualifier ref_qual;
20170 tree exception_specification;
20171 tree late_return;
20172 tree attrs;
20173 bool memfn = (member_p || (pushed_scope
20174 && CLASS_TYPE_P (pushed_scope)));
20175
20176 is_declarator = true;
20177
20178 if (ctor_dtor_or_conv_p)
20179 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20180 first = false;
20181
20182 /* Parse the cv-qualifier-seq. */
20183 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20184 /* Parse the ref-qualifier. */
20185 ref_qual = cp_parser_ref_qualifier_opt (parser);
20186 /* Parse the tx-qualifier. */
20187 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20188 /* And the exception-specification. */
20189 exception_specification
20190 = cp_parser_exception_specification_opt (parser);
20191
20192 attrs = cp_parser_std_attribute_spec_seq (parser);
20193
20194 /* In here, we handle cases where attribute is used after
20195 the function declaration. For example:
20196 void func (int x) __attribute__((vector(..))); */
20197 tree gnu_attrs = NULL_TREE;
20198 tree requires_clause = NULL_TREE;
20199 late_return = (cp_parser_late_return_type_opt
20200 (parser, declarator, requires_clause,
20201 memfn ? cv_quals : -1));
20202
20203 /* Parse the virt-specifier-seq. */
20204 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20205
20206 /* Create the function-declarator. */
20207 declarator = make_call_declarator (declarator,
20208 params,
20209 cv_quals,
20210 virt_specifiers,
20211 ref_qual,
20212 tx_qual,
20213 exception_specification,
20214 late_return,
20215 requires_clause);
20216 declarator->std_attributes = attrs;
20217 declarator->attributes = gnu_attrs;
20218 /* Any subsequent parameter lists are to do with
20219 return type, so are not those of the declared
20220 function. */
20221 parser->default_arg_ok_p = false;
20222 }
20223
20224 /* Remove the function parms from scope. */
20225 pop_bindings_and_leave_scope ();
20226
20227 if (is_declarator)
20228 /* Repeat the main loop. */
20229 continue;
20230 }
20231
20232 /* If this is the first, we can try a parenthesized
20233 declarator. */
20234 if (first)
20235 {
20236 bool saved_in_type_id_in_expr_p;
20237
20238 parser->default_arg_ok_p = saved_default_arg_ok_p;
20239 parser->in_declarator_p = saved_in_declarator_p;
20240
20241 open_paren = token;
20242 /* Consume the `('. */
20243 matching_parens parens;
20244 parens.consume_open (parser);
20245 /* Parse the nested declarator. */
20246 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20247 parser->in_type_id_in_expr_p = true;
20248 declarator
20249 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20250 /*parenthesized_p=*/NULL,
20251 member_p, friend_p);
20252 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20253 first = false;
20254 /* Expect a `)'. */
20255 close_paren = cp_lexer_peek_token (parser->lexer);
20256 if (!parens.require_close (parser))
20257 declarator = cp_error_declarator;
20258 if (declarator == cp_error_declarator)
20259 break;
20260
20261 goto handle_declarator;
20262 }
20263 /* Otherwise, we must be done. */
20264 else
20265 break;
20266 }
20267 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20268 && token->type == CPP_OPEN_SQUARE
20269 && !cp_next_tokens_can_be_attribute_p (parser))
20270 {
20271 /* Parse an array-declarator. */
20272 tree bounds, attrs;
20273
20274 if (ctor_dtor_or_conv_p)
20275 *ctor_dtor_or_conv_p = 0;
20276
20277 open_paren = NULL;
20278 first = false;
20279 parser->default_arg_ok_p = false;
20280 parser->in_declarator_p = true;
20281 /* Consume the `['. */
20282 cp_lexer_consume_token (parser->lexer);
20283 /* Peek at the next token. */
20284 token = cp_lexer_peek_token (parser->lexer);
20285 /* If the next token is `]', then there is no
20286 constant-expression. */
20287 if (token->type != CPP_CLOSE_SQUARE)
20288 {
20289 bool non_constant_p;
20290 bounds
20291 = cp_parser_constant_expression (parser,
20292 /*allow_non_constant=*/true,
20293 &non_constant_p);
20294 if (!non_constant_p)
20295 /* OK */;
20296 else if (error_operand_p (bounds))
20297 /* Already gave an error. */;
20298 else if (!parser->in_function_body
20299 || current_binding_level->kind == sk_function_parms)
20300 {
20301 /* Normally, the array bound must be an integral constant
20302 expression. However, as an extension, we allow VLAs
20303 in function scopes as long as they aren't part of a
20304 parameter declaration. */
20305 cp_parser_error (parser,
20306 "array bound is not an integer constant");
20307 bounds = error_mark_node;
20308 }
20309 else if (processing_template_decl
20310 && !type_dependent_expression_p (bounds))
20311 {
20312 /* Remember this wasn't a constant-expression. */
20313 bounds = build_nop (TREE_TYPE (bounds), bounds);
20314 TREE_SIDE_EFFECTS (bounds) = 1;
20315 }
20316 }
20317 else
20318 bounds = NULL_TREE;
20319 /* Look for the closing `]'. */
20320 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20321 {
20322 declarator = cp_error_declarator;
20323 break;
20324 }
20325
20326 attrs = cp_parser_std_attribute_spec_seq (parser);
20327 declarator = make_array_declarator (declarator, bounds);
20328 declarator->std_attributes = attrs;
20329 }
20330 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20331 {
20332 {
20333 tree qualifying_scope;
20334 tree unqualified_name;
20335 tree attrs;
20336 special_function_kind sfk;
20337 bool abstract_ok;
20338 bool pack_expansion_p = false;
20339 cp_token *declarator_id_start_token;
20340
20341 /* Parse a declarator-id */
20342 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20343 if (abstract_ok)
20344 {
20345 cp_parser_parse_tentatively (parser);
20346
20347 /* If we see an ellipsis, we should be looking at a
20348 parameter pack. */
20349 if (token->type == CPP_ELLIPSIS)
20350 {
20351 /* Consume the `...' */
20352 cp_lexer_consume_token (parser->lexer);
20353
20354 pack_expansion_p = true;
20355 }
20356 }
20357
20358 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20359 unqualified_name
20360 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20361 qualifying_scope = parser->scope;
20362 if (abstract_ok)
20363 {
20364 bool okay = false;
20365
20366 if (!unqualified_name && pack_expansion_p)
20367 {
20368 /* Check whether an error occurred. */
20369 okay = !cp_parser_error_occurred (parser);
20370
20371 /* We already consumed the ellipsis to mark a
20372 parameter pack, but we have no way to report it,
20373 so abort the tentative parse. We will be exiting
20374 immediately anyway. */
20375 cp_parser_abort_tentative_parse (parser);
20376 }
20377 else
20378 okay = cp_parser_parse_definitely (parser);
20379
20380 if (!okay)
20381 unqualified_name = error_mark_node;
20382 else if (unqualified_name
20383 && (qualifying_scope
20384 || (!identifier_p (unqualified_name))))
20385 {
20386 cp_parser_error (parser, "expected unqualified-id");
20387 unqualified_name = error_mark_node;
20388 }
20389 }
20390
20391 if (!unqualified_name)
20392 return NULL;
20393 if (unqualified_name == error_mark_node)
20394 {
20395 declarator = cp_error_declarator;
20396 pack_expansion_p = false;
20397 declarator->parameter_pack_p = false;
20398 break;
20399 }
20400
20401 attrs = cp_parser_std_attribute_spec_seq (parser);
20402
20403 if (qualifying_scope && at_namespace_scope_p ()
20404 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20405 {
20406 /* In the declaration of a member of a template class
20407 outside of the class itself, the SCOPE will sometimes
20408 be a TYPENAME_TYPE. For example, given:
20409
20410 template <typename T>
20411 int S<T>::R::i = 3;
20412
20413 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20414 this context, we must resolve S<T>::R to an ordinary
20415 type, rather than a typename type.
20416
20417 The reason we normally avoid resolving TYPENAME_TYPEs
20418 is that a specialization of `S' might render
20419 `S<T>::R' not a type. However, if `S' is
20420 specialized, then this `i' will not be used, so there
20421 is no harm in resolving the types here. */
20422 tree type;
20423
20424 /* Resolve the TYPENAME_TYPE. */
20425 type = resolve_typename_type (qualifying_scope,
20426 /*only_current_p=*/false);
20427 /* If that failed, the declarator is invalid. */
20428 if (TREE_CODE (type) == TYPENAME_TYPE)
20429 {
20430 if (typedef_variant_p (type))
20431 error_at (declarator_id_start_token->location,
20432 "cannot define member of dependent typedef "
20433 "%qT", type);
20434 else
20435 error_at (declarator_id_start_token->location,
20436 "%<%T::%E%> is not a type",
20437 TYPE_CONTEXT (qualifying_scope),
20438 TYPE_IDENTIFIER (qualifying_scope));
20439 }
20440 qualifying_scope = type;
20441 }
20442
20443 sfk = sfk_none;
20444
20445 if (unqualified_name)
20446 {
20447 tree class_type;
20448
20449 if (qualifying_scope
20450 && CLASS_TYPE_P (qualifying_scope))
20451 class_type = qualifying_scope;
20452 else
20453 class_type = current_class_type;
20454
20455 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20456 {
20457 tree name_type = TREE_TYPE (unqualified_name);
20458
20459 if (!class_type || !same_type_p (name_type, class_type))
20460 {
20461 /* We do not attempt to print the declarator
20462 here because we do not have enough
20463 information about its original syntactic
20464 form. */
20465 cp_parser_error (parser, "invalid declarator");
20466 declarator = cp_error_declarator;
20467 break;
20468 }
20469 else if (qualifying_scope
20470 && CLASSTYPE_USE_TEMPLATE (name_type))
20471 {
20472 error_at (declarator_id_start_token->location,
20473 "invalid use of constructor as a template");
20474 inform (declarator_id_start_token->location,
20475 "use %<%T::%D%> instead of %<%T::%D%> to "
20476 "name the constructor in a qualified name",
20477 class_type,
20478 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20479 class_type, name_type);
20480 declarator = cp_error_declarator;
20481 break;
20482 }
20483 unqualified_name = constructor_name (class_type);
20484 }
20485
20486 if (class_type)
20487 {
20488 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20489 sfk = sfk_destructor;
20490 else if (identifier_p (unqualified_name)
20491 && IDENTIFIER_CONV_OP_P (unqualified_name))
20492 sfk = sfk_conversion;
20493 else if (/* There's no way to declare a constructor
20494 for an unnamed type, even if the type
20495 got a name for linkage purposes. */
20496 !TYPE_WAS_UNNAMED (class_type)
20497 /* Handle correctly (c++/19200):
20498
20499 struct S {
20500 struct T{};
20501 friend void S(T);
20502 };
20503
20504 and also:
20505
20506 namespace N {
20507 void S();
20508 }
20509
20510 struct S {
20511 friend void N::S();
20512 }; */
20513 && (!friend_p || class_type == qualifying_scope)
20514 && constructor_name_p (unqualified_name,
20515 class_type))
20516 sfk = sfk_constructor;
20517 else if (is_overloaded_fn (unqualified_name)
20518 && DECL_CONSTRUCTOR_P (get_first_fn
20519 (unqualified_name)))
20520 sfk = sfk_constructor;
20521
20522 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20523 *ctor_dtor_or_conv_p = -1;
20524 }
20525 }
20526 declarator = make_id_declarator (qualifying_scope,
20527 unqualified_name,
20528 sfk);
20529 declarator->std_attributes = attrs;
20530 declarator->id_loc = token->location;
20531 declarator->parameter_pack_p = pack_expansion_p;
20532
20533 if (pack_expansion_p)
20534 maybe_warn_variadic_templates ();
20535 }
20536
20537 handle_declarator:;
20538 scope = get_scope_of_declarator (declarator);
20539 if (scope)
20540 {
20541 /* Any names that appear after the declarator-id for a
20542 member are looked up in the containing scope. */
20543 if (at_function_scope_p ())
20544 {
20545 /* But declarations with qualified-ids can't appear in a
20546 function. */
20547 cp_parser_error (parser, "qualified-id in declaration");
20548 declarator = cp_error_declarator;
20549 break;
20550 }
20551 pushed_scope = push_scope (scope);
20552 }
20553 parser->in_declarator_p = true;
20554 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20555 || (declarator && declarator->kind == cdk_id))
20556 /* Default args are only allowed on function
20557 declarations. */
20558 parser->default_arg_ok_p = saved_default_arg_ok_p;
20559 else
20560 parser->default_arg_ok_p = false;
20561
20562 first = false;
20563 }
20564 /* We're done. */
20565 else
20566 break;
20567 }
20568
20569 /* For an abstract declarator, we might wind up with nothing at this
20570 point. That's an error; the declarator is not optional. */
20571 if (!declarator)
20572 cp_parser_error (parser, "expected declarator");
20573 else if (open_paren)
20574 {
20575 /* Record overly parenthesized declarator so we can give a
20576 diagnostic about confusing decl/expr disambiguation. */
20577 if (declarator->kind == cdk_array)
20578 {
20579 /* If the open and close parens are on different lines, this
20580 is probably a formatting thing, so ignore. */
20581 expanded_location open = expand_location (open_paren->location);
20582 expanded_location close = expand_location (close_paren->location);
20583 if (open.line != close.line || open.file != close.file)
20584 open_paren = NULL;
20585 }
20586 if (open_paren)
20587 declarator->parenthesized = open_paren->location;
20588 }
20589
20590 /* If we entered a scope, we must exit it now. */
20591 if (pushed_scope)
20592 pop_scope (pushed_scope);
20593
20594 parser->default_arg_ok_p = saved_default_arg_ok_p;
20595 parser->in_declarator_p = saved_in_declarator_p;
20596
20597 return declarator;
20598 }
20599
20600 /* Parse a ptr-operator.
20601
20602 ptr-operator:
20603 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20604 * cv-qualifier-seq [opt]
20605 &
20606 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20607 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20608
20609 GNU Extension:
20610
20611 ptr-operator:
20612 & cv-qualifier-seq [opt]
20613
20614 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20615 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20616 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20617 filled in with the TYPE containing the member. *CV_QUALS is
20618 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20619 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20620 Note that the tree codes returned by this function have nothing
20621 to do with the types of trees that will be eventually be created
20622 to represent the pointer or reference type being parsed. They are
20623 just constants with suggestive names. */
20624 static enum tree_code
20625 cp_parser_ptr_operator (cp_parser* parser,
20626 tree* type,
20627 cp_cv_quals *cv_quals,
20628 tree *attributes)
20629 {
20630 enum tree_code code = ERROR_MARK;
20631 cp_token *token;
20632 tree attrs = NULL_TREE;
20633
20634 /* Assume that it's not a pointer-to-member. */
20635 *type = NULL_TREE;
20636 /* And that there are no cv-qualifiers. */
20637 *cv_quals = TYPE_UNQUALIFIED;
20638
20639 /* Peek at the next token. */
20640 token = cp_lexer_peek_token (parser->lexer);
20641
20642 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20643 if (token->type == CPP_MULT)
20644 code = INDIRECT_REF;
20645 else if (token->type == CPP_AND)
20646 code = ADDR_EXPR;
20647 else if ((cxx_dialect != cxx98) &&
20648 token->type == CPP_AND_AND) /* C++0x only */
20649 code = NON_LVALUE_EXPR;
20650
20651 if (code != ERROR_MARK)
20652 {
20653 /* Consume the `*', `&' or `&&'. */
20654 cp_lexer_consume_token (parser->lexer);
20655
20656 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20657 `&', if we are allowing GNU extensions. (The only qualifier
20658 that can legally appear after `&' is `restrict', but that is
20659 enforced during semantic analysis. */
20660 if (code == INDIRECT_REF
20661 || cp_parser_allow_gnu_extensions_p (parser))
20662 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20663
20664 attrs = cp_parser_std_attribute_spec_seq (parser);
20665 if (attributes != NULL)
20666 *attributes = attrs;
20667 }
20668 else
20669 {
20670 /* Try the pointer-to-member case. */
20671 cp_parser_parse_tentatively (parser);
20672 /* Look for the optional `::' operator. */
20673 cp_parser_global_scope_opt (parser,
20674 /*current_scope_valid_p=*/false);
20675 /* Look for the nested-name specifier. */
20676 token = cp_lexer_peek_token (parser->lexer);
20677 cp_parser_nested_name_specifier (parser,
20678 /*typename_keyword_p=*/false,
20679 /*check_dependency_p=*/true,
20680 /*type_p=*/false,
20681 /*is_declaration=*/false);
20682 /* If we found it, and the next token is a `*', then we are
20683 indeed looking at a pointer-to-member operator. */
20684 if (!cp_parser_error_occurred (parser)
20685 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20686 {
20687 /* Indicate that the `*' operator was used. */
20688 code = INDIRECT_REF;
20689
20690 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20691 error_at (token->location, "%qD is a namespace", parser->scope);
20692 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20693 error_at (token->location, "cannot form pointer to member of "
20694 "non-class %q#T", parser->scope);
20695 else
20696 {
20697 /* The type of which the member is a member is given by the
20698 current SCOPE. */
20699 *type = parser->scope;
20700 /* The next name will not be qualified. */
20701 parser->scope = NULL_TREE;
20702 parser->qualifying_scope = NULL_TREE;
20703 parser->object_scope = NULL_TREE;
20704 /* Look for optional c++11 attributes. */
20705 attrs = cp_parser_std_attribute_spec_seq (parser);
20706 if (attributes != NULL)
20707 *attributes = attrs;
20708 /* Look for the optional cv-qualifier-seq. */
20709 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20710 }
20711 }
20712 /* If that didn't work we don't have a ptr-operator. */
20713 if (!cp_parser_parse_definitely (parser))
20714 cp_parser_error (parser, "expected ptr-operator");
20715 }
20716
20717 return code;
20718 }
20719
20720 /* Parse an (optional) cv-qualifier-seq.
20721
20722 cv-qualifier-seq:
20723 cv-qualifier cv-qualifier-seq [opt]
20724
20725 cv-qualifier:
20726 const
20727 volatile
20728
20729 GNU Extension:
20730
20731 cv-qualifier:
20732 __restrict__
20733
20734 Returns a bitmask representing the cv-qualifiers. */
20735
20736 static cp_cv_quals
20737 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20738 {
20739 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20740
20741 while (true)
20742 {
20743 cp_token *token;
20744 cp_cv_quals cv_qualifier;
20745
20746 /* Peek at the next token. */
20747 token = cp_lexer_peek_token (parser->lexer);
20748 /* See if it's a cv-qualifier. */
20749 switch (token->keyword)
20750 {
20751 case RID_CONST:
20752 cv_qualifier = TYPE_QUAL_CONST;
20753 break;
20754
20755 case RID_VOLATILE:
20756 cv_qualifier = TYPE_QUAL_VOLATILE;
20757 break;
20758
20759 case RID_RESTRICT:
20760 cv_qualifier = TYPE_QUAL_RESTRICT;
20761 break;
20762
20763 default:
20764 cv_qualifier = TYPE_UNQUALIFIED;
20765 break;
20766 }
20767
20768 if (!cv_qualifier)
20769 break;
20770
20771 if (cv_quals & cv_qualifier)
20772 {
20773 gcc_rich_location richloc (token->location);
20774 richloc.add_fixit_remove ();
20775 error_at (&richloc, "duplicate cv-qualifier");
20776 cp_lexer_purge_token (parser->lexer);
20777 }
20778 else
20779 {
20780 cp_lexer_consume_token (parser->lexer);
20781 cv_quals |= cv_qualifier;
20782 }
20783 }
20784
20785 return cv_quals;
20786 }
20787
20788 /* Parse an (optional) ref-qualifier
20789
20790 ref-qualifier:
20791 &
20792 &&
20793
20794 Returns cp_ref_qualifier representing ref-qualifier. */
20795
20796 static cp_ref_qualifier
20797 cp_parser_ref_qualifier_opt (cp_parser* parser)
20798 {
20799 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20800
20801 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20802 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20803 return ref_qual;
20804
20805 while (true)
20806 {
20807 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20808 cp_token *token = cp_lexer_peek_token (parser->lexer);
20809
20810 switch (token->type)
20811 {
20812 case CPP_AND:
20813 curr_ref_qual = REF_QUAL_LVALUE;
20814 break;
20815
20816 case CPP_AND_AND:
20817 curr_ref_qual = REF_QUAL_RVALUE;
20818 break;
20819
20820 default:
20821 curr_ref_qual = REF_QUAL_NONE;
20822 break;
20823 }
20824
20825 if (!curr_ref_qual)
20826 break;
20827 else if (ref_qual)
20828 {
20829 error_at (token->location, "multiple ref-qualifiers");
20830 cp_lexer_purge_token (parser->lexer);
20831 }
20832 else
20833 {
20834 ref_qual = curr_ref_qual;
20835 cp_lexer_consume_token (parser->lexer);
20836 }
20837 }
20838
20839 return ref_qual;
20840 }
20841
20842 /* Parse an optional tx-qualifier.
20843
20844 tx-qualifier:
20845 transaction_safe
20846 transaction_safe_dynamic */
20847
20848 static tree
20849 cp_parser_tx_qualifier_opt (cp_parser *parser)
20850 {
20851 cp_token *token = cp_lexer_peek_token (parser->lexer);
20852 if (token->type == CPP_NAME)
20853 {
20854 tree name = token->u.value;
20855 const char *p = IDENTIFIER_POINTER (name);
20856 const int len = strlen ("transaction_safe");
20857 if (!strncmp (p, "transaction_safe", len))
20858 {
20859 p += len;
20860 if (*p == '\0'
20861 || !strcmp (p, "_dynamic"))
20862 {
20863 cp_lexer_consume_token (parser->lexer);
20864 if (!flag_tm)
20865 {
20866 error ("%qE requires %<-fgnu-tm%>", name);
20867 return NULL_TREE;
20868 }
20869 else
20870 return name;
20871 }
20872 }
20873 }
20874 return NULL_TREE;
20875 }
20876
20877 /* Parse an (optional) virt-specifier-seq.
20878
20879 virt-specifier-seq:
20880 virt-specifier virt-specifier-seq [opt]
20881
20882 virt-specifier:
20883 override
20884 final
20885
20886 Returns a bitmask representing the virt-specifiers. */
20887
20888 static cp_virt_specifiers
20889 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20890 {
20891 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20892
20893 while (true)
20894 {
20895 cp_token *token;
20896 cp_virt_specifiers virt_specifier;
20897
20898 /* Peek at the next token. */
20899 token = cp_lexer_peek_token (parser->lexer);
20900 /* See if it's a virt-specifier-qualifier. */
20901 if (token->type != CPP_NAME)
20902 break;
20903 if (id_equal (token->u.value, "override"))
20904 {
20905 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20906 virt_specifier = VIRT_SPEC_OVERRIDE;
20907 }
20908 else if (id_equal (token->u.value, "final"))
20909 {
20910 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20911 virt_specifier = VIRT_SPEC_FINAL;
20912 }
20913 else if (id_equal (token->u.value, "__final"))
20914 {
20915 virt_specifier = VIRT_SPEC_FINAL;
20916 }
20917 else
20918 break;
20919
20920 if (virt_specifiers & virt_specifier)
20921 {
20922 gcc_rich_location richloc (token->location);
20923 richloc.add_fixit_remove ();
20924 error_at (&richloc, "duplicate virt-specifier");
20925 cp_lexer_purge_token (parser->lexer);
20926 }
20927 else
20928 {
20929 cp_lexer_consume_token (parser->lexer);
20930 virt_specifiers |= virt_specifier;
20931 }
20932 }
20933 return virt_specifiers;
20934 }
20935
20936 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20937 is in scope even though it isn't real. */
20938
20939 void
20940 inject_this_parameter (tree ctype, cp_cv_quals quals)
20941 {
20942 tree this_parm;
20943
20944 if (current_class_ptr)
20945 {
20946 /* We don't clear this between NSDMIs. Is it already what we want? */
20947 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20948 if (DECL_P (current_class_ptr)
20949 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20950 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20951 && cp_type_quals (type) == quals)
20952 return;
20953 }
20954
20955 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20956 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20957 current_class_ptr = NULL_TREE;
20958 current_class_ref
20959 = cp_build_fold_indirect_ref (this_parm);
20960 current_class_ptr = this_parm;
20961 }
20962
20963 /* Return true iff our current scope is a non-static data member
20964 initializer. */
20965
20966 bool
20967 parsing_nsdmi (void)
20968 {
20969 /* We recognize NSDMI context by the context-less 'this' pointer set up
20970 by the function above. */
20971 if (current_class_ptr
20972 && TREE_CODE (current_class_ptr) == PARM_DECL
20973 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20974 return true;
20975 return false;
20976 }
20977
20978 /* Parse a late-specified return type, if any. This is not a separate
20979 non-terminal, but part of a function declarator, which looks like
20980
20981 -> trailing-type-specifier-seq abstract-declarator(opt)
20982
20983 Returns the type indicated by the type-id.
20984
20985 In addition to this, parse any queued up #pragma omp declare simd
20986 clauses, and #pragma acc routine clauses.
20987
20988 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20989 function. */
20990
20991 static tree
20992 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20993 tree& requires_clause, cp_cv_quals quals)
20994 {
20995 cp_token *token;
20996 tree type = NULL_TREE;
20997 bool declare_simd_p = (parser->omp_declare_simd
20998 && declarator
20999 && declarator->kind == cdk_id);
21000
21001 bool oacc_routine_p = (parser->oacc_routine
21002 && declarator
21003 && declarator->kind == cdk_id);
21004
21005 /* Peek at the next token. */
21006 token = cp_lexer_peek_token (parser->lexer);
21007 /* A late-specified return type is indicated by an initial '->'. */
21008 if (token->type != CPP_DEREF
21009 && token->keyword != RID_REQUIRES
21010 && !(token->type == CPP_NAME
21011 && token->u.value == ridpointers[RID_REQUIRES])
21012 && !(declare_simd_p || oacc_routine_p))
21013 return NULL_TREE;
21014
21015 tree save_ccp = current_class_ptr;
21016 tree save_ccr = current_class_ref;
21017 if (quals >= 0)
21018 {
21019 /* DR 1207: 'this' is in scope in the trailing return type. */
21020 inject_this_parameter (current_class_type, quals);
21021 }
21022
21023 if (token->type == CPP_DEREF)
21024 {
21025 /* Consume the ->. */
21026 cp_lexer_consume_token (parser->lexer);
21027
21028 type = cp_parser_trailing_type_id (parser);
21029 }
21030
21031 /* Function declarations may be followed by a trailing
21032 requires-clause. */
21033 requires_clause = cp_parser_requires_clause_opt (parser);
21034
21035 if (declare_simd_p)
21036 declarator->attributes
21037 = cp_parser_late_parsing_omp_declare_simd (parser,
21038 declarator->attributes);
21039 if (oacc_routine_p)
21040 declarator->attributes
21041 = cp_parser_late_parsing_oacc_routine (parser,
21042 declarator->attributes);
21043
21044 if (quals >= 0)
21045 {
21046 current_class_ptr = save_ccp;
21047 current_class_ref = save_ccr;
21048 }
21049
21050 return type;
21051 }
21052
21053 /* Parse a declarator-id.
21054
21055 declarator-id:
21056 id-expression
21057 :: [opt] nested-name-specifier [opt] type-name
21058
21059 In the `id-expression' case, the value returned is as for
21060 cp_parser_id_expression if the id-expression was an unqualified-id.
21061 If the id-expression was a qualified-id, then a SCOPE_REF is
21062 returned. The first operand is the scope (either a NAMESPACE_DECL
21063 or TREE_TYPE), but the second is still just a representation of an
21064 unqualified-id. */
21065
21066 static tree
21067 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21068 {
21069 tree id;
21070 /* The expression must be an id-expression. Assume that qualified
21071 names are the names of types so that:
21072
21073 template <class T>
21074 int S<T>::R::i = 3;
21075
21076 will work; we must treat `S<T>::R' as the name of a type.
21077 Similarly, assume that qualified names are templates, where
21078 required, so that:
21079
21080 template <class T>
21081 int S<T>::R<T>::i = 3;
21082
21083 will work, too. */
21084 id = cp_parser_id_expression (parser,
21085 /*template_keyword_p=*/false,
21086 /*check_dependency_p=*/false,
21087 /*template_p=*/NULL,
21088 /*declarator_p=*/true,
21089 optional_p);
21090 if (id && BASELINK_P (id))
21091 id = BASELINK_FUNCTIONS (id);
21092 return id;
21093 }
21094
21095 /* Parse a type-id.
21096
21097 type-id:
21098 type-specifier-seq abstract-declarator [opt]
21099
21100 Returns the TYPE specified. */
21101
21102 static tree
21103 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21104 bool is_trailing_return)
21105 {
21106 cp_decl_specifier_seq type_specifier_seq;
21107 cp_declarator *abstract_declarator;
21108
21109 /* Parse the type-specifier-seq. */
21110 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21111 is_trailing_return,
21112 &type_specifier_seq);
21113 if (is_template_arg && type_specifier_seq.type
21114 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21115 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21116 /* A bare template name as a template argument is a template template
21117 argument, not a placeholder, so fail parsing it as a type argument. */
21118 {
21119 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21120 cp_parser_simulate_error (parser);
21121 return error_mark_node;
21122 }
21123 if (type_specifier_seq.type == error_mark_node)
21124 return error_mark_node;
21125
21126 /* There might or might not be an abstract declarator. */
21127 cp_parser_parse_tentatively (parser);
21128 /* Look for the declarator. */
21129 abstract_declarator
21130 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21131 /*parenthesized_p=*/NULL,
21132 /*member_p=*/false,
21133 /*friend_p=*/false);
21134 /* Check to see if there really was a declarator. */
21135 if (!cp_parser_parse_definitely (parser))
21136 abstract_declarator = NULL;
21137
21138 if (type_specifier_seq.type
21139 /* The concepts TS allows 'auto' as a type-id. */
21140 && (!flag_concepts || parser->in_type_id_in_expr_p)
21141 /* None of the valid uses of 'auto' in C++14 involve the type-id
21142 nonterminal, but it is valid in a trailing-return-type. */
21143 && !(cxx_dialect >= cxx14 && is_trailing_return))
21144 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21145 {
21146 /* A type-id with type 'auto' is only ok if the abstract declarator
21147 is a function declarator with a late-specified return type.
21148
21149 A type-id with 'auto' is also valid in a trailing-return-type
21150 in a compound-requirement. */
21151 if (abstract_declarator
21152 && abstract_declarator->kind == cdk_function
21153 && abstract_declarator->u.function.late_return_type)
21154 /* OK */;
21155 else if (parser->in_result_type_constraint_p)
21156 /* OK */;
21157 else
21158 {
21159 location_t loc = type_specifier_seq.locations[ds_type_spec];
21160 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21161 {
21162 error_at (loc, "missing template arguments after %qT",
21163 auto_node);
21164 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21165 tmpl);
21166 }
21167 else
21168 error_at (loc, "invalid use of %qT", auto_node);
21169 return error_mark_node;
21170 }
21171 }
21172
21173 return groktypename (&type_specifier_seq, abstract_declarator,
21174 is_template_arg);
21175 }
21176
21177 static tree
21178 cp_parser_type_id (cp_parser *parser)
21179 {
21180 return cp_parser_type_id_1 (parser, false, false);
21181 }
21182
21183 static tree
21184 cp_parser_template_type_arg (cp_parser *parser)
21185 {
21186 tree r;
21187 const char *saved_message = parser->type_definition_forbidden_message;
21188 parser->type_definition_forbidden_message
21189 = G_("types may not be defined in template arguments");
21190 r = cp_parser_type_id_1 (parser, true, false);
21191 parser->type_definition_forbidden_message = saved_message;
21192 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21193 {
21194 error ("invalid use of %<auto%> in template argument");
21195 r = error_mark_node;
21196 }
21197 return r;
21198 }
21199
21200 static tree
21201 cp_parser_trailing_type_id (cp_parser *parser)
21202 {
21203 return cp_parser_type_id_1 (parser, false, true);
21204 }
21205
21206 /* Parse a type-specifier-seq.
21207
21208 type-specifier-seq:
21209 type-specifier type-specifier-seq [opt]
21210
21211 GNU extension:
21212
21213 type-specifier-seq:
21214 attributes type-specifier-seq [opt]
21215
21216 If IS_DECLARATION is true, we are at the start of a "condition" or
21217 exception-declaration, so we might be followed by a declarator-id.
21218
21219 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21220 i.e. we've just seen "->".
21221
21222 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21223
21224 static void
21225 cp_parser_type_specifier_seq (cp_parser* parser,
21226 bool is_declaration,
21227 bool is_trailing_return,
21228 cp_decl_specifier_seq *type_specifier_seq)
21229 {
21230 bool seen_type_specifier = false;
21231 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21232 cp_token *start_token = NULL;
21233
21234 /* Clear the TYPE_SPECIFIER_SEQ. */
21235 clear_decl_specs (type_specifier_seq);
21236
21237 /* In the context of a trailing return type, enum E { } is an
21238 elaborated-type-specifier followed by a function-body, not an
21239 enum-specifier. */
21240 if (is_trailing_return)
21241 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21242
21243 /* Parse the type-specifiers and attributes. */
21244 while (true)
21245 {
21246 tree type_specifier;
21247 bool is_cv_qualifier;
21248
21249 /* Check for attributes first. */
21250 if (cp_next_tokens_can_be_attribute_p (parser))
21251 {
21252 type_specifier_seq->attributes
21253 = attr_chainon (type_specifier_seq->attributes,
21254 cp_parser_attributes_opt (parser));
21255 continue;
21256 }
21257
21258 /* record the token of the beginning of the type specifier seq,
21259 for error reporting purposes*/
21260 if (!start_token)
21261 start_token = cp_lexer_peek_token (parser->lexer);
21262
21263 /* Look for the type-specifier. */
21264 type_specifier = cp_parser_type_specifier (parser,
21265 flags,
21266 type_specifier_seq,
21267 /*is_declaration=*/false,
21268 NULL,
21269 &is_cv_qualifier);
21270 if (!type_specifier)
21271 {
21272 /* If the first type-specifier could not be found, this is not a
21273 type-specifier-seq at all. */
21274 if (!seen_type_specifier)
21275 {
21276 /* Set in_declarator_p to avoid skipping to the semicolon. */
21277 int in_decl = parser->in_declarator_p;
21278 parser->in_declarator_p = true;
21279
21280 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21281 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21282 cp_parser_error (parser, "expected type-specifier");
21283
21284 parser->in_declarator_p = in_decl;
21285
21286 type_specifier_seq->type = error_mark_node;
21287 return;
21288 }
21289 /* If subsequent type-specifiers could not be found, the
21290 type-specifier-seq is complete. */
21291 break;
21292 }
21293
21294 seen_type_specifier = true;
21295 /* The standard says that a condition can be:
21296
21297 type-specifier-seq declarator = assignment-expression
21298
21299 However, given:
21300
21301 struct S {};
21302 if (int S = ...)
21303
21304 we should treat the "S" as a declarator, not as a
21305 type-specifier. The standard doesn't say that explicitly for
21306 type-specifier-seq, but it does say that for
21307 decl-specifier-seq in an ordinary declaration. Perhaps it
21308 would be clearer just to allow a decl-specifier-seq here, and
21309 then add a semantic restriction that if any decl-specifiers
21310 that are not type-specifiers appear, the program is invalid. */
21311 if (is_declaration && !is_cv_qualifier)
21312 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21313 }
21314 }
21315
21316 /* Return whether the function currently being declared has an associated
21317 template parameter list. */
21318
21319 static bool
21320 function_being_declared_is_template_p (cp_parser* parser)
21321 {
21322 if (!current_template_parms || processing_template_parmlist)
21323 return false;
21324
21325 if (parser->implicit_template_scope)
21326 return true;
21327
21328 if (at_class_scope_p ()
21329 && TYPE_BEING_DEFINED (current_class_type))
21330 return parser->num_template_parameter_lists != 0;
21331
21332 return ((int) parser->num_template_parameter_lists > template_class_depth
21333 (current_class_type));
21334 }
21335
21336 /* Parse a parameter-declaration-clause.
21337
21338 parameter-declaration-clause:
21339 parameter-declaration-list [opt] ... [opt]
21340 parameter-declaration-list , ...
21341
21342 Returns a representation for the parameter declarations. A return
21343 value of NULL indicates a parameter-declaration-clause consisting
21344 only of an ellipsis. */
21345
21346 static tree
21347 cp_parser_parameter_declaration_clause (cp_parser* parser)
21348 {
21349 tree parameters;
21350 cp_token *token;
21351 bool ellipsis_p;
21352
21353 temp_override<bool> cleanup
21354 (parser->auto_is_implicit_function_template_parm_p);
21355
21356 if (!processing_specialization
21357 && !processing_template_parmlist
21358 && !processing_explicit_instantiation
21359 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21360 actual function or a random abstract declarator. */
21361 && parser->default_arg_ok_p)
21362 if (!current_function_decl
21363 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21364 parser->auto_is_implicit_function_template_parm_p = true;
21365
21366 /* Peek at the next token. */
21367 token = cp_lexer_peek_token (parser->lexer);
21368 /* Check for trivial parameter-declaration-clauses. */
21369 if (token->type == CPP_ELLIPSIS)
21370 {
21371 /* Consume the `...' token. */
21372 cp_lexer_consume_token (parser->lexer);
21373 return NULL_TREE;
21374 }
21375 else if (token->type == CPP_CLOSE_PAREN)
21376 /* There are no parameters. */
21377 {
21378 #ifdef SYSTEM_IMPLICIT_EXTERN_C
21379 if (in_system_header_at (input_location)
21380 && current_class_type == NULL
21381 && current_lang_name == lang_name_c)
21382 return NULL_TREE;
21383 else
21384 #endif
21385 return void_list_node;
21386 }
21387 /* Check for `(void)', too, which is a special case. */
21388 else if (token->keyword == RID_VOID
21389 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21390 == CPP_CLOSE_PAREN))
21391 {
21392 /* Consume the `void' token. */
21393 cp_lexer_consume_token (parser->lexer);
21394 /* There are no parameters. */
21395 return void_list_node;
21396 }
21397
21398 /* Parse the parameter-declaration-list. */
21399 parameters = cp_parser_parameter_declaration_list (parser);
21400 /* If a parse error occurred while parsing the
21401 parameter-declaration-list, then the entire
21402 parameter-declaration-clause is erroneous. */
21403 if (parameters == error_mark_node)
21404 return NULL_TREE;
21405
21406 /* Peek at the next token. */
21407 token = cp_lexer_peek_token (parser->lexer);
21408 /* If it's a `,', the clause should terminate with an ellipsis. */
21409 if (token->type == CPP_COMMA)
21410 {
21411 /* Consume the `,'. */
21412 cp_lexer_consume_token (parser->lexer);
21413 /* Expect an ellipsis. */
21414 ellipsis_p
21415 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21416 }
21417 /* It might also be `...' if the optional trailing `,' was
21418 omitted. */
21419 else if (token->type == CPP_ELLIPSIS)
21420 {
21421 /* Consume the `...' token. */
21422 cp_lexer_consume_token (parser->lexer);
21423 /* And remember that we saw it. */
21424 ellipsis_p = true;
21425 }
21426 else
21427 ellipsis_p = false;
21428
21429 /* Finish the parameter list. */
21430 if (!ellipsis_p)
21431 parameters = chainon (parameters, void_list_node);
21432
21433 return parameters;
21434 }
21435
21436 /* Parse a parameter-declaration-list.
21437
21438 parameter-declaration-list:
21439 parameter-declaration
21440 parameter-declaration-list , parameter-declaration
21441
21442 Returns a representation of the parameter-declaration-list, as for
21443 cp_parser_parameter_declaration_clause. However, the
21444 `void_list_node' is never appended to the list. */
21445
21446 static tree
21447 cp_parser_parameter_declaration_list (cp_parser* parser)
21448 {
21449 tree parameters = NULL_TREE;
21450 tree *tail = &parameters;
21451 bool saved_in_unbraced_linkage_specification_p;
21452 int index = 0;
21453
21454 /* The special considerations that apply to a function within an
21455 unbraced linkage specifications do not apply to the parameters
21456 to the function. */
21457 saved_in_unbraced_linkage_specification_p
21458 = parser->in_unbraced_linkage_specification_p;
21459 parser->in_unbraced_linkage_specification_p = false;
21460
21461 /* Look for more parameters. */
21462 while (true)
21463 {
21464 cp_parameter_declarator *parameter;
21465 tree decl = error_mark_node;
21466 bool parenthesized_p = false;
21467
21468 /* Parse the parameter. */
21469 parameter
21470 = cp_parser_parameter_declaration (parser,
21471 /*template_parm_p=*/false,
21472 &parenthesized_p);
21473
21474 /* We don't know yet if the enclosing context is deprecated, so wait
21475 and warn in grokparms if appropriate. */
21476 deprecated_state = DEPRECATED_SUPPRESS;
21477
21478 if (parameter)
21479 {
21480 decl = grokdeclarator (parameter->declarator,
21481 &parameter->decl_specifiers,
21482 PARM,
21483 parameter->default_argument != NULL_TREE,
21484 &parameter->decl_specifiers.attributes);
21485 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21486 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21487 }
21488
21489 deprecated_state = DEPRECATED_NORMAL;
21490
21491 /* If a parse error occurred parsing the parameter declaration,
21492 then the entire parameter-declaration-list is erroneous. */
21493 if (decl == error_mark_node)
21494 {
21495 parameters = error_mark_node;
21496 break;
21497 }
21498
21499 if (parameter->decl_specifiers.attributes)
21500 cplus_decl_attributes (&decl,
21501 parameter->decl_specifiers.attributes,
21502 0);
21503 if (DECL_NAME (decl))
21504 decl = pushdecl (decl);
21505
21506 if (decl != error_mark_node)
21507 {
21508 retrofit_lang_decl (decl);
21509 DECL_PARM_INDEX (decl) = ++index;
21510 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21511 }
21512
21513 /* Add the new parameter to the list. */
21514 *tail = build_tree_list (parameter->default_argument, decl);
21515 tail = &TREE_CHAIN (*tail);
21516
21517 /* Peek at the next token. */
21518 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21519 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21520 /* These are for Objective-C++ */
21521 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21522 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21523 /* The parameter-declaration-list is complete. */
21524 break;
21525 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21526 {
21527 cp_token *token;
21528
21529 /* Peek at the next token. */
21530 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21531 /* If it's an ellipsis, then the list is complete. */
21532 if (token->type == CPP_ELLIPSIS)
21533 break;
21534 /* Otherwise, there must be more parameters. Consume the
21535 `,'. */
21536 cp_lexer_consume_token (parser->lexer);
21537 /* When parsing something like:
21538
21539 int i(float f, double d)
21540
21541 we can tell after seeing the declaration for "f" that we
21542 are not looking at an initialization of a variable "i",
21543 but rather at the declaration of a function "i".
21544
21545 Due to the fact that the parsing of template arguments
21546 (as specified to a template-id) requires backtracking we
21547 cannot use this technique when inside a template argument
21548 list. */
21549 if (!parser->in_template_argument_list_p
21550 && !parser->in_type_id_in_expr_p
21551 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21552 /* However, a parameter-declaration of the form
21553 "float(f)" (which is a valid declaration of a
21554 parameter "f") can also be interpreted as an
21555 expression (the conversion of "f" to "float"). */
21556 && !parenthesized_p)
21557 cp_parser_commit_to_tentative_parse (parser);
21558 }
21559 else
21560 {
21561 cp_parser_error (parser, "expected %<,%> or %<...%>");
21562 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21563 cp_parser_skip_to_closing_parenthesis (parser,
21564 /*recovering=*/true,
21565 /*or_comma=*/false,
21566 /*consume_paren=*/false);
21567 break;
21568 }
21569 }
21570
21571 parser->in_unbraced_linkage_specification_p
21572 = saved_in_unbraced_linkage_specification_p;
21573
21574 /* Reset implicit_template_scope if we are about to leave the function
21575 parameter list that introduced it. Note that for out-of-line member
21576 definitions, there will be one or more class scopes before we get to
21577 the template parameter scope. */
21578
21579 if (cp_binding_level *its = parser->implicit_template_scope)
21580 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21581 {
21582 while (maybe_its->kind == sk_class)
21583 maybe_its = maybe_its->level_chain;
21584 if (maybe_its == its)
21585 {
21586 parser->implicit_template_parms = 0;
21587 parser->implicit_template_scope = 0;
21588 }
21589 }
21590
21591 return parameters;
21592 }
21593
21594 /* Parse a parameter declaration.
21595
21596 parameter-declaration:
21597 decl-specifier-seq ... [opt] declarator
21598 decl-specifier-seq declarator = assignment-expression
21599 decl-specifier-seq ... [opt] abstract-declarator [opt]
21600 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21601
21602 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21603 declares a template parameter. (In that case, a non-nested `>'
21604 token encountered during the parsing of the assignment-expression
21605 is not interpreted as a greater-than operator.)
21606
21607 Returns a representation of the parameter, or NULL if an error
21608 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21609 true iff the declarator is of the form "(p)". */
21610
21611 static cp_parameter_declarator *
21612 cp_parser_parameter_declaration (cp_parser *parser,
21613 bool template_parm_p,
21614 bool *parenthesized_p)
21615 {
21616 int declares_class_or_enum;
21617 cp_decl_specifier_seq decl_specifiers;
21618 cp_declarator *declarator;
21619 tree default_argument;
21620 cp_token *token = NULL, *declarator_token_start = NULL;
21621 const char *saved_message;
21622 bool template_parameter_pack_p = false;
21623
21624 /* In a template parameter, `>' is not an operator.
21625
21626 [temp.param]
21627
21628 When parsing a default template-argument for a non-type
21629 template-parameter, the first non-nested `>' is taken as the end
21630 of the template parameter-list rather than a greater-than
21631 operator. */
21632
21633 /* Type definitions may not appear in parameter types. */
21634 saved_message = parser->type_definition_forbidden_message;
21635 parser->type_definition_forbidden_message
21636 = G_("types may not be defined in parameter types");
21637
21638 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21639 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21640 (current_template_parms)) : 0);
21641
21642 /* Parse the declaration-specifiers. */
21643 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21644 cp_parser_decl_specifier_seq (parser,
21645 CP_PARSER_FLAGS_NONE,
21646 &decl_specifiers,
21647 &declares_class_or_enum);
21648
21649 /* Complain about missing 'typename' or other invalid type names. */
21650 if (!decl_specifiers.any_type_specifiers_p
21651 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21652 decl_specifiers.type = error_mark_node;
21653
21654 /* If an error occurred, there's no reason to attempt to parse the
21655 rest of the declaration. */
21656 if (cp_parser_error_occurred (parser))
21657 {
21658 parser->type_definition_forbidden_message = saved_message;
21659 return NULL;
21660 }
21661
21662 /* Peek at the next token. */
21663 token = cp_lexer_peek_token (parser->lexer);
21664
21665 /* If the next token is a `)', `,', `=', `>', or `...', then there
21666 is no declarator. However, when variadic templates are enabled,
21667 there may be a declarator following `...'. */
21668 if (token->type == CPP_CLOSE_PAREN
21669 || token->type == CPP_COMMA
21670 || token->type == CPP_EQ
21671 || token->type == CPP_GREATER)
21672 {
21673 declarator = NULL;
21674 if (parenthesized_p)
21675 *parenthesized_p = false;
21676 }
21677 /* Otherwise, there should be a declarator. */
21678 else
21679 {
21680 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21681 parser->default_arg_ok_p = false;
21682
21683 /* After seeing a decl-specifier-seq, if the next token is not a
21684 "(", there is no possibility that the code is a valid
21685 expression. Therefore, if parsing tentatively, we commit at
21686 this point. */
21687 if (!parser->in_template_argument_list_p
21688 /* In an expression context, having seen:
21689
21690 (int((char ...
21691
21692 we cannot be sure whether we are looking at a
21693 function-type (taking a "char" as a parameter) or a cast
21694 of some object of type "char" to "int". */
21695 && !parser->in_type_id_in_expr_p
21696 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21697 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21698 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21699 cp_parser_commit_to_tentative_parse (parser);
21700 /* Parse the declarator. */
21701 declarator_token_start = token;
21702 declarator = cp_parser_declarator (parser,
21703 CP_PARSER_DECLARATOR_EITHER,
21704 /*ctor_dtor_or_conv_p=*/NULL,
21705 parenthesized_p,
21706 /*member_p=*/false,
21707 /*friend_p=*/false);
21708 parser->default_arg_ok_p = saved_default_arg_ok_p;
21709 /* After the declarator, allow more attributes. */
21710 decl_specifiers.attributes
21711 = attr_chainon (decl_specifiers.attributes,
21712 cp_parser_attributes_opt (parser));
21713
21714 /* If the declarator is a template parameter pack, remember that and
21715 clear the flag in the declarator itself so we don't get errors
21716 from grokdeclarator. */
21717 if (template_parm_p && declarator && declarator->parameter_pack_p)
21718 {
21719 declarator->parameter_pack_p = false;
21720 template_parameter_pack_p = true;
21721 }
21722 }
21723
21724 /* If the next token is an ellipsis, and we have not seen a declarator
21725 name, and if either the type of the declarator contains parameter
21726 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21727 for, eg, abbreviated integral type names), then we actually have a
21728 parameter pack expansion expression. Otherwise, leave the ellipsis
21729 for a C-style variadic function. */
21730 token = cp_lexer_peek_token (parser->lexer);
21731
21732 /* If a function parameter pack was specified and an implicit template
21733 parameter was introduced during cp_parser_parameter_declaration,
21734 change any implicit parameters introduced into packs. */
21735 if (parser->implicit_template_parms
21736 && ((token->type == CPP_ELLIPSIS
21737 && declarator_can_be_parameter_pack (declarator))
21738 || (declarator && declarator->parameter_pack_p)))
21739 {
21740 int latest_template_parm_idx = TREE_VEC_LENGTH
21741 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21742
21743 if (latest_template_parm_idx != template_parm_idx)
21744 decl_specifiers.type = convert_generic_types_to_packs
21745 (decl_specifiers.type,
21746 template_parm_idx, latest_template_parm_idx);
21747 }
21748
21749 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21750 {
21751 tree type = decl_specifiers.type;
21752
21753 if (type && DECL_P (type))
21754 type = TREE_TYPE (type);
21755
21756 if (((type
21757 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21758 && (template_parm_p || uses_parameter_packs (type)))
21759 || (!type && template_parm_p))
21760 && declarator_can_be_parameter_pack (declarator))
21761 {
21762 /* Consume the `...'. */
21763 cp_lexer_consume_token (parser->lexer);
21764 maybe_warn_variadic_templates ();
21765
21766 /* Build a pack expansion type */
21767 if (template_parm_p)
21768 template_parameter_pack_p = true;
21769 else if (declarator)
21770 declarator->parameter_pack_p = true;
21771 else
21772 decl_specifiers.type = make_pack_expansion (type);
21773 }
21774 }
21775
21776 /* The restriction on defining new types applies only to the type
21777 of the parameter, not to the default argument. */
21778 parser->type_definition_forbidden_message = saved_message;
21779
21780 /* If the next token is `=', then process a default argument. */
21781 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21782 {
21783 tree type = decl_specifiers.type;
21784 token = cp_lexer_peek_token (parser->lexer);
21785 /* If we are defining a class, then the tokens that make up the
21786 default argument must be saved and processed later. */
21787 if (!template_parm_p && at_class_scope_p ()
21788 && TYPE_BEING_DEFINED (current_class_type)
21789 && !LAMBDA_TYPE_P (current_class_type))
21790 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21791
21792 // A constrained-type-specifier may declare a type template-parameter.
21793 else if (declares_constrained_type_template_parameter (type))
21794 default_argument
21795 = cp_parser_default_type_template_argument (parser);
21796
21797 // A constrained-type-specifier may declare a template-template-parameter.
21798 else if (declares_constrained_template_template_parameter (type))
21799 default_argument
21800 = cp_parser_default_template_template_argument (parser);
21801
21802 /* Outside of a class definition, we can just parse the
21803 assignment-expression. */
21804 else
21805 default_argument
21806 = cp_parser_default_argument (parser, template_parm_p);
21807
21808 if (!parser->default_arg_ok_p)
21809 {
21810 permerror (token->location,
21811 "default arguments are only "
21812 "permitted for function parameters");
21813 }
21814 else if ((declarator && declarator->parameter_pack_p)
21815 || template_parameter_pack_p
21816 || (decl_specifiers.type
21817 && PACK_EXPANSION_P (decl_specifiers.type)))
21818 {
21819 /* Find the name of the parameter pack. */
21820 cp_declarator *id_declarator = declarator;
21821 while (id_declarator && id_declarator->kind != cdk_id)
21822 id_declarator = id_declarator->declarator;
21823
21824 if (id_declarator && id_declarator->kind == cdk_id)
21825 error_at (declarator_token_start->location,
21826 template_parm_p
21827 ? G_("template parameter pack %qD "
21828 "cannot have a default argument")
21829 : G_("parameter pack %qD cannot have "
21830 "a default argument"),
21831 id_declarator->u.id.unqualified_name);
21832 else
21833 error_at (declarator_token_start->location,
21834 template_parm_p
21835 ? G_("template parameter pack cannot have "
21836 "a default argument")
21837 : G_("parameter pack cannot have a "
21838 "default argument"));
21839
21840 default_argument = NULL_TREE;
21841 }
21842 }
21843 else
21844 default_argument = NULL_TREE;
21845
21846 /* Generate a location for the parameter, ranging from the start of the
21847 initial token to the end of the final token (using input_location for
21848 the latter, set up by cp_lexer_set_source_position_from_token when
21849 consuming tokens).
21850
21851 If we have a identifier, then use it for the caret location, e.g.
21852
21853 extern int callee (int one, int (*two)(int, int), float three);
21854 ~~~~~~^~~~~~~~~~~~~~
21855
21856 otherwise, reuse the start location for the caret location e.g.:
21857
21858 extern int callee (int one, int (*)(int, int), float three);
21859 ^~~~~~~~~~~~~~~~~
21860
21861 */
21862 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21863 ? declarator->id_loc
21864 : decl_spec_token_start->location);
21865 location_t param_loc = make_location (caret_loc,
21866 decl_spec_token_start->location,
21867 input_location);
21868
21869 return make_parameter_declarator (&decl_specifiers,
21870 declarator,
21871 default_argument,
21872 param_loc,
21873 template_parameter_pack_p);
21874 }
21875
21876 /* Parse a default argument and return it.
21877
21878 TEMPLATE_PARM_P is true if this is a default argument for a
21879 non-type template parameter. */
21880 static tree
21881 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21882 {
21883 tree default_argument = NULL_TREE;
21884 bool saved_greater_than_is_operator_p;
21885 bool saved_local_variables_forbidden_p;
21886 bool non_constant_p, is_direct_init;
21887
21888 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21889 set correctly. */
21890 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21891 parser->greater_than_is_operator_p = !template_parm_p;
21892 /* Local variable names (and the `this' keyword) may not
21893 appear in a default argument. */
21894 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21895 parser->local_variables_forbidden_p = true;
21896 /* Parse the assignment-expression. */
21897 if (template_parm_p)
21898 push_deferring_access_checks (dk_no_deferred);
21899 tree saved_class_ptr = NULL_TREE;
21900 tree saved_class_ref = NULL_TREE;
21901 /* The "this" pointer is not valid in a default argument. */
21902 if (cfun)
21903 {
21904 saved_class_ptr = current_class_ptr;
21905 cp_function_chain->x_current_class_ptr = NULL_TREE;
21906 saved_class_ref = current_class_ref;
21907 cp_function_chain->x_current_class_ref = NULL_TREE;
21908 }
21909 default_argument
21910 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21911 /* Restore the "this" pointer. */
21912 if (cfun)
21913 {
21914 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21915 cp_function_chain->x_current_class_ref = saved_class_ref;
21916 }
21917 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21918 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21919 if (template_parm_p)
21920 pop_deferring_access_checks ();
21921 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21922 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21923
21924 return default_argument;
21925 }
21926
21927 /* Parse a function-body.
21928
21929 function-body:
21930 compound_statement */
21931
21932 static void
21933 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21934 {
21935 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21936 ? BCS_TRY_BLOCK : BCS_NORMAL),
21937 true);
21938 }
21939
21940 /* Parse a ctor-initializer-opt followed by a function-body. Return
21941 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21942 is true we are parsing a function-try-block. */
21943
21944 static void
21945 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21946 bool in_function_try_block)
21947 {
21948 tree body, list;
21949 const bool check_body_p =
21950 DECL_CONSTRUCTOR_P (current_function_decl)
21951 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21952 tree last = NULL;
21953
21954 /* Begin the function body. */
21955 body = begin_function_body ();
21956 /* Parse the optional ctor-initializer. */
21957 cp_parser_ctor_initializer_opt (parser);
21958
21959 /* If we're parsing a constexpr constructor definition, we need
21960 to check that the constructor body is indeed empty. However,
21961 before we get to cp_parser_function_body lot of junk has been
21962 generated, so we can't just check that we have an empty block.
21963 Rather we take a snapshot of the outermost block, and check whether
21964 cp_parser_function_body changed its state. */
21965 if (check_body_p)
21966 {
21967 list = cur_stmt_list;
21968 if (STATEMENT_LIST_TAIL (list))
21969 last = STATEMENT_LIST_TAIL (list)->stmt;
21970 }
21971 /* Parse the function-body. */
21972 cp_parser_function_body (parser, in_function_try_block);
21973 if (check_body_p)
21974 check_constexpr_ctor_body (last, list, /*complain=*/true);
21975 /* Finish the function body. */
21976 finish_function_body (body);
21977 }
21978
21979 /* Parse an initializer.
21980
21981 initializer:
21982 = initializer-clause
21983 ( expression-list )
21984
21985 Returns an expression representing the initializer. If no
21986 initializer is present, NULL_TREE is returned.
21987
21988 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21989 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21990 set to TRUE if there is no initializer present. If there is an
21991 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21992 is set to true; otherwise it is set to false. */
21993
21994 static tree
21995 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21996 bool* non_constant_p, bool subexpression_p)
21997 {
21998 cp_token *token;
21999 tree init;
22000
22001 /* Peek at the next token. */
22002 token = cp_lexer_peek_token (parser->lexer);
22003
22004 /* Let our caller know whether or not this initializer was
22005 parenthesized. */
22006 *is_direct_init = (token->type != CPP_EQ);
22007 /* Assume that the initializer is constant. */
22008 *non_constant_p = false;
22009
22010 if (token->type == CPP_EQ)
22011 {
22012 /* Consume the `='. */
22013 cp_lexer_consume_token (parser->lexer);
22014 /* Parse the initializer-clause. */
22015 init = cp_parser_initializer_clause (parser, non_constant_p);
22016 }
22017 else if (token->type == CPP_OPEN_PAREN)
22018 {
22019 vec<tree, va_gc> *vec;
22020 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22021 /*cast_p=*/false,
22022 /*allow_expansion_p=*/true,
22023 non_constant_p);
22024 if (vec == NULL)
22025 return error_mark_node;
22026 init = build_tree_list_vec (vec);
22027 release_tree_vector (vec);
22028 }
22029 else if (token->type == CPP_OPEN_BRACE)
22030 {
22031 cp_lexer_set_source_position (parser->lexer);
22032 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22033 init = cp_parser_braced_list (parser, non_constant_p);
22034 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22035 }
22036 else
22037 {
22038 /* Anything else is an error. */
22039 cp_parser_error (parser, "expected initializer");
22040 init = error_mark_node;
22041 }
22042
22043 if (!subexpression_p && check_for_bare_parameter_packs (init))
22044 init = error_mark_node;
22045
22046 return init;
22047 }
22048
22049 /* Parse an initializer-clause.
22050
22051 initializer-clause:
22052 assignment-expression
22053 braced-init-list
22054
22055 Returns an expression representing the initializer.
22056
22057 If the `assignment-expression' production is used the value
22058 returned is simply a representation for the expression.
22059
22060 Otherwise, calls cp_parser_braced_list. */
22061
22062 static cp_expr
22063 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22064 {
22065 cp_expr initializer;
22066
22067 /* Assume the expression is constant. */
22068 *non_constant_p = false;
22069
22070 /* If it is not a `{', then we are looking at an
22071 assignment-expression. */
22072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22073 {
22074 initializer
22075 = cp_parser_constant_expression (parser,
22076 /*allow_non_constant_p=*/true,
22077 non_constant_p);
22078 }
22079 else
22080 initializer = cp_parser_braced_list (parser, non_constant_p);
22081
22082 return initializer;
22083 }
22084
22085 /* Parse a brace-enclosed initializer list.
22086
22087 braced-init-list:
22088 { initializer-list , [opt] }
22089 { designated-initializer-list , [opt] }
22090 { }
22091
22092 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22093 the elements of the initializer-list (or NULL, if the last
22094 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22095 NULL_TREE. There is no way to detect whether or not the optional
22096 trailing `,' was provided. NON_CONSTANT_P is as for
22097 cp_parser_initializer. */
22098
22099 static cp_expr
22100 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22101 {
22102 tree initializer;
22103 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22104
22105 /* Consume the `{' token. */
22106 matching_braces braces;
22107 braces.require_open (parser);
22108 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22109 initializer = make_node (CONSTRUCTOR);
22110 /* If it's not a `}', then there is a non-trivial initializer. */
22111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22112 {
22113 /* Parse the initializer list. */
22114 CONSTRUCTOR_ELTS (initializer)
22115 = cp_parser_initializer_list (parser, non_constant_p);
22116 /* A trailing `,' token is allowed. */
22117 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22118 cp_lexer_consume_token (parser->lexer);
22119 }
22120 else
22121 *non_constant_p = false;
22122 /* Now, there should be a trailing `}'. */
22123 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22124 braces.require_close (parser);
22125 TREE_TYPE (initializer) = init_list_type_node;
22126
22127 cp_expr result (initializer);
22128 /* Build a location of the form:
22129 { ... }
22130 ^~~~~~~
22131 with caret==start at the open brace, finish at the close brace. */
22132 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22133 result.set_location (combined_loc);
22134 return result;
22135 }
22136
22137 /* Consume tokens up to, and including, the next non-nested closing `]'.
22138 Returns true iff we found a closing `]'. */
22139
22140 static bool
22141 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22142 {
22143 unsigned square_depth = 0;
22144
22145 while (true)
22146 {
22147 cp_token * token = cp_lexer_peek_token (parser->lexer);
22148
22149 switch (token->type)
22150 {
22151 case CPP_EOF:
22152 case CPP_PRAGMA_EOL:
22153 /* If we've run out of tokens, then there is no closing `]'. */
22154 return false;
22155
22156 case CPP_OPEN_SQUARE:
22157 ++square_depth;
22158 break;
22159
22160 case CPP_CLOSE_SQUARE:
22161 if (!square_depth--)
22162 {
22163 cp_lexer_consume_token (parser->lexer);
22164 return true;
22165 }
22166 break;
22167
22168 default:
22169 break;
22170 }
22171
22172 /* Consume the token. */
22173 cp_lexer_consume_token (parser->lexer);
22174 }
22175 }
22176
22177 /* Return true if we are looking at an array-designator, false otherwise. */
22178
22179 static bool
22180 cp_parser_array_designator_p (cp_parser *parser)
22181 {
22182 /* Consume the `['. */
22183 cp_lexer_consume_token (parser->lexer);
22184
22185 cp_lexer_save_tokens (parser->lexer);
22186
22187 /* Skip tokens until the next token is a closing square bracket.
22188 If we find the closing `]', and the next token is a `=', then
22189 we are looking at an array designator. */
22190 bool array_designator_p
22191 = (cp_parser_skip_to_closing_square_bracket (parser)
22192 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22193
22194 /* Roll back the tokens we skipped. */
22195 cp_lexer_rollback_tokens (parser->lexer);
22196
22197 return array_designator_p;
22198 }
22199
22200 /* Parse an initializer-list.
22201
22202 initializer-list:
22203 initializer-clause ... [opt]
22204 initializer-list , initializer-clause ... [opt]
22205
22206 C++2A Extension:
22207
22208 designated-initializer-list:
22209 designated-initializer-clause
22210 designated-initializer-list , designated-initializer-clause
22211
22212 designated-initializer-clause:
22213 designator brace-or-equal-initializer
22214
22215 designator:
22216 . identifier
22217
22218 GNU Extension:
22219
22220 initializer-list:
22221 designation initializer-clause ...[opt]
22222 initializer-list , designation initializer-clause ...[opt]
22223
22224 designation:
22225 . identifier =
22226 identifier :
22227 [ constant-expression ] =
22228
22229 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22230 for the initializer. If the INDEX of the elt is non-NULL, it is the
22231 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22232 as for cp_parser_initializer. */
22233
22234 static vec<constructor_elt, va_gc> *
22235 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22236 {
22237 vec<constructor_elt, va_gc> *v = NULL;
22238 bool first_p = true;
22239 tree first_designator = NULL_TREE;
22240
22241 /* Assume all of the expressions are constant. */
22242 *non_constant_p = false;
22243
22244 /* Parse the rest of the list. */
22245 while (true)
22246 {
22247 cp_token *token;
22248 tree designator;
22249 tree initializer;
22250 bool clause_non_constant_p;
22251 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22252
22253 /* Handle the C++2A syntax, '. id ='. */
22254 if ((cxx_dialect >= cxx2a
22255 || cp_parser_allow_gnu_extensions_p (parser))
22256 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22257 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22258 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22259 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22260 == CPP_OPEN_BRACE)))
22261 {
22262 if (cxx_dialect < cxx2a)
22263 pedwarn (loc, OPT_Wpedantic,
22264 "C++ designated initializers only available with "
22265 "-std=c++2a or -std=gnu++2a");
22266 /* Consume the `.'. */
22267 cp_lexer_consume_token (parser->lexer);
22268 /* Consume the identifier. */
22269 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22270 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22271 /* Consume the `='. */
22272 cp_lexer_consume_token (parser->lexer);
22273 }
22274 /* Also, if the next token is an identifier and the following one is a
22275 colon, we are looking at the GNU designated-initializer
22276 syntax. */
22277 else if (cp_parser_allow_gnu_extensions_p (parser)
22278 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22279 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22280 == CPP_COLON))
22281 {
22282 /* Warn the user that they are using an extension. */
22283 pedwarn (loc, OPT_Wpedantic,
22284 "ISO C++ does not allow GNU designated initializers");
22285 /* Consume the identifier. */
22286 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22287 /* Consume the `:'. */
22288 cp_lexer_consume_token (parser->lexer);
22289 }
22290 /* Also handle C99 array designators, '[ const ] ='. */
22291 else if (cp_parser_allow_gnu_extensions_p (parser)
22292 && !c_dialect_objc ()
22293 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22294 {
22295 /* In C++11, [ could start a lambda-introducer. */
22296 bool non_const = false;
22297
22298 cp_parser_parse_tentatively (parser);
22299
22300 if (!cp_parser_array_designator_p (parser))
22301 {
22302 cp_parser_simulate_error (parser);
22303 designator = NULL_TREE;
22304 }
22305 else
22306 {
22307 designator = cp_parser_constant_expression (parser, true,
22308 &non_const);
22309 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22310 cp_parser_require (parser, CPP_EQ, RT_EQ);
22311 }
22312
22313 if (!cp_parser_parse_definitely (parser))
22314 designator = NULL_TREE;
22315 else if (non_const
22316 && (!require_potential_rvalue_constant_expression
22317 (designator)))
22318 designator = NULL_TREE;
22319 if (designator)
22320 /* Warn the user that they are using an extension. */
22321 pedwarn (loc, OPT_Wpedantic,
22322 "ISO C++ does not allow C99 designated initializers");
22323 }
22324 else
22325 designator = NULL_TREE;
22326
22327 if (first_p)
22328 {
22329 first_designator = designator;
22330 first_p = false;
22331 }
22332 else if (cxx_dialect >= cxx2a
22333 && first_designator != error_mark_node
22334 && (!first_designator != !designator))
22335 {
22336 error_at (loc, "either all initializer clauses should be designated "
22337 "or none of them should be");
22338 first_designator = error_mark_node;
22339 }
22340 else if (cxx_dialect < cxx2a && !first_designator)
22341 first_designator = designator;
22342
22343 /* Parse the initializer. */
22344 initializer = cp_parser_initializer_clause (parser,
22345 &clause_non_constant_p);
22346 /* If any clause is non-constant, so is the entire initializer. */
22347 if (clause_non_constant_p)
22348 *non_constant_p = true;
22349
22350 /* If we have an ellipsis, this is an initializer pack
22351 expansion. */
22352 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22353 {
22354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22355
22356 /* Consume the `...'. */
22357 cp_lexer_consume_token (parser->lexer);
22358
22359 if (designator && cxx_dialect >= cxx2a)
22360 error_at (loc,
22361 "%<...%> not allowed in designated initializer list");
22362
22363 /* Turn the initializer into an initializer expansion. */
22364 initializer = make_pack_expansion (initializer);
22365 }
22366
22367 /* Add it to the vector. */
22368 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22369
22370 /* If the next token is not a comma, we have reached the end of
22371 the list. */
22372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22373 break;
22374
22375 /* Peek at the next token. */
22376 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22377 /* If the next token is a `}', then we're still done. An
22378 initializer-clause can have a trailing `,' after the
22379 initializer-list and before the closing `}'. */
22380 if (token->type == CPP_CLOSE_BRACE)
22381 break;
22382
22383 /* Consume the `,' token. */
22384 cp_lexer_consume_token (parser->lexer);
22385 }
22386
22387 /* The same identifier shall not appear in multiple designators
22388 of a designated-initializer-list. */
22389 if (first_designator)
22390 {
22391 unsigned int i;
22392 tree designator, val;
22393 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22394 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22395 {
22396 if (IDENTIFIER_MARKED (designator))
22397 {
22398 error_at (cp_expr_loc_or_loc (val, input_location),
22399 "%<.%s%> designator used multiple times in "
22400 "the same initializer list",
22401 IDENTIFIER_POINTER (designator));
22402 (*v)[i].index = NULL_TREE;
22403 }
22404 else
22405 IDENTIFIER_MARKED (designator) = 1;
22406 }
22407 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22408 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22409 IDENTIFIER_MARKED (designator) = 0;
22410 }
22411
22412 return v;
22413 }
22414
22415 /* Classes [gram.class] */
22416
22417 /* Parse a class-name.
22418
22419 class-name:
22420 identifier
22421 template-id
22422
22423 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22424 to indicate that names looked up in dependent types should be
22425 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22426 keyword has been used to indicate that the name that appears next
22427 is a template. TAG_TYPE indicates the explicit tag given before
22428 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22429 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22430 is the class being defined in a class-head. If ENUM_OK is TRUE,
22431 enum-names are also accepted.
22432
22433 Returns the TYPE_DECL representing the class. */
22434
22435 static tree
22436 cp_parser_class_name (cp_parser *parser,
22437 bool typename_keyword_p,
22438 bool template_keyword_p,
22439 enum tag_types tag_type,
22440 bool check_dependency_p,
22441 bool class_head_p,
22442 bool is_declaration,
22443 bool enum_ok)
22444 {
22445 tree decl;
22446 tree scope;
22447 bool typename_p;
22448 cp_token *token;
22449 tree identifier = NULL_TREE;
22450
22451 /* All class-names start with an identifier. */
22452 token = cp_lexer_peek_token (parser->lexer);
22453 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22454 {
22455 cp_parser_error (parser, "expected class-name");
22456 return error_mark_node;
22457 }
22458
22459 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22460 to a template-id, so we save it here. */
22461 scope = parser->scope;
22462 if (scope == error_mark_node)
22463 return error_mark_node;
22464
22465 /* Any name names a type if we're following the `typename' keyword
22466 in a qualified name where the enclosing scope is type-dependent. */
22467 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22468 && dependent_type_p (scope));
22469 /* Handle the common case (an identifier, but not a template-id)
22470 efficiently. */
22471 if (token->type == CPP_NAME
22472 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22473 {
22474 cp_token *identifier_token;
22475 bool ambiguous_p;
22476
22477 /* Look for the identifier. */
22478 identifier_token = cp_lexer_peek_token (parser->lexer);
22479 ambiguous_p = identifier_token->error_reported;
22480 identifier = cp_parser_identifier (parser);
22481 /* If the next token isn't an identifier, we are certainly not
22482 looking at a class-name. */
22483 if (identifier == error_mark_node)
22484 decl = error_mark_node;
22485 /* If we know this is a type-name, there's no need to look it
22486 up. */
22487 else if (typename_p)
22488 decl = identifier;
22489 else
22490 {
22491 tree ambiguous_decls;
22492 /* If we already know that this lookup is ambiguous, then
22493 we've already issued an error message; there's no reason
22494 to check again. */
22495 if (ambiguous_p)
22496 {
22497 cp_parser_simulate_error (parser);
22498 return error_mark_node;
22499 }
22500 /* If the next token is a `::', then the name must be a type
22501 name.
22502
22503 [basic.lookup.qual]
22504
22505 During the lookup for a name preceding the :: scope
22506 resolution operator, object, function, and enumerator
22507 names are ignored. */
22508 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22509 tag_type = scope_type;
22510 /* Look up the name. */
22511 decl = cp_parser_lookup_name (parser, identifier,
22512 tag_type,
22513 /*is_template=*/false,
22514 /*is_namespace=*/false,
22515 check_dependency_p,
22516 &ambiguous_decls,
22517 identifier_token->location);
22518 if (ambiguous_decls)
22519 {
22520 if (cp_parser_parsing_tentatively (parser))
22521 cp_parser_simulate_error (parser);
22522 return error_mark_node;
22523 }
22524 }
22525 }
22526 else
22527 {
22528 /* Try a template-id. */
22529 decl = cp_parser_template_id (parser, template_keyword_p,
22530 check_dependency_p,
22531 tag_type,
22532 is_declaration);
22533 if (decl == error_mark_node)
22534 return error_mark_node;
22535 }
22536
22537 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22538
22539 /* If this is a typename, create a TYPENAME_TYPE. */
22540 if (typename_p && decl != error_mark_node)
22541 {
22542 decl = make_typename_type (scope, decl, typename_type,
22543 /*complain=*/tf_error);
22544 if (decl != error_mark_node)
22545 decl = TYPE_NAME (decl);
22546 }
22547
22548 decl = strip_using_decl (decl);
22549
22550 /* Check to see that it is really the name of a class. */
22551 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22552 && identifier_p (TREE_OPERAND (decl, 0))
22553 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22554 /* Situations like this:
22555
22556 template <typename T> struct A {
22557 typename T::template X<int>::I i;
22558 };
22559
22560 are problematic. Is `T::template X<int>' a class-name? The
22561 standard does not seem to be definitive, but there is no other
22562 valid interpretation of the following `::'. Therefore, those
22563 names are considered class-names. */
22564 {
22565 decl = make_typename_type (scope, decl, tag_type, tf_error);
22566 if (decl != error_mark_node)
22567 decl = TYPE_NAME (decl);
22568 }
22569 else if (TREE_CODE (decl) != TYPE_DECL
22570 || TREE_TYPE (decl) == error_mark_node
22571 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22572 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22573 /* In Objective-C 2.0, a classname followed by '.' starts a
22574 dot-syntax expression, and it's not a type-name. */
22575 || (c_dialect_objc ()
22576 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22577 && objc_is_class_name (decl)))
22578 decl = error_mark_node;
22579
22580 if (decl == error_mark_node)
22581 cp_parser_error (parser, "expected class-name");
22582 else if (identifier && !parser->scope)
22583 maybe_note_name_used_in_class (identifier, decl);
22584
22585 return decl;
22586 }
22587
22588 /* Parse a class-specifier.
22589
22590 class-specifier:
22591 class-head { member-specification [opt] }
22592
22593 Returns the TREE_TYPE representing the class. */
22594
22595 static tree
22596 cp_parser_class_specifier_1 (cp_parser* parser)
22597 {
22598 tree type;
22599 tree attributes = NULL_TREE;
22600 bool nested_name_specifier_p;
22601 unsigned saved_num_template_parameter_lists;
22602 bool saved_in_function_body;
22603 unsigned char in_statement;
22604 bool in_switch_statement_p;
22605 bool saved_in_unbraced_linkage_specification_p;
22606 tree old_scope = NULL_TREE;
22607 tree scope = NULL_TREE;
22608 cp_token *closing_brace;
22609
22610 push_deferring_access_checks (dk_no_deferred);
22611
22612 /* Parse the class-head. */
22613 type = cp_parser_class_head (parser,
22614 &nested_name_specifier_p);
22615 /* If the class-head was a semantic disaster, skip the entire body
22616 of the class. */
22617 if (!type)
22618 {
22619 cp_parser_skip_to_end_of_block_or_statement (parser);
22620 pop_deferring_access_checks ();
22621 return error_mark_node;
22622 }
22623
22624 /* Look for the `{'. */
22625 matching_braces braces;
22626 if (!braces.require_open (parser))
22627 {
22628 pop_deferring_access_checks ();
22629 return error_mark_node;
22630 }
22631
22632 cp_ensure_no_omp_declare_simd (parser);
22633 cp_ensure_no_oacc_routine (parser);
22634
22635 /* Issue an error message if type-definitions are forbidden here. */
22636 cp_parser_check_type_definition (parser);
22637 /* Remember that we are defining one more class. */
22638 ++parser->num_classes_being_defined;
22639 /* Inside the class, surrounding template-parameter-lists do not
22640 apply. */
22641 saved_num_template_parameter_lists
22642 = parser->num_template_parameter_lists;
22643 parser->num_template_parameter_lists = 0;
22644 /* We are not in a function body. */
22645 saved_in_function_body = parser->in_function_body;
22646 parser->in_function_body = false;
22647 /* Or in a loop. */
22648 in_statement = parser->in_statement;
22649 parser->in_statement = 0;
22650 /* Or in a switch. */
22651 in_switch_statement_p = parser->in_switch_statement_p;
22652 parser->in_switch_statement_p = false;
22653 /* We are not immediately inside an extern "lang" block. */
22654 saved_in_unbraced_linkage_specification_p
22655 = parser->in_unbraced_linkage_specification_p;
22656 parser->in_unbraced_linkage_specification_p = false;
22657
22658 // Associate constraints with the type.
22659 if (flag_concepts)
22660 type = associate_classtype_constraints (type);
22661
22662 /* Start the class. */
22663 if (nested_name_specifier_p)
22664 {
22665 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22666 old_scope = push_inner_scope (scope);
22667 }
22668 type = begin_class_definition (type);
22669
22670 if (type == error_mark_node)
22671 /* If the type is erroneous, skip the entire body of the class. */
22672 cp_parser_skip_to_closing_brace (parser);
22673 else
22674 /* Parse the member-specification. */
22675 cp_parser_member_specification_opt (parser);
22676
22677 /* Look for the trailing `}'. */
22678 closing_brace = braces.require_close (parser);
22679 /* Look for trailing attributes to apply to this class. */
22680 if (cp_parser_allow_gnu_extensions_p (parser))
22681 attributes = cp_parser_gnu_attributes_opt (parser);
22682 if (type != error_mark_node)
22683 type = finish_struct (type, attributes);
22684 if (nested_name_specifier_p)
22685 pop_inner_scope (old_scope, scope);
22686
22687 /* We've finished a type definition. Check for the common syntax
22688 error of forgetting a semicolon after the definition. We need to
22689 be careful, as we can't just check for not-a-semicolon and be done
22690 with it; the user might have typed:
22691
22692 class X { } c = ...;
22693 class X { } *p = ...;
22694
22695 and so forth. Instead, enumerate all the possible tokens that
22696 might follow this production; if we don't see one of them, then
22697 complain and silently insert the semicolon. */
22698 {
22699 cp_token *token = cp_lexer_peek_token (parser->lexer);
22700 bool want_semicolon = true;
22701
22702 if (cp_next_tokens_can_be_std_attribute_p (parser))
22703 /* Don't try to parse c++11 attributes here. As per the
22704 grammar, that should be a task for
22705 cp_parser_decl_specifier_seq. */
22706 want_semicolon = false;
22707
22708 switch (token->type)
22709 {
22710 case CPP_NAME:
22711 case CPP_SEMICOLON:
22712 case CPP_MULT:
22713 case CPP_AND:
22714 case CPP_OPEN_PAREN:
22715 case CPP_CLOSE_PAREN:
22716 case CPP_COMMA:
22717 want_semicolon = false;
22718 break;
22719
22720 /* While it's legal for type qualifiers and storage class
22721 specifiers to follow type definitions in the grammar, only
22722 compiler testsuites contain code like that. Assume that if
22723 we see such code, then what we're really seeing is a case
22724 like:
22725
22726 class X { }
22727 const <type> var = ...;
22728
22729 or
22730
22731 class Y { }
22732 static <type> func (...) ...
22733
22734 i.e. the qualifier or specifier applies to the next
22735 declaration. To do so, however, we need to look ahead one
22736 more token to see if *that* token is a type specifier.
22737
22738 This code could be improved to handle:
22739
22740 class Z { }
22741 static const <type> var = ...; */
22742 case CPP_KEYWORD:
22743 if (keyword_is_decl_specifier (token->keyword))
22744 {
22745 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22746
22747 /* Handling user-defined types here would be nice, but very
22748 tricky. */
22749 want_semicolon
22750 = (lookahead->type == CPP_KEYWORD
22751 && keyword_begins_type_specifier (lookahead->keyword));
22752 }
22753 break;
22754 default:
22755 break;
22756 }
22757
22758 /* If we don't have a type, then something is very wrong and we
22759 shouldn't try to do anything clever. Likewise for not seeing the
22760 closing brace. */
22761 if (closing_brace && TYPE_P (type) && want_semicolon)
22762 {
22763 /* Locate the closing brace. */
22764 cp_token_position prev
22765 = cp_lexer_previous_token_position (parser->lexer);
22766 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22767 location_t loc = prev_token->location;
22768
22769 /* We want to suggest insertion of a ';' immediately *after* the
22770 closing brace, so, if we can, offset the location by 1 column. */
22771 location_t next_loc = loc;
22772 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22773 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22774
22775 rich_location richloc (line_table, next_loc);
22776
22777 /* If we successfully offset the location, suggest the fix-it. */
22778 if (next_loc != loc)
22779 richloc.add_fixit_insert_before (next_loc, ";");
22780
22781 if (CLASSTYPE_DECLARED_CLASS (type))
22782 error_at (&richloc,
22783 "expected %<;%> after class definition");
22784 else if (TREE_CODE (type) == RECORD_TYPE)
22785 error_at (&richloc,
22786 "expected %<;%> after struct definition");
22787 else if (TREE_CODE (type) == UNION_TYPE)
22788 error_at (&richloc,
22789 "expected %<;%> after union definition");
22790 else
22791 gcc_unreachable ();
22792
22793 /* Unget one token and smash it to look as though we encountered
22794 a semicolon in the input stream. */
22795 cp_lexer_set_token_position (parser->lexer, prev);
22796 token = cp_lexer_peek_token (parser->lexer);
22797 token->type = CPP_SEMICOLON;
22798 token->keyword = RID_MAX;
22799 }
22800 }
22801
22802 /* If this class is not itself within the scope of another class,
22803 then we need to parse the bodies of all of the queued function
22804 definitions. Note that the queued functions defined in a class
22805 are not always processed immediately following the
22806 class-specifier for that class. Consider:
22807
22808 struct A {
22809 struct B { void f() { sizeof (A); } };
22810 };
22811
22812 If `f' were processed before the processing of `A' were
22813 completed, there would be no way to compute the size of `A'.
22814 Note that the nesting we are interested in here is lexical --
22815 not the semantic nesting given by TYPE_CONTEXT. In particular,
22816 for:
22817
22818 struct A { struct B; };
22819 struct A::B { void f() { } };
22820
22821 there is no need to delay the parsing of `A::B::f'. */
22822 if (--parser->num_classes_being_defined == 0)
22823 {
22824 tree decl;
22825 tree class_type = NULL_TREE;
22826 tree pushed_scope = NULL_TREE;
22827 unsigned ix;
22828 cp_default_arg_entry *e;
22829 tree save_ccp, save_ccr;
22830
22831 if (any_erroneous_template_args_p (type))
22832 {
22833 /* Skip default arguments, NSDMIs, etc, in order to improve
22834 error recovery (c++/71169, c++/71832). */
22835 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22836 vec_safe_truncate (unparsed_nsdmis, 0);
22837 vec_safe_truncate (unparsed_classes, 0);
22838 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22839 }
22840
22841 /* In a first pass, parse default arguments to the functions.
22842 Then, in a second pass, parse the bodies of the functions.
22843 This two-phased approach handles cases like:
22844
22845 struct S {
22846 void f() { g(); }
22847 void g(int i = 3);
22848 };
22849
22850 */
22851 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22852 {
22853 decl = e->decl;
22854 /* If there are default arguments that have not yet been processed,
22855 take care of them now. */
22856 if (class_type != e->class_type)
22857 {
22858 if (pushed_scope)
22859 pop_scope (pushed_scope);
22860 class_type = e->class_type;
22861 pushed_scope = push_scope (class_type);
22862 }
22863 /* Make sure that any template parameters are in scope. */
22864 maybe_begin_member_template_processing (decl);
22865 /* Parse the default argument expressions. */
22866 cp_parser_late_parsing_default_args (parser, decl);
22867 /* Remove any template parameters from the symbol table. */
22868 maybe_end_member_template_processing ();
22869 }
22870 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22871 /* Now parse any NSDMIs. */
22872 save_ccp = current_class_ptr;
22873 save_ccr = current_class_ref;
22874 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22875 {
22876 if (class_type != DECL_CONTEXT (decl))
22877 {
22878 if (pushed_scope)
22879 pop_scope (pushed_scope);
22880 class_type = DECL_CONTEXT (decl);
22881 pushed_scope = push_scope (class_type);
22882 }
22883 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22884 cp_parser_late_parsing_nsdmi (parser, decl);
22885 }
22886 vec_safe_truncate (unparsed_nsdmis, 0);
22887 current_class_ptr = save_ccp;
22888 current_class_ref = save_ccr;
22889 if (pushed_scope)
22890 pop_scope (pushed_scope);
22891
22892 /* Now do some post-NSDMI bookkeeping. */
22893 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22894 after_nsdmi_defaulted_late_checks (class_type);
22895 vec_safe_truncate (unparsed_classes, 0);
22896 after_nsdmi_defaulted_late_checks (type);
22897
22898 /* Now parse the body of the functions. */
22899 if (flag_openmp)
22900 {
22901 /* OpenMP UDRs need to be parsed before all other functions. */
22902 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22903 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22904 cp_parser_late_parsing_for_member (parser, decl);
22905 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22906 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22907 cp_parser_late_parsing_for_member (parser, decl);
22908 }
22909 else
22910 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22911 cp_parser_late_parsing_for_member (parser, decl);
22912 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22913 }
22914 else
22915 vec_safe_push (unparsed_classes, type);
22916
22917 /* Put back any saved access checks. */
22918 pop_deferring_access_checks ();
22919
22920 /* Restore saved state. */
22921 parser->in_switch_statement_p = in_switch_statement_p;
22922 parser->in_statement = in_statement;
22923 parser->in_function_body = saved_in_function_body;
22924 parser->num_template_parameter_lists
22925 = saved_num_template_parameter_lists;
22926 parser->in_unbraced_linkage_specification_p
22927 = saved_in_unbraced_linkage_specification_p;
22928
22929 return type;
22930 }
22931
22932 static tree
22933 cp_parser_class_specifier (cp_parser* parser)
22934 {
22935 tree ret;
22936 timevar_push (TV_PARSE_STRUCT);
22937 ret = cp_parser_class_specifier_1 (parser);
22938 timevar_pop (TV_PARSE_STRUCT);
22939 return ret;
22940 }
22941
22942 /* Parse a class-head.
22943
22944 class-head:
22945 class-key identifier [opt] base-clause [opt]
22946 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22947 class-key nested-name-specifier [opt] template-id
22948 base-clause [opt]
22949
22950 class-virt-specifier:
22951 final
22952
22953 GNU Extensions:
22954 class-key attributes identifier [opt] base-clause [opt]
22955 class-key attributes nested-name-specifier identifier base-clause [opt]
22956 class-key attributes nested-name-specifier [opt] template-id
22957 base-clause [opt]
22958
22959 Upon return BASES is initialized to the list of base classes (or
22960 NULL, if there are none) in the same form returned by
22961 cp_parser_base_clause.
22962
22963 Returns the TYPE of the indicated class. Sets
22964 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22965 involving a nested-name-specifier was used, and FALSE otherwise.
22966
22967 Returns error_mark_node if this is not a class-head.
22968
22969 Returns NULL_TREE if the class-head is syntactically valid, but
22970 semantically invalid in a way that means we should skip the entire
22971 body of the class. */
22972
22973 static tree
22974 cp_parser_class_head (cp_parser* parser,
22975 bool* nested_name_specifier_p)
22976 {
22977 tree nested_name_specifier;
22978 enum tag_types class_key;
22979 tree id = NULL_TREE;
22980 tree type = NULL_TREE;
22981 tree attributes;
22982 tree bases;
22983 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22984 bool template_id_p = false;
22985 bool qualified_p = false;
22986 bool invalid_nested_name_p = false;
22987 bool invalid_explicit_specialization_p = false;
22988 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22989 tree pushed_scope = NULL_TREE;
22990 unsigned num_templates;
22991 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22992 /* Assume no nested-name-specifier will be present. */
22993 *nested_name_specifier_p = false;
22994 /* Assume no template parameter lists will be used in defining the
22995 type. */
22996 num_templates = 0;
22997 parser->colon_corrects_to_scope_p = false;
22998
22999 /* Look for the class-key. */
23000 class_key = cp_parser_class_key (parser);
23001 if (class_key == none_type)
23002 return error_mark_node;
23003
23004 location_t class_head_start_location = input_location;
23005
23006 /* Parse the attributes. */
23007 attributes = cp_parser_attributes_opt (parser);
23008
23009 /* If the next token is `::', that is invalid -- but sometimes
23010 people do try to write:
23011
23012 struct ::S {};
23013
23014 Handle this gracefully by accepting the extra qualifier, and then
23015 issuing an error about it later if this really is a
23016 class-head. If it turns out just to be an elaborated type
23017 specifier, remain silent. */
23018 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23019 qualified_p = true;
23020
23021 push_deferring_access_checks (dk_no_check);
23022
23023 /* Determine the name of the class. Begin by looking for an
23024 optional nested-name-specifier. */
23025 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23026 nested_name_specifier
23027 = cp_parser_nested_name_specifier_opt (parser,
23028 /*typename_keyword_p=*/false,
23029 /*check_dependency_p=*/false,
23030 /*type_p=*/true,
23031 /*is_declaration=*/false);
23032 /* If there was a nested-name-specifier, then there *must* be an
23033 identifier. */
23034
23035 cp_token *bad_template_keyword = NULL;
23036
23037 if (nested_name_specifier)
23038 {
23039 type_start_token = cp_lexer_peek_token (parser->lexer);
23040 /* Although the grammar says `identifier', it really means
23041 `class-name' or `template-name'. You are only allowed to
23042 define a class that has already been declared with this
23043 syntax.
23044
23045 The proposed resolution for Core Issue 180 says that wherever
23046 you see `class T::X' you should treat `X' as a type-name.
23047
23048 It is OK to define an inaccessible class; for example:
23049
23050 class A { class B; };
23051 class A::B {};
23052
23053 We do not know if we will see a class-name, or a
23054 template-name. We look for a class-name first, in case the
23055 class-name is a template-id; if we looked for the
23056 template-name first we would stop after the template-name. */
23057 cp_parser_parse_tentatively (parser);
23058 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23059 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23060 type = cp_parser_class_name (parser,
23061 /*typename_keyword_p=*/false,
23062 /*template_keyword_p=*/false,
23063 class_type,
23064 /*check_dependency_p=*/false,
23065 /*class_head_p=*/true,
23066 /*is_declaration=*/false);
23067 /* If that didn't work, ignore the nested-name-specifier. */
23068 if (!cp_parser_parse_definitely (parser))
23069 {
23070 invalid_nested_name_p = true;
23071 type_start_token = cp_lexer_peek_token (parser->lexer);
23072 id = cp_parser_identifier (parser);
23073 if (id == error_mark_node)
23074 id = NULL_TREE;
23075 }
23076 /* If we could not find a corresponding TYPE, treat this
23077 declaration like an unqualified declaration. */
23078 if (type == error_mark_node)
23079 nested_name_specifier = NULL_TREE;
23080 /* Otherwise, count the number of templates used in TYPE and its
23081 containing scopes. */
23082 else
23083 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23084 }
23085 /* Otherwise, the identifier is optional. */
23086 else
23087 {
23088 /* We don't know whether what comes next is a template-id,
23089 an identifier, or nothing at all. */
23090 cp_parser_parse_tentatively (parser);
23091 /* Check for a template-id. */
23092 type_start_token = cp_lexer_peek_token (parser->lexer);
23093 id = cp_parser_template_id (parser,
23094 /*template_keyword_p=*/false,
23095 /*check_dependency_p=*/true,
23096 class_key,
23097 /*is_declaration=*/true);
23098 /* If that didn't work, it could still be an identifier. */
23099 if (!cp_parser_parse_definitely (parser))
23100 {
23101 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23102 {
23103 type_start_token = cp_lexer_peek_token (parser->lexer);
23104 id = cp_parser_identifier (parser);
23105 }
23106 else
23107 id = NULL_TREE;
23108 }
23109 else
23110 {
23111 template_id_p = true;
23112 ++num_templates;
23113 }
23114 }
23115
23116 pop_deferring_access_checks ();
23117
23118 if (id)
23119 {
23120 cp_parser_check_for_invalid_template_id (parser, id,
23121 class_key,
23122 type_start_token->location);
23123 }
23124 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23125
23126 /* If it's not a `:' or a `{' then we can't really be looking at a
23127 class-head, since a class-head only appears as part of a
23128 class-specifier. We have to detect this situation before calling
23129 xref_tag, since that has irreversible side-effects. */
23130 if (!cp_parser_next_token_starts_class_definition_p (parser))
23131 {
23132 cp_parser_error (parser, "expected %<{%> or %<:%>");
23133 type = error_mark_node;
23134 goto out;
23135 }
23136
23137 /* At this point, we're going ahead with the class-specifier, even
23138 if some other problem occurs. */
23139 cp_parser_commit_to_tentative_parse (parser);
23140 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23141 {
23142 cp_parser_error (parser,
23143 "cannot specify %<override%> for a class");
23144 type = error_mark_node;
23145 goto out;
23146 }
23147 /* Issue the error about the overly-qualified name now. */
23148 if (qualified_p)
23149 {
23150 cp_parser_error (parser,
23151 "global qualification of class name is invalid");
23152 type = error_mark_node;
23153 goto out;
23154 }
23155 else if (invalid_nested_name_p)
23156 {
23157 cp_parser_error (parser,
23158 "qualified name does not name a class");
23159 type = error_mark_node;
23160 goto out;
23161 }
23162 else if (nested_name_specifier)
23163 {
23164 tree scope;
23165
23166 if (bad_template_keyword)
23167 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23168 keyword template shall not appear at the top level. */
23169 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23170 "keyword %<template%> not allowed in class-head-name");
23171
23172 /* Reject typedef-names in class heads. */
23173 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23174 {
23175 error_at (type_start_token->location,
23176 "invalid class name in declaration of %qD",
23177 type);
23178 type = NULL_TREE;
23179 goto done;
23180 }
23181
23182 /* Figure out in what scope the declaration is being placed. */
23183 scope = current_scope ();
23184 /* If that scope does not contain the scope in which the
23185 class was originally declared, the program is invalid. */
23186 if (scope && !is_ancestor (scope, nested_name_specifier))
23187 {
23188 if (at_namespace_scope_p ())
23189 error_at (type_start_token->location,
23190 "declaration of %qD in namespace %qD which does not "
23191 "enclose %qD",
23192 type, scope, nested_name_specifier);
23193 else
23194 error_at (type_start_token->location,
23195 "declaration of %qD in %qD which does not enclose %qD",
23196 type, scope, nested_name_specifier);
23197 type = NULL_TREE;
23198 goto done;
23199 }
23200 /* [dcl.meaning]
23201
23202 A declarator-id shall not be qualified except for the
23203 definition of a ... nested class outside of its class
23204 ... [or] the definition or explicit instantiation of a
23205 class member of a namespace outside of its namespace. */
23206 if (scope == nested_name_specifier)
23207 {
23208 permerror (nested_name_specifier_token_start->location,
23209 "extra qualification not allowed");
23210 nested_name_specifier = NULL_TREE;
23211 num_templates = 0;
23212 }
23213 }
23214 /* An explicit-specialization must be preceded by "template <>". If
23215 it is not, try to recover gracefully. */
23216 if (at_namespace_scope_p ()
23217 && parser->num_template_parameter_lists == 0
23218 && !processing_template_parmlist
23219 && template_id_p)
23220 {
23221 /* Build a location of this form:
23222 struct typename <ARGS>
23223 ^~~~~~~~~~~~~~~~~~~~~~
23224 with caret==start at the start token, and
23225 finishing at the end of the type. */
23226 location_t reported_loc
23227 = make_location (class_head_start_location,
23228 class_head_start_location,
23229 get_finish (type_start_token->location));
23230 rich_location richloc (line_table, reported_loc);
23231 richloc.add_fixit_insert_before (class_head_start_location,
23232 "template <> ");
23233 error_at (&richloc,
23234 "an explicit specialization must be preceded by"
23235 " %<template <>%>");
23236 invalid_explicit_specialization_p = true;
23237 /* Take the same action that would have been taken by
23238 cp_parser_explicit_specialization. */
23239 ++parser->num_template_parameter_lists;
23240 begin_specialization ();
23241 }
23242 /* There must be no "return" statements between this point and the
23243 end of this function; set "type "to the correct return value and
23244 use "goto done;" to return. */
23245 /* Make sure that the right number of template parameters were
23246 present. */
23247 if (!cp_parser_check_template_parameters (parser, num_templates,
23248 template_id_p,
23249 type_start_token->location,
23250 /*declarator=*/NULL))
23251 {
23252 /* If something went wrong, there is no point in even trying to
23253 process the class-definition. */
23254 type = NULL_TREE;
23255 goto done;
23256 }
23257
23258 /* Look up the type. */
23259 if (template_id_p)
23260 {
23261 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23262 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23263 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23264 {
23265 error_at (type_start_token->location,
23266 "function template %qD redeclared as a class template", id);
23267 type = error_mark_node;
23268 }
23269 else
23270 {
23271 type = TREE_TYPE (id);
23272 type = maybe_process_partial_specialization (type);
23273
23274 /* Check the scope while we still know whether or not we had a
23275 nested-name-specifier. */
23276 if (type != error_mark_node)
23277 check_unqualified_spec_or_inst (type, type_start_token->location);
23278 }
23279 if (nested_name_specifier)
23280 pushed_scope = push_scope (nested_name_specifier);
23281 }
23282 else if (nested_name_specifier)
23283 {
23284 tree class_type;
23285
23286 /* Given:
23287
23288 template <typename T> struct S { struct T };
23289 template <typename T> struct S<T>::T { };
23290
23291 we will get a TYPENAME_TYPE when processing the definition of
23292 `S::T'. We need to resolve it to the actual type before we
23293 try to define it. */
23294 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23295 {
23296 class_type = resolve_typename_type (TREE_TYPE (type),
23297 /*only_current_p=*/false);
23298 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23299 type = TYPE_NAME (class_type);
23300 else
23301 {
23302 cp_parser_error (parser, "could not resolve typename type");
23303 type = error_mark_node;
23304 }
23305 }
23306
23307 if (maybe_process_partial_specialization (TREE_TYPE (type))
23308 == error_mark_node)
23309 {
23310 type = NULL_TREE;
23311 goto done;
23312 }
23313
23314 class_type = current_class_type;
23315 /* Enter the scope indicated by the nested-name-specifier. */
23316 pushed_scope = push_scope (nested_name_specifier);
23317 /* Get the canonical version of this type. */
23318 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23319 /* Call push_template_decl if it seems like we should be defining a
23320 template either from the template headers or the type we're
23321 defining, so that we diagnose both extra and missing headers. */
23322 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23323 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23324 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23325 {
23326 type = push_template_decl (type);
23327 if (type == error_mark_node)
23328 {
23329 type = NULL_TREE;
23330 goto done;
23331 }
23332 }
23333
23334 type = TREE_TYPE (type);
23335 *nested_name_specifier_p = true;
23336 }
23337 else /* The name is not a nested name. */
23338 {
23339 /* If the class was unnamed, create a dummy name. */
23340 if (!id)
23341 id = make_anon_name ();
23342 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23343 ? ts_within_enclosing_non_class
23344 : ts_current);
23345 type = xref_tag (class_key, id, tag_scope,
23346 parser->num_template_parameter_lists);
23347 }
23348
23349 /* Indicate whether this class was declared as a `class' or as a
23350 `struct'. */
23351 if (TREE_CODE (type) == RECORD_TYPE)
23352 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23353 cp_parser_check_class_key (class_key, type);
23354
23355 /* If this type was already complete, and we see another definition,
23356 that's an error. */
23357 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23358 {
23359 error_at (type_start_token->location, "redefinition of %q#T",
23360 type);
23361 inform (location_of (type), "previous definition of %q#T",
23362 type);
23363 type = NULL_TREE;
23364 goto done;
23365 }
23366 else if (type == error_mark_node)
23367 type = NULL_TREE;
23368
23369 if (type)
23370 {
23371 /* Apply attributes now, before any use of the class as a template
23372 argument in its base list. */
23373 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23374 fixup_attribute_variants (type);
23375 }
23376
23377 /* We will have entered the scope containing the class; the names of
23378 base classes should be looked up in that context. For example:
23379
23380 struct A { struct B {}; struct C; };
23381 struct A::C : B {};
23382
23383 is valid. */
23384
23385 /* Get the list of base-classes, if there is one. */
23386 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23387 {
23388 /* PR59482: enter the class scope so that base-specifiers are looked
23389 up correctly. */
23390 if (type)
23391 pushclass (type);
23392 bases = cp_parser_base_clause (parser);
23393 /* PR59482: get out of the previously pushed class scope so that the
23394 subsequent pops pop the right thing. */
23395 if (type)
23396 popclass ();
23397 }
23398 else
23399 bases = NULL_TREE;
23400
23401 /* If we're really defining a class, process the base classes.
23402 If they're invalid, fail. */
23403 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23404 xref_basetypes (type, bases);
23405
23406 done:
23407 /* Leave the scope given by the nested-name-specifier. We will
23408 enter the class scope itself while processing the members. */
23409 if (pushed_scope)
23410 pop_scope (pushed_scope);
23411
23412 if (invalid_explicit_specialization_p)
23413 {
23414 end_specialization ();
23415 --parser->num_template_parameter_lists;
23416 }
23417
23418 if (type)
23419 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23420 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23421 CLASSTYPE_FINAL (type) = 1;
23422 out:
23423 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23424 return type;
23425 }
23426
23427 /* Parse a class-key.
23428
23429 class-key:
23430 class
23431 struct
23432 union
23433
23434 Returns the kind of class-key specified, or none_type to indicate
23435 error. */
23436
23437 static enum tag_types
23438 cp_parser_class_key (cp_parser* parser)
23439 {
23440 cp_token *token;
23441 enum tag_types tag_type;
23442
23443 /* Look for the class-key. */
23444 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23445 if (!token)
23446 return none_type;
23447
23448 /* Check to see if the TOKEN is a class-key. */
23449 tag_type = cp_parser_token_is_class_key (token);
23450 if (!tag_type)
23451 cp_parser_error (parser, "expected class-key");
23452 return tag_type;
23453 }
23454
23455 /* Parse a type-parameter-key.
23456
23457 type-parameter-key:
23458 class
23459 typename
23460 */
23461
23462 static void
23463 cp_parser_type_parameter_key (cp_parser* parser)
23464 {
23465 /* Look for the type-parameter-key. */
23466 enum tag_types tag_type = none_type;
23467 cp_token *token = cp_lexer_peek_token (parser->lexer);
23468 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23469 {
23470 cp_lexer_consume_token (parser->lexer);
23471 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23472 /* typename is not allowed in a template template parameter
23473 by the standard until C++17. */
23474 pedwarn (token->location, OPT_Wpedantic,
23475 "ISO C++ forbids typename key in template template parameter;"
23476 " use -std=c++17 or -std=gnu++17");
23477 }
23478 else
23479 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23480
23481 return;
23482 }
23483
23484 /* Parse an (optional) member-specification.
23485
23486 member-specification:
23487 member-declaration member-specification [opt]
23488 access-specifier : member-specification [opt] */
23489
23490 static void
23491 cp_parser_member_specification_opt (cp_parser* parser)
23492 {
23493 while (true)
23494 {
23495 cp_token *token;
23496 enum rid keyword;
23497
23498 /* Peek at the next token. */
23499 token = cp_lexer_peek_token (parser->lexer);
23500 /* If it's a `}', or EOF then we've seen all the members. */
23501 if (token->type == CPP_CLOSE_BRACE
23502 || token->type == CPP_EOF
23503 || token->type == CPP_PRAGMA_EOL)
23504 break;
23505
23506 /* See if this token is a keyword. */
23507 keyword = token->keyword;
23508 switch (keyword)
23509 {
23510 case RID_PUBLIC:
23511 case RID_PROTECTED:
23512 case RID_PRIVATE:
23513 /* Consume the access-specifier. */
23514 cp_lexer_consume_token (parser->lexer);
23515 /* Remember which access-specifier is active. */
23516 current_access_specifier = token->u.value;
23517 /* Look for the `:'. */
23518 cp_parser_require (parser, CPP_COLON, RT_COLON);
23519 break;
23520
23521 default:
23522 /* Accept #pragmas at class scope. */
23523 if (token->type == CPP_PRAGMA)
23524 {
23525 cp_parser_pragma (parser, pragma_member, NULL);
23526 break;
23527 }
23528
23529 /* Otherwise, the next construction must be a
23530 member-declaration. */
23531 cp_parser_member_declaration (parser);
23532 }
23533 }
23534 }
23535
23536 /* Parse a member-declaration.
23537
23538 member-declaration:
23539 decl-specifier-seq [opt] member-declarator-list [opt] ;
23540 function-definition ; [opt]
23541 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23542 using-declaration
23543 template-declaration
23544 alias-declaration
23545
23546 member-declarator-list:
23547 member-declarator
23548 member-declarator-list , member-declarator
23549
23550 member-declarator:
23551 declarator pure-specifier [opt]
23552 declarator constant-initializer [opt]
23553 identifier [opt] : constant-expression
23554
23555 GNU Extensions:
23556
23557 member-declaration:
23558 __extension__ member-declaration
23559
23560 member-declarator:
23561 declarator attributes [opt] pure-specifier [opt]
23562 declarator attributes [opt] constant-initializer [opt]
23563 identifier [opt] attributes [opt] : constant-expression
23564
23565 C++0x Extensions:
23566
23567 member-declaration:
23568 static_assert-declaration */
23569
23570 static void
23571 cp_parser_member_declaration (cp_parser* parser)
23572 {
23573 cp_decl_specifier_seq decl_specifiers;
23574 tree prefix_attributes;
23575 tree decl;
23576 int declares_class_or_enum;
23577 bool friend_p;
23578 cp_token *token = NULL;
23579 cp_token *decl_spec_token_start = NULL;
23580 cp_token *initializer_token_start = NULL;
23581 int saved_pedantic;
23582 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23583
23584 /* Check for the `__extension__' keyword. */
23585 if (cp_parser_extension_opt (parser, &saved_pedantic))
23586 {
23587 /* Recurse. */
23588 cp_parser_member_declaration (parser);
23589 /* Restore the old value of the PEDANTIC flag. */
23590 pedantic = saved_pedantic;
23591
23592 return;
23593 }
23594
23595 /* Check for a template-declaration. */
23596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23597 {
23598 /* An explicit specialization here is an error condition, and we
23599 expect the specialization handler to detect and report this. */
23600 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23601 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23602 cp_parser_explicit_specialization (parser);
23603 else
23604 cp_parser_template_declaration (parser, /*member_p=*/true);
23605
23606 return;
23607 }
23608 /* Check for a template introduction. */
23609 else if (cp_parser_template_declaration_after_export (parser, true))
23610 return;
23611
23612 /* Check for a using-declaration. */
23613 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23614 {
23615 if (cxx_dialect < cxx11)
23616 {
23617 /* Parse the using-declaration. */
23618 cp_parser_using_declaration (parser,
23619 /*access_declaration_p=*/false);
23620 return;
23621 }
23622 else
23623 {
23624 tree decl;
23625 bool alias_decl_expected;
23626 cp_parser_parse_tentatively (parser);
23627 decl = cp_parser_alias_declaration (parser);
23628 /* Note that if we actually see the '=' token after the
23629 identifier, cp_parser_alias_declaration commits the
23630 tentative parse. In that case, we really expect an
23631 alias-declaration. Otherwise, we expect a using
23632 declaration. */
23633 alias_decl_expected =
23634 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23635 cp_parser_parse_definitely (parser);
23636
23637 if (alias_decl_expected)
23638 finish_member_declaration (decl);
23639 else
23640 cp_parser_using_declaration (parser,
23641 /*access_declaration_p=*/false);
23642 return;
23643 }
23644 }
23645
23646 /* Check for @defs. */
23647 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23648 {
23649 tree ivar, member;
23650 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23651 ivar = ivar_chains;
23652 while (ivar)
23653 {
23654 member = ivar;
23655 ivar = TREE_CHAIN (member);
23656 TREE_CHAIN (member) = NULL_TREE;
23657 finish_member_declaration (member);
23658 }
23659 return;
23660 }
23661
23662 /* If the next token is `static_assert' we have a static assertion. */
23663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23664 {
23665 cp_parser_static_assert (parser, /*member_p=*/true);
23666 return;
23667 }
23668
23669 parser->colon_corrects_to_scope_p = false;
23670
23671 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23672 goto out;
23673
23674 /* Parse the decl-specifier-seq. */
23675 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23676 cp_parser_decl_specifier_seq (parser,
23677 CP_PARSER_FLAGS_OPTIONAL,
23678 &decl_specifiers,
23679 &declares_class_or_enum);
23680 /* Check for an invalid type-name. */
23681 if (!decl_specifiers.any_type_specifiers_p
23682 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23683 goto out;
23684 /* If there is no declarator, then the decl-specifier-seq should
23685 specify a type. */
23686 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23687 {
23688 /* If there was no decl-specifier-seq, and the next token is a
23689 `;', then we have something like:
23690
23691 struct S { ; };
23692
23693 [class.mem]
23694
23695 Each member-declaration shall declare at least one member
23696 name of the class. */
23697 if (!decl_specifiers.any_specifiers_p)
23698 {
23699 cp_token *token = cp_lexer_peek_token (parser->lexer);
23700 if (!in_system_header_at (token->location))
23701 {
23702 gcc_rich_location richloc (token->location);
23703 richloc.add_fixit_remove ();
23704 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23705 }
23706 }
23707 else
23708 {
23709 tree type;
23710
23711 /* See if this declaration is a friend. */
23712 friend_p = cp_parser_friend_p (&decl_specifiers);
23713 /* If there were decl-specifiers, check to see if there was
23714 a class-declaration. */
23715 type = check_tag_decl (&decl_specifiers,
23716 /*explicit_type_instantiation_p=*/false);
23717 /* Nested classes have already been added to the class, but
23718 a `friend' needs to be explicitly registered. */
23719 if (friend_p)
23720 {
23721 /* If the `friend' keyword was present, the friend must
23722 be introduced with a class-key. */
23723 if (!declares_class_or_enum && cxx_dialect < cxx11)
23724 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23725 "in C++03 a class-key must be used "
23726 "when declaring a friend");
23727 /* In this case:
23728
23729 template <typename T> struct A {
23730 friend struct A<T>::B;
23731 };
23732
23733 A<T>::B will be represented by a TYPENAME_TYPE, and
23734 therefore not recognized by check_tag_decl. */
23735 if (!type)
23736 {
23737 type = decl_specifiers.type;
23738 if (type && TREE_CODE (type) == TYPE_DECL)
23739 type = TREE_TYPE (type);
23740 }
23741 if (!type || !TYPE_P (type))
23742 error_at (decl_spec_token_start->location,
23743 "friend declaration does not name a class or "
23744 "function");
23745 else
23746 make_friend_class (current_class_type, type,
23747 /*complain=*/true);
23748 }
23749 /* If there is no TYPE, an error message will already have
23750 been issued. */
23751 else if (!type || type == error_mark_node)
23752 ;
23753 /* An anonymous aggregate has to be handled specially; such
23754 a declaration really declares a data member (with a
23755 particular type), as opposed to a nested class. */
23756 else if (ANON_AGGR_TYPE_P (type))
23757 {
23758 /* C++11 9.5/6. */
23759 if (decl_specifiers.storage_class != sc_none)
23760 error_at (decl_spec_token_start->location,
23761 "a storage class on an anonymous aggregate "
23762 "in class scope is not allowed");
23763
23764 /* Remove constructors and such from TYPE, now that we
23765 know it is an anonymous aggregate. */
23766 fixup_anonymous_aggr (type);
23767 /* And make the corresponding data member. */
23768 decl = build_decl (decl_spec_token_start->location,
23769 FIELD_DECL, NULL_TREE, type);
23770 /* Add it to the class. */
23771 finish_member_declaration (decl);
23772 }
23773 else
23774 cp_parser_check_access_in_redeclaration
23775 (TYPE_NAME (type),
23776 decl_spec_token_start->location);
23777 }
23778 }
23779 else
23780 {
23781 bool assume_semicolon = false;
23782
23783 /* Clear attributes from the decl_specifiers but keep them
23784 around as prefix attributes that apply them to the entity
23785 being declared. */
23786 prefix_attributes = decl_specifiers.attributes;
23787 decl_specifiers.attributes = NULL_TREE;
23788
23789 /* See if these declarations will be friends. */
23790 friend_p = cp_parser_friend_p (&decl_specifiers);
23791
23792 /* Keep going until we hit the `;' at the end of the
23793 declaration. */
23794 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23795 {
23796 tree attributes = NULL_TREE;
23797 tree first_attribute;
23798 tree initializer;
23799 bool named_bitfld = false;
23800
23801 /* Peek at the next token. */
23802 token = cp_lexer_peek_token (parser->lexer);
23803
23804 /* The following code wants to know early if it is a bit-field
23805 or some other declaration. Attributes can appear before
23806 the `:' token. Skip over them without consuming any tokens
23807 to peek if they are followed by `:'. */
23808 if (cp_next_tokens_can_be_attribute_p (parser)
23809 || (token->type == CPP_NAME
23810 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23811 && (named_bitfld = true)))
23812 {
23813 size_t n
23814 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23815 token = cp_lexer_peek_nth_token (parser->lexer, n);
23816 }
23817
23818 /* Check for a bitfield declaration. */
23819 if (token->type == CPP_COLON
23820 || (token->type == CPP_NAME
23821 && token == cp_lexer_peek_token (parser->lexer)
23822 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23823 && (named_bitfld = true)))
23824 {
23825 tree identifier;
23826 tree width;
23827 tree late_attributes = NULL_TREE;
23828
23829 if (named_bitfld)
23830 identifier = cp_parser_identifier (parser);
23831 else
23832 identifier = NULL_TREE;
23833
23834 /* Look for attributes that apply to the bitfield. */
23835 attributes = cp_parser_attributes_opt (parser);
23836
23837 /* Consume the `:' token. */
23838 cp_lexer_consume_token (parser->lexer);
23839
23840 /* Get the width of the bitfield. */
23841 width = cp_parser_constant_expression (parser, false, NULL,
23842 cxx_dialect >= cxx11);
23843
23844 /* In C++2A and as extension for C++11 and above we allow
23845 default member initializers for bit-fields. */
23846 initializer = NULL_TREE;
23847 if (cxx_dialect >= cxx11
23848 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23849 || cp_lexer_next_token_is (parser->lexer,
23850 CPP_OPEN_BRACE)))
23851 {
23852 location_t loc
23853 = cp_lexer_peek_token (parser->lexer)->location;
23854 if (cxx_dialect < cxx2a
23855 && !in_system_header_at (loc)
23856 && identifier != NULL_TREE)
23857 pedwarn (loc, 0,
23858 "default member initializers for bit-fields "
23859 "only available with -std=c++2a or "
23860 "-std=gnu++2a");
23861
23862 initializer = cp_parser_save_nsdmi (parser);
23863 if (identifier == NULL_TREE)
23864 {
23865 error_at (loc, "default member initializer for "
23866 "unnamed bit-field");
23867 initializer = NULL_TREE;
23868 }
23869 }
23870 else
23871 {
23872 /* Look for attributes that apply to the bitfield after
23873 the `:' token and width. This is where GCC used to
23874 parse attributes in the past, pedwarn if there is
23875 a std attribute. */
23876 if (cp_next_tokens_can_be_std_attribute_p (parser))
23877 pedwarn (input_location, OPT_Wpedantic,
23878 "ISO C++ allows bit-field attributes only "
23879 "before the %<:%> token");
23880
23881 late_attributes = cp_parser_attributes_opt (parser);
23882 }
23883
23884 attributes = attr_chainon (attributes, late_attributes);
23885
23886 /* Remember which attributes are prefix attributes and
23887 which are not. */
23888 first_attribute = attributes;
23889 /* Combine the attributes. */
23890 attributes = attr_chainon (prefix_attributes, attributes);
23891
23892 /* Create the bitfield declaration. */
23893 decl = grokbitfield (identifier
23894 ? make_id_declarator (NULL_TREE,
23895 identifier,
23896 sfk_none)
23897 : NULL,
23898 &decl_specifiers,
23899 width, initializer,
23900 attributes);
23901 }
23902 else
23903 {
23904 cp_declarator *declarator;
23905 tree asm_specification;
23906 int ctor_dtor_or_conv_p;
23907
23908 /* Parse the declarator. */
23909 declarator
23910 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23911 &ctor_dtor_or_conv_p,
23912 /*parenthesized_p=*/NULL,
23913 /*member_p=*/true,
23914 friend_p);
23915
23916 /* If something went wrong parsing the declarator, make sure
23917 that we at least consume some tokens. */
23918 if (declarator == cp_error_declarator)
23919 {
23920 /* Skip to the end of the statement. */
23921 cp_parser_skip_to_end_of_statement (parser);
23922 /* If the next token is not a semicolon, that is
23923 probably because we just skipped over the body of
23924 a function. So, we consume a semicolon if
23925 present, but do not issue an error message if it
23926 is not present. */
23927 if (cp_lexer_next_token_is (parser->lexer,
23928 CPP_SEMICOLON))
23929 cp_lexer_consume_token (parser->lexer);
23930 goto out;
23931 }
23932
23933 if (declares_class_or_enum & 2)
23934 cp_parser_check_for_definition_in_return_type
23935 (declarator, decl_specifiers.type,
23936 decl_specifiers.locations[ds_type_spec]);
23937
23938 /* Look for an asm-specification. */
23939 asm_specification = cp_parser_asm_specification_opt (parser);
23940 /* Look for attributes that apply to the declaration. */
23941 attributes = cp_parser_attributes_opt (parser);
23942 /* Remember which attributes are prefix attributes and
23943 which are not. */
23944 first_attribute = attributes;
23945 /* Combine the attributes. */
23946 attributes = attr_chainon (prefix_attributes, attributes);
23947
23948 /* If it's an `=', then we have a constant-initializer or a
23949 pure-specifier. It is not correct to parse the
23950 initializer before registering the member declaration
23951 since the member declaration should be in scope while
23952 its initializer is processed. However, the rest of the
23953 front end does not yet provide an interface that allows
23954 us to handle this correctly. */
23955 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23956 {
23957 /* In [class.mem]:
23958
23959 A pure-specifier shall be used only in the declaration of
23960 a virtual function.
23961
23962 A member-declarator can contain a constant-initializer
23963 only if it declares a static member of integral or
23964 enumeration type.
23965
23966 Therefore, if the DECLARATOR is for a function, we look
23967 for a pure-specifier; otherwise, we look for a
23968 constant-initializer. When we call `grokfield', it will
23969 perform more stringent semantics checks. */
23970 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23971 if (function_declarator_p (declarator)
23972 || (decl_specifiers.type
23973 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23974 && declarator->kind == cdk_id
23975 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23976 == FUNCTION_TYPE)))
23977 initializer = cp_parser_pure_specifier (parser);
23978 else if (decl_specifiers.storage_class != sc_static)
23979 initializer = cp_parser_save_nsdmi (parser);
23980 else if (cxx_dialect >= cxx11)
23981 {
23982 bool nonconst;
23983 /* Don't require a constant rvalue in C++11, since we
23984 might want a reference constant. We'll enforce
23985 constancy later. */
23986 cp_lexer_consume_token (parser->lexer);
23987 /* Parse the initializer. */
23988 initializer = cp_parser_initializer_clause (parser,
23989 &nonconst);
23990 }
23991 else
23992 /* Parse the initializer. */
23993 initializer = cp_parser_constant_initializer (parser);
23994 }
23995 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23996 && !function_declarator_p (declarator))
23997 {
23998 bool x;
23999 if (decl_specifiers.storage_class != sc_static)
24000 initializer = cp_parser_save_nsdmi (parser);
24001 else
24002 initializer = cp_parser_initializer (parser, &x, &x);
24003 }
24004 /* Otherwise, there is no initializer. */
24005 else
24006 initializer = NULL_TREE;
24007
24008 /* See if we are probably looking at a function
24009 definition. We are certainly not looking at a
24010 member-declarator. Calling `grokfield' has
24011 side-effects, so we must not do it unless we are sure
24012 that we are looking at a member-declarator. */
24013 if (cp_parser_token_starts_function_definition_p
24014 (cp_lexer_peek_token (parser->lexer)))
24015 {
24016 /* The grammar does not allow a pure-specifier to be
24017 used when a member function is defined. (It is
24018 possible that this fact is an oversight in the
24019 standard, since a pure function may be defined
24020 outside of the class-specifier. */
24021 if (initializer && initializer_token_start)
24022 error_at (initializer_token_start->location,
24023 "pure-specifier on function-definition");
24024 decl = cp_parser_save_member_function_body (parser,
24025 &decl_specifiers,
24026 declarator,
24027 attributes);
24028 if (parser->fully_implicit_function_template_p)
24029 decl = finish_fully_implicit_template (parser, decl);
24030 /* If the member was not a friend, declare it here. */
24031 if (!friend_p)
24032 finish_member_declaration (decl);
24033 /* Peek at the next token. */
24034 token = cp_lexer_peek_token (parser->lexer);
24035 /* If the next token is a semicolon, consume it. */
24036 if (token->type == CPP_SEMICOLON)
24037 {
24038 location_t semicolon_loc
24039 = cp_lexer_consume_token (parser->lexer)->location;
24040 gcc_rich_location richloc (semicolon_loc);
24041 richloc.add_fixit_remove ();
24042 warning_at (&richloc, OPT_Wextra_semi,
24043 "extra %<;%> after in-class "
24044 "function definition");
24045 }
24046 goto out;
24047 }
24048 else
24049 if (declarator->kind == cdk_function)
24050 declarator->id_loc = token->location;
24051 /* Create the declaration. */
24052 decl = grokfield (declarator, &decl_specifiers,
24053 initializer, /*init_const_expr_p=*/true,
24054 asm_specification, attributes);
24055 if (parser->fully_implicit_function_template_p)
24056 {
24057 if (friend_p)
24058 finish_fully_implicit_template (parser, 0);
24059 else
24060 decl = finish_fully_implicit_template (parser, decl);
24061 }
24062 }
24063
24064 cp_finalize_omp_declare_simd (parser, decl);
24065 cp_finalize_oacc_routine (parser, decl, false);
24066
24067 /* Reset PREFIX_ATTRIBUTES. */
24068 if (attributes != error_mark_node)
24069 {
24070 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24071 attributes = TREE_CHAIN (attributes);
24072 if (attributes)
24073 TREE_CHAIN (attributes) = NULL_TREE;
24074 }
24075
24076 /* If there is any qualification still in effect, clear it
24077 now; we will be starting fresh with the next declarator. */
24078 parser->scope = NULL_TREE;
24079 parser->qualifying_scope = NULL_TREE;
24080 parser->object_scope = NULL_TREE;
24081 /* If it's a `,', then there are more declarators. */
24082 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24083 {
24084 cp_lexer_consume_token (parser->lexer);
24085 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24086 {
24087 cp_token *token = cp_lexer_previous_token (parser->lexer);
24088 gcc_rich_location richloc (token->location);
24089 richloc.add_fixit_remove ();
24090 error_at (&richloc, "stray %<,%> at end of "
24091 "member declaration");
24092 }
24093 }
24094 /* If the next token isn't a `;', then we have a parse error. */
24095 else if (cp_lexer_next_token_is_not (parser->lexer,
24096 CPP_SEMICOLON))
24097 {
24098 /* The next token might be a ways away from where the
24099 actual semicolon is missing. Find the previous token
24100 and use that for our error position. */
24101 cp_token *token = cp_lexer_previous_token (parser->lexer);
24102 gcc_rich_location richloc (token->location);
24103 richloc.add_fixit_insert_after (";");
24104 error_at (&richloc, "expected %<;%> at end of "
24105 "member declaration");
24106
24107 /* Assume that the user meant to provide a semicolon. If
24108 we were to cp_parser_skip_to_end_of_statement, we might
24109 skip to a semicolon inside a member function definition
24110 and issue nonsensical error messages. */
24111 assume_semicolon = true;
24112 }
24113
24114 if (decl)
24115 {
24116 /* Add DECL to the list of members. */
24117 if (!friend_p
24118 /* Explicitly include, eg, NSDMIs, for better error
24119 recovery (c++/58650). */
24120 || !DECL_DECLARES_FUNCTION_P (decl))
24121 finish_member_declaration (decl);
24122
24123 if (TREE_CODE (decl) == FUNCTION_DECL)
24124 cp_parser_save_default_args (parser, decl);
24125 else if (TREE_CODE (decl) == FIELD_DECL
24126 && DECL_INITIAL (decl))
24127 /* Add DECL to the queue of NSDMI to be parsed later. */
24128 vec_safe_push (unparsed_nsdmis, decl);
24129 }
24130
24131 if (assume_semicolon)
24132 goto out;
24133 }
24134 }
24135
24136 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24137 out:
24138 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24139 }
24140
24141 /* Parse a pure-specifier.
24142
24143 pure-specifier:
24144 = 0
24145
24146 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24147 Otherwise, ERROR_MARK_NODE is returned. */
24148
24149 static tree
24150 cp_parser_pure_specifier (cp_parser* parser)
24151 {
24152 cp_token *token;
24153
24154 /* Look for the `=' token. */
24155 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24156 return error_mark_node;
24157 /* Look for the `0' token. */
24158 token = cp_lexer_peek_token (parser->lexer);
24159
24160 if (token->type == CPP_EOF
24161 || token->type == CPP_PRAGMA_EOL)
24162 return error_mark_node;
24163
24164 cp_lexer_consume_token (parser->lexer);
24165
24166 /* Accept = default or = delete in c++0x mode. */
24167 if (token->keyword == RID_DEFAULT
24168 || token->keyword == RID_DELETE)
24169 {
24170 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24171 return token->u.value;
24172 }
24173
24174 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24175 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24176 {
24177 cp_parser_error (parser,
24178 "invalid pure specifier (only %<= 0%> is allowed)");
24179 cp_parser_skip_to_end_of_statement (parser);
24180 return error_mark_node;
24181 }
24182 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24183 {
24184 error_at (token->location, "templates may not be %<virtual%>");
24185 return error_mark_node;
24186 }
24187
24188 return integer_zero_node;
24189 }
24190
24191 /* Parse a constant-initializer.
24192
24193 constant-initializer:
24194 = constant-expression
24195
24196 Returns a representation of the constant-expression. */
24197
24198 static tree
24199 cp_parser_constant_initializer (cp_parser* parser)
24200 {
24201 /* Look for the `=' token. */
24202 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24203 return error_mark_node;
24204
24205 /* It is invalid to write:
24206
24207 struct S { static const int i = { 7 }; };
24208
24209 */
24210 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24211 {
24212 cp_parser_error (parser,
24213 "a brace-enclosed initializer is not allowed here");
24214 /* Consume the opening brace. */
24215 matching_braces braces;
24216 braces.consume_open (parser);
24217 /* Skip the initializer. */
24218 cp_parser_skip_to_closing_brace (parser);
24219 /* Look for the trailing `}'. */
24220 braces.require_close (parser);
24221
24222 return error_mark_node;
24223 }
24224
24225 return cp_parser_constant_expression (parser);
24226 }
24227
24228 /* Derived classes [gram.class.derived] */
24229
24230 /* Parse a base-clause.
24231
24232 base-clause:
24233 : base-specifier-list
24234
24235 base-specifier-list:
24236 base-specifier ... [opt]
24237 base-specifier-list , base-specifier ... [opt]
24238
24239 Returns a TREE_LIST representing the base-classes, in the order in
24240 which they were declared. The representation of each node is as
24241 described by cp_parser_base_specifier.
24242
24243 In the case that no bases are specified, this function will return
24244 NULL_TREE, not ERROR_MARK_NODE. */
24245
24246 static tree
24247 cp_parser_base_clause (cp_parser* parser)
24248 {
24249 tree bases = NULL_TREE;
24250
24251 /* Look for the `:' that begins the list. */
24252 cp_parser_require (parser, CPP_COLON, RT_COLON);
24253
24254 /* Scan the base-specifier-list. */
24255 while (true)
24256 {
24257 cp_token *token;
24258 tree base;
24259 bool pack_expansion_p = false;
24260
24261 /* Look for the base-specifier. */
24262 base = cp_parser_base_specifier (parser);
24263 /* Look for the (optional) ellipsis. */
24264 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24265 {
24266 /* Consume the `...'. */
24267 cp_lexer_consume_token (parser->lexer);
24268
24269 pack_expansion_p = true;
24270 }
24271
24272 /* Add BASE to the front of the list. */
24273 if (base && base != error_mark_node)
24274 {
24275 if (pack_expansion_p)
24276 /* Make this a pack expansion type. */
24277 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24278
24279 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24280 {
24281 TREE_CHAIN (base) = bases;
24282 bases = base;
24283 }
24284 }
24285 /* Peek at the next token. */
24286 token = cp_lexer_peek_token (parser->lexer);
24287 /* If it's not a comma, then the list is complete. */
24288 if (token->type != CPP_COMMA)
24289 break;
24290 /* Consume the `,'. */
24291 cp_lexer_consume_token (parser->lexer);
24292 }
24293
24294 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24295 base class had a qualified name. However, the next name that
24296 appears is certainly not qualified. */
24297 parser->scope = NULL_TREE;
24298 parser->qualifying_scope = NULL_TREE;
24299 parser->object_scope = NULL_TREE;
24300
24301 return nreverse (bases);
24302 }
24303
24304 /* Parse a base-specifier.
24305
24306 base-specifier:
24307 :: [opt] nested-name-specifier [opt] class-name
24308 virtual access-specifier [opt] :: [opt] nested-name-specifier
24309 [opt] class-name
24310 access-specifier virtual [opt] :: [opt] nested-name-specifier
24311 [opt] class-name
24312
24313 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24314 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24315 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24316 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24317
24318 static tree
24319 cp_parser_base_specifier (cp_parser* parser)
24320 {
24321 cp_token *token;
24322 bool done = false;
24323 bool virtual_p = false;
24324 bool duplicate_virtual_error_issued_p = false;
24325 bool duplicate_access_error_issued_p = false;
24326 bool class_scope_p, template_p;
24327 tree access = access_default_node;
24328 tree type;
24329
24330 /* Process the optional `virtual' and `access-specifier'. */
24331 while (!done)
24332 {
24333 /* Peek at the next token. */
24334 token = cp_lexer_peek_token (parser->lexer);
24335 /* Process `virtual'. */
24336 switch (token->keyword)
24337 {
24338 case RID_VIRTUAL:
24339 /* If `virtual' appears more than once, issue an error. */
24340 if (virtual_p && !duplicate_virtual_error_issued_p)
24341 {
24342 cp_parser_error (parser,
24343 "%<virtual%> specified more than once in base-specifier");
24344 duplicate_virtual_error_issued_p = true;
24345 }
24346
24347 virtual_p = true;
24348
24349 /* Consume the `virtual' token. */
24350 cp_lexer_consume_token (parser->lexer);
24351
24352 break;
24353
24354 case RID_PUBLIC:
24355 case RID_PROTECTED:
24356 case RID_PRIVATE:
24357 /* If more than one access specifier appears, issue an
24358 error. */
24359 if (access != access_default_node
24360 && !duplicate_access_error_issued_p)
24361 {
24362 cp_parser_error (parser,
24363 "more than one access specifier in base-specifier");
24364 duplicate_access_error_issued_p = true;
24365 }
24366
24367 access = ridpointers[(int) token->keyword];
24368
24369 /* Consume the access-specifier. */
24370 cp_lexer_consume_token (parser->lexer);
24371
24372 break;
24373
24374 default:
24375 done = true;
24376 break;
24377 }
24378 }
24379 /* It is not uncommon to see programs mechanically, erroneously, use
24380 the 'typename' keyword to denote (dependent) qualified types
24381 as base classes. */
24382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24383 {
24384 token = cp_lexer_peek_token (parser->lexer);
24385 if (!processing_template_decl)
24386 error_at (token->location,
24387 "keyword %<typename%> not allowed outside of templates");
24388 else
24389 error_at (token->location,
24390 "keyword %<typename%> not allowed in this context "
24391 "(the base class is implicitly a type)");
24392 cp_lexer_consume_token (parser->lexer);
24393 }
24394
24395 /* Look for the optional `::' operator. */
24396 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24397 /* Look for the nested-name-specifier. The simplest way to
24398 implement:
24399
24400 [temp.res]
24401
24402 The keyword `typename' is not permitted in a base-specifier or
24403 mem-initializer; in these contexts a qualified name that
24404 depends on a template-parameter is implicitly assumed to be a
24405 type name.
24406
24407 is to pretend that we have seen the `typename' keyword at this
24408 point. */
24409 cp_parser_nested_name_specifier_opt (parser,
24410 /*typename_keyword_p=*/true,
24411 /*check_dependency_p=*/true,
24412 /*type_p=*/true,
24413 /*is_declaration=*/true);
24414 /* If the base class is given by a qualified name, assume that names
24415 we see are type names or templates, as appropriate. */
24416 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24417 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24418
24419 if (!parser->scope
24420 && cp_lexer_next_token_is_decltype (parser->lexer))
24421 /* DR 950 allows decltype as a base-specifier. */
24422 type = cp_parser_decltype (parser);
24423 else
24424 {
24425 /* Otherwise, look for the class-name. */
24426 type = cp_parser_class_name (parser,
24427 class_scope_p,
24428 template_p,
24429 typename_type,
24430 /*check_dependency_p=*/true,
24431 /*class_head_p=*/false,
24432 /*is_declaration=*/true);
24433 type = TREE_TYPE (type);
24434 }
24435
24436 if (type == error_mark_node)
24437 return error_mark_node;
24438
24439 return finish_base_specifier (type, access, virtual_p);
24440 }
24441
24442 /* Exception handling [gram.exception] */
24443
24444 /* Parse an (optional) noexcept-specification.
24445
24446 noexcept-specification:
24447 noexcept ( constant-expression ) [opt]
24448
24449 If no noexcept-specification is present, returns NULL_TREE.
24450 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24451 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24452 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24453 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24454 in which case a boolean condition is returned instead. */
24455
24456 static tree
24457 cp_parser_noexcept_specification_opt (cp_parser* parser,
24458 bool require_constexpr,
24459 bool* consumed_expr,
24460 bool return_cond)
24461 {
24462 cp_token *token;
24463 const char *saved_message;
24464
24465 /* Peek at the next token. */
24466 token = cp_lexer_peek_token (parser->lexer);
24467
24468 /* Is it a noexcept-specification? */
24469 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24470 {
24471 tree expr;
24472 cp_lexer_consume_token (parser->lexer);
24473
24474 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24475 {
24476 matching_parens parens;
24477 parens.consume_open (parser);
24478
24479 if (require_constexpr)
24480 {
24481 /* Types may not be defined in an exception-specification. */
24482 saved_message = parser->type_definition_forbidden_message;
24483 parser->type_definition_forbidden_message
24484 = G_("types may not be defined in an exception-specification");
24485
24486 expr = cp_parser_constant_expression (parser);
24487
24488 /* Restore the saved message. */
24489 parser->type_definition_forbidden_message = saved_message;
24490 }
24491 else
24492 {
24493 expr = cp_parser_expression (parser);
24494 *consumed_expr = true;
24495 }
24496
24497 parens.require_close (parser);
24498 }
24499 else
24500 {
24501 expr = boolean_true_node;
24502 if (!require_constexpr)
24503 *consumed_expr = false;
24504 }
24505
24506 /* We cannot build a noexcept-spec right away because this will check
24507 that expr is a constexpr. */
24508 if (!return_cond)
24509 return build_noexcept_spec (expr, tf_warning_or_error);
24510 else
24511 return expr;
24512 }
24513 else
24514 return NULL_TREE;
24515 }
24516
24517 /* Parse an (optional) exception-specification.
24518
24519 exception-specification:
24520 throw ( type-id-list [opt] )
24521
24522 Returns a TREE_LIST representing the exception-specification. The
24523 TREE_VALUE of each node is a type. */
24524
24525 static tree
24526 cp_parser_exception_specification_opt (cp_parser* parser)
24527 {
24528 cp_token *token;
24529 tree type_id_list;
24530 const char *saved_message;
24531
24532 /* Peek at the next token. */
24533 token = cp_lexer_peek_token (parser->lexer);
24534
24535 /* Is it a noexcept-specification? */
24536 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24537 false);
24538 if (type_id_list != NULL_TREE)
24539 return type_id_list;
24540
24541 /* If it's not `throw', then there's no exception-specification. */
24542 if (!cp_parser_is_keyword (token, RID_THROW))
24543 return NULL_TREE;
24544
24545 location_t loc = token->location;
24546
24547 /* Consume the `throw'. */
24548 cp_lexer_consume_token (parser->lexer);
24549
24550 /* Look for the `('. */
24551 matching_parens parens;
24552 parens.require_open (parser);
24553
24554 /* Peek at the next token. */
24555 token = cp_lexer_peek_token (parser->lexer);
24556 /* If it's not a `)', then there is a type-id-list. */
24557 if (token->type != CPP_CLOSE_PAREN)
24558 {
24559 /* Types may not be defined in an exception-specification. */
24560 saved_message = parser->type_definition_forbidden_message;
24561 parser->type_definition_forbidden_message
24562 = G_("types may not be defined in an exception-specification");
24563 /* Parse the type-id-list. */
24564 type_id_list = cp_parser_type_id_list (parser);
24565 /* Restore the saved message. */
24566 parser->type_definition_forbidden_message = saved_message;
24567
24568 if (cxx_dialect >= cxx17)
24569 {
24570 error_at (loc, "ISO C++17 does not allow dynamic exception "
24571 "specifications");
24572 type_id_list = NULL_TREE;
24573 }
24574 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24575 warning_at (loc, OPT_Wdeprecated,
24576 "dynamic exception specifications are deprecated in "
24577 "C++11");
24578 }
24579 /* In C++17, throw() is equivalent to noexcept (true). throw()
24580 is deprecated in C++11 and above as well, but is still widely used,
24581 so don't warn about it yet. */
24582 else if (cxx_dialect >= cxx17)
24583 type_id_list = noexcept_true_spec;
24584 else
24585 type_id_list = empty_except_spec;
24586
24587 /* Look for the `)'. */
24588 parens.require_close (parser);
24589
24590 return type_id_list;
24591 }
24592
24593 /* Parse an (optional) type-id-list.
24594
24595 type-id-list:
24596 type-id ... [opt]
24597 type-id-list , type-id ... [opt]
24598
24599 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24600 in the order that the types were presented. */
24601
24602 static tree
24603 cp_parser_type_id_list (cp_parser* parser)
24604 {
24605 tree types = NULL_TREE;
24606
24607 while (true)
24608 {
24609 cp_token *token;
24610 tree type;
24611
24612 token = cp_lexer_peek_token (parser->lexer);
24613
24614 /* Get the next type-id. */
24615 type = cp_parser_type_id (parser);
24616 /* Check for invalid 'auto'. */
24617 if (flag_concepts && type_uses_auto (type))
24618 {
24619 error_at (token->location,
24620 "invalid use of %<auto%> in exception-specification");
24621 type = error_mark_node;
24622 }
24623 /* Parse the optional ellipsis. */
24624 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24625 {
24626 /* Consume the `...'. */
24627 cp_lexer_consume_token (parser->lexer);
24628
24629 /* Turn the type into a pack expansion expression. */
24630 type = make_pack_expansion (type);
24631 }
24632 /* Add it to the list. */
24633 types = add_exception_specifier (types, type, /*complain=*/1);
24634 /* Peek at the next token. */
24635 token = cp_lexer_peek_token (parser->lexer);
24636 /* If it is not a `,', we are done. */
24637 if (token->type != CPP_COMMA)
24638 break;
24639 /* Consume the `,'. */
24640 cp_lexer_consume_token (parser->lexer);
24641 }
24642
24643 return nreverse (types);
24644 }
24645
24646 /* Parse a try-block.
24647
24648 try-block:
24649 try compound-statement handler-seq */
24650
24651 static tree
24652 cp_parser_try_block (cp_parser* parser)
24653 {
24654 tree try_block;
24655
24656 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24657 if (parser->in_function_body
24658 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24659 error ("%<try%> in %<constexpr%> function");
24660
24661 try_block = begin_try_block ();
24662 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24663 finish_try_block (try_block);
24664 cp_parser_handler_seq (parser);
24665 finish_handler_sequence (try_block);
24666
24667 return try_block;
24668 }
24669
24670 /* Parse a function-try-block.
24671
24672 function-try-block:
24673 try ctor-initializer [opt] function-body handler-seq */
24674
24675 static void
24676 cp_parser_function_try_block (cp_parser* parser)
24677 {
24678 tree compound_stmt;
24679 tree try_block;
24680
24681 /* Look for the `try' keyword. */
24682 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24683 return;
24684 /* Let the rest of the front end know where we are. */
24685 try_block = begin_function_try_block (&compound_stmt);
24686 /* Parse the function-body. */
24687 cp_parser_ctor_initializer_opt_and_function_body
24688 (parser, /*in_function_try_block=*/true);
24689 /* We're done with the `try' part. */
24690 finish_function_try_block (try_block);
24691 /* Parse the handlers. */
24692 cp_parser_handler_seq (parser);
24693 /* We're done with the handlers. */
24694 finish_function_handler_sequence (try_block, compound_stmt);
24695 }
24696
24697 /* Parse a handler-seq.
24698
24699 handler-seq:
24700 handler handler-seq [opt] */
24701
24702 static void
24703 cp_parser_handler_seq (cp_parser* parser)
24704 {
24705 while (true)
24706 {
24707 cp_token *token;
24708
24709 /* Parse the handler. */
24710 cp_parser_handler (parser);
24711 /* Peek at the next token. */
24712 token = cp_lexer_peek_token (parser->lexer);
24713 /* If it's not `catch' then there are no more handlers. */
24714 if (!cp_parser_is_keyword (token, RID_CATCH))
24715 break;
24716 }
24717 }
24718
24719 /* Parse a handler.
24720
24721 handler:
24722 catch ( exception-declaration ) compound-statement */
24723
24724 static void
24725 cp_parser_handler (cp_parser* parser)
24726 {
24727 tree handler;
24728 tree declaration;
24729
24730 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24731 handler = begin_handler ();
24732 matching_parens parens;
24733 parens.require_open (parser);
24734 declaration = cp_parser_exception_declaration (parser);
24735 finish_handler_parms (declaration, handler);
24736 parens.require_close (parser);
24737 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24738 finish_handler (handler);
24739 }
24740
24741 /* Parse an exception-declaration.
24742
24743 exception-declaration:
24744 type-specifier-seq declarator
24745 type-specifier-seq abstract-declarator
24746 type-specifier-seq
24747 ...
24748
24749 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24750 ellipsis variant is used. */
24751
24752 static tree
24753 cp_parser_exception_declaration (cp_parser* parser)
24754 {
24755 cp_decl_specifier_seq type_specifiers;
24756 cp_declarator *declarator;
24757 const char *saved_message;
24758
24759 /* If it's an ellipsis, it's easy to handle. */
24760 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24761 {
24762 /* Consume the `...' token. */
24763 cp_lexer_consume_token (parser->lexer);
24764 return NULL_TREE;
24765 }
24766
24767 /* Types may not be defined in exception-declarations. */
24768 saved_message = parser->type_definition_forbidden_message;
24769 parser->type_definition_forbidden_message
24770 = G_("types may not be defined in exception-declarations");
24771
24772 /* Parse the type-specifier-seq. */
24773 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24774 /*is_trailing_return=*/false,
24775 &type_specifiers);
24776 /* If it's a `)', then there is no declarator. */
24777 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24778 declarator = NULL;
24779 else
24780 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24781 /*ctor_dtor_or_conv_p=*/NULL,
24782 /*parenthesized_p=*/NULL,
24783 /*member_p=*/false,
24784 /*friend_p=*/false);
24785
24786 /* Restore the saved message. */
24787 parser->type_definition_forbidden_message = saved_message;
24788
24789 if (!type_specifiers.any_specifiers_p)
24790 return error_mark_node;
24791
24792 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24793 }
24794
24795 /* Parse a throw-expression.
24796
24797 throw-expression:
24798 throw assignment-expression [opt]
24799
24800 Returns a THROW_EXPR representing the throw-expression. */
24801
24802 static tree
24803 cp_parser_throw_expression (cp_parser* parser)
24804 {
24805 tree expression;
24806 cp_token* token;
24807
24808 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24809 token = cp_lexer_peek_token (parser->lexer);
24810 /* Figure out whether or not there is an assignment-expression
24811 following the "throw" keyword. */
24812 if (token->type == CPP_COMMA
24813 || token->type == CPP_SEMICOLON
24814 || token->type == CPP_CLOSE_PAREN
24815 || token->type == CPP_CLOSE_SQUARE
24816 || token->type == CPP_CLOSE_BRACE
24817 || token->type == CPP_COLON)
24818 expression = NULL_TREE;
24819 else
24820 expression = cp_parser_assignment_expression (parser);
24821
24822 return build_throw (expression);
24823 }
24824
24825 /* GNU Extensions */
24826
24827 /* Parse an (optional) asm-specification.
24828
24829 asm-specification:
24830 asm ( string-literal )
24831
24832 If the asm-specification is present, returns a STRING_CST
24833 corresponding to the string-literal. Otherwise, returns
24834 NULL_TREE. */
24835
24836 static tree
24837 cp_parser_asm_specification_opt (cp_parser* parser)
24838 {
24839 cp_token *token;
24840 tree asm_specification;
24841
24842 /* Peek at the next token. */
24843 token = cp_lexer_peek_token (parser->lexer);
24844 /* If the next token isn't the `asm' keyword, then there's no
24845 asm-specification. */
24846 if (!cp_parser_is_keyword (token, RID_ASM))
24847 return NULL_TREE;
24848
24849 /* Consume the `asm' token. */
24850 cp_lexer_consume_token (parser->lexer);
24851 /* Look for the `('. */
24852 matching_parens parens;
24853 parens.require_open (parser);
24854
24855 /* Look for the string-literal. */
24856 asm_specification = cp_parser_string_literal (parser, false, false);
24857
24858 /* Look for the `)'. */
24859 parens.require_close (parser);
24860
24861 return asm_specification;
24862 }
24863
24864 /* Parse an asm-operand-list.
24865
24866 asm-operand-list:
24867 asm-operand
24868 asm-operand-list , asm-operand
24869
24870 asm-operand:
24871 string-literal ( expression )
24872 [ string-literal ] string-literal ( expression )
24873
24874 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24875 each node is the expression. The TREE_PURPOSE is itself a
24876 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24877 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24878 is a STRING_CST for the string literal before the parenthesis. Returns
24879 ERROR_MARK_NODE if any of the operands are invalid. */
24880
24881 static tree
24882 cp_parser_asm_operand_list (cp_parser* parser)
24883 {
24884 tree asm_operands = NULL_TREE;
24885 bool invalid_operands = false;
24886
24887 while (true)
24888 {
24889 tree string_literal;
24890 tree expression;
24891 tree name;
24892
24893 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24894 {
24895 /* Consume the `[' token. */
24896 cp_lexer_consume_token (parser->lexer);
24897 /* Read the operand name. */
24898 name = cp_parser_identifier (parser);
24899 if (name != error_mark_node)
24900 name = build_string (IDENTIFIER_LENGTH (name),
24901 IDENTIFIER_POINTER (name));
24902 /* Look for the closing `]'. */
24903 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24904 }
24905 else
24906 name = NULL_TREE;
24907 /* Look for the string-literal. */
24908 string_literal = cp_parser_string_literal (parser, false, false);
24909
24910 /* Look for the `('. */
24911 matching_parens parens;
24912 parens.require_open (parser);
24913 /* Parse the expression. */
24914 expression = cp_parser_expression (parser);
24915 /* Look for the `)'. */
24916 parens.require_close (parser);
24917
24918 if (name == error_mark_node
24919 || string_literal == error_mark_node
24920 || expression == error_mark_node)
24921 invalid_operands = true;
24922
24923 /* Add this operand to the list. */
24924 asm_operands = tree_cons (build_tree_list (name, string_literal),
24925 expression,
24926 asm_operands);
24927 /* If the next token is not a `,', there are no more
24928 operands. */
24929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24930 break;
24931 /* Consume the `,'. */
24932 cp_lexer_consume_token (parser->lexer);
24933 }
24934
24935 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24936 }
24937
24938 /* Parse an asm-clobber-list.
24939
24940 asm-clobber-list:
24941 string-literal
24942 asm-clobber-list , string-literal
24943
24944 Returns a TREE_LIST, indicating the clobbers in the order that they
24945 appeared. The TREE_VALUE of each node is a STRING_CST. */
24946
24947 static tree
24948 cp_parser_asm_clobber_list (cp_parser* parser)
24949 {
24950 tree clobbers = NULL_TREE;
24951
24952 while (true)
24953 {
24954 tree string_literal;
24955
24956 /* Look for the string literal. */
24957 string_literal = cp_parser_string_literal (parser, false, false);
24958 /* Add it to the list. */
24959 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24960 /* If the next token is not a `,', then the list is
24961 complete. */
24962 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24963 break;
24964 /* Consume the `,' token. */
24965 cp_lexer_consume_token (parser->lexer);
24966 }
24967
24968 return clobbers;
24969 }
24970
24971 /* Parse an asm-label-list.
24972
24973 asm-label-list:
24974 identifier
24975 asm-label-list , identifier
24976
24977 Returns a TREE_LIST, indicating the labels in the order that they
24978 appeared. The TREE_VALUE of each node is a label. */
24979
24980 static tree
24981 cp_parser_asm_label_list (cp_parser* parser)
24982 {
24983 tree labels = NULL_TREE;
24984
24985 while (true)
24986 {
24987 tree identifier, label, name;
24988
24989 /* Look for the identifier. */
24990 identifier = cp_parser_identifier (parser);
24991 if (!error_operand_p (identifier))
24992 {
24993 label = lookup_label (identifier);
24994 if (TREE_CODE (label) == LABEL_DECL)
24995 {
24996 TREE_USED (label) = 1;
24997 check_goto (label);
24998 name = build_string (IDENTIFIER_LENGTH (identifier),
24999 IDENTIFIER_POINTER (identifier));
25000 labels = tree_cons (name, label, labels);
25001 }
25002 }
25003 /* If the next token is not a `,', then the list is
25004 complete. */
25005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25006 break;
25007 /* Consume the `,' token. */
25008 cp_lexer_consume_token (parser->lexer);
25009 }
25010
25011 return nreverse (labels);
25012 }
25013
25014 /* Return TRUE iff the next tokens in the stream are possibly the
25015 beginning of a GNU extension attribute. */
25016
25017 static bool
25018 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25019 {
25020 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25021 }
25022
25023 /* Return TRUE iff the next tokens in the stream are possibly the
25024 beginning of a standard C++-11 attribute specifier. */
25025
25026 static bool
25027 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25028 {
25029 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25030 }
25031
25032 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25033 beginning of a standard C++-11 attribute specifier. */
25034
25035 static bool
25036 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25037 {
25038 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25039
25040 return (cxx_dialect >= cxx11
25041 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25042 || (token->type == CPP_OPEN_SQUARE
25043 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25044 && token->type == CPP_OPEN_SQUARE)));
25045 }
25046
25047 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25048 beginning of a GNU extension attribute. */
25049
25050 static bool
25051 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25052 {
25053 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25054
25055 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25056 }
25057
25058 /* Return true iff the next tokens can be the beginning of either a
25059 GNU attribute list, or a standard C++11 attribute sequence. */
25060
25061 static bool
25062 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25063 {
25064 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25065 || cp_next_tokens_can_be_std_attribute_p (parser));
25066 }
25067
25068 /* Return true iff the next Nth tokens can be the beginning of either
25069 a GNU attribute list, or a standard C++11 attribute sequence. */
25070
25071 static bool
25072 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25073 {
25074 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25075 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25076 }
25077
25078 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25079 of GNU attributes, or return NULL. */
25080
25081 static tree
25082 cp_parser_attributes_opt (cp_parser *parser)
25083 {
25084 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25085 return cp_parser_gnu_attributes_opt (parser);
25086 return cp_parser_std_attribute_spec_seq (parser);
25087 }
25088
25089 /* Parse an (optional) series of attributes.
25090
25091 attributes:
25092 attributes attribute
25093
25094 attribute:
25095 __attribute__ (( attribute-list [opt] ))
25096
25097 The return value is as for cp_parser_gnu_attribute_list. */
25098
25099 static tree
25100 cp_parser_gnu_attributes_opt (cp_parser* parser)
25101 {
25102 tree attributes = NULL_TREE;
25103
25104 temp_override<bool> cleanup
25105 (parser->auto_is_implicit_function_template_parm_p, false);
25106
25107 while (true)
25108 {
25109 cp_token *token;
25110 tree attribute_list;
25111 bool ok = true;
25112
25113 /* Peek at the next token. */
25114 token = cp_lexer_peek_token (parser->lexer);
25115 /* If it's not `__attribute__', then we're done. */
25116 if (token->keyword != RID_ATTRIBUTE)
25117 break;
25118
25119 /* Consume the `__attribute__' keyword. */
25120 cp_lexer_consume_token (parser->lexer);
25121 /* Look for the two `(' tokens. */
25122 matching_parens outer_parens;
25123 outer_parens.require_open (parser);
25124 matching_parens inner_parens;
25125 inner_parens.require_open (parser);
25126
25127 /* Peek at the next token. */
25128 token = cp_lexer_peek_token (parser->lexer);
25129 if (token->type != CPP_CLOSE_PAREN)
25130 /* Parse the attribute-list. */
25131 attribute_list = cp_parser_gnu_attribute_list (parser);
25132 else
25133 /* If the next token is a `)', then there is no attribute
25134 list. */
25135 attribute_list = NULL;
25136
25137 /* Look for the two `)' tokens. */
25138 if (!inner_parens.require_close (parser))
25139 ok = false;
25140 if (!outer_parens.require_close (parser))
25141 ok = false;
25142 if (!ok)
25143 cp_parser_skip_to_end_of_statement (parser);
25144
25145 /* Add these new attributes to the list. */
25146 attributes = attr_chainon (attributes, attribute_list);
25147 }
25148
25149 return attributes;
25150 }
25151
25152 /* Parse a GNU attribute-list.
25153
25154 attribute-list:
25155 attribute
25156 attribute-list , attribute
25157
25158 attribute:
25159 identifier
25160 identifier ( identifier )
25161 identifier ( identifier , expression-list )
25162 identifier ( expression-list )
25163
25164 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25165 to an attribute. The TREE_PURPOSE of each node is the identifier
25166 indicating which attribute is in use. The TREE_VALUE represents
25167 the arguments, if any. */
25168
25169 static tree
25170 cp_parser_gnu_attribute_list (cp_parser* parser)
25171 {
25172 tree attribute_list = NULL_TREE;
25173 bool save_translate_strings_p = parser->translate_strings_p;
25174
25175 parser->translate_strings_p = false;
25176 while (true)
25177 {
25178 cp_token *token;
25179 tree identifier;
25180 tree attribute;
25181
25182 /* Look for the identifier. We also allow keywords here; for
25183 example `__attribute__ ((const))' is legal. */
25184 token = cp_lexer_peek_token (parser->lexer);
25185 if (token->type == CPP_NAME
25186 || token->type == CPP_KEYWORD)
25187 {
25188 tree arguments = NULL_TREE;
25189
25190 /* Consume the token, but save it since we need it for the
25191 SIMD enabled function parsing. */
25192 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25193
25194 /* Save away the identifier that indicates which attribute
25195 this is. */
25196 identifier = (token->type == CPP_KEYWORD)
25197 /* For keywords, use the canonical spelling, not the
25198 parsed identifier. */
25199 ? ridpointers[(int) token->keyword]
25200 : id_token->u.value;
25201
25202 identifier = canonicalize_attr_name (identifier);
25203 attribute = build_tree_list (identifier, NULL_TREE);
25204
25205 /* Peek at the next token. */
25206 token = cp_lexer_peek_token (parser->lexer);
25207 /* If it's an `(', then parse the attribute arguments. */
25208 if (token->type == CPP_OPEN_PAREN)
25209 {
25210 vec<tree, va_gc> *vec;
25211 int attr_flag = (attribute_takes_identifier_p (identifier)
25212 ? id_attr : normal_attr);
25213 vec = cp_parser_parenthesized_expression_list
25214 (parser, attr_flag, /*cast_p=*/false,
25215 /*allow_expansion_p=*/false,
25216 /*non_constant_p=*/NULL);
25217 if (vec == NULL)
25218 arguments = error_mark_node;
25219 else
25220 {
25221 arguments = build_tree_list_vec (vec);
25222 release_tree_vector (vec);
25223 }
25224 /* Save the arguments away. */
25225 TREE_VALUE (attribute) = arguments;
25226 }
25227
25228 if (arguments != error_mark_node)
25229 {
25230 /* Add this attribute to the list. */
25231 TREE_CHAIN (attribute) = attribute_list;
25232 attribute_list = attribute;
25233 }
25234
25235 token = cp_lexer_peek_token (parser->lexer);
25236 }
25237 /* Now, look for more attributes. If the next token isn't a
25238 `,', we're done. */
25239 if (token->type != CPP_COMMA)
25240 break;
25241
25242 /* Consume the comma and keep going. */
25243 cp_lexer_consume_token (parser->lexer);
25244 }
25245 parser->translate_strings_p = save_translate_strings_p;
25246
25247 /* We built up the list in reverse order. */
25248 return nreverse (attribute_list);
25249 }
25250
25251 /* Parse a standard C++11 attribute.
25252
25253 The returned representation is a TREE_LIST which TREE_PURPOSE is
25254 the scoped name of the attribute, and the TREE_VALUE is its
25255 arguments list.
25256
25257 Note that the scoped name of the attribute is itself a TREE_LIST
25258 which TREE_PURPOSE is the namespace of the attribute, and
25259 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25260 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25261 and which TREE_PURPOSE is directly the attribute name.
25262
25263 Clients of the attribute code should use get_attribute_namespace
25264 and get_attribute_name to get the actual namespace and name of
25265 attributes, regardless of their being GNU or C++11 attributes.
25266
25267 attribute:
25268 attribute-token attribute-argument-clause [opt]
25269
25270 attribute-token:
25271 identifier
25272 attribute-scoped-token
25273
25274 attribute-scoped-token:
25275 attribute-namespace :: identifier
25276
25277 attribute-namespace:
25278 identifier
25279
25280 attribute-argument-clause:
25281 ( balanced-token-seq )
25282
25283 balanced-token-seq:
25284 balanced-token [opt]
25285 balanced-token-seq balanced-token
25286
25287 balanced-token:
25288 ( balanced-token-seq )
25289 [ balanced-token-seq ]
25290 { balanced-token-seq }. */
25291
25292 static tree
25293 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25294 {
25295 tree attribute, attr_id = NULL_TREE, arguments;
25296 cp_token *token;
25297
25298 temp_override<bool> cleanup
25299 (parser->auto_is_implicit_function_template_parm_p, false);
25300
25301 /* First, parse name of the attribute, a.k.a attribute-token. */
25302
25303 token = cp_lexer_peek_token (parser->lexer);
25304 if (token->type == CPP_NAME)
25305 attr_id = token->u.value;
25306 else if (token->type == CPP_KEYWORD)
25307 attr_id = ridpointers[(int) token->keyword];
25308 else if (token->flags & NAMED_OP)
25309 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25310
25311 if (attr_id == NULL_TREE)
25312 return NULL_TREE;
25313
25314 cp_lexer_consume_token (parser->lexer);
25315
25316 token = cp_lexer_peek_token (parser->lexer);
25317 if (token->type == CPP_SCOPE)
25318 {
25319 /* We are seeing a scoped attribute token. */
25320
25321 cp_lexer_consume_token (parser->lexer);
25322 if (attr_ns)
25323 error_at (token->location, "attribute using prefix used together "
25324 "with scoped attribute token");
25325 attr_ns = attr_id;
25326
25327 token = cp_lexer_consume_token (parser->lexer);
25328 if (token->type == CPP_NAME)
25329 attr_id = token->u.value;
25330 else if (token->type == CPP_KEYWORD)
25331 attr_id = ridpointers[(int) token->keyword];
25332 else if (token->flags & NAMED_OP)
25333 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25334 else
25335 {
25336 error_at (token->location,
25337 "expected an identifier for the attribute name");
25338 return error_mark_node;
25339 }
25340
25341 attr_id = canonicalize_attr_name (attr_id);
25342 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25343 NULL_TREE);
25344 token = cp_lexer_peek_token (parser->lexer);
25345 }
25346 else if (attr_ns)
25347 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25348 NULL_TREE);
25349 else
25350 {
25351 attr_id = canonicalize_attr_name (attr_id);
25352 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25353 NULL_TREE);
25354 /* C++11 noreturn attribute is equivalent to GNU's. */
25355 if (is_attribute_p ("noreturn", attr_id))
25356 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25357 /* C++14 deprecated attribute is equivalent to GNU's. */
25358 else if (is_attribute_p ("deprecated", attr_id))
25359 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25360 /* C++17 fallthrough attribute is equivalent to GNU's. */
25361 else if (is_attribute_p ("fallthrough", attr_id))
25362 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25363 /* Transactional Memory TS optimize_for_synchronized attribute is
25364 equivalent to GNU transaction_callable. */
25365 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25366 TREE_PURPOSE (attribute)
25367 = get_identifier ("transaction_callable");
25368 /* Transactional Memory attributes are GNU attributes. */
25369 else if (tm_attr_to_mask (attr_id))
25370 TREE_PURPOSE (attribute) = attr_id;
25371 }
25372
25373 /* Now parse the optional argument clause of the attribute. */
25374
25375 if (token->type != CPP_OPEN_PAREN)
25376 return attribute;
25377
25378 {
25379 vec<tree, va_gc> *vec;
25380 int attr_flag = normal_attr;
25381
25382 if (attr_ns == gnu_identifier
25383 && attribute_takes_identifier_p (attr_id))
25384 /* A GNU attribute that takes an identifier in parameter. */
25385 attr_flag = id_attr;
25386
25387 vec = cp_parser_parenthesized_expression_list
25388 (parser, attr_flag, /*cast_p=*/false,
25389 /*allow_expansion_p=*/true,
25390 /*non_constant_p=*/NULL);
25391 if (vec == NULL)
25392 arguments = error_mark_node;
25393 else
25394 {
25395 arguments = build_tree_list_vec (vec);
25396 release_tree_vector (vec);
25397 }
25398
25399 if (arguments == error_mark_node)
25400 attribute = error_mark_node;
25401 else
25402 TREE_VALUE (attribute) = arguments;
25403 }
25404
25405 return attribute;
25406 }
25407
25408 /* Check that the attribute ATTRIBUTE appears at most once in the
25409 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25410 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25411 isn't implemented yet in GCC. */
25412
25413 static void
25414 cp_parser_check_std_attribute (tree attributes, tree attribute)
25415 {
25416 if (attributes)
25417 {
25418 tree name = get_attribute_name (attribute);
25419 if (is_attribute_p ("noreturn", name)
25420 && lookup_attribute ("noreturn", attributes))
25421 error ("attribute %<noreturn%> can appear at most once "
25422 "in an attribute-list");
25423 else if (is_attribute_p ("deprecated", name)
25424 && lookup_attribute ("deprecated", attributes))
25425 error ("attribute %<deprecated%> can appear at most once "
25426 "in an attribute-list");
25427 }
25428 }
25429
25430 /* Parse a list of standard C++-11 attributes.
25431
25432 attribute-list:
25433 attribute [opt]
25434 attribute-list , attribute[opt]
25435 attribute ...
25436 attribute-list , attribute ...
25437 */
25438
25439 static tree
25440 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25441 {
25442 tree attributes = NULL_TREE, attribute = NULL_TREE;
25443 cp_token *token = NULL;
25444
25445 while (true)
25446 {
25447 attribute = cp_parser_std_attribute (parser, attr_ns);
25448 if (attribute == error_mark_node)
25449 break;
25450 if (attribute != NULL_TREE)
25451 {
25452 cp_parser_check_std_attribute (attributes, attribute);
25453 TREE_CHAIN (attribute) = attributes;
25454 attributes = attribute;
25455 }
25456 token = cp_lexer_peek_token (parser->lexer);
25457 if (token->type == CPP_ELLIPSIS)
25458 {
25459 cp_lexer_consume_token (parser->lexer);
25460 if (attribute == NULL_TREE)
25461 error_at (token->location,
25462 "expected attribute before %<...%>");
25463 else
25464 {
25465 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25466 if (pack == error_mark_node)
25467 return error_mark_node;
25468 TREE_VALUE (attribute) = pack;
25469 }
25470 token = cp_lexer_peek_token (parser->lexer);
25471 }
25472 if (token->type != CPP_COMMA)
25473 break;
25474 cp_lexer_consume_token (parser->lexer);
25475 }
25476 attributes = nreverse (attributes);
25477 return attributes;
25478 }
25479
25480 /* Parse a standard C++-11 attribute specifier.
25481
25482 attribute-specifier:
25483 [ [ attribute-using-prefix [opt] attribute-list ] ]
25484 alignment-specifier
25485
25486 attribute-using-prefix:
25487 using attribute-namespace :
25488
25489 alignment-specifier:
25490 alignas ( type-id ... [opt] )
25491 alignas ( alignment-expression ... [opt] ). */
25492
25493 static tree
25494 cp_parser_std_attribute_spec (cp_parser *parser)
25495 {
25496 tree attributes = NULL_TREE;
25497 cp_token *token = cp_lexer_peek_token (parser->lexer);
25498
25499 if (token->type == CPP_OPEN_SQUARE
25500 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25501 {
25502 tree attr_ns = NULL_TREE;
25503
25504 cp_lexer_consume_token (parser->lexer);
25505 cp_lexer_consume_token (parser->lexer);
25506
25507 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25508 {
25509 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25510 if (token->type == CPP_NAME)
25511 attr_ns = token->u.value;
25512 else if (token->type == CPP_KEYWORD)
25513 attr_ns = ridpointers[(int) token->keyword];
25514 else if (token->flags & NAMED_OP)
25515 attr_ns = get_identifier (cpp_type2name (token->type,
25516 token->flags));
25517 if (attr_ns
25518 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25519 {
25520 if (cxx_dialect < cxx17
25521 && !in_system_header_at (input_location))
25522 pedwarn (input_location, 0,
25523 "attribute using prefix only available "
25524 "with -std=c++17 or -std=gnu++17");
25525
25526 cp_lexer_consume_token (parser->lexer);
25527 cp_lexer_consume_token (parser->lexer);
25528 cp_lexer_consume_token (parser->lexer);
25529 }
25530 else
25531 attr_ns = NULL_TREE;
25532 }
25533
25534 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25535
25536 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25537 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25538 cp_parser_skip_to_end_of_statement (parser);
25539 else
25540 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25541 when we are sure that we have actually parsed them. */
25542 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25543 }
25544 else
25545 {
25546 tree alignas_expr;
25547
25548 /* Look for an alignment-specifier. */
25549
25550 token = cp_lexer_peek_token (parser->lexer);
25551
25552 if (token->type != CPP_KEYWORD
25553 || token->keyword != RID_ALIGNAS)
25554 return NULL_TREE;
25555
25556 cp_lexer_consume_token (parser->lexer);
25557 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25558
25559 matching_parens parens;
25560 if (!parens.require_open (parser))
25561 return error_mark_node;
25562
25563 cp_parser_parse_tentatively (parser);
25564 alignas_expr = cp_parser_type_id (parser);
25565
25566 if (!cp_parser_parse_definitely (parser))
25567 {
25568 alignas_expr = cp_parser_assignment_expression (parser);
25569 if (alignas_expr == error_mark_node)
25570 cp_parser_skip_to_end_of_statement (parser);
25571 if (alignas_expr == NULL_TREE
25572 || alignas_expr == error_mark_node)
25573 return alignas_expr;
25574 }
25575
25576 alignas_expr = cxx_alignas_expr (alignas_expr);
25577 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25578
25579 /* Handle alignas (pack...). */
25580 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25581 {
25582 cp_lexer_consume_token (parser->lexer);
25583 alignas_expr = make_pack_expansion (alignas_expr);
25584 }
25585
25586 /* Something went wrong, so don't build the attribute. */
25587 if (alignas_expr == error_mark_node)
25588 return error_mark_node;
25589
25590 if (!parens.require_close (parser))
25591 return error_mark_node;
25592
25593 /* Build the C++-11 representation of an 'aligned'
25594 attribute. */
25595 attributes
25596 = build_tree_list (build_tree_list (gnu_identifier,
25597 aligned_identifier), alignas_expr);
25598 }
25599
25600 return attributes;
25601 }
25602
25603 /* Parse a standard C++-11 attribute-specifier-seq.
25604
25605 attribute-specifier-seq:
25606 attribute-specifier-seq [opt] attribute-specifier
25607 */
25608
25609 static tree
25610 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25611 {
25612 tree attr_specs = NULL_TREE;
25613 tree attr_last = NULL_TREE;
25614
25615 while (true)
25616 {
25617 tree attr_spec = cp_parser_std_attribute_spec (parser);
25618 if (attr_spec == NULL_TREE)
25619 break;
25620 if (attr_spec == error_mark_node)
25621 return error_mark_node;
25622
25623 if (attr_last)
25624 TREE_CHAIN (attr_last) = attr_spec;
25625 else
25626 attr_specs = attr_last = attr_spec;
25627 attr_last = tree_last (attr_last);
25628 }
25629
25630 return attr_specs;
25631 }
25632
25633 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25634 return index of the first token after balanced-token, or N on failure. */
25635
25636 static size_t
25637 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25638 {
25639 size_t orig_n = n;
25640 int nparens = 0, nbraces = 0, nsquares = 0;
25641 do
25642 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25643 {
25644 case CPP_EOF:
25645 case CPP_PRAGMA_EOL:
25646 /* Ran out of tokens. */
25647 return orig_n;
25648 case CPP_OPEN_PAREN:
25649 ++nparens;
25650 break;
25651 case CPP_OPEN_BRACE:
25652 ++nbraces;
25653 break;
25654 case CPP_OPEN_SQUARE:
25655 ++nsquares;
25656 break;
25657 case CPP_CLOSE_PAREN:
25658 --nparens;
25659 break;
25660 case CPP_CLOSE_BRACE:
25661 --nbraces;
25662 break;
25663 case CPP_CLOSE_SQUARE:
25664 --nsquares;
25665 break;
25666 default:
25667 break;
25668 }
25669 while (nparens || nbraces || nsquares);
25670 return n;
25671 }
25672
25673 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25674 return index of the first token after the GNU attribute tokens, or N on
25675 failure. */
25676
25677 static size_t
25678 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25679 {
25680 while (true)
25681 {
25682 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25683 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25684 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25685 break;
25686
25687 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25688 if (n2 == n + 2)
25689 break;
25690 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25691 break;
25692 n = n2 + 1;
25693 }
25694 return n;
25695 }
25696
25697 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25698 next token), return index of the first token after the standard C++11
25699 attribute tokens, or N on failure. */
25700
25701 static size_t
25702 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25703 {
25704 while (true)
25705 {
25706 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25707 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25708 {
25709 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25710 if (n2 == n + 1)
25711 break;
25712 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25713 break;
25714 n = n2 + 1;
25715 }
25716 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25717 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25718 {
25719 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25720 if (n2 == n + 1)
25721 break;
25722 n = n2;
25723 }
25724 else
25725 break;
25726 }
25727 return n;
25728 }
25729
25730 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25731 as the next token), return index of the first token after the attribute
25732 tokens, or N on failure. */
25733
25734 static size_t
25735 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25736 {
25737 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25738 return cp_parser_skip_gnu_attributes_opt (parser, n);
25739 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25740 }
25741
25742 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25743 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25744 current value of the PEDANTIC flag, regardless of whether or not
25745 the `__extension__' keyword is present. The caller is responsible
25746 for restoring the value of the PEDANTIC flag. */
25747
25748 static bool
25749 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25750 {
25751 /* Save the old value of the PEDANTIC flag. */
25752 *saved_pedantic = pedantic;
25753
25754 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25755 {
25756 /* Consume the `__extension__' token. */
25757 cp_lexer_consume_token (parser->lexer);
25758 /* We're not being pedantic while the `__extension__' keyword is
25759 in effect. */
25760 pedantic = 0;
25761
25762 return true;
25763 }
25764
25765 return false;
25766 }
25767
25768 /* Parse a label declaration.
25769
25770 label-declaration:
25771 __label__ label-declarator-seq ;
25772
25773 label-declarator-seq:
25774 identifier , label-declarator-seq
25775 identifier */
25776
25777 static void
25778 cp_parser_label_declaration (cp_parser* parser)
25779 {
25780 /* Look for the `__label__' keyword. */
25781 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25782
25783 while (true)
25784 {
25785 tree identifier;
25786
25787 /* Look for an identifier. */
25788 identifier = cp_parser_identifier (parser);
25789 /* If we failed, stop. */
25790 if (identifier == error_mark_node)
25791 break;
25792 /* Declare it as a label. */
25793 finish_label_decl (identifier);
25794 /* If the next token is a `;', stop. */
25795 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25796 break;
25797 /* Look for the `,' separating the label declarations. */
25798 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25799 }
25800
25801 /* Look for the final `;'. */
25802 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25803 }
25804
25805 // -------------------------------------------------------------------------- //
25806 // Requires Clause
25807
25808 // Parse a requires clause.
25809 //
25810 // requires-clause:
25811 // 'requires' logical-or-expression
25812 //
25813 // The required logical-or-expression must be a constant expression. Note
25814 // that we don't check that the expression is constepxr here. We defer until
25815 // we analyze constraints and then, we only check atomic constraints.
25816 static tree
25817 cp_parser_requires_clause (cp_parser *parser)
25818 {
25819 // Parse the requires clause so that it is not automatically folded.
25820 ++processing_template_decl;
25821 tree expr = cp_parser_binary_expression (parser, false, false,
25822 PREC_NOT_OPERATOR, NULL);
25823 if (check_for_bare_parameter_packs (expr))
25824 expr = error_mark_node;
25825 --processing_template_decl;
25826 return expr;
25827 }
25828
25829 // Optionally parse a requires clause:
25830 static tree
25831 cp_parser_requires_clause_opt (cp_parser *parser)
25832 {
25833 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25834 if (tok->keyword != RID_REQUIRES)
25835 {
25836 if (!flag_concepts && tok->type == CPP_NAME
25837 && tok->u.value == ridpointers[RID_REQUIRES])
25838 {
25839 error_at (cp_lexer_peek_token (parser->lexer)->location,
25840 "%<requires%> only available with -fconcepts");
25841 /* Parse and discard the requires-clause. */
25842 cp_lexer_consume_token (parser->lexer);
25843 cp_parser_requires_clause (parser);
25844 }
25845 return NULL_TREE;
25846 }
25847 cp_lexer_consume_token (parser->lexer);
25848 return cp_parser_requires_clause (parser);
25849 }
25850
25851
25852 /*---------------------------------------------------------------------------
25853 Requires expressions
25854 ---------------------------------------------------------------------------*/
25855
25856 /* Parse a requires expression
25857
25858 requirement-expression:
25859 'requires' requirement-parameter-list [opt] requirement-body */
25860 static tree
25861 cp_parser_requires_expression (cp_parser *parser)
25862 {
25863 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25864 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25865
25866 /* A requires-expression shall appear only within a concept
25867 definition or a requires-clause.
25868
25869 TODO: Implement this diagnostic correctly. */
25870 if (!processing_template_decl)
25871 {
25872 error_at (loc, "a requires expression cannot appear outside a template");
25873 cp_parser_skip_to_end_of_statement (parser);
25874 return error_mark_node;
25875 }
25876
25877 tree parms, reqs;
25878 {
25879 /* Local parameters are delared as variables within the scope
25880 of the expression. They are not visible past the end of
25881 the expression. Expressions within the requires-expression
25882 are unevaluated. */
25883 struct scope_sentinel
25884 {
25885 scope_sentinel ()
25886 {
25887 ++cp_unevaluated_operand;
25888 begin_scope (sk_block, NULL_TREE);
25889 }
25890
25891 ~scope_sentinel ()
25892 {
25893 pop_bindings_and_leave_scope ();
25894 --cp_unevaluated_operand;
25895 }
25896 } s;
25897
25898 /* Parse the optional parameter list. */
25899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25900 {
25901 parms = cp_parser_requirement_parameter_list (parser);
25902 if (parms == error_mark_node)
25903 return error_mark_node;
25904 }
25905 else
25906 parms = NULL_TREE;
25907
25908 /* Parse the requirement body. */
25909 reqs = cp_parser_requirement_body (parser);
25910 if (reqs == error_mark_node)
25911 return error_mark_node;
25912 }
25913
25914 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25915 the parm chain. */
25916 grokparms (parms, &parms);
25917 return finish_requires_expr (parms, reqs);
25918 }
25919
25920 /* Parse a parameterized requirement.
25921
25922 requirement-parameter-list:
25923 '(' parameter-declaration-clause ')' */
25924 static tree
25925 cp_parser_requirement_parameter_list (cp_parser *parser)
25926 {
25927 matching_parens parens;
25928 if (!parens.require_open (parser))
25929 return error_mark_node;
25930
25931 tree parms = cp_parser_parameter_declaration_clause (parser);
25932
25933 if (!parens.require_close (parser))
25934 return error_mark_node;
25935
25936 return parms;
25937 }
25938
25939 /* Parse the body of a requirement.
25940
25941 requirement-body:
25942 '{' requirement-list '}' */
25943 static tree
25944 cp_parser_requirement_body (cp_parser *parser)
25945 {
25946 matching_braces braces;
25947 if (!braces.require_open (parser))
25948 return error_mark_node;
25949
25950 tree reqs = cp_parser_requirement_list (parser);
25951
25952 if (!braces.require_close (parser))
25953 return error_mark_node;
25954
25955 return reqs;
25956 }
25957
25958 /* Parse a list of requirements.
25959
25960 requirement-list:
25961 requirement
25962 requirement-list ';' requirement[opt] */
25963 static tree
25964 cp_parser_requirement_list (cp_parser *parser)
25965 {
25966 tree result = NULL_TREE;
25967 while (true)
25968 {
25969 tree req = cp_parser_requirement (parser);
25970 if (req == error_mark_node)
25971 return error_mark_node;
25972
25973 result = tree_cons (NULL_TREE, req, result);
25974
25975 /* If we see a semi-colon, consume it. */
25976 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25977 cp_lexer_consume_token (parser->lexer);
25978
25979 /* Stop processing at the end of the list. */
25980 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25981 break;
25982 }
25983
25984 /* Reverse the order of requirements so they are analyzed in
25985 declaration order. */
25986 return nreverse (result);
25987 }
25988
25989 /* Parse a syntactic requirement or type requirement.
25990
25991 requirement:
25992 simple-requirement
25993 compound-requirement
25994 type-requirement
25995 nested-requirement */
25996 static tree
25997 cp_parser_requirement (cp_parser *parser)
25998 {
25999 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26000 return cp_parser_compound_requirement (parser);
26001 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26002 return cp_parser_type_requirement (parser);
26003 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26004 return cp_parser_nested_requirement (parser);
26005 else
26006 return cp_parser_simple_requirement (parser);
26007 }
26008
26009 /* Parse a simple requirement.
26010
26011 simple-requirement:
26012 expression ';' */
26013 static tree
26014 cp_parser_simple_requirement (cp_parser *parser)
26015 {
26016 tree expr = cp_parser_expression (parser, NULL, false, false);
26017 if (!expr || expr == error_mark_node)
26018 return error_mark_node;
26019
26020 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26021 return error_mark_node;
26022
26023 return finish_simple_requirement (expr);
26024 }
26025
26026 /* Parse a type requirement
26027
26028 type-requirement
26029 nested-name-specifier [opt] required-type-name ';'
26030
26031 required-type-name:
26032 type-name
26033 'template' [opt] simple-template-id */
26034 static tree
26035 cp_parser_type_requirement (cp_parser *parser)
26036 {
26037 cp_lexer_consume_token (parser->lexer);
26038
26039 // Save the scope before parsing name specifiers.
26040 tree saved_scope = parser->scope;
26041 tree saved_object_scope = parser->object_scope;
26042 tree saved_qualifying_scope = parser->qualifying_scope;
26043 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26044 cp_parser_nested_name_specifier_opt (parser,
26045 /*typename_keyword_p=*/true,
26046 /*check_dependency_p=*/false,
26047 /*type_p=*/true,
26048 /*is_declaration=*/false);
26049
26050 tree type;
26051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26052 {
26053 cp_lexer_consume_token (parser->lexer);
26054 type = cp_parser_template_id (parser,
26055 /*template_keyword_p=*/true,
26056 /*check_dependency=*/false,
26057 /*tag_type=*/none_type,
26058 /*is_declaration=*/false);
26059 type = make_typename_type (parser->scope, type, typename_type,
26060 /*complain=*/tf_error);
26061 }
26062 else
26063 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26064
26065 if (TREE_CODE (type) == TYPE_DECL)
26066 type = TREE_TYPE (type);
26067
26068 parser->scope = saved_scope;
26069 parser->object_scope = saved_object_scope;
26070 parser->qualifying_scope = saved_qualifying_scope;
26071
26072 if (type == error_mark_node)
26073 cp_parser_skip_to_end_of_statement (parser);
26074
26075 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26076 return error_mark_node;
26077 if (type == error_mark_node)
26078 return error_mark_node;
26079
26080 return finish_type_requirement (type);
26081 }
26082
26083 /* Parse a compound requirement
26084
26085 compound-requirement:
26086 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26087 static tree
26088 cp_parser_compound_requirement (cp_parser *parser)
26089 {
26090 /* Parse an expression enclosed in '{ }'s. */
26091 matching_braces braces;
26092 if (!braces.require_open (parser))
26093 return error_mark_node;
26094
26095 tree expr = cp_parser_expression (parser, NULL, false, false);
26096 if (!expr || expr == error_mark_node)
26097 return error_mark_node;
26098
26099 if (!braces.require_close (parser))
26100 return error_mark_node;
26101
26102 /* Parse the optional noexcept. */
26103 bool noexcept_p = false;
26104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26105 {
26106 cp_lexer_consume_token (parser->lexer);
26107 noexcept_p = true;
26108 }
26109
26110 /* Parse the optional trailing return type. */
26111 tree type = NULL_TREE;
26112 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26113 {
26114 cp_lexer_consume_token (parser->lexer);
26115 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26116 parser->in_result_type_constraint_p = true;
26117 type = cp_parser_trailing_type_id (parser);
26118 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26119 if (type == error_mark_node)
26120 return error_mark_node;
26121 }
26122
26123 return finish_compound_requirement (expr, type, noexcept_p);
26124 }
26125
26126 /* Parse a nested requirement. This is the same as a requires clause.
26127
26128 nested-requirement:
26129 requires-clause */
26130 static tree
26131 cp_parser_nested_requirement (cp_parser *parser)
26132 {
26133 cp_lexer_consume_token (parser->lexer);
26134 tree req = cp_parser_requires_clause (parser);
26135 if (req == error_mark_node)
26136 return error_mark_node;
26137 return finish_nested_requirement (req);
26138 }
26139
26140 /* Support Functions */
26141
26142 /* Return the appropriate prefer_type argument for lookup_name_real based on
26143 tag_type and template_mem_access. */
26144
26145 static inline int
26146 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26147 {
26148 /* DR 141: When looking in the current enclosing context for a template-name
26149 after -> or ., only consider class templates. */
26150 if (template_mem_access)
26151 return 2;
26152 switch (tag_type)
26153 {
26154 case none_type: return 0; // No preference.
26155 case scope_type: return 1; // Type or namespace.
26156 default: return 2; // Type only.
26157 }
26158 }
26159
26160 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26161 NAME should have one of the representations used for an
26162 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26163 is returned. If PARSER->SCOPE is a dependent type, then a
26164 SCOPE_REF is returned.
26165
26166 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26167 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26168 was formed. Abstractly, such entities should not be passed to this
26169 function, because they do not need to be looked up, but it is
26170 simpler to check for this special case here, rather than at the
26171 call-sites.
26172
26173 In cases not explicitly covered above, this function returns a
26174 DECL, OVERLOAD, or baselink representing the result of the lookup.
26175 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26176 is returned.
26177
26178 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26179 (e.g., "struct") that was used. In that case bindings that do not
26180 refer to types are ignored.
26181
26182 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26183 ignored.
26184
26185 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26186 are ignored.
26187
26188 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26189 types.
26190
26191 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26192 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26193 NULL_TREE otherwise. */
26194
26195 static cp_expr
26196 cp_parser_lookup_name (cp_parser *parser, tree name,
26197 enum tag_types tag_type,
26198 bool is_template,
26199 bool is_namespace,
26200 bool check_dependency,
26201 tree *ambiguous_decls,
26202 location_t name_location)
26203 {
26204 tree decl;
26205 tree object_type = parser->context->object_type;
26206
26207 /* Assume that the lookup will be unambiguous. */
26208 if (ambiguous_decls)
26209 *ambiguous_decls = NULL_TREE;
26210
26211 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26212 no longer valid. Note that if we are parsing tentatively, and
26213 the parse fails, OBJECT_TYPE will be automatically restored. */
26214 parser->context->object_type = NULL_TREE;
26215
26216 if (name == error_mark_node)
26217 return error_mark_node;
26218
26219 /* A template-id has already been resolved; there is no lookup to
26220 do. */
26221 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26222 return name;
26223 if (BASELINK_P (name))
26224 {
26225 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26226 == TEMPLATE_ID_EXPR);
26227 return name;
26228 }
26229
26230 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26231 it should already have been checked to make sure that the name
26232 used matches the type being destroyed. */
26233 if (TREE_CODE (name) == BIT_NOT_EXPR)
26234 {
26235 tree type;
26236
26237 /* Figure out to which type this destructor applies. */
26238 if (parser->scope)
26239 type = parser->scope;
26240 else if (object_type)
26241 type = object_type;
26242 else
26243 type = current_class_type;
26244 /* If that's not a class type, there is no destructor. */
26245 if (!type || !CLASS_TYPE_P (type))
26246 return error_mark_node;
26247
26248 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26249 lazily_declare_fn (sfk_destructor, type);
26250
26251 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26252 return dtor;
26253
26254 return error_mark_node;
26255 }
26256
26257 /* By this point, the NAME should be an ordinary identifier. If
26258 the id-expression was a qualified name, the qualifying scope is
26259 stored in PARSER->SCOPE at this point. */
26260 gcc_assert (identifier_p (name));
26261
26262 /* Perform the lookup. */
26263 if (parser->scope)
26264 {
26265 bool dependent_p;
26266
26267 if (parser->scope == error_mark_node)
26268 return error_mark_node;
26269
26270 /* If the SCOPE is dependent, the lookup must be deferred until
26271 the template is instantiated -- unless we are explicitly
26272 looking up names in uninstantiated templates. Even then, we
26273 cannot look up the name if the scope is not a class type; it
26274 might, for example, be a template type parameter. */
26275 dependent_p = (TYPE_P (parser->scope)
26276 && dependent_scope_p (parser->scope));
26277 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26278 && dependent_p)
26279 /* Defer lookup. */
26280 decl = error_mark_node;
26281 else
26282 {
26283 tree pushed_scope = NULL_TREE;
26284
26285 /* If PARSER->SCOPE is a dependent type, then it must be a
26286 class type, and we must not be checking dependencies;
26287 otherwise, we would have processed this lookup above. So
26288 that PARSER->SCOPE is not considered a dependent base by
26289 lookup_member, we must enter the scope here. */
26290 if (dependent_p)
26291 pushed_scope = push_scope (parser->scope);
26292
26293 /* If the PARSER->SCOPE is a template specialization, it
26294 may be instantiated during name lookup. In that case,
26295 errors may be issued. Even if we rollback the current
26296 tentative parse, those errors are valid. */
26297 decl = lookup_qualified_name (parser->scope, name,
26298 prefer_type_arg (tag_type),
26299 /*complain=*/true);
26300
26301 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26302 lookup result and the nested-name-specifier nominates a class C:
26303 * if the name specified after the nested-name-specifier, when
26304 looked up in C, is the injected-class-name of C (Clause 9), or
26305 * if the name specified after the nested-name-specifier is the
26306 same as the identifier or the simple-template-id's template-
26307 name in the last component of the nested-name-specifier,
26308 the name is instead considered to name the constructor of
26309 class C. [ Note: for example, the constructor is not an
26310 acceptable lookup result in an elaborated-type-specifier so
26311 the constructor would not be used in place of the
26312 injected-class-name. --end note ] Such a constructor name
26313 shall be used only in the declarator-id of a declaration that
26314 names a constructor or in a using-declaration. */
26315 if (tag_type == none_type
26316 && DECL_SELF_REFERENCE_P (decl)
26317 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26318 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26319 prefer_type_arg (tag_type),
26320 /*complain=*/true);
26321
26322 /* If we have a single function from a using decl, pull it out. */
26323 if (TREE_CODE (decl) == OVERLOAD
26324 && !really_overloaded_fn (decl))
26325 decl = OVL_FUNCTION (decl);
26326
26327 if (pushed_scope)
26328 pop_scope (pushed_scope);
26329 }
26330
26331 /* If the scope is a dependent type and either we deferred lookup or
26332 we did lookup but didn't find the name, rememeber the name. */
26333 if (decl == error_mark_node && TYPE_P (parser->scope)
26334 && dependent_type_p (parser->scope))
26335 {
26336 if (tag_type)
26337 {
26338 tree type;
26339
26340 /* The resolution to Core Issue 180 says that `struct
26341 A::B' should be considered a type-name, even if `A'
26342 is dependent. */
26343 type = make_typename_type (parser->scope, name, tag_type,
26344 /*complain=*/tf_error);
26345 if (type != error_mark_node)
26346 decl = TYPE_NAME (type);
26347 }
26348 else if (is_template
26349 && (cp_parser_next_token_ends_template_argument_p (parser)
26350 || cp_lexer_next_token_is (parser->lexer,
26351 CPP_CLOSE_PAREN)))
26352 decl = make_unbound_class_template (parser->scope,
26353 name, NULL_TREE,
26354 /*complain=*/tf_error);
26355 else
26356 decl = build_qualified_name (/*type=*/NULL_TREE,
26357 parser->scope, name,
26358 is_template);
26359 }
26360 parser->qualifying_scope = parser->scope;
26361 parser->object_scope = NULL_TREE;
26362 }
26363 else if (object_type)
26364 {
26365 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26366 OBJECT_TYPE is not a class. */
26367 if (CLASS_TYPE_P (object_type))
26368 /* If the OBJECT_TYPE is a template specialization, it may
26369 be instantiated during name lookup. In that case, errors
26370 may be issued. Even if we rollback the current tentative
26371 parse, those errors are valid. */
26372 decl = lookup_member (object_type,
26373 name,
26374 /*protect=*/0,
26375 prefer_type_arg (tag_type),
26376 tf_warning_or_error);
26377 else
26378 decl = NULL_TREE;
26379
26380 if (!decl)
26381 /* Look it up in the enclosing context. DR 141: When looking for a
26382 template-name after -> or ., only consider class templates. */
26383 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26384 /*nonclass=*/0,
26385 /*block_p=*/true, is_namespace, 0);
26386 if (object_type == unknown_type_node)
26387 /* The object is type-dependent, so we can't look anything up; we used
26388 this to get the DR 141 behavior. */
26389 object_type = NULL_TREE;
26390 parser->object_scope = object_type;
26391 parser->qualifying_scope = NULL_TREE;
26392 }
26393 else
26394 {
26395 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26396 /*nonclass=*/0,
26397 /*block_p=*/true, is_namespace, 0);
26398 parser->qualifying_scope = NULL_TREE;
26399 parser->object_scope = NULL_TREE;
26400 }
26401
26402 /* If the lookup failed, let our caller know. */
26403 if (!decl || decl == error_mark_node)
26404 return error_mark_node;
26405
26406 /* Pull out the template from an injected-class-name (or multiple). */
26407 if (is_template)
26408 decl = maybe_get_template_decl_from_type_decl (decl);
26409
26410 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26411 if (TREE_CODE (decl) == TREE_LIST)
26412 {
26413 if (ambiguous_decls)
26414 *ambiguous_decls = decl;
26415 /* The error message we have to print is too complicated for
26416 cp_parser_error, so we incorporate its actions directly. */
26417 if (!cp_parser_simulate_error (parser))
26418 {
26419 error_at (name_location, "reference to %qD is ambiguous",
26420 name);
26421 print_candidates (decl);
26422 }
26423 return error_mark_node;
26424 }
26425
26426 gcc_assert (DECL_P (decl)
26427 || TREE_CODE (decl) == OVERLOAD
26428 || TREE_CODE (decl) == SCOPE_REF
26429 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26430 || BASELINK_P (decl));
26431
26432 /* If we have resolved the name of a member declaration, check to
26433 see if the declaration is accessible. When the name resolves to
26434 set of overloaded functions, accessibility is checked when
26435 overload resolution is done.
26436
26437 During an explicit instantiation, access is not checked at all,
26438 as per [temp.explicit]. */
26439 if (DECL_P (decl))
26440 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26441
26442 maybe_record_typedef_use (decl);
26443
26444 return cp_expr (decl, name_location);
26445 }
26446
26447 /* Like cp_parser_lookup_name, but for use in the typical case where
26448 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26449 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26450
26451 static tree
26452 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26453 {
26454 return cp_parser_lookup_name (parser, name,
26455 none_type,
26456 /*is_template=*/false,
26457 /*is_namespace=*/false,
26458 /*check_dependency=*/true,
26459 /*ambiguous_decls=*/NULL,
26460 location);
26461 }
26462
26463 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26464 the current context, return the TYPE_DECL. If TAG_NAME_P is
26465 true, the DECL indicates the class being defined in a class-head,
26466 or declared in an elaborated-type-specifier.
26467
26468 Otherwise, return DECL. */
26469
26470 static tree
26471 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26472 {
26473 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26474 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26475
26476 struct A {
26477 template <typename T> struct B;
26478 };
26479
26480 template <typename T> struct A::B {};
26481
26482 Similarly, in an elaborated-type-specifier:
26483
26484 namespace N { struct X{}; }
26485
26486 struct A {
26487 template <typename T> friend struct N::X;
26488 };
26489
26490 However, if the DECL refers to a class type, and we are in
26491 the scope of the class, then the name lookup automatically
26492 finds the TYPE_DECL created by build_self_reference rather
26493 than a TEMPLATE_DECL. For example, in:
26494
26495 template <class T> struct S {
26496 S s;
26497 };
26498
26499 there is no need to handle such case. */
26500
26501 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26502 return DECL_TEMPLATE_RESULT (decl);
26503
26504 return decl;
26505 }
26506
26507 /* If too many, or too few, template-parameter lists apply to the
26508 declarator, issue an error message. Returns TRUE if all went well,
26509 and FALSE otherwise. */
26510
26511 static bool
26512 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26513 cp_declarator *declarator,
26514 location_t declarator_location)
26515 {
26516 switch (declarator->kind)
26517 {
26518 case cdk_id:
26519 {
26520 unsigned num_templates = 0;
26521 tree scope = declarator->u.id.qualifying_scope;
26522 bool template_id_p = false;
26523
26524 if (scope)
26525 num_templates = num_template_headers_for_class (scope);
26526 else if (TREE_CODE (declarator->u.id.unqualified_name)
26527 == TEMPLATE_ID_EXPR)
26528 {
26529 /* If the DECLARATOR has the form `X<y>' then it uses one
26530 additional level of template parameters. */
26531 ++num_templates;
26532 template_id_p = true;
26533 }
26534
26535 return cp_parser_check_template_parameters
26536 (parser, num_templates, template_id_p, declarator_location,
26537 declarator);
26538 }
26539
26540 case cdk_function:
26541 case cdk_array:
26542 case cdk_pointer:
26543 case cdk_reference:
26544 case cdk_ptrmem:
26545 return (cp_parser_check_declarator_template_parameters
26546 (parser, declarator->declarator, declarator_location));
26547
26548 case cdk_decomp:
26549 case cdk_error:
26550 return true;
26551
26552 default:
26553 gcc_unreachable ();
26554 }
26555 return false;
26556 }
26557
26558 /* NUM_TEMPLATES were used in the current declaration. If that is
26559 invalid, return FALSE and issue an error messages. Otherwise,
26560 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26561 declarator and we can print more accurate diagnostics. */
26562
26563 static bool
26564 cp_parser_check_template_parameters (cp_parser* parser,
26565 unsigned num_templates,
26566 bool template_id_p,
26567 location_t location,
26568 cp_declarator *declarator)
26569 {
26570 /* If there are the same number of template classes and parameter
26571 lists, that's OK. */
26572 if (parser->num_template_parameter_lists == num_templates)
26573 return true;
26574 /* If there are more, but only one more, and the name ends in an identifier,
26575 then we are declaring a primary template. That's OK too. */
26576 if (!template_id_p
26577 && parser->num_template_parameter_lists == num_templates + 1)
26578 return true;
26579 /* If there are more template classes than parameter lists, we have
26580 something like:
26581
26582 template <class T> void S<T>::R<T>::f (); */
26583 if (parser->num_template_parameter_lists < num_templates)
26584 {
26585 if (declarator && !current_function_decl)
26586 error_at (location, "specializing member %<%T::%E%> "
26587 "requires %<template<>%> syntax",
26588 declarator->u.id.qualifying_scope,
26589 declarator->u.id.unqualified_name);
26590 else if (declarator)
26591 error_at (location, "invalid declaration of %<%T::%E%>",
26592 declarator->u.id.qualifying_scope,
26593 declarator->u.id.unqualified_name);
26594 else
26595 error_at (location, "too few template-parameter-lists");
26596 return false;
26597 }
26598 /* Otherwise, there are too many template parameter lists. We have
26599 something like:
26600
26601 template <class T> template <class U> void S::f(); */
26602 error_at (location, "too many template-parameter-lists");
26603 return false;
26604 }
26605
26606 /* Parse an optional `::' token indicating that the following name is
26607 from the global namespace. If so, PARSER->SCOPE is set to the
26608 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26609 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26610 Returns the new value of PARSER->SCOPE, if the `::' token is
26611 present, and NULL_TREE otherwise. */
26612
26613 static tree
26614 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26615 {
26616 cp_token *token;
26617
26618 /* Peek at the next token. */
26619 token = cp_lexer_peek_token (parser->lexer);
26620 /* If we're looking at a `::' token then we're starting from the
26621 global namespace, not our current location. */
26622 if (token->type == CPP_SCOPE)
26623 {
26624 /* Consume the `::' token. */
26625 cp_lexer_consume_token (parser->lexer);
26626 /* Set the SCOPE so that we know where to start the lookup. */
26627 parser->scope = global_namespace;
26628 parser->qualifying_scope = global_namespace;
26629 parser->object_scope = NULL_TREE;
26630
26631 return parser->scope;
26632 }
26633 else if (!current_scope_valid_p)
26634 {
26635 parser->scope = NULL_TREE;
26636 parser->qualifying_scope = NULL_TREE;
26637 parser->object_scope = NULL_TREE;
26638 }
26639
26640 return NULL_TREE;
26641 }
26642
26643 /* Returns TRUE if the upcoming token sequence is the start of a
26644 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26645 declarator is preceded by the `friend' specifier. */
26646
26647 static bool
26648 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26649 {
26650 bool constructor_p;
26651 bool outside_class_specifier_p;
26652 tree nested_name_specifier;
26653 cp_token *next_token;
26654
26655 /* The common case is that this is not a constructor declarator, so
26656 try to avoid doing lots of work if at all possible. It's not
26657 valid declare a constructor at function scope. */
26658 if (parser->in_function_body)
26659 return false;
26660 /* And only certain tokens can begin a constructor declarator. */
26661 next_token = cp_lexer_peek_token (parser->lexer);
26662 if (next_token->type != CPP_NAME
26663 && next_token->type != CPP_SCOPE
26664 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26665 && next_token->type != CPP_TEMPLATE_ID)
26666 return false;
26667
26668 /* Parse tentatively; we are going to roll back all of the tokens
26669 consumed here. */
26670 cp_parser_parse_tentatively (parser);
26671 /* Assume that we are looking at a constructor declarator. */
26672 constructor_p = true;
26673
26674 /* Look for the optional `::' operator. */
26675 cp_parser_global_scope_opt (parser,
26676 /*current_scope_valid_p=*/false);
26677 /* Look for the nested-name-specifier. */
26678 nested_name_specifier
26679 = (cp_parser_nested_name_specifier_opt (parser,
26680 /*typename_keyword_p=*/false,
26681 /*check_dependency_p=*/false,
26682 /*type_p=*/false,
26683 /*is_declaration=*/false));
26684
26685 outside_class_specifier_p = (!at_class_scope_p ()
26686 || !TYPE_BEING_DEFINED (current_class_type)
26687 || friend_p);
26688
26689 /* Outside of a class-specifier, there must be a
26690 nested-name-specifier. Except in C++17 mode, where we
26691 might be declaring a guiding declaration. */
26692 if (!nested_name_specifier && outside_class_specifier_p
26693 && cxx_dialect < cxx17)
26694 constructor_p = false;
26695 else if (nested_name_specifier == error_mark_node)
26696 constructor_p = false;
26697
26698 /* If we have a class scope, this is easy; DR 147 says that S::S always
26699 names the constructor, and no other qualified name could. */
26700 if (constructor_p && nested_name_specifier
26701 && CLASS_TYPE_P (nested_name_specifier))
26702 {
26703 tree id = cp_parser_unqualified_id (parser,
26704 /*template_keyword_p=*/false,
26705 /*check_dependency_p=*/false,
26706 /*declarator_p=*/true,
26707 /*optional_p=*/false);
26708 if (is_overloaded_fn (id))
26709 id = DECL_NAME (get_first_fn (id));
26710 if (!constructor_name_p (id, nested_name_specifier))
26711 constructor_p = false;
26712 }
26713 /* If we still think that this might be a constructor-declarator,
26714 look for a class-name. */
26715 else if (constructor_p)
26716 {
26717 /* If we have:
26718
26719 template <typename T> struct S {
26720 S();
26721 };
26722
26723 we must recognize that the nested `S' names a class. */
26724 if (cxx_dialect >= cxx17)
26725 cp_parser_parse_tentatively (parser);
26726
26727 tree type_decl;
26728 type_decl = cp_parser_class_name (parser,
26729 /*typename_keyword_p=*/false,
26730 /*template_keyword_p=*/false,
26731 none_type,
26732 /*check_dependency_p=*/false,
26733 /*class_head_p=*/false,
26734 /*is_declaration=*/false);
26735
26736 if (cxx_dialect >= cxx17
26737 && !cp_parser_parse_definitely (parser))
26738 {
26739 type_decl = NULL_TREE;
26740 tree tmpl = cp_parser_template_name (parser,
26741 /*template_keyword*/false,
26742 /*check_dependency_p*/false,
26743 /*is_declaration*/false,
26744 none_type,
26745 /*is_identifier*/NULL);
26746 if (DECL_CLASS_TEMPLATE_P (tmpl)
26747 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26748 /* It's a deduction guide, return true. */;
26749 else
26750 cp_parser_simulate_error (parser);
26751 }
26752
26753 /* If there was no class-name, then this is not a constructor.
26754 Otherwise, if we are in a class-specifier and we aren't
26755 handling a friend declaration, check that its type matches
26756 current_class_type (c++/38313). Note: error_mark_node
26757 is left alone for error recovery purposes. */
26758 constructor_p = (!cp_parser_error_occurred (parser)
26759 && (outside_class_specifier_p
26760 || type_decl == NULL_TREE
26761 || type_decl == error_mark_node
26762 || same_type_p (current_class_type,
26763 TREE_TYPE (type_decl))));
26764
26765 /* If we're still considering a constructor, we have to see a `(',
26766 to begin the parameter-declaration-clause, followed by either a
26767 `)', an `...', or a decl-specifier. We need to check for a
26768 type-specifier to avoid being fooled into thinking that:
26769
26770 S (f) (int);
26771
26772 is a constructor. (It is actually a function named `f' that
26773 takes one parameter (of type `int') and returns a value of type
26774 `S'. */
26775 if (constructor_p
26776 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26777 constructor_p = false;
26778
26779 if (constructor_p
26780 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26781 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26782 /* A parameter declaration begins with a decl-specifier,
26783 which is either the "attribute" keyword, a storage class
26784 specifier, or (usually) a type-specifier. */
26785 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26786 {
26787 tree type;
26788 tree pushed_scope = NULL_TREE;
26789 unsigned saved_num_template_parameter_lists;
26790
26791 /* Names appearing in the type-specifier should be looked up
26792 in the scope of the class. */
26793 if (current_class_type)
26794 type = NULL_TREE;
26795 else if (type_decl)
26796 {
26797 type = TREE_TYPE (type_decl);
26798 if (TREE_CODE (type) == TYPENAME_TYPE)
26799 {
26800 type = resolve_typename_type (type,
26801 /*only_current_p=*/false);
26802 if (TREE_CODE (type) == TYPENAME_TYPE)
26803 {
26804 cp_parser_abort_tentative_parse (parser);
26805 return false;
26806 }
26807 }
26808 pushed_scope = push_scope (type);
26809 }
26810
26811 /* Inside the constructor parameter list, surrounding
26812 template-parameter-lists do not apply. */
26813 saved_num_template_parameter_lists
26814 = parser->num_template_parameter_lists;
26815 parser->num_template_parameter_lists = 0;
26816
26817 /* Look for the type-specifier. */
26818 cp_parser_type_specifier (parser,
26819 CP_PARSER_FLAGS_NONE,
26820 /*decl_specs=*/NULL,
26821 /*is_declarator=*/true,
26822 /*declares_class_or_enum=*/NULL,
26823 /*is_cv_qualifier=*/NULL);
26824
26825 parser->num_template_parameter_lists
26826 = saved_num_template_parameter_lists;
26827
26828 /* Leave the scope of the class. */
26829 if (pushed_scope)
26830 pop_scope (pushed_scope);
26831
26832 constructor_p = !cp_parser_error_occurred (parser);
26833 }
26834 }
26835
26836 /* We did not really want to consume any tokens. */
26837 cp_parser_abort_tentative_parse (parser);
26838
26839 return constructor_p;
26840 }
26841
26842 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26843 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26844 they must be performed once we are in the scope of the function.
26845
26846 Returns the function defined. */
26847
26848 static tree
26849 cp_parser_function_definition_from_specifiers_and_declarator
26850 (cp_parser* parser,
26851 cp_decl_specifier_seq *decl_specifiers,
26852 tree attributes,
26853 const cp_declarator *declarator)
26854 {
26855 tree fn;
26856 bool success_p;
26857
26858 /* Begin the function-definition. */
26859 success_p = start_function (decl_specifiers, declarator, attributes);
26860
26861 /* The things we're about to see are not directly qualified by any
26862 template headers we've seen thus far. */
26863 reset_specialization ();
26864
26865 /* If there were names looked up in the decl-specifier-seq that we
26866 did not check, check them now. We must wait until we are in the
26867 scope of the function to perform the checks, since the function
26868 might be a friend. */
26869 perform_deferred_access_checks (tf_warning_or_error);
26870
26871 if (success_p)
26872 {
26873 cp_finalize_omp_declare_simd (parser, current_function_decl);
26874 parser->omp_declare_simd = NULL;
26875 cp_finalize_oacc_routine (parser, current_function_decl, true);
26876 parser->oacc_routine = NULL;
26877 }
26878
26879 if (!success_p)
26880 {
26881 /* Skip the entire function. */
26882 cp_parser_skip_to_end_of_block_or_statement (parser);
26883 fn = error_mark_node;
26884 }
26885 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26886 {
26887 /* Seen already, skip it. An error message has already been output. */
26888 cp_parser_skip_to_end_of_block_or_statement (parser);
26889 fn = current_function_decl;
26890 current_function_decl = NULL_TREE;
26891 /* If this is a function from a class, pop the nested class. */
26892 if (current_class_name)
26893 pop_nested_class ();
26894 }
26895 else
26896 {
26897 timevar_id_t tv;
26898 if (DECL_DECLARED_INLINE_P (current_function_decl))
26899 tv = TV_PARSE_INLINE;
26900 else
26901 tv = TV_PARSE_FUNC;
26902 timevar_push (tv);
26903 fn = cp_parser_function_definition_after_declarator (parser,
26904 /*inline_p=*/false);
26905 timevar_pop (tv);
26906 }
26907
26908 return fn;
26909 }
26910
26911 /* Parse the part of a function-definition that follows the
26912 declarator. INLINE_P is TRUE iff this function is an inline
26913 function defined within a class-specifier.
26914
26915 Returns the function defined. */
26916
26917 static tree
26918 cp_parser_function_definition_after_declarator (cp_parser* parser,
26919 bool inline_p)
26920 {
26921 tree fn;
26922 bool saved_in_unbraced_linkage_specification_p;
26923 bool saved_in_function_body;
26924 unsigned saved_num_template_parameter_lists;
26925 cp_token *token;
26926 bool fully_implicit_function_template_p
26927 = parser->fully_implicit_function_template_p;
26928 parser->fully_implicit_function_template_p = false;
26929 tree implicit_template_parms
26930 = parser->implicit_template_parms;
26931 parser->implicit_template_parms = 0;
26932 cp_binding_level* implicit_template_scope
26933 = parser->implicit_template_scope;
26934 parser->implicit_template_scope = 0;
26935
26936 saved_in_function_body = parser->in_function_body;
26937 parser->in_function_body = true;
26938 /* If the next token is `return', then the code may be trying to
26939 make use of the "named return value" extension that G++ used to
26940 support. */
26941 token = cp_lexer_peek_token (parser->lexer);
26942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26943 {
26944 /* Consume the `return' keyword. */
26945 cp_lexer_consume_token (parser->lexer);
26946 /* Look for the identifier that indicates what value is to be
26947 returned. */
26948 cp_parser_identifier (parser);
26949 /* Issue an error message. */
26950 error_at (token->location,
26951 "named return values are no longer supported");
26952 /* Skip tokens until we reach the start of the function body. */
26953 while (true)
26954 {
26955 cp_token *token = cp_lexer_peek_token (parser->lexer);
26956 if (token->type == CPP_OPEN_BRACE
26957 || token->type == CPP_EOF
26958 || token->type == CPP_PRAGMA_EOL)
26959 break;
26960 cp_lexer_consume_token (parser->lexer);
26961 }
26962 }
26963 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26964 anything declared inside `f'. */
26965 saved_in_unbraced_linkage_specification_p
26966 = parser->in_unbraced_linkage_specification_p;
26967 parser->in_unbraced_linkage_specification_p = false;
26968 /* Inside the function, surrounding template-parameter-lists do not
26969 apply. */
26970 saved_num_template_parameter_lists
26971 = parser->num_template_parameter_lists;
26972 parser->num_template_parameter_lists = 0;
26973
26974 /* If the next token is `try', `__transaction_atomic', or
26975 `__transaction_relaxed`, then we are looking at either function-try-block
26976 or function-transaction-block. Note that all of these include the
26977 function-body. */
26978 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26979 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26980 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26981 RID_TRANSACTION_RELAXED))
26982 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26983 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26984 cp_parser_function_try_block (parser);
26985 else
26986 cp_parser_ctor_initializer_opt_and_function_body
26987 (parser, /*in_function_try_block=*/false);
26988
26989 /* Finish the function. */
26990 fn = finish_function (inline_p);
26991 /* Generate code for it, if necessary. */
26992 expand_or_defer_fn (fn);
26993 /* Restore the saved values. */
26994 parser->in_unbraced_linkage_specification_p
26995 = saved_in_unbraced_linkage_specification_p;
26996 parser->num_template_parameter_lists
26997 = saved_num_template_parameter_lists;
26998 parser->in_function_body = saved_in_function_body;
26999
27000 parser->fully_implicit_function_template_p
27001 = fully_implicit_function_template_p;
27002 parser->implicit_template_parms
27003 = implicit_template_parms;
27004 parser->implicit_template_scope
27005 = implicit_template_scope;
27006
27007 if (parser->fully_implicit_function_template_p)
27008 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27009
27010 return fn;
27011 }
27012
27013 /* Parse a template-declaration body (following argument list). */
27014
27015 static void
27016 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27017 tree parameter_list,
27018 bool member_p)
27019 {
27020 tree decl = NULL_TREE;
27021 bool friend_p = false;
27022
27023 /* We just processed one more parameter list. */
27024 ++parser->num_template_parameter_lists;
27025
27026 /* Get the deferred access checks from the parameter list. These
27027 will be checked once we know what is being declared, as for a
27028 member template the checks must be performed in the scope of the
27029 class containing the member. */
27030 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27031
27032 /* Tentatively parse for a new template parameter list, which can either be
27033 the template keyword or a template introduction. */
27034 if (cp_parser_template_declaration_after_export (parser, member_p))
27035 /* OK */;
27036 else if (cxx_dialect >= cxx11
27037 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27038 decl = cp_parser_alias_declaration (parser);
27039 else
27040 {
27041 /* There are no access checks when parsing a template, as we do not
27042 know if a specialization will be a friend. */
27043 push_deferring_access_checks (dk_no_check);
27044 cp_token *token = cp_lexer_peek_token (parser->lexer);
27045 decl = cp_parser_single_declaration (parser,
27046 checks,
27047 member_p,
27048 /*explicit_specialization_p=*/false,
27049 &friend_p);
27050 pop_deferring_access_checks ();
27051
27052 /* If this is a member template declaration, let the front
27053 end know. */
27054 if (member_p && !friend_p && decl)
27055 {
27056 if (TREE_CODE (decl) == TYPE_DECL)
27057 cp_parser_check_access_in_redeclaration (decl, token->location);
27058
27059 decl = finish_member_template_decl (decl);
27060 }
27061 else if (friend_p && decl
27062 && DECL_DECLARES_TYPE_P (decl))
27063 make_friend_class (current_class_type, TREE_TYPE (decl),
27064 /*complain=*/true);
27065 }
27066 /* We are done with the current parameter list. */
27067 --parser->num_template_parameter_lists;
27068
27069 pop_deferring_access_checks ();
27070
27071 /* Finish up. */
27072 finish_template_decl (parameter_list);
27073
27074 /* Check the template arguments for a literal operator template. */
27075 if (decl
27076 && DECL_DECLARES_FUNCTION_P (decl)
27077 && UDLIT_OPER_P (DECL_NAME (decl)))
27078 {
27079 bool ok = true;
27080 if (parameter_list == NULL_TREE)
27081 ok = false;
27082 else
27083 {
27084 int num_parms = TREE_VEC_LENGTH (parameter_list);
27085 if (num_parms == 1)
27086 {
27087 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27088 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27089 if (TREE_TYPE (parm) != char_type_node
27090 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27091 ok = false;
27092 }
27093 else if (num_parms == 2 && cxx_dialect >= cxx14)
27094 {
27095 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27096 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27097 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27098 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27099 if (parm == error_mark_node
27100 || TREE_TYPE (parm) != TREE_TYPE (type)
27101 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27102 ok = false;
27103 }
27104 else
27105 ok = false;
27106 }
27107 if (!ok)
27108 {
27109 if (cxx_dialect >= cxx14)
27110 error ("literal operator template %qD has invalid parameter list."
27111 " Expected non-type template argument pack <char...>"
27112 " or <typename CharT, CharT...>",
27113 decl);
27114 else
27115 error ("literal operator template %qD has invalid parameter list."
27116 " Expected non-type template argument pack <char...>",
27117 decl);
27118 }
27119 }
27120
27121 /* Register member declarations. */
27122 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27123 finish_member_declaration (decl);
27124 /* If DECL is a function template, we must return to parse it later.
27125 (Even though there is no definition, there might be default
27126 arguments that need handling.) */
27127 if (member_p && decl
27128 && DECL_DECLARES_FUNCTION_P (decl))
27129 vec_safe_push (unparsed_funs_with_definitions, decl);
27130 }
27131
27132 /* Parse a template introduction header for a template-declaration. Returns
27133 false if tentative parse fails. */
27134
27135 static bool
27136 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27137 {
27138 cp_parser_parse_tentatively (parser);
27139
27140 tree saved_scope = parser->scope;
27141 tree saved_object_scope = parser->object_scope;
27142 tree saved_qualifying_scope = parser->qualifying_scope;
27143
27144 /* Look for the optional `::' operator. */
27145 cp_parser_global_scope_opt (parser,
27146 /*current_scope_valid_p=*/false);
27147 /* Look for the nested-name-specifier. */
27148 cp_parser_nested_name_specifier_opt (parser,
27149 /*typename_keyword_p=*/false,
27150 /*check_dependency_p=*/true,
27151 /*type_p=*/false,
27152 /*is_declaration=*/false);
27153
27154 cp_token *token = cp_lexer_peek_token (parser->lexer);
27155 tree concept_name = cp_parser_identifier (parser);
27156
27157 /* Look up the concept for which we will be matching
27158 template parameters. */
27159 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27160 token->location);
27161 parser->scope = saved_scope;
27162 parser->object_scope = saved_object_scope;
27163 parser->qualifying_scope = saved_qualifying_scope;
27164
27165 if (concept_name == error_mark_node)
27166 cp_parser_simulate_error (parser);
27167
27168 /* Look for opening brace for introduction. */
27169 matching_braces braces;
27170 braces.require_open (parser);
27171
27172 if (!cp_parser_parse_definitely (parser))
27173 return false;
27174
27175 push_deferring_access_checks (dk_deferred);
27176
27177 /* Build vector of placeholder parameters and grab
27178 matching identifiers. */
27179 tree introduction_list = cp_parser_introduction_list (parser);
27180
27181 /* The introduction-list shall not be empty. */
27182 int nargs = TREE_VEC_LENGTH (introduction_list);
27183 if (nargs == 0)
27184 {
27185 error ("empty introduction-list");
27186 return true;
27187 }
27188
27189 /* Look for closing brace for introduction. */
27190 if (!braces.require_close (parser))
27191 return true;
27192
27193 if (tmpl_decl == error_mark_node)
27194 {
27195 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27196 token->location);
27197 return true;
27198 }
27199
27200 /* Build and associate the constraint. */
27201 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27202 if (parms && parms != error_mark_node)
27203 {
27204 cp_parser_template_declaration_after_parameters (parser, parms,
27205 member_p);
27206 return true;
27207 }
27208
27209 error_at (token->location, "no matching concept for template-introduction");
27210 return true;
27211 }
27212
27213 /* Parse a normal template-declaration following the template keyword. */
27214
27215 static void
27216 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27217 {
27218 tree parameter_list;
27219 bool need_lang_pop;
27220 location_t location = input_location;
27221
27222 /* Look for the `<' token. */
27223 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27224 return;
27225 if (at_class_scope_p () && current_function_decl)
27226 {
27227 /* 14.5.2.2 [temp.mem]
27228
27229 A local class shall not have member templates. */
27230 error_at (location,
27231 "invalid declaration of member template in local class");
27232 cp_parser_skip_to_end_of_block_or_statement (parser);
27233 return;
27234 }
27235 /* [temp]
27236
27237 A template ... shall not have C linkage. */
27238 if (current_lang_name == lang_name_c)
27239 {
27240 error_at (location, "template with C linkage");
27241 maybe_show_extern_c_location ();
27242 /* Give it C++ linkage to avoid confusing other parts of the
27243 front end. */
27244 push_lang_context (lang_name_cplusplus);
27245 need_lang_pop = true;
27246 }
27247 else
27248 need_lang_pop = false;
27249
27250 /* We cannot perform access checks on the template parameter
27251 declarations until we know what is being declared, just as we
27252 cannot check the decl-specifier list. */
27253 push_deferring_access_checks (dk_deferred);
27254
27255 /* If the next token is `>', then we have an invalid
27256 specialization. Rather than complain about an invalid template
27257 parameter, issue an error message here. */
27258 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27259 {
27260 cp_parser_error (parser, "invalid explicit specialization");
27261 begin_specialization ();
27262 parameter_list = NULL_TREE;
27263 }
27264 else
27265 {
27266 /* Parse the template parameters. */
27267 parameter_list = cp_parser_template_parameter_list (parser);
27268 }
27269
27270 /* Look for the `>'. */
27271 cp_parser_skip_to_end_of_template_parameter_list (parser);
27272
27273 /* Manage template requirements */
27274 if (flag_concepts)
27275 {
27276 tree reqs = get_shorthand_constraints (current_template_parms);
27277 if (tree r = cp_parser_requires_clause_opt (parser))
27278 reqs = conjoin_constraints (reqs, normalize_expression (r));
27279 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27280 }
27281
27282 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27283 member_p);
27284
27285 /* For the erroneous case of a template with C linkage, we pushed an
27286 implicit C++ linkage scope; exit that scope now. */
27287 if (need_lang_pop)
27288 pop_lang_context ();
27289 }
27290
27291 /* Parse a template-declaration, assuming that the `export' (and
27292 `extern') keywords, if present, has already been scanned. MEMBER_P
27293 is as for cp_parser_template_declaration. */
27294
27295 static bool
27296 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27297 {
27298 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27299 {
27300 cp_lexer_consume_token (parser->lexer);
27301 cp_parser_explicit_template_declaration (parser, member_p);
27302 return true;
27303 }
27304 else if (flag_concepts)
27305 return cp_parser_template_introduction (parser, member_p);
27306
27307 return false;
27308 }
27309
27310 /* Perform the deferred access checks from a template-parameter-list.
27311 CHECKS is a TREE_LIST of access checks, as returned by
27312 get_deferred_access_checks. */
27313
27314 static void
27315 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27316 {
27317 ++processing_template_parmlist;
27318 perform_access_checks (checks, tf_warning_or_error);
27319 --processing_template_parmlist;
27320 }
27321
27322 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27323 `function-definition' sequence that follows a template header.
27324 If MEMBER_P is true, this declaration appears in a class scope.
27325
27326 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27327 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27328
27329 static tree
27330 cp_parser_single_declaration (cp_parser* parser,
27331 vec<deferred_access_check, va_gc> *checks,
27332 bool member_p,
27333 bool explicit_specialization_p,
27334 bool* friend_p)
27335 {
27336 int declares_class_or_enum;
27337 tree decl = NULL_TREE;
27338 cp_decl_specifier_seq decl_specifiers;
27339 bool function_definition_p = false;
27340 cp_token *decl_spec_token_start;
27341
27342 /* This function is only used when processing a template
27343 declaration. */
27344 gcc_assert (innermost_scope_kind () == sk_template_parms
27345 || innermost_scope_kind () == sk_template_spec);
27346
27347 /* Defer access checks until we know what is being declared. */
27348 push_deferring_access_checks (dk_deferred);
27349
27350 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27351 alternative. */
27352 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27353 cp_parser_decl_specifier_seq (parser,
27354 CP_PARSER_FLAGS_OPTIONAL,
27355 &decl_specifiers,
27356 &declares_class_or_enum);
27357 if (friend_p)
27358 *friend_p = cp_parser_friend_p (&decl_specifiers);
27359
27360 /* There are no template typedefs. */
27361 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27362 {
27363 error_at (decl_spec_token_start->location,
27364 "template declaration of %<typedef%>");
27365 decl = error_mark_node;
27366 }
27367
27368 /* Gather up the access checks that occurred the
27369 decl-specifier-seq. */
27370 stop_deferring_access_checks ();
27371
27372 /* Check for the declaration of a template class. */
27373 if (declares_class_or_enum)
27374 {
27375 if (cp_parser_declares_only_class_p (parser)
27376 || (declares_class_or_enum & 2))
27377 {
27378 // If this is a declaration, but not a definition, associate
27379 // any constraints with the type declaration. Constraints
27380 // are associated with definitions in cp_parser_class_specifier.
27381 if (declares_class_or_enum == 1)
27382 associate_classtype_constraints (decl_specifiers.type);
27383
27384 decl = shadow_tag (&decl_specifiers);
27385
27386 /* In this case:
27387
27388 struct C {
27389 friend template <typename T> struct A<T>::B;
27390 };
27391
27392 A<T>::B will be represented by a TYPENAME_TYPE, and
27393 therefore not recognized by shadow_tag. */
27394 if (friend_p && *friend_p
27395 && !decl
27396 && decl_specifiers.type
27397 && TYPE_P (decl_specifiers.type))
27398 decl = decl_specifiers.type;
27399
27400 if (decl && decl != error_mark_node)
27401 decl = TYPE_NAME (decl);
27402 else
27403 decl = error_mark_node;
27404
27405 /* Perform access checks for template parameters. */
27406 cp_parser_perform_template_parameter_access_checks (checks);
27407
27408 /* Give a helpful diagnostic for
27409 template <class T> struct A { } a;
27410 if we aren't already recovering from an error. */
27411 if (!cp_parser_declares_only_class_p (parser)
27412 && !seen_error ())
27413 {
27414 error_at (cp_lexer_peek_token (parser->lexer)->location,
27415 "a class template declaration must not declare "
27416 "anything else");
27417 cp_parser_skip_to_end_of_block_or_statement (parser);
27418 goto out;
27419 }
27420 }
27421 }
27422
27423 /* Complain about missing 'typename' or other invalid type names. */
27424 if (!decl_specifiers.any_type_specifiers_p
27425 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27426 {
27427 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27428 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27429 the rest of this declaration. */
27430 decl = error_mark_node;
27431 goto out;
27432 }
27433
27434 /* If it's not a template class, try for a template function. If
27435 the next token is a `;', then this declaration does not declare
27436 anything. But, if there were errors in the decl-specifiers, then
27437 the error might well have come from an attempted class-specifier.
27438 In that case, there's no need to warn about a missing declarator. */
27439 if (!decl
27440 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27441 || decl_specifiers.type != error_mark_node))
27442 {
27443 decl = cp_parser_init_declarator (parser,
27444 &decl_specifiers,
27445 checks,
27446 /*function_definition_allowed_p=*/true,
27447 member_p,
27448 declares_class_or_enum,
27449 &function_definition_p,
27450 NULL, NULL, NULL);
27451
27452 /* 7.1.1-1 [dcl.stc]
27453
27454 A storage-class-specifier shall not be specified in an explicit
27455 specialization... */
27456 if (decl
27457 && explicit_specialization_p
27458 && decl_specifiers.storage_class != sc_none)
27459 {
27460 error_at (decl_spec_token_start->location,
27461 "explicit template specialization cannot have a storage class");
27462 decl = error_mark_node;
27463 }
27464
27465 if (decl && VAR_P (decl))
27466 check_template_variable (decl);
27467 }
27468
27469 /* Look for a trailing `;' after the declaration. */
27470 if (!function_definition_p
27471 && (decl == error_mark_node
27472 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27473 cp_parser_skip_to_end_of_block_or_statement (parser);
27474
27475 out:
27476 pop_deferring_access_checks ();
27477
27478 /* Clear any current qualification; whatever comes next is the start
27479 of something new. */
27480 parser->scope = NULL_TREE;
27481 parser->qualifying_scope = NULL_TREE;
27482 parser->object_scope = NULL_TREE;
27483
27484 return decl;
27485 }
27486
27487 /* Parse a cast-expression that is not the operand of a unary "&". */
27488
27489 static cp_expr
27490 cp_parser_simple_cast_expression (cp_parser *parser)
27491 {
27492 return cp_parser_cast_expression (parser, /*address_p=*/false,
27493 /*cast_p=*/false, /*decltype*/false, NULL);
27494 }
27495
27496 /* Parse a functional cast to TYPE. Returns an expression
27497 representing the cast. */
27498
27499 static cp_expr
27500 cp_parser_functional_cast (cp_parser* parser, tree type)
27501 {
27502 vec<tree, va_gc> *vec;
27503 tree expression_list;
27504 cp_expr cast;
27505 bool nonconst_p;
27506
27507 location_t start_loc = input_location;
27508
27509 if (!type)
27510 type = error_mark_node;
27511
27512 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27513 {
27514 cp_lexer_set_source_position (parser->lexer);
27515 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27516 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27517 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27518 if (TREE_CODE (type) == TYPE_DECL)
27519 type = TREE_TYPE (type);
27520
27521 cast = finish_compound_literal (type, expression_list,
27522 tf_warning_or_error, fcl_functional);
27523 /* Create a location of the form:
27524 type_name{i, f}
27525 ^~~~~~~~~~~~~~~
27526 with caret == start at the start of the type name,
27527 finishing at the closing brace. */
27528 location_t finish_loc
27529 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27530 location_t combined_loc = make_location (start_loc, start_loc,
27531 finish_loc);
27532 cast.set_location (combined_loc);
27533 return cast;
27534 }
27535
27536
27537 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27538 /*cast_p=*/true,
27539 /*allow_expansion_p=*/true,
27540 /*non_constant_p=*/NULL);
27541 if (vec == NULL)
27542 expression_list = error_mark_node;
27543 else
27544 {
27545 expression_list = build_tree_list_vec (vec);
27546 release_tree_vector (vec);
27547 }
27548
27549 cast = build_functional_cast (type, expression_list,
27550 tf_warning_or_error);
27551 /* [expr.const]/1: In an integral constant expression "only type
27552 conversions to integral or enumeration type can be used". */
27553 if (TREE_CODE (type) == TYPE_DECL)
27554 type = TREE_TYPE (type);
27555 if (cast != error_mark_node
27556 && !cast_valid_in_integral_constant_expression_p (type)
27557 && cp_parser_non_integral_constant_expression (parser,
27558 NIC_CONSTRUCTOR))
27559 return error_mark_node;
27560
27561 /* Create a location of the form:
27562 float(i)
27563 ^~~~~~~~
27564 with caret == start at the start of the type name,
27565 finishing at the closing paren. */
27566 location_t finish_loc
27567 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27568 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27569 cast.set_location (combined_loc);
27570 return cast;
27571 }
27572
27573 /* Save the tokens that make up the body of a member function defined
27574 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27575 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27576 specifiers applied to the declaration. Returns the FUNCTION_DECL
27577 for the member function. */
27578
27579 static tree
27580 cp_parser_save_member_function_body (cp_parser* parser,
27581 cp_decl_specifier_seq *decl_specifiers,
27582 cp_declarator *declarator,
27583 tree attributes)
27584 {
27585 cp_token *first;
27586 cp_token *last;
27587 tree fn;
27588 bool function_try_block = false;
27589
27590 /* Create the FUNCTION_DECL. */
27591 fn = grokmethod (decl_specifiers, declarator, attributes);
27592 cp_finalize_omp_declare_simd (parser, fn);
27593 cp_finalize_oacc_routine (parser, fn, true);
27594 /* If something went badly wrong, bail out now. */
27595 if (fn == error_mark_node)
27596 {
27597 /* If there's a function-body, skip it. */
27598 if (cp_parser_token_starts_function_definition_p
27599 (cp_lexer_peek_token (parser->lexer)))
27600 cp_parser_skip_to_end_of_block_or_statement (parser);
27601 return error_mark_node;
27602 }
27603
27604 /* Remember it, if there default args to post process. */
27605 cp_parser_save_default_args (parser, fn);
27606
27607 /* Save away the tokens that make up the body of the
27608 function. */
27609 first = parser->lexer->next_token;
27610
27611 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27612 cp_lexer_consume_token (parser->lexer);
27613 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27614 RID_TRANSACTION_ATOMIC))
27615 {
27616 cp_lexer_consume_token (parser->lexer);
27617 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27618 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27619 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27620 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27621 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27622 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27623 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27624 {
27625 cp_lexer_consume_token (parser->lexer);
27626 cp_lexer_consume_token (parser->lexer);
27627 cp_lexer_consume_token (parser->lexer);
27628 cp_lexer_consume_token (parser->lexer);
27629 cp_lexer_consume_token (parser->lexer);
27630 }
27631 else
27632 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27633 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27634 {
27635 cp_lexer_consume_token (parser->lexer);
27636 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27637 break;
27638 }
27639 }
27640
27641 /* Handle function try blocks. */
27642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27643 {
27644 cp_lexer_consume_token (parser->lexer);
27645 function_try_block = true;
27646 }
27647 /* We can have braced-init-list mem-initializers before the fn body. */
27648 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27649 {
27650 cp_lexer_consume_token (parser->lexer);
27651 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27652 {
27653 /* cache_group will stop after an un-nested { } pair, too. */
27654 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27655 break;
27656
27657 /* variadic mem-inits have ... after the ')'. */
27658 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27659 cp_lexer_consume_token (parser->lexer);
27660 }
27661 }
27662 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27663 /* Handle function try blocks. */
27664 if (function_try_block)
27665 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27666 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27667 last = parser->lexer->next_token;
27668
27669 /* Save away the inline definition; we will process it when the
27670 class is complete. */
27671 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27672 DECL_PENDING_INLINE_P (fn) = 1;
27673
27674 /* We need to know that this was defined in the class, so that
27675 friend templates are handled correctly. */
27676 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27677
27678 /* Add FN to the queue of functions to be parsed later. */
27679 vec_safe_push (unparsed_funs_with_definitions, fn);
27680
27681 return fn;
27682 }
27683
27684 /* Save the tokens that make up the in-class initializer for a non-static
27685 data member. Returns a DEFAULT_ARG. */
27686
27687 static tree
27688 cp_parser_save_nsdmi (cp_parser* parser)
27689 {
27690 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27691 }
27692
27693 /* Parse a template-argument-list, as well as the trailing ">" (but
27694 not the opening "<"). See cp_parser_template_argument_list for the
27695 return value. */
27696
27697 static tree
27698 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27699 {
27700 tree arguments;
27701 tree saved_scope;
27702 tree saved_qualifying_scope;
27703 tree saved_object_scope;
27704 bool saved_greater_than_is_operator_p;
27705 int saved_unevaluated_operand;
27706 int saved_inhibit_evaluation_warnings;
27707
27708 /* [temp.names]
27709
27710 When parsing a template-id, the first non-nested `>' is taken as
27711 the end of the template-argument-list rather than a greater-than
27712 operator. */
27713 saved_greater_than_is_operator_p
27714 = parser->greater_than_is_operator_p;
27715 parser->greater_than_is_operator_p = false;
27716 /* Parsing the argument list may modify SCOPE, so we save it
27717 here. */
27718 saved_scope = parser->scope;
27719 saved_qualifying_scope = parser->qualifying_scope;
27720 saved_object_scope = parser->object_scope;
27721 /* We need to evaluate the template arguments, even though this
27722 template-id may be nested within a "sizeof". */
27723 saved_unevaluated_operand = cp_unevaluated_operand;
27724 cp_unevaluated_operand = 0;
27725 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27726 c_inhibit_evaluation_warnings = 0;
27727 /* Parse the template-argument-list itself. */
27728 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27729 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27730 arguments = NULL_TREE;
27731 else
27732 arguments = cp_parser_template_argument_list (parser);
27733 /* Look for the `>' that ends the template-argument-list. If we find
27734 a '>>' instead, it's probably just a typo. */
27735 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27736 {
27737 if (cxx_dialect != cxx98)
27738 {
27739 /* In C++0x, a `>>' in a template argument list or cast
27740 expression is considered to be two separate `>'
27741 tokens. So, change the current token to a `>', but don't
27742 consume it: it will be consumed later when the outer
27743 template argument list (or cast expression) is parsed.
27744 Note that this replacement of `>' for `>>' is necessary
27745 even if we are parsing tentatively: in the tentative
27746 case, after calling
27747 cp_parser_enclosed_template_argument_list we will always
27748 throw away all of the template arguments and the first
27749 closing `>', either because the template argument list
27750 was erroneous or because we are replacing those tokens
27751 with a CPP_TEMPLATE_ID token. The second `>' (which will
27752 not have been thrown away) is needed either to close an
27753 outer template argument list or to complete a new-style
27754 cast. */
27755 cp_token *token = cp_lexer_peek_token (parser->lexer);
27756 token->type = CPP_GREATER;
27757 }
27758 else if (!saved_greater_than_is_operator_p)
27759 {
27760 /* If we're in a nested template argument list, the '>>' has
27761 to be a typo for '> >'. We emit the error message, but we
27762 continue parsing and we push a '>' as next token, so that
27763 the argument list will be parsed correctly. Note that the
27764 global source location is still on the token before the
27765 '>>', so we need to say explicitly where we want it. */
27766 cp_token *token = cp_lexer_peek_token (parser->lexer);
27767 gcc_rich_location richloc (token->location);
27768 richloc.add_fixit_replace ("> >");
27769 error_at (&richloc, "%<>>%> should be %<> >%> "
27770 "within a nested template argument list");
27771
27772 token->type = CPP_GREATER;
27773 }
27774 else
27775 {
27776 /* If this is not a nested template argument list, the '>>'
27777 is a typo for '>'. Emit an error message and continue.
27778 Same deal about the token location, but here we can get it
27779 right by consuming the '>>' before issuing the diagnostic. */
27780 cp_token *token = cp_lexer_consume_token (parser->lexer);
27781 error_at (token->location,
27782 "spurious %<>>%>, use %<>%> to terminate "
27783 "a template argument list");
27784 }
27785 }
27786 else
27787 cp_parser_skip_to_end_of_template_parameter_list (parser);
27788 /* The `>' token might be a greater-than operator again now. */
27789 parser->greater_than_is_operator_p
27790 = saved_greater_than_is_operator_p;
27791 /* Restore the SAVED_SCOPE. */
27792 parser->scope = saved_scope;
27793 parser->qualifying_scope = saved_qualifying_scope;
27794 parser->object_scope = saved_object_scope;
27795 cp_unevaluated_operand = saved_unevaluated_operand;
27796 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27797
27798 return arguments;
27799 }
27800
27801 /* MEMBER_FUNCTION is a member function, or a friend. If default
27802 arguments, or the body of the function have not yet been parsed,
27803 parse them now. */
27804
27805 static void
27806 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27807 {
27808 timevar_push (TV_PARSE_INMETH);
27809 /* If this member is a template, get the underlying
27810 FUNCTION_DECL. */
27811 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27812 member_function = DECL_TEMPLATE_RESULT (member_function);
27813
27814 /* There should not be any class definitions in progress at this
27815 point; the bodies of members are only parsed outside of all class
27816 definitions. */
27817 gcc_assert (parser->num_classes_being_defined == 0);
27818 /* While we're parsing the member functions we might encounter more
27819 classes. We want to handle them right away, but we don't want
27820 them getting mixed up with functions that are currently in the
27821 queue. */
27822 push_unparsed_function_queues (parser);
27823
27824 /* Make sure that any template parameters are in scope. */
27825 maybe_begin_member_template_processing (member_function);
27826
27827 /* If the body of the function has not yet been parsed, parse it
27828 now. */
27829 if (DECL_PENDING_INLINE_P (member_function))
27830 {
27831 tree function_scope;
27832 cp_token_cache *tokens;
27833
27834 /* The function is no longer pending; we are processing it. */
27835 tokens = DECL_PENDING_INLINE_INFO (member_function);
27836 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27837 DECL_PENDING_INLINE_P (member_function) = 0;
27838
27839 /* If this is a local class, enter the scope of the containing
27840 function. */
27841 function_scope = current_function_decl;
27842 if (function_scope)
27843 push_function_context ();
27844
27845 /* Push the body of the function onto the lexer stack. */
27846 cp_parser_push_lexer_for_tokens (parser, tokens);
27847
27848 /* Let the front end know that we going to be defining this
27849 function. */
27850 start_preparsed_function (member_function, NULL_TREE,
27851 SF_PRE_PARSED | SF_INCLASS_INLINE);
27852
27853 /* Don't do access checking if it is a templated function. */
27854 if (processing_template_decl)
27855 push_deferring_access_checks (dk_no_check);
27856
27857 /* #pragma omp declare reduction needs special parsing. */
27858 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27859 {
27860 parser->lexer->in_pragma = true;
27861 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27862 finish_function (/*inline_p=*/true);
27863 cp_check_omp_declare_reduction (member_function);
27864 }
27865 else
27866 /* Now, parse the body of the function. */
27867 cp_parser_function_definition_after_declarator (parser,
27868 /*inline_p=*/true);
27869
27870 if (processing_template_decl)
27871 pop_deferring_access_checks ();
27872
27873 /* Leave the scope of the containing function. */
27874 if (function_scope)
27875 pop_function_context ();
27876 cp_parser_pop_lexer (parser);
27877 }
27878
27879 /* Remove any template parameters from the symbol table. */
27880 maybe_end_member_template_processing ();
27881
27882 /* Restore the queue. */
27883 pop_unparsed_function_queues (parser);
27884 timevar_pop (TV_PARSE_INMETH);
27885 }
27886
27887 /* If DECL contains any default args, remember it on the unparsed
27888 functions queue. */
27889
27890 static void
27891 cp_parser_save_default_args (cp_parser* parser, tree decl)
27892 {
27893 tree probe;
27894
27895 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27896 probe;
27897 probe = TREE_CHAIN (probe))
27898 if (TREE_PURPOSE (probe))
27899 {
27900 cp_default_arg_entry entry = {current_class_type, decl};
27901 vec_safe_push (unparsed_funs_with_default_args, entry);
27902 break;
27903 }
27904 }
27905
27906 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27907 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27908 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27909 from the parameter-type-list. */
27910
27911 static tree
27912 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27913 tree default_arg, tree parmtype)
27914 {
27915 cp_token_cache *tokens;
27916 tree parsed_arg;
27917 bool dummy;
27918
27919 if (default_arg == error_mark_node)
27920 return error_mark_node;
27921
27922 /* Push the saved tokens for the default argument onto the parser's
27923 lexer stack. */
27924 tokens = DEFARG_TOKENS (default_arg);
27925 cp_parser_push_lexer_for_tokens (parser, tokens);
27926
27927 start_lambda_scope (decl);
27928
27929 /* Parse the default argument. */
27930 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27931 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27932 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27933
27934 finish_lambda_scope ();
27935
27936 if (parsed_arg == error_mark_node)
27937 cp_parser_skip_to_end_of_statement (parser);
27938
27939 if (!processing_template_decl)
27940 {
27941 /* In a non-template class, check conversions now. In a template,
27942 we'll wait and instantiate these as needed. */
27943 if (TREE_CODE (decl) == PARM_DECL)
27944 parsed_arg = check_default_argument (parmtype, parsed_arg,
27945 tf_warning_or_error);
27946 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27947 parsed_arg = error_mark_node;
27948 else
27949 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27950 }
27951
27952 /* If the token stream has not been completely used up, then
27953 there was extra junk after the end of the default
27954 argument. */
27955 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27956 {
27957 if (TREE_CODE (decl) == PARM_DECL)
27958 cp_parser_error (parser, "expected %<,%>");
27959 else
27960 cp_parser_error (parser, "expected %<;%>");
27961 }
27962
27963 /* Revert to the main lexer. */
27964 cp_parser_pop_lexer (parser);
27965
27966 return parsed_arg;
27967 }
27968
27969 /* FIELD is a non-static data member with an initializer which we saved for
27970 later; parse it now. */
27971
27972 static void
27973 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27974 {
27975 tree def;
27976
27977 maybe_begin_member_template_processing (field);
27978
27979 push_unparsed_function_queues (parser);
27980 def = cp_parser_late_parse_one_default_arg (parser, field,
27981 DECL_INITIAL (field),
27982 NULL_TREE);
27983 pop_unparsed_function_queues (parser);
27984
27985 maybe_end_member_template_processing ();
27986
27987 DECL_INITIAL (field) = def;
27988 }
27989
27990 /* FN is a FUNCTION_DECL which may contains a parameter with an
27991 unparsed DEFAULT_ARG. Parse the default args now. This function
27992 assumes that the current scope is the scope in which the default
27993 argument should be processed. */
27994
27995 static void
27996 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27997 {
27998 bool saved_local_variables_forbidden_p;
27999 tree parm, parmdecl;
28000
28001 /* While we're parsing the default args, we might (due to the
28002 statement expression extension) encounter more classes. We want
28003 to handle them right away, but we don't want them getting mixed
28004 up with default args that are currently in the queue. */
28005 push_unparsed_function_queues (parser);
28006
28007 /* Local variable names (and the `this' keyword) may not appear
28008 in a default argument. */
28009 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28010 parser->local_variables_forbidden_p = true;
28011
28012 push_defarg_context (fn);
28013
28014 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28015 parmdecl = DECL_ARGUMENTS (fn);
28016 parm && parm != void_list_node;
28017 parm = TREE_CHAIN (parm),
28018 parmdecl = DECL_CHAIN (parmdecl))
28019 {
28020 tree default_arg = TREE_PURPOSE (parm);
28021 tree parsed_arg;
28022 vec<tree, va_gc> *insts;
28023 tree copy;
28024 unsigned ix;
28025
28026 if (!default_arg)
28027 continue;
28028
28029 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28030 /* This can happen for a friend declaration for a function
28031 already declared with default arguments. */
28032 continue;
28033
28034 parsed_arg
28035 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28036 default_arg,
28037 TREE_VALUE (parm));
28038 TREE_PURPOSE (parm) = parsed_arg;
28039
28040 /* Update any instantiations we've already created. */
28041 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28042 vec_safe_iterate (insts, ix, &copy); ix++)
28043 TREE_PURPOSE (copy) = parsed_arg;
28044 }
28045
28046 pop_defarg_context ();
28047
28048 /* Make sure no default arg is missing. */
28049 check_default_args (fn);
28050
28051 /* Restore the state of local_variables_forbidden_p. */
28052 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28053
28054 /* Restore the queue. */
28055 pop_unparsed_function_queues (parser);
28056 }
28057
28058 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28059
28060 sizeof ... ( identifier )
28061
28062 where the 'sizeof' token has already been consumed. */
28063
28064 static tree
28065 cp_parser_sizeof_pack (cp_parser *parser)
28066 {
28067 /* Consume the `...'. */
28068 cp_lexer_consume_token (parser->lexer);
28069 maybe_warn_variadic_templates ();
28070
28071 matching_parens parens;
28072 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28073 if (paren)
28074 parens.consume_open (parser);
28075 else
28076 permerror (cp_lexer_peek_token (parser->lexer)->location,
28077 "%<sizeof...%> argument must be surrounded by parentheses");
28078
28079 cp_token *token = cp_lexer_peek_token (parser->lexer);
28080 tree name = cp_parser_identifier (parser);
28081 if (name == error_mark_node)
28082 return error_mark_node;
28083 /* The name is not qualified. */
28084 parser->scope = NULL_TREE;
28085 parser->qualifying_scope = NULL_TREE;
28086 parser->object_scope = NULL_TREE;
28087 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28088 if (expr == error_mark_node)
28089 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28090 token->location);
28091 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28092 expr = TREE_TYPE (expr);
28093 else if (TREE_CODE (expr) == CONST_DECL)
28094 expr = DECL_INITIAL (expr);
28095 expr = make_pack_expansion (expr);
28096 PACK_EXPANSION_SIZEOF_P (expr) = true;
28097
28098 if (paren)
28099 parens.require_close (parser);
28100
28101 return expr;
28102 }
28103
28104 /* Parse the operand of `sizeof' (or a similar operator). Returns
28105 either a TYPE or an expression, depending on the form of the
28106 input. The KEYWORD indicates which kind of expression we have
28107 encountered. */
28108
28109 static tree
28110 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28111 {
28112 tree expr = NULL_TREE;
28113 const char *saved_message;
28114 char *tmp;
28115 bool saved_integral_constant_expression_p;
28116 bool saved_non_integral_constant_expression_p;
28117
28118 /* If it's a `...', then we are computing the length of a parameter
28119 pack. */
28120 if (keyword == RID_SIZEOF
28121 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28122 return cp_parser_sizeof_pack (parser);
28123
28124 /* Types cannot be defined in a `sizeof' expression. Save away the
28125 old message. */
28126 saved_message = parser->type_definition_forbidden_message;
28127 /* And create the new one. */
28128 tmp = concat ("types may not be defined in %<",
28129 IDENTIFIER_POINTER (ridpointers[keyword]),
28130 "%> expressions", NULL);
28131 parser->type_definition_forbidden_message = tmp;
28132
28133 /* The restrictions on constant-expressions do not apply inside
28134 sizeof expressions. */
28135 saved_integral_constant_expression_p
28136 = parser->integral_constant_expression_p;
28137 saved_non_integral_constant_expression_p
28138 = parser->non_integral_constant_expression_p;
28139 parser->integral_constant_expression_p = false;
28140
28141 /* Do not actually evaluate the expression. */
28142 ++cp_unevaluated_operand;
28143 ++c_inhibit_evaluation_warnings;
28144 /* If it's a `(', then we might be looking at the type-id
28145 construction. */
28146 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28147 {
28148 tree type = NULL_TREE;
28149
28150 /* We can't be sure yet whether we're looking at a type-id or an
28151 expression. */
28152 cp_parser_parse_tentatively (parser);
28153
28154 matching_parens parens;
28155 parens.consume_open (parser);
28156
28157 /* Note: as a GNU Extension, compound literals are considered
28158 postfix-expressions as they are in C99, so they are valid
28159 arguments to sizeof. See comment in cp_parser_cast_expression
28160 for details. */
28161 if (cp_parser_compound_literal_p (parser))
28162 cp_parser_simulate_error (parser);
28163 else
28164 {
28165 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28166 parser->in_type_id_in_expr_p = true;
28167 /* Look for the type-id. */
28168 type = cp_parser_type_id (parser);
28169 /* Look for the closing `)'. */
28170 parens.require_close (parser);
28171 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28172 }
28173
28174 /* If all went well, then we're done. */
28175 if (cp_parser_parse_definitely (parser))
28176 {
28177 cp_decl_specifier_seq decl_specs;
28178
28179 /* Build a trivial decl-specifier-seq. */
28180 clear_decl_specs (&decl_specs);
28181 decl_specs.type = type;
28182
28183 /* Call grokdeclarator to figure out what type this is. */
28184 expr = grokdeclarator (NULL,
28185 &decl_specs,
28186 TYPENAME,
28187 /*initialized=*/0,
28188 /*attrlist=*/NULL);
28189 }
28190 }
28191
28192 /* If the type-id production did not work out, then we must be
28193 looking at the unary-expression production. */
28194 if (!expr)
28195 expr = cp_parser_unary_expression (parser);
28196
28197 /* Go back to evaluating expressions. */
28198 --cp_unevaluated_operand;
28199 --c_inhibit_evaluation_warnings;
28200
28201 /* Free the message we created. */
28202 free (tmp);
28203 /* And restore the old one. */
28204 parser->type_definition_forbidden_message = saved_message;
28205 parser->integral_constant_expression_p
28206 = saved_integral_constant_expression_p;
28207 parser->non_integral_constant_expression_p
28208 = saved_non_integral_constant_expression_p;
28209
28210 return expr;
28211 }
28212
28213 /* If the current declaration has no declarator, return true. */
28214
28215 static bool
28216 cp_parser_declares_only_class_p (cp_parser *parser)
28217 {
28218 /* If the next token is a `;' or a `,' then there is no
28219 declarator. */
28220 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28221 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28222 }
28223
28224 /* Update the DECL_SPECS to reflect the storage class indicated by
28225 KEYWORD. */
28226
28227 static void
28228 cp_parser_set_storage_class (cp_parser *parser,
28229 cp_decl_specifier_seq *decl_specs,
28230 enum rid keyword,
28231 cp_token *token)
28232 {
28233 cp_storage_class storage_class;
28234
28235 if (parser->in_unbraced_linkage_specification_p)
28236 {
28237 error_at (token->location, "invalid use of %qD in linkage specification",
28238 ridpointers[keyword]);
28239 return;
28240 }
28241 else if (decl_specs->storage_class != sc_none)
28242 {
28243 decl_specs->conflicting_specifiers_p = true;
28244 return;
28245 }
28246
28247 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28248 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28249 && decl_specs->gnu_thread_keyword_p)
28250 {
28251 pedwarn (decl_specs->locations[ds_thread], 0,
28252 "%<__thread%> before %qD", ridpointers[keyword]);
28253 }
28254
28255 switch (keyword)
28256 {
28257 case RID_AUTO:
28258 storage_class = sc_auto;
28259 break;
28260 case RID_REGISTER:
28261 storage_class = sc_register;
28262 break;
28263 case RID_STATIC:
28264 storage_class = sc_static;
28265 break;
28266 case RID_EXTERN:
28267 storage_class = sc_extern;
28268 break;
28269 case RID_MUTABLE:
28270 storage_class = sc_mutable;
28271 break;
28272 default:
28273 gcc_unreachable ();
28274 }
28275 decl_specs->storage_class = storage_class;
28276 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28277
28278 /* A storage class specifier cannot be applied alongside a typedef
28279 specifier. If there is a typedef specifier present then set
28280 conflicting_specifiers_p which will trigger an error later
28281 on in grokdeclarator. */
28282 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28283 decl_specs->conflicting_specifiers_p = true;
28284 }
28285
28286 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28287 is true, the type is a class or enum definition. */
28288
28289 static void
28290 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28291 tree type_spec,
28292 cp_token *token,
28293 bool type_definition_p)
28294 {
28295 decl_specs->any_specifiers_p = true;
28296
28297 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28298 (with, for example, in "typedef int wchar_t;") we remember that
28299 this is what happened. In system headers, we ignore these
28300 declarations so that G++ can work with system headers that are not
28301 C++-safe. */
28302 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28303 && !type_definition_p
28304 && (type_spec == boolean_type_node
28305 || type_spec == char16_type_node
28306 || type_spec == char32_type_node
28307 || type_spec == wchar_type_node)
28308 && (decl_specs->type
28309 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28310 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28311 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28312 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28313 {
28314 decl_specs->redefined_builtin_type = type_spec;
28315 set_and_check_decl_spec_loc (decl_specs,
28316 ds_redefined_builtin_type_spec,
28317 token);
28318 if (!decl_specs->type)
28319 {
28320 decl_specs->type = type_spec;
28321 decl_specs->type_definition_p = false;
28322 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28323 }
28324 }
28325 else if (decl_specs->type)
28326 decl_specs->multiple_types_p = true;
28327 else
28328 {
28329 decl_specs->type = type_spec;
28330 decl_specs->type_definition_p = type_definition_p;
28331 decl_specs->redefined_builtin_type = NULL_TREE;
28332 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28333 }
28334 }
28335
28336 /* True iff TOKEN is the GNU keyword __thread. */
28337
28338 static bool
28339 token_is__thread (cp_token *token)
28340 {
28341 gcc_assert (token->keyword == RID_THREAD);
28342 return id_equal (token->u.value, "__thread");
28343 }
28344
28345 /* Set the location for a declarator specifier and check if it is
28346 duplicated.
28347
28348 DECL_SPECS is the sequence of declarator specifiers onto which to
28349 set the location.
28350
28351 DS is the single declarator specifier to set which location is to
28352 be set onto the existing sequence of declarators.
28353
28354 LOCATION is the location for the declarator specifier to
28355 consider. */
28356
28357 static void
28358 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28359 cp_decl_spec ds, cp_token *token)
28360 {
28361 gcc_assert (ds < ds_last);
28362
28363 if (decl_specs == NULL)
28364 return;
28365
28366 source_location location = token->location;
28367
28368 if (decl_specs->locations[ds] == 0)
28369 {
28370 decl_specs->locations[ds] = location;
28371 if (ds == ds_thread)
28372 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28373 }
28374 else
28375 {
28376 if (ds == ds_long)
28377 {
28378 if (decl_specs->locations[ds_long_long] != 0)
28379 error_at (location,
28380 "%<long long long%> is too long for GCC");
28381 else
28382 {
28383 decl_specs->locations[ds_long_long] = location;
28384 pedwarn_cxx98 (location,
28385 OPT_Wlong_long,
28386 "ISO C++ 1998 does not support %<long long%>");
28387 }
28388 }
28389 else if (ds == ds_thread)
28390 {
28391 bool gnu = token_is__thread (token);
28392 gcc_rich_location richloc (location);
28393 if (gnu != decl_specs->gnu_thread_keyword_p)
28394 {
28395 richloc.add_range (decl_specs->locations[ds_thread]);
28396 error_at (&richloc,
28397 "both %<__thread%> and %<thread_local%> specified");
28398 }
28399 else
28400 {
28401 richloc.add_fixit_remove ();
28402 error_at (&richloc, "duplicate %qD", token->u.value);
28403 }
28404 }
28405 else
28406 {
28407 static const char *const decl_spec_names[] = {
28408 "signed",
28409 "unsigned",
28410 "short",
28411 "long",
28412 "const",
28413 "volatile",
28414 "restrict",
28415 "inline",
28416 "virtual",
28417 "explicit",
28418 "friend",
28419 "typedef",
28420 "using",
28421 "constexpr",
28422 "__complex"
28423 };
28424 gcc_rich_location richloc (location);
28425 richloc.add_fixit_remove ();
28426 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28427 }
28428 }
28429 }
28430
28431 /* Return true iff the declarator specifier DS is present in the
28432 sequence of declarator specifiers DECL_SPECS. */
28433
28434 bool
28435 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28436 cp_decl_spec ds)
28437 {
28438 gcc_assert (ds < ds_last);
28439
28440 if (decl_specs == NULL)
28441 return false;
28442
28443 return decl_specs->locations[ds] != 0;
28444 }
28445
28446 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28447 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28448
28449 static bool
28450 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28451 {
28452 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28453 }
28454
28455 /* Issue an error message indicating that TOKEN_DESC was expected.
28456 If KEYWORD is true, it indicated this function is called by
28457 cp_parser_require_keword and the required token can only be
28458 a indicated keyword.
28459
28460 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28461 within any error as the location of an "opening" token matching
28462 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28463 RT_CLOSE_PAREN). */
28464
28465 static void
28466 cp_parser_required_error (cp_parser *parser,
28467 required_token token_desc,
28468 bool keyword,
28469 location_t matching_location)
28470 {
28471 if (cp_parser_simulate_error (parser))
28472 return;
28473
28474 const char *gmsgid = NULL;
28475 switch (token_desc)
28476 {
28477 case RT_NEW:
28478 gmsgid = G_("expected %<new%>");
28479 break;
28480 case RT_DELETE:
28481 gmsgid = G_("expected %<delete%>");
28482 break;
28483 case RT_RETURN:
28484 gmsgid = G_("expected %<return%>");
28485 break;
28486 case RT_WHILE:
28487 gmsgid = G_("expected %<while%>");
28488 break;
28489 case RT_EXTERN:
28490 gmsgid = G_("expected %<extern%>");
28491 break;
28492 case RT_STATIC_ASSERT:
28493 gmsgid = G_("expected %<static_assert%>");
28494 break;
28495 case RT_DECLTYPE:
28496 gmsgid = G_("expected %<decltype%>");
28497 break;
28498 case RT_OPERATOR:
28499 gmsgid = G_("expected %<operator%>");
28500 break;
28501 case RT_CLASS:
28502 gmsgid = G_("expected %<class%>");
28503 break;
28504 case RT_TEMPLATE:
28505 gmsgid = G_("expected %<template%>");
28506 break;
28507 case RT_NAMESPACE:
28508 gmsgid = G_("expected %<namespace%>");
28509 break;
28510 case RT_USING:
28511 gmsgid = G_("expected %<using%>");
28512 break;
28513 case RT_ASM:
28514 gmsgid = G_("expected %<asm%>");
28515 break;
28516 case RT_TRY:
28517 gmsgid = G_("expected %<try%>");
28518 break;
28519 case RT_CATCH:
28520 gmsgid = G_("expected %<catch%>");
28521 break;
28522 case RT_THROW:
28523 gmsgid = G_("expected %<throw%>");
28524 break;
28525 case RT_LABEL:
28526 gmsgid = G_("expected %<__label__%>");
28527 break;
28528 case RT_AT_TRY:
28529 gmsgid = G_("expected %<@try%>");
28530 break;
28531 case RT_AT_SYNCHRONIZED:
28532 gmsgid = G_("expected %<@synchronized%>");
28533 break;
28534 case RT_AT_THROW:
28535 gmsgid = G_("expected %<@throw%>");
28536 break;
28537 case RT_TRANSACTION_ATOMIC:
28538 gmsgid = G_("expected %<__transaction_atomic%>");
28539 break;
28540 case RT_TRANSACTION_RELAXED:
28541 gmsgid = G_("expected %<__transaction_relaxed%>");
28542 break;
28543 default:
28544 break;
28545 }
28546
28547 if (!gmsgid && !keyword)
28548 {
28549 switch (token_desc)
28550 {
28551 case RT_SEMICOLON:
28552 gmsgid = G_("expected %<;%>");
28553 break;
28554 case RT_OPEN_PAREN:
28555 gmsgid = G_("expected %<(%>");
28556 break;
28557 case RT_CLOSE_BRACE:
28558 gmsgid = G_("expected %<}%>");
28559 break;
28560 case RT_OPEN_BRACE:
28561 gmsgid = G_("expected %<{%>");
28562 break;
28563 case RT_CLOSE_SQUARE:
28564 gmsgid = G_("expected %<]%>");
28565 break;
28566 case RT_OPEN_SQUARE:
28567 gmsgid = G_("expected %<[%>");
28568 break;
28569 case RT_COMMA:
28570 gmsgid = G_("expected %<,%>");
28571 break;
28572 case RT_SCOPE:
28573 gmsgid = G_("expected %<::%>");
28574 break;
28575 case RT_LESS:
28576 gmsgid = G_("expected %<<%>");
28577 break;
28578 case RT_GREATER:
28579 gmsgid = G_("expected %<>%>");
28580 break;
28581 case RT_EQ:
28582 gmsgid = G_("expected %<=%>");
28583 break;
28584 case RT_ELLIPSIS:
28585 gmsgid = G_("expected %<...%>");
28586 break;
28587 case RT_MULT:
28588 gmsgid = G_("expected %<*%>");
28589 break;
28590 case RT_COMPL:
28591 gmsgid = G_("expected %<~%>");
28592 break;
28593 case RT_COLON:
28594 gmsgid = G_("expected %<:%>");
28595 break;
28596 case RT_COLON_SCOPE:
28597 gmsgid = G_("expected %<:%> or %<::%>");
28598 break;
28599 case RT_CLOSE_PAREN:
28600 gmsgid = G_("expected %<)%>");
28601 break;
28602 case RT_COMMA_CLOSE_PAREN:
28603 gmsgid = G_("expected %<,%> or %<)%>");
28604 break;
28605 case RT_PRAGMA_EOL:
28606 gmsgid = G_("expected end of line");
28607 break;
28608 case RT_NAME:
28609 gmsgid = G_("expected identifier");
28610 break;
28611 case RT_SELECT:
28612 gmsgid = G_("expected selection-statement");
28613 break;
28614 case RT_ITERATION:
28615 gmsgid = G_("expected iteration-statement");
28616 break;
28617 case RT_JUMP:
28618 gmsgid = G_("expected jump-statement");
28619 break;
28620 case RT_CLASS_KEY:
28621 gmsgid = G_("expected class-key");
28622 break;
28623 case RT_CLASS_TYPENAME_TEMPLATE:
28624 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28625 break;
28626 default:
28627 gcc_unreachable ();
28628 }
28629 }
28630
28631 if (gmsgid)
28632 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28633 }
28634
28635
28636 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28637 issue an error message indicating that TOKEN_DESC was expected.
28638
28639 Returns the token consumed, if the token had the appropriate type.
28640 Otherwise, returns NULL.
28641
28642 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28643 within any error as the location of an "opening" token matching
28644 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28645 RT_CLOSE_PAREN). */
28646
28647 static cp_token *
28648 cp_parser_require (cp_parser* parser,
28649 enum cpp_ttype type,
28650 required_token token_desc,
28651 location_t matching_location)
28652 {
28653 if (cp_lexer_next_token_is (parser->lexer, type))
28654 return cp_lexer_consume_token (parser->lexer);
28655 else
28656 {
28657 /* Output the MESSAGE -- unless we're parsing tentatively. */
28658 if (!cp_parser_simulate_error (parser))
28659 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28660 matching_location);
28661 return NULL;
28662 }
28663 }
28664
28665 /* An error message is produced if the next token is not '>'.
28666 All further tokens are skipped until the desired token is
28667 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28668
28669 static void
28670 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28671 {
28672 /* Current level of '< ... >'. */
28673 unsigned level = 0;
28674 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28675 unsigned nesting_depth = 0;
28676
28677 /* Are we ready, yet? If not, issue error message. */
28678 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28679 return;
28680
28681 /* Skip tokens until the desired token is found. */
28682 while (true)
28683 {
28684 /* Peek at the next token. */
28685 switch (cp_lexer_peek_token (parser->lexer)->type)
28686 {
28687 case CPP_LESS:
28688 if (!nesting_depth)
28689 ++level;
28690 break;
28691
28692 case CPP_RSHIFT:
28693 if (cxx_dialect == cxx98)
28694 /* C++0x views the `>>' operator as two `>' tokens, but
28695 C++98 does not. */
28696 break;
28697 else if (!nesting_depth && level-- == 0)
28698 {
28699 /* We've hit a `>>' where the first `>' closes the
28700 template argument list, and the second `>' is
28701 spurious. Just consume the `>>' and stop; we've
28702 already produced at least one error. */
28703 cp_lexer_consume_token (parser->lexer);
28704 return;
28705 }
28706 /* Fall through for C++0x, so we handle the second `>' in
28707 the `>>'. */
28708 gcc_fallthrough ();
28709
28710 case CPP_GREATER:
28711 if (!nesting_depth && level-- == 0)
28712 {
28713 /* We've reached the token we want, consume it and stop. */
28714 cp_lexer_consume_token (parser->lexer);
28715 return;
28716 }
28717 break;
28718
28719 case CPP_OPEN_PAREN:
28720 case CPP_OPEN_SQUARE:
28721 ++nesting_depth;
28722 break;
28723
28724 case CPP_CLOSE_PAREN:
28725 case CPP_CLOSE_SQUARE:
28726 if (nesting_depth-- == 0)
28727 return;
28728 break;
28729
28730 case CPP_EOF:
28731 case CPP_PRAGMA_EOL:
28732 case CPP_SEMICOLON:
28733 case CPP_OPEN_BRACE:
28734 case CPP_CLOSE_BRACE:
28735 /* The '>' was probably forgotten, don't look further. */
28736 return;
28737
28738 default:
28739 break;
28740 }
28741
28742 /* Consume this token. */
28743 cp_lexer_consume_token (parser->lexer);
28744 }
28745 }
28746
28747 /* If the next token is the indicated keyword, consume it. Otherwise,
28748 issue an error message indicating that TOKEN_DESC was expected.
28749
28750 Returns the token consumed, if the token had the appropriate type.
28751 Otherwise, returns NULL. */
28752
28753 static cp_token *
28754 cp_parser_require_keyword (cp_parser* parser,
28755 enum rid keyword,
28756 required_token token_desc)
28757 {
28758 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28759
28760 if (token && token->keyword != keyword)
28761 {
28762 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28763 UNKNOWN_LOCATION);
28764 return NULL;
28765 }
28766
28767 return token;
28768 }
28769
28770 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28771 function-definition. */
28772
28773 static bool
28774 cp_parser_token_starts_function_definition_p (cp_token* token)
28775 {
28776 return (/* An ordinary function-body begins with an `{'. */
28777 token->type == CPP_OPEN_BRACE
28778 /* A ctor-initializer begins with a `:'. */
28779 || token->type == CPP_COLON
28780 /* A function-try-block begins with `try'. */
28781 || token->keyword == RID_TRY
28782 /* A function-transaction-block begins with `__transaction_atomic'
28783 or `__transaction_relaxed'. */
28784 || token->keyword == RID_TRANSACTION_ATOMIC
28785 || token->keyword == RID_TRANSACTION_RELAXED
28786 /* The named return value extension begins with `return'. */
28787 || token->keyword == RID_RETURN);
28788 }
28789
28790 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28791 definition. */
28792
28793 static bool
28794 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28795 {
28796 cp_token *token;
28797
28798 token = cp_lexer_peek_token (parser->lexer);
28799 return (token->type == CPP_OPEN_BRACE
28800 || (token->type == CPP_COLON
28801 && !parser->colon_doesnt_start_class_def_p));
28802 }
28803
28804 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28805 C++0x) ending a template-argument. */
28806
28807 static bool
28808 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28809 {
28810 cp_token *token;
28811
28812 token = cp_lexer_peek_token (parser->lexer);
28813 return (token->type == CPP_COMMA
28814 || token->type == CPP_GREATER
28815 || token->type == CPP_ELLIPSIS
28816 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28817 }
28818
28819 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28820 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28821
28822 static bool
28823 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28824 size_t n)
28825 {
28826 cp_token *token;
28827
28828 token = cp_lexer_peek_nth_token (parser->lexer, n);
28829 if (token->type == CPP_LESS)
28830 return true;
28831 /* Check for the sequence `<::' in the original code. It would be lexed as
28832 `[:', where `[' is a digraph, and there is no whitespace before
28833 `:'. */
28834 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28835 {
28836 cp_token *token2;
28837 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28838 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28839 return true;
28840 }
28841 return false;
28842 }
28843
28844 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28845 or none_type otherwise. */
28846
28847 static enum tag_types
28848 cp_parser_token_is_class_key (cp_token* token)
28849 {
28850 switch (token->keyword)
28851 {
28852 case RID_CLASS:
28853 return class_type;
28854 case RID_STRUCT:
28855 return record_type;
28856 case RID_UNION:
28857 return union_type;
28858
28859 default:
28860 return none_type;
28861 }
28862 }
28863
28864 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28865 or none_type otherwise or if the token is null. */
28866
28867 static enum tag_types
28868 cp_parser_token_is_type_parameter_key (cp_token* token)
28869 {
28870 if (!token)
28871 return none_type;
28872
28873 switch (token->keyword)
28874 {
28875 case RID_CLASS:
28876 return class_type;
28877 case RID_TYPENAME:
28878 return typename_type;
28879
28880 default:
28881 return none_type;
28882 }
28883 }
28884
28885 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28886
28887 static void
28888 cp_parser_check_class_key (enum tag_types class_key, tree type)
28889 {
28890 if (type == error_mark_node)
28891 return;
28892 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28893 {
28894 if (permerror (input_location, "%qs tag used in naming %q#T",
28895 class_key == union_type ? "union"
28896 : class_key == record_type ? "struct" : "class",
28897 type))
28898 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28899 "%q#T was previously declared here", type);
28900 }
28901 }
28902
28903 /* Issue an error message if DECL is redeclared with different
28904 access than its original declaration [class.access.spec/3].
28905 This applies to nested classes, nested class templates and
28906 enumerations [class.mem/1]. */
28907
28908 static void
28909 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28910 {
28911 if (!decl
28912 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28913 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28914 return;
28915
28916 if ((TREE_PRIVATE (decl)
28917 != (current_access_specifier == access_private_node))
28918 || (TREE_PROTECTED (decl)
28919 != (current_access_specifier == access_protected_node)))
28920 error_at (location, "%qD redeclared with different access", decl);
28921 }
28922
28923 /* Look for the `template' keyword, as a syntactic disambiguator.
28924 Return TRUE iff it is present, in which case it will be
28925 consumed. */
28926
28927 static bool
28928 cp_parser_optional_template_keyword (cp_parser *parser)
28929 {
28930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28931 {
28932 /* In C++98 the `template' keyword can only be used within templates;
28933 outside templates the parser can always figure out what is a
28934 template and what is not. In C++11, per the resolution of DR 468,
28935 `template' is allowed in cases where it is not strictly necessary. */
28936 if (!processing_template_decl
28937 && pedantic && cxx_dialect == cxx98)
28938 {
28939 cp_token *token = cp_lexer_peek_token (parser->lexer);
28940 pedwarn (token->location, OPT_Wpedantic,
28941 "in C++98 %<template%> (as a disambiguator) is only "
28942 "allowed within templates");
28943 /* If this part of the token stream is rescanned, the same
28944 error message would be generated. So, we purge the token
28945 from the stream. */
28946 cp_lexer_purge_token (parser->lexer);
28947 return false;
28948 }
28949 else
28950 {
28951 /* Consume the `template' keyword. */
28952 cp_lexer_consume_token (parser->lexer);
28953 return true;
28954 }
28955 }
28956 return false;
28957 }
28958
28959 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28960 set PARSER->SCOPE, and perform other related actions. */
28961
28962 static void
28963 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28964 {
28965 struct tree_check *check_value;
28966
28967 /* Get the stored value. */
28968 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28969 /* Set the scope from the stored value. */
28970 parser->scope = saved_checks_value (check_value);
28971 parser->qualifying_scope = check_value->qualifying_scope;
28972 parser->object_scope = NULL_TREE;
28973 }
28974
28975 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28976 encounter the end of a block before what we were looking for. */
28977
28978 static bool
28979 cp_parser_cache_group (cp_parser *parser,
28980 enum cpp_ttype end,
28981 unsigned depth)
28982 {
28983 while (true)
28984 {
28985 cp_token *token = cp_lexer_peek_token (parser->lexer);
28986
28987 /* Abort a parenthesized expression if we encounter a semicolon. */
28988 if ((end == CPP_CLOSE_PAREN || depth == 0)
28989 && token->type == CPP_SEMICOLON)
28990 return true;
28991 /* If we've reached the end of the file, stop. */
28992 if (token->type == CPP_EOF
28993 || (end != CPP_PRAGMA_EOL
28994 && token->type == CPP_PRAGMA_EOL))
28995 return true;
28996 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28997 /* We've hit the end of an enclosing block, so there's been some
28998 kind of syntax error. */
28999 return true;
29000
29001 /* Consume the token. */
29002 cp_lexer_consume_token (parser->lexer);
29003 /* See if it starts a new group. */
29004 if (token->type == CPP_OPEN_BRACE)
29005 {
29006 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29007 /* In theory this should probably check end == '}', but
29008 cp_parser_save_member_function_body needs it to exit
29009 after either '}' or ')' when called with ')'. */
29010 if (depth == 0)
29011 return false;
29012 }
29013 else if (token->type == CPP_OPEN_PAREN)
29014 {
29015 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29016 if (depth == 0 && end == CPP_CLOSE_PAREN)
29017 return false;
29018 }
29019 else if (token->type == CPP_PRAGMA)
29020 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29021 else if (token->type == end)
29022 return false;
29023 }
29024 }
29025
29026 /* Like above, for caching a default argument or NSDMI. Both of these are
29027 terminated by a non-nested comma, but it can be unclear whether or not a
29028 comma is nested in a template argument list unless we do more parsing.
29029 In order to handle this ambiguity, when we encounter a ',' after a '<'
29030 we try to parse what follows as a parameter-declaration-list (in the
29031 case of a default argument) or a member-declarator (in the case of an
29032 NSDMI). If that succeeds, then we stop caching. */
29033
29034 static tree
29035 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29036 {
29037 unsigned depth = 0;
29038 int maybe_template_id = 0;
29039 cp_token *first_token;
29040 cp_token *token;
29041 tree default_argument;
29042
29043 /* Add tokens until we have processed the entire default
29044 argument. We add the range [first_token, token). */
29045 first_token = cp_lexer_peek_token (parser->lexer);
29046 if (first_token->type == CPP_OPEN_BRACE)
29047 {
29048 /* For list-initialization, this is straightforward. */
29049 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29050 token = cp_lexer_peek_token (parser->lexer);
29051 }
29052 else while (true)
29053 {
29054 bool done = false;
29055
29056 /* Peek at the next token. */
29057 token = cp_lexer_peek_token (parser->lexer);
29058 /* What we do depends on what token we have. */
29059 switch (token->type)
29060 {
29061 /* In valid code, a default argument must be
29062 immediately followed by a `,' `)', or `...'. */
29063 case CPP_COMMA:
29064 if (depth == 0 && maybe_template_id)
29065 {
29066 /* If we've seen a '<', we might be in a
29067 template-argument-list. Until Core issue 325 is
29068 resolved, we don't know how this situation ought
29069 to be handled, so try to DTRT. We check whether
29070 what comes after the comma is a valid parameter
29071 declaration list. If it is, then the comma ends
29072 the default argument; otherwise the default
29073 argument continues. */
29074 bool error = false;
29075 cp_token *peek;
29076
29077 /* Set ITALP so cp_parser_parameter_declaration_list
29078 doesn't decide to commit to this parse. */
29079 bool saved_italp = parser->in_template_argument_list_p;
29080 parser->in_template_argument_list_p = true;
29081
29082 cp_parser_parse_tentatively (parser);
29083
29084 if (nsdmi)
29085 {
29086 /* Parse declarators until we reach a non-comma or
29087 somthing that cannot be an initializer.
29088 Just checking whether we're looking at a single
29089 declarator is insufficient. Consider:
29090 int var = tuple<T,U>::x;
29091 The template parameter 'U' looks exactly like a
29092 declarator. */
29093 do
29094 {
29095 int ctor_dtor_or_conv_p;
29096 cp_lexer_consume_token (parser->lexer);
29097 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29098 &ctor_dtor_or_conv_p,
29099 /*parenthesized_p=*/NULL,
29100 /*member_p=*/true,
29101 /*friend_p=*/false);
29102 peek = cp_lexer_peek_token (parser->lexer);
29103 if (cp_parser_error_occurred (parser))
29104 break;
29105 }
29106 while (peek->type == CPP_COMMA);
29107 /* If we met an '=' or ';' then the original comma
29108 was the end of the NSDMI. Otherwise assume
29109 we're still in the NSDMI. */
29110 error = (peek->type != CPP_EQ
29111 && peek->type != CPP_SEMICOLON);
29112 }
29113 else
29114 {
29115 cp_lexer_consume_token (parser->lexer);
29116 begin_scope (sk_function_parms, NULL_TREE);
29117 if (cp_parser_parameter_declaration_list (parser)
29118 == error_mark_node)
29119 error = true;
29120 pop_bindings_and_leave_scope ();
29121 }
29122 if (!cp_parser_error_occurred (parser) && !error)
29123 done = true;
29124 cp_parser_abort_tentative_parse (parser);
29125
29126 parser->in_template_argument_list_p = saved_italp;
29127 break;
29128 }
29129 /* FALLTHRU */
29130 case CPP_CLOSE_PAREN:
29131 case CPP_ELLIPSIS:
29132 /* If we run into a non-nested `;', `}', or `]',
29133 then the code is invalid -- but the default
29134 argument is certainly over. */
29135 case CPP_SEMICOLON:
29136 case CPP_CLOSE_BRACE:
29137 case CPP_CLOSE_SQUARE:
29138 if (depth == 0
29139 /* Handle correctly int n = sizeof ... ( p ); */
29140 && token->type != CPP_ELLIPSIS)
29141 done = true;
29142 /* Update DEPTH, if necessary. */
29143 else if (token->type == CPP_CLOSE_PAREN
29144 || token->type == CPP_CLOSE_BRACE
29145 || token->type == CPP_CLOSE_SQUARE)
29146 --depth;
29147 break;
29148
29149 case CPP_OPEN_PAREN:
29150 case CPP_OPEN_SQUARE:
29151 case CPP_OPEN_BRACE:
29152 ++depth;
29153 break;
29154
29155 case CPP_LESS:
29156 if (depth == 0)
29157 /* This might be the comparison operator, or it might
29158 start a template argument list. */
29159 ++maybe_template_id;
29160 break;
29161
29162 case CPP_RSHIFT:
29163 if (cxx_dialect == cxx98)
29164 break;
29165 /* Fall through for C++0x, which treats the `>>'
29166 operator like two `>' tokens in certain
29167 cases. */
29168 gcc_fallthrough ();
29169
29170 case CPP_GREATER:
29171 if (depth == 0)
29172 {
29173 /* This might be an operator, or it might close a
29174 template argument list. But if a previous '<'
29175 started a template argument list, this will have
29176 closed it, so we can't be in one anymore. */
29177 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29178 if (maybe_template_id < 0)
29179 maybe_template_id = 0;
29180 }
29181 break;
29182
29183 /* If we run out of tokens, issue an error message. */
29184 case CPP_EOF:
29185 case CPP_PRAGMA_EOL:
29186 error_at (token->location, "file ends in default argument");
29187 return error_mark_node;
29188
29189 case CPP_NAME:
29190 case CPP_SCOPE:
29191 /* In these cases, we should look for template-ids.
29192 For example, if the default argument is
29193 `X<int, double>()', we need to do name lookup to
29194 figure out whether or not `X' is a template; if
29195 so, the `,' does not end the default argument.
29196
29197 That is not yet done. */
29198 break;
29199
29200 default:
29201 break;
29202 }
29203
29204 /* If we've reached the end, stop. */
29205 if (done)
29206 break;
29207
29208 /* Add the token to the token block. */
29209 token = cp_lexer_consume_token (parser->lexer);
29210 }
29211
29212 /* Create a DEFAULT_ARG to represent the unparsed default
29213 argument. */
29214 default_argument = make_node (DEFAULT_ARG);
29215 DEFARG_TOKENS (default_argument)
29216 = cp_token_cache_new (first_token, token);
29217 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29218
29219 return default_argument;
29220 }
29221
29222 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29223
29224 location_t
29225 defarg_location (tree default_argument)
29226 {
29227 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29228 location_t start = tokens->first->location;
29229 location_t end = tokens->last->location;
29230 return make_location (start, start, end);
29231 }
29232
29233 /* Begin parsing tentatively. We always save tokens while parsing
29234 tentatively so that if the tentative parsing fails we can restore the
29235 tokens. */
29236
29237 static void
29238 cp_parser_parse_tentatively (cp_parser* parser)
29239 {
29240 /* Enter a new parsing context. */
29241 parser->context = cp_parser_context_new (parser->context);
29242 /* Begin saving tokens. */
29243 cp_lexer_save_tokens (parser->lexer);
29244 /* In order to avoid repetitive access control error messages,
29245 access checks are queued up until we are no longer parsing
29246 tentatively. */
29247 push_deferring_access_checks (dk_deferred);
29248 }
29249
29250 /* Commit to the currently active tentative parse. */
29251
29252 static void
29253 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29254 {
29255 cp_parser_context *context;
29256 cp_lexer *lexer;
29257
29258 /* Mark all of the levels as committed. */
29259 lexer = parser->lexer;
29260 for (context = parser->context; context->next; context = context->next)
29261 {
29262 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29263 break;
29264 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29265 while (!cp_lexer_saving_tokens (lexer))
29266 lexer = lexer->next;
29267 cp_lexer_commit_tokens (lexer);
29268 }
29269 }
29270
29271 /* Commit to the topmost currently active tentative parse.
29272
29273 Note that this function shouldn't be called when there are
29274 irreversible side-effects while in a tentative state. For
29275 example, we shouldn't create a permanent entry in the symbol
29276 table, or issue an error message that might not apply if the
29277 tentative parse is aborted. */
29278
29279 static void
29280 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29281 {
29282 cp_parser_context *context = parser->context;
29283 cp_lexer *lexer = parser->lexer;
29284
29285 if (context)
29286 {
29287 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29288 return;
29289 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29290
29291 while (!cp_lexer_saving_tokens (lexer))
29292 lexer = lexer->next;
29293 cp_lexer_commit_tokens (lexer);
29294 }
29295 }
29296
29297 /* Abort the currently active tentative parse. All consumed tokens
29298 will be rolled back, and no diagnostics will be issued. */
29299
29300 static void
29301 cp_parser_abort_tentative_parse (cp_parser* parser)
29302 {
29303 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29304 || errorcount > 0);
29305 cp_parser_simulate_error (parser);
29306 /* Now, pretend that we want to see if the construct was
29307 successfully parsed. */
29308 cp_parser_parse_definitely (parser);
29309 }
29310
29311 /* Stop parsing tentatively. If a parse error has occurred, restore the
29312 token stream. Otherwise, commit to the tokens we have consumed.
29313 Returns true if no error occurred; false otherwise. */
29314
29315 static bool
29316 cp_parser_parse_definitely (cp_parser* parser)
29317 {
29318 bool error_occurred;
29319 cp_parser_context *context;
29320
29321 /* Remember whether or not an error occurred, since we are about to
29322 destroy that information. */
29323 error_occurred = cp_parser_error_occurred (parser);
29324 /* Remove the topmost context from the stack. */
29325 context = parser->context;
29326 parser->context = context->next;
29327 /* If no parse errors occurred, commit to the tentative parse. */
29328 if (!error_occurred)
29329 {
29330 /* Commit to the tokens read tentatively, unless that was
29331 already done. */
29332 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29333 cp_lexer_commit_tokens (parser->lexer);
29334
29335 pop_to_parent_deferring_access_checks ();
29336 }
29337 /* Otherwise, if errors occurred, roll back our state so that things
29338 are just as they were before we began the tentative parse. */
29339 else
29340 {
29341 cp_lexer_rollback_tokens (parser->lexer);
29342 pop_deferring_access_checks ();
29343 }
29344 /* Add the context to the front of the free list. */
29345 context->next = cp_parser_context_free_list;
29346 cp_parser_context_free_list = context;
29347
29348 return !error_occurred;
29349 }
29350
29351 /* Returns true if we are parsing tentatively and are not committed to
29352 this tentative parse. */
29353
29354 static bool
29355 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29356 {
29357 return (cp_parser_parsing_tentatively (parser)
29358 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29359 }
29360
29361 /* Returns nonzero iff an error has occurred during the most recent
29362 tentative parse. */
29363
29364 static bool
29365 cp_parser_error_occurred (cp_parser* parser)
29366 {
29367 return (cp_parser_parsing_tentatively (parser)
29368 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29369 }
29370
29371 /* Returns nonzero if GNU extensions are allowed. */
29372
29373 static bool
29374 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29375 {
29376 return parser->allow_gnu_extensions_p;
29377 }
29378 \f
29379 /* Objective-C++ Productions */
29380
29381
29382 /* Parse an Objective-C expression, which feeds into a primary-expression
29383 above.
29384
29385 objc-expression:
29386 objc-message-expression
29387 objc-string-literal
29388 objc-encode-expression
29389 objc-protocol-expression
29390 objc-selector-expression
29391
29392 Returns a tree representation of the expression. */
29393
29394 static cp_expr
29395 cp_parser_objc_expression (cp_parser* parser)
29396 {
29397 /* Try to figure out what kind of declaration is present. */
29398 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29399
29400 switch (kwd->type)
29401 {
29402 case CPP_OPEN_SQUARE:
29403 return cp_parser_objc_message_expression (parser);
29404
29405 case CPP_OBJC_STRING:
29406 kwd = cp_lexer_consume_token (parser->lexer);
29407 return objc_build_string_object (kwd->u.value);
29408
29409 case CPP_KEYWORD:
29410 switch (kwd->keyword)
29411 {
29412 case RID_AT_ENCODE:
29413 return cp_parser_objc_encode_expression (parser);
29414
29415 case RID_AT_PROTOCOL:
29416 return cp_parser_objc_protocol_expression (parser);
29417
29418 case RID_AT_SELECTOR:
29419 return cp_parser_objc_selector_expression (parser);
29420
29421 default:
29422 break;
29423 }
29424 /* FALLTHRU */
29425 default:
29426 error_at (kwd->location,
29427 "misplaced %<@%D%> Objective-C++ construct",
29428 kwd->u.value);
29429 cp_parser_skip_to_end_of_block_or_statement (parser);
29430 }
29431
29432 return error_mark_node;
29433 }
29434
29435 /* Parse an Objective-C message expression.
29436
29437 objc-message-expression:
29438 [ objc-message-receiver objc-message-args ]
29439
29440 Returns a representation of an Objective-C message. */
29441
29442 static tree
29443 cp_parser_objc_message_expression (cp_parser* parser)
29444 {
29445 tree receiver, messageargs;
29446
29447 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29448 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29449 receiver = cp_parser_objc_message_receiver (parser);
29450 messageargs = cp_parser_objc_message_args (parser);
29451 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29452 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29453
29454 tree result = objc_build_message_expr (receiver, messageargs);
29455
29456 /* Construct a location e.g.
29457 [self func1:5]
29458 ^~~~~~~~~~~~~~
29459 ranging from the '[' to the ']', with the caret at the start. */
29460 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29461 protected_set_expr_location (result, combined_loc);
29462
29463 return result;
29464 }
29465
29466 /* Parse an objc-message-receiver.
29467
29468 objc-message-receiver:
29469 expression
29470 simple-type-specifier
29471
29472 Returns a representation of the type or expression. */
29473
29474 static tree
29475 cp_parser_objc_message_receiver (cp_parser* parser)
29476 {
29477 tree rcv;
29478
29479 /* An Objective-C message receiver may be either (1) a type
29480 or (2) an expression. */
29481 cp_parser_parse_tentatively (parser);
29482 rcv = cp_parser_expression (parser);
29483
29484 /* If that worked out, fine. */
29485 if (cp_parser_parse_definitely (parser))
29486 return rcv;
29487
29488 cp_parser_parse_tentatively (parser);
29489 rcv = cp_parser_simple_type_specifier (parser,
29490 /*decl_specs=*/NULL,
29491 CP_PARSER_FLAGS_NONE);
29492
29493 if (cp_parser_parse_definitely (parser))
29494 return objc_get_class_reference (rcv);
29495
29496 cp_parser_error (parser, "objective-c++ message receiver expected");
29497 return error_mark_node;
29498 }
29499
29500 /* Parse the arguments and selectors comprising an Objective-C message.
29501
29502 objc-message-args:
29503 objc-selector
29504 objc-selector-args
29505 objc-selector-args , objc-comma-args
29506
29507 objc-selector-args:
29508 objc-selector [opt] : assignment-expression
29509 objc-selector-args objc-selector [opt] : assignment-expression
29510
29511 objc-comma-args:
29512 assignment-expression
29513 objc-comma-args , assignment-expression
29514
29515 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29516 selector arguments and TREE_VALUE containing a list of comma
29517 arguments. */
29518
29519 static tree
29520 cp_parser_objc_message_args (cp_parser* parser)
29521 {
29522 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29523 bool maybe_unary_selector_p = true;
29524 cp_token *token = cp_lexer_peek_token (parser->lexer);
29525
29526 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29527 {
29528 tree selector = NULL_TREE, arg;
29529
29530 if (token->type != CPP_COLON)
29531 selector = cp_parser_objc_selector (parser);
29532
29533 /* Detect if we have a unary selector. */
29534 if (maybe_unary_selector_p
29535 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29536 return build_tree_list (selector, NULL_TREE);
29537
29538 maybe_unary_selector_p = false;
29539 cp_parser_require (parser, CPP_COLON, RT_COLON);
29540 arg = cp_parser_assignment_expression (parser);
29541
29542 sel_args
29543 = chainon (sel_args,
29544 build_tree_list (selector, arg));
29545
29546 token = cp_lexer_peek_token (parser->lexer);
29547 }
29548
29549 /* Handle non-selector arguments, if any. */
29550 while (token->type == CPP_COMMA)
29551 {
29552 tree arg;
29553
29554 cp_lexer_consume_token (parser->lexer);
29555 arg = cp_parser_assignment_expression (parser);
29556
29557 addl_args
29558 = chainon (addl_args,
29559 build_tree_list (NULL_TREE, arg));
29560
29561 token = cp_lexer_peek_token (parser->lexer);
29562 }
29563
29564 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29565 {
29566 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29567 return build_tree_list (error_mark_node, error_mark_node);
29568 }
29569
29570 return build_tree_list (sel_args, addl_args);
29571 }
29572
29573 /* Parse an Objective-C encode expression.
29574
29575 objc-encode-expression:
29576 @encode objc-typename
29577
29578 Returns an encoded representation of the type argument. */
29579
29580 static cp_expr
29581 cp_parser_objc_encode_expression (cp_parser* parser)
29582 {
29583 tree type;
29584 cp_token *token;
29585 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29586
29587 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29588 matching_parens parens;
29589 parens.require_open (parser);
29590 token = cp_lexer_peek_token (parser->lexer);
29591 type = complete_type (cp_parser_type_id (parser));
29592 parens.require_close (parser);
29593
29594 if (!type)
29595 {
29596 error_at (token->location,
29597 "%<@encode%> must specify a type as an argument");
29598 return error_mark_node;
29599 }
29600
29601 /* This happens if we find @encode(T) (where T is a template
29602 typename or something dependent on a template typename) when
29603 parsing a template. In that case, we can't compile it
29604 immediately, but we rather create an AT_ENCODE_EXPR which will
29605 need to be instantiated when the template is used.
29606 */
29607 if (dependent_type_p (type))
29608 {
29609 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29610 TREE_READONLY (value) = 1;
29611 return value;
29612 }
29613
29614
29615 /* Build a location of the form:
29616 @encode(int)
29617 ^~~~~~~~~~~~
29618 with caret==start at the @ token, finishing at the close paren. */
29619 location_t combined_loc
29620 = make_location (start_loc, start_loc,
29621 cp_lexer_previous_token (parser->lexer)->location);
29622
29623 return cp_expr (objc_build_encode_expr (type), combined_loc);
29624 }
29625
29626 /* Parse an Objective-C @defs expression. */
29627
29628 static tree
29629 cp_parser_objc_defs_expression (cp_parser *parser)
29630 {
29631 tree name;
29632
29633 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29634 matching_parens parens;
29635 parens.require_open (parser);
29636 name = cp_parser_identifier (parser);
29637 parens.require_close (parser);
29638
29639 return objc_get_class_ivars (name);
29640 }
29641
29642 /* Parse an Objective-C protocol expression.
29643
29644 objc-protocol-expression:
29645 @protocol ( identifier )
29646
29647 Returns a representation of the protocol expression. */
29648
29649 static tree
29650 cp_parser_objc_protocol_expression (cp_parser* parser)
29651 {
29652 tree proto;
29653 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29654
29655 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29656 matching_parens parens;
29657 parens.require_open (parser);
29658 proto = cp_parser_identifier (parser);
29659 parens.require_close (parser);
29660
29661 /* Build a location of the form:
29662 @protocol(prot)
29663 ^~~~~~~~~~~~~~~
29664 with caret==start at the @ token, finishing at the close paren. */
29665 location_t combined_loc
29666 = make_location (start_loc, start_loc,
29667 cp_lexer_previous_token (parser->lexer)->location);
29668 tree result = objc_build_protocol_expr (proto);
29669 protected_set_expr_location (result, combined_loc);
29670 return result;
29671 }
29672
29673 /* Parse an Objective-C selector expression.
29674
29675 objc-selector-expression:
29676 @selector ( objc-method-signature )
29677
29678 objc-method-signature:
29679 objc-selector
29680 objc-selector-seq
29681
29682 objc-selector-seq:
29683 objc-selector :
29684 objc-selector-seq objc-selector :
29685
29686 Returns a representation of the method selector. */
29687
29688 static tree
29689 cp_parser_objc_selector_expression (cp_parser* parser)
29690 {
29691 tree sel_seq = NULL_TREE;
29692 bool maybe_unary_selector_p = true;
29693 cp_token *token;
29694 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29695
29696 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29697 matching_parens parens;
29698 parens.require_open (parser);
29699 token = cp_lexer_peek_token (parser->lexer);
29700
29701 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29702 || token->type == CPP_SCOPE)
29703 {
29704 tree selector = NULL_TREE;
29705
29706 if (token->type != CPP_COLON
29707 || token->type == CPP_SCOPE)
29708 selector = cp_parser_objc_selector (parser);
29709
29710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29711 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29712 {
29713 /* Detect if we have a unary selector. */
29714 if (maybe_unary_selector_p)
29715 {
29716 sel_seq = selector;
29717 goto finish_selector;
29718 }
29719 else
29720 {
29721 cp_parser_error (parser, "expected %<:%>");
29722 }
29723 }
29724 maybe_unary_selector_p = false;
29725 token = cp_lexer_consume_token (parser->lexer);
29726
29727 if (token->type == CPP_SCOPE)
29728 {
29729 sel_seq
29730 = chainon (sel_seq,
29731 build_tree_list (selector, NULL_TREE));
29732 sel_seq
29733 = chainon (sel_seq,
29734 build_tree_list (NULL_TREE, NULL_TREE));
29735 }
29736 else
29737 sel_seq
29738 = chainon (sel_seq,
29739 build_tree_list (selector, NULL_TREE));
29740
29741 token = cp_lexer_peek_token (parser->lexer);
29742 }
29743
29744 finish_selector:
29745 parens.require_close (parser);
29746
29747
29748 /* Build a location of the form:
29749 @selector(func)
29750 ^~~~~~~~~~~~~~~
29751 with caret==start at the @ token, finishing at the close paren. */
29752 location_t combined_loc
29753 = make_location (loc, loc,
29754 cp_lexer_previous_token (parser->lexer)->location);
29755 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29756 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29757 protected_set_expr_location (result, combined_loc);
29758 return result;
29759 }
29760
29761 /* Parse a list of identifiers.
29762
29763 objc-identifier-list:
29764 identifier
29765 objc-identifier-list , identifier
29766
29767 Returns a TREE_LIST of identifier nodes. */
29768
29769 static tree
29770 cp_parser_objc_identifier_list (cp_parser* parser)
29771 {
29772 tree identifier;
29773 tree list;
29774 cp_token *sep;
29775
29776 identifier = cp_parser_identifier (parser);
29777 if (identifier == error_mark_node)
29778 return error_mark_node;
29779
29780 list = build_tree_list (NULL_TREE, identifier);
29781 sep = cp_lexer_peek_token (parser->lexer);
29782
29783 while (sep->type == CPP_COMMA)
29784 {
29785 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29786 identifier = cp_parser_identifier (parser);
29787 if (identifier == error_mark_node)
29788 return list;
29789
29790 list = chainon (list, build_tree_list (NULL_TREE,
29791 identifier));
29792 sep = cp_lexer_peek_token (parser->lexer);
29793 }
29794
29795 return list;
29796 }
29797
29798 /* Parse an Objective-C alias declaration.
29799
29800 objc-alias-declaration:
29801 @compatibility_alias identifier identifier ;
29802
29803 This function registers the alias mapping with the Objective-C front end.
29804 It returns nothing. */
29805
29806 static void
29807 cp_parser_objc_alias_declaration (cp_parser* parser)
29808 {
29809 tree alias, orig;
29810
29811 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29812 alias = cp_parser_identifier (parser);
29813 orig = cp_parser_identifier (parser);
29814 objc_declare_alias (alias, orig);
29815 cp_parser_consume_semicolon_at_end_of_statement (parser);
29816 }
29817
29818 /* Parse an Objective-C class forward-declaration.
29819
29820 objc-class-declaration:
29821 @class objc-identifier-list ;
29822
29823 The function registers the forward declarations with the Objective-C
29824 front end. It returns nothing. */
29825
29826 static void
29827 cp_parser_objc_class_declaration (cp_parser* parser)
29828 {
29829 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29830 while (true)
29831 {
29832 tree id;
29833
29834 id = cp_parser_identifier (parser);
29835 if (id == error_mark_node)
29836 break;
29837
29838 objc_declare_class (id);
29839
29840 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29841 cp_lexer_consume_token (parser->lexer);
29842 else
29843 break;
29844 }
29845 cp_parser_consume_semicolon_at_end_of_statement (parser);
29846 }
29847
29848 /* Parse a list of Objective-C protocol references.
29849
29850 objc-protocol-refs-opt:
29851 objc-protocol-refs [opt]
29852
29853 objc-protocol-refs:
29854 < objc-identifier-list >
29855
29856 Returns a TREE_LIST of identifiers, if any. */
29857
29858 static tree
29859 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29860 {
29861 tree protorefs = NULL_TREE;
29862
29863 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29864 {
29865 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29866 protorefs = cp_parser_objc_identifier_list (parser);
29867 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29868 }
29869
29870 return protorefs;
29871 }
29872
29873 /* Parse a Objective-C visibility specification. */
29874
29875 static void
29876 cp_parser_objc_visibility_spec (cp_parser* parser)
29877 {
29878 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29879
29880 switch (vis->keyword)
29881 {
29882 case RID_AT_PRIVATE:
29883 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29884 break;
29885 case RID_AT_PROTECTED:
29886 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29887 break;
29888 case RID_AT_PUBLIC:
29889 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29890 break;
29891 case RID_AT_PACKAGE:
29892 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29893 break;
29894 default:
29895 return;
29896 }
29897
29898 /* Eat '@private'/'@protected'/'@public'. */
29899 cp_lexer_consume_token (parser->lexer);
29900 }
29901
29902 /* Parse an Objective-C method type. Return 'true' if it is a class
29903 (+) method, and 'false' if it is an instance (-) method. */
29904
29905 static inline bool
29906 cp_parser_objc_method_type (cp_parser* parser)
29907 {
29908 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29909 return true;
29910 else
29911 return false;
29912 }
29913
29914 /* Parse an Objective-C protocol qualifier. */
29915
29916 static tree
29917 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29918 {
29919 tree quals = NULL_TREE, node;
29920 cp_token *token = cp_lexer_peek_token (parser->lexer);
29921
29922 node = token->u.value;
29923
29924 while (node && identifier_p (node)
29925 && (node == ridpointers [(int) RID_IN]
29926 || node == ridpointers [(int) RID_OUT]
29927 || node == ridpointers [(int) RID_INOUT]
29928 || node == ridpointers [(int) RID_BYCOPY]
29929 || node == ridpointers [(int) RID_BYREF]
29930 || node == ridpointers [(int) RID_ONEWAY]))
29931 {
29932 quals = tree_cons (NULL_TREE, node, quals);
29933 cp_lexer_consume_token (parser->lexer);
29934 token = cp_lexer_peek_token (parser->lexer);
29935 node = token->u.value;
29936 }
29937
29938 return quals;
29939 }
29940
29941 /* Parse an Objective-C typename. */
29942
29943 static tree
29944 cp_parser_objc_typename (cp_parser* parser)
29945 {
29946 tree type_name = NULL_TREE;
29947
29948 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29949 {
29950 tree proto_quals, cp_type = NULL_TREE;
29951
29952 matching_parens parens;
29953 parens.consume_open (parser); /* Eat '('. */
29954 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29955
29956 /* An ObjC type name may consist of just protocol qualifiers, in which
29957 case the type shall default to 'id'. */
29958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29959 {
29960 cp_type = cp_parser_type_id (parser);
29961
29962 /* If the type could not be parsed, an error has already
29963 been produced. For error recovery, behave as if it had
29964 not been specified, which will use the default type
29965 'id'. */
29966 if (cp_type == error_mark_node)
29967 {
29968 cp_type = NULL_TREE;
29969 /* We need to skip to the closing parenthesis as
29970 cp_parser_type_id() does not seem to do it for
29971 us. */
29972 cp_parser_skip_to_closing_parenthesis (parser,
29973 /*recovering=*/true,
29974 /*or_comma=*/false,
29975 /*consume_paren=*/false);
29976 }
29977 }
29978
29979 parens.require_close (parser);
29980 type_name = build_tree_list (proto_quals, cp_type);
29981 }
29982
29983 return type_name;
29984 }
29985
29986 /* Check to see if TYPE refers to an Objective-C selector name. */
29987
29988 static bool
29989 cp_parser_objc_selector_p (enum cpp_ttype type)
29990 {
29991 return (type == CPP_NAME || type == CPP_KEYWORD
29992 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29993 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29994 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29995 || type == CPP_XOR || type == CPP_XOR_EQ);
29996 }
29997
29998 /* Parse an Objective-C selector. */
29999
30000 static tree
30001 cp_parser_objc_selector (cp_parser* parser)
30002 {
30003 cp_token *token = cp_lexer_consume_token (parser->lexer);
30004
30005 if (!cp_parser_objc_selector_p (token->type))
30006 {
30007 error_at (token->location, "invalid Objective-C++ selector name");
30008 return error_mark_node;
30009 }
30010
30011 /* C++ operator names are allowed to appear in ObjC selectors. */
30012 switch (token->type)
30013 {
30014 case CPP_AND_AND: return get_identifier ("and");
30015 case CPP_AND_EQ: return get_identifier ("and_eq");
30016 case CPP_AND: return get_identifier ("bitand");
30017 case CPP_OR: return get_identifier ("bitor");
30018 case CPP_COMPL: return get_identifier ("compl");
30019 case CPP_NOT: return get_identifier ("not");
30020 case CPP_NOT_EQ: return get_identifier ("not_eq");
30021 case CPP_OR_OR: return get_identifier ("or");
30022 case CPP_OR_EQ: return get_identifier ("or_eq");
30023 case CPP_XOR: return get_identifier ("xor");
30024 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30025 default: return token->u.value;
30026 }
30027 }
30028
30029 /* Parse an Objective-C params list. */
30030
30031 static tree
30032 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30033 {
30034 tree params = NULL_TREE;
30035 bool maybe_unary_selector_p = true;
30036 cp_token *token = cp_lexer_peek_token (parser->lexer);
30037
30038 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30039 {
30040 tree selector = NULL_TREE, type_name, identifier;
30041 tree parm_attr = NULL_TREE;
30042
30043 if (token->keyword == RID_ATTRIBUTE)
30044 break;
30045
30046 if (token->type != CPP_COLON)
30047 selector = cp_parser_objc_selector (parser);
30048
30049 /* Detect if we have a unary selector. */
30050 if (maybe_unary_selector_p
30051 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30052 {
30053 params = selector; /* Might be followed by attributes. */
30054 break;
30055 }
30056
30057 maybe_unary_selector_p = false;
30058 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30059 {
30060 /* Something went quite wrong. There should be a colon
30061 here, but there is not. Stop parsing parameters. */
30062 break;
30063 }
30064 type_name = cp_parser_objc_typename (parser);
30065 /* New ObjC allows attributes on parameters too. */
30066 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30067 parm_attr = cp_parser_attributes_opt (parser);
30068 identifier = cp_parser_identifier (parser);
30069
30070 params
30071 = chainon (params,
30072 objc_build_keyword_decl (selector,
30073 type_name,
30074 identifier,
30075 parm_attr));
30076
30077 token = cp_lexer_peek_token (parser->lexer);
30078 }
30079
30080 if (params == NULL_TREE)
30081 {
30082 cp_parser_error (parser, "objective-c++ method declaration is expected");
30083 return error_mark_node;
30084 }
30085
30086 /* We allow tail attributes for the method. */
30087 if (token->keyword == RID_ATTRIBUTE)
30088 {
30089 *attributes = cp_parser_attributes_opt (parser);
30090 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30091 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30092 return params;
30093 cp_parser_error (parser,
30094 "method attributes must be specified at the end");
30095 return error_mark_node;
30096 }
30097
30098 if (params == NULL_TREE)
30099 {
30100 cp_parser_error (parser, "objective-c++ method declaration is expected");
30101 return error_mark_node;
30102 }
30103 return params;
30104 }
30105
30106 /* Parse the non-keyword Objective-C params. */
30107
30108 static tree
30109 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30110 tree* attributes)
30111 {
30112 tree params = make_node (TREE_LIST);
30113 cp_token *token = cp_lexer_peek_token (parser->lexer);
30114 *ellipsisp = false; /* Initially, assume no ellipsis. */
30115
30116 while (token->type == CPP_COMMA)
30117 {
30118 cp_parameter_declarator *parmdecl;
30119 tree parm;
30120
30121 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30122 token = cp_lexer_peek_token (parser->lexer);
30123
30124 if (token->type == CPP_ELLIPSIS)
30125 {
30126 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30127 *ellipsisp = true;
30128 token = cp_lexer_peek_token (parser->lexer);
30129 break;
30130 }
30131
30132 /* TODO: parse attributes for tail parameters. */
30133 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30134 parm = grokdeclarator (parmdecl->declarator,
30135 &parmdecl->decl_specifiers,
30136 PARM, /*initialized=*/0,
30137 /*attrlist=*/NULL);
30138
30139 chainon (params, build_tree_list (NULL_TREE, parm));
30140 token = cp_lexer_peek_token (parser->lexer);
30141 }
30142
30143 /* We allow tail attributes for the method. */
30144 if (token->keyword == RID_ATTRIBUTE)
30145 {
30146 if (*attributes == NULL_TREE)
30147 {
30148 *attributes = cp_parser_attributes_opt (parser);
30149 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30150 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30151 return params;
30152 }
30153 else
30154 /* We have an error, but parse the attributes, so that we can
30155 carry on. */
30156 *attributes = cp_parser_attributes_opt (parser);
30157
30158 cp_parser_error (parser,
30159 "method attributes must be specified at the end");
30160 return error_mark_node;
30161 }
30162
30163 return params;
30164 }
30165
30166 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30167
30168 static void
30169 cp_parser_objc_interstitial_code (cp_parser* parser)
30170 {
30171 cp_token *token = cp_lexer_peek_token (parser->lexer);
30172
30173 /* If the next token is `extern' and the following token is a string
30174 literal, then we have a linkage specification. */
30175 if (token->keyword == RID_EXTERN
30176 && cp_parser_is_pure_string_literal
30177 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30178 cp_parser_linkage_specification (parser);
30179 /* Handle #pragma, if any. */
30180 else if (token->type == CPP_PRAGMA)
30181 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30182 /* Allow stray semicolons. */
30183 else if (token->type == CPP_SEMICOLON)
30184 cp_lexer_consume_token (parser->lexer);
30185 /* Mark methods as optional or required, when building protocols. */
30186 else if (token->keyword == RID_AT_OPTIONAL)
30187 {
30188 cp_lexer_consume_token (parser->lexer);
30189 objc_set_method_opt (true);
30190 }
30191 else if (token->keyword == RID_AT_REQUIRED)
30192 {
30193 cp_lexer_consume_token (parser->lexer);
30194 objc_set_method_opt (false);
30195 }
30196 else if (token->keyword == RID_NAMESPACE)
30197 cp_parser_namespace_definition (parser);
30198 /* Other stray characters must generate errors. */
30199 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30200 {
30201 cp_lexer_consume_token (parser->lexer);
30202 error ("stray %qs between Objective-C++ methods",
30203 token->type == CPP_OPEN_BRACE ? "{" : "}");
30204 }
30205 /* Finally, try to parse a block-declaration, or a function-definition. */
30206 else
30207 cp_parser_block_declaration (parser, /*statement_p=*/false);
30208 }
30209
30210 /* Parse a method signature. */
30211
30212 static tree
30213 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30214 {
30215 tree rettype, kwdparms, optparms;
30216 bool ellipsis = false;
30217 bool is_class_method;
30218
30219 is_class_method = cp_parser_objc_method_type (parser);
30220 rettype = cp_parser_objc_typename (parser);
30221 *attributes = NULL_TREE;
30222 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30223 if (kwdparms == error_mark_node)
30224 return error_mark_node;
30225 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30226 if (optparms == error_mark_node)
30227 return error_mark_node;
30228
30229 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30230 }
30231
30232 static bool
30233 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30234 {
30235 tree tattr;
30236 cp_lexer_save_tokens (parser->lexer);
30237 tattr = cp_parser_attributes_opt (parser);
30238 gcc_assert (tattr) ;
30239
30240 /* If the attributes are followed by a method introducer, this is not allowed.
30241 Dump the attributes and flag the situation. */
30242 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30243 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30244 return true;
30245
30246 /* Otherwise, the attributes introduce some interstitial code, possibly so
30247 rewind to allow that check. */
30248 cp_lexer_rollback_tokens (parser->lexer);
30249 return false;
30250 }
30251
30252 /* Parse an Objective-C method prototype list. */
30253
30254 static void
30255 cp_parser_objc_method_prototype_list (cp_parser* parser)
30256 {
30257 cp_token *token = cp_lexer_peek_token (parser->lexer);
30258
30259 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30260 {
30261 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30262 {
30263 tree attributes, sig;
30264 bool is_class_method;
30265 if (token->type == CPP_PLUS)
30266 is_class_method = true;
30267 else
30268 is_class_method = false;
30269 sig = cp_parser_objc_method_signature (parser, &attributes);
30270 if (sig == error_mark_node)
30271 {
30272 cp_parser_skip_to_end_of_block_or_statement (parser);
30273 token = cp_lexer_peek_token (parser->lexer);
30274 continue;
30275 }
30276 objc_add_method_declaration (is_class_method, sig, attributes);
30277 cp_parser_consume_semicolon_at_end_of_statement (parser);
30278 }
30279 else if (token->keyword == RID_AT_PROPERTY)
30280 cp_parser_objc_at_property_declaration (parser);
30281 else if (token->keyword == RID_ATTRIBUTE
30282 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30283 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30284 OPT_Wattributes,
30285 "prefix attributes are ignored for methods");
30286 else
30287 /* Allow for interspersed non-ObjC++ code. */
30288 cp_parser_objc_interstitial_code (parser);
30289
30290 token = cp_lexer_peek_token (parser->lexer);
30291 }
30292
30293 if (token->type != CPP_EOF)
30294 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30295 else
30296 cp_parser_error (parser, "expected %<@end%>");
30297
30298 objc_finish_interface ();
30299 }
30300
30301 /* Parse an Objective-C method definition list. */
30302
30303 static void
30304 cp_parser_objc_method_definition_list (cp_parser* parser)
30305 {
30306 cp_token *token = cp_lexer_peek_token (parser->lexer);
30307
30308 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30309 {
30310 tree meth;
30311
30312 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30313 {
30314 cp_token *ptk;
30315 tree sig, attribute;
30316 bool is_class_method;
30317 if (token->type == CPP_PLUS)
30318 is_class_method = true;
30319 else
30320 is_class_method = false;
30321 push_deferring_access_checks (dk_deferred);
30322 sig = cp_parser_objc_method_signature (parser, &attribute);
30323 if (sig == error_mark_node)
30324 {
30325 cp_parser_skip_to_end_of_block_or_statement (parser);
30326 token = cp_lexer_peek_token (parser->lexer);
30327 continue;
30328 }
30329 objc_start_method_definition (is_class_method, sig, attribute,
30330 NULL_TREE);
30331
30332 /* For historical reasons, we accept an optional semicolon. */
30333 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30334 cp_lexer_consume_token (parser->lexer);
30335
30336 ptk = cp_lexer_peek_token (parser->lexer);
30337 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30338 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30339 {
30340 perform_deferred_access_checks (tf_warning_or_error);
30341 stop_deferring_access_checks ();
30342 meth = cp_parser_function_definition_after_declarator (parser,
30343 false);
30344 pop_deferring_access_checks ();
30345 objc_finish_method_definition (meth);
30346 }
30347 }
30348 /* The following case will be removed once @synthesize is
30349 completely implemented. */
30350 else if (token->keyword == RID_AT_PROPERTY)
30351 cp_parser_objc_at_property_declaration (parser);
30352 else if (token->keyword == RID_AT_SYNTHESIZE)
30353 cp_parser_objc_at_synthesize_declaration (parser);
30354 else if (token->keyword == RID_AT_DYNAMIC)
30355 cp_parser_objc_at_dynamic_declaration (parser);
30356 else if (token->keyword == RID_ATTRIBUTE
30357 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30358 warning_at (token->location, OPT_Wattributes,
30359 "prefix attributes are ignored for methods");
30360 else
30361 /* Allow for interspersed non-ObjC++ code. */
30362 cp_parser_objc_interstitial_code (parser);
30363
30364 token = cp_lexer_peek_token (parser->lexer);
30365 }
30366
30367 if (token->type != CPP_EOF)
30368 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30369 else
30370 cp_parser_error (parser, "expected %<@end%>");
30371
30372 objc_finish_implementation ();
30373 }
30374
30375 /* Parse Objective-C ivars. */
30376
30377 static void
30378 cp_parser_objc_class_ivars (cp_parser* parser)
30379 {
30380 cp_token *token = cp_lexer_peek_token (parser->lexer);
30381
30382 if (token->type != CPP_OPEN_BRACE)
30383 return; /* No ivars specified. */
30384
30385 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30386 token = cp_lexer_peek_token (parser->lexer);
30387
30388 while (token->type != CPP_CLOSE_BRACE
30389 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30390 {
30391 cp_decl_specifier_seq declspecs;
30392 int decl_class_or_enum_p;
30393 tree prefix_attributes;
30394
30395 cp_parser_objc_visibility_spec (parser);
30396
30397 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30398 break;
30399
30400 cp_parser_decl_specifier_seq (parser,
30401 CP_PARSER_FLAGS_OPTIONAL,
30402 &declspecs,
30403 &decl_class_or_enum_p);
30404
30405 /* auto, register, static, extern, mutable. */
30406 if (declspecs.storage_class != sc_none)
30407 {
30408 cp_parser_error (parser, "invalid type for instance variable");
30409 declspecs.storage_class = sc_none;
30410 }
30411
30412 /* thread_local. */
30413 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30414 {
30415 cp_parser_error (parser, "invalid type for instance variable");
30416 declspecs.locations[ds_thread] = 0;
30417 }
30418
30419 /* typedef. */
30420 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30421 {
30422 cp_parser_error (parser, "invalid type for instance variable");
30423 declspecs.locations[ds_typedef] = 0;
30424 }
30425
30426 prefix_attributes = declspecs.attributes;
30427 declspecs.attributes = NULL_TREE;
30428
30429 /* Keep going until we hit the `;' at the end of the
30430 declaration. */
30431 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30432 {
30433 tree width = NULL_TREE, attributes, first_attribute, decl;
30434 cp_declarator *declarator = NULL;
30435 int ctor_dtor_or_conv_p;
30436
30437 /* Check for a (possibly unnamed) bitfield declaration. */
30438 token = cp_lexer_peek_token (parser->lexer);
30439 if (token->type == CPP_COLON)
30440 goto eat_colon;
30441
30442 if (token->type == CPP_NAME
30443 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30444 == CPP_COLON))
30445 {
30446 /* Get the name of the bitfield. */
30447 declarator = make_id_declarator (NULL_TREE,
30448 cp_parser_identifier (parser),
30449 sfk_none);
30450
30451 eat_colon:
30452 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30453 /* Get the width of the bitfield. */
30454 width
30455 = cp_parser_constant_expression (parser);
30456 }
30457 else
30458 {
30459 /* Parse the declarator. */
30460 declarator
30461 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30462 &ctor_dtor_or_conv_p,
30463 /*parenthesized_p=*/NULL,
30464 /*member_p=*/false,
30465 /*friend_p=*/false);
30466 }
30467
30468 /* Look for attributes that apply to the ivar. */
30469 attributes = cp_parser_attributes_opt (parser);
30470 /* Remember which attributes are prefix attributes and
30471 which are not. */
30472 first_attribute = attributes;
30473 /* Combine the attributes. */
30474 attributes = attr_chainon (prefix_attributes, attributes);
30475
30476 if (width)
30477 /* Create the bitfield declaration. */
30478 decl = grokbitfield (declarator, &declspecs,
30479 width, NULL_TREE, attributes);
30480 else
30481 decl = grokfield (declarator, &declspecs,
30482 NULL_TREE, /*init_const_expr_p=*/false,
30483 NULL_TREE, attributes);
30484
30485 /* Add the instance variable. */
30486 if (decl != error_mark_node && decl != NULL_TREE)
30487 objc_add_instance_variable (decl);
30488
30489 /* Reset PREFIX_ATTRIBUTES. */
30490 if (attributes != error_mark_node)
30491 {
30492 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30493 attributes = TREE_CHAIN (attributes);
30494 if (attributes)
30495 TREE_CHAIN (attributes) = NULL_TREE;
30496 }
30497
30498 token = cp_lexer_peek_token (parser->lexer);
30499
30500 if (token->type == CPP_COMMA)
30501 {
30502 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30503 continue;
30504 }
30505 break;
30506 }
30507
30508 cp_parser_consume_semicolon_at_end_of_statement (parser);
30509 token = cp_lexer_peek_token (parser->lexer);
30510 }
30511
30512 if (token->keyword == RID_AT_END)
30513 cp_parser_error (parser, "expected %<}%>");
30514
30515 /* Do not consume the RID_AT_END, so it will be read again as terminating
30516 the @interface of @implementation. */
30517 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30518 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30519
30520 /* For historical reasons, we accept an optional semicolon. */
30521 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30522 cp_lexer_consume_token (parser->lexer);
30523 }
30524
30525 /* Parse an Objective-C protocol declaration. */
30526
30527 static void
30528 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30529 {
30530 tree proto, protorefs;
30531 cp_token *tok;
30532
30533 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30535 {
30536 tok = cp_lexer_peek_token (parser->lexer);
30537 error_at (tok->location, "identifier expected after %<@protocol%>");
30538 cp_parser_consume_semicolon_at_end_of_statement (parser);
30539 return;
30540 }
30541
30542 /* See if we have a forward declaration or a definition. */
30543 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30544
30545 /* Try a forward declaration first. */
30546 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30547 {
30548 while (true)
30549 {
30550 tree id;
30551
30552 id = cp_parser_identifier (parser);
30553 if (id == error_mark_node)
30554 break;
30555
30556 objc_declare_protocol (id, attributes);
30557
30558 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30559 cp_lexer_consume_token (parser->lexer);
30560 else
30561 break;
30562 }
30563 cp_parser_consume_semicolon_at_end_of_statement (parser);
30564 }
30565
30566 /* Ok, we got a full-fledged definition (or at least should). */
30567 else
30568 {
30569 proto = cp_parser_identifier (parser);
30570 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30571 objc_start_protocol (proto, protorefs, attributes);
30572 cp_parser_objc_method_prototype_list (parser);
30573 }
30574 }
30575
30576 /* Parse an Objective-C superclass or category. */
30577
30578 static void
30579 cp_parser_objc_superclass_or_category (cp_parser *parser,
30580 bool iface_p,
30581 tree *super,
30582 tree *categ, bool *is_class_extension)
30583 {
30584 cp_token *next = cp_lexer_peek_token (parser->lexer);
30585
30586 *super = *categ = NULL_TREE;
30587 *is_class_extension = false;
30588 if (next->type == CPP_COLON)
30589 {
30590 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30591 *super = cp_parser_identifier (parser);
30592 }
30593 else if (next->type == CPP_OPEN_PAREN)
30594 {
30595 matching_parens parens;
30596 parens.consume_open (parser); /* Eat '('. */
30597
30598 /* If there is no category name, and this is an @interface, we
30599 have a class extension. */
30600 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30601 {
30602 *categ = NULL_TREE;
30603 *is_class_extension = true;
30604 }
30605 else
30606 *categ = cp_parser_identifier (parser);
30607
30608 parens.require_close (parser);
30609 }
30610 }
30611
30612 /* Parse an Objective-C class interface. */
30613
30614 static void
30615 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30616 {
30617 tree name, super, categ, protos;
30618 bool is_class_extension;
30619
30620 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30621 name = cp_parser_identifier (parser);
30622 if (name == error_mark_node)
30623 {
30624 /* It's hard to recover because even if valid @interface stuff
30625 is to follow, we can't compile it (or validate it) if we
30626 don't even know which class it refers to. Let's assume this
30627 was a stray '@interface' token in the stream and skip it.
30628 */
30629 return;
30630 }
30631 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30632 &is_class_extension);
30633 protos = cp_parser_objc_protocol_refs_opt (parser);
30634
30635 /* We have either a class or a category on our hands. */
30636 if (categ || is_class_extension)
30637 objc_start_category_interface (name, categ, protos, attributes);
30638 else
30639 {
30640 objc_start_class_interface (name, super, protos, attributes);
30641 /* Handle instance variable declarations, if any. */
30642 cp_parser_objc_class_ivars (parser);
30643 objc_continue_interface ();
30644 }
30645
30646 cp_parser_objc_method_prototype_list (parser);
30647 }
30648
30649 /* Parse an Objective-C class implementation. */
30650
30651 static void
30652 cp_parser_objc_class_implementation (cp_parser* parser)
30653 {
30654 tree name, super, categ;
30655 bool is_class_extension;
30656
30657 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30658 name = cp_parser_identifier (parser);
30659 if (name == error_mark_node)
30660 {
30661 /* It's hard to recover because even if valid @implementation
30662 stuff is to follow, we can't compile it (or validate it) if
30663 we don't even know which class it refers to. Let's assume
30664 this was a stray '@implementation' token in the stream and
30665 skip it.
30666 */
30667 return;
30668 }
30669 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30670 &is_class_extension);
30671
30672 /* We have either a class or a category on our hands. */
30673 if (categ)
30674 objc_start_category_implementation (name, categ);
30675 else
30676 {
30677 objc_start_class_implementation (name, super);
30678 /* Handle instance variable declarations, if any. */
30679 cp_parser_objc_class_ivars (parser);
30680 objc_continue_implementation ();
30681 }
30682
30683 cp_parser_objc_method_definition_list (parser);
30684 }
30685
30686 /* Consume the @end token and finish off the implementation. */
30687
30688 static void
30689 cp_parser_objc_end_implementation (cp_parser* parser)
30690 {
30691 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30692 objc_finish_implementation ();
30693 }
30694
30695 /* Parse an Objective-C declaration. */
30696
30697 static void
30698 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30699 {
30700 /* Try to figure out what kind of declaration is present. */
30701 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30702
30703 if (attributes)
30704 switch (kwd->keyword)
30705 {
30706 case RID_AT_ALIAS:
30707 case RID_AT_CLASS:
30708 case RID_AT_END:
30709 error_at (kwd->location, "attributes may not be specified before"
30710 " the %<@%D%> Objective-C++ keyword",
30711 kwd->u.value);
30712 attributes = NULL;
30713 break;
30714 case RID_AT_IMPLEMENTATION:
30715 warning_at (kwd->location, OPT_Wattributes,
30716 "prefix attributes are ignored before %<@%D%>",
30717 kwd->u.value);
30718 attributes = NULL;
30719 default:
30720 break;
30721 }
30722
30723 switch (kwd->keyword)
30724 {
30725 case RID_AT_ALIAS:
30726 cp_parser_objc_alias_declaration (parser);
30727 break;
30728 case RID_AT_CLASS:
30729 cp_parser_objc_class_declaration (parser);
30730 break;
30731 case RID_AT_PROTOCOL:
30732 cp_parser_objc_protocol_declaration (parser, attributes);
30733 break;
30734 case RID_AT_INTERFACE:
30735 cp_parser_objc_class_interface (parser, attributes);
30736 break;
30737 case RID_AT_IMPLEMENTATION:
30738 cp_parser_objc_class_implementation (parser);
30739 break;
30740 case RID_AT_END:
30741 cp_parser_objc_end_implementation (parser);
30742 break;
30743 default:
30744 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30745 kwd->u.value);
30746 cp_parser_skip_to_end_of_block_or_statement (parser);
30747 }
30748 }
30749
30750 /* Parse an Objective-C try-catch-finally statement.
30751
30752 objc-try-catch-finally-stmt:
30753 @try compound-statement objc-catch-clause-seq [opt]
30754 objc-finally-clause [opt]
30755
30756 objc-catch-clause-seq:
30757 objc-catch-clause objc-catch-clause-seq [opt]
30758
30759 objc-catch-clause:
30760 @catch ( objc-exception-declaration ) compound-statement
30761
30762 objc-finally-clause:
30763 @finally compound-statement
30764
30765 objc-exception-declaration:
30766 parameter-declaration
30767 '...'
30768
30769 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30770
30771 Returns NULL_TREE.
30772
30773 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30774 for C. Keep them in sync. */
30775
30776 static tree
30777 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30778 {
30779 location_t location;
30780 tree stmt;
30781
30782 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30783 location = cp_lexer_peek_token (parser->lexer)->location;
30784 objc_maybe_warn_exceptions (location);
30785 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30786 node, lest it get absorbed into the surrounding block. */
30787 stmt = push_stmt_list ();
30788 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30789 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30790
30791 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30792 {
30793 cp_parameter_declarator *parm;
30794 tree parameter_declaration = error_mark_node;
30795 bool seen_open_paren = false;
30796 matching_parens parens;
30797
30798 cp_lexer_consume_token (parser->lexer);
30799 if (parens.require_open (parser))
30800 seen_open_paren = true;
30801 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30802 {
30803 /* We have "@catch (...)" (where the '...' are literally
30804 what is in the code). Skip the '...'.
30805 parameter_declaration is set to NULL_TREE, and
30806 objc_being_catch_clauses() knows that that means
30807 '...'. */
30808 cp_lexer_consume_token (parser->lexer);
30809 parameter_declaration = NULL_TREE;
30810 }
30811 else
30812 {
30813 /* We have "@catch (NSException *exception)" or something
30814 like that. Parse the parameter declaration. */
30815 parm = cp_parser_parameter_declaration (parser, false, NULL);
30816 if (parm == NULL)
30817 parameter_declaration = error_mark_node;
30818 else
30819 parameter_declaration = grokdeclarator (parm->declarator,
30820 &parm->decl_specifiers,
30821 PARM, /*initialized=*/0,
30822 /*attrlist=*/NULL);
30823 }
30824 if (seen_open_paren)
30825 parens.require_close (parser);
30826 else
30827 {
30828 /* If there was no open parenthesis, we are recovering from
30829 an error, and we are trying to figure out what mistake
30830 the user has made. */
30831
30832 /* If there is an immediate closing parenthesis, the user
30833 probably forgot the opening one (ie, they typed "@catch
30834 NSException *e)". Parse the closing parenthesis and keep
30835 going. */
30836 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30837 cp_lexer_consume_token (parser->lexer);
30838
30839 /* If these is no immediate closing parenthesis, the user
30840 probably doesn't know that parenthesis are required at
30841 all (ie, they typed "@catch NSException *e"). So, just
30842 forget about the closing parenthesis and keep going. */
30843 }
30844 objc_begin_catch_clause (parameter_declaration);
30845 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30846 objc_finish_catch_clause ();
30847 }
30848 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30849 {
30850 cp_lexer_consume_token (parser->lexer);
30851 location = cp_lexer_peek_token (parser->lexer)->location;
30852 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30853 node, lest it get absorbed into the surrounding block. */
30854 stmt = push_stmt_list ();
30855 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30856 objc_build_finally_clause (location, pop_stmt_list (stmt));
30857 }
30858
30859 return objc_finish_try_stmt ();
30860 }
30861
30862 /* Parse an Objective-C synchronized statement.
30863
30864 objc-synchronized-stmt:
30865 @synchronized ( expression ) compound-statement
30866
30867 Returns NULL_TREE. */
30868
30869 static tree
30870 cp_parser_objc_synchronized_statement (cp_parser *parser)
30871 {
30872 location_t location;
30873 tree lock, stmt;
30874
30875 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30876
30877 location = cp_lexer_peek_token (parser->lexer)->location;
30878 objc_maybe_warn_exceptions (location);
30879 matching_parens parens;
30880 parens.require_open (parser);
30881 lock = cp_parser_expression (parser);
30882 parens.require_close (parser);
30883
30884 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30885 node, lest it get absorbed into the surrounding block. */
30886 stmt = push_stmt_list ();
30887 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30888
30889 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30890 }
30891
30892 /* Parse an Objective-C throw statement.
30893
30894 objc-throw-stmt:
30895 @throw assignment-expression [opt] ;
30896
30897 Returns a constructed '@throw' statement. */
30898
30899 static tree
30900 cp_parser_objc_throw_statement (cp_parser *parser)
30901 {
30902 tree expr = NULL_TREE;
30903 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30904
30905 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30906
30907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30908 expr = cp_parser_expression (parser);
30909
30910 cp_parser_consume_semicolon_at_end_of_statement (parser);
30911
30912 return objc_build_throw_stmt (loc, expr);
30913 }
30914
30915 /* Parse an Objective-C statement. */
30916
30917 static tree
30918 cp_parser_objc_statement (cp_parser * parser)
30919 {
30920 /* Try to figure out what kind of declaration is present. */
30921 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30922
30923 switch (kwd->keyword)
30924 {
30925 case RID_AT_TRY:
30926 return cp_parser_objc_try_catch_finally_statement (parser);
30927 case RID_AT_SYNCHRONIZED:
30928 return cp_parser_objc_synchronized_statement (parser);
30929 case RID_AT_THROW:
30930 return cp_parser_objc_throw_statement (parser);
30931 default:
30932 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30933 kwd->u.value);
30934 cp_parser_skip_to_end_of_block_or_statement (parser);
30935 }
30936
30937 return error_mark_node;
30938 }
30939
30940 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30941 look ahead to see if an objc keyword follows the attributes. This
30942 is to detect the use of prefix attributes on ObjC @interface and
30943 @protocol. */
30944
30945 static bool
30946 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30947 {
30948 cp_lexer_save_tokens (parser->lexer);
30949 *attrib = cp_parser_attributes_opt (parser);
30950 gcc_assert (*attrib);
30951 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30952 {
30953 cp_lexer_commit_tokens (parser->lexer);
30954 return true;
30955 }
30956 cp_lexer_rollback_tokens (parser->lexer);
30957 return false;
30958 }
30959
30960 /* This routine is a minimal replacement for
30961 c_parser_struct_declaration () used when parsing the list of
30962 types/names or ObjC++ properties. For example, when parsing the
30963 code
30964
30965 @property (readonly) int a, b, c;
30966
30967 this function is responsible for parsing "int a, int b, int c" and
30968 returning the declarations as CHAIN of DECLs.
30969
30970 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30971 similar parsing. */
30972 static tree
30973 cp_parser_objc_struct_declaration (cp_parser *parser)
30974 {
30975 tree decls = NULL_TREE;
30976 cp_decl_specifier_seq declspecs;
30977 int decl_class_or_enum_p;
30978 tree prefix_attributes;
30979
30980 cp_parser_decl_specifier_seq (parser,
30981 CP_PARSER_FLAGS_NONE,
30982 &declspecs,
30983 &decl_class_or_enum_p);
30984
30985 if (declspecs.type == error_mark_node)
30986 return error_mark_node;
30987
30988 /* auto, register, static, extern, mutable. */
30989 if (declspecs.storage_class != sc_none)
30990 {
30991 cp_parser_error (parser, "invalid type for property");
30992 declspecs.storage_class = sc_none;
30993 }
30994
30995 /* thread_local. */
30996 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30997 {
30998 cp_parser_error (parser, "invalid type for property");
30999 declspecs.locations[ds_thread] = 0;
31000 }
31001
31002 /* typedef. */
31003 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31004 {
31005 cp_parser_error (parser, "invalid type for property");
31006 declspecs.locations[ds_typedef] = 0;
31007 }
31008
31009 prefix_attributes = declspecs.attributes;
31010 declspecs.attributes = NULL_TREE;
31011
31012 /* Keep going until we hit the `;' at the end of the declaration. */
31013 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31014 {
31015 tree attributes, first_attribute, decl;
31016 cp_declarator *declarator;
31017 cp_token *token;
31018
31019 /* Parse the declarator. */
31020 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31021 NULL, NULL, false, false);
31022
31023 /* Look for attributes that apply to the ivar. */
31024 attributes = cp_parser_attributes_opt (parser);
31025 /* Remember which attributes are prefix attributes and
31026 which are not. */
31027 first_attribute = attributes;
31028 /* Combine the attributes. */
31029 attributes = attr_chainon (prefix_attributes, attributes);
31030
31031 decl = grokfield (declarator, &declspecs,
31032 NULL_TREE, /*init_const_expr_p=*/false,
31033 NULL_TREE, attributes);
31034
31035 if (decl == error_mark_node || decl == NULL_TREE)
31036 return error_mark_node;
31037
31038 /* Reset PREFIX_ATTRIBUTES. */
31039 if (attributes != error_mark_node)
31040 {
31041 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31042 attributes = TREE_CHAIN (attributes);
31043 if (attributes)
31044 TREE_CHAIN (attributes) = NULL_TREE;
31045 }
31046
31047 DECL_CHAIN (decl) = decls;
31048 decls = decl;
31049
31050 token = cp_lexer_peek_token (parser->lexer);
31051 if (token->type == CPP_COMMA)
31052 {
31053 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31054 continue;
31055 }
31056 else
31057 break;
31058 }
31059 return decls;
31060 }
31061
31062 /* Parse an Objective-C @property declaration. The syntax is:
31063
31064 objc-property-declaration:
31065 '@property' objc-property-attributes[opt] struct-declaration ;
31066
31067 objc-property-attributes:
31068 '(' objc-property-attribute-list ')'
31069
31070 objc-property-attribute-list:
31071 objc-property-attribute
31072 objc-property-attribute-list, objc-property-attribute
31073
31074 objc-property-attribute
31075 'getter' = identifier
31076 'setter' = identifier
31077 'readonly'
31078 'readwrite'
31079 'assign'
31080 'retain'
31081 'copy'
31082 'nonatomic'
31083
31084 For example:
31085 @property NSString *name;
31086 @property (readonly) id object;
31087 @property (retain, nonatomic, getter=getTheName) id name;
31088 @property int a, b, c;
31089
31090 PS: This function is identical to
31091 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31092 static void
31093 cp_parser_objc_at_property_declaration (cp_parser *parser)
31094 {
31095 /* The following variables hold the attributes of the properties as
31096 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31097 seen. When we see an attribute, we set them to 'true' (if they
31098 are boolean properties) or to the identifier (if they have an
31099 argument, ie, for getter and setter). Note that here we only
31100 parse the list of attributes, check the syntax and accumulate the
31101 attributes that we find. objc_add_property_declaration() will
31102 then process the information. */
31103 bool property_assign = false;
31104 bool property_copy = false;
31105 tree property_getter_ident = NULL_TREE;
31106 bool property_nonatomic = false;
31107 bool property_readonly = false;
31108 bool property_readwrite = false;
31109 bool property_retain = false;
31110 tree property_setter_ident = NULL_TREE;
31111
31112 /* 'properties' is the list of properties that we read. Usually a
31113 single one, but maybe more (eg, in "@property int a, b, c;" there
31114 are three). */
31115 tree properties;
31116 location_t loc;
31117
31118 loc = cp_lexer_peek_token (parser->lexer)->location;
31119
31120 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31121
31122 /* Parse the optional attribute list... */
31123 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31124 {
31125 /* Eat the '('. */
31126 matching_parens parens;
31127 parens.consume_open (parser);
31128
31129 while (true)
31130 {
31131 bool syntax_error = false;
31132 cp_token *token = cp_lexer_peek_token (parser->lexer);
31133 enum rid keyword;
31134
31135 if (token->type != CPP_NAME)
31136 {
31137 cp_parser_error (parser, "expected identifier");
31138 break;
31139 }
31140 keyword = C_RID_CODE (token->u.value);
31141 cp_lexer_consume_token (parser->lexer);
31142 switch (keyword)
31143 {
31144 case RID_ASSIGN: property_assign = true; break;
31145 case RID_COPY: property_copy = true; break;
31146 case RID_NONATOMIC: property_nonatomic = true; break;
31147 case RID_READONLY: property_readonly = true; break;
31148 case RID_READWRITE: property_readwrite = true; break;
31149 case RID_RETAIN: property_retain = true; break;
31150
31151 case RID_GETTER:
31152 case RID_SETTER:
31153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31154 {
31155 if (keyword == RID_GETTER)
31156 cp_parser_error (parser,
31157 "missing %<=%> (after %<getter%> attribute)");
31158 else
31159 cp_parser_error (parser,
31160 "missing %<=%> (after %<setter%> attribute)");
31161 syntax_error = true;
31162 break;
31163 }
31164 cp_lexer_consume_token (parser->lexer); /* eat the = */
31165 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31166 {
31167 cp_parser_error (parser, "expected identifier");
31168 syntax_error = true;
31169 break;
31170 }
31171 if (keyword == RID_SETTER)
31172 {
31173 if (property_setter_ident != NULL_TREE)
31174 {
31175 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31176 cp_lexer_consume_token (parser->lexer);
31177 }
31178 else
31179 property_setter_ident = cp_parser_objc_selector (parser);
31180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31181 cp_parser_error (parser, "setter name must terminate with %<:%>");
31182 else
31183 cp_lexer_consume_token (parser->lexer);
31184 }
31185 else
31186 {
31187 if (property_getter_ident != NULL_TREE)
31188 {
31189 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31190 cp_lexer_consume_token (parser->lexer);
31191 }
31192 else
31193 property_getter_ident = cp_parser_objc_selector (parser);
31194 }
31195 break;
31196 default:
31197 cp_parser_error (parser, "unknown property attribute");
31198 syntax_error = true;
31199 break;
31200 }
31201
31202 if (syntax_error)
31203 break;
31204
31205 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31206 cp_lexer_consume_token (parser->lexer);
31207 else
31208 break;
31209 }
31210
31211 /* FIXME: "@property (setter, assign);" will generate a spurious
31212 "error: expected ‘)’ before ‘,’ token". This is because
31213 cp_parser_require, unlike the C counterpart, will produce an
31214 error even if we are in error recovery. */
31215 if (!parens.require_close (parser))
31216 {
31217 cp_parser_skip_to_closing_parenthesis (parser,
31218 /*recovering=*/true,
31219 /*or_comma=*/false,
31220 /*consume_paren=*/true);
31221 }
31222 }
31223
31224 /* ... and the property declaration(s). */
31225 properties = cp_parser_objc_struct_declaration (parser);
31226
31227 if (properties == error_mark_node)
31228 {
31229 cp_parser_skip_to_end_of_statement (parser);
31230 /* If the next token is now a `;', consume it. */
31231 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31232 cp_lexer_consume_token (parser->lexer);
31233 return;
31234 }
31235
31236 if (properties == NULL_TREE)
31237 cp_parser_error (parser, "expected identifier");
31238 else
31239 {
31240 /* Comma-separated properties are chained together in
31241 reverse order; add them one by one. */
31242 properties = nreverse (properties);
31243
31244 for (; properties; properties = TREE_CHAIN (properties))
31245 objc_add_property_declaration (loc, copy_node (properties),
31246 property_readonly, property_readwrite,
31247 property_assign, property_retain,
31248 property_copy, property_nonatomic,
31249 property_getter_ident, property_setter_ident);
31250 }
31251
31252 cp_parser_consume_semicolon_at_end_of_statement (parser);
31253 }
31254
31255 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31256
31257 objc-synthesize-declaration:
31258 @synthesize objc-synthesize-identifier-list ;
31259
31260 objc-synthesize-identifier-list:
31261 objc-synthesize-identifier
31262 objc-synthesize-identifier-list, objc-synthesize-identifier
31263
31264 objc-synthesize-identifier
31265 identifier
31266 identifier = identifier
31267
31268 For example:
31269 @synthesize MyProperty;
31270 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31271
31272 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31273 for C. Keep them in sync.
31274 */
31275 static void
31276 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31277 {
31278 tree list = NULL_TREE;
31279 location_t loc;
31280 loc = cp_lexer_peek_token (parser->lexer)->location;
31281
31282 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31283 while (true)
31284 {
31285 tree property, ivar;
31286 property = cp_parser_identifier (parser);
31287 if (property == error_mark_node)
31288 {
31289 cp_parser_consume_semicolon_at_end_of_statement (parser);
31290 return;
31291 }
31292 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31293 {
31294 cp_lexer_consume_token (parser->lexer);
31295 ivar = cp_parser_identifier (parser);
31296 if (ivar == error_mark_node)
31297 {
31298 cp_parser_consume_semicolon_at_end_of_statement (parser);
31299 return;
31300 }
31301 }
31302 else
31303 ivar = NULL_TREE;
31304 list = chainon (list, build_tree_list (ivar, property));
31305 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31306 cp_lexer_consume_token (parser->lexer);
31307 else
31308 break;
31309 }
31310 cp_parser_consume_semicolon_at_end_of_statement (parser);
31311 objc_add_synthesize_declaration (loc, list);
31312 }
31313
31314 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31315
31316 objc-dynamic-declaration:
31317 @dynamic identifier-list ;
31318
31319 For example:
31320 @dynamic MyProperty;
31321 @dynamic MyProperty, AnotherProperty;
31322
31323 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31324 for C. Keep them in sync.
31325 */
31326 static void
31327 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31328 {
31329 tree list = NULL_TREE;
31330 location_t loc;
31331 loc = cp_lexer_peek_token (parser->lexer)->location;
31332
31333 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31334 while (true)
31335 {
31336 tree property;
31337 property = cp_parser_identifier (parser);
31338 if (property == error_mark_node)
31339 {
31340 cp_parser_consume_semicolon_at_end_of_statement (parser);
31341 return;
31342 }
31343 list = chainon (list, build_tree_list (NULL, property));
31344 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31345 cp_lexer_consume_token (parser->lexer);
31346 else
31347 break;
31348 }
31349 cp_parser_consume_semicolon_at_end_of_statement (parser);
31350 objc_add_dynamic_declaration (loc, list);
31351 }
31352
31353 \f
31354 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31355
31356 /* Returns name of the next clause.
31357 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31358 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31359 returned and the token is consumed. */
31360
31361 static pragma_omp_clause
31362 cp_parser_omp_clause_name (cp_parser *parser)
31363 {
31364 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31365
31366 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31367 result = PRAGMA_OACC_CLAUSE_AUTO;
31368 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31369 result = PRAGMA_OMP_CLAUSE_IF;
31370 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31371 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31372 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31373 result = PRAGMA_OACC_CLAUSE_DELETE;
31374 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31375 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31376 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31377 result = PRAGMA_OMP_CLAUSE_FOR;
31378 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31379 {
31380 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31381 const char *p = IDENTIFIER_POINTER (id);
31382
31383 switch (p[0])
31384 {
31385 case 'a':
31386 if (!strcmp ("aligned", p))
31387 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31388 else if (!strcmp ("async", p))
31389 result = PRAGMA_OACC_CLAUSE_ASYNC;
31390 break;
31391 case 'c':
31392 if (!strcmp ("collapse", p))
31393 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31394 else if (!strcmp ("copy", p))
31395 result = PRAGMA_OACC_CLAUSE_COPY;
31396 else if (!strcmp ("copyin", p))
31397 result = PRAGMA_OMP_CLAUSE_COPYIN;
31398 else if (!strcmp ("copyout", p))
31399 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31400 else if (!strcmp ("copyprivate", p))
31401 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31402 else if (!strcmp ("create", p))
31403 result = PRAGMA_OACC_CLAUSE_CREATE;
31404 break;
31405 case 'd':
31406 if (!strcmp ("defaultmap", p))
31407 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31408 else if (!strcmp ("depend", p))
31409 result = PRAGMA_OMP_CLAUSE_DEPEND;
31410 else if (!strcmp ("device", p))
31411 result = PRAGMA_OMP_CLAUSE_DEVICE;
31412 else if (!strcmp ("deviceptr", p))
31413 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31414 else if (!strcmp ("device_resident", p))
31415 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31416 else if (!strcmp ("dist_schedule", p))
31417 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31418 break;
31419 case 'f':
31420 if (!strcmp ("final", p))
31421 result = PRAGMA_OMP_CLAUSE_FINAL;
31422 else if (!strcmp ("finalize", p))
31423 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31424 else if (!strcmp ("firstprivate", p))
31425 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31426 else if (!strcmp ("from", p))
31427 result = PRAGMA_OMP_CLAUSE_FROM;
31428 break;
31429 case 'g':
31430 if (!strcmp ("gang", p))
31431 result = PRAGMA_OACC_CLAUSE_GANG;
31432 else if (!strcmp ("grainsize", p))
31433 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31434 break;
31435 case 'h':
31436 if (!strcmp ("hint", p))
31437 result = PRAGMA_OMP_CLAUSE_HINT;
31438 else if (!strcmp ("host", p))
31439 result = PRAGMA_OACC_CLAUSE_HOST;
31440 break;
31441 case 'i':
31442 if (!strcmp ("if_present", p))
31443 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31444 else if (!strcmp ("inbranch", p))
31445 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31446 else if (!strcmp ("independent", p))
31447 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31448 else if (!strcmp ("is_device_ptr", p))
31449 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31450 break;
31451 case 'l':
31452 if (!strcmp ("lastprivate", p))
31453 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31454 else if (!strcmp ("linear", p))
31455 result = PRAGMA_OMP_CLAUSE_LINEAR;
31456 else if (!strcmp ("link", p))
31457 result = PRAGMA_OMP_CLAUSE_LINK;
31458 break;
31459 case 'm':
31460 if (!strcmp ("map", p))
31461 result = PRAGMA_OMP_CLAUSE_MAP;
31462 else if (!strcmp ("mergeable", p))
31463 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31464 break;
31465 case 'n':
31466 if (!strcmp ("nogroup", p))
31467 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31468 else if (!strcmp ("notinbranch", p))
31469 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31470 else if (!strcmp ("nowait", p))
31471 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31472 else if (!strcmp ("num_gangs", p))
31473 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31474 else if (!strcmp ("num_tasks", p))
31475 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31476 else if (!strcmp ("num_teams", p))
31477 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31478 else if (!strcmp ("num_threads", p))
31479 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31480 else if (!strcmp ("num_workers", p))
31481 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31482 break;
31483 case 'o':
31484 if (!strcmp ("ordered", p))
31485 result = PRAGMA_OMP_CLAUSE_ORDERED;
31486 break;
31487 case 'p':
31488 if (!strcmp ("parallel", p))
31489 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31490 else if (!strcmp ("present", p))
31491 result = PRAGMA_OACC_CLAUSE_PRESENT;
31492 else if (!strcmp ("present_or_copy", p)
31493 || !strcmp ("pcopy", p))
31494 result = PRAGMA_OACC_CLAUSE_COPY;
31495 else if (!strcmp ("present_or_copyin", p)
31496 || !strcmp ("pcopyin", p))
31497 result = PRAGMA_OACC_CLAUSE_COPYIN;
31498 else if (!strcmp ("present_or_copyout", p)
31499 || !strcmp ("pcopyout", p))
31500 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31501 else if (!strcmp ("present_or_create", p)
31502 || !strcmp ("pcreate", p))
31503 result = PRAGMA_OACC_CLAUSE_CREATE;
31504 else if (!strcmp ("priority", p))
31505 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31506 else if (!strcmp ("proc_bind", p))
31507 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31508 break;
31509 case 'r':
31510 if (!strcmp ("reduction", p))
31511 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31512 break;
31513 case 's':
31514 if (!strcmp ("safelen", p))
31515 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31516 else if (!strcmp ("schedule", p))
31517 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31518 else if (!strcmp ("sections", p))
31519 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31520 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31521 result = PRAGMA_OACC_CLAUSE_HOST;
31522 else if (!strcmp ("seq", p))
31523 result = PRAGMA_OACC_CLAUSE_SEQ;
31524 else if (!strcmp ("shared", p))
31525 result = PRAGMA_OMP_CLAUSE_SHARED;
31526 else if (!strcmp ("simd", p))
31527 result = PRAGMA_OMP_CLAUSE_SIMD;
31528 else if (!strcmp ("simdlen", p))
31529 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31530 break;
31531 case 't':
31532 if (!strcmp ("taskgroup", p))
31533 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31534 else if (!strcmp ("thread_limit", p))
31535 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31536 else if (!strcmp ("threads", p))
31537 result = PRAGMA_OMP_CLAUSE_THREADS;
31538 else if (!strcmp ("tile", p))
31539 result = PRAGMA_OACC_CLAUSE_TILE;
31540 else if (!strcmp ("to", p))
31541 result = PRAGMA_OMP_CLAUSE_TO;
31542 break;
31543 case 'u':
31544 if (!strcmp ("uniform", p))
31545 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31546 else if (!strcmp ("untied", p))
31547 result = PRAGMA_OMP_CLAUSE_UNTIED;
31548 else if (!strcmp ("use_device", p))
31549 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31550 else if (!strcmp ("use_device_ptr", p))
31551 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31552 break;
31553 case 'v':
31554 if (!strcmp ("vector", p))
31555 result = PRAGMA_OACC_CLAUSE_VECTOR;
31556 else if (!strcmp ("vector_length", p))
31557 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31558 break;
31559 case 'w':
31560 if (!strcmp ("wait", p))
31561 result = PRAGMA_OACC_CLAUSE_WAIT;
31562 else if (!strcmp ("worker", p))
31563 result = PRAGMA_OACC_CLAUSE_WORKER;
31564 break;
31565 }
31566 }
31567
31568 if (result != PRAGMA_OMP_CLAUSE_NONE)
31569 cp_lexer_consume_token (parser->lexer);
31570
31571 return result;
31572 }
31573
31574 /* Validate that a clause of the given type does not already exist. */
31575
31576 static void
31577 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31578 const char *name, location_t location)
31579 {
31580 tree c;
31581
31582 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31583 if (OMP_CLAUSE_CODE (c) == code)
31584 {
31585 error_at (location, "too many %qs clauses", name);
31586 break;
31587 }
31588 }
31589
31590 /* OpenMP 2.5:
31591 variable-list:
31592 identifier
31593 variable-list , identifier
31594
31595 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31596 colon). An opening parenthesis will have been consumed by the caller.
31597
31598 If KIND is nonzero, create the appropriate node and install the decl
31599 in OMP_CLAUSE_DECL and add the node to the head of the list.
31600
31601 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31602 return the list created.
31603
31604 COLON can be NULL if only closing parenthesis should end the list,
31605 or pointer to bool which will receive false if the list is terminated
31606 by closing parenthesis or true if the list is terminated by colon. */
31607
31608 static tree
31609 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31610 tree list, bool *colon)
31611 {
31612 cp_token *token;
31613 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31614 if (colon)
31615 {
31616 parser->colon_corrects_to_scope_p = false;
31617 *colon = false;
31618 }
31619 while (1)
31620 {
31621 tree name, decl;
31622
31623 token = cp_lexer_peek_token (parser->lexer);
31624 if (kind != 0
31625 && current_class_ptr
31626 && cp_parser_is_keyword (token, RID_THIS))
31627 {
31628 decl = finish_this_expr ();
31629 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31630 || CONVERT_EXPR_P (decl))
31631 decl = TREE_OPERAND (decl, 0);
31632 cp_lexer_consume_token (parser->lexer);
31633 }
31634 else
31635 {
31636 name = cp_parser_id_expression (parser, /*template_p=*/false,
31637 /*check_dependency_p=*/true,
31638 /*template_p=*/NULL,
31639 /*declarator_p=*/false,
31640 /*optional_p=*/false);
31641 if (name == error_mark_node)
31642 goto skip_comma;
31643
31644 if (identifier_p (name))
31645 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31646 else
31647 decl = name;
31648 if (decl == error_mark_node)
31649 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31650 token->location);
31651 }
31652 if (decl == error_mark_node)
31653 ;
31654 else if (kind != 0)
31655 {
31656 switch (kind)
31657 {
31658 case OMP_CLAUSE__CACHE_:
31659 /* The OpenACC cache directive explicitly only allows "array
31660 elements or subarrays". */
31661 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31662 {
31663 error_at (token->location, "expected %<[%>");
31664 decl = error_mark_node;
31665 break;
31666 }
31667 /* FALLTHROUGH. */
31668 case OMP_CLAUSE_MAP:
31669 case OMP_CLAUSE_FROM:
31670 case OMP_CLAUSE_TO:
31671 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31672 {
31673 location_t loc
31674 = cp_lexer_peek_token (parser->lexer)->location;
31675 cp_id_kind idk = CP_ID_KIND_NONE;
31676 cp_lexer_consume_token (parser->lexer);
31677 decl = convert_from_reference (decl);
31678 decl
31679 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31680 decl, false,
31681 &idk, loc);
31682 }
31683 /* FALLTHROUGH. */
31684 case OMP_CLAUSE_DEPEND:
31685 case OMP_CLAUSE_REDUCTION:
31686 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31687 {
31688 tree low_bound = NULL_TREE, length = NULL_TREE;
31689
31690 parser->colon_corrects_to_scope_p = false;
31691 cp_lexer_consume_token (parser->lexer);
31692 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31693 low_bound = cp_parser_expression (parser);
31694 if (!colon)
31695 parser->colon_corrects_to_scope_p
31696 = saved_colon_corrects_to_scope_p;
31697 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31698 length = integer_one_node;
31699 else
31700 {
31701 /* Look for `:'. */
31702 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31703 goto skip_comma;
31704 if (!cp_lexer_next_token_is (parser->lexer,
31705 CPP_CLOSE_SQUARE))
31706 length = cp_parser_expression (parser);
31707 }
31708 /* Look for the closing `]'. */
31709 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31710 RT_CLOSE_SQUARE))
31711 goto skip_comma;
31712
31713 decl = tree_cons (low_bound, length, decl);
31714 }
31715 break;
31716 default:
31717 break;
31718 }
31719
31720 tree u = build_omp_clause (token->location, kind);
31721 OMP_CLAUSE_DECL (u) = decl;
31722 OMP_CLAUSE_CHAIN (u) = list;
31723 list = u;
31724 }
31725 else
31726 list = tree_cons (decl, NULL_TREE, list);
31727
31728 get_comma:
31729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31730 break;
31731 cp_lexer_consume_token (parser->lexer);
31732 }
31733
31734 if (colon)
31735 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31736
31737 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31738 {
31739 *colon = true;
31740 cp_parser_require (parser, CPP_COLON, RT_COLON);
31741 return list;
31742 }
31743
31744 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31745 {
31746 int ending;
31747
31748 /* Try to resync to an unnested comma. Copied from
31749 cp_parser_parenthesized_expression_list. */
31750 skip_comma:
31751 if (colon)
31752 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31753 ending = cp_parser_skip_to_closing_parenthesis (parser,
31754 /*recovering=*/true,
31755 /*or_comma=*/true,
31756 /*consume_paren=*/true);
31757 if (ending < 0)
31758 goto get_comma;
31759 }
31760
31761 return list;
31762 }
31763
31764 /* Similarly, but expect leading and trailing parenthesis. This is a very
31765 common case for omp clauses. */
31766
31767 static tree
31768 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31769 {
31770 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31771 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31772 return list;
31773 }
31774
31775 /* OpenACC 2.0:
31776 copy ( variable-list )
31777 copyin ( variable-list )
31778 copyout ( variable-list )
31779 create ( variable-list )
31780 delete ( variable-list )
31781 present ( variable-list ) */
31782
31783 static tree
31784 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31785 tree list)
31786 {
31787 enum gomp_map_kind kind;
31788 switch (c_kind)
31789 {
31790 case PRAGMA_OACC_CLAUSE_COPY:
31791 kind = GOMP_MAP_TOFROM;
31792 break;
31793 case PRAGMA_OACC_CLAUSE_COPYIN:
31794 kind = GOMP_MAP_TO;
31795 break;
31796 case PRAGMA_OACC_CLAUSE_COPYOUT:
31797 kind = GOMP_MAP_FROM;
31798 break;
31799 case PRAGMA_OACC_CLAUSE_CREATE:
31800 kind = GOMP_MAP_ALLOC;
31801 break;
31802 case PRAGMA_OACC_CLAUSE_DELETE:
31803 kind = GOMP_MAP_RELEASE;
31804 break;
31805 case PRAGMA_OACC_CLAUSE_DEVICE:
31806 kind = GOMP_MAP_FORCE_TO;
31807 break;
31808 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31809 kind = GOMP_MAP_DEVICE_RESIDENT;
31810 break;
31811 case PRAGMA_OACC_CLAUSE_HOST:
31812 kind = GOMP_MAP_FORCE_FROM;
31813 break;
31814 case PRAGMA_OACC_CLAUSE_LINK:
31815 kind = GOMP_MAP_LINK;
31816 break;
31817 case PRAGMA_OACC_CLAUSE_PRESENT:
31818 kind = GOMP_MAP_FORCE_PRESENT;
31819 break;
31820 default:
31821 gcc_unreachable ();
31822 }
31823 tree nl, c;
31824 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31825
31826 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31827 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31828
31829 return nl;
31830 }
31831
31832 /* OpenACC 2.0:
31833 deviceptr ( variable-list ) */
31834
31835 static tree
31836 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31837 {
31838 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31839 tree vars, t;
31840
31841 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31842 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31843 variable-list must only allow for pointer variables. */
31844 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31845 for (t = vars; t; t = TREE_CHAIN (t))
31846 {
31847 tree v = TREE_PURPOSE (t);
31848 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31849 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31850 OMP_CLAUSE_DECL (u) = v;
31851 OMP_CLAUSE_CHAIN (u) = list;
31852 list = u;
31853 }
31854
31855 return list;
31856 }
31857
31858 /* OpenACC 2.5:
31859 auto
31860 finalize
31861 independent
31862 nohost
31863 seq */
31864
31865 static tree
31866 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31867 enum omp_clause_code code,
31868 tree list, location_t location)
31869 {
31870 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31871 tree c = build_omp_clause (location, code);
31872 OMP_CLAUSE_CHAIN (c) = list;
31873 return c;
31874 }
31875
31876 /* OpenACC:
31877 num_gangs ( expression )
31878 num_workers ( expression )
31879 vector_length ( expression ) */
31880
31881 static tree
31882 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31883 const char *str, tree list)
31884 {
31885 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31886
31887 matching_parens parens;
31888 if (!parens.require_open (parser))
31889 return list;
31890
31891 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31892
31893 if (t == error_mark_node
31894 || !parens.require_close (parser))
31895 {
31896 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31897 /*or_comma=*/false,
31898 /*consume_paren=*/true);
31899 return list;
31900 }
31901
31902 check_no_duplicate_clause (list, code, str, loc);
31903
31904 tree c = build_omp_clause (loc, code);
31905 OMP_CLAUSE_OPERAND (c, 0) = t;
31906 OMP_CLAUSE_CHAIN (c) = list;
31907 return c;
31908 }
31909
31910 /* OpenACC:
31911
31912 gang [( gang-arg-list )]
31913 worker [( [num:] int-expr )]
31914 vector [( [length:] int-expr )]
31915
31916 where gang-arg is one of:
31917
31918 [num:] int-expr
31919 static: size-expr
31920
31921 and size-expr may be:
31922
31923 *
31924 int-expr
31925 */
31926
31927 static tree
31928 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31929 const char *str, tree list)
31930 {
31931 const char *id = "num";
31932 cp_lexer *lexer = parser->lexer;
31933 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31934 location_t loc = cp_lexer_peek_token (lexer)->location;
31935
31936 if (kind == OMP_CLAUSE_VECTOR)
31937 id = "length";
31938
31939 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31940 {
31941 matching_parens parens;
31942 parens.consume_open (parser);
31943
31944 do
31945 {
31946 cp_token *next = cp_lexer_peek_token (lexer);
31947 int idx = 0;
31948
31949 /* Gang static argument. */
31950 if (kind == OMP_CLAUSE_GANG
31951 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31952 {
31953 cp_lexer_consume_token (lexer);
31954
31955 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31956 goto cleanup_error;
31957
31958 idx = 1;
31959 if (ops[idx] != NULL)
31960 {
31961 cp_parser_error (parser, "too many %<static%> arguments");
31962 goto cleanup_error;
31963 }
31964
31965 /* Check for the '*' argument. */
31966 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31967 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31968 || cp_lexer_nth_token_is (parser->lexer, 2,
31969 CPP_CLOSE_PAREN)))
31970 {
31971 cp_lexer_consume_token (lexer);
31972 ops[idx] = integer_minus_one_node;
31973
31974 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31975 {
31976 cp_lexer_consume_token (lexer);
31977 continue;
31978 }
31979 else break;
31980 }
31981 }
31982 /* Worker num: argument and vector length: arguments. */
31983 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31984 && id_equal (next->u.value, id)
31985 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31986 {
31987 cp_lexer_consume_token (lexer); /* id */
31988 cp_lexer_consume_token (lexer); /* ':' */
31989 }
31990
31991 /* Now collect the actual argument. */
31992 if (ops[idx] != NULL_TREE)
31993 {
31994 cp_parser_error (parser, "unexpected argument");
31995 goto cleanup_error;
31996 }
31997
31998 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31999 false);
32000 if (expr == error_mark_node)
32001 goto cleanup_error;
32002
32003 mark_exp_read (expr);
32004 ops[idx] = expr;
32005
32006 if (kind == OMP_CLAUSE_GANG
32007 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32008 {
32009 cp_lexer_consume_token (lexer);
32010 continue;
32011 }
32012 break;
32013 }
32014 while (1);
32015
32016 if (!parens.require_close (parser))
32017 goto cleanup_error;
32018 }
32019
32020 check_no_duplicate_clause (list, kind, str, loc);
32021
32022 c = build_omp_clause (loc, kind);
32023
32024 if (ops[1])
32025 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32026
32027 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32028 OMP_CLAUSE_CHAIN (c) = list;
32029
32030 return c;
32031
32032 cleanup_error:
32033 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32034 return list;
32035 }
32036
32037 /* OpenACC 2.0:
32038 tile ( size-expr-list ) */
32039
32040 static tree
32041 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32042 {
32043 tree c, expr = error_mark_node;
32044 tree tile = NULL_TREE;
32045
32046 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32047 so, but the spec authors never considered such a case and have
32048 differing opinions on what it might mean, including 'not
32049 allowed'.) */
32050 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32051 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32052 clause_loc);
32053
32054 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32055 return list;
32056
32057 do
32058 {
32059 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32060 return list;
32061
32062 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32063 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32064 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32065 {
32066 cp_lexer_consume_token (parser->lexer);
32067 expr = integer_zero_node;
32068 }
32069 else
32070 expr = cp_parser_constant_expression (parser);
32071
32072 tile = tree_cons (NULL_TREE, expr, tile);
32073 }
32074 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32075
32076 /* Consume the trailing ')'. */
32077 cp_lexer_consume_token (parser->lexer);
32078
32079 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32080 tile = nreverse (tile);
32081 OMP_CLAUSE_TILE_LIST (c) = tile;
32082 OMP_CLAUSE_CHAIN (c) = list;
32083 return c;
32084 }
32085
32086 /* OpenACC 2.0
32087 Parse wait clause or directive parameters. */
32088
32089 static tree
32090 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32091 {
32092 vec<tree, va_gc> *args;
32093 tree t, args_tree;
32094
32095 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32096 /*cast_p=*/false,
32097 /*allow_expansion_p=*/true,
32098 /*non_constant_p=*/NULL);
32099
32100 if (args == NULL || args->length () == 0)
32101 {
32102 cp_parser_error (parser, "expected integer expression before ')'");
32103 if (args != NULL)
32104 release_tree_vector (args);
32105 return list;
32106 }
32107
32108 args_tree = build_tree_list_vec (args);
32109
32110 release_tree_vector (args);
32111
32112 for (t = args_tree; t; t = TREE_CHAIN (t))
32113 {
32114 tree targ = TREE_VALUE (t);
32115
32116 if (targ != error_mark_node)
32117 {
32118 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32119 error ("%<wait%> expression must be integral");
32120 else
32121 {
32122 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32123
32124 targ = mark_rvalue_use (targ);
32125 OMP_CLAUSE_DECL (c) = targ;
32126 OMP_CLAUSE_CHAIN (c) = list;
32127 list = c;
32128 }
32129 }
32130 }
32131
32132 return list;
32133 }
32134
32135 /* OpenACC:
32136 wait ( int-expr-list ) */
32137
32138 static tree
32139 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32140 {
32141 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32142
32143 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32144 return list;
32145
32146 list = cp_parser_oacc_wait_list (parser, location, list);
32147
32148 return list;
32149 }
32150
32151 /* OpenMP 3.0:
32152 collapse ( constant-expression ) */
32153
32154 static tree
32155 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32156 {
32157 tree c, num;
32158 location_t loc;
32159 HOST_WIDE_INT n;
32160
32161 loc = cp_lexer_peek_token (parser->lexer)->location;
32162 matching_parens parens;
32163 if (!parens.require_open (parser))
32164 return list;
32165
32166 num = cp_parser_constant_expression (parser);
32167
32168 if (!parens.require_close (parser))
32169 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32170 /*or_comma=*/false,
32171 /*consume_paren=*/true);
32172
32173 if (num == error_mark_node)
32174 return list;
32175 num = fold_non_dependent_expr (num);
32176 if (!tree_fits_shwi_p (num)
32177 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32178 || (n = tree_to_shwi (num)) <= 0
32179 || (int) n != n)
32180 {
32181 error_at (loc, "collapse argument needs positive constant integer expression");
32182 return list;
32183 }
32184
32185 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32186 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32187 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32188 OMP_CLAUSE_CHAIN (c) = list;
32189 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32190
32191 return c;
32192 }
32193
32194 /* OpenMP 2.5:
32195 default ( none | shared )
32196
32197 OpenACC:
32198 default ( none | present ) */
32199
32200 static tree
32201 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32202 location_t location, bool is_oacc)
32203 {
32204 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32205 tree c;
32206
32207 matching_parens parens;
32208 if (!parens.require_open (parser))
32209 return list;
32210 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32211 {
32212 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32213 const char *p = IDENTIFIER_POINTER (id);
32214
32215 switch (p[0])
32216 {
32217 case 'n':
32218 if (strcmp ("none", p) != 0)
32219 goto invalid_kind;
32220 kind = OMP_CLAUSE_DEFAULT_NONE;
32221 break;
32222
32223 case 'p':
32224 if (strcmp ("present", p) != 0 || !is_oacc)
32225 goto invalid_kind;
32226 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32227 break;
32228
32229 case 's':
32230 if (strcmp ("shared", p) != 0 || is_oacc)
32231 goto invalid_kind;
32232 kind = OMP_CLAUSE_DEFAULT_SHARED;
32233 break;
32234
32235 default:
32236 goto invalid_kind;
32237 }
32238
32239 cp_lexer_consume_token (parser->lexer);
32240 }
32241 else
32242 {
32243 invalid_kind:
32244 if (is_oacc)
32245 cp_parser_error (parser, "expected %<none%> or %<present%>");
32246 else
32247 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32248 }
32249
32250 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32251 || !parens.require_close (parser))
32252 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32253 /*or_comma=*/false,
32254 /*consume_paren=*/true);
32255
32256 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32257 return list;
32258
32259 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32260 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32261 OMP_CLAUSE_CHAIN (c) = list;
32262 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32263
32264 return c;
32265 }
32266
32267 /* OpenMP 3.1:
32268 final ( expression ) */
32269
32270 static tree
32271 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32272 {
32273 tree t, c;
32274
32275 matching_parens parens;
32276 if (!parens.require_open (parser))
32277 return list;
32278
32279 t = cp_parser_condition (parser);
32280
32281 if (t == error_mark_node
32282 || !parens.require_close (parser))
32283 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32284 /*or_comma=*/false,
32285 /*consume_paren=*/true);
32286
32287 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32288
32289 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32290 OMP_CLAUSE_FINAL_EXPR (c) = t;
32291 OMP_CLAUSE_CHAIN (c) = list;
32292
32293 return c;
32294 }
32295
32296 /* OpenMP 2.5:
32297 if ( expression )
32298
32299 OpenMP 4.5:
32300 if ( directive-name-modifier : expression )
32301
32302 directive-name-modifier:
32303 parallel | task | taskloop | target data | target | target update
32304 | target enter data | target exit data */
32305
32306 static tree
32307 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32308 bool is_omp)
32309 {
32310 tree t, c;
32311 enum tree_code if_modifier = ERROR_MARK;
32312
32313 matching_parens parens;
32314 if (!parens.require_open (parser))
32315 return list;
32316
32317 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32318 {
32319 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32320 const char *p = IDENTIFIER_POINTER (id);
32321 int n = 2;
32322
32323 if (strcmp ("parallel", p) == 0)
32324 if_modifier = OMP_PARALLEL;
32325 else if (strcmp ("task", p) == 0)
32326 if_modifier = OMP_TASK;
32327 else if (strcmp ("taskloop", p) == 0)
32328 if_modifier = OMP_TASKLOOP;
32329 else if (strcmp ("target", p) == 0)
32330 {
32331 if_modifier = OMP_TARGET;
32332 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32333 {
32334 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32335 p = IDENTIFIER_POINTER (id);
32336 if (strcmp ("data", p) == 0)
32337 if_modifier = OMP_TARGET_DATA;
32338 else if (strcmp ("update", p) == 0)
32339 if_modifier = OMP_TARGET_UPDATE;
32340 else if (strcmp ("enter", p) == 0)
32341 if_modifier = OMP_TARGET_ENTER_DATA;
32342 else if (strcmp ("exit", p) == 0)
32343 if_modifier = OMP_TARGET_EXIT_DATA;
32344 if (if_modifier != OMP_TARGET)
32345 n = 3;
32346 else
32347 {
32348 location_t loc
32349 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32350 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32351 "or %<exit%>");
32352 if_modifier = ERROR_MARK;
32353 }
32354 if (if_modifier == OMP_TARGET_ENTER_DATA
32355 || if_modifier == OMP_TARGET_EXIT_DATA)
32356 {
32357 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32358 {
32359 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32360 p = IDENTIFIER_POINTER (id);
32361 if (strcmp ("data", p) == 0)
32362 n = 4;
32363 }
32364 if (n != 4)
32365 {
32366 location_t loc
32367 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32368 error_at (loc, "expected %<data%>");
32369 if_modifier = ERROR_MARK;
32370 }
32371 }
32372 }
32373 }
32374 if (if_modifier != ERROR_MARK)
32375 {
32376 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32377 {
32378 while (n-- > 0)
32379 cp_lexer_consume_token (parser->lexer);
32380 }
32381 else
32382 {
32383 if (n > 2)
32384 {
32385 location_t loc
32386 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32387 error_at (loc, "expected %<:%>");
32388 }
32389 if_modifier = ERROR_MARK;
32390 }
32391 }
32392 }
32393
32394 t = cp_parser_condition (parser);
32395
32396 if (t == error_mark_node
32397 || !parens.require_close (parser))
32398 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32399 /*or_comma=*/false,
32400 /*consume_paren=*/true);
32401
32402 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32403 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32404 {
32405 if (if_modifier != ERROR_MARK
32406 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32407 {
32408 const char *p = NULL;
32409 switch (if_modifier)
32410 {
32411 case OMP_PARALLEL: p = "parallel"; break;
32412 case OMP_TASK: p = "task"; break;
32413 case OMP_TASKLOOP: p = "taskloop"; break;
32414 case OMP_TARGET_DATA: p = "target data"; break;
32415 case OMP_TARGET: p = "target"; break;
32416 case OMP_TARGET_UPDATE: p = "target update"; break;
32417 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32418 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32419 default: gcc_unreachable ();
32420 }
32421 error_at (location, "too many %<if%> clauses with %qs modifier",
32422 p);
32423 return list;
32424 }
32425 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32426 {
32427 if (!is_omp)
32428 error_at (location, "too many %<if%> clauses");
32429 else
32430 error_at (location, "too many %<if%> clauses without modifier");
32431 return list;
32432 }
32433 else if (if_modifier == ERROR_MARK
32434 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32435 {
32436 error_at (location, "if any %<if%> clause has modifier, then all "
32437 "%<if%> clauses have to use modifier");
32438 return list;
32439 }
32440 }
32441
32442 c = build_omp_clause (location, OMP_CLAUSE_IF);
32443 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32444 OMP_CLAUSE_IF_EXPR (c) = t;
32445 OMP_CLAUSE_CHAIN (c) = list;
32446
32447 return c;
32448 }
32449
32450 /* OpenMP 3.1:
32451 mergeable */
32452
32453 static tree
32454 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32455 tree list, location_t location)
32456 {
32457 tree c;
32458
32459 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32460 location);
32461
32462 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32463 OMP_CLAUSE_CHAIN (c) = list;
32464 return c;
32465 }
32466
32467 /* OpenMP 2.5:
32468 nowait */
32469
32470 static tree
32471 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32472 tree list, location_t location)
32473 {
32474 tree c;
32475
32476 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32477
32478 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32479 OMP_CLAUSE_CHAIN (c) = list;
32480 return c;
32481 }
32482
32483 /* OpenMP 2.5:
32484 num_threads ( expression ) */
32485
32486 static tree
32487 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32488 location_t location)
32489 {
32490 tree t, c;
32491
32492 matching_parens parens;
32493 if (!parens.require_open (parser))
32494 return list;
32495
32496 t = cp_parser_expression (parser);
32497
32498 if (t == error_mark_node
32499 || !parens.require_close (parser))
32500 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32501 /*or_comma=*/false,
32502 /*consume_paren=*/true);
32503
32504 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32505 "num_threads", location);
32506
32507 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32508 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32509 OMP_CLAUSE_CHAIN (c) = list;
32510
32511 return c;
32512 }
32513
32514 /* OpenMP 4.5:
32515 num_tasks ( expression ) */
32516
32517 static tree
32518 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32519 location_t location)
32520 {
32521 tree t, c;
32522
32523 matching_parens parens;
32524 if (!parens.require_open (parser))
32525 return list;
32526
32527 t = cp_parser_expression (parser);
32528
32529 if (t == error_mark_node
32530 || !parens.require_close (parser))
32531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32532 /*or_comma=*/false,
32533 /*consume_paren=*/true);
32534
32535 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32536 "num_tasks", location);
32537
32538 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32539 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32540 OMP_CLAUSE_CHAIN (c) = list;
32541
32542 return c;
32543 }
32544
32545 /* OpenMP 4.5:
32546 grainsize ( expression ) */
32547
32548 static tree
32549 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32550 location_t location)
32551 {
32552 tree t, c;
32553
32554 matching_parens parens;
32555 if (!parens.require_open (parser))
32556 return list;
32557
32558 t = cp_parser_expression (parser);
32559
32560 if (t == error_mark_node
32561 || !parens.require_close (parser))
32562 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32563 /*or_comma=*/false,
32564 /*consume_paren=*/true);
32565
32566 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32567 "grainsize", location);
32568
32569 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32570 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32571 OMP_CLAUSE_CHAIN (c) = list;
32572
32573 return c;
32574 }
32575
32576 /* OpenMP 4.5:
32577 priority ( expression ) */
32578
32579 static tree
32580 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32581 location_t location)
32582 {
32583 tree t, c;
32584
32585 matching_parens parens;
32586 if (!parens.require_open (parser))
32587 return list;
32588
32589 t = cp_parser_expression (parser);
32590
32591 if (t == error_mark_node
32592 || !parens.require_close (parser))
32593 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32594 /*or_comma=*/false,
32595 /*consume_paren=*/true);
32596
32597 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32598 "priority", location);
32599
32600 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32601 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32602 OMP_CLAUSE_CHAIN (c) = list;
32603
32604 return c;
32605 }
32606
32607 /* OpenMP 4.5:
32608 hint ( expression ) */
32609
32610 static tree
32611 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32612 location_t location)
32613 {
32614 tree t, c;
32615
32616 matching_parens parens;
32617 if (!parens.require_open (parser))
32618 return list;
32619
32620 t = cp_parser_expression (parser);
32621
32622 if (t == error_mark_node
32623 || !parens.require_close (parser))
32624 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32625 /*or_comma=*/false,
32626 /*consume_paren=*/true);
32627
32628 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32629
32630 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32631 OMP_CLAUSE_HINT_EXPR (c) = t;
32632 OMP_CLAUSE_CHAIN (c) = list;
32633
32634 return c;
32635 }
32636
32637 /* OpenMP 4.5:
32638 defaultmap ( tofrom : scalar ) */
32639
32640 static tree
32641 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32642 location_t location)
32643 {
32644 tree c, id;
32645 const char *p;
32646
32647 matching_parens parens;
32648 if (!parens.require_open (parser))
32649 return list;
32650
32651 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32652 {
32653 cp_parser_error (parser, "expected %<tofrom%>");
32654 goto out_err;
32655 }
32656 id = cp_lexer_peek_token (parser->lexer)->u.value;
32657 p = IDENTIFIER_POINTER (id);
32658 if (strcmp (p, "tofrom") != 0)
32659 {
32660 cp_parser_error (parser, "expected %<tofrom%>");
32661 goto out_err;
32662 }
32663 cp_lexer_consume_token (parser->lexer);
32664 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32665 goto out_err;
32666
32667 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32668 {
32669 cp_parser_error (parser, "expected %<scalar%>");
32670 goto out_err;
32671 }
32672 id = cp_lexer_peek_token (parser->lexer)->u.value;
32673 p = IDENTIFIER_POINTER (id);
32674 if (strcmp (p, "scalar") != 0)
32675 {
32676 cp_parser_error (parser, "expected %<scalar%>");
32677 goto out_err;
32678 }
32679 cp_lexer_consume_token (parser->lexer);
32680 if (!parens.require_close (parser))
32681 goto out_err;
32682
32683 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32684 location);
32685
32686 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32687 OMP_CLAUSE_CHAIN (c) = list;
32688 return c;
32689
32690 out_err:
32691 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32692 /*or_comma=*/false,
32693 /*consume_paren=*/true);
32694 return list;
32695 }
32696
32697 /* OpenMP 2.5:
32698 ordered
32699
32700 OpenMP 4.5:
32701 ordered ( constant-expression ) */
32702
32703 static tree
32704 cp_parser_omp_clause_ordered (cp_parser *parser,
32705 tree list, location_t location)
32706 {
32707 tree c, num = NULL_TREE;
32708 HOST_WIDE_INT n;
32709
32710 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32711 "ordered", location);
32712
32713 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32714 {
32715 matching_parens parens;
32716 parens.consume_open (parser);
32717
32718 num = cp_parser_constant_expression (parser);
32719
32720 if (!parens.require_close (parser))
32721 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32722 /*or_comma=*/false,
32723 /*consume_paren=*/true);
32724
32725 if (num == error_mark_node)
32726 return list;
32727 num = fold_non_dependent_expr (num);
32728 if (!tree_fits_shwi_p (num)
32729 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32730 || (n = tree_to_shwi (num)) <= 0
32731 || (int) n != n)
32732 {
32733 error_at (location,
32734 "ordered argument needs positive constant integer "
32735 "expression");
32736 return list;
32737 }
32738 }
32739
32740 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32741 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32742 OMP_CLAUSE_CHAIN (c) = list;
32743 return c;
32744 }
32745
32746 /* OpenMP 2.5:
32747 reduction ( reduction-operator : variable-list )
32748
32749 reduction-operator:
32750 One of: + * - & ^ | && ||
32751
32752 OpenMP 3.1:
32753
32754 reduction-operator:
32755 One of: + * - & ^ | && || min max
32756
32757 OpenMP 4.0:
32758
32759 reduction-operator:
32760 One of: + * - & ^ | && ||
32761 id-expression */
32762
32763 static tree
32764 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32765 {
32766 enum tree_code code = ERROR_MARK;
32767 tree nlist, c, id = NULL_TREE;
32768
32769 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32770 return list;
32771
32772 switch (cp_lexer_peek_token (parser->lexer)->type)
32773 {
32774 case CPP_PLUS: code = PLUS_EXPR; break;
32775 case CPP_MULT: code = MULT_EXPR; break;
32776 case CPP_MINUS: code = MINUS_EXPR; break;
32777 case CPP_AND: code = BIT_AND_EXPR; break;
32778 case CPP_XOR: code = BIT_XOR_EXPR; break;
32779 case CPP_OR: code = BIT_IOR_EXPR; break;
32780 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32781 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32782 default: break;
32783 }
32784
32785 if (code != ERROR_MARK)
32786 cp_lexer_consume_token (parser->lexer);
32787 else
32788 {
32789 bool saved_colon_corrects_to_scope_p;
32790 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32791 parser->colon_corrects_to_scope_p = false;
32792 id = cp_parser_id_expression (parser, /*template_p=*/false,
32793 /*check_dependency_p=*/true,
32794 /*template_p=*/NULL,
32795 /*declarator_p=*/false,
32796 /*optional_p=*/false);
32797 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32798 if (identifier_p (id))
32799 {
32800 const char *p = IDENTIFIER_POINTER (id);
32801
32802 if (strcmp (p, "min") == 0)
32803 code = MIN_EXPR;
32804 else if (strcmp (p, "max") == 0)
32805 code = MAX_EXPR;
32806 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32807 code = PLUS_EXPR;
32808 else if (id == ovl_op_identifier (false, MULT_EXPR))
32809 code = MULT_EXPR;
32810 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32811 code = MINUS_EXPR;
32812 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32813 code = BIT_AND_EXPR;
32814 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32815 code = BIT_IOR_EXPR;
32816 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32817 code = BIT_XOR_EXPR;
32818 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32819 code = TRUTH_ANDIF_EXPR;
32820 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32821 code = TRUTH_ORIF_EXPR;
32822 id = omp_reduction_id (code, id, NULL_TREE);
32823 tree scope = parser->scope;
32824 if (scope)
32825 id = build_qualified_name (NULL_TREE, scope, id, false);
32826 parser->scope = NULL_TREE;
32827 parser->qualifying_scope = NULL_TREE;
32828 parser->object_scope = NULL_TREE;
32829 }
32830 else
32831 {
32832 error ("invalid reduction-identifier");
32833 resync_fail:
32834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32835 /*or_comma=*/false,
32836 /*consume_paren=*/true);
32837 return list;
32838 }
32839 }
32840
32841 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32842 goto resync_fail;
32843
32844 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32845 NULL);
32846 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32847 {
32848 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32849 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32850 }
32851
32852 return nlist;
32853 }
32854
32855 /* OpenMP 2.5:
32856 schedule ( schedule-kind )
32857 schedule ( schedule-kind , expression )
32858
32859 schedule-kind:
32860 static | dynamic | guided | runtime | auto
32861
32862 OpenMP 4.5:
32863 schedule ( schedule-modifier : schedule-kind )
32864 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32865
32866 schedule-modifier:
32867 simd
32868 monotonic
32869 nonmonotonic */
32870
32871 static tree
32872 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32873 {
32874 tree c, t;
32875 int modifiers = 0, nmodifiers = 0;
32876
32877 matching_parens parens;
32878 if (!parens.require_open (parser))
32879 return list;
32880
32881 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32882
32883 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32884 {
32885 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32886 const char *p = IDENTIFIER_POINTER (id);
32887 if (strcmp ("simd", p) == 0)
32888 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32889 else if (strcmp ("monotonic", p) == 0)
32890 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32891 else if (strcmp ("nonmonotonic", p) == 0)
32892 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32893 else
32894 break;
32895 cp_lexer_consume_token (parser->lexer);
32896 if (nmodifiers++ == 0
32897 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32898 cp_lexer_consume_token (parser->lexer);
32899 else
32900 {
32901 cp_parser_require (parser, CPP_COLON, RT_COLON);
32902 break;
32903 }
32904 }
32905
32906 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32907 {
32908 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32909 const char *p = IDENTIFIER_POINTER (id);
32910
32911 switch (p[0])
32912 {
32913 case 'd':
32914 if (strcmp ("dynamic", p) != 0)
32915 goto invalid_kind;
32916 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32917 break;
32918
32919 case 'g':
32920 if (strcmp ("guided", p) != 0)
32921 goto invalid_kind;
32922 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32923 break;
32924
32925 case 'r':
32926 if (strcmp ("runtime", p) != 0)
32927 goto invalid_kind;
32928 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32929 break;
32930
32931 default:
32932 goto invalid_kind;
32933 }
32934 }
32935 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32936 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32937 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32938 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32939 else
32940 goto invalid_kind;
32941 cp_lexer_consume_token (parser->lexer);
32942
32943 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32944 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32945 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32946 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32947 {
32948 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32949 "specified");
32950 modifiers = 0;
32951 }
32952
32953 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32954 {
32955 cp_token *token;
32956 cp_lexer_consume_token (parser->lexer);
32957
32958 token = cp_lexer_peek_token (parser->lexer);
32959 t = cp_parser_assignment_expression (parser);
32960
32961 if (t == error_mark_node)
32962 goto resync_fail;
32963 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32964 error_at (token->location, "schedule %<runtime%> does not take "
32965 "a %<chunk_size%> parameter");
32966 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32967 error_at (token->location, "schedule %<auto%> does not take "
32968 "a %<chunk_size%> parameter");
32969 else
32970 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32971
32972 if (!parens.require_close (parser))
32973 goto resync_fail;
32974 }
32975 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32976 goto resync_fail;
32977
32978 OMP_CLAUSE_SCHEDULE_KIND (c)
32979 = (enum omp_clause_schedule_kind)
32980 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32981
32982 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32983 OMP_CLAUSE_CHAIN (c) = list;
32984 return c;
32985
32986 invalid_kind:
32987 cp_parser_error (parser, "invalid schedule kind");
32988 resync_fail:
32989 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32990 /*or_comma=*/false,
32991 /*consume_paren=*/true);
32992 return list;
32993 }
32994
32995 /* OpenMP 3.0:
32996 untied */
32997
32998 static tree
32999 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33000 tree list, location_t location)
33001 {
33002 tree c;
33003
33004 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33005
33006 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33007 OMP_CLAUSE_CHAIN (c) = list;
33008 return c;
33009 }
33010
33011 /* OpenMP 4.0:
33012 inbranch
33013 notinbranch */
33014
33015 static tree
33016 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33017 tree list, location_t location)
33018 {
33019 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33020 tree c = build_omp_clause (location, code);
33021 OMP_CLAUSE_CHAIN (c) = list;
33022 return c;
33023 }
33024
33025 /* OpenMP 4.0:
33026 parallel
33027 for
33028 sections
33029 taskgroup */
33030
33031 static tree
33032 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33033 enum omp_clause_code code,
33034 tree list, location_t location)
33035 {
33036 tree c = build_omp_clause (location, code);
33037 OMP_CLAUSE_CHAIN (c) = list;
33038 return c;
33039 }
33040
33041 /* OpenMP 4.5:
33042 nogroup */
33043
33044 static tree
33045 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33046 tree list, location_t location)
33047 {
33048 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33049 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33050 OMP_CLAUSE_CHAIN (c) = list;
33051 return c;
33052 }
33053
33054 /* OpenMP 4.5:
33055 simd
33056 threads */
33057
33058 static tree
33059 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33060 enum omp_clause_code code,
33061 tree list, location_t location)
33062 {
33063 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33064 tree c = build_omp_clause (location, code);
33065 OMP_CLAUSE_CHAIN (c) = list;
33066 return c;
33067 }
33068
33069 /* OpenMP 4.0:
33070 num_teams ( expression ) */
33071
33072 static tree
33073 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33074 location_t location)
33075 {
33076 tree t, c;
33077
33078 matching_parens parens;
33079 if (!parens.require_open (parser))
33080 return list;
33081
33082 t = cp_parser_expression (parser);
33083
33084 if (t == error_mark_node
33085 || !parens.require_close (parser))
33086 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33087 /*or_comma=*/false,
33088 /*consume_paren=*/true);
33089
33090 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33091 "num_teams", location);
33092
33093 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33094 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33095 OMP_CLAUSE_CHAIN (c) = list;
33096
33097 return c;
33098 }
33099
33100 /* OpenMP 4.0:
33101 thread_limit ( expression ) */
33102
33103 static tree
33104 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33105 location_t location)
33106 {
33107 tree t, c;
33108
33109 matching_parens parens;
33110 if (!parens.require_open (parser))
33111 return list;
33112
33113 t = cp_parser_expression (parser);
33114
33115 if (t == error_mark_node
33116 || !parens.require_close (parser))
33117 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33118 /*or_comma=*/false,
33119 /*consume_paren=*/true);
33120
33121 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33122 "thread_limit", location);
33123
33124 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33125 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33126 OMP_CLAUSE_CHAIN (c) = list;
33127
33128 return c;
33129 }
33130
33131 /* OpenMP 4.0:
33132 aligned ( variable-list )
33133 aligned ( variable-list : constant-expression ) */
33134
33135 static tree
33136 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33137 {
33138 tree nlist, c, alignment = NULL_TREE;
33139 bool colon;
33140
33141 matching_parens parens;
33142 if (!parens.require_open (parser))
33143 return list;
33144
33145 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33146 &colon);
33147
33148 if (colon)
33149 {
33150 alignment = cp_parser_constant_expression (parser);
33151
33152 if (!parens.require_close (parser))
33153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33154 /*or_comma=*/false,
33155 /*consume_paren=*/true);
33156
33157 if (alignment == error_mark_node)
33158 alignment = NULL_TREE;
33159 }
33160
33161 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33162 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33163
33164 return nlist;
33165 }
33166
33167 /* OpenMP 4.0:
33168 linear ( variable-list )
33169 linear ( variable-list : expression )
33170
33171 OpenMP 4.5:
33172 linear ( modifier ( variable-list ) )
33173 linear ( modifier ( variable-list ) : expression ) */
33174
33175 static tree
33176 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33177 bool declare_simd)
33178 {
33179 tree nlist, c, step = integer_one_node;
33180 bool colon;
33181 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33182
33183 matching_parens parens;
33184 if (!parens.require_open (parser))
33185 return list;
33186
33187 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33188 {
33189 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33190 const char *p = IDENTIFIER_POINTER (id);
33191
33192 if (strcmp ("ref", p) == 0)
33193 kind = OMP_CLAUSE_LINEAR_REF;
33194 else if (strcmp ("val", p) == 0)
33195 kind = OMP_CLAUSE_LINEAR_VAL;
33196 else if (strcmp ("uval", p) == 0)
33197 kind = OMP_CLAUSE_LINEAR_UVAL;
33198 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33199 cp_lexer_consume_token (parser->lexer);
33200 else
33201 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33202 }
33203
33204 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33205 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33206 &colon);
33207 else
33208 {
33209 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33210 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33211 if (colon)
33212 cp_parser_require (parser, CPP_COLON, RT_COLON);
33213 else if (!parens.require_close (parser))
33214 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33215 /*or_comma=*/false,
33216 /*consume_paren=*/true);
33217 }
33218
33219 if (colon)
33220 {
33221 step = NULL_TREE;
33222 if (declare_simd
33223 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33224 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33225 {
33226 cp_token *token = cp_lexer_peek_token (parser->lexer);
33227 cp_parser_parse_tentatively (parser);
33228 step = cp_parser_id_expression (parser, /*template_p=*/false,
33229 /*check_dependency_p=*/true,
33230 /*template_p=*/NULL,
33231 /*declarator_p=*/false,
33232 /*optional_p=*/false);
33233 if (step != error_mark_node)
33234 step = cp_parser_lookup_name_simple (parser, step, token->location);
33235 if (step == error_mark_node)
33236 {
33237 step = NULL_TREE;
33238 cp_parser_abort_tentative_parse (parser);
33239 }
33240 else if (!cp_parser_parse_definitely (parser))
33241 step = NULL_TREE;
33242 }
33243 if (!step)
33244 step = cp_parser_expression (parser);
33245
33246 if (!parens.require_close (parser))
33247 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33248 /*or_comma=*/false,
33249 /*consume_paren=*/true);
33250
33251 if (step == error_mark_node)
33252 return list;
33253 }
33254
33255 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33256 {
33257 OMP_CLAUSE_LINEAR_STEP (c) = step;
33258 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33259 }
33260
33261 return nlist;
33262 }
33263
33264 /* OpenMP 4.0:
33265 safelen ( constant-expression ) */
33266
33267 static tree
33268 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33269 location_t location)
33270 {
33271 tree t, c;
33272
33273 matching_parens parens;
33274 if (!parens.require_open (parser))
33275 return list;
33276
33277 t = cp_parser_constant_expression (parser);
33278
33279 if (t == error_mark_node
33280 || !parens.require_close (parser))
33281 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33282 /*or_comma=*/false,
33283 /*consume_paren=*/true);
33284
33285 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33286
33287 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33288 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33289 OMP_CLAUSE_CHAIN (c) = list;
33290
33291 return c;
33292 }
33293
33294 /* OpenMP 4.0:
33295 simdlen ( constant-expression ) */
33296
33297 static tree
33298 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33299 location_t location)
33300 {
33301 tree t, c;
33302
33303 matching_parens parens;
33304 if (!parens.require_open (parser))
33305 return list;
33306
33307 t = cp_parser_constant_expression (parser);
33308
33309 if (t == error_mark_node
33310 || !parens.require_close (parser))
33311 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33312 /*or_comma=*/false,
33313 /*consume_paren=*/true);
33314
33315 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33316
33317 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33318 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33319 OMP_CLAUSE_CHAIN (c) = list;
33320
33321 return c;
33322 }
33323
33324 /* OpenMP 4.5:
33325 vec:
33326 identifier [+/- integer]
33327 vec , identifier [+/- integer]
33328 */
33329
33330 static tree
33331 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33332 tree list)
33333 {
33334 tree vec = NULL;
33335
33336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33337 {
33338 cp_parser_error (parser, "expected identifier");
33339 return list;
33340 }
33341
33342 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33343 {
33344 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33345 tree t, identifier = cp_parser_identifier (parser);
33346 tree addend = NULL;
33347
33348 if (identifier == error_mark_node)
33349 t = error_mark_node;
33350 else
33351 {
33352 t = cp_parser_lookup_name_simple
33353 (parser, identifier,
33354 cp_lexer_peek_token (parser->lexer)->location);
33355 if (t == error_mark_node)
33356 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33357 id_loc);
33358 }
33359
33360 bool neg = false;
33361 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33362 neg = true;
33363 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33364 {
33365 addend = integer_zero_node;
33366 goto add_to_vector;
33367 }
33368 cp_lexer_consume_token (parser->lexer);
33369
33370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33371 {
33372 cp_parser_error (parser, "expected integer");
33373 return list;
33374 }
33375
33376 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33377 if (TREE_CODE (addend) != INTEGER_CST)
33378 {
33379 cp_parser_error (parser, "expected integer");
33380 return list;
33381 }
33382 cp_lexer_consume_token (parser->lexer);
33383
33384 add_to_vector:
33385 if (t != error_mark_node)
33386 {
33387 vec = tree_cons (addend, t, vec);
33388 if (neg)
33389 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33390 }
33391
33392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33393 break;
33394
33395 cp_lexer_consume_token (parser->lexer);
33396 }
33397
33398 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33399 {
33400 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33401 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33402 OMP_CLAUSE_DECL (u) = nreverse (vec);
33403 OMP_CLAUSE_CHAIN (u) = list;
33404 return u;
33405 }
33406 return list;
33407 }
33408
33409 /* OpenMP 4.0:
33410 depend ( depend-kind : variable-list )
33411
33412 depend-kind:
33413 in | out | inout
33414
33415 OpenMP 4.5:
33416 depend ( source )
33417
33418 depend ( sink : vec ) */
33419
33420 static tree
33421 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33422 {
33423 tree nlist, c;
33424 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33425
33426 matching_parens parens;
33427 if (!parens.require_open (parser))
33428 return list;
33429
33430 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33431 {
33432 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33433 const char *p = IDENTIFIER_POINTER (id);
33434
33435 if (strcmp ("in", p) == 0)
33436 kind = OMP_CLAUSE_DEPEND_IN;
33437 else if (strcmp ("inout", p) == 0)
33438 kind = OMP_CLAUSE_DEPEND_INOUT;
33439 else if (strcmp ("out", p) == 0)
33440 kind = OMP_CLAUSE_DEPEND_OUT;
33441 else if (strcmp ("source", p) == 0)
33442 kind = OMP_CLAUSE_DEPEND_SOURCE;
33443 else if (strcmp ("sink", p) == 0)
33444 kind = OMP_CLAUSE_DEPEND_SINK;
33445 else
33446 goto invalid_kind;
33447 }
33448 else
33449 goto invalid_kind;
33450
33451 cp_lexer_consume_token (parser->lexer);
33452
33453 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33454 {
33455 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33456 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33457 OMP_CLAUSE_DECL (c) = NULL_TREE;
33458 OMP_CLAUSE_CHAIN (c) = list;
33459 if (!parens.require_close (parser))
33460 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33461 /*or_comma=*/false,
33462 /*consume_paren=*/true);
33463 return c;
33464 }
33465
33466 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33467 goto resync_fail;
33468
33469 if (kind == OMP_CLAUSE_DEPEND_SINK)
33470 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33471 else
33472 {
33473 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33474 list, NULL);
33475
33476 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33477 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33478 }
33479 return nlist;
33480
33481 invalid_kind:
33482 cp_parser_error (parser, "invalid depend kind");
33483 resync_fail:
33484 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33485 /*or_comma=*/false,
33486 /*consume_paren=*/true);
33487 return list;
33488 }
33489
33490 /* OpenMP 4.0:
33491 map ( map-kind : variable-list )
33492 map ( variable-list )
33493
33494 map-kind:
33495 alloc | to | from | tofrom
33496
33497 OpenMP 4.5:
33498 map-kind:
33499 alloc | to | from | tofrom | release | delete
33500
33501 map ( always [,] map-kind: variable-list ) */
33502
33503 static tree
33504 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33505 {
33506 tree nlist, c;
33507 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33508 bool always = false;
33509
33510 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33511 return list;
33512
33513 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33514 {
33515 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33516 const char *p = IDENTIFIER_POINTER (id);
33517
33518 if (strcmp ("always", p) == 0)
33519 {
33520 int nth = 2;
33521 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33522 nth++;
33523 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33524 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33525 == RID_DELETE))
33526 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33527 == CPP_COLON))
33528 {
33529 always = true;
33530 cp_lexer_consume_token (parser->lexer);
33531 if (nth == 3)
33532 cp_lexer_consume_token (parser->lexer);
33533 }
33534 }
33535 }
33536
33537 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33538 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33539 {
33540 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33541 const char *p = IDENTIFIER_POINTER (id);
33542
33543 if (strcmp ("alloc", p) == 0)
33544 kind = GOMP_MAP_ALLOC;
33545 else if (strcmp ("to", p) == 0)
33546 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33547 else if (strcmp ("from", p) == 0)
33548 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33549 else if (strcmp ("tofrom", p) == 0)
33550 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33551 else if (strcmp ("release", p) == 0)
33552 kind = GOMP_MAP_RELEASE;
33553 else
33554 {
33555 cp_parser_error (parser, "invalid map kind");
33556 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33557 /*or_comma=*/false,
33558 /*consume_paren=*/true);
33559 return list;
33560 }
33561 cp_lexer_consume_token (parser->lexer);
33562 cp_lexer_consume_token (parser->lexer);
33563 }
33564 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33565 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33566 {
33567 kind = GOMP_MAP_DELETE;
33568 cp_lexer_consume_token (parser->lexer);
33569 cp_lexer_consume_token (parser->lexer);
33570 }
33571
33572 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33573 NULL);
33574
33575 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33576 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33577
33578 return nlist;
33579 }
33580
33581 /* OpenMP 4.0:
33582 device ( expression ) */
33583
33584 static tree
33585 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33586 location_t location)
33587 {
33588 tree t, c;
33589
33590 matching_parens parens;
33591 if (!parens.require_open (parser))
33592 return list;
33593
33594 t = cp_parser_expression (parser);
33595
33596 if (t == error_mark_node
33597 || !parens.require_close (parser))
33598 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33599 /*or_comma=*/false,
33600 /*consume_paren=*/true);
33601
33602 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33603 "device", location);
33604
33605 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33606 OMP_CLAUSE_DEVICE_ID (c) = t;
33607 OMP_CLAUSE_CHAIN (c) = list;
33608
33609 return c;
33610 }
33611
33612 /* OpenMP 4.0:
33613 dist_schedule ( static )
33614 dist_schedule ( static , expression ) */
33615
33616 static tree
33617 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33618 location_t location)
33619 {
33620 tree c, t;
33621
33622 matching_parens parens;
33623 if (!parens.require_open (parser))
33624 return list;
33625
33626 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33627
33628 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33629 goto invalid_kind;
33630 cp_lexer_consume_token (parser->lexer);
33631
33632 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33633 {
33634 cp_lexer_consume_token (parser->lexer);
33635
33636 t = cp_parser_assignment_expression (parser);
33637
33638 if (t == error_mark_node)
33639 goto resync_fail;
33640 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33641
33642 if (!parens.require_close (parser))
33643 goto resync_fail;
33644 }
33645 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33646 goto resync_fail;
33647
33648 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33649 location);
33650 OMP_CLAUSE_CHAIN (c) = list;
33651 return c;
33652
33653 invalid_kind:
33654 cp_parser_error (parser, "invalid dist_schedule kind");
33655 resync_fail:
33656 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33657 /*or_comma=*/false,
33658 /*consume_paren=*/true);
33659 return list;
33660 }
33661
33662 /* OpenMP 4.0:
33663 proc_bind ( proc-bind-kind )
33664
33665 proc-bind-kind:
33666 master | close | spread */
33667
33668 static tree
33669 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33670 location_t location)
33671 {
33672 tree c;
33673 enum omp_clause_proc_bind_kind kind;
33674
33675 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33676 return list;
33677
33678 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33679 {
33680 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33681 const char *p = IDENTIFIER_POINTER (id);
33682
33683 if (strcmp ("master", p) == 0)
33684 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33685 else if (strcmp ("close", p) == 0)
33686 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33687 else if (strcmp ("spread", p) == 0)
33688 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33689 else
33690 goto invalid_kind;
33691 }
33692 else
33693 goto invalid_kind;
33694
33695 cp_lexer_consume_token (parser->lexer);
33696 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33697 goto resync_fail;
33698
33699 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33700 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33701 location);
33702 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33703 OMP_CLAUSE_CHAIN (c) = list;
33704 return c;
33705
33706 invalid_kind:
33707 cp_parser_error (parser, "invalid depend kind");
33708 resync_fail:
33709 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33710 /*or_comma=*/false,
33711 /*consume_paren=*/true);
33712 return list;
33713 }
33714
33715 /* OpenACC:
33716 async [( int-expr )] */
33717
33718 static tree
33719 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33720 {
33721 tree c, t;
33722 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33723
33724 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33725
33726 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33727 {
33728 matching_parens parens;
33729 parens.consume_open (parser);
33730
33731 t = cp_parser_expression (parser);
33732 if (t == error_mark_node
33733 || !parens.require_close (parser))
33734 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33735 /*or_comma=*/false,
33736 /*consume_paren=*/true);
33737 }
33738
33739 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33740
33741 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33742 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33743 OMP_CLAUSE_CHAIN (c) = list;
33744 list = c;
33745
33746 return list;
33747 }
33748
33749 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33750 is a bitmask in MASK. Return the list of clauses found. */
33751
33752 static tree
33753 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33754 const char *where, cp_token *pragma_tok,
33755 bool finish_p = true)
33756 {
33757 tree clauses = NULL;
33758 bool first = true;
33759
33760 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33761 {
33762 location_t here;
33763 pragma_omp_clause c_kind;
33764 omp_clause_code code;
33765 const char *c_name;
33766 tree prev = clauses;
33767
33768 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33769 cp_lexer_consume_token (parser->lexer);
33770
33771 here = cp_lexer_peek_token (parser->lexer)->location;
33772 c_kind = cp_parser_omp_clause_name (parser);
33773
33774 switch (c_kind)
33775 {
33776 case PRAGMA_OACC_CLAUSE_ASYNC:
33777 clauses = cp_parser_oacc_clause_async (parser, clauses);
33778 c_name = "async";
33779 break;
33780 case PRAGMA_OACC_CLAUSE_AUTO:
33781 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33782 clauses, here);
33783 c_name = "auto";
33784 break;
33785 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33786 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33787 c_name = "collapse";
33788 break;
33789 case PRAGMA_OACC_CLAUSE_COPY:
33790 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33791 c_name = "copy";
33792 break;
33793 case PRAGMA_OACC_CLAUSE_COPYIN:
33794 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33795 c_name = "copyin";
33796 break;
33797 case PRAGMA_OACC_CLAUSE_COPYOUT:
33798 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33799 c_name = "copyout";
33800 break;
33801 case PRAGMA_OACC_CLAUSE_CREATE:
33802 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33803 c_name = "create";
33804 break;
33805 case PRAGMA_OACC_CLAUSE_DELETE:
33806 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33807 c_name = "delete";
33808 break;
33809 case PRAGMA_OMP_CLAUSE_DEFAULT:
33810 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33811 c_name = "default";
33812 break;
33813 case PRAGMA_OACC_CLAUSE_DEVICE:
33814 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33815 c_name = "device";
33816 break;
33817 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33818 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33819 c_name = "deviceptr";
33820 break;
33821 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33822 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33823 c_name = "device_resident";
33824 break;
33825 case PRAGMA_OACC_CLAUSE_FINALIZE:
33826 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
33827 clauses, here);
33828 c_name = "finalize";
33829 break;
33830 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33831 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33832 clauses);
33833 c_name = "firstprivate";
33834 break;
33835 case PRAGMA_OACC_CLAUSE_GANG:
33836 c_name = "gang";
33837 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33838 c_name, clauses);
33839 break;
33840 case PRAGMA_OACC_CLAUSE_HOST:
33841 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33842 c_name = "host";
33843 break;
33844 case PRAGMA_OACC_CLAUSE_IF:
33845 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33846 c_name = "if";
33847 break;
33848 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
33849 clauses = cp_parser_oacc_simple_clause (parser,
33850 OMP_CLAUSE_IF_PRESENT,
33851 clauses, here);
33852 c_name = "if_present";
33853 break;
33854 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33855 clauses = cp_parser_oacc_simple_clause (parser,
33856 OMP_CLAUSE_INDEPENDENT,
33857 clauses, here);
33858 c_name = "independent";
33859 break;
33860 case PRAGMA_OACC_CLAUSE_LINK:
33861 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33862 c_name = "link";
33863 break;
33864 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33865 code = OMP_CLAUSE_NUM_GANGS;
33866 c_name = "num_gangs";
33867 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33868 clauses);
33869 break;
33870 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33871 c_name = "num_workers";
33872 code = OMP_CLAUSE_NUM_WORKERS;
33873 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33874 clauses);
33875 break;
33876 case PRAGMA_OACC_CLAUSE_PRESENT:
33877 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33878 c_name = "present";
33879 break;
33880 case PRAGMA_OACC_CLAUSE_PRIVATE:
33881 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33882 clauses);
33883 c_name = "private";
33884 break;
33885 case PRAGMA_OACC_CLAUSE_REDUCTION:
33886 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33887 c_name = "reduction";
33888 break;
33889 case PRAGMA_OACC_CLAUSE_SEQ:
33890 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33891 clauses, here);
33892 c_name = "seq";
33893 break;
33894 case PRAGMA_OACC_CLAUSE_TILE:
33895 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33896 c_name = "tile";
33897 break;
33898 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33899 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33900 clauses);
33901 c_name = "use_device";
33902 break;
33903 case PRAGMA_OACC_CLAUSE_VECTOR:
33904 c_name = "vector";
33905 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33906 c_name, clauses);
33907 break;
33908 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33909 c_name = "vector_length";
33910 code = OMP_CLAUSE_VECTOR_LENGTH;
33911 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33912 clauses);
33913 break;
33914 case PRAGMA_OACC_CLAUSE_WAIT:
33915 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33916 c_name = "wait";
33917 break;
33918 case PRAGMA_OACC_CLAUSE_WORKER:
33919 c_name = "worker";
33920 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33921 c_name, clauses);
33922 break;
33923 default:
33924 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33925 goto saw_error;
33926 }
33927
33928 first = false;
33929
33930 if (((mask >> c_kind) & 1) == 0)
33931 {
33932 /* Remove the invalid clause(s) from the list to avoid
33933 confusing the rest of the compiler. */
33934 clauses = prev;
33935 error_at (here, "%qs is not valid for %qs", c_name, where);
33936 }
33937 }
33938
33939 saw_error:
33940 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33941
33942 if (finish_p)
33943 return finish_omp_clauses (clauses, C_ORT_ACC);
33944
33945 return clauses;
33946 }
33947
33948 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33949 is a bitmask in MASK. Return the list of clauses found; the result
33950 of clause default goes in *pdefault. */
33951
33952 static tree
33953 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33954 const char *where, cp_token *pragma_tok,
33955 bool finish_p = true)
33956 {
33957 tree clauses = NULL;
33958 bool first = true;
33959 cp_token *token = NULL;
33960
33961 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33962 {
33963 pragma_omp_clause c_kind;
33964 const char *c_name;
33965 tree prev = clauses;
33966
33967 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33968 cp_lexer_consume_token (parser->lexer);
33969
33970 token = cp_lexer_peek_token (parser->lexer);
33971 c_kind = cp_parser_omp_clause_name (parser);
33972
33973 switch (c_kind)
33974 {
33975 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33976 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33977 token->location);
33978 c_name = "collapse";
33979 break;
33980 case PRAGMA_OMP_CLAUSE_COPYIN:
33981 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33982 c_name = "copyin";
33983 break;
33984 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33985 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33986 clauses);
33987 c_name = "copyprivate";
33988 break;
33989 case PRAGMA_OMP_CLAUSE_DEFAULT:
33990 clauses = cp_parser_omp_clause_default (parser, clauses,
33991 token->location, false);
33992 c_name = "default";
33993 break;
33994 case PRAGMA_OMP_CLAUSE_FINAL:
33995 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33996 c_name = "final";
33997 break;
33998 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33999 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34000 clauses);
34001 c_name = "firstprivate";
34002 break;
34003 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34004 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34005 token->location);
34006 c_name = "grainsize";
34007 break;
34008 case PRAGMA_OMP_CLAUSE_HINT:
34009 clauses = cp_parser_omp_clause_hint (parser, clauses,
34010 token->location);
34011 c_name = "hint";
34012 break;
34013 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34014 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34015 token->location);
34016 c_name = "defaultmap";
34017 break;
34018 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34019 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34020 clauses);
34021 c_name = "use_device_ptr";
34022 break;
34023 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34024 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34025 clauses);
34026 c_name = "is_device_ptr";
34027 break;
34028 case PRAGMA_OMP_CLAUSE_IF:
34029 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34030 true);
34031 c_name = "if";
34032 break;
34033 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34034 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
34035 clauses);
34036 c_name = "lastprivate";
34037 break;
34038 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34039 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34040 token->location);
34041 c_name = "mergeable";
34042 break;
34043 case PRAGMA_OMP_CLAUSE_NOWAIT:
34044 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34045 c_name = "nowait";
34046 break;
34047 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34048 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34049 token->location);
34050 c_name = "num_tasks";
34051 break;
34052 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34053 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34054 token->location);
34055 c_name = "num_threads";
34056 break;
34057 case PRAGMA_OMP_CLAUSE_ORDERED:
34058 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34059 token->location);
34060 c_name = "ordered";
34061 break;
34062 case PRAGMA_OMP_CLAUSE_PRIORITY:
34063 clauses = cp_parser_omp_clause_priority (parser, clauses,
34064 token->location);
34065 c_name = "priority";
34066 break;
34067 case PRAGMA_OMP_CLAUSE_PRIVATE:
34068 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34069 clauses);
34070 c_name = "private";
34071 break;
34072 case PRAGMA_OMP_CLAUSE_REDUCTION:
34073 clauses = cp_parser_omp_clause_reduction (parser, clauses);
34074 c_name = "reduction";
34075 break;
34076 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34077 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34078 token->location);
34079 c_name = "schedule";
34080 break;
34081 case PRAGMA_OMP_CLAUSE_SHARED:
34082 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34083 clauses);
34084 c_name = "shared";
34085 break;
34086 case PRAGMA_OMP_CLAUSE_UNTIED:
34087 clauses = cp_parser_omp_clause_untied (parser, clauses,
34088 token->location);
34089 c_name = "untied";
34090 break;
34091 case PRAGMA_OMP_CLAUSE_INBRANCH:
34092 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34093 clauses, token->location);
34094 c_name = "inbranch";
34095 break;
34096 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34097 clauses = cp_parser_omp_clause_branch (parser,
34098 OMP_CLAUSE_NOTINBRANCH,
34099 clauses, token->location);
34100 c_name = "notinbranch";
34101 break;
34102 case PRAGMA_OMP_CLAUSE_PARALLEL:
34103 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34104 clauses, token->location);
34105 c_name = "parallel";
34106 if (!first)
34107 {
34108 clause_not_first:
34109 error_at (token->location, "%qs must be the first clause of %qs",
34110 c_name, where);
34111 clauses = prev;
34112 }
34113 break;
34114 case PRAGMA_OMP_CLAUSE_FOR:
34115 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34116 clauses, token->location);
34117 c_name = "for";
34118 if (!first)
34119 goto clause_not_first;
34120 break;
34121 case PRAGMA_OMP_CLAUSE_SECTIONS:
34122 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34123 clauses, token->location);
34124 c_name = "sections";
34125 if (!first)
34126 goto clause_not_first;
34127 break;
34128 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34129 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34130 clauses, token->location);
34131 c_name = "taskgroup";
34132 if (!first)
34133 goto clause_not_first;
34134 break;
34135 case PRAGMA_OMP_CLAUSE_LINK:
34136 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34137 c_name = "to";
34138 break;
34139 case PRAGMA_OMP_CLAUSE_TO:
34140 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34141 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34142 clauses);
34143 else
34144 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34145 c_name = "to";
34146 break;
34147 case PRAGMA_OMP_CLAUSE_FROM:
34148 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34149 c_name = "from";
34150 break;
34151 case PRAGMA_OMP_CLAUSE_UNIFORM:
34152 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34153 clauses);
34154 c_name = "uniform";
34155 break;
34156 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34157 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34158 token->location);
34159 c_name = "num_teams";
34160 break;
34161 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34162 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34163 token->location);
34164 c_name = "thread_limit";
34165 break;
34166 case PRAGMA_OMP_CLAUSE_ALIGNED:
34167 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34168 c_name = "aligned";
34169 break;
34170 case PRAGMA_OMP_CLAUSE_LINEAR:
34171 {
34172 bool declare_simd = false;
34173 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34174 declare_simd = true;
34175 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34176 }
34177 c_name = "linear";
34178 break;
34179 case PRAGMA_OMP_CLAUSE_DEPEND:
34180 clauses = cp_parser_omp_clause_depend (parser, clauses,
34181 token->location);
34182 c_name = "depend";
34183 break;
34184 case PRAGMA_OMP_CLAUSE_MAP:
34185 clauses = cp_parser_omp_clause_map (parser, clauses);
34186 c_name = "map";
34187 break;
34188 case PRAGMA_OMP_CLAUSE_DEVICE:
34189 clauses = cp_parser_omp_clause_device (parser, clauses,
34190 token->location);
34191 c_name = "device";
34192 break;
34193 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34194 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34195 token->location);
34196 c_name = "dist_schedule";
34197 break;
34198 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34199 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34200 token->location);
34201 c_name = "proc_bind";
34202 break;
34203 case PRAGMA_OMP_CLAUSE_SAFELEN:
34204 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34205 token->location);
34206 c_name = "safelen";
34207 break;
34208 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34209 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34210 token->location);
34211 c_name = "simdlen";
34212 break;
34213 case PRAGMA_OMP_CLAUSE_NOGROUP:
34214 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34215 token->location);
34216 c_name = "nogroup";
34217 break;
34218 case PRAGMA_OMP_CLAUSE_THREADS:
34219 clauses
34220 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34221 clauses, token->location);
34222 c_name = "threads";
34223 break;
34224 case PRAGMA_OMP_CLAUSE_SIMD:
34225 clauses
34226 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34227 clauses, token->location);
34228 c_name = "simd";
34229 break;
34230 default:
34231 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34232 goto saw_error;
34233 }
34234
34235 first = false;
34236
34237 if (((mask >> c_kind) & 1) == 0)
34238 {
34239 /* Remove the invalid clause(s) from the list to avoid
34240 confusing the rest of the compiler. */
34241 clauses = prev;
34242 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34243 }
34244 }
34245 saw_error:
34246 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34247 if (finish_p)
34248 {
34249 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34250 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34251 else
34252 return finish_omp_clauses (clauses, C_ORT_OMP);
34253 }
34254 return clauses;
34255 }
34256
34257 /* OpenMP 2.5:
34258 structured-block:
34259 statement
34260
34261 In practice, we're also interested in adding the statement to an
34262 outer node. So it is convenient if we work around the fact that
34263 cp_parser_statement calls add_stmt. */
34264
34265 static unsigned
34266 cp_parser_begin_omp_structured_block (cp_parser *parser)
34267 {
34268 unsigned save = parser->in_statement;
34269
34270 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34271 This preserves the "not within loop or switch" style error messages
34272 for nonsense cases like
34273 void foo() {
34274 #pragma omp single
34275 break;
34276 }
34277 */
34278 if (parser->in_statement)
34279 parser->in_statement = IN_OMP_BLOCK;
34280
34281 return save;
34282 }
34283
34284 static void
34285 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34286 {
34287 parser->in_statement = save;
34288 }
34289
34290 static tree
34291 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34292 {
34293 tree stmt = begin_omp_structured_block ();
34294 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34295
34296 cp_parser_statement (parser, NULL_TREE, false, if_p);
34297
34298 cp_parser_end_omp_structured_block (parser, save);
34299 return finish_omp_structured_block (stmt);
34300 }
34301
34302 /* OpenMP 2.5:
34303 # pragma omp atomic new-line
34304 expression-stmt
34305
34306 expression-stmt:
34307 x binop= expr | x++ | ++x | x-- | --x
34308 binop:
34309 +, *, -, /, &, ^, |, <<, >>
34310
34311 where x is an lvalue expression with scalar type.
34312
34313 OpenMP 3.1:
34314 # pragma omp atomic new-line
34315 update-stmt
34316
34317 # pragma omp atomic read new-line
34318 read-stmt
34319
34320 # pragma omp atomic write new-line
34321 write-stmt
34322
34323 # pragma omp atomic update new-line
34324 update-stmt
34325
34326 # pragma omp atomic capture new-line
34327 capture-stmt
34328
34329 # pragma omp atomic capture new-line
34330 capture-block
34331
34332 read-stmt:
34333 v = x
34334 write-stmt:
34335 x = expr
34336 update-stmt:
34337 expression-stmt | x = x binop expr
34338 capture-stmt:
34339 v = expression-stmt
34340 capture-block:
34341 { v = x; update-stmt; } | { update-stmt; v = x; }
34342
34343 OpenMP 4.0:
34344 update-stmt:
34345 expression-stmt | x = x binop expr | x = expr binop x
34346 capture-stmt:
34347 v = update-stmt
34348 capture-block:
34349 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34350
34351 where x and v are lvalue expressions with scalar type. */
34352
34353 static void
34354 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34355 {
34356 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34357 tree rhs1 = NULL_TREE, orig_lhs;
34358 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34359 bool structured_block = false;
34360 bool seq_cst = false;
34361
34362 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34363 {
34364 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34365 const char *p = IDENTIFIER_POINTER (id);
34366
34367 if (!strcmp (p, "seq_cst"))
34368 {
34369 seq_cst = true;
34370 cp_lexer_consume_token (parser->lexer);
34371 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34372 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34373 cp_lexer_consume_token (parser->lexer);
34374 }
34375 }
34376 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34377 {
34378 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34379 const char *p = IDENTIFIER_POINTER (id);
34380
34381 if (!strcmp (p, "read"))
34382 code = OMP_ATOMIC_READ;
34383 else if (!strcmp (p, "write"))
34384 code = NOP_EXPR;
34385 else if (!strcmp (p, "update"))
34386 code = OMP_ATOMIC;
34387 else if (!strcmp (p, "capture"))
34388 code = OMP_ATOMIC_CAPTURE_NEW;
34389 else
34390 p = NULL;
34391 if (p)
34392 cp_lexer_consume_token (parser->lexer);
34393 }
34394 if (!seq_cst)
34395 {
34396 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34397 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34398 cp_lexer_consume_token (parser->lexer);
34399
34400 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34401 {
34402 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34403 const char *p = IDENTIFIER_POINTER (id);
34404
34405 if (!strcmp (p, "seq_cst"))
34406 {
34407 seq_cst = true;
34408 cp_lexer_consume_token (parser->lexer);
34409 }
34410 }
34411 }
34412 cp_parser_require_pragma_eol (parser, pragma_tok);
34413
34414 switch (code)
34415 {
34416 case OMP_ATOMIC_READ:
34417 case NOP_EXPR: /* atomic write */
34418 v = cp_parser_unary_expression (parser);
34419 if (v == error_mark_node)
34420 goto saw_error;
34421 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34422 goto saw_error;
34423 if (code == NOP_EXPR)
34424 lhs = cp_parser_expression (parser);
34425 else
34426 lhs = cp_parser_unary_expression (parser);
34427 if (lhs == error_mark_node)
34428 goto saw_error;
34429 if (code == NOP_EXPR)
34430 {
34431 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34432 opcode. */
34433 code = OMP_ATOMIC;
34434 rhs = lhs;
34435 lhs = v;
34436 v = NULL_TREE;
34437 }
34438 goto done;
34439 case OMP_ATOMIC_CAPTURE_NEW:
34440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34441 {
34442 cp_lexer_consume_token (parser->lexer);
34443 structured_block = true;
34444 }
34445 else
34446 {
34447 v = cp_parser_unary_expression (parser);
34448 if (v == error_mark_node)
34449 goto saw_error;
34450 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34451 goto saw_error;
34452 }
34453 default:
34454 break;
34455 }
34456
34457 restart:
34458 lhs = cp_parser_unary_expression (parser);
34459 orig_lhs = lhs;
34460 switch (TREE_CODE (lhs))
34461 {
34462 case ERROR_MARK:
34463 goto saw_error;
34464
34465 case POSTINCREMENT_EXPR:
34466 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34467 code = OMP_ATOMIC_CAPTURE_OLD;
34468 /* FALLTHROUGH */
34469 case PREINCREMENT_EXPR:
34470 lhs = TREE_OPERAND (lhs, 0);
34471 opcode = PLUS_EXPR;
34472 rhs = integer_one_node;
34473 break;
34474
34475 case POSTDECREMENT_EXPR:
34476 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34477 code = OMP_ATOMIC_CAPTURE_OLD;
34478 /* FALLTHROUGH */
34479 case PREDECREMENT_EXPR:
34480 lhs = TREE_OPERAND (lhs, 0);
34481 opcode = MINUS_EXPR;
34482 rhs = integer_one_node;
34483 break;
34484
34485 case COMPOUND_EXPR:
34486 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34487 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34488 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34489 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34490 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34491 (TREE_OPERAND (lhs, 1), 0), 0)))
34492 == BOOLEAN_TYPE)
34493 /* Undo effects of boolean_increment for post {in,de}crement. */
34494 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34495 /* FALLTHRU */
34496 case MODIFY_EXPR:
34497 if (TREE_CODE (lhs) == MODIFY_EXPR
34498 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34499 {
34500 /* Undo effects of boolean_increment. */
34501 if (integer_onep (TREE_OPERAND (lhs, 1)))
34502 {
34503 /* This is pre or post increment. */
34504 rhs = TREE_OPERAND (lhs, 1);
34505 lhs = TREE_OPERAND (lhs, 0);
34506 opcode = NOP_EXPR;
34507 if (code == OMP_ATOMIC_CAPTURE_NEW
34508 && !structured_block
34509 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34510 code = OMP_ATOMIC_CAPTURE_OLD;
34511 break;
34512 }
34513 }
34514 /* FALLTHRU */
34515 default:
34516 switch (cp_lexer_peek_token (parser->lexer)->type)
34517 {
34518 case CPP_MULT_EQ:
34519 opcode = MULT_EXPR;
34520 break;
34521 case CPP_DIV_EQ:
34522 opcode = TRUNC_DIV_EXPR;
34523 break;
34524 case CPP_PLUS_EQ:
34525 opcode = PLUS_EXPR;
34526 break;
34527 case CPP_MINUS_EQ:
34528 opcode = MINUS_EXPR;
34529 break;
34530 case CPP_LSHIFT_EQ:
34531 opcode = LSHIFT_EXPR;
34532 break;
34533 case CPP_RSHIFT_EQ:
34534 opcode = RSHIFT_EXPR;
34535 break;
34536 case CPP_AND_EQ:
34537 opcode = BIT_AND_EXPR;
34538 break;
34539 case CPP_OR_EQ:
34540 opcode = BIT_IOR_EXPR;
34541 break;
34542 case CPP_XOR_EQ:
34543 opcode = BIT_XOR_EXPR;
34544 break;
34545 case CPP_EQ:
34546 enum cp_parser_prec oprec;
34547 cp_token *token;
34548 cp_lexer_consume_token (parser->lexer);
34549 cp_parser_parse_tentatively (parser);
34550 rhs1 = cp_parser_simple_cast_expression (parser);
34551 if (rhs1 == error_mark_node)
34552 {
34553 cp_parser_abort_tentative_parse (parser);
34554 cp_parser_simple_cast_expression (parser);
34555 goto saw_error;
34556 }
34557 token = cp_lexer_peek_token (parser->lexer);
34558 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34559 {
34560 cp_parser_abort_tentative_parse (parser);
34561 cp_parser_parse_tentatively (parser);
34562 rhs = cp_parser_binary_expression (parser, false, true,
34563 PREC_NOT_OPERATOR, NULL);
34564 if (rhs == error_mark_node)
34565 {
34566 cp_parser_abort_tentative_parse (parser);
34567 cp_parser_binary_expression (parser, false, true,
34568 PREC_NOT_OPERATOR, NULL);
34569 goto saw_error;
34570 }
34571 switch (TREE_CODE (rhs))
34572 {
34573 case MULT_EXPR:
34574 case TRUNC_DIV_EXPR:
34575 case RDIV_EXPR:
34576 case PLUS_EXPR:
34577 case MINUS_EXPR:
34578 case LSHIFT_EXPR:
34579 case RSHIFT_EXPR:
34580 case BIT_AND_EXPR:
34581 case BIT_IOR_EXPR:
34582 case BIT_XOR_EXPR:
34583 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34584 {
34585 if (cp_parser_parse_definitely (parser))
34586 {
34587 opcode = TREE_CODE (rhs);
34588 rhs1 = TREE_OPERAND (rhs, 0);
34589 rhs = TREE_OPERAND (rhs, 1);
34590 goto stmt_done;
34591 }
34592 else
34593 goto saw_error;
34594 }
34595 break;
34596 default:
34597 break;
34598 }
34599 cp_parser_abort_tentative_parse (parser);
34600 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34601 {
34602 rhs = cp_parser_expression (parser);
34603 if (rhs == error_mark_node)
34604 goto saw_error;
34605 opcode = NOP_EXPR;
34606 rhs1 = NULL_TREE;
34607 goto stmt_done;
34608 }
34609 cp_parser_error (parser,
34610 "invalid form of %<#pragma omp atomic%>");
34611 goto saw_error;
34612 }
34613 if (!cp_parser_parse_definitely (parser))
34614 goto saw_error;
34615 switch (token->type)
34616 {
34617 case CPP_SEMICOLON:
34618 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34619 {
34620 code = OMP_ATOMIC_CAPTURE_OLD;
34621 v = lhs;
34622 lhs = NULL_TREE;
34623 lhs1 = rhs1;
34624 rhs1 = NULL_TREE;
34625 cp_lexer_consume_token (parser->lexer);
34626 goto restart;
34627 }
34628 else if (structured_block)
34629 {
34630 opcode = NOP_EXPR;
34631 rhs = rhs1;
34632 rhs1 = NULL_TREE;
34633 goto stmt_done;
34634 }
34635 cp_parser_error (parser,
34636 "invalid form of %<#pragma omp atomic%>");
34637 goto saw_error;
34638 case CPP_MULT:
34639 opcode = MULT_EXPR;
34640 break;
34641 case CPP_DIV:
34642 opcode = TRUNC_DIV_EXPR;
34643 break;
34644 case CPP_PLUS:
34645 opcode = PLUS_EXPR;
34646 break;
34647 case CPP_MINUS:
34648 opcode = MINUS_EXPR;
34649 break;
34650 case CPP_LSHIFT:
34651 opcode = LSHIFT_EXPR;
34652 break;
34653 case CPP_RSHIFT:
34654 opcode = RSHIFT_EXPR;
34655 break;
34656 case CPP_AND:
34657 opcode = BIT_AND_EXPR;
34658 break;
34659 case CPP_OR:
34660 opcode = BIT_IOR_EXPR;
34661 break;
34662 case CPP_XOR:
34663 opcode = BIT_XOR_EXPR;
34664 break;
34665 default:
34666 cp_parser_error (parser,
34667 "invalid operator for %<#pragma omp atomic%>");
34668 goto saw_error;
34669 }
34670 oprec = TOKEN_PRECEDENCE (token);
34671 gcc_assert (oprec != PREC_NOT_OPERATOR);
34672 if (commutative_tree_code (opcode))
34673 oprec = (enum cp_parser_prec) (oprec - 1);
34674 cp_lexer_consume_token (parser->lexer);
34675 rhs = cp_parser_binary_expression (parser, false, false,
34676 oprec, NULL);
34677 if (rhs == error_mark_node)
34678 goto saw_error;
34679 goto stmt_done;
34680 /* FALLTHROUGH */
34681 default:
34682 cp_parser_error (parser,
34683 "invalid operator for %<#pragma omp atomic%>");
34684 goto saw_error;
34685 }
34686 cp_lexer_consume_token (parser->lexer);
34687
34688 rhs = cp_parser_expression (parser);
34689 if (rhs == error_mark_node)
34690 goto saw_error;
34691 break;
34692 }
34693 stmt_done:
34694 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34695 {
34696 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34697 goto saw_error;
34698 v = cp_parser_unary_expression (parser);
34699 if (v == error_mark_node)
34700 goto saw_error;
34701 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34702 goto saw_error;
34703 lhs1 = cp_parser_unary_expression (parser);
34704 if (lhs1 == error_mark_node)
34705 goto saw_error;
34706 }
34707 if (structured_block)
34708 {
34709 cp_parser_consume_semicolon_at_end_of_statement (parser);
34710 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34711 }
34712 done:
34713 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34714 if (!structured_block)
34715 cp_parser_consume_semicolon_at_end_of_statement (parser);
34716 return;
34717
34718 saw_error:
34719 cp_parser_skip_to_end_of_block_or_statement (parser);
34720 if (structured_block)
34721 {
34722 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34723 cp_lexer_consume_token (parser->lexer);
34724 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34725 {
34726 cp_parser_skip_to_end_of_block_or_statement (parser);
34727 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34728 cp_lexer_consume_token (parser->lexer);
34729 }
34730 }
34731 }
34732
34733
34734 /* OpenMP 2.5:
34735 # pragma omp barrier new-line */
34736
34737 static void
34738 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34739 {
34740 cp_parser_require_pragma_eol (parser, pragma_tok);
34741 finish_omp_barrier ();
34742 }
34743
34744 /* OpenMP 2.5:
34745 # pragma omp critical [(name)] new-line
34746 structured-block
34747
34748 OpenMP 4.5:
34749 # pragma omp critical [(name) [hint(expression)]] new-line
34750 structured-block */
34751
34752 #define OMP_CRITICAL_CLAUSE_MASK \
34753 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34754
34755 static tree
34756 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34757 {
34758 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34759
34760 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34761 {
34762 matching_parens parens;
34763 parens.consume_open (parser);
34764
34765 name = cp_parser_identifier (parser);
34766
34767 if (name == error_mark_node
34768 || !parens.require_close (parser))
34769 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34770 /*or_comma=*/false,
34771 /*consume_paren=*/true);
34772 if (name == error_mark_node)
34773 name = NULL;
34774
34775 clauses = cp_parser_omp_all_clauses (parser,
34776 OMP_CRITICAL_CLAUSE_MASK,
34777 "#pragma omp critical", pragma_tok);
34778 }
34779 else
34780 cp_parser_require_pragma_eol (parser, pragma_tok);
34781
34782 stmt = cp_parser_omp_structured_block (parser, if_p);
34783 return c_finish_omp_critical (input_location, stmt, name, clauses);
34784 }
34785
34786 /* OpenMP 2.5:
34787 # pragma omp flush flush-vars[opt] new-line
34788
34789 flush-vars:
34790 ( variable-list ) */
34791
34792 static void
34793 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34794 {
34795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34796 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34797 cp_parser_require_pragma_eol (parser, pragma_tok);
34798
34799 finish_omp_flush ();
34800 }
34801
34802 /* Helper function, to parse omp for increment expression. */
34803
34804 static tree
34805 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34806 {
34807 tree cond = cp_parser_binary_expression (parser, false, true,
34808 PREC_NOT_OPERATOR, NULL);
34809 if (cond == error_mark_node
34810 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34811 {
34812 cp_parser_skip_to_end_of_statement (parser);
34813 return error_mark_node;
34814 }
34815
34816 switch (TREE_CODE (cond))
34817 {
34818 case GT_EXPR:
34819 case GE_EXPR:
34820 case LT_EXPR:
34821 case LE_EXPR:
34822 break;
34823 case NE_EXPR:
34824 /* Fall through: OpenMP disallows NE_EXPR. */
34825 gcc_fallthrough ();
34826 default:
34827 return error_mark_node;
34828 }
34829
34830 /* If decl is an iterator, preserve LHS and RHS of the relational
34831 expr until finish_omp_for. */
34832 if (decl
34833 && (type_dependent_expression_p (decl)
34834 || CLASS_TYPE_P (TREE_TYPE (decl))))
34835 return cond;
34836
34837 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
34838 TREE_CODE (cond),
34839 TREE_OPERAND (cond, 0), ERROR_MARK,
34840 TREE_OPERAND (cond, 1), ERROR_MARK,
34841 /*overload=*/NULL, tf_warning_or_error);
34842 }
34843
34844 /* Helper function, to parse omp for increment expression. */
34845
34846 static tree
34847 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34848 {
34849 cp_token *token = cp_lexer_peek_token (parser->lexer);
34850 enum tree_code op;
34851 tree lhs, rhs;
34852 cp_id_kind idk;
34853 bool decl_first;
34854
34855 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34856 {
34857 op = (token->type == CPP_PLUS_PLUS
34858 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34859 cp_lexer_consume_token (parser->lexer);
34860 lhs = cp_parser_simple_cast_expression (parser);
34861 if (lhs != decl
34862 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34863 return error_mark_node;
34864 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34865 }
34866
34867 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34868 if (lhs != decl
34869 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34870 return error_mark_node;
34871
34872 token = cp_lexer_peek_token (parser->lexer);
34873 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34874 {
34875 op = (token->type == CPP_PLUS_PLUS
34876 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34877 cp_lexer_consume_token (parser->lexer);
34878 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34879 }
34880
34881 op = cp_parser_assignment_operator_opt (parser);
34882 if (op == ERROR_MARK)
34883 return error_mark_node;
34884
34885 if (op != NOP_EXPR)
34886 {
34887 rhs = cp_parser_assignment_expression (parser);
34888 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34889 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34890 }
34891
34892 lhs = cp_parser_binary_expression (parser, false, false,
34893 PREC_ADDITIVE_EXPRESSION, NULL);
34894 token = cp_lexer_peek_token (parser->lexer);
34895 decl_first = (lhs == decl
34896 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34897 if (decl_first)
34898 lhs = NULL_TREE;
34899 if (token->type != CPP_PLUS
34900 && token->type != CPP_MINUS)
34901 return error_mark_node;
34902
34903 do
34904 {
34905 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34906 cp_lexer_consume_token (parser->lexer);
34907 rhs = cp_parser_binary_expression (parser, false, false,
34908 PREC_ADDITIVE_EXPRESSION, NULL);
34909 token = cp_lexer_peek_token (parser->lexer);
34910 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34911 {
34912 if (lhs == NULL_TREE)
34913 {
34914 if (op == PLUS_EXPR)
34915 lhs = rhs;
34916 else
34917 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34918 tf_warning_or_error);
34919 }
34920 else
34921 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34922 ERROR_MARK, NULL, tf_warning_or_error);
34923 }
34924 }
34925 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34926
34927 if (!decl_first)
34928 {
34929 if ((rhs != decl
34930 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34931 || op == MINUS_EXPR)
34932 return error_mark_node;
34933 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34934 }
34935 else
34936 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34937
34938 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34939 }
34940
34941 /* Parse the initialization statement of an OpenMP for loop.
34942
34943 Return true if the resulting construct should have an
34944 OMP_CLAUSE_PRIVATE added to it. */
34945
34946 static tree
34947 cp_parser_omp_for_loop_init (cp_parser *parser,
34948 tree &this_pre_body,
34949 vec<tree, va_gc> *&for_block,
34950 tree &init,
34951 tree &orig_init,
34952 tree &decl,
34953 tree &real_decl)
34954 {
34955 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34956 return NULL_TREE;
34957
34958 tree add_private_clause = NULL_TREE;
34959
34960 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34961
34962 init-expr:
34963 var = lb
34964 integer-type var = lb
34965 random-access-iterator-type var = lb
34966 pointer-type var = lb
34967 */
34968 cp_decl_specifier_seq type_specifiers;
34969
34970 /* First, try to parse as an initialized declaration. See
34971 cp_parser_condition, from whence the bulk of this is copied. */
34972
34973 cp_parser_parse_tentatively (parser);
34974 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34975 /*is_trailing_return=*/false,
34976 &type_specifiers);
34977 if (cp_parser_parse_definitely (parser))
34978 {
34979 /* If parsing a type specifier seq succeeded, then this
34980 MUST be a initialized declaration. */
34981 tree asm_specification, attributes;
34982 cp_declarator *declarator;
34983
34984 declarator = cp_parser_declarator (parser,
34985 CP_PARSER_DECLARATOR_NAMED,
34986 /*ctor_dtor_or_conv_p=*/NULL,
34987 /*parenthesized_p=*/NULL,
34988 /*member_p=*/false,
34989 /*friend_p=*/false);
34990 attributes = cp_parser_attributes_opt (parser);
34991 asm_specification = cp_parser_asm_specification_opt (parser);
34992
34993 if (declarator == cp_error_declarator)
34994 cp_parser_skip_to_end_of_statement (parser);
34995
34996 else
34997 {
34998 tree pushed_scope, auto_node;
34999
35000 decl = start_decl (declarator, &type_specifiers,
35001 SD_INITIALIZED, attributes,
35002 /*prefix_attributes=*/NULL_TREE,
35003 &pushed_scope);
35004
35005 auto_node = type_uses_auto (TREE_TYPE (decl));
35006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35007 {
35008 if (cp_lexer_next_token_is (parser->lexer,
35009 CPP_OPEN_PAREN))
35010 error ("parenthesized initialization is not allowed in "
35011 "OpenMP %<for%> loop");
35012 else
35013 /* Trigger an error. */
35014 cp_parser_require (parser, CPP_EQ, RT_EQ);
35015
35016 init = error_mark_node;
35017 cp_parser_skip_to_end_of_statement (parser);
35018 }
35019 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35020 || type_dependent_expression_p (decl)
35021 || auto_node)
35022 {
35023 bool is_direct_init, is_non_constant_init;
35024
35025 init = cp_parser_initializer (parser,
35026 &is_direct_init,
35027 &is_non_constant_init);
35028
35029 if (auto_node)
35030 {
35031 TREE_TYPE (decl)
35032 = do_auto_deduction (TREE_TYPE (decl), init,
35033 auto_node);
35034
35035 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35036 && !type_dependent_expression_p (decl))
35037 goto non_class;
35038 }
35039
35040 cp_finish_decl (decl, init, !is_non_constant_init,
35041 asm_specification,
35042 LOOKUP_ONLYCONVERTING);
35043 orig_init = init;
35044 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35045 {
35046 vec_safe_push (for_block, this_pre_body);
35047 init = NULL_TREE;
35048 }
35049 else
35050 {
35051 init = pop_stmt_list (this_pre_body);
35052 if (init && TREE_CODE (init) == STATEMENT_LIST)
35053 {
35054 tree_stmt_iterator i = tsi_start (init);
35055 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35056 while (!tsi_end_p (i))
35057 {
35058 tree t = tsi_stmt (i);
35059 if (TREE_CODE (t) == DECL_EXPR
35060 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35061 {
35062 tsi_delink (&i);
35063 vec_safe_push (for_block, t);
35064 continue;
35065 }
35066 break;
35067 }
35068 if (tsi_one_before_end_p (i))
35069 {
35070 tree t = tsi_stmt (i);
35071 tsi_delink (&i);
35072 free_stmt_list (init);
35073 init = t;
35074 }
35075 }
35076 }
35077 this_pre_body = NULL_TREE;
35078 }
35079 else
35080 {
35081 /* Consume '='. */
35082 cp_lexer_consume_token (parser->lexer);
35083 init = cp_parser_assignment_expression (parser);
35084
35085 non_class:
35086 if (TYPE_REF_P (TREE_TYPE (decl)))
35087 init = error_mark_node;
35088 else
35089 cp_finish_decl (decl, NULL_TREE,
35090 /*init_const_expr_p=*/false,
35091 asm_specification,
35092 LOOKUP_ONLYCONVERTING);
35093 }
35094
35095 if (pushed_scope)
35096 pop_scope (pushed_scope);
35097 }
35098 }
35099 else
35100 {
35101 cp_id_kind idk;
35102 /* If parsing a type specifier sequence failed, then
35103 this MUST be a simple expression. */
35104 cp_parser_parse_tentatively (parser);
35105 decl = cp_parser_primary_expression (parser, false, false,
35106 false, &idk);
35107 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35108 if (!cp_parser_error_occurred (parser)
35109 && decl
35110 && (TREE_CODE (decl) == COMPONENT_REF
35111 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35112 {
35113 cp_parser_abort_tentative_parse (parser);
35114 cp_parser_parse_tentatively (parser);
35115 cp_token *token = cp_lexer_peek_token (parser->lexer);
35116 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35117 /*check_dependency_p=*/true,
35118 /*template_p=*/NULL,
35119 /*declarator_p=*/false,
35120 /*optional_p=*/false);
35121 if (name != error_mark_node
35122 && last_tok == cp_lexer_peek_token (parser->lexer))
35123 {
35124 decl = cp_parser_lookup_name_simple (parser, name,
35125 token->location);
35126 if (TREE_CODE (decl) == FIELD_DECL)
35127 add_private_clause = omp_privatize_field (decl, false);
35128 }
35129 cp_parser_abort_tentative_parse (parser);
35130 cp_parser_parse_tentatively (parser);
35131 decl = cp_parser_primary_expression (parser, false, false,
35132 false, &idk);
35133 }
35134 if (!cp_parser_error_occurred (parser)
35135 && decl
35136 && DECL_P (decl)
35137 && CLASS_TYPE_P (TREE_TYPE (decl)))
35138 {
35139 tree rhs;
35140
35141 cp_parser_parse_definitely (parser);
35142 cp_parser_require (parser, CPP_EQ, RT_EQ);
35143 rhs = cp_parser_assignment_expression (parser);
35144 orig_init = rhs;
35145 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35146 decl, NOP_EXPR,
35147 rhs,
35148 tf_warning_or_error));
35149 if (!add_private_clause)
35150 add_private_clause = decl;
35151 }
35152 else
35153 {
35154 decl = NULL;
35155 cp_parser_abort_tentative_parse (parser);
35156 init = cp_parser_expression (parser);
35157 if (init)
35158 {
35159 if (TREE_CODE (init) == MODIFY_EXPR
35160 || TREE_CODE (init) == MODOP_EXPR)
35161 real_decl = TREE_OPERAND (init, 0);
35162 }
35163 }
35164 }
35165 return add_private_clause;
35166 }
35167
35168 /* Parse the restricted form of the for statement allowed by OpenMP. */
35169
35170 static tree
35171 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35172 tree *cclauses, bool *if_p)
35173 {
35174 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35175 tree real_decl, initv, condv, incrv, declv;
35176 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35177 location_t loc_first;
35178 bool collapse_err = false;
35179 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35180 vec<tree, va_gc> *for_block = make_tree_vector ();
35181 auto_vec<tree, 4> orig_inits;
35182 bool tiling = false;
35183
35184 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35185 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35186 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35187 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35188 {
35189 tiling = true;
35190 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35191 }
35192 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35193 && OMP_CLAUSE_ORDERED_EXPR (cl))
35194 {
35195 ordered_cl = cl;
35196 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35197 }
35198
35199 if (ordered && ordered < collapse)
35200 {
35201 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35202 "%<ordered%> clause parameter is less than %<collapse%>");
35203 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35204 = build_int_cst (NULL_TREE, collapse);
35205 ordered = collapse;
35206 }
35207 if (ordered)
35208 {
35209 for (tree *pc = &clauses; *pc; )
35210 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35211 {
35212 error_at (OMP_CLAUSE_LOCATION (*pc),
35213 "%<linear%> clause may not be specified together "
35214 "with %<ordered%> clause with a parameter");
35215 *pc = OMP_CLAUSE_CHAIN (*pc);
35216 }
35217 else
35218 pc = &OMP_CLAUSE_CHAIN (*pc);
35219 }
35220
35221 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35222 count = ordered ? ordered : collapse;
35223
35224 declv = make_tree_vec (count);
35225 initv = make_tree_vec (count);
35226 condv = make_tree_vec (count);
35227 incrv = make_tree_vec (count);
35228
35229 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35230
35231 for (i = 0; i < count; i++)
35232 {
35233 int bracecount = 0;
35234 tree add_private_clause = NULL_TREE;
35235 location_t loc;
35236
35237 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35238 {
35239 if (!collapse_err)
35240 cp_parser_error (parser, "for statement expected");
35241 return NULL;
35242 }
35243 loc = cp_lexer_consume_token (parser->lexer)->location;
35244
35245 matching_parens parens;
35246 if (!parens.require_open (parser))
35247 return NULL;
35248
35249 init = orig_init = decl = real_decl = NULL;
35250 this_pre_body = push_stmt_list ();
35251
35252 add_private_clause
35253 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
35254 init, orig_init, decl, real_decl);
35255
35256 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35257 if (this_pre_body)
35258 {
35259 this_pre_body = pop_stmt_list (this_pre_body);
35260 if (pre_body)
35261 {
35262 tree t = pre_body;
35263 pre_body = push_stmt_list ();
35264 add_stmt (t);
35265 add_stmt (this_pre_body);
35266 pre_body = pop_stmt_list (pre_body);
35267 }
35268 else
35269 pre_body = this_pre_body;
35270 }
35271
35272 if (decl)
35273 real_decl = decl;
35274 if (cclauses != NULL
35275 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35276 && real_decl != NULL_TREE)
35277 {
35278 tree *c;
35279 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35280 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35281 && OMP_CLAUSE_DECL (*c) == real_decl)
35282 {
35283 error_at (loc, "iteration variable %qD"
35284 " should not be firstprivate", real_decl);
35285 *c = OMP_CLAUSE_CHAIN (*c);
35286 }
35287 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35288 && OMP_CLAUSE_DECL (*c) == real_decl)
35289 {
35290 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35291 tree l = *c;
35292 *c = OMP_CLAUSE_CHAIN (*c);
35293 if (code == OMP_SIMD)
35294 {
35295 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35296 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35297 }
35298 else
35299 {
35300 OMP_CLAUSE_CHAIN (l) = clauses;
35301 clauses = l;
35302 }
35303 add_private_clause = NULL_TREE;
35304 }
35305 else
35306 {
35307 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35308 && OMP_CLAUSE_DECL (*c) == real_decl)
35309 add_private_clause = NULL_TREE;
35310 c = &OMP_CLAUSE_CHAIN (*c);
35311 }
35312 }
35313
35314 if (add_private_clause)
35315 {
35316 tree c;
35317 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35318 {
35319 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35320 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35321 && OMP_CLAUSE_DECL (c) == decl)
35322 break;
35323 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35324 && OMP_CLAUSE_DECL (c) == decl)
35325 error_at (loc, "iteration variable %qD "
35326 "should not be firstprivate",
35327 decl);
35328 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35329 && OMP_CLAUSE_DECL (c) == decl)
35330 error_at (loc, "iteration variable %qD should not be reduction",
35331 decl);
35332 }
35333 if (c == NULL)
35334 {
35335 if (code != OMP_SIMD)
35336 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35337 else if (collapse == 1)
35338 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35339 else
35340 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35341 OMP_CLAUSE_DECL (c) = add_private_clause;
35342 c = finish_omp_clauses (c, C_ORT_OMP);
35343 if (c)
35344 {
35345 OMP_CLAUSE_CHAIN (c) = clauses;
35346 clauses = c;
35347 /* For linear, signal that we need to fill up
35348 the so far unknown linear step. */
35349 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35350 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35351 }
35352 }
35353 }
35354
35355 cond = NULL;
35356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35357 cond = cp_parser_omp_for_cond (parser, decl);
35358 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35359
35360 incr = NULL;
35361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35362 {
35363 /* If decl is an iterator, preserve the operator on decl
35364 until finish_omp_for. */
35365 if (real_decl
35366 && ((processing_template_decl
35367 && (TREE_TYPE (real_decl) == NULL_TREE
35368 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
35369 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35370 incr = cp_parser_omp_for_incr (parser, real_decl);
35371 else
35372 incr = cp_parser_expression (parser);
35373 if (!EXPR_HAS_LOCATION (incr))
35374 protected_set_expr_location (incr, input_location);
35375 }
35376
35377 if (!parens.require_close (parser))
35378 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35379 /*or_comma=*/false,
35380 /*consume_paren=*/true);
35381
35382 TREE_VEC_ELT (declv, i) = decl;
35383 TREE_VEC_ELT (initv, i) = init;
35384 TREE_VEC_ELT (condv, i) = cond;
35385 TREE_VEC_ELT (incrv, i) = incr;
35386 if (orig_init)
35387 {
35388 orig_inits.safe_grow_cleared (i + 1);
35389 orig_inits[i] = orig_init;
35390 }
35391
35392 if (i == count - 1)
35393 break;
35394
35395 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35396 in between the collapsed for loops to be still considered perfectly
35397 nested. Hopefully the final version clarifies this.
35398 For now handle (multiple) {'s and empty statements. */
35399 cp_parser_parse_tentatively (parser);
35400 for (;;)
35401 {
35402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35403 break;
35404 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35405 {
35406 cp_lexer_consume_token (parser->lexer);
35407 bracecount++;
35408 }
35409 else if (bracecount
35410 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35411 cp_lexer_consume_token (parser->lexer);
35412 else
35413 {
35414 loc = cp_lexer_peek_token (parser->lexer)->location;
35415 error_at (loc, "not enough for loops to collapse");
35416 collapse_err = true;
35417 cp_parser_abort_tentative_parse (parser);
35418 declv = NULL_TREE;
35419 break;
35420 }
35421 }
35422
35423 if (declv)
35424 {
35425 cp_parser_parse_definitely (parser);
35426 nbraces += bracecount;
35427 }
35428 }
35429
35430 if (nbraces)
35431 if_p = NULL;
35432
35433 /* Note that we saved the original contents of this flag when we entered
35434 the structured block, and so we don't need to re-save it here. */
35435 parser->in_statement = IN_OMP_FOR;
35436
35437 /* Note that the grammar doesn't call for a structured block here,
35438 though the loop as a whole is a structured block. */
35439 body = push_stmt_list ();
35440 cp_parser_statement (parser, NULL_TREE, false, if_p);
35441 body = pop_stmt_list (body);
35442
35443 if (declv == NULL_TREE)
35444 ret = NULL_TREE;
35445 else
35446 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35447 body, pre_body, &orig_inits, clauses);
35448
35449 while (nbraces)
35450 {
35451 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35452 {
35453 cp_lexer_consume_token (parser->lexer);
35454 nbraces--;
35455 }
35456 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35457 cp_lexer_consume_token (parser->lexer);
35458 else
35459 {
35460 if (!collapse_err)
35461 {
35462 error_at (cp_lexer_peek_token (parser->lexer)->location,
35463 "collapsed loops not perfectly nested");
35464 }
35465 collapse_err = true;
35466 cp_parser_statement_seq_opt (parser, NULL);
35467 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35468 break;
35469 }
35470 }
35471
35472 while (!for_block->is_empty ())
35473 {
35474 tree t = for_block->pop ();
35475 if (TREE_CODE (t) == STATEMENT_LIST)
35476 add_stmt (pop_stmt_list (t));
35477 else
35478 add_stmt (t);
35479 }
35480 release_tree_vector (for_block);
35481
35482 return ret;
35483 }
35484
35485 /* Helper function for OpenMP parsing, split clauses and call
35486 finish_omp_clauses on each of the set of clauses afterwards. */
35487
35488 static void
35489 cp_omp_split_clauses (location_t loc, enum tree_code code,
35490 omp_clause_mask mask, tree clauses, tree *cclauses)
35491 {
35492 int i;
35493 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35494 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35495 if (cclauses[i])
35496 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35497 }
35498
35499 /* OpenMP 4.0:
35500 #pragma omp simd simd-clause[optseq] new-line
35501 for-loop */
35502
35503 #define OMP_SIMD_CLAUSE_MASK \
35504 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35512
35513 static tree
35514 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35515 char *p_name, omp_clause_mask mask, tree *cclauses,
35516 bool *if_p)
35517 {
35518 tree clauses, sb, ret;
35519 unsigned int save;
35520 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35521
35522 strcat (p_name, " simd");
35523 mask |= OMP_SIMD_CLAUSE_MASK;
35524
35525 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35526 cclauses == NULL);
35527 if (cclauses)
35528 {
35529 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35530 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35531 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35532 OMP_CLAUSE_ORDERED);
35533 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35534 {
35535 error_at (OMP_CLAUSE_LOCATION (c),
35536 "%<ordered%> clause with parameter may not be specified "
35537 "on %qs construct", p_name);
35538 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35539 }
35540 }
35541
35542 sb = begin_omp_structured_block ();
35543 save = cp_parser_begin_omp_structured_block (parser);
35544
35545 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35546
35547 cp_parser_end_omp_structured_block (parser, save);
35548 add_stmt (finish_omp_structured_block (sb));
35549
35550 return ret;
35551 }
35552
35553 /* OpenMP 2.5:
35554 #pragma omp for for-clause[optseq] new-line
35555 for-loop
35556
35557 OpenMP 4.0:
35558 #pragma omp for simd for-simd-clause[optseq] new-line
35559 for-loop */
35560
35561 #define OMP_FOR_CLAUSE_MASK \
35562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35571
35572 static tree
35573 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35574 char *p_name, omp_clause_mask mask, tree *cclauses,
35575 bool *if_p)
35576 {
35577 tree clauses, sb, ret;
35578 unsigned int save;
35579 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35580
35581 strcat (p_name, " for");
35582 mask |= OMP_FOR_CLAUSE_MASK;
35583 /* parallel for{, simd} disallows nowait clause, but for
35584 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35585 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35586 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35587 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35588 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35589 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35590
35591 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35592 {
35593 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35594 const char *p = IDENTIFIER_POINTER (id);
35595
35596 if (strcmp (p, "simd") == 0)
35597 {
35598 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35599 if (cclauses == NULL)
35600 cclauses = cclauses_buf;
35601
35602 cp_lexer_consume_token (parser->lexer);
35603 if (!flag_openmp) /* flag_openmp_simd */
35604 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35605 cclauses, if_p);
35606 sb = begin_omp_structured_block ();
35607 save = cp_parser_begin_omp_structured_block (parser);
35608 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35609 cclauses, if_p);
35610 cp_parser_end_omp_structured_block (parser, save);
35611 tree body = finish_omp_structured_block (sb);
35612 if (ret == NULL)
35613 return ret;
35614 ret = make_node (OMP_FOR);
35615 TREE_TYPE (ret) = void_type_node;
35616 OMP_FOR_BODY (ret) = body;
35617 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35618 SET_EXPR_LOCATION (ret, loc);
35619 add_stmt (ret);
35620 return ret;
35621 }
35622 }
35623 if (!flag_openmp) /* flag_openmp_simd */
35624 {
35625 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35626 return NULL_TREE;
35627 }
35628
35629 /* Composite distribute parallel for disallows linear clause. */
35630 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35631 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35632
35633 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35634 cclauses == NULL);
35635 if (cclauses)
35636 {
35637 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35638 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35639 }
35640
35641 sb = begin_omp_structured_block ();
35642 save = cp_parser_begin_omp_structured_block (parser);
35643
35644 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35645
35646 cp_parser_end_omp_structured_block (parser, save);
35647 add_stmt (finish_omp_structured_block (sb));
35648
35649 return ret;
35650 }
35651
35652 /* OpenMP 2.5:
35653 # pragma omp master new-line
35654 structured-block */
35655
35656 static tree
35657 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35658 {
35659 cp_parser_require_pragma_eol (parser, pragma_tok);
35660 return c_finish_omp_master (input_location,
35661 cp_parser_omp_structured_block (parser, if_p));
35662 }
35663
35664 /* OpenMP 2.5:
35665 # pragma omp ordered new-line
35666 structured-block
35667
35668 OpenMP 4.5:
35669 # pragma omp ordered ordered-clauses new-line
35670 structured-block */
35671
35672 #define OMP_ORDERED_CLAUSE_MASK \
35673 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35675
35676 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35677 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35678
35679 static bool
35680 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35681 enum pragma_context context, bool *if_p)
35682 {
35683 location_t loc = pragma_tok->location;
35684
35685 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35686 {
35687 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35688 const char *p = IDENTIFIER_POINTER (id);
35689
35690 if (strcmp (p, "depend") == 0)
35691 {
35692 if (!flag_openmp) /* flag_openmp_simd */
35693 {
35694 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35695 return false;
35696 }
35697 if (context == pragma_stmt)
35698 {
35699 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35700 "%<depend%> clause may only be used in compound "
35701 "statements");
35702 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35703 return false;
35704 }
35705 tree clauses
35706 = cp_parser_omp_all_clauses (parser,
35707 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35708 "#pragma omp ordered", pragma_tok);
35709 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35710 return false;
35711 }
35712 }
35713
35714 tree clauses
35715 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35716 "#pragma omp ordered", pragma_tok);
35717
35718 if (!flag_openmp /* flag_openmp_simd */
35719 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35720 return false;
35721
35722 c_finish_omp_ordered (loc, clauses,
35723 cp_parser_omp_structured_block (parser, if_p));
35724 return true;
35725 }
35726
35727 /* OpenMP 2.5:
35728
35729 section-scope:
35730 { section-sequence }
35731
35732 section-sequence:
35733 section-directive[opt] structured-block
35734 section-sequence section-directive structured-block */
35735
35736 static tree
35737 cp_parser_omp_sections_scope (cp_parser *parser)
35738 {
35739 tree stmt, substmt;
35740 bool error_suppress = false;
35741 cp_token *tok;
35742
35743 matching_braces braces;
35744 if (!braces.require_open (parser))
35745 return NULL_TREE;
35746
35747 stmt = push_stmt_list ();
35748
35749 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35750 != PRAGMA_OMP_SECTION)
35751 {
35752 substmt = cp_parser_omp_structured_block (parser, NULL);
35753 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35754 add_stmt (substmt);
35755 }
35756
35757 while (1)
35758 {
35759 tok = cp_lexer_peek_token (parser->lexer);
35760 if (tok->type == CPP_CLOSE_BRACE)
35761 break;
35762 if (tok->type == CPP_EOF)
35763 break;
35764
35765 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35766 {
35767 cp_lexer_consume_token (parser->lexer);
35768 cp_parser_require_pragma_eol (parser, tok);
35769 error_suppress = false;
35770 }
35771 else if (!error_suppress)
35772 {
35773 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35774 error_suppress = true;
35775 }
35776
35777 substmt = cp_parser_omp_structured_block (parser, NULL);
35778 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35779 add_stmt (substmt);
35780 }
35781 braces.require_close (parser);
35782
35783 substmt = pop_stmt_list (stmt);
35784
35785 stmt = make_node (OMP_SECTIONS);
35786 TREE_TYPE (stmt) = void_type_node;
35787 OMP_SECTIONS_BODY (stmt) = substmt;
35788
35789 add_stmt (stmt);
35790 return stmt;
35791 }
35792
35793 /* OpenMP 2.5:
35794 # pragma omp sections sections-clause[optseq] newline
35795 sections-scope */
35796
35797 #define OMP_SECTIONS_CLAUSE_MASK \
35798 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35803
35804 static tree
35805 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35806 char *p_name, omp_clause_mask mask, tree *cclauses)
35807 {
35808 tree clauses, ret;
35809 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35810
35811 strcat (p_name, " sections");
35812 mask |= OMP_SECTIONS_CLAUSE_MASK;
35813 if (cclauses)
35814 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35815
35816 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35817 cclauses == NULL);
35818 if (cclauses)
35819 {
35820 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35821 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35822 }
35823
35824 ret = cp_parser_omp_sections_scope (parser);
35825 if (ret)
35826 OMP_SECTIONS_CLAUSES (ret) = clauses;
35827
35828 return ret;
35829 }
35830
35831 /* OpenMP 2.5:
35832 # pragma omp parallel parallel-clause[optseq] new-line
35833 structured-block
35834 # pragma omp parallel for parallel-for-clause[optseq] new-line
35835 structured-block
35836 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35837 structured-block
35838
35839 OpenMP 4.0:
35840 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35841 structured-block */
35842
35843 #define OMP_PARALLEL_CLAUSE_MASK \
35844 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35853
35854 static tree
35855 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35856 char *p_name, omp_clause_mask mask, tree *cclauses,
35857 bool *if_p)
35858 {
35859 tree stmt, clauses, block;
35860 unsigned int save;
35861 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35862
35863 strcat (p_name, " parallel");
35864 mask |= OMP_PARALLEL_CLAUSE_MASK;
35865 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35866 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35867 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35868 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35869
35870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35871 {
35872 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35873 if (cclauses == NULL)
35874 cclauses = cclauses_buf;
35875
35876 cp_lexer_consume_token (parser->lexer);
35877 if (!flag_openmp) /* flag_openmp_simd */
35878 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35879 if_p);
35880 block = begin_omp_parallel ();
35881 save = cp_parser_begin_omp_structured_block (parser);
35882 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35883 if_p);
35884 cp_parser_end_omp_structured_block (parser, save);
35885 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35886 block);
35887 if (ret == NULL_TREE)
35888 return ret;
35889 OMP_PARALLEL_COMBINED (stmt) = 1;
35890 return stmt;
35891 }
35892 /* When combined with distribute, parallel has to be followed by for.
35893 #pragma omp target parallel is allowed though. */
35894 else if (cclauses
35895 && (mask & (OMP_CLAUSE_MASK_1
35896 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35897 {
35898 error_at (loc, "expected %<for%> after %qs", p_name);
35899 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35900 return NULL_TREE;
35901 }
35902 else if (!flag_openmp) /* flag_openmp_simd */
35903 {
35904 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35905 return NULL_TREE;
35906 }
35907 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35908 {
35909 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35910 const char *p = IDENTIFIER_POINTER (id);
35911 if (strcmp (p, "sections") == 0)
35912 {
35913 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35914 cclauses = cclauses_buf;
35915
35916 cp_lexer_consume_token (parser->lexer);
35917 block = begin_omp_parallel ();
35918 save = cp_parser_begin_omp_structured_block (parser);
35919 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35920 cp_parser_end_omp_structured_block (parser, save);
35921 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35922 block);
35923 OMP_PARALLEL_COMBINED (stmt) = 1;
35924 return stmt;
35925 }
35926 }
35927
35928 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35929 cclauses == NULL);
35930 if (cclauses)
35931 {
35932 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35933 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35934 }
35935
35936 block = begin_omp_parallel ();
35937 save = cp_parser_begin_omp_structured_block (parser);
35938 cp_parser_statement (parser, NULL_TREE, false, if_p);
35939 cp_parser_end_omp_structured_block (parser, save);
35940 stmt = finish_omp_parallel (clauses, block);
35941 return stmt;
35942 }
35943
35944 /* OpenMP 2.5:
35945 # pragma omp single single-clause[optseq] new-line
35946 structured-block */
35947
35948 #define OMP_SINGLE_CLAUSE_MASK \
35949 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35951 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35952 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35953
35954 static tree
35955 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35956 {
35957 tree stmt = make_node (OMP_SINGLE);
35958 TREE_TYPE (stmt) = void_type_node;
35959
35960 OMP_SINGLE_CLAUSES (stmt)
35961 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35962 "#pragma omp single", pragma_tok);
35963 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35964
35965 return add_stmt (stmt);
35966 }
35967
35968 /* OpenMP 3.0:
35969 # pragma omp task task-clause[optseq] new-line
35970 structured-block */
35971
35972 #define OMP_TASK_CLAUSE_MASK \
35973 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35983
35984 static tree
35985 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35986 {
35987 tree clauses, block;
35988 unsigned int save;
35989
35990 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35991 "#pragma omp task", pragma_tok);
35992 block = begin_omp_task ();
35993 save = cp_parser_begin_omp_structured_block (parser);
35994 cp_parser_statement (parser, NULL_TREE, false, if_p);
35995 cp_parser_end_omp_structured_block (parser, save);
35996 return finish_omp_task (clauses, block);
35997 }
35998
35999 /* OpenMP 3.0:
36000 # pragma omp taskwait new-line */
36001
36002 static void
36003 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
36004 {
36005 cp_parser_require_pragma_eol (parser, pragma_tok);
36006 finish_omp_taskwait ();
36007 }
36008
36009 /* OpenMP 3.1:
36010 # pragma omp taskyield new-line */
36011
36012 static void
36013 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
36014 {
36015 cp_parser_require_pragma_eol (parser, pragma_tok);
36016 finish_omp_taskyield ();
36017 }
36018
36019 /* OpenMP 4.0:
36020 # pragma omp taskgroup new-line
36021 structured-block */
36022
36023 static tree
36024 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36025 {
36026 cp_parser_require_pragma_eol (parser, pragma_tok);
36027 return c_finish_omp_taskgroup (input_location,
36028 cp_parser_omp_structured_block (parser,
36029 if_p));
36030 }
36031
36032
36033 /* OpenMP 2.5:
36034 # pragma omp threadprivate (variable-list) */
36035
36036 static void
36037 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
36038 {
36039 tree vars;
36040
36041 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36042 cp_parser_require_pragma_eol (parser, pragma_tok);
36043
36044 finish_omp_threadprivate (vars);
36045 }
36046
36047 /* OpenMP 4.0:
36048 # pragma omp cancel cancel-clause[optseq] new-line */
36049
36050 #define OMP_CANCEL_CLAUSE_MASK \
36051 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
36055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
36056
36057 static void
36058 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
36059 {
36060 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
36061 "#pragma omp cancel", pragma_tok);
36062 finish_omp_cancel (clauses);
36063 }
36064
36065 /* OpenMP 4.0:
36066 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
36067
36068 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
36069 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
36072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
36073
36074 static void
36075 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
36076 enum pragma_context context)
36077 {
36078 tree clauses;
36079 bool point_seen = false;
36080
36081 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36082 {
36083 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36084 const char *p = IDENTIFIER_POINTER (id);
36085
36086 if (strcmp (p, "point") == 0)
36087 {
36088 cp_lexer_consume_token (parser->lexer);
36089 point_seen = true;
36090 }
36091 }
36092 if (!point_seen)
36093 {
36094 cp_parser_error (parser, "expected %<point%>");
36095 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36096 return;
36097 }
36098
36099 if (context != pragma_compound)
36100 {
36101 if (context == pragma_stmt)
36102 error_at (pragma_tok->location,
36103 "%<#pragma %s%> may only be used in compound statements",
36104 "omp cancellation point");
36105 else
36106 cp_parser_error (parser, "expected declaration specifiers");
36107 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36108 return;
36109 }
36110
36111 clauses = cp_parser_omp_all_clauses (parser,
36112 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36113 "#pragma omp cancellation point",
36114 pragma_tok);
36115 finish_omp_cancellation_point (clauses);
36116 }
36117
36118 /* OpenMP 4.0:
36119 #pragma omp distribute distribute-clause[optseq] new-line
36120 for-loop */
36121
36122 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36123 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36128
36129 static tree
36130 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36131 char *p_name, omp_clause_mask mask, tree *cclauses,
36132 bool *if_p)
36133 {
36134 tree clauses, sb, ret;
36135 unsigned int save;
36136 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36137
36138 strcat (p_name, " distribute");
36139 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36140
36141 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36142 {
36143 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36144 const char *p = IDENTIFIER_POINTER (id);
36145 bool simd = false;
36146 bool parallel = false;
36147
36148 if (strcmp (p, "simd") == 0)
36149 simd = true;
36150 else
36151 parallel = strcmp (p, "parallel") == 0;
36152 if (parallel || simd)
36153 {
36154 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36155 if (cclauses == NULL)
36156 cclauses = cclauses_buf;
36157 cp_lexer_consume_token (parser->lexer);
36158 if (!flag_openmp) /* flag_openmp_simd */
36159 {
36160 if (simd)
36161 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36162 cclauses, if_p);
36163 else
36164 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36165 cclauses, if_p);
36166 }
36167 sb = begin_omp_structured_block ();
36168 save = cp_parser_begin_omp_structured_block (parser);
36169 if (simd)
36170 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36171 cclauses, if_p);
36172 else
36173 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36174 cclauses, if_p);
36175 cp_parser_end_omp_structured_block (parser, save);
36176 tree body = finish_omp_structured_block (sb);
36177 if (ret == NULL)
36178 return ret;
36179 ret = make_node (OMP_DISTRIBUTE);
36180 TREE_TYPE (ret) = void_type_node;
36181 OMP_FOR_BODY (ret) = body;
36182 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36183 SET_EXPR_LOCATION (ret, loc);
36184 add_stmt (ret);
36185 return ret;
36186 }
36187 }
36188 if (!flag_openmp) /* flag_openmp_simd */
36189 {
36190 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36191 return NULL_TREE;
36192 }
36193
36194 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36195 cclauses == NULL);
36196 if (cclauses)
36197 {
36198 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36199 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36200 }
36201
36202 sb = begin_omp_structured_block ();
36203 save = cp_parser_begin_omp_structured_block (parser);
36204
36205 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36206
36207 cp_parser_end_omp_structured_block (parser, save);
36208 add_stmt (finish_omp_structured_block (sb));
36209
36210 return ret;
36211 }
36212
36213 /* OpenMP 4.0:
36214 # pragma omp teams teams-clause[optseq] new-line
36215 structured-block */
36216
36217 #define OMP_TEAMS_CLAUSE_MASK \
36218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36225
36226 static tree
36227 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36228 char *p_name, omp_clause_mask mask, tree *cclauses,
36229 bool *if_p)
36230 {
36231 tree clauses, sb, ret;
36232 unsigned int save;
36233 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36234
36235 strcat (p_name, " teams");
36236 mask |= OMP_TEAMS_CLAUSE_MASK;
36237
36238 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36239 {
36240 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36241 const char *p = IDENTIFIER_POINTER (id);
36242 if (strcmp (p, "distribute") == 0)
36243 {
36244 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36245 if (cclauses == NULL)
36246 cclauses = cclauses_buf;
36247
36248 cp_lexer_consume_token (parser->lexer);
36249 if (!flag_openmp) /* flag_openmp_simd */
36250 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36251 cclauses, if_p);
36252 sb = begin_omp_structured_block ();
36253 save = cp_parser_begin_omp_structured_block (parser);
36254 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36255 cclauses, if_p);
36256 cp_parser_end_omp_structured_block (parser, save);
36257 tree body = finish_omp_structured_block (sb);
36258 if (ret == NULL)
36259 return ret;
36260 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36261 ret = make_node (OMP_TEAMS);
36262 TREE_TYPE (ret) = void_type_node;
36263 OMP_TEAMS_CLAUSES (ret) = clauses;
36264 OMP_TEAMS_BODY (ret) = body;
36265 OMP_TEAMS_COMBINED (ret) = 1;
36266 SET_EXPR_LOCATION (ret, loc);
36267 return add_stmt (ret);
36268 }
36269 }
36270 if (!flag_openmp) /* flag_openmp_simd */
36271 {
36272 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36273 return NULL_TREE;
36274 }
36275
36276 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36277 cclauses == NULL);
36278 if (cclauses)
36279 {
36280 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36281 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36282 }
36283
36284 tree stmt = make_node (OMP_TEAMS);
36285 TREE_TYPE (stmt) = void_type_node;
36286 OMP_TEAMS_CLAUSES (stmt) = clauses;
36287 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36288 SET_EXPR_LOCATION (stmt, loc);
36289
36290 return add_stmt (stmt);
36291 }
36292
36293 /* OpenMP 4.0:
36294 # pragma omp target data target-data-clause[optseq] new-line
36295 structured-block */
36296
36297 #define OMP_TARGET_DATA_CLAUSE_MASK \
36298 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36302
36303 static tree
36304 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36305 {
36306 tree clauses
36307 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36308 "#pragma omp target data", pragma_tok);
36309 int map_seen = 0;
36310 for (tree *pc = &clauses; *pc;)
36311 {
36312 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36313 switch (OMP_CLAUSE_MAP_KIND (*pc))
36314 {
36315 case GOMP_MAP_TO:
36316 case GOMP_MAP_ALWAYS_TO:
36317 case GOMP_MAP_FROM:
36318 case GOMP_MAP_ALWAYS_FROM:
36319 case GOMP_MAP_TOFROM:
36320 case GOMP_MAP_ALWAYS_TOFROM:
36321 case GOMP_MAP_ALLOC:
36322 map_seen = 3;
36323 break;
36324 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36325 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36326 case GOMP_MAP_ALWAYS_POINTER:
36327 break;
36328 default:
36329 map_seen |= 1;
36330 error_at (OMP_CLAUSE_LOCATION (*pc),
36331 "%<#pragma omp target data%> with map-type other "
36332 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36333 "on %<map%> clause");
36334 *pc = OMP_CLAUSE_CHAIN (*pc);
36335 continue;
36336 }
36337 pc = &OMP_CLAUSE_CHAIN (*pc);
36338 }
36339
36340 if (map_seen != 3)
36341 {
36342 if (map_seen == 0)
36343 error_at (pragma_tok->location,
36344 "%<#pragma omp target data%> must contain at least "
36345 "one %<map%> clause");
36346 return NULL_TREE;
36347 }
36348
36349 tree stmt = make_node (OMP_TARGET_DATA);
36350 TREE_TYPE (stmt) = void_type_node;
36351 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36352
36353 keep_next_level (true);
36354 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36355
36356 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36357 return add_stmt (stmt);
36358 }
36359
36360 /* OpenMP 4.5:
36361 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36362 structured-block */
36363
36364 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36365 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36370
36371 static tree
36372 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36373 enum pragma_context context)
36374 {
36375 bool data_seen = false;
36376 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36377 {
36378 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36379 const char *p = IDENTIFIER_POINTER (id);
36380
36381 if (strcmp (p, "data") == 0)
36382 {
36383 cp_lexer_consume_token (parser->lexer);
36384 data_seen = true;
36385 }
36386 }
36387 if (!data_seen)
36388 {
36389 cp_parser_error (parser, "expected %<data%>");
36390 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36391 return NULL_TREE;
36392 }
36393
36394 if (context == pragma_stmt)
36395 {
36396 error_at (pragma_tok->location,
36397 "%<#pragma %s%> may only be used in compound statements",
36398 "omp target enter data");
36399 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36400 return NULL_TREE;
36401 }
36402
36403 tree clauses
36404 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36405 "#pragma omp target enter data", pragma_tok);
36406 int map_seen = 0;
36407 for (tree *pc = &clauses; *pc;)
36408 {
36409 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36410 switch (OMP_CLAUSE_MAP_KIND (*pc))
36411 {
36412 case GOMP_MAP_TO:
36413 case GOMP_MAP_ALWAYS_TO:
36414 case GOMP_MAP_ALLOC:
36415 map_seen = 3;
36416 break;
36417 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36418 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36419 case GOMP_MAP_ALWAYS_POINTER:
36420 break;
36421 default:
36422 map_seen |= 1;
36423 error_at (OMP_CLAUSE_LOCATION (*pc),
36424 "%<#pragma omp target enter data%> with map-type other "
36425 "than %<to%> or %<alloc%> on %<map%> clause");
36426 *pc = OMP_CLAUSE_CHAIN (*pc);
36427 continue;
36428 }
36429 pc = &OMP_CLAUSE_CHAIN (*pc);
36430 }
36431
36432 if (map_seen != 3)
36433 {
36434 if (map_seen == 0)
36435 error_at (pragma_tok->location,
36436 "%<#pragma omp target enter data%> must contain at least "
36437 "one %<map%> clause");
36438 return NULL_TREE;
36439 }
36440
36441 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36442 TREE_TYPE (stmt) = void_type_node;
36443 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36444 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36445 return add_stmt (stmt);
36446 }
36447
36448 /* OpenMP 4.5:
36449 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36450 structured-block */
36451
36452 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36453 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36458
36459 static tree
36460 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36461 enum pragma_context context)
36462 {
36463 bool data_seen = false;
36464 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36465 {
36466 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36467 const char *p = IDENTIFIER_POINTER (id);
36468
36469 if (strcmp (p, "data") == 0)
36470 {
36471 cp_lexer_consume_token (parser->lexer);
36472 data_seen = true;
36473 }
36474 }
36475 if (!data_seen)
36476 {
36477 cp_parser_error (parser, "expected %<data%>");
36478 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36479 return NULL_TREE;
36480 }
36481
36482 if (context == pragma_stmt)
36483 {
36484 error_at (pragma_tok->location,
36485 "%<#pragma %s%> may only be used in compound statements",
36486 "omp target exit data");
36487 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36488 return NULL_TREE;
36489 }
36490
36491 tree clauses
36492 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36493 "#pragma omp target exit data", pragma_tok);
36494 int map_seen = 0;
36495 for (tree *pc = &clauses; *pc;)
36496 {
36497 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36498 switch (OMP_CLAUSE_MAP_KIND (*pc))
36499 {
36500 case GOMP_MAP_FROM:
36501 case GOMP_MAP_ALWAYS_FROM:
36502 case GOMP_MAP_RELEASE:
36503 case GOMP_MAP_DELETE:
36504 map_seen = 3;
36505 break;
36506 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36507 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36508 case GOMP_MAP_ALWAYS_POINTER:
36509 break;
36510 default:
36511 map_seen |= 1;
36512 error_at (OMP_CLAUSE_LOCATION (*pc),
36513 "%<#pragma omp target exit data%> with map-type other "
36514 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36515 " clause");
36516 *pc = OMP_CLAUSE_CHAIN (*pc);
36517 continue;
36518 }
36519 pc = &OMP_CLAUSE_CHAIN (*pc);
36520 }
36521
36522 if (map_seen != 3)
36523 {
36524 if (map_seen == 0)
36525 error_at (pragma_tok->location,
36526 "%<#pragma omp target exit data%> must contain at least "
36527 "one %<map%> clause");
36528 return NULL_TREE;
36529 }
36530
36531 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36532 TREE_TYPE (stmt) = void_type_node;
36533 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36534 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36535 return add_stmt (stmt);
36536 }
36537
36538 /* OpenMP 4.0:
36539 # pragma omp target update target-update-clause[optseq] new-line */
36540
36541 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36548
36549 static bool
36550 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36551 enum pragma_context context)
36552 {
36553 if (context == pragma_stmt)
36554 {
36555 error_at (pragma_tok->location,
36556 "%<#pragma %s%> may only be used in compound statements",
36557 "omp target update");
36558 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36559 return false;
36560 }
36561
36562 tree clauses
36563 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36564 "#pragma omp target update", pragma_tok);
36565 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36566 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36567 {
36568 error_at (pragma_tok->location,
36569 "%<#pragma omp target update%> must contain at least one "
36570 "%<from%> or %<to%> clauses");
36571 return false;
36572 }
36573
36574 tree stmt = make_node (OMP_TARGET_UPDATE);
36575 TREE_TYPE (stmt) = void_type_node;
36576 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36577 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36578 add_stmt (stmt);
36579 return false;
36580 }
36581
36582 /* OpenMP 4.0:
36583 # pragma omp target target-clause[optseq] new-line
36584 structured-block */
36585
36586 #define OMP_TARGET_CLAUSE_MASK \
36587 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36596
36597 static bool
36598 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36599 enum pragma_context context, bool *if_p)
36600 {
36601 tree *pc = NULL, stmt;
36602
36603 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36604 {
36605 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36606 const char *p = IDENTIFIER_POINTER (id);
36607 enum tree_code ccode = ERROR_MARK;
36608
36609 if (strcmp (p, "teams") == 0)
36610 ccode = OMP_TEAMS;
36611 else if (strcmp (p, "parallel") == 0)
36612 ccode = OMP_PARALLEL;
36613 else if (strcmp (p, "simd") == 0)
36614 ccode = OMP_SIMD;
36615 if (ccode != ERROR_MARK)
36616 {
36617 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36618 char p_name[sizeof ("#pragma omp target teams distribute "
36619 "parallel for simd")];
36620
36621 cp_lexer_consume_token (parser->lexer);
36622 strcpy (p_name, "#pragma omp target");
36623 if (!flag_openmp) /* flag_openmp_simd */
36624 {
36625 tree stmt;
36626 switch (ccode)
36627 {
36628 case OMP_TEAMS:
36629 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36630 OMP_TARGET_CLAUSE_MASK,
36631 cclauses, if_p);
36632 break;
36633 case OMP_PARALLEL:
36634 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36635 OMP_TARGET_CLAUSE_MASK,
36636 cclauses, if_p);
36637 break;
36638 case OMP_SIMD:
36639 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36640 OMP_TARGET_CLAUSE_MASK,
36641 cclauses, if_p);
36642 break;
36643 default:
36644 gcc_unreachable ();
36645 }
36646 return stmt != NULL_TREE;
36647 }
36648 keep_next_level (true);
36649 tree sb = begin_omp_structured_block (), ret;
36650 unsigned save = cp_parser_begin_omp_structured_block (parser);
36651 switch (ccode)
36652 {
36653 case OMP_TEAMS:
36654 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36655 OMP_TARGET_CLAUSE_MASK, cclauses,
36656 if_p);
36657 break;
36658 case OMP_PARALLEL:
36659 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36660 OMP_TARGET_CLAUSE_MASK, cclauses,
36661 if_p);
36662 break;
36663 case OMP_SIMD:
36664 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36665 OMP_TARGET_CLAUSE_MASK, cclauses,
36666 if_p);
36667 break;
36668 default:
36669 gcc_unreachable ();
36670 }
36671 cp_parser_end_omp_structured_block (parser, save);
36672 tree body = finish_omp_structured_block (sb);
36673 if (ret == NULL_TREE)
36674 return false;
36675 if (ccode == OMP_TEAMS && !processing_template_decl)
36676 {
36677 /* For combined target teams, ensure the num_teams and
36678 thread_limit clause expressions are evaluated on the host,
36679 before entering the target construct. */
36680 tree c;
36681 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36682 c; c = OMP_CLAUSE_CHAIN (c))
36683 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36684 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36685 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36686 {
36687 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36688 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36689 if (expr == error_mark_node)
36690 continue;
36691 tree tmp = TARGET_EXPR_SLOT (expr);
36692 add_stmt (expr);
36693 OMP_CLAUSE_OPERAND (c, 0) = expr;
36694 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36695 OMP_CLAUSE_FIRSTPRIVATE);
36696 OMP_CLAUSE_DECL (tc) = tmp;
36697 OMP_CLAUSE_CHAIN (tc)
36698 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36699 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36700 }
36701 }
36702 tree stmt = make_node (OMP_TARGET);
36703 TREE_TYPE (stmt) = void_type_node;
36704 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36705 OMP_TARGET_BODY (stmt) = body;
36706 OMP_TARGET_COMBINED (stmt) = 1;
36707 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36708 add_stmt (stmt);
36709 pc = &OMP_TARGET_CLAUSES (stmt);
36710 goto check_clauses;
36711 }
36712 else if (!flag_openmp) /* flag_openmp_simd */
36713 {
36714 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36715 return false;
36716 }
36717 else if (strcmp (p, "data") == 0)
36718 {
36719 cp_lexer_consume_token (parser->lexer);
36720 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36721 return true;
36722 }
36723 else if (strcmp (p, "enter") == 0)
36724 {
36725 cp_lexer_consume_token (parser->lexer);
36726 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36727 return false;
36728 }
36729 else if (strcmp (p, "exit") == 0)
36730 {
36731 cp_lexer_consume_token (parser->lexer);
36732 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36733 return false;
36734 }
36735 else if (strcmp (p, "update") == 0)
36736 {
36737 cp_lexer_consume_token (parser->lexer);
36738 return cp_parser_omp_target_update (parser, pragma_tok, context);
36739 }
36740 }
36741 if (!flag_openmp) /* flag_openmp_simd */
36742 {
36743 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36744 return false;
36745 }
36746
36747 stmt = make_node (OMP_TARGET);
36748 TREE_TYPE (stmt) = void_type_node;
36749
36750 OMP_TARGET_CLAUSES (stmt)
36751 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36752 "#pragma omp target", pragma_tok);
36753 pc = &OMP_TARGET_CLAUSES (stmt);
36754 keep_next_level (true);
36755 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36756
36757 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36758 add_stmt (stmt);
36759
36760 check_clauses:
36761 while (*pc)
36762 {
36763 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36764 switch (OMP_CLAUSE_MAP_KIND (*pc))
36765 {
36766 case GOMP_MAP_TO:
36767 case GOMP_MAP_ALWAYS_TO:
36768 case GOMP_MAP_FROM:
36769 case GOMP_MAP_ALWAYS_FROM:
36770 case GOMP_MAP_TOFROM:
36771 case GOMP_MAP_ALWAYS_TOFROM:
36772 case GOMP_MAP_ALLOC:
36773 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36774 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36775 case GOMP_MAP_ALWAYS_POINTER:
36776 break;
36777 default:
36778 error_at (OMP_CLAUSE_LOCATION (*pc),
36779 "%<#pragma omp target%> with map-type other "
36780 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36781 "on %<map%> clause");
36782 *pc = OMP_CLAUSE_CHAIN (*pc);
36783 continue;
36784 }
36785 pc = &OMP_CLAUSE_CHAIN (*pc);
36786 }
36787 return true;
36788 }
36789
36790 /* OpenACC 2.0:
36791 # pragma acc cache (variable-list) new-line
36792 */
36793
36794 static tree
36795 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36796 {
36797 tree stmt, clauses;
36798
36799 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36800 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36801
36802 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36803
36804 stmt = make_node (OACC_CACHE);
36805 TREE_TYPE (stmt) = void_type_node;
36806 OACC_CACHE_CLAUSES (stmt) = clauses;
36807 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36808 add_stmt (stmt);
36809
36810 return stmt;
36811 }
36812
36813 /* OpenACC 2.0:
36814 # pragma acc data oacc-data-clause[optseq] new-line
36815 structured-block */
36816
36817 #define OACC_DATA_CLAUSE_MASK \
36818 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36825
36826 static tree
36827 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36828 {
36829 tree stmt, clauses, block;
36830 unsigned int save;
36831
36832 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36833 "#pragma acc data", pragma_tok);
36834
36835 block = begin_omp_parallel ();
36836 save = cp_parser_begin_omp_structured_block (parser);
36837 cp_parser_statement (parser, NULL_TREE, false, if_p);
36838 cp_parser_end_omp_structured_block (parser, save);
36839 stmt = finish_oacc_data (clauses, block);
36840 return stmt;
36841 }
36842
36843 /* OpenACC 2.0:
36844 # pragma acc host_data <clauses> new-line
36845 structured-block */
36846
36847 #define OACC_HOST_DATA_CLAUSE_MASK \
36848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36849
36850 static tree
36851 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36852 {
36853 tree stmt, clauses, block;
36854 unsigned int save;
36855
36856 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36857 "#pragma acc host_data", pragma_tok);
36858
36859 block = begin_omp_parallel ();
36860 save = cp_parser_begin_omp_structured_block (parser);
36861 cp_parser_statement (parser, NULL_TREE, false, if_p);
36862 cp_parser_end_omp_structured_block (parser, save);
36863 stmt = finish_oacc_host_data (clauses, block);
36864 return stmt;
36865 }
36866
36867 /* OpenACC 2.0:
36868 # pragma acc declare oacc-data-clause[optseq] new-line
36869 */
36870
36871 #define OACC_DECLARE_CLAUSE_MASK \
36872 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
36880
36881 static tree
36882 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36883 {
36884 tree clauses, stmt;
36885 bool error = false;
36886
36887 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36888 "#pragma acc declare", pragma_tok, true);
36889
36890
36891 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36892 {
36893 error_at (pragma_tok->location,
36894 "no valid clauses specified in %<#pragma acc declare%>");
36895 return NULL_TREE;
36896 }
36897
36898 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36899 {
36900 location_t loc = OMP_CLAUSE_LOCATION (t);
36901 tree decl = OMP_CLAUSE_DECL (t);
36902 if (!DECL_P (decl))
36903 {
36904 error_at (loc, "array section in %<#pragma acc declare%>");
36905 error = true;
36906 continue;
36907 }
36908 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36909 switch (OMP_CLAUSE_MAP_KIND (t))
36910 {
36911 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36912 case GOMP_MAP_ALLOC:
36913 case GOMP_MAP_TO:
36914 case GOMP_MAP_FORCE_DEVICEPTR:
36915 case GOMP_MAP_DEVICE_RESIDENT:
36916 break;
36917
36918 case GOMP_MAP_LINK:
36919 if (!global_bindings_p ()
36920 && (TREE_STATIC (decl)
36921 || !DECL_EXTERNAL (decl)))
36922 {
36923 error_at (loc,
36924 "%qD must be a global variable in "
36925 "%<#pragma acc declare link%>",
36926 decl);
36927 error = true;
36928 continue;
36929 }
36930 break;
36931
36932 default:
36933 if (global_bindings_p ())
36934 {
36935 error_at (loc, "invalid OpenACC clause at file scope");
36936 error = true;
36937 continue;
36938 }
36939 if (DECL_EXTERNAL (decl))
36940 {
36941 error_at (loc,
36942 "invalid use of %<extern%> variable %qD "
36943 "in %<#pragma acc declare%>", decl);
36944 error = true;
36945 continue;
36946 }
36947 else if (TREE_PUBLIC (decl))
36948 {
36949 error_at (loc,
36950 "invalid use of %<global%> variable %qD "
36951 "in %<#pragma acc declare%>", decl);
36952 error = true;
36953 continue;
36954 }
36955 break;
36956 }
36957
36958 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36959 || lookup_attribute ("omp declare target link",
36960 DECL_ATTRIBUTES (decl)))
36961 {
36962 error_at (loc, "variable %qD used more than once with "
36963 "%<#pragma acc declare%>", decl);
36964 error = true;
36965 continue;
36966 }
36967
36968 if (!error)
36969 {
36970 tree id;
36971
36972 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36973 id = get_identifier ("omp declare target link");
36974 else
36975 id = get_identifier ("omp declare target");
36976
36977 DECL_ATTRIBUTES (decl)
36978 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36979 if (global_bindings_p ())
36980 {
36981 symtab_node *node = symtab_node::get (decl);
36982 if (node != NULL)
36983 {
36984 node->offloadable = 1;
36985 if (ENABLE_OFFLOADING)
36986 {
36987 g->have_offload = true;
36988 if (is_a <varpool_node *> (node))
36989 vec_safe_push (offload_vars, decl);
36990 }
36991 }
36992 }
36993 }
36994 }
36995
36996 if (error || global_bindings_p ())
36997 return NULL_TREE;
36998
36999 stmt = make_node (OACC_DECLARE);
37000 TREE_TYPE (stmt) = void_type_node;
37001 OACC_DECLARE_CLAUSES (stmt) = clauses;
37002 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37003
37004 add_stmt (stmt);
37005
37006 return NULL_TREE;
37007 }
37008
37009 /* OpenACC 2.0:
37010 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
37011
37012 or
37013
37014 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
37015
37016 LOC is the location of the #pragma token.
37017 */
37018
37019 #define OACC_ENTER_DATA_CLAUSE_MASK \
37020 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37025
37026 #define OACC_EXIT_DATA_CLAUSE_MASK \
37027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
37031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
37032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37033
37034 static tree
37035 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
37036 bool enter)
37037 {
37038 location_t loc = pragma_tok->location;
37039 tree stmt, clauses;
37040 const char *p = "";
37041
37042 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37043 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37044
37045 if (strcmp (p, "data") != 0)
37046 {
37047 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
37048 enter ? "enter" : "exit");
37049 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37050 return NULL_TREE;
37051 }
37052
37053 cp_lexer_consume_token (parser->lexer);
37054
37055 if (enter)
37056 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
37057 "#pragma acc enter data", pragma_tok);
37058 else
37059 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
37060 "#pragma acc exit data", pragma_tok);
37061
37062 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37063 {
37064 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
37065 enter ? "enter" : "exit");
37066 return NULL_TREE;
37067 }
37068
37069 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
37070 TREE_TYPE (stmt) = void_type_node;
37071 OMP_STANDALONE_CLAUSES (stmt) = clauses;
37072 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37073 add_stmt (stmt);
37074 return stmt;
37075 }
37076
37077 /* OpenACC 2.0:
37078 # pragma acc loop oacc-loop-clause[optseq] new-line
37079 structured-block */
37080
37081 #define OACC_LOOP_CLAUSE_MASK \
37082 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37092
37093 static tree
37094 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37095 omp_clause_mask mask, tree *cclauses, bool *if_p)
37096 {
37097 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37098
37099 strcat (p_name, " loop");
37100 mask |= OACC_LOOP_CLAUSE_MASK;
37101
37102 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37103 cclauses == NULL);
37104 if (cclauses)
37105 {
37106 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37107 if (*cclauses)
37108 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37109 if (clauses)
37110 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37111 }
37112
37113 tree block = begin_omp_structured_block ();
37114 int save = cp_parser_begin_omp_structured_block (parser);
37115 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37116 cp_parser_end_omp_structured_block (parser, save);
37117 add_stmt (finish_omp_structured_block (block));
37118
37119 return stmt;
37120 }
37121
37122 /* OpenACC 2.0:
37123 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37124 structured-block
37125
37126 or
37127
37128 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37129 structured-block
37130 */
37131
37132 #define OACC_KERNELS_CLAUSE_MASK \
37133 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37146
37147 #define OACC_PARALLEL_CLAUSE_MASK \
37148 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37164
37165 static tree
37166 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37167 char *p_name, bool *if_p)
37168 {
37169 omp_clause_mask mask;
37170 enum tree_code code;
37171 switch (cp_parser_pragma_kind (pragma_tok))
37172 {
37173 case PRAGMA_OACC_KERNELS:
37174 strcat (p_name, " kernels");
37175 mask = OACC_KERNELS_CLAUSE_MASK;
37176 code = OACC_KERNELS;
37177 break;
37178 case PRAGMA_OACC_PARALLEL:
37179 strcat (p_name, " parallel");
37180 mask = OACC_PARALLEL_CLAUSE_MASK;
37181 code = OACC_PARALLEL;
37182 break;
37183 default:
37184 gcc_unreachable ();
37185 }
37186
37187 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37188 {
37189 const char *p
37190 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37191 if (strcmp (p, "loop") == 0)
37192 {
37193 cp_lexer_consume_token (parser->lexer);
37194 tree block = begin_omp_parallel ();
37195 tree clauses;
37196 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37197 if_p);
37198 return finish_omp_construct (code, block, clauses);
37199 }
37200 }
37201
37202 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37203
37204 tree block = begin_omp_parallel ();
37205 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37206 cp_parser_statement (parser, NULL_TREE, false, if_p);
37207 cp_parser_end_omp_structured_block (parser, save);
37208 return finish_omp_construct (code, block, clauses);
37209 }
37210
37211 /* OpenACC 2.0:
37212 # pragma acc update oacc-update-clause[optseq] new-line
37213 */
37214
37215 #define OACC_UPDATE_CLAUSE_MASK \
37216 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
37221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37222
37223 static tree
37224 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37225 {
37226 tree stmt, clauses;
37227
37228 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37229 "#pragma acc update", pragma_tok);
37230
37231 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37232 {
37233 error_at (pragma_tok->location,
37234 "%<#pragma acc update%> must contain at least one "
37235 "%<device%> or %<host%> or %<self%> clause");
37236 return NULL_TREE;
37237 }
37238
37239 stmt = make_node (OACC_UPDATE);
37240 TREE_TYPE (stmt) = void_type_node;
37241 OACC_UPDATE_CLAUSES (stmt) = clauses;
37242 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37243 add_stmt (stmt);
37244 return stmt;
37245 }
37246
37247 /* OpenACC 2.0:
37248 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37249
37250 LOC is the location of the #pragma token.
37251 */
37252
37253 #define OACC_WAIT_CLAUSE_MASK \
37254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37255
37256 static tree
37257 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37258 {
37259 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37260 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37261
37262 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37263 list = cp_parser_oacc_wait_list (parser, loc, list);
37264
37265 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37266 "#pragma acc wait", pragma_tok);
37267
37268 stmt = c_finish_oacc_wait (loc, list, clauses);
37269 stmt = finish_expr_stmt (stmt);
37270
37271 return stmt;
37272 }
37273
37274 /* OpenMP 4.0:
37275 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37276
37277 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37278 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37284
37285 static void
37286 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37287 enum pragma_context context)
37288 {
37289 bool first_p = parser->omp_declare_simd == NULL;
37290 cp_omp_declare_simd_data data;
37291 if (first_p)
37292 {
37293 data.error_seen = false;
37294 data.fndecl_seen = false;
37295 data.tokens = vNULL;
37296 data.clauses = NULL_TREE;
37297 /* It is safe to take the address of a local variable; it will only be
37298 used while this scope is live. */
37299 parser->omp_declare_simd = &data;
37300 }
37301
37302 /* Store away all pragma tokens. */
37303 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37304 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37305 cp_lexer_consume_token (parser->lexer);
37306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37307 parser->omp_declare_simd->error_seen = true;
37308 cp_parser_require_pragma_eol (parser, pragma_tok);
37309 struct cp_token_cache *cp
37310 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37311 parser->omp_declare_simd->tokens.safe_push (cp);
37312
37313 if (first_p)
37314 {
37315 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37316 cp_parser_pragma (parser, context, NULL);
37317 switch (context)
37318 {
37319 case pragma_external:
37320 cp_parser_declaration (parser);
37321 break;
37322 case pragma_member:
37323 cp_parser_member_declaration (parser);
37324 break;
37325 case pragma_objc_icode:
37326 cp_parser_block_declaration (parser, /*statement_p=*/false);
37327 break;
37328 default:
37329 cp_parser_declaration_statement (parser);
37330 break;
37331 }
37332 if (parser->omp_declare_simd
37333 && !parser->omp_declare_simd->error_seen
37334 && !parser->omp_declare_simd->fndecl_seen)
37335 error_at (pragma_tok->location,
37336 "%<#pragma omp declare simd%> not immediately followed by "
37337 "function declaration or definition");
37338 data.tokens.release ();
37339 parser->omp_declare_simd = NULL;
37340 }
37341 }
37342
37343 /* Finalize #pragma omp declare simd clauses after direct declarator has
37344 been parsed, and put that into "omp declare simd" attribute. */
37345
37346 static tree
37347 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37348 {
37349 struct cp_token_cache *ce;
37350 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37351 int i;
37352
37353 if (!data->error_seen && data->fndecl_seen)
37354 {
37355 error ("%<#pragma omp declare simd%> not immediately followed by "
37356 "a single function declaration or definition");
37357 data->error_seen = true;
37358 }
37359 if (data->error_seen)
37360 return attrs;
37361
37362 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37363 {
37364 tree c, cl;
37365
37366 cp_parser_push_lexer_for_tokens (parser, ce);
37367 parser->lexer->in_pragma = true;
37368 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37369 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37370 cp_lexer_consume_token (parser->lexer);
37371 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37372 "#pragma omp declare simd", pragma_tok);
37373 cp_parser_pop_lexer (parser);
37374 if (cl)
37375 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37376 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37377 TREE_CHAIN (c) = attrs;
37378 if (processing_template_decl)
37379 ATTR_IS_DEPENDENT (c) = 1;
37380 attrs = c;
37381 }
37382
37383 data->fndecl_seen = true;
37384 return attrs;
37385 }
37386
37387
37388 /* OpenMP 4.0:
37389 # pragma omp declare target new-line
37390 declarations and definitions
37391 # pragma omp end declare target new-line
37392
37393 OpenMP 4.5:
37394 # pragma omp declare target ( extended-list ) new-line
37395
37396 # pragma omp declare target declare-target-clauses[seq] new-line */
37397
37398 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37401
37402 static void
37403 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37404 {
37405 tree clauses = NULL_TREE;
37406 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37407 clauses
37408 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37409 "#pragma omp declare target", pragma_tok);
37410 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37411 {
37412 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37413 clauses);
37414 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37415 cp_parser_require_pragma_eol (parser, pragma_tok);
37416 }
37417 else
37418 {
37419 cp_parser_require_pragma_eol (parser, pragma_tok);
37420 scope_chain->omp_declare_target_attribute++;
37421 return;
37422 }
37423 if (scope_chain->omp_declare_target_attribute)
37424 error_at (pragma_tok->location,
37425 "%<#pragma omp declare target%> with clauses in between "
37426 "%<#pragma omp declare target%> without clauses and "
37427 "%<#pragma omp end declare target%>");
37428 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37429 {
37430 tree t = OMP_CLAUSE_DECL (c), id;
37431 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37432 tree at2 = lookup_attribute ("omp declare target link",
37433 DECL_ATTRIBUTES (t));
37434 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37435 {
37436 id = get_identifier ("omp declare target link");
37437 std::swap (at1, at2);
37438 }
37439 else
37440 id = get_identifier ("omp declare target");
37441 if (at2)
37442 {
37443 error_at (OMP_CLAUSE_LOCATION (c),
37444 "%qD specified both in declare target %<link%> and %<to%>"
37445 " clauses", t);
37446 continue;
37447 }
37448 if (!at1)
37449 {
37450 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37451 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37452 continue;
37453
37454 symtab_node *node = symtab_node::get (t);
37455 if (node != NULL)
37456 {
37457 node->offloadable = 1;
37458 if (ENABLE_OFFLOADING)
37459 {
37460 g->have_offload = true;
37461 if (is_a <varpool_node *> (node))
37462 vec_safe_push (offload_vars, t);
37463 }
37464 }
37465 }
37466 }
37467 }
37468
37469 static void
37470 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37471 {
37472 const char *p = "";
37473 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37474 {
37475 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37476 p = IDENTIFIER_POINTER (id);
37477 }
37478 if (strcmp (p, "declare") == 0)
37479 {
37480 cp_lexer_consume_token (parser->lexer);
37481 p = "";
37482 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37483 {
37484 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37485 p = IDENTIFIER_POINTER (id);
37486 }
37487 if (strcmp (p, "target") == 0)
37488 cp_lexer_consume_token (parser->lexer);
37489 else
37490 {
37491 cp_parser_error (parser, "expected %<target%>");
37492 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37493 return;
37494 }
37495 }
37496 else
37497 {
37498 cp_parser_error (parser, "expected %<declare%>");
37499 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37500 return;
37501 }
37502 cp_parser_require_pragma_eol (parser, pragma_tok);
37503 if (!scope_chain->omp_declare_target_attribute)
37504 error_at (pragma_tok->location,
37505 "%<#pragma omp end declare target%> without corresponding "
37506 "%<#pragma omp declare target%>");
37507 else
37508 scope_chain->omp_declare_target_attribute--;
37509 }
37510
37511 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37512 expression and optional initializer clause of
37513 #pragma omp declare reduction. We store the expression(s) as
37514 either 3, 6 or 7 special statements inside of the artificial function's
37515 body. The first two statements are DECL_EXPRs for the artificial
37516 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37517 expression that uses those variables.
37518 If there was any INITIALIZER clause, this is followed by further statements,
37519 the fourth and fifth statements are DECL_EXPRs for the artificial
37520 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37521 constructor variant (first token after open paren is not omp_priv),
37522 then the sixth statement is a statement with the function call expression
37523 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37524 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37525 to initialize the OMP_PRIV artificial variable and there is seventh
37526 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37527
37528 static bool
37529 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37530 {
37531 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37532 gcc_assert (TYPE_REF_P (type));
37533 type = TREE_TYPE (type);
37534 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37535 DECL_ARTIFICIAL (omp_out) = 1;
37536 pushdecl (omp_out);
37537 add_decl_expr (omp_out);
37538 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37539 DECL_ARTIFICIAL (omp_in) = 1;
37540 pushdecl (omp_in);
37541 add_decl_expr (omp_in);
37542 tree combiner;
37543 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37544
37545 keep_next_level (true);
37546 tree block = begin_omp_structured_block ();
37547 combiner = cp_parser_expression (parser);
37548 finish_expr_stmt (combiner);
37549 block = finish_omp_structured_block (block);
37550 add_stmt (block);
37551
37552 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37553 return false;
37554
37555 const char *p = "";
37556 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37557 {
37558 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37559 p = IDENTIFIER_POINTER (id);
37560 }
37561
37562 if (strcmp (p, "initializer") == 0)
37563 {
37564 cp_lexer_consume_token (parser->lexer);
37565 matching_parens parens;
37566 if (!parens.require_open (parser))
37567 return false;
37568
37569 p = "";
37570 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37571 {
37572 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37573 p = IDENTIFIER_POINTER (id);
37574 }
37575
37576 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37577 DECL_ARTIFICIAL (omp_priv) = 1;
37578 pushdecl (omp_priv);
37579 add_decl_expr (omp_priv);
37580 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37581 DECL_ARTIFICIAL (omp_orig) = 1;
37582 pushdecl (omp_orig);
37583 add_decl_expr (omp_orig);
37584
37585 keep_next_level (true);
37586 block = begin_omp_structured_block ();
37587
37588 bool ctor = false;
37589 if (strcmp (p, "omp_priv") == 0)
37590 {
37591 bool is_direct_init, is_non_constant_init;
37592 ctor = true;
37593 cp_lexer_consume_token (parser->lexer);
37594 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37595 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37596 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37597 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37598 == CPP_CLOSE_PAREN
37599 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37600 == CPP_CLOSE_PAREN))
37601 {
37602 finish_omp_structured_block (block);
37603 error ("invalid initializer clause");
37604 return false;
37605 }
37606 initializer = cp_parser_initializer (parser, &is_direct_init,
37607 &is_non_constant_init);
37608 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37609 NULL_TREE, LOOKUP_ONLYCONVERTING);
37610 }
37611 else
37612 {
37613 cp_parser_parse_tentatively (parser);
37614 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37615 /*check_dependency_p=*/true,
37616 /*template_p=*/NULL,
37617 /*declarator_p=*/false,
37618 /*optional_p=*/false);
37619 vec<tree, va_gc> *args;
37620 if (fn_name == error_mark_node
37621 || cp_parser_error_occurred (parser)
37622 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37623 || ((args = cp_parser_parenthesized_expression_list
37624 (parser, non_attr, /*cast_p=*/false,
37625 /*allow_expansion_p=*/true,
37626 /*non_constant_p=*/NULL)),
37627 cp_parser_error_occurred (parser)))
37628 {
37629 finish_omp_structured_block (block);
37630 cp_parser_abort_tentative_parse (parser);
37631 cp_parser_error (parser, "expected id-expression (arguments)");
37632 return false;
37633 }
37634 unsigned int i;
37635 tree arg;
37636 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37637 if (arg == omp_priv
37638 || (TREE_CODE (arg) == ADDR_EXPR
37639 && TREE_OPERAND (arg, 0) == omp_priv))
37640 break;
37641 cp_parser_abort_tentative_parse (parser);
37642 if (arg == NULL_TREE)
37643 error ("one of the initializer call arguments should be %<omp_priv%>"
37644 " or %<&omp_priv%>");
37645 initializer = cp_parser_postfix_expression (parser, false, false, false,
37646 false, NULL);
37647 finish_expr_stmt (initializer);
37648 }
37649
37650 block = finish_omp_structured_block (block);
37651 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37652 add_stmt (block);
37653
37654 if (ctor)
37655 add_decl_expr (omp_orig);
37656
37657 if (!parens.require_close (parser))
37658 return false;
37659 }
37660
37661 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37662 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37663 UNKNOWN_LOCATION);
37664
37665 return true;
37666 }
37667
37668 /* OpenMP 4.0
37669 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37670 initializer-clause[opt] new-line
37671
37672 initializer-clause:
37673 initializer (omp_priv initializer)
37674 initializer (function-name (argument-list)) */
37675
37676 static void
37677 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37678 enum pragma_context)
37679 {
37680 auto_vec<tree> types;
37681 enum tree_code reduc_code = ERROR_MARK;
37682 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37683 unsigned int i;
37684 cp_token *first_token;
37685 cp_token_cache *cp;
37686 int errs;
37687 void *p;
37688
37689 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37690 p = obstack_alloc (&declarator_obstack, 0);
37691
37692 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37693 goto fail;
37694
37695 switch (cp_lexer_peek_token (parser->lexer)->type)
37696 {
37697 case CPP_PLUS:
37698 reduc_code = PLUS_EXPR;
37699 break;
37700 case CPP_MULT:
37701 reduc_code = MULT_EXPR;
37702 break;
37703 case CPP_MINUS:
37704 reduc_code = MINUS_EXPR;
37705 break;
37706 case CPP_AND:
37707 reduc_code = BIT_AND_EXPR;
37708 break;
37709 case CPP_XOR:
37710 reduc_code = BIT_XOR_EXPR;
37711 break;
37712 case CPP_OR:
37713 reduc_code = BIT_IOR_EXPR;
37714 break;
37715 case CPP_AND_AND:
37716 reduc_code = TRUTH_ANDIF_EXPR;
37717 break;
37718 case CPP_OR_OR:
37719 reduc_code = TRUTH_ORIF_EXPR;
37720 break;
37721 case CPP_NAME:
37722 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37723 break;
37724 default:
37725 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37726 "%<|%>, %<&&%>, %<||%> or identifier");
37727 goto fail;
37728 }
37729
37730 if (reduc_code != ERROR_MARK)
37731 cp_lexer_consume_token (parser->lexer);
37732
37733 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37734 if (reduc_id == error_mark_node)
37735 goto fail;
37736
37737 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37738 goto fail;
37739
37740 /* Types may not be defined in declare reduction type list. */
37741 const char *saved_message;
37742 saved_message = parser->type_definition_forbidden_message;
37743 parser->type_definition_forbidden_message
37744 = G_("types may not be defined in declare reduction type list");
37745 bool saved_colon_corrects_to_scope_p;
37746 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37747 parser->colon_corrects_to_scope_p = false;
37748 bool saved_colon_doesnt_start_class_def_p;
37749 saved_colon_doesnt_start_class_def_p
37750 = parser->colon_doesnt_start_class_def_p;
37751 parser->colon_doesnt_start_class_def_p = true;
37752
37753 while (true)
37754 {
37755 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37756 type = cp_parser_type_id (parser);
37757 if (type == error_mark_node)
37758 ;
37759 else if (ARITHMETIC_TYPE_P (type)
37760 && (orig_reduc_id == NULL_TREE
37761 || (TREE_CODE (type) != COMPLEX_TYPE
37762 && (id_equal (orig_reduc_id, "min")
37763 || id_equal (orig_reduc_id, "max")))))
37764 error_at (loc, "predeclared arithmetic type %qT in "
37765 "%<#pragma omp declare reduction%>", type);
37766 else if (TREE_CODE (type) == FUNCTION_TYPE
37767 || TREE_CODE (type) == METHOD_TYPE
37768 || TREE_CODE (type) == ARRAY_TYPE)
37769 error_at (loc, "function or array type %qT in "
37770 "%<#pragma omp declare reduction%>", type);
37771 else if (TYPE_REF_P (type))
37772 error_at (loc, "reference type %qT in "
37773 "%<#pragma omp declare reduction%>", type);
37774 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37775 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37776 "%<#pragma omp declare reduction%>", type);
37777 else
37778 types.safe_push (type);
37779
37780 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37781 cp_lexer_consume_token (parser->lexer);
37782 else
37783 break;
37784 }
37785
37786 /* Restore the saved message. */
37787 parser->type_definition_forbidden_message = saved_message;
37788 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37789 parser->colon_doesnt_start_class_def_p
37790 = saved_colon_doesnt_start_class_def_p;
37791
37792 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37793 || types.is_empty ())
37794 {
37795 fail:
37796 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37797 goto done;
37798 }
37799
37800 first_token = cp_lexer_peek_token (parser->lexer);
37801 cp = NULL;
37802 errs = errorcount;
37803 FOR_EACH_VEC_ELT (types, i, type)
37804 {
37805 tree fntype
37806 = build_function_type_list (void_type_node,
37807 cp_build_reference_type (type, false),
37808 NULL_TREE);
37809 tree this_reduc_id = reduc_id;
37810 if (!dependent_type_p (type))
37811 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37812 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37813 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37814 DECL_ARTIFICIAL (fndecl) = 1;
37815 DECL_EXTERNAL (fndecl) = 1;
37816 DECL_DECLARED_INLINE_P (fndecl) = 1;
37817 DECL_IGNORED_P (fndecl) = 1;
37818 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37819 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37820 DECL_ATTRIBUTES (fndecl)
37821 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37822 DECL_ATTRIBUTES (fndecl));
37823 if (processing_template_decl)
37824 fndecl = push_template_decl (fndecl);
37825 bool block_scope = false;
37826 tree block = NULL_TREE;
37827 if (current_function_decl)
37828 {
37829 block_scope = true;
37830 DECL_CONTEXT (fndecl) = global_namespace;
37831 if (!processing_template_decl)
37832 pushdecl (fndecl);
37833 }
37834 else if (current_class_type)
37835 {
37836 if (cp == NULL)
37837 {
37838 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37839 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37840 cp_lexer_consume_token (parser->lexer);
37841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37842 goto fail;
37843 cp = cp_token_cache_new (first_token,
37844 cp_lexer_peek_nth_token (parser->lexer,
37845 2));
37846 }
37847 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37848 finish_member_declaration (fndecl);
37849 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37850 DECL_PENDING_INLINE_P (fndecl) = 1;
37851 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37852 continue;
37853 }
37854 else
37855 {
37856 DECL_CONTEXT (fndecl) = current_namespace;
37857 pushdecl (fndecl);
37858 }
37859 if (!block_scope)
37860 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37861 else
37862 block = begin_omp_structured_block ();
37863 if (cp)
37864 {
37865 cp_parser_push_lexer_for_tokens (parser, cp);
37866 parser->lexer->in_pragma = true;
37867 }
37868 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37869 {
37870 if (!block_scope)
37871 finish_function (/*inline_p=*/false);
37872 else
37873 DECL_CONTEXT (fndecl) = current_function_decl;
37874 if (cp)
37875 cp_parser_pop_lexer (parser);
37876 goto fail;
37877 }
37878 if (cp)
37879 cp_parser_pop_lexer (parser);
37880 if (!block_scope)
37881 finish_function (/*inline_p=*/false);
37882 else
37883 {
37884 DECL_CONTEXT (fndecl) = current_function_decl;
37885 block = finish_omp_structured_block (block);
37886 if (TREE_CODE (block) == BIND_EXPR)
37887 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37888 else if (TREE_CODE (block) == STATEMENT_LIST)
37889 DECL_SAVED_TREE (fndecl) = block;
37890 if (processing_template_decl)
37891 add_decl_expr (fndecl);
37892 }
37893 cp_check_omp_declare_reduction (fndecl);
37894 if (cp == NULL && types.length () > 1)
37895 cp = cp_token_cache_new (first_token,
37896 cp_lexer_peek_nth_token (parser->lexer, 2));
37897 if (errs != errorcount)
37898 break;
37899 }
37900
37901 cp_parser_require_pragma_eol (parser, pragma_tok);
37902
37903 done:
37904 /* Free any declarators allocated. */
37905 obstack_free (&declarator_obstack, p);
37906 }
37907
37908 /* OpenMP 4.0
37909 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37910 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37911 initializer-clause[opt] new-line
37912 #pragma omp declare target new-line */
37913
37914 static bool
37915 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37916 enum pragma_context context)
37917 {
37918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37919 {
37920 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37921 const char *p = IDENTIFIER_POINTER (id);
37922
37923 if (strcmp (p, "simd") == 0)
37924 {
37925 cp_lexer_consume_token (parser->lexer);
37926 cp_parser_omp_declare_simd (parser, pragma_tok,
37927 context);
37928 return true;
37929 }
37930 cp_ensure_no_omp_declare_simd (parser);
37931 if (strcmp (p, "reduction") == 0)
37932 {
37933 cp_lexer_consume_token (parser->lexer);
37934 cp_parser_omp_declare_reduction (parser, pragma_tok,
37935 context);
37936 return false;
37937 }
37938 if (!flag_openmp) /* flag_openmp_simd */
37939 {
37940 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37941 return false;
37942 }
37943 if (strcmp (p, "target") == 0)
37944 {
37945 cp_lexer_consume_token (parser->lexer);
37946 cp_parser_omp_declare_target (parser, pragma_tok);
37947 return false;
37948 }
37949 }
37950 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37951 "or %<target%>");
37952 cp_parser_require_pragma_eol (parser, pragma_tok);
37953 return false;
37954 }
37955
37956 /* OpenMP 4.5:
37957 #pragma omp taskloop taskloop-clause[optseq] new-line
37958 for-loop
37959
37960 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37961 for-loop */
37962
37963 #define OMP_TASKLOOP_CLAUSE_MASK \
37964 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37978
37979 static tree
37980 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37981 char *p_name, omp_clause_mask mask, tree *cclauses,
37982 bool *if_p)
37983 {
37984 tree clauses, sb, ret;
37985 unsigned int save;
37986 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37987
37988 strcat (p_name, " taskloop");
37989 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37990
37991 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37992 {
37993 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37994 const char *p = IDENTIFIER_POINTER (id);
37995
37996 if (strcmp (p, "simd") == 0)
37997 {
37998 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37999 if (cclauses == NULL)
38000 cclauses = cclauses_buf;
38001
38002 cp_lexer_consume_token (parser->lexer);
38003 if (!flag_openmp) /* flag_openmp_simd */
38004 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38005 cclauses, if_p);
38006 sb = begin_omp_structured_block ();
38007 save = cp_parser_begin_omp_structured_block (parser);
38008 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38009 cclauses, if_p);
38010 cp_parser_end_omp_structured_block (parser, save);
38011 tree body = finish_omp_structured_block (sb);
38012 if (ret == NULL)
38013 return ret;
38014 ret = make_node (OMP_TASKLOOP);
38015 TREE_TYPE (ret) = void_type_node;
38016 OMP_FOR_BODY (ret) = body;
38017 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38018 SET_EXPR_LOCATION (ret, loc);
38019 add_stmt (ret);
38020 return ret;
38021 }
38022 }
38023 if (!flag_openmp) /* flag_openmp_simd */
38024 {
38025 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38026 return NULL_TREE;
38027 }
38028
38029 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38030 cclauses == NULL);
38031 if (cclauses)
38032 {
38033 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38034 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38035 }
38036
38037 sb = begin_omp_structured_block ();
38038 save = cp_parser_begin_omp_structured_block (parser);
38039
38040 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38041 if_p);
38042
38043 cp_parser_end_omp_structured_block (parser, save);
38044 add_stmt (finish_omp_structured_block (sb));
38045
38046 return ret;
38047 }
38048
38049
38050 /* OpenACC 2.0:
38051 # pragma acc routine oacc-routine-clause[optseq] new-line
38052 function-definition
38053
38054 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38055 */
38056
38057 #define OACC_ROUTINE_CLAUSE_MASK \
38058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38062
38063
38064 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38065 component, which must resolve to a declared namespace-scope
38066 function. The clauses are either processed directly (for a named
38067 function), or defered until the immediatley following declaration
38068 is parsed. */
38069
38070 static void
38071 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38072 enum pragma_context context)
38073 {
38074 gcc_checking_assert (context == pragma_external);
38075 /* The checking for "another pragma following this one" in the "no optional
38076 '( name )'" case makes sure that we dont re-enter. */
38077 gcc_checking_assert (parser->oacc_routine == NULL);
38078
38079 cp_oacc_routine_data data;
38080 data.error_seen = false;
38081 data.fndecl_seen = false;
38082 data.tokens = vNULL;
38083 data.clauses = NULL_TREE;
38084 data.loc = pragma_tok->location;
38085 /* It is safe to take the address of a local variable; it will only be
38086 used while this scope is live. */
38087 parser->oacc_routine = &data;
38088
38089 /* Look for optional '( name )'. */
38090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38091 {
38092 matching_parens parens;
38093 parens.consume_open (parser); /* '(' */
38094
38095 /* We parse the name as an id-expression. If it resolves to
38096 anything other than a non-overloaded function at namespace
38097 scope, it's an error. */
38098 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38099 tree name = cp_parser_id_expression (parser,
38100 /*template_keyword_p=*/false,
38101 /*check_dependency_p=*/false,
38102 /*template_p=*/NULL,
38103 /*declarator_p=*/false,
38104 /*optional_p=*/false);
38105 tree decl = (identifier_p (name)
38106 ? cp_parser_lookup_name_simple (parser, name, name_loc)
38107 : name);
38108 if (name != error_mark_node && decl == error_mark_node)
38109 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38110
38111 if (decl == error_mark_node
38112 || !parens.require_close (parser))
38113 {
38114 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38115 parser->oacc_routine = NULL;
38116 return;
38117 }
38118
38119 data.clauses
38120 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38121 "#pragma acc routine",
38122 cp_lexer_peek_token (parser->lexer));
38123
38124 if (decl && is_overloaded_fn (decl)
38125 && (TREE_CODE (decl) != FUNCTION_DECL
38126 || DECL_FUNCTION_TEMPLATE_P (decl)))
38127 {
38128 error_at (name_loc,
38129 "%<#pragma acc routine%> names a set of overloads");
38130 parser->oacc_routine = NULL;
38131 return;
38132 }
38133
38134 /* Perhaps we should use the same rule as declarations in different
38135 namespaces? */
38136 if (!DECL_NAMESPACE_SCOPE_P (decl))
38137 {
38138 error_at (name_loc,
38139 "%qD does not refer to a namespace scope function", decl);
38140 parser->oacc_routine = NULL;
38141 return;
38142 }
38143
38144 if (TREE_CODE (decl) != FUNCTION_DECL)
38145 {
38146 error_at (name_loc, "%qD does not refer to a function", decl);
38147 parser->oacc_routine = NULL;
38148 return;
38149 }
38150
38151 cp_finalize_oacc_routine (parser, decl, false);
38152 parser->oacc_routine = NULL;
38153 }
38154 else /* No optional '( name )'. */
38155 {
38156 /* Store away all pragma tokens. */
38157 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38158 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38159 cp_lexer_consume_token (parser->lexer);
38160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38161 parser->oacc_routine->error_seen = true;
38162 cp_parser_require_pragma_eol (parser, pragma_tok);
38163 struct cp_token_cache *cp
38164 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38165 parser->oacc_routine->tokens.safe_push (cp);
38166
38167 /* Emit a helpful diagnostic if there's another pragma following this
38168 one. */
38169 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38170 {
38171 cp_ensure_no_oacc_routine (parser);
38172 data.tokens.release ();
38173 /* ..., and then just keep going. */
38174 return;
38175 }
38176
38177 /* We only have to consider the pragma_external case here. */
38178 cp_parser_declaration (parser);
38179 if (parser->oacc_routine
38180 && !parser->oacc_routine->fndecl_seen)
38181 cp_ensure_no_oacc_routine (parser);
38182 else
38183 parser->oacc_routine = NULL;
38184 data.tokens.release ();
38185 }
38186 }
38187
38188 /* Finalize #pragma acc routine clauses after direct declarator has
38189 been parsed. */
38190
38191 static tree
38192 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38193 {
38194 struct cp_token_cache *ce;
38195 cp_oacc_routine_data *data = parser->oacc_routine;
38196
38197 if (!data->error_seen && data->fndecl_seen)
38198 {
38199 error_at (data->loc,
38200 "%<#pragma acc routine%> not immediately followed by "
38201 "a single function declaration or definition");
38202 data->error_seen = true;
38203 }
38204 if (data->error_seen)
38205 return attrs;
38206
38207 gcc_checking_assert (data->tokens.length () == 1);
38208 ce = data->tokens[0];
38209
38210 cp_parser_push_lexer_for_tokens (parser, ce);
38211 parser->lexer->in_pragma = true;
38212 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38213
38214 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38215 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38216 parser->oacc_routine->clauses
38217 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38218 "#pragma acc routine", pragma_tok);
38219 cp_parser_pop_lexer (parser);
38220 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38221 fndecl_seen. */
38222
38223 return attrs;
38224 }
38225
38226 /* Apply any saved OpenACC routine clauses to a just-parsed
38227 declaration. */
38228
38229 static void
38230 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38231 {
38232 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38233 {
38234 /* Keep going if we're in error reporting mode. */
38235 if (parser->oacc_routine->error_seen
38236 || fndecl == error_mark_node)
38237 return;
38238
38239 if (parser->oacc_routine->fndecl_seen)
38240 {
38241 error_at (parser->oacc_routine->loc,
38242 "%<#pragma acc routine%> not immediately followed by"
38243 " a single function declaration or definition");
38244 parser->oacc_routine = NULL;
38245 return;
38246 }
38247 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38248 {
38249 cp_ensure_no_oacc_routine (parser);
38250 return;
38251 }
38252
38253 if (oacc_get_fn_attrib (fndecl))
38254 {
38255 error_at (parser->oacc_routine->loc,
38256 "%<#pragma acc routine%> already applied to %qD", fndecl);
38257 parser->oacc_routine = NULL;
38258 return;
38259 }
38260
38261 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38262 {
38263 error_at (parser->oacc_routine->loc,
38264 TREE_USED (fndecl)
38265 ? G_("%<#pragma acc routine%> must be applied before use")
38266 : G_("%<#pragma acc routine%> must be applied before "
38267 "definition"));
38268 parser->oacc_routine = NULL;
38269 return;
38270 }
38271
38272 /* Process the routine's dimension clauses. */
38273 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38274 oacc_replace_fn_attrib (fndecl, dims);
38275
38276 /* Add an "omp declare target" attribute. */
38277 DECL_ATTRIBUTES (fndecl)
38278 = tree_cons (get_identifier ("omp declare target"),
38279 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38280
38281 /* Don't unset parser->oacc_routine here: we may still need it to
38282 diagnose wrong usage. But, remember that we've used this "#pragma acc
38283 routine". */
38284 parser->oacc_routine->fndecl_seen = true;
38285 }
38286 }
38287
38288 /* Main entry point to OpenMP statement pragmas. */
38289
38290 static void
38291 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38292 {
38293 tree stmt;
38294 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38295 omp_clause_mask mask (0);
38296
38297 switch (cp_parser_pragma_kind (pragma_tok))
38298 {
38299 case PRAGMA_OACC_ATOMIC:
38300 cp_parser_omp_atomic (parser, pragma_tok);
38301 return;
38302 case PRAGMA_OACC_CACHE:
38303 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38304 break;
38305 case PRAGMA_OACC_DATA:
38306 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38307 break;
38308 case PRAGMA_OACC_ENTER_DATA:
38309 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38310 break;
38311 case PRAGMA_OACC_EXIT_DATA:
38312 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38313 break;
38314 case PRAGMA_OACC_HOST_DATA:
38315 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38316 break;
38317 case PRAGMA_OACC_KERNELS:
38318 case PRAGMA_OACC_PARALLEL:
38319 strcpy (p_name, "#pragma acc");
38320 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38321 if_p);
38322 break;
38323 case PRAGMA_OACC_LOOP:
38324 strcpy (p_name, "#pragma acc");
38325 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38326 if_p);
38327 break;
38328 case PRAGMA_OACC_UPDATE:
38329 stmt = cp_parser_oacc_update (parser, pragma_tok);
38330 break;
38331 case PRAGMA_OACC_WAIT:
38332 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38333 break;
38334 case PRAGMA_OMP_ATOMIC:
38335 cp_parser_omp_atomic (parser, pragma_tok);
38336 return;
38337 case PRAGMA_OMP_CRITICAL:
38338 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38339 break;
38340 case PRAGMA_OMP_DISTRIBUTE:
38341 strcpy (p_name, "#pragma omp");
38342 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38343 if_p);
38344 break;
38345 case PRAGMA_OMP_FOR:
38346 strcpy (p_name, "#pragma omp");
38347 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38348 if_p);
38349 break;
38350 case PRAGMA_OMP_MASTER:
38351 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38352 break;
38353 case PRAGMA_OMP_PARALLEL:
38354 strcpy (p_name, "#pragma omp");
38355 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38356 if_p);
38357 break;
38358 case PRAGMA_OMP_SECTIONS:
38359 strcpy (p_name, "#pragma omp");
38360 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38361 break;
38362 case PRAGMA_OMP_SIMD:
38363 strcpy (p_name, "#pragma omp");
38364 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38365 if_p);
38366 break;
38367 case PRAGMA_OMP_SINGLE:
38368 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38369 break;
38370 case PRAGMA_OMP_TASK:
38371 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38372 break;
38373 case PRAGMA_OMP_TASKGROUP:
38374 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38375 break;
38376 case PRAGMA_OMP_TASKLOOP:
38377 strcpy (p_name, "#pragma omp");
38378 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38379 if_p);
38380 break;
38381 case PRAGMA_OMP_TEAMS:
38382 strcpy (p_name, "#pragma omp");
38383 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38384 if_p);
38385 break;
38386 default:
38387 gcc_unreachable ();
38388 }
38389
38390 protected_set_expr_location (stmt, pragma_tok->location);
38391 }
38392 \f
38393 /* Transactional Memory parsing routines. */
38394
38395 /* Parse a transaction attribute.
38396
38397 txn-attribute:
38398 attribute
38399 [ [ identifier ] ]
38400
38401 We use this instead of cp_parser_attributes_opt for transactions to avoid
38402 the pedwarn in C++98 mode. */
38403
38404 static tree
38405 cp_parser_txn_attribute_opt (cp_parser *parser)
38406 {
38407 cp_token *token;
38408 tree attr_name, attr = NULL;
38409
38410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38411 return cp_parser_attributes_opt (parser);
38412
38413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38414 return NULL_TREE;
38415 cp_lexer_consume_token (parser->lexer);
38416 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38417 goto error1;
38418
38419 token = cp_lexer_peek_token (parser->lexer);
38420 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38421 {
38422 token = cp_lexer_consume_token (parser->lexer);
38423
38424 attr_name = (token->type == CPP_KEYWORD
38425 /* For keywords, use the canonical spelling,
38426 not the parsed identifier. */
38427 ? ridpointers[(int) token->keyword]
38428 : token->u.value);
38429 attr = build_tree_list (attr_name, NULL_TREE);
38430 }
38431 else
38432 cp_parser_error (parser, "expected identifier");
38433
38434 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38435 error1:
38436 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38437 return attr;
38438 }
38439
38440 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38441
38442 transaction-statement:
38443 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38444 compound-statement
38445 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38446 */
38447
38448 static tree
38449 cp_parser_transaction (cp_parser *parser, cp_token *token)
38450 {
38451 unsigned char old_in = parser->in_transaction;
38452 unsigned char this_in = 1, new_in;
38453 enum rid keyword = token->keyword;
38454 tree stmt, attrs, noex;
38455
38456 cp_lexer_consume_token (parser->lexer);
38457
38458 if (keyword == RID_TRANSACTION_RELAXED
38459 || keyword == RID_SYNCHRONIZED)
38460 this_in |= TM_STMT_ATTR_RELAXED;
38461 else
38462 {
38463 attrs = cp_parser_txn_attribute_opt (parser);
38464 if (attrs)
38465 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38466 }
38467
38468 /* Parse a noexcept specification. */
38469 if (keyword == RID_ATOMIC_NOEXCEPT)
38470 noex = boolean_true_node;
38471 else if (keyword == RID_ATOMIC_CANCEL)
38472 {
38473 /* cancel-and-throw is unimplemented. */
38474 sorry ("atomic_cancel");
38475 noex = NULL_TREE;
38476 }
38477 else
38478 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38479
38480 /* Keep track if we're in the lexical scope of an outer transaction. */
38481 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38482
38483 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38484
38485 parser->in_transaction = new_in;
38486 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38487 parser->in_transaction = old_in;
38488
38489 finish_transaction_stmt (stmt, NULL, this_in, noex);
38490
38491 return stmt;
38492 }
38493
38494 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38495
38496 transaction-expression:
38497 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38498 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38499 */
38500
38501 static tree
38502 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38503 {
38504 unsigned char old_in = parser->in_transaction;
38505 unsigned char this_in = 1;
38506 cp_token *token;
38507 tree expr, noex;
38508 bool noex_expr;
38509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38510
38511 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38512 || keyword == RID_TRANSACTION_RELAXED);
38513
38514 if (!flag_tm)
38515 error_at (loc,
38516 keyword == RID_TRANSACTION_RELAXED
38517 ? G_("%<__transaction_relaxed%> without transactional memory "
38518 "support enabled")
38519 : G_("%<__transaction_atomic%> without transactional memory "
38520 "support enabled"));
38521
38522 token = cp_parser_require_keyword (parser, keyword,
38523 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38524 : RT_TRANSACTION_RELAXED));
38525 gcc_assert (token != NULL);
38526
38527 if (keyword == RID_TRANSACTION_RELAXED)
38528 this_in |= TM_STMT_ATTR_RELAXED;
38529
38530 /* Set this early. This might mean that we allow transaction_cancel in
38531 an expression that we find out later actually has to be a constexpr.
38532 However, we expect that cxx_constant_value will be able to deal with
38533 this; also, if the noexcept has no constexpr, then what we parse next
38534 really is a transaction's body. */
38535 parser->in_transaction = this_in;
38536
38537 /* Parse a noexcept specification. */
38538 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38539 true);
38540
38541 if (!noex || !noex_expr
38542 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38543 {
38544 matching_parens parens;
38545 parens.require_open (parser);
38546
38547 expr = cp_parser_expression (parser);
38548 expr = finish_parenthesized_expr (expr);
38549
38550 parens.require_close (parser);
38551 }
38552 else
38553 {
38554 /* The only expression that is available got parsed for the noexcept
38555 already. noexcept is true then. */
38556 expr = noex;
38557 noex = boolean_true_node;
38558 }
38559
38560 expr = build_transaction_expr (token->location, expr, this_in, noex);
38561 parser->in_transaction = old_in;
38562
38563 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38564 return error_mark_node;
38565
38566 return (flag_tm ? expr : error_mark_node);
38567 }
38568
38569 /* Parse a function-transaction-block.
38570
38571 function-transaction-block:
38572 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38573 function-body
38574 __transaction_atomic txn-attribute[opt] function-try-block
38575 __transaction_relaxed ctor-initializer[opt] function-body
38576 __transaction_relaxed function-try-block
38577 */
38578
38579 static void
38580 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38581 {
38582 unsigned char old_in = parser->in_transaction;
38583 unsigned char new_in = 1;
38584 tree compound_stmt, stmt, attrs;
38585 cp_token *token;
38586
38587 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38588 || keyword == RID_TRANSACTION_RELAXED);
38589 token = cp_parser_require_keyword (parser, keyword,
38590 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38591 : RT_TRANSACTION_RELAXED));
38592 gcc_assert (token != NULL);
38593
38594 if (keyword == RID_TRANSACTION_RELAXED)
38595 new_in |= TM_STMT_ATTR_RELAXED;
38596 else
38597 {
38598 attrs = cp_parser_txn_attribute_opt (parser);
38599 if (attrs)
38600 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38601 }
38602
38603 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38604
38605 parser->in_transaction = new_in;
38606
38607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38608 cp_parser_function_try_block (parser);
38609 else
38610 cp_parser_ctor_initializer_opt_and_function_body
38611 (parser, /*in_function_try_block=*/false);
38612
38613 parser->in_transaction = old_in;
38614
38615 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38616 }
38617
38618 /* Parse a __transaction_cancel statement.
38619
38620 cancel-statement:
38621 __transaction_cancel txn-attribute[opt] ;
38622 __transaction_cancel txn-attribute[opt] throw-expression ;
38623
38624 ??? Cancel and throw is not yet implemented. */
38625
38626 static tree
38627 cp_parser_transaction_cancel (cp_parser *parser)
38628 {
38629 cp_token *token;
38630 bool is_outer = false;
38631 tree stmt, attrs;
38632
38633 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38634 RT_TRANSACTION_CANCEL);
38635 gcc_assert (token != NULL);
38636
38637 attrs = cp_parser_txn_attribute_opt (parser);
38638 if (attrs)
38639 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38640
38641 /* ??? Parse cancel-and-throw here. */
38642
38643 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38644
38645 if (!flag_tm)
38646 {
38647 error_at (token->location, "%<__transaction_cancel%> without "
38648 "transactional memory support enabled");
38649 return error_mark_node;
38650 }
38651 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38652 {
38653 error_at (token->location, "%<__transaction_cancel%> within a "
38654 "%<__transaction_relaxed%>");
38655 return error_mark_node;
38656 }
38657 else if (is_outer)
38658 {
38659 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38660 && !is_tm_may_cancel_outer (current_function_decl))
38661 {
38662 error_at (token->location, "outer %<__transaction_cancel%> not "
38663 "within outer %<__transaction_atomic%>");
38664 error_at (token->location,
38665 " or a %<transaction_may_cancel_outer%> function");
38666 return error_mark_node;
38667 }
38668 }
38669 else if (parser->in_transaction == 0)
38670 {
38671 error_at (token->location, "%<__transaction_cancel%> not within "
38672 "%<__transaction_atomic%>");
38673 return error_mark_node;
38674 }
38675
38676 stmt = build_tm_abort_call (token->location, is_outer);
38677 add_stmt (stmt);
38678
38679 return stmt;
38680 }
38681 \f
38682 /* The parser. */
38683
38684 static GTY (()) cp_parser *the_parser;
38685
38686 \f
38687 /* Special handling for the first token or line in the file. The first
38688 thing in the file might be #pragma GCC pch_preprocess, which loads a
38689 PCH file, which is a GC collection point. So we need to handle this
38690 first pragma without benefit of an existing lexer structure.
38691
38692 Always returns one token to the caller in *FIRST_TOKEN. This is
38693 either the true first token of the file, or the first token after
38694 the initial pragma. */
38695
38696 static void
38697 cp_parser_initial_pragma (cp_token *first_token)
38698 {
38699 tree name = NULL;
38700
38701 cp_lexer_get_preprocessor_token (NULL, first_token);
38702 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38703 return;
38704
38705 cp_lexer_get_preprocessor_token (NULL, first_token);
38706 if (first_token->type == CPP_STRING)
38707 {
38708 name = first_token->u.value;
38709
38710 cp_lexer_get_preprocessor_token (NULL, first_token);
38711 if (first_token->type != CPP_PRAGMA_EOL)
38712 error_at (first_token->location,
38713 "junk at end of %<#pragma GCC pch_preprocess%>");
38714 }
38715 else
38716 error_at (first_token->location, "expected string literal");
38717
38718 /* Skip to the end of the pragma. */
38719 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38720 cp_lexer_get_preprocessor_token (NULL, first_token);
38721
38722 /* Now actually load the PCH file. */
38723 if (name)
38724 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38725
38726 /* Read one more token to return to our caller. We have to do this
38727 after reading the PCH file in, since its pointers have to be
38728 live. */
38729 cp_lexer_get_preprocessor_token (NULL, first_token);
38730 }
38731
38732 /* Parse a pragma GCC ivdep. */
38733
38734 static bool
38735 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
38736 {
38737 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38738 return true;
38739 }
38740
38741 /* Parse a pragma GCC unroll. */
38742
38743 static unsigned short
38744 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
38745 {
38746 location_t location = cp_lexer_peek_token (parser->lexer)->location;
38747 tree expr = cp_parser_constant_expression (parser);
38748 unsigned short unroll;
38749 expr = maybe_constant_value (expr);
38750 HOST_WIDE_INT lunroll = 0;
38751 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
38752 || TREE_CODE (expr) != INTEGER_CST
38753 || (lunroll = tree_to_shwi (expr)) < 0
38754 || lunroll >= USHRT_MAX)
38755 {
38756 error_at (location, "%<#pragma GCC unroll%> requires an"
38757 " assignment-expression that evaluates to a non-negative"
38758 " integral constant less than %u", USHRT_MAX);
38759 unroll = 0;
38760 }
38761 else
38762 {
38763 unroll = (unsigned short)lunroll;
38764 if (unroll == 0)
38765 unroll = 1;
38766 }
38767 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38768 return unroll;
38769 }
38770
38771 /* Normal parsing of a pragma token. Here we can (and must) use the
38772 regular lexer. */
38773
38774 static bool
38775 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38776 {
38777 cp_token *pragma_tok;
38778 unsigned int id;
38779 tree stmt;
38780 bool ret;
38781
38782 pragma_tok = cp_lexer_consume_token (parser->lexer);
38783 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38784 parser->lexer->in_pragma = true;
38785
38786 id = cp_parser_pragma_kind (pragma_tok);
38787 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38788 cp_ensure_no_omp_declare_simd (parser);
38789 switch (id)
38790 {
38791 case PRAGMA_GCC_PCH_PREPROCESS:
38792 error_at (pragma_tok->location,
38793 "%<#pragma GCC pch_preprocess%> must be first");
38794 break;
38795
38796 case PRAGMA_OMP_BARRIER:
38797 switch (context)
38798 {
38799 case pragma_compound:
38800 cp_parser_omp_barrier (parser, pragma_tok);
38801 return false;
38802 case pragma_stmt:
38803 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38804 "used in compound statements", "omp barrier");
38805 break;
38806 default:
38807 goto bad_stmt;
38808 }
38809 break;
38810
38811 case PRAGMA_OMP_FLUSH:
38812 switch (context)
38813 {
38814 case pragma_compound:
38815 cp_parser_omp_flush (parser, pragma_tok);
38816 return false;
38817 case pragma_stmt:
38818 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38819 "used in compound statements", "omp flush");
38820 break;
38821 default:
38822 goto bad_stmt;
38823 }
38824 break;
38825
38826 case PRAGMA_OMP_TASKWAIT:
38827 switch (context)
38828 {
38829 case pragma_compound:
38830 cp_parser_omp_taskwait (parser, pragma_tok);
38831 return false;
38832 case pragma_stmt:
38833 error_at (pragma_tok->location,
38834 "%<#pragma %s%> may only be used in compound statements",
38835 "omp taskwait");
38836 break;
38837 default:
38838 goto bad_stmt;
38839 }
38840 break;
38841
38842 case PRAGMA_OMP_TASKYIELD:
38843 switch (context)
38844 {
38845 case pragma_compound:
38846 cp_parser_omp_taskyield (parser, pragma_tok);
38847 return false;
38848 case pragma_stmt:
38849 error_at (pragma_tok->location,
38850 "%<#pragma %s%> may only be used in compound statements",
38851 "omp taskyield");
38852 break;
38853 default:
38854 goto bad_stmt;
38855 }
38856 break;
38857
38858 case PRAGMA_OMP_CANCEL:
38859 switch (context)
38860 {
38861 case pragma_compound:
38862 cp_parser_omp_cancel (parser, pragma_tok);
38863 return false;
38864 case pragma_stmt:
38865 error_at (pragma_tok->location,
38866 "%<#pragma %s%> may only be used in compound statements",
38867 "omp cancel");
38868 break;
38869 default:
38870 goto bad_stmt;
38871 }
38872 break;
38873
38874 case PRAGMA_OMP_CANCELLATION_POINT:
38875 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38876 return false;
38877
38878 case PRAGMA_OMP_THREADPRIVATE:
38879 cp_parser_omp_threadprivate (parser, pragma_tok);
38880 return false;
38881
38882 case PRAGMA_OMP_DECLARE:
38883 return cp_parser_omp_declare (parser, pragma_tok, context);
38884
38885 case PRAGMA_OACC_DECLARE:
38886 cp_parser_oacc_declare (parser, pragma_tok);
38887 return false;
38888
38889 case PRAGMA_OACC_ENTER_DATA:
38890 if (context == pragma_stmt)
38891 {
38892 error_at (pragma_tok->location,
38893 "%<#pragma %s%> may only be used in compound statements",
38894 "acc enter data");
38895 break;
38896 }
38897 else if (context != pragma_compound)
38898 goto bad_stmt;
38899 cp_parser_omp_construct (parser, pragma_tok, if_p);
38900 return true;
38901
38902 case PRAGMA_OACC_EXIT_DATA:
38903 if (context == pragma_stmt)
38904 {
38905 error_at (pragma_tok->location,
38906 "%<#pragma %s%> may only be used in compound statements",
38907 "acc exit data");
38908 break;
38909 }
38910 else if (context != pragma_compound)
38911 goto bad_stmt;
38912 cp_parser_omp_construct (parser, pragma_tok, if_p);
38913 return true;
38914
38915 case PRAGMA_OACC_ROUTINE:
38916 if (context != pragma_external)
38917 {
38918 error_at (pragma_tok->location,
38919 "%<#pragma acc routine%> must be at file scope");
38920 break;
38921 }
38922 cp_parser_oacc_routine (parser, pragma_tok, context);
38923 return false;
38924
38925 case PRAGMA_OACC_UPDATE:
38926 if (context == pragma_stmt)
38927 {
38928 error_at (pragma_tok->location,
38929 "%<#pragma %s%> may only be used in compound statements",
38930 "acc update");
38931 break;
38932 }
38933 else if (context != pragma_compound)
38934 goto bad_stmt;
38935 cp_parser_omp_construct (parser, pragma_tok, if_p);
38936 return true;
38937
38938 case PRAGMA_OACC_WAIT:
38939 if (context == pragma_stmt)
38940 {
38941 error_at (pragma_tok->location,
38942 "%<#pragma %s%> may only be used in compound statements",
38943 "acc wait");
38944 break;
38945 }
38946 else if (context != pragma_compound)
38947 goto bad_stmt;
38948 cp_parser_omp_construct (parser, pragma_tok, if_p);
38949 return true;
38950
38951 case PRAGMA_OACC_ATOMIC:
38952 case PRAGMA_OACC_CACHE:
38953 case PRAGMA_OACC_DATA:
38954 case PRAGMA_OACC_HOST_DATA:
38955 case PRAGMA_OACC_KERNELS:
38956 case PRAGMA_OACC_PARALLEL:
38957 case PRAGMA_OACC_LOOP:
38958 case PRAGMA_OMP_ATOMIC:
38959 case PRAGMA_OMP_CRITICAL:
38960 case PRAGMA_OMP_DISTRIBUTE:
38961 case PRAGMA_OMP_FOR:
38962 case PRAGMA_OMP_MASTER:
38963 case PRAGMA_OMP_PARALLEL:
38964 case PRAGMA_OMP_SECTIONS:
38965 case PRAGMA_OMP_SIMD:
38966 case PRAGMA_OMP_SINGLE:
38967 case PRAGMA_OMP_TASK:
38968 case PRAGMA_OMP_TASKGROUP:
38969 case PRAGMA_OMP_TASKLOOP:
38970 case PRAGMA_OMP_TEAMS:
38971 if (context != pragma_stmt && context != pragma_compound)
38972 goto bad_stmt;
38973 stmt = push_omp_privatization_clauses (false);
38974 cp_parser_omp_construct (parser, pragma_tok, if_p);
38975 pop_omp_privatization_clauses (stmt);
38976 return true;
38977
38978 case PRAGMA_OMP_ORDERED:
38979 if (context != pragma_stmt && context != pragma_compound)
38980 goto bad_stmt;
38981 stmt = push_omp_privatization_clauses (false);
38982 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38983 pop_omp_privatization_clauses (stmt);
38984 return ret;
38985
38986 case PRAGMA_OMP_TARGET:
38987 if (context != pragma_stmt && context != pragma_compound)
38988 goto bad_stmt;
38989 stmt = push_omp_privatization_clauses (false);
38990 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38991 pop_omp_privatization_clauses (stmt);
38992 return ret;
38993
38994 case PRAGMA_OMP_END_DECLARE_TARGET:
38995 cp_parser_omp_end_declare_target (parser, pragma_tok);
38996 return false;
38997
38998 case PRAGMA_OMP_SECTION:
38999 error_at (pragma_tok->location,
39000 "%<#pragma omp section%> may only be used in "
39001 "%<#pragma omp sections%> construct");
39002 break;
39003
39004 case PRAGMA_IVDEP:
39005 {
39006 if (context == pragma_external)
39007 {
39008 error_at (pragma_tok->location,
39009 "%<#pragma GCC ivdep%> must be inside a function");
39010 break;
39011 }
39012 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
39013 unsigned short unroll;
39014 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39015 if (tok->type == CPP_PRAGMA
39016 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
39017 {
39018 tok = cp_lexer_consume_token (parser->lexer);
39019 unroll = cp_parser_pragma_unroll (parser, tok);
39020 tok = cp_lexer_peek_token (the_parser->lexer);
39021 }
39022 else
39023 unroll = 0;
39024 if (tok->type != CPP_KEYWORD
39025 || (tok->keyword != RID_FOR
39026 && tok->keyword != RID_WHILE
39027 && tok->keyword != RID_DO))
39028 {
39029 cp_parser_error (parser, "for, while or do statement expected");
39030 return false;
39031 }
39032 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39033 return true;
39034 }
39035
39036 case PRAGMA_UNROLL:
39037 {
39038 if (context == pragma_external)
39039 {
39040 error_at (pragma_tok->location,
39041 "%<#pragma GCC unroll%> must be inside a function");
39042 break;
39043 }
39044 const unsigned short unroll
39045 = cp_parser_pragma_unroll (parser, pragma_tok);
39046 bool ivdep;
39047 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39048 if (tok->type == CPP_PRAGMA
39049 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
39050 {
39051 tok = cp_lexer_consume_token (parser->lexer);
39052 ivdep = cp_parser_pragma_ivdep (parser, tok);
39053 tok = cp_lexer_peek_token (the_parser->lexer);
39054 }
39055 else
39056 ivdep = false;
39057 if (tok->type != CPP_KEYWORD
39058 || (tok->keyword != RID_FOR
39059 && tok->keyword != RID_WHILE
39060 && tok->keyword != RID_DO))
39061 {
39062 cp_parser_error (parser, "for, while or do statement expected");
39063 return false;
39064 }
39065 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
39066 return true;
39067 }
39068
39069 default:
39070 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39071 c_invoke_pragma_handler (id);
39072 break;
39073
39074 bad_stmt:
39075 cp_parser_error (parser, "expected declaration specifiers");
39076 break;
39077 }
39078
39079 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39080 return false;
39081 }
39082
39083 /* The interface the pragma parsers have to the lexer. */
39084
39085 enum cpp_ttype
39086 pragma_lex (tree *value, location_t *loc)
39087 {
39088 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39089 enum cpp_ttype ret = tok->type;
39090
39091 *value = tok->u.value;
39092 if (loc)
39093 *loc = tok->location;
39094
39095 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39096 ret = CPP_EOF;
39097 else if (ret == CPP_STRING)
39098 *value = cp_parser_string_literal (the_parser, false, false);
39099 else
39100 {
39101 if (ret == CPP_KEYWORD)
39102 ret = CPP_NAME;
39103 cp_lexer_consume_token (the_parser->lexer);
39104 }
39105
39106 return ret;
39107 }
39108
39109 \f
39110 /* External interface. */
39111
39112 /* Parse one entire translation unit. */
39113
39114 void
39115 c_parse_file (void)
39116 {
39117 static bool already_called = false;
39118
39119 if (already_called)
39120 fatal_error (input_location,
39121 "inter-module optimizations not implemented for C++");
39122 already_called = true;
39123
39124 the_parser = cp_parser_new ();
39125 push_deferring_access_checks (flag_access_control
39126 ? dk_no_deferred : dk_no_check);
39127 cp_parser_translation_unit (the_parser);
39128 the_parser = NULL;
39129 }
39130
39131 /* Create an identifier for a generic parameter type (a synthesized
39132 template parameter implied by `auto' or a concept identifier). */
39133
39134 static GTY(()) int generic_parm_count;
39135 static tree
39136 make_generic_type_name ()
39137 {
39138 char buf[32];
39139 sprintf (buf, "auto:%d", ++generic_parm_count);
39140 return get_identifier (buf);
39141 }
39142
39143 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39144 (creating a new template parameter list if necessary). Returns the newly
39145 created template type parm. */
39146
39147 static tree
39148 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39149 {
39150 gcc_assert (current_binding_level->kind == sk_function_parms);
39151
39152 /* Before committing to modifying any scope, if we're in an
39153 implicit template scope, and we're trying to synthesize a
39154 constrained parameter, try to find a previous parameter with
39155 the same name. This is the same-type rule for abbreviated
39156 function templates.
39157
39158 NOTE: We can generate implicit parameters when tentatively
39159 parsing a nested name specifier, only to reject that parse
39160 later. However, matching the same template-id as part of a
39161 direct-declarator should generate an identical template
39162 parameter, so this rule will merge them. */
39163 if (parser->implicit_template_scope && constr)
39164 {
39165 tree t = parser->implicit_template_parms;
39166 while (t)
39167 {
39168 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39169 {
39170 tree d = TREE_VALUE (t);
39171 if (TREE_CODE (d) == PARM_DECL)
39172 /* Return the TEMPLATE_PARM_INDEX. */
39173 d = DECL_INITIAL (d);
39174 return d;
39175 }
39176 t = TREE_CHAIN (t);
39177 }
39178 }
39179
39180 /* We are either continuing a function template that already contains implicit
39181 template parameters, creating a new fully-implicit function template, or
39182 extending an existing explicit function template with implicit template
39183 parameters. */
39184
39185 cp_binding_level *const entry_scope = current_binding_level;
39186
39187 bool become_template = false;
39188 cp_binding_level *parent_scope = 0;
39189
39190 if (parser->implicit_template_scope)
39191 {
39192 gcc_assert (parser->implicit_template_parms);
39193
39194 current_binding_level = parser->implicit_template_scope;
39195 }
39196 else
39197 {
39198 /* Roll back to the existing template parameter scope (in the case of
39199 extending an explicit function template) or introduce a new template
39200 parameter scope ahead of the function parameter scope (or class scope
39201 in the case of out-of-line member definitions). The function scope is
39202 added back after template parameter synthesis below. */
39203
39204 cp_binding_level *scope = entry_scope;
39205
39206 while (scope->kind == sk_function_parms)
39207 {
39208 parent_scope = scope;
39209 scope = scope->level_chain;
39210 }
39211 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39212 {
39213 /* If not defining a class, then any class scope is a scope level in
39214 an out-of-line member definition. In this case simply wind back
39215 beyond the first such scope to inject the template parameter list.
39216 Otherwise wind back to the class being defined. The latter can
39217 occur in class member friend declarations such as:
39218
39219 class A {
39220 void foo (auto);
39221 };
39222 class B {
39223 friend void A::foo (auto);
39224 };
39225
39226 The template parameter list synthesized for the friend declaration
39227 must be injected in the scope of 'B'. This can also occur in
39228 erroneous cases such as:
39229
39230 struct A {
39231 struct B {
39232 void foo (auto);
39233 };
39234 void B::foo (auto) {}
39235 };
39236
39237 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39238 but, nevertheless, the template parameter list synthesized for the
39239 declarator should be injected into the scope of 'A' as if the
39240 ill-formed template was specified explicitly. */
39241
39242 while (scope->kind == sk_class && !scope->defining_class_p)
39243 {
39244 parent_scope = scope;
39245 scope = scope->level_chain;
39246 }
39247 }
39248
39249 current_binding_level = scope;
39250
39251 if (scope->kind != sk_template_parms
39252 || !function_being_declared_is_template_p (parser))
39253 {
39254 /* Introduce a new template parameter list for implicit template
39255 parameters. */
39256
39257 become_template = true;
39258
39259 parser->implicit_template_scope
39260 = begin_scope (sk_template_parms, NULL);
39261
39262 ++processing_template_decl;
39263
39264 parser->fully_implicit_function_template_p = true;
39265 ++parser->num_template_parameter_lists;
39266 }
39267 else
39268 {
39269 /* Synthesize implicit template parameters at the end of the explicit
39270 template parameter list. */
39271
39272 gcc_assert (current_template_parms);
39273
39274 parser->implicit_template_scope = scope;
39275
39276 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39277 parser->implicit_template_parms
39278 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39279 }
39280 }
39281
39282 /* Synthesize a new template parameter and track the current template
39283 parameter chain with implicit_template_parms. */
39284
39285 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39286 tree synth_id = make_generic_type_name ();
39287 tree synth_tmpl_parm;
39288 bool non_type = false;
39289
39290 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39291 synth_tmpl_parm
39292 = finish_template_type_parm (class_type_node, synth_id);
39293 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39294 synth_tmpl_parm
39295 = finish_constrained_template_template_parm (proto, synth_id);
39296 else
39297 {
39298 synth_tmpl_parm = copy_decl (proto);
39299 DECL_NAME (synth_tmpl_parm) = synth_id;
39300 non_type = true;
39301 }
39302
39303 // Attach the constraint to the parm before processing.
39304 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39305 TREE_TYPE (node) = constr;
39306 tree new_parm
39307 = process_template_parm (parser->implicit_template_parms,
39308 input_location,
39309 node,
39310 /*non_type=*/non_type,
39311 /*param_pack=*/false);
39312
39313 // Chain the new parameter to the list of implicit parameters.
39314 if (parser->implicit_template_parms)
39315 parser->implicit_template_parms
39316 = TREE_CHAIN (parser->implicit_template_parms);
39317 else
39318 parser->implicit_template_parms = new_parm;
39319
39320 tree new_decl = get_local_decls ();
39321 if (non_type)
39322 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39323 new_decl = DECL_INITIAL (new_decl);
39324
39325 /* If creating a fully implicit function template, start the new implicit
39326 template parameter list with this synthesized type, otherwise grow the
39327 current template parameter list. */
39328
39329 if (become_template)
39330 {
39331 parent_scope->level_chain = current_binding_level;
39332
39333 tree new_parms = make_tree_vec (1);
39334 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39335 current_template_parms = tree_cons (size_int (processing_template_decl),
39336 new_parms, current_template_parms);
39337 }
39338 else
39339 {
39340 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39341 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39342 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39343 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39344 }
39345
39346 // If the new parameter was constrained, we need to add that to the
39347 // constraints in the template parameter list.
39348 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39349 {
39350 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39351 reqs = conjoin_constraints (reqs, req);
39352 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39353 }
39354
39355 current_binding_level = entry_scope;
39356
39357 return new_decl;
39358 }
39359
39360 /* Finish the declaration of a fully implicit function template. Such a
39361 template has no explicit template parameter list so has not been through the
39362 normal template head and tail processing. synthesize_implicit_template_parm
39363 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39364 provided if the declaration is a class member such that its template
39365 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39366 form is returned. Otherwise NULL_TREE is returned. */
39367
39368 static tree
39369 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39370 {
39371 gcc_assert (parser->fully_implicit_function_template_p);
39372
39373 if (member_decl_opt && member_decl_opt != error_mark_node
39374 && DECL_VIRTUAL_P (member_decl_opt))
39375 {
39376 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39377 "implicit templates may not be %<virtual%>");
39378 DECL_VIRTUAL_P (member_decl_opt) = false;
39379 }
39380
39381 if (member_decl_opt)
39382 member_decl_opt = finish_member_template_decl (member_decl_opt);
39383 end_template_decl ();
39384
39385 parser->fully_implicit_function_template_p = false;
39386 parser->implicit_template_parms = 0;
39387 parser->implicit_template_scope = 0;
39388 --parser->num_template_parameter_lists;
39389
39390 return member_decl_opt;
39391 }
39392
39393 /* Like finish_fully_implicit_template, but to be used in error
39394 recovery, rearranging scopes so that we restore the state we had
39395 before synthesize_implicit_template_parm inserted the implement
39396 template parms scope. */
39397
39398 static void
39399 abort_fully_implicit_template (cp_parser *parser)
39400 {
39401 cp_binding_level *return_to_scope = current_binding_level;
39402
39403 if (parser->implicit_template_scope
39404 && return_to_scope != parser->implicit_template_scope)
39405 {
39406 cp_binding_level *child = return_to_scope;
39407 for (cp_binding_level *scope = child->level_chain;
39408 scope != parser->implicit_template_scope;
39409 scope = child->level_chain)
39410 child = scope;
39411 child->level_chain = parser->implicit_template_scope->level_chain;
39412 parser->implicit_template_scope->level_chain = return_to_scope;
39413 current_binding_level = parser->implicit_template_scope;
39414 }
39415 else
39416 return_to_scope = return_to_scope->level_chain;
39417
39418 finish_fully_implicit_template (parser, NULL);
39419
39420 gcc_assert (current_binding_level == return_to_scope);
39421 }
39422
39423 /* Helper function for diagnostics that have complained about things
39424 being used with 'extern "C"' linkage.
39425
39426 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39427
39428 void
39429 maybe_show_extern_c_location (void)
39430 {
39431 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39432 inform (the_parser->innermost_linkage_specification_location,
39433 "%<extern \"C\"%> linkage started here");
39434 }
39435
39436 #include "gt-cp-parser.h"