]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
eae73fcab2fd4ae0708240c492dd1e09af2cc159
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #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 "cp-cilkplus.h"
45 #include "gcc-rich-location.h"
46 #include "tree-iterator.h"
47 #include "c-family/name-hint.h"
48
49 \f
50 /* The lexer. */
51
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
54
55 static cp_token eof_token =
56 {
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 };
59
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
117 };
118
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
129 };
130
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
154
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
176
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 };
186
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
189
190 class type_id_in_expr_sentinel
191 {
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
201 };
202
203 /* Prototypes. */
204
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
250
251 static void cp_parser_initial_pragma
252 (cp_token *);
253
254 static void cp_parser_cilk_simd
255 (cp_parser *, cp_token *, bool *);
256 static tree cp_parser_cilk_for
257 (cp_parser *, tree, bool *);
258 static bool cp_parser_omp_declare_reduction_exprs
259 (tree, cp_parser *);
260 static tree cp_parser_cilk_simd_vectorlength
261 (cp_parser *, tree, bool);
262 static void cp_finalize_oacc_routine
263 (cp_parser *, tree, bool);
264
265 /* Manifest constants. */
266 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
267 #define CP_SAVED_TOKEN_STACK 5
268
269 /* Variables. */
270
271 /* The stream to which debugging output should be written. */
272 static FILE *cp_lexer_debug_stream;
273
274 /* Nonzero if we are parsing an unevaluated operand: an operand to
275 sizeof, typeof, or alignof. */
276 int cp_unevaluated_operand;
277
278 /* Dump up to NUM tokens in BUFFER to FILE starting with token
279 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
280 first token in BUFFER. If NUM is 0, dump all the tokens. If
281 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
282 highlighted by surrounding it in [[ ]]. */
283
284 static void
285 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
286 cp_token *start_token, unsigned num,
287 cp_token *curr_token)
288 {
289 unsigned i, nprinted;
290 cp_token *token;
291 bool do_print;
292
293 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
294
295 if (buffer == NULL)
296 return;
297
298 if (num == 0)
299 num = buffer->length ();
300
301 if (start_token == NULL)
302 start_token = buffer->address ();
303
304 if (start_token > buffer->address ())
305 {
306 cp_lexer_print_token (file, &(*buffer)[0]);
307 fprintf (file, " ... ");
308 }
309
310 do_print = false;
311 nprinted = 0;
312 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
313 {
314 if (token == start_token)
315 do_print = true;
316
317 if (!do_print)
318 continue;
319
320 nprinted++;
321 if (token == curr_token)
322 fprintf (file, "[[");
323
324 cp_lexer_print_token (file, token);
325
326 if (token == curr_token)
327 fprintf (file, "]]");
328
329 switch (token->type)
330 {
331 case CPP_SEMICOLON:
332 case CPP_OPEN_BRACE:
333 case CPP_CLOSE_BRACE:
334 case CPP_EOF:
335 fputc ('\n', file);
336 break;
337
338 default:
339 fputc (' ', file);
340 }
341 }
342
343 if (i == num && i < buffer->length ())
344 {
345 fprintf (file, " ... ");
346 cp_lexer_print_token (file, &buffer->last ());
347 }
348
349 fprintf (file, "\n");
350 }
351
352
353 /* Dump all tokens in BUFFER to stderr. */
354
355 void
356 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
357 {
358 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 }
360
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> &ref)
363 {
364 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 }
366
367 DEBUG_FUNCTION void
368 debug (vec<cp_token, va_gc> *ptr)
369 {
370 if (ptr)
371 debug (*ptr);
372 else
373 fprintf (stderr, "<nil>\n");
374 }
375
376
377 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
378 description for T. */
379
380 static void
381 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
382 {
383 if (t)
384 {
385 fprintf (file, "%s: ", desc);
386 print_node_brief (file, "", t, 0);
387 }
388 }
389
390
391 /* Dump parser context C to FILE. */
392
393 static void
394 cp_debug_print_context (FILE *file, cp_parser_context *c)
395 {
396 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
397 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
398 print_node_brief (file, "", c->object_type, 0);
399 fprintf (file, "}\n");
400 }
401
402
403 /* Print the stack of parsing contexts to FILE starting with FIRST. */
404
405 static void
406 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
407 {
408 unsigned i;
409 cp_parser_context *c;
410
411 fprintf (file, "Parsing context stack:\n");
412 for (i = 0, c = first; c; c = c->next, i++)
413 {
414 fprintf (file, "\t#%u: ", i);
415 cp_debug_print_context (file, c);
416 }
417 }
418
419
420 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
421
422 static void
423 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
424 {
425 if (flag)
426 fprintf (file, "%s: true\n", desc);
427 }
428
429
430 /* Print an unparsed function entry UF to FILE. */
431
432 static void
433 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
434 {
435 unsigned i;
436 cp_default_arg_entry *default_arg_fn;
437 tree fn;
438
439 fprintf (file, "\tFunctions with default args:\n");
440 for (i = 0;
441 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
442 i++)
443 {
444 fprintf (file, "\t\tClass type: ");
445 print_node_brief (file, "", default_arg_fn->class_type, 0);
446 fprintf (file, "\t\tDeclaration: ");
447 print_node_brief (file, "", default_arg_fn->decl, 0);
448 fprintf (file, "\n");
449 }
450
451 fprintf (file, "\n\tFunctions with definitions that require "
452 "post-processing\n\t\t");
453 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
454 {
455 print_node_brief (file, "", fn, 0);
456 fprintf (file, " ");
457 }
458 fprintf (file, "\n");
459
460 fprintf (file, "\n\tNon-static data members with initializers that require "
461 "post-processing\n\t\t");
462 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
463 {
464 print_node_brief (file, "", fn, 0);
465 fprintf (file, " ");
466 }
467 fprintf (file, "\n");
468 }
469
470
471 /* Print the stack of unparsed member functions S to FILE. */
472
473 static void
474 cp_debug_print_unparsed_queues (FILE *file,
475 vec<cp_unparsed_functions_entry, va_gc> *s)
476 {
477 unsigned i;
478 cp_unparsed_functions_entry *uf;
479
480 fprintf (file, "Unparsed functions\n");
481 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
482 {
483 fprintf (file, "#%u:\n", i);
484 cp_debug_print_unparsed_function (file, uf);
485 }
486 }
487
488
489 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
490 the given PARSER. If FILE is NULL, the output is printed on stderr. */
491
492 static void
493 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
494 {
495 cp_token *next_token, *first_token, *start_token;
496
497 if (file == NULL)
498 file = stderr;
499
500 next_token = parser->lexer->next_token;
501 first_token = parser->lexer->buffer->address ();
502 start_token = (next_token > first_token + window_size / 2)
503 ? next_token - window_size / 2
504 : first_token;
505 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
506 next_token);
507 }
508
509
510 /* Dump debugging information for the given PARSER. If FILE is NULL,
511 the output is printed on stderr. */
512
513 void
514 cp_debug_parser (FILE *file, cp_parser *parser)
515 {
516 const size_t window_size = 20;
517 cp_token *token;
518 expanded_location eloc;
519
520 if (file == NULL)
521 file = stderr;
522
523 fprintf (file, "Parser state\n\n");
524 fprintf (file, "Number of tokens: %u\n",
525 vec_safe_length (parser->lexer->buffer));
526 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
527 cp_debug_print_tree_if_set (file, "Object scope",
528 parser->object_scope);
529 cp_debug_print_tree_if_set (file, "Qualifying scope",
530 parser->qualifying_scope);
531 cp_debug_print_context_stack (file, parser->context);
532 cp_debug_print_flag (file, "Allow GNU extensions",
533 parser->allow_gnu_extensions_p);
534 cp_debug_print_flag (file, "'>' token is greater-than",
535 parser->greater_than_is_operator_p);
536 cp_debug_print_flag (file, "Default args allowed in current "
537 "parameter list", parser->default_arg_ok_p);
538 cp_debug_print_flag (file, "Parsing integral constant-expression",
539 parser->integral_constant_expression_p);
540 cp_debug_print_flag (file, "Allow non-constant expression in current "
541 "constant-expression",
542 parser->allow_non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Seen non-constant expression",
544 parser->non_integral_constant_expression_p);
545 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
546 "current context",
547 parser->local_variables_forbidden_p);
548 cp_debug_print_flag (file, "In unbraced linkage specification",
549 parser->in_unbraced_linkage_specification_p);
550 cp_debug_print_flag (file, "Parsing a declarator",
551 parser->in_declarator_p);
552 cp_debug_print_flag (file, "In template argument list",
553 parser->in_template_argument_list_p);
554 cp_debug_print_flag (file, "Parsing an iteration statement",
555 parser->in_statement & IN_ITERATION_STMT);
556 cp_debug_print_flag (file, "Parsing a switch statement",
557 parser->in_statement & IN_SWITCH_STMT);
558 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
559 parser->in_statement & IN_OMP_BLOCK);
560 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
561 parser->in_statement & IN_CILK_SIMD_FOR);
562 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
563 parser->in_statement & IN_OMP_FOR);
564 cp_debug_print_flag (file, "Parsing an if statement",
565 parser->in_statement & IN_IF_STMT);
566 cp_debug_print_flag (file, "Parsing a type-id in an expression "
567 "context", parser->in_type_id_in_expr_p);
568 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
569 parser->implicit_extern_c);
570 cp_debug_print_flag (file, "String expressions should be translated "
571 "to execution character set",
572 parser->translate_strings_p);
573 cp_debug_print_flag (file, "Parsing function body outside of a "
574 "local class", parser->in_function_body);
575 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
576 parser->colon_corrects_to_scope_p);
577 cp_debug_print_flag (file, "Colon doesn't start a class definition",
578 parser->colon_doesnt_start_class_def_p);
579 if (parser->type_definition_forbidden_message)
580 fprintf (file, "Error message for forbidden type definitions: %s\n",
581 parser->type_definition_forbidden_message);
582 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
583 fprintf (file, "Number of class definitions in progress: %u\n",
584 parser->num_classes_being_defined);
585 fprintf (file, "Number of template parameter lists for the current "
586 "declaration: %u\n", parser->num_template_parameter_lists);
587 cp_debug_parser_tokens (file, parser, window_size);
588 token = parser->lexer->next_token;
589 fprintf (file, "Next token to parse:\n");
590 fprintf (file, "\tToken: ");
591 cp_lexer_print_token (file, token);
592 eloc = expand_location (token->location);
593 fprintf (file, "\n\tFile: %s\n", eloc.file);
594 fprintf (file, "\tLine: %d\n", eloc.line);
595 fprintf (file, "\tColumn: %d\n", eloc.column);
596 }
597
598 DEBUG_FUNCTION void
599 debug (cp_parser &ref)
600 {
601 cp_debug_parser (stderr, &ref);
602 }
603
604 DEBUG_FUNCTION void
605 debug (cp_parser *ptr)
606 {
607 if (ptr)
608 debug (*ptr);
609 else
610 fprintf (stderr, "<nil>\n");
611 }
612
613 /* Allocate memory for a new lexer object and return it. */
614
615 static cp_lexer *
616 cp_lexer_alloc (void)
617 {
618 cp_lexer *lexer;
619
620 c_common_no_more_pch ();
621
622 /* Allocate the memory. */
623 lexer = ggc_cleared_alloc<cp_lexer> ();
624
625 /* Initially we are not debugging. */
626 lexer->debugging_p = false;
627
628 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
629
630 /* Create the buffer. */
631 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
632
633 return lexer;
634 }
635
636
637 /* Create a new main C++ lexer, the lexer that gets tokens from the
638 preprocessor. */
639
640 static cp_lexer *
641 cp_lexer_new_main (void)
642 {
643 cp_lexer *lexer;
644 cp_token token;
645
646 /* It's possible that parsing the first pragma will load a PCH file,
647 which is a GC collection point. So we have to do that before
648 allocating any memory. */
649 cp_parser_initial_pragma (&token);
650
651 lexer = cp_lexer_alloc ();
652
653 /* Put the first token in the buffer. */
654 lexer->buffer->quick_push (token);
655
656 /* Get the remaining tokens from the preprocessor. */
657 while (token.type != CPP_EOF)
658 {
659 cp_lexer_get_preprocessor_token (lexer, &token);
660 vec_safe_push (lexer->buffer, token);
661 }
662
663 lexer->last_token = lexer->buffer->address ()
664 + lexer->buffer->length ()
665 - 1;
666 lexer->next_token = lexer->buffer->length ()
667 ? lexer->buffer->address ()
668 : &eof_token;
669
670 /* Subsequent preprocessor diagnostics should use compiler
671 diagnostic functions to get the compiler source location. */
672 done_lexing = true;
673
674 gcc_assert (!lexer->next_token->purged_p);
675 return lexer;
676 }
677
678 /* Create a new lexer whose token stream is primed with the tokens in
679 CACHE. When these tokens are exhausted, no new tokens will be read. */
680
681 static cp_lexer *
682 cp_lexer_new_from_tokens (cp_token_cache *cache)
683 {
684 cp_token *first = cache->first;
685 cp_token *last = cache->last;
686 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
687
688 /* We do not own the buffer. */
689 lexer->buffer = NULL;
690 lexer->next_token = first == last ? &eof_token : first;
691 lexer->last_token = last;
692
693 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
694
695 /* Initially we are not debugging. */
696 lexer->debugging_p = false;
697
698 gcc_assert (!lexer->next_token->purged_p);
699 return lexer;
700 }
701
702 /* Frees all resources associated with LEXER. */
703
704 static void
705 cp_lexer_destroy (cp_lexer *lexer)
706 {
707 vec_free (lexer->buffer);
708 lexer->saved_tokens.release ();
709 ggc_free (lexer);
710 }
711
712 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
713 be used. The point of this flag is to help the compiler to fold away calls
714 to cp_lexer_debugging_p within this source file at compile time, when the
715 lexer is not being debugged. */
716
717 #define LEXER_DEBUGGING_ENABLED_P false
718
719 /* Returns nonzero if debugging information should be output. */
720
721 static inline bool
722 cp_lexer_debugging_p (cp_lexer *lexer)
723 {
724 if (!LEXER_DEBUGGING_ENABLED_P)
725 return false;
726
727 return lexer->debugging_p;
728 }
729
730
731 static inline cp_token_position
732 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
733 {
734 gcc_assert (!previous_p || lexer->next_token != &eof_token);
735
736 return lexer->next_token - previous_p;
737 }
738
739 static inline cp_token *
740 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
741 {
742 return pos;
743 }
744
745 static inline void
746 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
747 {
748 lexer->next_token = cp_lexer_token_at (lexer, pos);
749 }
750
751 static inline cp_token_position
752 cp_lexer_previous_token_position (cp_lexer *lexer)
753 {
754 if (lexer->next_token == &eof_token)
755 return lexer->last_token - 1;
756 else
757 return cp_lexer_token_position (lexer, true);
758 }
759
760 static inline cp_token *
761 cp_lexer_previous_token (cp_lexer *lexer)
762 {
763 cp_token_position tp = cp_lexer_previous_token_position (lexer);
764
765 /* Skip past purged tokens. */
766 while (tp->purged_p)
767 {
768 gcc_assert (tp != vec_safe_address (lexer->buffer));
769 tp--;
770 }
771
772 return cp_lexer_token_at (lexer, tp);
773 }
774
775 /* nonzero if we are presently saving tokens. */
776
777 static inline int
778 cp_lexer_saving_tokens (const cp_lexer* lexer)
779 {
780 return lexer->saved_tokens.length () != 0;
781 }
782
783 /* Store the next token from the preprocessor in *TOKEN. Return true
784 if we reach EOF. If LEXER is NULL, assume we are handling an
785 initial #pragma pch_preprocess, and thus want the lexer to return
786 processed strings. */
787
788 static void
789 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
790 {
791 static int is_extern_c = 0;
792
793 /* Get a new token from the preprocessor. */
794 token->type
795 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
796 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
797 token->keyword = RID_MAX;
798 token->purged_p = false;
799 token->error_reported = false;
800
801 /* On some systems, some header files are surrounded by an
802 implicit extern "C" block. Set a flag in the token if it
803 comes from such a header. */
804 is_extern_c += pending_lang_change;
805 pending_lang_change = 0;
806 token->implicit_extern_c = is_extern_c > 0;
807
808 /* Check to see if this token is a keyword. */
809 if (token->type == CPP_NAME)
810 {
811 if (IDENTIFIER_KEYWORD_P (token->u.value))
812 {
813 /* Mark this token as a keyword. */
814 token->type = CPP_KEYWORD;
815 /* Record which keyword. */
816 token->keyword = C_RID_CODE (token->u.value);
817 }
818 else
819 {
820 if (warn_cxx11_compat
821 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
822 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
823 {
824 /* Warn about the C++0x keyword (but still treat it as
825 an identifier). */
826 warning (OPT_Wc__11_compat,
827 "identifier %qE is a keyword in C++11",
828 token->u.value);
829
830 /* Clear out the C_RID_CODE so we don't warn about this
831 particular identifier-turned-keyword again. */
832 C_SET_RID_CODE (token->u.value, RID_MAX);
833 }
834
835 token->keyword = RID_MAX;
836 }
837 }
838 else if (token->type == CPP_AT_NAME)
839 {
840 /* This only happens in Objective-C++; it must be a keyword. */
841 token->type = CPP_KEYWORD;
842 switch (C_RID_CODE (token->u.value))
843 {
844 /* Replace 'class' with '@class', 'private' with '@private',
845 etc. This prevents confusion with the C++ keyword
846 'class', and makes the tokens consistent with other
847 Objective-C 'AT' keywords. For example '@class' is
848 reported as RID_AT_CLASS which is consistent with
849 '@synchronized', which is reported as
850 RID_AT_SYNCHRONIZED.
851 */
852 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
853 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
854 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
855 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
856 case RID_THROW: token->keyword = RID_AT_THROW; break;
857 case RID_TRY: token->keyword = RID_AT_TRY; break;
858 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
859 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
860 default: token->keyword = C_RID_CODE (token->u.value);
861 }
862 }
863 }
864
865 /* Update the globals input_location and the input file stack from TOKEN. */
866 static inline void
867 cp_lexer_set_source_position_from_token (cp_token *token)
868 {
869 if (token->type != CPP_EOF)
870 {
871 input_location = token->location;
872 }
873 }
874
875 /* Update the globals input_location and the input file stack from LEXER. */
876 static inline void
877 cp_lexer_set_source_position (cp_lexer *lexer)
878 {
879 cp_token *token = cp_lexer_peek_token (lexer);
880 cp_lexer_set_source_position_from_token (token);
881 }
882
883 /* Return a pointer to the next token in the token stream, but do not
884 consume it. */
885
886 static inline cp_token *
887 cp_lexer_peek_token (cp_lexer *lexer)
888 {
889 if (cp_lexer_debugging_p (lexer))
890 {
891 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
892 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
893 putc ('\n', cp_lexer_debug_stream);
894 }
895 return lexer->next_token;
896 }
897
898 /* Return true if the next token has the indicated TYPE. */
899
900 static inline bool
901 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
902 {
903 return cp_lexer_peek_token (lexer)->type == type;
904 }
905
906 /* Return true if the next token does not have the indicated TYPE. */
907
908 static inline bool
909 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
910 {
911 return !cp_lexer_next_token_is (lexer, type);
912 }
913
914 /* Return true if the next token is the indicated KEYWORD. */
915
916 static inline bool
917 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
918 {
919 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 }
921
922 static inline bool
923 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
924 {
925 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 }
927
928 static inline bool
929 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
930 {
931 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
932 }
933
934 /* Return true if the next token is not the indicated KEYWORD. */
935
936 static inline bool
937 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
938 {
939 return cp_lexer_peek_token (lexer)->keyword != keyword;
940 }
941
942 /* Return true if KEYWORD can start a decl-specifier. */
943
944 bool
945 cp_keyword_starts_decl_specifier_p (enum rid keyword)
946 {
947 switch (keyword)
948 {
949 /* auto specifier: storage-class-specifier in C++,
950 simple-type-specifier in C++0x. */
951 case RID_AUTO:
952 /* Storage classes. */
953 case RID_REGISTER:
954 case RID_STATIC:
955 case RID_EXTERN:
956 case RID_MUTABLE:
957 case RID_THREAD:
958 /* Elaborated type specifiers. */
959 case RID_ENUM:
960 case RID_CLASS:
961 case RID_STRUCT:
962 case RID_UNION:
963 case RID_TYPENAME:
964 /* Simple type specifiers. */
965 case RID_CHAR:
966 case RID_CHAR16:
967 case RID_CHAR32:
968 case RID_WCHAR:
969 case RID_BOOL:
970 case RID_SHORT:
971 case RID_INT:
972 case RID_LONG:
973 case RID_SIGNED:
974 case RID_UNSIGNED:
975 case RID_FLOAT:
976 case RID_DOUBLE:
977 case RID_VOID:
978 /* GNU extensions. */
979 case RID_ATTRIBUTE:
980 case RID_TYPEOF:
981 /* C++0x extensions. */
982 case RID_DECLTYPE:
983 case RID_UNDERLYING_TYPE:
984 case RID_CONSTEXPR:
985 return true;
986
987 default:
988 if (keyword >= RID_FIRST_INT_N
989 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
990 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
991 return true;
992 return false;
993 }
994 }
995
996 /* Return true if the next token is a keyword for a decl-specifier. */
997
998 static bool
999 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1000 {
1001 cp_token *token;
1002
1003 token = cp_lexer_peek_token (lexer);
1004 return cp_keyword_starts_decl_specifier_p (token->keyword);
1005 }
1006
1007 /* Returns TRUE iff the token T begins a decltype type. */
1008
1009 static bool
1010 token_is_decltype (cp_token *t)
1011 {
1012 return (t->keyword == RID_DECLTYPE
1013 || t->type == CPP_DECLTYPE);
1014 }
1015
1016 /* Returns TRUE iff the next token begins a decltype type. */
1017
1018 static bool
1019 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1020 {
1021 cp_token *t = cp_lexer_peek_token (lexer);
1022 return token_is_decltype (t);
1023 }
1024
1025 /* Called when processing a token with tree_check_value; perform or defer the
1026 associated checks and return the value. */
1027
1028 static tree
1029 saved_checks_value (struct tree_check *check_value)
1030 {
1031 /* Perform any access checks that were deferred. */
1032 vec<deferred_access_check, va_gc> *checks;
1033 deferred_access_check *chk;
1034 checks = check_value->checks;
1035 if (checks)
1036 {
1037 int i;
1038 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1039 perform_or_defer_access_check (chk->binfo,
1040 chk->decl,
1041 chk->diag_decl, tf_warning_or_error);
1042 }
1043 /* Return the stored value. */
1044 return check_value->value;
1045 }
1046
1047 /* Return a pointer to the Nth token in the token stream. If N is 1,
1048 then this is precisely equivalent to cp_lexer_peek_token (except
1049 that it is not inline). One would like to disallow that case, but
1050 there is one case (cp_parser_nth_token_starts_template_id) where
1051 the caller passes a variable for N and it might be 1. */
1052
1053 static cp_token *
1054 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1055 {
1056 cp_token *token;
1057
1058 /* N is 1-based, not zero-based. */
1059 gcc_assert (n > 0);
1060
1061 if (cp_lexer_debugging_p (lexer))
1062 fprintf (cp_lexer_debug_stream,
1063 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1064
1065 --n;
1066 token = lexer->next_token;
1067 gcc_assert (!n || token != &eof_token);
1068 while (n != 0)
1069 {
1070 ++token;
1071 if (token == lexer->last_token)
1072 {
1073 token = &eof_token;
1074 break;
1075 }
1076
1077 if (!token->purged_p)
1078 --n;
1079 }
1080
1081 if (cp_lexer_debugging_p (lexer))
1082 {
1083 cp_lexer_print_token (cp_lexer_debug_stream, token);
1084 putc ('\n', cp_lexer_debug_stream);
1085 }
1086
1087 return token;
1088 }
1089
1090 /* Return the next token, and advance the lexer's next_token pointer
1091 to point to the next non-purged token. */
1092
1093 static cp_token *
1094 cp_lexer_consume_token (cp_lexer* lexer)
1095 {
1096 cp_token *token = lexer->next_token;
1097
1098 gcc_assert (token != &eof_token);
1099 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1100
1101 do
1102 {
1103 lexer->next_token++;
1104 if (lexer->next_token == lexer->last_token)
1105 {
1106 lexer->next_token = &eof_token;
1107 break;
1108 }
1109
1110 }
1111 while (lexer->next_token->purged_p);
1112
1113 cp_lexer_set_source_position_from_token (token);
1114
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer))
1117 {
1118 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1119 cp_lexer_print_token (cp_lexer_debug_stream, token);
1120 putc ('\n', cp_lexer_debug_stream);
1121 }
1122
1123 return token;
1124 }
1125
1126 /* Permanently remove the next token from the token stream, and
1127 advance the next_token pointer to refer to the next non-purged
1128 token. */
1129
1130 static void
1131 cp_lexer_purge_token (cp_lexer *lexer)
1132 {
1133 cp_token *tok = lexer->next_token;
1134
1135 gcc_assert (tok != &eof_token);
1136 tok->purged_p = true;
1137 tok->location = UNKNOWN_LOCATION;
1138 tok->u.value = NULL_TREE;
1139 tok->keyword = RID_MAX;
1140
1141 do
1142 {
1143 tok++;
1144 if (tok == lexer->last_token)
1145 {
1146 tok = &eof_token;
1147 break;
1148 }
1149 }
1150 while (tok->purged_p);
1151 lexer->next_token = tok;
1152 }
1153
1154 /* Permanently remove all tokens after TOK, up to, but not
1155 including, the token that will be returned next by
1156 cp_lexer_peek_token. */
1157
1158 static void
1159 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1160 {
1161 cp_token *peek = lexer->next_token;
1162
1163 if (peek == &eof_token)
1164 peek = lexer->last_token;
1165
1166 gcc_assert (tok < peek);
1167
1168 for ( tok += 1; tok != peek; tok += 1)
1169 {
1170 tok->purged_p = true;
1171 tok->location = UNKNOWN_LOCATION;
1172 tok->u.value = NULL_TREE;
1173 tok->keyword = RID_MAX;
1174 }
1175 }
1176
1177 /* Begin saving tokens. All tokens consumed after this point will be
1178 preserved. */
1179
1180 static void
1181 cp_lexer_save_tokens (cp_lexer* lexer)
1182 {
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer))
1185 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1186
1187 lexer->saved_tokens.safe_push (lexer->next_token);
1188 }
1189
1190 /* Commit to the portion of the token stream most recently saved. */
1191
1192 static void
1193 cp_lexer_commit_tokens (cp_lexer* lexer)
1194 {
1195 /* Provide debugging output. */
1196 if (cp_lexer_debugging_p (lexer))
1197 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1198
1199 lexer->saved_tokens.pop ();
1200 }
1201
1202 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1203 to the token stream. Stop saving tokens. */
1204
1205 static void
1206 cp_lexer_rollback_tokens (cp_lexer* lexer)
1207 {
1208 /* Provide debugging output. */
1209 if (cp_lexer_debugging_p (lexer))
1210 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1211
1212 lexer->next_token = lexer->saved_tokens.pop ();
1213 }
1214
1215 /* RAII wrapper around the above functions, with sanity checking. Creating
1216 a variable saves tokens, which are committed when the variable is
1217 destroyed unless they are explicitly rolled back by calling the rollback
1218 member function. */
1219
1220 struct saved_token_sentinel
1221 {
1222 cp_lexer *lexer;
1223 unsigned len;
1224 bool commit;
1225 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1226 {
1227 len = lexer->saved_tokens.length ();
1228 cp_lexer_save_tokens (lexer);
1229 }
1230 void rollback ()
1231 {
1232 cp_lexer_rollback_tokens (lexer);
1233 commit = false;
1234 }
1235 ~saved_token_sentinel()
1236 {
1237 if (commit)
1238 cp_lexer_commit_tokens (lexer);
1239 gcc_assert (lexer->saved_tokens.length () == len);
1240 }
1241 };
1242
1243 /* Print a representation of the TOKEN on the STREAM. */
1244
1245 static void
1246 cp_lexer_print_token (FILE * stream, cp_token *token)
1247 {
1248 /* We don't use cpp_type2name here because the parser defines
1249 a few tokens of its own. */
1250 static const char *const token_names[] = {
1251 /* cpplib-defined token types */
1252 #define OP(e, s) #e,
1253 #define TK(e, s) #e,
1254 TTYPE_TABLE
1255 #undef OP
1256 #undef TK
1257 /* C++ parser token types - see "Manifest constants", above. */
1258 "KEYWORD",
1259 "TEMPLATE_ID",
1260 "NESTED_NAME_SPECIFIER",
1261 };
1262
1263 /* For some tokens, print the associated data. */
1264 switch (token->type)
1265 {
1266 case CPP_KEYWORD:
1267 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1268 For example, `struct' is mapped to an INTEGER_CST. */
1269 if (!identifier_p (token->u.value))
1270 break;
1271 /* fall through */
1272 case CPP_NAME:
1273 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1274 break;
1275
1276 case CPP_STRING:
1277 case CPP_STRING16:
1278 case CPP_STRING32:
1279 case CPP_WSTRING:
1280 case CPP_UTF8STRING:
1281 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1282 break;
1283
1284 case CPP_NUMBER:
1285 print_generic_expr (stream, token->u.value);
1286 break;
1287
1288 default:
1289 /* If we have a name for the token, print it out. Otherwise, we
1290 simply give the numeric code. */
1291 if (token->type < ARRAY_SIZE(token_names))
1292 fputs (token_names[token->type], stream);
1293 else
1294 fprintf (stream, "[%d]", token->type);
1295 break;
1296 }
1297 }
1298
1299 DEBUG_FUNCTION void
1300 debug (cp_token &ref)
1301 {
1302 cp_lexer_print_token (stderr, &ref);
1303 fprintf (stderr, "\n");
1304 }
1305
1306 DEBUG_FUNCTION void
1307 debug (cp_token *ptr)
1308 {
1309 if (ptr)
1310 debug (*ptr);
1311 else
1312 fprintf (stderr, "<nil>\n");
1313 }
1314
1315
1316 /* Start emitting debugging information. */
1317
1318 static void
1319 cp_lexer_start_debugging (cp_lexer* lexer)
1320 {
1321 if (!LEXER_DEBUGGING_ENABLED_P)
1322 fatal_error (input_location,
1323 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1324
1325 lexer->debugging_p = true;
1326 cp_lexer_debug_stream = stderr;
1327 }
1328
1329 /* Stop emitting debugging information. */
1330
1331 static void
1332 cp_lexer_stop_debugging (cp_lexer* lexer)
1333 {
1334 if (!LEXER_DEBUGGING_ENABLED_P)
1335 fatal_error (input_location,
1336 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1337
1338 lexer->debugging_p = false;
1339 cp_lexer_debug_stream = NULL;
1340 }
1341
1342 /* Create a new cp_token_cache, representing a range of tokens. */
1343
1344 static cp_token_cache *
1345 cp_token_cache_new (cp_token *first, cp_token *last)
1346 {
1347 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1348 cache->first = first;
1349 cache->last = last;
1350 return cache;
1351 }
1352
1353 /* Diagnose if #pragma omp declare simd isn't followed immediately
1354 by function declaration or definition. */
1355
1356 static inline void
1357 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1358 {
1359 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1360 {
1361 error ("%<#pragma omp declare simd%> not immediately followed by "
1362 "function declaration or definition");
1363 parser->omp_declare_simd = NULL;
1364 }
1365 }
1366
1367 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1368 and put that into "omp declare simd" attribute. */
1369
1370 static inline void
1371 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1372 {
1373 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1374 {
1375 if (fndecl == error_mark_node)
1376 {
1377 parser->omp_declare_simd = NULL;
1378 return;
1379 }
1380 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1381 {
1382 cp_ensure_no_omp_declare_simd (parser);
1383 return;
1384 }
1385 }
1386 }
1387
1388 /* Diagnose if #pragma acc routine isn't followed immediately by function
1389 declaration or definition. */
1390
1391 static inline void
1392 cp_ensure_no_oacc_routine (cp_parser *parser)
1393 {
1394 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1395 {
1396 error_at (parser->oacc_routine->loc,
1397 "%<#pragma acc routine%> not immediately followed by "
1398 "function declaration or definition");
1399 parser->oacc_routine = NULL;
1400 }
1401 }
1402 \f
1403 /* Decl-specifiers. */
1404
1405 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1406
1407 static void
1408 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1409 {
1410 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1411 }
1412
1413 /* Declarators. */
1414
1415 /* Nothing other than the parser should be creating declarators;
1416 declarators are a semi-syntactic representation of C++ entities.
1417 Other parts of the front end that need to create entities (like
1418 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1419
1420 static cp_declarator *make_call_declarator
1421 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1422 static cp_declarator *make_array_declarator
1423 (cp_declarator *, tree);
1424 static cp_declarator *make_pointer_declarator
1425 (cp_cv_quals, cp_declarator *, tree);
1426 static cp_declarator *make_reference_declarator
1427 (cp_cv_quals, cp_declarator *, bool, tree);
1428 static cp_declarator *make_ptrmem_declarator
1429 (cp_cv_quals, tree, cp_declarator *, tree);
1430
1431 /* An erroneous declarator. */
1432 static cp_declarator *cp_error_declarator;
1433
1434 /* The obstack on which declarators and related data structures are
1435 allocated. */
1436 static struct obstack declarator_obstack;
1437
1438 /* Alloc BYTES from the declarator memory pool. */
1439
1440 static inline void *
1441 alloc_declarator (size_t bytes)
1442 {
1443 return obstack_alloc (&declarator_obstack, bytes);
1444 }
1445
1446 /* Allocate a declarator of the indicated KIND. Clear fields that are
1447 common to all declarators. */
1448
1449 static cp_declarator *
1450 make_declarator (cp_declarator_kind kind)
1451 {
1452 cp_declarator *declarator;
1453
1454 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1455 declarator->kind = kind;
1456 declarator->parenthesized = UNKNOWN_LOCATION;
1457 declarator->attributes = NULL_TREE;
1458 declarator->std_attributes = NULL_TREE;
1459 declarator->declarator = NULL;
1460 declarator->parameter_pack_p = false;
1461 declarator->id_loc = UNKNOWN_LOCATION;
1462
1463 return declarator;
1464 }
1465
1466 /* Make a declarator for a generalized identifier. If
1467 QUALIFYING_SCOPE is non-NULL, the identifier is
1468 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1469 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1470 is, if any. */
1471
1472 static cp_declarator *
1473 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1474 special_function_kind sfk)
1475 {
1476 cp_declarator *declarator;
1477
1478 /* It is valid to write:
1479
1480 class C { void f(); };
1481 typedef C D;
1482 void D::f();
1483
1484 The standard is not clear about whether `typedef const C D' is
1485 legal; as of 2002-09-15 the committee is considering that
1486 question. EDG 3.0 allows that syntax. Therefore, we do as
1487 well. */
1488 if (qualifying_scope && TYPE_P (qualifying_scope))
1489 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1490
1491 gcc_assert (identifier_p (unqualified_name)
1492 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1493 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1494
1495 declarator = make_declarator (cdk_id);
1496 declarator->u.id.qualifying_scope = qualifying_scope;
1497 declarator->u.id.unqualified_name = unqualified_name;
1498 declarator->u.id.sfk = sfk;
1499
1500 return declarator;
1501 }
1502
1503 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1504 of modifiers such as const or volatile to apply to the pointer
1505 type, represented as identifiers. ATTRIBUTES represent the attributes that
1506 appertain to the pointer or reference. */
1507
1508 cp_declarator *
1509 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1510 tree attributes)
1511 {
1512 cp_declarator *declarator;
1513
1514 declarator = make_declarator (cdk_pointer);
1515 declarator->declarator = target;
1516 declarator->u.pointer.qualifiers = cv_qualifiers;
1517 declarator->u.pointer.class_type = NULL_TREE;
1518 if (target)
1519 {
1520 declarator->id_loc = target->id_loc;
1521 declarator->parameter_pack_p = target->parameter_pack_p;
1522 target->parameter_pack_p = false;
1523 }
1524 else
1525 declarator->parameter_pack_p = false;
1526
1527 declarator->std_attributes = attributes;
1528
1529 return declarator;
1530 }
1531
1532 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1533 represent the attributes that appertain to the pointer or
1534 reference. */
1535
1536 cp_declarator *
1537 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1538 bool rvalue_ref, tree attributes)
1539 {
1540 cp_declarator *declarator;
1541
1542 declarator = make_declarator (cdk_reference);
1543 declarator->declarator = target;
1544 declarator->u.reference.qualifiers = cv_qualifiers;
1545 declarator->u.reference.rvalue_ref = rvalue_ref;
1546 if (target)
1547 {
1548 declarator->id_loc = target->id_loc;
1549 declarator->parameter_pack_p = target->parameter_pack_p;
1550 target->parameter_pack_p = false;
1551 }
1552 else
1553 declarator->parameter_pack_p = false;
1554
1555 declarator->std_attributes = attributes;
1556
1557 return declarator;
1558 }
1559
1560 /* Like make_pointer_declarator -- but for a pointer to a non-static
1561 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1562 appertain to the pointer or reference. */
1563
1564 cp_declarator *
1565 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1566 cp_declarator *pointee,
1567 tree attributes)
1568 {
1569 cp_declarator *declarator;
1570
1571 declarator = make_declarator (cdk_ptrmem);
1572 declarator->declarator = pointee;
1573 declarator->u.pointer.qualifiers = cv_qualifiers;
1574 declarator->u.pointer.class_type = class_type;
1575
1576 if (pointee)
1577 {
1578 declarator->parameter_pack_p = pointee->parameter_pack_p;
1579 pointee->parameter_pack_p = false;
1580 }
1581 else
1582 declarator->parameter_pack_p = false;
1583
1584 declarator->std_attributes = attributes;
1585
1586 return declarator;
1587 }
1588
1589 /* Make a declarator for the function given by TARGET, with the
1590 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1591 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1592 indicates what exceptions can be thrown. */
1593
1594 cp_declarator *
1595 make_call_declarator (cp_declarator *target,
1596 tree parms,
1597 cp_cv_quals cv_qualifiers,
1598 cp_virt_specifiers virt_specifiers,
1599 cp_ref_qualifier ref_qualifier,
1600 tree tx_qualifier,
1601 tree exception_specification,
1602 tree late_return_type,
1603 tree requires_clause)
1604 {
1605 cp_declarator *declarator;
1606
1607 declarator = make_declarator (cdk_function);
1608 declarator->declarator = target;
1609 declarator->u.function.parameters = parms;
1610 declarator->u.function.qualifiers = cv_qualifiers;
1611 declarator->u.function.virt_specifiers = virt_specifiers;
1612 declarator->u.function.ref_qualifier = ref_qualifier;
1613 declarator->u.function.tx_qualifier = tx_qualifier;
1614 declarator->u.function.exception_specification = exception_specification;
1615 declarator->u.function.late_return_type = late_return_type;
1616 declarator->u.function.requires_clause = requires_clause;
1617 if (target)
1618 {
1619 declarator->id_loc = target->id_loc;
1620 declarator->parameter_pack_p = target->parameter_pack_p;
1621 target->parameter_pack_p = false;
1622 }
1623 else
1624 declarator->parameter_pack_p = false;
1625
1626 return declarator;
1627 }
1628
1629 /* Make a declarator for an array of BOUNDS elements, each of which is
1630 defined by ELEMENT. */
1631
1632 cp_declarator *
1633 make_array_declarator (cp_declarator *element, tree bounds)
1634 {
1635 cp_declarator *declarator;
1636
1637 declarator = make_declarator (cdk_array);
1638 declarator->declarator = element;
1639 declarator->u.array.bounds = bounds;
1640 if (element)
1641 {
1642 declarator->id_loc = element->id_loc;
1643 declarator->parameter_pack_p = element->parameter_pack_p;
1644 element->parameter_pack_p = false;
1645 }
1646 else
1647 declarator->parameter_pack_p = false;
1648
1649 return declarator;
1650 }
1651
1652 /* Determine whether the declarator we've seen so far can be a
1653 parameter pack, when followed by an ellipsis. */
1654 static bool
1655 declarator_can_be_parameter_pack (cp_declarator *declarator)
1656 {
1657 if (declarator && declarator->parameter_pack_p)
1658 /* We already saw an ellipsis. */
1659 return false;
1660
1661 /* Search for a declarator name, or any other declarator that goes
1662 after the point where the ellipsis could appear in a parameter
1663 pack. If we find any of these, then this declarator can not be
1664 made into a parameter pack. */
1665 bool found = false;
1666 while (declarator && !found)
1667 {
1668 switch ((int)declarator->kind)
1669 {
1670 case cdk_id:
1671 case cdk_array:
1672 case cdk_decomp:
1673 found = true;
1674 break;
1675
1676 case cdk_error:
1677 return true;
1678
1679 default:
1680 declarator = declarator->declarator;
1681 break;
1682 }
1683 }
1684
1685 return !found;
1686 }
1687
1688 cp_parameter_declarator *no_parameters;
1689
1690 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1691 DECLARATOR and DEFAULT_ARGUMENT. */
1692
1693 cp_parameter_declarator *
1694 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1695 cp_declarator *declarator,
1696 tree default_argument,
1697 location_t loc,
1698 bool template_parameter_pack_p = false)
1699 {
1700 cp_parameter_declarator *parameter;
1701
1702 parameter = ((cp_parameter_declarator *)
1703 alloc_declarator (sizeof (cp_parameter_declarator)));
1704 parameter->next = NULL;
1705 if (decl_specifiers)
1706 parameter->decl_specifiers = *decl_specifiers;
1707 else
1708 clear_decl_specs (&parameter->decl_specifiers);
1709 parameter->declarator = declarator;
1710 parameter->default_argument = default_argument;
1711 parameter->template_parameter_pack_p = template_parameter_pack_p;
1712 parameter->loc = loc;
1713
1714 return parameter;
1715 }
1716
1717 /* Returns true iff DECLARATOR is a declaration for a function. */
1718
1719 static bool
1720 function_declarator_p (const cp_declarator *declarator)
1721 {
1722 while (declarator)
1723 {
1724 if (declarator->kind == cdk_function
1725 && declarator->declarator->kind == cdk_id)
1726 return true;
1727 if (declarator->kind == cdk_id
1728 || declarator->kind == cdk_decomp
1729 || declarator->kind == cdk_error)
1730 return false;
1731 declarator = declarator->declarator;
1732 }
1733 return false;
1734 }
1735
1736 /* The parser. */
1737
1738 /* Overview
1739 --------
1740
1741 A cp_parser parses the token stream as specified by the C++
1742 grammar. Its job is purely parsing, not semantic analysis. For
1743 example, the parser breaks the token stream into declarators,
1744 expressions, statements, and other similar syntactic constructs.
1745 It does not check that the types of the expressions on either side
1746 of an assignment-statement are compatible, or that a function is
1747 not declared with a parameter of type `void'.
1748
1749 The parser invokes routines elsewhere in the compiler to perform
1750 semantic analysis and to build up the abstract syntax tree for the
1751 code processed.
1752
1753 The parser (and the template instantiation code, which is, in a
1754 way, a close relative of parsing) are the only parts of the
1755 compiler that should be calling push_scope and pop_scope, or
1756 related functions. The parser (and template instantiation code)
1757 keeps track of what scope is presently active; everything else
1758 should simply honor that. (The code that generates static
1759 initializers may also need to set the scope, in order to check
1760 access control correctly when emitting the initializers.)
1761
1762 Methodology
1763 -----------
1764
1765 The parser is of the standard recursive-descent variety. Upcoming
1766 tokens in the token stream are examined in order to determine which
1767 production to use when parsing a non-terminal. Some C++ constructs
1768 require arbitrary look ahead to disambiguate. For example, it is
1769 impossible, in the general case, to tell whether a statement is an
1770 expression or declaration without scanning the entire statement.
1771 Therefore, the parser is capable of "parsing tentatively." When the
1772 parser is not sure what construct comes next, it enters this mode.
1773 Then, while we attempt to parse the construct, the parser queues up
1774 error messages, rather than issuing them immediately, and saves the
1775 tokens it consumes. If the construct is parsed successfully, the
1776 parser "commits", i.e., it issues any queued error messages and
1777 the tokens that were being preserved are permanently discarded.
1778 If, however, the construct is not parsed successfully, the parser
1779 rolls back its state completely so that it can resume parsing using
1780 a different alternative.
1781
1782 Future Improvements
1783 -------------------
1784
1785 The performance of the parser could probably be improved substantially.
1786 We could often eliminate the need to parse tentatively by looking ahead
1787 a little bit. In some places, this approach might not entirely eliminate
1788 the need to parse tentatively, but it might still speed up the average
1789 case. */
1790
1791 /* Flags that are passed to some parsing functions. These values can
1792 be bitwise-ored together. */
1793
1794 enum
1795 {
1796 /* No flags. */
1797 CP_PARSER_FLAGS_NONE = 0x0,
1798 /* The construct is optional. If it is not present, then no error
1799 should be issued. */
1800 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1801 /* When parsing a type-specifier, treat user-defined type-names
1802 as non-type identifiers. */
1803 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1804 /* When parsing a type-specifier, do not try to parse a class-specifier
1805 or enum-specifier. */
1806 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1807 /* When parsing a decl-specifier-seq, only allow type-specifier or
1808 constexpr. */
1809 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1810 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1811 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1812 };
1813
1814 /* This type is used for parameters and variables which hold
1815 combinations of the above flags. */
1816 typedef int cp_parser_flags;
1817
1818 /* The different kinds of declarators we want to parse. */
1819
1820 enum cp_parser_declarator_kind
1821 {
1822 /* We want an abstract declarator. */
1823 CP_PARSER_DECLARATOR_ABSTRACT,
1824 /* We want a named declarator. */
1825 CP_PARSER_DECLARATOR_NAMED,
1826 /* We don't mind, but the name must be an unqualified-id. */
1827 CP_PARSER_DECLARATOR_EITHER
1828 };
1829
1830 /* The precedence values used to parse binary expressions. The minimum value
1831 of PREC must be 1, because zero is reserved to quickly discriminate
1832 binary operators from other tokens. */
1833
1834 enum cp_parser_prec
1835 {
1836 PREC_NOT_OPERATOR,
1837 PREC_LOGICAL_OR_EXPRESSION,
1838 PREC_LOGICAL_AND_EXPRESSION,
1839 PREC_INCLUSIVE_OR_EXPRESSION,
1840 PREC_EXCLUSIVE_OR_EXPRESSION,
1841 PREC_AND_EXPRESSION,
1842 PREC_EQUALITY_EXPRESSION,
1843 PREC_RELATIONAL_EXPRESSION,
1844 PREC_SHIFT_EXPRESSION,
1845 PREC_ADDITIVE_EXPRESSION,
1846 PREC_MULTIPLICATIVE_EXPRESSION,
1847 PREC_PM_EXPRESSION,
1848 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1849 };
1850
1851 /* A mapping from a token type to a corresponding tree node type, with a
1852 precedence value. */
1853
1854 struct cp_parser_binary_operations_map_node
1855 {
1856 /* The token type. */
1857 enum cpp_ttype token_type;
1858 /* The corresponding tree code. */
1859 enum tree_code tree_type;
1860 /* The precedence of this operator. */
1861 enum cp_parser_prec prec;
1862 };
1863
1864 struct cp_parser_expression_stack_entry
1865 {
1866 /* Left hand side of the binary operation we are currently
1867 parsing. */
1868 cp_expr lhs;
1869 /* Original tree code for left hand side, if it was a binary
1870 expression itself (used for -Wparentheses). */
1871 enum tree_code lhs_type;
1872 /* Tree code for the binary operation we are parsing. */
1873 enum tree_code tree_type;
1874 /* Precedence of the binary operation we are parsing. */
1875 enum cp_parser_prec prec;
1876 /* Location of the binary operation we are parsing. */
1877 location_t loc;
1878 };
1879
1880 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1881 entries because precedence levels on the stack are monotonically
1882 increasing. */
1883 typedef struct cp_parser_expression_stack_entry
1884 cp_parser_expression_stack[NUM_PREC_VALUES];
1885
1886 /* Prototypes. */
1887
1888 /* Constructors and destructors. */
1889
1890 static cp_parser_context *cp_parser_context_new
1891 (cp_parser_context *);
1892
1893 /* Class variables. */
1894
1895 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1896
1897 /* The operator-precedence table used by cp_parser_binary_expression.
1898 Transformed into an associative array (binops_by_token) by
1899 cp_parser_new. */
1900
1901 static const cp_parser_binary_operations_map_node binops[] = {
1902 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1903 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1904
1905 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1906 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1907 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1908
1909 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1910 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1911
1912 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1913 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1914
1915 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1916 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1917 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1918 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1919
1920 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1921 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1922
1923 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1924
1925 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1926
1927 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1928
1929 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1930
1931 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1932 };
1933
1934 /* The same as binops, but initialized by cp_parser_new so that
1935 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1936 for speed. */
1937 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1938
1939 /* Constructors and destructors. */
1940
1941 /* Construct a new context. The context below this one on the stack
1942 is given by NEXT. */
1943
1944 static cp_parser_context *
1945 cp_parser_context_new (cp_parser_context* next)
1946 {
1947 cp_parser_context *context;
1948
1949 /* Allocate the storage. */
1950 if (cp_parser_context_free_list != NULL)
1951 {
1952 /* Pull the first entry from the free list. */
1953 context = cp_parser_context_free_list;
1954 cp_parser_context_free_list = context->next;
1955 memset (context, 0, sizeof (*context));
1956 }
1957 else
1958 context = ggc_cleared_alloc<cp_parser_context> ();
1959
1960 /* No errors have occurred yet in this context. */
1961 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1962 /* If this is not the bottommost context, copy information that we
1963 need from the previous context. */
1964 if (next)
1965 {
1966 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1967 expression, then we are parsing one in this context, too. */
1968 context->object_type = next->object_type;
1969 /* Thread the stack. */
1970 context->next = next;
1971 }
1972
1973 return context;
1974 }
1975
1976 /* Managing the unparsed function queues. */
1977
1978 #define unparsed_funs_with_default_args \
1979 parser->unparsed_queues->last ().funs_with_default_args
1980 #define unparsed_funs_with_definitions \
1981 parser->unparsed_queues->last ().funs_with_definitions
1982 #define unparsed_nsdmis \
1983 parser->unparsed_queues->last ().nsdmis
1984 #define unparsed_classes \
1985 parser->unparsed_queues->last ().classes
1986
1987 static void
1988 push_unparsed_function_queues (cp_parser *parser)
1989 {
1990 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1991 vec_safe_push (parser->unparsed_queues, e);
1992 }
1993
1994 static void
1995 pop_unparsed_function_queues (cp_parser *parser)
1996 {
1997 release_tree_vector (unparsed_funs_with_definitions);
1998 parser->unparsed_queues->pop ();
1999 }
2000
2001 /* Prototypes. */
2002
2003 /* Constructors and destructors. */
2004
2005 static cp_parser *cp_parser_new
2006 (void);
2007
2008 /* Routines to parse various constructs.
2009
2010 Those that return `tree' will return the error_mark_node (rather
2011 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2012 Sometimes, they will return an ordinary node if error-recovery was
2013 attempted, even though a parse error occurred. So, to check
2014 whether or not a parse error occurred, you should always use
2015 cp_parser_error_occurred. If the construct is optional (indicated
2016 either by an `_opt' in the name of the function that does the
2017 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2018 the construct is not present. */
2019
2020 /* Lexical conventions [gram.lex] */
2021
2022 static cp_expr cp_parser_identifier
2023 (cp_parser *);
2024 static cp_expr cp_parser_string_literal
2025 (cp_parser *, bool, bool, bool);
2026 static cp_expr cp_parser_userdef_char_literal
2027 (cp_parser *);
2028 static tree cp_parser_userdef_string_literal
2029 (tree);
2030 static cp_expr cp_parser_userdef_numeric_literal
2031 (cp_parser *);
2032
2033 /* Basic concepts [gram.basic] */
2034
2035 static bool cp_parser_translation_unit
2036 (cp_parser *);
2037
2038 /* Expressions [gram.expr] */
2039
2040 static cp_expr cp_parser_primary_expression
2041 (cp_parser *, bool, bool, bool, cp_id_kind *);
2042 static cp_expr cp_parser_id_expression
2043 (cp_parser *, bool, bool, bool *, bool, bool);
2044 static cp_expr cp_parser_unqualified_id
2045 (cp_parser *, bool, bool, bool, bool);
2046 static tree cp_parser_nested_name_specifier_opt
2047 (cp_parser *, bool, bool, bool, bool, bool = false);
2048 static tree cp_parser_nested_name_specifier
2049 (cp_parser *, bool, bool, bool, bool);
2050 static tree cp_parser_qualifying_entity
2051 (cp_parser *, bool, bool, bool, bool, bool);
2052 static cp_expr cp_parser_postfix_expression
2053 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2054 static tree cp_parser_postfix_open_square_expression
2055 (cp_parser *, tree, bool, bool);
2056 static tree cp_parser_postfix_dot_deref_expression
2057 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2058 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2059 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2060 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2061 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2062 static void cp_parser_pseudo_destructor_name
2063 (cp_parser *, tree, tree *, tree *);
2064 static cp_expr cp_parser_unary_expression
2065 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2066 static enum tree_code cp_parser_unary_operator
2067 (cp_token *);
2068 static tree cp_parser_new_expression
2069 (cp_parser *);
2070 static vec<tree, va_gc> *cp_parser_new_placement
2071 (cp_parser *);
2072 static tree cp_parser_new_type_id
2073 (cp_parser *, tree *);
2074 static cp_declarator *cp_parser_new_declarator_opt
2075 (cp_parser *);
2076 static cp_declarator *cp_parser_direct_new_declarator
2077 (cp_parser *);
2078 static vec<tree, va_gc> *cp_parser_new_initializer
2079 (cp_parser *);
2080 static tree cp_parser_delete_expression
2081 (cp_parser *);
2082 static cp_expr cp_parser_cast_expression
2083 (cp_parser *, bool, bool, bool, cp_id_kind *);
2084 static cp_expr cp_parser_binary_expression
2085 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2086 static tree cp_parser_question_colon_clause
2087 (cp_parser *, cp_expr);
2088 static cp_expr cp_parser_assignment_expression
2089 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2090 static enum tree_code cp_parser_assignment_operator_opt
2091 (cp_parser *);
2092 static cp_expr cp_parser_expression
2093 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2094 static cp_expr cp_parser_constant_expression
2095 (cp_parser *, bool = false, bool * = NULL, bool = false);
2096 static cp_expr cp_parser_builtin_offsetof
2097 (cp_parser *);
2098 static cp_expr cp_parser_lambda_expression
2099 (cp_parser *);
2100 static void cp_parser_lambda_introducer
2101 (cp_parser *, tree);
2102 static bool cp_parser_lambda_declarator_opt
2103 (cp_parser *, tree);
2104 static void cp_parser_lambda_body
2105 (cp_parser *, tree);
2106
2107 /* Statements [gram.stmt.stmt] */
2108
2109 static void cp_parser_statement
2110 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2111 static void cp_parser_label_for_labeled_statement
2112 (cp_parser *, tree);
2113 static tree cp_parser_expression_statement
2114 (cp_parser *, tree);
2115 static tree cp_parser_compound_statement
2116 (cp_parser *, tree, int, bool);
2117 static void cp_parser_statement_seq_opt
2118 (cp_parser *, tree);
2119 static tree cp_parser_selection_statement
2120 (cp_parser *, bool *, vec<tree> *);
2121 static tree cp_parser_condition
2122 (cp_parser *);
2123 static tree cp_parser_iteration_statement
2124 (cp_parser *, bool *, bool);
2125 static bool cp_parser_init_statement
2126 (cp_parser *, tree *decl);
2127 static tree cp_parser_for
2128 (cp_parser *, bool);
2129 static tree cp_parser_c_for
2130 (cp_parser *, tree, tree, bool);
2131 static tree cp_parser_range_for
2132 (cp_parser *, tree, tree, tree, bool);
2133 static void do_range_for_auto_deduction
2134 (tree, tree);
2135 static tree cp_parser_perform_range_for_lookup
2136 (tree, tree *, tree *);
2137 static tree cp_parser_range_for_member_function
2138 (tree, tree);
2139 static tree cp_parser_jump_statement
2140 (cp_parser *);
2141 static void cp_parser_declaration_statement
2142 (cp_parser *);
2143
2144 static tree cp_parser_implicitly_scoped_statement
2145 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2146 static void cp_parser_already_scoped_statement
2147 (cp_parser *, bool *, const token_indent_info &);
2148
2149 /* Declarations [gram.dcl.dcl] */
2150
2151 static void cp_parser_declaration_seq_opt
2152 (cp_parser *);
2153 static void cp_parser_declaration
2154 (cp_parser *);
2155 static void cp_parser_block_declaration
2156 (cp_parser *, bool);
2157 static void cp_parser_simple_declaration
2158 (cp_parser *, bool, tree *);
2159 static void cp_parser_decl_specifier_seq
2160 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2161 static tree cp_parser_storage_class_specifier_opt
2162 (cp_parser *);
2163 static tree cp_parser_function_specifier_opt
2164 (cp_parser *, cp_decl_specifier_seq *);
2165 static tree cp_parser_type_specifier
2166 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2167 int *, bool *);
2168 static tree cp_parser_simple_type_specifier
2169 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2170 static tree cp_parser_type_name
2171 (cp_parser *, bool);
2172 static tree cp_parser_type_name
2173 (cp_parser *);
2174 static tree cp_parser_nonclass_name
2175 (cp_parser* parser);
2176 static tree cp_parser_elaborated_type_specifier
2177 (cp_parser *, bool, bool);
2178 static tree cp_parser_enum_specifier
2179 (cp_parser *);
2180 static void cp_parser_enumerator_list
2181 (cp_parser *, tree);
2182 static void cp_parser_enumerator_definition
2183 (cp_parser *, tree);
2184 static tree cp_parser_namespace_name
2185 (cp_parser *);
2186 static void cp_parser_namespace_definition
2187 (cp_parser *);
2188 static void cp_parser_namespace_body
2189 (cp_parser *);
2190 static tree cp_parser_qualified_namespace_specifier
2191 (cp_parser *);
2192 static void cp_parser_namespace_alias_definition
2193 (cp_parser *);
2194 static bool cp_parser_using_declaration
2195 (cp_parser *, bool);
2196 static void cp_parser_using_directive
2197 (cp_parser *);
2198 static tree cp_parser_alias_declaration
2199 (cp_parser *);
2200 static void cp_parser_asm_definition
2201 (cp_parser *);
2202 static void cp_parser_linkage_specification
2203 (cp_parser *);
2204 static void cp_parser_static_assert
2205 (cp_parser *, bool);
2206 static tree cp_parser_decltype
2207 (cp_parser *);
2208 static tree cp_parser_decomposition_declaration
2209 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2210
2211 /* Declarators [gram.dcl.decl] */
2212
2213 static tree cp_parser_init_declarator
2214 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2215 bool, bool, int, bool *, tree *, location_t *, tree *);
2216 static cp_declarator *cp_parser_declarator
2217 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2218 static cp_declarator *cp_parser_direct_declarator
2219 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2220 static enum tree_code cp_parser_ptr_operator
2221 (cp_parser *, tree *, cp_cv_quals *, tree *);
2222 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2223 (cp_parser *);
2224 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2225 (cp_parser *);
2226 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2227 (cp_parser *);
2228 static tree cp_parser_tx_qualifier_opt
2229 (cp_parser *);
2230 static tree cp_parser_late_return_type_opt
2231 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2232 static tree cp_parser_declarator_id
2233 (cp_parser *, bool);
2234 static tree cp_parser_type_id
2235 (cp_parser *);
2236 static tree cp_parser_template_type_arg
2237 (cp_parser *);
2238 static tree cp_parser_trailing_type_id (cp_parser *);
2239 static tree cp_parser_type_id_1
2240 (cp_parser *, bool, bool);
2241 static void cp_parser_type_specifier_seq
2242 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2243 static tree cp_parser_parameter_declaration_clause
2244 (cp_parser *);
2245 static tree cp_parser_parameter_declaration_list
2246 (cp_parser *, bool *);
2247 static cp_parameter_declarator *cp_parser_parameter_declaration
2248 (cp_parser *, bool, bool *);
2249 static tree cp_parser_default_argument
2250 (cp_parser *, bool);
2251 static void cp_parser_function_body
2252 (cp_parser *, bool);
2253 static tree cp_parser_initializer
2254 (cp_parser *, bool *, bool *);
2255 static cp_expr cp_parser_initializer_clause
2256 (cp_parser *, bool *);
2257 static cp_expr cp_parser_braced_list
2258 (cp_parser*, bool*);
2259 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2260 (cp_parser *, bool *);
2261
2262 static void cp_parser_ctor_initializer_opt_and_function_body
2263 (cp_parser *, bool);
2264
2265 static tree cp_parser_late_parsing_omp_declare_simd
2266 (cp_parser *, tree);
2267
2268 static tree cp_parser_late_parsing_cilk_simd_fn_info
2269 (cp_parser *, tree);
2270
2271 static tree cp_parser_late_parsing_oacc_routine
2272 (cp_parser *, tree);
2273
2274 static tree synthesize_implicit_template_parm
2275 (cp_parser *, tree);
2276 static tree finish_fully_implicit_template
2277 (cp_parser *, tree);
2278
2279 /* Classes [gram.class] */
2280
2281 static tree cp_parser_class_name
2282 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2283 static tree cp_parser_class_specifier
2284 (cp_parser *);
2285 static tree cp_parser_class_head
2286 (cp_parser *, bool *);
2287 static enum tag_types cp_parser_class_key
2288 (cp_parser *);
2289 static void cp_parser_type_parameter_key
2290 (cp_parser* parser);
2291 static void cp_parser_member_specification_opt
2292 (cp_parser *);
2293 static void cp_parser_member_declaration
2294 (cp_parser *);
2295 static tree cp_parser_pure_specifier
2296 (cp_parser *);
2297 static tree cp_parser_constant_initializer
2298 (cp_parser *);
2299
2300 /* Derived classes [gram.class.derived] */
2301
2302 static tree cp_parser_base_clause
2303 (cp_parser *);
2304 static tree cp_parser_base_specifier
2305 (cp_parser *);
2306
2307 /* Special member functions [gram.special] */
2308
2309 static tree cp_parser_conversion_function_id
2310 (cp_parser *);
2311 static tree cp_parser_conversion_type_id
2312 (cp_parser *);
2313 static cp_declarator *cp_parser_conversion_declarator_opt
2314 (cp_parser *);
2315 static void cp_parser_ctor_initializer_opt
2316 (cp_parser *);
2317 static void cp_parser_mem_initializer_list
2318 (cp_parser *);
2319 static tree cp_parser_mem_initializer
2320 (cp_parser *);
2321 static tree cp_parser_mem_initializer_id
2322 (cp_parser *);
2323
2324 /* Overloading [gram.over] */
2325
2326 static cp_expr cp_parser_operator_function_id
2327 (cp_parser *);
2328 static cp_expr cp_parser_operator
2329 (cp_parser *);
2330
2331 /* Templates [gram.temp] */
2332
2333 static void cp_parser_template_declaration
2334 (cp_parser *, bool);
2335 static tree cp_parser_template_parameter_list
2336 (cp_parser *);
2337 static tree cp_parser_template_parameter
2338 (cp_parser *, bool *, bool *);
2339 static tree cp_parser_type_parameter
2340 (cp_parser *, bool *);
2341 static tree cp_parser_template_id
2342 (cp_parser *, bool, bool, enum tag_types, bool);
2343 static tree cp_parser_template_name
2344 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2345 static tree cp_parser_template_argument_list
2346 (cp_parser *);
2347 static tree cp_parser_template_argument
2348 (cp_parser *);
2349 static void cp_parser_explicit_instantiation
2350 (cp_parser *);
2351 static void cp_parser_explicit_specialization
2352 (cp_parser *);
2353
2354 /* Exception handling [gram.exception] */
2355
2356 static tree cp_parser_try_block
2357 (cp_parser *);
2358 static void cp_parser_function_try_block
2359 (cp_parser *);
2360 static void cp_parser_handler_seq
2361 (cp_parser *);
2362 static void cp_parser_handler
2363 (cp_parser *);
2364 static tree cp_parser_exception_declaration
2365 (cp_parser *);
2366 static tree cp_parser_throw_expression
2367 (cp_parser *);
2368 static tree cp_parser_exception_specification_opt
2369 (cp_parser *);
2370 static tree cp_parser_type_id_list
2371 (cp_parser *);
2372
2373 /* GNU Extensions */
2374
2375 static tree cp_parser_asm_specification_opt
2376 (cp_parser *);
2377 static tree cp_parser_asm_operand_list
2378 (cp_parser *);
2379 static tree cp_parser_asm_clobber_list
2380 (cp_parser *);
2381 static tree cp_parser_asm_label_list
2382 (cp_parser *);
2383 static bool cp_next_tokens_can_be_attribute_p
2384 (cp_parser *);
2385 static bool cp_next_tokens_can_be_gnu_attribute_p
2386 (cp_parser *);
2387 static bool cp_next_tokens_can_be_std_attribute_p
2388 (cp_parser *);
2389 static bool cp_nth_tokens_can_be_std_attribute_p
2390 (cp_parser *, size_t);
2391 static bool cp_nth_tokens_can_be_gnu_attribute_p
2392 (cp_parser *, size_t);
2393 static bool cp_nth_tokens_can_be_attribute_p
2394 (cp_parser *, size_t);
2395 static tree cp_parser_attributes_opt
2396 (cp_parser *);
2397 static tree cp_parser_gnu_attributes_opt
2398 (cp_parser *);
2399 static tree cp_parser_gnu_attribute_list
2400 (cp_parser *);
2401 static tree cp_parser_std_attribute
2402 (cp_parser *, tree);
2403 static tree cp_parser_std_attribute_spec
2404 (cp_parser *);
2405 static tree cp_parser_std_attribute_spec_seq
2406 (cp_parser *);
2407 static bool cp_parser_extension_opt
2408 (cp_parser *, int *);
2409 static void cp_parser_label_declaration
2410 (cp_parser *);
2411
2412 /* Concept Extensions */
2413
2414 static tree cp_parser_requires_clause
2415 (cp_parser *);
2416 static tree cp_parser_requires_clause_opt
2417 (cp_parser *);
2418 static tree cp_parser_requires_expression
2419 (cp_parser *);
2420 static tree cp_parser_requirement_parameter_list
2421 (cp_parser *);
2422 static tree cp_parser_requirement_body
2423 (cp_parser *);
2424 static tree cp_parser_requirement_list
2425 (cp_parser *);
2426 static tree cp_parser_requirement
2427 (cp_parser *);
2428 static tree cp_parser_simple_requirement
2429 (cp_parser *);
2430 static tree cp_parser_compound_requirement
2431 (cp_parser *);
2432 static tree cp_parser_type_requirement
2433 (cp_parser *);
2434 static tree cp_parser_nested_requirement
2435 (cp_parser *);
2436
2437 /* Transactional Memory Extensions */
2438
2439 static tree cp_parser_transaction
2440 (cp_parser *, cp_token *);
2441 static tree cp_parser_transaction_expression
2442 (cp_parser *, enum rid);
2443 static void cp_parser_function_transaction
2444 (cp_parser *, enum rid);
2445 static tree cp_parser_transaction_cancel
2446 (cp_parser *);
2447
2448 enum pragma_context {
2449 pragma_external,
2450 pragma_member,
2451 pragma_objc_icode,
2452 pragma_stmt,
2453 pragma_compound
2454 };
2455 static bool cp_parser_pragma
2456 (cp_parser *, enum pragma_context, bool *);
2457
2458 /* Objective-C++ Productions */
2459
2460 static tree cp_parser_objc_message_receiver
2461 (cp_parser *);
2462 static tree cp_parser_objc_message_args
2463 (cp_parser *);
2464 static tree cp_parser_objc_message_expression
2465 (cp_parser *);
2466 static cp_expr cp_parser_objc_encode_expression
2467 (cp_parser *);
2468 static tree cp_parser_objc_defs_expression
2469 (cp_parser *);
2470 static tree cp_parser_objc_protocol_expression
2471 (cp_parser *);
2472 static tree cp_parser_objc_selector_expression
2473 (cp_parser *);
2474 static cp_expr cp_parser_objc_expression
2475 (cp_parser *);
2476 static bool cp_parser_objc_selector_p
2477 (enum cpp_ttype);
2478 static tree cp_parser_objc_selector
2479 (cp_parser *);
2480 static tree cp_parser_objc_protocol_refs_opt
2481 (cp_parser *);
2482 static void cp_parser_objc_declaration
2483 (cp_parser *, tree);
2484 static tree cp_parser_objc_statement
2485 (cp_parser *);
2486 static bool cp_parser_objc_valid_prefix_attributes
2487 (cp_parser *, tree *);
2488 static void cp_parser_objc_at_property_declaration
2489 (cp_parser *) ;
2490 static void cp_parser_objc_at_synthesize_declaration
2491 (cp_parser *) ;
2492 static void cp_parser_objc_at_dynamic_declaration
2493 (cp_parser *) ;
2494 static tree cp_parser_objc_struct_declaration
2495 (cp_parser *) ;
2496
2497 /* Utility Routines */
2498
2499 static cp_expr cp_parser_lookup_name
2500 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2501 static tree cp_parser_lookup_name_simple
2502 (cp_parser *, tree, location_t);
2503 static tree cp_parser_maybe_treat_template_as_class
2504 (tree, bool);
2505 static bool cp_parser_check_declarator_template_parameters
2506 (cp_parser *, cp_declarator *, location_t);
2507 static bool cp_parser_check_template_parameters
2508 (cp_parser *, unsigned, location_t, cp_declarator *);
2509 static cp_expr cp_parser_simple_cast_expression
2510 (cp_parser *);
2511 static tree cp_parser_global_scope_opt
2512 (cp_parser *, bool);
2513 static bool cp_parser_constructor_declarator_p
2514 (cp_parser *, bool);
2515 static tree cp_parser_function_definition_from_specifiers_and_declarator
2516 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2517 static tree cp_parser_function_definition_after_declarator
2518 (cp_parser *, bool);
2519 static bool cp_parser_template_declaration_after_export
2520 (cp_parser *, bool);
2521 static void cp_parser_perform_template_parameter_access_checks
2522 (vec<deferred_access_check, va_gc> *);
2523 static tree cp_parser_single_declaration
2524 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2525 static cp_expr cp_parser_functional_cast
2526 (cp_parser *, tree);
2527 static tree cp_parser_save_member_function_body
2528 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2529 static tree cp_parser_save_nsdmi
2530 (cp_parser *);
2531 static tree cp_parser_enclosed_template_argument_list
2532 (cp_parser *);
2533 static void cp_parser_save_default_args
2534 (cp_parser *, tree);
2535 static void cp_parser_late_parsing_for_member
2536 (cp_parser *, tree);
2537 static tree cp_parser_late_parse_one_default_arg
2538 (cp_parser *, tree, tree, tree);
2539 static void cp_parser_late_parsing_nsdmi
2540 (cp_parser *, tree);
2541 static void cp_parser_late_parsing_default_args
2542 (cp_parser *, tree);
2543 static tree cp_parser_sizeof_operand
2544 (cp_parser *, enum rid);
2545 static tree cp_parser_trait_expr
2546 (cp_parser *, enum rid);
2547 static bool cp_parser_declares_only_class_p
2548 (cp_parser *);
2549 static void cp_parser_set_storage_class
2550 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2551 static void cp_parser_set_decl_spec_type
2552 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2553 static void set_and_check_decl_spec_loc
2554 (cp_decl_specifier_seq *decl_specs,
2555 cp_decl_spec ds, cp_token *);
2556 static bool cp_parser_friend_p
2557 (const cp_decl_specifier_seq *);
2558 static void cp_parser_required_error
2559 (cp_parser *, required_token, bool, location_t);
2560 static cp_token *cp_parser_require
2561 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2562 static cp_token *cp_parser_require_keyword
2563 (cp_parser *, enum rid, required_token);
2564 static bool cp_parser_token_starts_function_definition_p
2565 (cp_token *);
2566 static bool cp_parser_next_token_starts_class_definition_p
2567 (cp_parser *);
2568 static bool cp_parser_next_token_ends_template_argument_p
2569 (cp_parser *);
2570 static bool cp_parser_nth_token_starts_template_argument_list_p
2571 (cp_parser *, size_t);
2572 static enum tag_types cp_parser_token_is_class_key
2573 (cp_token *);
2574 static enum tag_types cp_parser_token_is_type_parameter_key
2575 (cp_token *);
2576 static void cp_parser_check_class_key
2577 (enum tag_types, tree type);
2578 static void cp_parser_check_access_in_redeclaration
2579 (tree type, location_t location);
2580 static bool cp_parser_optional_template_keyword
2581 (cp_parser *);
2582 static void cp_parser_pre_parsed_nested_name_specifier
2583 (cp_parser *);
2584 static bool cp_parser_cache_group
2585 (cp_parser *, enum cpp_ttype, unsigned);
2586 static tree cp_parser_cache_defarg
2587 (cp_parser *parser, bool nsdmi);
2588 static void cp_parser_parse_tentatively
2589 (cp_parser *);
2590 static void cp_parser_commit_to_tentative_parse
2591 (cp_parser *);
2592 static void cp_parser_commit_to_topmost_tentative_parse
2593 (cp_parser *);
2594 static void cp_parser_abort_tentative_parse
2595 (cp_parser *);
2596 static bool cp_parser_parse_definitely
2597 (cp_parser *);
2598 static inline bool cp_parser_parsing_tentatively
2599 (cp_parser *);
2600 static bool cp_parser_uncommitted_to_tentative_parse_p
2601 (cp_parser *);
2602 static void cp_parser_error
2603 (cp_parser *, const char *);
2604 static void cp_parser_name_lookup_error
2605 (cp_parser *, tree, tree, name_lookup_error, location_t);
2606 static bool cp_parser_simulate_error
2607 (cp_parser *);
2608 static bool cp_parser_check_type_definition
2609 (cp_parser *);
2610 static void cp_parser_check_for_definition_in_return_type
2611 (cp_declarator *, tree, location_t type_location);
2612 static void cp_parser_check_for_invalid_template_id
2613 (cp_parser *, tree, enum tag_types, location_t location);
2614 static bool cp_parser_non_integral_constant_expression
2615 (cp_parser *, non_integral_constant);
2616 static void cp_parser_diagnose_invalid_type_name
2617 (cp_parser *, tree, location_t);
2618 static bool cp_parser_parse_and_diagnose_invalid_type_name
2619 (cp_parser *);
2620 static int cp_parser_skip_to_closing_parenthesis
2621 (cp_parser *, bool, bool, bool);
2622 static void cp_parser_skip_to_end_of_statement
2623 (cp_parser *);
2624 static void cp_parser_consume_semicolon_at_end_of_statement
2625 (cp_parser *);
2626 static void cp_parser_skip_to_end_of_block_or_statement
2627 (cp_parser *);
2628 static bool cp_parser_skip_to_closing_brace
2629 (cp_parser *);
2630 static void cp_parser_skip_to_end_of_template_parameter_list
2631 (cp_parser *);
2632 static void cp_parser_skip_to_pragma_eol
2633 (cp_parser*, cp_token *);
2634 static bool cp_parser_error_occurred
2635 (cp_parser *);
2636 static bool cp_parser_allow_gnu_extensions_p
2637 (cp_parser *);
2638 static bool cp_parser_is_pure_string_literal
2639 (cp_token *);
2640 static bool cp_parser_is_string_literal
2641 (cp_token *);
2642 static bool cp_parser_is_keyword
2643 (cp_token *, enum rid);
2644 static tree cp_parser_make_typename_type
2645 (cp_parser *, tree, location_t location);
2646 static cp_declarator * cp_parser_make_indirect_declarator
2647 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2648 static bool cp_parser_compound_literal_p
2649 (cp_parser *);
2650 static bool cp_parser_array_designator_p
2651 (cp_parser *);
2652 static bool cp_parser_init_statement_p
2653 (cp_parser *);
2654 static bool cp_parser_skip_to_closing_square_bracket
2655 (cp_parser *);
2656
2657 /* Concept-related syntactic transformations */
2658
2659 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2660 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2661
2662 // -------------------------------------------------------------------------- //
2663 // Unevaluated Operand Guard
2664 //
2665 // Implementation of an RAII helper for unevaluated operand parsing.
2666 cp_unevaluated::cp_unevaluated ()
2667 {
2668 ++cp_unevaluated_operand;
2669 ++c_inhibit_evaluation_warnings;
2670 }
2671
2672 cp_unevaluated::~cp_unevaluated ()
2673 {
2674 --c_inhibit_evaluation_warnings;
2675 --cp_unevaluated_operand;
2676 }
2677
2678 // -------------------------------------------------------------------------- //
2679 // Tentative Parsing
2680
2681 /* Returns nonzero if we are parsing tentatively. */
2682
2683 static inline bool
2684 cp_parser_parsing_tentatively (cp_parser* parser)
2685 {
2686 return parser->context->next != NULL;
2687 }
2688
2689 /* Returns nonzero if TOKEN is a string literal. */
2690
2691 static bool
2692 cp_parser_is_pure_string_literal (cp_token* token)
2693 {
2694 return (token->type == CPP_STRING ||
2695 token->type == CPP_STRING16 ||
2696 token->type == CPP_STRING32 ||
2697 token->type == CPP_WSTRING ||
2698 token->type == CPP_UTF8STRING);
2699 }
2700
2701 /* Returns nonzero if TOKEN is a string literal
2702 of a user-defined string literal. */
2703
2704 static bool
2705 cp_parser_is_string_literal (cp_token* token)
2706 {
2707 return (cp_parser_is_pure_string_literal (token) ||
2708 token->type == CPP_STRING_USERDEF ||
2709 token->type == CPP_STRING16_USERDEF ||
2710 token->type == CPP_STRING32_USERDEF ||
2711 token->type == CPP_WSTRING_USERDEF ||
2712 token->type == CPP_UTF8STRING_USERDEF);
2713 }
2714
2715 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2716
2717 static bool
2718 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2719 {
2720 return token->keyword == keyword;
2721 }
2722
2723 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2724 PRAGMA_NONE. */
2725
2726 static enum pragma_kind
2727 cp_parser_pragma_kind (cp_token *token)
2728 {
2729 if (token->type != CPP_PRAGMA)
2730 return PRAGMA_NONE;
2731 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2732 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2733 }
2734
2735 /* Helper function for cp_parser_error.
2736 Having peeked a token of kind TOK1_KIND that might signify
2737 a conflict marker, peek successor tokens to determine
2738 if we actually do have a conflict marker.
2739 Specifically, we consider a run of 7 '<', '=' or '>' characters
2740 at the start of a line as a conflict marker.
2741 These come through the lexer as three pairs and a single,
2742 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2743 If it returns true, *OUT_LOC is written to with the location/range
2744 of the marker. */
2745
2746 static bool
2747 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2748 location_t *out_loc)
2749 {
2750 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2751 if (token2->type != tok1_kind)
2752 return false;
2753 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2754 if (token3->type != tok1_kind)
2755 return false;
2756 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2757 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2758 return false;
2759
2760 /* It must be at the start of the line. */
2761 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2762 if (LOCATION_COLUMN (start_loc) != 1)
2763 return false;
2764
2765 /* We have a conflict marker. Construct a location of the form:
2766 <<<<<<<
2767 ^~~~~~~
2768 with start == caret, finishing at the end of the marker. */
2769 location_t finish_loc = get_finish (token4->location);
2770 *out_loc = make_location (start_loc, start_loc, finish_loc);
2771
2772 return true;
2773 }
2774
2775 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2776 RT_CLOSE_PAREN. */
2777
2778 static const char *
2779 get_matching_symbol (required_token token_desc)
2780 {
2781 switch (token_desc)
2782 {
2783 default:
2784 gcc_unreachable ();
2785 return "";
2786 case RT_CLOSE_BRACE:
2787 return "{";
2788 case RT_CLOSE_PAREN:
2789 return "(";
2790 }
2791 }
2792
2793 /* Attempt to convert TOKEN_DESC from a required_token to an
2794 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2795
2796 static enum cpp_ttype
2797 get_required_cpp_ttype (required_token token_desc)
2798 {
2799 switch (token_desc)
2800 {
2801 case RT_SEMICOLON:
2802 return CPP_SEMICOLON;
2803 case RT_OPEN_PAREN:
2804 return CPP_OPEN_PAREN;
2805 case RT_CLOSE_BRACE:
2806 return CPP_CLOSE_BRACE;
2807 case RT_OPEN_BRACE:
2808 return CPP_OPEN_BRACE;
2809 case RT_CLOSE_SQUARE:
2810 return CPP_CLOSE_SQUARE;
2811 case RT_OPEN_SQUARE:
2812 return CPP_OPEN_SQUARE;
2813 case RT_COMMA:
2814 return CPP_COMMA;
2815 case RT_COLON:
2816 return CPP_COLON;
2817 case RT_CLOSE_PAREN:
2818 return CPP_CLOSE_PAREN;
2819
2820 default:
2821 /* Use CPP_EOF as a "no completions possible" code. */
2822 return CPP_EOF;
2823 }
2824 }
2825
2826
2827 /* Subroutine of cp_parser_error and cp_parser_required_error.
2828
2829 Issue a diagnostic of the form
2830 FILE:LINE: MESSAGE before TOKEN
2831 where TOKEN is the next token in the input stream. MESSAGE
2832 (specified by the caller) is usually of the form "expected
2833 OTHER-TOKEN".
2834
2835 This bypasses the check for tentative passing, and potentially
2836 adds material needed by cp_parser_required_error.
2837
2838 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2839 suggesting insertion of the missing token.
2840
2841 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2842 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2843 location. */
2844
2845 static void
2846 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2847 required_token missing_token_desc,
2848 location_t matching_location)
2849 {
2850 cp_token *token = cp_lexer_peek_token (parser->lexer);
2851 /* This diagnostic makes more sense if it is tagged to the line
2852 of the token we just peeked at. */
2853 cp_lexer_set_source_position_from_token (token);
2854
2855 if (token->type == CPP_PRAGMA)
2856 {
2857 error_at (token->location,
2858 "%<#pragma%> is not allowed here");
2859 cp_parser_skip_to_pragma_eol (parser, token);
2860 return;
2861 }
2862
2863 /* If this is actually a conflict marker, report it as such. */
2864 if (token->type == CPP_LSHIFT
2865 || token->type == CPP_RSHIFT
2866 || token->type == CPP_EQ_EQ)
2867 {
2868 location_t loc;
2869 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2870 {
2871 error_at (loc, "version control conflict marker in file");
2872 return;
2873 }
2874 }
2875
2876 gcc_rich_location richloc (input_location);
2877
2878 bool added_matching_location = false;
2879
2880 if (missing_token_desc != RT_NONE)
2881 {
2882 /* Potentially supply a fix-it hint, suggesting to add the
2883 missing token immediately after the *previous* token.
2884 This may move the primary location within richloc. */
2885 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2886 location_t prev_token_loc
2887 = cp_lexer_previous_token (parser->lexer)->location;
2888 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2889
2890 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2891 Attempt to consolidate diagnostics by printing it as a
2892 secondary range within the main diagnostic. */
2893 if (matching_location != UNKNOWN_LOCATION)
2894 added_matching_location
2895 = richloc.add_location_if_nearby (matching_location);
2896 }
2897
2898 /* Actually emit the error. */
2899 c_parse_error (gmsgid,
2900 /* Because c_parser_error does not understand
2901 CPP_KEYWORD, keywords are treated like
2902 identifiers. */
2903 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2904 token->u.value, token->flags, &richloc);
2905
2906 if (missing_token_desc != RT_NONE)
2907 {
2908 /* If we weren't able to consolidate matching_location, then
2909 print it as a secondary diagnostic. */
2910 if (matching_location != UNKNOWN_LOCATION
2911 && !added_matching_location)
2912 inform (matching_location, "to match this %qs",
2913 get_matching_symbol (missing_token_desc));
2914 }
2915 }
2916
2917 /* If not parsing tentatively, issue a diagnostic of the form
2918 FILE:LINE: MESSAGE before TOKEN
2919 where TOKEN is the next token in the input stream. MESSAGE
2920 (specified by the caller) is usually of the form "expected
2921 OTHER-TOKEN". */
2922
2923 static void
2924 cp_parser_error (cp_parser* parser, const char* gmsgid)
2925 {
2926 if (!cp_parser_simulate_error (parser))
2927 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2928 }
2929
2930 /* Issue an error about name-lookup failing. NAME is the
2931 IDENTIFIER_NODE DECL is the result of
2932 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2933 the thing that we hoped to find. */
2934
2935 static void
2936 cp_parser_name_lookup_error (cp_parser* parser,
2937 tree name,
2938 tree decl,
2939 name_lookup_error desired,
2940 location_t location)
2941 {
2942 /* If name lookup completely failed, tell the user that NAME was not
2943 declared. */
2944 if (decl == error_mark_node)
2945 {
2946 if (parser->scope && parser->scope != global_namespace)
2947 error_at (location, "%<%E::%E%> has not been declared",
2948 parser->scope, name);
2949 else if (parser->scope == global_namespace)
2950 error_at (location, "%<::%E%> has not been declared", name);
2951 else if (parser->object_scope
2952 && !CLASS_TYPE_P (parser->object_scope))
2953 error_at (location, "request for member %qE in non-class type %qT",
2954 name, parser->object_scope);
2955 else if (parser->object_scope)
2956 error_at (location, "%<%T::%E%> has not been declared",
2957 parser->object_scope, name);
2958 else
2959 error_at (location, "%qE has not been declared", name);
2960 }
2961 else if (parser->scope && parser->scope != global_namespace)
2962 {
2963 switch (desired)
2964 {
2965 case NLE_TYPE:
2966 error_at (location, "%<%E::%E%> is not a type",
2967 parser->scope, name);
2968 break;
2969 case NLE_CXX98:
2970 error_at (location, "%<%E::%E%> is not a class or namespace",
2971 parser->scope, name);
2972 break;
2973 case NLE_NOT_CXX98:
2974 error_at (location,
2975 "%<%E::%E%> is not a class, namespace, or enumeration",
2976 parser->scope, name);
2977 break;
2978 default:
2979 gcc_unreachable ();
2980
2981 }
2982 }
2983 else if (parser->scope == global_namespace)
2984 {
2985 switch (desired)
2986 {
2987 case NLE_TYPE:
2988 error_at (location, "%<::%E%> is not a type", name);
2989 break;
2990 case NLE_CXX98:
2991 error_at (location, "%<::%E%> is not a class or namespace", name);
2992 break;
2993 case NLE_NOT_CXX98:
2994 error_at (location,
2995 "%<::%E%> is not a class, namespace, or enumeration",
2996 name);
2997 break;
2998 default:
2999 gcc_unreachable ();
3000 }
3001 }
3002 else
3003 {
3004 switch (desired)
3005 {
3006 case NLE_TYPE:
3007 error_at (location, "%qE is not a type", name);
3008 break;
3009 case NLE_CXX98:
3010 error_at (location, "%qE is not a class or namespace", name);
3011 break;
3012 case NLE_NOT_CXX98:
3013 error_at (location,
3014 "%qE is not a class, namespace, or enumeration", name);
3015 break;
3016 default:
3017 gcc_unreachable ();
3018 }
3019 }
3020 }
3021
3022 /* If we are parsing tentatively, remember that an error has occurred
3023 during this tentative parse. Returns true if the error was
3024 simulated; false if a message should be issued by the caller. */
3025
3026 static bool
3027 cp_parser_simulate_error (cp_parser* parser)
3028 {
3029 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3030 {
3031 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3032 return true;
3033 }
3034 return false;
3035 }
3036
3037 /* This function is called when a type is defined. If type
3038 definitions are forbidden at this point, an error message is
3039 issued. */
3040
3041 static bool
3042 cp_parser_check_type_definition (cp_parser* parser)
3043 {
3044 /* If types are forbidden here, issue a message. */
3045 if (parser->type_definition_forbidden_message)
3046 {
3047 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3048 in the message need to be interpreted. */
3049 error (parser->type_definition_forbidden_message);
3050 return false;
3051 }
3052 return true;
3053 }
3054
3055 /* This function is called when the DECLARATOR is processed. The TYPE
3056 was a type defined in the decl-specifiers. If it is invalid to
3057 define a type in the decl-specifiers for DECLARATOR, an error is
3058 issued. TYPE_LOCATION is the location of TYPE and is used
3059 for error reporting. */
3060
3061 static void
3062 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3063 tree type, location_t type_location)
3064 {
3065 /* [dcl.fct] forbids type definitions in return types.
3066 Unfortunately, it's not easy to know whether or not we are
3067 processing a return type until after the fact. */
3068 while (declarator
3069 && (declarator->kind == cdk_pointer
3070 || declarator->kind == cdk_reference
3071 || declarator->kind == cdk_ptrmem))
3072 declarator = declarator->declarator;
3073 if (declarator
3074 && declarator->kind == cdk_function)
3075 {
3076 error_at (type_location,
3077 "new types may not be defined in a return type");
3078 inform (type_location,
3079 "(perhaps a semicolon is missing after the definition of %qT)",
3080 type);
3081 }
3082 }
3083
3084 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3085 "<" in any valid C++ program. If the next token is indeed "<",
3086 issue a message warning the user about what appears to be an
3087 invalid attempt to form a template-id. LOCATION is the location
3088 of the type-specifier (TYPE) */
3089
3090 static void
3091 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3092 tree type,
3093 enum tag_types tag_type,
3094 location_t location)
3095 {
3096 cp_token_position start = 0;
3097
3098 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3099 {
3100 if (TREE_CODE (type) == TYPE_DECL)
3101 type = TREE_TYPE (type);
3102 if (TYPE_P (type) && !template_placeholder_p (type))
3103 error_at (location, "%qT is not a template", type);
3104 else if (identifier_p (type))
3105 {
3106 if (tag_type != none_type)
3107 error_at (location, "%qE is not a class template", type);
3108 else
3109 error_at (location, "%qE is not a template", type);
3110 }
3111 else
3112 error_at (location, "invalid template-id");
3113 /* Remember the location of the invalid "<". */
3114 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3115 start = cp_lexer_token_position (parser->lexer, true);
3116 /* Consume the "<". */
3117 cp_lexer_consume_token (parser->lexer);
3118 /* Parse the template arguments. */
3119 cp_parser_enclosed_template_argument_list (parser);
3120 /* Permanently remove the invalid template arguments so that
3121 this error message is not issued again. */
3122 if (start)
3123 cp_lexer_purge_tokens_after (parser->lexer, start);
3124 }
3125 }
3126
3127 /* If parsing an integral constant-expression, issue an error message
3128 about the fact that THING appeared and return true. Otherwise,
3129 return false. In either case, set
3130 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3131
3132 static bool
3133 cp_parser_non_integral_constant_expression (cp_parser *parser,
3134 non_integral_constant thing)
3135 {
3136 parser->non_integral_constant_expression_p = true;
3137 if (parser->integral_constant_expression_p)
3138 {
3139 if (!parser->allow_non_integral_constant_expression_p)
3140 {
3141 const char *msg = NULL;
3142 switch (thing)
3143 {
3144 case NIC_FLOAT:
3145 pedwarn (input_location, OPT_Wpedantic,
3146 "ISO C++ forbids using a floating-point literal "
3147 "in a constant-expression");
3148 return true;
3149 case NIC_CAST:
3150 error ("a cast to a type other than an integral or "
3151 "enumeration type cannot appear in a "
3152 "constant-expression");
3153 return true;
3154 case NIC_TYPEID:
3155 error ("%<typeid%> operator "
3156 "cannot appear in a constant-expression");
3157 return true;
3158 case NIC_NCC:
3159 error ("non-constant compound literals "
3160 "cannot appear in a constant-expression");
3161 return true;
3162 case NIC_FUNC_CALL:
3163 error ("a function call "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_INC:
3167 error ("an increment "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_DEC:
3171 error ("an decrement "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_ARRAY_REF:
3175 error ("an array reference "
3176 "cannot appear in a constant-expression");
3177 return true;
3178 case NIC_ADDR_LABEL:
3179 error ("the address of a label "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_OVERLOADED:
3183 error ("calls to overloaded operators "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_ASSIGNMENT:
3187 error ("an assignment cannot appear in a constant-expression");
3188 return true;
3189 case NIC_COMMA:
3190 error ("a comma operator "
3191 "cannot appear in a constant-expression");
3192 return true;
3193 case NIC_CONSTRUCTOR:
3194 error ("a call to a constructor "
3195 "cannot appear in a constant-expression");
3196 return true;
3197 case NIC_TRANSACTION:
3198 error ("a transaction expression "
3199 "cannot appear in a constant-expression");
3200 return true;
3201 case NIC_THIS:
3202 msg = "this";
3203 break;
3204 case NIC_FUNC_NAME:
3205 msg = "__FUNCTION__";
3206 break;
3207 case NIC_PRETTY_FUNC:
3208 msg = "__PRETTY_FUNCTION__";
3209 break;
3210 case NIC_C99_FUNC:
3211 msg = "__func__";
3212 break;
3213 case NIC_VA_ARG:
3214 msg = "va_arg";
3215 break;
3216 case NIC_ARROW:
3217 msg = "->";
3218 break;
3219 case NIC_POINT:
3220 msg = ".";
3221 break;
3222 case NIC_STAR:
3223 msg = "*";
3224 break;
3225 case NIC_ADDR:
3226 msg = "&";
3227 break;
3228 case NIC_PREINCREMENT:
3229 msg = "++";
3230 break;
3231 case NIC_PREDECREMENT:
3232 msg = "--";
3233 break;
3234 case NIC_NEW:
3235 msg = "new";
3236 break;
3237 case NIC_DEL:
3238 msg = "delete";
3239 break;
3240 default:
3241 gcc_unreachable ();
3242 }
3243 if (msg)
3244 error ("%qs cannot appear in a constant-expression", msg);
3245 return true;
3246 }
3247 }
3248 return false;
3249 }
3250
3251 /* Emit a diagnostic for an invalid type name. This function commits
3252 to the current active tentative parse, if any. (Otherwise, the
3253 problematic construct might be encountered again later, resulting
3254 in duplicate error messages.) LOCATION is the location of ID. */
3255
3256 static void
3257 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3258 location_t location)
3259 {
3260 tree decl, ambiguous_decls;
3261 cp_parser_commit_to_tentative_parse (parser);
3262 /* Try to lookup the identifier. */
3263 decl = cp_parser_lookup_name (parser, id, none_type,
3264 /*is_template=*/false,
3265 /*is_namespace=*/false,
3266 /*check_dependency=*/true,
3267 &ambiguous_decls, location);
3268 if (ambiguous_decls)
3269 /* If the lookup was ambiguous, an error will already have
3270 been issued. */
3271 return;
3272 /* If the lookup found a template-name, it means that the user forgot
3273 to specify an argument list. Emit a useful error message. */
3274 if (DECL_TYPE_TEMPLATE_P (decl))
3275 {
3276 error_at (location,
3277 "invalid use of template-name %qE without an argument list",
3278 decl);
3279 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3280 inform (location, "class template argument deduction is only available "
3281 "with -std=c++17 or -std=gnu++17");
3282 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3283 }
3284 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3285 error_at (location, "invalid use of destructor %qD as a type", id);
3286 else if (TREE_CODE (decl) == TYPE_DECL)
3287 /* Something like 'unsigned A a;' */
3288 error_at (location, "invalid combination of multiple type-specifiers");
3289 else if (!parser->scope)
3290 {
3291 /* Issue an error message. */
3292 name_hint hint;
3293 if (TREE_CODE (id) == IDENTIFIER_NODE)
3294 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3295 if (hint)
3296 {
3297 gcc_rich_location richloc (location);
3298 richloc.add_fixit_replace (hint.suggestion ());
3299 error_at (&richloc,
3300 "%qE does not name a type; did you mean %qs?",
3301 id, hint.suggestion ());
3302 }
3303 else
3304 error_at (location, "%qE does not name a type", id);
3305 /* If we're in a template class, it's possible that the user was
3306 referring to a type from a base class. For example:
3307
3308 template <typename T> struct A { typedef T X; };
3309 template <typename T> struct B : public A<T> { X x; };
3310
3311 The user should have said "typename A<T>::X". */
3312 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3313 inform (location, "C++11 %<constexpr%> only available with "
3314 "-std=c++11 or -std=gnu++11");
3315 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3316 inform (location, "C++11 %<noexcept%> only available with "
3317 "-std=c++11 or -std=gnu++11");
3318 else if (cxx_dialect < cxx11
3319 && TREE_CODE (id) == IDENTIFIER_NODE
3320 && id_equal (id, "thread_local"))
3321 inform (location, "C++11 %<thread_local%> only available with "
3322 "-std=c++11 or -std=gnu++11");
3323 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3324 inform (location, "%<concept%> only available with -fconcepts");
3325 else if (processing_template_decl && current_class_type
3326 && TYPE_BINFO (current_class_type))
3327 {
3328 tree b;
3329
3330 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3331 b;
3332 b = TREE_CHAIN (b))
3333 {
3334 tree base_type = BINFO_TYPE (b);
3335 if (CLASS_TYPE_P (base_type)
3336 && dependent_type_p (base_type))
3337 {
3338 tree field;
3339 /* Go from a particular instantiation of the
3340 template (which will have an empty TYPE_FIELDs),
3341 to the main version. */
3342 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3343 for (field = TYPE_FIELDS (base_type);
3344 field;
3345 field = DECL_CHAIN (field))
3346 if (TREE_CODE (field) == TYPE_DECL
3347 && DECL_NAME (field) == id)
3348 {
3349 inform (location,
3350 "(perhaps %<typename %T::%E%> was intended)",
3351 BINFO_TYPE (b), id);
3352 break;
3353 }
3354 if (field)
3355 break;
3356 }
3357 }
3358 }
3359 }
3360 /* Here we diagnose qualified-ids where the scope is actually correct,
3361 but the identifier does not resolve to a valid type name. */
3362 else if (parser->scope != error_mark_node)
3363 {
3364 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3365 {
3366 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3367 error_at (location_of (id),
3368 "%qE in namespace %qE does not name a template type",
3369 id, parser->scope);
3370 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3371 error_at (location_of (id),
3372 "%qE in namespace %qE does not name a template type",
3373 TREE_OPERAND (id, 0), parser->scope);
3374 else
3375 error_at (location_of (id),
3376 "%qE in namespace %qE does not name a type",
3377 id, parser->scope);
3378 if (DECL_P (decl))
3379 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3380 }
3381 else if (CLASS_TYPE_P (parser->scope)
3382 && constructor_name_p (id, parser->scope))
3383 {
3384 /* A<T>::A<T>() */
3385 error_at (location, "%<%T::%E%> names the constructor, not"
3386 " the type", parser->scope, id);
3387 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3388 error_at (location, "and %qT has no template constructors",
3389 parser->scope);
3390 }
3391 else if (TYPE_P (parser->scope)
3392 && dependent_scope_p (parser->scope))
3393 {
3394 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3395 error_at (location,
3396 "need %<typename%> before %<%T::%D::%E%> because "
3397 "%<%T::%D%> is a dependent scope",
3398 TYPE_CONTEXT (parser->scope),
3399 TYPENAME_TYPE_FULLNAME (parser->scope),
3400 id,
3401 TYPE_CONTEXT (parser->scope),
3402 TYPENAME_TYPE_FULLNAME (parser->scope));
3403 else
3404 error_at (location, "need %<typename%> before %<%T::%E%> because "
3405 "%qT is a dependent scope",
3406 parser->scope, id, parser->scope);
3407 }
3408 else if (TYPE_P (parser->scope))
3409 {
3410 if (!COMPLETE_TYPE_P (parser->scope))
3411 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3412 parser->scope);
3413 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3414 error_at (location_of (id),
3415 "%qE in %q#T does not name a template type",
3416 id, parser->scope);
3417 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3418 error_at (location_of (id),
3419 "%qE in %q#T does not name a template type",
3420 TREE_OPERAND (id, 0), parser->scope);
3421 else
3422 error_at (location_of (id),
3423 "%qE in %q#T does not name a type",
3424 id, parser->scope);
3425 if (DECL_P (decl))
3426 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3427 }
3428 else
3429 gcc_unreachable ();
3430 }
3431 }
3432
3433 /* Check for a common situation where a type-name should be present,
3434 but is not, and issue a sensible error message. Returns true if an
3435 invalid type-name was detected.
3436
3437 The situation handled by this function are variable declarations of the
3438 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3439 Usually, `ID' should name a type, but if we got here it means that it
3440 does not. We try to emit the best possible error message depending on
3441 how exactly the id-expression looks like. */
3442
3443 static bool
3444 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3445 {
3446 tree id;
3447 cp_token *token = cp_lexer_peek_token (parser->lexer);
3448
3449 /* Avoid duplicate error about ambiguous lookup. */
3450 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3451 {
3452 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3453 if (next->type == CPP_NAME && next->error_reported)
3454 goto out;
3455 }
3456
3457 cp_parser_parse_tentatively (parser);
3458 id = cp_parser_id_expression (parser,
3459 /*template_keyword_p=*/false,
3460 /*check_dependency_p=*/true,
3461 /*template_p=*/NULL,
3462 /*declarator_p=*/true,
3463 /*optional_p=*/false);
3464 /* If the next token is a (, this is a function with no explicit return
3465 type, i.e. constructor, destructor or conversion op. */
3466 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3467 || TREE_CODE (id) == TYPE_DECL)
3468 {
3469 cp_parser_abort_tentative_parse (parser);
3470 return false;
3471 }
3472 if (!cp_parser_parse_definitely (parser))
3473 return false;
3474
3475 /* Emit a diagnostic for the invalid type. */
3476 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3477 out:
3478 /* If we aren't in the middle of a declarator (i.e. in a
3479 parameter-declaration-clause), skip to the end of the declaration;
3480 there's no point in trying to process it. */
3481 if (!parser->in_declarator_p)
3482 cp_parser_skip_to_end_of_block_or_statement (parser);
3483 return true;
3484 }
3485
3486 /* Consume tokens up to, and including, the next non-nested closing `)'.
3487 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3488 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3489 found an unnested token of that type. */
3490
3491 static int
3492 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3493 bool recovering,
3494 cpp_ttype or_ttype,
3495 bool consume_paren)
3496 {
3497 unsigned paren_depth = 0;
3498 unsigned brace_depth = 0;
3499 unsigned square_depth = 0;
3500
3501 if (recovering && or_ttype == CPP_EOF
3502 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3503 return 0;
3504
3505 while (true)
3506 {
3507 cp_token * token = cp_lexer_peek_token (parser->lexer);
3508
3509 /* Have we found what we're looking for before the closing paren? */
3510 if (token->type == or_ttype && or_ttype != CPP_EOF
3511 && !brace_depth && !paren_depth && !square_depth)
3512 return -1;
3513
3514 switch (token->type)
3515 {
3516 case CPP_EOF:
3517 case CPP_PRAGMA_EOL:
3518 /* If we've run out of tokens, then there is no closing `)'. */
3519 return 0;
3520
3521 /* This is good for lambda expression capture-lists. */
3522 case CPP_OPEN_SQUARE:
3523 ++square_depth;
3524 break;
3525 case CPP_CLOSE_SQUARE:
3526 if (!square_depth--)
3527 return 0;
3528 break;
3529
3530 case CPP_SEMICOLON:
3531 /* This matches the processing in skip_to_end_of_statement. */
3532 if (!brace_depth)
3533 return 0;
3534 break;
3535
3536 case CPP_OPEN_BRACE:
3537 ++brace_depth;
3538 break;
3539 case CPP_CLOSE_BRACE:
3540 if (!brace_depth--)
3541 return 0;
3542 break;
3543
3544 case CPP_OPEN_PAREN:
3545 if (!brace_depth)
3546 ++paren_depth;
3547 break;
3548
3549 case CPP_CLOSE_PAREN:
3550 if (!brace_depth && !paren_depth--)
3551 {
3552 if (consume_paren)
3553 cp_lexer_consume_token (parser->lexer);
3554 return 1;
3555 }
3556 break;
3557
3558 default:
3559 break;
3560 }
3561
3562 /* Consume the token. */
3563 cp_lexer_consume_token (parser->lexer);
3564 }
3565 }
3566
3567 /* Consume tokens up to, and including, the next non-nested closing `)'.
3568 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3569 are doing error recovery. Returns -1 if OR_COMMA is true and we
3570 found an unnested token of that type. */
3571
3572 static int
3573 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3574 bool recovering,
3575 bool or_comma,
3576 bool consume_paren)
3577 {
3578 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3579 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3580 ttype, consume_paren);
3581 }
3582
3583 /* Consume tokens until we reach the end of the current statement.
3584 Normally, that will be just before consuming a `;'. However, if a
3585 non-nested `}' comes first, then we stop before consuming that. */
3586
3587 static void
3588 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3589 {
3590 unsigned nesting_depth = 0;
3591
3592 /* Unwind generic function template scope if necessary. */
3593 if (parser->fully_implicit_function_template_p)
3594 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3595
3596 while (true)
3597 {
3598 cp_token *token = cp_lexer_peek_token (parser->lexer);
3599
3600 switch (token->type)
3601 {
3602 case CPP_EOF:
3603 case CPP_PRAGMA_EOL:
3604 /* If we've run out of tokens, stop. */
3605 return;
3606
3607 case CPP_SEMICOLON:
3608 /* If the next token is a `;', we have reached the end of the
3609 statement. */
3610 if (!nesting_depth)
3611 return;
3612 break;
3613
3614 case CPP_CLOSE_BRACE:
3615 /* If this is a non-nested '}', stop before consuming it.
3616 That way, when confronted with something like:
3617
3618 { 3 + }
3619
3620 we stop before consuming the closing '}', even though we
3621 have not yet reached a `;'. */
3622 if (nesting_depth == 0)
3623 return;
3624
3625 /* If it is the closing '}' for a block that we have
3626 scanned, stop -- but only after consuming the token.
3627 That way given:
3628
3629 void f g () { ... }
3630 typedef int I;
3631
3632 we will stop after the body of the erroneously declared
3633 function, but before consuming the following `typedef'
3634 declaration. */
3635 if (--nesting_depth == 0)
3636 {
3637 cp_lexer_consume_token (parser->lexer);
3638 return;
3639 }
3640 break;
3641
3642 case CPP_OPEN_BRACE:
3643 ++nesting_depth;
3644 break;
3645
3646 default:
3647 break;
3648 }
3649
3650 /* Consume the token. */
3651 cp_lexer_consume_token (parser->lexer);
3652 }
3653 }
3654
3655 /* This function is called at the end of a statement or declaration.
3656 If the next token is a semicolon, it is consumed; otherwise, error
3657 recovery is attempted. */
3658
3659 static void
3660 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3661 {
3662 /* Look for the trailing `;'. */
3663 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3664 {
3665 /* If there is additional (erroneous) input, skip to the end of
3666 the statement. */
3667 cp_parser_skip_to_end_of_statement (parser);
3668 /* If the next token is now a `;', consume it. */
3669 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3670 cp_lexer_consume_token (parser->lexer);
3671 }
3672 }
3673
3674 /* Skip tokens until we have consumed an entire block, or until we
3675 have consumed a non-nested `;'. */
3676
3677 static void
3678 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3679 {
3680 int nesting_depth = 0;
3681
3682 /* Unwind generic function template scope if necessary. */
3683 if (parser->fully_implicit_function_template_p)
3684 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3685
3686 while (nesting_depth >= 0)
3687 {
3688 cp_token *token = cp_lexer_peek_token (parser->lexer);
3689
3690 switch (token->type)
3691 {
3692 case CPP_EOF:
3693 case CPP_PRAGMA_EOL:
3694 /* If we've run out of tokens, stop. */
3695 return;
3696
3697 case CPP_SEMICOLON:
3698 /* Stop if this is an unnested ';'. */
3699 if (!nesting_depth)
3700 nesting_depth = -1;
3701 break;
3702
3703 case CPP_CLOSE_BRACE:
3704 /* Stop if this is an unnested '}', or closes the outermost
3705 nesting level. */
3706 nesting_depth--;
3707 if (nesting_depth < 0)
3708 return;
3709 if (!nesting_depth)
3710 nesting_depth = -1;
3711 break;
3712
3713 case CPP_OPEN_BRACE:
3714 /* Nest. */
3715 nesting_depth++;
3716 break;
3717
3718 default:
3719 break;
3720 }
3721
3722 /* Consume the token. */
3723 cp_lexer_consume_token (parser->lexer);
3724 }
3725 }
3726
3727 /* Skip tokens until a non-nested closing curly brace is the next
3728 token, or there are no more tokens. Return true in the first case,
3729 false otherwise. */
3730
3731 static bool
3732 cp_parser_skip_to_closing_brace (cp_parser *parser)
3733 {
3734 unsigned nesting_depth = 0;
3735
3736 while (true)
3737 {
3738 cp_token *token = cp_lexer_peek_token (parser->lexer);
3739
3740 switch (token->type)
3741 {
3742 case CPP_EOF:
3743 case CPP_PRAGMA_EOL:
3744 /* If we've run out of tokens, stop. */
3745 return false;
3746
3747 case CPP_CLOSE_BRACE:
3748 /* If the next token is a non-nested `}', then we have reached
3749 the end of the current block. */
3750 if (nesting_depth-- == 0)
3751 return true;
3752 break;
3753
3754 case CPP_OPEN_BRACE:
3755 /* If it the next token is a `{', then we are entering a new
3756 block. Consume the entire block. */
3757 ++nesting_depth;
3758 break;
3759
3760 default:
3761 break;
3762 }
3763
3764 /* Consume the token. */
3765 cp_lexer_consume_token (parser->lexer);
3766 }
3767 }
3768
3769 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3770 parameter is the PRAGMA token, allowing us to purge the entire pragma
3771 sequence. */
3772
3773 static void
3774 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3775 {
3776 cp_token *token;
3777
3778 parser->lexer->in_pragma = false;
3779
3780 do
3781 token = cp_lexer_consume_token (parser->lexer);
3782 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3783
3784 /* Ensure that the pragma is not parsed again. */
3785 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3786 }
3787
3788 /* Require pragma end of line, resyncing with it as necessary. The
3789 arguments are as for cp_parser_skip_to_pragma_eol. */
3790
3791 static void
3792 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3793 {
3794 parser->lexer->in_pragma = false;
3795 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3796 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3797 }
3798
3799 /* This is a simple wrapper around make_typename_type. When the id is
3800 an unresolved identifier node, we can provide a superior diagnostic
3801 using cp_parser_diagnose_invalid_type_name. */
3802
3803 static tree
3804 cp_parser_make_typename_type (cp_parser *parser, tree id,
3805 location_t id_location)
3806 {
3807 tree result;
3808 if (identifier_p (id))
3809 {
3810 result = make_typename_type (parser->scope, id, typename_type,
3811 /*complain=*/tf_none);
3812 if (result == error_mark_node)
3813 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3814 return result;
3815 }
3816 return make_typename_type (parser->scope, id, typename_type, tf_error);
3817 }
3818
3819 /* This is a wrapper around the
3820 make_{pointer,ptrmem,reference}_declarator functions that decides
3821 which one to call based on the CODE and CLASS_TYPE arguments. The
3822 CODE argument should be one of the values returned by
3823 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3824 appertain to the pointer or reference. */
3825
3826 static cp_declarator *
3827 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3828 cp_cv_quals cv_qualifiers,
3829 cp_declarator *target,
3830 tree attributes)
3831 {
3832 if (code == ERROR_MARK)
3833 return cp_error_declarator;
3834
3835 if (code == INDIRECT_REF)
3836 if (class_type == NULL_TREE)
3837 return make_pointer_declarator (cv_qualifiers, target, attributes);
3838 else
3839 return make_ptrmem_declarator (cv_qualifiers, class_type,
3840 target, attributes);
3841 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3842 return make_reference_declarator (cv_qualifiers, target,
3843 false, attributes);
3844 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3845 return make_reference_declarator (cv_qualifiers, target,
3846 true, attributes);
3847 gcc_unreachable ();
3848 }
3849
3850 /* Create a new C++ parser. */
3851
3852 static cp_parser *
3853 cp_parser_new (void)
3854 {
3855 cp_parser *parser;
3856 cp_lexer *lexer;
3857 unsigned i;
3858
3859 /* cp_lexer_new_main is called before doing GC allocation because
3860 cp_lexer_new_main might load a PCH file. */
3861 lexer = cp_lexer_new_main ();
3862
3863 /* Initialize the binops_by_token so that we can get the tree
3864 directly from the token. */
3865 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3866 binops_by_token[binops[i].token_type] = binops[i];
3867
3868 parser = ggc_cleared_alloc<cp_parser> ();
3869 parser->lexer = lexer;
3870 parser->context = cp_parser_context_new (NULL);
3871
3872 /* For now, we always accept GNU extensions. */
3873 parser->allow_gnu_extensions_p = 1;
3874
3875 /* The `>' token is a greater-than operator, not the end of a
3876 template-id. */
3877 parser->greater_than_is_operator_p = true;
3878
3879 parser->default_arg_ok_p = true;
3880
3881 /* We are not parsing a constant-expression. */
3882 parser->integral_constant_expression_p = false;
3883 parser->allow_non_integral_constant_expression_p = false;
3884 parser->non_integral_constant_expression_p = false;
3885
3886 /* Local variable names are not forbidden. */
3887 parser->local_variables_forbidden_p = false;
3888
3889 /* We are not processing an `extern "C"' declaration. */
3890 parser->in_unbraced_linkage_specification_p = false;
3891
3892 /* We are not processing a declarator. */
3893 parser->in_declarator_p = false;
3894
3895 /* We are not processing a template-argument-list. */
3896 parser->in_template_argument_list_p = false;
3897
3898 /* We are not in an iteration statement. */
3899 parser->in_statement = 0;
3900
3901 /* We are not in a switch statement. */
3902 parser->in_switch_statement_p = false;
3903
3904 /* We are not parsing a type-id inside an expression. */
3905 parser->in_type_id_in_expr_p = false;
3906
3907 /* Declarations aren't implicitly extern "C". */
3908 parser->implicit_extern_c = false;
3909
3910 /* String literals should be translated to the execution character set. */
3911 parser->translate_strings_p = true;
3912
3913 /* We are not parsing a function body. */
3914 parser->in_function_body = false;
3915
3916 /* We can correct until told otherwise. */
3917 parser->colon_corrects_to_scope_p = true;
3918
3919 /* The unparsed function queue is empty. */
3920 push_unparsed_function_queues (parser);
3921
3922 /* There are no classes being defined. */
3923 parser->num_classes_being_defined = 0;
3924
3925 /* No template parameters apply. */
3926 parser->num_template_parameter_lists = 0;
3927
3928 /* Special parsing data structures. */
3929 parser->omp_declare_simd = NULL;
3930 parser->cilk_simd_fn_info = NULL;
3931 parser->oacc_routine = NULL;
3932
3933 /* Not declaring an implicit function template. */
3934 parser->auto_is_implicit_function_template_parm_p = false;
3935 parser->fully_implicit_function_template_p = false;
3936 parser->implicit_template_parms = 0;
3937 parser->implicit_template_scope = 0;
3938
3939 /* Allow constrained-type-specifiers. */
3940 parser->prevent_constrained_type_specifiers = 0;
3941
3942 /* We haven't yet seen an 'extern "C"'. */
3943 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3944
3945 return parser;
3946 }
3947
3948 /* Create a cp_lexer structure which will emit the tokens in CACHE
3949 and push it onto the parser's lexer stack. This is used for delayed
3950 parsing of in-class method bodies and default arguments, and should
3951 not be confused with tentative parsing. */
3952 static void
3953 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3954 {
3955 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3956 lexer->next = parser->lexer;
3957 parser->lexer = lexer;
3958
3959 /* Move the current source position to that of the first token in the
3960 new lexer. */
3961 cp_lexer_set_source_position_from_token (lexer->next_token);
3962 }
3963
3964 /* Pop the top lexer off the parser stack. This is never used for the
3965 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3966 static void
3967 cp_parser_pop_lexer (cp_parser *parser)
3968 {
3969 cp_lexer *lexer = parser->lexer;
3970 parser->lexer = lexer->next;
3971 cp_lexer_destroy (lexer);
3972
3973 /* Put the current source position back where it was before this
3974 lexer was pushed. */
3975 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3976 }
3977
3978 /* Lexical conventions [gram.lex] */
3979
3980 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3981 identifier. */
3982
3983 static cp_expr
3984 cp_parser_identifier (cp_parser* parser)
3985 {
3986 cp_token *token;
3987
3988 /* Look for the identifier. */
3989 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3990 /* Return the value. */
3991 if (token)
3992 return cp_expr (token->u.value, token->location);
3993 else
3994 return error_mark_node;
3995 }
3996
3997 /* Parse a sequence of adjacent string constants. Returns a
3998 TREE_STRING representing the combined, nul-terminated string
3999 constant. If TRANSLATE is true, translate the string to the
4000 execution character set. If WIDE_OK is true, a wide string is
4001 invalid here.
4002
4003 C++98 [lex.string] says that if a narrow string literal token is
4004 adjacent to a wide string literal token, the behavior is undefined.
4005 However, C99 6.4.5p4 says that this results in a wide string literal.
4006 We follow C99 here, for consistency with the C front end.
4007
4008 This code is largely lifted from lex_string() in c-lex.c.
4009
4010 FUTURE: ObjC++ will need to handle @-strings here. */
4011 static cp_expr
4012 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4013 bool lookup_udlit = true)
4014 {
4015 tree value;
4016 size_t count;
4017 struct obstack str_ob;
4018 cpp_string str, istr, *strs;
4019 cp_token *tok;
4020 enum cpp_ttype type, curr_type;
4021 int have_suffix_p = 0;
4022 tree string_tree;
4023 tree suffix_id = NULL_TREE;
4024 bool curr_tok_is_userdef_p = false;
4025
4026 tok = cp_lexer_peek_token (parser->lexer);
4027 if (!cp_parser_is_string_literal (tok))
4028 {
4029 cp_parser_error (parser, "expected string-literal");
4030 return error_mark_node;
4031 }
4032
4033 location_t loc = tok->location;
4034
4035 if (cpp_userdef_string_p (tok->type))
4036 {
4037 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4038 curr_type = cpp_userdef_string_remove_type (tok->type);
4039 curr_tok_is_userdef_p = true;
4040 }
4041 else
4042 {
4043 string_tree = tok->u.value;
4044 curr_type = tok->type;
4045 }
4046 type = curr_type;
4047
4048 /* Try to avoid the overhead of creating and destroying an obstack
4049 for the common case of just one string. */
4050 if (!cp_parser_is_string_literal
4051 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4052 {
4053 cp_lexer_consume_token (parser->lexer);
4054
4055 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4056 str.len = TREE_STRING_LENGTH (string_tree);
4057 count = 1;
4058
4059 if (curr_tok_is_userdef_p)
4060 {
4061 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4062 have_suffix_p = 1;
4063 curr_type = cpp_userdef_string_remove_type (tok->type);
4064 }
4065 else
4066 curr_type = tok->type;
4067
4068 strs = &str;
4069 }
4070 else
4071 {
4072 location_t last_tok_loc = tok->location;
4073 gcc_obstack_init (&str_ob);
4074 count = 0;
4075
4076 do
4077 {
4078 cp_lexer_consume_token (parser->lexer);
4079 count++;
4080 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4081 str.len = TREE_STRING_LENGTH (string_tree);
4082
4083 if (curr_tok_is_userdef_p)
4084 {
4085 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4086 if (have_suffix_p == 0)
4087 {
4088 suffix_id = curr_suffix_id;
4089 have_suffix_p = 1;
4090 }
4091 else if (have_suffix_p == 1
4092 && curr_suffix_id != suffix_id)
4093 {
4094 error ("inconsistent user-defined literal suffixes"
4095 " %qD and %qD in string literal",
4096 suffix_id, curr_suffix_id);
4097 have_suffix_p = -1;
4098 }
4099 curr_type = cpp_userdef_string_remove_type (tok->type);
4100 }
4101 else
4102 curr_type = tok->type;
4103
4104 if (type != curr_type)
4105 {
4106 if (type == CPP_STRING)
4107 type = curr_type;
4108 else if (curr_type != CPP_STRING)
4109 {
4110 rich_location rich_loc (line_table, tok->location);
4111 rich_loc.add_range (last_tok_loc, false);
4112 error_at (&rich_loc,
4113 "unsupported non-standard concatenation "
4114 "of string literals");
4115 }
4116 }
4117
4118 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4119
4120 last_tok_loc = tok->location;
4121
4122 tok = cp_lexer_peek_token (parser->lexer);
4123 if (cpp_userdef_string_p (tok->type))
4124 {
4125 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4126 curr_type = cpp_userdef_string_remove_type (tok->type);
4127 curr_tok_is_userdef_p = true;
4128 }
4129 else
4130 {
4131 string_tree = tok->u.value;
4132 curr_type = tok->type;
4133 curr_tok_is_userdef_p = false;
4134 }
4135 }
4136 while (cp_parser_is_string_literal (tok));
4137
4138 /* A string literal built by concatenation has its caret=start at
4139 the start of the initial string, and its finish at the finish of
4140 the final string literal. */
4141 loc = make_location (loc, loc, get_finish (last_tok_loc));
4142
4143 strs = (cpp_string *) obstack_finish (&str_ob);
4144 }
4145
4146 if (type != CPP_STRING && !wide_ok)
4147 {
4148 cp_parser_error (parser, "a wide string is invalid in this context");
4149 type = CPP_STRING;
4150 }
4151
4152 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4153 (parse_in, strs, count, &istr, type))
4154 {
4155 value = build_string (istr.len, (const char *)istr.text);
4156 free (CONST_CAST (unsigned char *, istr.text));
4157
4158 switch (type)
4159 {
4160 default:
4161 case CPP_STRING:
4162 case CPP_UTF8STRING:
4163 TREE_TYPE (value) = char_array_type_node;
4164 break;
4165 case CPP_STRING16:
4166 TREE_TYPE (value) = char16_array_type_node;
4167 break;
4168 case CPP_STRING32:
4169 TREE_TYPE (value) = char32_array_type_node;
4170 break;
4171 case CPP_WSTRING:
4172 TREE_TYPE (value) = wchar_array_type_node;
4173 break;
4174 }
4175
4176 value = fix_string_type (value);
4177
4178 if (have_suffix_p)
4179 {
4180 tree literal = build_userdef_literal (suffix_id, value,
4181 OT_NONE, NULL_TREE);
4182 if (lookup_udlit)
4183 value = cp_parser_userdef_string_literal (literal);
4184 else
4185 value = literal;
4186 }
4187 }
4188 else
4189 /* cpp_interpret_string has issued an error. */
4190 value = error_mark_node;
4191
4192 if (count > 1)
4193 obstack_free (&str_ob, 0);
4194
4195 return cp_expr (value, loc);
4196 }
4197
4198 /* Look up a literal operator with the name and the exact arguments. */
4199
4200 static tree
4201 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4202 {
4203 tree decl;
4204 decl = lookup_name (name);
4205 if (!decl || !is_overloaded_fn (decl))
4206 return error_mark_node;
4207
4208 for (lkp_iterator iter (decl); iter; ++iter)
4209 {
4210 unsigned int ix;
4211 bool found = true;
4212 tree fn = *iter;
4213 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4214 if (parmtypes != NULL_TREE)
4215 {
4216 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4217 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4218 {
4219 tree tparm = TREE_VALUE (parmtypes);
4220 tree targ = TREE_TYPE ((*args)[ix]);
4221 bool ptr = TYPE_PTR_P (tparm);
4222 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4223 if ((ptr || arr || !same_type_p (tparm, targ))
4224 && (!ptr || !arr
4225 || !same_type_p (TREE_TYPE (tparm),
4226 TREE_TYPE (targ))))
4227 found = false;
4228 }
4229 if (found
4230 && ix == vec_safe_length (args)
4231 /* May be this should be sufficient_parms_p instead,
4232 depending on how exactly should user-defined literals
4233 work in presence of default arguments on the literal
4234 operator parameters. */
4235 && parmtypes == void_list_node)
4236 return decl;
4237 }
4238 }
4239
4240 return error_mark_node;
4241 }
4242
4243 /* Parse a user-defined char constant. Returns a call to a user-defined
4244 literal operator taking the character as an argument. */
4245
4246 static cp_expr
4247 cp_parser_userdef_char_literal (cp_parser *parser)
4248 {
4249 cp_token *token = cp_lexer_consume_token (parser->lexer);
4250 tree literal = token->u.value;
4251 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4252 tree value = USERDEF_LITERAL_VALUE (literal);
4253 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4254 tree decl, result;
4255
4256 /* Build up a call to the user-defined operator */
4257 /* Lookup the name we got back from the id-expression. */
4258 vec<tree, va_gc> *args = make_tree_vector ();
4259 vec_safe_push (args, value);
4260 decl = lookup_literal_operator (name, args);
4261 if (!decl || decl == error_mark_node)
4262 {
4263 error ("unable to find character literal operator %qD with %qT argument",
4264 name, TREE_TYPE (value));
4265 release_tree_vector (args);
4266 return error_mark_node;
4267 }
4268 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4269 release_tree_vector (args);
4270 return result;
4271 }
4272
4273 /* A subroutine of cp_parser_userdef_numeric_literal to
4274 create a char... template parameter pack from a string node. */
4275
4276 static tree
4277 make_char_string_pack (tree value)
4278 {
4279 tree charvec;
4280 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4281 const char *str = TREE_STRING_POINTER (value);
4282 int i, len = TREE_STRING_LENGTH (value) - 1;
4283 tree argvec = make_tree_vec (1);
4284
4285 /* Fill in CHARVEC with all of the parameters. */
4286 charvec = make_tree_vec (len);
4287 for (i = 0; i < len; ++i)
4288 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4289
4290 /* Build the argument packs. */
4291 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4292
4293 TREE_VEC_ELT (argvec, 0) = argpack;
4294
4295 return argvec;
4296 }
4297
4298 /* A subroutine of cp_parser_userdef_numeric_literal to
4299 create a char... template parameter pack from a string node. */
4300
4301 static tree
4302 make_string_pack (tree value)
4303 {
4304 tree charvec;
4305 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4306 const unsigned char *str
4307 = (const unsigned char *) TREE_STRING_POINTER (value);
4308 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4309 int len = TREE_STRING_LENGTH (value) / sz - 1;
4310 tree argvec = make_tree_vec (2);
4311
4312 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4313 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4314
4315 /* First template parm is character type. */
4316 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4317
4318 /* Fill in CHARVEC with all of the parameters. */
4319 charvec = make_tree_vec (len);
4320 for (int i = 0; i < len; ++i)
4321 TREE_VEC_ELT (charvec, i)
4322 = double_int_to_tree (str_char_type_node,
4323 double_int::from_buffer (str + i * sz, sz));
4324
4325 /* Build the argument packs. */
4326 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4327
4328 TREE_VEC_ELT (argvec, 1) = argpack;
4329
4330 return argvec;
4331 }
4332
4333 /* Parse a user-defined numeric constant. returns a call to a user-defined
4334 literal operator. */
4335
4336 static cp_expr
4337 cp_parser_userdef_numeric_literal (cp_parser *parser)
4338 {
4339 cp_token *token = cp_lexer_consume_token (parser->lexer);
4340 tree literal = token->u.value;
4341 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4342 tree value = USERDEF_LITERAL_VALUE (literal);
4343 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4344 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4345 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4346 tree decl, result;
4347 vec<tree, va_gc> *args;
4348
4349 /* Look for a literal operator taking the exact type of numeric argument
4350 as the literal value. */
4351 args = make_tree_vector ();
4352 vec_safe_push (args, value);
4353 decl = lookup_literal_operator (name, args);
4354 if (decl && decl != error_mark_node)
4355 {
4356 result = finish_call_expr (decl, &args, false, true,
4357 tf_warning_or_error);
4358
4359 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4360 {
4361 warning_at (token->location, OPT_Woverflow,
4362 "integer literal exceeds range of %qT type",
4363 long_long_unsigned_type_node);
4364 }
4365 else
4366 {
4367 if (overflow > 0)
4368 warning_at (token->location, OPT_Woverflow,
4369 "floating literal exceeds range of %qT type",
4370 long_double_type_node);
4371 else if (overflow < 0)
4372 warning_at (token->location, OPT_Woverflow,
4373 "floating literal truncated to zero");
4374 }
4375
4376 release_tree_vector (args);
4377 return result;
4378 }
4379 release_tree_vector (args);
4380
4381 /* If the numeric argument didn't work, look for a raw literal
4382 operator taking a const char* argument consisting of the number
4383 in string format. */
4384 args = make_tree_vector ();
4385 vec_safe_push (args, num_string);
4386 decl = lookup_literal_operator (name, args);
4387 if (decl && decl != error_mark_node)
4388 {
4389 result = finish_call_expr (decl, &args, false, true,
4390 tf_warning_or_error);
4391 release_tree_vector (args);
4392 return result;
4393 }
4394 release_tree_vector (args);
4395
4396 /* If the raw literal didn't work, look for a non-type template
4397 function with parameter pack char.... Call the function with
4398 template parameter characters representing the number. */
4399 args = make_tree_vector ();
4400 decl = lookup_literal_operator (name, args);
4401 if (decl && decl != error_mark_node)
4402 {
4403 tree tmpl_args = make_char_string_pack (num_string);
4404 decl = lookup_template_function (decl, tmpl_args);
4405 result = finish_call_expr (decl, &args, false, true,
4406 tf_warning_or_error);
4407 release_tree_vector (args);
4408 return result;
4409 }
4410
4411 release_tree_vector (args);
4412
4413 error ("unable to find numeric literal operator %qD", name);
4414 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4415 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4416 "to enable more built-in suffixes");
4417 return error_mark_node;
4418 }
4419
4420 /* Parse a user-defined string constant. Returns a call to a user-defined
4421 literal operator taking a character pointer and the length of the string
4422 as arguments. */
4423
4424 static tree
4425 cp_parser_userdef_string_literal (tree literal)
4426 {
4427 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4428 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4429 tree value = USERDEF_LITERAL_VALUE (literal);
4430 int len = TREE_STRING_LENGTH (value)
4431 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4432 tree decl, result;
4433 vec<tree, va_gc> *args;
4434
4435 /* Build up a call to the user-defined operator. */
4436 /* Lookup the name we got back from the id-expression. */
4437 args = make_tree_vector ();
4438 vec_safe_push (args, value);
4439 vec_safe_push (args, build_int_cst (size_type_node, len));
4440 decl = lookup_literal_operator (name, args);
4441
4442 if (decl && decl != error_mark_node)
4443 {
4444 result = finish_call_expr (decl, &args, false, true,
4445 tf_warning_or_error);
4446 release_tree_vector (args);
4447 return result;
4448 }
4449 release_tree_vector (args);
4450
4451 /* Look for a template function with typename parameter CharT
4452 and parameter pack CharT... Call the function with
4453 template parameter characters representing the string. */
4454 args = make_tree_vector ();
4455 decl = lookup_literal_operator (name, args);
4456 if (decl && decl != error_mark_node)
4457 {
4458 tree tmpl_args = make_string_pack (value);
4459 decl = lookup_template_function (decl, tmpl_args);
4460 result = finish_call_expr (decl, &args, false, true,
4461 tf_warning_or_error);
4462 release_tree_vector (args);
4463 return result;
4464 }
4465 release_tree_vector (args);
4466
4467 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4468 name, TREE_TYPE (value), size_type_node);
4469 return error_mark_node;
4470 }
4471
4472
4473 /* Basic concepts [gram.basic] */
4474
4475 /* Parse a translation-unit.
4476
4477 translation-unit:
4478 declaration-seq [opt]
4479
4480 Returns TRUE if all went well. */
4481
4482 static bool
4483 cp_parser_translation_unit (cp_parser* parser)
4484 {
4485 /* The address of the first non-permanent object on the declarator
4486 obstack. */
4487 static void *declarator_obstack_base;
4488
4489 bool success;
4490
4491 /* Create the declarator obstack, if necessary. */
4492 if (!cp_error_declarator)
4493 {
4494 gcc_obstack_init (&declarator_obstack);
4495 /* Create the error declarator. */
4496 cp_error_declarator = make_declarator (cdk_error);
4497 /* Create the empty parameter list. */
4498 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4499 UNKNOWN_LOCATION);
4500 /* Remember where the base of the declarator obstack lies. */
4501 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4502 }
4503
4504 cp_parser_declaration_seq_opt (parser);
4505
4506 /* If there are no tokens left then all went well. */
4507 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4508 {
4509 /* Get rid of the token array; we don't need it any more. */
4510 cp_lexer_destroy (parser->lexer);
4511 parser->lexer = NULL;
4512
4513 /* This file might have been a context that's implicitly extern
4514 "C". If so, pop the lang context. (Only relevant for PCH.) */
4515 if (parser->implicit_extern_c)
4516 {
4517 pop_lang_context ();
4518 parser->implicit_extern_c = false;
4519 }
4520
4521 /* Finish up. */
4522 finish_translation_unit ();
4523
4524 success = true;
4525 }
4526 else
4527 {
4528 cp_parser_error (parser, "expected declaration");
4529 success = false;
4530 }
4531
4532 /* Make sure the declarator obstack was fully cleaned up. */
4533 gcc_assert (obstack_next_free (&declarator_obstack)
4534 == declarator_obstack_base);
4535
4536 /* All went well. */
4537 return success;
4538 }
4539
4540 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4541 decltype context. */
4542
4543 static inline tsubst_flags_t
4544 complain_flags (bool decltype_p)
4545 {
4546 tsubst_flags_t complain = tf_warning_or_error;
4547 if (decltype_p)
4548 complain |= tf_decltype;
4549 return complain;
4550 }
4551
4552 /* We're about to parse a collection of statements. If we're currently
4553 parsing tentatively, set up a firewall so that any nested
4554 cp_parser_commit_to_tentative_parse won't affect the current context. */
4555
4556 static cp_token_position
4557 cp_parser_start_tentative_firewall (cp_parser *parser)
4558 {
4559 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4560 return 0;
4561
4562 cp_parser_parse_tentatively (parser);
4563 cp_parser_commit_to_topmost_tentative_parse (parser);
4564 return cp_lexer_token_position (parser->lexer, false);
4565 }
4566
4567 /* We've finished parsing the collection of statements. Wrap up the
4568 firewall and replace the relevant tokens with the parsed form. */
4569
4570 static void
4571 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4572 tree expr)
4573 {
4574 if (!start)
4575 return;
4576
4577 /* Finish the firewall level. */
4578 cp_parser_parse_definitely (parser);
4579 /* And remember the result of the parse for when we try again. */
4580 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4581 token->type = CPP_PREPARSED_EXPR;
4582 token->u.value = expr;
4583 token->keyword = RID_MAX;
4584 cp_lexer_purge_tokens_after (parser->lexer, start);
4585 }
4586
4587 /* Like the above functions, but let the user modify the tokens. Used by
4588 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4589 later parses, so it makes sense to localize the effects of
4590 cp_parser_commit_to_tentative_parse. */
4591
4592 struct tentative_firewall
4593 {
4594 cp_parser *parser;
4595 bool set;
4596
4597 tentative_firewall (cp_parser *p): parser(p)
4598 {
4599 /* If we're currently parsing tentatively, start a committed level as a
4600 firewall and then an inner tentative parse. */
4601 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4602 {
4603 cp_parser_parse_tentatively (parser);
4604 cp_parser_commit_to_topmost_tentative_parse (parser);
4605 cp_parser_parse_tentatively (parser);
4606 }
4607 }
4608
4609 ~tentative_firewall()
4610 {
4611 if (set)
4612 {
4613 /* Finish the inner tentative parse and the firewall, propagating any
4614 uncommitted error state to the outer tentative parse. */
4615 bool err = cp_parser_error_occurred (parser);
4616 cp_parser_parse_definitely (parser);
4617 cp_parser_parse_definitely (parser);
4618 if (err)
4619 cp_parser_simulate_error (parser);
4620 }
4621 }
4622 };
4623
4624 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4625 This class is for tracking such a matching pair of symbols.
4626 In particular, it tracks the location of the first token,
4627 so that if the second token is missing, we can highlight the
4628 location of the first token when notifying the user about the
4629 problem. */
4630
4631 template <typename traits_t>
4632 class token_pair
4633 {
4634 public:
4635 /* token_pair's ctor. */
4636 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4637
4638 /* If the next token is the opening symbol for this pair, consume it and
4639 return true.
4640 Otherwise, issue an error and return false.
4641 In either case, record the location of the opening token. */
4642
4643 bool require_open (cp_parser *parser)
4644 {
4645 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4646 return cp_parser_require (parser, traits_t::open_token_type,
4647 traits_t::required_token_open);
4648 }
4649
4650 /* Consume the next token from PARSER, recording its location as
4651 that of the opening token within the pair. */
4652
4653 cp_token * consume_open (cp_parser *parser)
4654 {
4655 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4656 gcc_assert (tok->type == traits_t::open_token_type);
4657 m_open_loc = tok->location;
4658 return tok;
4659 }
4660
4661 /* If the next token is the closing symbol for this pair, consume it
4662 and return it.
4663 Otherwise, issue an error, highlighting the location of the
4664 corresponding opening token, and return NULL. */
4665
4666 cp_token *require_close (cp_parser *parser) const
4667 {
4668 return cp_parser_require (parser, traits_t::close_token_type,
4669 traits_t::required_token_close,
4670 m_open_loc);
4671 }
4672
4673 private:
4674 location_t m_open_loc;
4675 };
4676
4677 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4678
4679 struct matching_paren_traits
4680 {
4681 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4682 static const enum required_token required_token_open = RT_OPEN_PAREN;
4683 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4684 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4685 };
4686
4687 /* "matching_parens" is a token_pair<T> class for tracking matching
4688 pairs of parentheses. */
4689
4690 typedef token_pair<matching_paren_traits> matching_parens;
4691
4692 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4693
4694 struct matching_brace_traits
4695 {
4696 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4697 static const enum required_token required_token_open = RT_OPEN_BRACE;
4698 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4699 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4700 };
4701
4702 /* "matching_braces" is a token_pair<T> class for tracking matching
4703 pairs of braces. */
4704
4705 typedef token_pair<matching_brace_traits> matching_braces;
4706
4707
4708 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4709 enclosing parentheses. */
4710
4711 static cp_expr
4712 cp_parser_statement_expr (cp_parser *parser)
4713 {
4714 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4715
4716 /* Consume the '('. */
4717 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4718 matching_parens parens;
4719 parens.consume_open (parser);
4720 /* Start the statement-expression. */
4721 tree expr = begin_stmt_expr ();
4722 /* Parse the compound-statement. */
4723 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4724 /* Finish up. */
4725 expr = finish_stmt_expr (expr, false);
4726 /* Consume the ')'. */
4727 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4728 if (!parens.require_close (parser))
4729 cp_parser_skip_to_end_of_statement (parser);
4730
4731 cp_parser_end_tentative_firewall (parser, start, expr);
4732 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4733 return cp_expr (expr, combined_loc);
4734 }
4735
4736 /* Expressions [gram.expr] */
4737
4738 /* Parse a fold-operator.
4739
4740 fold-operator:
4741 - * / % ^ & | = < > << >>
4742 = -= *= /= %= ^= &= |= <<= >>=
4743 == != <= >= && || , .* ->*
4744
4745 This returns the tree code corresponding to the matched operator
4746 as an int. When the current token matches a compound assignment
4747 opertor, the resulting tree code is the negative value of the
4748 non-assignment operator. */
4749
4750 static int
4751 cp_parser_fold_operator (cp_token *token)
4752 {
4753 switch (token->type)
4754 {
4755 case CPP_PLUS: return PLUS_EXPR;
4756 case CPP_MINUS: return MINUS_EXPR;
4757 case CPP_MULT: return MULT_EXPR;
4758 case CPP_DIV: return TRUNC_DIV_EXPR;
4759 case CPP_MOD: return TRUNC_MOD_EXPR;
4760 case CPP_XOR: return BIT_XOR_EXPR;
4761 case CPP_AND: return BIT_AND_EXPR;
4762 case CPP_OR: return BIT_IOR_EXPR;
4763 case CPP_LSHIFT: return LSHIFT_EXPR;
4764 case CPP_RSHIFT: return RSHIFT_EXPR;
4765
4766 case CPP_EQ: return -NOP_EXPR;
4767 case CPP_PLUS_EQ: return -PLUS_EXPR;
4768 case CPP_MINUS_EQ: return -MINUS_EXPR;
4769 case CPP_MULT_EQ: return -MULT_EXPR;
4770 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4771 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4772 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4773 case CPP_AND_EQ: return -BIT_AND_EXPR;
4774 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4775 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4776 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4777
4778 case CPP_EQ_EQ: return EQ_EXPR;
4779 case CPP_NOT_EQ: return NE_EXPR;
4780 case CPP_LESS: return LT_EXPR;
4781 case CPP_GREATER: return GT_EXPR;
4782 case CPP_LESS_EQ: return LE_EXPR;
4783 case CPP_GREATER_EQ: return GE_EXPR;
4784
4785 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4786 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4787
4788 case CPP_COMMA: return COMPOUND_EXPR;
4789
4790 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4791 case CPP_DEREF_STAR: return MEMBER_REF;
4792
4793 default: return ERROR_MARK;
4794 }
4795 }
4796
4797 /* Returns true if CODE indicates a binary expression, which is not allowed in
4798 the LHS of a fold-expression. More codes will need to be added to use this
4799 function in other contexts. */
4800
4801 static bool
4802 is_binary_op (tree_code code)
4803 {
4804 switch (code)
4805 {
4806 case PLUS_EXPR:
4807 case POINTER_PLUS_EXPR:
4808 case MINUS_EXPR:
4809 case MULT_EXPR:
4810 case TRUNC_DIV_EXPR:
4811 case TRUNC_MOD_EXPR:
4812 case BIT_XOR_EXPR:
4813 case BIT_AND_EXPR:
4814 case BIT_IOR_EXPR:
4815 case LSHIFT_EXPR:
4816 case RSHIFT_EXPR:
4817
4818 case MODOP_EXPR:
4819
4820 case EQ_EXPR:
4821 case NE_EXPR:
4822 case LE_EXPR:
4823 case GE_EXPR:
4824 case LT_EXPR:
4825 case GT_EXPR:
4826
4827 case TRUTH_ANDIF_EXPR:
4828 case TRUTH_ORIF_EXPR:
4829
4830 case COMPOUND_EXPR:
4831
4832 case DOTSTAR_EXPR:
4833 case MEMBER_REF:
4834 return true;
4835
4836 default:
4837 return false;
4838 }
4839 }
4840
4841 /* If the next token is a suitable fold operator, consume it and return as
4842 the function above. */
4843
4844 static int
4845 cp_parser_fold_operator (cp_parser *parser)
4846 {
4847 cp_token* token = cp_lexer_peek_token (parser->lexer);
4848 int code = cp_parser_fold_operator (token);
4849 if (code != ERROR_MARK)
4850 cp_lexer_consume_token (parser->lexer);
4851 return code;
4852 }
4853
4854 /* Parse a fold-expression.
4855
4856 fold-expression:
4857 ( ... folding-operator cast-expression)
4858 ( cast-expression folding-operator ... )
4859 ( cast-expression folding operator ... folding-operator cast-expression)
4860
4861 Note that the '(' and ')' are matched in primary expression. */
4862
4863 static cp_expr
4864 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4865 {
4866 cp_id_kind pidk;
4867
4868 // Left fold.
4869 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4870 {
4871 cp_lexer_consume_token (parser->lexer);
4872 int op = cp_parser_fold_operator (parser);
4873 if (op == ERROR_MARK)
4874 {
4875 cp_parser_error (parser, "expected binary operator");
4876 return error_mark_node;
4877 }
4878
4879 tree expr = cp_parser_cast_expression (parser, false, false,
4880 false, &pidk);
4881 if (expr == error_mark_node)
4882 return error_mark_node;
4883 return finish_left_unary_fold_expr (expr, op);
4884 }
4885
4886 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4887 int op = cp_parser_fold_operator (parser);
4888 if (op == ERROR_MARK)
4889 {
4890 cp_parser_error (parser, "expected binary operator");
4891 return error_mark_node;
4892 }
4893
4894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4895 {
4896 cp_parser_error (parser, "expected ...");
4897 return error_mark_node;
4898 }
4899 cp_lexer_consume_token (parser->lexer);
4900
4901 /* The operands of a fold-expression are cast-expressions, so binary or
4902 conditional expressions are not allowed. We check this here to avoid
4903 tentative parsing. */
4904 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4905 /* OK, the expression was parenthesized. */;
4906 else if (is_binary_op (TREE_CODE (expr1)))
4907 error_at (location_of (expr1),
4908 "binary expression in operand of fold-expression");
4909 else if (TREE_CODE (expr1) == COND_EXPR)
4910 error_at (location_of (expr1),
4911 "conditional expression in operand of fold-expression");
4912
4913 // Right fold.
4914 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4915 return finish_right_unary_fold_expr (expr1, op);
4916
4917 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4918 {
4919 cp_parser_error (parser, "mismatched operator in fold-expression");
4920 return error_mark_node;
4921 }
4922 cp_lexer_consume_token (parser->lexer);
4923
4924 // Binary left or right fold.
4925 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4926 if (expr2 == error_mark_node)
4927 return error_mark_node;
4928 return finish_binary_fold_expr (expr1, expr2, op);
4929 }
4930
4931 /* Parse a primary-expression.
4932
4933 primary-expression:
4934 literal
4935 this
4936 ( expression )
4937 id-expression
4938 lambda-expression (C++11)
4939
4940 GNU Extensions:
4941
4942 primary-expression:
4943 ( compound-statement )
4944 __builtin_va_arg ( assignment-expression , type-id )
4945 __builtin_offsetof ( type-id , offsetof-expression )
4946
4947 C++ Extensions:
4948 __has_nothrow_assign ( type-id )
4949 __has_nothrow_constructor ( type-id )
4950 __has_nothrow_copy ( type-id )
4951 __has_trivial_assign ( type-id )
4952 __has_trivial_constructor ( type-id )
4953 __has_trivial_copy ( type-id )
4954 __has_trivial_destructor ( type-id )
4955 __has_virtual_destructor ( type-id )
4956 __is_abstract ( type-id )
4957 __is_base_of ( type-id , type-id )
4958 __is_class ( type-id )
4959 __is_empty ( type-id )
4960 __is_enum ( type-id )
4961 __is_final ( type-id )
4962 __is_literal_type ( type-id )
4963 __is_pod ( type-id )
4964 __is_polymorphic ( type-id )
4965 __is_std_layout ( type-id )
4966 __is_trivial ( type-id )
4967 __is_union ( type-id )
4968
4969 Objective-C++ Extension:
4970
4971 primary-expression:
4972 objc-expression
4973
4974 literal:
4975 __null
4976
4977 ADDRESS_P is true iff this expression was immediately preceded by
4978 "&" and therefore might denote a pointer-to-member. CAST_P is true
4979 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4980 true iff this expression is a template argument.
4981
4982 Returns a representation of the expression. Upon return, *IDK
4983 indicates what kind of id-expression (if any) was present. */
4984
4985 static cp_expr
4986 cp_parser_primary_expression (cp_parser *parser,
4987 bool address_p,
4988 bool cast_p,
4989 bool template_arg_p,
4990 bool decltype_p,
4991 cp_id_kind *idk)
4992 {
4993 cp_token *token = NULL;
4994
4995 /* Assume the primary expression is not an id-expression. */
4996 *idk = CP_ID_KIND_NONE;
4997
4998 /* Peek at the next token. */
4999 token = cp_lexer_peek_token (parser->lexer);
5000 switch ((int) token->type)
5001 {
5002 /* literal:
5003 integer-literal
5004 character-literal
5005 floating-literal
5006 string-literal
5007 boolean-literal
5008 pointer-literal
5009 user-defined-literal */
5010 case CPP_CHAR:
5011 case CPP_CHAR16:
5012 case CPP_CHAR32:
5013 case CPP_WCHAR:
5014 case CPP_UTF8CHAR:
5015 case CPP_NUMBER:
5016 case CPP_PREPARSED_EXPR:
5017 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5018 return cp_parser_userdef_numeric_literal (parser);
5019 token = cp_lexer_consume_token (parser->lexer);
5020 if (TREE_CODE (token->u.value) == FIXED_CST)
5021 {
5022 error_at (token->location,
5023 "fixed-point types not supported in C++");
5024 return error_mark_node;
5025 }
5026 /* Floating-point literals are only allowed in an integral
5027 constant expression if they are cast to an integral or
5028 enumeration type. */
5029 if (TREE_CODE (token->u.value) == REAL_CST
5030 && parser->integral_constant_expression_p
5031 && pedantic)
5032 {
5033 /* CAST_P will be set even in invalid code like "int(2.7 +
5034 ...)". Therefore, we have to check that the next token
5035 is sure to end the cast. */
5036 if (cast_p)
5037 {
5038 cp_token *next_token;
5039
5040 next_token = cp_lexer_peek_token (parser->lexer);
5041 if (/* The comma at the end of an
5042 enumerator-definition. */
5043 next_token->type != CPP_COMMA
5044 /* The curly brace at the end of an enum-specifier. */
5045 && next_token->type != CPP_CLOSE_BRACE
5046 /* The end of a statement. */
5047 && next_token->type != CPP_SEMICOLON
5048 /* The end of the cast-expression. */
5049 && next_token->type != CPP_CLOSE_PAREN
5050 /* The end of an array bound. */
5051 && next_token->type != CPP_CLOSE_SQUARE
5052 /* The closing ">" in a template-argument-list. */
5053 && (next_token->type != CPP_GREATER
5054 || parser->greater_than_is_operator_p)
5055 /* C++0x only: A ">>" treated like two ">" tokens,
5056 in a template-argument-list. */
5057 && (next_token->type != CPP_RSHIFT
5058 || (cxx_dialect == cxx98)
5059 || parser->greater_than_is_operator_p))
5060 cast_p = false;
5061 }
5062
5063 /* If we are within a cast, then the constraint that the
5064 cast is to an integral or enumeration type will be
5065 checked at that point. If we are not within a cast, then
5066 this code is invalid. */
5067 if (!cast_p)
5068 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5069 }
5070 return cp_expr (token->u.value, token->location);
5071
5072 case CPP_CHAR_USERDEF:
5073 case CPP_CHAR16_USERDEF:
5074 case CPP_CHAR32_USERDEF:
5075 case CPP_WCHAR_USERDEF:
5076 case CPP_UTF8CHAR_USERDEF:
5077 return cp_parser_userdef_char_literal (parser);
5078
5079 case CPP_STRING:
5080 case CPP_STRING16:
5081 case CPP_STRING32:
5082 case CPP_WSTRING:
5083 case CPP_UTF8STRING:
5084 case CPP_STRING_USERDEF:
5085 case CPP_STRING16_USERDEF:
5086 case CPP_STRING32_USERDEF:
5087 case CPP_WSTRING_USERDEF:
5088 case CPP_UTF8STRING_USERDEF:
5089 /* ??? Should wide strings be allowed when parser->translate_strings_p
5090 is false (i.e. in attributes)? If not, we can kill the third
5091 argument to cp_parser_string_literal. */
5092 return cp_parser_string_literal (parser,
5093 parser->translate_strings_p,
5094 true);
5095
5096 case CPP_OPEN_PAREN:
5097 /* If we see `( { ' then we are looking at the beginning of
5098 a GNU statement-expression. */
5099 if (cp_parser_allow_gnu_extensions_p (parser)
5100 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5101 {
5102 /* Statement-expressions are not allowed by the standard. */
5103 pedwarn (token->location, OPT_Wpedantic,
5104 "ISO C++ forbids braced-groups within expressions");
5105
5106 /* And they're not allowed outside of a function-body; you
5107 cannot, for example, write:
5108
5109 int i = ({ int j = 3; j + 1; });
5110
5111 at class or namespace scope. */
5112 if (!parser->in_function_body
5113 || parser->in_template_argument_list_p)
5114 {
5115 error_at (token->location,
5116 "statement-expressions are not allowed outside "
5117 "functions nor in template-argument lists");
5118 cp_parser_skip_to_end_of_block_or_statement (parser);
5119 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5120 cp_lexer_consume_token (parser->lexer);
5121 return error_mark_node;
5122 }
5123 else
5124 return cp_parser_statement_expr (parser);
5125 }
5126 /* Otherwise it's a normal parenthesized expression. */
5127 {
5128 cp_expr expr;
5129 bool saved_greater_than_is_operator_p;
5130
5131 location_t open_paren_loc = token->location;
5132
5133 /* Consume the `('. */
5134 matching_parens parens;
5135 parens.consume_open (parser);
5136 /* Within a parenthesized expression, a `>' token is always
5137 the greater-than operator. */
5138 saved_greater_than_is_operator_p
5139 = parser->greater_than_is_operator_p;
5140 parser->greater_than_is_operator_p = true;
5141
5142 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5143 /* Left fold expression. */
5144 expr = NULL_TREE;
5145 else
5146 /* Parse the parenthesized expression. */
5147 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5148
5149 token = cp_lexer_peek_token (parser->lexer);
5150 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5151 {
5152 expr = cp_parser_fold_expression (parser, expr);
5153 if (expr != error_mark_node
5154 && cxx_dialect < cxx17
5155 && !in_system_header_at (input_location))
5156 pedwarn (input_location, 0, "fold-expressions only available "
5157 "with -std=c++17 or -std=gnu++17");
5158 }
5159 else
5160 /* Let the front end know that this expression was
5161 enclosed in parentheses. This matters in case, for
5162 example, the expression is of the form `A::B', since
5163 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5164 not. */
5165 expr = finish_parenthesized_expr (expr);
5166
5167 /* DR 705: Wrapping an unqualified name in parentheses
5168 suppresses arg-dependent lookup. We want to pass back
5169 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5170 (c++/37862), but none of the others. */
5171 if (*idk != CP_ID_KIND_QUALIFIED)
5172 *idk = CP_ID_KIND_NONE;
5173
5174 /* The `>' token might be the end of a template-id or
5175 template-parameter-list now. */
5176 parser->greater_than_is_operator_p
5177 = saved_greater_than_is_operator_p;
5178
5179 /* Consume the `)'. */
5180 token = cp_lexer_peek_token (parser->lexer);
5181 location_t close_paren_loc = token->location;
5182 expr.set_range (open_paren_loc, close_paren_loc);
5183 if (!parens.require_close (parser)
5184 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5185 cp_parser_skip_to_end_of_statement (parser);
5186
5187 return expr;
5188 }
5189
5190 case CPP_OPEN_SQUARE:
5191 {
5192 if (c_dialect_objc ())
5193 {
5194 /* We might have an Objective-C++ message. */
5195 cp_parser_parse_tentatively (parser);
5196 tree msg = cp_parser_objc_message_expression (parser);
5197 /* If that works out, we're done ... */
5198 if (cp_parser_parse_definitely (parser))
5199 return msg;
5200 /* ... else, fall though to see if it's a lambda. */
5201 }
5202 cp_expr lam = cp_parser_lambda_expression (parser);
5203 /* Don't warn about a failed tentative parse. */
5204 if (cp_parser_error_occurred (parser))
5205 return error_mark_node;
5206 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5207 return lam;
5208 }
5209
5210 case CPP_OBJC_STRING:
5211 if (c_dialect_objc ())
5212 /* We have an Objective-C++ string literal. */
5213 return cp_parser_objc_expression (parser);
5214 cp_parser_error (parser, "expected primary-expression");
5215 return error_mark_node;
5216
5217 case CPP_KEYWORD:
5218 switch (token->keyword)
5219 {
5220 /* These two are the boolean literals. */
5221 case RID_TRUE:
5222 cp_lexer_consume_token (parser->lexer);
5223 return cp_expr (boolean_true_node, token->location);
5224 case RID_FALSE:
5225 cp_lexer_consume_token (parser->lexer);
5226 return cp_expr (boolean_false_node, token->location);
5227
5228 /* The `__null' literal. */
5229 case RID_NULL:
5230 cp_lexer_consume_token (parser->lexer);
5231 return cp_expr (null_node, token->location);
5232
5233 /* The `nullptr' literal. */
5234 case RID_NULLPTR:
5235 cp_lexer_consume_token (parser->lexer);
5236 return cp_expr (nullptr_node, token->location);
5237
5238 /* Recognize the `this' keyword. */
5239 case RID_THIS:
5240 cp_lexer_consume_token (parser->lexer);
5241 if (parser->local_variables_forbidden_p)
5242 {
5243 error_at (token->location,
5244 "%<this%> may not be used in this context");
5245 return error_mark_node;
5246 }
5247 /* Pointers cannot appear in constant-expressions. */
5248 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5249 return error_mark_node;
5250 return cp_expr (finish_this_expr (), token->location);
5251
5252 /* The `operator' keyword can be the beginning of an
5253 id-expression. */
5254 case RID_OPERATOR:
5255 goto id_expression;
5256
5257 case RID_FUNCTION_NAME:
5258 case RID_PRETTY_FUNCTION_NAME:
5259 case RID_C99_FUNCTION_NAME:
5260 {
5261 non_integral_constant name;
5262
5263 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5264 __func__ are the names of variables -- but they are
5265 treated specially. Therefore, they are handled here,
5266 rather than relying on the generic id-expression logic
5267 below. Grammatically, these names are id-expressions.
5268
5269 Consume the token. */
5270 token = cp_lexer_consume_token (parser->lexer);
5271
5272 switch (token->keyword)
5273 {
5274 case RID_FUNCTION_NAME:
5275 name = NIC_FUNC_NAME;
5276 break;
5277 case RID_PRETTY_FUNCTION_NAME:
5278 name = NIC_PRETTY_FUNC;
5279 break;
5280 case RID_C99_FUNCTION_NAME:
5281 name = NIC_C99_FUNC;
5282 break;
5283 default:
5284 gcc_unreachable ();
5285 }
5286
5287 if (cp_parser_non_integral_constant_expression (parser, name))
5288 return error_mark_node;
5289
5290 /* Look up the name. */
5291 return finish_fname (token->u.value);
5292 }
5293
5294 case RID_VA_ARG:
5295 {
5296 tree expression;
5297 tree type;
5298 source_location type_location;
5299 location_t start_loc
5300 = cp_lexer_peek_token (parser->lexer)->location;
5301 /* The `__builtin_va_arg' construct is used to handle
5302 `va_arg'. Consume the `__builtin_va_arg' token. */
5303 cp_lexer_consume_token (parser->lexer);
5304 /* Look for the opening `('. */
5305 matching_parens parens;
5306 parens.require_open (parser);
5307 /* Now, parse the assignment-expression. */
5308 expression = cp_parser_assignment_expression (parser);
5309 /* Look for the `,'. */
5310 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5311 type_location = cp_lexer_peek_token (parser->lexer)->location;
5312 /* Parse the type-id. */
5313 {
5314 type_id_in_expr_sentinel s (parser);
5315 type = cp_parser_type_id (parser);
5316 }
5317 /* Look for the closing `)'. */
5318 location_t finish_loc
5319 = cp_lexer_peek_token (parser->lexer)->location;
5320 parens.require_close (parser);
5321 /* Using `va_arg' in a constant-expression is not
5322 allowed. */
5323 if (cp_parser_non_integral_constant_expression (parser,
5324 NIC_VA_ARG))
5325 return error_mark_node;
5326 /* Construct a location of the form:
5327 __builtin_va_arg (v, int)
5328 ~~~~~~~~~~~~~~~~~~~~~^~~~
5329 with the caret at the type, ranging from the start of the
5330 "__builtin_va_arg" token to the close paren. */
5331 location_t combined_loc
5332 = make_location (type_location, start_loc, finish_loc);
5333 return build_x_va_arg (combined_loc, expression, type);
5334 }
5335
5336 case RID_OFFSETOF:
5337 return cp_parser_builtin_offsetof (parser);
5338
5339 case RID_HAS_NOTHROW_ASSIGN:
5340 case RID_HAS_NOTHROW_CONSTRUCTOR:
5341 case RID_HAS_NOTHROW_COPY:
5342 case RID_HAS_TRIVIAL_ASSIGN:
5343 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5344 case RID_HAS_TRIVIAL_COPY:
5345 case RID_HAS_TRIVIAL_DESTRUCTOR:
5346 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5347 case RID_HAS_VIRTUAL_DESTRUCTOR:
5348 case RID_IS_ABSTRACT:
5349 case RID_IS_AGGREGATE:
5350 case RID_IS_BASE_OF:
5351 case RID_IS_CLASS:
5352 case RID_IS_EMPTY:
5353 case RID_IS_ENUM:
5354 case RID_IS_FINAL:
5355 case RID_IS_LITERAL_TYPE:
5356 case RID_IS_POD:
5357 case RID_IS_POLYMORPHIC:
5358 case RID_IS_SAME_AS:
5359 case RID_IS_STD_LAYOUT:
5360 case RID_IS_TRIVIAL:
5361 case RID_IS_TRIVIALLY_ASSIGNABLE:
5362 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5363 case RID_IS_TRIVIALLY_COPYABLE:
5364 case RID_IS_UNION:
5365 case RID_IS_ASSIGNABLE:
5366 case RID_IS_CONSTRUCTIBLE:
5367 return cp_parser_trait_expr (parser, token->keyword);
5368
5369 // C++ concepts
5370 case RID_REQUIRES:
5371 return cp_parser_requires_expression (parser);
5372
5373 /* Objective-C++ expressions. */
5374 case RID_AT_ENCODE:
5375 case RID_AT_PROTOCOL:
5376 case RID_AT_SELECTOR:
5377 return cp_parser_objc_expression (parser);
5378
5379 case RID_TEMPLATE:
5380 if (parser->in_function_body
5381 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5382 == CPP_LESS))
5383 {
5384 error_at (token->location,
5385 "a template declaration cannot appear at block scope");
5386 cp_parser_skip_to_end_of_block_or_statement (parser);
5387 return error_mark_node;
5388 }
5389 /* FALLTHRU */
5390 default:
5391 cp_parser_error (parser, "expected primary-expression");
5392 return error_mark_node;
5393 }
5394
5395 /* An id-expression can start with either an identifier, a
5396 `::' as the beginning of a qualified-id, or the "operator"
5397 keyword. */
5398 case CPP_NAME:
5399 case CPP_SCOPE:
5400 case CPP_TEMPLATE_ID:
5401 case CPP_NESTED_NAME_SPECIFIER:
5402 {
5403 id_expression:
5404 cp_expr id_expression;
5405 cp_expr decl;
5406 const char *error_msg;
5407 bool template_p;
5408 bool done;
5409 cp_token *id_expr_token;
5410
5411 /* Parse the id-expression. */
5412 id_expression
5413 = cp_parser_id_expression (parser,
5414 /*template_keyword_p=*/false,
5415 /*check_dependency_p=*/true,
5416 &template_p,
5417 /*declarator_p=*/false,
5418 /*optional_p=*/false);
5419 if (id_expression == error_mark_node)
5420 return error_mark_node;
5421 id_expr_token = token;
5422 token = cp_lexer_peek_token (parser->lexer);
5423 done = (token->type != CPP_OPEN_SQUARE
5424 && token->type != CPP_OPEN_PAREN
5425 && token->type != CPP_DOT
5426 && token->type != CPP_DEREF
5427 && token->type != CPP_PLUS_PLUS
5428 && token->type != CPP_MINUS_MINUS);
5429 /* If we have a template-id, then no further lookup is
5430 required. If the template-id was for a template-class, we
5431 will sometimes have a TYPE_DECL at this point. */
5432 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5433 || TREE_CODE (id_expression) == TYPE_DECL)
5434 decl = id_expression;
5435 /* Look up the name. */
5436 else
5437 {
5438 tree ambiguous_decls;
5439
5440 /* If we already know that this lookup is ambiguous, then
5441 we've already issued an error message; there's no reason
5442 to check again. */
5443 if (id_expr_token->type == CPP_NAME
5444 && id_expr_token->error_reported)
5445 {
5446 cp_parser_simulate_error (parser);
5447 return error_mark_node;
5448 }
5449
5450 decl = cp_parser_lookup_name (parser, id_expression,
5451 none_type,
5452 template_p,
5453 /*is_namespace=*/false,
5454 /*check_dependency=*/true,
5455 &ambiguous_decls,
5456 id_expr_token->location);
5457 /* If the lookup was ambiguous, an error will already have
5458 been issued. */
5459 if (ambiguous_decls)
5460 return error_mark_node;
5461
5462 /* In Objective-C++, we may have an Objective-C 2.0
5463 dot-syntax for classes here. */
5464 if (c_dialect_objc ()
5465 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5466 && TREE_CODE (decl) == TYPE_DECL
5467 && objc_is_class_name (decl))
5468 {
5469 tree component;
5470 cp_lexer_consume_token (parser->lexer);
5471 component = cp_parser_identifier (parser);
5472 if (component == error_mark_node)
5473 return error_mark_node;
5474
5475 tree result = objc_build_class_component_ref (id_expression,
5476 component);
5477 /* Build a location of the form:
5478 expr.component
5479 ~~~~~^~~~~~~~~
5480 with caret at the start of the component name (at
5481 input_location), ranging from the start of the id_expression
5482 to the end of the component name. */
5483 location_t combined_loc
5484 = make_location (input_location, id_expression.get_start (),
5485 get_finish (input_location));
5486 protected_set_expr_location (result, combined_loc);
5487 return result;
5488 }
5489
5490 /* In Objective-C++, an instance variable (ivar) may be preferred
5491 to whatever cp_parser_lookup_name() found.
5492 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5493 rest of c-family, we have to do a little extra work to preserve
5494 any location information in cp_expr "decl". Given that
5495 objc_lookup_ivar is implemented in "c-family" and "objc", we
5496 have a trip through the pure "tree" type, rather than cp_expr.
5497 Naively copying it back to "decl" would implicitly give the
5498 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5499 store an EXPR_LOCATION. Hence we only update "decl" (and
5500 hence its location_t) if we get back a different tree node. */
5501 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5502 id_expression);
5503 if (decl_tree != decl.get_value ())
5504 decl = cp_expr (decl_tree);
5505
5506 /* If name lookup gives us a SCOPE_REF, then the
5507 qualifying scope was dependent. */
5508 if (TREE_CODE (decl) == SCOPE_REF)
5509 {
5510 /* At this point, we do not know if DECL is a valid
5511 integral constant expression. We assume that it is
5512 in fact such an expression, so that code like:
5513
5514 template <int N> struct A {
5515 int a[B<N>::i];
5516 };
5517
5518 is accepted. At template-instantiation time, we
5519 will check that B<N>::i is actually a constant. */
5520 return decl;
5521 }
5522 /* Check to see if DECL is a local variable in a context
5523 where that is forbidden. */
5524 if (parser->local_variables_forbidden_p
5525 && local_variable_p (decl))
5526 {
5527 /* It might be that we only found DECL because we are
5528 trying to be generous with pre-ISO scoping rules.
5529 For example, consider:
5530
5531 int i;
5532 void g() {
5533 for (int i = 0; i < 10; ++i) {}
5534 extern void f(int j = i);
5535 }
5536
5537 Here, name look up will originally find the out
5538 of scope `i'. We need to issue a warning message,
5539 but then use the global `i'. */
5540 decl = check_for_out_of_scope_variable (decl);
5541 if (local_variable_p (decl))
5542 {
5543 error_at (id_expr_token->location,
5544 "local variable %qD may not appear in this context",
5545 decl.get_value ());
5546 return error_mark_node;
5547 }
5548 }
5549 }
5550
5551 decl = (finish_id_expression
5552 (id_expression, decl, parser->scope,
5553 idk,
5554 parser->integral_constant_expression_p,
5555 parser->allow_non_integral_constant_expression_p,
5556 &parser->non_integral_constant_expression_p,
5557 template_p, done, address_p,
5558 template_arg_p,
5559 &error_msg,
5560 id_expression.get_location ()));
5561 if (error_msg)
5562 cp_parser_error (parser, error_msg);
5563 decl.set_location (id_expr_token->location);
5564 return decl;
5565 }
5566
5567 /* Anything else is an error. */
5568 default:
5569 cp_parser_error (parser, "expected primary-expression");
5570 return error_mark_node;
5571 }
5572 }
5573
5574 static inline cp_expr
5575 cp_parser_primary_expression (cp_parser *parser,
5576 bool address_p,
5577 bool cast_p,
5578 bool template_arg_p,
5579 cp_id_kind *idk)
5580 {
5581 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5582 /*decltype*/false, idk);
5583 }
5584
5585 /* Parse an id-expression.
5586
5587 id-expression:
5588 unqualified-id
5589 qualified-id
5590
5591 qualified-id:
5592 :: [opt] nested-name-specifier template [opt] unqualified-id
5593 :: identifier
5594 :: operator-function-id
5595 :: template-id
5596
5597 Return a representation of the unqualified portion of the
5598 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5599 a `::' or nested-name-specifier.
5600
5601 Often, if the id-expression was a qualified-id, the caller will
5602 want to make a SCOPE_REF to represent the qualified-id. This
5603 function does not do this in order to avoid wastefully creating
5604 SCOPE_REFs when they are not required.
5605
5606 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5607 `template' keyword.
5608
5609 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5610 uninstantiated templates.
5611
5612 If *TEMPLATE_P is non-NULL, it is set to true iff the
5613 `template' keyword is used to explicitly indicate that the entity
5614 named is a template.
5615
5616 If DECLARATOR_P is true, the id-expression is appearing as part of
5617 a declarator, rather than as part of an expression. */
5618
5619 static cp_expr
5620 cp_parser_id_expression (cp_parser *parser,
5621 bool template_keyword_p,
5622 bool check_dependency_p,
5623 bool *template_p,
5624 bool declarator_p,
5625 bool optional_p)
5626 {
5627 bool global_scope_p;
5628 bool nested_name_specifier_p;
5629
5630 /* Assume the `template' keyword was not used. */
5631 if (template_p)
5632 *template_p = template_keyword_p;
5633
5634 /* Look for the optional `::' operator. */
5635 global_scope_p
5636 = (!template_keyword_p
5637 && (cp_parser_global_scope_opt (parser,
5638 /*current_scope_valid_p=*/false)
5639 != NULL_TREE));
5640
5641 /* Look for the optional nested-name-specifier. */
5642 nested_name_specifier_p
5643 = (cp_parser_nested_name_specifier_opt (parser,
5644 /*typename_keyword_p=*/false,
5645 check_dependency_p,
5646 /*type_p=*/false,
5647 declarator_p,
5648 template_keyword_p)
5649 != NULL_TREE);
5650
5651 /* If there is a nested-name-specifier, then we are looking at
5652 the first qualified-id production. */
5653 if (nested_name_specifier_p)
5654 {
5655 tree saved_scope;
5656 tree saved_object_scope;
5657 tree saved_qualifying_scope;
5658 cp_expr unqualified_id;
5659 bool is_template;
5660
5661 /* See if the next token is the `template' keyword. */
5662 if (!template_p)
5663 template_p = &is_template;
5664 *template_p = cp_parser_optional_template_keyword (parser);
5665 /* Name lookup we do during the processing of the
5666 unqualified-id might obliterate SCOPE. */
5667 saved_scope = parser->scope;
5668 saved_object_scope = parser->object_scope;
5669 saved_qualifying_scope = parser->qualifying_scope;
5670 /* Process the final unqualified-id. */
5671 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5672 check_dependency_p,
5673 declarator_p,
5674 /*optional_p=*/false);
5675 /* Restore the SAVED_SCOPE for our caller. */
5676 parser->scope = saved_scope;
5677 parser->object_scope = saved_object_scope;
5678 parser->qualifying_scope = saved_qualifying_scope;
5679
5680 return unqualified_id;
5681 }
5682 /* Otherwise, if we are in global scope, then we are looking at one
5683 of the other qualified-id productions. */
5684 else if (global_scope_p)
5685 {
5686 cp_token *token;
5687 tree id;
5688
5689 /* Peek at the next token. */
5690 token = cp_lexer_peek_token (parser->lexer);
5691
5692 /* If it's an identifier, and the next token is not a "<", then
5693 we can avoid the template-id case. This is an optimization
5694 for this common case. */
5695 if (token->type == CPP_NAME
5696 && !cp_parser_nth_token_starts_template_argument_list_p
5697 (parser, 2))
5698 return cp_parser_identifier (parser);
5699
5700 cp_parser_parse_tentatively (parser);
5701 /* Try a template-id. */
5702 id = cp_parser_template_id (parser,
5703 /*template_keyword_p=*/false,
5704 /*check_dependency_p=*/true,
5705 none_type,
5706 declarator_p);
5707 /* If that worked, we're done. */
5708 if (cp_parser_parse_definitely (parser))
5709 return id;
5710
5711 /* Peek at the next token. (Changes in the token buffer may
5712 have invalidated the pointer obtained above.) */
5713 token = cp_lexer_peek_token (parser->lexer);
5714
5715 switch (token->type)
5716 {
5717 case CPP_NAME:
5718 return cp_parser_identifier (parser);
5719
5720 case CPP_KEYWORD:
5721 if (token->keyword == RID_OPERATOR)
5722 return cp_parser_operator_function_id (parser);
5723 /* Fall through. */
5724
5725 default:
5726 cp_parser_error (parser, "expected id-expression");
5727 return error_mark_node;
5728 }
5729 }
5730 else
5731 return cp_parser_unqualified_id (parser, template_keyword_p,
5732 /*check_dependency_p=*/true,
5733 declarator_p,
5734 optional_p);
5735 }
5736
5737 /* Parse an unqualified-id.
5738
5739 unqualified-id:
5740 identifier
5741 operator-function-id
5742 conversion-function-id
5743 ~ class-name
5744 template-id
5745
5746 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5747 keyword, in a construct like `A::template ...'.
5748
5749 Returns a representation of unqualified-id. For the `identifier'
5750 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5751 production a BIT_NOT_EXPR is returned; the operand of the
5752 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5753 other productions, see the documentation accompanying the
5754 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5755 names are looked up in uninstantiated templates. If DECLARATOR_P
5756 is true, the unqualified-id is appearing as part of a declarator,
5757 rather than as part of an expression. */
5758
5759 static cp_expr
5760 cp_parser_unqualified_id (cp_parser* parser,
5761 bool template_keyword_p,
5762 bool check_dependency_p,
5763 bool declarator_p,
5764 bool optional_p)
5765 {
5766 cp_token *token;
5767
5768 /* Peek at the next token. */
5769 token = cp_lexer_peek_token (parser->lexer);
5770
5771 switch ((int) token->type)
5772 {
5773 case CPP_NAME:
5774 {
5775 tree id;
5776
5777 /* We don't know yet whether or not this will be a
5778 template-id. */
5779 cp_parser_parse_tentatively (parser);
5780 /* Try a template-id. */
5781 id = cp_parser_template_id (parser, template_keyword_p,
5782 check_dependency_p,
5783 none_type,
5784 declarator_p);
5785 /* If it worked, we're done. */
5786 if (cp_parser_parse_definitely (parser))
5787 return id;
5788 /* Otherwise, it's an ordinary identifier. */
5789 return cp_parser_identifier (parser);
5790 }
5791
5792 case CPP_TEMPLATE_ID:
5793 return cp_parser_template_id (parser, template_keyword_p,
5794 check_dependency_p,
5795 none_type,
5796 declarator_p);
5797
5798 case CPP_COMPL:
5799 {
5800 tree type_decl;
5801 tree qualifying_scope;
5802 tree object_scope;
5803 tree scope;
5804 bool done;
5805
5806 /* Consume the `~' token. */
5807 cp_lexer_consume_token (parser->lexer);
5808 /* Parse the class-name. The standard, as written, seems to
5809 say that:
5810
5811 template <typename T> struct S { ~S (); };
5812 template <typename T> S<T>::~S() {}
5813
5814 is invalid, since `~' must be followed by a class-name, but
5815 `S<T>' is dependent, and so not known to be a class.
5816 That's not right; we need to look in uninstantiated
5817 templates. A further complication arises from:
5818
5819 template <typename T> void f(T t) {
5820 t.T::~T();
5821 }
5822
5823 Here, it is not possible to look up `T' in the scope of `T'
5824 itself. We must look in both the current scope, and the
5825 scope of the containing complete expression.
5826
5827 Yet another issue is:
5828
5829 struct S {
5830 int S;
5831 ~S();
5832 };
5833
5834 S::~S() {}
5835
5836 The standard does not seem to say that the `S' in `~S'
5837 should refer to the type `S' and not the data member
5838 `S::S'. */
5839
5840 /* DR 244 says that we look up the name after the "~" in the
5841 same scope as we looked up the qualifying name. That idea
5842 isn't fully worked out; it's more complicated than that. */
5843 scope = parser->scope;
5844 object_scope = parser->object_scope;
5845 qualifying_scope = parser->qualifying_scope;
5846
5847 /* Check for invalid scopes. */
5848 if (scope == error_mark_node)
5849 {
5850 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5851 cp_lexer_consume_token (parser->lexer);
5852 return error_mark_node;
5853 }
5854 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5855 {
5856 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5857 error_at (token->location,
5858 "scope %qT before %<~%> is not a class-name",
5859 scope);
5860 cp_parser_simulate_error (parser);
5861 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5862 cp_lexer_consume_token (parser->lexer);
5863 return error_mark_node;
5864 }
5865 gcc_assert (!scope || TYPE_P (scope));
5866
5867 /* If the name is of the form "X::~X" it's OK even if X is a
5868 typedef. */
5869 token = cp_lexer_peek_token (parser->lexer);
5870 if (scope
5871 && token->type == CPP_NAME
5872 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5873 != CPP_LESS)
5874 && (token->u.value == TYPE_IDENTIFIER (scope)
5875 || (CLASS_TYPE_P (scope)
5876 && constructor_name_p (token->u.value, scope))))
5877 {
5878 cp_lexer_consume_token (parser->lexer);
5879 return build_nt (BIT_NOT_EXPR, scope);
5880 }
5881
5882 /* ~auto means the destructor of whatever the object is. */
5883 if (cp_parser_is_keyword (token, RID_AUTO))
5884 {
5885 if (cxx_dialect < cxx14)
5886 pedwarn (input_location, 0,
5887 "%<~auto%> only available with "
5888 "-std=c++14 or -std=gnu++14");
5889 cp_lexer_consume_token (parser->lexer);
5890 return build_nt (BIT_NOT_EXPR, make_auto ());
5891 }
5892
5893 /* If there was an explicit qualification (S::~T), first look
5894 in the scope given by the qualification (i.e., S).
5895
5896 Note: in the calls to cp_parser_class_name below we pass
5897 typename_type so that lookup finds the injected-class-name
5898 rather than the constructor. */
5899 done = false;
5900 type_decl = NULL_TREE;
5901 if (scope)
5902 {
5903 cp_parser_parse_tentatively (parser);
5904 type_decl = cp_parser_class_name (parser,
5905 /*typename_keyword_p=*/false,
5906 /*template_keyword_p=*/false,
5907 typename_type,
5908 /*check_dependency=*/false,
5909 /*class_head_p=*/false,
5910 declarator_p);
5911 if (cp_parser_parse_definitely (parser))
5912 done = true;
5913 }
5914 /* In "N::S::~S", look in "N" as well. */
5915 if (!done && scope && qualifying_scope)
5916 {
5917 cp_parser_parse_tentatively (parser);
5918 parser->scope = qualifying_scope;
5919 parser->object_scope = NULL_TREE;
5920 parser->qualifying_scope = NULL_TREE;
5921 type_decl
5922 = cp_parser_class_name (parser,
5923 /*typename_keyword_p=*/false,
5924 /*template_keyword_p=*/false,
5925 typename_type,
5926 /*check_dependency=*/false,
5927 /*class_head_p=*/false,
5928 declarator_p);
5929 if (cp_parser_parse_definitely (parser))
5930 done = true;
5931 }
5932 /* In "p->S::~T", look in the scope given by "*p" as well. */
5933 else if (!done && object_scope)
5934 {
5935 cp_parser_parse_tentatively (parser);
5936 parser->scope = object_scope;
5937 parser->object_scope = NULL_TREE;
5938 parser->qualifying_scope = NULL_TREE;
5939 type_decl
5940 = cp_parser_class_name (parser,
5941 /*typename_keyword_p=*/false,
5942 /*template_keyword_p=*/false,
5943 typename_type,
5944 /*check_dependency=*/false,
5945 /*class_head_p=*/false,
5946 declarator_p);
5947 if (cp_parser_parse_definitely (parser))
5948 done = true;
5949 }
5950 /* Look in the surrounding context. */
5951 if (!done)
5952 {
5953 parser->scope = NULL_TREE;
5954 parser->object_scope = NULL_TREE;
5955 parser->qualifying_scope = NULL_TREE;
5956 if (processing_template_decl)
5957 cp_parser_parse_tentatively (parser);
5958 type_decl
5959 = cp_parser_class_name (parser,
5960 /*typename_keyword_p=*/false,
5961 /*template_keyword_p=*/false,
5962 typename_type,
5963 /*check_dependency=*/false,
5964 /*class_head_p=*/false,
5965 declarator_p);
5966 if (processing_template_decl
5967 && ! cp_parser_parse_definitely (parser))
5968 {
5969 /* We couldn't find a type with this name. If we're parsing
5970 tentatively, fail and try something else. */
5971 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5972 {
5973 cp_parser_simulate_error (parser);
5974 return error_mark_node;
5975 }
5976 /* Otherwise, accept it and check for a match at instantiation
5977 time. */
5978 type_decl = cp_parser_identifier (parser);
5979 if (type_decl != error_mark_node)
5980 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5981 return type_decl;
5982 }
5983 }
5984 /* If an error occurred, assume that the name of the
5985 destructor is the same as the name of the qualifying
5986 class. That allows us to keep parsing after running
5987 into ill-formed destructor names. */
5988 if (type_decl == error_mark_node && scope)
5989 return build_nt (BIT_NOT_EXPR, scope);
5990 else if (type_decl == error_mark_node)
5991 return error_mark_node;
5992
5993 /* Check that destructor name and scope match. */
5994 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5995 {
5996 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5997 error_at (token->location,
5998 "declaration of %<~%T%> as member of %qT",
5999 type_decl, scope);
6000 cp_parser_simulate_error (parser);
6001 return error_mark_node;
6002 }
6003
6004 /* [class.dtor]
6005
6006 A typedef-name that names a class shall not be used as the
6007 identifier in the declarator for a destructor declaration. */
6008 if (declarator_p
6009 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6010 && !DECL_SELF_REFERENCE_P (type_decl)
6011 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6012 error_at (token->location,
6013 "typedef-name %qD used as destructor declarator",
6014 type_decl);
6015
6016 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6017 }
6018
6019 case CPP_KEYWORD:
6020 if (token->keyword == RID_OPERATOR)
6021 {
6022 cp_expr id;
6023
6024 /* This could be a template-id, so we try that first. */
6025 cp_parser_parse_tentatively (parser);
6026 /* Try a template-id. */
6027 id = cp_parser_template_id (parser, template_keyword_p,
6028 /*check_dependency_p=*/true,
6029 none_type,
6030 declarator_p);
6031 /* If that worked, we're done. */
6032 if (cp_parser_parse_definitely (parser))
6033 return id;
6034 /* We still don't know whether we're looking at an
6035 operator-function-id or a conversion-function-id. */
6036 cp_parser_parse_tentatively (parser);
6037 /* Try an operator-function-id. */
6038 id = cp_parser_operator_function_id (parser);
6039 /* If that didn't work, try a conversion-function-id. */
6040 if (!cp_parser_parse_definitely (parser))
6041 id = cp_parser_conversion_function_id (parser);
6042 else if (UDLIT_OPER_P (id))
6043 {
6044 /* 17.6.3.3.5 */
6045 const char *name = UDLIT_OP_SUFFIX (id);
6046 if (name[0] != '_' && !in_system_header_at (input_location)
6047 && declarator_p)
6048 warning (OPT_Wliteral_suffix,
6049 "literal operator suffixes not preceded by %<_%>"
6050 " are reserved for future standardization");
6051 }
6052
6053 return id;
6054 }
6055 /* Fall through. */
6056
6057 default:
6058 if (optional_p)
6059 return NULL_TREE;
6060 cp_parser_error (parser, "expected unqualified-id");
6061 return error_mark_node;
6062 }
6063 }
6064
6065 /* Parse an (optional) nested-name-specifier.
6066
6067 nested-name-specifier: [C++98]
6068 class-or-namespace-name :: nested-name-specifier [opt]
6069 class-or-namespace-name :: template nested-name-specifier [opt]
6070
6071 nested-name-specifier: [C++0x]
6072 type-name ::
6073 namespace-name ::
6074 nested-name-specifier identifier ::
6075 nested-name-specifier template [opt] simple-template-id ::
6076
6077 PARSER->SCOPE should be set appropriately before this function is
6078 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6079 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6080 in name lookups.
6081
6082 Sets PARSER->SCOPE to the class (TYPE) or namespace
6083 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6084 it unchanged if there is no nested-name-specifier. Returns the new
6085 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6086
6087 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6088 part of a declaration and/or decl-specifier. */
6089
6090 static tree
6091 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6092 bool typename_keyword_p,
6093 bool check_dependency_p,
6094 bool type_p,
6095 bool is_declaration,
6096 bool template_keyword_p /* = false */)
6097 {
6098 bool success = false;
6099 cp_token_position start = 0;
6100 cp_token *token;
6101
6102 /* Remember where the nested-name-specifier starts. */
6103 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6104 {
6105 start = cp_lexer_token_position (parser->lexer, false);
6106 push_deferring_access_checks (dk_deferred);
6107 }
6108
6109 while (true)
6110 {
6111 tree new_scope;
6112 tree old_scope;
6113 tree saved_qualifying_scope;
6114
6115 /* Spot cases that cannot be the beginning of a
6116 nested-name-specifier. */
6117 token = cp_lexer_peek_token (parser->lexer);
6118
6119 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6120 the already parsed nested-name-specifier. */
6121 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6122 {
6123 /* Grab the nested-name-specifier and continue the loop. */
6124 cp_parser_pre_parsed_nested_name_specifier (parser);
6125 /* If we originally encountered this nested-name-specifier
6126 with IS_DECLARATION set to false, we will not have
6127 resolved TYPENAME_TYPEs, so we must do so here. */
6128 if (is_declaration
6129 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6130 {
6131 new_scope = resolve_typename_type (parser->scope,
6132 /*only_current_p=*/false);
6133 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6134 parser->scope = new_scope;
6135 }
6136 success = true;
6137 continue;
6138 }
6139
6140 /* Spot cases that cannot be the beginning of a
6141 nested-name-specifier. On the second and subsequent times
6142 through the loop, we look for the `template' keyword. */
6143 if (success && token->keyword == RID_TEMPLATE)
6144 ;
6145 /* A template-id can start a nested-name-specifier. */
6146 else if (token->type == CPP_TEMPLATE_ID)
6147 ;
6148 /* DR 743: decltype can be used in a nested-name-specifier. */
6149 else if (token_is_decltype (token))
6150 ;
6151 else
6152 {
6153 /* If the next token is not an identifier, then it is
6154 definitely not a type-name or namespace-name. */
6155 if (token->type != CPP_NAME)
6156 break;
6157 /* If the following token is neither a `<' (to begin a
6158 template-id), nor a `::', then we are not looking at a
6159 nested-name-specifier. */
6160 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6161
6162 if (token->type == CPP_COLON
6163 && parser->colon_corrects_to_scope_p
6164 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6165 {
6166 gcc_rich_location richloc (token->location);
6167 richloc.add_fixit_replace ("::");
6168 error_at (&richloc,
6169 "found %<:%> in nested-name-specifier, "
6170 "expected %<::%>");
6171 token->type = CPP_SCOPE;
6172 }
6173
6174 if (token->type != CPP_SCOPE
6175 && !cp_parser_nth_token_starts_template_argument_list_p
6176 (parser, 2))
6177 break;
6178 }
6179
6180 /* The nested-name-specifier is optional, so we parse
6181 tentatively. */
6182 cp_parser_parse_tentatively (parser);
6183
6184 /* Look for the optional `template' keyword, if this isn't the
6185 first time through the loop. */
6186 if (success)
6187 template_keyword_p = cp_parser_optional_template_keyword (parser);
6188
6189 /* Save the old scope since the name lookup we are about to do
6190 might destroy it. */
6191 old_scope = parser->scope;
6192 saved_qualifying_scope = parser->qualifying_scope;
6193 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6194 look up names in "X<T>::I" in order to determine that "Y" is
6195 a template. So, if we have a typename at this point, we make
6196 an effort to look through it. */
6197 if (is_declaration
6198 && !typename_keyword_p
6199 && parser->scope
6200 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6201 parser->scope = resolve_typename_type (parser->scope,
6202 /*only_current_p=*/false);
6203 /* Parse the qualifying entity. */
6204 new_scope
6205 = cp_parser_qualifying_entity (parser,
6206 typename_keyword_p,
6207 template_keyword_p,
6208 check_dependency_p,
6209 type_p,
6210 is_declaration);
6211 /* Look for the `::' token. */
6212 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6213
6214 /* If we found what we wanted, we keep going; otherwise, we're
6215 done. */
6216 if (!cp_parser_parse_definitely (parser))
6217 {
6218 bool error_p = false;
6219
6220 /* Restore the OLD_SCOPE since it was valid before the
6221 failed attempt at finding the last
6222 class-or-namespace-name. */
6223 parser->scope = old_scope;
6224 parser->qualifying_scope = saved_qualifying_scope;
6225
6226 /* If the next token is a decltype, and the one after that is a
6227 `::', then the decltype has failed to resolve to a class or
6228 enumeration type. Give this error even when parsing
6229 tentatively since it can't possibly be valid--and we're going
6230 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6231 won't get another chance.*/
6232 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6233 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6234 == CPP_SCOPE))
6235 {
6236 token = cp_lexer_consume_token (parser->lexer);
6237 error_at (token->location, "decltype evaluates to %qT, "
6238 "which is not a class or enumeration type",
6239 token->u.tree_check_value->value);
6240 parser->scope = error_mark_node;
6241 error_p = true;
6242 /* As below. */
6243 success = true;
6244 cp_lexer_consume_token (parser->lexer);
6245 }
6246
6247 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6248 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6249 {
6250 /* If we have a non-type template-id followed by ::, it can't
6251 possibly be valid. */
6252 token = cp_lexer_peek_token (parser->lexer);
6253 tree tid = token->u.tree_check_value->value;
6254 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6255 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6256 {
6257 tree tmpl = NULL_TREE;
6258 if (is_overloaded_fn (tid))
6259 {
6260 tree fns = get_fns (tid);
6261 if (OVL_SINGLE_P (fns))
6262 tmpl = OVL_FIRST (fns);
6263 error_at (token->location, "function template-id %qD "
6264 "in nested-name-specifier", tid);
6265 }
6266 else
6267 {
6268 /* Variable template. */
6269 tmpl = TREE_OPERAND (tid, 0);
6270 gcc_assert (variable_template_p (tmpl));
6271 error_at (token->location, "variable template-id %qD "
6272 "in nested-name-specifier", tid);
6273 }
6274 if (tmpl)
6275 inform (DECL_SOURCE_LOCATION (tmpl),
6276 "%qD declared here", tmpl);
6277
6278 parser->scope = error_mark_node;
6279 error_p = true;
6280 /* As below. */
6281 success = true;
6282 cp_lexer_consume_token (parser->lexer);
6283 cp_lexer_consume_token (parser->lexer);
6284 }
6285 }
6286
6287 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6288 break;
6289 /* If the next token is an identifier, and the one after
6290 that is a `::', then any valid interpretation would have
6291 found a class-or-namespace-name. */
6292 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6293 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6294 == CPP_SCOPE)
6295 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6296 != CPP_COMPL))
6297 {
6298 token = cp_lexer_consume_token (parser->lexer);
6299 if (!error_p)
6300 {
6301 if (!token->error_reported)
6302 {
6303 tree decl;
6304 tree ambiguous_decls;
6305
6306 decl = cp_parser_lookup_name (parser, token->u.value,
6307 none_type,
6308 /*is_template=*/false,
6309 /*is_namespace=*/false,
6310 /*check_dependency=*/true,
6311 &ambiguous_decls,
6312 token->location);
6313 if (TREE_CODE (decl) == TEMPLATE_DECL)
6314 error_at (token->location,
6315 "%qD used without template parameters",
6316 decl);
6317 else if (ambiguous_decls)
6318 {
6319 // cp_parser_lookup_name has the same diagnostic,
6320 // thus make sure to emit it at most once.
6321 if (cp_parser_uncommitted_to_tentative_parse_p
6322 (parser))
6323 {
6324 error_at (token->location,
6325 "reference to %qD is ambiguous",
6326 token->u.value);
6327 print_candidates (ambiguous_decls);
6328 }
6329 decl = error_mark_node;
6330 }
6331 else
6332 {
6333 if (cxx_dialect != cxx98)
6334 cp_parser_name_lookup_error
6335 (parser, token->u.value, decl, NLE_NOT_CXX98,
6336 token->location);
6337 else
6338 cp_parser_name_lookup_error
6339 (parser, token->u.value, decl, NLE_CXX98,
6340 token->location);
6341 }
6342 }
6343 parser->scope = error_mark_node;
6344 error_p = true;
6345 /* Treat this as a successful nested-name-specifier
6346 due to:
6347
6348 [basic.lookup.qual]
6349
6350 If the name found is not a class-name (clause
6351 _class_) or namespace-name (_namespace.def_), the
6352 program is ill-formed. */
6353 success = true;
6354 }
6355 cp_lexer_consume_token (parser->lexer);
6356 }
6357 break;
6358 }
6359 /* We've found one valid nested-name-specifier. */
6360 success = true;
6361 /* Name lookup always gives us a DECL. */
6362 if (TREE_CODE (new_scope) == TYPE_DECL)
6363 new_scope = TREE_TYPE (new_scope);
6364 /* Uses of "template" must be followed by actual templates. */
6365 if (template_keyword_p
6366 && !(CLASS_TYPE_P (new_scope)
6367 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6368 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6369 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6370 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6371 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6372 == TEMPLATE_ID_EXPR)))
6373 permerror (input_location, TYPE_P (new_scope)
6374 ? G_("%qT is not a template")
6375 : G_("%qD is not a template"),
6376 new_scope);
6377 /* If it is a class scope, try to complete it; we are about to
6378 be looking up names inside the class. */
6379 if (TYPE_P (new_scope)
6380 /* Since checking types for dependency can be expensive,
6381 avoid doing it if the type is already complete. */
6382 && !COMPLETE_TYPE_P (new_scope)
6383 /* Do not try to complete dependent types. */
6384 && !dependent_type_p (new_scope))
6385 {
6386 new_scope = complete_type (new_scope);
6387 /* If it is a typedef to current class, use the current
6388 class instead, as the typedef won't have any names inside
6389 it yet. */
6390 if (!COMPLETE_TYPE_P (new_scope)
6391 && currently_open_class (new_scope))
6392 new_scope = TYPE_MAIN_VARIANT (new_scope);
6393 }
6394 /* Make sure we look in the right scope the next time through
6395 the loop. */
6396 parser->scope = new_scope;
6397 }
6398
6399 /* If parsing tentatively, replace the sequence of tokens that makes
6400 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6401 token. That way, should we re-parse the token stream, we will
6402 not have to repeat the effort required to do the parse, nor will
6403 we issue duplicate error messages. */
6404 if (success && start)
6405 {
6406 cp_token *token;
6407
6408 token = cp_lexer_token_at (parser->lexer, start);
6409 /* Reset the contents of the START token. */
6410 token->type = CPP_NESTED_NAME_SPECIFIER;
6411 /* Retrieve any deferred checks. Do not pop this access checks yet
6412 so the memory will not be reclaimed during token replacing below. */
6413 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6414 token->u.tree_check_value->value = parser->scope;
6415 token->u.tree_check_value->checks = get_deferred_access_checks ();
6416 token->u.tree_check_value->qualifying_scope =
6417 parser->qualifying_scope;
6418 token->keyword = RID_MAX;
6419
6420 /* Purge all subsequent tokens. */
6421 cp_lexer_purge_tokens_after (parser->lexer, start);
6422 }
6423
6424 if (start)
6425 pop_to_parent_deferring_access_checks ();
6426
6427 return success ? parser->scope : NULL_TREE;
6428 }
6429
6430 /* Parse a nested-name-specifier. See
6431 cp_parser_nested_name_specifier_opt for details. This function
6432 behaves identically, except that it will an issue an error if no
6433 nested-name-specifier is present. */
6434
6435 static tree
6436 cp_parser_nested_name_specifier (cp_parser *parser,
6437 bool typename_keyword_p,
6438 bool check_dependency_p,
6439 bool type_p,
6440 bool is_declaration)
6441 {
6442 tree scope;
6443
6444 /* Look for the nested-name-specifier. */
6445 scope = cp_parser_nested_name_specifier_opt (parser,
6446 typename_keyword_p,
6447 check_dependency_p,
6448 type_p,
6449 is_declaration);
6450 /* If it was not present, issue an error message. */
6451 if (!scope)
6452 {
6453 cp_parser_error (parser, "expected nested-name-specifier");
6454 parser->scope = NULL_TREE;
6455 }
6456
6457 return scope;
6458 }
6459
6460 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6461 this is either a class-name or a namespace-name (which corresponds
6462 to the class-or-namespace-name production in the grammar). For
6463 C++0x, it can also be a type-name that refers to an enumeration
6464 type or a simple-template-id.
6465
6466 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6467 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6468 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6469 TYPE_P is TRUE iff the next name should be taken as a class-name,
6470 even the same name is declared to be another entity in the same
6471 scope.
6472
6473 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6474 specified by the class-or-namespace-name. If neither is found the
6475 ERROR_MARK_NODE is returned. */
6476
6477 static tree
6478 cp_parser_qualifying_entity (cp_parser *parser,
6479 bool typename_keyword_p,
6480 bool template_keyword_p,
6481 bool check_dependency_p,
6482 bool type_p,
6483 bool is_declaration)
6484 {
6485 tree saved_scope;
6486 tree saved_qualifying_scope;
6487 tree saved_object_scope;
6488 tree scope;
6489 bool only_class_p;
6490 bool successful_parse_p;
6491
6492 /* DR 743: decltype can appear in a nested-name-specifier. */
6493 if (cp_lexer_next_token_is_decltype (parser->lexer))
6494 {
6495 scope = cp_parser_decltype (parser);
6496 if (TREE_CODE (scope) != ENUMERAL_TYPE
6497 && !MAYBE_CLASS_TYPE_P (scope))
6498 {
6499 cp_parser_simulate_error (parser);
6500 return error_mark_node;
6501 }
6502 if (TYPE_NAME (scope))
6503 scope = TYPE_NAME (scope);
6504 return scope;
6505 }
6506
6507 /* Before we try to parse the class-name, we must save away the
6508 current PARSER->SCOPE since cp_parser_class_name will destroy
6509 it. */
6510 saved_scope = parser->scope;
6511 saved_qualifying_scope = parser->qualifying_scope;
6512 saved_object_scope = parser->object_scope;
6513 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6514 there is no need to look for a namespace-name. */
6515 only_class_p = template_keyword_p
6516 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6517 if (!only_class_p)
6518 cp_parser_parse_tentatively (parser);
6519 scope = cp_parser_class_name (parser,
6520 typename_keyword_p,
6521 template_keyword_p,
6522 type_p ? class_type : none_type,
6523 check_dependency_p,
6524 /*class_head_p=*/false,
6525 is_declaration,
6526 /*enum_ok=*/cxx_dialect > cxx98);
6527 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6528 /* If that didn't work, try for a namespace-name. */
6529 if (!only_class_p && !successful_parse_p)
6530 {
6531 /* Restore the saved scope. */
6532 parser->scope = saved_scope;
6533 parser->qualifying_scope = saved_qualifying_scope;
6534 parser->object_scope = saved_object_scope;
6535 /* If we are not looking at an identifier followed by the scope
6536 resolution operator, then this is not part of a
6537 nested-name-specifier. (Note that this function is only used
6538 to parse the components of a nested-name-specifier.) */
6539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6540 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6541 return error_mark_node;
6542 scope = cp_parser_namespace_name (parser);
6543 }
6544
6545 return scope;
6546 }
6547
6548 /* Return true if we are looking at a compound-literal, false otherwise. */
6549
6550 static bool
6551 cp_parser_compound_literal_p (cp_parser *parser)
6552 {
6553 cp_lexer_save_tokens (parser->lexer);
6554
6555 /* Skip tokens until the next token is a closing parenthesis.
6556 If we find the closing `)', and the next token is a `{', then
6557 we are looking at a compound-literal. */
6558 bool compound_literal_p
6559 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6560 /*consume_paren=*/true)
6561 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6562
6563 /* Roll back the tokens we skipped. */
6564 cp_lexer_rollback_tokens (parser->lexer);
6565
6566 return compound_literal_p;
6567 }
6568
6569 /* Parse a postfix-expression.
6570
6571 postfix-expression:
6572 primary-expression
6573 postfix-expression [ expression ]
6574 postfix-expression ( expression-list [opt] )
6575 simple-type-specifier ( expression-list [opt] )
6576 typename :: [opt] nested-name-specifier identifier
6577 ( expression-list [opt] )
6578 typename :: [opt] nested-name-specifier template [opt] template-id
6579 ( expression-list [opt] )
6580 postfix-expression . template [opt] id-expression
6581 postfix-expression -> template [opt] id-expression
6582 postfix-expression . pseudo-destructor-name
6583 postfix-expression -> pseudo-destructor-name
6584 postfix-expression ++
6585 postfix-expression --
6586 dynamic_cast < type-id > ( expression )
6587 static_cast < type-id > ( expression )
6588 reinterpret_cast < type-id > ( expression )
6589 const_cast < type-id > ( expression )
6590 typeid ( expression )
6591 typeid ( type-id )
6592
6593 GNU Extension:
6594
6595 postfix-expression:
6596 ( type-id ) { initializer-list , [opt] }
6597
6598 This extension is a GNU version of the C99 compound-literal
6599 construct. (The C99 grammar uses `type-name' instead of `type-id',
6600 but they are essentially the same concept.)
6601
6602 If ADDRESS_P is true, the postfix expression is the operand of the
6603 `&' operator. CAST_P is true if this expression is the target of a
6604 cast.
6605
6606 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6607 class member access expressions [expr.ref].
6608
6609 Returns a representation of the expression. */
6610
6611 static cp_expr
6612 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6613 bool member_access_only_p, bool decltype_p,
6614 cp_id_kind * pidk_return)
6615 {
6616 cp_token *token;
6617 location_t loc;
6618 enum rid keyword;
6619 cp_id_kind idk = CP_ID_KIND_NONE;
6620 cp_expr postfix_expression = NULL_TREE;
6621 bool is_member_access = false;
6622 int saved_in_statement = -1;
6623
6624 /* Peek at the next token. */
6625 token = cp_lexer_peek_token (parser->lexer);
6626 loc = token->location;
6627 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6628
6629 /* Some of the productions are determined by keywords. */
6630 keyword = token->keyword;
6631 switch (keyword)
6632 {
6633 case RID_DYNCAST:
6634 case RID_STATCAST:
6635 case RID_REINTCAST:
6636 case RID_CONSTCAST:
6637 {
6638 tree type;
6639 cp_expr expression;
6640 const char *saved_message;
6641 bool saved_in_type_id_in_expr_p;
6642
6643 /* All of these can be handled in the same way from the point
6644 of view of parsing. Begin by consuming the token
6645 identifying the cast. */
6646 cp_lexer_consume_token (parser->lexer);
6647
6648 /* New types cannot be defined in the cast. */
6649 saved_message = parser->type_definition_forbidden_message;
6650 parser->type_definition_forbidden_message
6651 = G_("types may not be defined in casts");
6652
6653 /* Look for the opening `<'. */
6654 cp_parser_require (parser, CPP_LESS, RT_LESS);
6655 /* Parse the type to which we are casting. */
6656 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6657 parser->in_type_id_in_expr_p = true;
6658 type = cp_parser_type_id (parser);
6659 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6660 /* Look for the closing `>'. */
6661 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6662 /* Restore the old message. */
6663 parser->type_definition_forbidden_message = saved_message;
6664
6665 bool saved_greater_than_is_operator_p
6666 = parser->greater_than_is_operator_p;
6667 parser->greater_than_is_operator_p = true;
6668
6669 /* And the expression which is being cast. */
6670 matching_parens parens;
6671 parens.require_open (parser);
6672 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6673 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6674 RT_CLOSE_PAREN);
6675 location_t end_loc = close_paren ?
6676 close_paren->location : UNKNOWN_LOCATION;
6677
6678 parser->greater_than_is_operator_p
6679 = saved_greater_than_is_operator_p;
6680
6681 /* Only type conversions to integral or enumeration types
6682 can be used in constant-expressions. */
6683 if (!cast_valid_in_integral_constant_expression_p (type)
6684 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6685 {
6686 postfix_expression = error_mark_node;
6687 break;
6688 }
6689
6690 switch (keyword)
6691 {
6692 case RID_DYNCAST:
6693 postfix_expression
6694 = build_dynamic_cast (type, expression, tf_warning_or_error);
6695 break;
6696 case RID_STATCAST:
6697 postfix_expression
6698 = build_static_cast (type, expression, tf_warning_or_error);
6699 break;
6700 case RID_REINTCAST:
6701 postfix_expression
6702 = build_reinterpret_cast (type, expression,
6703 tf_warning_or_error);
6704 break;
6705 case RID_CONSTCAST:
6706 postfix_expression
6707 = build_const_cast (type, expression, tf_warning_or_error);
6708 break;
6709 default:
6710 gcc_unreachable ();
6711 }
6712
6713 /* Construct a location e.g. :
6714 reinterpret_cast <int *> (expr)
6715 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6716 ranging from the start of the "*_cast" token to the final closing
6717 paren, with the caret at the start. */
6718 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6719 postfix_expression.set_location (cp_cast_loc);
6720 }
6721 break;
6722
6723 case RID_TYPEID:
6724 {
6725 tree type;
6726 const char *saved_message;
6727 bool saved_in_type_id_in_expr_p;
6728
6729 /* Consume the `typeid' token. */
6730 cp_lexer_consume_token (parser->lexer);
6731 /* Look for the `(' token. */
6732 matching_parens parens;
6733 parens.require_open (parser);
6734 /* Types cannot be defined in a `typeid' expression. */
6735 saved_message = parser->type_definition_forbidden_message;
6736 parser->type_definition_forbidden_message
6737 = G_("types may not be defined in a %<typeid%> expression");
6738 /* We can't be sure yet whether we're looking at a type-id or an
6739 expression. */
6740 cp_parser_parse_tentatively (parser);
6741 /* Try a type-id first. */
6742 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6743 parser->in_type_id_in_expr_p = true;
6744 type = cp_parser_type_id (parser);
6745 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6746 /* Look for the `)' token. Otherwise, we can't be sure that
6747 we're not looking at an expression: consider `typeid (int
6748 (3))', for example. */
6749 cp_token *close_paren = parens.require_close (parser);
6750 /* If all went well, simply lookup the type-id. */
6751 if (cp_parser_parse_definitely (parser))
6752 postfix_expression = get_typeid (type, tf_warning_or_error);
6753 /* Otherwise, fall back to the expression variant. */
6754 else
6755 {
6756 tree expression;
6757
6758 /* Look for an expression. */
6759 expression = cp_parser_expression (parser, & idk);
6760 /* Compute its typeid. */
6761 postfix_expression = build_typeid (expression, tf_warning_or_error);
6762 /* Look for the `)' token. */
6763 close_paren = parens.require_close (parser);
6764 }
6765 /* Restore the saved message. */
6766 parser->type_definition_forbidden_message = saved_message;
6767 /* `typeid' may not appear in an integral constant expression. */
6768 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6769 postfix_expression = error_mark_node;
6770
6771 /* Construct a location e.g. :
6772 typeid (expr)
6773 ^~~~~~~~~~~~~
6774 ranging from the start of the "typeid" token to the final closing
6775 paren, with the caret at the start. */
6776 if (close_paren)
6777 {
6778 location_t typeid_loc
6779 = make_location (start_loc, start_loc, close_paren->location);
6780 postfix_expression.set_location (typeid_loc);
6781 }
6782 }
6783 break;
6784
6785 case RID_TYPENAME:
6786 {
6787 tree type;
6788 /* The syntax permitted here is the same permitted for an
6789 elaborated-type-specifier. */
6790 ++parser->prevent_constrained_type_specifiers;
6791 type = cp_parser_elaborated_type_specifier (parser,
6792 /*is_friend=*/false,
6793 /*is_declaration=*/false);
6794 --parser->prevent_constrained_type_specifiers;
6795 postfix_expression = cp_parser_functional_cast (parser, type);
6796 }
6797 break;
6798
6799 case RID_CILK_SPAWN:
6800 {
6801 location_t cilk_spawn_loc
6802 = cp_lexer_peek_token (parser->lexer)->location;
6803 cp_lexer_consume_token (parser->lexer);
6804 token = cp_lexer_peek_token (parser->lexer);
6805 if (token->type == CPP_SEMICOLON)
6806 {
6807 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6808 "an expression");
6809 postfix_expression = error_mark_node;
6810 break;
6811 }
6812 else if (!current_function_decl)
6813 {
6814 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6815 "inside a function");
6816 postfix_expression = error_mark_node;
6817 break;
6818 }
6819 else
6820 {
6821 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6822 saved_in_statement = parser->in_statement;
6823 parser->in_statement |= IN_CILK_SPAWN;
6824 }
6825 cfun->calls_cilk_spawn = 1;
6826 postfix_expression =
6827 cp_parser_postfix_expression (parser, false, false,
6828 false, false, &idk);
6829 if (!flag_cilkplus)
6830 {
6831 error_at (token->location, "-fcilkplus must be enabled to use"
6832 " %<_Cilk_spawn%>");
6833 cfun->calls_cilk_spawn = 0;
6834 }
6835 else if (saved_in_statement & IN_CILK_SPAWN)
6836 {
6837 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6838 "are not permitted");
6839 postfix_expression = error_mark_node;
6840 cfun->calls_cilk_spawn = 0;
6841 }
6842 else
6843 {
6844 location_t loc = postfix_expression.get_location ();
6845 postfix_expression = build_cilk_spawn (token->location,
6846 postfix_expression);
6847 /* Build a location of the form:
6848 _Cilk_spawn expr
6849 ~~~~~~~~~~~~^~~~
6850 with caret at the expr, ranging from the start of the
6851 _Cilk_spawn token to the end of the expression. */
6852 location_t combined_loc =
6853 make_location (loc, cilk_spawn_loc, get_finish (loc));
6854 postfix_expression.set_location (combined_loc);
6855 if (postfix_expression != error_mark_node)
6856 SET_EXPR_LOCATION (postfix_expression, input_location);
6857 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6858 }
6859 break;
6860 }
6861
6862 case RID_ADDRESSOF:
6863 case RID_BUILTIN_SHUFFLE:
6864 case RID_BUILTIN_LAUNDER:
6865 {
6866 vec<tree, va_gc> *vec;
6867 unsigned int i;
6868 tree p;
6869
6870 cp_lexer_consume_token (parser->lexer);
6871 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6872 /*cast_p=*/false, /*allow_expansion_p=*/true,
6873 /*non_constant_p=*/NULL);
6874 if (vec == NULL)
6875 {
6876 postfix_expression = error_mark_node;
6877 break;
6878 }
6879
6880 FOR_EACH_VEC_ELT (*vec, i, p)
6881 mark_exp_read (p);
6882
6883 switch (keyword)
6884 {
6885 case RID_ADDRESSOF:
6886 if (vec->length () == 1)
6887 postfix_expression
6888 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6889 else
6890 {
6891 error_at (loc, "wrong number of arguments to "
6892 "%<__builtin_addressof%>");
6893 postfix_expression = error_mark_node;
6894 }
6895 break;
6896
6897 case RID_BUILTIN_LAUNDER:
6898 if (vec->length () == 1)
6899 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6900 tf_warning_or_error);
6901 else
6902 {
6903 error_at (loc, "wrong number of arguments to "
6904 "%<__builtin_launder%>");
6905 postfix_expression = error_mark_node;
6906 }
6907 break;
6908
6909 case RID_BUILTIN_SHUFFLE:
6910 if (vec->length () == 2)
6911 postfix_expression
6912 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6913 (*vec)[1], tf_warning_or_error);
6914 else if (vec->length () == 3)
6915 postfix_expression
6916 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6917 (*vec)[2], tf_warning_or_error);
6918 else
6919 {
6920 error_at (loc, "wrong number of arguments to "
6921 "%<__builtin_shuffle%>");
6922 postfix_expression = error_mark_node;
6923 }
6924 break;
6925
6926 default:
6927 gcc_unreachable ();
6928 }
6929 break;
6930 }
6931
6932 default:
6933 {
6934 tree type;
6935
6936 /* If the next thing is a simple-type-specifier, we may be
6937 looking at a functional cast. We could also be looking at
6938 an id-expression. So, we try the functional cast, and if
6939 that doesn't work we fall back to the primary-expression. */
6940 cp_parser_parse_tentatively (parser);
6941 /* Look for the simple-type-specifier. */
6942 ++parser->prevent_constrained_type_specifiers;
6943 type = cp_parser_simple_type_specifier (parser,
6944 /*decl_specs=*/NULL,
6945 CP_PARSER_FLAGS_NONE);
6946 --parser->prevent_constrained_type_specifiers;
6947 /* Parse the cast itself. */
6948 if (!cp_parser_error_occurred (parser))
6949 postfix_expression
6950 = cp_parser_functional_cast (parser, type);
6951 /* If that worked, we're done. */
6952 if (cp_parser_parse_definitely (parser))
6953 break;
6954
6955 /* If the functional-cast didn't work out, try a
6956 compound-literal. */
6957 if (cp_parser_allow_gnu_extensions_p (parser)
6958 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6959 {
6960 cp_expr initializer = NULL_TREE;
6961
6962 cp_parser_parse_tentatively (parser);
6963
6964 matching_parens parens;
6965 parens.consume_open (parser);
6966
6967 /* Avoid calling cp_parser_type_id pointlessly, see comment
6968 in cp_parser_cast_expression about c++/29234. */
6969 if (!cp_parser_compound_literal_p (parser))
6970 cp_parser_simulate_error (parser);
6971 else
6972 {
6973 /* Parse the type. */
6974 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6975 parser->in_type_id_in_expr_p = true;
6976 type = cp_parser_type_id (parser);
6977 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6978 parens.require_close (parser);
6979 }
6980
6981 /* If things aren't going well, there's no need to
6982 keep going. */
6983 if (!cp_parser_error_occurred (parser))
6984 {
6985 bool non_constant_p;
6986 /* Parse the brace-enclosed initializer list. */
6987 initializer = cp_parser_braced_list (parser,
6988 &non_constant_p);
6989 }
6990 /* If that worked, we're definitely looking at a
6991 compound-literal expression. */
6992 if (cp_parser_parse_definitely (parser))
6993 {
6994 /* Warn the user that a compound literal is not
6995 allowed in standard C++. */
6996 pedwarn (input_location, OPT_Wpedantic,
6997 "ISO C++ forbids compound-literals");
6998 /* For simplicity, we disallow compound literals in
6999 constant-expressions. We could
7000 allow compound literals of integer type, whose
7001 initializer was a constant, in constant
7002 expressions. Permitting that usage, as a further
7003 extension, would not change the meaning of any
7004 currently accepted programs. (Of course, as
7005 compound literals are not part of ISO C++, the
7006 standard has nothing to say.) */
7007 if (cp_parser_non_integral_constant_expression (parser,
7008 NIC_NCC))
7009 {
7010 postfix_expression = error_mark_node;
7011 break;
7012 }
7013 /* Form the representation of the compound-literal. */
7014 postfix_expression
7015 = finish_compound_literal (type, initializer,
7016 tf_warning_or_error, fcl_c99);
7017 postfix_expression.set_location (initializer.get_location ());
7018 break;
7019 }
7020 }
7021
7022 /* It must be a primary-expression. */
7023 postfix_expression
7024 = cp_parser_primary_expression (parser, address_p, cast_p,
7025 /*template_arg_p=*/false,
7026 decltype_p,
7027 &idk);
7028 }
7029 break;
7030 }
7031
7032 /* Note that we don't need to worry about calling build_cplus_new on a
7033 class-valued CALL_EXPR in decltype when it isn't the end of the
7034 postfix-expression; unary_complex_lvalue will take care of that for
7035 all these cases. */
7036
7037 /* Keep looping until the postfix-expression is complete. */
7038 while (true)
7039 {
7040 if (idk == CP_ID_KIND_UNQUALIFIED
7041 && identifier_p (postfix_expression)
7042 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7043 /* It is not a Koenig lookup function call. */
7044 postfix_expression
7045 = unqualified_name_lookup_error (postfix_expression);
7046
7047 /* Peek at the next token. */
7048 token = cp_lexer_peek_token (parser->lexer);
7049
7050 switch (token->type)
7051 {
7052 case CPP_OPEN_SQUARE:
7053 if (cp_next_tokens_can_be_std_attribute_p (parser))
7054 {
7055 cp_parser_error (parser,
7056 "two consecutive %<[%> shall "
7057 "only introduce an attribute");
7058 return error_mark_node;
7059 }
7060 postfix_expression
7061 = cp_parser_postfix_open_square_expression (parser,
7062 postfix_expression,
7063 false,
7064 decltype_p);
7065 postfix_expression.set_range (start_loc,
7066 postfix_expression.get_location ());
7067
7068 idk = CP_ID_KIND_NONE;
7069 is_member_access = false;
7070 break;
7071
7072 case CPP_OPEN_PAREN:
7073 /* postfix-expression ( expression-list [opt] ) */
7074 {
7075 bool koenig_p;
7076 bool is_builtin_constant_p;
7077 bool saved_integral_constant_expression_p = false;
7078 bool saved_non_integral_constant_expression_p = false;
7079 tsubst_flags_t complain = complain_flags (decltype_p);
7080 vec<tree, va_gc> *args;
7081 location_t close_paren_loc = UNKNOWN_LOCATION;
7082
7083 is_member_access = false;
7084
7085 is_builtin_constant_p
7086 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7087 if (is_builtin_constant_p)
7088 {
7089 /* The whole point of __builtin_constant_p is to allow
7090 non-constant expressions to appear as arguments. */
7091 saved_integral_constant_expression_p
7092 = parser->integral_constant_expression_p;
7093 saved_non_integral_constant_expression_p
7094 = parser->non_integral_constant_expression_p;
7095 parser->integral_constant_expression_p = false;
7096 }
7097 args = (cp_parser_parenthesized_expression_list
7098 (parser, non_attr,
7099 /*cast_p=*/false, /*allow_expansion_p=*/true,
7100 /*non_constant_p=*/NULL,
7101 /*close_paren_loc=*/&close_paren_loc));
7102 if (is_builtin_constant_p)
7103 {
7104 parser->integral_constant_expression_p
7105 = saved_integral_constant_expression_p;
7106 parser->non_integral_constant_expression_p
7107 = saved_non_integral_constant_expression_p;
7108 }
7109
7110 if (args == NULL)
7111 {
7112 postfix_expression = error_mark_node;
7113 break;
7114 }
7115
7116 /* Function calls are not permitted in
7117 constant-expressions. */
7118 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7119 && cp_parser_non_integral_constant_expression (parser,
7120 NIC_FUNC_CALL))
7121 {
7122 postfix_expression = error_mark_node;
7123 release_tree_vector (args);
7124 break;
7125 }
7126
7127 koenig_p = false;
7128 if (idk == CP_ID_KIND_UNQUALIFIED
7129 || idk == CP_ID_KIND_TEMPLATE_ID)
7130 {
7131 if (identifier_p (postfix_expression))
7132 {
7133 if (!args->is_empty ())
7134 {
7135 koenig_p = true;
7136 if (!any_type_dependent_arguments_p (args))
7137 postfix_expression
7138 = perform_koenig_lookup (postfix_expression, args,
7139 complain);
7140 }
7141 else
7142 postfix_expression
7143 = unqualified_fn_lookup_error (postfix_expression);
7144 }
7145 /* We do not perform argument-dependent lookup if
7146 normal lookup finds a non-function, in accordance
7147 with the expected resolution of DR 218. */
7148 else if (!args->is_empty ()
7149 && is_overloaded_fn (postfix_expression))
7150 {
7151 tree fn = get_first_fn (postfix_expression);
7152 fn = STRIP_TEMPLATE (fn);
7153
7154 /* Do not do argument dependent lookup if regular
7155 lookup finds a member function or a block-scope
7156 function declaration. [basic.lookup.argdep]/3 */
7157 if (!DECL_FUNCTION_MEMBER_P (fn)
7158 && !DECL_LOCAL_FUNCTION_P (fn))
7159 {
7160 koenig_p = true;
7161 if (!any_type_dependent_arguments_p (args))
7162 postfix_expression
7163 = perform_koenig_lookup (postfix_expression, args,
7164 complain);
7165 }
7166 }
7167 }
7168
7169 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7170 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7171 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7172 && vec_safe_length (args) == 3)
7173 {
7174 tree arg0 = (*args)[0];
7175 tree arg1 = (*args)[1];
7176 tree arg2 = (*args)[2];
7177 int literal_mask = ((!!integer_zerop (arg1) << 1)
7178 | (!!integer_zerop (arg2) << 2));
7179 if (TREE_CODE (arg2) == CONST_DECL)
7180 arg2 = DECL_INITIAL (arg2);
7181 warn_for_memset (input_location, arg0, arg2, literal_mask);
7182 }
7183
7184 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7185 {
7186 tree instance = TREE_OPERAND (postfix_expression, 0);
7187 tree fn = TREE_OPERAND (postfix_expression, 1);
7188
7189 if (processing_template_decl
7190 && (type_dependent_object_expression_p (instance)
7191 || (!BASELINK_P (fn)
7192 && TREE_CODE (fn) != FIELD_DECL)
7193 || type_dependent_expression_p (fn)
7194 || any_type_dependent_arguments_p (args)))
7195 {
7196 maybe_generic_this_capture (instance, fn);
7197 postfix_expression
7198 = build_min_nt_call_vec (postfix_expression, args);
7199 release_tree_vector (args);
7200 break;
7201 }
7202
7203 if (BASELINK_P (fn))
7204 {
7205 postfix_expression
7206 = (build_new_method_call
7207 (instance, fn, &args, NULL_TREE,
7208 (idk == CP_ID_KIND_QUALIFIED
7209 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7210 : LOOKUP_NORMAL),
7211 /*fn_p=*/NULL,
7212 complain));
7213 }
7214 else
7215 postfix_expression
7216 = finish_call_expr (postfix_expression, &args,
7217 /*disallow_virtual=*/false,
7218 /*koenig_p=*/false,
7219 complain);
7220 }
7221 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7222 || TREE_CODE (postfix_expression) == MEMBER_REF
7223 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7224 postfix_expression = (build_offset_ref_call_from_tree
7225 (postfix_expression, &args,
7226 complain));
7227 else if (idk == CP_ID_KIND_QUALIFIED)
7228 /* A call to a static class member, or a namespace-scope
7229 function. */
7230 postfix_expression
7231 = finish_call_expr (postfix_expression, &args,
7232 /*disallow_virtual=*/true,
7233 koenig_p,
7234 complain);
7235 else
7236 /* All other function calls. */
7237 postfix_expression
7238 = finish_call_expr (postfix_expression, &args,
7239 /*disallow_virtual=*/false,
7240 koenig_p,
7241 complain);
7242
7243 if (close_paren_loc != UNKNOWN_LOCATION)
7244 {
7245 location_t combined_loc = make_location (token->location,
7246 start_loc,
7247 close_paren_loc);
7248 postfix_expression.set_location (combined_loc);
7249 }
7250
7251 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7252 idk = CP_ID_KIND_NONE;
7253
7254 release_tree_vector (args);
7255 }
7256 break;
7257
7258 case CPP_DOT:
7259 case CPP_DEREF:
7260 /* postfix-expression . template [opt] id-expression
7261 postfix-expression . pseudo-destructor-name
7262 postfix-expression -> template [opt] id-expression
7263 postfix-expression -> pseudo-destructor-name */
7264
7265 /* Consume the `.' or `->' operator. */
7266 cp_lexer_consume_token (parser->lexer);
7267
7268 postfix_expression
7269 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7270 postfix_expression,
7271 false, &idk, loc);
7272
7273 is_member_access = true;
7274 break;
7275
7276 case CPP_PLUS_PLUS:
7277 /* postfix-expression ++ */
7278 /* Consume the `++' token. */
7279 cp_lexer_consume_token (parser->lexer);
7280 /* Generate a representation for the complete expression. */
7281 postfix_expression
7282 = finish_increment_expr (postfix_expression,
7283 POSTINCREMENT_EXPR);
7284 /* Increments may not appear in constant-expressions. */
7285 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7286 postfix_expression = error_mark_node;
7287 idk = CP_ID_KIND_NONE;
7288 is_member_access = false;
7289 break;
7290
7291 case CPP_MINUS_MINUS:
7292 /* postfix-expression -- */
7293 /* Consume the `--' token. */
7294 cp_lexer_consume_token (parser->lexer);
7295 /* Generate a representation for the complete expression. */
7296 postfix_expression
7297 = finish_increment_expr (postfix_expression,
7298 POSTDECREMENT_EXPR);
7299 /* Decrements may not appear in constant-expressions. */
7300 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7301 postfix_expression = error_mark_node;
7302 idk = CP_ID_KIND_NONE;
7303 is_member_access = false;
7304 break;
7305
7306 default:
7307 if (pidk_return != NULL)
7308 * pidk_return = idk;
7309 if (member_access_only_p)
7310 return is_member_access
7311 ? postfix_expression
7312 : cp_expr (error_mark_node);
7313 else
7314 return postfix_expression;
7315 }
7316 }
7317
7318 /* We should never get here. */
7319 gcc_unreachable ();
7320 return error_mark_node;
7321 }
7322
7323 /* This function parses Cilk Plus array notations. If a normal array expr. is
7324 parsed then the array index is passed back to the caller through *INIT_INDEX
7325 and the function returns a NULL_TREE. If array notation expr. is parsed,
7326 then *INIT_INDEX is ignored by the caller and the function returns
7327 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7328 error_mark_node. */
7329
7330 static tree
7331 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7332 tree array_value)
7333 {
7334 cp_token *token = NULL;
7335 tree length_index, stride = NULL_TREE, value_tree, array_type;
7336 if (!array_value || array_value == error_mark_node)
7337 {
7338 cp_parser_skip_to_end_of_statement (parser);
7339 return error_mark_node;
7340 }
7341
7342 array_type = TREE_TYPE (array_value);
7343
7344 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7345 parser->colon_corrects_to_scope_p = false;
7346 token = cp_lexer_peek_token (parser->lexer);
7347
7348 if (!token)
7349 {
7350 cp_parser_error (parser, "expected %<:%> or numeral");
7351 return error_mark_node;
7352 }
7353 else if (token->type == CPP_COLON)
7354 {
7355 /* Consume the ':'. */
7356 cp_lexer_consume_token (parser->lexer);
7357
7358 /* If we are here, then we have a case like this A[:]. */
7359 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7360 {
7361 cp_parser_error (parser, "expected %<]%>");
7362 cp_parser_skip_to_end_of_statement (parser);
7363 return error_mark_node;
7364 }
7365 *init_index = NULL_TREE;
7366 stride = NULL_TREE;
7367 length_index = NULL_TREE;
7368 }
7369 else
7370 {
7371 /* If we are here, then there are three valid possibilities:
7372 1. ARRAY [ EXP ]
7373 2. ARRAY [ EXP : EXP ]
7374 3. ARRAY [ EXP : EXP : EXP ] */
7375
7376 *init_index = cp_parser_expression (parser);
7377 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7378 {
7379 /* This indicates that we have a normal array expression. */
7380 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7381 return NULL_TREE;
7382 }
7383
7384 /* Consume the ':'. */
7385 cp_lexer_consume_token (parser->lexer);
7386 length_index = cp_parser_expression (parser);
7387 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7388 {
7389 cp_lexer_consume_token (parser->lexer);
7390 stride = cp_parser_expression (parser);
7391 }
7392 }
7393 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7394
7395 if (*init_index == error_mark_node || length_index == error_mark_node
7396 || stride == error_mark_node || array_type == error_mark_node)
7397 {
7398 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7399 cp_lexer_consume_token (parser->lexer);
7400 return error_mark_node;
7401 }
7402 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7403
7404 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7405 length_index, stride, array_type);
7406 return value_tree;
7407 }
7408
7409 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7410 by cp_parser_builtin_offsetof. We're looking for
7411
7412 postfix-expression [ expression ]
7413 postfix-expression [ braced-init-list ] (C++11)
7414
7415 FOR_OFFSETOF is set if we're being called in that context, which
7416 changes how we deal with integer constant expressions. */
7417
7418 static tree
7419 cp_parser_postfix_open_square_expression (cp_parser *parser,
7420 tree postfix_expression,
7421 bool for_offsetof,
7422 bool decltype_p)
7423 {
7424 tree index = NULL_TREE;
7425 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7426 bool saved_greater_than_is_operator_p;
7427
7428 /* Consume the `[' token. */
7429 cp_lexer_consume_token (parser->lexer);
7430
7431 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7432 parser->greater_than_is_operator_p = true;
7433
7434 /* Parse the index expression. */
7435 /* ??? For offsetof, there is a question of what to allow here. If
7436 offsetof is not being used in an integral constant expression context,
7437 then we *could* get the right answer by computing the value at runtime.
7438 If we are in an integral constant expression context, then we might
7439 could accept any constant expression; hard to say without analysis.
7440 Rather than open the barn door too wide right away, allow only integer
7441 constant expressions here. */
7442 if (for_offsetof)
7443 index = cp_parser_constant_expression (parser);
7444 else
7445 {
7446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7447 {
7448 bool expr_nonconst_p;
7449 cp_lexer_set_source_position (parser->lexer);
7450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7451 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7452 if (flag_cilkplus
7453 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7454 {
7455 error_at (cp_lexer_peek_token (parser->lexer)->location,
7456 "braced list index is not allowed with array "
7457 "notation");
7458 cp_parser_skip_to_end_of_statement (parser);
7459 return error_mark_node;
7460 }
7461 }
7462 else if (flag_cilkplus)
7463 {
7464 /* Here are have these two options:
7465 ARRAY[EXP : EXP] - Array notation expr with default
7466 stride of 1.
7467 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7468 stride. */
7469 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7470 postfix_expression);
7471 if (an_exp)
7472 return an_exp;
7473 }
7474 else
7475 index = cp_parser_expression (parser);
7476 }
7477
7478 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7479
7480 /* Look for the closing `]'. */
7481 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7482
7483 /* Build the ARRAY_REF. */
7484 postfix_expression = grok_array_decl (loc, postfix_expression,
7485 index, decltype_p);
7486
7487 /* When not doing offsetof, array references are not permitted in
7488 constant-expressions. */
7489 if (!for_offsetof
7490 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7491 postfix_expression = error_mark_node;
7492
7493 return postfix_expression;
7494 }
7495
7496 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7497 by cp_parser_builtin_offsetof. We're looking for
7498
7499 postfix-expression . template [opt] id-expression
7500 postfix-expression . pseudo-destructor-name
7501 postfix-expression -> template [opt] id-expression
7502 postfix-expression -> pseudo-destructor-name
7503
7504 FOR_OFFSETOF is set if we're being called in that context. That sorta
7505 limits what of the above we'll actually accept, but nevermind.
7506 TOKEN_TYPE is the "." or "->" token, which will already have been
7507 removed from the stream. */
7508
7509 static tree
7510 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7511 enum cpp_ttype token_type,
7512 cp_expr postfix_expression,
7513 bool for_offsetof, cp_id_kind *idk,
7514 location_t location)
7515 {
7516 tree name;
7517 bool dependent_p;
7518 bool pseudo_destructor_p;
7519 tree scope = NULL_TREE;
7520 location_t start_loc = postfix_expression.get_start ();
7521
7522 /* If this is a `->' operator, dereference the pointer. */
7523 if (token_type == CPP_DEREF)
7524 postfix_expression = build_x_arrow (location, postfix_expression,
7525 tf_warning_or_error);
7526 /* Check to see whether or not the expression is type-dependent and
7527 not the current instantiation. */
7528 dependent_p = type_dependent_object_expression_p (postfix_expression);
7529 /* The identifier following the `->' or `.' is not qualified. */
7530 parser->scope = NULL_TREE;
7531 parser->qualifying_scope = NULL_TREE;
7532 parser->object_scope = NULL_TREE;
7533 *idk = CP_ID_KIND_NONE;
7534
7535 /* Enter the scope corresponding to the type of the object
7536 given by the POSTFIX_EXPRESSION. */
7537 if (!dependent_p)
7538 {
7539 scope = TREE_TYPE (postfix_expression);
7540 /* According to the standard, no expression should ever have
7541 reference type. Unfortunately, we do not currently match
7542 the standard in this respect in that our internal representation
7543 of an expression may have reference type even when the standard
7544 says it does not. Therefore, we have to manually obtain the
7545 underlying type here. */
7546 scope = non_reference (scope);
7547 /* The type of the POSTFIX_EXPRESSION must be complete. */
7548 /* Unlike the object expression in other contexts, *this is not
7549 required to be of complete type for purposes of class member
7550 access (5.2.5) outside the member function body. */
7551 if (postfix_expression != current_class_ref
7552 && scope != error_mark_node
7553 && !(processing_template_decl
7554 && current_class_type
7555 && (same_type_ignoring_top_level_qualifiers_p
7556 (scope, current_class_type))))
7557 {
7558 scope = complete_type (scope);
7559 if (!COMPLETE_TYPE_P (scope)
7560 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7561 && EXPR_P (postfix_expression))
7562 {
7563 /* In a template, be permissive by treating an object expression
7564 of incomplete type as dependent (after a pedwarn). */
7565 diagnostic_t kind = (processing_template_decl
7566 && MAYBE_CLASS_TYPE_P (scope)
7567 ? DK_PEDWARN
7568 : DK_ERROR);
7569 cxx_incomplete_type_diagnostic
7570 (location_of (postfix_expression),
7571 postfix_expression, scope, kind);
7572 if (!MAYBE_CLASS_TYPE_P (scope))
7573 return error_mark_node;
7574 if (processing_template_decl)
7575 {
7576 dependent_p = true;
7577 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7578 }
7579 }
7580 }
7581
7582 if (!dependent_p)
7583 {
7584 /* Let the name lookup machinery know that we are processing a
7585 class member access expression. */
7586 parser->context->object_type = scope;
7587 /* If something went wrong, we want to be able to discern that case,
7588 as opposed to the case where there was no SCOPE due to the type
7589 of expression being dependent. */
7590 if (!scope)
7591 scope = error_mark_node;
7592 /* If the SCOPE was erroneous, make the various semantic analysis
7593 functions exit quickly -- and without issuing additional error
7594 messages. */
7595 if (scope == error_mark_node)
7596 postfix_expression = error_mark_node;
7597 }
7598 }
7599
7600 if (dependent_p)
7601 /* Tell cp_parser_lookup_name that there was an object, even though it's
7602 type-dependent. */
7603 parser->context->object_type = unknown_type_node;
7604
7605 /* Assume this expression is not a pseudo-destructor access. */
7606 pseudo_destructor_p = false;
7607
7608 /* If the SCOPE is a scalar type, then, if this is a valid program,
7609 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7610 is type dependent, it can be pseudo-destructor-name or something else.
7611 Try to parse it as pseudo-destructor-name first. */
7612 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7613 {
7614 tree s;
7615 tree type;
7616
7617 cp_parser_parse_tentatively (parser);
7618 /* Parse the pseudo-destructor-name. */
7619 s = NULL_TREE;
7620 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7621 &s, &type);
7622 if (dependent_p
7623 && (cp_parser_error_occurred (parser)
7624 || !SCALAR_TYPE_P (type)))
7625 cp_parser_abort_tentative_parse (parser);
7626 else if (cp_parser_parse_definitely (parser))
7627 {
7628 pseudo_destructor_p = true;
7629 postfix_expression
7630 = finish_pseudo_destructor_expr (postfix_expression,
7631 s, type, location);
7632 }
7633 }
7634
7635 if (!pseudo_destructor_p)
7636 {
7637 /* If the SCOPE is not a scalar type, we are looking at an
7638 ordinary class member access expression, rather than a
7639 pseudo-destructor-name. */
7640 bool template_p;
7641 cp_token *token = cp_lexer_peek_token (parser->lexer);
7642 /* Parse the id-expression. */
7643 name = (cp_parser_id_expression
7644 (parser,
7645 cp_parser_optional_template_keyword (parser),
7646 /*check_dependency_p=*/true,
7647 &template_p,
7648 /*declarator_p=*/false,
7649 /*optional_p=*/false));
7650 /* In general, build a SCOPE_REF if the member name is qualified.
7651 However, if the name was not dependent and has already been
7652 resolved; there is no need to build the SCOPE_REF. For example;
7653
7654 struct X { void f(); };
7655 template <typename T> void f(T* t) { t->X::f(); }
7656
7657 Even though "t" is dependent, "X::f" is not and has been resolved
7658 to a BASELINK; there is no need to include scope information. */
7659
7660 /* But we do need to remember that there was an explicit scope for
7661 virtual function calls. */
7662 if (parser->scope)
7663 *idk = CP_ID_KIND_QUALIFIED;
7664
7665 /* If the name is a template-id that names a type, we will get a
7666 TYPE_DECL here. That is invalid code. */
7667 if (TREE_CODE (name) == TYPE_DECL)
7668 {
7669 error_at (token->location, "invalid use of %qD", name);
7670 postfix_expression = error_mark_node;
7671 }
7672 else
7673 {
7674 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7675 {
7676 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7677 {
7678 error_at (token->location, "%<%D::%D%> is not a class member",
7679 parser->scope, name);
7680 postfix_expression = error_mark_node;
7681 }
7682 else
7683 name = build_qualified_name (/*type=*/NULL_TREE,
7684 parser->scope,
7685 name,
7686 template_p);
7687 parser->scope = NULL_TREE;
7688 parser->qualifying_scope = NULL_TREE;
7689 parser->object_scope = NULL_TREE;
7690 }
7691 if (parser->scope && name && BASELINK_P (name))
7692 adjust_result_of_qualified_name_lookup
7693 (name, parser->scope, scope);
7694 postfix_expression
7695 = finish_class_member_access_expr (postfix_expression, name,
7696 template_p,
7697 tf_warning_or_error);
7698 /* Build a location e.g.:
7699 ptr->access_expr
7700 ~~~^~~~~~~~~~~~~
7701 where the caret is at the deref token, ranging from
7702 the start of postfix_expression to the end of the access expr. */
7703 location_t end_loc
7704 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7705 location_t combined_loc
7706 = make_location (input_location, start_loc, end_loc);
7707 protected_set_expr_location (postfix_expression, combined_loc);
7708 }
7709 }
7710
7711 /* We no longer need to look up names in the scope of the object on
7712 the left-hand side of the `.' or `->' operator. */
7713 parser->context->object_type = NULL_TREE;
7714
7715 /* Outside of offsetof, these operators may not appear in
7716 constant-expressions. */
7717 if (!for_offsetof
7718 && (cp_parser_non_integral_constant_expression
7719 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7720 postfix_expression = error_mark_node;
7721
7722 return postfix_expression;
7723 }
7724
7725 /* Parse a parenthesized expression-list.
7726
7727 expression-list:
7728 assignment-expression
7729 expression-list, assignment-expression
7730
7731 attribute-list:
7732 expression-list
7733 identifier
7734 identifier, expression-list
7735
7736 CAST_P is true if this expression is the target of a cast.
7737
7738 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7739 argument pack.
7740
7741 Returns a vector of trees. Each element is a representation of an
7742 assignment-expression. NULL is returned if the ( and or ) are
7743 missing. An empty, but allocated, vector is returned on no
7744 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7745 if we are parsing an attribute list for an attribute that wants a
7746 plain identifier argument, normal_attr for an attribute that wants
7747 an expression, or non_attr if we aren't parsing an attribute list. If
7748 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7749 not all of the expressions in the list were constant.
7750 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7751 will be written to with the location of the closing parenthesis. If
7752 an error occurs, it may or may not be written to. */
7753
7754 static vec<tree, va_gc> *
7755 cp_parser_parenthesized_expression_list (cp_parser* parser,
7756 int is_attribute_list,
7757 bool cast_p,
7758 bool allow_expansion_p,
7759 bool *non_constant_p,
7760 location_t *close_paren_loc)
7761 {
7762 vec<tree, va_gc> *expression_list;
7763 bool fold_expr_p = is_attribute_list != non_attr;
7764 tree identifier = NULL_TREE;
7765 bool saved_greater_than_is_operator_p;
7766
7767 /* Assume all the expressions will be constant. */
7768 if (non_constant_p)
7769 *non_constant_p = false;
7770
7771 matching_parens parens;
7772 if (!parens.require_open (parser))
7773 return NULL;
7774
7775 expression_list = make_tree_vector ();
7776
7777 /* Within a parenthesized expression, a `>' token is always
7778 the greater-than operator. */
7779 saved_greater_than_is_operator_p
7780 = parser->greater_than_is_operator_p;
7781 parser->greater_than_is_operator_p = true;
7782
7783 /* Consume expressions until there are no more. */
7784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7785 while (true)
7786 {
7787 tree expr;
7788
7789 /* At the beginning of attribute lists, check to see if the
7790 next token is an identifier. */
7791 if (is_attribute_list == id_attr
7792 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7793 {
7794 cp_token *token;
7795
7796 /* Consume the identifier. */
7797 token = cp_lexer_consume_token (parser->lexer);
7798 /* Save the identifier. */
7799 identifier = token->u.value;
7800 }
7801 else
7802 {
7803 bool expr_non_constant_p;
7804
7805 /* Parse the next assignment-expression. */
7806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7807 {
7808 /* A braced-init-list. */
7809 cp_lexer_set_source_position (parser->lexer);
7810 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7811 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7812 if (non_constant_p && expr_non_constant_p)
7813 *non_constant_p = true;
7814 }
7815 else if (non_constant_p)
7816 {
7817 expr = (cp_parser_constant_expression
7818 (parser, /*allow_non_constant_p=*/true,
7819 &expr_non_constant_p));
7820 if (expr_non_constant_p)
7821 *non_constant_p = true;
7822 }
7823 else
7824 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7825 cast_p);
7826
7827 if (fold_expr_p)
7828 expr = instantiate_non_dependent_expr (expr);
7829
7830 /* If we have an ellipsis, then this is an expression
7831 expansion. */
7832 if (allow_expansion_p
7833 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7834 {
7835 /* Consume the `...'. */
7836 cp_lexer_consume_token (parser->lexer);
7837
7838 /* Build the argument pack. */
7839 expr = make_pack_expansion (expr);
7840 }
7841
7842 /* Add it to the list. We add error_mark_node
7843 expressions to the list, so that we can still tell if
7844 the correct form for a parenthesized expression-list
7845 is found. That gives better errors. */
7846 vec_safe_push (expression_list, expr);
7847
7848 if (expr == error_mark_node)
7849 goto skip_comma;
7850 }
7851
7852 /* After the first item, attribute lists look the same as
7853 expression lists. */
7854 is_attribute_list = non_attr;
7855
7856 get_comma:;
7857 /* If the next token isn't a `,', then we are done. */
7858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7859 break;
7860
7861 /* Otherwise, consume the `,' and keep going. */
7862 cp_lexer_consume_token (parser->lexer);
7863 }
7864
7865 if (close_paren_loc)
7866 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7867
7868 if (!parens.require_close (parser))
7869 {
7870 int ending;
7871
7872 skip_comma:;
7873 /* We try and resync to an unnested comma, as that will give the
7874 user better diagnostics. */
7875 ending = cp_parser_skip_to_closing_parenthesis (parser,
7876 /*recovering=*/true,
7877 /*or_comma=*/true,
7878 /*consume_paren=*/true);
7879 if (ending < 0)
7880 goto get_comma;
7881 if (!ending)
7882 {
7883 parser->greater_than_is_operator_p
7884 = saved_greater_than_is_operator_p;
7885 return NULL;
7886 }
7887 }
7888
7889 parser->greater_than_is_operator_p
7890 = saved_greater_than_is_operator_p;
7891
7892 if (identifier)
7893 vec_safe_insert (expression_list, 0, identifier);
7894
7895 return expression_list;
7896 }
7897
7898 /* Parse a pseudo-destructor-name.
7899
7900 pseudo-destructor-name:
7901 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7902 :: [opt] nested-name-specifier template template-id :: ~ type-name
7903 :: [opt] nested-name-specifier [opt] ~ type-name
7904
7905 If either of the first two productions is used, sets *SCOPE to the
7906 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7907 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7908 or ERROR_MARK_NODE if the parse fails. */
7909
7910 static void
7911 cp_parser_pseudo_destructor_name (cp_parser* parser,
7912 tree object,
7913 tree* scope,
7914 tree* type)
7915 {
7916 bool nested_name_specifier_p;
7917
7918 /* Handle ~auto. */
7919 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7920 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7921 && !type_dependent_expression_p (object))
7922 {
7923 if (cxx_dialect < cxx14)
7924 pedwarn (input_location, 0,
7925 "%<~auto%> only available with "
7926 "-std=c++14 or -std=gnu++14");
7927 cp_lexer_consume_token (parser->lexer);
7928 cp_lexer_consume_token (parser->lexer);
7929 *scope = NULL_TREE;
7930 *type = TREE_TYPE (object);
7931 return;
7932 }
7933
7934 /* Assume that things will not work out. */
7935 *type = error_mark_node;
7936
7937 /* Look for the optional `::' operator. */
7938 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7939 /* Look for the optional nested-name-specifier. */
7940 nested_name_specifier_p
7941 = (cp_parser_nested_name_specifier_opt (parser,
7942 /*typename_keyword_p=*/false,
7943 /*check_dependency_p=*/true,
7944 /*type_p=*/false,
7945 /*is_declaration=*/false)
7946 != NULL_TREE);
7947 /* Now, if we saw a nested-name-specifier, we might be doing the
7948 second production. */
7949 if (nested_name_specifier_p
7950 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7951 {
7952 /* Consume the `template' keyword. */
7953 cp_lexer_consume_token (parser->lexer);
7954 /* Parse the template-id. */
7955 cp_parser_template_id (parser,
7956 /*template_keyword_p=*/true,
7957 /*check_dependency_p=*/false,
7958 class_type,
7959 /*is_declaration=*/true);
7960 /* Look for the `::' token. */
7961 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7962 }
7963 /* If the next token is not a `~', then there might be some
7964 additional qualification. */
7965 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7966 {
7967 /* At this point, we're looking for "type-name :: ~". The type-name
7968 must not be a class-name, since this is a pseudo-destructor. So,
7969 it must be either an enum-name, or a typedef-name -- both of which
7970 are just identifiers. So, we peek ahead to check that the "::"
7971 and "~" tokens are present; if they are not, then we can avoid
7972 calling type_name. */
7973 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7974 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7975 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7976 {
7977 cp_parser_error (parser, "non-scalar type");
7978 return;
7979 }
7980
7981 /* Look for the type-name. */
7982 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7983 if (*scope == error_mark_node)
7984 return;
7985
7986 /* Look for the `::' token. */
7987 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7988 }
7989 else
7990 *scope = NULL_TREE;
7991
7992 /* Look for the `~'. */
7993 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7994
7995 /* Once we see the ~, this has to be a pseudo-destructor. */
7996 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7997 cp_parser_commit_to_topmost_tentative_parse (parser);
7998
7999 /* Look for the type-name again. We are not responsible for
8000 checking that it matches the first type-name. */
8001 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8002 }
8003
8004 /* Parse a unary-expression.
8005
8006 unary-expression:
8007 postfix-expression
8008 ++ cast-expression
8009 -- cast-expression
8010 unary-operator cast-expression
8011 sizeof unary-expression
8012 sizeof ( type-id )
8013 alignof ( type-id ) [C++0x]
8014 new-expression
8015 delete-expression
8016
8017 GNU Extensions:
8018
8019 unary-expression:
8020 __extension__ cast-expression
8021 __alignof__ unary-expression
8022 __alignof__ ( type-id )
8023 alignof unary-expression [C++0x]
8024 __real__ cast-expression
8025 __imag__ cast-expression
8026 && identifier
8027 sizeof ( type-id ) { initializer-list , [opt] }
8028 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8029 __alignof__ ( type-id ) { initializer-list , [opt] }
8030
8031 ADDRESS_P is true iff the unary-expression is appearing as the
8032 operand of the `&' operator. CAST_P is true if this expression is
8033 the target of a cast.
8034
8035 Returns a representation of the expression. */
8036
8037 static cp_expr
8038 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8039 bool address_p, bool cast_p, bool decltype_p)
8040 {
8041 cp_token *token;
8042 enum tree_code unary_operator;
8043
8044 /* Peek at the next token. */
8045 token = cp_lexer_peek_token (parser->lexer);
8046 /* Some keywords give away the kind of expression. */
8047 if (token->type == CPP_KEYWORD)
8048 {
8049 enum rid keyword = token->keyword;
8050
8051 switch (keyword)
8052 {
8053 case RID_ALIGNOF:
8054 case RID_SIZEOF:
8055 {
8056 tree operand, ret;
8057 enum tree_code op;
8058 location_t start_loc = token->location;
8059
8060 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8061 /* Consume the token. */
8062 cp_lexer_consume_token (parser->lexer);
8063 /* Parse the operand. */
8064 operand = cp_parser_sizeof_operand (parser, keyword);
8065
8066 if (TYPE_P (operand))
8067 ret = cxx_sizeof_or_alignof_type (operand, op, true);
8068 else
8069 {
8070 /* ISO C++ defines alignof only with types, not with
8071 expressions. So pedwarn if alignof is used with a non-
8072 type expression. However, __alignof__ is ok. */
8073 if (id_equal (token->u.value, "alignof"))
8074 pedwarn (token->location, OPT_Wpedantic,
8075 "ISO C++ does not allow %<alignof%> "
8076 "with a non-type");
8077
8078 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8079 }
8080 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8081 SIZEOF_EXPR with the original operand. */
8082 if (op == SIZEOF_EXPR && ret != error_mark_node)
8083 {
8084 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8085 {
8086 if (!processing_template_decl && TYPE_P (operand))
8087 {
8088 ret = build_min (SIZEOF_EXPR, size_type_node,
8089 build1 (NOP_EXPR, operand,
8090 error_mark_node));
8091 SIZEOF_EXPR_TYPE_P (ret) = 1;
8092 }
8093 else
8094 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8095 TREE_SIDE_EFFECTS (ret) = 0;
8096 TREE_READONLY (ret) = 1;
8097 }
8098 }
8099
8100 /* Construct a location e.g. :
8101 alignof (expr)
8102 ^~~~~~~~~~~~~~
8103 with start == caret at the start of the "alignof"/"sizeof"
8104 token, with the endpoint at the final closing paren. */
8105 location_t finish_loc
8106 = cp_lexer_previous_token (parser->lexer)->location;
8107 location_t compound_loc
8108 = make_location (start_loc, start_loc, finish_loc);
8109
8110 cp_expr ret_expr (ret);
8111 ret_expr.set_location (compound_loc);
8112 return ret_expr;
8113 }
8114
8115 case RID_NEW:
8116 return cp_parser_new_expression (parser);
8117
8118 case RID_DELETE:
8119 return cp_parser_delete_expression (parser);
8120
8121 case RID_EXTENSION:
8122 {
8123 /* The saved value of the PEDANTIC flag. */
8124 int saved_pedantic;
8125 tree expr;
8126
8127 /* Save away the PEDANTIC flag. */
8128 cp_parser_extension_opt (parser, &saved_pedantic);
8129 /* Parse the cast-expression. */
8130 expr = cp_parser_simple_cast_expression (parser);
8131 /* Restore the PEDANTIC flag. */
8132 pedantic = saved_pedantic;
8133
8134 return expr;
8135 }
8136
8137 case RID_REALPART:
8138 case RID_IMAGPART:
8139 {
8140 tree expression;
8141
8142 /* Consume the `__real__' or `__imag__' token. */
8143 cp_lexer_consume_token (parser->lexer);
8144 /* Parse the cast-expression. */
8145 expression = cp_parser_simple_cast_expression (parser);
8146 /* Create the complete representation. */
8147 return build_x_unary_op (token->location,
8148 (keyword == RID_REALPART
8149 ? REALPART_EXPR : IMAGPART_EXPR),
8150 expression,
8151 tf_warning_or_error);
8152 }
8153 break;
8154
8155 case RID_TRANSACTION_ATOMIC:
8156 case RID_TRANSACTION_RELAXED:
8157 return cp_parser_transaction_expression (parser, keyword);
8158
8159 case RID_NOEXCEPT:
8160 {
8161 tree expr;
8162 const char *saved_message;
8163 bool saved_integral_constant_expression_p;
8164 bool saved_non_integral_constant_expression_p;
8165 bool saved_greater_than_is_operator_p;
8166
8167 cp_lexer_consume_token (parser->lexer);
8168 matching_parens parens;
8169 parens.require_open (parser);
8170
8171 saved_message = parser->type_definition_forbidden_message;
8172 parser->type_definition_forbidden_message
8173 = G_("types may not be defined in %<noexcept%> expressions");
8174
8175 saved_integral_constant_expression_p
8176 = parser->integral_constant_expression_p;
8177 saved_non_integral_constant_expression_p
8178 = parser->non_integral_constant_expression_p;
8179 parser->integral_constant_expression_p = false;
8180
8181 saved_greater_than_is_operator_p
8182 = parser->greater_than_is_operator_p;
8183 parser->greater_than_is_operator_p = true;
8184
8185 ++cp_unevaluated_operand;
8186 ++c_inhibit_evaluation_warnings;
8187 ++cp_noexcept_operand;
8188 expr = cp_parser_expression (parser);
8189 --cp_noexcept_operand;
8190 --c_inhibit_evaluation_warnings;
8191 --cp_unevaluated_operand;
8192
8193 parser->greater_than_is_operator_p
8194 = saved_greater_than_is_operator_p;
8195
8196 parser->integral_constant_expression_p
8197 = saved_integral_constant_expression_p;
8198 parser->non_integral_constant_expression_p
8199 = saved_non_integral_constant_expression_p;
8200
8201 parser->type_definition_forbidden_message = saved_message;
8202
8203 parens.require_close (parser);
8204 return finish_noexcept_expr (expr, tf_warning_or_error);
8205 }
8206
8207 default:
8208 break;
8209 }
8210 }
8211
8212 /* Look for the `:: new' and `:: delete', which also signal the
8213 beginning of a new-expression, or delete-expression,
8214 respectively. If the next token is `::', then it might be one of
8215 these. */
8216 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8217 {
8218 enum rid keyword;
8219
8220 /* See if the token after the `::' is one of the keywords in
8221 which we're interested. */
8222 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8223 /* If it's `new', we have a new-expression. */
8224 if (keyword == RID_NEW)
8225 return cp_parser_new_expression (parser);
8226 /* Similarly, for `delete'. */
8227 else if (keyword == RID_DELETE)
8228 return cp_parser_delete_expression (parser);
8229 }
8230
8231 /* Look for a unary operator. */
8232 unary_operator = cp_parser_unary_operator (token);
8233 /* The `++' and `--' operators can be handled similarly, even though
8234 they are not technically unary-operators in the grammar. */
8235 if (unary_operator == ERROR_MARK)
8236 {
8237 if (token->type == CPP_PLUS_PLUS)
8238 unary_operator = PREINCREMENT_EXPR;
8239 else if (token->type == CPP_MINUS_MINUS)
8240 unary_operator = PREDECREMENT_EXPR;
8241 /* Handle the GNU address-of-label extension. */
8242 else if (cp_parser_allow_gnu_extensions_p (parser)
8243 && token->type == CPP_AND_AND)
8244 {
8245 tree identifier;
8246 tree expression;
8247 location_t start_loc = token->location;
8248
8249 /* Consume the '&&' token. */
8250 cp_lexer_consume_token (parser->lexer);
8251 /* Look for the identifier. */
8252 location_t finish_loc
8253 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8254 identifier = cp_parser_identifier (parser);
8255 /* Construct a location of the form:
8256 &&label
8257 ^~~~~~~
8258 with caret==start at the "&&", finish at the end of the label. */
8259 location_t combined_loc
8260 = make_location (start_loc, start_loc, finish_loc);
8261 /* Create an expression representing the address. */
8262 expression = finish_label_address_expr (identifier, combined_loc);
8263 if (cp_parser_non_integral_constant_expression (parser,
8264 NIC_ADDR_LABEL))
8265 expression = error_mark_node;
8266 return expression;
8267 }
8268 }
8269 if (unary_operator != ERROR_MARK)
8270 {
8271 cp_expr cast_expression;
8272 cp_expr expression = error_mark_node;
8273 non_integral_constant non_constant_p = NIC_NONE;
8274 location_t loc = token->location;
8275 tsubst_flags_t complain = complain_flags (decltype_p);
8276
8277 /* Consume the operator token. */
8278 token = cp_lexer_consume_token (parser->lexer);
8279 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8280
8281 /* Parse the cast-expression. */
8282 cast_expression
8283 = cp_parser_cast_expression (parser,
8284 unary_operator == ADDR_EXPR,
8285 /*cast_p=*/false,
8286 /*decltype*/false,
8287 pidk);
8288
8289 /* Make a location:
8290 OP_TOKEN CAST_EXPRESSION
8291 ^~~~~~~~~~~~~~~~~~~~~~~~~
8292 with start==caret at the operator token, and
8293 extending to the end of the cast_expression. */
8294 loc = make_location (loc, loc, cast_expression.get_finish ());
8295
8296 /* Now, build an appropriate representation. */
8297 switch (unary_operator)
8298 {
8299 case INDIRECT_REF:
8300 non_constant_p = NIC_STAR;
8301 expression = build_x_indirect_ref (loc, cast_expression,
8302 RO_UNARY_STAR,
8303 complain);
8304 /* TODO: build_x_indirect_ref does not always honor the
8305 location, so ensure it is set. */
8306 expression.set_location (loc);
8307 break;
8308
8309 case ADDR_EXPR:
8310 non_constant_p = NIC_ADDR;
8311 /* Fall through. */
8312 case BIT_NOT_EXPR:
8313 expression = build_x_unary_op (loc, unary_operator,
8314 cast_expression,
8315 complain);
8316 /* TODO: build_x_unary_op does not always honor the location,
8317 so ensure it is set. */
8318 expression.set_location (loc);
8319 break;
8320
8321 case PREINCREMENT_EXPR:
8322 case PREDECREMENT_EXPR:
8323 non_constant_p = unary_operator == PREINCREMENT_EXPR
8324 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8325 /* Fall through. */
8326 case NEGATE_EXPR:
8327 /* Immediately fold negation of a constant, unless the constant is 0
8328 (since -0 == 0) or it would overflow. */
8329 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8330 && CONSTANT_CLASS_P (cast_expression)
8331 && !integer_zerop (cast_expression)
8332 && !TREE_OVERFLOW (cast_expression))
8333 {
8334 tree folded = fold_build1 (unary_operator,
8335 TREE_TYPE (cast_expression),
8336 cast_expression);
8337 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8338 {
8339 expression = cp_expr (folded, loc);
8340 break;
8341 }
8342 }
8343 /* Fall through. */
8344 case UNARY_PLUS_EXPR:
8345 case TRUTH_NOT_EXPR:
8346 expression = finish_unary_op_expr (loc, unary_operator,
8347 cast_expression, complain);
8348 break;
8349
8350 default:
8351 gcc_unreachable ();
8352 }
8353
8354 if (non_constant_p != NIC_NONE
8355 && cp_parser_non_integral_constant_expression (parser,
8356 non_constant_p))
8357 expression = error_mark_node;
8358
8359 return expression;
8360 }
8361
8362 return cp_parser_postfix_expression (parser, address_p, cast_p,
8363 /*member_access_only_p=*/false,
8364 decltype_p,
8365 pidk);
8366 }
8367
8368 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8369 unary-operator, the corresponding tree code is returned. */
8370
8371 static enum tree_code
8372 cp_parser_unary_operator (cp_token* token)
8373 {
8374 switch (token->type)
8375 {
8376 case CPP_MULT:
8377 return INDIRECT_REF;
8378
8379 case CPP_AND:
8380 return ADDR_EXPR;
8381
8382 case CPP_PLUS:
8383 return UNARY_PLUS_EXPR;
8384
8385 case CPP_MINUS:
8386 return NEGATE_EXPR;
8387
8388 case CPP_NOT:
8389 return TRUTH_NOT_EXPR;
8390
8391 case CPP_COMPL:
8392 return BIT_NOT_EXPR;
8393
8394 default:
8395 return ERROR_MARK;
8396 }
8397 }
8398
8399 /* Parse a new-expression.
8400
8401 new-expression:
8402 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8403 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8404
8405 Returns a representation of the expression. */
8406
8407 static tree
8408 cp_parser_new_expression (cp_parser* parser)
8409 {
8410 bool global_scope_p;
8411 vec<tree, va_gc> *placement;
8412 tree type;
8413 vec<tree, va_gc> *initializer;
8414 tree nelts = NULL_TREE;
8415 tree ret;
8416
8417 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8418
8419 /* Look for the optional `::' operator. */
8420 global_scope_p
8421 = (cp_parser_global_scope_opt (parser,
8422 /*current_scope_valid_p=*/false)
8423 != NULL_TREE);
8424 /* Look for the `new' operator. */
8425 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8426 /* There's no easy way to tell a new-placement from the
8427 `( type-id )' construct. */
8428 cp_parser_parse_tentatively (parser);
8429 /* Look for a new-placement. */
8430 placement = cp_parser_new_placement (parser);
8431 /* If that didn't work out, there's no new-placement. */
8432 if (!cp_parser_parse_definitely (parser))
8433 {
8434 if (placement != NULL)
8435 release_tree_vector (placement);
8436 placement = NULL;
8437 }
8438
8439 /* If the next token is a `(', then we have a parenthesized
8440 type-id. */
8441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8442 {
8443 cp_token *token;
8444 const char *saved_message = parser->type_definition_forbidden_message;
8445
8446 /* Consume the `('. */
8447 matching_parens parens;
8448 parens.consume_open (parser);
8449
8450 /* Parse the type-id. */
8451 parser->type_definition_forbidden_message
8452 = G_("types may not be defined in a new-expression");
8453 {
8454 type_id_in_expr_sentinel s (parser);
8455 type = cp_parser_type_id (parser);
8456 }
8457 parser->type_definition_forbidden_message = saved_message;
8458
8459 /* Look for the closing `)'. */
8460 parens.require_close (parser);
8461 token = cp_lexer_peek_token (parser->lexer);
8462 /* There should not be a direct-new-declarator in this production,
8463 but GCC used to allowed this, so we check and emit a sensible error
8464 message for this case. */
8465 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8466 {
8467 error_at (token->location,
8468 "array bound forbidden after parenthesized type-id");
8469 inform (token->location,
8470 "try removing the parentheses around the type-id");
8471 cp_parser_direct_new_declarator (parser);
8472 }
8473 }
8474 /* Otherwise, there must be a new-type-id. */
8475 else
8476 type = cp_parser_new_type_id (parser, &nelts);
8477
8478 /* If the next token is a `(' or '{', then we have a new-initializer. */
8479 cp_token *token = cp_lexer_peek_token (parser->lexer);
8480 if (token->type == CPP_OPEN_PAREN
8481 || token->type == CPP_OPEN_BRACE)
8482 initializer = cp_parser_new_initializer (parser);
8483 else
8484 initializer = NULL;
8485
8486 /* A new-expression may not appear in an integral constant
8487 expression. */
8488 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8489 ret = error_mark_node;
8490 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8491 of a new-type-id or type-id of a new-expression, the new-expression shall
8492 contain a new-initializer of the form ( assignment-expression )".
8493 Additionally, consistently with the spirit of DR 1467, we want to accept
8494 'new auto { 2 }' too. */
8495 else if ((ret = type_uses_auto (type))
8496 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8497 && (vec_safe_length (initializer) != 1
8498 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8499 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8500 {
8501 error_at (token->location,
8502 "initialization of new-expression for type %<auto%> "
8503 "requires exactly one element");
8504 ret = error_mark_node;
8505 }
8506 else
8507 {
8508 /* Construct a location e.g.:
8509 ptr = new int[100]
8510 ^~~~~~~~~~~~
8511 with caret == start at the start of the "new" token, and the end
8512 at the end of the final token we consumed. */
8513 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8514 location_t end_loc = get_finish (end_tok->location);
8515 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8516
8517 /* Create a representation of the new-expression. */
8518 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8519 tf_warning_or_error);
8520 protected_set_expr_location (ret, combined_loc);
8521 }
8522
8523 if (placement != NULL)
8524 release_tree_vector (placement);
8525 if (initializer != NULL)
8526 release_tree_vector (initializer);
8527
8528 return ret;
8529 }
8530
8531 /* Parse a new-placement.
8532
8533 new-placement:
8534 ( expression-list )
8535
8536 Returns the same representation as for an expression-list. */
8537
8538 static vec<tree, va_gc> *
8539 cp_parser_new_placement (cp_parser* parser)
8540 {
8541 vec<tree, va_gc> *expression_list;
8542
8543 /* Parse the expression-list. */
8544 expression_list = (cp_parser_parenthesized_expression_list
8545 (parser, non_attr, /*cast_p=*/false,
8546 /*allow_expansion_p=*/true,
8547 /*non_constant_p=*/NULL));
8548
8549 if (expression_list && expression_list->is_empty ())
8550 error ("expected expression-list or type-id");
8551
8552 return expression_list;
8553 }
8554
8555 /* Parse a new-type-id.
8556
8557 new-type-id:
8558 type-specifier-seq new-declarator [opt]
8559
8560 Returns the TYPE allocated. If the new-type-id indicates an array
8561 type, *NELTS is set to the number of elements in the last array
8562 bound; the TYPE will not include the last array bound. */
8563
8564 static tree
8565 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8566 {
8567 cp_decl_specifier_seq type_specifier_seq;
8568 cp_declarator *new_declarator;
8569 cp_declarator *declarator;
8570 cp_declarator *outer_declarator;
8571 const char *saved_message;
8572
8573 /* The type-specifier sequence must not contain type definitions.
8574 (It cannot contain declarations of new types either, but if they
8575 are not definitions we will catch that because they are not
8576 complete.) */
8577 saved_message = parser->type_definition_forbidden_message;
8578 parser->type_definition_forbidden_message
8579 = G_("types may not be defined in a new-type-id");
8580 /* Parse the type-specifier-seq. */
8581 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8582 /*is_trailing_return=*/false,
8583 &type_specifier_seq);
8584 /* Restore the old message. */
8585 parser->type_definition_forbidden_message = saved_message;
8586
8587 if (type_specifier_seq.type == error_mark_node)
8588 return error_mark_node;
8589
8590 /* Parse the new-declarator. */
8591 new_declarator = cp_parser_new_declarator_opt (parser);
8592
8593 /* Determine the number of elements in the last array dimension, if
8594 any. */
8595 *nelts = NULL_TREE;
8596 /* Skip down to the last array dimension. */
8597 declarator = new_declarator;
8598 outer_declarator = NULL;
8599 while (declarator && (declarator->kind == cdk_pointer
8600 || declarator->kind == cdk_ptrmem))
8601 {
8602 outer_declarator = declarator;
8603 declarator = declarator->declarator;
8604 }
8605 while (declarator
8606 && declarator->kind == cdk_array
8607 && declarator->declarator
8608 && declarator->declarator->kind == cdk_array)
8609 {
8610 outer_declarator = declarator;
8611 declarator = declarator->declarator;
8612 }
8613
8614 if (declarator && declarator->kind == cdk_array)
8615 {
8616 *nelts = declarator->u.array.bounds;
8617 if (*nelts == error_mark_node)
8618 *nelts = integer_one_node;
8619
8620 if (outer_declarator)
8621 outer_declarator->declarator = declarator->declarator;
8622 else
8623 new_declarator = NULL;
8624 }
8625
8626 return groktypename (&type_specifier_seq, new_declarator, false);
8627 }
8628
8629 /* Parse an (optional) new-declarator.
8630
8631 new-declarator:
8632 ptr-operator new-declarator [opt]
8633 direct-new-declarator
8634
8635 Returns the declarator. */
8636
8637 static cp_declarator *
8638 cp_parser_new_declarator_opt (cp_parser* parser)
8639 {
8640 enum tree_code code;
8641 tree type, std_attributes = NULL_TREE;
8642 cp_cv_quals cv_quals;
8643
8644 /* We don't know if there's a ptr-operator next, or not. */
8645 cp_parser_parse_tentatively (parser);
8646 /* Look for a ptr-operator. */
8647 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8648 /* If that worked, look for more new-declarators. */
8649 if (cp_parser_parse_definitely (parser))
8650 {
8651 cp_declarator *declarator;
8652
8653 /* Parse another optional declarator. */
8654 declarator = cp_parser_new_declarator_opt (parser);
8655
8656 declarator = cp_parser_make_indirect_declarator
8657 (code, type, cv_quals, declarator, std_attributes);
8658
8659 return declarator;
8660 }
8661
8662 /* If the next token is a `[', there is a direct-new-declarator. */
8663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8664 return cp_parser_direct_new_declarator (parser);
8665
8666 return NULL;
8667 }
8668
8669 /* Parse a direct-new-declarator.
8670
8671 direct-new-declarator:
8672 [ expression ]
8673 direct-new-declarator [constant-expression]
8674
8675 */
8676
8677 static cp_declarator *
8678 cp_parser_direct_new_declarator (cp_parser* parser)
8679 {
8680 cp_declarator *declarator = NULL;
8681
8682 while (true)
8683 {
8684 tree expression;
8685 cp_token *token;
8686
8687 /* Look for the opening `['. */
8688 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8689
8690 token = cp_lexer_peek_token (parser->lexer);
8691 expression = cp_parser_expression (parser);
8692 /* The standard requires that the expression have integral
8693 type. DR 74 adds enumeration types. We believe that the
8694 real intent is that these expressions be handled like the
8695 expression in a `switch' condition, which also allows
8696 classes with a single conversion to integral or
8697 enumeration type. */
8698 if (!processing_template_decl)
8699 {
8700 expression
8701 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8702 expression,
8703 /*complain=*/true);
8704 if (!expression)
8705 {
8706 error_at (token->location,
8707 "expression in new-declarator must have integral "
8708 "or enumeration type");
8709 expression = error_mark_node;
8710 }
8711 }
8712
8713 /* Look for the closing `]'. */
8714 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8715
8716 /* Add this bound to the declarator. */
8717 declarator = make_array_declarator (declarator, expression);
8718
8719 /* If the next token is not a `[', then there are no more
8720 bounds. */
8721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8722 break;
8723 }
8724
8725 return declarator;
8726 }
8727
8728 /* Parse a new-initializer.
8729
8730 new-initializer:
8731 ( expression-list [opt] )
8732 braced-init-list
8733
8734 Returns a representation of the expression-list. */
8735
8736 static vec<tree, va_gc> *
8737 cp_parser_new_initializer (cp_parser* parser)
8738 {
8739 vec<tree, va_gc> *expression_list;
8740
8741 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8742 {
8743 tree t;
8744 bool expr_non_constant_p;
8745 cp_lexer_set_source_position (parser->lexer);
8746 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8747 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8748 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8749 expression_list = make_tree_vector_single (t);
8750 }
8751 else
8752 expression_list = (cp_parser_parenthesized_expression_list
8753 (parser, non_attr, /*cast_p=*/false,
8754 /*allow_expansion_p=*/true,
8755 /*non_constant_p=*/NULL));
8756
8757 return expression_list;
8758 }
8759
8760 /* Parse a delete-expression.
8761
8762 delete-expression:
8763 :: [opt] delete cast-expression
8764 :: [opt] delete [ ] cast-expression
8765
8766 Returns a representation of the expression. */
8767
8768 static tree
8769 cp_parser_delete_expression (cp_parser* parser)
8770 {
8771 bool global_scope_p;
8772 bool array_p;
8773 tree expression;
8774
8775 /* Look for the optional `::' operator. */
8776 global_scope_p
8777 = (cp_parser_global_scope_opt (parser,
8778 /*current_scope_valid_p=*/false)
8779 != NULL_TREE);
8780 /* Look for the `delete' keyword. */
8781 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8782 /* See if the array syntax is in use. */
8783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8784 {
8785 /* Consume the `[' token. */
8786 cp_lexer_consume_token (parser->lexer);
8787 /* Look for the `]' token. */
8788 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8789 /* Remember that this is the `[]' construct. */
8790 array_p = true;
8791 }
8792 else
8793 array_p = false;
8794
8795 /* Parse the cast-expression. */
8796 expression = cp_parser_simple_cast_expression (parser);
8797
8798 /* A delete-expression may not appear in an integral constant
8799 expression. */
8800 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8801 return error_mark_node;
8802
8803 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8804 tf_warning_or_error);
8805 }
8806
8807 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8808 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8809 0 otherwise. */
8810
8811 static int
8812 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8813 {
8814 cp_token *token = cp_lexer_peek_token (parser->lexer);
8815 switch (token->type)
8816 {
8817 case CPP_COMMA:
8818 case CPP_SEMICOLON:
8819 case CPP_QUERY:
8820 case CPP_COLON:
8821 case CPP_CLOSE_SQUARE:
8822 case CPP_CLOSE_PAREN:
8823 case CPP_CLOSE_BRACE:
8824 case CPP_OPEN_BRACE:
8825 case CPP_DOT:
8826 case CPP_DOT_STAR:
8827 case CPP_DEREF:
8828 case CPP_DEREF_STAR:
8829 case CPP_DIV:
8830 case CPP_MOD:
8831 case CPP_LSHIFT:
8832 case CPP_RSHIFT:
8833 case CPP_LESS:
8834 case CPP_GREATER:
8835 case CPP_LESS_EQ:
8836 case CPP_GREATER_EQ:
8837 case CPP_EQ_EQ:
8838 case CPP_NOT_EQ:
8839 case CPP_EQ:
8840 case CPP_MULT_EQ:
8841 case CPP_DIV_EQ:
8842 case CPP_MOD_EQ:
8843 case CPP_PLUS_EQ:
8844 case CPP_MINUS_EQ:
8845 case CPP_RSHIFT_EQ:
8846 case CPP_LSHIFT_EQ:
8847 case CPP_AND_EQ:
8848 case CPP_XOR_EQ:
8849 case CPP_OR_EQ:
8850 case CPP_XOR:
8851 case CPP_OR:
8852 case CPP_OR_OR:
8853 case CPP_EOF:
8854 case CPP_ELLIPSIS:
8855 return 0;
8856
8857 case CPP_OPEN_PAREN:
8858 /* In ((type ()) () the last () isn't a valid cast-expression,
8859 so the whole must be parsed as postfix-expression. */
8860 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8861 != CPP_CLOSE_PAREN;
8862
8863 case CPP_OPEN_SQUARE:
8864 /* '[' may start a primary-expression in obj-c++ and in C++11,
8865 as a lambda-expression, eg, '(void)[]{}'. */
8866 if (cxx_dialect >= cxx11)
8867 return -1;
8868 return c_dialect_objc ();
8869
8870 case CPP_PLUS_PLUS:
8871 case CPP_MINUS_MINUS:
8872 /* '++' and '--' may or may not start a cast-expression:
8873
8874 struct T { void operator++(int); };
8875 void f() { (T())++; }
8876
8877 vs
8878
8879 int a;
8880 (int)++a; */
8881 return -1;
8882
8883 default:
8884 return 1;
8885 }
8886 }
8887
8888 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8889 in the order: const_cast, static_cast, reinterpret_cast.
8890
8891 Don't suggest dynamic_cast.
8892
8893 Return the first legal cast kind found, or NULL otherwise. */
8894
8895 static const char *
8896 get_cast_suggestion (tree dst_type, tree orig_expr)
8897 {
8898 tree trial;
8899
8900 /* Reuse the parser logic by attempting to build the various kinds of
8901 cast, with "complain" disabled.
8902 Identify the first such cast that is valid. */
8903
8904 /* Don't attempt to run such logic within template processing. */
8905 if (processing_template_decl)
8906 return NULL;
8907
8908 /* First try const_cast. */
8909 trial = build_const_cast (dst_type, orig_expr, tf_none);
8910 if (trial != error_mark_node)
8911 return "const_cast";
8912
8913 /* If that fails, try static_cast. */
8914 trial = build_static_cast (dst_type, orig_expr, tf_none);
8915 if (trial != error_mark_node)
8916 return "static_cast";
8917
8918 /* Finally, try reinterpret_cast. */
8919 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8920 if (trial != error_mark_node)
8921 return "reinterpret_cast";
8922
8923 /* No such cast possible. */
8924 return NULL;
8925 }
8926
8927 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8928 suggesting how to convert a C-style cast of the form:
8929
8930 (DST_TYPE)ORIG_EXPR
8931
8932 to a C++-style cast.
8933
8934 The primary range of RICHLOC is asssumed to be that of the original
8935 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8936 of the parens in the C-style cast. */
8937
8938 static void
8939 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8940 location_t close_paren_loc, tree orig_expr,
8941 tree dst_type)
8942 {
8943 /* This function is non-trivial, so bail out now if the warning isn't
8944 going to be emitted. */
8945 if (!warn_old_style_cast)
8946 return;
8947
8948 /* Try to find a legal C++ cast, trying them in order:
8949 const_cast, static_cast, reinterpret_cast. */
8950 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8951 if (!cast_suggestion)
8952 return;
8953
8954 /* Replace the open paren with "CAST_SUGGESTION<". */
8955 pretty_printer pp;
8956 pp_printf (&pp, "%s<", cast_suggestion);
8957 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8958
8959 /* Replace the close paren with "> (". */
8960 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8961
8962 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8963 rich_loc->add_fixit_insert_after (")");
8964 }
8965
8966
8967 /* Parse a cast-expression.
8968
8969 cast-expression:
8970 unary-expression
8971 ( type-id ) cast-expression
8972
8973 ADDRESS_P is true iff the unary-expression is appearing as the
8974 operand of the `&' operator. CAST_P is true if this expression is
8975 the target of a cast.
8976
8977 Returns a representation of the expression. */
8978
8979 static cp_expr
8980 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8981 bool decltype_p, cp_id_kind * pidk)
8982 {
8983 /* If it's a `(', then we might be looking at a cast. */
8984 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8985 {
8986 tree type = NULL_TREE;
8987 cp_expr expr (NULL_TREE);
8988 int cast_expression = 0;
8989 const char *saved_message;
8990
8991 /* There's no way to know yet whether or not this is a cast.
8992 For example, `(int (3))' is a unary-expression, while `(int)
8993 3' is a cast. So, we resort to parsing tentatively. */
8994 cp_parser_parse_tentatively (parser);
8995 /* Types may not be defined in a cast. */
8996 saved_message = parser->type_definition_forbidden_message;
8997 parser->type_definition_forbidden_message
8998 = G_("types may not be defined in casts");
8999 /* Consume the `('. */
9000 matching_parens parens;
9001 cp_token *open_paren = parens.consume_open (parser);
9002 location_t open_paren_loc = open_paren->location;
9003 location_t close_paren_loc = UNKNOWN_LOCATION;
9004
9005 /* A very tricky bit is that `(struct S) { 3 }' is a
9006 compound-literal (which we permit in C++ as an extension).
9007 But, that construct is not a cast-expression -- it is a
9008 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9009 is legal; if the compound-literal were a cast-expression,
9010 you'd need an extra set of parentheses.) But, if we parse
9011 the type-id, and it happens to be a class-specifier, then we
9012 will commit to the parse at that point, because we cannot
9013 undo the action that is done when creating a new class. So,
9014 then we cannot back up and do a postfix-expression.
9015
9016 Another tricky case is the following (c++/29234):
9017
9018 struct S { void operator () (); };
9019
9020 void foo ()
9021 {
9022 ( S()() );
9023 }
9024
9025 As a type-id we parse the parenthesized S()() as a function
9026 returning a function, groktypename complains and we cannot
9027 back up in this case either.
9028
9029 Therefore, we scan ahead to the closing `)', and check to see
9030 if the tokens after the `)' can start a cast-expression. Otherwise
9031 we are dealing with an unary-expression, a postfix-expression
9032 or something else.
9033
9034 Yet another tricky case, in C++11, is the following (c++/54891):
9035
9036 (void)[]{};
9037
9038 The issue is that usually, besides the case of lambda-expressions,
9039 the parenthesized type-id cannot be followed by '[', and, eg, we
9040 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9041 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9042 we don't commit, we try a cast-expression, then an unary-expression.
9043
9044 Save tokens so that we can put them back. */
9045 cp_lexer_save_tokens (parser->lexer);
9046
9047 /* We may be looking at a cast-expression. */
9048 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9049 /*consume_paren=*/true))
9050 cast_expression
9051 = cp_parser_tokens_start_cast_expression (parser);
9052
9053 /* Roll back the tokens we skipped. */
9054 cp_lexer_rollback_tokens (parser->lexer);
9055 /* If we aren't looking at a cast-expression, simulate an error so
9056 that the call to cp_parser_error_occurred below returns true. */
9057 if (!cast_expression)
9058 cp_parser_simulate_error (parser);
9059 else
9060 {
9061 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9062 parser->in_type_id_in_expr_p = true;
9063 /* Look for the type-id. */
9064 type = cp_parser_type_id (parser);
9065 /* Look for the closing `)'. */
9066 cp_token *close_paren = parens.require_close (parser);
9067 if (close_paren)
9068 close_paren_loc = close_paren->location;
9069 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9070 }
9071
9072 /* Restore the saved message. */
9073 parser->type_definition_forbidden_message = saved_message;
9074
9075 /* At this point this can only be either a cast or a
9076 parenthesized ctor such as `(T ())' that looks like a cast to
9077 function returning T. */
9078 if (!cp_parser_error_occurred (parser))
9079 {
9080 /* Only commit if the cast-expression doesn't start with
9081 '++', '--', or '[' in C++11. */
9082 if (cast_expression > 0)
9083 cp_parser_commit_to_topmost_tentative_parse (parser);
9084
9085 expr = cp_parser_cast_expression (parser,
9086 /*address_p=*/false,
9087 /*cast_p=*/true,
9088 /*decltype_p=*/false,
9089 pidk);
9090
9091 if (cp_parser_parse_definitely (parser))
9092 {
9093 /* Warn about old-style casts, if so requested. */
9094 if (warn_old_style_cast
9095 && !in_system_header_at (input_location)
9096 && !VOID_TYPE_P (type)
9097 && current_lang_name != lang_name_c)
9098 {
9099 gcc_rich_location rich_loc (input_location);
9100 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9101 expr, type);
9102 warning_at (&rich_loc, OPT_Wold_style_cast,
9103 "use of old-style cast to %q#T", type);
9104 }
9105
9106 /* Only type conversions to integral or enumeration types
9107 can be used in constant-expressions. */
9108 if (!cast_valid_in_integral_constant_expression_p (type)
9109 && cp_parser_non_integral_constant_expression (parser,
9110 NIC_CAST))
9111 return error_mark_node;
9112
9113 /* Perform the cast. */
9114 /* Make a location:
9115 (TYPE) EXPR
9116 ^~~~~~~~~~~
9117 with start==caret at the open paren, extending to the
9118 end of "expr". */
9119 location_t cast_loc = make_location (open_paren_loc,
9120 open_paren_loc,
9121 expr.get_finish ());
9122 expr = build_c_cast (cast_loc, type, expr);
9123 return expr;
9124 }
9125 }
9126 else
9127 cp_parser_abort_tentative_parse (parser);
9128 }
9129
9130 /* If we get here, then it's not a cast, so it must be a
9131 unary-expression. */
9132 return cp_parser_unary_expression (parser, pidk, address_p,
9133 cast_p, decltype_p);
9134 }
9135
9136 /* Parse a binary expression of the general form:
9137
9138 pm-expression:
9139 cast-expression
9140 pm-expression .* cast-expression
9141 pm-expression ->* cast-expression
9142
9143 multiplicative-expression:
9144 pm-expression
9145 multiplicative-expression * pm-expression
9146 multiplicative-expression / pm-expression
9147 multiplicative-expression % pm-expression
9148
9149 additive-expression:
9150 multiplicative-expression
9151 additive-expression + multiplicative-expression
9152 additive-expression - multiplicative-expression
9153
9154 shift-expression:
9155 additive-expression
9156 shift-expression << additive-expression
9157 shift-expression >> additive-expression
9158
9159 relational-expression:
9160 shift-expression
9161 relational-expression < shift-expression
9162 relational-expression > shift-expression
9163 relational-expression <= shift-expression
9164 relational-expression >= shift-expression
9165
9166 GNU Extension:
9167
9168 relational-expression:
9169 relational-expression <? shift-expression
9170 relational-expression >? shift-expression
9171
9172 equality-expression:
9173 relational-expression
9174 equality-expression == relational-expression
9175 equality-expression != relational-expression
9176
9177 and-expression:
9178 equality-expression
9179 and-expression & equality-expression
9180
9181 exclusive-or-expression:
9182 and-expression
9183 exclusive-or-expression ^ and-expression
9184
9185 inclusive-or-expression:
9186 exclusive-or-expression
9187 inclusive-or-expression | exclusive-or-expression
9188
9189 logical-and-expression:
9190 inclusive-or-expression
9191 logical-and-expression && inclusive-or-expression
9192
9193 logical-or-expression:
9194 logical-and-expression
9195 logical-or-expression || logical-and-expression
9196
9197 All these are implemented with a single function like:
9198
9199 binary-expression:
9200 simple-cast-expression
9201 binary-expression <token> binary-expression
9202
9203 CAST_P is true if this expression is the target of a cast.
9204
9205 The binops_by_token map is used to get the tree codes for each <token> type.
9206 binary-expressions are associated according to a precedence table. */
9207
9208 #define TOKEN_PRECEDENCE(token) \
9209 (((token->type == CPP_GREATER \
9210 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9211 && !parser->greater_than_is_operator_p) \
9212 ? PREC_NOT_OPERATOR \
9213 : binops_by_token[token->type].prec)
9214
9215 static cp_expr
9216 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9217 bool no_toplevel_fold_p,
9218 bool decltype_p,
9219 enum cp_parser_prec prec,
9220 cp_id_kind * pidk)
9221 {
9222 cp_parser_expression_stack stack;
9223 cp_parser_expression_stack_entry *sp = &stack[0];
9224 cp_parser_expression_stack_entry current;
9225 cp_expr rhs;
9226 cp_token *token;
9227 enum tree_code rhs_type;
9228 enum cp_parser_prec new_prec, lookahead_prec;
9229 tree overload;
9230
9231 /* Parse the first expression. */
9232 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9233 ? TRUTH_NOT_EXPR : ERROR_MARK);
9234 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9235 cast_p, decltype_p, pidk);
9236 current.prec = prec;
9237
9238 if (cp_parser_error_occurred (parser))
9239 return error_mark_node;
9240
9241 for (;;)
9242 {
9243 /* Get an operator token. */
9244 token = cp_lexer_peek_token (parser->lexer);
9245
9246 if (warn_cxx11_compat
9247 && token->type == CPP_RSHIFT
9248 && !parser->greater_than_is_operator_p)
9249 {
9250 if (warning_at (token->location, OPT_Wc__11_compat,
9251 "%<>>%> operator is treated"
9252 " as two right angle brackets in C++11"))
9253 inform (token->location,
9254 "suggest parentheses around %<>>%> expression");
9255 }
9256
9257 new_prec = TOKEN_PRECEDENCE (token);
9258 if (new_prec != PREC_NOT_OPERATOR
9259 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9260 /* This is a fold-expression; handle it later. */
9261 new_prec = PREC_NOT_OPERATOR;
9262
9263 /* Popping an entry off the stack means we completed a subexpression:
9264 - either we found a token which is not an operator (`>' where it is not
9265 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9266 will happen repeatedly;
9267 - or, we found an operator which has lower priority. This is the case
9268 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9269 parsing `3 * 4'. */
9270 if (new_prec <= current.prec)
9271 {
9272 if (sp == stack)
9273 break;
9274 else
9275 goto pop;
9276 }
9277
9278 get_rhs:
9279 current.tree_type = binops_by_token[token->type].tree_type;
9280 current.loc = token->location;
9281
9282 /* We used the operator token. */
9283 cp_lexer_consume_token (parser->lexer);
9284
9285 /* For "false && x" or "true || x", x will never be executed;
9286 disable warnings while evaluating it. */
9287 if (current.tree_type == TRUTH_ANDIF_EXPR)
9288 c_inhibit_evaluation_warnings +=
9289 cp_fully_fold (current.lhs) == truthvalue_false_node;
9290 else if (current.tree_type == TRUTH_ORIF_EXPR)
9291 c_inhibit_evaluation_warnings +=
9292 cp_fully_fold (current.lhs) == truthvalue_true_node;
9293
9294 /* Extract another operand. It may be the RHS of this expression
9295 or the LHS of a new, higher priority expression. */
9296 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9297 ? TRUTH_NOT_EXPR : ERROR_MARK);
9298 rhs = cp_parser_simple_cast_expression (parser);
9299
9300 /* Get another operator token. Look up its precedence to avoid
9301 building a useless (immediately popped) stack entry for common
9302 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9303 token = cp_lexer_peek_token (parser->lexer);
9304 lookahead_prec = TOKEN_PRECEDENCE (token);
9305 if (lookahead_prec != PREC_NOT_OPERATOR
9306 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9307 lookahead_prec = PREC_NOT_OPERATOR;
9308 if (lookahead_prec > new_prec)
9309 {
9310 /* ... and prepare to parse the RHS of the new, higher priority
9311 expression. Since precedence levels on the stack are
9312 monotonically increasing, we do not have to care about
9313 stack overflows. */
9314 *sp = current;
9315 ++sp;
9316 current.lhs = rhs;
9317 current.lhs_type = rhs_type;
9318 current.prec = new_prec;
9319 new_prec = lookahead_prec;
9320 goto get_rhs;
9321
9322 pop:
9323 lookahead_prec = new_prec;
9324 /* If the stack is not empty, we have parsed into LHS the right side
9325 (`4' in the example above) of an expression we had suspended.
9326 We can use the information on the stack to recover the LHS (`3')
9327 from the stack together with the tree code (`MULT_EXPR'), and
9328 the precedence of the higher level subexpression
9329 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9330 which will be used to actually build the additive expression. */
9331 rhs = current.lhs;
9332 rhs_type = current.lhs_type;
9333 --sp;
9334 current = *sp;
9335 }
9336
9337 /* Undo the disabling of warnings done above. */
9338 if (current.tree_type == TRUTH_ANDIF_EXPR)
9339 c_inhibit_evaluation_warnings -=
9340 cp_fully_fold (current.lhs) == truthvalue_false_node;
9341 else if (current.tree_type == TRUTH_ORIF_EXPR)
9342 c_inhibit_evaluation_warnings -=
9343 cp_fully_fold (current.lhs) == truthvalue_true_node;
9344
9345 if (warn_logical_not_paren
9346 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9347 && current.lhs_type == TRUTH_NOT_EXPR
9348 /* Avoid warning for !!x == y. */
9349 && (TREE_CODE (current.lhs) != NE_EXPR
9350 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9351 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9352 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9353 /* Avoid warning for !b == y where b is boolean. */
9354 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9355 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9356 != BOOLEAN_TYPE))))
9357 /* Avoid warning for !!b == y where b is boolean. */
9358 && (!DECL_P (current.lhs)
9359 || TREE_TYPE (current.lhs) == NULL_TREE
9360 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9361 warn_logical_not_parentheses (current.loc, current.tree_type,
9362 current.lhs, maybe_constant_value (rhs));
9363
9364 overload = NULL;
9365
9366 location_t combined_loc = make_location (current.loc,
9367 current.lhs.get_start (),
9368 rhs.get_finish ());
9369
9370 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9371 ERROR_MARK for everything that is not a binary expression.
9372 This makes warn_about_parentheses miss some warnings that
9373 involve unary operators. For unary expressions we should
9374 pass the correct tree_code unless the unary expression was
9375 surrounded by parentheses.
9376 */
9377 if (no_toplevel_fold_p
9378 && lookahead_prec <= current.prec
9379 && sp == stack)
9380 current.lhs = build2_loc (combined_loc,
9381 current.tree_type,
9382 TREE_CODE_CLASS (current.tree_type)
9383 == tcc_comparison
9384 ? boolean_type_node : TREE_TYPE (current.lhs),
9385 current.lhs, rhs);
9386 else
9387 {
9388 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9389 current.lhs, current.lhs_type,
9390 rhs, rhs_type, &overload,
9391 complain_flags (decltype_p));
9392 /* TODO: build_x_binary_op doesn't always honor the location. */
9393 current.lhs.set_location (combined_loc);
9394 }
9395 current.lhs_type = current.tree_type;
9396
9397 /* If the binary operator required the use of an overloaded operator,
9398 then this expression cannot be an integral constant-expression.
9399 An overloaded operator can be used even if both operands are
9400 otherwise permissible in an integral constant-expression if at
9401 least one of the operands is of enumeration type. */
9402
9403 if (overload
9404 && cp_parser_non_integral_constant_expression (parser,
9405 NIC_OVERLOADED))
9406 return error_mark_node;
9407 }
9408
9409 return current.lhs;
9410 }
9411
9412 static cp_expr
9413 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9414 bool no_toplevel_fold_p,
9415 enum cp_parser_prec prec,
9416 cp_id_kind * pidk)
9417 {
9418 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9419 /*decltype*/false, prec, pidk);
9420 }
9421
9422 /* Parse the `? expression : assignment-expression' part of a
9423 conditional-expression. The LOGICAL_OR_EXPR is the
9424 logical-or-expression that started the conditional-expression.
9425 Returns a representation of the entire conditional-expression.
9426
9427 This routine is used by cp_parser_assignment_expression.
9428
9429 ? expression : assignment-expression
9430
9431 GNU Extensions:
9432
9433 ? : assignment-expression */
9434
9435 static tree
9436 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9437 {
9438 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9439 cp_expr assignment_expr;
9440 struct cp_token *token;
9441 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9442
9443 /* Consume the `?' token. */
9444 cp_lexer_consume_token (parser->lexer);
9445 token = cp_lexer_peek_token (parser->lexer);
9446 if (cp_parser_allow_gnu_extensions_p (parser)
9447 && token->type == CPP_COLON)
9448 {
9449 pedwarn (token->location, OPT_Wpedantic,
9450 "ISO C++ does not allow ?: with omitted middle operand");
9451 /* Implicit true clause. */
9452 expr = NULL_TREE;
9453 c_inhibit_evaluation_warnings +=
9454 folded_logical_or_expr == truthvalue_true_node;
9455 warn_for_omitted_condop (token->location, logical_or_expr);
9456 }
9457 else
9458 {
9459 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9460 parser->colon_corrects_to_scope_p = false;
9461 /* Parse the expression. */
9462 c_inhibit_evaluation_warnings +=
9463 folded_logical_or_expr == truthvalue_false_node;
9464 expr = cp_parser_expression (parser);
9465 c_inhibit_evaluation_warnings +=
9466 ((folded_logical_or_expr == truthvalue_true_node)
9467 - (folded_logical_or_expr == truthvalue_false_node));
9468 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9469 }
9470
9471 /* The next token should be a `:'. */
9472 cp_parser_require (parser, CPP_COLON, RT_COLON);
9473 /* Parse the assignment-expression. */
9474 assignment_expr = cp_parser_assignment_expression (parser);
9475 c_inhibit_evaluation_warnings -=
9476 folded_logical_or_expr == truthvalue_true_node;
9477
9478 /* Make a location:
9479 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9480 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9481 with the caret at the "?", ranging from the start of
9482 the logical_or_expr to the end of the assignment_expr. */
9483 loc = make_location (loc,
9484 logical_or_expr.get_start (),
9485 assignment_expr.get_finish ());
9486
9487 /* Build the conditional-expression. */
9488 return build_x_conditional_expr (loc, logical_or_expr,
9489 expr,
9490 assignment_expr,
9491 tf_warning_or_error);
9492 }
9493
9494 /* Parse an assignment-expression.
9495
9496 assignment-expression:
9497 conditional-expression
9498 logical-or-expression assignment-operator assignment_expression
9499 throw-expression
9500
9501 CAST_P is true if this expression is the target of a cast.
9502 DECLTYPE_P is true if this expression is the operand of decltype.
9503
9504 Returns a representation for the expression. */
9505
9506 static cp_expr
9507 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9508 bool cast_p, bool decltype_p)
9509 {
9510 cp_expr expr;
9511
9512 /* If the next token is the `throw' keyword, then we're looking at
9513 a throw-expression. */
9514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9515 expr = cp_parser_throw_expression (parser);
9516 /* Otherwise, it must be that we are looking at a
9517 logical-or-expression. */
9518 else
9519 {
9520 /* Parse the binary expressions (logical-or-expression). */
9521 expr = cp_parser_binary_expression (parser, cast_p, false,
9522 decltype_p,
9523 PREC_NOT_OPERATOR, pidk);
9524 /* If the next token is a `?' then we're actually looking at a
9525 conditional-expression. */
9526 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9527 return cp_parser_question_colon_clause (parser, expr);
9528 else
9529 {
9530 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9531
9532 /* If it's an assignment-operator, we're using the second
9533 production. */
9534 enum tree_code assignment_operator
9535 = cp_parser_assignment_operator_opt (parser);
9536 if (assignment_operator != ERROR_MARK)
9537 {
9538 bool non_constant_p;
9539
9540 /* Parse the right-hand side of the assignment. */
9541 cp_expr rhs = cp_parser_initializer_clause (parser,
9542 &non_constant_p);
9543
9544 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9546
9547 /* An assignment may not appear in a
9548 constant-expression. */
9549 if (cp_parser_non_integral_constant_expression (parser,
9550 NIC_ASSIGNMENT))
9551 return error_mark_node;
9552 /* Build the assignment expression. Its default
9553 location:
9554 LHS = RHS
9555 ~~~~^~~~~
9556 is the location of the '=' token as the
9557 caret, ranging from the start of the lhs to the
9558 end of the rhs. */
9559 loc = make_location (loc,
9560 expr.get_start (),
9561 rhs.get_finish ());
9562 expr = build_x_modify_expr (loc, expr,
9563 assignment_operator,
9564 rhs,
9565 complain_flags (decltype_p));
9566 /* TODO: build_x_modify_expr doesn't honor the location,
9567 so we must set it here. */
9568 expr.set_location (loc);
9569 }
9570 }
9571 }
9572
9573 return expr;
9574 }
9575
9576 /* Parse an (optional) assignment-operator.
9577
9578 assignment-operator: one of
9579 = *= /= %= += -= >>= <<= &= ^= |=
9580
9581 GNU Extension:
9582
9583 assignment-operator: one of
9584 <?= >?=
9585
9586 If the next token is an assignment operator, the corresponding tree
9587 code is returned, and the token is consumed. For example, for
9588 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9589 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9590 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9591 operator, ERROR_MARK is returned. */
9592
9593 static enum tree_code
9594 cp_parser_assignment_operator_opt (cp_parser* parser)
9595 {
9596 enum tree_code op;
9597 cp_token *token;
9598
9599 /* Peek at the next token. */
9600 token = cp_lexer_peek_token (parser->lexer);
9601
9602 switch (token->type)
9603 {
9604 case CPP_EQ:
9605 op = NOP_EXPR;
9606 break;
9607
9608 case CPP_MULT_EQ:
9609 op = MULT_EXPR;
9610 break;
9611
9612 case CPP_DIV_EQ:
9613 op = TRUNC_DIV_EXPR;
9614 break;
9615
9616 case CPP_MOD_EQ:
9617 op = TRUNC_MOD_EXPR;
9618 break;
9619
9620 case CPP_PLUS_EQ:
9621 op = PLUS_EXPR;
9622 break;
9623
9624 case CPP_MINUS_EQ:
9625 op = MINUS_EXPR;
9626 break;
9627
9628 case CPP_RSHIFT_EQ:
9629 op = RSHIFT_EXPR;
9630 break;
9631
9632 case CPP_LSHIFT_EQ:
9633 op = LSHIFT_EXPR;
9634 break;
9635
9636 case CPP_AND_EQ:
9637 op = BIT_AND_EXPR;
9638 break;
9639
9640 case CPP_XOR_EQ:
9641 op = BIT_XOR_EXPR;
9642 break;
9643
9644 case CPP_OR_EQ:
9645 op = BIT_IOR_EXPR;
9646 break;
9647
9648 default:
9649 /* Nothing else is an assignment operator. */
9650 op = ERROR_MARK;
9651 }
9652
9653 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9654 if (op != ERROR_MARK
9655 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9656 op = ERROR_MARK;
9657
9658 /* If it was an assignment operator, consume it. */
9659 if (op != ERROR_MARK)
9660 cp_lexer_consume_token (parser->lexer);
9661
9662 return op;
9663 }
9664
9665 /* Parse an expression.
9666
9667 expression:
9668 assignment-expression
9669 expression , assignment-expression
9670
9671 CAST_P is true if this expression is the target of a cast.
9672 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9673 except possibly parenthesized or on the RHS of a comma (N3276).
9674
9675 Returns a representation of the expression. */
9676
9677 static cp_expr
9678 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9679 bool cast_p, bool decltype_p)
9680 {
9681 cp_expr expression = NULL_TREE;
9682 location_t loc = UNKNOWN_LOCATION;
9683
9684 while (true)
9685 {
9686 cp_expr assignment_expression;
9687
9688 /* Parse the next assignment-expression. */
9689 assignment_expression
9690 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9691
9692 /* We don't create a temporary for a call that is the immediate operand
9693 of decltype or on the RHS of a comma. But when we see a comma, we
9694 need to create a temporary for a call on the LHS. */
9695 if (decltype_p && !processing_template_decl
9696 && TREE_CODE (assignment_expression) == CALL_EXPR
9697 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9698 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9699 assignment_expression
9700 = build_cplus_new (TREE_TYPE (assignment_expression),
9701 assignment_expression, tf_warning_or_error);
9702
9703 /* If this is the first assignment-expression, we can just
9704 save it away. */
9705 if (!expression)
9706 expression = assignment_expression;
9707 else
9708 {
9709 /* Create a location with caret at the comma, ranging
9710 from the start of the LHS to the end of the RHS. */
9711 loc = make_location (loc,
9712 expression.get_start (),
9713 assignment_expression.get_finish ());
9714 expression = build_x_compound_expr (loc, expression,
9715 assignment_expression,
9716 complain_flags (decltype_p));
9717 expression.set_location (loc);
9718 }
9719 /* If the next token is not a comma, or we're in a fold-expression, then
9720 we are done with the expression. */
9721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9722 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9723 break;
9724 /* Consume the `,'. */
9725 loc = cp_lexer_peek_token (parser->lexer)->location;
9726 cp_lexer_consume_token (parser->lexer);
9727 /* A comma operator cannot appear in a constant-expression. */
9728 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9729 expression = error_mark_node;
9730 }
9731
9732 return expression;
9733 }
9734
9735 /* Parse a constant-expression.
9736
9737 constant-expression:
9738 conditional-expression
9739
9740 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9741 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9742 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9743 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9744 only parse a conditional-expression, otherwise parse an
9745 assignment-expression. See below for rationale. */
9746
9747 static cp_expr
9748 cp_parser_constant_expression (cp_parser* parser,
9749 bool allow_non_constant_p,
9750 bool *non_constant_p,
9751 bool strict_p)
9752 {
9753 bool saved_integral_constant_expression_p;
9754 bool saved_allow_non_integral_constant_expression_p;
9755 bool saved_non_integral_constant_expression_p;
9756 cp_expr expression;
9757
9758 /* It might seem that we could simply parse the
9759 conditional-expression, and then check to see if it were
9760 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9761 one that the compiler can figure out is constant, possibly after
9762 doing some simplifications or optimizations. The standard has a
9763 precise definition of constant-expression, and we must honor
9764 that, even though it is somewhat more restrictive.
9765
9766 For example:
9767
9768 int i[(2, 3)];
9769
9770 is not a legal declaration, because `(2, 3)' is not a
9771 constant-expression. The `,' operator is forbidden in a
9772 constant-expression. However, GCC's constant-folding machinery
9773 will fold this operation to an INTEGER_CST for `3'. */
9774
9775 /* Save the old settings. */
9776 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9777 saved_allow_non_integral_constant_expression_p
9778 = parser->allow_non_integral_constant_expression_p;
9779 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9780 /* We are now parsing a constant-expression. */
9781 parser->integral_constant_expression_p = true;
9782 parser->allow_non_integral_constant_expression_p
9783 = (allow_non_constant_p || cxx_dialect >= cxx11);
9784 parser->non_integral_constant_expression_p = false;
9785 /* Although the grammar says "conditional-expression", when not STRICT_P,
9786 we parse an "assignment-expression", which also permits
9787 "throw-expression" and the use of assignment operators. In the case
9788 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9789 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9790 actually essential that we look for an assignment-expression.
9791 For example, cp_parser_initializer_clauses uses this function to
9792 determine whether a particular assignment-expression is in fact
9793 constant. */
9794 if (strict_p)
9795 {
9796 /* Parse the binary expressions (logical-or-expression). */
9797 expression = cp_parser_binary_expression (parser, false, false, false,
9798 PREC_NOT_OPERATOR, NULL);
9799 /* If the next token is a `?' then we're actually looking at
9800 a conditional-expression; otherwise we're done. */
9801 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9802 expression = cp_parser_question_colon_clause (parser, expression);
9803 }
9804 else
9805 expression = cp_parser_assignment_expression (parser);
9806 /* Restore the old settings. */
9807 parser->integral_constant_expression_p
9808 = saved_integral_constant_expression_p;
9809 parser->allow_non_integral_constant_expression_p
9810 = saved_allow_non_integral_constant_expression_p;
9811 if (cxx_dialect >= cxx11)
9812 {
9813 /* Require an rvalue constant expression here; that's what our
9814 callers expect. Reference constant expressions are handled
9815 separately in e.g. cp_parser_template_argument. */
9816 tree decay = expression;
9817 if (TREE_TYPE (expression)
9818 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9819 decay = build_address (expression);
9820 bool is_const = potential_rvalue_constant_expression (decay);
9821 parser->non_integral_constant_expression_p = !is_const;
9822 if (!is_const && !allow_non_constant_p)
9823 require_potential_rvalue_constant_expression (decay);
9824 }
9825 if (allow_non_constant_p)
9826 *non_constant_p = parser->non_integral_constant_expression_p;
9827 parser->non_integral_constant_expression_p
9828 = saved_non_integral_constant_expression_p;
9829
9830 return expression;
9831 }
9832
9833 /* Parse __builtin_offsetof.
9834
9835 offsetof-expression:
9836 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9837
9838 offsetof-member-designator:
9839 id-expression
9840 | offsetof-member-designator "." id-expression
9841 | offsetof-member-designator "[" expression "]"
9842 | offsetof-member-designator "->" id-expression */
9843
9844 static cp_expr
9845 cp_parser_builtin_offsetof (cp_parser *parser)
9846 {
9847 int save_ice_p, save_non_ice_p;
9848 tree type;
9849 cp_expr expr;
9850 cp_id_kind dummy;
9851 cp_token *token;
9852 location_t finish_loc;
9853
9854 /* We're about to accept non-integral-constant things, but will
9855 definitely yield an integral constant expression. Save and
9856 restore these values around our local parsing. */
9857 save_ice_p = parser->integral_constant_expression_p;
9858 save_non_ice_p = parser->non_integral_constant_expression_p;
9859
9860 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9861
9862 /* Consume the "__builtin_offsetof" token. */
9863 cp_lexer_consume_token (parser->lexer);
9864 /* Consume the opening `('. */
9865 matching_parens parens;
9866 parens.require_open (parser);
9867 /* Parse the type-id. */
9868 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9869 type = cp_parser_type_id (parser);
9870 /* Look for the `,'. */
9871 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9872 token = cp_lexer_peek_token (parser->lexer);
9873
9874 /* Build the (type *)null that begins the traditional offsetof macro. */
9875 tree object_ptr
9876 = build_static_cast (build_pointer_type (type), null_pointer_node,
9877 tf_warning_or_error);
9878
9879 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9880 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9881 true, &dummy, token->location);
9882 while (true)
9883 {
9884 token = cp_lexer_peek_token (parser->lexer);
9885 switch (token->type)
9886 {
9887 case CPP_OPEN_SQUARE:
9888 /* offsetof-member-designator "[" expression "]" */
9889 expr = cp_parser_postfix_open_square_expression (parser, expr,
9890 true, false);
9891 break;
9892
9893 case CPP_DEREF:
9894 /* offsetof-member-designator "->" identifier */
9895 expr = grok_array_decl (token->location, expr,
9896 integer_zero_node, false);
9897 /* FALLTHRU */
9898
9899 case CPP_DOT:
9900 /* offsetof-member-designator "." identifier */
9901 cp_lexer_consume_token (parser->lexer);
9902 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9903 expr, true, &dummy,
9904 token->location);
9905 break;
9906
9907 case CPP_CLOSE_PAREN:
9908 /* Consume the ")" token. */
9909 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9910 cp_lexer_consume_token (parser->lexer);
9911 goto success;
9912
9913 default:
9914 /* Error. We know the following require will fail, but
9915 that gives the proper error message. */
9916 parens.require_close (parser);
9917 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9918 expr = error_mark_node;
9919 goto failure;
9920 }
9921 }
9922
9923 success:
9924 /* Make a location of the form:
9925 __builtin_offsetof (struct s, f)
9926 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9927 with caret at the type-id, ranging from the start of the
9928 "_builtin_offsetof" token to the close paren. */
9929 loc = make_location (loc, start_loc, finish_loc);
9930 /* The result will be an INTEGER_CST, so we need to explicitly
9931 preserve the location. */
9932 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9933
9934 failure:
9935 parser->integral_constant_expression_p = save_ice_p;
9936 parser->non_integral_constant_expression_p = save_non_ice_p;
9937
9938 return expr;
9939 }
9940
9941 /* Parse a trait expression.
9942
9943 Returns a representation of the expression, the underlying type
9944 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9945
9946 static tree
9947 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9948 {
9949 cp_trait_kind kind;
9950 tree type1, type2 = NULL_TREE;
9951 bool binary = false;
9952 bool variadic = false;
9953
9954 switch (keyword)
9955 {
9956 case RID_HAS_NOTHROW_ASSIGN:
9957 kind = CPTK_HAS_NOTHROW_ASSIGN;
9958 break;
9959 case RID_HAS_NOTHROW_CONSTRUCTOR:
9960 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9961 break;
9962 case RID_HAS_NOTHROW_COPY:
9963 kind = CPTK_HAS_NOTHROW_COPY;
9964 break;
9965 case RID_HAS_TRIVIAL_ASSIGN:
9966 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9967 break;
9968 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9969 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9970 break;
9971 case RID_HAS_TRIVIAL_COPY:
9972 kind = CPTK_HAS_TRIVIAL_COPY;
9973 break;
9974 case RID_HAS_TRIVIAL_DESTRUCTOR:
9975 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9976 break;
9977 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9978 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9979 break;
9980 case RID_HAS_VIRTUAL_DESTRUCTOR:
9981 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9982 break;
9983 case RID_IS_ABSTRACT:
9984 kind = CPTK_IS_ABSTRACT;
9985 break;
9986 case RID_IS_AGGREGATE:
9987 kind = CPTK_IS_AGGREGATE;
9988 break;
9989 case RID_IS_BASE_OF:
9990 kind = CPTK_IS_BASE_OF;
9991 binary = true;
9992 break;
9993 case RID_IS_CLASS:
9994 kind = CPTK_IS_CLASS;
9995 break;
9996 case RID_IS_EMPTY:
9997 kind = CPTK_IS_EMPTY;
9998 break;
9999 case RID_IS_ENUM:
10000 kind = CPTK_IS_ENUM;
10001 break;
10002 case RID_IS_FINAL:
10003 kind = CPTK_IS_FINAL;
10004 break;
10005 case RID_IS_LITERAL_TYPE:
10006 kind = CPTK_IS_LITERAL_TYPE;
10007 break;
10008 case RID_IS_POD:
10009 kind = CPTK_IS_POD;
10010 break;
10011 case RID_IS_POLYMORPHIC:
10012 kind = CPTK_IS_POLYMORPHIC;
10013 break;
10014 case RID_IS_SAME_AS:
10015 kind = CPTK_IS_SAME_AS;
10016 binary = true;
10017 break;
10018 case RID_IS_STD_LAYOUT:
10019 kind = CPTK_IS_STD_LAYOUT;
10020 break;
10021 case RID_IS_TRIVIAL:
10022 kind = CPTK_IS_TRIVIAL;
10023 break;
10024 case RID_IS_TRIVIALLY_ASSIGNABLE:
10025 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10026 binary = true;
10027 break;
10028 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10029 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10030 variadic = true;
10031 break;
10032 case RID_IS_TRIVIALLY_COPYABLE:
10033 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10034 break;
10035 case RID_IS_UNION:
10036 kind = CPTK_IS_UNION;
10037 break;
10038 case RID_UNDERLYING_TYPE:
10039 kind = CPTK_UNDERLYING_TYPE;
10040 break;
10041 case RID_BASES:
10042 kind = CPTK_BASES;
10043 break;
10044 case RID_DIRECT_BASES:
10045 kind = CPTK_DIRECT_BASES;
10046 break;
10047 case RID_IS_ASSIGNABLE:
10048 kind = CPTK_IS_ASSIGNABLE;
10049 binary = true;
10050 break;
10051 case RID_IS_CONSTRUCTIBLE:
10052 kind = CPTK_IS_CONSTRUCTIBLE;
10053 variadic = true;
10054 break;
10055 default:
10056 gcc_unreachable ();
10057 }
10058
10059 /* Consume the token. */
10060 cp_lexer_consume_token (parser->lexer);
10061
10062 matching_parens parens;
10063 parens.require_open (parser);
10064
10065 {
10066 type_id_in_expr_sentinel s (parser);
10067 type1 = cp_parser_type_id (parser);
10068 }
10069
10070 if (type1 == error_mark_node)
10071 return error_mark_node;
10072
10073 if (binary)
10074 {
10075 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10076
10077 {
10078 type_id_in_expr_sentinel s (parser);
10079 type2 = cp_parser_type_id (parser);
10080 }
10081
10082 if (type2 == error_mark_node)
10083 return error_mark_node;
10084 }
10085 else if (variadic)
10086 {
10087 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10088 {
10089 cp_lexer_consume_token (parser->lexer);
10090 tree elt = cp_parser_type_id (parser);
10091 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10092 {
10093 cp_lexer_consume_token (parser->lexer);
10094 elt = make_pack_expansion (elt);
10095 }
10096 if (elt == error_mark_node)
10097 return error_mark_node;
10098 type2 = tree_cons (NULL_TREE, elt, type2);
10099 }
10100 }
10101
10102 parens.require_close (parser);
10103
10104 /* Complete the trait expression, which may mean either processing
10105 the trait expr now or saving it for template instantiation. */
10106 switch (kind)
10107 {
10108 case CPTK_UNDERLYING_TYPE:
10109 return finish_underlying_type (type1);
10110 case CPTK_BASES:
10111 return finish_bases (type1, false);
10112 case CPTK_DIRECT_BASES:
10113 return finish_bases (type1, true);
10114 default:
10115 return finish_trait_expr (kind, type1, type2);
10116 }
10117 }
10118
10119 /* Parse a lambda expression.
10120
10121 lambda-expression:
10122 lambda-introducer lambda-declarator [opt] compound-statement
10123
10124 Returns a representation of the expression. */
10125
10126 static cp_expr
10127 cp_parser_lambda_expression (cp_parser* parser)
10128 {
10129 tree lambda_expr = build_lambda_expr ();
10130 tree type;
10131 bool ok = true;
10132 cp_token *token = cp_lexer_peek_token (parser->lexer);
10133 cp_token_position start = 0;
10134
10135 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10136
10137 if (cp_unevaluated_operand)
10138 {
10139 if (!token->error_reported)
10140 {
10141 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10142 "lambda-expression in unevaluated context");
10143 token->error_reported = true;
10144 }
10145 ok = false;
10146 }
10147 else if (parser->in_template_argument_list_p)
10148 {
10149 if (!token->error_reported)
10150 {
10151 error_at (token->location, "lambda-expression in template-argument");
10152 token->error_reported = true;
10153 }
10154 ok = false;
10155 }
10156
10157 /* We may be in the middle of deferred access check. Disable
10158 it now. */
10159 push_deferring_access_checks (dk_no_deferred);
10160
10161 cp_parser_lambda_introducer (parser, lambda_expr);
10162
10163 type = begin_lambda_type (lambda_expr);
10164 if (type == error_mark_node)
10165 return error_mark_node;
10166
10167 record_lambda_scope (lambda_expr);
10168
10169 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10170 determine_visibility (TYPE_NAME (type));
10171
10172 /* Now that we've started the type, add the capture fields for any
10173 explicit captures. */
10174 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10175
10176 {
10177 /* Inside the class, surrounding template-parameter-lists do not apply. */
10178 unsigned int saved_num_template_parameter_lists
10179 = parser->num_template_parameter_lists;
10180 unsigned char in_statement = parser->in_statement;
10181 bool in_switch_statement_p = parser->in_switch_statement_p;
10182 bool fully_implicit_function_template_p
10183 = parser->fully_implicit_function_template_p;
10184 tree implicit_template_parms = parser->implicit_template_parms;
10185 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10186 bool auto_is_implicit_function_template_parm_p
10187 = parser->auto_is_implicit_function_template_parm_p;
10188
10189 parser->num_template_parameter_lists = 0;
10190 parser->in_statement = 0;
10191 parser->in_switch_statement_p = false;
10192 parser->fully_implicit_function_template_p = false;
10193 parser->implicit_template_parms = 0;
10194 parser->implicit_template_scope = 0;
10195 parser->auto_is_implicit_function_template_parm_p = false;
10196
10197 /* By virtue of defining a local class, a lambda expression has access to
10198 the private variables of enclosing classes. */
10199
10200 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10201
10202 if (ok && cp_parser_error_occurred (parser))
10203 ok = false;
10204
10205 if (ok)
10206 {
10207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10208 && cp_parser_start_tentative_firewall (parser))
10209 start = token;
10210 cp_parser_lambda_body (parser, lambda_expr);
10211 }
10212 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10213 {
10214 if (cp_parser_skip_to_closing_brace (parser))
10215 cp_lexer_consume_token (parser->lexer);
10216 }
10217
10218 /* The capture list was built up in reverse order; fix that now. */
10219 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10220 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10221
10222 if (ok)
10223 maybe_add_lambda_conv_op (type);
10224
10225 type = finish_struct (type, /*attributes=*/NULL_TREE);
10226
10227 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10228 parser->in_statement = in_statement;
10229 parser->in_switch_statement_p = in_switch_statement_p;
10230 parser->fully_implicit_function_template_p
10231 = fully_implicit_function_template_p;
10232 parser->implicit_template_parms = implicit_template_parms;
10233 parser->implicit_template_scope = implicit_template_scope;
10234 parser->auto_is_implicit_function_template_parm_p
10235 = auto_is_implicit_function_template_parm_p;
10236 }
10237
10238 /* This field is only used during parsing of the lambda. */
10239 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10240
10241 /* This lambda shouldn't have any proxies left at this point. */
10242 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10243 /* And now that we're done, push proxies for an enclosing lambda. */
10244 insert_pending_capture_proxies ();
10245
10246 if (ok)
10247 lambda_expr = build_lambda_object (lambda_expr);
10248 else
10249 lambda_expr = error_mark_node;
10250
10251 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10252
10253 pop_deferring_access_checks ();
10254
10255 return lambda_expr;
10256 }
10257
10258 /* Parse the beginning of a lambda expression.
10259
10260 lambda-introducer:
10261 [ lambda-capture [opt] ]
10262
10263 LAMBDA_EXPR is the current representation of the lambda expression. */
10264
10265 static void
10266 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10267 {
10268 /* Need commas after the first capture. */
10269 bool first = true;
10270
10271 /* Eat the leading `['. */
10272 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10273
10274 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10275 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10276 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10277 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10278 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10279 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10280
10281 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10282 {
10283 cp_lexer_consume_token (parser->lexer);
10284 first = false;
10285 }
10286
10287 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10288 {
10289 cp_token* capture_token;
10290 tree capture_id;
10291 tree capture_init_expr;
10292 cp_id_kind idk = CP_ID_KIND_NONE;
10293 bool explicit_init_p = false;
10294
10295 enum capture_kind_type
10296 {
10297 BY_COPY,
10298 BY_REFERENCE
10299 };
10300 enum capture_kind_type capture_kind = BY_COPY;
10301
10302 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10303 {
10304 error ("expected end of capture-list");
10305 return;
10306 }
10307
10308 if (first)
10309 first = false;
10310 else
10311 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10312
10313 /* Possibly capture `this'. */
10314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10315 {
10316 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10317 if (cxx_dialect < cxx2a
10318 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10319 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10320 "with by-copy capture default");
10321 cp_lexer_consume_token (parser->lexer);
10322 add_capture (lambda_expr,
10323 /*id=*/this_identifier,
10324 /*initializer=*/finish_this_expr (),
10325 /*by_reference_p=*/true,
10326 explicit_init_p);
10327 continue;
10328 }
10329
10330 /* Possibly capture `*this'. */
10331 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10332 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10333 {
10334 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10335 if (cxx_dialect < cxx17)
10336 pedwarn (loc, 0, "%<*this%> capture only available with "
10337 "-std=c++17 or -std=gnu++17");
10338 cp_lexer_consume_token (parser->lexer);
10339 cp_lexer_consume_token (parser->lexer);
10340 add_capture (lambda_expr,
10341 /*id=*/this_identifier,
10342 /*initializer=*/finish_this_expr (),
10343 /*by_reference_p=*/false,
10344 explicit_init_p);
10345 continue;
10346 }
10347
10348 /* Remember whether we want to capture as a reference or not. */
10349 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10350 {
10351 capture_kind = BY_REFERENCE;
10352 cp_lexer_consume_token (parser->lexer);
10353 }
10354
10355 /* Get the identifier. */
10356 capture_token = cp_lexer_peek_token (parser->lexer);
10357 capture_id = cp_parser_identifier (parser);
10358
10359 if (capture_id == error_mark_node)
10360 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10361 delimiters, but I modified this to stop on unnested ']' as well. It
10362 was already changed to stop on unnested '}', so the
10363 "closing_parenthesis" name is no more misleading with my change. */
10364 {
10365 cp_parser_skip_to_closing_parenthesis (parser,
10366 /*recovering=*/true,
10367 /*or_comma=*/true,
10368 /*consume_paren=*/true);
10369 break;
10370 }
10371
10372 /* Find the initializer for this capture. */
10373 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10374 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10375 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10376 {
10377 bool direct, non_constant;
10378 /* An explicit initializer exists. */
10379 if (cxx_dialect < cxx14)
10380 pedwarn (input_location, 0,
10381 "lambda capture initializers "
10382 "only available with -std=c++14 or -std=gnu++14");
10383 capture_init_expr = cp_parser_initializer (parser, &direct,
10384 &non_constant);
10385 explicit_init_p = true;
10386 if (capture_init_expr == NULL_TREE)
10387 {
10388 error ("empty initializer for lambda init-capture");
10389 capture_init_expr = error_mark_node;
10390 }
10391 }
10392 else
10393 {
10394 const char* error_msg;
10395
10396 /* Turn the identifier into an id-expression. */
10397 capture_init_expr
10398 = cp_parser_lookup_name_simple (parser, capture_id,
10399 capture_token->location);
10400
10401 if (capture_init_expr == error_mark_node)
10402 {
10403 unqualified_name_lookup_error (capture_id);
10404 continue;
10405 }
10406 else if (DECL_P (capture_init_expr)
10407 && (!VAR_P (capture_init_expr)
10408 && TREE_CODE (capture_init_expr) != PARM_DECL))
10409 {
10410 error_at (capture_token->location,
10411 "capture of non-variable %qD ",
10412 capture_init_expr);
10413 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10414 "%q#D declared here", capture_init_expr);
10415 continue;
10416 }
10417 if (VAR_P (capture_init_expr)
10418 && decl_storage_duration (capture_init_expr) != dk_auto)
10419 {
10420 if (pedwarn (capture_token->location, 0, "capture of variable "
10421 "%qD with non-automatic storage duration",
10422 capture_init_expr))
10423 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10424 "%q#D declared here", capture_init_expr);
10425 continue;
10426 }
10427
10428 capture_init_expr
10429 = finish_id_expression
10430 (capture_id,
10431 capture_init_expr,
10432 parser->scope,
10433 &idk,
10434 /*integral_constant_expression_p=*/false,
10435 /*allow_non_integral_constant_expression_p=*/false,
10436 /*non_integral_constant_expression_p=*/NULL,
10437 /*template_p=*/false,
10438 /*done=*/true,
10439 /*address_p=*/false,
10440 /*template_arg_p=*/false,
10441 &error_msg,
10442 capture_token->location);
10443
10444 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10445 {
10446 cp_lexer_consume_token (parser->lexer);
10447 capture_init_expr = make_pack_expansion (capture_init_expr);
10448 }
10449 else
10450 check_for_bare_parameter_packs (capture_init_expr);
10451 }
10452
10453 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10454 && !explicit_init_p)
10455 {
10456 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10457 && capture_kind == BY_COPY)
10458 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10459 "of %qD redundant with by-copy capture default",
10460 capture_id);
10461 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10462 && capture_kind == BY_REFERENCE)
10463 pedwarn (capture_token->location, 0, "explicit by-reference "
10464 "capture of %qD redundant with by-reference capture "
10465 "default", capture_id);
10466 }
10467
10468 add_capture (lambda_expr,
10469 capture_id,
10470 capture_init_expr,
10471 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10472 explicit_init_p);
10473 }
10474
10475 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10476 }
10477
10478 /* Parse the (optional) middle of a lambda expression.
10479
10480 lambda-declarator:
10481 < template-parameter-list [opt] >
10482 ( parameter-declaration-clause [opt] )
10483 attribute-specifier [opt]
10484 decl-specifier-seq [opt]
10485 exception-specification [opt]
10486 lambda-return-type-clause [opt]
10487
10488 LAMBDA_EXPR is the current representation of the lambda expression. */
10489
10490 static bool
10491 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10492 {
10493 /* 5.1.1.4 of the standard says:
10494 If a lambda-expression does not include a lambda-declarator, it is as if
10495 the lambda-declarator were ().
10496 This means an empty parameter list, no attributes, and no exception
10497 specification. */
10498 tree param_list = void_list_node;
10499 tree attributes = NULL_TREE;
10500 tree exception_spec = NULL_TREE;
10501 tree template_param_list = NULL_TREE;
10502 tree tx_qual = NULL_TREE;
10503 tree return_type = NULL_TREE;
10504 cp_decl_specifier_seq lambda_specs;
10505 clear_decl_specs (&lambda_specs);
10506
10507 /* The template-parameter-list is optional, but must begin with
10508 an opening angle if present. */
10509 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10510 {
10511 if (cxx_dialect < cxx14)
10512 pedwarn (parser->lexer->next_token->location, 0,
10513 "lambda templates are only available with "
10514 "-std=c++14 or -std=gnu++14");
10515 else if (cxx_dialect < cxx2a)
10516 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10517 "lambda templates are only available with "
10518 "-std=c++2a or -std=gnu++2a");
10519
10520 cp_lexer_consume_token (parser->lexer);
10521
10522 template_param_list = cp_parser_template_parameter_list (parser);
10523
10524 cp_parser_skip_to_end_of_template_parameter_list (parser);
10525
10526 /* We just processed one more parameter list. */
10527 ++parser->num_template_parameter_lists;
10528 }
10529
10530 /* The parameter-declaration-clause is optional (unless
10531 template-parameter-list was given), but must begin with an
10532 opening parenthesis if present. */
10533 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10534 {
10535 matching_parens parens;
10536 parens.consume_open (parser);
10537
10538 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10539
10540 /* Parse parameters. */
10541 param_list = cp_parser_parameter_declaration_clause (parser);
10542
10543 /* Default arguments shall not be specified in the
10544 parameter-declaration-clause of a lambda-declarator. */
10545 if (cxx_dialect < cxx14)
10546 for (tree t = param_list; t; t = TREE_CHAIN (t))
10547 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10548 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10549 "default argument specified for lambda parameter");
10550
10551 parens.require_close (parser);
10552
10553 attributes = cp_parser_attributes_opt (parser);
10554
10555 /* In the decl-specifier-seq of the lambda-declarator, each
10556 decl-specifier shall either be mutable or constexpr. */
10557 int declares_class_or_enum;
10558 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10559 cp_parser_decl_specifier_seq (parser,
10560 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10561 &lambda_specs, &declares_class_or_enum);
10562 if (lambda_specs.storage_class == sc_mutable)
10563 {
10564 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10565 if (lambda_specs.conflicting_specifiers_p)
10566 error_at (lambda_specs.locations[ds_storage_class],
10567 "duplicate %<mutable%>");
10568 }
10569
10570 tx_qual = cp_parser_tx_qualifier_opt (parser);
10571
10572 /* Parse optional exception specification. */
10573 exception_spec = cp_parser_exception_specification_opt (parser);
10574
10575 /* Parse optional trailing return type. */
10576 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10577 {
10578 cp_lexer_consume_token (parser->lexer);
10579 return_type = cp_parser_trailing_type_id (parser);
10580 }
10581
10582 /* The function parameters must be in scope all the way until after the
10583 trailing-return-type in case of decltype. */
10584 pop_bindings_and_leave_scope ();
10585 }
10586 else if (template_param_list != NULL_TREE) // generate diagnostic
10587 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10588
10589 /* Create the function call operator.
10590
10591 Messing with declarators like this is no uglier than building up the
10592 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10593 other code. */
10594 {
10595 cp_decl_specifier_seq return_type_specs;
10596 cp_declarator* declarator;
10597 tree fco;
10598 int quals;
10599 void *p;
10600
10601 clear_decl_specs (&return_type_specs);
10602 if (return_type)
10603 return_type_specs.type = return_type;
10604 else
10605 /* Maybe we will deduce the return type later. */
10606 return_type_specs.type = make_auto ();
10607
10608 if (lambda_specs.locations[ds_constexpr])
10609 {
10610 if (cxx_dialect >= cxx17)
10611 return_type_specs.locations[ds_constexpr]
10612 = lambda_specs.locations[ds_constexpr];
10613 else
10614 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10615 "lambda only available with -std=c++17 or -std=gnu++17");
10616 }
10617
10618 p = obstack_alloc (&declarator_obstack, 0);
10619
10620 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10621
10622 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10623 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10624 declarator = make_call_declarator (declarator, param_list, quals,
10625 VIRT_SPEC_UNSPECIFIED,
10626 REF_QUAL_NONE,
10627 tx_qual,
10628 exception_spec,
10629 /*late_return_type=*/NULL_TREE,
10630 /*requires_clause*/NULL_TREE);
10631 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10632
10633 fco = grokmethod (&return_type_specs,
10634 declarator,
10635 attributes);
10636 if (fco != error_mark_node)
10637 {
10638 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10639 DECL_ARTIFICIAL (fco) = 1;
10640 /* Give the object parameter a different name. */
10641 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10642 if (return_type)
10643 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10644 }
10645 if (template_param_list)
10646 {
10647 fco = finish_member_template_decl (fco);
10648 finish_template_decl (template_param_list);
10649 --parser->num_template_parameter_lists;
10650 }
10651 else if (parser->fully_implicit_function_template_p)
10652 fco = finish_fully_implicit_template (parser, fco);
10653
10654 finish_member_declaration (fco);
10655
10656 obstack_free (&declarator_obstack, p);
10657
10658 return (fco != error_mark_node);
10659 }
10660 }
10661
10662 /* Parse the body of a lambda expression, which is simply
10663
10664 compound-statement
10665
10666 but which requires special handling.
10667 LAMBDA_EXPR is the current representation of the lambda expression. */
10668
10669 static void
10670 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10671 {
10672 bool nested = (current_function_decl != NULL_TREE);
10673 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10674 bool in_function_body = parser->in_function_body;
10675 if (nested)
10676 push_function_context ();
10677 else
10678 /* Still increment function_depth so that we don't GC in the
10679 middle of an expression. */
10680 ++function_depth;
10681 vec<tree> omp_privatization_save;
10682 save_omp_privatization_clauses (omp_privatization_save);
10683 /* Clear this in case we're in the middle of a default argument. */
10684 parser->local_variables_forbidden_p = false;
10685 parser->in_function_body = true;
10686
10687 /* Finish the function call operator
10688 - class_specifier
10689 + late_parsing_for_member
10690 + function_definition_after_declarator
10691 + ctor_initializer_opt_and_function_body */
10692 {
10693 local_specialization_stack s (lss_copy);
10694
10695 tree fco = lambda_function (lambda_expr);
10696 tree body = start_lambda_function (fco, lambda_expr);
10697 bool done = false;
10698 tree compound_stmt;
10699
10700 matching_braces braces;
10701 if (!braces.require_open (parser))
10702 goto out;
10703
10704 compound_stmt = begin_compound_stmt (0);
10705
10706 /* 5.1.1.4 of the standard says:
10707 If a lambda-expression does not include a trailing-return-type, it
10708 is as if the trailing-return-type denotes the following type:
10709 * if the compound-statement is of the form
10710 { return attribute-specifier [opt] expression ; }
10711 the type of the returned expression after lvalue-to-rvalue
10712 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10713 (_conv.array_ 4.2), and function-to-pointer conversion
10714 (_conv.func_ 4.3);
10715 * otherwise, void. */
10716
10717 /* In a lambda that has neither a lambda-return-type-clause
10718 nor a deducible form, errors should be reported for return statements
10719 in the body. Since we used void as the placeholder return type, parsing
10720 the body as usual will give such desired behavior. */
10721 if (is_auto (TREE_TYPE (TREE_TYPE (fco)))
10722 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10723 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10724 {
10725 tree expr = NULL_TREE;
10726 cp_id_kind idk = CP_ID_KIND_NONE;
10727
10728 /* Parse tentatively in case there's more after the initial return
10729 statement. */
10730 cp_parser_parse_tentatively (parser);
10731
10732 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10733
10734 expr = cp_parser_expression (parser, &idk);
10735
10736 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10737 braces.require_close (parser);
10738
10739 if (cp_parser_parse_definitely (parser))
10740 {
10741 if (!processing_template_decl)
10742 {
10743 tree type = lambda_return_type (expr);
10744 apply_deduced_return_type (fco, type);
10745 if (type == error_mark_node)
10746 expr = error_mark_node;
10747 }
10748
10749 /* Will get error here if type not deduced yet. */
10750 finish_return_stmt (expr);
10751
10752 done = true;
10753 }
10754 }
10755
10756 if (!done)
10757 {
10758 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10759 cp_parser_label_declaration (parser);
10760 cp_parser_statement_seq_opt (parser, NULL_TREE);
10761 braces.require_close (parser);
10762 }
10763
10764 finish_compound_stmt (compound_stmt);
10765
10766 out:
10767 finish_lambda_function (body);
10768 }
10769
10770 restore_omp_privatization_clauses (omp_privatization_save);
10771 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10772 parser->in_function_body = in_function_body;
10773 if (nested)
10774 pop_function_context();
10775 else
10776 --function_depth;
10777 }
10778
10779 /* Statements [gram.stmt.stmt] */
10780
10781 /* Parse a statement.
10782
10783 statement:
10784 labeled-statement
10785 expression-statement
10786 compound-statement
10787 selection-statement
10788 iteration-statement
10789 jump-statement
10790 declaration-statement
10791 try-block
10792
10793 C++11:
10794
10795 statement:
10796 labeled-statement
10797 attribute-specifier-seq (opt) expression-statement
10798 attribute-specifier-seq (opt) compound-statement
10799 attribute-specifier-seq (opt) selection-statement
10800 attribute-specifier-seq (opt) iteration-statement
10801 attribute-specifier-seq (opt) jump-statement
10802 declaration-statement
10803 attribute-specifier-seq (opt) try-block
10804
10805 init-statement:
10806 expression-statement
10807 simple-declaration
10808
10809 TM Extension:
10810
10811 statement:
10812 atomic-statement
10813
10814 IN_COMPOUND is true when the statement is nested inside a
10815 cp_parser_compound_statement; this matters for certain pragmas.
10816
10817 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10818 is a (possibly labeled) if statement which is not enclosed in braces
10819 and has an else clause. This is used to implement -Wparentheses.
10820
10821 CHAIN is a vector of if-else-if conditions. */
10822
10823 static void
10824 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10825 bool in_compound, bool *if_p, vec<tree> *chain,
10826 location_t *loc_after_labels)
10827 {
10828 tree statement, std_attrs = NULL_TREE;
10829 cp_token *token;
10830 location_t statement_location, attrs_location;
10831
10832 restart:
10833 if (if_p != NULL)
10834 *if_p = false;
10835 /* There is no statement yet. */
10836 statement = NULL_TREE;
10837
10838 saved_token_sentinel saved_tokens (parser->lexer);
10839 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10840 if (c_dialect_objc ())
10841 /* In obj-c++, seeing '[[' might be the either the beginning of
10842 c++11 attributes, or a nested objc-message-expression. So
10843 let's parse the c++11 attributes tentatively. */
10844 cp_parser_parse_tentatively (parser);
10845 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10846 if (c_dialect_objc ())
10847 {
10848 if (!cp_parser_parse_definitely (parser))
10849 std_attrs = NULL_TREE;
10850 }
10851
10852 /* Peek at the next token. */
10853 token = cp_lexer_peek_token (parser->lexer);
10854 /* Remember the location of the first token in the statement. */
10855 statement_location = token->location;
10856 /* If this is a keyword, then that will often determine what kind of
10857 statement we have. */
10858 if (token->type == CPP_KEYWORD)
10859 {
10860 enum rid keyword = token->keyword;
10861
10862 switch (keyword)
10863 {
10864 case RID_CASE:
10865 case RID_DEFAULT:
10866 /* Looks like a labeled-statement with a case label.
10867 Parse the label, and then use tail recursion to parse
10868 the statement. */
10869 cp_parser_label_for_labeled_statement (parser, std_attrs);
10870 in_compound = false;
10871 goto restart;
10872
10873 case RID_IF:
10874 case RID_SWITCH:
10875 statement = cp_parser_selection_statement (parser, if_p, chain);
10876 break;
10877
10878 case RID_WHILE:
10879 case RID_DO:
10880 case RID_FOR:
10881 statement = cp_parser_iteration_statement (parser, if_p, false);
10882 break;
10883
10884 case RID_CILK_FOR:
10885 if (!flag_cilkplus)
10886 {
10887 error_at (cp_lexer_peek_token (parser->lexer)->location,
10888 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10889 cp_lexer_consume_token (parser->lexer);
10890 statement = error_mark_node;
10891 }
10892 else
10893 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10894 break;
10895
10896 case RID_BREAK:
10897 case RID_CONTINUE:
10898 case RID_RETURN:
10899 case RID_GOTO:
10900 statement = cp_parser_jump_statement (parser);
10901 break;
10902
10903 case RID_CILK_SYNC:
10904 cp_lexer_consume_token (parser->lexer);
10905 if (flag_cilkplus)
10906 {
10907 tree sync_expr = build_cilk_sync ();
10908 SET_EXPR_LOCATION (sync_expr,
10909 token->location);
10910 statement = finish_expr_stmt (sync_expr);
10911 }
10912 else
10913 {
10914 error_at (token->location, "-fcilkplus must be enabled to use"
10915 " %<_Cilk_sync%>");
10916 statement = error_mark_node;
10917 }
10918 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10919 break;
10920
10921 /* Objective-C++ exception-handling constructs. */
10922 case RID_AT_TRY:
10923 case RID_AT_CATCH:
10924 case RID_AT_FINALLY:
10925 case RID_AT_SYNCHRONIZED:
10926 case RID_AT_THROW:
10927 statement = cp_parser_objc_statement (parser);
10928 break;
10929
10930 case RID_TRY:
10931 statement = cp_parser_try_block (parser);
10932 break;
10933
10934 case RID_NAMESPACE:
10935 /* This must be a namespace alias definition. */
10936 cp_parser_declaration_statement (parser);
10937 return;
10938
10939 case RID_TRANSACTION_ATOMIC:
10940 case RID_TRANSACTION_RELAXED:
10941 case RID_SYNCHRONIZED:
10942 case RID_ATOMIC_NOEXCEPT:
10943 case RID_ATOMIC_CANCEL:
10944 statement = cp_parser_transaction (parser, token);
10945 break;
10946 case RID_TRANSACTION_CANCEL:
10947 statement = cp_parser_transaction_cancel (parser);
10948 break;
10949
10950 default:
10951 /* It might be a keyword like `int' that can start a
10952 declaration-statement. */
10953 break;
10954 }
10955 }
10956 else if (token->type == CPP_NAME)
10957 {
10958 /* If the next token is a `:', then we are looking at a
10959 labeled-statement. */
10960 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10961 if (token->type == CPP_COLON)
10962 {
10963 /* Looks like a labeled-statement with an ordinary label.
10964 Parse the label, and then use tail recursion to parse
10965 the statement. */
10966
10967 cp_parser_label_for_labeled_statement (parser, std_attrs);
10968 in_compound = false;
10969 goto restart;
10970 }
10971 }
10972 /* Anything that starts with a `{' must be a compound-statement. */
10973 else if (token->type == CPP_OPEN_BRACE)
10974 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10975 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10976 a statement all its own. */
10977 else if (token->type == CPP_PRAGMA)
10978 {
10979 /* Only certain OpenMP pragmas are attached to statements, and thus
10980 are considered statements themselves. All others are not. In
10981 the context of a compound, accept the pragma as a "statement" and
10982 return so that we can check for a close brace. Otherwise we
10983 require a real statement and must go back and read one. */
10984 if (in_compound)
10985 cp_parser_pragma (parser, pragma_compound, if_p);
10986 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10987 goto restart;
10988 return;
10989 }
10990 else if (token->type == CPP_EOF)
10991 {
10992 cp_parser_error (parser, "expected statement");
10993 return;
10994 }
10995
10996 /* Everything else must be a declaration-statement or an
10997 expression-statement. Try for the declaration-statement
10998 first, unless we are looking at a `;', in which case we know that
10999 we have an expression-statement. */
11000 if (!statement)
11001 {
11002 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11003 {
11004 if (std_attrs != NULL_TREE)
11005 {
11006 /* Attributes should be parsed as part of the the
11007 declaration, so let's un-parse them. */
11008 saved_tokens.rollback();
11009 std_attrs = NULL_TREE;
11010 }
11011
11012 cp_parser_parse_tentatively (parser);
11013 /* Try to parse the declaration-statement. */
11014 cp_parser_declaration_statement (parser);
11015 /* If that worked, we're done. */
11016 if (cp_parser_parse_definitely (parser))
11017 return;
11018 }
11019 /* All preceding labels have been parsed at this point. */
11020 if (loc_after_labels != NULL)
11021 *loc_after_labels = statement_location;
11022
11023 /* Look for an expression-statement instead. */
11024 statement = cp_parser_expression_statement (parser, in_statement_expr);
11025
11026 /* Handle [[fallthrough]];. */
11027 if (attribute_fallthrough_p (std_attrs))
11028 {
11029 /* The next token after the fallthrough attribute is ';'. */
11030 if (statement == NULL_TREE)
11031 {
11032 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11033 statement = build_call_expr_internal_loc (statement_location,
11034 IFN_FALLTHROUGH,
11035 void_type_node, 0);
11036 finish_expr_stmt (statement);
11037 }
11038 else
11039 warning_at (statement_location, OPT_Wattributes,
11040 "%<fallthrough%> attribute not followed by %<;%>");
11041 std_attrs = NULL_TREE;
11042 }
11043 }
11044
11045 /* Set the line number for the statement. */
11046 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11047 SET_EXPR_LOCATION (statement, statement_location);
11048
11049 /* Allow "[[fallthrough]];", but warn otherwise. */
11050 if (std_attrs != NULL_TREE)
11051 warning_at (attrs_location,
11052 OPT_Wattributes,
11053 "attributes at the beginning of statement are ignored");
11054 }
11055
11056 /* Parse the label for a labeled-statement, i.e.
11057
11058 identifier :
11059 case constant-expression :
11060 default :
11061
11062 GNU Extension:
11063 case constant-expression ... constant-expression : statement
11064
11065 When a label is parsed without errors, the label is added to the
11066 parse tree by the finish_* functions, so this function doesn't
11067 have to return the label. */
11068
11069 static void
11070 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11071 {
11072 cp_token *token;
11073 tree label = NULL_TREE;
11074 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11075
11076 /* The next token should be an identifier. */
11077 token = cp_lexer_peek_token (parser->lexer);
11078 if (token->type != CPP_NAME
11079 && token->type != CPP_KEYWORD)
11080 {
11081 cp_parser_error (parser, "expected labeled-statement");
11082 return;
11083 }
11084
11085 /* Remember whether this case or a user-defined label is allowed to fall
11086 through to. */
11087 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11088
11089 parser->colon_corrects_to_scope_p = false;
11090 switch (token->keyword)
11091 {
11092 case RID_CASE:
11093 {
11094 tree expr, expr_hi;
11095 cp_token *ellipsis;
11096
11097 /* Consume the `case' token. */
11098 cp_lexer_consume_token (parser->lexer);
11099 /* Parse the constant-expression. */
11100 expr = cp_parser_constant_expression (parser);
11101 if (check_for_bare_parameter_packs (expr))
11102 expr = error_mark_node;
11103
11104 ellipsis = cp_lexer_peek_token (parser->lexer);
11105 if (ellipsis->type == CPP_ELLIPSIS)
11106 {
11107 /* Consume the `...' token. */
11108 cp_lexer_consume_token (parser->lexer);
11109 expr_hi = cp_parser_constant_expression (parser);
11110 if (check_for_bare_parameter_packs (expr_hi))
11111 expr_hi = error_mark_node;
11112
11113 /* We don't need to emit warnings here, as the common code
11114 will do this for us. */
11115 }
11116 else
11117 expr_hi = NULL_TREE;
11118
11119 if (parser->in_switch_statement_p)
11120 {
11121 tree l = finish_case_label (token->location, expr, expr_hi);
11122 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11123 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11124 }
11125 else
11126 error_at (token->location,
11127 "case label %qE not within a switch statement",
11128 expr);
11129 }
11130 break;
11131
11132 case RID_DEFAULT:
11133 /* Consume the `default' token. */
11134 cp_lexer_consume_token (parser->lexer);
11135
11136 if (parser->in_switch_statement_p)
11137 {
11138 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11139 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11140 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11141 }
11142 else
11143 error_at (token->location, "case label not within a switch statement");
11144 break;
11145
11146 default:
11147 /* Anything else must be an ordinary label. */
11148 label = finish_label_stmt (cp_parser_identifier (parser));
11149 if (label && TREE_CODE (label) == LABEL_DECL)
11150 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11151 break;
11152 }
11153
11154 /* Require the `:' token. */
11155 cp_parser_require (parser, CPP_COLON, RT_COLON);
11156
11157 /* An ordinary label may optionally be followed by attributes.
11158 However, this is only permitted if the attributes are then
11159 followed by a semicolon. This is because, for backward
11160 compatibility, when parsing
11161 lab: __attribute__ ((unused)) int i;
11162 we want the attribute to attach to "i", not "lab". */
11163 if (label != NULL_TREE
11164 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11165 {
11166 tree attrs;
11167 cp_parser_parse_tentatively (parser);
11168 attrs = cp_parser_gnu_attributes_opt (parser);
11169 if (attrs == NULL_TREE
11170 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11171 cp_parser_abort_tentative_parse (parser);
11172 else if (!cp_parser_parse_definitely (parser))
11173 ;
11174 else
11175 attributes = chainon (attributes, attrs);
11176 }
11177
11178 if (attributes != NULL_TREE)
11179 cplus_decl_attributes (&label, attributes, 0);
11180
11181 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11182 }
11183
11184 /* Parse an expression-statement.
11185
11186 expression-statement:
11187 expression [opt] ;
11188
11189 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11190 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11191 indicates whether this expression-statement is part of an
11192 expression statement. */
11193
11194 static tree
11195 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11196 {
11197 tree statement = NULL_TREE;
11198 cp_token *token = cp_lexer_peek_token (parser->lexer);
11199 location_t loc = token->location;
11200
11201 /* There might be attribute fallthrough. */
11202 tree attr = cp_parser_gnu_attributes_opt (parser);
11203
11204 /* If the next token is a ';', then there is no expression
11205 statement. */
11206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11207 {
11208 statement = cp_parser_expression (parser);
11209 if (statement == error_mark_node
11210 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11211 {
11212 cp_parser_skip_to_end_of_block_or_statement (parser);
11213 return error_mark_node;
11214 }
11215 }
11216
11217 /* Handle [[fallthrough]];. */
11218 if (attribute_fallthrough_p (attr))
11219 {
11220 /* The next token after the fallthrough attribute is ';'. */
11221 if (statement == NULL_TREE)
11222 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11223 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11224 void_type_node, 0);
11225 else
11226 warning_at (loc, OPT_Wattributes,
11227 "%<fallthrough%> attribute not followed by %<;%>");
11228 attr = NULL_TREE;
11229 }
11230
11231 /* Allow "[[fallthrough]];", but warn otherwise. */
11232 if (attr != NULL_TREE)
11233 warning_at (loc, OPT_Wattributes,
11234 "attributes at the beginning of statement are ignored");
11235
11236 /* Give a helpful message for "A<T>::type t;" and the like. */
11237 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11238 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11239 {
11240 if (TREE_CODE (statement) == SCOPE_REF)
11241 error_at (token->location, "need %<typename%> before %qE because "
11242 "%qT is a dependent scope",
11243 statement, TREE_OPERAND (statement, 0));
11244 else if (is_overloaded_fn (statement)
11245 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11246 {
11247 /* A::A a; */
11248 tree fn = get_first_fn (statement);
11249 error_at (token->location,
11250 "%<%T::%D%> names the constructor, not the type",
11251 DECL_CONTEXT (fn), DECL_NAME (fn));
11252 }
11253 }
11254
11255 /* Consume the final `;'. */
11256 cp_parser_consume_semicolon_at_end_of_statement (parser);
11257
11258 if (in_statement_expr
11259 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11260 /* This is the final expression statement of a statement
11261 expression. */
11262 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11263 else if (statement)
11264 statement = finish_expr_stmt (statement);
11265
11266 return statement;
11267 }
11268
11269 /* Parse a compound-statement.
11270
11271 compound-statement:
11272 { statement-seq [opt] }
11273
11274 GNU extension:
11275
11276 compound-statement:
11277 { label-declaration-seq [opt] statement-seq [opt] }
11278
11279 label-declaration-seq:
11280 label-declaration
11281 label-declaration-seq label-declaration
11282
11283 Returns a tree representing the statement. */
11284
11285 static tree
11286 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11287 int bcs_flags, bool function_body)
11288 {
11289 tree compound_stmt;
11290 matching_braces braces;
11291
11292 /* Consume the `{'. */
11293 if (!braces.require_open (parser))
11294 return error_mark_node;
11295 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11296 && !function_body && cxx_dialect < cxx14)
11297 pedwarn (input_location, OPT_Wpedantic,
11298 "compound-statement in %<constexpr%> function");
11299 /* Begin the compound-statement. */
11300 compound_stmt = begin_compound_stmt (bcs_flags);
11301 /* If the next keyword is `__label__' we have a label declaration. */
11302 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11303 cp_parser_label_declaration (parser);
11304 /* Parse an (optional) statement-seq. */
11305 cp_parser_statement_seq_opt (parser, in_statement_expr);
11306 /* Finish the compound-statement. */
11307 finish_compound_stmt (compound_stmt);
11308 /* Consume the `}'. */
11309 braces.require_close (parser);
11310
11311 return compound_stmt;
11312 }
11313
11314 /* Parse an (optional) statement-seq.
11315
11316 statement-seq:
11317 statement
11318 statement-seq [opt] statement */
11319
11320 static void
11321 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11322 {
11323 /* Scan statements until there aren't any more. */
11324 while (true)
11325 {
11326 cp_token *token = cp_lexer_peek_token (parser->lexer);
11327
11328 /* If we are looking at a `}', then we have run out of
11329 statements; the same is true if we have reached the end
11330 of file, or have stumbled upon a stray '@end'. */
11331 if (token->type == CPP_CLOSE_BRACE
11332 || token->type == CPP_EOF
11333 || token->type == CPP_PRAGMA_EOL
11334 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11335 break;
11336
11337 /* If we are in a compound statement and find 'else' then
11338 something went wrong. */
11339 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11340 {
11341 if (parser->in_statement & IN_IF_STMT)
11342 break;
11343 else
11344 {
11345 token = cp_lexer_consume_token (parser->lexer);
11346 error_at (token->location, "%<else%> without a previous %<if%>");
11347 }
11348 }
11349
11350 /* Parse the statement. */
11351 cp_parser_statement (parser, in_statement_expr, true, NULL);
11352 }
11353 }
11354
11355 /* Return true if we're looking at (init; cond), false otherwise. */
11356
11357 static bool
11358 cp_parser_init_statement_p (cp_parser *parser)
11359 {
11360 /* Save tokens so that we can put them back. */
11361 cp_lexer_save_tokens (parser->lexer);
11362
11363 /* Look for ';' that is not nested in () or {}. */
11364 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11365 /*recovering=*/false,
11366 CPP_SEMICOLON,
11367 /*consume_paren=*/false);
11368
11369 /* Roll back the tokens we skipped. */
11370 cp_lexer_rollback_tokens (parser->lexer);
11371
11372 return ret == -1;
11373 }
11374
11375 /* Parse a selection-statement.
11376
11377 selection-statement:
11378 if ( init-statement [opt] condition ) statement
11379 if ( init-statement [opt] condition ) statement else statement
11380 switch ( init-statement [opt] condition ) statement
11381
11382 Returns the new IF_STMT or SWITCH_STMT.
11383
11384 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11385 is a (possibly labeled) if statement which is not enclosed in
11386 braces and has an else clause. This is used to implement
11387 -Wparentheses.
11388
11389 CHAIN is a vector of if-else-if conditions. This is used to implement
11390 -Wduplicated-cond. */
11391
11392 static tree
11393 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11394 vec<tree> *chain)
11395 {
11396 cp_token *token;
11397 enum rid keyword;
11398 token_indent_info guard_tinfo;
11399
11400 if (if_p != NULL)
11401 *if_p = false;
11402
11403 /* Peek at the next token. */
11404 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11405 guard_tinfo = get_token_indent_info (token);
11406
11407 /* See what kind of keyword it is. */
11408 keyword = token->keyword;
11409 switch (keyword)
11410 {
11411 case RID_IF:
11412 case RID_SWITCH:
11413 {
11414 tree statement;
11415 tree condition;
11416
11417 bool cx = false;
11418 if (keyword == RID_IF
11419 && cp_lexer_next_token_is_keyword (parser->lexer,
11420 RID_CONSTEXPR))
11421 {
11422 cx = true;
11423 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11424 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11425 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11426 "with -std=c++17 or -std=gnu++17");
11427 }
11428
11429 /* Look for the `('. */
11430 matching_parens parens;
11431 if (!parens.require_open (parser))
11432 {
11433 cp_parser_skip_to_end_of_statement (parser);
11434 return error_mark_node;
11435 }
11436
11437 /* Begin the selection-statement. */
11438 if (keyword == RID_IF)
11439 {
11440 statement = begin_if_stmt ();
11441 IF_STMT_CONSTEXPR_P (statement) = cx;
11442 }
11443 else
11444 statement = begin_switch_stmt ();
11445
11446 /* Parse the optional init-statement. */
11447 if (cp_parser_init_statement_p (parser))
11448 {
11449 tree decl;
11450 if (cxx_dialect < cxx17)
11451 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11452 "init-statement in selection statements only available "
11453 "with -std=c++17 or -std=gnu++17");
11454 cp_parser_init_statement (parser, &decl);
11455 }
11456
11457 /* Parse the condition. */
11458 condition = cp_parser_condition (parser);
11459 /* Look for the `)'. */
11460 if (!parens.require_close (parser))
11461 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11462 /*consume_paren=*/true);
11463
11464 if (keyword == RID_IF)
11465 {
11466 bool nested_if;
11467 unsigned char in_statement;
11468
11469 /* Add the condition. */
11470 condition = finish_if_stmt_cond (condition, statement);
11471
11472 if (warn_duplicated_cond)
11473 warn_duplicated_cond_add_or_warn (token->location, condition,
11474 &chain);
11475
11476 /* Parse the then-clause. */
11477 in_statement = parser->in_statement;
11478 parser->in_statement |= IN_IF_STMT;
11479
11480 /* Outside a template, the non-selected branch of a constexpr
11481 if is a 'discarded statement', i.e. unevaluated. */
11482 bool was_discarded = in_discarded_stmt;
11483 bool discard_then = (cx && !processing_template_decl
11484 && integer_zerop (condition));
11485 if (discard_then)
11486 {
11487 in_discarded_stmt = true;
11488 ++c_inhibit_evaluation_warnings;
11489 }
11490
11491 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11492 guard_tinfo);
11493
11494 parser->in_statement = in_statement;
11495
11496 finish_then_clause (statement);
11497
11498 if (discard_then)
11499 {
11500 THEN_CLAUSE (statement) = NULL_TREE;
11501 in_discarded_stmt = was_discarded;
11502 --c_inhibit_evaluation_warnings;
11503 }
11504
11505 /* If the next token is `else', parse the else-clause. */
11506 if (cp_lexer_next_token_is_keyword (parser->lexer,
11507 RID_ELSE))
11508 {
11509 bool discard_else = (cx && !processing_template_decl
11510 && integer_nonzerop (condition));
11511 if (discard_else)
11512 {
11513 in_discarded_stmt = true;
11514 ++c_inhibit_evaluation_warnings;
11515 }
11516
11517 guard_tinfo
11518 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11519 /* Consume the `else' keyword. */
11520 cp_lexer_consume_token (parser->lexer);
11521 if (warn_duplicated_cond)
11522 {
11523 if (cp_lexer_next_token_is_keyword (parser->lexer,
11524 RID_IF)
11525 && chain == NULL)
11526 {
11527 /* We've got "if (COND) else if (COND2)". Start
11528 the condition chain and add COND as the first
11529 element. */
11530 chain = new vec<tree> ();
11531 if (!CONSTANT_CLASS_P (condition)
11532 && !TREE_SIDE_EFFECTS (condition))
11533 {
11534 /* Wrap it in a NOP_EXPR so that we can set the
11535 location of the condition. */
11536 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11537 condition);
11538 SET_EXPR_LOCATION (e, token->location);
11539 chain->safe_push (e);
11540 }
11541 }
11542 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11543 RID_IF))
11544 {
11545 /* This is if-else without subsequent if. Zap the
11546 condition chain; we would have already warned at
11547 this point. */
11548 delete chain;
11549 chain = NULL;
11550 }
11551 }
11552 begin_else_clause (statement);
11553 /* Parse the else-clause. */
11554 cp_parser_implicitly_scoped_statement (parser, NULL,
11555 guard_tinfo, chain);
11556
11557 finish_else_clause (statement);
11558
11559 /* If we are currently parsing a then-clause, then
11560 IF_P will not be NULL. We set it to true to
11561 indicate that this if statement has an else clause.
11562 This may trigger the Wparentheses warning below
11563 when we get back up to the parent if statement. */
11564 if (if_p != NULL)
11565 *if_p = true;
11566
11567 if (discard_else)
11568 {
11569 ELSE_CLAUSE (statement) = NULL_TREE;
11570 in_discarded_stmt = was_discarded;
11571 --c_inhibit_evaluation_warnings;
11572 }
11573 }
11574 else
11575 {
11576 /* This if statement does not have an else clause. If
11577 NESTED_IF is true, then the then-clause has an if
11578 statement which does have an else clause. We warn
11579 about the potential ambiguity. */
11580 if (nested_if)
11581 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11582 "suggest explicit braces to avoid ambiguous"
11583 " %<else%>");
11584 if (warn_duplicated_cond)
11585 {
11586 /* We don't need the condition chain anymore. */
11587 delete chain;
11588 chain = NULL;
11589 }
11590 }
11591
11592 /* Now we're all done with the if-statement. */
11593 finish_if_stmt (statement);
11594 }
11595 else
11596 {
11597 bool in_switch_statement_p;
11598 unsigned char in_statement;
11599
11600 /* Add the condition. */
11601 finish_switch_cond (condition, statement);
11602
11603 /* Parse the body of the switch-statement. */
11604 in_switch_statement_p = parser->in_switch_statement_p;
11605 in_statement = parser->in_statement;
11606 parser->in_switch_statement_p = true;
11607 parser->in_statement |= IN_SWITCH_STMT;
11608 cp_parser_implicitly_scoped_statement (parser, if_p,
11609 guard_tinfo);
11610 parser->in_switch_statement_p = in_switch_statement_p;
11611 parser->in_statement = in_statement;
11612
11613 /* Now we're all done with the switch-statement. */
11614 finish_switch_stmt (statement);
11615 }
11616
11617 return statement;
11618 }
11619 break;
11620
11621 default:
11622 cp_parser_error (parser, "expected selection-statement");
11623 return error_mark_node;
11624 }
11625 }
11626
11627 /* Parse a condition.
11628
11629 condition:
11630 expression
11631 type-specifier-seq declarator = initializer-clause
11632 type-specifier-seq declarator braced-init-list
11633
11634 GNU Extension:
11635
11636 condition:
11637 type-specifier-seq declarator asm-specification [opt]
11638 attributes [opt] = assignment-expression
11639
11640 Returns the expression that should be tested. */
11641
11642 static tree
11643 cp_parser_condition (cp_parser* parser)
11644 {
11645 cp_decl_specifier_seq type_specifiers;
11646 const char *saved_message;
11647 int declares_class_or_enum;
11648
11649 /* Try the declaration first. */
11650 cp_parser_parse_tentatively (parser);
11651 /* New types are not allowed in the type-specifier-seq for a
11652 condition. */
11653 saved_message = parser->type_definition_forbidden_message;
11654 parser->type_definition_forbidden_message
11655 = G_("types may not be defined in conditions");
11656 /* Parse the type-specifier-seq. */
11657 cp_parser_decl_specifier_seq (parser,
11658 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11659 &type_specifiers,
11660 &declares_class_or_enum);
11661 /* Restore the saved message. */
11662 parser->type_definition_forbidden_message = saved_message;
11663 /* If all is well, we might be looking at a declaration. */
11664 if (!cp_parser_error_occurred (parser))
11665 {
11666 tree decl;
11667 tree asm_specification;
11668 tree attributes;
11669 cp_declarator *declarator;
11670 tree initializer = NULL_TREE;
11671
11672 /* Parse the declarator. */
11673 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11674 /*ctor_dtor_or_conv_p=*/NULL,
11675 /*parenthesized_p=*/NULL,
11676 /*member_p=*/false,
11677 /*friend_p=*/false);
11678 /* Parse the attributes. */
11679 attributes = cp_parser_attributes_opt (parser);
11680 /* Parse the asm-specification. */
11681 asm_specification = cp_parser_asm_specification_opt (parser);
11682 /* If the next token is not an `=' or '{', then we might still be
11683 looking at an expression. For example:
11684
11685 if (A(a).x)
11686
11687 looks like a decl-specifier-seq and a declarator -- but then
11688 there is no `=', so this is an expression. */
11689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11690 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11691 cp_parser_simulate_error (parser);
11692
11693 /* If we did see an `=' or '{', then we are looking at a declaration
11694 for sure. */
11695 if (cp_parser_parse_definitely (parser))
11696 {
11697 tree pushed_scope;
11698 bool non_constant_p;
11699 int flags = LOOKUP_ONLYCONVERTING;
11700
11701 /* Create the declaration. */
11702 decl = start_decl (declarator, &type_specifiers,
11703 /*initialized_p=*/true,
11704 attributes, /*prefix_attributes=*/NULL_TREE,
11705 &pushed_scope);
11706
11707 /* Parse the initializer. */
11708 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11709 {
11710 initializer = cp_parser_braced_list (parser, &non_constant_p);
11711 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11712 flags = 0;
11713 }
11714 else
11715 {
11716 /* Consume the `='. */
11717 cp_parser_require (parser, CPP_EQ, RT_EQ);
11718 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11719 }
11720 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11721 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11722
11723 /* Process the initializer. */
11724 cp_finish_decl (decl,
11725 initializer, !non_constant_p,
11726 asm_specification,
11727 flags);
11728
11729 if (pushed_scope)
11730 pop_scope (pushed_scope);
11731
11732 return convert_from_reference (decl);
11733 }
11734 }
11735 /* If we didn't even get past the declarator successfully, we are
11736 definitely not looking at a declaration. */
11737 else
11738 cp_parser_abort_tentative_parse (parser);
11739
11740 /* Otherwise, we are looking at an expression. */
11741 return cp_parser_expression (parser);
11742 }
11743
11744 /* Parses a for-statement or range-for-statement until the closing ')',
11745 not included. */
11746
11747 static tree
11748 cp_parser_for (cp_parser *parser, bool ivdep)
11749 {
11750 tree init, scope, decl;
11751 bool is_range_for;
11752
11753 /* Begin the for-statement. */
11754 scope = begin_for_scope (&init);
11755
11756 /* Parse the initialization. */
11757 is_range_for = cp_parser_init_statement (parser, &decl);
11758
11759 if (is_range_for)
11760 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11761 else
11762 return cp_parser_c_for (parser, scope, init, ivdep);
11763 }
11764
11765 static tree
11766 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11767 {
11768 /* Normal for loop */
11769 tree condition = NULL_TREE;
11770 tree expression = NULL_TREE;
11771 tree stmt;
11772
11773 stmt = begin_for_stmt (scope, init);
11774 /* The init-statement has already been parsed in
11775 cp_parser_init_statement, so no work is needed here. */
11776 finish_init_stmt (stmt);
11777
11778 /* If there's a condition, process it. */
11779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11780 condition = cp_parser_condition (parser);
11781 else if (ivdep)
11782 {
11783 cp_parser_error (parser, "missing loop condition in loop with "
11784 "%<GCC ivdep%> pragma");
11785 condition = error_mark_node;
11786 }
11787 finish_for_cond (condition, stmt, ivdep);
11788 /* Look for the `;'. */
11789 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11790
11791 /* If there's an expression, process it. */
11792 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11793 expression = cp_parser_expression (parser);
11794 finish_for_expr (expression, stmt);
11795
11796 return stmt;
11797 }
11798
11799 /* Tries to parse a range-based for-statement:
11800
11801 range-based-for:
11802 decl-specifier-seq declarator : expression
11803
11804 The decl-specifier-seq declarator and the `:' are already parsed by
11805 cp_parser_init_statement. If processing_template_decl it returns a
11806 newly created RANGE_FOR_STMT; if not, it is converted to a
11807 regular FOR_STMT. */
11808
11809 static tree
11810 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11811 bool ivdep)
11812 {
11813 tree stmt, range_expr;
11814 auto_vec <cxx_binding *, 16> bindings;
11815 auto_vec <tree, 16> names;
11816 tree decomp_first_name = NULL_TREE;
11817 unsigned int decomp_cnt = 0;
11818
11819 /* Get the range declaration momentarily out of the way so that
11820 the range expression doesn't clash with it. */
11821 if (range_decl != error_mark_node)
11822 {
11823 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11824 {
11825 tree v = DECL_VALUE_EXPR (range_decl);
11826 /* For decomposition declaration get all of the corresponding
11827 declarations out of the way. */
11828 if (TREE_CODE (v) == ARRAY_REF
11829 && VAR_P (TREE_OPERAND (v, 0))
11830 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11831 {
11832 tree d = range_decl;
11833 range_decl = TREE_OPERAND (v, 0);
11834 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11835 decomp_first_name = d;
11836 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11837 {
11838 tree name = DECL_NAME (d);
11839 names.safe_push (name);
11840 bindings.safe_push (IDENTIFIER_BINDING (name));
11841 IDENTIFIER_BINDING (name)
11842 = IDENTIFIER_BINDING (name)->previous;
11843 }
11844 }
11845 }
11846 if (names.is_empty ())
11847 {
11848 tree name = DECL_NAME (range_decl);
11849 names.safe_push (name);
11850 bindings.safe_push (IDENTIFIER_BINDING (name));
11851 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11852 }
11853 }
11854
11855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11856 {
11857 bool expr_non_constant_p;
11858 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11859 }
11860 else
11861 range_expr = cp_parser_expression (parser);
11862
11863 /* Put the range declaration(s) back into scope. */
11864 for (unsigned int i = 0; i < names.length (); i++)
11865 {
11866 cxx_binding *binding = bindings[i];
11867 binding->previous = IDENTIFIER_BINDING (names[i]);
11868 IDENTIFIER_BINDING (names[i]) = binding;
11869 }
11870
11871 /* If in template, STMT is converted to a normal for-statement
11872 at instantiation. If not, it is done just ahead. */
11873 if (processing_template_decl)
11874 {
11875 if (check_for_bare_parameter_packs (range_expr))
11876 range_expr = error_mark_node;
11877 stmt = begin_range_for_stmt (scope, init);
11878 if (ivdep)
11879 RANGE_FOR_IVDEP (stmt) = 1;
11880 finish_range_for_decl (stmt, range_decl, range_expr);
11881 if (!type_dependent_expression_p (range_expr)
11882 /* do_auto_deduction doesn't mess with template init-lists. */
11883 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11884 do_range_for_auto_deduction (range_decl, range_expr);
11885 }
11886 else
11887 {
11888 stmt = begin_for_stmt (scope, init);
11889 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11890 decomp_first_name, decomp_cnt, ivdep);
11891 }
11892 return stmt;
11893 }
11894
11895 /* Subroutine of cp_convert_range_for: given the initializer expression,
11896 builds up the range temporary. */
11897
11898 static tree
11899 build_range_temp (tree range_expr)
11900 {
11901 tree range_type, range_temp;
11902
11903 /* Find out the type deduced by the declaration
11904 `auto &&__range = range_expr'. */
11905 range_type = cp_build_reference_type (make_auto (), true);
11906 range_type = do_auto_deduction (range_type, range_expr,
11907 type_uses_auto (range_type));
11908
11909 /* Create the __range variable. */
11910 range_temp = build_decl (input_location, VAR_DECL,
11911 get_identifier ("__for_range"), range_type);
11912 TREE_USED (range_temp) = 1;
11913 DECL_ARTIFICIAL (range_temp) = 1;
11914
11915 return range_temp;
11916 }
11917
11918 /* Used by cp_parser_range_for in template context: we aren't going to
11919 do a full conversion yet, but we still need to resolve auto in the
11920 type of the for-range-declaration if present. This is basically
11921 a shortcut version of cp_convert_range_for. */
11922
11923 static void
11924 do_range_for_auto_deduction (tree decl, tree range_expr)
11925 {
11926 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11927 if (auto_node)
11928 {
11929 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11930 range_temp = convert_from_reference (build_range_temp (range_expr));
11931 iter_type = (cp_parser_perform_range_for_lookup
11932 (range_temp, &begin_dummy, &end_dummy));
11933 if (iter_type)
11934 {
11935 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11936 iter_type);
11937 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11938 RO_UNARY_STAR,
11939 tf_warning_or_error);
11940 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11941 iter_decl, auto_node);
11942 }
11943 }
11944 }
11945
11946 /* Converts a range-based for-statement into a normal
11947 for-statement, as per the definition.
11948
11949 for (RANGE_DECL : RANGE_EXPR)
11950 BLOCK
11951
11952 should be equivalent to:
11953
11954 {
11955 auto &&__range = RANGE_EXPR;
11956 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11957 __begin != __end;
11958 ++__begin)
11959 {
11960 RANGE_DECL = *__begin;
11961 BLOCK
11962 }
11963 }
11964
11965 If RANGE_EXPR is an array:
11966 BEGIN_EXPR = __range
11967 END_EXPR = __range + ARRAY_SIZE(__range)
11968 Else if RANGE_EXPR has a member 'begin' or 'end':
11969 BEGIN_EXPR = __range.begin()
11970 END_EXPR = __range.end()
11971 Else:
11972 BEGIN_EXPR = begin(__range)
11973 END_EXPR = end(__range);
11974
11975 If __range has a member 'begin' but not 'end', or vice versa, we must
11976 still use the second alternative (it will surely fail, however).
11977 When calling begin()/end() in the third alternative we must use
11978 argument dependent lookup, but always considering 'std' as an associated
11979 namespace. */
11980
11981 tree
11982 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11983 tree decomp_first_name, unsigned int decomp_cnt,
11984 bool ivdep)
11985 {
11986 tree begin, end;
11987 tree iter_type, begin_expr, end_expr;
11988 tree condition, expression;
11989
11990 range_expr = mark_lvalue_use (range_expr);
11991
11992 if (range_decl == error_mark_node || range_expr == error_mark_node)
11993 /* If an error happened previously do nothing or else a lot of
11994 unhelpful errors would be issued. */
11995 begin_expr = end_expr = iter_type = error_mark_node;
11996 else
11997 {
11998 tree range_temp;
11999
12000 if (VAR_P (range_expr)
12001 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12002 /* Can't bind a reference to an array of runtime bound. */
12003 range_temp = range_expr;
12004 else
12005 {
12006 range_temp = build_range_temp (range_expr);
12007 pushdecl (range_temp);
12008 cp_finish_decl (range_temp, range_expr,
12009 /*is_constant_init*/false, NULL_TREE,
12010 LOOKUP_ONLYCONVERTING);
12011 range_temp = convert_from_reference (range_temp);
12012 }
12013 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12014 &begin_expr, &end_expr);
12015 }
12016
12017 /* The new for initialization statement. */
12018 begin = build_decl (input_location, VAR_DECL,
12019 get_identifier ("__for_begin"), iter_type);
12020 TREE_USED (begin) = 1;
12021 DECL_ARTIFICIAL (begin) = 1;
12022 pushdecl (begin);
12023 cp_finish_decl (begin, begin_expr,
12024 /*is_constant_init*/false, NULL_TREE,
12025 LOOKUP_ONLYCONVERTING);
12026
12027 if (cxx_dialect >= cxx17)
12028 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12029 end = build_decl (input_location, VAR_DECL,
12030 get_identifier ("__for_end"), iter_type);
12031 TREE_USED (end) = 1;
12032 DECL_ARTIFICIAL (end) = 1;
12033 pushdecl (end);
12034 cp_finish_decl (end, end_expr,
12035 /*is_constant_init*/false, NULL_TREE,
12036 LOOKUP_ONLYCONVERTING);
12037
12038 finish_init_stmt (statement);
12039
12040 /* The new for condition. */
12041 condition = build_x_binary_op (input_location, NE_EXPR,
12042 begin, ERROR_MARK,
12043 end, ERROR_MARK,
12044 NULL, tf_warning_or_error);
12045 finish_for_cond (condition, statement, ivdep);
12046
12047 /* The new increment expression. */
12048 expression = finish_unary_op_expr (input_location,
12049 PREINCREMENT_EXPR, begin,
12050 tf_warning_or_error);
12051 finish_for_expr (expression, statement);
12052
12053 /* The declaration is initialized with *__begin inside the loop body. */
12054 cp_finish_decl (range_decl,
12055 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12056 tf_warning_or_error),
12057 /*is_constant_init*/false, NULL_TREE,
12058 LOOKUP_ONLYCONVERTING);
12059 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12060 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12061
12062 return statement;
12063 }
12064
12065 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12066 We need to solve both at the same time because the method used
12067 depends on the existence of members begin or end.
12068 Returns the type deduced for the iterator expression. */
12069
12070 static tree
12071 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12072 {
12073 if (error_operand_p (range))
12074 {
12075 *begin = *end = error_mark_node;
12076 return error_mark_node;
12077 }
12078
12079 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12080 {
12081 error ("range-based %<for%> expression of type %qT "
12082 "has incomplete type", TREE_TYPE (range));
12083 *begin = *end = error_mark_node;
12084 return error_mark_node;
12085 }
12086 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12087 {
12088 /* If RANGE is an array, we will use pointer arithmetic. */
12089 *begin = decay_conversion (range, tf_warning_or_error);
12090 *end = build_binary_op (input_location, PLUS_EXPR,
12091 range,
12092 array_type_nelts_top (TREE_TYPE (range)),
12093 false);
12094 return TREE_TYPE (*begin);
12095 }
12096 else
12097 {
12098 /* If it is not an array, we must do a bit of magic. */
12099 tree id_begin, id_end;
12100 tree member_begin, member_end;
12101
12102 *begin = *end = error_mark_node;
12103
12104 id_begin = get_identifier ("begin");
12105 id_end = get_identifier ("end");
12106 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12107 /*protect=*/2, /*want_type=*/false,
12108 tf_warning_or_error);
12109 member_end = lookup_member (TREE_TYPE (range), id_end,
12110 /*protect=*/2, /*want_type=*/false,
12111 tf_warning_or_error);
12112
12113 if (member_begin != NULL_TREE || member_end != NULL_TREE)
12114 {
12115 /* Use the member functions. */
12116 if (member_begin != NULL_TREE)
12117 *begin = cp_parser_range_for_member_function (range, id_begin);
12118 else
12119 error ("range-based %<for%> expression of type %qT has an "
12120 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12121
12122 if (member_end != NULL_TREE)
12123 *end = cp_parser_range_for_member_function (range, id_end);
12124 else
12125 error ("range-based %<for%> expression of type %qT has a "
12126 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12127 }
12128 else
12129 {
12130 /* Use global functions with ADL. */
12131 vec<tree, va_gc> *vec;
12132 vec = make_tree_vector ();
12133
12134 vec_safe_push (vec, range);
12135
12136 member_begin = perform_koenig_lookup (id_begin, vec,
12137 tf_warning_or_error);
12138 *begin = finish_call_expr (member_begin, &vec, false, true,
12139 tf_warning_or_error);
12140 member_end = perform_koenig_lookup (id_end, vec,
12141 tf_warning_or_error);
12142 *end = finish_call_expr (member_end, &vec, false, true,
12143 tf_warning_or_error);
12144
12145 release_tree_vector (vec);
12146 }
12147
12148 /* Last common checks. */
12149 if (*begin == error_mark_node || *end == error_mark_node)
12150 {
12151 /* If one of the expressions is an error do no more checks. */
12152 *begin = *end = error_mark_node;
12153 return error_mark_node;
12154 }
12155 else if (type_dependent_expression_p (*begin)
12156 || type_dependent_expression_p (*end))
12157 /* Can happen, when, eg, in a template context, Koenig lookup
12158 can't resolve begin/end (c++/58503). */
12159 return NULL_TREE;
12160 else
12161 {
12162 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12163 /* The unqualified type of the __begin and __end temporaries should
12164 be the same, as required by the multiple auto declaration. */
12165 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12166 {
12167 if (cxx_dialect >= cxx17
12168 && (build_x_binary_op (input_location, NE_EXPR,
12169 *begin, ERROR_MARK,
12170 *end, ERROR_MARK,
12171 NULL, tf_none)
12172 != error_mark_node))
12173 /* P0184R0 allows __begin and __end to have different types,
12174 but make sure they are comparable so we can give a better
12175 diagnostic. */;
12176 else
12177 error ("inconsistent begin/end types in range-based %<for%> "
12178 "statement: %qT and %qT",
12179 TREE_TYPE (*begin), TREE_TYPE (*end));
12180 }
12181 return iter_type;
12182 }
12183 }
12184 }
12185
12186 /* Helper function for cp_parser_perform_range_for_lookup.
12187 Builds a tree for RANGE.IDENTIFIER(). */
12188
12189 static tree
12190 cp_parser_range_for_member_function (tree range, tree identifier)
12191 {
12192 tree member, res;
12193 vec<tree, va_gc> *vec;
12194
12195 member = finish_class_member_access_expr (range, identifier,
12196 false, tf_warning_or_error);
12197 if (member == error_mark_node)
12198 return error_mark_node;
12199
12200 vec = make_tree_vector ();
12201 res = finish_call_expr (member, &vec,
12202 /*disallow_virtual=*/false,
12203 /*koenig_p=*/false,
12204 tf_warning_or_error);
12205 release_tree_vector (vec);
12206 return res;
12207 }
12208
12209 /* Parse an iteration-statement.
12210
12211 iteration-statement:
12212 while ( condition ) statement
12213 do statement while ( expression ) ;
12214 for ( init-statement condition [opt] ; expression [opt] )
12215 statement
12216
12217 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12218
12219 static tree
12220 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12221 {
12222 cp_token *token;
12223 enum rid keyword;
12224 tree statement;
12225 unsigned char in_statement;
12226 token_indent_info guard_tinfo;
12227
12228 /* Peek at the next token. */
12229 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12230 if (!token)
12231 return error_mark_node;
12232
12233 guard_tinfo = get_token_indent_info (token);
12234
12235 /* Remember whether or not we are already within an iteration
12236 statement. */
12237 in_statement = parser->in_statement;
12238
12239 /* See what kind of keyword it is. */
12240 keyword = token->keyword;
12241 switch (keyword)
12242 {
12243 case RID_WHILE:
12244 {
12245 tree condition;
12246
12247 /* Begin the while-statement. */
12248 statement = begin_while_stmt ();
12249 /* Look for the `('. */
12250 matching_parens parens;
12251 parens.require_open (parser);
12252 /* Parse the condition. */
12253 condition = cp_parser_condition (parser);
12254 finish_while_stmt_cond (condition, statement, ivdep);
12255 /* Look for the `)'. */
12256 parens.require_close (parser);
12257 /* Parse the dependent statement. */
12258 parser->in_statement = IN_ITERATION_STMT;
12259 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12260 parser->in_statement = in_statement;
12261 /* We're done with the while-statement. */
12262 finish_while_stmt (statement);
12263 }
12264 break;
12265
12266 case RID_DO:
12267 {
12268 tree expression;
12269
12270 /* Begin the do-statement. */
12271 statement = begin_do_stmt ();
12272 /* Parse the body of the do-statement. */
12273 parser->in_statement = IN_ITERATION_STMT;
12274 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12275 parser->in_statement = in_statement;
12276 finish_do_body (statement);
12277 /* Look for the `while' keyword. */
12278 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12279 /* Look for the `('. */
12280 matching_parens parens;
12281 parens.require_open (parser);
12282 /* Parse the expression. */
12283 expression = cp_parser_expression (parser);
12284 /* We're done with the do-statement. */
12285 finish_do_stmt (expression, statement, ivdep);
12286 /* Look for the `)'. */
12287 parens.require_close (parser);
12288 /* Look for the `;'. */
12289 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12290 }
12291 break;
12292
12293 case RID_FOR:
12294 {
12295 /* Look for the `('. */
12296 matching_parens parens;
12297 parens.require_open (parser);
12298
12299 statement = cp_parser_for (parser, ivdep);
12300
12301 /* Look for the `)'. */
12302 parens.require_close (parser);
12303
12304 /* Parse the body of the for-statement. */
12305 parser->in_statement = IN_ITERATION_STMT;
12306 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12307 parser->in_statement = in_statement;
12308
12309 /* We're done with the for-statement. */
12310 finish_for_stmt (statement);
12311 }
12312 break;
12313
12314 default:
12315 cp_parser_error (parser, "expected iteration-statement");
12316 statement = error_mark_node;
12317 break;
12318 }
12319
12320 return statement;
12321 }
12322
12323 /* Parse a init-statement or the declarator of a range-based-for.
12324 Returns true if a range-based-for declaration is seen.
12325
12326 init-statement:
12327 expression-statement
12328 simple-declaration */
12329
12330 static bool
12331 cp_parser_init_statement (cp_parser* parser, tree *decl)
12332 {
12333 /* If the next token is a `;', then we have an empty
12334 expression-statement. Grammatically, this is also a
12335 simple-declaration, but an invalid one, because it does not
12336 declare anything. Therefore, if we did not handle this case
12337 specially, we would issue an error message about an invalid
12338 declaration. */
12339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12340 {
12341 bool is_range_for = false;
12342 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12343
12344 /* A colon is used in range-based for. */
12345 parser->colon_corrects_to_scope_p = false;
12346
12347 /* We're going to speculatively look for a declaration, falling back
12348 to an expression, if necessary. */
12349 cp_parser_parse_tentatively (parser);
12350 /* Parse the declaration. */
12351 cp_parser_simple_declaration (parser,
12352 /*function_definition_allowed_p=*/false,
12353 decl);
12354 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12355 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12356 {
12357 /* It is a range-for, consume the ':' */
12358 cp_lexer_consume_token (parser->lexer);
12359 is_range_for = true;
12360 if (cxx_dialect < cxx11)
12361 {
12362 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12363 "range-based %<for%> loops only available with "
12364 "-std=c++11 or -std=gnu++11");
12365 *decl = error_mark_node;
12366 }
12367 }
12368 else
12369 /* The ';' is not consumed yet because we told
12370 cp_parser_simple_declaration not to. */
12371 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12372
12373 if (cp_parser_parse_definitely (parser))
12374 return is_range_for;
12375 /* If the tentative parse failed, then we shall need to look for an
12376 expression-statement. */
12377 }
12378 /* If we are here, it is an expression-statement. */
12379 cp_parser_expression_statement (parser, NULL_TREE);
12380 return false;
12381 }
12382
12383 /* Parse a jump-statement.
12384
12385 jump-statement:
12386 break ;
12387 continue ;
12388 return expression [opt] ;
12389 return braced-init-list ;
12390 goto identifier ;
12391
12392 GNU extension:
12393
12394 jump-statement:
12395 goto * expression ;
12396
12397 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12398
12399 static tree
12400 cp_parser_jump_statement (cp_parser* parser)
12401 {
12402 tree statement = error_mark_node;
12403 cp_token *token;
12404 enum rid keyword;
12405 unsigned char in_statement;
12406
12407 /* Peek at the next token. */
12408 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12409 if (!token)
12410 return error_mark_node;
12411
12412 /* See what kind of keyword it is. */
12413 keyword = token->keyword;
12414 switch (keyword)
12415 {
12416 case RID_BREAK:
12417 in_statement = parser->in_statement & ~IN_IF_STMT;
12418 switch (in_statement)
12419 {
12420 case 0:
12421 error_at (token->location, "break statement not within loop or switch");
12422 break;
12423 default:
12424 gcc_assert ((in_statement & IN_SWITCH_STMT)
12425 || in_statement == IN_ITERATION_STMT);
12426 statement = finish_break_stmt ();
12427 if (in_statement == IN_ITERATION_STMT)
12428 break_maybe_infinite_loop ();
12429 break;
12430 case IN_OMP_BLOCK:
12431 error_at (token->location, "invalid exit from OpenMP structured block");
12432 break;
12433 case IN_OMP_FOR:
12434 error_at (token->location, "break statement used with OpenMP for loop");
12435 break;
12436 case IN_CILK_SIMD_FOR:
12437 error_at (token->location, "break statement used with Cilk Plus for loop");
12438 break;
12439 }
12440 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12441 break;
12442
12443 case RID_CONTINUE:
12444 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12445 {
12446 case 0:
12447 error_at (token->location, "continue statement not within a loop");
12448 break;
12449 case IN_CILK_SIMD_FOR:
12450 error_at (token->location,
12451 "continue statement within %<#pragma simd%> loop body");
12452 /* Fall through. */
12453 case IN_ITERATION_STMT:
12454 case IN_OMP_FOR:
12455 statement = finish_continue_stmt ();
12456 break;
12457 case IN_OMP_BLOCK:
12458 error_at (token->location, "invalid exit from OpenMP structured block");
12459 break;
12460 default:
12461 gcc_unreachable ();
12462 }
12463 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12464 break;
12465
12466 case RID_RETURN:
12467 {
12468 tree expr;
12469 bool expr_non_constant_p;
12470
12471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12472 {
12473 cp_lexer_set_source_position (parser->lexer);
12474 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12475 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12476 }
12477 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12478 expr = cp_parser_expression (parser);
12479 else
12480 /* If the next token is a `;', then there is no
12481 expression. */
12482 expr = NULL_TREE;
12483 /* Build the return-statement. */
12484 if (current_function_auto_return_pattern && in_discarded_stmt)
12485 /* Don't deduce from a discarded return statement. */;
12486 else
12487 statement = finish_return_stmt (expr);
12488 /* Look for the final `;'. */
12489 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12490 }
12491 break;
12492
12493 case RID_GOTO:
12494 if (parser->in_function_body
12495 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12496 {
12497 error ("%<goto%> in %<constexpr%> function");
12498 cp_function_chain->invalid_constexpr = true;
12499 }
12500
12501 /* Create the goto-statement. */
12502 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12503 {
12504 /* Issue a warning about this use of a GNU extension. */
12505 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12506 /* Consume the '*' token. */
12507 cp_lexer_consume_token (parser->lexer);
12508 /* Parse the dependent expression. */
12509 finish_goto_stmt (cp_parser_expression (parser));
12510 }
12511 else
12512 finish_goto_stmt (cp_parser_identifier (parser));
12513 /* Look for the final `;'. */
12514 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12515 break;
12516
12517 default:
12518 cp_parser_error (parser, "expected jump-statement");
12519 break;
12520 }
12521
12522 return statement;
12523 }
12524
12525 /* Parse a declaration-statement.
12526
12527 declaration-statement:
12528 block-declaration */
12529
12530 static void
12531 cp_parser_declaration_statement (cp_parser* parser)
12532 {
12533 void *p;
12534
12535 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12536 p = obstack_alloc (&declarator_obstack, 0);
12537
12538 /* Parse the block-declaration. */
12539 cp_parser_block_declaration (parser, /*statement_p=*/true);
12540
12541 /* Free any declarators allocated. */
12542 obstack_free (&declarator_obstack, p);
12543 }
12544
12545 /* Some dependent statements (like `if (cond) statement'), are
12546 implicitly in their own scope. In other words, if the statement is
12547 a single statement (as opposed to a compound-statement), it is
12548 none-the-less treated as if it were enclosed in braces. Any
12549 declarations appearing in the dependent statement are out of scope
12550 after control passes that point. This function parses a statement,
12551 but ensures that is in its own scope, even if it is not a
12552 compound-statement.
12553
12554 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12555 is a (possibly labeled) if statement which is not enclosed in
12556 braces and has an else clause. This is used to implement
12557 -Wparentheses.
12558
12559 CHAIN is a vector of if-else-if conditions. This is used to implement
12560 -Wduplicated-cond.
12561
12562 Returns the new statement. */
12563
12564 static tree
12565 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12566 const token_indent_info &guard_tinfo,
12567 vec<tree> *chain)
12568 {
12569 tree statement;
12570 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12571 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12572 token_indent_info body_tinfo
12573 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12574
12575 if (if_p != NULL)
12576 *if_p = false;
12577
12578 /* Mark if () ; with a special NOP_EXPR. */
12579 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12580 {
12581 cp_lexer_consume_token (parser->lexer);
12582 statement = add_stmt (build_empty_stmt (body_loc));
12583
12584 if (guard_tinfo.keyword == RID_IF
12585 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12586 warning_at (body_loc, OPT_Wempty_body,
12587 "suggest braces around empty body in an %<if%> statement");
12588 else if (guard_tinfo.keyword == RID_ELSE)
12589 warning_at (body_loc, OPT_Wempty_body,
12590 "suggest braces around empty body in an %<else%> statement");
12591 }
12592 /* if a compound is opened, we simply parse the statement directly. */
12593 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12594 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12595 /* If the token is not a `{', then we must take special action. */
12596 else
12597 {
12598 /* Create a compound-statement. */
12599 statement = begin_compound_stmt (0);
12600 /* Parse the dependent-statement. */
12601 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12602 &body_loc_after_labels);
12603 /* Finish the dummy compound-statement. */
12604 finish_compound_stmt (statement);
12605 }
12606
12607 token_indent_info next_tinfo
12608 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12609 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12610
12611 if (body_loc_after_labels != UNKNOWN_LOCATION
12612 && next_tinfo.type != CPP_SEMICOLON)
12613 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12614 guard_tinfo.location, guard_tinfo.keyword);
12615
12616 /* Return the statement. */
12617 return statement;
12618 }
12619
12620 /* For some dependent statements (like `while (cond) statement'), we
12621 have already created a scope. Therefore, even if the dependent
12622 statement is a compound-statement, we do not want to create another
12623 scope. */
12624
12625 static void
12626 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12627 const token_indent_info &guard_tinfo)
12628 {
12629 /* If the token is a `{', then we must take special action. */
12630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12631 {
12632 token_indent_info body_tinfo
12633 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12634 location_t loc_after_labels = UNKNOWN_LOCATION;
12635
12636 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12637 &loc_after_labels);
12638 token_indent_info next_tinfo
12639 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12640 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12641
12642 if (loc_after_labels != UNKNOWN_LOCATION
12643 && next_tinfo.type != CPP_SEMICOLON)
12644 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12645 guard_tinfo.location,
12646 guard_tinfo.keyword);
12647 }
12648 else
12649 {
12650 /* Avoid calling cp_parser_compound_statement, so that we
12651 don't create a new scope. Do everything else by hand. */
12652 matching_braces braces;
12653 braces.require_open (parser);
12654 /* If the next keyword is `__label__' we have a label declaration. */
12655 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12656 cp_parser_label_declaration (parser);
12657 /* Parse an (optional) statement-seq. */
12658 cp_parser_statement_seq_opt (parser, NULL_TREE);
12659 braces.require_close (parser);
12660 }
12661 }
12662
12663 /* Declarations [gram.dcl.dcl] */
12664
12665 /* Parse an optional declaration-sequence.
12666
12667 declaration-seq:
12668 declaration
12669 declaration-seq declaration */
12670
12671 static void
12672 cp_parser_declaration_seq_opt (cp_parser* parser)
12673 {
12674 while (true)
12675 {
12676 cp_token *token;
12677
12678 token = cp_lexer_peek_token (parser->lexer);
12679
12680 if (token->type == CPP_CLOSE_BRACE
12681 || token->type == CPP_EOF
12682 || token->type == CPP_PRAGMA_EOL)
12683 break;
12684
12685 if (token->type == CPP_SEMICOLON)
12686 {
12687 /* A declaration consisting of a single semicolon is
12688 invalid. Allow it unless we're being pedantic. */
12689 cp_lexer_consume_token (parser->lexer);
12690 if (!in_system_header_at (input_location))
12691 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12692 continue;
12693 }
12694
12695 /* If we're entering or exiting a region that's implicitly
12696 extern "C", modify the lang context appropriately. */
12697 if (!parser->implicit_extern_c && token->implicit_extern_c)
12698 {
12699 push_lang_context (lang_name_c);
12700 parser->implicit_extern_c = true;
12701 }
12702 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12703 {
12704 pop_lang_context ();
12705 parser->implicit_extern_c = false;
12706 }
12707
12708 if (token->type == CPP_PRAGMA)
12709 {
12710 /* A top-level declaration can consist solely of a #pragma.
12711 A nested declaration cannot, so this is done here and not
12712 in cp_parser_declaration. (A #pragma at block scope is
12713 handled in cp_parser_statement.) */
12714 cp_parser_pragma (parser, pragma_external, NULL);
12715 continue;
12716 }
12717
12718 /* Parse the declaration itself. */
12719 cp_parser_declaration (parser);
12720 }
12721 }
12722
12723 /* Parse a declaration.
12724
12725 declaration:
12726 block-declaration
12727 function-definition
12728 template-declaration
12729 explicit-instantiation
12730 explicit-specialization
12731 linkage-specification
12732 namespace-definition
12733
12734 C++17:
12735 deduction-guide
12736
12737 GNU extension:
12738
12739 declaration:
12740 __extension__ declaration */
12741
12742 static void
12743 cp_parser_declaration (cp_parser* parser)
12744 {
12745 cp_token token1;
12746 cp_token token2;
12747 int saved_pedantic;
12748 void *p;
12749 tree attributes = NULL_TREE;
12750
12751 /* Check for the `__extension__' keyword. */
12752 if (cp_parser_extension_opt (parser, &saved_pedantic))
12753 {
12754 /* Parse the qualified declaration. */
12755 cp_parser_declaration (parser);
12756 /* Restore the PEDANTIC flag. */
12757 pedantic = saved_pedantic;
12758
12759 return;
12760 }
12761
12762 /* Try to figure out what kind of declaration is present. */
12763 token1 = *cp_lexer_peek_token (parser->lexer);
12764
12765 if (token1.type != CPP_EOF)
12766 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12767 else
12768 {
12769 token2.type = CPP_EOF;
12770 token2.keyword = RID_MAX;
12771 }
12772
12773 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12774 p = obstack_alloc (&declarator_obstack, 0);
12775
12776 /* If the next token is `extern' and the following token is a string
12777 literal, then we have a linkage specification. */
12778 if (token1.keyword == RID_EXTERN
12779 && cp_parser_is_pure_string_literal (&token2))
12780 cp_parser_linkage_specification (parser);
12781 /* If the next token is `template', then we have either a template
12782 declaration, an explicit instantiation, or an explicit
12783 specialization. */
12784 else if (token1.keyword == RID_TEMPLATE)
12785 {
12786 /* `template <>' indicates a template specialization. */
12787 if (token2.type == CPP_LESS
12788 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12789 cp_parser_explicit_specialization (parser);
12790 /* `template <' indicates a template declaration. */
12791 else if (token2.type == CPP_LESS)
12792 cp_parser_template_declaration (parser, /*member_p=*/false);
12793 /* Anything else must be an explicit instantiation. */
12794 else
12795 cp_parser_explicit_instantiation (parser);
12796 }
12797 /* If the next token is `export', then we have a template
12798 declaration. */
12799 else if (token1.keyword == RID_EXPORT)
12800 cp_parser_template_declaration (parser, /*member_p=*/false);
12801 /* If the next token is `extern', 'static' or 'inline' and the one
12802 after that is `template', we have a GNU extended explicit
12803 instantiation directive. */
12804 else if (cp_parser_allow_gnu_extensions_p (parser)
12805 && (token1.keyword == RID_EXTERN
12806 || token1.keyword == RID_STATIC
12807 || token1.keyword == RID_INLINE)
12808 && token2.keyword == RID_TEMPLATE)
12809 cp_parser_explicit_instantiation (parser);
12810 /* If the next token is `namespace', check for a named or unnamed
12811 namespace definition. */
12812 else if (token1.keyword == RID_NAMESPACE
12813 && (/* A named namespace definition. */
12814 (token2.type == CPP_NAME
12815 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12816 != CPP_EQ))
12817 || (token2.type == CPP_OPEN_SQUARE
12818 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12819 == CPP_OPEN_SQUARE)
12820 /* An unnamed namespace definition. */
12821 || token2.type == CPP_OPEN_BRACE
12822 || token2.keyword == RID_ATTRIBUTE))
12823 cp_parser_namespace_definition (parser);
12824 /* An inline (associated) namespace definition. */
12825 else if (token1.keyword == RID_INLINE
12826 && token2.keyword == RID_NAMESPACE)
12827 cp_parser_namespace_definition (parser);
12828 /* Objective-C++ declaration/definition. */
12829 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12830 cp_parser_objc_declaration (parser, NULL_TREE);
12831 else if (c_dialect_objc ()
12832 && token1.keyword == RID_ATTRIBUTE
12833 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12834 cp_parser_objc_declaration (parser, attributes);
12835 /* At this point we may have a template declared by a concept
12836 introduction. */
12837 else if (flag_concepts
12838 && cp_parser_template_declaration_after_export (parser,
12839 /*member_p=*/false))
12840 /* We did. */;
12841 else
12842 /* Try to parse a block-declaration, or a function-definition. */
12843 cp_parser_block_declaration (parser, /*statement_p=*/false);
12844
12845 /* Free any declarators allocated. */
12846 obstack_free (&declarator_obstack, p);
12847 }
12848
12849 /* Parse a block-declaration.
12850
12851 block-declaration:
12852 simple-declaration
12853 asm-definition
12854 namespace-alias-definition
12855 using-declaration
12856 using-directive
12857
12858 GNU Extension:
12859
12860 block-declaration:
12861 __extension__ block-declaration
12862
12863 C++0x Extension:
12864
12865 block-declaration:
12866 static_assert-declaration
12867
12868 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12869 part of a declaration-statement. */
12870
12871 static void
12872 cp_parser_block_declaration (cp_parser *parser,
12873 bool statement_p)
12874 {
12875 cp_token *token1;
12876 int saved_pedantic;
12877
12878 /* Check for the `__extension__' keyword. */
12879 if (cp_parser_extension_opt (parser, &saved_pedantic))
12880 {
12881 /* Parse the qualified declaration. */
12882 cp_parser_block_declaration (parser, statement_p);
12883 /* Restore the PEDANTIC flag. */
12884 pedantic = saved_pedantic;
12885
12886 return;
12887 }
12888
12889 /* Peek at the next token to figure out which kind of declaration is
12890 present. */
12891 token1 = cp_lexer_peek_token (parser->lexer);
12892
12893 /* If the next keyword is `asm', we have an asm-definition. */
12894 if (token1->keyword == RID_ASM)
12895 {
12896 if (statement_p)
12897 cp_parser_commit_to_tentative_parse (parser);
12898 cp_parser_asm_definition (parser);
12899 }
12900 /* If the next keyword is `namespace', we have a
12901 namespace-alias-definition. */
12902 else if (token1->keyword == RID_NAMESPACE)
12903 cp_parser_namespace_alias_definition (parser);
12904 /* If the next keyword is `using', we have a
12905 using-declaration, a using-directive, or an alias-declaration. */
12906 else if (token1->keyword == RID_USING)
12907 {
12908 cp_token *token2;
12909
12910 if (statement_p)
12911 cp_parser_commit_to_tentative_parse (parser);
12912 /* If the token after `using' is `namespace', then we have a
12913 using-directive. */
12914 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12915 if (token2->keyword == RID_NAMESPACE)
12916 cp_parser_using_directive (parser);
12917 /* If the second token after 'using' is '=', then we have an
12918 alias-declaration. */
12919 else if (cxx_dialect >= cxx11
12920 && token2->type == CPP_NAME
12921 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12922 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12923 cp_parser_alias_declaration (parser);
12924 /* Otherwise, it's a using-declaration. */
12925 else
12926 cp_parser_using_declaration (parser,
12927 /*access_declaration_p=*/false);
12928 }
12929 /* If the next keyword is `__label__' we have a misplaced label
12930 declaration. */
12931 else if (token1->keyword == RID_LABEL)
12932 {
12933 cp_lexer_consume_token (parser->lexer);
12934 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12935 cp_parser_skip_to_end_of_statement (parser);
12936 /* If the next token is now a `;', consume it. */
12937 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12938 cp_lexer_consume_token (parser->lexer);
12939 }
12940 /* If the next token is `static_assert' we have a static assertion. */
12941 else if (token1->keyword == RID_STATIC_ASSERT)
12942 cp_parser_static_assert (parser, /*member_p=*/false);
12943 /* Anything else must be a simple-declaration. */
12944 else
12945 cp_parser_simple_declaration (parser, !statement_p,
12946 /*maybe_range_for_decl*/NULL);
12947 }
12948
12949 /* Parse a simple-declaration.
12950
12951 simple-declaration:
12952 decl-specifier-seq [opt] init-declarator-list [opt] ;
12953 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12954 brace-or-equal-initializer ;
12955
12956 init-declarator-list:
12957 init-declarator
12958 init-declarator-list , init-declarator
12959
12960 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12961 function-definition as a simple-declaration.
12962
12963 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12964 parsed declaration if it is an uninitialized single declarator not followed
12965 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12966 if present, will not be consumed. */
12967
12968 static void
12969 cp_parser_simple_declaration (cp_parser* parser,
12970 bool function_definition_allowed_p,
12971 tree *maybe_range_for_decl)
12972 {
12973 cp_decl_specifier_seq decl_specifiers;
12974 int declares_class_or_enum;
12975 bool saw_declarator;
12976 location_t comma_loc = UNKNOWN_LOCATION;
12977 location_t init_loc = UNKNOWN_LOCATION;
12978
12979 if (maybe_range_for_decl)
12980 *maybe_range_for_decl = NULL_TREE;
12981
12982 /* Defer access checks until we know what is being declared; the
12983 checks for names appearing in the decl-specifier-seq should be
12984 done as if we were in the scope of the thing being declared. */
12985 push_deferring_access_checks (dk_deferred);
12986
12987 /* Parse the decl-specifier-seq. We have to keep track of whether
12988 or not the decl-specifier-seq declares a named class or
12989 enumeration type, since that is the only case in which the
12990 init-declarator-list is allowed to be empty.
12991
12992 [dcl.dcl]
12993
12994 In a simple-declaration, the optional init-declarator-list can be
12995 omitted only when declaring a class or enumeration, that is when
12996 the decl-specifier-seq contains either a class-specifier, an
12997 elaborated-type-specifier, or an enum-specifier. */
12998 cp_parser_decl_specifier_seq (parser,
12999 CP_PARSER_FLAGS_OPTIONAL,
13000 &decl_specifiers,
13001 &declares_class_or_enum);
13002 /* We no longer need to defer access checks. */
13003 stop_deferring_access_checks ();
13004
13005 /* In a block scope, a valid declaration must always have a
13006 decl-specifier-seq. By not trying to parse declarators, we can
13007 resolve the declaration/expression ambiguity more quickly. */
13008 if (!function_definition_allowed_p
13009 && !decl_specifiers.any_specifiers_p)
13010 {
13011 cp_parser_error (parser, "expected declaration");
13012 goto done;
13013 }
13014
13015 /* If the next two tokens are both identifiers, the code is
13016 erroneous. The usual cause of this situation is code like:
13017
13018 T t;
13019
13020 where "T" should name a type -- but does not. */
13021 if (!decl_specifiers.any_type_specifiers_p
13022 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13023 {
13024 /* If parsing tentatively, we should commit; we really are
13025 looking at a declaration. */
13026 cp_parser_commit_to_tentative_parse (parser);
13027 /* Give up. */
13028 goto done;
13029 }
13030
13031 /* If we have seen at least one decl-specifier, and the next token
13032 is not a parenthesis, then we must be looking at a declaration.
13033 (After "int (" we might be looking at a functional cast.) */
13034 if (decl_specifiers.any_specifiers_p
13035 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13036 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13037 && !cp_parser_error_occurred (parser))
13038 cp_parser_commit_to_tentative_parse (parser);
13039
13040 /* Look for C++17 decomposition declaration. */
13041 for (size_t n = 1; ; n++)
13042 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13043 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13044 continue;
13045 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13046 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13047 && decl_specifiers.any_specifiers_p)
13048 {
13049 tree decl
13050 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13051 maybe_range_for_decl,
13052 &init_loc);
13053
13054 /* The next token should be either a `,' or a `;'. */
13055 cp_token *token = cp_lexer_peek_token (parser->lexer);
13056 /* If it's a `;', we are done. */
13057 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
13058 goto finish;
13059 /* Anything else is an error. */
13060 else
13061 {
13062 /* If we have already issued an error message we don't need
13063 to issue another one. */
13064 if ((decl != error_mark_node
13065 && DECL_INITIAL (decl) != error_mark_node)
13066 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13067 cp_parser_error (parser, "expected %<,%> or %<;%>");
13068 /* Skip tokens until we reach the end of the statement. */
13069 cp_parser_skip_to_end_of_statement (parser);
13070 /* If the next token is now a `;', consume it. */
13071 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13072 cp_lexer_consume_token (parser->lexer);
13073 goto done;
13074 }
13075 }
13076 else
13077 break;
13078
13079 tree last_type;
13080 bool auto_specifier_p;
13081 /* NULL_TREE if both variable and function declaration are allowed,
13082 error_mark_node if function declaration are not allowed and
13083 a FUNCTION_DECL that should be diagnosed if it is followed by
13084 variable declarations. */
13085 tree auto_function_declaration;
13086
13087 last_type = NULL_TREE;
13088 auto_specifier_p
13089 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13090 auto_function_declaration = NULL_TREE;
13091
13092 /* Keep going until we hit the `;' at the end of the simple
13093 declaration. */
13094 saw_declarator = false;
13095 while (cp_lexer_next_token_is_not (parser->lexer,
13096 CPP_SEMICOLON))
13097 {
13098 cp_token *token;
13099 bool function_definition_p;
13100 tree decl;
13101 tree auto_result = NULL_TREE;
13102
13103 if (saw_declarator)
13104 {
13105 /* If we are processing next declarator, comma is expected */
13106 token = cp_lexer_peek_token (parser->lexer);
13107 gcc_assert (token->type == CPP_COMMA);
13108 cp_lexer_consume_token (parser->lexer);
13109 if (maybe_range_for_decl)
13110 {
13111 *maybe_range_for_decl = error_mark_node;
13112 if (comma_loc == UNKNOWN_LOCATION)
13113 comma_loc = token->location;
13114 }
13115 }
13116 else
13117 saw_declarator = true;
13118
13119 /* Parse the init-declarator. */
13120 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13121 /*checks=*/NULL,
13122 function_definition_allowed_p,
13123 /*member_p=*/false,
13124 declares_class_or_enum,
13125 &function_definition_p,
13126 maybe_range_for_decl,
13127 &init_loc,
13128 &auto_result);
13129 /* If an error occurred while parsing tentatively, exit quickly.
13130 (That usually happens when in the body of a function; each
13131 statement is treated as a declaration-statement until proven
13132 otherwise.) */
13133 if (cp_parser_error_occurred (parser))
13134 goto done;
13135
13136 if (auto_specifier_p && cxx_dialect >= cxx14)
13137 {
13138 /* If the init-declarator-list contains more than one
13139 init-declarator, they shall all form declarations of
13140 variables. */
13141 if (auto_function_declaration == NULL_TREE)
13142 auto_function_declaration
13143 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13144 else if (TREE_CODE (decl) == FUNCTION_DECL
13145 || auto_function_declaration != error_mark_node)
13146 {
13147 error_at (decl_specifiers.locations[ds_type_spec],
13148 "non-variable %qD in declaration with more than one "
13149 "declarator with placeholder type",
13150 TREE_CODE (decl) == FUNCTION_DECL
13151 ? decl : auto_function_declaration);
13152 auto_function_declaration = error_mark_node;
13153 }
13154 }
13155
13156 if (auto_result
13157 && (!processing_template_decl || !type_uses_auto (auto_result)))
13158 {
13159 if (last_type
13160 && last_type != error_mark_node
13161 && !same_type_p (auto_result, last_type))
13162 {
13163 /* If the list of declarators contains more than one declarator,
13164 the type of each declared variable is determined as described
13165 above. If the type deduced for the template parameter U is not
13166 the same in each deduction, the program is ill-formed. */
13167 error_at (decl_specifiers.locations[ds_type_spec],
13168 "inconsistent deduction for %qT: %qT and then %qT",
13169 decl_specifiers.type, last_type, auto_result);
13170 last_type = error_mark_node;
13171 }
13172 else
13173 last_type = auto_result;
13174 }
13175
13176 /* Handle function definitions specially. */
13177 if (function_definition_p)
13178 {
13179 /* If the next token is a `,', then we are probably
13180 processing something like:
13181
13182 void f() {}, *p;
13183
13184 which is erroneous. */
13185 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13186 {
13187 cp_token *token = cp_lexer_peek_token (parser->lexer);
13188 error_at (token->location,
13189 "mixing"
13190 " declarations and function-definitions is forbidden");
13191 }
13192 /* Otherwise, we're done with the list of declarators. */
13193 else
13194 {
13195 pop_deferring_access_checks ();
13196 return;
13197 }
13198 }
13199 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13200 *maybe_range_for_decl = decl;
13201 /* The next token should be either a `,' or a `;'. */
13202 token = cp_lexer_peek_token (parser->lexer);
13203 /* If it's a `,', there are more declarators to come. */
13204 if (token->type == CPP_COMMA)
13205 /* will be consumed next time around */;
13206 /* If it's a `;', we are done. */
13207 else if (token->type == CPP_SEMICOLON)
13208 break;
13209 else if (maybe_range_for_decl)
13210 {
13211 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13212 permerror (decl_specifiers.locations[ds_type_spec],
13213 "types may not be defined in a for-range-declaration");
13214 break;
13215 }
13216 /* Anything else is an error. */
13217 else
13218 {
13219 /* If we have already issued an error message we don't need
13220 to issue another one. */
13221 if ((decl != error_mark_node
13222 && DECL_INITIAL (decl) != error_mark_node)
13223 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13224 cp_parser_error (parser, "expected %<,%> or %<;%>");
13225 /* Skip tokens until we reach the end of the statement. */
13226 cp_parser_skip_to_end_of_statement (parser);
13227 /* If the next token is now a `;', consume it. */
13228 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13229 cp_lexer_consume_token (parser->lexer);
13230 goto done;
13231 }
13232 /* After the first time around, a function-definition is not
13233 allowed -- even if it was OK at first. For example:
13234
13235 int i, f() {}
13236
13237 is not valid. */
13238 function_definition_allowed_p = false;
13239 }
13240
13241 /* Issue an error message if no declarators are present, and the
13242 decl-specifier-seq does not itself declare a class or
13243 enumeration: [dcl.dcl]/3. */
13244 if (!saw_declarator)
13245 {
13246 if (cp_parser_declares_only_class_p (parser))
13247 {
13248 if (!declares_class_or_enum
13249 && decl_specifiers.type
13250 && OVERLOAD_TYPE_P (decl_specifiers.type))
13251 /* Ensure an error is issued anyway when finish_decltype_type,
13252 called via cp_parser_decl_specifier_seq, returns a class or
13253 an enumeration (c++/51786). */
13254 decl_specifiers.type = NULL_TREE;
13255 shadow_tag (&decl_specifiers);
13256 }
13257 /* Perform any deferred access checks. */
13258 perform_deferred_access_checks (tf_warning_or_error);
13259 }
13260
13261 /* Consume the `;'. */
13262 finish:
13263 if (!maybe_range_for_decl)
13264 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13265 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13266 {
13267 if (init_loc != UNKNOWN_LOCATION)
13268 error_at (init_loc, "initializer in range-based %<for%> loop");
13269 if (comma_loc != UNKNOWN_LOCATION)
13270 error_at (comma_loc,
13271 "multiple declarations in range-based %<for%> loop");
13272 }
13273
13274 done:
13275 pop_deferring_access_checks ();
13276 }
13277
13278 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13279 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13280 initializer ; */
13281
13282 static tree
13283 cp_parser_decomposition_declaration (cp_parser *parser,
13284 cp_decl_specifier_seq *decl_specifiers,
13285 tree *maybe_range_for_decl,
13286 location_t *init_loc)
13287 {
13288 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13289 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13290 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13291
13292 /* Parse the identifier-list. */
13293 auto_vec<cp_expr, 10> v;
13294 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13295 while (true)
13296 {
13297 cp_expr e = cp_parser_identifier (parser);
13298 if (e.get_value () == error_mark_node)
13299 break;
13300 v.safe_push (e);
13301 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13302 break;
13303 cp_lexer_consume_token (parser->lexer);
13304 }
13305
13306 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13307 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13308 {
13309 end_loc = UNKNOWN_LOCATION;
13310 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13311 false);
13312 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13313 cp_lexer_consume_token (parser->lexer);
13314 else
13315 {
13316 cp_parser_skip_to_end_of_statement (parser);
13317 return error_mark_node;
13318 }
13319 }
13320
13321 if (cxx_dialect < cxx17)
13322 pedwarn (loc, 0, "structured bindings only available with "
13323 "-std=c++17 or -std=gnu++17");
13324
13325 tree pushed_scope;
13326 cp_declarator *declarator = make_declarator (cdk_decomp);
13327 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13328 declarator->id_loc = loc;
13329 if (ref_qual != REF_QUAL_NONE)
13330 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13331 ref_qual == REF_QUAL_RVALUE,
13332 NULL_TREE);
13333 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13334 NULL_TREE, decl_specifiers->attributes,
13335 &pushed_scope);
13336 tree orig_decl = decl;
13337
13338 unsigned int i;
13339 cp_expr e;
13340 cp_decl_specifier_seq decl_specs;
13341 clear_decl_specs (&decl_specs);
13342 decl_specs.type = make_auto ();
13343 tree prev = decl;
13344 FOR_EACH_VEC_ELT (v, i, e)
13345 {
13346 if (i == 0)
13347 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13348 else
13349 declarator->u.id.unqualified_name = e.get_value ();
13350 declarator->id_loc = e.get_location ();
13351 tree elt_pushed_scope;
13352 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13353 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13354 if (decl2 == error_mark_node)
13355 decl = error_mark_node;
13356 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13357 {
13358 /* Ensure we've diagnosed redeclaration if we aren't creating
13359 a new VAR_DECL. */
13360 gcc_assert (errorcount);
13361 decl = error_mark_node;
13362 }
13363 else
13364 prev = decl2;
13365 if (elt_pushed_scope)
13366 pop_scope (elt_pushed_scope);
13367 }
13368
13369 if (v.is_empty ())
13370 {
13371 error_at (loc, "empty structured binding declaration");
13372 decl = error_mark_node;
13373 }
13374
13375 if (maybe_range_for_decl == NULL
13376 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13377 {
13378 bool non_constant_p = false, is_direct_init = false;
13379 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13380 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13381 &non_constant_p);
13382 if (initializer == NULL_TREE
13383 || (TREE_CODE (initializer) == TREE_LIST
13384 && TREE_CHAIN (initializer))
13385 || (is_direct_init
13386 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13387 && CONSTRUCTOR_NELTS (initializer) != 1))
13388 {
13389 error_at (loc, "invalid initializer for structured binding "
13390 "declaration");
13391 initializer = error_mark_node;
13392 }
13393
13394 if (decl != error_mark_node)
13395 {
13396 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13397 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13398 cp_finish_decomp (decl, prev, v.length ());
13399 }
13400 }
13401 else if (decl != error_mark_node)
13402 {
13403 *maybe_range_for_decl = prev;
13404 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13405 the underlying DECL. */
13406 cp_finish_decomp (decl, prev, v.length ());
13407 }
13408
13409 if (pushed_scope)
13410 pop_scope (pushed_scope);
13411
13412 if (decl == error_mark_node && DECL_P (orig_decl))
13413 {
13414 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13415 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13416 }
13417
13418 return decl;
13419 }
13420
13421 /* Parse a decl-specifier-seq.
13422
13423 decl-specifier-seq:
13424 decl-specifier-seq [opt] decl-specifier
13425 decl-specifier attribute-specifier-seq [opt] (C++11)
13426
13427 decl-specifier:
13428 storage-class-specifier
13429 type-specifier
13430 function-specifier
13431 friend
13432 typedef
13433
13434 GNU Extension:
13435
13436 decl-specifier:
13437 attributes
13438
13439 Concepts Extension:
13440
13441 decl-specifier:
13442 concept
13443
13444 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13445
13446 The parser flags FLAGS is used to control type-specifier parsing.
13447
13448 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13449 flags:
13450
13451 1: one of the decl-specifiers is an elaborated-type-specifier
13452 (i.e., a type declaration)
13453 2: one of the decl-specifiers is an enum-specifier or a
13454 class-specifier (i.e., a type definition)
13455
13456 */
13457
13458 static void
13459 cp_parser_decl_specifier_seq (cp_parser* parser,
13460 cp_parser_flags flags,
13461 cp_decl_specifier_seq *decl_specs,
13462 int* declares_class_or_enum)
13463 {
13464 bool constructor_possible_p = !parser->in_declarator_p;
13465 bool found_decl_spec = false;
13466 cp_token *start_token = NULL;
13467 cp_decl_spec ds;
13468
13469 /* Clear DECL_SPECS. */
13470 clear_decl_specs (decl_specs);
13471
13472 /* Assume no class or enumeration type is declared. */
13473 *declares_class_or_enum = 0;
13474
13475 /* Keep reading specifiers until there are no more to read. */
13476 while (true)
13477 {
13478 bool constructor_p;
13479 cp_token *token;
13480 ds = ds_last;
13481
13482 /* Peek at the next token. */
13483 token = cp_lexer_peek_token (parser->lexer);
13484
13485 /* Save the first token of the decl spec list for error
13486 reporting. */
13487 if (!start_token)
13488 start_token = token;
13489 /* Handle attributes. */
13490 if (cp_next_tokens_can_be_attribute_p (parser))
13491 {
13492 /* Parse the attributes. */
13493 tree attrs = cp_parser_attributes_opt (parser);
13494
13495 /* In a sequence of declaration specifiers, c++11 attributes
13496 appertain to the type that precede them. In that case
13497 [dcl.spec]/1 says:
13498
13499 The attribute-specifier-seq affects the type only for
13500 the declaration it appears in, not other declarations
13501 involving the same type.
13502
13503 But for now let's force the user to position the
13504 attribute either at the beginning of the declaration or
13505 after the declarator-id, which would clearly mean that it
13506 applies to the declarator. */
13507 if (cxx11_attribute_p (attrs))
13508 {
13509 if (!found_decl_spec)
13510 /* The c++11 attribute is at the beginning of the
13511 declaration. It appertains to the entity being
13512 declared. */;
13513 else
13514 {
13515 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13516 {
13517 /* This is an attribute following a
13518 class-specifier. */
13519 if (decl_specs->type_definition_p)
13520 warn_misplaced_attr_for_class_type (token->location,
13521 decl_specs->type);
13522 attrs = NULL_TREE;
13523 }
13524 else
13525 {
13526 decl_specs->std_attributes
13527 = chainon (decl_specs->std_attributes,
13528 attrs);
13529 if (decl_specs->locations[ds_std_attribute] == 0)
13530 decl_specs->locations[ds_std_attribute] = token->location;
13531 }
13532 continue;
13533 }
13534 }
13535
13536 decl_specs->attributes
13537 = chainon (decl_specs->attributes,
13538 attrs);
13539 if (decl_specs->locations[ds_attribute] == 0)
13540 decl_specs->locations[ds_attribute] = token->location;
13541 continue;
13542 }
13543 /* Assume we will find a decl-specifier keyword. */
13544 found_decl_spec = true;
13545 /* If the next token is an appropriate keyword, we can simply
13546 add it to the list. */
13547 switch (token->keyword)
13548 {
13549 /* decl-specifier:
13550 friend
13551 constexpr */
13552 case RID_FRIEND:
13553 if (!at_class_scope_p ())
13554 {
13555 gcc_rich_location richloc (token->location);
13556 richloc.add_fixit_remove ();
13557 error_at (&richloc, "%<friend%> used outside of class");
13558 cp_lexer_purge_token (parser->lexer);
13559 }
13560 else
13561 {
13562 ds = ds_friend;
13563 /* Consume the token. */
13564 cp_lexer_consume_token (parser->lexer);
13565 }
13566 break;
13567
13568 case RID_CONSTEXPR:
13569 ds = ds_constexpr;
13570 cp_lexer_consume_token (parser->lexer);
13571 break;
13572
13573 case RID_CONCEPT:
13574 ds = ds_concept;
13575 cp_lexer_consume_token (parser->lexer);
13576 break;
13577
13578 /* function-specifier:
13579 inline
13580 virtual
13581 explicit */
13582 case RID_INLINE:
13583 case RID_VIRTUAL:
13584 case RID_EXPLICIT:
13585 cp_parser_function_specifier_opt (parser, decl_specs);
13586 break;
13587
13588 /* decl-specifier:
13589 typedef */
13590 case RID_TYPEDEF:
13591 ds = ds_typedef;
13592 /* Consume the token. */
13593 cp_lexer_consume_token (parser->lexer);
13594 /* A constructor declarator cannot appear in a typedef. */
13595 constructor_possible_p = false;
13596 /* The "typedef" keyword can only occur in a declaration; we
13597 may as well commit at this point. */
13598 cp_parser_commit_to_tentative_parse (parser);
13599
13600 if (decl_specs->storage_class != sc_none)
13601 decl_specs->conflicting_specifiers_p = true;
13602 break;
13603
13604 /* storage-class-specifier:
13605 auto
13606 register
13607 static
13608 extern
13609 mutable
13610
13611 GNU Extension:
13612 thread */
13613 case RID_AUTO:
13614 if (cxx_dialect == cxx98)
13615 {
13616 /* Consume the token. */
13617 cp_lexer_consume_token (parser->lexer);
13618
13619 /* Complain about `auto' as a storage specifier, if
13620 we're complaining about C++0x compatibility. */
13621 gcc_rich_location richloc (token->location);
13622 richloc.add_fixit_remove ();
13623 warning_at (&richloc, OPT_Wc__11_compat,
13624 "%<auto%> changes meaning in C++11; "
13625 "please remove it");
13626
13627 /* Set the storage class anyway. */
13628 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13629 token);
13630 }
13631 else
13632 /* C++0x auto type-specifier. */
13633 found_decl_spec = false;
13634 break;
13635
13636 case RID_REGISTER:
13637 case RID_STATIC:
13638 case RID_EXTERN:
13639 case RID_MUTABLE:
13640 /* Consume the token. */
13641 cp_lexer_consume_token (parser->lexer);
13642 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13643 token);
13644 break;
13645 case RID_THREAD:
13646 /* Consume the token. */
13647 ds = ds_thread;
13648 cp_lexer_consume_token (parser->lexer);
13649 break;
13650
13651 default:
13652 /* We did not yet find a decl-specifier yet. */
13653 found_decl_spec = false;
13654 break;
13655 }
13656
13657 if (found_decl_spec
13658 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13659 && token->keyword != RID_CONSTEXPR)
13660 error ("decl-specifier invalid in condition");
13661
13662 if (found_decl_spec
13663 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13664 && token->keyword != RID_MUTABLE
13665 && token->keyword != RID_CONSTEXPR)
13666 error_at (token->location, "%qD invalid in lambda",
13667 ridpointers[token->keyword]);
13668
13669 if (ds != ds_last)
13670 set_and_check_decl_spec_loc (decl_specs, ds, token);
13671
13672 /* Constructors are a special case. The `S' in `S()' is not a
13673 decl-specifier; it is the beginning of the declarator. */
13674 constructor_p
13675 = (!found_decl_spec
13676 && constructor_possible_p
13677 && (cp_parser_constructor_declarator_p
13678 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13679
13680 /* If we don't have a DECL_SPEC yet, then we must be looking at
13681 a type-specifier. */
13682 if (!found_decl_spec && !constructor_p)
13683 {
13684 int decl_spec_declares_class_or_enum;
13685 bool is_cv_qualifier;
13686 tree type_spec;
13687
13688 type_spec
13689 = cp_parser_type_specifier (parser, flags,
13690 decl_specs,
13691 /*is_declaration=*/true,
13692 &decl_spec_declares_class_or_enum,
13693 &is_cv_qualifier);
13694 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13695
13696 /* If this type-specifier referenced a user-defined type
13697 (a typedef, class-name, etc.), then we can't allow any
13698 more such type-specifiers henceforth.
13699
13700 [dcl.spec]
13701
13702 The longest sequence of decl-specifiers that could
13703 possibly be a type name is taken as the
13704 decl-specifier-seq of a declaration. The sequence shall
13705 be self-consistent as described below.
13706
13707 [dcl.type]
13708
13709 As a general rule, at most one type-specifier is allowed
13710 in the complete decl-specifier-seq of a declaration. The
13711 only exceptions are the following:
13712
13713 -- const or volatile can be combined with any other
13714 type-specifier.
13715
13716 -- signed or unsigned can be combined with char, long,
13717 short, or int.
13718
13719 -- ..
13720
13721 Example:
13722
13723 typedef char* Pc;
13724 void g (const int Pc);
13725
13726 Here, Pc is *not* part of the decl-specifier seq; it's
13727 the declarator. Therefore, once we see a type-specifier
13728 (other than a cv-qualifier), we forbid any additional
13729 user-defined types. We *do* still allow things like `int
13730 int' to be considered a decl-specifier-seq, and issue the
13731 error message later. */
13732 if (type_spec && !is_cv_qualifier)
13733 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13734 /* A constructor declarator cannot follow a type-specifier. */
13735 if (type_spec)
13736 {
13737 constructor_possible_p = false;
13738 found_decl_spec = true;
13739 if (!is_cv_qualifier)
13740 decl_specs->any_type_specifiers_p = true;
13741 }
13742 }
13743
13744 /* If we still do not have a DECL_SPEC, then there are no more
13745 decl-specifiers. */
13746 if (!found_decl_spec)
13747 break;
13748
13749 decl_specs->any_specifiers_p = true;
13750 /* After we see one decl-specifier, further decl-specifiers are
13751 always optional. */
13752 flags |= CP_PARSER_FLAGS_OPTIONAL;
13753 }
13754
13755 /* Don't allow a friend specifier with a class definition. */
13756 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13757 && (*declares_class_or_enum & 2))
13758 error_at (decl_specs->locations[ds_friend],
13759 "class definition may not be declared a friend");
13760 }
13761
13762 /* Parse an (optional) storage-class-specifier.
13763
13764 storage-class-specifier:
13765 auto
13766 register
13767 static
13768 extern
13769 mutable
13770
13771 GNU Extension:
13772
13773 storage-class-specifier:
13774 thread
13775
13776 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13777
13778 static tree
13779 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13780 {
13781 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13782 {
13783 case RID_AUTO:
13784 if (cxx_dialect != cxx98)
13785 return NULL_TREE;
13786 /* Fall through for C++98. */
13787 gcc_fallthrough ();
13788
13789 case RID_REGISTER:
13790 case RID_STATIC:
13791 case RID_EXTERN:
13792 case RID_MUTABLE:
13793 case RID_THREAD:
13794 /* Consume the token. */
13795 return cp_lexer_consume_token (parser->lexer)->u.value;
13796
13797 default:
13798 return NULL_TREE;
13799 }
13800 }
13801
13802 /* Parse an (optional) function-specifier.
13803
13804 function-specifier:
13805 inline
13806 virtual
13807 explicit
13808
13809 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13810 Updates DECL_SPECS, if it is non-NULL. */
13811
13812 static tree
13813 cp_parser_function_specifier_opt (cp_parser* parser,
13814 cp_decl_specifier_seq *decl_specs)
13815 {
13816 cp_token *token = cp_lexer_peek_token (parser->lexer);
13817 switch (token->keyword)
13818 {
13819 case RID_INLINE:
13820 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13821 break;
13822
13823 case RID_VIRTUAL:
13824 /* 14.5.2.3 [temp.mem]
13825
13826 A member function template shall not be virtual. */
13827 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13828 && current_class_type)
13829 error_at (token->location, "templates may not be %<virtual%>");
13830 else
13831 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13832 break;
13833
13834 case RID_EXPLICIT:
13835 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13836 break;
13837
13838 default:
13839 return NULL_TREE;
13840 }
13841
13842 /* Consume the token. */
13843 return cp_lexer_consume_token (parser->lexer)->u.value;
13844 }
13845
13846 /* Parse a linkage-specification.
13847
13848 linkage-specification:
13849 extern string-literal { declaration-seq [opt] }
13850 extern string-literal declaration */
13851
13852 static void
13853 cp_parser_linkage_specification (cp_parser* parser)
13854 {
13855 tree linkage;
13856
13857 /* Look for the `extern' keyword. */
13858 cp_token *extern_token
13859 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13860
13861 /* Look for the string-literal. */
13862 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13863 linkage = cp_parser_string_literal (parser, false, false);
13864
13865 /* Transform the literal into an identifier. If the literal is a
13866 wide-character string, or contains embedded NULs, then we can't
13867 handle it as the user wants. */
13868 if (strlen (TREE_STRING_POINTER (linkage))
13869 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13870 {
13871 cp_parser_error (parser, "invalid linkage-specification");
13872 /* Assume C++ linkage. */
13873 linkage = lang_name_cplusplus;
13874 }
13875 else
13876 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13877
13878 /* We're now using the new linkage. */
13879 push_lang_context (linkage);
13880
13881 /* Preserve the location of the the innermost linkage specification,
13882 tracking the locations of nested specifications via a local. */
13883 location_t saved_location
13884 = parser->innermost_linkage_specification_location;
13885 /* Construct a location ranging from the start of the "extern" to
13886 the end of the string-literal, with the caret at the start, e.g.:
13887 extern "C" {
13888 ^~~~~~~~~~
13889 */
13890 parser->innermost_linkage_specification_location
13891 = make_location (extern_token->location,
13892 extern_token->location,
13893 get_finish (string_token->location));
13894
13895 /* If the next token is a `{', then we're using the first
13896 production. */
13897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13898 {
13899 cp_ensure_no_omp_declare_simd (parser);
13900 cp_ensure_no_oacc_routine (parser);
13901
13902 /* Consume the `{' token. */
13903 matching_braces braces;
13904 braces.consume_open (parser)->location;
13905 /* Parse the declarations. */
13906 cp_parser_declaration_seq_opt (parser);
13907 /* Look for the closing `}'. */
13908 braces.require_close (parser);
13909 }
13910 /* Otherwise, there's just one declaration. */
13911 else
13912 {
13913 bool saved_in_unbraced_linkage_specification_p;
13914
13915 saved_in_unbraced_linkage_specification_p
13916 = parser->in_unbraced_linkage_specification_p;
13917 parser->in_unbraced_linkage_specification_p = true;
13918 cp_parser_declaration (parser);
13919 parser->in_unbraced_linkage_specification_p
13920 = saved_in_unbraced_linkage_specification_p;
13921 }
13922
13923 /* We're done with the linkage-specification. */
13924 pop_lang_context ();
13925
13926 /* Restore location of parent linkage specification, if any. */
13927 parser->innermost_linkage_specification_location = saved_location;
13928 }
13929
13930 /* Parse a static_assert-declaration.
13931
13932 static_assert-declaration:
13933 static_assert ( constant-expression , string-literal ) ;
13934 static_assert ( constant-expression ) ; (C++17)
13935
13936 If MEMBER_P, this static_assert is a class member. */
13937
13938 static void
13939 cp_parser_static_assert(cp_parser *parser, bool member_p)
13940 {
13941 tree condition;
13942 tree message;
13943 cp_token *token;
13944 location_t saved_loc;
13945 bool dummy;
13946
13947 /* Peek at the `static_assert' token so we can keep track of exactly
13948 where the static assertion started. */
13949 token = cp_lexer_peek_token (parser->lexer);
13950 saved_loc = token->location;
13951
13952 /* Look for the `static_assert' keyword. */
13953 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13954 RT_STATIC_ASSERT))
13955 return;
13956
13957 /* We know we are in a static assertion; commit to any tentative
13958 parse. */
13959 if (cp_parser_parsing_tentatively (parser))
13960 cp_parser_commit_to_tentative_parse (parser);
13961
13962 /* Parse the `(' starting the static assertion condition. */
13963 matching_parens parens;
13964 parens.require_open (parser);
13965
13966 /* Parse the constant-expression. Allow a non-constant expression
13967 here in order to give better diagnostics in finish_static_assert. */
13968 condition =
13969 cp_parser_constant_expression (parser,
13970 /*allow_non_constant_p=*/true,
13971 /*non_constant_p=*/&dummy);
13972
13973 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13974 {
13975 if (cxx_dialect < cxx17)
13976 pedwarn (input_location, OPT_Wpedantic,
13977 "static_assert without a message "
13978 "only available with -std=c++17 or -std=gnu++17");
13979 /* Eat the ')' */
13980 cp_lexer_consume_token (parser->lexer);
13981 message = build_string (1, "");
13982 TREE_TYPE (message) = char_array_type_node;
13983 fix_string_type (message);
13984 }
13985 else
13986 {
13987 /* Parse the separating `,'. */
13988 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13989
13990 /* Parse the string-literal message. */
13991 message = cp_parser_string_literal (parser,
13992 /*translate=*/false,
13993 /*wide_ok=*/true);
13994
13995 /* A `)' completes the static assertion. */
13996 if (!parens.require_close (parser))
13997 cp_parser_skip_to_closing_parenthesis (parser,
13998 /*recovering=*/true,
13999 /*or_comma=*/false,
14000 /*consume_paren=*/true);
14001 }
14002
14003 /* A semicolon terminates the declaration. */
14004 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14005
14006 /* Complete the static assertion, which may mean either processing
14007 the static assert now or saving it for template instantiation. */
14008 finish_static_assert (condition, message, saved_loc, member_p);
14009 }
14010
14011 /* Parse the expression in decltype ( expression ). */
14012
14013 static tree
14014 cp_parser_decltype_expr (cp_parser *parser,
14015 bool &id_expression_or_member_access_p)
14016 {
14017 cp_token *id_expr_start_token;
14018 tree expr;
14019
14020 /* Since we're going to preserve any side-effects from this parse, set up a
14021 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14022 in the expression. */
14023 tentative_firewall firewall (parser);
14024
14025 /* First, try parsing an id-expression. */
14026 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14027 cp_parser_parse_tentatively (parser);
14028 expr = cp_parser_id_expression (parser,
14029 /*template_keyword_p=*/false,
14030 /*check_dependency_p=*/true,
14031 /*template_p=*/NULL,
14032 /*declarator_p=*/false,
14033 /*optional_p=*/false);
14034
14035 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14036 {
14037 bool non_integral_constant_expression_p = false;
14038 tree id_expression = expr;
14039 cp_id_kind idk;
14040 const char *error_msg;
14041
14042 if (identifier_p (expr))
14043 /* Lookup the name we got back from the id-expression. */
14044 expr = cp_parser_lookup_name_simple (parser, expr,
14045 id_expr_start_token->location);
14046
14047 if (expr
14048 && expr != error_mark_node
14049 && TREE_CODE (expr) != TYPE_DECL
14050 && (TREE_CODE (expr) != BIT_NOT_EXPR
14051 || !TYPE_P (TREE_OPERAND (expr, 0)))
14052 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14053 {
14054 /* Complete lookup of the id-expression. */
14055 expr = (finish_id_expression
14056 (id_expression, expr, parser->scope, &idk,
14057 /*integral_constant_expression_p=*/false,
14058 /*allow_non_integral_constant_expression_p=*/true,
14059 &non_integral_constant_expression_p,
14060 /*template_p=*/false,
14061 /*done=*/true,
14062 /*address_p=*/false,
14063 /*template_arg_p=*/false,
14064 &error_msg,
14065 id_expr_start_token->location));
14066
14067 if (expr == error_mark_node)
14068 /* We found an id-expression, but it was something that we
14069 should not have found. This is an error, not something
14070 we can recover from, so note that we found an
14071 id-expression and we'll recover as gracefully as
14072 possible. */
14073 id_expression_or_member_access_p = true;
14074 }
14075
14076 if (expr
14077 && expr != error_mark_node
14078 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14079 /* We have an id-expression. */
14080 id_expression_or_member_access_p = true;
14081 }
14082
14083 if (!id_expression_or_member_access_p)
14084 {
14085 /* Abort the id-expression parse. */
14086 cp_parser_abort_tentative_parse (parser);
14087
14088 /* Parsing tentatively, again. */
14089 cp_parser_parse_tentatively (parser);
14090
14091 /* Parse a class member access. */
14092 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14093 /*cast_p=*/false, /*decltype*/true,
14094 /*member_access_only_p=*/true, NULL);
14095
14096 if (expr
14097 && expr != error_mark_node
14098 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14099 /* We have an id-expression. */
14100 id_expression_or_member_access_p = true;
14101 }
14102
14103 if (id_expression_or_member_access_p)
14104 /* We have parsed the complete id-expression or member access. */
14105 cp_parser_parse_definitely (parser);
14106 else
14107 {
14108 /* Abort our attempt to parse an id-expression or member access
14109 expression. */
14110 cp_parser_abort_tentative_parse (parser);
14111
14112 /* Parse a full expression. */
14113 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14114 /*decltype_p=*/true);
14115 }
14116
14117 return expr;
14118 }
14119
14120 /* Parse a `decltype' type. Returns the type.
14121
14122 simple-type-specifier:
14123 decltype ( expression )
14124 C++14 proposal:
14125 decltype ( auto ) */
14126
14127 static tree
14128 cp_parser_decltype (cp_parser *parser)
14129 {
14130 tree expr;
14131 bool id_expression_or_member_access_p = false;
14132 const char *saved_message;
14133 bool saved_integral_constant_expression_p;
14134 bool saved_non_integral_constant_expression_p;
14135 bool saved_greater_than_is_operator_p;
14136 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14137
14138 if (start_token->type == CPP_DECLTYPE)
14139 {
14140 /* Already parsed. */
14141 cp_lexer_consume_token (parser->lexer);
14142 return saved_checks_value (start_token->u.tree_check_value);
14143 }
14144
14145 /* Look for the `decltype' token. */
14146 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14147 return error_mark_node;
14148
14149 /* Parse the opening `('. */
14150 matching_parens parens;
14151 if (!parens.require_open (parser))
14152 return error_mark_node;
14153
14154 /* decltype (auto) */
14155 if (cxx_dialect >= cxx14
14156 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14157 {
14158 cp_lexer_consume_token (parser->lexer);
14159 if (!parens.require_close (parser))
14160 return error_mark_node;
14161 expr = make_decltype_auto ();
14162 AUTO_IS_DECLTYPE (expr) = true;
14163 goto rewrite;
14164 }
14165
14166 /* Types cannot be defined in a `decltype' expression. Save away the
14167 old message. */
14168 saved_message = parser->type_definition_forbidden_message;
14169
14170 /* And create the new one. */
14171 parser->type_definition_forbidden_message
14172 = G_("types may not be defined in %<decltype%> expressions");
14173
14174 /* The restrictions on constant-expressions do not apply inside
14175 decltype expressions. */
14176 saved_integral_constant_expression_p
14177 = parser->integral_constant_expression_p;
14178 saved_non_integral_constant_expression_p
14179 = parser->non_integral_constant_expression_p;
14180 parser->integral_constant_expression_p = false;
14181
14182 /* Within a parenthesized expression, a `>' token is always
14183 the greater-than operator. */
14184 saved_greater_than_is_operator_p
14185 = parser->greater_than_is_operator_p;
14186 parser->greater_than_is_operator_p = true;
14187
14188 /* Do not actually evaluate the expression. */
14189 ++cp_unevaluated_operand;
14190
14191 /* Do not warn about problems with the expression. */
14192 ++c_inhibit_evaluation_warnings;
14193
14194 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14195
14196 /* Go back to evaluating expressions. */
14197 --cp_unevaluated_operand;
14198 --c_inhibit_evaluation_warnings;
14199
14200 /* The `>' token might be the end of a template-id or
14201 template-parameter-list now. */
14202 parser->greater_than_is_operator_p
14203 = saved_greater_than_is_operator_p;
14204
14205 /* Restore the old message and the integral constant expression
14206 flags. */
14207 parser->type_definition_forbidden_message = saved_message;
14208 parser->integral_constant_expression_p
14209 = saved_integral_constant_expression_p;
14210 parser->non_integral_constant_expression_p
14211 = saved_non_integral_constant_expression_p;
14212
14213 /* Parse to the closing `)'. */
14214 if (!parens.require_close (parser))
14215 {
14216 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14217 /*consume_paren=*/true);
14218 return error_mark_node;
14219 }
14220
14221 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14222 tf_warning_or_error);
14223
14224 rewrite:
14225 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14226 it again. */
14227 start_token->type = CPP_DECLTYPE;
14228 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14229 start_token->u.tree_check_value->value = expr;
14230 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14231 start_token->keyword = RID_MAX;
14232 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14233
14234 return expr;
14235 }
14236
14237 /* Special member functions [gram.special] */
14238
14239 /* Parse a conversion-function-id.
14240
14241 conversion-function-id:
14242 operator conversion-type-id
14243
14244 Returns an IDENTIFIER_NODE representing the operator. */
14245
14246 static tree
14247 cp_parser_conversion_function_id (cp_parser* parser)
14248 {
14249 tree type;
14250 tree saved_scope;
14251 tree saved_qualifying_scope;
14252 tree saved_object_scope;
14253 tree pushed_scope = NULL_TREE;
14254
14255 /* Look for the `operator' token. */
14256 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14257 return error_mark_node;
14258 /* When we parse the conversion-type-id, the current scope will be
14259 reset. However, we need that information in able to look up the
14260 conversion function later, so we save it here. */
14261 saved_scope = parser->scope;
14262 saved_qualifying_scope = parser->qualifying_scope;
14263 saved_object_scope = parser->object_scope;
14264 /* We must enter the scope of the class so that the names of
14265 entities declared within the class are available in the
14266 conversion-type-id. For example, consider:
14267
14268 struct S {
14269 typedef int I;
14270 operator I();
14271 };
14272
14273 S::operator I() { ... }
14274
14275 In order to see that `I' is a type-name in the definition, we
14276 must be in the scope of `S'. */
14277 if (saved_scope)
14278 pushed_scope = push_scope (saved_scope);
14279 /* Parse the conversion-type-id. */
14280 type = cp_parser_conversion_type_id (parser);
14281 /* Leave the scope of the class, if any. */
14282 if (pushed_scope)
14283 pop_scope (pushed_scope);
14284 /* Restore the saved scope. */
14285 parser->scope = saved_scope;
14286 parser->qualifying_scope = saved_qualifying_scope;
14287 parser->object_scope = saved_object_scope;
14288 /* If the TYPE is invalid, indicate failure. */
14289 if (type == error_mark_node)
14290 return error_mark_node;
14291 return make_conv_op_name (type);
14292 }
14293
14294 /* Parse a conversion-type-id:
14295
14296 conversion-type-id:
14297 type-specifier-seq conversion-declarator [opt]
14298
14299 Returns the TYPE specified. */
14300
14301 static tree
14302 cp_parser_conversion_type_id (cp_parser* parser)
14303 {
14304 tree attributes;
14305 cp_decl_specifier_seq type_specifiers;
14306 cp_declarator *declarator;
14307 tree type_specified;
14308 const char *saved_message;
14309
14310 /* Parse the attributes. */
14311 attributes = cp_parser_attributes_opt (parser);
14312
14313 saved_message = parser->type_definition_forbidden_message;
14314 parser->type_definition_forbidden_message
14315 = G_("types may not be defined in a conversion-type-id");
14316
14317 /* Parse the type-specifiers. */
14318 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14319 /*is_trailing_return=*/false,
14320 &type_specifiers);
14321
14322 parser->type_definition_forbidden_message = saved_message;
14323
14324 /* If that didn't work, stop. */
14325 if (type_specifiers.type == error_mark_node)
14326 return error_mark_node;
14327 /* Parse the conversion-declarator. */
14328 declarator = cp_parser_conversion_declarator_opt (parser);
14329
14330 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14331 /*initialized=*/0, &attributes);
14332 if (attributes)
14333 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14334
14335 /* Don't give this error when parsing tentatively. This happens to
14336 work because we always parse this definitively once. */
14337 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14338 && type_uses_auto (type_specified))
14339 {
14340 if (cxx_dialect < cxx14)
14341 {
14342 error ("invalid use of %<auto%> in conversion operator");
14343 return error_mark_node;
14344 }
14345 else if (template_parm_scope_p ())
14346 warning (0, "use of %<auto%> in member template "
14347 "conversion operator can never be deduced");
14348 }
14349
14350 return type_specified;
14351 }
14352
14353 /* Parse an (optional) conversion-declarator.
14354
14355 conversion-declarator:
14356 ptr-operator conversion-declarator [opt]
14357
14358 */
14359
14360 static cp_declarator *
14361 cp_parser_conversion_declarator_opt (cp_parser* parser)
14362 {
14363 enum tree_code code;
14364 tree class_type, std_attributes = NULL_TREE;
14365 cp_cv_quals cv_quals;
14366
14367 /* We don't know if there's a ptr-operator next, or not. */
14368 cp_parser_parse_tentatively (parser);
14369 /* Try the ptr-operator. */
14370 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14371 &std_attributes);
14372 /* If it worked, look for more conversion-declarators. */
14373 if (cp_parser_parse_definitely (parser))
14374 {
14375 cp_declarator *declarator;
14376
14377 /* Parse another optional declarator. */
14378 declarator = cp_parser_conversion_declarator_opt (parser);
14379
14380 declarator = cp_parser_make_indirect_declarator
14381 (code, class_type, cv_quals, declarator, std_attributes);
14382
14383 return declarator;
14384 }
14385
14386 return NULL;
14387 }
14388
14389 /* Parse an (optional) ctor-initializer.
14390
14391 ctor-initializer:
14392 : mem-initializer-list */
14393
14394 static void
14395 cp_parser_ctor_initializer_opt (cp_parser* parser)
14396 {
14397 /* If the next token is not a `:', then there is no
14398 ctor-initializer. */
14399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14400 {
14401 /* Do default initialization of any bases and members. */
14402 if (DECL_CONSTRUCTOR_P (current_function_decl))
14403 finish_mem_initializers (NULL_TREE);
14404 return;
14405 }
14406
14407 /* Consume the `:' token. */
14408 cp_lexer_consume_token (parser->lexer);
14409 /* And the mem-initializer-list. */
14410 cp_parser_mem_initializer_list (parser);
14411 }
14412
14413 /* Parse a mem-initializer-list.
14414
14415 mem-initializer-list:
14416 mem-initializer ... [opt]
14417 mem-initializer ... [opt] , mem-initializer-list */
14418
14419 static void
14420 cp_parser_mem_initializer_list (cp_parser* parser)
14421 {
14422 tree mem_initializer_list = NULL_TREE;
14423 tree target_ctor = error_mark_node;
14424 cp_token *token = cp_lexer_peek_token (parser->lexer);
14425
14426 /* Let the semantic analysis code know that we are starting the
14427 mem-initializer-list. */
14428 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14429 error_at (token->location,
14430 "only constructors take member initializers");
14431
14432 /* Loop through the list. */
14433 while (true)
14434 {
14435 tree mem_initializer;
14436
14437 token = cp_lexer_peek_token (parser->lexer);
14438 /* Parse the mem-initializer. */
14439 mem_initializer = cp_parser_mem_initializer (parser);
14440 /* If the next token is a `...', we're expanding member initializers. */
14441 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14442 {
14443 /* Consume the `...'. */
14444 cp_lexer_consume_token (parser->lexer);
14445
14446 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14447 can be expanded but members cannot. */
14448 if (mem_initializer != error_mark_node
14449 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14450 {
14451 error_at (token->location,
14452 "cannot expand initializer for member %qD",
14453 TREE_PURPOSE (mem_initializer));
14454 mem_initializer = error_mark_node;
14455 }
14456
14457 /* Construct the pack expansion type. */
14458 if (mem_initializer != error_mark_node)
14459 mem_initializer = make_pack_expansion (mem_initializer);
14460 }
14461 if (target_ctor != error_mark_node
14462 && mem_initializer != error_mark_node)
14463 {
14464 error ("mem-initializer for %qD follows constructor delegation",
14465 TREE_PURPOSE (mem_initializer));
14466 mem_initializer = error_mark_node;
14467 }
14468 /* Look for a target constructor. */
14469 if (mem_initializer != error_mark_node
14470 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14471 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14472 {
14473 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14474 if (mem_initializer_list)
14475 {
14476 error ("constructor delegation follows mem-initializer for %qD",
14477 TREE_PURPOSE (mem_initializer_list));
14478 mem_initializer = error_mark_node;
14479 }
14480 target_ctor = mem_initializer;
14481 }
14482 /* Add it to the list, unless it was erroneous. */
14483 if (mem_initializer != error_mark_node)
14484 {
14485 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14486 mem_initializer_list = mem_initializer;
14487 }
14488 /* If the next token is not a `,', we're done. */
14489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14490 break;
14491 /* Consume the `,' token. */
14492 cp_lexer_consume_token (parser->lexer);
14493 }
14494
14495 /* Perform semantic analysis. */
14496 if (DECL_CONSTRUCTOR_P (current_function_decl))
14497 finish_mem_initializers (mem_initializer_list);
14498 }
14499
14500 /* Parse a mem-initializer.
14501
14502 mem-initializer:
14503 mem-initializer-id ( expression-list [opt] )
14504 mem-initializer-id braced-init-list
14505
14506 GNU extension:
14507
14508 mem-initializer:
14509 ( expression-list [opt] )
14510
14511 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14512 class) or FIELD_DECL (for a non-static data member) to initialize;
14513 the TREE_VALUE is the expression-list. An empty initialization
14514 list is represented by void_list_node. */
14515
14516 static tree
14517 cp_parser_mem_initializer (cp_parser* parser)
14518 {
14519 tree mem_initializer_id;
14520 tree expression_list;
14521 tree member;
14522 cp_token *token = cp_lexer_peek_token (parser->lexer);
14523
14524 /* Find out what is being initialized. */
14525 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14526 {
14527 permerror (token->location,
14528 "anachronistic old-style base class initializer");
14529 mem_initializer_id = NULL_TREE;
14530 }
14531 else
14532 {
14533 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14534 if (mem_initializer_id == error_mark_node)
14535 return mem_initializer_id;
14536 }
14537 member = expand_member_init (mem_initializer_id);
14538 if (member && !DECL_P (member))
14539 in_base_initializer = 1;
14540
14541 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14542 {
14543 bool expr_non_constant_p;
14544 cp_lexer_set_source_position (parser->lexer);
14545 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14546 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14547 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14548 expression_list = build_tree_list (NULL_TREE, expression_list);
14549 }
14550 else
14551 {
14552 vec<tree, va_gc> *vec;
14553 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14554 /*cast_p=*/false,
14555 /*allow_expansion_p=*/true,
14556 /*non_constant_p=*/NULL);
14557 if (vec == NULL)
14558 return error_mark_node;
14559 expression_list = build_tree_list_vec (vec);
14560 release_tree_vector (vec);
14561 }
14562
14563 if (expression_list == error_mark_node)
14564 return error_mark_node;
14565 if (!expression_list)
14566 expression_list = void_type_node;
14567
14568 in_base_initializer = 0;
14569
14570 return member ? build_tree_list (member, expression_list) : error_mark_node;
14571 }
14572
14573 /* Parse a mem-initializer-id.
14574
14575 mem-initializer-id:
14576 :: [opt] nested-name-specifier [opt] class-name
14577 decltype-specifier (C++11)
14578 identifier
14579
14580 Returns a TYPE indicating the class to be initialized for the first
14581 production (and the second in C++11). Returns an IDENTIFIER_NODE
14582 indicating the data member to be initialized for the last production. */
14583
14584 static tree
14585 cp_parser_mem_initializer_id (cp_parser* parser)
14586 {
14587 bool global_scope_p;
14588 bool nested_name_specifier_p;
14589 bool template_p = false;
14590 tree id;
14591
14592 cp_token *token = cp_lexer_peek_token (parser->lexer);
14593
14594 /* `typename' is not allowed in this context ([temp.res]). */
14595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14596 {
14597 error_at (token->location,
14598 "keyword %<typename%> not allowed in this context (a qualified "
14599 "member initializer is implicitly a type)");
14600 cp_lexer_consume_token (parser->lexer);
14601 }
14602 /* Look for the optional `::' operator. */
14603 global_scope_p
14604 = (cp_parser_global_scope_opt (parser,
14605 /*current_scope_valid_p=*/false)
14606 != NULL_TREE);
14607 /* Look for the optional nested-name-specifier. The simplest way to
14608 implement:
14609
14610 [temp.res]
14611
14612 The keyword `typename' is not permitted in a base-specifier or
14613 mem-initializer; in these contexts a qualified name that
14614 depends on a template-parameter is implicitly assumed to be a
14615 type name.
14616
14617 is to assume that we have seen the `typename' keyword at this
14618 point. */
14619 nested_name_specifier_p
14620 = (cp_parser_nested_name_specifier_opt (parser,
14621 /*typename_keyword_p=*/true,
14622 /*check_dependency_p=*/true,
14623 /*type_p=*/true,
14624 /*is_declaration=*/true)
14625 != NULL_TREE);
14626 if (nested_name_specifier_p)
14627 template_p = cp_parser_optional_template_keyword (parser);
14628 /* If there is a `::' operator or a nested-name-specifier, then we
14629 are definitely looking for a class-name. */
14630 if (global_scope_p || nested_name_specifier_p)
14631 return cp_parser_class_name (parser,
14632 /*typename_keyword_p=*/true,
14633 /*template_keyword_p=*/template_p,
14634 typename_type,
14635 /*check_dependency_p=*/true,
14636 /*class_head_p=*/false,
14637 /*is_declaration=*/true);
14638 /* Otherwise, we could also be looking for an ordinary identifier. */
14639 cp_parser_parse_tentatively (parser);
14640 if (cp_lexer_next_token_is_decltype (parser->lexer))
14641 /* Try a decltype-specifier. */
14642 id = cp_parser_decltype (parser);
14643 else
14644 /* Otherwise, try a class-name. */
14645 id = cp_parser_class_name (parser,
14646 /*typename_keyword_p=*/true,
14647 /*template_keyword_p=*/false,
14648 none_type,
14649 /*check_dependency_p=*/true,
14650 /*class_head_p=*/false,
14651 /*is_declaration=*/true);
14652 /* If we found one, we're done. */
14653 if (cp_parser_parse_definitely (parser))
14654 return id;
14655 /* Otherwise, look for an ordinary identifier. */
14656 return cp_parser_identifier (parser);
14657 }
14658
14659 /* Overloading [gram.over] */
14660
14661 /* Parse an operator-function-id.
14662
14663 operator-function-id:
14664 operator operator
14665
14666 Returns an IDENTIFIER_NODE for the operator which is a
14667 human-readable spelling of the identifier, e.g., `operator +'. */
14668
14669 static cp_expr
14670 cp_parser_operator_function_id (cp_parser* parser)
14671 {
14672 /* Look for the `operator' keyword. */
14673 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14674 return error_mark_node;
14675 /* And then the name of the operator itself. */
14676 return cp_parser_operator (parser);
14677 }
14678
14679 /* Return an identifier node for a user-defined literal operator.
14680 The suffix identifier is chained to the operator name identifier. */
14681
14682 tree
14683 cp_literal_operator_id (const char* name)
14684 {
14685 tree identifier;
14686 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14687 + strlen (name) + 10);
14688 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14689 identifier = get_identifier (buffer);
14690
14691 return identifier;
14692 }
14693
14694 /* Parse an operator.
14695
14696 operator:
14697 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14698 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14699 || ++ -- , ->* -> () []
14700
14701 GNU Extensions:
14702
14703 operator:
14704 <? >? <?= >?=
14705
14706 Returns an IDENTIFIER_NODE for the operator which is a
14707 human-readable spelling of the identifier, e.g., `operator +'. */
14708
14709 static cp_expr
14710 cp_parser_operator (cp_parser* parser)
14711 {
14712 tree id = NULL_TREE;
14713 cp_token *token;
14714 bool utf8 = false;
14715
14716 /* Peek at the next token. */
14717 token = cp_lexer_peek_token (parser->lexer);
14718
14719 location_t start_loc = token->location;
14720
14721 /* Figure out which operator we have. */
14722 enum tree_code op = ERROR_MARK;
14723 bool assop = false;
14724 bool consumed = false;
14725 switch (token->type)
14726 {
14727 case CPP_KEYWORD:
14728 {
14729 /* The keyword should be either `new' or `delete'. */
14730 if (token->keyword == RID_NEW)
14731 op = NEW_EXPR;
14732 else if (token->keyword == RID_DELETE)
14733 op = DELETE_EXPR;
14734 else
14735 break;
14736
14737 /* Consume the `new' or `delete' token. */
14738 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14739
14740 /* Peek at the next token. */
14741 token = cp_lexer_peek_token (parser->lexer);
14742 /* If it's a `[' token then this is the array variant of the
14743 operator. */
14744 if (token->type == CPP_OPEN_SQUARE)
14745 {
14746 /* Consume the `[' token. */
14747 cp_lexer_consume_token (parser->lexer);
14748 /* Look for the `]' token. */
14749 if (cp_token *close_token
14750 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14751 end_loc = close_token->location;
14752 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14753 }
14754 start_loc = make_location (start_loc, start_loc, end_loc);
14755 consumed = true;
14756 break;
14757 }
14758
14759 case CPP_PLUS:
14760 op = PLUS_EXPR;
14761 break;
14762
14763 case CPP_MINUS:
14764 op = MINUS_EXPR;
14765 break;
14766
14767 case CPP_MULT:
14768 op = MULT_EXPR;
14769 break;
14770
14771 case CPP_DIV:
14772 op = TRUNC_DIV_EXPR;
14773 break;
14774
14775 case CPP_MOD:
14776 op = TRUNC_MOD_EXPR;
14777 break;
14778
14779 case CPP_XOR:
14780 op = BIT_XOR_EXPR;
14781 break;
14782
14783 case CPP_AND:
14784 op = BIT_AND_EXPR;
14785 break;
14786
14787 case CPP_OR:
14788 op = BIT_IOR_EXPR;
14789 break;
14790
14791 case CPP_COMPL:
14792 op = BIT_NOT_EXPR;
14793 break;
14794
14795 case CPP_NOT:
14796 op = TRUTH_NOT_EXPR;
14797 break;
14798
14799 case CPP_EQ:
14800 assop = true;
14801 op = NOP_EXPR;
14802 break;
14803
14804 case CPP_LESS:
14805 op = LT_EXPR;
14806 break;
14807
14808 case CPP_GREATER:
14809 op = GT_EXPR;
14810 break;
14811
14812 case CPP_PLUS_EQ:
14813 assop = true;
14814 op = PLUS_EXPR;
14815 break;
14816
14817 case CPP_MINUS_EQ:
14818 assop = true;
14819 op = MINUS_EXPR;
14820 break;
14821
14822 case CPP_MULT_EQ:
14823 assop = true;
14824 op = MULT_EXPR;
14825 break;
14826
14827 case CPP_DIV_EQ:
14828 assop = true;
14829 op = TRUNC_DIV_EXPR;
14830 break;
14831
14832 case CPP_MOD_EQ:
14833 assop = true;
14834 op = TRUNC_MOD_EXPR;
14835 break;
14836
14837 case CPP_XOR_EQ:
14838 assop = true;
14839 op = BIT_XOR_EXPR;
14840 break;
14841
14842 case CPP_AND_EQ:
14843 assop = true;
14844 op = BIT_AND_EXPR;
14845 break;
14846
14847 case CPP_OR_EQ:
14848 assop = true;
14849 op = BIT_IOR_EXPR;
14850 break;
14851
14852 case CPP_LSHIFT:
14853 op = LSHIFT_EXPR;
14854 break;
14855
14856 case CPP_RSHIFT:
14857 op = RSHIFT_EXPR;
14858 break;
14859
14860 case CPP_LSHIFT_EQ:
14861 assop = true;
14862 op = LSHIFT_EXPR;
14863 break;
14864
14865 case CPP_RSHIFT_EQ:
14866 assop = true;
14867 op = RSHIFT_EXPR;
14868 break;
14869
14870 case CPP_EQ_EQ:
14871 op = EQ_EXPR;
14872 break;
14873
14874 case CPP_NOT_EQ:
14875 op = NE_EXPR;
14876 break;
14877
14878 case CPP_LESS_EQ:
14879 op = LE_EXPR;
14880 break;
14881
14882 case CPP_GREATER_EQ:
14883 op = GE_EXPR;
14884 break;
14885
14886 case CPP_AND_AND:
14887 op = TRUTH_ANDIF_EXPR;
14888 break;
14889
14890 case CPP_OR_OR:
14891 op = TRUTH_ORIF_EXPR;
14892 break;
14893
14894 case CPP_PLUS_PLUS:
14895 op = POSTINCREMENT_EXPR;
14896 break;
14897
14898 case CPP_MINUS_MINUS:
14899 op = PREDECREMENT_EXPR;
14900 break;
14901
14902 case CPP_COMMA:
14903 op = COMPOUND_EXPR;
14904 break;
14905
14906 case CPP_DEREF_STAR:
14907 op = MEMBER_REF;
14908 break;
14909
14910 case CPP_DEREF:
14911 op = COMPONENT_REF;
14912 break;
14913
14914 case CPP_OPEN_PAREN:
14915 {
14916 /* Consume the `('. */
14917 matching_parens parens;
14918 parens.consume_open (parser);
14919 /* Look for the matching `)'. */
14920 parens.require_close (parser);
14921 op = CALL_EXPR;
14922 consumed = true;
14923 break;
14924 }
14925
14926 case CPP_OPEN_SQUARE:
14927 /* Consume the `['. */
14928 cp_lexer_consume_token (parser->lexer);
14929 /* Look for the matching `]'. */
14930 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14931 op = ARRAY_REF;
14932 consumed = true;
14933 break;
14934
14935 case CPP_UTF8STRING:
14936 case CPP_UTF8STRING_USERDEF:
14937 utf8 = true;
14938 /* FALLTHRU */
14939 case CPP_STRING:
14940 case CPP_WSTRING:
14941 case CPP_STRING16:
14942 case CPP_STRING32:
14943 case CPP_STRING_USERDEF:
14944 case CPP_WSTRING_USERDEF:
14945 case CPP_STRING16_USERDEF:
14946 case CPP_STRING32_USERDEF:
14947 {
14948 tree str, string_tree;
14949 int sz, len;
14950
14951 if (cxx_dialect == cxx98)
14952 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14953
14954 /* Consume the string. */
14955 str = cp_parser_string_literal (parser, /*translate=*/true,
14956 /*wide_ok=*/true, /*lookup_udlit=*/false);
14957 if (str == error_mark_node)
14958 return error_mark_node;
14959 else if (TREE_CODE (str) == USERDEF_LITERAL)
14960 {
14961 string_tree = USERDEF_LITERAL_VALUE (str);
14962 id = USERDEF_LITERAL_SUFFIX_ID (str);
14963 }
14964 else
14965 {
14966 string_tree = str;
14967 /* Look for the suffix identifier. */
14968 token = cp_lexer_peek_token (parser->lexer);
14969 if (token->type == CPP_NAME)
14970 id = cp_parser_identifier (parser);
14971 else if (token->type == CPP_KEYWORD)
14972 {
14973 error ("unexpected keyword;"
14974 " remove space between quotes and suffix identifier");
14975 return error_mark_node;
14976 }
14977 else
14978 {
14979 error ("expected suffix identifier");
14980 return error_mark_node;
14981 }
14982 }
14983 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14984 (TREE_TYPE (TREE_TYPE (string_tree))));
14985 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14986 if (len != 0)
14987 {
14988 error ("expected empty string after %<operator%> keyword");
14989 return error_mark_node;
14990 }
14991 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14992 != char_type_node)
14993 {
14994 error ("invalid encoding prefix in literal operator");
14995 return error_mark_node;
14996 }
14997 if (id != error_mark_node)
14998 {
14999 const char *name = IDENTIFIER_POINTER (id);
15000 id = cp_literal_operator_id (name);
15001 }
15002 return id;
15003 }
15004
15005 default:
15006 /* Anything else is an error. */
15007 break;
15008 }
15009
15010 /* If we have selected an identifier, we need to consume the
15011 operator token. */
15012 if (op != ERROR_MARK)
15013 {
15014 id = ovl_op_identifier (assop, op);
15015 if (!consumed)
15016 cp_lexer_consume_token (parser->lexer);
15017 }
15018 /* Otherwise, no valid operator name was present. */
15019 else
15020 {
15021 cp_parser_error (parser, "expected operator");
15022 id = error_mark_node;
15023 }
15024
15025 return cp_expr (id, start_loc);
15026 }
15027
15028 /* Parse a template-declaration.
15029
15030 template-declaration:
15031 export [opt] template < template-parameter-list > declaration
15032
15033 If MEMBER_P is TRUE, this template-declaration occurs within a
15034 class-specifier.
15035
15036 The grammar rule given by the standard isn't correct. What
15037 is really meant is:
15038
15039 template-declaration:
15040 export [opt] template-parameter-list-seq
15041 decl-specifier-seq [opt] init-declarator [opt] ;
15042 export [opt] template-parameter-list-seq
15043 function-definition
15044
15045 template-parameter-list-seq:
15046 template-parameter-list-seq [opt]
15047 template < template-parameter-list >
15048
15049 Concept Extensions:
15050
15051 template-parameter-list-seq:
15052 template < template-parameter-list > requires-clause [opt]
15053
15054 requires-clause:
15055 requires logical-or-expression */
15056
15057 static void
15058 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15059 {
15060 /* Check for `export'. */
15061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15062 {
15063 /* Consume the `export' token. */
15064 cp_lexer_consume_token (parser->lexer);
15065 /* Warn that we do not support `export'. */
15066 warning (0, "keyword %<export%> not implemented, and will be ignored");
15067 }
15068
15069 cp_parser_template_declaration_after_export (parser, member_p);
15070 }
15071
15072 /* Parse a template-parameter-list.
15073
15074 template-parameter-list:
15075 template-parameter
15076 template-parameter-list , template-parameter
15077
15078 Returns a TREE_LIST. Each node represents a template parameter.
15079 The nodes are connected via their TREE_CHAINs. */
15080
15081 static tree
15082 cp_parser_template_parameter_list (cp_parser* parser)
15083 {
15084 tree parameter_list = NULL_TREE;
15085
15086 begin_template_parm_list ();
15087
15088 /* The loop below parses the template parms. We first need to know
15089 the total number of template parms to be able to compute proper
15090 canonical types of each dependent type. So after the loop, when
15091 we know the total number of template parms,
15092 end_template_parm_list computes the proper canonical types and
15093 fixes up the dependent types accordingly. */
15094 while (true)
15095 {
15096 tree parameter;
15097 bool is_non_type;
15098 bool is_parameter_pack;
15099 location_t parm_loc;
15100
15101 /* Parse the template-parameter. */
15102 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15103 parameter = cp_parser_template_parameter (parser,
15104 &is_non_type,
15105 &is_parameter_pack);
15106 /* Add it to the list. */
15107 if (parameter != error_mark_node)
15108 parameter_list = process_template_parm (parameter_list,
15109 parm_loc,
15110 parameter,
15111 is_non_type,
15112 is_parameter_pack);
15113 else
15114 {
15115 tree err_parm = build_tree_list (parameter, parameter);
15116 parameter_list = chainon (parameter_list, err_parm);
15117 }
15118
15119 /* If the next token is not a `,', we're done. */
15120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15121 break;
15122 /* Otherwise, consume the `,' token. */
15123 cp_lexer_consume_token (parser->lexer);
15124 }
15125
15126 return end_template_parm_list (parameter_list);
15127 }
15128
15129 /* Parse a introduction-list.
15130
15131 introduction-list:
15132 introduced-parameter
15133 introduction-list , introduced-parameter
15134
15135 introduced-parameter:
15136 ...[opt] identifier
15137
15138 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15139 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15140 WILDCARD_DECL will also have DECL_NAME set and token location in
15141 DECL_SOURCE_LOCATION. */
15142
15143 static tree
15144 cp_parser_introduction_list (cp_parser *parser)
15145 {
15146 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15147
15148 while (true)
15149 {
15150 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15151 if (is_pack)
15152 cp_lexer_consume_token (parser->lexer);
15153
15154 /* Build placeholder. */
15155 tree parm = build_nt (WILDCARD_DECL);
15156 DECL_SOURCE_LOCATION (parm)
15157 = cp_lexer_peek_token (parser->lexer)->location;
15158 DECL_NAME (parm) = cp_parser_identifier (parser);
15159 WILDCARD_PACK_P (parm) = is_pack;
15160 vec_safe_push (introduction_vec, parm);
15161
15162 /* If the next token is not a `,', we're done. */
15163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15164 break;
15165 /* Otherwise, consume the `,' token. */
15166 cp_lexer_consume_token (parser->lexer);
15167 }
15168
15169 /* Convert the vec into a TREE_VEC. */
15170 tree introduction_list = make_tree_vec (introduction_vec->length ());
15171 unsigned int n;
15172 tree parm;
15173 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15174 TREE_VEC_ELT (introduction_list, n) = parm;
15175
15176 release_tree_vector (introduction_vec);
15177 return introduction_list;
15178 }
15179
15180 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15181 is an abstract declarator. */
15182
15183 static inline cp_declarator*
15184 get_id_declarator (cp_declarator *declarator)
15185 {
15186 cp_declarator *d = declarator;
15187 while (d && d->kind != cdk_id)
15188 d = d->declarator;
15189 return d;
15190 }
15191
15192 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15193 is an abstract declarator. */
15194
15195 static inline tree
15196 get_unqualified_id (cp_declarator *declarator)
15197 {
15198 declarator = get_id_declarator (declarator);
15199 if (declarator)
15200 return declarator->u.id.unqualified_name;
15201 else
15202 return NULL_TREE;
15203 }
15204
15205 /* Returns true if DECL represents a constrained-parameter. */
15206
15207 static inline bool
15208 is_constrained_parameter (tree decl)
15209 {
15210 return (decl
15211 && TREE_CODE (decl) == TYPE_DECL
15212 && CONSTRAINED_PARM_CONCEPT (decl)
15213 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15214 }
15215
15216 /* Returns true if PARM declares a constrained-parameter. */
15217
15218 static inline bool
15219 is_constrained_parameter (cp_parameter_declarator *parm)
15220 {
15221 return is_constrained_parameter (parm->decl_specifiers.type);
15222 }
15223
15224 /* Check that the type parameter is only a declarator-id, and that its
15225 type is not cv-qualified. */
15226
15227 bool
15228 cp_parser_check_constrained_type_parm (cp_parser *parser,
15229 cp_parameter_declarator *parm)
15230 {
15231 if (!parm->declarator)
15232 return true;
15233
15234 if (parm->declarator->kind != cdk_id)
15235 {
15236 cp_parser_error (parser, "invalid constrained type parameter");
15237 return false;
15238 }
15239
15240 /* Don't allow cv-qualified type parameters. */
15241 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15242 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15243 {
15244 cp_parser_error (parser, "cv-qualified type parameter");
15245 return false;
15246 }
15247
15248 return true;
15249 }
15250
15251 /* Finish parsing/processing a template type parameter and checking
15252 various restrictions. */
15253
15254 static inline tree
15255 cp_parser_constrained_type_template_parm (cp_parser *parser,
15256 tree id,
15257 cp_parameter_declarator* parmdecl)
15258 {
15259 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15260 return finish_template_type_parm (class_type_node, id);
15261 else
15262 return error_mark_node;
15263 }
15264
15265 static tree
15266 finish_constrained_template_template_parm (tree proto, tree id)
15267 {
15268 /* FIXME: This should probably be copied, and we may need to adjust
15269 the template parameter depths. */
15270 tree saved_parms = current_template_parms;
15271 begin_template_parm_list ();
15272 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15273 end_template_parm_list ();
15274
15275 tree parm = finish_template_template_parm (class_type_node, id);
15276 current_template_parms = saved_parms;
15277
15278 return parm;
15279 }
15280
15281 /* Finish parsing/processing a template template parameter by borrowing
15282 the template parameter list from the prototype parameter. */
15283
15284 static tree
15285 cp_parser_constrained_template_template_parm (cp_parser *parser,
15286 tree proto,
15287 tree id,
15288 cp_parameter_declarator *parmdecl)
15289 {
15290 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15291 return error_mark_node;
15292 return finish_constrained_template_template_parm (proto, id);
15293 }
15294
15295 /* Create a new non-type template parameter from the given PARM
15296 declarator. */
15297
15298 static tree
15299 constrained_non_type_template_parm (bool *is_non_type,
15300 cp_parameter_declarator *parm)
15301 {
15302 *is_non_type = true;
15303 cp_declarator *decl = parm->declarator;
15304 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15305 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15306 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15307 }
15308
15309 /* Build a constrained template parameter based on the PARMDECL
15310 declarator. The type of PARMDECL is the constrained type, which
15311 refers to the prototype template parameter that ultimately
15312 specifies the type of the declared parameter. */
15313
15314 static tree
15315 finish_constrained_parameter (cp_parser *parser,
15316 cp_parameter_declarator *parmdecl,
15317 bool *is_non_type,
15318 bool *is_parameter_pack)
15319 {
15320 tree decl = parmdecl->decl_specifiers.type;
15321 tree id = get_unqualified_id (parmdecl->declarator);
15322 tree def = parmdecl->default_argument;
15323 tree proto = DECL_INITIAL (decl);
15324
15325 /* A template parameter constrained by a variadic concept shall also
15326 be declared as a template parameter pack. */
15327 bool is_variadic = template_parameter_pack_p (proto);
15328 if (is_variadic && !*is_parameter_pack)
15329 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15330
15331 /* Build the parameter. Return an error if the declarator was invalid. */
15332 tree parm;
15333 if (TREE_CODE (proto) == TYPE_DECL)
15334 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15335 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15336 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15337 parmdecl);
15338 else
15339 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15340 if (parm == error_mark_node)
15341 return error_mark_node;
15342
15343 /* Finish the parameter decl and create a node attaching the
15344 default argument and constraint. */
15345 parm = build_tree_list (def, parm);
15346 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15347
15348 return parm;
15349 }
15350
15351 /* Returns true if the parsed type actually represents the declaration
15352 of a type template-parameter. */
15353
15354 static inline bool
15355 declares_constrained_type_template_parameter (tree type)
15356 {
15357 return (is_constrained_parameter (type)
15358 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15359 }
15360
15361
15362 /* Returns true if the parsed type actually represents the declaration of
15363 a template template-parameter. */
15364
15365 static bool
15366 declares_constrained_template_template_parameter (tree type)
15367 {
15368 return (is_constrained_parameter (type)
15369 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15370 }
15371
15372 /* Parse a default argument for a type template-parameter.
15373 Note that diagnostics are handled in cp_parser_template_parameter. */
15374
15375 static tree
15376 cp_parser_default_type_template_argument (cp_parser *parser)
15377 {
15378 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15379
15380 /* Consume the `=' token. */
15381 cp_lexer_consume_token (parser->lexer);
15382
15383 cp_token *token = cp_lexer_peek_token (parser->lexer);
15384
15385 /* Parse the default-argument. */
15386 push_deferring_access_checks (dk_no_deferred);
15387 tree default_argument = cp_parser_type_id (parser);
15388 pop_deferring_access_checks ();
15389
15390 if (flag_concepts && type_uses_auto (default_argument))
15391 {
15392 error_at (token->location,
15393 "invalid use of %<auto%> in default template argument");
15394 return error_mark_node;
15395 }
15396
15397 return default_argument;
15398 }
15399
15400 /* Parse a default argument for a template template-parameter. */
15401
15402 static tree
15403 cp_parser_default_template_template_argument (cp_parser *parser)
15404 {
15405 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15406
15407 bool is_template;
15408
15409 /* Consume the `='. */
15410 cp_lexer_consume_token (parser->lexer);
15411 /* Parse the id-expression. */
15412 push_deferring_access_checks (dk_no_deferred);
15413 /* save token before parsing the id-expression, for error
15414 reporting */
15415 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15416 tree default_argument
15417 = cp_parser_id_expression (parser,
15418 /*template_keyword_p=*/false,
15419 /*check_dependency_p=*/true,
15420 /*template_p=*/&is_template,
15421 /*declarator_p=*/false,
15422 /*optional_p=*/false);
15423 if (TREE_CODE (default_argument) == TYPE_DECL)
15424 /* If the id-expression was a template-id that refers to
15425 a template-class, we already have the declaration here,
15426 so no further lookup is needed. */
15427 ;
15428 else
15429 /* Look up the name. */
15430 default_argument
15431 = cp_parser_lookup_name (parser, default_argument,
15432 none_type,
15433 /*is_template=*/is_template,
15434 /*is_namespace=*/false,
15435 /*check_dependency=*/true,
15436 /*ambiguous_decls=*/NULL,
15437 token->location);
15438 /* See if the default argument is valid. */
15439 default_argument = check_template_template_default_arg (default_argument);
15440 pop_deferring_access_checks ();
15441 return default_argument;
15442 }
15443
15444 /* Parse a template-parameter.
15445
15446 template-parameter:
15447 type-parameter
15448 parameter-declaration
15449
15450 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15451 the parameter. The TREE_PURPOSE is the default value, if any.
15452 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15453 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15454 set to true iff this parameter is a parameter pack. */
15455
15456 static tree
15457 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15458 bool *is_parameter_pack)
15459 {
15460 cp_token *token;
15461 cp_parameter_declarator *parameter_declarator;
15462 tree parm;
15463
15464 /* Assume it is a type parameter or a template parameter. */
15465 *is_non_type = false;
15466 /* Assume it not a parameter pack. */
15467 *is_parameter_pack = false;
15468 /* Peek at the next token. */
15469 token = cp_lexer_peek_token (parser->lexer);
15470 /* If it is `template', we have a type-parameter. */
15471 if (token->keyword == RID_TEMPLATE)
15472 return cp_parser_type_parameter (parser, is_parameter_pack);
15473 /* If it is `class' or `typename' we do not know yet whether it is a
15474 type parameter or a non-type parameter. Consider:
15475
15476 template <typename T, typename T::X X> ...
15477
15478 or:
15479
15480 template <class C, class D*> ...
15481
15482 Here, the first parameter is a type parameter, and the second is
15483 a non-type parameter. We can tell by looking at the token after
15484 the identifier -- if it is a `,', `=', or `>' then we have a type
15485 parameter. */
15486 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15487 {
15488 /* Peek at the token after `class' or `typename'. */
15489 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15490 /* If it's an ellipsis, we have a template type parameter
15491 pack. */
15492 if (token->type == CPP_ELLIPSIS)
15493 return cp_parser_type_parameter (parser, is_parameter_pack);
15494 /* If it's an identifier, skip it. */
15495 if (token->type == CPP_NAME)
15496 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15497 /* Now, see if the token looks like the end of a template
15498 parameter. */
15499 if (token->type == CPP_COMMA
15500 || token->type == CPP_EQ
15501 || token->type == CPP_GREATER)
15502 return cp_parser_type_parameter (parser, is_parameter_pack);
15503 }
15504
15505 /* Otherwise, it is a non-type parameter or a constrained parameter.
15506
15507 [temp.param]
15508
15509 When parsing a default template-argument for a non-type
15510 template-parameter, the first non-nested `>' is taken as the end
15511 of the template parameter-list rather than a greater-than
15512 operator. */
15513 parameter_declarator
15514 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15515 /*parenthesized_p=*/NULL);
15516
15517 if (!parameter_declarator)
15518 return error_mark_node;
15519
15520 /* If the parameter declaration is marked as a parameter pack, set
15521 *IS_PARAMETER_PACK to notify the caller. */
15522 if (parameter_declarator->template_parameter_pack_p)
15523 *is_parameter_pack = true;
15524
15525 if (parameter_declarator->default_argument)
15526 {
15527 /* Can happen in some cases of erroneous input (c++/34892). */
15528 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15529 /* Consume the `...' for better error recovery. */
15530 cp_lexer_consume_token (parser->lexer);
15531 }
15532
15533 // The parameter may have been constrained.
15534 if (is_constrained_parameter (parameter_declarator))
15535 return finish_constrained_parameter (parser,
15536 parameter_declarator,
15537 is_non_type,
15538 is_parameter_pack);
15539
15540 // Now we're sure that the parameter is a non-type parameter.
15541 *is_non_type = true;
15542
15543 parm = grokdeclarator (parameter_declarator->declarator,
15544 &parameter_declarator->decl_specifiers,
15545 TPARM, /*initialized=*/0,
15546 /*attrlist=*/NULL);
15547 if (parm == error_mark_node)
15548 return error_mark_node;
15549
15550 return build_tree_list (parameter_declarator->default_argument, parm);
15551 }
15552
15553 /* Parse a type-parameter.
15554
15555 type-parameter:
15556 class identifier [opt]
15557 class identifier [opt] = type-id
15558 typename identifier [opt]
15559 typename identifier [opt] = type-id
15560 template < template-parameter-list > class identifier [opt]
15561 template < template-parameter-list > class identifier [opt]
15562 = id-expression
15563
15564 GNU Extension (variadic templates):
15565
15566 type-parameter:
15567 class ... identifier [opt]
15568 typename ... identifier [opt]
15569
15570 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15571 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15572 the declaration of the parameter.
15573
15574 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15575
15576 static tree
15577 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15578 {
15579 cp_token *token;
15580 tree parameter;
15581
15582 /* Look for a keyword to tell us what kind of parameter this is. */
15583 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15584 if (!token)
15585 return error_mark_node;
15586
15587 switch (token->keyword)
15588 {
15589 case RID_CLASS:
15590 case RID_TYPENAME:
15591 {
15592 tree identifier;
15593 tree default_argument;
15594
15595 /* If the next token is an ellipsis, we have a template
15596 argument pack. */
15597 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15598 {
15599 /* Consume the `...' token. */
15600 cp_lexer_consume_token (parser->lexer);
15601 maybe_warn_variadic_templates ();
15602
15603 *is_parameter_pack = true;
15604 }
15605
15606 /* If the next token is an identifier, then it names the
15607 parameter. */
15608 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15609 identifier = cp_parser_identifier (parser);
15610 else
15611 identifier = NULL_TREE;
15612
15613 /* Create the parameter. */
15614 parameter = finish_template_type_parm (class_type_node, identifier);
15615
15616 /* If the next token is an `=', we have a default argument. */
15617 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15618 {
15619 default_argument
15620 = cp_parser_default_type_template_argument (parser);
15621
15622 /* Template parameter packs cannot have default
15623 arguments. */
15624 if (*is_parameter_pack)
15625 {
15626 if (identifier)
15627 error_at (token->location,
15628 "template parameter pack %qD cannot have a "
15629 "default argument", identifier);
15630 else
15631 error_at (token->location,
15632 "template parameter packs cannot have "
15633 "default arguments");
15634 default_argument = NULL_TREE;
15635 }
15636 else if (check_for_bare_parameter_packs (default_argument))
15637 default_argument = error_mark_node;
15638 }
15639 else
15640 default_argument = NULL_TREE;
15641
15642 /* Create the combined representation of the parameter and the
15643 default argument. */
15644 parameter = build_tree_list (default_argument, parameter);
15645 }
15646 break;
15647
15648 case RID_TEMPLATE:
15649 {
15650 tree identifier;
15651 tree default_argument;
15652
15653 /* Look for the `<'. */
15654 cp_parser_require (parser, CPP_LESS, RT_LESS);
15655 /* Parse the template-parameter-list. */
15656 cp_parser_template_parameter_list (parser);
15657 /* Look for the `>'. */
15658 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15659
15660 // If template requirements are present, parse them.
15661 if (flag_concepts)
15662 {
15663 tree reqs = get_shorthand_constraints (current_template_parms);
15664 if (tree r = cp_parser_requires_clause_opt (parser))
15665 reqs = conjoin_constraints (reqs, normalize_expression (r));
15666 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15667 }
15668
15669 /* Look for the `class' or 'typename' keywords. */
15670 cp_parser_type_parameter_key (parser);
15671 /* If the next token is an ellipsis, we have a template
15672 argument pack. */
15673 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15674 {
15675 /* Consume the `...' token. */
15676 cp_lexer_consume_token (parser->lexer);
15677 maybe_warn_variadic_templates ();
15678
15679 *is_parameter_pack = true;
15680 }
15681 /* If the next token is an `=', then there is a
15682 default-argument. If the next token is a `>', we are at
15683 the end of the parameter-list. If the next token is a `,',
15684 then we are at the end of this parameter. */
15685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15686 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15687 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15688 {
15689 identifier = cp_parser_identifier (parser);
15690 /* Treat invalid names as if the parameter were nameless. */
15691 if (identifier == error_mark_node)
15692 identifier = NULL_TREE;
15693 }
15694 else
15695 identifier = NULL_TREE;
15696
15697 /* Create the template parameter. */
15698 parameter = finish_template_template_parm (class_type_node,
15699 identifier);
15700
15701 /* If the next token is an `=', then there is a
15702 default-argument. */
15703 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15704 {
15705 default_argument
15706 = cp_parser_default_template_template_argument (parser);
15707
15708 /* Template parameter packs cannot have default
15709 arguments. */
15710 if (*is_parameter_pack)
15711 {
15712 if (identifier)
15713 error_at (token->location,
15714 "template parameter pack %qD cannot "
15715 "have a default argument",
15716 identifier);
15717 else
15718 error_at (token->location, "template parameter packs cannot "
15719 "have default arguments");
15720 default_argument = NULL_TREE;
15721 }
15722 }
15723 else
15724 default_argument = NULL_TREE;
15725
15726 /* Create the combined representation of the parameter and the
15727 default argument. */
15728 parameter = build_tree_list (default_argument, parameter);
15729 }
15730 break;
15731
15732 default:
15733 gcc_unreachable ();
15734 break;
15735 }
15736
15737 return parameter;
15738 }
15739
15740 /* Parse a template-id.
15741
15742 template-id:
15743 template-name < template-argument-list [opt] >
15744
15745 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15746 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15747 returned. Otherwise, if the template-name names a function, or set
15748 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15749 names a class, returns a TYPE_DECL for the specialization.
15750
15751 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15752 uninstantiated templates. */
15753
15754 static tree
15755 cp_parser_template_id (cp_parser *parser,
15756 bool template_keyword_p,
15757 bool check_dependency_p,
15758 enum tag_types tag_type,
15759 bool is_declaration)
15760 {
15761 tree templ;
15762 tree arguments;
15763 tree template_id;
15764 cp_token_position start_of_id = 0;
15765 cp_token *next_token = NULL, *next_token_2 = NULL;
15766 bool is_identifier;
15767
15768 /* If the next token corresponds to a template-id, there is no need
15769 to reparse it. */
15770 cp_token *token = cp_lexer_peek_token (parser->lexer);
15771 if (token->type == CPP_TEMPLATE_ID)
15772 {
15773 cp_lexer_consume_token (parser->lexer);
15774 return saved_checks_value (token->u.tree_check_value);
15775 }
15776
15777 /* Avoid performing name lookup if there is no possibility of
15778 finding a template-id. */
15779 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15780 || (token->type == CPP_NAME
15781 && !cp_parser_nth_token_starts_template_argument_list_p
15782 (parser, 2)))
15783 {
15784 cp_parser_error (parser, "expected template-id");
15785 return error_mark_node;
15786 }
15787
15788 /* Remember where the template-id starts. */
15789 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15790 start_of_id = cp_lexer_token_position (parser->lexer, false);
15791
15792 push_deferring_access_checks (dk_deferred);
15793
15794 /* Parse the template-name. */
15795 is_identifier = false;
15796 templ = cp_parser_template_name (parser, template_keyword_p,
15797 check_dependency_p,
15798 is_declaration,
15799 tag_type,
15800 &is_identifier);
15801 if (templ == error_mark_node || is_identifier)
15802 {
15803 pop_deferring_access_checks ();
15804 return templ;
15805 }
15806
15807 /* Since we're going to preserve any side-effects from this parse, set up a
15808 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15809 in the template arguments. */
15810 tentative_firewall firewall (parser);
15811
15812 /* If we find the sequence `[:' after a template-name, it's probably
15813 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15814 parse correctly the argument list. */
15815 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15816 == CPP_OPEN_SQUARE)
15817 && next_token->flags & DIGRAPH
15818 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15819 == CPP_COLON)
15820 && !(next_token_2->flags & PREV_WHITE))
15821 {
15822 cp_parser_parse_tentatively (parser);
15823 /* Change `:' into `::'. */
15824 next_token_2->type = CPP_SCOPE;
15825 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15826 CPP_LESS. */
15827 cp_lexer_consume_token (parser->lexer);
15828
15829 /* Parse the arguments. */
15830 arguments = cp_parser_enclosed_template_argument_list (parser);
15831 if (!cp_parser_parse_definitely (parser))
15832 {
15833 /* If we couldn't parse an argument list, then we revert our changes
15834 and return simply an error. Maybe this is not a template-id
15835 after all. */
15836 next_token_2->type = CPP_COLON;
15837 cp_parser_error (parser, "expected %<<%>");
15838 pop_deferring_access_checks ();
15839 return error_mark_node;
15840 }
15841 /* Otherwise, emit an error about the invalid digraph, but continue
15842 parsing because we got our argument list. */
15843 if (permerror (next_token->location,
15844 "%<<::%> cannot begin a template-argument list"))
15845 {
15846 static bool hint = false;
15847 inform (next_token->location,
15848 "%<<:%> is an alternate spelling for %<[%>."
15849 " Insert whitespace between %<<%> and %<::%>");
15850 if (!hint && !flag_permissive)
15851 {
15852 inform (next_token->location, "(if you use %<-fpermissive%> "
15853 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15854 "accept your code)");
15855 hint = true;
15856 }
15857 }
15858 }
15859 else
15860 {
15861 /* Look for the `<' that starts the template-argument-list. */
15862 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15863 {
15864 pop_deferring_access_checks ();
15865 return error_mark_node;
15866 }
15867 /* Parse the arguments. */
15868 arguments = cp_parser_enclosed_template_argument_list (parser);
15869 }
15870
15871 /* Set the location to be of the form:
15872 template-name < template-argument-list [opt] >
15873 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15874 with caret == start at the start of the template-name,
15875 ranging until the closing '>'. */
15876 location_t finish_loc
15877 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15878 location_t combined_loc
15879 = make_location (token->location, token->location, finish_loc);
15880
15881 /* Build a representation of the specialization. */
15882 if (identifier_p (templ))
15883 template_id = build_min_nt_loc (combined_loc,
15884 TEMPLATE_ID_EXPR,
15885 templ, arguments);
15886 else if (DECL_TYPE_TEMPLATE_P (templ)
15887 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15888 {
15889 bool entering_scope;
15890 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15891 template (rather than some instantiation thereof) only if
15892 is not nested within some other construct. For example, in
15893 "template <typename T> void f(T) { A<T>::", A<T> is just an
15894 instantiation of A. */
15895 entering_scope = (template_parm_scope_p ()
15896 && cp_lexer_next_token_is (parser->lexer,
15897 CPP_SCOPE));
15898 template_id
15899 = finish_template_type (templ, arguments, entering_scope);
15900 }
15901 /* A template-like identifier may be a partial concept id. */
15902 else if (flag_concepts
15903 && (template_id = (cp_parser_maybe_partial_concept_id
15904 (parser, templ, arguments))))
15905 return template_id;
15906 else if (variable_template_p (templ))
15907 {
15908 template_id = lookup_template_variable (templ, arguments);
15909 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15910 SET_EXPR_LOCATION (template_id, combined_loc);
15911 }
15912 else
15913 {
15914 /* If it's not a class-template or a template-template, it should be
15915 a function-template. */
15916 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15917 || TREE_CODE (templ) == OVERLOAD
15918 || BASELINK_P (templ)));
15919
15920 template_id = lookup_template_function (templ, arguments);
15921 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15922 SET_EXPR_LOCATION (template_id, combined_loc);
15923 }
15924
15925 /* If parsing tentatively, replace the sequence of tokens that makes
15926 up the template-id with a CPP_TEMPLATE_ID token. That way,
15927 should we re-parse the token stream, we will not have to repeat
15928 the effort required to do the parse, nor will we issue duplicate
15929 error messages about problems during instantiation of the
15930 template. */
15931 if (start_of_id
15932 /* Don't do this if we had a parse error in a declarator; re-parsing
15933 might succeed if a name changes meaning (60361). */
15934 && !(cp_parser_error_occurred (parser)
15935 && cp_parser_parsing_tentatively (parser)
15936 && parser->in_declarator_p))
15937 {
15938 /* Reset the contents of the START_OF_ID token. */
15939 token->type = CPP_TEMPLATE_ID;
15940 token->location = combined_loc;
15941
15942 /* We must mark the lookup as kept, so we don't throw it away on
15943 the first parse. */
15944 if (is_overloaded_fn (template_id))
15945 lookup_keep (get_fns (template_id), true);
15946
15947 /* Retrieve any deferred checks. Do not pop this access checks yet
15948 so the memory will not be reclaimed during token replacing below. */
15949 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15950 token->u.tree_check_value->value = template_id;
15951 token->u.tree_check_value->checks = get_deferred_access_checks ();
15952 token->keyword = RID_MAX;
15953
15954 /* Purge all subsequent tokens. */
15955 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15956
15957 /* ??? Can we actually assume that, if template_id ==
15958 error_mark_node, we will have issued a diagnostic to the
15959 user, as opposed to simply marking the tentative parse as
15960 failed? */
15961 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15962 error_at (token->location, "parse error in template argument list");
15963 }
15964
15965 pop_to_parent_deferring_access_checks ();
15966 return template_id;
15967 }
15968
15969 /* Parse a template-name.
15970
15971 template-name:
15972 identifier
15973
15974 The standard should actually say:
15975
15976 template-name:
15977 identifier
15978 operator-function-id
15979
15980 A defect report has been filed about this issue.
15981
15982 A conversion-function-id cannot be a template name because they cannot
15983 be part of a template-id. In fact, looking at this code:
15984
15985 a.operator K<int>()
15986
15987 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15988 It is impossible to call a templated conversion-function-id with an
15989 explicit argument list, since the only allowed template parameter is
15990 the type to which it is converting.
15991
15992 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15993 `template' keyword, in a construction like:
15994
15995 T::template f<3>()
15996
15997 In that case `f' is taken to be a template-name, even though there
15998 is no way of knowing for sure.
15999
16000 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16001 name refers to a set of overloaded functions, at least one of which
16002 is a template, or an IDENTIFIER_NODE with the name of the template,
16003 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16004 names are looked up inside uninstantiated templates. */
16005
16006 static tree
16007 cp_parser_template_name (cp_parser* parser,
16008 bool template_keyword_p,
16009 bool check_dependency_p,
16010 bool is_declaration,
16011 enum tag_types tag_type,
16012 bool *is_identifier)
16013 {
16014 tree identifier;
16015 tree decl;
16016 cp_token *token = cp_lexer_peek_token (parser->lexer);
16017
16018 /* If the next token is `operator', then we have either an
16019 operator-function-id or a conversion-function-id. */
16020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16021 {
16022 /* We don't know whether we're looking at an
16023 operator-function-id or a conversion-function-id. */
16024 cp_parser_parse_tentatively (parser);
16025 /* Try an operator-function-id. */
16026 identifier = cp_parser_operator_function_id (parser);
16027 /* If that didn't work, try a conversion-function-id. */
16028 if (!cp_parser_parse_definitely (parser))
16029 {
16030 cp_parser_error (parser, "expected template-name");
16031 return error_mark_node;
16032 }
16033 }
16034 /* Look for the identifier. */
16035 else
16036 identifier = cp_parser_identifier (parser);
16037
16038 /* If we didn't find an identifier, we don't have a template-id. */
16039 if (identifier == error_mark_node)
16040 return error_mark_node;
16041
16042 /* If the name immediately followed the `template' keyword, then it
16043 is a template-name. However, if the next token is not `<', then
16044 we do not treat it as a template-name, since it is not being used
16045 as part of a template-id. This enables us to handle constructs
16046 like:
16047
16048 template <typename T> struct S { S(); };
16049 template <typename T> S<T>::S();
16050
16051 correctly. We would treat `S' as a template -- if it were `S<T>'
16052 -- but we do not if there is no `<'. */
16053
16054 if (processing_template_decl
16055 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16056 {
16057 /* In a declaration, in a dependent context, we pretend that the
16058 "template" keyword was present in order to improve error
16059 recovery. For example, given:
16060
16061 template <typename T> void f(T::X<int>);
16062
16063 we want to treat "X<int>" as a template-id. */
16064 if (is_declaration
16065 && !template_keyword_p
16066 && parser->scope && TYPE_P (parser->scope)
16067 && check_dependency_p
16068 && dependent_scope_p (parser->scope)
16069 /* Do not do this for dtors (or ctors), since they never
16070 need the template keyword before their name. */
16071 && !constructor_name_p (identifier, parser->scope))
16072 {
16073 cp_token_position start = 0;
16074
16075 /* Explain what went wrong. */
16076 error_at (token->location, "non-template %qD used as template",
16077 identifier);
16078 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16079 parser->scope, identifier);
16080 /* If parsing tentatively, find the location of the "<" token. */
16081 if (cp_parser_simulate_error (parser))
16082 start = cp_lexer_token_position (parser->lexer, true);
16083 /* Parse the template arguments so that we can issue error
16084 messages about them. */
16085 cp_lexer_consume_token (parser->lexer);
16086 cp_parser_enclosed_template_argument_list (parser);
16087 /* Skip tokens until we find a good place from which to
16088 continue parsing. */
16089 cp_parser_skip_to_closing_parenthesis (parser,
16090 /*recovering=*/true,
16091 /*or_comma=*/true,
16092 /*consume_paren=*/false);
16093 /* If parsing tentatively, permanently remove the
16094 template argument list. That will prevent duplicate
16095 error messages from being issued about the missing
16096 "template" keyword. */
16097 if (start)
16098 cp_lexer_purge_tokens_after (parser->lexer, start);
16099 if (is_identifier)
16100 *is_identifier = true;
16101 parser->context->object_type = NULL_TREE;
16102 return identifier;
16103 }
16104
16105 /* If the "template" keyword is present, then there is generally
16106 no point in doing name-lookup, so we just return IDENTIFIER.
16107 But, if the qualifying scope is non-dependent then we can
16108 (and must) do name-lookup normally. */
16109 if (template_keyword_p)
16110 {
16111 tree scope = (parser->scope ? parser->scope
16112 : parser->context->object_type);
16113 if (scope && TYPE_P (scope)
16114 && (!CLASS_TYPE_P (scope)
16115 || (check_dependency_p && dependent_type_p (scope))))
16116 {
16117 /* We're optimizing away the call to cp_parser_lookup_name, but
16118 we still need to do this. */
16119 parser->context->object_type = NULL_TREE;
16120 return identifier;
16121 }
16122 }
16123 }
16124
16125 /* Look up the name. */
16126 decl = cp_parser_lookup_name (parser, identifier,
16127 tag_type,
16128 /*is_template=*/true,
16129 /*is_namespace=*/false,
16130 check_dependency_p,
16131 /*ambiguous_decls=*/NULL,
16132 token->location);
16133
16134 decl = strip_using_decl (decl);
16135
16136 /* If DECL is a template, then the name was a template-name. */
16137 if (TREE_CODE (decl) == TEMPLATE_DECL)
16138 {
16139 if (TREE_DEPRECATED (decl)
16140 && deprecated_state != DEPRECATED_SUPPRESS)
16141 warn_deprecated_use (decl, NULL_TREE);
16142 }
16143 else
16144 {
16145 /* The standard does not explicitly indicate whether a name that
16146 names a set of overloaded declarations, some of which are
16147 templates, is a template-name. However, such a name should
16148 be a template-name; otherwise, there is no way to form a
16149 template-id for the overloaded templates. */
16150 bool found = false;
16151
16152 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16153 !found && iter; ++iter)
16154 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16155 found = true;
16156
16157 if (!found)
16158 {
16159 /* The name does not name a template. */
16160 cp_parser_error (parser, "expected template-name");
16161 return error_mark_node;
16162 }
16163 }
16164
16165 /* If DECL is dependent, and refers to a function, then just return
16166 its name; we will look it up again during template instantiation. */
16167 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16168 {
16169 tree scope = ovl_scope (decl);
16170 if (TYPE_P (scope) && dependent_type_p (scope))
16171 return identifier;
16172 }
16173
16174 return decl;
16175 }
16176
16177 /* Parse a template-argument-list.
16178
16179 template-argument-list:
16180 template-argument ... [opt]
16181 template-argument-list , template-argument ... [opt]
16182
16183 Returns a TREE_VEC containing the arguments. */
16184
16185 static tree
16186 cp_parser_template_argument_list (cp_parser* parser)
16187 {
16188 tree fixed_args[10];
16189 unsigned n_args = 0;
16190 unsigned alloced = 10;
16191 tree *arg_ary = fixed_args;
16192 tree vec;
16193 bool saved_in_template_argument_list_p;
16194 bool saved_ice_p;
16195 bool saved_non_ice_p;
16196
16197 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16198 parser->in_template_argument_list_p = true;
16199 /* Even if the template-id appears in an integral
16200 constant-expression, the contents of the argument list do
16201 not. */
16202 saved_ice_p = parser->integral_constant_expression_p;
16203 parser->integral_constant_expression_p = false;
16204 saved_non_ice_p = parser->non_integral_constant_expression_p;
16205 parser->non_integral_constant_expression_p = false;
16206
16207 /* Parse the arguments. */
16208 do
16209 {
16210 tree argument;
16211
16212 if (n_args)
16213 /* Consume the comma. */
16214 cp_lexer_consume_token (parser->lexer);
16215
16216 /* Parse the template-argument. */
16217 argument = cp_parser_template_argument (parser);
16218
16219 /* If the next token is an ellipsis, we're expanding a template
16220 argument pack. */
16221 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16222 {
16223 if (argument == error_mark_node)
16224 {
16225 cp_token *token = cp_lexer_peek_token (parser->lexer);
16226 error_at (token->location,
16227 "expected parameter pack before %<...%>");
16228 }
16229 /* Consume the `...' token. */
16230 cp_lexer_consume_token (parser->lexer);
16231
16232 /* Make the argument into a TYPE_PACK_EXPANSION or
16233 EXPR_PACK_EXPANSION. */
16234 argument = make_pack_expansion (argument);
16235 }
16236
16237 if (n_args == alloced)
16238 {
16239 alloced *= 2;
16240
16241 if (arg_ary == fixed_args)
16242 {
16243 arg_ary = XNEWVEC (tree, alloced);
16244 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16245 }
16246 else
16247 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16248 }
16249 arg_ary[n_args++] = argument;
16250 }
16251 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16252
16253 vec = make_tree_vec (n_args);
16254
16255 while (n_args--)
16256 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16257
16258 if (arg_ary != fixed_args)
16259 free (arg_ary);
16260 parser->non_integral_constant_expression_p = saved_non_ice_p;
16261 parser->integral_constant_expression_p = saved_ice_p;
16262 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16263 if (CHECKING_P)
16264 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16265 return vec;
16266 }
16267
16268 /* Parse a template-argument.
16269
16270 template-argument:
16271 assignment-expression
16272 type-id
16273 id-expression
16274
16275 The representation is that of an assignment-expression, type-id, or
16276 id-expression -- except that the qualified id-expression is
16277 evaluated, so that the value returned is either a DECL or an
16278 OVERLOAD.
16279
16280 Although the standard says "assignment-expression", it forbids
16281 throw-expressions or assignments in the template argument.
16282 Therefore, we use "conditional-expression" instead. */
16283
16284 static tree
16285 cp_parser_template_argument (cp_parser* parser)
16286 {
16287 tree argument;
16288 bool template_p;
16289 bool address_p;
16290 bool maybe_type_id = false;
16291 cp_token *token = NULL, *argument_start_token = NULL;
16292 location_t loc = 0;
16293 cp_id_kind idk;
16294
16295 /* There's really no way to know what we're looking at, so we just
16296 try each alternative in order.
16297
16298 [temp.arg]
16299
16300 In a template-argument, an ambiguity between a type-id and an
16301 expression is resolved to a type-id, regardless of the form of
16302 the corresponding template-parameter.
16303
16304 Therefore, we try a type-id first. */
16305 cp_parser_parse_tentatively (parser);
16306 argument = cp_parser_template_type_arg (parser);
16307 /* If there was no error parsing the type-id but the next token is a
16308 '>>', our behavior depends on which dialect of C++ we're
16309 parsing. In C++98, we probably found a typo for '> >'. But there
16310 are type-id which are also valid expressions. For instance:
16311
16312 struct X { int operator >> (int); };
16313 template <int V> struct Foo {};
16314 Foo<X () >> 5> r;
16315
16316 Here 'X()' is a valid type-id of a function type, but the user just
16317 wanted to write the expression "X() >> 5". Thus, we remember that we
16318 found a valid type-id, but we still try to parse the argument as an
16319 expression to see what happens.
16320
16321 In C++0x, the '>>' will be considered two separate '>'
16322 tokens. */
16323 if (!cp_parser_error_occurred (parser)
16324 && cxx_dialect == cxx98
16325 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16326 {
16327 maybe_type_id = true;
16328 cp_parser_abort_tentative_parse (parser);
16329 }
16330 else
16331 {
16332 /* If the next token isn't a `,' or a `>', then this argument wasn't
16333 really finished. This means that the argument is not a valid
16334 type-id. */
16335 if (!cp_parser_next_token_ends_template_argument_p (parser))
16336 cp_parser_error (parser, "expected template-argument");
16337 /* If that worked, we're done. */
16338 if (cp_parser_parse_definitely (parser))
16339 return argument;
16340 }
16341 /* We're still not sure what the argument will be. */
16342 cp_parser_parse_tentatively (parser);
16343 /* Try a template. */
16344 argument_start_token = cp_lexer_peek_token (parser->lexer);
16345 argument = cp_parser_id_expression (parser,
16346 /*template_keyword_p=*/false,
16347 /*check_dependency_p=*/true,
16348 &template_p,
16349 /*declarator_p=*/false,
16350 /*optional_p=*/false);
16351 /* If the next token isn't a `,' or a `>', then this argument wasn't
16352 really finished. */
16353 if (!cp_parser_next_token_ends_template_argument_p (parser))
16354 cp_parser_error (parser, "expected template-argument");
16355 if (!cp_parser_error_occurred (parser))
16356 {
16357 /* Figure out what is being referred to. If the id-expression
16358 was for a class template specialization, then we will have a
16359 TYPE_DECL at this point. There is no need to do name lookup
16360 at this point in that case. */
16361 if (TREE_CODE (argument) != TYPE_DECL)
16362 argument = cp_parser_lookup_name (parser, argument,
16363 none_type,
16364 /*is_template=*/template_p,
16365 /*is_namespace=*/false,
16366 /*check_dependency=*/true,
16367 /*ambiguous_decls=*/NULL,
16368 argument_start_token->location);
16369 /* Handle a constrained-type-specifier for a non-type template
16370 parameter. */
16371 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16372 argument = decl;
16373 else if (TREE_CODE (argument) != TEMPLATE_DECL
16374 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16375 cp_parser_error (parser, "expected template-name");
16376 }
16377 if (cp_parser_parse_definitely (parser))
16378 {
16379 if (TREE_DEPRECATED (argument))
16380 warn_deprecated_use (argument, NULL_TREE);
16381 return argument;
16382 }
16383 /* It must be a non-type argument. In C++17 any constant-expression is
16384 allowed. */
16385 if (cxx_dialect > cxx14)
16386 goto general_expr;
16387
16388 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16389
16390 -- an integral constant-expression of integral or enumeration
16391 type; or
16392
16393 -- the name of a non-type template-parameter; or
16394
16395 -- the name of an object or function with external linkage...
16396
16397 -- the address of an object or function with external linkage...
16398
16399 -- a pointer to member... */
16400 /* Look for a non-type template parameter. */
16401 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16402 {
16403 cp_parser_parse_tentatively (parser);
16404 argument = cp_parser_primary_expression (parser,
16405 /*address_p=*/false,
16406 /*cast_p=*/false,
16407 /*template_arg_p=*/true,
16408 &idk);
16409 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16410 || !cp_parser_next_token_ends_template_argument_p (parser))
16411 cp_parser_simulate_error (parser);
16412 if (cp_parser_parse_definitely (parser))
16413 return argument;
16414 }
16415
16416 /* If the next token is "&", the argument must be the address of an
16417 object or function with external linkage. */
16418 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16419 if (address_p)
16420 {
16421 loc = cp_lexer_peek_token (parser->lexer)->location;
16422 cp_lexer_consume_token (parser->lexer);
16423 }
16424 /* See if we might have an id-expression. */
16425 token = cp_lexer_peek_token (parser->lexer);
16426 if (token->type == CPP_NAME
16427 || token->keyword == RID_OPERATOR
16428 || token->type == CPP_SCOPE
16429 || token->type == CPP_TEMPLATE_ID
16430 || token->type == CPP_NESTED_NAME_SPECIFIER)
16431 {
16432 cp_parser_parse_tentatively (parser);
16433 argument = cp_parser_primary_expression (parser,
16434 address_p,
16435 /*cast_p=*/false,
16436 /*template_arg_p=*/true,
16437 &idk);
16438 if (cp_parser_error_occurred (parser)
16439 || !cp_parser_next_token_ends_template_argument_p (parser))
16440 cp_parser_abort_tentative_parse (parser);
16441 else
16442 {
16443 tree probe;
16444
16445 if (INDIRECT_REF_P (argument))
16446 {
16447 /* Strip the dereference temporarily. */
16448 gcc_assert (REFERENCE_REF_P (argument));
16449 argument = TREE_OPERAND (argument, 0);
16450 }
16451
16452 /* If we're in a template, we represent a qualified-id referring
16453 to a static data member as a SCOPE_REF even if the scope isn't
16454 dependent so that we can check access control later. */
16455 probe = argument;
16456 if (TREE_CODE (probe) == SCOPE_REF)
16457 probe = TREE_OPERAND (probe, 1);
16458 if (VAR_P (probe))
16459 {
16460 /* A variable without external linkage might still be a
16461 valid constant-expression, so no error is issued here
16462 if the external-linkage check fails. */
16463 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16464 cp_parser_simulate_error (parser);
16465 }
16466 else if (is_overloaded_fn (argument))
16467 /* All overloaded functions are allowed; if the external
16468 linkage test does not pass, an error will be issued
16469 later. */
16470 ;
16471 else if (address_p
16472 && (TREE_CODE (argument) == OFFSET_REF
16473 || TREE_CODE (argument) == SCOPE_REF))
16474 /* A pointer-to-member. */
16475 ;
16476 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16477 ;
16478 else
16479 cp_parser_simulate_error (parser);
16480
16481 if (cp_parser_parse_definitely (parser))
16482 {
16483 if (address_p)
16484 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16485 tf_warning_or_error);
16486 else
16487 argument = convert_from_reference (argument);
16488 return argument;
16489 }
16490 }
16491 }
16492 /* If the argument started with "&", there are no other valid
16493 alternatives at this point. */
16494 if (address_p)
16495 {
16496 cp_parser_error (parser, "invalid non-type template argument");
16497 return error_mark_node;
16498 }
16499
16500 general_expr:
16501 /* If the argument wasn't successfully parsed as a type-id followed
16502 by '>>', the argument can only be a constant expression now.
16503 Otherwise, we try parsing the constant-expression tentatively,
16504 because the argument could really be a type-id. */
16505 if (maybe_type_id)
16506 cp_parser_parse_tentatively (parser);
16507
16508 if (cxx_dialect <= cxx14)
16509 argument = cp_parser_constant_expression (parser);
16510 else
16511 {
16512 /* With C++17 generalized non-type template arguments we need to handle
16513 lvalue constant expressions, too. */
16514 argument = cp_parser_assignment_expression (parser);
16515 require_potential_constant_expression (argument);
16516 }
16517
16518 if (!maybe_type_id)
16519 return argument;
16520 if (!cp_parser_next_token_ends_template_argument_p (parser))
16521 cp_parser_error (parser, "expected template-argument");
16522 if (cp_parser_parse_definitely (parser))
16523 return argument;
16524 /* We did our best to parse the argument as a non type-id, but that
16525 was the only alternative that matched (albeit with a '>' after
16526 it). We can assume it's just a typo from the user, and a
16527 diagnostic will then be issued. */
16528 return cp_parser_template_type_arg (parser);
16529 }
16530
16531 /* Parse an explicit-instantiation.
16532
16533 explicit-instantiation:
16534 template declaration
16535
16536 Although the standard says `declaration', what it really means is:
16537
16538 explicit-instantiation:
16539 template decl-specifier-seq [opt] declarator [opt] ;
16540
16541 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16542 supposed to be allowed. A defect report has been filed about this
16543 issue.
16544
16545 GNU Extension:
16546
16547 explicit-instantiation:
16548 storage-class-specifier template
16549 decl-specifier-seq [opt] declarator [opt] ;
16550 function-specifier template
16551 decl-specifier-seq [opt] declarator [opt] ; */
16552
16553 static void
16554 cp_parser_explicit_instantiation (cp_parser* parser)
16555 {
16556 int declares_class_or_enum;
16557 cp_decl_specifier_seq decl_specifiers;
16558 tree extension_specifier = NULL_TREE;
16559
16560 timevar_push (TV_TEMPLATE_INST);
16561
16562 /* Look for an (optional) storage-class-specifier or
16563 function-specifier. */
16564 if (cp_parser_allow_gnu_extensions_p (parser))
16565 {
16566 extension_specifier
16567 = cp_parser_storage_class_specifier_opt (parser);
16568 if (!extension_specifier)
16569 extension_specifier
16570 = cp_parser_function_specifier_opt (parser,
16571 /*decl_specs=*/NULL);
16572 }
16573
16574 /* Look for the `template' keyword. */
16575 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16576 /* Let the front end know that we are processing an explicit
16577 instantiation. */
16578 begin_explicit_instantiation ();
16579 /* [temp.explicit] says that we are supposed to ignore access
16580 control while processing explicit instantiation directives. */
16581 push_deferring_access_checks (dk_no_check);
16582 /* Parse a decl-specifier-seq. */
16583 cp_parser_decl_specifier_seq (parser,
16584 CP_PARSER_FLAGS_OPTIONAL,
16585 &decl_specifiers,
16586 &declares_class_or_enum);
16587 /* If there was exactly one decl-specifier, and it declared a class,
16588 and there's no declarator, then we have an explicit type
16589 instantiation. */
16590 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16591 {
16592 tree type;
16593
16594 type = check_tag_decl (&decl_specifiers,
16595 /*explicit_type_instantiation_p=*/true);
16596 /* Turn access control back on for names used during
16597 template instantiation. */
16598 pop_deferring_access_checks ();
16599 if (type)
16600 do_type_instantiation (type, extension_specifier,
16601 /*complain=*/tf_error);
16602 }
16603 else
16604 {
16605 cp_declarator *declarator;
16606 tree decl;
16607
16608 /* Parse the declarator. */
16609 declarator
16610 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16611 /*ctor_dtor_or_conv_p=*/NULL,
16612 /*parenthesized_p=*/NULL,
16613 /*member_p=*/false,
16614 /*friend_p=*/false);
16615 if (declares_class_or_enum & 2)
16616 cp_parser_check_for_definition_in_return_type (declarator,
16617 decl_specifiers.type,
16618 decl_specifiers.locations[ds_type_spec]);
16619 if (declarator != cp_error_declarator)
16620 {
16621 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16622 permerror (decl_specifiers.locations[ds_inline],
16623 "explicit instantiation shall not use"
16624 " %<inline%> specifier");
16625 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16626 permerror (decl_specifiers.locations[ds_constexpr],
16627 "explicit instantiation shall not use"
16628 " %<constexpr%> specifier");
16629
16630 decl = grokdeclarator (declarator, &decl_specifiers,
16631 NORMAL, 0, &decl_specifiers.attributes);
16632 /* Turn access control back on for names used during
16633 template instantiation. */
16634 pop_deferring_access_checks ();
16635 /* Do the explicit instantiation. */
16636 do_decl_instantiation (decl, extension_specifier);
16637 }
16638 else
16639 {
16640 pop_deferring_access_checks ();
16641 /* Skip the body of the explicit instantiation. */
16642 cp_parser_skip_to_end_of_statement (parser);
16643 }
16644 }
16645 /* We're done with the instantiation. */
16646 end_explicit_instantiation ();
16647
16648 cp_parser_consume_semicolon_at_end_of_statement (parser);
16649
16650 timevar_pop (TV_TEMPLATE_INST);
16651 }
16652
16653 /* Parse an explicit-specialization.
16654
16655 explicit-specialization:
16656 template < > declaration
16657
16658 Although the standard says `declaration', what it really means is:
16659
16660 explicit-specialization:
16661 template <> decl-specifier [opt] init-declarator [opt] ;
16662 template <> function-definition
16663 template <> explicit-specialization
16664 template <> template-declaration */
16665
16666 static void
16667 cp_parser_explicit_specialization (cp_parser* parser)
16668 {
16669 bool need_lang_pop;
16670 cp_token *token = cp_lexer_peek_token (parser->lexer);
16671
16672 /* Look for the `template' keyword. */
16673 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16674 /* Look for the `<'. */
16675 cp_parser_require (parser, CPP_LESS, RT_LESS);
16676 /* Look for the `>'. */
16677 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16678 /* We have processed another parameter list. */
16679 ++parser->num_template_parameter_lists;
16680 /* [temp]
16681
16682 A template ... explicit specialization ... shall not have C
16683 linkage. */
16684 if (current_lang_name == lang_name_c)
16685 {
16686 error_at (token->location, "template specialization with C linkage");
16687 maybe_show_extern_c_location ();
16688 /* Give it C++ linkage to avoid confusing other parts of the
16689 front end. */
16690 push_lang_context (lang_name_cplusplus);
16691 need_lang_pop = true;
16692 }
16693 else
16694 need_lang_pop = false;
16695 /* Let the front end know that we are beginning a specialization. */
16696 if (!begin_specialization ())
16697 {
16698 end_specialization ();
16699 return;
16700 }
16701
16702 /* If the next keyword is `template', we need to figure out whether
16703 or not we're looking a template-declaration. */
16704 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16705 {
16706 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16707 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16708 cp_parser_template_declaration_after_export (parser,
16709 /*member_p=*/false);
16710 else
16711 cp_parser_explicit_specialization (parser);
16712 }
16713 else
16714 /* Parse the dependent declaration. */
16715 cp_parser_single_declaration (parser,
16716 /*checks=*/NULL,
16717 /*member_p=*/false,
16718 /*explicit_specialization_p=*/true,
16719 /*friend_p=*/NULL);
16720 /* We're done with the specialization. */
16721 end_specialization ();
16722 /* For the erroneous case of a template with C linkage, we pushed an
16723 implicit C++ linkage scope; exit that scope now. */
16724 if (need_lang_pop)
16725 pop_lang_context ();
16726 /* We're done with this parameter list. */
16727 --parser->num_template_parameter_lists;
16728 }
16729
16730 /* Parse a type-specifier.
16731
16732 type-specifier:
16733 simple-type-specifier
16734 class-specifier
16735 enum-specifier
16736 elaborated-type-specifier
16737 cv-qualifier
16738
16739 GNU Extension:
16740
16741 type-specifier:
16742 __complex__
16743
16744 Returns a representation of the type-specifier. For a
16745 class-specifier, enum-specifier, or elaborated-type-specifier, a
16746 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16747
16748 The parser flags FLAGS is used to control type-specifier parsing.
16749
16750 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16751 in a decl-specifier-seq.
16752
16753 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16754 class-specifier, enum-specifier, or elaborated-type-specifier, then
16755 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16756 if a type is declared; 2 if it is defined. Otherwise, it is set to
16757 zero.
16758
16759 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16760 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16761 is set to FALSE. */
16762
16763 static tree
16764 cp_parser_type_specifier (cp_parser* parser,
16765 cp_parser_flags flags,
16766 cp_decl_specifier_seq *decl_specs,
16767 bool is_declaration,
16768 int* declares_class_or_enum,
16769 bool* is_cv_qualifier)
16770 {
16771 tree type_spec = NULL_TREE;
16772 cp_token *token;
16773 enum rid keyword;
16774 cp_decl_spec ds = ds_last;
16775
16776 /* Assume this type-specifier does not declare a new type. */
16777 if (declares_class_or_enum)
16778 *declares_class_or_enum = 0;
16779 /* And that it does not specify a cv-qualifier. */
16780 if (is_cv_qualifier)
16781 *is_cv_qualifier = false;
16782 /* Peek at the next token. */
16783 token = cp_lexer_peek_token (parser->lexer);
16784
16785 /* If we're looking at a keyword, we can use that to guide the
16786 production we choose. */
16787 keyword = token->keyword;
16788 switch (keyword)
16789 {
16790 case RID_ENUM:
16791 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16792 goto elaborated_type_specifier;
16793
16794 /* Look for the enum-specifier. */
16795 type_spec = cp_parser_enum_specifier (parser);
16796 /* If that worked, we're done. */
16797 if (type_spec)
16798 {
16799 if (declares_class_or_enum)
16800 *declares_class_or_enum = 2;
16801 if (decl_specs)
16802 cp_parser_set_decl_spec_type (decl_specs,
16803 type_spec,
16804 token,
16805 /*type_definition_p=*/true);
16806 return type_spec;
16807 }
16808 else
16809 goto elaborated_type_specifier;
16810
16811 /* Any of these indicate either a class-specifier, or an
16812 elaborated-type-specifier. */
16813 case RID_CLASS:
16814 case RID_STRUCT:
16815 case RID_UNION:
16816 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16817 goto elaborated_type_specifier;
16818
16819 /* Parse tentatively so that we can back up if we don't find a
16820 class-specifier. */
16821 cp_parser_parse_tentatively (parser);
16822 /* Look for the class-specifier. */
16823 type_spec = cp_parser_class_specifier (parser);
16824 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16825 /* If that worked, we're done. */
16826 if (cp_parser_parse_definitely (parser))
16827 {
16828 if (declares_class_or_enum)
16829 *declares_class_or_enum = 2;
16830 if (decl_specs)
16831 cp_parser_set_decl_spec_type (decl_specs,
16832 type_spec,
16833 token,
16834 /*type_definition_p=*/true);
16835 return type_spec;
16836 }
16837
16838 /* Fall through. */
16839 elaborated_type_specifier:
16840 /* We're declaring (not defining) a class or enum. */
16841 if (declares_class_or_enum)
16842 *declares_class_or_enum = 1;
16843
16844 /* Fall through. */
16845 case RID_TYPENAME:
16846 /* Look for an elaborated-type-specifier. */
16847 type_spec
16848 = (cp_parser_elaborated_type_specifier
16849 (parser,
16850 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16851 is_declaration));
16852 if (decl_specs)
16853 cp_parser_set_decl_spec_type (decl_specs,
16854 type_spec,
16855 token,
16856 /*type_definition_p=*/false);
16857 return type_spec;
16858
16859 case RID_CONST:
16860 ds = ds_const;
16861 if (is_cv_qualifier)
16862 *is_cv_qualifier = true;
16863 break;
16864
16865 case RID_VOLATILE:
16866 ds = ds_volatile;
16867 if (is_cv_qualifier)
16868 *is_cv_qualifier = true;
16869 break;
16870
16871 case RID_RESTRICT:
16872 ds = ds_restrict;
16873 if (is_cv_qualifier)
16874 *is_cv_qualifier = true;
16875 break;
16876
16877 case RID_COMPLEX:
16878 /* The `__complex__' keyword is a GNU extension. */
16879 ds = ds_complex;
16880 break;
16881
16882 default:
16883 break;
16884 }
16885
16886 /* Handle simple keywords. */
16887 if (ds != ds_last)
16888 {
16889 if (decl_specs)
16890 {
16891 set_and_check_decl_spec_loc (decl_specs, ds, token);
16892 decl_specs->any_specifiers_p = true;
16893 }
16894 return cp_lexer_consume_token (parser->lexer)->u.value;
16895 }
16896
16897 /* If we do not already have a type-specifier, assume we are looking
16898 at a simple-type-specifier. */
16899 type_spec = cp_parser_simple_type_specifier (parser,
16900 decl_specs,
16901 flags);
16902
16903 /* If we didn't find a type-specifier, and a type-specifier was not
16904 optional in this context, issue an error message. */
16905 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16906 {
16907 cp_parser_error (parser, "expected type specifier");
16908 return error_mark_node;
16909 }
16910
16911 return type_spec;
16912 }
16913
16914 /* Parse a simple-type-specifier.
16915
16916 simple-type-specifier:
16917 :: [opt] nested-name-specifier [opt] type-name
16918 :: [opt] nested-name-specifier template template-id
16919 char
16920 wchar_t
16921 bool
16922 short
16923 int
16924 long
16925 signed
16926 unsigned
16927 float
16928 double
16929 void
16930
16931 C++11 Extension:
16932
16933 simple-type-specifier:
16934 auto
16935 decltype ( expression )
16936 char16_t
16937 char32_t
16938 __underlying_type ( type-id )
16939
16940 C++17 extension:
16941
16942 nested-name-specifier(opt) template-name
16943
16944 GNU Extension:
16945
16946 simple-type-specifier:
16947 __int128
16948 __typeof__ unary-expression
16949 __typeof__ ( type-id )
16950 __typeof__ ( type-id ) { initializer-list , [opt] }
16951
16952 Concepts Extension:
16953
16954 simple-type-specifier:
16955 constrained-type-specifier
16956
16957 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16958 appropriately updated. */
16959
16960 static tree
16961 cp_parser_simple_type_specifier (cp_parser* parser,
16962 cp_decl_specifier_seq *decl_specs,
16963 cp_parser_flags flags)
16964 {
16965 tree type = NULL_TREE;
16966 cp_token *token;
16967 int idx;
16968
16969 /* Peek at the next token. */
16970 token = cp_lexer_peek_token (parser->lexer);
16971
16972 /* If we're looking at a keyword, things are easy. */
16973 switch (token->keyword)
16974 {
16975 case RID_CHAR:
16976 if (decl_specs)
16977 decl_specs->explicit_char_p = true;
16978 type = char_type_node;
16979 break;
16980 case RID_CHAR16:
16981 type = char16_type_node;
16982 break;
16983 case RID_CHAR32:
16984 type = char32_type_node;
16985 break;
16986 case RID_WCHAR:
16987 type = wchar_type_node;
16988 break;
16989 case RID_BOOL:
16990 type = boolean_type_node;
16991 break;
16992 case RID_SHORT:
16993 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16994 type = short_integer_type_node;
16995 break;
16996 case RID_INT:
16997 if (decl_specs)
16998 decl_specs->explicit_int_p = true;
16999 type = integer_type_node;
17000 break;
17001 case RID_INT_N_0:
17002 case RID_INT_N_1:
17003 case RID_INT_N_2:
17004 case RID_INT_N_3:
17005 idx = token->keyword - RID_INT_N_0;
17006 if (! int_n_enabled_p [idx])
17007 break;
17008 if (decl_specs)
17009 {
17010 decl_specs->explicit_intN_p = true;
17011 decl_specs->int_n_idx = idx;
17012 }
17013 type = int_n_trees [idx].signed_type;
17014 break;
17015 case RID_LONG:
17016 if (decl_specs)
17017 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17018 type = long_integer_type_node;
17019 break;
17020 case RID_SIGNED:
17021 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17022 type = integer_type_node;
17023 break;
17024 case RID_UNSIGNED:
17025 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17026 type = unsigned_type_node;
17027 break;
17028 case RID_FLOAT:
17029 type = float_type_node;
17030 break;
17031 case RID_DOUBLE:
17032 type = double_type_node;
17033 break;
17034 case RID_VOID:
17035 type = void_type_node;
17036 break;
17037
17038 case RID_AUTO:
17039 maybe_warn_cpp0x (CPP0X_AUTO);
17040 if (parser->auto_is_implicit_function_template_parm_p)
17041 {
17042 /* The 'auto' might be the placeholder return type for a function decl
17043 with trailing return type. */
17044 bool have_trailing_return_fn_decl = false;
17045
17046 cp_parser_parse_tentatively (parser);
17047 cp_lexer_consume_token (parser->lexer);
17048 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17049 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17050 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17051 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17052 {
17053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17054 {
17055 cp_lexer_consume_token (parser->lexer);
17056 cp_parser_skip_to_closing_parenthesis (parser,
17057 /*recovering*/false,
17058 /*or_comma*/false,
17059 /*consume_paren*/true);
17060 continue;
17061 }
17062
17063 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17064 {
17065 have_trailing_return_fn_decl = true;
17066 break;
17067 }
17068
17069 cp_lexer_consume_token (parser->lexer);
17070 }
17071 cp_parser_abort_tentative_parse (parser);
17072
17073 if (have_trailing_return_fn_decl)
17074 {
17075 type = make_auto ();
17076 break;
17077 }
17078
17079 if (cxx_dialect >= cxx14)
17080 {
17081 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17082 type = TREE_TYPE (type);
17083 }
17084 else
17085 type = error_mark_node;
17086
17087 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17088 {
17089 if (cxx_dialect < cxx14)
17090 error_at (token->location,
17091 "use of %<auto%> in lambda parameter declaration "
17092 "only available with "
17093 "-std=c++14 or -std=gnu++14");
17094 }
17095 else if (cxx_dialect < cxx14)
17096 error_at (token->location,
17097 "use of %<auto%> in parameter declaration "
17098 "only available with "
17099 "-std=c++14 or -std=gnu++14");
17100 else if (!flag_concepts)
17101 pedwarn (token->location, OPT_Wpedantic,
17102 "ISO C++ forbids use of %<auto%> in parameter "
17103 "declaration");
17104 }
17105 else
17106 type = make_auto ();
17107 break;
17108
17109 case RID_DECLTYPE:
17110 /* Since DR 743, decltype can either be a simple-type-specifier by
17111 itself or begin a nested-name-specifier. Parsing it will replace
17112 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17113 handling below decide what to do. */
17114 cp_parser_decltype (parser);
17115 cp_lexer_set_token_position (parser->lexer, token);
17116 break;
17117
17118 case RID_TYPEOF:
17119 /* Consume the `typeof' token. */
17120 cp_lexer_consume_token (parser->lexer);
17121 /* Parse the operand to `typeof'. */
17122 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17123 /* If it is not already a TYPE, take its type. */
17124 if (!TYPE_P (type))
17125 type = finish_typeof (type);
17126
17127 if (decl_specs)
17128 cp_parser_set_decl_spec_type (decl_specs, type,
17129 token,
17130 /*type_definition_p=*/false);
17131
17132 return type;
17133
17134 case RID_UNDERLYING_TYPE:
17135 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17136 if (decl_specs)
17137 cp_parser_set_decl_spec_type (decl_specs, type,
17138 token,
17139 /*type_definition_p=*/false);
17140
17141 return type;
17142
17143 case RID_BASES:
17144 case RID_DIRECT_BASES:
17145 type = cp_parser_trait_expr (parser, token->keyword);
17146 if (decl_specs)
17147 cp_parser_set_decl_spec_type (decl_specs, type,
17148 token,
17149 /*type_definition_p=*/false);
17150 return type;
17151 default:
17152 break;
17153 }
17154
17155 /* If token is an already-parsed decltype not followed by ::,
17156 it's a simple-type-specifier. */
17157 if (token->type == CPP_DECLTYPE
17158 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17159 {
17160 type = saved_checks_value (token->u.tree_check_value);
17161 if (decl_specs)
17162 {
17163 cp_parser_set_decl_spec_type (decl_specs, type,
17164 token,
17165 /*type_definition_p=*/false);
17166 /* Remember that we are handling a decltype in order to
17167 implement the resolution of DR 1510 when the argument
17168 isn't instantiation dependent. */
17169 decl_specs->decltype_p = true;
17170 }
17171 cp_lexer_consume_token (parser->lexer);
17172 return type;
17173 }
17174
17175 /* If the type-specifier was for a built-in type, we're done. */
17176 if (type)
17177 {
17178 /* Record the type. */
17179 if (decl_specs
17180 && (token->keyword != RID_SIGNED
17181 && token->keyword != RID_UNSIGNED
17182 && token->keyword != RID_SHORT
17183 && token->keyword != RID_LONG))
17184 cp_parser_set_decl_spec_type (decl_specs,
17185 type,
17186 token,
17187 /*type_definition_p=*/false);
17188 if (decl_specs)
17189 decl_specs->any_specifiers_p = true;
17190
17191 /* Consume the token. */
17192 cp_lexer_consume_token (parser->lexer);
17193
17194 if (type == error_mark_node)
17195 return error_mark_node;
17196
17197 /* There is no valid C++ program where a non-template type is
17198 followed by a "<". That usually indicates that the user thought
17199 that the type was a template. */
17200 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17201 token->location);
17202
17203 return TYPE_NAME (type);
17204 }
17205
17206 /* The type-specifier must be a user-defined type. */
17207 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17208 {
17209 bool qualified_p;
17210 bool global_p;
17211
17212 /* Don't gobble tokens or issue error messages if this is an
17213 optional type-specifier. */
17214 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17215 cp_parser_parse_tentatively (parser);
17216
17217 token = cp_lexer_peek_token (parser->lexer);
17218
17219 /* Look for the optional `::' operator. */
17220 global_p
17221 = (cp_parser_global_scope_opt (parser,
17222 /*current_scope_valid_p=*/false)
17223 != NULL_TREE);
17224 /* Look for the nested-name specifier. */
17225 qualified_p
17226 = (cp_parser_nested_name_specifier_opt (parser,
17227 /*typename_keyword_p=*/false,
17228 /*check_dependency_p=*/true,
17229 /*type_p=*/false,
17230 /*is_declaration=*/false)
17231 != NULL_TREE);
17232 /* If we have seen a nested-name-specifier, and the next token
17233 is `template', then we are using the template-id production. */
17234 if (parser->scope
17235 && cp_parser_optional_template_keyword (parser))
17236 {
17237 /* Look for the template-id. */
17238 type = cp_parser_template_id (parser,
17239 /*template_keyword_p=*/true,
17240 /*check_dependency_p=*/true,
17241 none_type,
17242 /*is_declaration=*/false);
17243 /* If the template-id did not name a type, we are out of
17244 luck. */
17245 if (TREE_CODE (type) != TYPE_DECL)
17246 {
17247 cp_parser_error (parser, "expected template-id for type");
17248 type = NULL_TREE;
17249 }
17250 }
17251 /* Otherwise, look for a type-name. */
17252 else
17253 type = cp_parser_type_name (parser);
17254 /* Keep track of all name-lookups performed in class scopes. */
17255 if (type
17256 && !global_p
17257 && !qualified_p
17258 && TREE_CODE (type) == TYPE_DECL
17259 && identifier_p (DECL_NAME (type)))
17260 maybe_note_name_used_in_class (DECL_NAME (type), type);
17261 /* If it didn't work out, we don't have a TYPE. */
17262 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17263 && !cp_parser_parse_definitely (parser))
17264 type = NULL_TREE;
17265 if (!type && cxx_dialect >= cxx17)
17266 {
17267 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17268 cp_parser_parse_tentatively (parser);
17269
17270 cp_parser_global_scope_opt (parser,
17271 /*current_scope_valid_p=*/false);
17272 cp_parser_nested_name_specifier_opt (parser,
17273 /*typename_keyword_p=*/false,
17274 /*check_dependency_p=*/true,
17275 /*type_p=*/false,
17276 /*is_declaration=*/false);
17277 tree name = cp_parser_identifier (parser);
17278 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17279 && parser->scope != error_mark_node)
17280 {
17281 tree tmpl = cp_parser_lookup_name (parser, name,
17282 none_type,
17283 /*is_template=*/false,
17284 /*is_namespace=*/false,
17285 /*check_dependency=*/true,
17286 /*ambiguous_decls=*/NULL,
17287 token->location);
17288 if (tmpl && tmpl != error_mark_node
17289 && (DECL_CLASS_TEMPLATE_P (tmpl)
17290 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17291 type = make_template_placeholder (tmpl);
17292 else
17293 {
17294 type = error_mark_node;
17295 if (!cp_parser_simulate_error (parser))
17296 cp_parser_name_lookup_error (parser, name, tmpl,
17297 NLE_TYPE, token->location);
17298 }
17299 }
17300 else
17301 type = error_mark_node;
17302
17303 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17304 && !cp_parser_parse_definitely (parser))
17305 type = NULL_TREE;
17306 }
17307 if (type && decl_specs)
17308 cp_parser_set_decl_spec_type (decl_specs, type,
17309 token,
17310 /*type_definition_p=*/false);
17311 }
17312
17313 /* If we didn't get a type-name, issue an error message. */
17314 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17315 {
17316 cp_parser_error (parser, "expected type-name");
17317 return error_mark_node;
17318 }
17319
17320 if (type && type != error_mark_node)
17321 {
17322 /* See if TYPE is an Objective-C type, and if so, parse and
17323 accept any protocol references following it. Do this before
17324 the cp_parser_check_for_invalid_template_id() call, because
17325 Objective-C types can be followed by '<...>' which would
17326 enclose protocol names rather than template arguments, and so
17327 everything is fine. */
17328 if (c_dialect_objc () && !parser->scope
17329 && (objc_is_id (type) || objc_is_class_name (type)))
17330 {
17331 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17332 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17333
17334 /* Clobber the "unqualified" type previously entered into
17335 DECL_SPECS with the new, improved protocol-qualified version. */
17336 if (decl_specs)
17337 decl_specs->type = qual_type;
17338
17339 return qual_type;
17340 }
17341
17342 /* There is no valid C++ program where a non-template type is
17343 followed by a "<". That usually indicates that the user
17344 thought that the type was a template. */
17345 cp_parser_check_for_invalid_template_id (parser, type,
17346 none_type,
17347 token->location);
17348 }
17349
17350 return type;
17351 }
17352
17353 /* Parse a type-name.
17354
17355 type-name:
17356 class-name
17357 enum-name
17358 typedef-name
17359 simple-template-id [in c++0x]
17360
17361 enum-name:
17362 identifier
17363
17364 typedef-name:
17365 identifier
17366
17367 Concepts:
17368
17369 type-name:
17370 concept-name
17371 partial-concept-id
17372
17373 concept-name:
17374 identifier
17375
17376 Returns a TYPE_DECL for the type. */
17377
17378 static tree
17379 cp_parser_type_name (cp_parser* parser)
17380 {
17381 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17382 }
17383
17384 /* See above. */
17385 static tree
17386 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17387 {
17388 tree type_decl;
17389
17390 /* We can't know yet whether it is a class-name or not. */
17391 cp_parser_parse_tentatively (parser);
17392 /* Try a class-name. */
17393 type_decl = cp_parser_class_name (parser,
17394 typename_keyword_p,
17395 /*template_keyword_p=*/false,
17396 none_type,
17397 /*check_dependency_p=*/true,
17398 /*class_head_p=*/false,
17399 /*is_declaration=*/false);
17400 /* If it's not a class-name, keep looking. */
17401 if (!cp_parser_parse_definitely (parser))
17402 {
17403 if (cxx_dialect < cxx11)
17404 /* It must be a typedef-name or an enum-name. */
17405 return cp_parser_nonclass_name (parser);
17406
17407 cp_parser_parse_tentatively (parser);
17408 /* It is either a simple-template-id representing an
17409 instantiation of an alias template... */
17410 type_decl = cp_parser_template_id (parser,
17411 /*template_keyword_p=*/false,
17412 /*check_dependency_p=*/true,
17413 none_type,
17414 /*is_declaration=*/false);
17415 /* Note that this must be an instantiation of an alias template
17416 because [temp.names]/6 says:
17417
17418 A template-id that names an alias template specialization
17419 is a type-name.
17420
17421 Whereas [temp.names]/7 says:
17422
17423 A simple-template-id that names a class template
17424 specialization is a class-name.
17425
17426 With concepts, this could also be a partial-concept-id that
17427 declares a non-type template parameter. */
17428 if (type_decl != NULL_TREE
17429 && TREE_CODE (type_decl) == TYPE_DECL
17430 && TYPE_DECL_ALIAS_P (type_decl))
17431 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17432 else if (is_constrained_parameter (type_decl))
17433 /* Don't do anything. */ ;
17434 else
17435 cp_parser_simulate_error (parser);
17436
17437 if (!cp_parser_parse_definitely (parser))
17438 /* ... Or a typedef-name or an enum-name. */
17439 return cp_parser_nonclass_name (parser);
17440 }
17441
17442 return type_decl;
17443 }
17444
17445 /* Check if DECL and ARGS can form a constrained-type-specifier.
17446 If ARGS is non-null, we try to form a concept check of the
17447 form DECL<?, ARGS> where ? is a wildcard that matches any
17448 kind of template argument. If ARGS is NULL, then we try to
17449 form a concept check of the form DECL<?>. */
17450
17451 static tree
17452 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17453 tree decl, tree args)
17454 {
17455 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17456
17457 /* If we a constrained-type-specifier cannot be deduced. */
17458 if (parser->prevent_constrained_type_specifiers)
17459 return NULL_TREE;
17460
17461 /* A constrained type specifier can only be found in an
17462 overload set or as a reference to a template declaration.
17463
17464 FIXME: This might be masking a bug. It's possible that
17465 that the deduction below is causing template specializations
17466 to be formed with the wildcard as an argument. */
17467 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17468 return NULL_TREE;
17469
17470 /* Try to build a call expression that evaluates the
17471 concept. This can fail if the overload set refers
17472 only to non-templates. */
17473 tree placeholder = build_nt (WILDCARD_DECL);
17474 tree check = build_concept_check (decl, placeholder, args);
17475 if (check == error_mark_node)
17476 return NULL_TREE;
17477
17478 /* Deduce the checked constraint and the prototype parameter.
17479
17480 FIXME: In certain cases, failure to deduce should be a
17481 diagnosable error. */
17482 tree conc;
17483 tree proto;
17484 if (!deduce_constrained_parameter (check, conc, proto))
17485 return NULL_TREE;
17486
17487 /* In template parameter scope, this results in a constrained
17488 parameter. Return a descriptor of that parm. */
17489 if (processing_template_parmlist)
17490 return build_constrained_parameter (conc, proto, args);
17491
17492 /* In a parameter-declaration-clause, constrained-type
17493 specifiers result in invented template parameters. */
17494 if (parser->auto_is_implicit_function_template_parm_p)
17495 {
17496 tree x = build_constrained_parameter (conc, proto, args);
17497 return synthesize_implicit_template_parm (parser, x);
17498 }
17499 else
17500 {
17501 /* Otherwise, we're in a context where the constrained
17502 type name is deduced and the constraint applies
17503 after deduction. */
17504 return make_constrained_auto (conc, args);
17505 }
17506
17507 return NULL_TREE;
17508 }
17509
17510 /* If DECL refers to a concept, return a TYPE_DECL representing
17511 the result of using the constrained type specifier in the
17512 current context. DECL refers to a concept if
17513
17514 - it is an overload set containing a function concept taking a single
17515 type argument, or
17516
17517 - it is a variable concept taking a single type argument. */
17518
17519 static tree
17520 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17521 {
17522 if (flag_concepts
17523 && (TREE_CODE (decl) == OVERLOAD
17524 || BASELINK_P (decl)
17525 || variable_concept_p (decl)))
17526 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17527 else
17528 return NULL_TREE;
17529 }
17530
17531 /* Check if DECL and ARGS form a partial-concept-id. If so,
17532 assign ID to the resulting constrained placeholder.
17533
17534 Returns true if the partial-concept-id designates a placeholder
17535 and false otherwise. Note that *id is set to NULL_TREE in
17536 this case. */
17537
17538 static tree
17539 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17540 {
17541 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17542 }
17543
17544 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17545 or a concept-name.
17546
17547 enum-name:
17548 identifier
17549
17550 typedef-name:
17551 identifier
17552
17553 concept-name:
17554 identifier
17555
17556 Returns a TYPE_DECL for the type. */
17557
17558 static tree
17559 cp_parser_nonclass_name (cp_parser* parser)
17560 {
17561 tree type_decl;
17562 tree identifier;
17563
17564 cp_token *token = cp_lexer_peek_token (parser->lexer);
17565 identifier = cp_parser_identifier (parser);
17566 if (identifier == error_mark_node)
17567 return error_mark_node;
17568
17569 /* Look up the type-name. */
17570 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17571
17572 type_decl = strip_using_decl (type_decl);
17573
17574 /* If we found an overload set, then it may refer to a concept-name. */
17575 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17576 type_decl = decl;
17577
17578 if (TREE_CODE (type_decl) != TYPE_DECL
17579 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17580 {
17581 /* See if this is an Objective-C type. */
17582 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17583 tree type = objc_get_protocol_qualified_type (identifier, protos);
17584 if (type)
17585 type_decl = TYPE_NAME (type);
17586 }
17587
17588 /* Issue an error if we did not find a type-name. */
17589 if (TREE_CODE (type_decl) != TYPE_DECL
17590 /* In Objective-C, we have the complication that class names are
17591 normally type names and start declarations (eg, the
17592 "NSObject" in "NSObject *object;"), but can be used in an
17593 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17594 is an expression. So, a classname followed by a dot is not a
17595 valid type-name. */
17596 || (objc_is_class_name (TREE_TYPE (type_decl))
17597 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17598 {
17599 if (!cp_parser_simulate_error (parser))
17600 cp_parser_name_lookup_error (parser, identifier, type_decl,
17601 NLE_TYPE, token->location);
17602 return error_mark_node;
17603 }
17604 /* Remember that the name was used in the definition of the
17605 current class so that we can check later to see if the
17606 meaning would have been different after the class was
17607 entirely defined. */
17608 else if (type_decl != error_mark_node
17609 && !parser->scope)
17610 maybe_note_name_used_in_class (identifier, type_decl);
17611
17612 return type_decl;
17613 }
17614
17615 /* Parse an elaborated-type-specifier. Note that the grammar given
17616 here incorporates the resolution to DR68.
17617
17618 elaborated-type-specifier:
17619 class-key :: [opt] nested-name-specifier [opt] identifier
17620 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17621 enum-key :: [opt] nested-name-specifier [opt] identifier
17622 typename :: [opt] nested-name-specifier identifier
17623 typename :: [opt] nested-name-specifier template [opt]
17624 template-id
17625
17626 GNU extension:
17627
17628 elaborated-type-specifier:
17629 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17630 class-key attributes :: [opt] nested-name-specifier [opt]
17631 template [opt] template-id
17632 enum attributes :: [opt] nested-name-specifier [opt] identifier
17633
17634 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17635 declared `friend'. If IS_DECLARATION is TRUE, then this
17636 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17637 something is being declared.
17638
17639 Returns the TYPE specified. */
17640
17641 static tree
17642 cp_parser_elaborated_type_specifier (cp_parser* parser,
17643 bool is_friend,
17644 bool is_declaration)
17645 {
17646 enum tag_types tag_type;
17647 tree identifier;
17648 tree type = NULL_TREE;
17649 tree attributes = NULL_TREE;
17650 tree globalscope;
17651 cp_token *token = NULL;
17652
17653 /* See if we're looking at the `enum' keyword. */
17654 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17655 {
17656 /* Consume the `enum' token. */
17657 cp_lexer_consume_token (parser->lexer);
17658 /* Remember that it's an enumeration type. */
17659 tag_type = enum_type;
17660 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17661 enums) is used here. */
17662 cp_token *token = cp_lexer_peek_token (parser->lexer);
17663 if (cp_parser_is_keyword (token, RID_CLASS)
17664 || cp_parser_is_keyword (token, RID_STRUCT))
17665 {
17666 gcc_rich_location richloc (token->location);
17667 richloc.add_range (input_location, false);
17668 richloc.add_fixit_remove ();
17669 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17670 "a scoped enum must not use the %qD keyword",
17671 token->u.value);
17672 /* Consume the `struct' or `class' and parse it anyway. */
17673 cp_lexer_consume_token (parser->lexer);
17674 }
17675 /* Parse the attributes. */
17676 attributes = cp_parser_attributes_opt (parser);
17677 }
17678 /* Or, it might be `typename'. */
17679 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17680 RID_TYPENAME))
17681 {
17682 /* Consume the `typename' token. */
17683 cp_lexer_consume_token (parser->lexer);
17684 /* Remember that it's a `typename' type. */
17685 tag_type = typename_type;
17686 }
17687 /* Otherwise it must be a class-key. */
17688 else
17689 {
17690 tag_type = cp_parser_class_key (parser);
17691 if (tag_type == none_type)
17692 return error_mark_node;
17693 /* Parse the attributes. */
17694 attributes = cp_parser_attributes_opt (parser);
17695 }
17696
17697 /* Look for the `::' operator. */
17698 globalscope = cp_parser_global_scope_opt (parser,
17699 /*current_scope_valid_p=*/false);
17700 /* Look for the nested-name-specifier. */
17701 tree nested_name_specifier;
17702 if (tag_type == typename_type && !globalscope)
17703 {
17704 nested_name_specifier
17705 = cp_parser_nested_name_specifier (parser,
17706 /*typename_keyword_p=*/true,
17707 /*check_dependency_p=*/true,
17708 /*type_p=*/true,
17709 is_declaration);
17710 if (!nested_name_specifier)
17711 return error_mark_node;
17712 }
17713 else
17714 /* Even though `typename' is not present, the proposed resolution
17715 to Core Issue 180 says that in `class A<T>::B', `B' should be
17716 considered a type-name, even if `A<T>' is dependent. */
17717 nested_name_specifier
17718 = cp_parser_nested_name_specifier_opt (parser,
17719 /*typename_keyword_p=*/true,
17720 /*check_dependency_p=*/true,
17721 /*type_p=*/true,
17722 is_declaration);
17723 /* For everything but enumeration types, consider a template-id.
17724 For an enumeration type, consider only a plain identifier. */
17725 if (tag_type != enum_type)
17726 {
17727 bool template_p = false;
17728 tree decl;
17729
17730 /* Allow the `template' keyword. */
17731 template_p = cp_parser_optional_template_keyword (parser);
17732 /* If we didn't see `template', we don't know if there's a
17733 template-id or not. */
17734 if (!template_p)
17735 cp_parser_parse_tentatively (parser);
17736 /* Parse the template-id. */
17737 token = cp_lexer_peek_token (parser->lexer);
17738 decl = cp_parser_template_id (parser, template_p,
17739 /*check_dependency_p=*/true,
17740 tag_type,
17741 is_declaration);
17742 /* If we didn't find a template-id, look for an ordinary
17743 identifier. */
17744 if (!template_p && !cp_parser_parse_definitely (parser))
17745 ;
17746 /* We can get here when cp_parser_template_id, called by
17747 cp_parser_class_name with tag_type == none_type, succeeds
17748 and caches a BASELINK. Then, when called again here,
17749 instead of failing and returning an error_mark_node
17750 returns it (see template/typename17.C in C++11).
17751 ??? Could we diagnose this earlier? */
17752 else if (tag_type == typename_type && BASELINK_P (decl))
17753 {
17754 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17755 type = error_mark_node;
17756 }
17757 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17758 in effect, then we must assume that, upon instantiation, the
17759 template will correspond to a class. */
17760 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17761 && tag_type == typename_type)
17762 type = make_typename_type (parser->scope, decl,
17763 typename_type,
17764 /*complain=*/tf_error);
17765 /* If the `typename' keyword is in effect and DECL is not a type
17766 decl, then type is non existent. */
17767 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17768 ;
17769 else if (TREE_CODE (decl) == TYPE_DECL)
17770 {
17771 type = check_elaborated_type_specifier (tag_type, decl,
17772 /*allow_template_p=*/true);
17773
17774 /* If the next token is a semicolon, this must be a specialization,
17775 instantiation, or friend declaration. Check the scope while we
17776 still know whether or not we had a nested-name-specifier. */
17777 if (type != error_mark_node
17778 && !nested_name_specifier && !is_friend
17779 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17780 check_unqualified_spec_or_inst (type, token->location);
17781 }
17782 else if (decl == error_mark_node)
17783 type = error_mark_node;
17784 }
17785
17786 if (!type)
17787 {
17788 token = cp_lexer_peek_token (parser->lexer);
17789 identifier = cp_parser_identifier (parser);
17790
17791 if (identifier == error_mark_node)
17792 {
17793 parser->scope = NULL_TREE;
17794 return error_mark_node;
17795 }
17796
17797 /* For a `typename', we needn't call xref_tag. */
17798 if (tag_type == typename_type
17799 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17800 return cp_parser_make_typename_type (parser, identifier,
17801 token->location);
17802
17803 /* Template parameter lists apply only if we are not within a
17804 function parameter list. */
17805 bool template_parm_lists_apply
17806 = parser->num_template_parameter_lists;
17807 if (template_parm_lists_apply)
17808 for (cp_binding_level *s = current_binding_level;
17809 s && s->kind != sk_template_parms;
17810 s = s->level_chain)
17811 if (s->kind == sk_function_parms)
17812 template_parm_lists_apply = false;
17813
17814 /* Look up a qualified name in the usual way. */
17815 if (parser->scope)
17816 {
17817 tree decl;
17818 tree ambiguous_decls;
17819
17820 decl = cp_parser_lookup_name (parser, identifier,
17821 tag_type,
17822 /*is_template=*/false,
17823 /*is_namespace=*/false,
17824 /*check_dependency=*/true,
17825 &ambiguous_decls,
17826 token->location);
17827
17828 /* If the lookup was ambiguous, an error will already have been
17829 issued. */
17830 if (ambiguous_decls)
17831 return error_mark_node;
17832
17833 /* If we are parsing friend declaration, DECL may be a
17834 TEMPLATE_DECL tree node here. However, we need to check
17835 whether this TEMPLATE_DECL results in valid code. Consider
17836 the following example:
17837
17838 namespace N {
17839 template <class T> class C {};
17840 }
17841 class X {
17842 template <class T> friend class N::C; // #1, valid code
17843 };
17844 template <class T> class Y {
17845 friend class N::C; // #2, invalid code
17846 };
17847
17848 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17849 name lookup of `N::C'. We see that friend declaration must
17850 be template for the code to be valid. Note that
17851 processing_template_decl does not work here since it is
17852 always 1 for the above two cases. */
17853
17854 decl = (cp_parser_maybe_treat_template_as_class
17855 (decl, /*tag_name_p=*/is_friend
17856 && template_parm_lists_apply));
17857
17858 if (TREE_CODE (decl) != TYPE_DECL)
17859 {
17860 cp_parser_diagnose_invalid_type_name (parser,
17861 identifier,
17862 token->location);
17863 return error_mark_node;
17864 }
17865
17866 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17867 {
17868 bool allow_template = (template_parm_lists_apply
17869 || DECL_SELF_REFERENCE_P (decl));
17870 type = check_elaborated_type_specifier (tag_type, decl,
17871 allow_template);
17872
17873 if (type == error_mark_node)
17874 return error_mark_node;
17875 }
17876
17877 /* Forward declarations of nested types, such as
17878
17879 class C1::C2;
17880 class C1::C2::C3;
17881
17882 are invalid unless all components preceding the final '::'
17883 are complete. If all enclosing types are complete, these
17884 declarations become merely pointless.
17885
17886 Invalid forward declarations of nested types are errors
17887 caught elsewhere in parsing. Those that are pointless arrive
17888 here. */
17889
17890 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17891 && !is_friend && !processing_explicit_instantiation)
17892 warning (0, "declaration %qD does not declare anything", decl);
17893
17894 type = TREE_TYPE (decl);
17895 }
17896 else
17897 {
17898 /* An elaborated-type-specifier sometimes introduces a new type and
17899 sometimes names an existing type. Normally, the rule is that it
17900 introduces a new type only if there is not an existing type of
17901 the same name already in scope. For example, given:
17902
17903 struct S {};
17904 void f() { struct S s; }
17905
17906 the `struct S' in the body of `f' is the same `struct S' as in
17907 the global scope; the existing definition is used. However, if
17908 there were no global declaration, this would introduce a new
17909 local class named `S'.
17910
17911 An exception to this rule applies to the following code:
17912
17913 namespace N { struct S; }
17914
17915 Here, the elaborated-type-specifier names a new type
17916 unconditionally; even if there is already an `S' in the
17917 containing scope this declaration names a new type.
17918 This exception only applies if the elaborated-type-specifier
17919 forms the complete declaration:
17920
17921 [class.name]
17922
17923 A declaration consisting solely of `class-key identifier ;' is
17924 either a redeclaration of the name in the current scope or a
17925 forward declaration of the identifier as a class name. It
17926 introduces the name into the current scope.
17927
17928 We are in this situation precisely when the next token is a `;'.
17929
17930 An exception to the exception is that a `friend' declaration does
17931 *not* name a new type; i.e., given:
17932
17933 struct S { friend struct T; };
17934
17935 `T' is not a new type in the scope of `S'.
17936
17937 Also, `new struct S' or `sizeof (struct S)' never results in the
17938 definition of a new type; a new type can only be declared in a
17939 declaration context. */
17940
17941 tag_scope ts;
17942 bool template_p;
17943
17944 if (is_friend)
17945 /* Friends have special name lookup rules. */
17946 ts = ts_within_enclosing_non_class;
17947 else if (is_declaration
17948 && cp_lexer_next_token_is (parser->lexer,
17949 CPP_SEMICOLON))
17950 /* This is a `class-key identifier ;' */
17951 ts = ts_current;
17952 else
17953 ts = ts_global;
17954
17955 template_p =
17956 (template_parm_lists_apply
17957 && (cp_parser_next_token_starts_class_definition_p (parser)
17958 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17959 /* An unqualified name was used to reference this type, so
17960 there were no qualifying templates. */
17961 if (template_parm_lists_apply
17962 && !cp_parser_check_template_parameters (parser,
17963 /*num_templates=*/0,
17964 token->location,
17965 /*declarator=*/NULL))
17966 return error_mark_node;
17967 type = xref_tag (tag_type, identifier, ts, template_p);
17968 }
17969 }
17970
17971 if (type == error_mark_node)
17972 return error_mark_node;
17973
17974 /* Allow attributes on forward declarations of classes. */
17975 if (attributes)
17976 {
17977 if (TREE_CODE (type) == TYPENAME_TYPE)
17978 warning (OPT_Wattributes,
17979 "attributes ignored on uninstantiated type");
17980 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17981 && ! processing_explicit_instantiation)
17982 warning (OPT_Wattributes,
17983 "attributes ignored on template instantiation");
17984 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17985 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17986 else
17987 warning (OPT_Wattributes,
17988 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17989 }
17990
17991 if (tag_type != enum_type)
17992 {
17993 /* Indicate whether this class was declared as a `class' or as a
17994 `struct'. */
17995 if (CLASS_TYPE_P (type))
17996 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17997 cp_parser_check_class_key (tag_type, type);
17998 }
17999
18000 /* A "<" cannot follow an elaborated type specifier. If that
18001 happens, the user was probably trying to form a template-id. */
18002 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18003 token->location);
18004
18005 return type;
18006 }
18007
18008 /* Parse an enum-specifier.
18009
18010 enum-specifier:
18011 enum-head { enumerator-list [opt] }
18012 enum-head { enumerator-list , } [C++0x]
18013
18014 enum-head:
18015 enum-key identifier [opt] enum-base [opt]
18016 enum-key nested-name-specifier identifier enum-base [opt]
18017
18018 enum-key:
18019 enum
18020 enum class [C++0x]
18021 enum struct [C++0x]
18022
18023 enum-base: [C++0x]
18024 : type-specifier-seq
18025
18026 opaque-enum-specifier:
18027 enum-key identifier enum-base [opt] ;
18028
18029 GNU Extensions:
18030 enum-key attributes[opt] identifier [opt] enum-base [opt]
18031 { enumerator-list [opt] }attributes[opt]
18032 enum-key attributes[opt] identifier [opt] enum-base [opt]
18033 { enumerator-list, }attributes[opt] [C++0x]
18034
18035 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18036 if the token stream isn't an enum-specifier after all. */
18037
18038 static tree
18039 cp_parser_enum_specifier (cp_parser* parser)
18040 {
18041 tree identifier;
18042 tree type = NULL_TREE;
18043 tree prev_scope;
18044 tree nested_name_specifier = NULL_TREE;
18045 tree attributes;
18046 bool scoped_enum_p = false;
18047 bool has_underlying_type = false;
18048 bool nested_being_defined = false;
18049 bool new_value_list = false;
18050 bool is_new_type = false;
18051 bool is_unnamed = false;
18052 tree underlying_type = NULL_TREE;
18053 cp_token *type_start_token = NULL;
18054 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18055
18056 parser->colon_corrects_to_scope_p = false;
18057
18058 /* Parse tentatively so that we can back up if we don't find a
18059 enum-specifier. */
18060 cp_parser_parse_tentatively (parser);
18061
18062 /* Caller guarantees that the current token is 'enum', an identifier
18063 possibly follows, and the token after that is an opening brace.
18064 If we don't have an identifier, fabricate an anonymous name for
18065 the enumeration being defined. */
18066 cp_lexer_consume_token (parser->lexer);
18067
18068 /* Parse the "class" or "struct", which indicates a scoped
18069 enumeration type in C++0x. */
18070 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18071 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18072 {
18073 if (cxx_dialect < cxx11)
18074 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18075
18076 /* Consume the `struct' or `class' token. */
18077 cp_lexer_consume_token (parser->lexer);
18078
18079 scoped_enum_p = true;
18080 }
18081
18082 attributes = cp_parser_attributes_opt (parser);
18083
18084 /* Clear the qualification. */
18085 parser->scope = NULL_TREE;
18086 parser->qualifying_scope = NULL_TREE;
18087 parser->object_scope = NULL_TREE;
18088
18089 /* Figure out in what scope the declaration is being placed. */
18090 prev_scope = current_scope ();
18091
18092 type_start_token = cp_lexer_peek_token (parser->lexer);
18093
18094 push_deferring_access_checks (dk_no_check);
18095 nested_name_specifier
18096 = cp_parser_nested_name_specifier_opt (parser,
18097 /*typename_keyword_p=*/true,
18098 /*check_dependency_p=*/false,
18099 /*type_p=*/false,
18100 /*is_declaration=*/false);
18101
18102 if (nested_name_specifier)
18103 {
18104 tree name;
18105
18106 identifier = cp_parser_identifier (parser);
18107 name = cp_parser_lookup_name (parser, identifier,
18108 enum_type,
18109 /*is_template=*/false,
18110 /*is_namespace=*/false,
18111 /*check_dependency=*/true,
18112 /*ambiguous_decls=*/NULL,
18113 input_location);
18114 if (name && name != error_mark_node)
18115 {
18116 type = TREE_TYPE (name);
18117 if (TREE_CODE (type) == TYPENAME_TYPE)
18118 {
18119 /* Are template enums allowed in ISO? */
18120 if (template_parm_scope_p ())
18121 pedwarn (type_start_token->location, OPT_Wpedantic,
18122 "%qD is an enumeration template", name);
18123 /* ignore a typename reference, for it will be solved by name
18124 in start_enum. */
18125 type = NULL_TREE;
18126 }
18127 }
18128 else if (nested_name_specifier == error_mark_node)
18129 /* We already issued an error. */;
18130 else
18131 {
18132 error_at (type_start_token->location,
18133 "%qD does not name an enumeration in %qT",
18134 identifier, nested_name_specifier);
18135 nested_name_specifier = error_mark_node;
18136 }
18137 }
18138 else
18139 {
18140 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18141 identifier = cp_parser_identifier (parser);
18142 else
18143 {
18144 identifier = make_anon_name ();
18145 is_unnamed = true;
18146 if (scoped_enum_p)
18147 error_at (type_start_token->location,
18148 "unnamed scoped enum is not allowed");
18149 }
18150 }
18151 pop_deferring_access_checks ();
18152
18153 /* Check for the `:' that denotes a specified underlying type in C++0x.
18154 Note that a ':' could also indicate a bitfield width, however. */
18155 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18156 {
18157 cp_decl_specifier_seq type_specifiers;
18158
18159 /* Consume the `:'. */
18160 cp_lexer_consume_token (parser->lexer);
18161
18162 /* Parse the type-specifier-seq. */
18163 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18164 /*is_trailing_return=*/false,
18165 &type_specifiers);
18166
18167 /* At this point this is surely not elaborated type specifier. */
18168 if (!cp_parser_parse_definitely (parser))
18169 return NULL_TREE;
18170
18171 if (cxx_dialect < cxx11)
18172 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18173
18174 has_underlying_type = true;
18175
18176 /* If that didn't work, stop. */
18177 if (type_specifiers.type != error_mark_node)
18178 {
18179 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18180 /*initialized=*/0, NULL);
18181 if (underlying_type == error_mark_node
18182 || check_for_bare_parameter_packs (underlying_type))
18183 underlying_type = NULL_TREE;
18184 }
18185 }
18186
18187 /* Look for the `{' but don't consume it yet. */
18188 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18189 {
18190 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18191 {
18192 cp_parser_error (parser, "expected %<{%>");
18193 if (has_underlying_type)
18194 {
18195 type = NULL_TREE;
18196 goto out;
18197 }
18198 }
18199 /* An opaque-enum-specifier must have a ';' here. */
18200 if ((scoped_enum_p || underlying_type)
18201 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18202 {
18203 cp_parser_error (parser, "expected %<;%> or %<{%>");
18204 if (has_underlying_type)
18205 {
18206 type = NULL_TREE;
18207 goto out;
18208 }
18209 }
18210 }
18211
18212 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18213 return NULL_TREE;
18214
18215 if (nested_name_specifier)
18216 {
18217 if (CLASS_TYPE_P (nested_name_specifier))
18218 {
18219 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18220 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18221 push_scope (nested_name_specifier);
18222 }
18223 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18224 {
18225 push_nested_namespace (nested_name_specifier);
18226 }
18227 }
18228
18229 /* Issue an error message if type-definitions are forbidden here. */
18230 if (!cp_parser_check_type_definition (parser))
18231 type = error_mark_node;
18232 else
18233 /* Create the new type. We do this before consuming the opening
18234 brace so the enum will be recorded as being on the line of its
18235 tag (or the 'enum' keyword, if there is no tag). */
18236 type = start_enum (identifier, type, underlying_type,
18237 attributes, scoped_enum_p, &is_new_type);
18238
18239 /* If the next token is not '{' it is an opaque-enum-specifier or an
18240 elaborated-type-specifier. */
18241 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18242 {
18243 timevar_push (TV_PARSE_ENUM);
18244 if (nested_name_specifier
18245 && nested_name_specifier != error_mark_node)
18246 {
18247 /* The following catches invalid code such as:
18248 enum class S<int>::E { A, B, C }; */
18249 if (!processing_specialization
18250 && CLASS_TYPE_P (nested_name_specifier)
18251 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18252 error_at (type_start_token->location, "cannot add an enumerator "
18253 "list to a template instantiation");
18254
18255 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18256 {
18257 error_at (type_start_token->location,
18258 "%<%T::%E%> has not been declared",
18259 TYPE_CONTEXT (nested_name_specifier),
18260 nested_name_specifier);
18261 type = error_mark_node;
18262 }
18263 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18264 && !CLASS_TYPE_P (nested_name_specifier))
18265 {
18266 error_at (type_start_token->location, "nested name specifier "
18267 "%qT for enum declaration does not name a class "
18268 "or namespace", nested_name_specifier);
18269 type = error_mark_node;
18270 }
18271 /* If that scope does not contain the scope in which the
18272 class was originally declared, the program is invalid. */
18273 else if (prev_scope && !is_ancestor (prev_scope,
18274 nested_name_specifier))
18275 {
18276 if (at_namespace_scope_p ())
18277 error_at (type_start_token->location,
18278 "declaration of %qD in namespace %qD which does not "
18279 "enclose %qD",
18280 type, prev_scope, nested_name_specifier);
18281 else
18282 error_at (type_start_token->location,
18283 "declaration of %qD in %qD which does not "
18284 "enclose %qD",
18285 type, prev_scope, nested_name_specifier);
18286 type = error_mark_node;
18287 }
18288 /* If that scope is the scope where the declaration is being placed
18289 the program is invalid. */
18290 else if (CLASS_TYPE_P (nested_name_specifier)
18291 && CLASS_TYPE_P (prev_scope)
18292 && same_type_p (nested_name_specifier, prev_scope))
18293 {
18294 permerror (type_start_token->location,
18295 "extra qualification not allowed");
18296 nested_name_specifier = NULL_TREE;
18297 }
18298 }
18299
18300 if (scoped_enum_p)
18301 begin_scope (sk_scoped_enum, type);
18302
18303 /* Consume the opening brace. */
18304 matching_braces braces;
18305 braces.consume_open (parser);
18306
18307 if (type == error_mark_node)
18308 ; /* Nothing to add */
18309 else if (OPAQUE_ENUM_P (type)
18310 || (cxx_dialect > cxx98 && processing_specialization))
18311 {
18312 new_value_list = true;
18313 SET_OPAQUE_ENUM_P (type, false);
18314 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18315 }
18316 else
18317 {
18318 error_at (type_start_token->location,
18319 "multiple definition of %q#T", type);
18320 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18321 "previous definition here");
18322 type = error_mark_node;
18323 }
18324
18325 if (type == error_mark_node)
18326 cp_parser_skip_to_end_of_block_or_statement (parser);
18327 /* If the next token is not '}', then there are some enumerators. */
18328 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18329 {
18330 if (is_unnamed && !scoped_enum_p)
18331 pedwarn (type_start_token->location, OPT_Wpedantic,
18332 "ISO C++ forbids empty unnamed enum");
18333 }
18334 else
18335 cp_parser_enumerator_list (parser, type);
18336
18337 /* Consume the final '}'. */
18338 braces.require_close (parser);
18339
18340 if (scoped_enum_p)
18341 finish_scope ();
18342 timevar_pop (TV_PARSE_ENUM);
18343 }
18344 else
18345 {
18346 /* If a ';' follows, then it is an opaque-enum-specifier
18347 and additional restrictions apply. */
18348 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18349 {
18350 if (is_unnamed)
18351 error_at (type_start_token->location,
18352 "opaque-enum-specifier without name");
18353 else if (nested_name_specifier)
18354 error_at (type_start_token->location,
18355 "opaque-enum-specifier must use a simple identifier");
18356 }
18357 }
18358
18359 /* Look for trailing attributes to apply to this enumeration, and
18360 apply them if appropriate. */
18361 if (cp_parser_allow_gnu_extensions_p (parser))
18362 {
18363 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18364 cplus_decl_attributes (&type,
18365 trailing_attr,
18366 (int) ATTR_FLAG_TYPE_IN_PLACE);
18367 }
18368
18369 /* Finish up the enumeration. */
18370 if (type != error_mark_node)
18371 {
18372 if (new_value_list)
18373 finish_enum_value_list (type);
18374 if (is_new_type)
18375 finish_enum (type);
18376 }
18377
18378 if (nested_name_specifier)
18379 {
18380 if (CLASS_TYPE_P (nested_name_specifier))
18381 {
18382 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18383 pop_scope (nested_name_specifier);
18384 }
18385 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18386 {
18387 pop_nested_namespace (nested_name_specifier);
18388 }
18389 }
18390 out:
18391 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18392 return type;
18393 }
18394
18395 /* Parse an enumerator-list. The enumerators all have the indicated
18396 TYPE.
18397
18398 enumerator-list:
18399 enumerator-definition
18400 enumerator-list , enumerator-definition */
18401
18402 static void
18403 cp_parser_enumerator_list (cp_parser* parser, tree type)
18404 {
18405 while (true)
18406 {
18407 /* Parse an enumerator-definition. */
18408 cp_parser_enumerator_definition (parser, type);
18409
18410 /* If the next token is not a ',', we've reached the end of
18411 the list. */
18412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18413 break;
18414 /* Otherwise, consume the `,' and keep going. */
18415 cp_lexer_consume_token (parser->lexer);
18416 /* If the next token is a `}', there is a trailing comma. */
18417 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18418 {
18419 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18420 pedwarn (input_location, OPT_Wpedantic,
18421 "comma at end of enumerator list");
18422 break;
18423 }
18424 }
18425 }
18426
18427 /* Parse an enumerator-definition. The enumerator has the indicated
18428 TYPE.
18429
18430 enumerator-definition:
18431 enumerator
18432 enumerator = constant-expression
18433
18434 enumerator:
18435 identifier
18436
18437 GNU Extensions:
18438
18439 enumerator-definition:
18440 enumerator attributes [opt]
18441 enumerator attributes [opt] = constant-expression */
18442
18443 static void
18444 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18445 {
18446 tree identifier;
18447 tree value;
18448 location_t loc;
18449
18450 /* Save the input location because we are interested in the location
18451 of the identifier and not the location of the explicit value. */
18452 loc = cp_lexer_peek_token (parser->lexer)->location;
18453
18454 /* Look for the identifier. */
18455 identifier = cp_parser_identifier (parser);
18456 if (identifier == error_mark_node)
18457 return;
18458
18459 /* Parse any specified attributes. */
18460 tree attrs = cp_parser_attributes_opt (parser);
18461
18462 /* If the next token is an '=', then there is an explicit value. */
18463 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18464 {
18465 /* Consume the `=' token. */
18466 cp_lexer_consume_token (parser->lexer);
18467 /* Parse the value. */
18468 value = cp_parser_constant_expression (parser);
18469 }
18470 else
18471 value = NULL_TREE;
18472
18473 /* If we are processing a template, make sure the initializer of the
18474 enumerator doesn't contain any bare template parameter pack. */
18475 if (check_for_bare_parameter_packs (value))
18476 value = error_mark_node;
18477
18478 /* Create the enumerator. */
18479 build_enumerator (identifier, value, type, attrs, loc);
18480 }
18481
18482 /* Parse a namespace-name.
18483
18484 namespace-name:
18485 original-namespace-name
18486 namespace-alias
18487
18488 Returns the NAMESPACE_DECL for the namespace. */
18489
18490 static tree
18491 cp_parser_namespace_name (cp_parser* parser)
18492 {
18493 tree identifier;
18494 tree namespace_decl;
18495
18496 cp_token *token = cp_lexer_peek_token (parser->lexer);
18497
18498 /* Get the name of the namespace. */
18499 identifier = cp_parser_identifier (parser);
18500 if (identifier == error_mark_node)
18501 return error_mark_node;
18502
18503 /* Look up the identifier in the currently active scope. Look only
18504 for namespaces, due to:
18505
18506 [basic.lookup.udir]
18507
18508 When looking up a namespace-name in a using-directive or alias
18509 definition, only namespace names are considered.
18510
18511 And:
18512
18513 [basic.lookup.qual]
18514
18515 During the lookup of a name preceding the :: scope resolution
18516 operator, object, function, and enumerator names are ignored.
18517
18518 (Note that cp_parser_qualifying_entity only calls this
18519 function if the token after the name is the scope resolution
18520 operator.) */
18521 namespace_decl = cp_parser_lookup_name (parser, identifier,
18522 none_type,
18523 /*is_template=*/false,
18524 /*is_namespace=*/true,
18525 /*check_dependency=*/true,
18526 /*ambiguous_decls=*/NULL,
18527 token->location);
18528 /* If it's not a namespace, issue an error. */
18529 if (namespace_decl == error_mark_node
18530 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18531 {
18532 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18533 error_at (token->location, "%qD is not a namespace-name", identifier);
18534 cp_parser_error (parser, "expected namespace-name");
18535 namespace_decl = error_mark_node;
18536 }
18537
18538 return namespace_decl;
18539 }
18540
18541 /* Parse a namespace-definition.
18542
18543 namespace-definition:
18544 named-namespace-definition
18545 unnamed-namespace-definition
18546
18547 named-namespace-definition:
18548 original-namespace-definition
18549 extension-namespace-definition
18550
18551 original-namespace-definition:
18552 namespace identifier { namespace-body }
18553
18554 extension-namespace-definition:
18555 namespace original-namespace-name { namespace-body }
18556
18557 unnamed-namespace-definition:
18558 namespace { namespace-body } */
18559
18560 static void
18561 cp_parser_namespace_definition (cp_parser* parser)
18562 {
18563 tree identifier;
18564 int nested_definition_count = 0;
18565
18566 cp_ensure_no_omp_declare_simd (parser);
18567 cp_ensure_no_oacc_routine (parser);
18568
18569 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18570
18571 if (is_inline)
18572 {
18573 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18574 cp_lexer_consume_token (parser->lexer);
18575 }
18576
18577 /* Look for the `namespace' keyword. */
18578 cp_token* token
18579 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18580
18581 /* Parse any specified attributes before the identifier. */
18582 tree attribs = cp_parser_attributes_opt (parser);
18583
18584 for (;;)
18585 {
18586 identifier = NULL_TREE;
18587
18588 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18589 {
18590 identifier = cp_parser_identifier (parser);
18591
18592 /* Parse any attributes specified after the identifier. */
18593 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18594 }
18595
18596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18597 break;
18598
18599 if (!nested_definition_count && cxx_dialect < cxx17)
18600 pedwarn (input_location, OPT_Wpedantic,
18601 "nested namespace definitions only available with "
18602 "-std=c++17 or -std=gnu++17");
18603
18604 /* Nested namespace names can create new namespaces (unlike
18605 other qualified-ids). */
18606 if (int count = identifier ? push_namespace (identifier) : 0)
18607 nested_definition_count += count;
18608 else
18609 cp_parser_error (parser, "nested namespace name required");
18610 cp_lexer_consume_token (parser->lexer);
18611 }
18612
18613 if (nested_definition_count && !identifier)
18614 cp_parser_error (parser, "namespace name required");
18615
18616 if (nested_definition_count && attribs)
18617 error_at (token->location,
18618 "a nested namespace definition cannot have attributes");
18619 if (nested_definition_count && is_inline)
18620 error_at (token->location,
18621 "a nested namespace definition cannot be inline");
18622
18623 /* Start the namespace. */
18624 nested_definition_count += push_namespace (identifier, is_inline);
18625
18626 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18627
18628 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18629
18630 /* Look for the `{' to validate starting the namespace. */
18631 matching_braces braces;
18632 if (braces.require_open (parser))
18633 {
18634 /* Parse the body of the namespace. */
18635 cp_parser_namespace_body (parser);
18636
18637 /* Look for the final `}'. */
18638 braces.require_close (parser);
18639 }
18640
18641 if (has_visibility)
18642 pop_visibility (1);
18643
18644 /* Pop the nested namespace definitions. */
18645 while (nested_definition_count--)
18646 pop_namespace ();
18647 }
18648
18649 /* Parse a namespace-body.
18650
18651 namespace-body:
18652 declaration-seq [opt] */
18653
18654 static void
18655 cp_parser_namespace_body (cp_parser* parser)
18656 {
18657 cp_parser_declaration_seq_opt (parser);
18658 }
18659
18660 /* Parse a namespace-alias-definition.
18661
18662 namespace-alias-definition:
18663 namespace identifier = qualified-namespace-specifier ; */
18664
18665 static void
18666 cp_parser_namespace_alias_definition (cp_parser* parser)
18667 {
18668 tree identifier;
18669 tree namespace_specifier;
18670
18671 cp_token *token = cp_lexer_peek_token (parser->lexer);
18672
18673 /* Look for the `namespace' keyword. */
18674 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18675 /* Look for the identifier. */
18676 identifier = cp_parser_identifier (parser);
18677 if (identifier == error_mark_node)
18678 return;
18679 /* Look for the `=' token. */
18680 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18681 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18682 {
18683 error_at (token->location, "%<namespace%> definition is not allowed here");
18684 /* Skip the definition. */
18685 cp_lexer_consume_token (parser->lexer);
18686 if (cp_parser_skip_to_closing_brace (parser))
18687 cp_lexer_consume_token (parser->lexer);
18688 return;
18689 }
18690 cp_parser_require (parser, CPP_EQ, RT_EQ);
18691 /* Look for the qualified-namespace-specifier. */
18692 namespace_specifier
18693 = cp_parser_qualified_namespace_specifier (parser);
18694 /* Look for the `;' token. */
18695 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18696
18697 /* Register the alias in the symbol table. */
18698 do_namespace_alias (identifier, namespace_specifier);
18699 }
18700
18701 /* Parse a qualified-namespace-specifier.
18702
18703 qualified-namespace-specifier:
18704 :: [opt] nested-name-specifier [opt] namespace-name
18705
18706 Returns a NAMESPACE_DECL corresponding to the specified
18707 namespace. */
18708
18709 static tree
18710 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18711 {
18712 /* Look for the optional `::'. */
18713 cp_parser_global_scope_opt (parser,
18714 /*current_scope_valid_p=*/false);
18715
18716 /* Look for the optional nested-name-specifier. */
18717 cp_parser_nested_name_specifier_opt (parser,
18718 /*typename_keyword_p=*/false,
18719 /*check_dependency_p=*/true,
18720 /*type_p=*/false,
18721 /*is_declaration=*/true);
18722
18723 return cp_parser_namespace_name (parser);
18724 }
18725
18726 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18727 access declaration.
18728
18729 using-declaration:
18730 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18731 using :: unqualified-id ;
18732
18733 access-declaration:
18734 qualified-id ;
18735
18736 */
18737
18738 static bool
18739 cp_parser_using_declaration (cp_parser* parser,
18740 bool access_declaration_p)
18741 {
18742 cp_token *token;
18743 bool typename_p = false;
18744 bool global_scope_p;
18745 tree decl;
18746 tree identifier;
18747 tree qscope;
18748 int oldcount = errorcount;
18749 cp_token *diag_token = NULL;
18750
18751 if (access_declaration_p)
18752 {
18753 diag_token = cp_lexer_peek_token (parser->lexer);
18754 cp_parser_parse_tentatively (parser);
18755 }
18756 else
18757 {
18758 /* Look for the `using' keyword. */
18759 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18760
18761 again:
18762 /* Peek at the next token. */
18763 token = cp_lexer_peek_token (parser->lexer);
18764 /* See if it's `typename'. */
18765 if (token->keyword == RID_TYPENAME)
18766 {
18767 /* Remember that we've seen it. */
18768 typename_p = true;
18769 /* Consume the `typename' token. */
18770 cp_lexer_consume_token (parser->lexer);
18771 }
18772 }
18773
18774 /* Look for the optional global scope qualification. */
18775 global_scope_p
18776 = (cp_parser_global_scope_opt (parser,
18777 /*current_scope_valid_p=*/false)
18778 != NULL_TREE);
18779
18780 /* If we saw `typename', or didn't see `::', then there must be a
18781 nested-name-specifier present. */
18782 if (typename_p || !global_scope_p)
18783 {
18784 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18785 /*check_dependency_p=*/true,
18786 /*type_p=*/false,
18787 /*is_declaration=*/true);
18788 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18789 {
18790 cp_parser_skip_to_end_of_block_or_statement (parser);
18791 return false;
18792 }
18793 }
18794 /* Otherwise, we could be in either of the two productions. In that
18795 case, treat the nested-name-specifier as optional. */
18796 else
18797 qscope = cp_parser_nested_name_specifier_opt (parser,
18798 /*typename_keyword_p=*/false,
18799 /*check_dependency_p=*/true,
18800 /*type_p=*/false,
18801 /*is_declaration=*/true);
18802 if (!qscope)
18803 qscope = global_namespace;
18804 else if (UNSCOPED_ENUM_P (qscope))
18805 qscope = CP_TYPE_CONTEXT (qscope);
18806
18807 if (access_declaration_p && cp_parser_error_occurred (parser))
18808 /* Something has already gone wrong; there's no need to parse
18809 further. Since an error has occurred, the return value of
18810 cp_parser_parse_definitely will be false, as required. */
18811 return cp_parser_parse_definitely (parser);
18812
18813 token = cp_lexer_peek_token (parser->lexer);
18814 /* Parse the unqualified-id. */
18815 identifier = cp_parser_unqualified_id (parser,
18816 /*template_keyword_p=*/false,
18817 /*check_dependency_p=*/true,
18818 /*declarator_p=*/true,
18819 /*optional_p=*/false);
18820
18821 if (access_declaration_p)
18822 {
18823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18824 cp_parser_simulate_error (parser);
18825 if (!cp_parser_parse_definitely (parser))
18826 return false;
18827 }
18828 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18829 {
18830 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18831 if (cxx_dialect < cxx17
18832 && !in_system_header_at (ell->location))
18833 pedwarn (ell->location, 0,
18834 "pack expansion in using-declaration only available "
18835 "with -std=c++17 or -std=gnu++17");
18836 qscope = make_pack_expansion (qscope);
18837 }
18838
18839 /* The function we call to handle a using-declaration is different
18840 depending on what scope we are in. */
18841 if (qscope == error_mark_node || identifier == error_mark_node)
18842 ;
18843 else if (!identifier_p (identifier)
18844 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18845 /* [namespace.udecl]
18846
18847 A using declaration shall not name a template-id. */
18848 error_at (token->location,
18849 "a template-id may not appear in a using-declaration");
18850 else
18851 {
18852 if (at_class_scope_p ())
18853 {
18854 /* Create the USING_DECL. */
18855 decl = do_class_using_decl (qscope, identifier);
18856
18857 if (decl && typename_p)
18858 USING_DECL_TYPENAME_P (decl) = 1;
18859
18860 if (check_for_bare_parameter_packs (decl))
18861 {
18862 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18863 return false;
18864 }
18865 else
18866 /* Add it to the list of members in this class. */
18867 finish_member_declaration (decl);
18868 }
18869 else
18870 {
18871 decl = cp_parser_lookup_name_simple (parser,
18872 identifier,
18873 token->location);
18874 if (decl == error_mark_node)
18875 cp_parser_name_lookup_error (parser, identifier,
18876 decl, NLE_NULL,
18877 token->location);
18878 else if (check_for_bare_parameter_packs (decl))
18879 {
18880 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18881 return false;
18882 }
18883 else if (!at_namespace_scope_p ())
18884 finish_local_using_decl (decl, qscope, identifier);
18885 else
18886 finish_namespace_using_decl (decl, qscope, identifier);
18887 }
18888 }
18889
18890 if (!access_declaration_p
18891 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18892 {
18893 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18894 if (cxx_dialect < cxx17)
18895 pedwarn (comma->location, 0,
18896 "comma-separated list in using-declaration only available "
18897 "with -std=c++17 or -std=gnu++17");
18898 goto again;
18899 }
18900
18901 /* Look for the final `;'. */
18902 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18903
18904 if (access_declaration_p && errorcount == oldcount)
18905 warning_at (diag_token->location, OPT_Wdeprecated,
18906 "access declarations are deprecated "
18907 "in favour of using-declarations; "
18908 "suggestion: add the %<using%> keyword");
18909
18910 return true;
18911 }
18912
18913 /* Parse an alias-declaration.
18914
18915 alias-declaration:
18916 using identifier attribute-specifier-seq [opt] = type-id */
18917
18918 static tree
18919 cp_parser_alias_declaration (cp_parser* parser)
18920 {
18921 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18922 location_t id_location;
18923 cp_declarator *declarator;
18924 cp_decl_specifier_seq decl_specs;
18925 bool member_p;
18926 const char *saved_message = NULL;
18927
18928 /* Look for the `using' keyword. */
18929 cp_token *using_token
18930 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18931 if (using_token == NULL)
18932 return error_mark_node;
18933
18934 id_location = cp_lexer_peek_token (parser->lexer)->location;
18935 id = cp_parser_identifier (parser);
18936 if (id == error_mark_node)
18937 return error_mark_node;
18938
18939 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18940 attributes = cp_parser_attributes_opt (parser);
18941 if (attributes == error_mark_node)
18942 return error_mark_node;
18943
18944 cp_parser_require (parser, CPP_EQ, RT_EQ);
18945
18946 if (cp_parser_error_occurred (parser))
18947 return error_mark_node;
18948
18949 cp_parser_commit_to_tentative_parse (parser);
18950
18951 /* Now we are going to parse the type-id of the declaration. */
18952
18953 /*
18954 [dcl.type]/3 says:
18955
18956 "A type-specifier-seq shall not define a class or enumeration
18957 unless it appears in the type-id of an alias-declaration (7.1.3) that
18958 is not the declaration of a template-declaration."
18959
18960 In other words, if we currently are in an alias template, the
18961 type-id should not define a type.
18962
18963 So let's set parser->type_definition_forbidden_message in that
18964 case; cp_parser_check_type_definition (called by
18965 cp_parser_class_specifier) will then emit an error if a type is
18966 defined in the type-id. */
18967 if (parser->num_template_parameter_lists)
18968 {
18969 saved_message = parser->type_definition_forbidden_message;
18970 parser->type_definition_forbidden_message =
18971 G_("types may not be defined in alias template declarations");
18972 }
18973
18974 type = cp_parser_type_id (parser);
18975
18976 /* Restore the error message if need be. */
18977 if (parser->num_template_parameter_lists)
18978 parser->type_definition_forbidden_message = saved_message;
18979
18980 if (type == error_mark_node
18981 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18982 {
18983 cp_parser_skip_to_end_of_block_or_statement (parser);
18984 return error_mark_node;
18985 }
18986
18987 /* A typedef-name can also be introduced by an alias-declaration. The
18988 identifier following the using keyword becomes a typedef-name. It has
18989 the same semantics as if it were introduced by the typedef
18990 specifier. In particular, it does not define a new type and it shall
18991 not appear in the type-id. */
18992
18993 clear_decl_specs (&decl_specs);
18994 decl_specs.type = type;
18995 if (attributes != NULL_TREE)
18996 {
18997 decl_specs.attributes = attributes;
18998 set_and_check_decl_spec_loc (&decl_specs,
18999 ds_attribute,
19000 attrs_token);
19001 }
19002 set_and_check_decl_spec_loc (&decl_specs,
19003 ds_typedef,
19004 using_token);
19005 set_and_check_decl_spec_loc (&decl_specs,
19006 ds_alias,
19007 using_token);
19008
19009 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19010 declarator->id_loc = id_location;
19011
19012 member_p = at_class_scope_p ();
19013 if (member_p)
19014 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19015 NULL_TREE, attributes);
19016 else
19017 decl = start_decl (declarator, &decl_specs, 0,
19018 attributes, NULL_TREE, &pushed_scope);
19019 if (decl == error_mark_node)
19020 return decl;
19021
19022 // Attach constraints to the alias declaration.
19023 if (flag_concepts && current_template_parms)
19024 {
19025 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19026 tree constr = build_constraints (reqs, NULL_TREE);
19027 set_constraints (decl, constr);
19028 }
19029
19030 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19031
19032 if (pushed_scope)
19033 pop_scope (pushed_scope);
19034
19035 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19036 added into the symbol table; otherwise, return the TYPE_DECL. */
19037 if (DECL_LANG_SPECIFIC (decl)
19038 && DECL_TEMPLATE_INFO (decl)
19039 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19040 {
19041 decl = DECL_TI_TEMPLATE (decl);
19042 if (member_p)
19043 check_member_template (decl);
19044 }
19045
19046 return decl;
19047 }
19048
19049 /* Parse a using-directive.
19050
19051 using-directive:
19052 using namespace :: [opt] nested-name-specifier [opt]
19053 namespace-name ; */
19054
19055 static void
19056 cp_parser_using_directive (cp_parser* parser)
19057 {
19058 tree namespace_decl;
19059 tree attribs;
19060
19061 /* Look for the `using' keyword. */
19062 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19063 /* And the `namespace' keyword. */
19064 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19065 /* Look for the optional `::' operator. */
19066 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19067 /* And the optional nested-name-specifier. */
19068 cp_parser_nested_name_specifier_opt (parser,
19069 /*typename_keyword_p=*/false,
19070 /*check_dependency_p=*/true,
19071 /*type_p=*/false,
19072 /*is_declaration=*/true);
19073 /* Get the namespace being used. */
19074 namespace_decl = cp_parser_namespace_name (parser);
19075 /* And any specified attributes. */
19076 attribs = cp_parser_attributes_opt (parser);
19077
19078 /* Update the symbol table. */
19079 if (namespace_bindings_p ())
19080 finish_namespace_using_directive (namespace_decl, attribs);
19081 else
19082 finish_local_using_directive (namespace_decl, attribs);
19083
19084 /* Look for the final `;'. */
19085 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19086 }
19087
19088 /* Parse an asm-definition.
19089
19090 asm-definition:
19091 asm ( string-literal ) ;
19092
19093 GNU Extension:
19094
19095 asm-definition:
19096 asm volatile [opt] ( string-literal ) ;
19097 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19098 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19099 : asm-operand-list [opt] ) ;
19100 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19101 : asm-operand-list [opt]
19102 : asm-clobber-list [opt] ) ;
19103 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19104 : asm-clobber-list [opt]
19105 : asm-goto-list ) ; */
19106
19107 static void
19108 cp_parser_asm_definition (cp_parser* parser)
19109 {
19110 tree string;
19111 tree outputs = NULL_TREE;
19112 tree inputs = NULL_TREE;
19113 tree clobbers = NULL_TREE;
19114 tree labels = NULL_TREE;
19115 tree asm_stmt;
19116 bool volatile_p = false;
19117 bool extended_p = false;
19118 bool invalid_inputs_p = false;
19119 bool invalid_outputs_p = false;
19120 bool goto_p = false;
19121 required_token missing = RT_NONE;
19122
19123 /* Look for the `asm' keyword. */
19124 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19125
19126 if (parser->in_function_body
19127 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19128 {
19129 error ("%<asm%> in %<constexpr%> function");
19130 cp_function_chain->invalid_constexpr = true;
19131 }
19132
19133 /* See if the next token is `volatile'. */
19134 if (cp_parser_allow_gnu_extensions_p (parser)
19135 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19136 {
19137 /* Remember that we saw the `volatile' keyword. */
19138 volatile_p = true;
19139 /* Consume the token. */
19140 cp_lexer_consume_token (parser->lexer);
19141 }
19142 if (cp_parser_allow_gnu_extensions_p (parser)
19143 && parser->in_function_body
19144 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19145 {
19146 /* Remember that we saw the `goto' keyword. */
19147 goto_p = true;
19148 /* Consume the token. */
19149 cp_lexer_consume_token (parser->lexer);
19150 }
19151 /* Look for the opening `('. */
19152 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19153 return;
19154 /* Look for the string. */
19155 string = cp_parser_string_literal (parser, false, false);
19156 if (string == error_mark_node)
19157 {
19158 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19159 /*consume_paren=*/true);
19160 return;
19161 }
19162
19163 /* If we're allowing GNU extensions, check for the extended assembly
19164 syntax. Unfortunately, the `:' tokens need not be separated by
19165 a space in C, and so, for compatibility, we tolerate that here
19166 too. Doing that means that we have to treat the `::' operator as
19167 two `:' tokens. */
19168 if (cp_parser_allow_gnu_extensions_p (parser)
19169 && parser->in_function_body
19170 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19171 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19172 {
19173 bool inputs_p = false;
19174 bool clobbers_p = false;
19175 bool labels_p = false;
19176
19177 /* The extended syntax was used. */
19178 extended_p = true;
19179
19180 /* Look for outputs. */
19181 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19182 {
19183 /* Consume the `:'. */
19184 cp_lexer_consume_token (parser->lexer);
19185 /* Parse the output-operands. */
19186 if (cp_lexer_next_token_is_not (parser->lexer,
19187 CPP_COLON)
19188 && cp_lexer_next_token_is_not (parser->lexer,
19189 CPP_SCOPE)
19190 && cp_lexer_next_token_is_not (parser->lexer,
19191 CPP_CLOSE_PAREN)
19192 && !goto_p)
19193 {
19194 outputs = cp_parser_asm_operand_list (parser);
19195 if (outputs == error_mark_node)
19196 invalid_outputs_p = true;
19197 }
19198 }
19199 /* If the next token is `::', there are no outputs, and the
19200 next token is the beginning of the inputs. */
19201 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19202 /* The inputs are coming next. */
19203 inputs_p = true;
19204
19205 /* Look for inputs. */
19206 if (inputs_p
19207 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19208 {
19209 /* Consume the `:' or `::'. */
19210 cp_lexer_consume_token (parser->lexer);
19211 /* Parse the output-operands. */
19212 if (cp_lexer_next_token_is_not (parser->lexer,
19213 CPP_COLON)
19214 && cp_lexer_next_token_is_not (parser->lexer,
19215 CPP_SCOPE)
19216 && cp_lexer_next_token_is_not (parser->lexer,
19217 CPP_CLOSE_PAREN))
19218 {
19219 inputs = cp_parser_asm_operand_list (parser);
19220 if (inputs == error_mark_node)
19221 invalid_inputs_p = true;
19222 }
19223 }
19224 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19225 /* The clobbers are coming next. */
19226 clobbers_p = true;
19227
19228 /* Look for clobbers. */
19229 if (clobbers_p
19230 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19231 {
19232 clobbers_p = true;
19233 /* Consume the `:' or `::'. */
19234 cp_lexer_consume_token (parser->lexer);
19235 /* Parse the clobbers. */
19236 if (cp_lexer_next_token_is_not (parser->lexer,
19237 CPP_COLON)
19238 && cp_lexer_next_token_is_not (parser->lexer,
19239 CPP_CLOSE_PAREN))
19240 clobbers = cp_parser_asm_clobber_list (parser);
19241 }
19242 else if (goto_p
19243 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19244 /* The labels are coming next. */
19245 labels_p = true;
19246
19247 /* Look for labels. */
19248 if (labels_p
19249 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19250 {
19251 labels_p = true;
19252 /* Consume the `:' or `::'. */
19253 cp_lexer_consume_token (parser->lexer);
19254 /* Parse the labels. */
19255 labels = cp_parser_asm_label_list (parser);
19256 }
19257
19258 if (goto_p && !labels_p)
19259 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19260 }
19261 else if (goto_p)
19262 missing = RT_COLON_SCOPE;
19263
19264 /* Look for the closing `)'. */
19265 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19266 missing ? missing : RT_CLOSE_PAREN))
19267 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19268 /*consume_paren=*/true);
19269 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19270
19271 if (!invalid_inputs_p && !invalid_outputs_p)
19272 {
19273 /* Create the ASM_EXPR. */
19274 if (parser->in_function_body)
19275 {
19276 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19277 inputs, clobbers, labels);
19278 /* If the extended syntax was not used, mark the ASM_EXPR. */
19279 if (!extended_p)
19280 {
19281 tree temp = asm_stmt;
19282 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19283 temp = TREE_OPERAND (temp, 0);
19284
19285 ASM_INPUT_P (temp) = 1;
19286 }
19287 }
19288 else
19289 symtab->finalize_toplevel_asm (string);
19290 }
19291 }
19292
19293 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19294 type that comes from the decl-specifier-seq. */
19295
19296 static tree
19297 strip_declarator_types (tree type, cp_declarator *declarator)
19298 {
19299 for (cp_declarator *d = declarator; d;)
19300 switch (d->kind)
19301 {
19302 case cdk_id:
19303 case cdk_decomp:
19304 case cdk_error:
19305 d = NULL;
19306 break;
19307
19308 default:
19309 if (TYPE_PTRMEMFUNC_P (type))
19310 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19311 type = TREE_TYPE (type);
19312 d = d->declarator;
19313 break;
19314 }
19315
19316 return type;
19317 }
19318
19319 /* Declarators [gram.dcl.decl] */
19320
19321 /* Parse an init-declarator.
19322
19323 init-declarator:
19324 declarator initializer [opt]
19325
19326 GNU Extension:
19327
19328 init-declarator:
19329 declarator asm-specification [opt] attributes [opt] initializer [opt]
19330
19331 function-definition:
19332 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19333 function-body
19334 decl-specifier-seq [opt] declarator function-try-block
19335
19336 GNU Extension:
19337
19338 function-definition:
19339 __extension__ function-definition
19340
19341 TM Extension:
19342
19343 function-definition:
19344 decl-specifier-seq [opt] declarator function-transaction-block
19345
19346 The DECL_SPECIFIERS apply to this declarator. Returns a
19347 representation of the entity declared. If MEMBER_P is TRUE, then
19348 this declarator appears in a class scope. The new DECL created by
19349 this declarator is returned.
19350
19351 The CHECKS are access checks that should be performed once we know
19352 what entity is being declared (and, therefore, what classes have
19353 befriended it).
19354
19355 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19356 for a function-definition here as well. If the declarator is a
19357 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19358 be TRUE upon return. By that point, the function-definition will
19359 have been completely parsed.
19360
19361 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19362 is FALSE.
19363
19364 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19365 parsed declaration if it is an uninitialized single declarator not followed
19366 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19367 if present, will not be consumed. If returned, this declarator will be
19368 created with SD_INITIALIZED but will not call cp_finish_decl.
19369
19370 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19371 and there is an initializer, the pointed location_t is set to the
19372 location of the '=' or `(', or '{' in C++11 token introducing the
19373 initializer. */
19374
19375 static tree
19376 cp_parser_init_declarator (cp_parser* parser,
19377 cp_decl_specifier_seq *decl_specifiers,
19378 vec<deferred_access_check, va_gc> *checks,
19379 bool function_definition_allowed_p,
19380 bool member_p,
19381 int declares_class_or_enum,
19382 bool* function_definition_p,
19383 tree* maybe_range_for_decl,
19384 location_t* init_loc,
19385 tree* auto_result)
19386 {
19387 cp_token *token = NULL, *asm_spec_start_token = NULL,
19388 *attributes_start_token = NULL;
19389 cp_declarator *declarator;
19390 tree prefix_attributes;
19391 tree attributes = NULL;
19392 tree asm_specification;
19393 tree initializer;
19394 tree decl = NULL_TREE;
19395 tree scope;
19396 int is_initialized;
19397 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19398 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19399 "(...)". */
19400 enum cpp_ttype initialization_kind;
19401 bool is_direct_init = false;
19402 bool is_non_constant_init;
19403 int ctor_dtor_or_conv_p;
19404 bool friend_p = cp_parser_friend_p (decl_specifiers);
19405 tree pushed_scope = NULL_TREE;
19406 bool range_for_decl_p = false;
19407 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19408 location_t tmp_init_loc = UNKNOWN_LOCATION;
19409
19410 /* Gather the attributes that were provided with the
19411 decl-specifiers. */
19412 prefix_attributes = decl_specifiers->attributes;
19413
19414 /* Assume that this is not the declarator for a function
19415 definition. */
19416 if (function_definition_p)
19417 *function_definition_p = false;
19418
19419 /* Default arguments are only permitted for function parameters. */
19420 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19421 parser->default_arg_ok_p = false;
19422
19423 /* Defer access checks while parsing the declarator; we cannot know
19424 what names are accessible until we know what is being
19425 declared. */
19426 resume_deferring_access_checks ();
19427
19428 token = cp_lexer_peek_token (parser->lexer);
19429
19430 /* Parse the declarator. */
19431 declarator
19432 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19433 &ctor_dtor_or_conv_p,
19434 /*parenthesized_p=*/NULL,
19435 member_p, friend_p);
19436 /* Gather up the deferred checks. */
19437 stop_deferring_access_checks ();
19438
19439 parser->default_arg_ok_p = saved_default_arg_ok_p;
19440
19441 /* If the DECLARATOR was erroneous, there's no need to go
19442 further. */
19443 if (declarator == cp_error_declarator)
19444 return error_mark_node;
19445
19446 /* Check that the number of template-parameter-lists is OK. */
19447 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19448 token->location))
19449 return error_mark_node;
19450
19451 if (declares_class_or_enum & 2)
19452 cp_parser_check_for_definition_in_return_type (declarator,
19453 decl_specifiers->type,
19454 decl_specifiers->locations[ds_type_spec]);
19455
19456 /* Figure out what scope the entity declared by the DECLARATOR is
19457 located in. `grokdeclarator' sometimes changes the scope, so
19458 we compute it now. */
19459 scope = get_scope_of_declarator (declarator);
19460
19461 /* Perform any lookups in the declared type which were thought to be
19462 dependent, but are not in the scope of the declarator. */
19463 decl_specifiers->type
19464 = maybe_update_decl_type (decl_specifiers->type, scope);
19465
19466 /* If we're allowing GNU extensions, look for an
19467 asm-specification. */
19468 if (cp_parser_allow_gnu_extensions_p (parser))
19469 {
19470 /* Look for an asm-specification. */
19471 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19472 asm_specification = cp_parser_asm_specification_opt (parser);
19473 }
19474 else
19475 asm_specification = NULL_TREE;
19476
19477 /* Look for attributes. */
19478 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19479 attributes = cp_parser_attributes_opt (parser);
19480
19481 /* Peek at the next token. */
19482 token = cp_lexer_peek_token (parser->lexer);
19483
19484 bool bogus_implicit_tmpl = false;
19485
19486 if (function_declarator_p (declarator))
19487 {
19488 /* Handle C++17 deduction guides. */
19489 if (!decl_specifiers->type
19490 && ctor_dtor_or_conv_p <= 0
19491 && cxx_dialect >= cxx17)
19492 {
19493 cp_declarator *id = get_id_declarator (declarator);
19494 tree name = id->u.id.unqualified_name;
19495 parser->scope = id->u.id.qualifying_scope;
19496 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19497 if (tmpl
19498 && (DECL_CLASS_TEMPLATE_P (tmpl)
19499 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19500 {
19501 id->u.id.unqualified_name = dguide_name (tmpl);
19502 id->u.id.sfk = sfk_deduction_guide;
19503 ctor_dtor_or_conv_p = 1;
19504 }
19505 }
19506
19507 /* Check to see if the token indicates the start of a
19508 function-definition. */
19509 if (cp_parser_token_starts_function_definition_p (token))
19510 {
19511 if (!function_definition_allowed_p)
19512 {
19513 /* If a function-definition should not appear here, issue an
19514 error message. */
19515 cp_parser_error (parser,
19516 "a function-definition is not allowed here");
19517 return error_mark_node;
19518 }
19519
19520 location_t func_brace_location
19521 = cp_lexer_peek_token (parser->lexer)->location;
19522
19523 /* Neither attributes nor an asm-specification are allowed
19524 on a function-definition. */
19525 if (asm_specification)
19526 error_at (asm_spec_start_token->location,
19527 "an asm-specification is not allowed "
19528 "on a function-definition");
19529 if (attributes)
19530 error_at (attributes_start_token->location,
19531 "attributes are not allowed "
19532 "on a function-definition");
19533 /* This is a function-definition. */
19534 *function_definition_p = true;
19535
19536 /* Parse the function definition. */
19537 if (member_p)
19538 decl = cp_parser_save_member_function_body (parser,
19539 decl_specifiers,
19540 declarator,
19541 prefix_attributes);
19542 else
19543 decl =
19544 (cp_parser_function_definition_from_specifiers_and_declarator
19545 (parser, decl_specifiers, prefix_attributes, declarator));
19546
19547 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19548 {
19549 /* This is where the prologue starts... */
19550 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19551 = func_brace_location;
19552 }
19553
19554 return decl;
19555 }
19556 }
19557 else if (parser->fully_implicit_function_template_p)
19558 {
19559 /* A non-template declaration involving a function parameter list
19560 containing an implicit template parameter will be made into a
19561 template. If the resulting declaration is not going to be an
19562 actual function then finish the template scope here to prevent it.
19563 An error message will be issued once we have a decl to talk about.
19564
19565 FIXME probably we should do type deduction rather than create an
19566 implicit template, but the standard currently doesn't allow it. */
19567 bogus_implicit_tmpl = true;
19568 finish_fully_implicit_template (parser, NULL_TREE);
19569 }
19570
19571 /* [dcl.dcl]
19572
19573 Only in function declarations for constructors, destructors, type
19574 conversions, and deduction guides can the decl-specifier-seq be omitted.
19575
19576 We explicitly postpone this check past the point where we handle
19577 function-definitions because we tolerate function-definitions
19578 that are missing their return types in some modes. */
19579 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19580 {
19581 cp_parser_error (parser,
19582 "expected constructor, destructor, or type conversion");
19583 return error_mark_node;
19584 }
19585
19586 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19587 if (token->type == CPP_EQ
19588 || token->type == CPP_OPEN_PAREN
19589 || token->type == CPP_OPEN_BRACE)
19590 {
19591 is_initialized = SD_INITIALIZED;
19592 initialization_kind = token->type;
19593 if (maybe_range_for_decl)
19594 *maybe_range_for_decl = error_mark_node;
19595 tmp_init_loc = token->location;
19596 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19597 *init_loc = tmp_init_loc;
19598
19599 if (token->type == CPP_EQ
19600 && function_declarator_p (declarator))
19601 {
19602 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19603 if (t2->keyword == RID_DEFAULT)
19604 is_initialized = SD_DEFAULTED;
19605 else if (t2->keyword == RID_DELETE)
19606 is_initialized = SD_DELETED;
19607 }
19608 }
19609 else
19610 {
19611 /* If the init-declarator isn't initialized and isn't followed by a
19612 `,' or `;', it's not a valid init-declarator. */
19613 if (token->type != CPP_COMMA
19614 && token->type != CPP_SEMICOLON)
19615 {
19616 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19617 range_for_decl_p = true;
19618 else
19619 {
19620 if (!maybe_range_for_decl)
19621 cp_parser_error (parser, "expected initializer");
19622 return error_mark_node;
19623 }
19624 }
19625 is_initialized = SD_UNINITIALIZED;
19626 initialization_kind = CPP_EOF;
19627 }
19628
19629 /* Because start_decl has side-effects, we should only call it if we
19630 know we're going ahead. By this point, we know that we cannot
19631 possibly be looking at any other construct. */
19632 cp_parser_commit_to_tentative_parse (parser);
19633
19634 /* Enter the newly declared entry in the symbol table. If we're
19635 processing a declaration in a class-specifier, we wait until
19636 after processing the initializer. */
19637 if (!member_p)
19638 {
19639 if (parser->in_unbraced_linkage_specification_p)
19640 decl_specifiers->storage_class = sc_extern;
19641 decl = start_decl (declarator, decl_specifiers,
19642 range_for_decl_p? SD_INITIALIZED : is_initialized,
19643 attributes, prefix_attributes, &pushed_scope);
19644 cp_finalize_omp_declare_simd (parser, decl);
19645 cp_finalize_oacc_routine (parser, decl, false);
19646 /* Adjust location of decl if declarator->id_loc is more appropriate:
19647 set, and decl wasn't merged with another decl, in which case its
19648 location would be different from input_location, and more accurate. */
19649 if (DECL_P (decl)
19650 && declarator->id_loc != UNKNOWN_LOCATION
19651 && DECL_SOURCE_LOCATION (decl) == input_location)
19652 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19653 }
19654 else if (scope)
19655 /* Enter the SCOPE. That way unqualified names appearing in the
19656 initializer will be looked up in SCOPE. */
19657 pushed_scope = push_scope (scope);
19658
19659 /* Perform deferred access control checks, now that we know in which
19660 SCOPE the declared entity resides. */
19661 if (!member_p && decl)
19662 {
19663 tree saved_current_function_decl = NULL_TREE;
19664
19665 /* If the entity being declared is a function, pretend that we
19666 are in its scope. If it is a `friend', it may have access to
19667 things that would not otherwise be accessible. */
19668 if (TREE_CODE (decl) == FUNCTION_DECL)
19669 {
19670 saved_current_function_decl = current_function_decl;
19671 current_function_decl = decl;
19672 }
19673
19674 /* Perform access checks for template parameters. */
19675 cp_parser_perform_template_parameter_access_checks (checks);
19676
19677 /* Perform the access control checks for the declarator and the
19678 decl-specifiers. */
19679 perform_deferred_access_checks (tf_warning_or_error);
19680
19681 /* Restore the saved value. */
19682 if (TREE_CODE (decl) == FUNCTION_DECL)
19683 current_function_decl = saved_current_function_decl;
19684 }
19685
19686 /* Parse the initializer. */
19687 initializer = NULL_TREE;
19688 is_direct_init = false;
19689 is_non_constant_init = true;
19690 if (is_initialized)
19691 {
19692 if (function_declarator_p (declarator))
19693 {
19694 if (initialization_kind == CPP_EQ)
19695 initializer = cp_parser_pure_specifier (parser);
19696 else
19697 {
19698 /* If the declaration was erroneous, we don't really
19699 know what the user intended, so just silently
19700 consume the initializer. */
19701 if (decl != error_mark_node)
19702 error_at (tmp_init_loc, "initializer provided for function");
19703 cp_parser_skip_to_closing_parenthesis (parser,
19704 /*recovering=*/true,
19705 /*or_comma=*/false,
19706 /*consume_paren=*/true);
19707 }
19708 }
19709 else
19710 {
19711 /* We want to record the extra mangling scope for in-class
19712 initializers of class members and initializers of static data
19713 member templates. The former involves deferring
19714 parsing of the initializer until end of class as with default
19715 arguments. So right here we only handle the latter. */
19716 if (!member_p && processing_template_decl)
19717 start_lambda_scope (decl);
19718 initializer = cp_parser_initializer (parser,
19719 &is_direct_init,
19720 &is_non_constant_init);
19721 if (!member_p && processing_template_decl)
19722 finish_lambda_scope ();
19723 if (initializer == error_mark_node)
19724 cp_parser_skip_to_end_of_statement (parser);
19725 }
19726 }
19727
19728 /* The old parser allows attributes to appear after a parenthesized
19729 initializer. Mark Mitchell proposed removing this functionality
19730 on the GCC mailing lists on 2002-08-13. This parser accepts the
19731 attributes -- but ignores them. */
19732 if (cp_parser_allow_gnu_extensions_p (parser)
19733 && initialization_kind == CPP_OPEN_PAREN)
19734 if (cp_parser_attributes_opt (parser))
19735 warning (OPT_Wattributes,
19736 "attributes after parenthesized initializer ignored");
19737
19738 /* And now complain about a non-function implicit template. */
19739 if (bogus_implicit_tmpl && decl != error_mark_node)
19740 error_at (DECL_SOURCE_LOCATION (decl),
19741 "non-function %qD declared as implicit template", decl);
19742
19743 /* For an in-class declaration, use `grokfield' to create the
19744 declaration. */
19745 if (member_p)
19746 {
19747 if (pushed_scope)
19748 {
19749 pop_scope (pushed_scope);
19750 pushed_scope = NULL_TREE;
19751 }
19752 decl = grokfield (declarator, decl_specifiers,
19753 initializer, !is_non_constant_init,
19754 /*asmspec=*/NULL_TREE,
19755 chainon (attributes, prefix_attributes));
19756 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19757 cp_parser_save_default_args (parser, decl);
19758 cp_finalize_omp_declare_simd (parser, decl);
19759 cp_finalize_oacc_routine (parser, decl, false);
19760 }
19761
19762 /* Finish processing the declaration. But, skip member
19763 declarations. */
19764 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19765 {
19766 cp_finish_decl (decl,
19767 initializer, !is_non_constant_init,
19768 asm_specification,
19769 /* If the initializer is in parentheses, then this is
19770 a direct-initialization, which means that an
19771 `explicit' constructor is OK. Otherwise, an
19772 `explicit' constructor cannot be used. */
19773 ((is_direct_init || !is_initialized)
19774 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19775 }
19776 else if ((cxx_dialect != cxx98) && friend_p
19777 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19778 /* Core issue #226 (C++0x only): A default template-argument
19779 shall not be specified in a friend class template
19780 declaration. */
19781 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19782 /*is_partial=*/false, /*is_friend_decl=*/1);
19783
19784 if (!friend_p && pushed_scope)
19785 pop_scope (pushed_scope);
19786
19787 if (function_declarator_p (declarator)
19788 && parser->fully_implicit_function_template_p)
19789 {
19790 if (member_p)
19791 decl = finish_fully_implicit_template (parser, decl);
19792 else
19793 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19794 }
19795
19796 if (auto_result && is_initialized && decl_specifiers->type
19797 && type_uses_auto (decl_specifiers->type))
19798 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19799
19800 return decl;
19801 }
19802
19803 /* Parse a declarator.
19804
19805 declarator:
19806 direct-declarator
19807 ptr-operator declarator
19808
19809 abstract-declarator:
19810 ptr-operator abstract-declarator [opt]
19811 direct-abstract-declarator
19812
19813 GNU Extensions:
19814
19815 declarator:
19816 attributes [opt] direct-declarator
19817 attributes [opt] ptr-operator declarator
19818
19819 abstract-declarator:
19820 attributes [opt] ptr-operator abstract-declarator [opt]
19821 attributes [opt] direct-abstract-declarator
19822
19823 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19824 detect constructors, destructors, deduction guides, or conversion operators.
19825 It is set to -1 if the declarator is a name, and +1 if it is a
19826 function. Otherwise it is set to zero. Usually you just want to
19827 test for >0, but internally the negative value is used.
19828
19829 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19830 a decl-specifier-seq unless it declares a constructor, destructor,
19831 or conversion. It might seem that we could check this condition in
19832 semantic analysis, rather than parsing, but that makes it difficult
19833 to handle something like `f()'. We want to notice that there are
19834 no decl-specifiers, and therefore realize that this is an
19835 expression, not a declaration.)
19836
19837 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19838 the declarator is a direct-declarator of the form "(...)".
19839
19840 MEMBER_P is true iff this declarator is a member-declarator.
19841
19842 FRIEND_P is true iff this declarator is a friend. */
19843
19844 static cp_declarator *
19845 cp_parser_declarator (cp_parser* parser,
19846 cp_parser_declarator_kind dcl_kind,
19847 int* ctor_dtor_or_conv_p,
19848 bool* parenthesized_p,
19849 bool member_p, bool friend_p)
19850 {
19851 cp_declarator *declarator;
19852 enum tree_code code;
19853 cp_cv_quals cv_quals;
19854 tree class_type;
19855 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19856
19857 /* Assume this is not a constructor, destructor, or type-conversion
19858 operator. */
19859 if (ctor_dtor_or_conv_p)
19860 *ctor_dtor_or_conv_p = 0;
19861
19862 if (cp_parser_allow_gnu_extensions_p (parser))
19863 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19864
19865 /* Check for the ptr-operator production. */
19866 cp_parser_parse_tentatively (parser);
19867 /* Parse the ptr-operator. */
19868 code = cp_parser_ptr_operator (parser,
19869 &class_type,
19870 &cv_quals,
19871 &std_attributes);
19872
19873 /* If that worked, then we have a ptr-operator. */
19874 if (cp_parser_parse_definitely (parser))
19875 {
19876 /* If a ptr-operator was found, then this declarator was not
19877 parenthesized. */
19878 if (parenthesized_p)
19879 *parenthesized_p = true;
19880 /* The dependent declarator is optional if we are parsing an
19881 abstract-declarator. */
19882 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19883 cp_parser_parse_tentatively (parser);
19884
19885 /* Parse the dependent declarator. */
19886 declarator = cp_parser_declarator (parser, dcl_kind,
19887 /*ctor_dtor_or_conv_p=*/NULL,
19888 /*parenthesized_p=*/NULL,
19889 /*member_p=*/false,
19890 friend_p);
19891
19892 /* If we are parsing an abstract-declarator, we must handle the
19893 case where the dependent declarator is absent. */
19894 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19895 && !cp_parser_parse_definitely (parser))
19896 declarator = NULL;
19897
19898 declarator = cp_parser_make_indirect_declarator
19899 (code, class_type, cv_quals, declarator, std_attributes);
19900 }
19901 /* Everything else is a direct-declarator. */
19902 else
19903 {
19904 if (parenthesized_p)
19905 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19906 CPP_OPEN_PAREN);
19907 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19908 ctor_dtor_or_conv_p,
19909 member_p, friend_p);
19910 }
19911
19912 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19913 declarator->attributes = gnu_attributes;
19914 return declarator;
19915 }
19916
19917 /* Parse a direct-declarator or direct-abstract-declarator.
19918
19919 direct-declarator:
19920 declarator-id
19921 direct-declarator ( parameter-declaration-clause )
19922 cv-qualifier-seq [opt]
19923 ref-qualifier [opt]
19924 exception-specification [opt]
19925 direct-declarator [ constant-expression [opt] ]
19926 ( declarator )
19927
19928 direct-abstract-declarator:
19929 direct-abstract-declarator [opt]
19930 ( parameter-declaration-clause )
19931 cv-qualifier-seq [opt]
19932 ref-qualifier [opt]
19933 exception-specification [opt]
19934 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19935 ( abstract-declarator )
19936
19937 Returns a representation of the declarator. DCL_KIND is
19938 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19939 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19940 we are parsing a direct-declarator. It is
19941 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19942 of ambiguity we prefer an abstract declarator, as per
19943 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19944 as for cp_parser_declarator. */
19945
19946 static cp_declarator *
19947 cp_parser_direct_declarator (cp_parser* parser,
19948 cp_parser_declarator_kind dcl_kind,
19949 int* ctor_dtor_or_conv_p,
19950 bool member_p, bool friend_p)
19951 {
19952 cp_token *token;
19953 cp_declarator *declarator = NULL;
19954 tree scope = NULL_TREE;
19955 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19956 bool saved_in_declarator_p = parser->in_declarator_p;
19957 bool first = true;
19958 tree pushed_scope = NULL_TREE;
19959 cp_token *open_paren = NULL, *close_paren = NULL;
19960
19961 while (true)
19962 {
19963 /* Peek at the next token. */
19964 token = cp_lexer_peek_token (parser->lexer);
19965 if (token->type == CPP_OPEN_PAREN)
19966 {
19967 /* This is either a parameter-declaration-clause, or a
19968 parenthesized declarator. When we know we are parsing a
19969 named declarator, it must be a parenthesized declarator
19970 if FIRST is true. For instance, `(int)' is a
19971 parameter-declaration-clause, with an omitted
19972 direct-abstract-declarator. But `((*))', is a
19973 parenthesized abstract declarator. Finally, when T is a
19974 template parameter `(T)' is a
19975 parameter-declaration-clause, and not a parenthesized
19976 named declarator.
19977
19978 We first try and parse a parameter-declaration-clause,
19979 and then try a nested declarator (if FIRST is true).
19980
19981 It is not an error for it not to be a
19982 parameter-declaration-clause, even when FIRST is
19983 false. Consider,
19984
19985 int i (int);
19986 int i (3);
19987
19988 The first is the declaration of a function while the
19989 second is the definition of a variable, including its
19990 initializer.
19991
19992 Having seen only the parenthesis, we cannot know which of
19993 these two alternatives should be selected. Even more
19994 complex are examples like:
19995
19996 int i (int (a));
19997 int i (int (3));
19998
19999 The former is a function-declaration; the latter is a
20000 variable initialization.
20001
20002 Thus again, we try a parameter-declaration-clause, and if
20003 that fails, we back out and return. */
20004
20005 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20006 {
20007 tree params;
20008 bool is_declarator = false;
20009
20010 open_paren = NULL;
20011
20012 /* In a member-declarator, the only valid interpretation
20013 of a parenthesis is the start of a
20014 parameter-declaration-clause. (It is invalid to
20015 initialize a static data member with a parenthesized
20016 initializer; only the "=" form of initialization is
20017 permitted.) */
20018 if (!member_p)
20019 cp_parser_parse_tentatively (parser);
20020
20021 /* Consume the `('. */
20022 matching_parens parens;
20023 parens.consume_open (parser);
20024 if (first)
20025 {
20026 /* If this is going to be an abstract declarator, we're
20027 in a declarator and we can't have default args. */
20028 parser->default_arg_ok_p = false;
20029 parser->in_declarator_p = true;
20030 }
20031
20032 begin_scope (sk_function_parms, NULL_TREE);
20033
20034 /* Parse the parameter-declaration-clause. */
20035 params = cp_parser_parameter_declaration_clause (parser);
20036
20037 /* Consume the `)'. */
20038 parens.require_close (parser);
20039
20040 /* If all went well, parse the cv-qualifier-seq,
20041 ref-qualifier and the exception-specification. */
20042 if (member_p || cp_parser_parse_definitely (parser))
20043 {
20044 cp_cv_quals cv_quals;
20045 cp_virt_specifiers virt_specifiers;
20046 cp_ref_qualifier ref_qual;
20047 tree exception_specification;
20048 tree late_return;
20049 tree attrs;
20050 bool memfn = (member_p || (pushed_scope
20051 && CLASS_TYPE_P (pushed_scope)));
20052
20053 is_declarator = true;
20054
20055 if (ctor_dtor_or_conv_p)
20056 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20057 first = false;
20058
20059 /* Parse the cv-qualifier-seq. */
20060 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20061 /* Parse the ref-qualifier. */
20062 ref_qual = cp_parser_ref_qualifier_opt (parser);
20063 /* Parse the tx-qualifier. */
20064 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20065 /* And the exception-specification. */
20066 exception_specification
20067 = cp_parser_exception_specification_opt (parser);
20068
20069 attrs = cp_parser_std_attribute_spec_seq (parser);
20070
20071 /* In here, we handle cases where attribute is used after
20072 the function declaration. For example:
20073 void func (int x) __attribute__((vector(..))); */
20074 tree gnu_attrs = NULL_TREE;
20075 if (flag_cilkplus
20076 && cp_next_tokens_can_be_gnu_attribute_p (parser))
20077 {
20078 cp_parser_parse_tentatively (parser);
20079 tree attr = cp_parser_gnu_attributes_opt (parser);
20080 if (cp_lexer_next_token_is_not (parser->lexer,
20081 CPP_SEMICOLON)
20082 && cp_lexer_next_token_is_not (parser->lexer,
20083 CPP_OPEN_BRACE))
20084 cp_parser_abort_tentative_parse (parser);
20085 else if (!cp_parser_parse_definitely (parser))
20086 ;
20087 else
20088 gnu_attrs = attr;
20089 }
20090 tree requires_clause = NULL_TREE;
20091 late_return = (cp_parser_late_return_type_opt
20092 (parser, declarator, requires_clause,
20093 memfn ? cv_quals : -1));
20094
20095 /* Parse the virt-specifier-seq. */
20096 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20097
20098 /* Create the function-declarator. */
20099 declarator = make_call_declarator (declarator,
20100 params,
20101 cv_quals,
20102 virt_specifiers,
20103 ref_qual,
20104 tx_qual,
20105 exception_specification,
20106 late_return,
20107 requires_clause);
20108 declarator->std_attributes = attrs;
20109 declarator->attributes = gnu_attrs;
20110 /* Any subsequent parameter lists are to do with
20111 return type, so are not those of the declared
20112 function. */
20113 parser->default_arg_ok_p = false;
20114 }
20115
20116 /* Remove the function parms from scope. */
20117 pop_bindings_and_leave_scope ();
20118
20119 if (is_declarator)
20120 /* Repeat the main loop. */
20121 continue;
20122 }
20123
20124 /* If this is the first, we can try a parenthesized
20125 declarator. */
20126 if (first)
20127 {
20128 bool saved_in_type_id_in_expr_p;
20129
20130 parser->default_arg_ok_p = saved_default_arg_ok_p;
20131 parser->in_declarator_p = saved_in_declarator_p;
20132
20133 open_paren = token;
20134 /* Consume the `('. */
20135 matching_parens parens;
20136 parens.consume_open (parser);
20137 /* Parse the nested declarator. */
20138 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20139 parser->in_type_id_in_expr_p = true;
20140 declarator
20141 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20142 /*parenthesized_p=*/NULL,
20143 member_p, friend_p);
20144 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20145 first = false;
20146 /* Expect a `)'. */
20147 close_paren = cp_lexer_peek_token (parser->lexer);
20148 if (!parens.require_close (parser))
20149 declarator = cp_error_declarator;
20150 if (declarator == cp_error_declarator)
20151 break;
20152
20153 goto handle_declarator;
20154 }
20155 /* Otherwise, we must be done. */
20156 else
20157 break;
20158 }
20159 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20160 && token->type == CPP_OPEN_SQUARE
20161 && !cp_next_tokens_can_be_attribute_p (parser))
20162 {
20163 /* Parse an array-declarator. */
20164 tree bounds, attrs;
20165
20166 if (ctor_dtor_or_conv_p)
20167 *ctor_dtor_or_conv_p = 0;
20168
20169 open_paren = NULL;
20170 first = false;
20171 parser->default_arg_ok_p = false;
20172 parser->in_declarator_p = true;
20173 /* Consume the `['. */
20174 cp_lexer_consume_token (parser->lexer);
20175 /* Peek at the next token. */
20176 token = cp_lexer_peek_token (parser->lexer);
20177 /* If the next token is `]', then there is no
20178 constant-expression. */
20179 if (token->type != CPP_CLOSE_SQUARE)
20180 {
20181 bool non_constant_p;
20182 bounds
20183 = cp_parser_constant_expression (parser,
20184 /*allow_non_constant=*/true,
20185 &non_constant_p);
20186 if (!non_constant_p)
20187 /* OK */;
20188 else if (error_operand_p (bounds))
20189 /* Already gave an error. */;
20190 else if (!parser->in_function_body
20191 || current_binding_level->kind == sk_function_parms)
20192 {
20193 /* Normally, the array bound must be an integral constant
20194 expression. However, as an extension, we allow VLAs
20195 in function scopes as long as they aren't part of a
20196 parameter declaration. */
20197 cp_parser_error (parser,
20198 "array bound is not an integer constant");
20199 bounds = error_mark_node;
20200 }
20201 else if (processing_template_decl
20202 && !type_dependent_expression_p (bounds))
20203 {
20204 /* Remember this wasn't a constant-expression. */
20205 bounds = build_nop (TREE_TYPE (bounds), bounds);
20206 TREE_SIDE_EFFECTS (bounds) = 1;
20207 }
20208 }
20209 else
20210 bounds = NULL_TREE;
20211 /* Look for the closing `]'. */
20212 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20213 {
20214 declarator = cp_error_declarator;
20215 break;
20216 }
20217
20218 attrs = cp_parser_std_attribute_spec_seq (parser);
20219 declarator = make_array_declarator (declarator, bounds);
20220 declarator->std_attributes = attrs;
20221 }
20222 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20223 {
20224 {
20225 tree qualifying_scope;
20226 tree unqualified_name;
20227 tree attrs;
20228 special_function_kind sfk;
20229 bool abstract_ok;
20230 bool pack_expansion_p = false;
20231 cp_token *declarator_id_start_token;
20232
20233 /* Parse a declarator-id */
20234 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20235 if (abstract_ok)
20236 {
20237 cp_parser_parse_tentatively (parser);
20238
20239 /* If we see an ellipsis, we should be looking at a
20240 parameter pack. */
20241 if (token->type == CPP_ELLIPSIS)
20242 {
20243 /* Consume the `...' */
20244 cp_lexer_consume_token (parser->lexer);
20245
20246 pack_expansion_p = true;
20247 }
20248 }
20249
20250 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20251 unqualified_name
20252 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20253 qualifying_scope = parser->scope;
20254 if (abstract_ok)
20255 {
20256 bool okay = false;
20257
20258 if (!unqualified_name && pack_expansion_p)
20259 {
20260 /* Check whether an error occurred. */
20261 okay = !cp_parser_error_occurred (parser);
20262
20263 /* We already consumed the ellipsis to mark a
20264 parameter pack, but we have no way to report it,
20265 so abort the tentative parse. We will be exiting
20266 immediately anyway. */
20267 cp_parser_abort_tentative_parse (parser);
20268 }
20269 else
20270 okay = cp_parser_parse_definitely (parser);
20271
20272 if (!okay)
20273 unqualified_name = error_mark_node;
20274 else if (unqualified_name
20275 && (qualifying_scope
20276 || (!identifier_p (unqualified_name))))
20277 {
20278 cp_parser_error (parser, "expected unqualified-id");
20279 unqualified_name = error_mark_node;
20280 }
20281 }
20282
20283 if (!unqualified_name)
20284 return NULL;
20285 if (unqualified_name == error_mark_node)
20286 {
20287 declarator = cp_error_declarator;
20288 pack_expansion_p = false;
20289 declarator->parameter_pack_p = false;
20290 break;
20291 }
20292
20293 attrs = cp_parser_std_attribute_spec_seq (parser);
20294
20295 if (qualifying_scope && at_namespace_scope_p ()
20296 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20297 {
20298 /* In the declaration of a member of a template class
20299 outside of the class itself, the SCOPE will sometimes
20300 be a TYPENAME_TYPE. For example, given:
20301
20302 template <typename T>
20303 int S<T>::R::i = 3;
20304
20305 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20306 this context, we must resolve S<T>::R to an ordinary
20307 type, rather than a typename type.
20308
20309 The reason we normally avoid resolving TYPENAME_TYPEs
20310 is that a specialization of `S' might render
20311 `S<T>::R' not a type. However, if `S' is
20312 specialized, then this `i' will not be used, so there
20313 is no harm in resolving the types here. */
20314 tree type;
20315
20316 /* Resolve the TYPENAME_TYPE. */
20317 type = resolve_typename_type (qualifying_scope,
20318 /*only_current_p=*/false);
20319 /* If that failed, the declarator is invalid. */
20320 if (TREE_CODE (type) == TYPENAME_TYPE)
20321 {
20322 if (typedef_variant_p (type))
20323 error_at (declarator_id_start_token->location,
20324 "cannot define member of dependent typedef "
20325 "%qT", type);
20326 else
20327 error_at (declarator_id_start_token->location,
20328 "%<%T::%E%> is not a type",
20329 TYPE_CONTEXT (qualifying_scope),
20330 TYPE_IDENTIFIER (qualifying_scope));
20331 }
20332 qualifying_scope = type;
20333 }
20334
20335 sfk = sfk_none;
20336
20337 if (unqualified_name)
20338 {
20339 tree class_type;
20340
20341 if (qualifying_scope
20342 && CLASS_TYPE_P (qualifying_scope))
20343 class_type = qualifying_scope;
20344 else
20345 class_type = current_class_type;
20346
20347 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20348 {
20349 tree name_type = TREE_TYPE (unqualified_name);
20350
20351 if (!class_type || !same_type_p (name_type, class_type))
20352 {
20353 /* We do not attempt to print the declarator
20354 here because we do not have enough
20355 information about its original syntactic
20356 form. */
20357 cp_parser_error (parser, "invalid declarator");
20358 declarator = cp_error_declarator;
20359 break;
20360 }
20361 else if (qualifying_scope
20362 && CLASSTYPE_USE_TEMPLATE (name_type))
20363 {
20364 error_at (declarator_id_start_token->location,
20365 "invalid use of constructor as a template");
20366 inform (declarator_id_start_token->location,
20367 "use %<%T::%D%> instead of %<%T::%D%> to "
20368 "name the constructor in a qualified name",
20369 class_type,
20370 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20371 class_type, name_type);
20372 declarator = cp_error_declarator;
20373 break;
20374 }
20375 unqualified_name = constructor_name (class_type);
20376 }
20377
20378 if (class_type)
20379 {
20380 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20381 sfk = sfk_destructor;
20382 else if (identifier_p (unqualified_name)
20383 && IDENTIFIER_CONV_OP_P (unqualified_name))
20384 sfk = sfk_conversion;
20385 else if (/* There's no way to declare a constructor
20386 for an unnamed type, even if the type
20387 got a name for linkage purposes. */
20388 !TYPE_WAS_UNNAMED (class_type)
20389 /* Handle correctly (c++/19200):
20390
20391 struct S {
20392 struct T{};
20393 friend void S(T);
20394 };
20395
20396 and also:
20397
20398 namespace N {
20399 void S();
20400 }
20401
20402 struct S {
20403 friend void N::S();
20404 }; */
20405 && (!friend_p || class_type == qualifying_scope)
20406 && constructor_name_p (unqualified_name,
20407 class_type))
20408 sfk = sfk_constructor;
20409 else if (is_overloaded_fn (unqualified_name)
20410 && DECL_CONSTRUCTOR_P (get_first_fn
20411 (unqualified_name)))
20412 sfk = sfk_constructor;
20413
20414 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20415 *ctor_dtor_or_conv_p = -1;
20416 }
20417 }
20418 declarator = make_id_declarator (qualifying_scope,
20419 unqualified_name,
20420 sfk);
20421 declarator->std_attributes = attrs;
20422 declarator->id_loc = token->location;
20423 declarator->parameter_pack_p = pack_expansion_p;
20424
20425 if (pack_expansion_p)
20426 maybe_warn_variadic_templates ();
20427 }
20428
20429 handle_declarator:;
20430 scope = get_scope_of_declarator (declarator);
20431 if (scope)
20432 {
20433 /* Any names that appear after the declarator-id for a
20434 member are looked up in the containing scope. */
20435 if (at_function_scope_p ())
20436 {
20437 /* But declarations with qualified-ids can't appear in a
20438 function. */
20439 cp_parser_error (parser, "qualified-id in declaration");
20440 declarator = cp_error_declarator;
20441 break;
20442 }
20443 pushed_scope = push_scope (scope);
20444 }
20445 parser->in_declarator_p = true;
20446 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20447 || (declarator && declarator->kind == cdk_id))
20448 /* Default args are only allowed on function
20449 declarations. */
20450 parser->default_arg_ok_p = saved_default_arg_ok_p;
20451 else
20452 parser->default_arg_ok_p = false;
20453
20454 first = false;
20455 }
20456 /* We're done. */
20457 else
20458 break;
20459 }
20460
20461 /* For an abstract declarator, we might wind up with nothing at this
20462 point. That's an error; the declarator is not optional. */
20463 if (!declarator)
20464 cp_parser_error (parser, "expected declarator");
20465 else if (open_paren)
20466 {
20467 /* Record overly parenthesized declarator so we can give a
20468 diagnostic about confusing decl/expr disambiguation. */
20469 if (declarator->kind == cdk_array)
20470 {
20471 /* If the open and close parens are on different lines, this
20472 is probably a formatting thing, so ignore. */
20473 expanded_location open = expand_location (open_paren->location);
20474 expanded_location close = expand_location (close_paren->location);
20475 if (open.line != close.line || open.file != close.file)
20476 open_paren = NULL;
20477 }
20478 if (open_paren)
20479 declarator->parenthesized = open_paren->location;
20480 }
20481
20482 /* If we entered a scope, we must exit it now. */
20483 if (pushed_scope)
20484 pop_scope (pushed_scope);
20485
20486 parser->default_arg_ok_p = saved_default_arg_ok_p;
20487 parser->in_declarator_p = saved_in_declarator_p;
20488
20489 return declarator;
20490 }
20491
20492 /* Parse a ptr-operator.
20493
20494 ptr-operator:
20495 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20496 * cv-qualifier-seq [opt]
20497 &
20498 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20499 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20500
20501 GNU Extension:
20502
20503 ptr-operator:
20504 & cv-qualifier-seq [opt]
20505
20506 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20507 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20508 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20509 filled in with the TYPE containing the member. *CV_QUALS is
20510 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20511 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20512 Note that the tree codes returned by this function have nothing
20513 to do with the types of trees that will be eventually be created
20514 to represent the pointer or reference type being parsed. They are
20515 just constants with suggestive names. */
20516 static enum tree_code
20517 cp_parser_ptr_operator (cp_parser* parser,
20518 tree* type,
20519 cp_cv_quals *cv_quals,
20520 tree *attributes)
20521 {
20522 enum tree_code code = ERROR_MARK;
20523 cp_token *token;
20524 tree attrs = NULL_TREE;
20525
20526 /* Assume that it's not a pointer-to-member. */
20527 *type = NULL_TREE;
20528 /* And that there are no cv-qualifiers. */
20529 *cv_quals = TYPE_UNQUALIFIED;
20530
20531 /* Peek at the next token. */
20532 token = cp_lexer_peek_token (parser->lexer);
20533
20534 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20535 if (token->type == CPP_MULT)
20536 code = INDIRECT_REF;
20537 else if (token->type == CPP_AND)
20538 code = ADDR_EXPR;
20539 else if ((cxx_dialect != cxx98) &&
20540 token->type == CPP_AND_AND) /* C++0x only */
20541 code = NON_LVALUE_EXPR;
20542
20543 if (code != ERROR_MARK)
20544 {
20545 /* Consume the `*', `&' or `&&'. */
20546 cp_lexer_consume_token (parser->lexer);
20547
20548 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20549 `&', if we are allowing GNU extensions. (The only qualifier
20550 that can legally appear after `&' is `restrict', but that is
20551 enforced during semantic analysis. */
20552 if (code == INDIRECT_REF
20553 || cp_parser_allow_gnu_extensions_p (parser))
20554 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20555
20556 attrs = cp_parser_std_attribute_spec_seq (parser);
20557 if (attributes != NULL)
20558 *attributes = attrs;
20559 }
20560 else
20561 {
20562 /* Try the pointer-to-member case. */
20563 cp_parser_parse_tentatively (parser);
20564 /* Look for the optional `::' operator. */
20565 cp_parser_global_scope_opt (parser,
20566 /*current_scope_valid_p=*/false);
20567 /* Look for the nested-name specifier. */
20568 token = cp_lexer_peek_token (parser->lexer);
20569 cp_parser_nested_name_specifier (parser,
20570 /*typename_keyword_p=*/false,
20571 /*check_dependency_p=*/true,
20572 /*type_p=*/false,
20573 /*is_declaration=*/false);
20574 /* If we found it, and the next token is a `*', then we are
20575 indeed looking at a pointer-to-member operator. */
20576 if (!cp_parser_error_occurred (parser)
20577 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20578 {
20579 /* Indicate that the `*' operator was used. */
20580 code = INDIRECT_REF;
20581
20582 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20583 error_at (token->location, "%qD is a namespace", parser->scope);
20584 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20585 error_at (token->location, "cannot form pointer to member of "
20586 "non-class %q#T", parser->scope);
20587 else
20588 {
20589 /* The type of which the member is a member is given by the
20590 current SCOPE. */
20591 *type = parser->scope;
20592 /* The next name will not be qualified. */
20593 parser->scope = NULL_TREE;
20594 parser->qualifying_scope = NULL_TREE;
20595 parser->object_scope = NULL_TREE;
20596 /* Look for optional c++11 attributes. */
20597 attrs = cp_parser_std_attribute_spec_seq (parser);
20598 if (attributes != NULL)
20599 *attributes = attrs;
20600 /* Look for the optional cv-qualifier-seq. */
20601 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20602 }
20603 }
20604 /* If that didn't work we don't have a ptr-operator. */
20605 if (!cp_parser_parse_definitely (parser))
20606 cp_parser_error (parser, "expected ptr-operator");
20607 }
20608
20609 return code;
20610 }
20611
20612 /* Parse an (optional) cv-qualifier-seq.
20613
20614 cv-qualifier-seq:
20615 cv-qualifier cv-qualifier-seq [opt]
20616
20617 cv-qualifier:
20618 const
20619 volatile
20620
20621 GNU Extension:
20622
20623 cv-qualifier:
20624 __restrict__
20625
20626 Returns a bitmask representing the cv-qualifiers. */
20627
20628 static cp_cv_quals
20629 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20630 {
20631 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20632
20633 while (true)
20634 {
20635 cp_token *token;
20636 cp_cv_quals cv_qualifier;
20637
20638 /* Peek at the next token. */
20639 token = cp_lexer_peek_token (parser->lexer);
20640 /* See if it's a cv-qualifier. */
20641 switch (token->keyword)
20642 {
20643 case RID_CONST:
20644 cv_qualifier = TYPE_QUAL_CONST;
20645 break;
20646
20647 case RID_VOLATILE:
20648 cv_qualifier = TYPE_QUAL_VOLATILE;
20649 break;
20650
20651 case RID_RESTRICT:
20652 cv_qualifier = TYPE_QUAL_RESTRICT;
20653 break;
20654
20655 default:
20656 cv_qualifier = TYPE_UNQUALIFIED;
20657 break;
20658 }
20659
20660 if (!cv_qualifier)
20661 break;
20662
20663 if (cv_quals & cv_qualifier)
20664 {
20665 gcc_rich_location richloc (token->location);
20666 richloc.add_fixit_remove ();
20667 error_at (&richloc, "duplicate cv-qualifier");
20668 cp_lexer_purge_token (parser->lexer);
20669 }
20670 else
20671 {
20672 cp_lexer_consume_token (parser->lexer);
20673 cv_quals |= cv_qualifier;
20674 }
20675 }
20676
20677 return cv_quals;
20678 }
20679
20680 /* Parse an (optional) ref-qualifier
20681
20682 ref-qualifier:
20683 &
20684 &&
20685
20686 Returns cp_ref_qualifier representing ref-qualifier. */
20687
20688 static cp_ref_qualifier
20689 cp_parser_ref_qualifier_opt (cp_parser* parser)
20690 {
20691 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20692
20693 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20694 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20695 return ref_qual;
20696
20697 while (true)
20698 {
20699 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20700 cp_token *token = cp_lexer_peek_token (parser->lexer);
20701
20702 switch (token->type)
20703 {
20704 case CPP_AND:
20705 curr_ref_qual = REF_QUAL_LVALUE;
20706 break;
20707
20708 case CPP_AND_AND:
20709 curr_ref_qual = REF_QUAL_RVALUE;
20710 break;
20711
20712 default:
20713 curr_ref_qual = REF_QUAL_NONE;
20714 break;
20715 }
20716
20717 if (!curr_ref_qual)
20718 break;
20719 else if (ref_qual)
20720 {
20721 error_at (token->location, "multiple ref-qualifiers");
20722 cp_lexer_purge_token (parser->lexer);
20723 }
20724 else
20725 {
20726 ref_qual = curr_ref_qual;
20727 cp_lexer_consume_token (parser->lexer);
20728 }
20729 }
20730
20731 return ref_qual;
20732 }
20733
20734 /* Parse an optional tx-qualifier.
20735
20736 tx-qualifier:
20737 transaction_safe
20738 transaction_safe_dynamic */
20739
20740 static tree
20741 cp_parser_tx_qualifier_opt (cp_parser *parser)
20742 {
20743 cp_token *token = cp_lexer_peek_token (parser->lexer);
20744 if (token->type == CPP_NAME)
20745 {
20746 tree name = token->u.value;
20747 const char *p = IDENTIFIER_POINTER (name);
20748 const int len = strlen ("transaction_safe");
20749 if (!strncmp (p, "transaction_safe", len))
20750 {
20751 p += len;
20752 if (*p == '\0'
20753 || !strcmp (p, "_dynamic"))
20754 {
20755 cp_lexer_consume_token (parser->lexer);
20756 if (!flag_tm)
20757 {
20758 error ("%qE requires %<-fgnu-tm%>", name);
20759 return NULL_TREE;
20760 }
20761 else
20762 return name;
20763 }
20764 }
20765 }
20766 return NULL_TREE;
20767 }
20768
20769 /* Parse an (optional) virt-specifier-seq.
20770
20771 virt-specifier-seq:
20772 virt-specifier virt-specifier-seq [opt]
20773
20774 virt-specifier:
20775 override
20776 final
20777
20778 Returns a bitmask representing the virt-specifiers. */
20779
20780 static cp_virt_specifiers
20781 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20782 {
20783 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20784
20785 while (true)
20786 {
20787 cp_token *token;
20788 cp_virt_specifiers virt_specifier;
20789
20790 /* Peek at the next token. */
20791 token = cp_lexer_peek_token (parser->lexer);
20792 /* See if it's a virt-specifier-qualifier. */
20793 if (token->type != CPP_NAME)
20794 break;
20795 if (id_equal (token->u.value, "override"))
20796 {
20797 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20798 virt_specifier = VIRT_SPEC_OVERRIDE;
20799 }
20800 else if (id_equal (token->u.value, "final"))
20801 {
20802 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20803 virt_specifier = VIRT_SPEC_FINAL;
20804 }
20805 else if (id_equal (token->u.value, "__final"))
20806 {
20807 virt_specifier = VIRT_SPEC_FINAL;
20808 }
20809 else
20810 break;
20811
20812 if (virt_specifiers & virt_specifier)
20813 {
20814 gcc_rich_location richloc (token->location);
20815 richloc.add_fixit_remove ();
20816 error_at (&richloc, "duplicate virt-specifier");
20817 cp_lexer_purge_token (parser->lexer);
20818 }
20819 else
20820 {
20821 cp_lexer_consume_token (parser->lexer);
20822 virt_specifiers |= virt_specifier;
20823 }
20824 }
20825 return virt_specifiers;
20826 }
20827
20828 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20829 is in scope even though it isn't real. */
20830
20831 void
20832 inject_this_parameter (tree ctype, cp_cv_quals quals)
20833 {
20834 tree this_parm;
20835
20836 if (current_class_ptr)
20837 {
20838 /* We don't clear this between NSDMIs. Is it already what we want? */
20839 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20840 if (DECL_P (current_class_ptr)
20841 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20842 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20843 && cp_type_quals (type) == quals)
20844 return;
20845 }
20846
20847 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20848 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20849 current_class_ptr = NULL_TREE;
20850 current_class_ref
20851 = cp_build_fold_indirect_ref (this_parm);
20852 current_class_ptr = this_parm;
20853 }
20854
20855 /* Return true iff our current scope is a non-static data member
20856 initializer. */
20857
20858 bool
20859 parsing_nsdmi (void)
20860 {
20861 /* We recognize NSDMI context by the context-less 'this' pointer set up
20862 by the function above. */
20863 if (current_class_ptr
20864 && TREE_CODE (current_class_ptr) == PARM_DECL
20865 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20866 return true;
20867 return false;
20868 }
20869
20870 /* Parse a late-specified return type, if any. This is not a separate
20871 non-terminal, but part of a function declarator, which looks like
20872
20873 -> trailing-type-specifier-seq abstract-declarator(opt)
20874
20875 Returns the type indicated by the type-id.
20876
20877 In addition to this, parse any queued up #pragma omp declare simd
20878 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20879 #pragma acc routine clauses.
20880
20881 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20882 function. */
20883
20884 static tree
20885 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20886 tree& requires_clause, cp_cv_quals quals)
20887 {
20888 cp_token *token;
20889 tree type = NULL_TREE;
20890 bool declare_simd_p = (parser->omp_declare_simd
20891 && declarator
20892 && declarator->kind == cdk_id);
20893
20894 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20895 && declarator && declarator->kind == cdk_id);
20896
20897 bool oacc_routine_p = (parser->oacc_routine
20898 && declarator
20899 && declarator->kind == cdk_id);
20900
20901 /* Peek at the next token. */
20902 token = cp_lexer_peek_token (parser->lexer);
20903 /* A late-specified return type is indicated by an initial '->'. */
20904 if (token->type != CPP_DEREF
20905 && token->keyword != RID_REQUIRES
20906 && !(token->type == CPP_NAME
20907 && token->u.value == ridpointers[RID_REQUIRES])
20908 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20909 return NULL_TREE;
20910
20911 tree save_ccp = current_class_ptr;
20912 tree save_ccr = current_class_ref;
20913 if (quals >= 0)
20914 {
20915 /* DR 1207: 'this' is in scope in the trailing return type. */
20916 inject_this_parameter (current_class_type, quals);
20917 }
20918
20919 if (token->type == CPP_DEREF)
20920 {
20921 /* Consume the ->. */
20922 cp_lexer_consume_token (parser->lexer);
20923
20924 type = cp_parser_trailing_type_id (parser);
20925 }
20926
20927 /* Function declarations may be followed by a trailing
20928 requires-clause. */
20929 requires_clause = cp_parser_requires_clause_opt (parser);
20930
20931 if (cilk_simd_fn_vector_p)
20932 declarator->attributes
20933 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20934 declarator->attributes);
20935 if (declare_simd_p)
20936 declarator->attributes
20937 = cp_parser_late_parsing_omp_declare_simd (parser,
20938 declarator->attributes);
20939 if (oacc_routine_p)
20940 declarator->attributes
20941 = cp_parser_late_parsing_oacc_routine (parser,
20942 declarator->attributes);
20943
20944 if (quals >= 0)
20945 {
20946 current_class_ptr = save_ccp;
20947 current_class_ref = save_ccr;
20948 }
20949
20950 return type;
20951 }
20952
20953 /* Parse a declarator-id.
20954
20955 declarator-id:
20956 id-expression
20957 :: [opt] nested-name-specifier [opt] type-name
20958
20959 In the `id-expression' case, the value returned is as for
20960 cp_parser_id_expression if the id-expression was an unqualified-id.
20961 If the id-expression was a qualified-id, then a SCOPE_REF is
20962 returned. The first operand is the scope (either a NAMESPACE_DECL
20963 or TREE_TYPE), but the second is still just a representation of an
20964 unqualified-id. */
20965
20966 static tree
20967 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20968 {
20969 tree id;
20970 /* The expression must be an id-expression. Assume that qualified
20971 names are the names of types so that:
20972
20973 template <class T>
20974 int S<T>::R::i = 3;
20975
20976 will work; we must treat `S<T>::R' as the name of a type.
20977 Similarly, assume that qualified names are templates, where
20978 required, so that:
20979
20980 template <class T>
20981 int S<T>::R<T>::i = 3;
20982
20983 will work, too. */
20984 id = cp_parser_id_expression (parser,
20985 /*template_keyword_p=*/false,
20986 /*check_dependency_p=*/false,
20987 /*template_p=*/NULL,
20988 /*declarator_p=*/true,
20989 optional_p);
20990 if (id && BASELINK_P (id))
20991 id = BASELINK_FUNCTIONS (id);
20992 return id;
20993 }
20994
20995 /* Parse a type-id.
20996
20997 type-id:
20998 type-specifier-seq abstract-declarator [opt]
20999
21000 Returns the TYPE specified. */
21001
21002 static tree
21003 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21004 bool is_trailing_return)
21005 {
21006 cp_decl_specifier_seq type_specifier_seq;
21007 cp_declarator *abstract_declarator;
21008
21009 /* Parse the type-specifier-seq. */
21010 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21011 is_trailing_return,
21012 &type_specifier_seq);
21013 if (is_template_arg && type_specifier_seq.type
21014 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21015 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21016 /* A bare template name as a template argument is a template template
21017 argument, not a placeholder, so fail parsing it as a type argument. */
21018 {
21019 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21020 cp_parser_simulate_error (parser);
21021 return error_mark_node;
21022 }
21023 if (type_specifier_seq.type == error_mark_node)
21024 return error_mark_node;
21025
21026 /* There might or might not be an abstract declarator. */
21027 cp_parser_parse_tentatively (parser);
21028 /* Look for the declarator. */
21029 abstract_declarator
21030 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21031 /*parenthesized_p=*/NULL,
21032 /*member_p=*/false,
21033 /*friend_p=*/false);
21034 /* Check to see if there really was a declarator. */
21035 if (!cp_parser_parse_definitely (parser))
21036 abstract_declarator = NULL;
21037
21038 if (type_specifier_seq.type
21039 /* The concepts TS allows 'auto' as a type-id. */
21040 && (!flag_concepts || parser->in_type_id_in_expr_p)
21041 /* None of the valid uses of 'auto' in C++14 involve the type-id
21042 nonterminal, but it is valid in a trailing-return-type. */
21043 && !(cxx_dialect >= cxx14 && is_trailing_return))
21044 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21045 {
21046 /* A type-id with type 'auto' is only ok if the abstract declarator
21047 is a function declarator with a late-specified return type.
21048
21049 A type-id with 'auto' is also valid in a trailing-return-type
21050 in a compound-requirement. */
21051 if (abstract_declarator
21052 && abstract_declarator->kind == cdk_function
21053 && abstract_declarator->u.function.late_return_type)
21054 /* OK */;
21055 else if (parser->in_result_type_constraint_p)
21056 /* OK */;
21057 else
21058 {
21059 location_t loc = type_specifier_seq.locations[ds_type_spec];
21060 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21061 {
21062 error_at (loc, "missing template arguments after %qT",
21063 auto_node);
21064 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21065 tmpl);
21066 }
21067 else
21068 error_at (loc, "invalid use of %qT", auto_node);
21069 return error_mark_node;
21070 }
21071 }
21072
21073 return groktypename (&type_specifier_seq, abstract_declarator,
21074 is_template_arg);
21075 }
21076
21077 static tree
21078 cp_parser_type_id (cp_parser *parser)
21079 {
21080 return cp_parser_type_id_1 (parser, false, false);
21081 }
21082
21083 static tree
21084 cp_parser_template_type_arg (cp_parser *parser)
21085 {
21086 tree r;
21087 const char *saved_message = parser->type_definition_forbidden_message;
21088 parser->type_definition_forbidden_message
21089 = G_("types may not be defined in template arguments");
21090 r = cp_parser_type_id_1 (parser, true, false);
21091 parser->type_definition_forbidden_message = saved_message;
21092 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21093 {
21094 error ("invalid use of %<auto%> in template argument");
21095 r = error_mark_node;
21096 }
21097 return r;
21098 }
21099
21100 static tree
21101 cp_parser_trailing_type_id (cp_parser *parser)
21102 {
21103 return cp_parser_type_id_1 (parser, false, true);
21104 }
21105
21106 /* Parse a type-specifier-seq.
21107
21108 type-specifier-seq:
21109 type-specifier type-specifier-seq [opt]
21110
21111 GNU extension:
21112
21113 type-specifier-seq:
21114 attributes type-specifier-seq [opt]
21115
21116 If IS_DECLARATION is true, we are at the start of a "condition" or
21117 exception-declaration, so we might be followed by a declarator-id.
21118
21119 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21120 i.e. we've just seen "->".
21121
21122 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21123
21124 static void
21125 cp_parser_type_specifier_seq (cp_parser* parser,
21126 bool is_declaration,
21127 bool is_trailing_return,
21128 cp_decl_specifier_seq *type_specifier_seq)
21129 {
21130 bool seen_type_specifier = false;
21131 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21132 cp_token *start_token = NULL;
21133
21134 /* Clear the TYPE_SPECIFIER_SEQ. */
21135 clear_decl_specs (type_specifier_seq);
21136
21137 /* In the context of a trailing return type, enum E { } is an
21138 elaborated-type-specifier followed by a function-body, not an
21139 enum-specifier. */
21140 if (is_trailing_return)
21141 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21142
21143 /* Parse the type-specifiers and attributes. */
21144 while (true)
21145 {
21146 tree type_specifier;
21147 bool is_cv_qualifier;
21148
21149 /* Check for attributes first. */
21150 if (cp_next_tokens_can_be_attribute_p (parser))
21151 {
21152 type_specifier_seq->attributes =
21153 chainon (type_specifier_seq->attributes,
21154 cp_parser_attributes_opt (parser));
21155 continue;
21156 }
21157
21158 /* record the token of the beginning of the type specifier seq,
21159 for error reporting purposes*/
21160 if (!start_token)
21161 start_token = cp_lexer_peek_token (parser->lexer);
21162
21163 /* Look for the type-specifier. */
21164 type_specifier = cp_parser_type_specifier (parser,
21165 flags,
21166 type_specifier_seq,
21167 /*is_declaration=*/false,
21168 NULL,
21169 &is_cv_qualifier);
21170 if (!type_specifier)
21171 {
21172 /* If the first type-specifier could not be found, this is not a
21173 type-specifier-seq at all. */
21174 if (!seen_type_specifier)
21175 {
21176 /* Set in_declarator_p to avoid skipping to the semicolon. */
21177 int in_decl = parser->in_declarator_p;
21178 parser->in_declarator_p = true;
21179
21180 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21181 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21182 cp_parser_error (parser, "expected type-specifier");
21183
21184 parser->in_declarator_p = in_decl;
21185
21186 type_specifier_seq->type = error_mark_node;
21187 return;
21188 }
21189 /* If subsequent type-specifiers could not be found, the
21190 type-specifier-seq is complete. */
21191 break;
21192 }
21193
21194 seen_type_specifier = true;
21195 /* The standard says that a condition can be:
21196
21197 type-specifier-seq declarator = assignment-expression
21198
21199 However, given:
21200
21201 struct S {};
21202 if (int S = ...)
21203
21204 we should treat the "S" as a declarator, not as a
21205 type-specifier. The standard doesn't say that explicitly for
21206 type-specifier-seq, but it does say that for
21207 decl-specifier-seq in an ordinary declaration. Perhaps it
21208 would be clearer just to allow a decl-specifier-seq here, and
21209 then add a semantic restriction that if any decl-specifiers
21210 that are not type-specifiers appear, the program is invalid. */
21211 if (is_declaration && !is_cv_qualifier)
21212 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21213 }
21214 }
21215
21216 /* Return whether the function currently being declared has an associated
21217 template parameter list. */
21218
21219 static bool
21220 function_being_declared_is_template_p (cp_parser* parser)
21221 {
21222 if (!current_template_parms || processing_template_parmlist)
21223 return false;
21224
21225 if (parser->implicit_template_scope)
21226 return true;
21227
21228 if (at_class_scope_p ()
21229 && TYPE_BEING_DEFINED (current_class_type))
21230 return parser->num_template_parameter_lists != 0;
21231
21232 return ((int) parser->num_template_parameter_lists > template_class_depth
21233 (current_class_type));
21234 }
21235
21236 /* Parse a parameter-declaration-clause.
21237
21238 parameter-declaration-clause:
21239 parameter-declaration-list [opt] ... [opt]
21240 parameter-declaration-list , ...
21241
21242 Returns a representation for the parameter declarations. A return
21243 value of NULL indicates a parameter-declaration-clause consisting
21244 only of an ellipsis. */
21245
21246 static tree
21247 cp_parser_parameter_declaration_clause (cp_parser* parser)
21248 {
21249 tree parameters;
21250 cp_token *token;
21251 bool ellipsis_p;
21252 bool is_error;
21253
21254 struct cleanup {
21255 cp_parser* parser;
21256 int auto_is_implicit_function_template_parm_p;
21257 ~cleanup() {
21258 parser->auto_is_implicit_function_template_parm_p
21259 = auto_is_implicit_function_template_parm_p;
21260 }
21261 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21262
21263 (void) cleanup;
21264
21265 if (!processing_specialization
21266 && !processing_template_parmlist
21267 && !processing_explicit_instantiation)
21268 if (!current_function_decl
21269 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21270 parser->auto_is_implicit_function_template_parm_p = true;
21271
21272 /* Peek at the next token. */
21273 token = cp_lexer_peek_token (parser->lexer);
21274 /* Check for trivial parameter-declaration-clauses. */
21275 if (token->type == CPP_ELLIPSIS)
21276 {
21277 /* Consume the `...' token. */
21278 cp_lexer_consume_token (parser->lexer);
21279 return NULL_TREE;
21280 }
21281 else if (token->type == CPP_CLOSE_PAREN)
21282 /* There are no parameters. */
21283 {
21284 #ifndef NO_IMPLICIT_EXTERN_C
21285 if (in_system_header_at (input_location)
21286 && current_class_type == NULL
21287 && current_lang_name == lang_name_c)
21288 return NULL_TREE;
21289 else
21290 #endif
21291 return void_list_node;
21292 }
21293 /* Check for `(void)', too, which is a special case. */
21294 else if (token->keyword == RID_VOID
21295 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21296 == CPP_CLOSE_PAREN))
21297 {
21298 /* Consume the `void' token. */
21299 cp_lexer_consume_token (parser->lexer);
21300 /* There are no parameters. */
21301 return void_list_node;
21302 }
21303
21304 /* Parse the parameter-declaration-list. */
21305 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21306 /* If a parse error occurred while parsing the
21307 parameter-declaration-list, then the entire
21308 parameter-declaration-clause is erroneous. */
21309 if (is_error)
21310 return NULL;
21311
21312 /* Peek at the next token. */
21313 token = cp_lexer_peek_token (parser->lexer);
21314 /* If it's a `,', the clause should terminate with an ellipsis. */
21315 if (token->type == CPP_COMMA)
21316 {
21317 /* Consume the `,'. */
21318 cp_lexer_consume_token (parser->lexer);
21319 /* Expect an ellipsis. */
21320 ellipsis_p
21321 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21322 }
21323 /* It might also be `...' if the optional trailing `,' was
21324 omitted. */
21325 else if (token->type == CPP_ELLIPSIS)
21326 {
21327 /* Consume the `...' token. */
21328 cp_lexer_consume_token (parser->lexer);
21329 /* And remember that we saw it. */
21330 ellipsis_p = true;
21331 }
21332 else
21333 ellipsis_p = false;
21334
21335 /* Finish the parameter list. */
21336 if (!ellipsis_p)
21337 parameters = chainon (parameters, void_list_node);
21338
21339 return parameters;
21340 }
21341
21342 /* Parse a parameter-declaration-list.
21343
21344 parameter-declaration-list:
21345 parameter-declaration
21346 parameter-declaration-list , parameter-declaration
21347
21348 Returns a representation of the parameter-declaration-list, as for
21349 cp_parser_parameter_declaration_clause. However, the
21350 `void_list_node' is never appended to the list. Upon return,
21351 *IS_ERROR will be true iff an error occurred. */
21352
21353 static tree
21354 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21355 {
21356 tree parameters = NULL_TREE;
21357 tree *tail = &parameters;
21358 bool saved_in_unbraced_linkage_specification_p;
21359 int index = 0;
21360
21361 /* Assume all will go well. */
21362 *is_error = false;
21363 /* The special considerations that apply to a function within an
21364 unbraced linkage specifications do not apply to the parameters
21365 to the function. */
21366 saved_in_unbraced_linkage_specification_p
21367 = parser->in_unbraced_linkage_specification_p;
21368 parser->in_unbraced_linkage_specification_p = false;
21369
21370 /* Look for more parameters. */
21371 while (true)
21372 {
21373 cp_parameter_declarator *parameter;
21374 tree decl = error_mark_node;
21375 bool parenthesized_p = false;
21376 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21377 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21378 (current_template_parms)) : 0);
21379
21380 /* Parse the parameter. */
21381 parameter
21382 = cp_parser_parameter_declaration (parser,
21383 /*template_parm_p=*/false,
21384 &parenthesized_p);
21385
21386 /* We don't know yet if the enclosing context is deprecated, so wait
21387 and warn in grokparms if appropriate. */
21388 deprecated_state = DEPRECATED_SUPPRESS;
21389
21390 if (parameter)
21391 {
21392 /* If a function parameter pack was specified and an implicit template
21393 parameter was introduced during cp_parser_parameter_declaration,
21394 change any implicit parameters introduced into packs. */
21395 if (parser->implicit_template_parms
21396 && parameter->declarator
21397 && parameter->declarator->parameter_pack_p)
21398 {
21399 int latest_template_parm_idx = TREE_VEC_LENGTH
21400 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21401
21402 if (latest_template_parm_idx != template_parm_idx)
21403 parameter->decl_specifiers.type = convert_generic_types_to_packs
21404 (parameter->decl_specifiers.type,
21405 template_parm_idx, latest_template_parm_idx);
21406 }
21407
21408 decl = grokdeclarator (parameter->declarator,
21409 &parameter->decl_specifiers,
21410 PARM,
21411 parameter->default_argument != NULL_TREE,
21412 &parameter->decl_specifiers.attributes);
21413 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21414 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21415 }
21416
21417 deprecated_state = DEPRECATED_NORMAL;
21418
21419 /* If a parse error occurred parsing the parameter declaration,
21420 then the entire parameter-declaration-list is erroneous. */
21421 if (decl == error_mark_node)
21422 {
21423 *is_error = true;
21424 parameters = error_mark_node;
21425 break;
21426 }
21427
21428 if (parameter->decl_specifiers.attributes)
21429 cplus_decl_attributes (&decl,
21430 parameter->decl_specifiers.attributes,
21431 0);
21432 if (DECL_NAME (decl))
21433 decl = pushdecl (decl);
21434
21435 if (decl != error_mark_node)
21436 {
21437 retrofit_lang_decl (decl);
21438 DECL_PARM_INDEX (decl) = ++index;
21439 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21440 }
21441
21442 /* Add the new parameter to the list. */
21443 *tail = build_tree_list (parameter->default_argument, decl);
21444 tail = &TREE_CHAIN (*tail);
21445
21446 /* Peek at the next token. */
21447 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21448 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21449 /* These are for Objective-C++ */
21450 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21451 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21452 /* The parameter-declaration-list is complete. */
21453 break;
21454 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21455 {
21456 cp_token *token;
21457
21458 /* Peek at the next token. */
21459 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21460 /* If it's an ellipsis, then the list is complete. */
21461 if (token->type == CPP_ELLIPSIS)
21462 break;
21463 /* Otherwise, there must be more parameters. Consume the
21464 `,'. */
21465 cp_lexer_consume_token (parser->lexer);
21466 /* When parsing something like:
21467
21468 int i(float f, double d)
21469
21470 we can tell after seeing the declaration for "f" that we
21471 are not looking at an initialization of a variable "i",
21472 but rather at the declaration of a function "i".
21473
21474 Due to the fact that the parsing of template arguments
21475 (as specified to a template-id) requires backtracking we
21476 cannot use this technique when inside a template argument
21477 list. */
21478 if (!parser->in_template_argument_list_p
21479 && !parser->in_type_id_in_expr_p
21480 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21481 /* However, a parameter-declaration of the form
21482 "float(f)" (which is a valid declaration of a
21483 parameter "f") can also be interpreted as an
21484 expression (the conversion of "f" to "float"). */
21485 && !parenthesized_p)
21486 cp_parser_commit_to_tentative_parse (parser);
21487 }
21488 else
21489 {
21490 cp_parser_error (parser, "expected %<,%> or %<...%>");
21491 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21492 cp_parser_skip_to_closing_parenthesis (parser,
21493 /*recovering=*/true,
21494 /*or_comma=*/false,
21495 /*consume_paren=*/false);
21496 break;
21497 }
21498 }
21499
21500 parser->in_unbraced_linkage_specification_p
21501 = saved_in_unbraced_linkage_specification_p;
21502
21503 /* Reset implicit_template_scope if we are about to leave the function
21504 parameter list that introduced it. Note that for out-of-line member
21505 definitions, there will be one or more class scopes before we get to
21506 the template parameter scope. */
21507
21508 if (cp_binding_level *its = parser->implicit_template_scope)
21509 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21510 {
21511 while (maybe_its->kind == sk_class)
21512 maybe_its = maybe_its->level_chain;
21513 if (maybe_its == its)
21514 {
21515 parser->implicit_template_parms = 0;
21516 parser->implicit_template_scope = 0;
21517 }
21518 }
21519
21520 return parameters;
21521 }
21522
21523 /* Parse a parameter declaration.
21524
21525 parameter-declaration:
21526 decl-specifier-seq ... [opt] declarator
21527 decl-specifier-seq declarator = assignment-expression
21528 decl-specifier-seq ... [opt] abstract-declarator [opt]
21529 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21530
21531 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21532 declares a template parameter. (In that case, a non-nested `>'
21533 token encountered during the parsing of the assignment-expression
21534 is not interpreted as a greater-than operator.)
21535
21536 Returns a representation of the parameter, or NULL if an error
21537 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21538 true iff the declarator is of the form "(p)". */
21539
21540 static cp_parameter_declarator *
21541 cp_parser_parameter_declaration (cp_parser *parser,
21542 bool template_parm_p,
21543 bool *parenthesized_p)
21544 {
21545 int declares_class_or_enum;
21546 cp_decl_specifier_seq decl_specifiers;
21547 cp_declarator *declarator;
21548 tree default_argument;
21549 cp_token *token = NULL, *declarator_token_start = NULL;
21550 const char *saved_message;
21551 bool template_parameter_pack_p = false;
21552
21553 /* In a template parameter, `>' is not an operator.
21554
21555 [temp.param]
21556
21557 When parsing a default template-argument for a non-type
21558 template-parameter, the first non-nested `>' is taken as the end
21559 of the template parameter-list rather than a greater-than
21560 operator. */
21561
21562 /* Type definitions may not appear in parameter types. */
21563 saved_message = parser->type_definition_forbidden_message;
21564 parser->type_definition_forbidden_message
21565 = G_("types may not be defined in parameter types");
21566
21567 /* Parse the declaration-specifiers. */
21568 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21569 cp_parser_decl_specifier_seq (parser,
21570 CP_PARSER_FLAGS_NONE,
21571 &decl_specifiers,
21572 &declares_class_or_enum);
21573
21574 /* Complain about missing 'typename' or other invalid type names. */
21575 if (!decl_specifiers.any_type_specifiers_p
21576 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21577 decl_specifiers.type = error_mark_node;
21578
21579 /* If an error occurred, there's no reason to attempt to parse the
21580 rest of the declaration. */
21581 if (cp_parser_error_occurred (parser))
21582 {
21583 parser->type_definition_forbidden_message = saved_message;
21584 return NULL;
21585 }
21586
21587 /* Peek at the next token. */
21588 token = cp_lexer_peek_token (parser->lexer);
21589
21590 /* If the next token is a `)', `,', `=', `>', or `...', then there
21591 is no declarator. However, when variadic templates are enabled,
21592 there may be a declarator following `...'. */
21593 if (token->type == CPP_CLOSE_PAREN
21594 || token->type == CPP_COMMA
21595 || token->type == CPP_EQ
21596 || token->type == CPP_GREATER)
21597 {
21598 declarator = NULL;
21599 if (parenthesized_p)
21600 *parenthesized_p = false;
21601 }
21602 /* Otherwise, there should be a declarator. */
21603 else
21604 {
21605 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21606 parser->default_arg_ok_p = false;
21607
21608 /* After seeing a decl-specifier-seq, if the next token is not a
21609 "(", there is no possibility that the code is a valid
21610 expression. Therefore, if parsing tentatively, we commit at
21611 this point. */
21612 if (!parser->in_template_argument_list_p
21613 /* In an expression context, having seen:
21614
21615 (int((char ...
21616
21617 we cannot be sure whether we are looking at a
21618 function-type (taking a "char" as a parameter) or a cast
21619 of some object of type "char" to "int". */
21620 && !parser->in_type_id_in_expr_p
21621 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21622 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21623 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21624 cp_parser_commit_to_tentative_parse (parser);
21625 /* Parse the declarator. */
21626 declarator_token_start = token;
21627 declarator = cp_parser_declarator (parser,
21628 CP_PARSER_DECLARATOR_EITHER,
21629 /*ctor_dtor_or_conv_p=*/NULL,
21630 parenthesized_p,
21631 /*member_p=*/false,
21632 /*friend_p=*/false);
21633 parser->default_arg_ok_p = saved_default_arg_ok_p;
21634 /* After the declarator, allow more attributes. */
21635 decl_specifiers.attributes
21636 = chainon (decl_specifiers.attributes,
21637 cp_parser_attributes_opt (parser));
21638
21639 /* If the declarator is a template parameter pack, remember that and
21640 clear the flag in the declarator itself so we don't get errors
21641 from grokdeclarator. */
21642 if (template_parm_p && declarator && declarator->parameter_pack_p)
21643 {
21644 declarator->parameter_pack_p = false;
21645 template_parameter_pack_p = true;
21646 }
21647 }
21648
21649 /* If the next token is an ellipsis, and we have not seen a declarator
21650 name, and if either the type of the declarator contains parameter
21651 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21652 for, eg, abbreviated integral type names), then we actually have a
21653 parameter pack expansion expression. Otherwise, leave the ellipsis
21654 for a C-style variadic function. */
21655 token = cp_lexer_peek_token (parser->lexer);
21656 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21657 {
21658 tree type = decl_specifiers.type;
21659
21660 if (type && DECL_P (type))
21661 type = TREE_TYPE (type);
21662
21663 if (((type
21664 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21665 && (template_parm_p || uses_parameter_packs (type)))
21666 || (!type && template_parm_p))
21667 && declarator_can_be_parameter_pack (declarator))
21668 {
21669 /* Consume the `...'. */
21670 cp_lexer_consume_token (parser->lexer);
21671 maybe_warn_variadic_templates ();
21672
21673 /* Build a pack expansion type */
21674 if (template_parm_p)
21675 template_parameter_pack_p = true;
21676 else if (declarator)
21677 declarator->parameter_pack_p = true;
21678 else
21679 decl_specifiers.type = make_pack_expansion (type);
21680 }
21681 }
21682
21683 /* The restriction on defining new types applies only to the type
21684 of the parameter, not to the default argument. */
21685 parser->type_definition_forbidden_message = saved_message;
21686
21687 /* If the next token is `=', then process a default argument. */
21688 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21689 {
21690 tree type = decl_specifiers.type;
21691 token = cp_lexer_peek_token (parser->lexer);
21692 /* If we are defining a class, then the tokens that make up the
21693 default argument must be saved and processed later. */
21694 if (!template_parm_p && at_class_scope_p ()
21695 && TYPE_BEING_DEFINED (current_class_type)
21696 && !LAMBDA_TYPE_P (current_class_type))
21697 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21698
21699 // A constrained-type-specifier may declare a type template-parameter.
21700 else if (declares_constrained_type_template_parameter (type))
21701 default_argument
21702 = cp_parser_default_type_template_argument (parser);
21703
21704 // A constrained-type-specifier may declare a template-template-parameter.
21705 else if (declares_constrained_template_template_parameter (type))
21706 default_argument
21707 = cp_parser_default_template_template_argument (parser);
21708
21709 /* Outside of a class definition, we can just parse the
21710 assignment-expression. */
21711 else
21712 default_argument
21713 = cp_parser_default_argument (parser, template_parm_p);
21714
21715 if (!parser->default_arg_ok_p)
21716 {
21717 permerror (token->location,
21718 "default arguments are only "
21719 "permitted for function parameters");
21720 }
21721 else if ((declarator && declarator->parameter_pack_p)
21722 || template_parameter_pack_p
21723 || (decl_specifiers.type
21724 && PACK_EXPANSION_P (decl_specifiers.type)))
21725 {
21726 /* Find the name of the parameter pack. */
21727 cp_declarator *id_declarator = declarator;
21728 while (id_declarator && id_declarator->kind != cdk_id)
21729 id_declarator = id_declarator->declarator;
21730
21731 if (id_declarator && id_declarator->kind == cdk_id)
21732 error_at (declarator_token_start->location,
21733 template_parm_p
21734 ? G_("template parameter pack %qD "
21735 "cannot have a default argument")
21736 : G_("parameter pack %qD cannot have "
21737 "a default argument"),
21738 id_declarator->u.id.unqualified_name);
21739 else
21740 error_at (declarator_token_start->location,
21741 template_parm_p
21742 ? G_("template parameter pack cannot have "
21743 "a default argument")
21744 : G_("parameter pack cannot have a "
21745 "default argument"));
21746
21747 default_argument = NULL_TREE;
21748 }
21749 }
21750 else
21751 default_argument = NULL_TREE;
21752
21753 /* Generate a location for the parameter, ranging from the start of the
21754 initial token to the end of the final token (using input_location for
21755 the latter, set up by cp_lexer_set_source_position_from_token when
21756 consuming tokens).
21757
21758 If we have a identifier, then use it for the caret location, e.g.
21759
21760 extern int callee (int one, int (*two)(int, int), float three);
21761 ~~~~~~^~~~~~~~~~~~~~
21762
21763 otherwise, reuse the start location for the caret location e.g.:
21764
21765 extern int callee (int one, int (*)(int, int), float three);
21766 ^~~~~~~~~~~~~~~~~
21767
21768 */
21769 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21770 ? declarator->id_loc
21771 : decl_spec_token_start->location);
21772 location_t param_loc = make_location (caret_loc,
21773 decl_spec_token_start->location,
21774 input_location);
21775
21776 return make_parameter_declarator (&decl_specifiers,
21777 declarator,
21778 default_argument,
21779 param_loc,
21780 template_parameter_pack_p);
21781 }
21782
21783 /* Parse a default argument and return it.
21784
21785 TEMPLATE_PARM_P is true if this is a default argument for a
21786 non-type template parameter. */
21787 static tree
21788 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21789 {
21790 tree default_argument = NULL_TREE;
21791 bool saved_greater_than_is_operator_p;
21792 bool saved_local_variables_forbidden_p;
21793 bool non_constant_p, is_direct_init;
21794
21795 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21796 set correctly. */
21797 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21798 parser->greater_than_is_operator_p = !template_parm_p;
21799 /* Local variable names (and the `this' keyword) may not
21800 appear in a default argument. */
21801 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21802 parser->local_variables_forbidden_p = true;
21803 /* Parse the assignment-expression. */
21804 if (template_parm_p)
21805 push_deferring_access_checks (dk_no_deferred);
21806 tree saved_class_ptr = NULL_TREE;
21807 tree saved_class_ref = NULL_TREE;
21808 /* The "this" pointer is not valid in a default argument. */
21809 if (cfun)
21810 {
21811 saved_class_ptr = current_class_ptr;
21812 cp_function_chain->x_current_class_ptr = NULL_TREE;
21813 saved_class_ref = current_class_ref;
21814 cp_function_chain->x_current_class_ref = NULL_TREE;
21815 }
21816 default_argument
21817 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21818 /* Restore the "this" pointer. */
21819 if (cfun)
21820 {
21821 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21822 cp_function_chain->x_current_class_ref = saved_class_ref;
21823 }
21824 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21825 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21826 if (template_parm_p)
21827 pop_deferring_access_checks ();
21828 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21829 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21830
21831 return default_argument;
21832 }
21833
21834 /* Parse a function-body.
21835
21836 function-body:
21837 compound_statement */
21838
21839 static void
21840 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21841 {
21842 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21843 ? BCS_TRY_BLOCK : BCS_NORMAL),
21844 true);
21845 }
21846
21847 /* Parse a ctor-initializer-opt followed by a function-body. Return
21848 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21849 is true we are parsing a function-try-block. */
21850
21851 static void
21852 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21853 bool in_function_try_block)
21854 {
21855 tree body, list;
21856 const bool check_body_p =
21857 DECL_CONSTRUCTOR_P (current_function_decl)
21858 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21859 tree last = NULL;
21860
21861 /* Begin the function body. */
21862 body = begin_function_body ();
21863 /* Parse the optional ctor-initializer. */
21864 cp_parser_ctor_initializer_opt (parser);
21865
21866 /* If we're parsing a constexpr constructor definition, we need
21867 to check that the constructor body is indeed empty. However,
21868 before we get to cp_parser_function_body lot of junk has been
21869 generated, so we can't just check that we have an empty block.
21870 Rather we take a snapshot of the outermost block, and check whether
21871 cp_parser_function_body changed its state. */
21872 if (check_body_p)
21873 {
21874 list = cur_stmt_list;
21875 if (STATEMENT_LIST_TAIL (list))
21876 last = STATEMENT_LIST_TAIL (list)->stmt;
21877 }
21878 /* Parse the function-body. */
21879 cp_parser_function_body (parser, in_function_try_block);
21880 if (check_body_p)
21881 check_constexpr_ctor_body (last, list, /*complain=*/true);
21882 /* Finish the function body. */
21883 finish_function_body (body);
21884 }
21885
21886 /* Parse an initializer.
21887
21888 initializer:
21889 = initializer-clause
21890 ( expression-list )
21891
21892 Returns an expression representing the initializer. If no
21893 initializer is present, NULL_TREE is returned.
21894
21895 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21896 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21897 set to TRUE if there is no initializer present. If there is an
21898 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21899 is set to true; otherwise it is set to false. */
21900
21901 static tree
21902 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21903 bool* non_constant_p)
21904 {
21905 cp_token *token;
21906 tree init;
21907
21908 /* Peek at the next token. */
21909 token = cp_lexer_peek_token (parser->lexer);
21910
21911 /* Let our caller know whether or not this initializer was
21912 parenthesized. */
21913 *is_direct_init = (token->type != CPP_EQ);
21914 /* Assume that the initializer is constant. */
21915 *non_constant_p = false;
21916
21917 if (token->type == CPP_EQ)
21918 {
21919 /* Consume the `='. */
21920 cp_lexer_consume_token (parser->lexer);
21921 /* Parse the initializer-clause. */
21922 init = cp_parser_initializer_clause (parser, non_constant_p);
21923 }
21924 else if (token->type == CPP_OPEN_PAREN)
21925 {
21926 vec<tree, va_gc> *vec;
21927 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21928 /*cast_p=*/false,
21929 /*allow_expansion_p=*/true,
21930 non_constant_p);
21931 if (vec == NULL)
21932 return error_mark_node;
21933 init = build_tree_list_vec (vec);
21934 release_tree_vector (vec);
21935 }
21936 else if (token->type == CPP_OPEN_BRACE)
21937 {
21938 cp_lexer_set_source_position (parser->lexer);
21939 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21940 init = cp_parser_braced_list (parser, non_constant_p);
21941 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21942 }
21943 else
21944 {
21945 /* Anything else is an error. */
21946 cp_parser_error (parser, "expected initializer");
21947 init = error_mark_node;
21948 }
21949
21950 if (check_for_bare_parameter_packs (init))
21951 init = error_mark_node;
21952
21953 return init;
21954 }
21955
21956 /* Parse an initializer-clause.
21957
21958 initializer-clause:
21959 assignment-expression
21960 braced-init-list
21961
21962 Returns an expression representing the initializer.
21963
21964 If the `assignment-expression' production is used the value
21965 returned is simply a representation for the expression.
21966
21967 Otherwise, calls cp_parser_braced_list. */
21968
21969 static cp_expr
21970 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21971 {
21972 cp_expr initializer;
21973
21974 /* Assume the expression is constant. */
21975 *non_constant_p = false;
21976
21977 /* If it is not a `{', then we are looking at an
21978 assignment-expression. */
21979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21980 {
21981 initializer
21982 = cp_parser_constant_expression (parser,
21983 /*allow_non_constant_p=*/true,
21984 non_constant_p);
21985 }
21986 else
21987 initializer = cp_parser_braced_list (parser, non_constant_p);
21988
21989 return initializer;
21990 }
21991
21992 /* Parse a brace-enclosed initializer list.
21993
21994 braced-init-list:
21995 { initializer-list , [opt] }
21996 { designated-initializer-list , [opt] }
21997 { }
21998
21999 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22000 the elements of the initializer-list (or NULL, if the last
22001 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22002 NULL_TREE. There is no way to detect whether or not the optional
22003 trailing `,' was provided. NON_CONSTANT_P is as for
22004 cp_parser_initializer. */
22005
22006 static cp_expr
22007 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22008 {
22009 tree initializer;
22010 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22011
22012 /* Consume the `{' token. */
22013 matching_braces braces;
22014 braces.consume_open (parser);
22015 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22016 initializer = make_node (CONSTRUCTOR);
22017 /* If it's not a `}', then there is a non-trivial initializer. */
22018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22019 {
22020 /* Parse the initializer list. */
22021 CONSTRUCTOR_ELTS (initializer)
22022 = cp_parser_initializer_list (parser, non_constant_p);
22023 /* A trailing `,' token is allowed. */
22024 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22025 cp_lexer_consume_token (parser->lexer);
22026 }
22027 else
22028 *non_constant_p = false;
22029 /* Now, there should be a trailing `}'. */
22030 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22031 braces.require_close (parser);
22032 TREE_TYPE (initializer) = init_list_type_node;
22033
22034 cp_expr result (initializer);
22035 /* Build a location of the form:
22036 { ... }
22037 ^~~~~~~
22038 with caret==start at the open brace, finish at the close brace. */
22039 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22040 result.set_location (combined_loc);
22041 return result;
22042 }
22043
22044 /* Consume tokens up to, and including, the next non-nested closing `]'.
22045 Returns true iff we found a closing `]'. */
22046
22047 static bool
22048 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22049 {
22050 unsigned square_depth = 0;
22051
22052 while (true)
22053 {
22054 cp_token * token = cp_lexer_peek_token (parser->lexer);
22055
22056 switch (token->type)
22057 {
22058 case CPP_EOF:
22059 case CPP_PRAGMA_EOL:
22060 /* If we've run out of tokens, then there is no closing `]'. */
22061 return false;
22062
22063 case CPP_OPEN_SQUARE:
22064 ++square_depth;
22065 break;
22066
22067 case CPP_CLOSE_SQUARE:
22068 if (!square_depth--)
22069 {
22070 cp_lexer_consume_token (parser->lexer);
22071 return true;
22072 }
22073 break;
22074
22075 default:
22076 break;
22077 }
22078
22079 /* Consume the token. */
22080 cp_lexer_consume_token (parser->lexer);
22081 }
22082 }
22083
22084 /* Return true if we are looking at an array-designator, false otherwise. */
22085
22086 static bool
22087 cp_parser_array_designator_p (cp_parser *parser)
22088 {
22089 /* Consume the `['. */
22090 cp_lexer_consume_token (parser->lexer);
22091
22092 cp_lexer_save_tokens (parser->lexer);
22093
22094 /* Skip tokens until the next token is a closing square bracket.
22095 If we find the closing `]', and the next token is a `=', then
22096 we are looking at an array designator. */
22097 bool array_designator_p
22098 = (cp_parser_skip_to_closing_square_bracket (parser)
22099 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22100
22101 /* Roll back the tokens we skipped. */
22102 cp_lexer_rollback_tokens (parser->lexer);
22103
22104 return array_designator_p;
22105 }
22106
22107 /* Parse an initializer-list.
22108
22109 initializer-list:
22110 initializer-clause ... [opt]
22111 initializer-list , initializer-clause ... [opt]
22112
22113 C++2A Extension:
22114
22115 designated-initializer-list:
22116 designated-initializer-clause
22117 designated-initializer-list , designated-initializer-clause
22118
22119 designated-initializer-clause:
22120 designator brace-or-equal-initializer
22121
22122 designator:
22123 . identifier
22124
22125 GNU Extension:
22126
22127 initializer-list:
22128 designation initializer-clause ...[opt]
22129 initializer-list , designation initializer-clause ...[opt]
22130
22131 designation:
22132 . identifier =
22133 identifier :
22134 [ constant-expression ] =
22135
22136 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22137 for the initializer. If the INDEX of the elt is non-NULL, it is the
22138 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22139 as for cp_parser_initializer. */
22140
22141 static vec<constructor_elt, va_gc> *
22142 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22143 {
22144 vec<constructor_elt, va_gc> *v = NULL;
22145 bool first_p = true;
22146 tree first_designator = NULL_TREE;
22147
22148 /* Assume all of the expressions are constant. */
22149 *non_constant_p = false;
22150
22151 /* Parse the rest of the list. */
22152 while (true)
22153 {
22154 cp_token *token;
22155 tree designator;
22156 tree initializer;
22157 bool clause_non_constant_p;
22158 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22159
22160 /* Handle the C++2A syntax, '. id ='. */
22161 if ((cxx_dialect >= cxx2a
22162 || cp_parser_allow_gnu_extensions_p (parser))
22163 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22164 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22165 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22166 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22167 == CPP_OPEN_BRACE)))
22168 {
22169 if (cxx_dialect < cxx2a)
22170 pedwarn (loc, OPT_Wpedantic,
22171 "C++ designated initializers only available with "
22172 "-std=c++2a or -std=gnu++2a");
22173 /* Consume the `.'. */
22174 cp_lexer_consume_token (parser->lexer);
22175 /* Consume the identifier. */
22176 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22177 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22178 /* Consume the `='. */
22179 cp_lexer_consume_token (parser->lexer);
22180 }
22181 /* Also, if the next token is an identifier and the following one is a
22182 colon, we are looking at the GNU designated-initializer
22183 syntax. */
22184 else if (cp_parser_allow_gnu_extensions_p (parser)
22185 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22186 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22187 == CPP_COLON))
22188 {
22189 /* Warn the user that they are using an extension. */
22190 pedwarn (loc, OPT_Wpedantic,
22191 "ISO C++ does not allow GNU designated initializers");
22192 /* Consume the identifier. */
22193 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22194 /* Consume the `:'. */
22195 cp_lexer_consume_token (parser->lexer);
22196 }
22197 /* Also handle C99 array designators, '[ const ] ='. */
22198 else if (cp_parser_allow_gnu_extensions_p (parser)
22199 && !c_dialect_objc ()
22200 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22201 {
22202 /* In C++11, [ could start a lambda-introducer. */
22203 bool non_const = false;
22204
22205 cp_parser_parse_tentatively (parser);
22206
22207 if (!cp_parser_array_designator_p (parser))
22208 {
22209 cp_parser_simulate_error (parser);
22210 designator = NULL_TREE;
22211 }
22212 else
22213 {
22214 designator = cp_parser_constant_expression (parser, true,
22215 &non_const);
22216 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22217 cp_parser_require (parser, CPP_EQ, RT_EQ);
22218 }
22219
22220 if (!cp_parser_parse_definitely (parser))
22221 designator = NULL_TREE;
22222 else if (non_const)
22223 require_potential_rvalue_constant_expression (designator);
22224 if (designator)
22225 /* Warn the user that they are using an extension. */
22226 pedwarn (loc, OPT_Wpedantic,
22227 "ISO C++ does not allow C99 designated initializers");
22228 }
22229 else
22230 designator = NULL_TREE;
22231
22232 if (first_p)
22233 {
22234 first_designator = designator;
22235 first_p = false;
22236 }
22237 else if (cxx_dialect >= cxx2a
22238 && first_designator != error_mark_node
22239 && (!first_designator != !designator))
22240 {
22241 error_at (loc, "either all initializer clauses should be designated "
22242 "or none of them should be");
22243 first_designator = error_mark_node;
22244 }
22245 else if (cxx_dialect < cxx2a && !first_designator)
22246 first_designator = designator;
22247
22248 /* Parse the initializer. */
22249 initializer = cp_parser_initializer_clause (parser,
22250 &clause_non_constant_p);
22251 /* If any clause is non-constant, so is the entire initializer. */
22252 if (clause_non_constant_p)
22253 *non_constant_p = true;
22254
22255 /* If we have an ellipsis, this is an initializer pack
22256 expansion. */
22257 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22258 {
22259 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22260
22261 /* Consume the `...'. */
22262 cp_lexer_consume_token (parser->lexer);
22263
22264 if (designator && cxx_dialect >= cxx2a)
22265 error_at (loc,
22266 "%<...%> not allowed in designated initializer list");
22267
22268 /* Turn the initializer into an initializer expansion. */
22269 initializer = make_pack_expansion (initializer);
22270 }
22271
22272 /* Add it to the vector. */
22273 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22274
22275 /* If the next token is not a comma, we have reached the end of
22276 the list. */
22277 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22278 break;
22279
22280 /* Peek at the next token. */
22281 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22282 /* If the next token is a `}', then we're still done. An
22283 initializer-clause can have a trailing `,' after the
22284 initializer-list and before the closing `}'. */
22285 if (token->type == CPP_CLOSE_BRACE)
22286 break;
22287
22288 /* Consume the `,' token. */
22289 cp_lexer_consume_token (parser->lexer);
22290 }
22291
22292 /* The same identifier shall not appear in multiple designators
22293 of a designated-initializer-list. */
22294 if (first_designator)
22295 {
22296 unsigned int i;
22297 tree designator, val;
22298 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22299 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22300 {
22301 if (IDENTIFIER_MARKED (designator))
22302 {
22303 error_at (EXPR_LOC_OR_LOC (val, input_location),
22304 "%<.%s%> designator used multiple times in "
22305 "the same initializer list",
22306 IDENTIFIER_POINTER (designator));
22307 (*v)[i].index = NULL_TREE;
22308 }
22309 else
22310 IDENTIFIER_MARKED (designator) = 1;
22311 }
22312 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22313 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22314 IDENTIFIER_MARKED (designator) = 0;
22315 }
22316
22317 return v;
22318 }
22319
22320 /* Classes [gram.class] */
22321
22322 /* Parse a class-name.
22323
22324 class-name:
22325 identifier
22326 template-id
22327
22328 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22329 to indicate that names looked up in dependent types should be
22330 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22331 keyword has been used to indicate that the name that appears next
22332 is a template. TAG_TYPE indicates the explicit tag given before
22333 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22334 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22335 is the class being defined in a class-head. If ENUM_OK is TRUE,
22336 enum-names are also accepted.
22337
22338 Returns the TYPE_DECL representing the class. */
22339
22340 static tree
22341 cp_parser_class_name (cp_parser *parser,
22342 bool typename_keyword_p,
22343 bool template_keyword_p,
22344 enum tag_types tag_type,
22345 bool check_dependency_p,
22346 bool class_head_p,
22347 bool is_declaration,
22348 bool enum_ok)
22349 {
22350 tree decl;
22351 tree scope;
22352 bool typename_p;
22353 cp_token *token;
22354 tree identifier = NULL_TREE;
22355
22356 /* All class-names start with an identifier. */
22357 token = cp_lexer_peek_token (parser->lexer);
22358 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22359 {
22360 cp_parser_error (parser, "expected class-name");
22361 return error_mark_node;
22362 }
22363
22364 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22365 to a template-id, so we save it here. */
22366 scope = parser->scope;
22367 if (scope == error_mark_node)
22368 return error_mark_node;
22369
22370 /* Any name names a type if we're following the `typename' keyword
22371 in a qualified name where the enclosing scope is type-dependent. */
22372 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22373 && dependent_type_p (scope));
22374 /* Handle the common case (an identifier, but not a template-id)
22375 efficiently. */
22376 if (token->type == CPP_NAME
22377 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22378 {
22379 cp_token *identifier_token;
22380 bool ambiguous_p;
22381
22382 /* Look for the identifier. */
22383 identifier_token = cp_lexer_peek_token (parser->lexer);
22384 ambiguous_p = identifier_token->error_reported;
22385 identifier = cp_parser_identifier (parser);
22386 /* If the next token isn't an identifier, we are certainly not
22387 looking at a class-name. */
22388 if (identifier == error_mark_node)
22389 decl = error_mark_node;
22390 /* If we know this is a type-name, there's no need to look it
22391 up. */
22392 else if (typename_p)
22393 decl = identifier;
22394 else
22395 {
22396 tree ambiguous_decls;
22397 /* If we already know that this lookup is ambiguous, then
22398 we've already issued an error message; there's no reason
22399 to check again. */
22400 if (ambiguous_p)
22401 {
22402 cp_parser_simulate_error (parser);
22403 return error_mark_node;
22404 }
22405 /* If the next token is a `::', then the name must be a type
22406 name.
22407
22408 [basic.lookup.qual]
22409
22410 During the lookup for a name preceding the :: scope
22411 resolution operator, object, function, and enumerator
22412 names are ignored. */
22413 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22414 tag_type = scope_type;
22415 /* Look up the name. */
22416 decl = cp_parser_lookup_name (parser, identifier,
22417 tag_type,
22418 /*is_template=*/false,
22419 /*is_namespace=*/false,
22420 check_dependency_p,
22421 &ambiguous_decls,
22422 identifier_token->location);
22423 if (ambiguous_decls)
22424 {
22425 if (cp_parser_parsing_tentatively (parser))
22426 cp_parser_simulate_error (parser);
22427 return error_mark_node;
22428 }
22429 }
22430 }
22431 else
22432 {
22433 /* Try a template-id. */
22434 decl = cp_parser_template_id (parser, template_keyword_p,
22435 check_dependency_p,
22436 tag_type,
22437 is_declaration);
22438 if (decl == error_mark_node)
22439 return error_mark_node;
22440 }
22441
22442 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22443
22444 /* If this is a typename, create a TYPENAME_TYPE. */
22445 if (typename_p && decl != error_mark_node)
22446 {
22447 decl = make_typename_type (scope, decl, typename_type,
22448 /*complain=*/tf_error);
22449 if (decl != error_mark_node)
22450 decl = TYPE_NAME (decl);
22451 }
22452
22453 decl = strip_using_decl (decl);
22454
22455 /* Check to see that it is really the name of a class. */
22456 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22457 && identifier_p (TREE_OPERAND (decl, 0))
22458 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22459 /* Situations like this:
22460
22461 template <typename T> struct A {
22462 typename T::template X<int>::I i;
22463 };
22464
22465 are problematic. Is `T::template X<int>' a class-name? The
22466 standard does not seem to be definitive, but there is no other
22467 valid interpretation of the following `::'. Therefore, those
22468 names are considered class-names. */
22469 {
22470 decl = make_typename_type (scope, decl, tag_type, tf_error);
22471 if (decl != error_mark_node)
22472 decl = TYPE_NAME (decl);
22473 }
22474 else if (TREE_CODE (decl) != TYPE_DECL
22475 || TREE_TYPE (decl) == error_mark_node
22476 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22477 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22478 /* In Objective-C 2.0, a classname followed by '.' starts a
22479 dot-syntax expression, and it's not a type-name. */
22480 || (c_dialect_objc ()
22481 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22482 && objc_is_class_name (decl)))
22483 decl = error_mark_node;
22484
22485 if (decl == error_mark_node)
22486 cp_parser_error (parser, "expected class-name");
22487 else if (identifier && !parser->scope)
22488 maybe_note_name_used_in_class (identifier, decl);
22489
22490 return decl;
22491 }
22492
22493 /* Parse a class-specifier.
22494
22495 class-specifier:
22496 class-head { member-specification [opt] }
22497
22498 Returns the TREE_TYPE representing the class. */
22499
22500 static tree
22501 cp_parser_class_specifier_1 (cp_parser* parser)
22502 {
22503 tree type;
22504 tree attributes = NULL_TREE;
22505 bool nested_name_specifier_p;
22506 unsigned saved_num_template_parameter_lists;
22507 bool saved_in_function_body;
22508 unsigned char in_statement;
22509 bool in_switch_statement_p;
22510 bool saved_in_unbraced_linkage_specification_p;
22511 tree old_scope = NULL_TREE;
22512 tree scope = NULL_TREE;
22513 cp_token *closing_brace;
22514
22515 push_deferring_access_checks (dk_no_deferred);
22516
22517 /* Parse the class-head. */
22518 type = cp_parser_class_head (parser,
22519 &nested_name_specifier_p);
22520 /* If the class-head was a semantic disaster, skip the entire body
22521 of the class. */
22522 if (!type)
22523 {
22524 cp_parser_skip_to_end_of_block_or_statement (parser);
22525 pop_deferring_access_checks ();
22526 return error_mark_node;
22527 }
22528
22529 /* Look for the `{'. */
22530 matching_braces braces;
22531 if (!braces.require_open (parser))
22532 {
22533 pop_deferring_access_checks ();
22534 return error_mark_node;
22535 }
22536
22537 cp_ensure_no_omp_declare_simd (parser);
22538 cp_ensure_no_oacc_routine (parser);
22539
22540 /* Issue an error message if type-definitions are forbidden here. */
22541 cp_parser_check_type_definition (parser);
22542 /* Remember that we are defining one more class. */
22543 ++parser->num_classes_being_defined;
22544 /* Inside the class, surrounding template-parameter-lists do not
22545 apply. */
22546 saved_num_template_parameter_lists
22547 = parser->num_template_parameter_lists;
22548 parser->num_template_parameter_lists = 0;
22549 /* We are not in a function body. */
22550 saved_in_function_body = parser->in_function_body;
22551 parser->in_function_body = false;
22552 /* Or in a loop. */
22553 in_statement = parser->in_statement;
22554 parser->in_statement = 0;
22555 /* Or in a switch. */
22556 in_switch_statement_p = parser->in_switch_statement_p;
22557 parser->in_switch_statement_p = false;
22558 /* We are not immediately inside an extern "lang" block. */
22559 saved_in_unbraced_linkage_specification_p
22560 = parser->in_unbraced_linkage_specification_p;
22561 parser->in_unbraced_linkage_specification_p = false;
22562
22563 // Associate constraints with the type.
22564 if (flag_concepts)
22565 type = associate_classtype_constraints (type);
22566
22567 /* Start the class. */
22568 if (nested_name_specifier_p)
22569 {
22570 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22571 old_scope = push_inner_scope (scope);
22572 }
22573 type = begin_class_definition (type);
22574
22575 if (type == error_mark_node)
22576 /* If the type is erroneous, skip the entire body of the class. */
22577 cp_parser_skip_to_closing_brace (parser);
22578 else
22579 /* Parse the member-specification. */
22580 cp_parser_member_specification_opt (parser);
22581
22582 /* Look for the trailing `}'. */
22583 closing_brace = braces.require_close (parser);
22584 /* Look for trailing attributes to apply to this class. */
22585 if (cp_parser_allow_gnu_extensions_p (parser))
22586 attributes = cp_parser_gnu_attributes_opt (parser);
22587 if (type != error_mark_node)
22588 type = finish_struct (type, attributes);
22589 if (nested_name_specifier_p)
22590 pop_inner_scope (old_scope, scope);
22591
22592 /* We've finished a type definition. Check for the common syntax
22593 error of forgetting a semicolon after the definition. We need to
22594 be careful, as we can't just check for not-a-semicolon and be done
22595 with it; the user might have typed:
22596
22597 class X { } c = ...;
22598 class X { } *p = ...;
22599
22600 and so forth. Instead, enumerate all the possible tokens that
22601 might follow this production; if we don't see one of them, then
22602 complain and silently insert the semicolon. */
22603 {
22604 cp_token *token = cp_lexer_peek_token (parser->lexer);
22605 bool want_semicolon = true;
22606
22607 if (cp_next_tokens_can_be_std_attribute_p (parser))
22608 /* Don't try to parse c++11 attributes here. As per the
22609 grammar, that should be a task for
22610 cp_parser_decl_specifier_seq. */
22611 want_semicolon = false;
22612
22613 switch (token->type)
22614 {
22615 case CPP_NAME:
22616 case CPP_SEMICOLON:
22617 case CPP_MULT:
22618 case CPP_AND:
22619 case CPP_OPEN_PAREN:
22620 case CPP_CLOSE_PAREN:
22621 case CPP_COMMA:
22622 want_semicolon = false;
22623 break;
22624
22625 /* While it's legal for type qualifiers and storage class
22626 specifiers to follow type definitions in the grammar, only
22627 compiler testsuites contain code like that. Assume that if
22628 we see such code, then what we're really seeing is a case
22629 like:
22630
22631 class X { }
22632 const <type> var = ...;
22633
22634 or
22635
22636 class Y { }
22637 static <type> func (...) ...
22638
22639 i.e. the qualifier or specifier applies to the next
22640 declaration. To do so, however, we need to look ahead one
22641 more token to see if *that* token is a type specifier.
22642
22643 This code could be improved to handle:
22644
22645 class Z { }
22646 static const <type> var = ...; */
22647 case CPP_KEYWORD:
22648 if (keyword_is_decl_specifier (token->keyword))
22649 {
22650 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22651
22652 /* Handling user-defined types here would be nice, but very
22653 tricky. */
22654 want_semicolon
22655 = (lookahead->type == CPP_KEYWORD
22656 && keyword_begins_type_specifier (lookahead->keyword));
22657 }
22658 break;
22659 default:
22660 break;
22661 }
22662
22663 /* If we don't have a type, then something is very wrong and we
22664 shouldn't try to do anything clever. Likewise for not seeing the
22665 closing brace. */
22666 if (closing_brace && TYPE_P (type) && want_semicolon)
22667 {
22668 /* Locate the closing brace. */
22669 cp_token_position prev
22670 = cp_lexer_previous_token_position (parser->lexer);
22671 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22672 location_t loc = prev_token->location;
22673
22674 /* We want to suggest insertion of a ';' immediately *after* the
22675 closing brace, so, if we can, offset the location by 1 column. */
22676 location_t next_loc = loc;
22677 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22678 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22679
22680 rich_location richloc (line_table, next_loc);
22681
22682 /* If we successfully offset the location, suggest the fix-it. */
22683 if (next_loc != loc)
22684 richloc.add_fixit_insert_before (next_loc, ";");
22685
22686 if (CLASSTYPE_DECLARED_CLASS (type))
22687 error_at (&richloc,
22688 "expected %<;%> after class definition");
22689 else if (TREE_CODE (type) == RECORD_TYPE)
22690 error_at (&richloc,
22691 "expected %<;%> after struct definition");
22692 else if (TREE_CODE (type) == UNION_TYPE)
22693 error_at (&richloc,
22694 "expected %<;%> after union definition");
22695 else
22696 gcc_unreachable ();
22697
22698 /* Unget one token and smash it to look as though we encountered
22699 a semicolon in the input stream. */
22700 cp_lexer_set_token_position (parser->lexer, prev);
22701 token = cp_lexer_peek_token (parser->lexer);
22702 token->type = CPP_SEMICOLON;
22703 token->keyword = RID_MAX;
22704 }
22705 }
22706
22707 /* If this class is not itself within the scope of another class,
22708 then we need to parse the bodies of all of the queued function
22709 definitions. Note that the queued functions defined in a class
22710 are not always processed immediately following the
22711 class-specifier for that class. Consider:
22712
22713 struct A {
22714 struct B { void f() { sizeof (A); } };
22715 };
22716
22717 If `f' were processed before the processing of `A' were
22718 completed, there would be no way to compute the size of `A'.
22719 Note that the nesting we are interested in here is lexical --
22720 not the semantic nesting given by TYPE_CONTEXT. In particular,
22721 for:
22722
22723 struct A { struct B; };
22724 struct A::B { void f() { } };
22725
22726 there is no need to delay the parsing of `A::B::f'. */
22727 if (--parser->num_classes_being_defined == 0)
22728 {
22729 tree decl;
22730 tree class_type = NULL_TREE;
22731 tree pushed_scope = NULL_TREE;
22732 unsigned ix;
22733 cp_default_arg_entry *e;
22734 tree save_ccp, save_ccr;
22735
22736 /* In a first pass, parse default arguments to the functions.
22737 Then, in a second pass, parse the bodies of the functions.
22738 This two-phased approach handles cases like:
22739
22740 struct S {
22741 void f() { g(); }
22742 void g(int i = 3);
22743 };
22744
22745 */
22746 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22747 {
22748 decl = e->decl;
22749 /* If there are default arguments that have not yet been processed,
22750 take care of them now. */
22751 if (class_type != e->class_type)
22752 {
22753 if (pushed_scope)
22754 pop_scope (pushed_scope);
22755 class_type = e->class_type;
22756 pushed_scope = push_scope (class_type);
22757 }
22758 /* Make sure that any template parameters are in scope. */
22759 maybe_begin_member_template_processing (decl);
22760 /* Parse the default argument expressions. */
22761 cp_parser_late_parsing_default_args (parser, decl);
22762 /* Remove any template parameters from the symbol table. */
22763 maybe_end_member_template_processing ();
22764 }
22765 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22766 /* Now parse any NSDMIs. */
22767 save_ccp = current_class_ptr;
22768 save_ccr = current_class_ref;
22769 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22770 {
22771 if (class_type != DECL_CONTEXT (decl))
22772 {
22773 if (pushed_scope)
22774 pop_scope (pushed_scope);
22775 class_type = DECL_CONTEXT (decl);
22776 pushed_scope = push_scope (class_type);
22777 }
22778 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22779 cp_parser_late_parsing_nsdmi (parser, decl);
22780 }
22781 vec_safe_truncate (unparsed_nsdmis, 0);
22782 current_class_ptr = save_ccp;
22783 current_class_ref = save_ccr;
22784 if (pushed_scope)
22785 pop_scope (pushed_scope);
22786
22787 /* Now do some post-NSDMI bookkeeping. */
22788 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22789 after_nsdmi_defaulted_late_checks (class_type);
22790 vec_safe_truncate (unparsed_classes, 0);
22791 after_nsdmi_defaulted_late_checks (type);
22792
22793 /* Now parse the body of the functions. */
22794 if (flag_openmp)
22795 {
22796 /* OpenMP UDRs need to be parsed before all other functions. */
22797 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22798 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22799 cp_parser_late_parsing_for_member (parser, decl);
22800 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22801 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22802 cp_parser_late_parsing_for_member (parser, decl);
22803 }
22804 else
22805 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22806 cp_parser_late_parsing_for_member (parser, decl);
22807 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22808 }
22809 else
22810 vec_safe_push (unparsed_classes, type);
22811
22812 /* Put back any saved access checks. */
22813 pop_deferring_access_checks ();
22814
22815 /* Restore saved state. */
22816 parser->in_switch_statement_p = in_switch_statement_p;
22817 parser->in_statement = in_statement;
22818 parser->in_function_body = saved_in_function_body;
22819 parser->num_template_parameter_lists
22820 = saved_num_template_parameter_lists;
22821 parser->in_unbraced_linkage_specification_p
22822 = saved_in_unbraced_linkage_specification_p;
22823
22824 return type;
22825 }
22826
22827 static tree
22828 cp_parser_class_specifier (cp_parser* parser)
22829 {
22830 tree ret;
22831 timevar_push (TV_PARSE_STRUCT);
22832 ret = cp_parser_class_specifier_1 (parser);
22833 timevar_pop (TV_PARSE_STRUCT);
22834 return ret;
22835 }
22836
22837 /* Parse a class-head.
22838
22839 class-head:
22840 class-key identifier [opt] base-clause [opt]
22841 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22842 class-key nested-name-specifier [opt] template-id
22843 base-clause [opt]
22844
22845 class-virt-specifier:
22846 final
22847
22848 GNU Extensions:
22849 class-key attributes identifier [opt] base-clause [opt]
22850 class-key attributes nested-name-specifier identifier base-clause [opt]
22851 class-key attributes nested-name-specifier [opt] template-id
22852 base-clause [opt]
22853
22854 Upon return BASES is initialized to the list of base classes (or
22855 NULL, if there are none) in the same form returned by
22856 cp_parser_base_clause.
22857
22858 Returns the TYPE of the indicated class. Sets
22859 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22860 involving a nested-name-specifier was used, and FALSE otherwise.
22861
22862 Returns error_mark_node if this is not a class-head.
22863
22864 Returns NULL_TREE if the class-head is syntactically valid, but
22865 semantically invalid in a way that means we should skip the entire
22866 body of the class. */
22867
22868 static tree
22869 cp_parser_class_head (cp_parser* parser,
22870 bool* nested_name_specifier_p)
22871 {
22872 tree nested_name_specifier;
22873 enum tag_types class_key;
22874 tree id = NULL_TREE;
22875 tree type = NULL_TREE;
22876 tree attributes;
22877 tree bases;
22878 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22879 bool template_id_p = false;
22880 bool qualified_p = false;
22881 bool invalid_nested_name_p = false;
22882 bool invalid_explicit_specialization_p = false;
22883 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22884 tree pushed_scope = NULL_TREE;
22885 unsigned num_templates;
22886 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22887 /* Assume no nested-name-specifier will be present. */
22888 *nested_name_specifier_p = false;
22889 /* Assume no template parameter lists will be used in defining the
22890 type. */
22891 num_templates = 0;
22892 parser->colon_corrects_to_scope_p = false;
22893
22894 /* Look for the class-key. */
22895 class_key = cp_parser_class_key (parser);
22896 if (class_key == none_type)
22897 return error_mark_node;
22898
22899 location_t class_head_start_location = input_location;
22900
22901 /* Parse the attributes. */
22902 attributes = cp_parser_attributes_opt (parser);
22903
22904 /* If the next token is `::', that is invalid -- but sometimes
22905 people do try to write:
22906
22907 struct ::S {};
22908
22909 Handle this gracefully by accepting the extra qualifier, and then
22910 issuing an error about it later if this really is a
22911 class-head. If it turns out just to be an elaborated type
22912 specifier, remain silent. */
22913 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22914 qualified_p = true;
22915
22916 push_deferring_access_checks (dk_no_check);
22917
22918 /* Determine the name of the class. Begin by looking for an
22919 optional nested-name-specifier. */
22920 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22921 nested_name_specifier
22922 = cp_parser_nested_name_specifier_opt (parser,
22923 /*typename_keyword_p=*/false,
22924 /*check_dependency_p=*/false,
22925 /*type_p=*/true,
22926 /*is_declaration=*/false);
22927 /* If there was a nested-name-specifier, then there *must* be an
22928 identifier. */
22929
22930 cp_token *bad_template_keyword = NULL;
22931
22932 if (nested_name_specifier)
22933 {
22934 type_start_token = cp_lexer_peek_token (parser->lexer);
22935 /* Although the grammar says `identifier', it really means
22936 `class-name' or `template-name'. You are only allowed to
22937 define a class that has already been declared with this
22938 syntax.
22939
22940 The proposed resolution for Core Issue 180 says that wherever
22941 you see `class T::X' you should treat `X' as a type-name.
22942
22943 It is OK to define an inaccessible class; for example:
22944
22945 class A { class B; };
22946 class A::B {};
22947
22948 We do not know if we will see a class-name, or a
22949 template-name. We look for a class-name first, in case the
22950 class-name is a template-id; if we looked for the
22951 template-name first we would stop after the template-name. */
22952 cp_parser_parse_tentatively (parser);
22953 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22954 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22955 type = cp_parser_class_name (parser,
22956 /*typename_keyword_p=*/false,
22957 /*template_keyword_p=*/false,
22958 class_type,
22959 /*check_dependency_p=*/false,
22960 /*class_head_p=*/true,
22961 /*is_declaration=*/false);
22962 /* If that didn't work, ignore the nested-name-specifier. */
22963 if (!cp_parser_parse_definitely (parser))
22964 {
22965 invalid_nested_name_p = true;
22966 type_start_token = cp_lexer_peek_token (parser->lexer);
22967 id = cp_parser_identifier (parser);
22968 if (id == error_mark_node)
22969 id = NULL_TREE;
22970 }
22971 /* If we could not find a corresponding TYPE, treat this
22972 declaration like an unqualified declaration. */
22973 if (type == error_mark_node)
22974 nested_name_specifier = NULL_TREE;
22975 /* Otherwise, count the number of templates used in TYPE and its
22976 containing scopes. */
22977 else
22978 {
22979 tree scope;
22980
22981 for (scope = TREE_TYPE (type);
22982 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22983 scope = get_containing_scope (scope))
22984 if (TYPE_P (scope)
22985 && CLASS_TYPE_P (scope)
22986 && CLASSTYPE_TEMPLATE_INFO (scope)
22987 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22988 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22989 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22990 ++num_templates;
22991 }
22992 }
22993 /* Otherwise, the identifier is optional. */
22994 else
22995 {
22996 /* We don't know whether what comes next is a template-id,
22997 an identifier, or nothing at all. */
22998 cp_parser_parse_tentatively (parser);
22999 /* Check for a template-id. */
23000 type_start_token = cp_lexer_peek_token (parser->lexer);
23001 id = cp_parser_template_id (parser,
23002 /*template_keyword_p=*/false,
23003 /*check_dependency_p=*/true,
23004 class_key,
23005 /*is_declaration=*/true);
23006 /* If that didn't work, it could still be an identifier. */
23007 if (!cp_parser_parse_definitely (parser))
23008 {
23009 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23010 {
23011 type_start_token = cp_lexer_peek_token (parser->lexer);
23012 id = cp_parser_identifier (parser);
23013 }
23014 else
23015 id = NULL_TREE;
23016 }
23017 else
23018 {
23019 template_id_p = true;
23020 ++num_templates;
23021 }
23022 }
23023
23024 pop_deferring_access_checks ();
23025
23026 if (id)
23027 {
23028 cp_parser_check_for_invalid_template_id (parser, id,
23029 class_key,
23030 type_start_token->location);
23031 }
23032 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23033
23034 /* If it's not a `:' or a `{' then we can't really be looking at a
23035 class-head, since a class-head only appears as part of a
23036 class-specifier. We have to detect this situation before calling
23037 xref_tag, since that has irreversible side-effects. */
23038 if (!cp_parser_next_token_starts_class_definition_p (parser))
23039 {
23040 cp_parser_error (parser, "expected %<{%> or %<:%>");
23041 type = error_mark_node;
23042 goto out;
23043 }
23044
23045 /* At this point, we're going ahead with the class-specifier, even
23046 if some other problem occurs. */
23047 cp_parser_commit_to_tentative_parse (parser);
23048 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23049 {
23050 cp_parser_error (parser,
23051 "cannot specify %<override%> for a class");
23052 type = error_mark_node;
23053 goto out;
23054 }
23055 /* Issue the error about the overly-qualified name now. */
23056 if (qualified_p)
23057 {
23058 cp_parser_error (parser,
23059 "global qualification of class name is invalid");
23060 type = error_mark_node;
23061 goto out;
23062 }
23063 else if (invalid_nested_name_p)
23064 {
23065 cp_parser_error (parser,
23066 "qualified name does not name a class");
23067 type = error_mark_node;
23068 goto out;
23069 }
23070 else if (nested_name_specifier)
23071 {
23072 tree scope;
23073
23074 if (bad_template_keyword)
23075 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23076 keyword template shall not appear at the top level. */
23077 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23078 "keyword %<template%> not allowed in class-head-name");
23079
23080 /* Reject typedef-names in class heads. */
23081 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23082 {
23083 error_at (type_start_token->location,
23084 "invalid class name in declaration of %qD",
23085 type);
23086 type = NULL_TREE;
23087 goto done;
23088 }
23089
23090 /* Figure out in what scope the declaration is being placed. */
23091 scope = current_scope ();
23092 /* If that scope does not contain the scope in which the
23093 class was originally declared, the program is invalid. */
23094 if (scope && !is_ancestor (scope, nested_name_specifier))
23095 {
23096 if (at_namespace_scope_p ())
23097 error_at (type_start_token->location,
23098 "declaration of %qD in namespace %qD which does not "
23099 "enclose %qD",
23100 type, scope, nested_name_specifier);
23101 else
23102 error_at (type_start_token->location,
23103 "declaration of %qD in %qD which does not enclose %qD",
23104 type, scope, nested_name_specifier);
23105 type = NULL_TREE;
23106 goto done;
23107 }
23108 /* [dcl.meaning]
23109
23110 A declarator-id shall not be qualified except for the
23111 definition of a ... nested class outside of its class
23112 ... [or] the definition or explicit instantiation of a
23113 class member of a namespace outside of its namespace. */
23114 if (scope == nested_name_specifier)
23115 {
23116 permerror (nested_name_specifier_token_start->location,
23117 "extra qualification not allowed");
23118 nested_name_specifier = NULL_TREE;
23119 num_templates = 0;
23120 }
23121 }
23122 /* An explicit-specialization must be preceded by "template <>". If
23123 it is not, try to recover gracefully. */
23124 if (at_namespace_scope_p ()
23125 && parser->num_template_parameter_lists == 0
23126 && !processing_template_parmlist
23127 && template_id_p)
23128 {
23129 /* Build a location of this form:
23130 struct typename <ARGS>
23131 ^~~~~~~~~~~~~~~~~~~~~~
23132 with caret==start at the start token, and
23133 finishing at the end of the type. */
23134 location_t reported_loc
23135 = make_location (class_head_start_location,
23136 class_head_start_location,
23137 get_finish (type_start_token->location));
23138 rich_location richloc (line_table, reported_loc);
23139 richloc.add_fixit_insert_before (class_head_start_location,
23140 "template <> ");
23141 error_at (&richloc,
23142 "an explicit specialization must be preceded by"
23143 " %<template <>%>");
23144 invalid_explicit_specialization_p = true;
23145 /* Take the same action that would have been taken by
23146 cp_parser_explicit_specialization. */
23147 ++parser->num_template_parameter_lists;
23148 begin_specialization ();
23149 }
23150 /* There must be no "return" statements between this point and the
23151 end of this function; set "type "to the correct return value and
23152 use "goto done;" to return. */
23153 /* Make sure that the right number of template parameters were
23154 present. */
23155 if (!cp_parser_check_template_parameters (parser, num_templates,
23156 type_start_token->location,
23157 /*declarator=*/NULL))
23158 {
23159 /* If something went wrong, there is no point in even trying to
23160 process the class-definition. */
23161 type = NULL_TREE;
23162 goto done;
23163 }
23164
23165 /* Look up the type. */
23166 if (template_id_p)
23167 {
23168 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23169 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23170 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23171 {
23172 error_at (type_start_token->location,
23173 "function template %qD redeclared as a class template", id);
23174 type = error_mark_node;
23175 }
23176 else
23177 {
23178 type = TREE_TYPE (id);
23179 type = maybe_process_partial_specialization (type);
23180
23181 /* Check the scope while we still know whether or not we had a
23182 nested-name-specifier. */
23183 if (type != error_mark_node)
23184 check_unqualified_spec_or_inst (type, type_start_token->location);
23185 }
23186 if (nested_name_specifier)
23187 pushed_scope = push_scope (nested_name_specifier);
23188 }
23189 else if (nested_name_specifier)
23190 {
23191 tree class_type;
23192
23193 /* Given:
23194
23195 template <typename T> struct S { struct T };
23196 template <typename T> struct S<T>::T { };
23197
23198 we will get a TYPENAME_TYPE when processing the definition of
23199 `S::T'. We need to resolve it to the actual type before we
23200 try to define it. */
23201 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23202 {
23203 class_type = resolve_typename_type (TREE_TYPE (type),
23204 /*only_current_p=*/false);
23205 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23206 type = TYPE_NAME (class_type);
23207 else
23208 {
23209 cp_parser_error (parser, "could not resolve typename type");
23210 type = error_mark_node;
23211 }
23212 }
23213
23214 if (maybe_process_partial_specialization (TREE_TYPE (type))
23215 == error_mark_node)
23216 {
23217 type = NULL_TREE;
23218 goto done;
23219 }
23220
23221 class_type = current_class_type;
23222 /* Enter the scope indicated by the nested-name-specifier. */
23223 pushed_scope = push_scope (nested_name_specifier);
23224 /* Get the canonical version of this type. */
23225 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23226 /* Call push_template_decl if it seems like we should be defining a
23227 template either from the template headers or the type we're
23228 defining, so that we diagnose both extra and missing headers. */
23229 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23230 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23231 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23232 {
23233 type = push_template_decl (type);
23234 if (type == error_mark_node)
23235 {
23236 type = NULL_TREE;
23237 goto done;
23238 }
23239 }
23240
23241 type = TREE_TYPE (type);
23242 *nested_name_specifier_p = true;
23243 }
23244 else /* The name is not a nested name. */
23245 {
23246 /* If the class was unnamed, create a dummy name. */
23247 if (!id)
23248 id = make_anon_name ();
23249 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23250 ? ts_within_enclosing_non_class
23251 : ts_current);
23252 type = xref_tag (class_key, id, tag_scope,
23253 parser->num_template_parameter_lists);
23254 }
23255
23256 /* Indicate whether this class was declared as a `class' or as a
23257 `struct'. */
23258 if (TREE_CODE (type) == RECORD_TYPE)
23259 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23260 cp_parser_check_class_key (class_key, type);
23261
23262 /* If this type was already complete, and we see another definition,
23263 that's an error. */
23264 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23265 {
23266 error_at (type_start_token->location, "redefinition of %q#T",
23267 type);
23268 inform (location_of (type), "previous definition of %q#T",
23269 type);
23270 type = NULL_TREE;
23271 goto done;
23272 }
23273 else if (type == error_mark_node)
23274 type = NULL_TREE;
23275
23276 if (type)
23277 {
23278 /* Apply attributes now, before any use of the class as a template
23279 argument in its base list. */
23280 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23281 fixup_attribute_variants (type);
23282 }
23283
23284 /* We will have entered the scope containing the class; the names of
23285 base classes should be looked up in that context. For example:
23286
23287 struct A { struct B {}; struct C; };
23288 struct A::C : B {};
23289
23290 is valid. */
23291
23292 /* Get the list of base-classes, if there is one. */
23293 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23294 {
23295 /* PR59482: enter the class scope so that base-specifiers are looked
23296 up correctly. */
23297 if (type)
23298 pushclass (type);
23299 bases = cp_parser_base_clause (parser);
23300 /* PR59482: get out of the previously pushed class scope so that the
23301 subsequent pops pop the right thing. */
23302 if (type)
23303 popclass ();
23304 }
23305 else
23306 bases = NULL_TREE;
23307
23308 /* If we're really defining a class, process the base classes.
23309 If they're invalid, fail. */
23310 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23311 xref_basetypes (type, bases);
23312
23313 done:
23314 /* Leave the scope given by the nested-name-specifier. We will
23315 enter the class scope itself while processing the members. */
23316 if (pushed_scope)
23317 pop_scope (pushed_scope);
23318
23319 if (invalid_explicit_specialization_p)
23320 {
23321 end_specialization ();
23322 --parser->num_template_parameter_lists;
23323 }
23324
23325 if (type)
23326 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23327 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23328 CLASSTYPE_FINAL (type) = 1;
23329 out:
23330 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23331 return type;
23332 }
23333
23334 /* Parse a class-key.
23335
23336 class-key:
23337 class
23338 struct
23339 union
23340
23341 Returns the kind of class-key specified, or none_type to indicate
23342 error. */
23343
23344 static enum tag_types
23345 cp_parser_class_key (cp_parser* parser)
23346 {
23347 cp_token *token;
23348 enum tag_types tag_type;
23349
23350 /* Look for the class-key. */
23351 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23352 if (!token)
23353 return none_type;
23354
23355 /* Check to see if the TOKEN is a class-key. */
23356 tag_type = cp_parser_token_is_class_key (token);
23357 if (!tag_type)
23358 cp_parser_error (parser, "expected class-key");
23359 return tag_type;
23360 }
23361
23362 /* Parse a type-parameter-key.
23363
23364 type-parameter-key:
23365 class
23366 typename
23367 */
23368
23369 static void
23370 cp_parser_type_parameter_key (cp_parser* parser)
23371 {
23372 /* Look for the type-parameter-key. */
23373 enum tag_types tag_type = none_type;
23374 cp_token *token = cp_lexer_peek_token (parser->lexer);
23375 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23376 {
23377 cp_lexer_consume_token (parser->lexer);
23378 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23379 /* typename is not allowed in a template template parameter
23380 by the standard until C++17. */
23381 pedwarn (token->location, OPT_Wpedantic,
23382 "ISO C++ forbids typename key in template template parameter;"
23383 " use -std=c++17 or -std=gnu++17");
23384 }
23385 else
23386 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23387
23388 return;
23389 }
23390
23391 /* Parse an (optional) member-specification.
23392
23393 member-specification:
23394 member-declaration member-specification [opt]
23395 access-specifier : member-specification [opt] */
23396
23397 static void
23398 cp_parser_member_specification_opt (cp_parser* parser)
23399 {
23400 while (true)
23401 {
23402 cp_token *token;
23403 enum rid keyword;
23404
23405 /* Peek at the next token. */
23406 token = cp_lexer_peek_token (parser->lexer);
23407 /* If it's a `}', or EOF then we've seen all the members. */
23408 if (token->type == CPP_CLOSE_BRACE
23409 || token->type == CPP_EOF
23410 || token->type == CPP_PRAGMA_EOL)
23411 break;
23412
23413 /* See if this token is a keyword. */
23414 keyword = token->keyword;
23415 switch (keyword)
23416 {
23417 case RID_PUBLIC:
23418 case RID_PROTECTED:
23419 case RID_PRIVATE:
23420 /* Consume the access-specifier. */
23421 cp_lexer_consume_token (parser->lexer);
23422 /* Remember which access-specifier is active. */
23423 current_access_specifier = token->u.value;
23424 /* Look for the `:'. */
23425 cp_parser_require (parser, CPP_COLON, RT_COLON);
23426 break;
23427
23428 default:
23429 /* Accept #pragmas at class scope. */
23430 if (token->type == CPP_PRAGMA)
23431 {
23432 cp_parser_pragma (parser, pragma_member, NULL);
23433 break;
23434 }
23435
23436 /* Otherwise, the next construction must be a
23437 member-declaration. */
23438 cp_parser_member_declaration (parser);
23439 }
23440 }
23441 }
23442
23443 /* Parse a member-declaration.
23444
23445 member-declaration:
23446 decl-specifier-seq [opt] member-declarator-list [opt] ;
23447 function-definition ; [opt]
23448 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23449 using-declaration
23450 template-declaration
23451 alias-declaration
23452
23453 member-declarator-list:
23454 member-declarator
23455 member-declarator-list , member-declarator
23456
23457 member-declarator:
23458 declarator pure-specifier [opt]
23459 declarator constant-initializer [opt]
23460 identifier [opt] : constant-expression
23461
23462 GNU Extensions:
23463
23464 member-declaration:
23465 __extension__ member-declaration
23466
23467 member-declarator:
23468 declarator attributes [opt] pure-specifier [opt]
23469 declarator attributes [opt] constant-initializer [opt]
23470 identifier [opt] attributes [opt] : constant-expression
23471
23472 C++0x Extensions:
23473
23474 member-declaration:
23475 static_assert-declaration */
23476
23477 static void
23478 cp_parser_member_declaration (cp_parser* parser)
23479 {
23480 cp_decl_specifier_seq decl_specifiers;
23481 tree prefix_attributes;
23482 tree decl;
23483 int declares_class_or_enum;
23484 bool friend_p;
23485 cp_token *token = NULL;
23486 cp_token *decl_spec_token_start = NULL;
23487 cp_token *initializer_token_start = NULL;
23488 int saved_pedantic;
23489 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23490
23491 /* Check for the `__extension__' keyword. */
23492 if (cp_parser_extension_opt (parser, &saved_pedantic))
23493 {
23494 /* Recurse. */
23495 cp_parser_member_declaration (parser);
23496 /* Restore the old value of the PEDANTIC flag. */
23497 pedantic = saved_pedantic;
23498
23499 return;
23500 }
23501
23502 /* Check for a template-declaration. */
23503 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23504 {
23505 /* An explicit specialization here is an error condition, and we
23506 expect the specialization handler to detect and report this. */
23507 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23508 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23509 cp_parser_explicit_specialization (parser);
23510 else
23511 cp_parser_template_declaration (parser, /*member_p=*/true);
23512
23513 return;
23514 }
23515 /* Check for a template introduction. */
23516 else if (cp_parser_template_declaration_after_export (parser, true))
23517 return;
23518
23519 /* Check for a using-declaration. */
23520 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23521 {
23522 if (cxx_dialect < cxx11)
23523 {
23524 /* Parse the using-declaration. */
23525 cp_parser_using_declaration (parser,
23526 /*access_declaration_p=*/false);
23527 return;
23528 }
23529 else
23530 {
23531 tree decl;
23532 bool alias_decl_expected;
23533 cp_parser_parse_tentatively (parser);
23534 decl = cp_parser_alias_declaration (parser);
23535 /* Note that if we actually see the '=' token after the
23536 identifier, cp_parser_alias_declaration commits the
23537 tentative parse. In that case, we really expect an
23538 alias-declaration. Otherwise, we expect a using
23539 declaration. */
23540 alias_decl_expected =
23541 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23542 cp_parser_parse_definitely (parser);
23543
23544 if (alias_decl_expected)
23545 finish_member_declaration (decl);
23546 else
23547 cp_parser_using_declaration (parser,
23548 /*access_declaration_p=*/false);
23549 return;
23550 }
23551 }
23552
23553 /* Check for @defs. */
23554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23555 {
23556 tree ivar, member;
23557 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23558 ivar = ivar_chains;
23559 while (ivar)
23560 {
23561 member = ivar;
23562 ivar = TREE_CHAIN (member);
23563 TREE_CHAIN (member) = NULL_TREE;
23564 finish_member_declaration (member);
23565 }
23566 return;
23567 }
23568
23569 /* If the next token is `static_assert' we have a static assertion. */
23570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23571 {
23572 cp_parser_static_assert (parser, /*member_p=*/true);
23573 return;
23574 }
23575
23576 parser->colon_corrects_to_scope_p = false;
23577
23578 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23579 goto out;
23580
23581 /* Parse the decl-specifier-seq. */
23582 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23583 cp_parser_decl_specifier_seq (parser,
23584 CP_PARSER_FLAGS_OPTIONAL,
23585 &decl_specifiers,
23586 &declares_class_or_enum);
23587 /* Check for an invalid type-name. */
23588 if (!decl_specifiers.any_type_specifiers_p
23589 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23590 goto out;
23591 /* If there is no declarator, then the decl-specifier-seq should
23592 specify a type. */
23593 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23594 {
23595 /* If there was no decl-specifier-seq, and the next token is a
23596 `;', then we have something like:
23597
23598 struct S { ; };
23599
23600 [class.mem]
23601
23602 Each member-declaration shall declare at least one member
23603 name of the class. */
23604 if (!decl_specifiers.any_specifiers_p)
23605 {
23606 cp_token *token = cp_lexer_peek_token (parser->lexer);
23607 if (!in_system_header_at (token->location))
23608 {
23609 gcc_rich_location richloc (token->location);
23610 richloc.add_fixit_remove ();
23611 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23612 }
23613 }
23614 else
23615 {
23616 tree type;
23617
23618 /* See if this declaration is a friend. */
23619 friend_p = cp_parser_friend_p (&decl_specifiers);
23620 /* If there were decl-specifiers, check to see if there was
23621 a class-declaration. */
23622 type = check_tag_decl (&decl_specifiers,
23623 /*explicit_type_instantiation_p=*/false);
23624 /* Nested classes have already been added to the class, but
23625 a `friend' needs to be explicitly registered. */
23626 if (friend_p)
23627 {
23628 /* If the `friend' keyword was present, the friend must
23629 be introduced with a class-key. */
23630 if (!declares_class_or_enum && cxx_dialect < cxx11)
23631 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23632 "in C++03 a class-key must be used "
23633 "when declaring a friend");
23634 /* In this case:
23635
23636 template <typename T> struct A {
23637 friend struct A<T>::B;
23638 };
23639
23640 A<T>::B will be represented by a TYPENAME_TYPE, and
23641 therefore not recognized by check_tag_decl. */
23642 if (!type)
23643 {
23644 type = decl_specifiers.type;
23645 if (type && TREE_CODE (type) == TYPE_DECL)
23646 type = TREE_TYPE (type);
23647 }
23648 if (!type || !TYPE_P (type))
23649 error_at (decl_spec_token_start->location,
23650 "friend declaration does not name a class or "
23651 "function");
23652 else
23653 make_friend_class (current_class_type, type,
23654 /*complain=*/true);
23655 }
23656 /* If there is no TYPE, an error message will already have
23657 been issued. */
23658 else if (!type || type == error_mark_node)
23659 ;
23660 /* An anonymous aggregate has to be handled specially; such
23661 a declaration really declares a data member (with a
23662 particular type), as opposed to a nested class. */
23663 else if (ANON_AGGR_TYPE_P (type))
23664 {
23665 /* C++11 9.5/6. */
23666 if (decl_specifiers.storage_class != sc_none)
23667 error_at (decl_spec_token_start->location,
23668 "a storage class on an anonymous aggregate "
23669 "in class scope is not allowed");
23670
23671 /* Remove constructors and such from TYPE, now that we
23672 know it is an anonymous aggregate. */
23673 fixup_anonymous_aggr (type);
23674 /* And make the corresponding data member. */
23675 decl = build_decl (decl_spec_token_start->location,
23676 FIELD_DECL, NULL_TREE, type);
23677 /* Add it to the class. */
23678 finish_member_declaration (decl);
23679 }
23680 else
23681 cp_parser_check_access_in_redeclaration
23682 (TYPE_NAME (type),
23683 decl_spec_token_start->location);
23684 }
23685 }
23686 else
23687 {
23688 bool assume_semicolon = false;
23689
23690 /* Clear attributes from the decl_specifiers but keep them
23691 around as prefix attributes that apply them to the entity
23692 being declared. */
23693 prefix_attributes = decl_specifiers.attributes;
23694 decl_specifiers.attributes = NULL_TREE;
23695
23696 /* See if these declarations will be friends. */
23697 friend_p = cp_parser_friend_p (&decl_specifiers);
23698
23699 /* Keep going until we hit the `;' at the end of the
23700 declaration. */
23701 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23702 {
23703 tree attributes = NULL_TREE;
23704 tree first_attribute;
23705 tree initializer;
23706 bool is_bitfld = false;
23707 bool named_bitfld = false;
23708
23709 /* Peek at the next token. */
23710 token = cp_lexer_peek_token (parser->lexer);
23711
23712 /* The following code wants to know early if it is a bit-field
23713 or some other declaration. Attributes can appear before
23714 the `:' token, but are hopefully rare enough that the
23715 simplicity of the tentative lookup pays off. */
23716 if (cp_next_tokens_can_be_attribute_p (parser)
23717 || (token->type == CPP_NAME
23718 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23719 && (named_bitfld = true)))
23720 {
23721 cp_parser_parse_tentatively (parser);
23722 if (named_bitfld)
23723 cp_lexer_consume_token (parser->lexer);
23724 cp_parser_attributes_opt (parser);
23725 token = cp_lexer_peek_token (parser->lexer);
23726 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23727 cp_parser_abort_tentative_parse (parser);
23728 }
23729
23730 /* Check for a bitfield declaration. */
23731 if (is_bitfld
23732 || token->type == CPP_COLON
23733 || (token->type == CPP_NAME
23734 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23735 && (named_bitfld = true)))
23736 {
23737 tree identifier;
23738 tree width;
23739 tree late_attributes = NULL_TREE;
23740
23741 if (named_bitfld)
23742 identifier = cp_parser_identifier (parser);
23743 else
23744 identifier = NULL_TREE;
23745
23746 /* Look for attributes that apply to the bitfield. */
23747 attributes = cp_parser_attributes_opt (parser);
23748
23749 /* Consume the `:' token. */
23750 cp_lexer_consume_token (parser->lexer);
23751
23752 /* Get the width of the bitfield. */
23753 width = cp_parser_constant_expression (parser, false, NULL,
23754 cxx_dialect >= cxx11);
23755
23756 /* In C++2A and as extension for C++11 and above we allow
23757 default member initializers for bit-fields. */
23758 initializer = NULL_TREE;
23759 if (cxx_dialect >= cxx11
23760 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23761 || cp_lexer_next_token_is (parser->lexer,
23762 CPP_OPEN_BRACE)))
23763 {
23764 location_t loc
23765 = cp_lexer_peek_token (parser->lexer)->location;
23766 if (cxx_dialect < cxx2a
23767 && !in_system_header_at (loc)
23768 && identifier != NULL_TREE)
23769 pedwarn (loc, 0,
23770 "default member initializers for bit-fields "
23771 "only available with -std=c++2a or "
23772 "-std=gnu++2a");
23773
23774 initializer = cp_parser_save_nsdmi (parser);
23775 if (identifier == NULL_TREE)
23776 {
23777 error_at (loc, "default member initializer for "
23778 "unnamed bit-field");
23779 initializer = NULL_TREE;
23780 }
23781 }
23782 else
23783 {
23784 /* Look for attributes that apply to the bitfield after
23785 the `:' token and width. This is where GCC used to
23786 parse attributes in the past, pedwarn if there is
23787 a std attribute. */
23788 if (cp_next_tokens_can_be_std_attribute_p (parser))
23789 pedwarn (input_location, OPT_Wpedantic,
23790 "ISO C++ allows bit-field attributes only "
23791 "before the %<:%> token");
23792
23793 late_attributes = cp_parser_attributes_opt (parser);
23794 }
23795
23796 attributes = chainon (attributes, late_attributes);
23797
23798 /* Remember which attributes are prefix attributes and
23799 which are not. */
23800 first_attribute = attributes;
23801 /* Combine the attributes. */
23802 attributes = chainon (prefix_attributes, attributes);
23803
23804 /* Create the bitfield declaration. */
23805 decl = grokbitfield (identifier
23806 ? make_id_declarator (NULL_TREE,
23807 identifier,
23808 sfk_none)
23809 : NULL,
23810 &decl_specifiers,
23811 width, initializer,
23812 attributes);
23813 }
23814 else
23815 {
23816 cp_declarator *declarator;
23817 tree asm_specification;
23818 int ctor_dtor_or_conv_p;
23819
23820 /* Parse the declarator. */
23821 declarator
23822 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23823 &ctor_dtor_or_conv_p,
23824 /*parenthesized_p=*/NULL,
23825 /*member_p=*/true,
23826 friend_p);
23827
23828 /* If something went wrong parsing the declarator, make sure
23829 that we at least consume some tokens. */
23830 if (declarator == cp_error_declarator)
23831 {
23832 /* Skip to the end of the statement. */
23833 cp_parser_skip_to_end_of_statement (parser);
23834 /* If the next token is not a semicolon, that is
23835 probably because we just skipped over the body of
23836 a function. So, we consume a semicolon if
23837 present, but do not issue an error message if it
23838 is not present. */
23839 if (cp_lexer_next_token_is (parser->lexer,
23840 CPP_SEMICOLON))
23841 cp_lexer_consume_token (parser->lexer);
23842 goto out;
23843 }
23844
23845 if (declares_class_or_enum & 2)
23846 cp_parser_check_for_definition_in_return_type
23847 (declarator, decl_specifiers.type,
23848 decl_specifiers.locations[ds_type_spec]);
23849
23850 /* Look for an asm-specification. */
23851 asm_specification = cp_parser_asm_specification_opt (parser);
23852 /* Look for attributes that apply to the declaration. */
23853 attributes = cp_parser_attributes_opt (parser);
23854 /* Remember which attributes are prefix attributes and
23855 which are not. */
23856 first_attribute = attributes;
23857 /* Combine the attributes. */
23858 attributes = chainon (prefix_attributes, attributes);
23859
23860 /* If it's an `=', then we have a constant-initializer or a
23861 pure-specifier. It is not correct to parse the
23862 initializer before registering the member declaration
23863 since the member declaration should be in scope while
23864 its initializer is processed. However, the rest of the
23865 front end does not yet provide an interface that allows
23866 us to handle this correctly. */
23867 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23868 {
23869 /* In [class.mem]:
23870
23871 A pure-specifier shall be used only in the declaration of
23872 a virtual function.
23873
23874 A member-declarator can contain a constant-initializer
23875 only if it declares a static member of integral or
23876 enumeration type.
23877
23878 Therefore, if the DECLARATOR is for a function, we look
23879 for a pure-specifier; otherwise, we look for a
23880 constant-initializer. When we call `grokfield', it will
23881 perform more stringent semantics checks. */
23882 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23883 if (function_declarator_p (declarator)
23884 || (decl_specifiers.type
23885 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23886 && declarator->kind == cdk_id
23887 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23888 == FUNCTION_TYPE)))
23889 initializer = cp_parser_pure_specifier (parser);
23890 else if (decl_specifiers.storage_class != sc_static)
23891 initializer = cp_parser_save_nsdmi (parser);
23892 else if (cxx_dialect >= cxx11)
23893 {
23894 bool nonconst;
23895 /* Don't require a constant rvalue in C++11, since we
23896 might want a reference constant. We'll enforce
23897 constancy later. */
23898 cp_lexer_consume_token (parser->lexer);
23899 /* Parse the initializer. */
23900 initializer = cp_parser_initializer_clause (parser,
23901 &nonconst);
23902 }
23903 else
23904 /* Parse the initializer. */
23905 initializer = cp_parser_constant_initializer (parser);
23906 }
23907 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23908 && !function_declarator_p (declarator))
23909 {
23910 bool x;
23911 if (decl_specifiers.storage_class != sc_static)
23912 initializer = cp_parser_save_nsdmi (parser);
23913 else
23914 initializer = cp_parser_initializer (parser, &x, &x);
23915 }
23916 /* Otherwise, there is no initializer. */
23917 else
23918 initializer = NULL_TREE;
23919
23920 /* See if we are probably looking at a function
23921 definition. We are certainly not looking at a
23922 member-declarator. Calling `grokfield' has
23923 side-effects, so we must not do it unless we are sure
23924 that we are looking at a member-declarator. */
23925 if (cp_parser_token_starts_function_definition_p
23926 (cp_lexer_peek_token (parser->lexer)))
23927 {
23928 /* The grammar does not allow a pure-specifier to be
23929 used when a member function is defined. (It is
23930 possible that this fact is an oversight in the
23931 standard, since a pure function may be defined
23932 outside of the class-specifier. */
23933 if (initializer && initializer_token_start)
23934 error_at (initializer_token_start->location,
23935 "pure-specifier on function-definition");
23936 decl = cp_parser_save_member_function_body (parser,
23937 &decl_specifiers,
23938 declarator,
23939 attributes);
23940 if (parser->fully_implicit_function_template_p)
23941 decl = finish_fully_implicit_template (parser, decl);
23942 /* If the member was not a friend, declare it here. */
23943 if (!friend_p)
23944 finish_member_declaration (decl);
23945 /* Peek at the next token. */
23946 token = cp_lexer_peek_token (parser->lexer);
23947 /* If the next token is a semicolon, consume it. */
23948 if (token->type == CPP_SEMICOLON)
23949 {
23950 location_t semicolon_loc
23951 = cp_lexer_consume_token (parser->lexer)->location;
23952 gcc_rich_location richloc (semicolon_loc);
23953 richloc.add_fixit_remove ();
23954 warning_at (&richloc, OPT_Wextra_semi,
23955 "extra %<;%> after in-class "
23956 "function definition");
23957 }
23958 goto out;
23959 }
23960 else
23961 if (declarator->kind == cdk_function)
23962 declarator->id_loc = token->location;
23963 /* Create the declaration. */
23964 decl = grokfield (declarator, &decl_specifiers,
23965 initializer, /*init_const_expr_p=*/true,
23966 asm_specification, attributes);
23967 if (parser->fully_implicit_function_template_p)
23968 {
23969 if (friend_p)
23970 finish_fully_implicit_template (parser, 0);
23971 else
23972 decl = finish_fully_implicit_template (parser, decl);
23973 }
23974 }
23975
23976 cp_finalize_omp_declare_simd (parser, decl);
23977 cp_finalize_oacc_routine (parser, decl, false);
23978
23979 /* Reset PREFIX_ATTRIBUTES. */
23980 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23981 attributes = TREE_CHAIN (attributes);
23982 if (attributes)
23983 TREE_CHAIN (attributes) = NULL_TREE;
23984
23985 /* If there is any qualification still in effect, clear it
23986 now; we will be starting fresh with the next declarator. */
23987 parser->scope = NULL_TREE;
23988 parser->qualifying_scope = NULL_TREE;
23989 parser->object_scope = NULL_TREE;
23990 /* If it's a `,', then there are more declarators. */
23991 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23992 {
23993 cp_lexer_consume_token (parser->lexer);
23994 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23995 {
23996 cp_token *token = cp_lexer_previous_token (parser->lexer);
23997 gcc_rich_location richloc (token->location);
23998 richloc.add_fixit_remove ();
23999 error_at (&richloc, "stray %<,%> at end of "
24000 "member declaration");
24001 }
24002 }
24003 /* If the next token isn't a `;', then we have a parse error. */
24004 else if (cp_lexer_next_token_is_not (parser->lexer,
24005 CPP_SEMICOLON))
24006 {
24007 /* The next token might be a ways away from where the
24008 actual semicolon is missing. Find the previous token
24009 and use that for our error position. */
24010 cp_token *token = cp_lexer_previous_token (parser->lexer);
24011 gcc_rich_location richloc (token->location);
24012 richloc.add_fixit_insert_after (";");
24013 error_at (&richloc, "expected %<;%> at end of "
24014 "member declaration");
24015
24016 /* Assume that the user meant to provide a semicolon. If
24017 we were to cp_parser_skip_to_end_of_statement, we might
24018 skip to a semicolon inside a member function definition
24019 and issue nonsensical error messages. */
24020 assume_semicolon = true;
24021 }
24022
24023 if (decl)
24024 {
24025 /* Add DECL to the list of members. */
24026 if (!friend_p
24027 /* Explicitly include, eg, NSDMIs, for better error
24028 recovery (c++/58650). */
24029 || !DECL_DECLARES_FUNCTION_P (decl))
24030 finish_member_declaration (decl);
24031
24032 if (TREE_CODE (decl) == FUNCTION_DECL)
24033 cp_parser_save_default_args (parser, decl);
24034 else if (TREE_CODE (decl) == FIELD_DECL
24035 && DECL_INITIAL (decl))
24036 /* Add DECL to the queue of NSDMI to be parsed later. */
24037 vec_safe_push (unparsed_nsdmis, decl);
24038 }
24039
24040 if (assume_semicolon)
24041 goto out;
24042 }
24043 }
24044
24045 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24046 out:
24047 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24048 }
24049
24050 /* Parse a pure-specifier.
24051
24052 pure-specifier:
24053 = 0
24054
24055 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24056 Otherwise, ERROR_MARK_NODE is returned. */
24057
24058 static tree
24059 cp_parser_pure_specifier (cp_parser* parser)
24060 {
24061 cp_token *token;
24062
24063 /* Look for the `=' token. */
24064 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24065 return error_mark_node;
24066 /* Look for the `0' token. */
24067 token = cp_lexer_peek_token (parser->lexer);
24068
24069 if (token->type == CPP_EOF
24070 || token->type == CPP_PRAGMA_EOL)
24071 return error_mark_node;
24072
24073 cp_lexer_consume_token (parser->lexer);
24074
24075 /* Accept = default or = delete in c++0x mode. */
24076 if (token->keyword == RID_DEFAULT
24077 || token->keyword == RID_DELETE)
24078 {
24079 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24080 return token->u.value;
24081 }
24082
24083 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24084 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24085 {
24086 cp_parser_error (parser,
24087 "invalid pure specifier (only %<= 0%> is allowed)");
24088 cp_parser_skip_to_end_of_statement (parser);
24089 return error_mark_node;
24090 }
24091 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24092 {
24093 error_at (token->location, "templates may not be %<virtual%>");
24094 return error_mark_node;
24095 }
24096
24097 return integer_zero_node;
24098 }
24099
24100 /* Parse a constant-initializer.
24101
24102 constant-initializer:
24103 = constant-expression
24104
24105 Returns a representation of the constant-expression. */
24106
24107 static tree
24108 cp_parser_constant_initializer (cp_parser* parser)
24109 {
24110 /* Look for the `=' token. */
24111 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24112 return error_mark_node;
24113
24114 /* It is invalid to write:
24115
24116 struct S { static const int i = { 7 }; };
24117
24118 */
24119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24120 {
24121 cp_parser_error (parser,
24122 "a brace-enclosed initializer is not allowed here");
24123 /* Consume the opening brace. */
24124 matching_braces braces;
24125 braces.consume_open (parser);
24126 /* Skip the initializer. */
24127 cp_parser_skip_to_closing_brace (parser);
24128 /* Look for the trailing `}'. */
24129 braces.require_close (parser);
24130
24131 return error_mark_node;
24132 }
24133
24134 return cp_parser_constant_expression (parser);
24135 }
24136
24137 /* Derived classes [gram.class.derived] */
24138
24139 /* Parse a base-clause.
24140
24141 base-clause:
24142 : base-specifier-list
24143
24144 base-specifier-list:
24145 base-specifier ... [opt]
24146 base-specifier-list , base-specifier ... [opt]
24147
24148 Returns a TREE_LIST representing the base-classes, in the order in
24149 which they were declared. The representation of each node is as
24150 described by cp_parser_base_specifier.
24151
24152 In the case that no bases are specified, this function will return
24153 NULL_TREE, not ERROR_MARK_NODE. */
24154
24155 static tree
24156 cp_parser_base_clause (cp_parser* parser)
24157 {
24158 tree bases = NULL_TREE;
24159
24160 /* Look for the `:' that begins the list. */
24161 cp_parser_require (parser, CPP_COLON, RT_COLON);
24162
24163 /* Scan the base-specifier-list. */
24164 while (true)
24165 {
24166 cp_token *token;
24167 tree base;
24168 bool pack_expansion_p = false;
24169
24170 /* Look for the base-specifier. */
24171 base = cp_parser_base_specifier (parser);
24172 /* Look for the (optional) ellipsis. */
24173 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24174 {
24175 /* Consume the `...'. */
24176 cp_lexer_consume_token (parser->lexer);
24177
24178 pack_expansion_p = true;
24179 }
24180
24181 /* Add BASE to the front of the list. */
24182 if (base && base != error_mark_node)
24183 {
24184 if (pack_expansion_p)
24185 /* Make this a pack expansion type. */
24186 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24187
24188 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24189 {
24190 TREE_CHAIN (base) = bases;
24191 bases = base;
24192 }
24193 }
24194 /* Peek at the next token. */
24195 token = cp_lexer_peek_token (parser->lexer);
24196 /* If it's not a comma, then the list is complete. */
24197 if (token->type != CPP_COMMA)
24198 break;
24199 /* Consume the `,'. */
24200 cp_lexer_consume_token (parser->lexer);
24201 }
24202
24203 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24204 base class had a qualified name. However, the next name that
24205 appears is certainly not qualified. */
24206 parser->scope = NULL_TREE;
24207 parser->qualifying_scope = NULL_TREE;
24208 parser->object_scope = NULL_TREE;
24209
24210 return nreverse (bases);
24211 }
24212
24213 /* Parse a base-specifier.
24214
24215 base-specifier:
24216 :: [opt] nested-name-specifier [opt] class-name
24217 virtual access-specifier [opt] :: [opt] nested-name-specifier
24218 [opt] class-name
24219 access-specifier virtual [opt] :: [opt] nested-name-specifier
24220 [opt] class-name
24221
24222 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24223 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24224 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24225 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24226
24227 static tree
24228 cp_parser_base_specifier (cp_parser* parser)
24229 {
24230 cp_token *token;
24231 bool done = false;
24232 bool virtual_p = false;
24233 bool duplicate_virtual_error_issued_p = false;
24234 bool duplicate_access_error_issued_p = false;
24235 bool class_scope_p, template_p;
24236 tree access = access_default_node;
24237 tree type;
24238
24239 /* Process the optional `virtual' and `access-specifier'. */
24240 while (!done)
24241 {
24242 /* Peek at the next token. */
24243 token = cp_lexer_peek_token (parser->lexer);
24244 /* Process `virtual'. */
24245 switch (token->keyword)
24246 {
24247 case RID_VIRTUAL:
24248 /* If `virtual' appears more than once, issue an error. */
24249 if (virtual_p && !duplicate_virtual_error_issued_p)
24250 {
24251 cp_parser_error (parser,
24252 "%<virtual%> specified more than once in base-specifier");
24253 duplicate_virtual_error_issued_p = true;
24254 }
24255
24256 virtual_p = true;
24257
24258 /* Consume the `virtual' token. */
24259 cp_lexer_consume_token (parser->lexer);
24260
24261 break;
24262
24263 case RID_PUBLIC:
24264 case RID_PROTECTED:
24265 case RID_PRIVATE:
24266 /* If more than one access specifier appears, issue an
24267 error. */
24268 if (access != access_default_node
24269 && !duplicate_access_error_issued_p)
24270 {
24271 cp_parser_error (parser,
24272 "more than one access specifier in base-specifier");
24273 duplicate_access_error_issued_p = true;
24274 }
24275
24276 access = ridpointers[(int) token->keyword];
24277
24278 /* Consume the access-specifier. */
24279 cp_lexer_consume_token (parser->lexer);
24280
24281 break;
24282
24283 default:
24284 done = true;
24285 break;
24286 }
24287 }
24288 /* It is not uncommon to see programs mechanically, erroneously, use
24289 the 'typename' keyword to denote (dependent) qualified types
24290 as base classes. */
24291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24292 {
24293 token = cp_lexer_peek_token (parser->lexer);
24294 if (!processing_template_decl)
24295 error_at (token->location,
24296 "keyword %<typename%> not allowed outside of templates");
24297 else
24298 error_at (token->location,
24299 "keyword %<typename%> not allowed in this context "
24300 "(the base class is implicitly a type)");
24301 cp_lexer_consume_token (parser->lexer);
24302 }
24303
24304 /* Look for the optional `::' operator. */
24305 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24306 /* Look for the nested-name-specifier. The simplest way to
24307 implement:
24308
24309 [temp.res]
24310
24311 The keyword `typename' is not permitted in a base-specifier or
24312 mem-initializer; in these contexts a qualified name that
24313 depends on a template-parameter is implicitly assumed to be a
24314 type name.
24315
24316 is to pretend that we have seen the `typename' keyword at this
24317 point. */
24318 cp_parser_nested_name_specifier_opt (parser,
24319 /*typename_keyword_p=*/true,
24320 /*check_dependency_p=*/true,
24321 /*type_p=*/true,
24322 /*is_declaration=*/true);
24323 /* If the base class is given by a qualified name, assume that names
24324 we see are type names or templates, as appropriate. */
24325 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24326 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24327
24328 if (!parser->scope
24329 && cp_lexer_next_token_is_decltype (parser->lexer))
24330 /* DR 950 allows decltype as a base-specifier. */
24331 type = cp_parser_decltype (parser);
24332 else
24333 {
24334 /* Otherwise, look for the class-name. */
24335 type = cp_parser_class_name (parser,
24336 class_scope_p,
24337 template_p,
24338 typename_type,
24339 /*check_dependency_p=*/true,
24340 /*class_head_p=*/false,
24341 /*is_declaration=*/true);
24342 type = TREE_TYPE (type);
24343 }
24344
24345 if (type == error_mark_node)
24346 return error_mark_node;
24347
24348 return finish_base_specifier (type, access, virtual_p);
24349 }
24350
24351 /* Exception handling [gram.exception] */
24352
24353 /* Parse an (optional) noexcept-specification.
24354
24355 noexcept-specification:
24356 noexcept ( constant-expression ) [opt]
24357
24358 If no noexcept-specification is present, returns NULL_TREE.
24359 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24360 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24361 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24362 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24363 in which case a boolean condition is returned instead. */
24364
24365 static tree
24366 cp_parser_noexcept_specification_opt (cp_parser* parser,
24367 bool require_constexpr,
24368 bool* consumed_expr,
24369 bool return_cond)
24370 {
24371 cp_token *token;
24372 const char *saved_message;
24373
24374 /* Peek at the next token. */
24375 token = cp_lexer_peek_token (parser->lexer);
24376
24377 /* Is it a noexcept-specification? */
24378 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24379 {
24380 tree expr;
24381 cp_lexer_consume_token (parser->lexer);
24382
24383 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24384 {
24385 matching_parens parens;
24386 parens.consume_open (parser);
24387
24388 if (require_constexpr)
24389 {
24390 /* Types may not be defined in an exception-specification. */
24391 saved_message = parser->type_definition_forbidden_message;
24392 parser->type_definition_forbidden_message
24393 = G_("types may not be defined in an exception-specification");
24394
24395 expr = cp_parser_constant_expression (parser);
24396
24397 /* Restore the saved message. */
24398 parser->type_definition_forbidden_message = saved_message;
24399 }
24400 else
24401 {
24402 expr = cp_parser_expression (parser);
24403 *consumed_expr = true;
24404 }
24405
24406 parens.require_close (parser);
24407 }
24408 else
24409 {
24410 expr = boolean_true_node;
24411 if (!require_constexpr)
24412 *consumed_expr = false;
24413 }
24414
24415 /* We cannot build a noexcept-spec right away because this will check
24416 that expr is a constexpr. */
24417 if (!return_cond)
24418 return build_noexcept_spec (expr, tf_warning_or_error);
24419 else
24420 return expr;
24421 }
24422 else
24423 return NULL_TREE;
24424 }
24425
24426 /* Parse an (optional) exception-specification.
24427
24428 exception-specification:
24429 throw ( type-id-list [opt] )
24430
24431 Returns a TREE_LIST representing the exception-specification. The
24432 TREE_VALUE of each node is a type. */
24433
24434 static tree
24435 cp_parser_exception_specification_opt (cp_parser* parser)
24436 {
24437 cp_token *token;
24438 tree type_id_list;
24439 const char *saved_message;
24440
24441 /* Peek at the next token. */
24442 token = cp_lexer_peek_token (parser->lexer);
24443
24444 /* Is it a noexcept-specification? */
24445 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24446 false);
24447 if (type_id_list != NULL_TREE)
24448 return type_id_list;
24449
24450 /* If it's not `throw', then there's no exception-specification. */
24451 if (!cp_parser_is_keyword (token, RID_THROW))
24452 return NULL_TREE;
24453
24454 location_t loc = token->location;
24455
24456 /* Consume the `throw'. */
24457 cp_lexer_consume_token (parser->lexer);
24458
24459 /* Look for the `('. */
24460 matching_parens parens;
24461 parens.require_open (parser);
24462
24463 /* Peek at the next token. */
24464 token = cp_lexer_peek_token (parser->lexer);
24465 /* If it's not a `)', then there is a type-id-list. */
24466 if (token->type != CPP_CLOSE_PAREN)
24467 {
24468 /* Types may not be defined in an exception-specification. */
24469 saved_message = parser->type_definition_forbidden_message;
24470 parser->type_definition_forbidden_message
24471 = G_("types may not be defined in an exception-specification");
24472 /* Parse the type-id-list. */
24473 type_id_list = cp_parser_type_id_list (parser);
24474 /* Restore the saved message. */
24475 parser->type_definition_forbidden_message = saved_message;
24476
24477 if (cxx_dialect >= cxx17)
24478 {
24479 error_at (loc, "ISO C++17 does not allow dynamic exception "
24480 "specifications");
24481 type_id_list = NULL_TREE;
24482 }
24483 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24484 warning_at (loc, OPT_Wdeprecated,
24485 "dynamic exception specifications are deprecated in "
24486 "C++11");
24487 }
24488 /* In C++17, throw() is equivalent to noexcept (true). throw()
24489 is deprecated in C++11 and above as well, but is still widely used,
24490 so don't warn about it yet. */
24491 else if (cxx_dialect >= cxx17)
24492 type_id_list = noexcept_true_spec;
24493 else
24494 type_id_list = empty_except_spec;
24495
24496 /* Look for the `)'. */
24497 parens.require_close (parser);
24498
24499 return type_id_list;
24500 }
24501
24502 /* Parse an (optional) type-id-list.
24503
24504 type-id-list:
24505 type-id ... [opt]
24506 type-id-list , type-id ... [opt]
24507
24508 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24509 in the order that the types were presented. */
24510
24511 static tree
24512 cp_parser_type_id_list (cp_parser* parser)
24513 {
24514 tree types = NULL_TREE;
24515
24516 while (true)
24517 {
24518 cp_token *token;
24519 tree type;
24520
24521 token = cp_lexer_peek_token (parser->lexer);
24522
24523 /* Get the next type-id. */
24524 type = cp_parser_type_id (parser);
24525 /* Check for invalid 'auto'. */
24526 if (flag_concepts && type_uses_auto (type))
24527 {
24528 error_at (token->location,
24529 "invalid use of %<auto%> in exception-specification");
24530 type = error_mark_node;
24531 }
24532 /* Parse the optional ellipsis. */
24533 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24534 {
24535 /* Consume the `...'. */
24536 cp_lexer_consume_token (parser->lexer);
24537
24538 /* Turn the type into a pack expansion expression. */
24539 type = make_pack_expansion (type);
24540 }
24541 /* Add it to the list. */
24542 types = add_exception_specifier (types, type, /*complain=*/1);
24543 /* Peek at the next token. */
24544 token = cp_lexer_peek_token (parser->lexer);
24545 /* If it is not a `,', we are done. */
24546 if (token->type != CPP_COMMA)
24547 break;
24548 /* Consume the `,'. */
24549 cp_lexer_consume_token (parser->lexer);
24550 }
24551
24552 return nreverse (types);
24553 }
24554
24555 /* Parse a try-block.
24556
24557 try-block:
24558 try compound-statement handler-seq */
24559
24560 static tree
24561 cp_parser_try_block (cp_parser* parser)
24562 {
24563 tree try_block;
24564
24565 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24566 if (parser->in_function_body
24567 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24568 error ("%<try%> in %<constexpr%> function");
24569
24570 try_block = begin_try_block ();
24571 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24572 finish_try_block (try_block);
24573 cp_parser_handler_seq (parser);
24574 finish_handler_sequence (try_block);
24575
24576 return try_block;
24577 }
24578
24579 /* Parse a function-try-block.
24580
24581 function-try-block:
24582 try ctor-initializer [opt] function-body handler-seq */
24583
24584 static void
24585 cp_parser_function_try_block (cp_parser* parser)
24586 {
24587 tree compound_stmt;
24588 tree try_block;
24589
24590 /* Look for the `try' keyword. */
24591 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24592 return;
24593 /* Let the rest of the front end know where we are. */
24594 try_block = begin_function_try_block (&compound_stmt);
24595 /* Parse the function-body. */
24596 cp_parser_ctor_initializer_opt_and_function_body
24597 (parser, /*in_function_try_block=*/true);
24598 /* We're done with the `try' part. */
24599 finish_function_try_block (try_block);
24600 /* Parse the handlers. */
24601 cp_parser_handler_seq (parser);
24602 /* We're done with the handlers. */
24603 finish_function_handler_sequence (try_block, compound_stmt);
24604 }
24605
24606 /* Parse a handler-seq.
24607
24608 handler-seq:
24609 handler handler-seq [opt] */
24610
24611 static void
24612 cp_parser_handler_seq (cp_parser* parser)
24613 {
24614 while (true)
24615 {
24616 cp_token *token;
24617
24618 /* Parse the handler. */
24619 cp_parser_handler (parser);
24620 /* Peek at the next token. */
24621 token = cp_lexer_peek_token (parser->lexer);
24622 /* If it's not `catch' then there are no more handlers. */
24623 if (!cp_parser_is_keyword (token, RID_CATCH))
24624 break;
24625 }
24626 }
24627
24628 /* Parse a handler.
24629
24630 handler:
24631 catch ( exception-declaration ) compound-statement */
24632
24633 static void
24634 cp_parser_handler (cp_parser* parser)
24635 {
24636 tree handler;
24637 tree declaration;
24638
24639 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24640 handler = begin_handler ();
24641 matching_parens parens;
24642 parens.require_open (parser);
24643 declaration = cp_parser_exception_declaration (parser);
24644 finish_handler_parms (declaration, handler);
24645 parens.require_close (parser);
24646 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24647 finish_handler (handler);
24648 }
24649
24650 /* Parse an exception-declaration.
24651
24652 exception-declaration:
24653 type-specifier-seq declarator
24654 type-specifier-seq abstract-declarator
24655 type-specifier-seq
24656 ...
24657
24658 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24659 ellipsis variant is used. */
24660
24661 static tree
24662 cp_parser_exception_declaration (cp_parser* parser)
24663 {
24664 cp_decl_specifier_seq type_specifiers;
24665 cp_declarator *declarator;
24666 const char *saved_message;
24667
24668 /* If it's an ellipsis, it's easy to handle. */
24669 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24670 {
24671 /* Consume the `...' token. */
24672 cp_lexer_consume_token (parser->lexer);
24673 return NULL_TREE;
24674 }
24675
24676 /* Types may not be defined in exception-declarations. */
24677 saved_message = parser->type_definition_forbidden_message;
24678 parser->type_definition_forbidden_message
24679 = G_("types may not be defined in exception-declarations");
24680
24681 /* Parse the type-specifier-seq. */
24682 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24683 /*is_trailing_return=*/false,
24684 &type_specifiers);
24685 /* If it's a `)', then there is no declarator. */
24686 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24687 declarator = NULL;
24688 else
24689 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24690 /*ctor_dtor_or_conv_p=*/NULL,
24691 /*parenthesized_p=*/NULL,
24692 /*member_p=*/false,
24693 /*friend_p=*/false);
24694
24695 /* Restore the saved message. */
24696 parser->type_definition_forbidden_message = saved_message;
24697
24698 if (!type_specifiers.any_specifiers_p)
24699 return error_mark_node;
24700
24701 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24702 }
24703
24704 /* Parse a throw-expression.
24705
24706 throw-expression:
24707 throw assignment-expression [opt]
24708
24709 Returns a THROW_EXPR representing the throw-expression. */
24710
24711 static tree
24712 cp_parser_throw_expression (cp_parser* parser)
24713 {
24714 tree expression;
24715 cp_token* token;
24716
24717 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24718 token = cp_lexer_peek_token (parser->lexer);
24719 /* Figure out whether or not there is an assignment-expression
24720 following the "throw" keyword. */
24721 if (token->type == CPP_COMMA
24722 || token->type == CPP_SEMICOLON
24723 || token->type == CPP_CLOSE_PAREN
24724 || token->type == CPP_CLOSE_SQUARE
24725 || token->type == CPP_CLOSE_BRACE
24726 || token->type == CPP_COLON)
24727 expression = NULL_TREE;
24728 else
24729 expression = cp_parser_assignment_expression (parser);
24730
24731 return build_throw (expression);
24732 }
24733
24734 /* GNU Extensions */
24735
24736 /* Parse an (optional) asm-specification.
24737
24738 asm-specification:
24739 asm ( string-literal )
24740
24741 If the asm-specification is present, returns a STRING_CST
24742 corresponding to the string-literal. Otherwise, returns
24743 NULL_TREE. */
24744
24745 static tree
24746 cp_parser_asm_specification_opt (cp_parser* parser)
24747 {
24748 cp_token *token;
24749 tree asm_specification;
24750
24751 /* Peek at the next token. */
24752 token = cp_lexer_peek_token (parser->lexer);
24753 /* If the next token isn't the `asm' keyword, then there's no
24754 asm-specification. */
24755 if (!cp_parser_is_keyword (token, RID_ASM))
24756 return NULL_TREE;
24757
24758 /* Consume the `asm' token. */
24759 cp_lexer_consume_token (parser->lexer);
24760 /* Look for the `('. */
24761 matching_parens parens;
24762 parens.require_open (parser);
24763
24764 /* Look for the string-literal. */
24765 asm_specification = cp_parser_string_literal (parser, false, false);
24766
24767 /* Look for the `)'. */
24768 parens.require_close (parser);
24769
24770 return asm_specification;
24771 }
24772
24773 /* Parse an asm-operand-list.
24774
24775 asm-operand-list:
24776 asm-operand
24777 asm-operand-list , asm-operand
24778
24779 asm-operand:
24780 string-literal ( expression )
24781 [ string-literal ] string-literal ( expression )
24782
24783 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24784 each node is the expression. The TREE_PURPOSE is itself a
24785 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24786 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24787 is a STRING_CST for the string literal before the parenthesis. Returns
24788 ERROR_MARK_NODE if any of the operands are invalid. */
24789
24790 static tree
24791 cp_parser_asm_operand_list (cp_parser* parser)
24792 {
24793 tree asm_operands = NULL_TREE;
24794 bool invalid_operands = false;
24795
24796 while (true)
24797 {
24798 tree string_literal;
24799 tree expression;
24800 tree name;
24801
24802 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24803 {
24804 /* Consume the `[' token. */
24805 cp_lexer_consume_token (parser->lexer);
24806 /* Read the operand name. */
24807 name = cp_parser_identifier (parser);
24808 if (name != error_mark_node)
24809 name = build_string (IDENTIFIER_LENGTH (name),
24810 IDENTIFIER_POINTER (name));
24811 /* Look for the closing `]'. */
24812 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24813 }
24814 else
24815 name = NULL_TREE;
24816 /* Look for the string-literal. */
24817 string_literal = cp_parser_string_literal (parser, false, false);
24818
24819 /* Look for the `('. */
24820 matching_parens parens;
24821 parens.require_open (parser);
24822 /* Parse the expression. */
24823 expression = cp_parser_expression (parser);
24824 /* Look for the `)'. */
24825 parens.require_close (parser);
24826
24827 if (name == error_mark_node
24828 || string_literal == error_mark_node
24829 || expression == error_mark_node)
24830 invalid_operands = true;
24831
24832 /* Add this operand to the list. */
24833 asm_operands = tree_cons (build_tree_list (name, string_literal),
24834 expression,
24835 asm_operands);
24836 /* If the next token is not a `,', there are no more
24837 operands. */
24838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24839 break;
24840 /* Consume the `,'. */
24841 cp_lexer_consume_token (parser->lexer);
24842 }
24843
24844 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24845 }
24846
24847 /* Parse an asm-clobber-list.
24848
24849 asm-clobber-list:
24850 string-literal
24851 asm-clobber-list , string-literal
24852
24853 Returns a TREE_LIST, indicating the clobbers in the order that they
24854 appeared. The TREE_VALUE of each node is a STRING_CST. */
24855
24856 static tree
24857 cp_parser_asm_clobber_list (cp_parser* parser)
24858 {
24859 tree clobbers = NULL_TREE;
24860
24861 while (true)
24862 {
24863 tree string_literal;
24864
24865 /* Look for the string literal. */
24866 string_literal = cp_parser_string_literal (parser, false, false);
24867 /* Add it to the list. */
24868 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24869 /* If the next token is not a `,', then the list is
24870 complete. */
24871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24872 break;
24873 /* Consume the `,' token. */
24874 cp_lexer_consume_token (parser->lexer);
24875 }
24876
24877 return clobbers;
24878 }
24879
24880 /* Parse an asm-label-list.
24881
24882 asm-label-list:
24883 identifier
24884 asm-label-list , identifier
24885
24886 Returns a TREE_LIST, indicating the labels in the order that they
24887 appeared. The TREE_VALUE of each node is a label. */
24888
24889 static tree
24890 cp_parser_asm_label_list (cp_parser* parser)
24891 {
24892 tree labels = NULL_TREE;
24893
24894 while (true)
24895 {
24896 tree identifier, label, name;
24897
24898 /* Look for the identifier. */
24899 identifier = cp_parser_identifier (parser);
24900 if (!error_operand_p (identifier))
24901 {
24902 label = lookup_label (identifier);
24903 if (TREE_CODE (label) == LABEL_DECL)
24904 {
24905 TREE_USED (label) = 1;
24906 check_goto (label);
24907 name = build_string (IDENTIFIER_LENGTH (identifier),
24908 IDENTIFIER_POINTER (identifier));
24909 labels = tree_cons (name, label, labels);
24910 }
24911 }
24912 /* If the next token is not a `,', then the list is
24913 complete. */
24914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24915 break;
24916 /* Consume the `,' token. */
24917 cp_lexer_consume_token (parser->lexer);
24918 }
24919
24920 return nreverse (labels);
24921 }
24922
24923 /* Return TRUE iff the next tokens in the stream are possibly the
24924 beginning of a GNU extension attribute. */
24925
24926 static bool
24927 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24928 {
24929 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24930 }
24931
24932 /* Return TRUE iff the next tokens in the stream are possibly the
24933 beginning of a standard C++-11 attribute specifier. */
24934
24935 static bool
24936 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24937 {
24938 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24939 }
24940
24941 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24942 beginning of a standard C++-11 attribute specifier. */
24943
24944 static bool
24945 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24946 {
24947 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24948
24949 return (cxx_dialect >= cxx11
24950 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24951 || (token->type == CPP_OPEN_SQUARE
24952 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24953 && token->type == CPP_OPEN_SQUARE)));
24954 }
24955
24956 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24957 beginning of a GNU extension attribute. */
24958
24959 static bool
24960 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24961 {
24962 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24963
24964 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24965 }
24966
24967 /* Return true iff the next tokens can be the beginning of either a
24968 GNU attribute list, or a standard C++11 attribute sequence. */
24969
24970 static bool
24971 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24972 {
24973 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24974 || cp_next_tokens_can_be_std_attribute_p (parser));
24975 }
24976
24977 /* Return true iff the next Nth tokens can be the beginning of either
24978 a GNU attribute list, or a standard C++11 attribute sequence. */
24979
24980 static bool
24981 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24982 {
24983 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24984 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24985 }
24986
24987 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24988 of GNU attributes, or return NULL. */
24989
24990 static tree
24991 cp_parser_attributes_opt (cp_parser *parser)
24992 {
24993 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24994 return cp_parser_gnu_attributes_opt (parser);
24995 return cp_parser_std_attribute_spec_seq (parser);
24996 }
24997
24998 #define CILK_SIMD_FN_CLAUSE_MASK \
24999 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
25000 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
25001 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
25002 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
25003 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
25004
25005 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
25006 vector [(<clauses>)] */
25007
25008 static void
25009 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
25010 {
25011 bool first_p = parser->cilk_simd_fn_info == NULL;
25012 cp_token *token = v_token;
25013 if (first_p)
25014 {
25015 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
25016 parser->cilk_simd_fn_info->error_seen = false;
25017 parser->cilk_simd_fn_info->fndecl_seen = false;
25018 parser->cilk_simd_fn_info->tokens = vNULL;
25019 parser->cilk_simd_fn_info->clauses = NULL_TREE;
25020 }
25021 int paren_scope = 0;
25022 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25023 {
25024 cp_lexer_consume_token (parser->lexer);
25025 v_token = cp_lexer_peek_token (parser->lexer);
25026 paren_scope++;
25027 }
25028 while (paren_scope > 0)
25029 {
25030 token = cp_lexer_peek_token (parser->lexer);
25031 if (token->type == CPP_OPEN_PAREN)
25032 paren_scope++;
25033 else if (token->type == CPP_CLOSE_PAREN)
25034 paren_scope--;
25035 /* Do not push the last ')' */
25036 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
25037 cp_lexer_consume_token (parser->lexer);
25038 }
25039
25040 token->type = CPP_PRAGMA_EOL;
25041 parser->lexer->next_token = token;
25042 cp_lexer_consume_token (parser->lexer);
25043
25044 struct cp_token_cache *cp
25045 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
25046 parser->cilk_simd_fn_info->tokens.safe_push (cp);
25047 }
25048
25049 /* Parse an (optional) series of attributes.
25050
25051 attributes:
25052 attributes attribute
25053
25054 attribute:
25055 __attribute__ (( attribute-list [opt] ))
25056
25057 The return value is as for cp_parser_gnu_attribute_list. */
25058
25059 static tree
25060 cp_parser_gnu_attributes_opt (cp_parser* parser)
25061 {
25062 tree attributes = NULL_TREE;
25063
25064 while (true)
25065 {
25066 cp_token *token;
25067 tree attribute_list;
25068 bool ok = true;
25069
25070 /* Peek at the next token. */
25071 token = cp_lexer_peek_token (parser->lexer);
25072 /* If it's not `__attribute__', then we're done. */
25073 if (token->keyword != RID_ATTRIBUTE)
25074 break;
25075
25076 /* Consume the `__attribute__' keyword. */
25077 cp_lexer_consume_token (parser->lexer);
25078 /* Look for the two `(' tokens. */
25079 matching_parens outer_parens;
25080 outer_parens.require_open (parser);
25081 matching_parens inner_parens;
25082 inner_parens.require_open (parser);
25083
25084 /* Peek at the next token. */
25085 token = cp_lexer_peek_token (parser->lexer);
25086 if (token->type != CPP_CLOSE_PAREN)
25087 /* Parse the attribute-list. */
25088 attribute_list = cp_parser_gnu_attribute_list (parser);
25089 else
25090 /* If the next token is a `)', then there is no attribute
25091 list. */
25092 attribute_list = NULL;
25093
25094 /* Look for the two `)' tokens. */
25095 if (!inner_parens.require_close (parser))
25096 ok = false;
25097 if (!outer_parens.require_close (parser))
25098 ok = false;
25099 if (!ok)
25100 cp_parser_skip_to_end_of_statement (parser);
25101
25102 /* Add these new attributes to the list. */
25103 attributes = chainon (attributes, attribute_list);
25104 }
25105
25106 return attributes;
25107 }
25108
25109 /* Parse a GNU attribute-list.
25110
25111 attribute-list:
25112 attribute
25113 attribute-list , attribute
25114
25115 attribute:
25116 identifier
25117 identifier ( identifier )
25118 identifier ( identifier , expression-list )
25119 identifier ( expression-list )
25120
25121 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25122 to an attribute. The TREE_PURPOSE of each node is the identifier
25123 indicating which attribute is in use. The TREE_VALUE represents
25124 the arguments, if any. */
25125
25126 static tree
25127 cp_parser_gnu_attribute_list (cp_parser* parser)
25128 {
25129 tree attribute_list = NULL_TREE;
25130 bool save_translate_strings_p = parser->translate_strings_p;
25131
25132 parser->translate_strings_p = false;
25133 while (true)
25134 {
25135 cp_token *token;
25136 tree identifier;
25137 tree attribute;
25138
25139 /* Look for the identifier. We also allow keywords here; for
25140 example `__attribute__ ((const))' is legal. */
25141 token = cp_lexer_peek_token (parser->lexer);
25142 if (token->type == CPP_NAME
25143 || token->type == CPP_KEYWORD)
25144 {
25145 tree arguments = NULL_TREE;
25146
25147 /* Consume the token, but save it since we need it for the
25148 SIMD enabled function parsing. */
25149 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25150
25151 /* Save away the identifier that indicates which attribute
25152 this is. */
25153 identifier = (token->type == CPP_KEYWORD)
25154 /* For keywords, use the canonical spelling, not the
25155 parsed identifier. */
25156 ? ridpointers[(int) token->keyword]
25157 : id_token->u.value;
25158
25159 identifier = canonicalize_attr_name (identifier);
25160 attribute = build_tree_list (identifier, NULL_TREE);
25161
25162 /* Peek at the next token. */
25163 token = cp_lexer_peek_token (parser->lexer);
25164 /* If it's an `(', then parse the attribute arguments. */
25165 if (token->type == CPP_OPEN_PAREN)
25166 {
25167 vec<tree, va_gc> *vec;
25168 int attr_flag = (attribute_takes_identifier_p (identifier)
25169 ? id_attr : normal_attr);
25170 if (is_cilkplus_vector_p (identifier))
25171 {
25172 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
25173 continue;
25174 }
25175 else
25176 vec = cp_parser_parenthesized_expression_list
25177 (parser, attr_flag, /*cast_p=*/false,
25178 /*allow_expansion_p=*/false,
25179 /*non_constant_p=*/NULL);
25180 if (vec == NULL)
25181 arguments = error_mark_node;
25182 else
25183 {
25184 arguments = build_tree_list_vec (vec);
25185 release_tree_vector (vec);
25186 }
25187 /* Save the arguments away. */
25188 TREE_VALUE (attribute) = arguments;
25189 }
25190 else if (is_cilkplus_vector_p (identifier))
25191 {
25192 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
25193 continue;
25194 }
25195
25196 if (arguments != error_mark_node)
25197 {
25198 /* Add this attribute to the list. */
25199 TREE_CHAIN (attribute) = attribute_list;
25200 attribute_list = attribute;
25201 }
25202
25203 token = cp_lexer_peek_token (parser->lexer);
25204 }
25205 /* Now, look for more attributes. If the next token isn't a
25206 `,', we're done. */
25207 if (token->type != CPP_COMMA)
25208 break;
25209
25210 /* Consume the comma and keep going. */
25211 cp_lexer_consume_token (parser->lexer);
25212 }
25213 parser->translate_strings_p = save_translate_strings_p;
25214
25215 /* We built up the list in reverse order. */
25216 return nreverse (attribute_list);
25217 }
25218
25219 /* Parse a standard C++11 attribute.
25220
25221 The returned representation is a TREE_LIST which TREE_PURPOSE is
25222 the scoped name of the attribute, and the TREE_VALUE is its
25223 arguments list.
25224
25225 Note that the scoped name of the attribute is itself a TREE_LIST
25226 which TREE_PURPOSE is the namespace of the attribute, and
25227 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25228 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25229 and which TREE_PURPOSE is directly the attribute name.
25230
25231 Clients of the attribute code should use get_attribute_namespace
25232 and get_attribute_name to get the actual namespace and name of
25233 attributes, regardless of their being GNU or C++11 attributes.
25234
25235 attribute:
25236 attribute-token attribute-argument-clause [opt]
25237
25238 attribute-token:
25239 identifier
25240 attribute-scoped-token
25241
25242 attribute-scoped-token:
25243 attribute-namespace :: identifier
25244
25245 attribute-namespace:
25246 identifier
25247
25248 attribute-argument-clause:
25249 ( balanced-token-seq )
25250
25251 balanced-token-seq:
25252 balanced-token [opt]
25253 balanced-token-seq balanced-token
25254
25255 balanced-token:
25256 ( balanced-token-seq )
25257 [ balanced-token-seq ]
25258 { balanced-token-seq }. */
25259
25260 static tree
25261 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25262 {
25263 tree attribute, attr_id = NULL_TREE, arguments;
25264 cp_token *token;
25265
25266 /* First, parse name of the attribute, a.k.a attribute-token. */
25267
25268 token = cp_lexer_peek_token (parser->lexer);
25269 if (token->type == CPP_NAME)
25270 attr_id = token->u.value;
25271 else if (token->type == CPP_KEYWORD)
25272 attr_id = ridpointers[(int) token->keyword];
25273 else if (token->flags & NAMED_OP)
25274 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25275
25276 if (attr_id == NULL_TREE)
25277 return NULL_TREE;
25278
25279 cp_lexer_consume_token (parser->lexer);
25280
25281 token = cp_lexer_peek_token (parser->lexer);
25282 if (token->type == CPP_SCOPE)
25283 {
25284 /* We are seeing a scoped attribute token. */
25285
25286 cp_lexer_consume_token (parser->lexer);
25287 if (attr_ns)
25288 error_at (token->location, "attribute using prefix used together "
25289 "with scoped attribute token");
25290 attr_ns = attr_id;
25291
25292 token = cp_lexer_consume_token (parser->lexer);
25293 if (token->type == CPP_NAME)
25294 attr_id = token->u.value;
25295 else if (token->type == CPP_KEYWORD)
25296 attr_id = ridpointers[(int) token->keyword];
25297 else if (token->flags & NAMED_OP)
25298 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25299 else
25300 {
25301 error_at (token->location,
25302 "expected an identifier for the attribute name");
25303 return error_mark_node;
25304 }
25305
25306 attr_id = canonicalize_attr_name (attr_id);
25307 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25308 NULL_TREE);
25309 token = cp_lexer_peek_token (parser->lexer);
25310 }
25311 else if (attr_ns)
25312 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25313 NULL_TREE);
25314 else
25315 {
25316 attr_id = canonicalize_attr_name (attr_id);
25317 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25318 NULL_TREE);
25319 /* C++11 noreturn attribute is equivalent to GNU's. */
25320 if (is_attribute_p ("noreturn", attr_id))
25321 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25322 /* C++14 deprecated attribute is equivalent to GNU's. */
25323 else if (is_attribute_p ("deprecated", attr_id))
25324 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25325 /* C++17 fallthrough attribute is equivalent to GNU's. */
25326 else if (is_attribute_p ("fallthrough", attr_id))
25327 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25328 /* Transactional Memory TS optimize_for_synchronized attribute is
25329 equivalent to GNU transaction_callable. */
25330 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25331 TREE_PURPOSE (attribute)
25332 = get_identifier ("transaction_callable");
25333 /* Transactional Memory attributes are GNU attributes. */
25334 else if (tm_attr_to_mask (attr_id))
25335 TREE_PURPOSE (attribute) = attr_id;
25336 }
25337
25338 /* Now parse the optional argument clause of the attribute. */
25339
25340 if (token->type != CPP_OPEN_PAREN)
25341 return attribute;
25342
25343 {
25344 vec<tree, va_gc> *vec;
25345 int attr_flag = normal_attr;
25346
25347 if (attr_ns == get_identifier ("gnu")
25348 && attribute_takes_identifier_p (attr_id))
25349 /* A GNU attribute that takes an identifier in parameter. */
25350 attr_flag = id_attr;
25351
25352 vec = cp_parser_parenthesized_expression_list
25353 (parser, attr_flag, /*cast_p=*/false,
25354 /*allow_expansion_p=*/true,
25355 /*non_constant_p=*/NULL);
25356 if (vec == NULL)
25357 arguments = error_mark_node;
25358 else
25359 {
25360 arguments = build_tree_list_vec (vec);
25361 release_tree_vector (vec);
25362 }
25363
25364 if (arguments == error_mark_node)
25365 attribute = error_mark_node;
25366 else
25367 TREE_VALUE (attribute) = arguments;
25368 }
25369
25370 return attribute;
25371 }
25372
25373 /* Check that the attribute ATTRIBUTE appears at most once in the
25374 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25375 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25376 isn't implemented yet in GCC. */
25377
25378 static void
25379 cp_parser_check_std_attribute (tree attributes, tree attribute)
25380 {
25381 if (attributes)
25382 {
25383 tree name = get_attribute_name (attribute);
25384 if (is_attribute_p ("noreturn", name)
25385 && lookup_attribute ("noreturn", attributes))
25386 error ("attribute %<noreturn%> can appear at most once "
25387 "in an attribute-list");
25388 else if (is_attribute_p ("deprecated", name)
25389 && lookup_attribute ("deprecated", attributes))
25390 error ("attribute %<deprecated%> can appear at most once "
25391 "in an attribute-list");
25392 }
25393 }
25394
25395 /* Parse a list of standard C++-11 attributes.
25396
25397 attribute-list:
25398 attribute [opt]
25399 attribute-list , attribute[opt]
25400 attribute ...
25401 attribute-list , attribute ...
25402 */
25403
25404 static tree
25405 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25406 {
25407 tree attributes = NULL_TREE, attribute = NULL_TREE;
25408 cp_token *token = NULL;
25409
25410 while (true)
25411 {
25412 attribute = cp_parser_std_attribute (parser, attr_ns);
25413 if (attribute == error_mark_node)
25414 break;
25415 if (attribute != NULL_TREE)
25416 {
25417 cp_parser_check_std_attribute (attributes, attribute);
25418 TREE_CHAIN (attribute) = attributes;
25419 attributes = attribute;
25420 }
25421 token = cp_lexer_peek_token (parser->lexer);
25422 if (token->type == CPP_ELLIPSIS)
25423 {
25424 cp_lexer_consume_token (parser->lexer);
25425 if (attribute == NULL_TREE)
25426 error_at (token->location,
25427 "expected attribute before %<...%>");
25428 else
25429 {
25430 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25431 if (pack == error_mark_node)
25432 return error_mark_node;
25433 TREE_VALUE (attribute) = pack;
25434 }
25435 token = cp_lexer_peek_token (parser->lexer);
25436 }
25437 if (token->type != CPP_COMMA)
25438 break;
25439 cp_lexer_consume_token (parser->lexer);
25440 }
25441 attributes = nreverse (attributes);
25442 return attributes;
25443 }
25444
25445 /* Parse a standard C++-11 attribute specifier.
25446
25447 attribute-specifier:
25448 [ [ attribute-using-prefix [opt] attribute-list ] ]
25449 alignment-specifier
25450
25451 attribute-using-prefix:
25452 using attribute-namespace :
25453
25454 alignment-specifier:
25455 alignas ( type-id ... [opt] )
25456 alignas ( alignment-expression ... [opt] ). */
25457
25458 static tree
25459 cp_parser_std_attribute_spec (cp_parser *parser)
25460 {
25461 tree attributes = NULL_TREE;
25462 cp_token *token = cp_lexer_peek_token (parser->lexer);
25463
25464 if (token->type == CPP_OPEN_SQUARE
25465 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25466 {
25467 tree attr_ns = NULL_TREE;
25468
25469 cp_lexer_consume_token (parser->lexer);
25470 cp_lexer_consume_token (parser->lexer);
25471
25472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25473 {
25474 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25475 if (token->type == CPP_NAME)
25476 attr_ns = token->u.value;
25477 else if (token->type == CPP_KEYWORD)
25478 attr_ns = ridpointers[(int) token->keyword];
25479 else if (token->flags & NAMED_OP)
25480 attr_ns = get_identifier (cpp_type2name (token->type,
25481 token->flags));
25482 if (attr_ns
25483 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25484 {
25485 if (cxx_dialect < cxx17
25486 && !in_system_header_at (input_location))
25487 pedwarn (input_location, 0,
25488 "attribute using prefix only available "
25489 "with -std=c++17 or -std=gnu++17");
25490
25491 cp_lexer_consume_token (parser->lexer);
25492 cp_lexer_consume_token (parser->lexer);
25493 cp_lexer_consume_token (parser->lexer);
25494 }
25495 else
25496 attr_ns = NULL_TREE;
25497 }
25498
25499 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25500
25501 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25502 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25503 cp_parser_skip_to_end_of_statement (parser);
25504 else
25505 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25506 when we are sure that we have actually parsed them. */
25507 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25508 }
25509 else
25510 {
25511 tree alignas_expr;
25512
25513 /* Look for an alignment-specifier. */
25514
25515 token = cp_lexer_peek_token (parser->lexer);
25516
25517 if (token->type != CPP_KEYWORD
25518 || token->keyword != RID_ALIGNAS)
25519 return NULL_TREE;
25520
25521 cp_lexer_consume_token (parser->lexer);
25522 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25523
25524 matching_parens parens;
25525 if (!parens.require_open (parser))
25526 {
25527 cp_parser_error (parser, "expected %<(%>");
25528 return error_mark_node;
25529 }
25530
25531 cp_parser_parse_tentatively (parser);
25532 alignas_expr = cp_parser_type_id (parser);
25533
25534 if (!cp_parser_parse_definitely (parser))
25535 {
25536 alignas_expr = cp_parser_assignment_expression (parser);
25537 if (alignas_expr == error_mark_node)
25538 cp_parser_skip_to_end_of_statement (parser);
25539 if (alignas_expr == NULL_TREE
25540 || alignas_expr == error_mark_node)
25541 return alignas_expr;
25542 }
25543
25544 alignas_expr = cxx_alignas_expr (alignas_expr);
25545 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25546
25547 /* Handle alignas (pack...). */
25548 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25549 {
25550 cp_lexer_consume_token (parser->lexer);
25551 alignas_expr = make_pack_expansion (alignas_expr);
25552 }
25553
25554 /* Something went wrong, so don't build the attribute. */
25555 if (alignas_expr == error_mark_node)
25556 return error_mark_node;
25557
25558 if (!parens.require_close (parser))
25559 {
25560 cp_parser_error (parser, "expected %<)%>");
25561 return error_mark_node;
25562 }
25563
25564 /* Build the C++-11 representation of an 'aligned'
25565 attribute. */
25566 attributes =
25567 build_tree_list (build_tree_list (get_identifier ("gnu"),
25568 get_identifier ("aligned")),
25569 alignas_expr);
25570 }
25571
25572 return attributes;
25573 }
25574
25575 /* Parse a standard C++-11 attribute-specifier-seq.
25576
25577 attribute-specifier-seq:
25578 attribute-specifier-seq [opt] attribute-specifier
25579 */
25580
25581 static tree
25582 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25583 {
25584 tree attr_specs = NULL_TREE;
25585 tree attr_last = NULL_TREE;
25586
25587 while (true)
25588 {
25589 tree attr_spec = cp_parser_std_attribute_spec (parser);
25590 if (attr_spec == NULL_TREE)
25591 break;
25592 if (attr_spec == error_mark_node)
25593 return error_mark_node;
25594
25595 if (attr_last)
25596 TREE_CHAIN (attr_last) = attr_spec;
25597 else
25598 attr_specs = attr_last = attr_spec;
25599 attr_last = tree_last (attr_last);
25600 }
25601
25602 return attr_specs;
25603 }
25604
25605 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25606 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25607 current value of the PEDANTIC flag, regardless of whether or not
25608 the `__extension__' keyword is present. The caller is responsible
25609 for restoring the value of the PEDANTIC flag. */
25610
25611 static bool
25612 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25613 {
25614 /* Save the old value of the PEDANTIC flag. */
25615 *saved_pedantic = pedantic;
25616
25617 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25618 {
25619 /* Consume the `__extension__' token. */
25620 cp_lexer_consume_token (parser->lexer);
25621 /* We're not being pedantic while the `__extension__' keyword is
25622 in effect. */
25623 pedantic = 0;
25624
25625 return true;
25626 }
25627
25628 return false;
25629 }
25630
25631 /* Parse a label declaration.
25632
25633 label-declaration:
25634 __label__ label-declarator-seq ;
25635
25636 label-declarator-seq:
25637 identifier , label-declarator-seq
25638 identifier */
25639
25640 static void
25641 cp_parser_label_declaration (cp_parser* parser)
25642 {
25643 /* Look for the `__label__' keyword. */
25644 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25645
25646 while (true)
25647 {
25648 tree identifier;
25649
25650 /* Look for an identifier. */
25651 identifier = cp_parser_identifier (parser);
25652 /* If we failed, stop. */
25653 if (identifier == error_mark_node)
25654 break;
25655 /* Declare it as a label. */
25656 finish_label_decl (identifier);
25657 /* If the next token is a `;', stop. */
25658 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25659 break;
25660 /* Look for the `,' separating the label declarations. */
25661 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25662 }
25663
25664 /* Look for the final `;'. */
25665 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25666 }
25667
25668 // -------------------------------------------------------------------------- //
25669 // Requires Clause
25670
25671 // Parse a requires clause.
25672 //
25673 // requires-clause:
25674 // 'requires' logical-or-expression
25675 //
25676 // The required logical-or-expression must be a constant expression. Note
25677 // that we don't check that the expression is constepxr here. We defer until
25678 // we analyze constraints and then, we only check atomic constraints.
25679 static tree
25680 cp_parser_requires_clause (cp_parser *parser)
25681 {
25682 // Parse the requires clause so that it is not automatically folded.
25683 ++processing_template_decl;
25684 tree expr = cp_parser_binary_expression (parser, false, false,
25685 PREC_NOT_OPERATOR, NULL);
25686 if (check_for_bare_parameter_packs (expr))
25687 expr = error_mark_node;
25688 --processing_template_decl;
25689 return expr;
25690 }
25691
25692 // Optionally parse a requires clause:
25693 static tree
25694 cp_parser_requires_clause_opt (cp_parser *parser)
25695 {
25696 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25697 if (tok->keyword != RID_REQUIRES)
25698 {
25699 if (!flag_concepts && tok->type == CPP_NAME
25700 && tok->u.value == ridpointers[RID_REQUIRES])
25701 {
25702 error_at (cp_lexer_peek_token (parser->lexer)->location,
25703 "%<requires%> only available with -fconcepts");
25704 /* Parse and discard the requires-clause. */
25705 cp_lexer_consume_token (parser->lexer);
25706 cp_parser_requires_clause (parser);
25707 }
25708 return NULL_TREE;
25709 }
25710 cp_lexer_consume_token (parser->lexer);
25711 return cp_parser_requires_clause (parser);
25712 }
25713
25714
25715 /*---------------------------------------------------------------------------
25716 Requires expressions
25717 ---------------------------------------------------------------------------*/
25718
25719 /* Parse a requires expression
25720
25721 requirement-expression:
25722 'requires' requirement-parameter-list [opt] requirement-body */
25723 static tree
25724 cp_parser_requires_expression (cp_parser *parser)
25725 {
25726 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25727 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25728
25729 /* A requires-expression shall appear only within a concept
25730 definition or a requires-clause.
25731
25732 TODO: Implement this diagnostic correctly. */
25733 if (!processing_template_decl)
25734 {
25735 error_at (loc, "a requires expression cannot appear outside a template");
25736 cp_parser_skip_to_end_of_statement (parser);
25737 return error_mark_node;
25738 }
25739
25740 tree parms, reqs;
25741 {
25742 /* Local parameters are delared as variables within the scope
25743 of the expression. They are not visible past the end of
25744 the expression. Expressions within the requires-expression
25745 are unevaluated. */
25746 struct scope_sentinel
25747 {
25748 scope_sentinel ()
25749 {
25750 ++cp_unevaluated_operand;
25751 begin_scope (sk_block, NULL_TREE);
25752 }
25753
25754 ~scope_sentinel ()
25755 {
25756 pop_bindings_and_leave_scope ();
25757 --cp_unevaluated_operand;
25758 }
25759 } s;
25760
25761 /* Parse the optional parameter list. */
25762 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25763 {
25764 parms = cp_parser_requirement_parameter_list (parser);
25765 if (parms == error_mark_node)
25766 return error_mark_node;
25767 }
25768 else
25769 parms = NULL_TREE;
25770
25771 /* Parse the requirement body. */
25772 reqs = cp_parser_requirement_body (parser);
25773 if (reqs == error_mark_node)
25774 return error_mark_node;
25775 }
25776
25777 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25778 the parm chain. */
25779 grokparms (parms, &parms);
25780 return finish_requires_expr (parms, reqs);
25781 }
25782
25783 /* Parse a parameterized requirement.
25784
25785 requirement-parameter-list:
25786 '(' parameter-declaration-clause ')' */
25787 static tree
25788 cp_parser_requirement_parameter_list (cp_parser *parser)
25789 {
25790 matching_parens parens;
25791 if (!parens.require_open (parser))
25792 return error_mark_node;
25793
25794 tree parms = cp_parser_parameter_declaration_clause (parser);
25795
25796 if (!parens.require_close (parser))
25797 return error_mark_node;
25798
25799 return parms;
25800 }
25801
25802 /* Parse the body of a requirement.
25803
25804 requirement-body:
25805 '{' requirement-list '}' */
25806 static tree
25807 cp_parser_requirement_body (cp_parser *parser)
25808 {
25809 matching_braces braces;
25810 if (!braces.require_open (parser))
25811 return error_mark_node;
25812
25813 tree reqs = cp_parser_requirement_list (parser);
25814
25815 if (!braces.require_close (parser))
25816 return error_mark_node;
25817
25818 return reqs;
25819 }
25820
25821 /* Parse a list of requirements.
25822
25823 requirement-list:
25824 requirement
25825 requirement-list ';' requirement[opt] */
25826 static tree
25827 cp_parser_requirement_list (cp_parser *parser)
25828 {
25829 tree result = NULL_TREE;
25830 while (true)
25831 {
25832 tree req = cp_parser_requirement (parser);
25833 if (req == error_mark_node)
25834 return error_mark_node;
25835
25836 result = tree_cons (NULL_TREE, req, result);
25837
25838 /* If we see a semi-colon, consume it. */
25839 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25840 cp_lexer_consume_token (parser->lexer);
25841
25842 /* Stop processing at the end of the list. */
25843 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25844 break;
25845 }
25846
25847 /* Reverse the order of requirements so they are analyzed in
25848 declaration order. */
25849 return nreverse (result);
25850 }
25851
25852 /* Parse a syntactic requirement or type requirement.
25853
25854 requirement:
25855 simple-requirement
25856 compound-requirement
25857 type-requirement
25858 nested-requirement */
25859 static tree
25860 cp_parser_requirement (cp_parser *parser)
25861 {
25862 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25863 return cp_parser_compound_requirement (parser);
25864 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25865 return cp_parser_type_requirement (parser);
25866 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25867 return cp_parser_nested_requirement (parser);
25868 else
25869 return cp_parser_simple_requirement (parser);
25870 }
25871
25872 /* Parse a simple requirement.
25873
25874 simple-requirement:
25875 expression ';' */
25876 static tree
25877 cp_parser_simple_requirement (cp_parser *parser)
25878 {
25879 tree expr = cp_parser_expression (parser, NULL, false, false);
25880 if (!expr || expr == error_mark_node)
25881 return error_mark_node;
25882
25883 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25884 return error_mark_node;
25885
25886 return finish_simple_requirement (expr);
25887 }
25888
25889 /* Parse a type requirement
25890
25891 type-requirement
25892 nested-name-specifier [opt] required-type-name ';'
25893
25894 required-type-name:
25895 type-name
25896 'template' [opt] simple-template-id */
25897 static tree
25898 cp_parser_type_requirement (cp_parser *parser)
25899 {
25900 cp_lexer_consume_token (parser->lexer);
25901
25902 // Save the scope before parsing name specifiers.
25903 tree saved_scope = parser->scope;
25904 tree saved_object_scope = parser->object_scope;
25905 tree saved_qualifying_scope = parser->qualifying_scope;
25906 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25907 cp_parser_nested_name_specifier_opt (parser,
25908 /*typename_keyword_p=*/true,
25909 /*check_dependency_p=*/false,
25910 /*type_p=*/true,
25911 /*is_declaration=*/false);
25912
25913 tree type;
25914 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25915 {
25916 cp_lexer_consume_token (parser->lexer);
25917 type = cp_parser_template_id (parser,
25918 /*template_keyword_p=*/true,
25919 /*check_dependency=*/false,
25920 /*tag_type=*/none_type,
25921 /*is_declaration=*/false);
25922 type = make_typename_type (parser->scope, type, typename_type,
25923 /*complain=*/tf_error);
25924 }
25925 else
25926 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25927
25928 if (TREE_CODE (type) == TYPE_DECL)
25929 type = TREE_TYPE (type);
25930
25931 parser->scope = saved_scope;
25932 parser->object_scope = saved_object_scope;
25933 parser->qualifying_scope = saved_qualifying_scope;
25934
25935 if (type == error_mark_node)
25936 cp_parser_skip_to_end_of_statement (parser);
25937
25938 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25939 return error_mark_node;
25940 if (type == error_mark_node)
25941 return error_mark_node;
25942
25943 return finish_type_requirement (type);
25944 }
25945
25946 /* Parse a compound requirement
25947
25948 compound-requirement:
25949 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25950 static tree
25951 cp_parser_compound_requirement (cp_parser *parser)
25952 {
25953 /* Parse an expression enclosed in '{ }'s. */
25954 matching_braces braces;
25955 if (!braces.require_open (parser))
25956 return error_mark_node;
25957
25958 tree expr = cp_parser_expression (parser, NULL, false, false);
25959 if (!expr || expr == error_mark_node)
25960 return error_mark_node;
25961
25962 if (!braces.require_close (parser))
25963 return error_mark_node;
25964
25965 /* Parse the optional noexcept. */
25966 bool noexcept_p = false;
25967 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25968 {
25969 cp_lexer_consume_token (parser->lexer);
25970 noexcept_p = true;
25971 }
25972
25973 /* Parse the optional trailing return type. */
25974 tree type = NULL_TREE;
25975 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25976 {
25977 cp_lexer_consume_token (parser->lexer);
25978 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25979 parser->in_result_type_constraint_p = true;
25980 type = cp_parser_trailing_type_id (parser);
25981 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25982 if (type == error_mark_node)
25983 return error_mark_node;
25984 }
25985
25986 return finish_compound_requirement (expr, type, noexcept_p);
25987 }
25988
25989 /* Parse a nested requirement. This is the same as a requires clause.
25990
25991 nested-requirement:
25992 requires-clause */
25993 static tree
25994 cp_parser_nested_requirement (cp_parser *parser)
25995 {
25996 cp_lexer_consume_token (parser->lexer);
25997 tree req = cp_parser_requires_clause (parser);
25998 if (req == error_mark_node)
25999 return error_mark_node;
26000 return finish_nested_requirement (req);
26001 }
26002
26003 /* Support Functions */
26004
26005 /* Return the appropriate prefer_type argument for lookup_name_real based on
26006 tag_type and template_mem_access. */
26007
26008 static inline int
26009 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26010 {
26011 /* DR 141: When looking in the current enclosing context for a template-name
26012 after -> or ., only consider class templates. */
26013 if (template_mem_access)
26014 return 2;
26015 switch (tag_type)
26016 {
26017 case none_type: return 0; // No preference.
26018 case scope_type: return 1; // Type or namespace.
26019 default: return 2; // Type only.
26020 }
26021 }
26022
26023 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26024 NAME should have one of the representations used for an
26025 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26026 is returned. If PARSER->SCOPE is a dependent type, then a
26027 SCOPE_REF is returned.
26028
26029 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26030 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26031 was formed. Abstractly, such entities should not be passed to this
26032 function, because they do not need to be looked up, but it is
26033 simpler to check for this special case here, rather than at the
26034 call-sites.
26035
26036 In cases not explicitly covered above, this function returns a
26037 DECL, OVERLOAD, or baselink representing the result of the lookup.
26038 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26039 is returned.
26040
26041 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26042 (e.g., "struct") that was used. In that case bindings that do not
26043 refer to types are ignored.
26044
26045 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26046 ignored.
26047
26048 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26049 are ignored.
26050
26051 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26052 types.
26053
26054 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26055 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26056 NULL_TREE otherwise. */
26057
26058 static cp_expr
26059 cp_parser_lookup_name (cp_parser *parser, tree name,
26060 enum tag_types tag_type,
26061 bool is_template,
26062 bool is_namespace,
26063 bool check_dependency,
26064 tree *ambiguous_decls,
26065 location_t name_location)
26066 {
26067 tree decl;
26068 tree object_type = parser->context->object_type;
26069
26070 /* Assume that the lookup will be unambiguous. */
26071 if (ambiguous_decls)
26072 *ambiguous_decls = NULL_TREE;
26073
26074 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26075 no longer valid. Note that if we are parsing tentatively, and
26076 the parse fails, OBJECT_TYPE will be automatically restored. */
26077 parser->context->object_type = NULL_TREE;
26078
26079 if (name == error_mark_node)
26080 return error_mark_node;
26081
26082 /* A template-id has already been resolved; there is no lookup to
26083 do. */
26084 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26085 return name;
26086 if (BASELINK_P (name))
26087 {
26088 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26089 == TEMPLATE_ID_EXPR);
26090 return name;
26091 }
26092
26093 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26094 it should already have been checked to make sure that the name
26095 used matches the type being destroyed. */
26096 if (TREE_CODE (name) == BIT_NOT_EXPR)
26097 {
26098 tree type;
26099
26100 /* Figure out to which type this destructor applies. */
26101 if (parser->scope)
26102 type = parser->scope;
26103 else if (object_type)
26104 type = object_type;
26105 else
26106 type = current_class_type;
26107 /* If that's not a class type, there is no destructor. */
26108 if (!type || !CLASS_TYPE_P (type))
26109 return error_mark_node;
26110
26111 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26112 lazily_declare_fn (sfk_destructor, type);
26113
26114 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26115 return dtor;
26116
26117 return error_mark_node;
26118 }
26119
26120 /* By this point, the NAME should be an ordinary identifier. If
26121 the id-expression was a qualified name, the qualifying scope is
26122 stored in PARSER->SCOPE at this point. */
26123 gcc_assert (identifier_p (name));
26124
26125 /* Perform the lookup. */
26126 if (parser->scope)
26127 {
26128 bool dependent_p;
26129
26130 if (parser->scope == error_mark_node)
26131 return error_mark_node;
26132
26133 /* If the SCOPE is dependent, the lookup must be deferred until
26134 the template is instantiated -- unless we are explicitly
26135 looking up names in uninstantiated templates. Even then, we
26136 cannot look up the name if the scope is not a class type; it
26137 might, for example, be a template type parameter. */
26138 dependent_p = (TYPE_P (parser->scope)
26139 && dependent_scope_p (parser->scope));
26140 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26141 && dependent_p)
26142 /* Defer lookup. */
26143 decl = error_mark_node;
26144 else
26145 {
26146 tree pushed_scope = NULL_TREE;
26147
26148 /* If PARSER->SCOPE is a dependent type, then it must be a
26149 class type, and we must not be checking dependencies;
26150 otherwise, we would have processed this lookup above. So
26151 that PARSER->SCOPE is not considered a dependent base by
26152 lookup_member, we must enter the scope here. */
26153 if (dependent_p)
26154 pushed_scope = push_scope (parser->scope);
26155
26156 /* If the PARSER->SCOPE is a template specialization, it
26157 may be instantiated during name lookup. In that case,
26158 errors may be issued. Even if we rollback the current
26159 tentative parse, those errors are valid. */
26160 decl = lookup_qualified_name (parser->scope, name,
26161 prefer_type_arg (tag_type),
26162 /*complain=*/true);
26163
26164 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26165 lookup result and the nested-name-specifier nominates a class C:
26166 * if the name specified after the nested-name-specifier, when
26167 looked up in C, is the injected-class-name of C (Clause 9), or
26168 * if the name specified after the nested-name-specifier is the
26169 same as the identifier or the simple-template-id's template-
26170 name in the last component of the nested-name-specifier,
26171 the name is instead considered to name the constructor of
26172 class C. [ Note: for example, the constructor is not an
26173 acceptable lookup result in an elaborated-type-specifier so
26174 the constructor would not be used in place of the
26175 injected-class-name. --end note ] Such a constructor name
26176 shall be used only in the declarator-id of a declaration that
26177 names a constructor or in a using-declaration. */
26178 if (tag_type == none_type
26179 && DECL_SELF_REFERENCE_P (decl)
26180 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26181 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26182 prefer_type_arg (tag_type),
26183 /*complain=*/true);
26184
26185 /* If we have a single function from a using decl, pull it out. */
26186 if (TREE_CODE (decl) == OVERLOAD
26187 && !really_overloaded_fn (decl))
26188 decl = OVL_FUNCTION (decl);
26189
26190 if (pushed_scope)
26191 pop_scope (pushed_scope);
26192 }
26193
26194 /* If the scope is a dependent type and either we deferred lookup or
26195 we did lookup but didn't find the name, rememeber the name. */
26196 if (decl == error_mark_node && TYPE_P (parser->scope)
26197 && dependent_type_p (parser->scope))
26198 {
26199 if (tag_type)
26200 {
26201 tree type;
26202
26203 /* The resolution to Core Issue 180 says that `struct
26204 A::B' should be considered a type-name, even if `A'
26205 is dependent. */
26206 type = make_typename_type (parser->scope, name, tag_type,
26207 /*complain=*/tf_error);
26208 if (type != error_mark_node)
26209 decl = TYPE_NAME (type);
26210 }
26211 else if (is_template
26212 && (cp_parser_next_token_ends_template_argument_p (parser)
26213 || cp_lexer_next_token_is (parser->lexer,
26214 CPP_CLOSE_PAREN)))
26215 decl = make_unbound_class_template (parser->scope,
26216 name, NULL_TREE,
26217 /*complain=*/tf_error);
26218 else
26219 decl = build_qualified_name (/*type=*/NULL_TREE,
26220 parser->scope, name,
26221 is_template);
26222 }
26223 parser->qualifying_scope = parser->scope;
26224 parser->object_scope = NULL_TREE;
26225 }
26226 else if (object_type)
26227 {
26228 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26229 OBJECT_TYPE is not a class. */
26230 if (CLASS_TYPE_P (object_type))
26231 /* If the OBJECT_TYPE is a template specialization, it may
26232 be instantiated during name lookup. In that case, errors
26233 may be issued. Even if we rollback the current tentative
26234 parse, those errors are valid. */
26235 decl = lookup_member (object_type,
26236 name,
26237 /*protect=*/0,
26238 prefer_type_arg (tag_type),
26239 tf_warning_or_error);
26240 else
26241 decl = NULL_TREE;
26242
26243 if (!decl)
26244 /* Look it up in the enclosing context. DR 141: When looking for a
26245 template-name after -> or ., only consider class templates. */
26246 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26247 /*nonclass=*/0,
26248 /*block_p=*/true, is_namespace, 0);
26249 if (object_type == unknown_type_node)
26250 /* The object is type-dependent, so we can't look anything up; we used
26251 this to get the DR 141 behavior. */
26252 object_type = NULL_TREE;
26253 parser->object_scope = object_type;
26254 parser->qualifying_scope = NULL_TREE;
26255 }
26256 else
26257 {
26258 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26259 /*nonclass=*/0,
26260 /*block_p=*/true, is_namespace, 0);
26261 parser->qualifying_scope = NULL_TREE;
26262 parser->object_scope = NULL_TREE;
26263 }
26264
26265 /* If the lookup failed, let our caller know. */
26266 if (!decl || decl == error_mark_node)
26267 return error_mark_node;
26268
26269 /* Pull out the template from an injected-class-name (or multiple). */
26270 if (is_template)
26271 decl = maybe_get_template_decl_from_type_decl (decl);
26272
26273 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26274 if (TREE_CODE (decl) == TREE_LIST)
26275 {
26276 if (ambiguous_decls)
26277 *ambiguous_decls = decl;
26278 /* The error message we have to print is too complicated for
26279 cp_parser_error, so we incorporate its actions directly. */
26280 if (!cp_parser_simulate_error (parser))
26281 {
26282 error_at (name_location, "reference to %qD is ambiguous",
26283 name);
26284 print_candidates (decl);
26285 }
26286 return error_mark_node;
26287 }
26288
26289 gcc_assert (DECL_P (decl)
26290 || TREE_CODE (decl) == OVERLOAD
26291 || TREE_CODE (decl) == SCOPE_REF
26292 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26293 || BASELINK_P (decl));
26294
26295 /* If we have resolved the name of a member declaration, check to
26296 see if the declaration is accessible. When the name resolves to
26297 set of overloaded functions, accessibility is checked when
26298 overload resolution is done.
26299
26300 During an explicit instantiation, access is not checked at all,
26301 as per [temp.explicit]. */
26302 if (DECL_P (decl))
26303 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26304
26305 maybe_record_typedef_use (decl);
26306
26307 return cp_expr (decl, name_location);
26308 }
26309
26310 /* Like cp_parser_lookup_name, but for use in the typical case where
26311 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26312 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26313
26314 static tree
26315 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26316 {
26317 return cp_parser_lookup_name (parser, name,
26318 none_type,
26319 /*is_template=*/false,
26320 /*is_namespace=*/false,
26321 /*check_dependency=*/true,
26322 /*ambiguous_decls=*/NULL,
26323 location);
26324 }
26325
26326 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26327 the current context, return the TYPE_DECL. If TAG_NAME_P is
26328 true, the DECL indicates the class being defined in a class-head,
26329 or declared in an elaborated-type-specifier.
26330
26331 Otherwise, return DECL. */
26332
26333 static tree
26334 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26335 {
26336 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26337 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26338
26339 struct A {
26340 template <typename T> struct B;
26341 };
26342
26343 template <typename T> struct A::B {};
26344
26345 Similarly, in an elaborated-type-specifier:
26346
26347 namespace N { struct X{}; }
26348
26349 struct A {
26350 template <typename T> friend struct N::X;
26351 };
26352
26353 However, if the DECL refers to a class type, and we are in
26354 the scope of the class, then the name lookup automatically
26355 finds the TYPE_DECL created by build_self_reference rather
26356 than a TEMPLATE_DECL. For example, in:
26357
26358 template <class T> struct S {
26359 S s;
26360 };
26361
26362 there is no need to handle such case. */
26363
26364 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26365 return DECL_TEMPLATE_RESULT (decl);
26366
26367 return decl;
26368 }
26369
26370 /* If too many, or too few, template-parameter lists apply to the
26371 declarator, issue an error message. Returns TRUE if all went well,
26372 and FALSE otherwise. */
26373
26374 static bool
26375 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26376 cp_declarator *declarator,
26377 location_t declarator_location)
26378 {
26379 switch (declarator->kind)
26380 {
26381 case cdk_id:
26382 {
26383 unsigned num_templates = 0;
26384 tree scope = declarator->u.id.qualifying_scope;
26385
26386 if (scope)
26387 num_templates = num_template_headers_for_class (scope);
26388 else if (TREE_CODE (declarator->u.id.unqualified_name)
26389 == TEMPLATE_ID_EXPR)
26390 /* If the DECLARATOR has the form `X<y>' then it uses one
26391 additional level of template parameters. */
26392 ++num_templates;
26393
26394 return cp_parser_check_template_parameters
26395 (parser, num_templates, declarator_location, declarator);
26396 }
26397
26398 case cdk_function:
26399 case cdk_array:
26400 case cdk_pointer:
26401 case cdk_reference:
26402 case cdk_ptrmem:
26403 return (cp_parser_check_declarator_template_parameters
26404 (parser, declarator->declarator, declarator_location));
26405
26406 case cdk_decomp:
26407 case cdk_error:
26408 return true;
26409
26410 default:
26411 gcc_unreachable ();
26412 }
26413 return false;
26414 }
26415
26416 /* NUM_TEMPLATES were used in the current declaration. If that is
26417 invalid, return FALSE and issue an error messages. Otherwise,
26418 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26419 declarator and we can print more accurate diagnostics. */
26420
26421 static bool
26422 cp_parser_check_template_parameters (cp_parser* parser,
26423 unsigned num_templates,
26424 location_t location,
26425 cp_declarator *declarator)
26426 {
26427 /* If there are the same number of template classes and parameter
26428 lists, that's OK. */
26429 if (parser->num_template_parameter_lists == num_templates)
26430 return true;
26431 /* If there are more, but only one more, then we are referring to a
26432 member template. That's OK too. */
26433 if (parser->num_template_parameter_lists == num_templates + 1)
26434 return true;
26435 /* If there are more template classes than parameter lists, we have
26436 something like:
26437
26438 template <class T> void S<T>::R<T>::f (); */
26439 if (parser->num_template_parameter_lists < num_templates)
26440 {
26441 if (declarator && !current_function_decl)
26442 error_at (location, "specializing member %<%T::%E%> "
26443 "requires %<template<>%> syntax",
26444 declarator->u.id.qualifying_scope,
26445 declarator->u.id.unqualified_name);
26446 else if (declarator)
26447 error_at (location, "invalid declaration of %<%T::%E%>",
26448 declarator->u.id.qualifying_scope,
26449 declarator->u.id.unqualified_name);
26450 else
26451 error_at (location, "too few template-parameter-lists");
26452 return false;
26453 }
26454 /* Otherwise, there are too many template parameter lists. We have
26455 something like:
26456
26457 template <class T> template <class U> void S::f(); */
26458 error_at (location, "too many template-parameter-lists");
26459 return false;
26460 }
26461
26462 /* Parse an optional `::' token indicating that the following name is
26463 from the global namespace. If so, PARSER->SCOPE is set to the
26464 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26465 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26466 Returns the new value of PARSER->SCOPE, if the `::' token is
26467 present, and NULL_TREE otherwise. */
26468
26469 static tree
26470 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26471 {
26472 cp_token *token;
26473
26474 /* Peek at the next token. */
26475 token = cp_lexer_peek_token (parser->lexer);
26476 /* If we're looking at a `::' token then we're starting from the
26477 global namespace, not our current location. */
26478 if (token->type == CPP_SCOPE)
26479 {
26480 /* Consume the `::' token. */
26481 cp_lexer_consume_token (parser->lexer);
26482 /* Set the SCOPE so that we know where to start the lookup. */
26483 parser->scope = global_namespace;
26484 parser->qualifying_scope = global_namespace;
26485 parser->object_scope = NULL_TREE;
26486
26487 return parser->scope;
26488 }
26489 else if (!current_scope_valid_p)
26490 {
26491 parser->scope = NULL_TREE;
26492 parser->qualifying_scope = NULL_TREE;
26493 parser->object_scope = NULL_TREE;
26494 }
26495
26496 return NULL_TREE;
26497 }
26498
26499 /* Returns TRUE if the upcoming token sequence is the start of a
26500 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26501 declarator is preceded by the `friend' specifier. */
26502
26503 static bool
26504 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26505 {
26506 bool constructor_p;
26507 bool outside_class_specifier_p;
26508 tree nested_name_specifier;
26509 cp_token *next_token;
26510
26511 /* The common case is that this is not a constructor declarator, so
26512 try to avoid doing lots of work if at all possible. It's not
26513 valid declare a constructor at function scope. */
26514 if (parser->in_function_body)
26515 return false;
26516 /* And only certain tokens can begin a constructor declarator. */
26517 next_token = cp_lexer_peek_token (parser->lexer);
26518 if (next_token->type != CPP_NAME
26519 && next_token->type != CPP_SCOPE
26520 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26521 && next_token->type != CPP_TEMPLATE_ID)
26522 return false;
26523
26524 /* Parse tentatively; we are going to roll back all of the tokens
26525 consumed here. */
26526 cp_parser_parse_tentatively (parser);
26527 /* Assume that we are looking at a constructor declarator. */
26528 constructor_p = true;
26529
26530 /* Look for the optional `::' operator. */
26531 cp_parser_global_scope_opt (parser,
26532 /*current_scope_valid_p=*/false);
26533 /* Look for the nested-name-specifier. */
26534 nested_name_specifier
26535 = (cp_parser_nested_name_specifier_opt (parser,
26536 /*typename_keyword_p=*/false,
26537 /*check_dependency_p=*/false,
26538 /*type_p=*/false,
26539 /*is_declaration=*/false));
26540
26541 outside_class_specifier_p = (!at_class_scope_p ()
26542 || !TYPE_BEING_DEFINED (current_class_type)
26543 || friend_p);
26544
26545 /* Outside of a class-specifier, there must be a
26546 nested-name-specifier. Except in C++17 mode, where we
26547 might be declaring a guiding declaration. */
26548 if (!nested_name_specifier && outside_class_specifier_p
26549 && cxx_dialect < cxx17)
26550 constructor_p = false;
26551 else if (nested_name_specifier == error_mark_node)
26552 constructor_p = false;
26553
26554 /* If we have a class scope, this is easy; DR 147 says that S::S always
26555 names the constructor, and no other qualified name could. */
26556 if (constructor_p && nested_name_specifier
26557 && CLASS_TYPE_P (nested_name_specifier))
26558 {
26559 tree id = cp_parser_unqualified_id (parser,
26560 /*template_keyword_p=*/false,
26561 /*check_dependency_p=*/false,
26562 /*declarator_p=*/true,
26563 /*optional_p=*/false);
26564 if (is_overloaded_fn (id))
26565 id = DECL_NAME (get_first_fn (id));
26566 if (!constructor_name_p (id, nested_name_specifier))
26567 constructor_p = false;
26568 }
26569 /* If we still think that this might be a constructor-declarator,
26570 look for a class-name. */
26571 else if (constructor_p)
26572 {
26573 /* If we have:
26574
26575 template <typename T> struct S {
26576 S();
26577 };
26578
26579 we must recognize that the nested `S' names a class. */
26580 if (cxx_dialect >= cxx17)
26581 cp_parser_parse_tentatively (parser);
26582
26583 tree type_decl;
26584 type_decl = cp_parser_class_name (parser,
26585 /*typename_keyword_p=*/false,
26586 /*template_keyword_p=*/false,
26587 none_type,
26588 /*check_dependency_p=*/false,
26589 /*class_head_p=*/false,
26590 /*is_declaration=*/false);
26591
26592 if (cxx_dialect >= cxx17
26593 && !cp_parser_parse_definitely (parser))
26594 {
26595 type_decl = NULL_TREE;
26596 tree tmpl = cp_parser_template_name (parser,
26597 /*template_keyword*/false,
26598 /*check_dependency_p*/false,
26599 /*is_declaration*/false,
26600 none_type,
26601 /*is_identifier*/NULL);
26602 if (DECL_CLASS_TEMPLATE_P (tmpl)
26603 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26604 /* It's a deduction guide, return true. */;
26605 else
26606 cp_parser_simulate_error (parser);
26607 }
26608
26609 /* If there was no class-name, then this is not a constructor.
26610 Otherwise, if we are in a class-specifier and we aren't
26611 handling a friend declaration, check that its type matches
26612 current_class_type (c++/38313). Note: error_mark_node
26613 is left alone for error recovery purposes. */
26614 constructor_p = (!cp_parser_error_occurred (parser)
26615 && (outside_class_specifier_p
26616 || type_decl == NULL_TREE
26617 || type_decl == error_mark_node
26618 || same_type_p (current_class_type,
26619 TREE_TYPE (type_decl))));
26620
26621 /* If we're still considering a constructor, we have to see a `(',
26622 to begin the parameter-declaration-clause, followed by either a
26623 `)', an `...', or a decl-specifier. We need to check for a
26624 type-specifier to avoid being fooled into thinking that:
26625
26626 S (f) (int);
26627
26628 is a constructor. (It is actually a function named `f' that
26629 takes one parameter (of type `int') and returns a value of type
26630 `S'. */
26631 if (constructor_p
26632 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26633 constructor_p = false;
26634
26635 if (constructor_p
26636 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26637 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26638 /* A parameter declaration begins with a decl-specifier,
26639 which is either the "attribute" keyword, a storage class
26640 specifier, or (usually) a type-specifier. */
26641 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26642 {
26643 tree type;
26644 tree pushed_scope = NULL_TREE;
26645 unsigned saved_num_template_parameter_lists;
26646
26647 /* Names appearing in the type-specifier should be looked up
26648 in the scope of the class. */
26649 if (current_class_type)
26650 type = NULL_TREE;
26651 else if (type_decl)
26652 {
26653 type = TREE_TYPE (type_decl);
26654 if (TREE_CODE (type) == TYPENAME_TYPE)
26655 {
26656 type = resolve_typename_type (type,
26657 /*only_current_p=*/false);
26658 if (TREE_CODE (type) == TYPENAME_TYPE)
26659 {
26660 cp_parser_abort_tentative_parse (parser);
26661 return false;
26662 }
26663 }
26664 pushed_scope = push_scope (type);
26665 }
26666
26667 /* Inside the constructor parameter list, surrounding
26668 template-parameter-lists do not apply. */
26669 saved_num_template_parameter_lists
26670 = parser->num_template_parameter_lists;
26671 parser->num_template_parameter_lists = 0;
26672
26673 /* Look for the type-specifier. */
26674 cp_parser_type_specifier (parser,
26675 CP_PARSER_FLAGS_NONE,
26676 /*decl_specs=*/NULL,
26677 /*is_declarator=*/true,
26678 /*declares_class_or_enum=*/NULL,
26679 /*is_cv_qualifier=*/NULL);
26680
26681 parser->num_template_parameter_lists
26682 = saved_num_template_parameter_lists;
26683
26684 /* Leave the scope of the class. */
26685 if (pushed_scope)
26686 pop_scope (pushed_scope);
26687
26688 constructor_p = !cp_parser_error_occurred (parser);
26689 }
26690 }
26691
26692 /* We did not really want to consume any tokens. */
26693 cp_parser_abort_tentative_parse (parser);
26694
26695 return constructor_p;
26696 }
26697
26698 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26699 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26700 they must be performed once we are in the scope of the function.
26701
26702 Returns the function defined. */
26703
26704 static tree
26705 cp_parser_function_definition_from_specifiers_and_declarator
26706 (cp_parser* parser,
26707 cp_decl_specifier_seq *decl_specifiers,
26708 tree attributes,
26709 const cp_declarator *declarator)
26710 {
26711 tree fn;
26712 bool success_p;
26713
26714 /* Begin the function-definition. */
26715 success_p = start_function (decl_specifiers, declarator, attributes);
26716
26717 /* The things we're about to see are not directly qualified by any
26718 template headers we've seen thus far. */
26719 reset_specialization ();
26720
26721 /* If there were names looked up in the decl-specifier-seq that we
26722 did not check, check them now. We must wait until we are in the
26723 scope of the function to perform the checks, since the function
26724 might be a friend. */
26725 perform_deferred_access_checks (tf_warning_or_error);
26726
26727 if (success_p)
26728 {
26729 cp_finalize_omp_declare_simd (parser, current_function_decl);
26730 parser->omp_declare_simd = NULL;
26731 cp_finalize_oacc_routine (parser, current_function_decl, true);
26732 parser->oacc_routine = NULL;
26733 }
26734
26735 if (!success_p)
26736 {
26737 /* Skip the entire function. */
26738 cp_parser_skip_to_end_of_block_or_statement (parser);
26739 fn = error_mark_node;
26740 }
26741 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26742 {
26743 /* Seen already, skip it. An error message has already been output. */
26744 cp_parser_skip_to_end_of_block_or_statement (parser);
26745 fn = current_function_decl;
26746 current_function_decl = NULL_TREE;
26747 /* If this is a function from a class, pop the nested class. */
26748 if (current_class_name)
26749 pop_nested_class ();
26750 }
26751 else
26752 {
26753 timevar_id_t tv;
26754 if (DECL_DECLARED_INLINE_P (current_function_decl))
26755 tv = TV_PARSE_INLINE;
26756 else
26757 tv = TV_PARSE_FUNC;
26758 timevar_push (tv);
26759 fn = cp_parser_function_definition_after_declarator (parser,
26760 /*inline_p=*/false);
26761 timevar_pop (tv);
26762 }
26763
26764 return fn;
26765 }
26766
26767 /* Parse the part of a function-definition that follows the
26768 declarator. INLINE_P is TRUE iff this function is an inline
26769 function defined within a class-specifier.
26770
26771 Returns the function defined. */
26772
26773 static tree
26774 cp_parser_function_definition_after_declarator (cp_parser* parser,
26775 bool inline_p)
26776 {
26777 tree fn;
26778 bool saved_in_unbraced_linkage_specification_p;
26779 bool saved_in_function_body;
26780 unsigned saved_num_template_parameter_lists;
26781 cp_token *token;
26782 bool fully_implicit_function_template_p
26783 = parser->fully_implicit_function_template_p;
26784 parser->fully_implicit_function_template_p = false;
26785 tree implicit_template_parms
26786 = parser->implicit_template_parms;
26787 parser->implicit_template_parms = 0;
26788 cp_binding_level* implicit_template_scope
26789 = parser->implicit_template_scope;
26790 parser->implicit_template_scope = 0;
26791
26792 saved_in_function_body = parser->in_function_body;
26793 parser->in_function_body = true;
26794 /* If the next token is `return', then the code may be trying to
26795 make use of the "named return value" extension that G++ used to
26796 support. */
26797 token = cp_lexer_peek_token (parser->lexer);
26798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26799 {
26800 /* Consume the `return' keyword. */
26801 cp_lexer_consume_token (parser->lexer);
26802 /* Look for the identifier that indicates what value is to be
26803 returned. */
26804 cp_parser_identifier (parser);
26805 /* Issue an error message. */
26806 error_at (token->location,
26807 "named return values are no longer supported");
26808 /* Skip tokens until we reach the start of the function body. */
26809 while (true)
26810 {
26811 cp_token *token = cp_lexer_peek_token (parser->lexer);
26812 if (token->type == CPP_OPEN_BRACE
26813 || token->type == CPP_EOF
26814 || token->type == CPP_PRAGMA_EOL)
26815 break;
26816 cp_lexer_consume_token (parser->lexer);
26817 }
26818 }
26819 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26820 anything declared inside `f'. */
26821 saved_in_unbraced_linkage_specification_p
26822 = parser->in_unbraced_linkage_specification_p;
26823 parser->in_unbraced_linkage_specification_p = false;
26824 /* Inside the function, surrounding template-parameter-lists do not
26825 apply. */
26826 saved_num_template_parameter_lists
26827 = parser->num_template_parameter_lists;
26828 parser->num_template_parameter_lists = 0;
26829
26830 /* If the next token is `try', `__transaction_atomic', or
26831 `__transaction_relaxed`, then we are looking at either function-try-block
26832 or function-transaction-block. Note that all of these include the
26833 function-body. */
26834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26835 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26836 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26837 RID_TRANSACTION_RELAXED))
26838 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26839 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26840 cp_parser_function_try_block (parser);
26841 else
26842 cp_parser_ctor_initializer_opt_and_function_body
26843 (parser, /*in_function_try_block=*/false);
26844
26845 /* Finish the function. */
26846 fn = finish_function (inline_p);
26847 /* Generate code for it, if necessary. */
26848 expand_or_defer_fn (fn);
26849 /* Restore the saved values. */
26850 parser->in_unbraced_linkage_specification_p
26851 = saved_in_unbraced_linkage_specification_p;
26852 parser->num_template_parameter_lists
26853 = saved_num_template_parameter_lists;
26854 parser->in_function_body = saved_in_function_body;
26855
26856 parser->fully_implicit_function_template_p
26857 = fully_implicit_function_template_p;
26858 parser->implicit_template_parms
26859 = implicit_template_parms;
26860 parser->implicit_template_scope
26861 = implicit_template_scope;
26862
26863 if (parser->fully_implicit_function_template_p)
26864 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26865
26866 return fn;
26867 }
26868
26869 /* Parse a template-declaration body (following argument list). */
26870
26871 static void
26872 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26873 tree parameter_list,
26874 bool member_p)
26875 {
26876 tree decl = NULL_TREE;
26877 bool friend_p = false;
26878
26879 /* We just processed one more parameter list. */
26880 ++parser->num_template_parameter_lists;
26881
26882 /* Get the deferred access checks from the parameter list. These
26883 will be checked once we know what is being declared, as for a
26884 member template the checks must be performed in the scope of the
26885 class containing the member. */
26886 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26887
26888 /* Tentatively parse for a new template parameter list, which can either be
26889 the template keyword or a template introduction. */
26890 if (cp_parser_template_declaration_after_export (parser, member_p))
26891 /* OK */;
26892 else if (cxx_dialect >= cxx11
26893 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26894 decl = cp_parser_alias_declaration (parser);
26895 else
26896 {
26897 /* There are no access checks when parsing a template, as we do not
26898 know if a specialization will be a friend. */
26899 push_deferring_access_checks (dk_no_check);
26900 cp_token *token = cp_lexer_peek_token (parser->lexer);
26901 decl = cp_parser_single_declaration (parser,
26902 checks,
26903 member_p,
26904 /*explicit_specialization_p=*/false,
26905 &friend_p);
26906 pop_deferring_access_checks ();
26907
26908 /* If this is a member template declaration, let the front
26909 end know. */
26910 if (member_p && !friend_p && decl)
26911 {
26912 if (TREE_CODE (decl) == TYPE_DECL)
26913 cp_parser_check_access_in_redeclaration (decl, token->location);
26914
26915 decl = finish_member_template_decl (decl);
26916 }
26917 else if (friend_p && decl
26918 && DECL_DECLARES_TYPE_P (decl))
26919 make_friend_class (current_class_type, TREE_TYPE (decl),
26920 /*complain=*/true);
26921 }
26922 /* We are done with the current parameter list. */
26923 --parser->num_template_parameter_lists;
26924
26925 pop_deferring_access_checks ();
26926
26927 /* Finish up. */
26928 finish_template_decl (parameter_list);
26929
26930 /* Check the template arguments for a literal operator template. */
26931 if (decl
26932 && DECL_DECLARES_FUNCTION_P (decl)
26933 && UDLIT_OPER_P (DECL_NAME (decl)))
26934 {
26935 bool ok = true;
26936 if (parameter_list == NULL_TREE)
26937 ok = false;
26938 else
26939 {
26940 int num_parms = TREE_VEC_LENGTH (parameter_list);
26941 if (num_parms == 1)
26942 {
26943 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26944 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26945 if (TREE_TYPE (parm) != char_type_node
26946 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26947 ok = false;
26948 }
26949 else if (num_parms == 2 && cxx_dialect >= cxx14)
26950 {
26951 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26952 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26953 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26954 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26955 if (parm == error_mark_node
26956 || TREE_TYPE (parm) != TREE_TYPE (type)
26957 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26958 ok = false;
26959 }
26960 else
26961 ok = false;
26962 }
26963 if (!ok)
26964 {
26965 if (cxx_dialect >= cxx14)
26966 error ("literal operator template %qD has invalid parameter list."
26967 " Expected non-type template argument pack <char...>"
26968 " or <typename CharT, CharT...>",
26969 decl);
26970 else
26971 error ("literal operator template %qD has invalid parameter list."
26972 " Expected non-type template argument pack <char...>",
26973 decl);
26974 }
26975 }
26976
26977 /* Register member declarations. */
26978 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26979 finish_member_declaration (decl);
26980 /* If DECL is a function template, we must return to parse it later.
26981 (Even though there is no definition, there might be default
26982 arguments that need handling.) */
26983 if (member_p && decl
26984 && DECL_DECLARES_FUNCTION_P (decl))
26985 vec_safe_push (unparsed_funs_with_definitions, decl);
26986 }
26987
26988 /* Parse a template introduction header for a template-declaration. Returns
26989 false if tentative parse fails. */
26990
26991 static bool
26992 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26993 {
26994 cp_parser_parse_tentatively (parser);
26995
26996 tree saved_scope = parser->scope;
26997 tree saved_object_scope = parser->object_scope;
26998 tree saved_qualifying_scope = parser->qualifying_scope;
26999
27000 /* Look for the optional `::' operator. */
27001 cp_parser_global_scope_opt (parser,
27002 /*current_scope_valid_p=*/false);
27003 /* Look for the nested-name-specifier. */
27004 cp_parser_nested_name_specifier_opt (parser,
27005 /*typename_keyword_p=*/false,
27006 /*check_dependency_p=*/true,
27007 /*type_p=*/false,
27008 /*is_declaration=*/false);
27009
27010 cp_token *token = cp_lexer_peek_token (parser->lexer);
27011 tree concept_name = cp_parser_identifier (parser);
27012
27013 /* Look up the concept for which we will be matching
27014 template parameters. */
27015 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27016 token->location);
27017 parser->scope = saved_scope;
27018 parser->object_scope = saved_object_scope;
27019 parser->qualifying_scope = saved_qualifying_scope;
27020
27021 if (concept_name == error_mark_node)
27022 cp_parser_simulate_error (parser);
27023
27024 /* Look for opening brace for introduction. */
27025 matching_braces braces;
27026 braces.require_open (parser);
27027
27028 if (!cp_parser_parse_definitely (parser))
27029 return false;
27030
27031 push_deferring_access_checks (dk_deferred);
27032
27033 /* Build vector of placeholder parameters and grab
27034 matching identifiers. */
27035 tree introduction_list = cp_parser_introduction_list (parser);
27036
27037 /* The introduction-list shall not be empty. */
27038 int nargs = TREE_VEC_LENGTH (introduction_list);
27039 if (nargs == 0)
27040 {
27041 error ("empty introduction-list");
27042 return true;
27043 }
27044
27045 /* Look for closing brace for introduction. */
27046 if (!braces.require_close (parser))
27047 return true;
27048
27049 if (tmpl_decl == error_mark_node)
27050 {
27051 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27052 token->location);
27053 return true;
27054 }
27055
27056 /* Build and associate the constraint. */
27057 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27058 if (parms && parms != error_mark_node)
27059 {
27060 cp_parser_template_declaration_after_parameters (parser, parms,
27061 member_p);
27062 return true;
27063 }
27064
27065 error_at (token->location, "no matching concept for template-introduction");
27066 return true;
27067 }
27068
27069 /* Parse a normal template-declaration following the template keyword. */
27070
27071 static void
27072 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27073 {
27074 tree parameter_list;
27075 bool need_lang_pop;
27076 location_t location = input_location;
27077
27078 /* Look for the `<' token. */
27079 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27080 return;
27081 if (at_class_scope_p () && current_function_decl)
27082 {
27083 /* 14.5.2.2 [temp.mem]
27084
27085 A local class shall not have member templates. */
27086 error_at (location,
27087 "invalid declaration of member template in local class");
27088 cp_parser_skip_to_end_of_block_or_statement (parser);
27089 return;
27090 }
27091 /* [temp]
27092
27093 A template ... shall not have C linkage. */
27094 if (current_lang_name == lang_name_c)
27095 {
27096 error_at (location, "template with C linkage");
27097 maybe_show_extern_c_location ();
27098 /* Give it C++ linkage to avoid confusing other parts of the
27099 front end. */
27100 push_lang_context (lang_name_cplusplus);
27101 need_lang_pop = true;
27102 }
27103 else
27104 need_lang_pop = false;
27105
27106 /* We cannot perform access checks on the template parameter
27107 declarations until we know what is being declared, just as we
27108 cannot check the decl-specifier list. */
27109 push_deferring_access_checks (dk_deferred);
27110
27111 /* If the next token is `>', then we have an invalid
27112 specialization. Rather than complain about an invalid template
27113 parameter, issue an error message here. */
27114 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27115 {
27116 cp_parser_error (parser, "invalid explicit specialization");
27117 begin_specialization ();
27118 parameter_list = NULL_TREE;
27119 }
27120 else
27121 {
27122 /* Parse the template parameters. */
27123 parameter_list = cp_parser_template_parameter_list (parser);
27124 }
27125
27126 /* Look for the `>'. */
27127 cp_parser_skip_to_end_of_template_parameter_list (parser);
27128
27129 /* Manage template requirements */
27130 if (flag_concepts)
27131 {
27132 tree reqs = get_shorthand_constraints (current_template_parms);
27133 if (tree r = cp_parser_requires_clause_opt (parser))
27134 reqs = conjoin_constraints (reqs, normalize_expression (r));
27135 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27136 }
27137
27138 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27139 member_p);
27140
27141 /* For the erroneous case of a template with C linkage, we pushed an
27142 implicit C++ linkage scope; exit that scope now. */
27143 if (need_lang_pop)
27144 pop_lang_context ();
27145 }
27146
27147 /* Parse a template-declaration, assuming that the `export' (and
27148 `extern') keywords, if present, has already been scanned. MEMBER_P
27149 is as for cp_parser_template_declaration. */
27150
27151 static bool
27152 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27153 {
27154 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27155 {
27156 cp_lexer_consume_token (parser->lexer);
27157 cp_parser_explicit_template_declaration (parser, member_p);
27158 return true;
27159 }
27160 else if (flag_concepts)
27161 return cp_parser_template_introduction (parser, member_p);
27162
27163 return false;
27164 }
27165
27166 /* Perform the deferred access checks from a template-parameter-list.
27167 CHECKS is a TREE_LIST of access checks, as returned by
27168 get_deferred_access_checks. */
27169
27170 static void
27171 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27172 {
27173 ++processing_template_parmlist;
27174 perform_access_checks (checks, tf_warning_or_error);
27175 --processing_template_parmlist;
27176 }
27177
27178 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27179 `function-definition' sequence that follows a template header.
27180 If MEMBER_P is true, this declaration appears in a class scope.
27181
27182 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27183 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27184
27185 static tree
27186 cp_parser_single_declaration (cp_parser* parser,
27187 vec<deferred_access_check, va_gc> *checks,
27188 bool member_p,
27189 bool explicit_specialization_p,
27190 bool* friend_p)
27191 {
27192 int declares_class_or_enum;
27193 tree decl = NULL_TREE;
27194 cp_decl_specifier_seq decl_specifiers;
27195 bool function_definition_p = false;
27196 cp_token *decl_spec_token_start;
27197
27198 /* This function is only used when processing a template
27199 declaration. */
27200 gcc_assert (innermost_scope_kind () == sk_template_parms
27201 || innermost_scope_kind () == sk_template_spec);
27202
27203 /* Defer access checks until we know what is being declared. */
27204 push_deferring_access_checks (dk_deferred);
27205
27206 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27207 alternative. */
27208 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27209 cp_parser_decl_specifier_seq (parser,
27210 CP_PARSER_FLAGS_OPTIONAL,
27211 &decl_specifiers,
27212 &declares_class_or_enum);
27213 if (friend_p)
27214 *friend_p = cp_parser_friend_p (&decl_specifiers);
27215
27216 /* There are no template typedefs. */
27217 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27218 {
27219 error_at (decl_spec_token_start->location,
27220 "template declaration of %<typedef%>");
27221 decl = error_mark_node;
27222 }
27223
27224 /* Gather up the access checks that occurred the
27225 decl-specifier-seq. */
27226 stop_deferring_access_checks ();
27227
27228 /* Check for the declaration of a template class. */
27229 if (declares_class_or_enum)
27230 {
27231 if (cp_parser_declares_only_class_p (parser)
27232 || (declares_class_or_enum & 2))
27233 {
27234 // If this is a declaration, but not a definition, associate
27235 // any constraints with the type declaration. Constraints
27236 // are associated with definitions in cp_parser_class_specifier.
27237 if (declares_class_or_enum == 1)
27238 associate_classtype_constraints (decl_specifiers.type);
27239
27240 decl = shadow_tag (&decl_specifiers);
27241
27242 /* In this case:
27243
27244 struct C {
27245 friend template <typename T> struct A<T>::B;
27246 };
27247
27248 A<T>::B will be represented by a TYPENAME_TYPE, and
27249 therefore not recognized by shadow_tag. */
27250 if (friend_p && *friend_p
27251 && !decl
27252 && decl_specifiers.type
27253 && TYPE_P (decl_specifiers.type))
27254 decl = decl_specifiers.type;
27255
27256 if (decl && decl != error_mark_node)
27257 decl = TYPE_NAME (decl);
27258 else
27259 decl = error_mark_node;
27260
27261 /* Perform access checks for template parameters. */
27262 cp_parser_perform_template_parameter_access_checks (checks);
27263
27264 /* Give a helpful diagnostic for
27265 template <class T> struct A { } a;
27266 if we aren't already recovering from an error. */
27267 if (!cp_parser_declares_only_class_p (parser)
27268 && !seen_error ())
27269 {
27270 error_at (cp_lexer_peek_token (parser->lexer)->location,
27271 "a class template declaration must not declare "
27272 "anything else");
27273 cp_parser_skip_to_end_of_block_or_statement (parser);
27274 goto out;
27275 }
27276 }
27277 }
27278
27279 /* Complain about missing 'typename' or other invalid type names. */
27280 if (!decl_specifiers.any_type_specifiers_p
27281 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27282 {
27283 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27284 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27285 the rest of this declaration. */
27286 decl = error_mark_node;
27287 goto out;
27288 }
27289
27290 /* If it's not a template class, try for a template function. If
27291 the next token is a `;', then this declaration does not declare
27292 anything. But, if there were errors in the decl-specifiers, then
27293 the error might well have come from an attempted class-specifier.
27294 In that case, there's no need to warn about a missing declarator. */
27295 if (!decl
27296 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27297 || decl_specifiers.type != error_mark_node))
27298 {
27299 decl = cp_parser_init_declarator (parser,
27300 &decl_specifiers,
27301 checks,
27302 /*function_definition_allowed_p=*/true,
27303 member_p,
27304 declares_class_or_enum,
27305 &function_definition_p,
27306 NULL, NULL, NULL);
27307
27308 /* 7.1.1-1 [dcl.stc]
27309
27310 A storage-class-specifier shall not be specified in an explicit
27311 specialization... */
27312 if (decl
27313 && explicit_specialization_p
27314 && decl_specifiers.storage_class != sc_none)
27315 {
27316 error_at (decl_spec_token_start->location,
27317 "explicit template specialization cannot have a storage class");
27318 decl = error_mark_node;
27319 }
27320
27321 if (decl && VAR_P (decl))
27322 check_template_variable (decl);
27323 }
27324
27325 /* Look for a trailing `;' after the declaration. */
27326 if (!function_definition_p
27327 && (decl == error_mark_node
27328 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27329 cp_parser_skip_to_end_of_block_or_statement (parser);
27330
27331 out:
27332 pop_deferring_access_checks ();
27333
27334 /* Clear any current qualification; whatever comes next is the start
27335 of something new. */
27336 parser->scope = NULL_TREE;
27337 parser->qualifying_scope = NULL_TREE;
27338 parser->object_scope = NULL_TREE;
27339
27340 return decl;
27341 }
27342
27343 /* Parse a cast-expression that is not the operand of a unary "&". */
27344
27345 static cp_expr
27346 cp_parser_simple_cast_expression (cp_parser *parser)
27347 {
27348 return cp_parser_cast_expression (parser, /*address_p=*/false,
27349 /*cast_p=*/false, /*decltype*/false, NULL);
27350 }
27351
27352 /* Parse a functional cast to TYPE. Returns an expression
27353 representing the cast. */
27354
27355 static cp_expr
27356 cp_parser_functional_cast (cp_parser* parser, tree type)
27357 {
27358 vec<tree, va_gc> *vec;
27359 tree expression_list;
27360 cp_expr cast;
27361 bool nonconst_p;
27362
27363 location_t start_loc = input_location;
27364
27365 if (!type)
27366 type = error_mark_node;
27367
27368 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27369 {
27370 cp_lexer_set_source_position (parser->lexer);
27371 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27372 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27373 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27374 if (TREE_CODE (type) == TYPE_DECL)
27375 type = TREE_TYPE (type);
27376
27377 cast = finish_compound_literal (type, expression_list,
27378 tf_warning_or_error, fcl_functional);
27379 /* Create a location of the form:
27380 type_name{i, f}
27381 ^~~~~~~~~~~~~~~
27382 with caret == start at the start of the type name,
27383 finishing at the closing brace. */
27384 location_t finish_loc
27385 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27386 location_t combined_loc = make_location (start_loc, start_loc,
27387 finish_loc);
27388 cast.set_location (combined_loc);
27389 return cast;
27390 }
27391
27392
27393 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27394 /*cast_p=*/true,
27395 /*allow_expansion_p=*/true,
27396 /*non_constant_p=*/NULL);
27397 if (vec == NULL)
27398 expression_list = error_mark_node;
27399 else
27400 {
27401 expression_list = build_tree_list_vec (vec);
27402 release_tree_vector (vec);
27403 }
27404
27405 cast = build_functional_cast (type, expression_list,
27406 tf_warning_or_error);
27407 /* [expr.const]/1: In an integral constant expression "only type
27408 conversions to integral or enumeration type can be used". */
27409 if (TREE_CODE (type) == TYPE_DECL)
27410 type = TREE_TYPE (type);
27411 if (cast != error_mark_node
27412 && !cast_valid_in_integral_constant_expression_p (type)
27413 && cp_parser_non_integral_constant_expression (parser,
27414 NIC_CONSTRUCTOR))
27415 return error_mark_node;
27416
27417 /* Create a location of the form:
27418 float(i)
27419 ^~~~~~~~
27420 with caret == start at the start of the type name,
27421 finishing at the closing paren. */
27422 location_t finish_loc
27423 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27424 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27425 cast.set_location (combined_loc);
27426 return cast;
27427 }
27428
27429 /* Save the tokens that make up the body of a member function defined
27430 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27431 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27432 specifiers applied to the declaration. Returns the FUNCTION_DECL
27433 for the member function. */
27434
27435 static tree
27436 cp_parser_save_member_function_body (cp_parser* parser,
27437 cp_decl_specifier_seq *decl_specifiers,
27438 cp_declarator *declarator,
27439 tree attributes)
27440 {
27441 cp_token *first;
27442 cp_token *last;
27443 tree fn;
27444 bool function_try_block = false;
27445
27446 /* Create the FUNCTION_DECL. */
27447 fn = grokmethod (decl_specifiers, declarator, attributes);
27448 cp_finalize_omp_declare_simd (parser, fn);
27449 cp_finalize_oacc_routine (parser, fn, true);
27450 /* If something went badly wrong, bail out now. */
27451 if (fn == error_mark_node)
27452 {
27453 /* If there's a function-body, skip it. */
27454 if (cp_parser_token_starts_function_definition_p
27455 (cp_lexer_peek_token (parser->lexer)))
27456 cp_parser_skip_to_end_of_block_or_statement (parser);
27457 return error_mark_node;
27458 }
27459
27460 /* Remember it, if there default args to post process. */
27461 cp_parser_save_default_args (parser, fn);
27462
27463 /* Save away the tokens that make up the body of the
27464 function. */
27465 first = parser->lexer->next_token;
27466
27467 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27468 cp_lexer_consume_token (parser->lexer);
27469 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27470 RID_TRANSACTION_ATOMIC))
27471 {
27472 cp_lexer_consume_token (parser->lexer);
27473 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27475 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27476 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27477 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27478 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27479 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27480 {
27481 cp_lexer_consume_token (parser->lexer);
27482 cp_lexer_consume_token (parser->lexer);
27483 cp_lexer_consume_token (parser->lexer);
27484 cp_lexer_consume_token (parser->lexer);
27485 cp_lexer_consume_token (parser->lexer);
27486 }
27487 else
27488 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27489 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27490 {
27491 cp_lexer_consume_token (parser->lexer);
27492 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27493 break;
27494 }
27495 }
27496
27497 /* Handle function try blocks. */
27498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27499 {
27500 cp_lexer_consume_token (parser->lexer);
27501 function_try_block = true;
27502 }
27503 /* We can have braced-init-list mem-initializers before the fn body. */
27504 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27505 {
27506 cp_lexer_consume_token (parser->lexer);
27507 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27508 {
27509 /* cache_group will stop after an un-nested { } pair, too. */
27510 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27511 break;
27512
27513 /* variadic mem-inits have ... after the ')'. */
27514 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27515 cp_lexer_consume_token (parser->lexer);
27516 }
27517 }
27518 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27519 /* Handle function try blocks. */
27520 if (function_try_block)
27521 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27522 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27523 last = parser->lexer->next_token;
27524
27525 /* Save away the inline definition; we will process it when the
27526 class is complete. */
27527 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27528 DECL_PENDING_INLINE_P (fn) = 1;
27529
27530 /* We need to know that this was defined in the class, so that
27531 friend templates are handled correctly. */
27532 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27533
27534 /* Add FN to the queue of functions to be parsed later. */
27535 vec_safe_push (unparsed_funs_with_definitions, fn);
27536
27537 return fn;
27538 }
27539
27540 /* Save the tokens that make up the in-class initializer for a non-static
27541 data member. Returns a DEFAULT_ARG. */
27542
27543 static tree
27544 cp_parser_save_nsdmi (cp_parser* parser)
27545 {
27546 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27547 }
27548
27549 /* Parse a template-argument-list, as well as the trailing ">" (but
27550 not the opening "<"). See cp_parser_template_argument_list for the
27551 return value. */
27552
27553 static tree
27554 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27555 {
27556 tree arguments;
27557 tree saved_scope;
27558 tree saved_qualifying_scope;
27559 tree saved_object_scope;
27560 bool saved_greater_than_is_operator_p;
27561 int saved_unevaluated_operand;
27562 int saved_inhibit_evaluation_warnings;
27563
27564 /* [temp.names]
27565
27566 When parsing a template-id, the first non-nested `>' is taken as
27567 the end of the template-argument-list rather than a greater-than
27568 operator. */
27569 saved_greater_than_is_operator_p
27570 = parser->greater_than_is_operator_p;
27571 parser->greater_than_is_operator_p = false;
27572 /* Parsing the argument list may modify SCOPE, so we save it
27573 here. */
27574 saved_scope = parser->scope;
27575 saved_qualifying_scope = parser->qualifying_scope;
27576 saved_object_scope = parser->object_scope;
27577 /* We need to evaluate the template arguments, even though this
27578 template-id may be nested within a "sizeof". */
27579 saved_unevaluated_operand = cp_unevaluated_operand;
27580 cp_unevaluated_operand = 0;
27581 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27582 c_inhibit_evaluation_warnings = 0;
27583 /* Parse the template-argument-list itself. */
27584 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27585 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27586 arguments = NULL_TREE;
27587 else
27588 arguments = cp_parser_template_argument_list (parser);
27589 /* Look for the `>' that ends the template-argument-list. If we find
27590 a '>>' instead, it's probably just a typo. */
27591 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27592 {
27593 if (cxx_dialect != cxx98)
27594 {
27595 /* In C++0x, a `>>' in a template argument list or cast
27596 expression is considered to be two separate `>'
27597 tokens. So, change the current token to a `>', but don't
27598 consume it: it will be consumed later when the outer
27599 template argument list (or cast expression) is parsed.
27600 Note that this replacement of `>' for `>>' is necessary
27601 even if we are parsing tentatively: in the tentative
27602 case, after calling
27603 cp_parser_enclosed_template_argument_list we will always
27604 throw away all of the template arguments and the first
27605 closing `>', either because the template argument list
27606 was erroneous or because we are replacing those tokens
27607 with a CPP_TEMPLATE_ID token. The second `>' (which will
27608 not have been thrown away) is needed either to close an
27609 outer template argument list or to complete a new-style
27610 cast. */
27611 cp_token *token = cp_lexer_peek_token (parser->lexer);
27612 token->type = CPP_GREATER;
27613 }
27614 else if (!saved_greater_than_is_operator_p)
27615 {
27616 /* If we're in a nested template argument list, the '>>' has
27617 to be a typo for '> >'. We emit the error message, but we
27618 continue parsing and we push a '>' as next token, so that
27619 the argument list will be parsed correctly. Note that the
27620 global source location is still on the token before the
27621 '>>', so we need to say explicitly where we want it. */
27622 cp_token *token = cp_lexer_peek_token (parser->lexer);
27623 gcc_rich_location richloc (token->location);
27624 richloc.add_fixit_replace ("> >");
27625 error_at (&richloc, "%<>>%> should be %<> >%> "
27626 "within a nested template argument list");
27627
27628 token->type = CPP_GREATER;
27629 }
27630 else
27631 {
27632 /* If this is not a nested template argument list, the '>>'
27633 is a typo for '>'. Emit an error message and continue.
27634 Same deal about the token location, but here we can get it
27635 right by consuming the '>>' before issuing the diagnostic. */
27636 cp_token *token = cp_lexer_consume_token (parser->lexer);
27637 error_at (token->location,
27638 "spurious %<>>%>, use %<>%> to terminate "
27639 "a template argument list");
27640 }
27641 }
27642 else
27643 cp_parser_skip_to_end_of_template_parameter_list (parser);
27644 /* The `>' token might be a greater-than operator again now. */
27645 parser->greater_than_is_operator_p
27646 = saved_greater_than_is_operator_p;
27647 /* Restore the SAVED_SCOPE. */
27648 parser->scope = saved_scope;
27649 parser->qualifying_scope = saved_qualifying_scope;
27650 parser->object_scope = saved_object_scope;
27651 cp_unevaluated_operand = saved_unevaluated_operand;
27652 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27653
27654 return arguments;
27655 }
27656
27657 /* MEMBER_FUNCTION is a member function, or a friend. If default
27658 arguments, or the body of the function have not yet been parsed,
27659 parse them now. */
27660
27661 static void
27662 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27663 {
27664 timevar_push (TV_PARSE_INMETH);
27665 /* If this member is a template, get the underlying
27666 FUNCTION_DECL. */
27667 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27668 member_function = DECL_TEMPLATE_RESULT (member_function);
27669
27670 /* There should not be any class definitions in progress at this
27671 point; the bodies of members are only parsed outside of all class
27672 definitions. */
27673 gcc_assert (parser->num_classes_being_defined == 0);
27674 /* While we're parsing the member functions we might encounter more
27675 classes. We want to handle them right away, but we don't want
27676 them getting mixed up with functions that are currently in the
27677 queue. */
27678 push_unparsed_function_queues (parser);
27679
27680 /* Make sure that any template parameters are in scope. */
27681 maybe_begin_member_template_processing (member_function);
27682
27683 /* If the body of the function has not yet been parsed, parse it
27684 now. */
27685 if (DECL_PENDING_INLINE_P (member_function))
27686 {
27687 tree function_scope;
27688 cp_token_cache *tokens;
27689
27690 /* The function is no longer pending; we are processing it. */
27691 tokens = DECL_PENDING_INLINE_INFO (member_function);
27692 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27693 DECL_PENDING_INLINE_P (member_function) = 0;
27694
27695 /* If this is a local class, enter the scope of the containing
27696 function. */
27697 function_scope = current_function_decl;
27698 if (function_scope)
27699 push_function_context ();
27700
27701 /* Push the body of the function onto the lexer stack. */
27702 cp_parser_push_lexer_for_tokens (parser, tokens);
27703
27704 /* Let the front end know that we going to be defining this
27705 function. */
27706 start_preparsed_function (member_function, NULL_TREE,
27707 SF_PRE_PARSED | SF_INCLASS_INLINE);
27708
27709 /* Don't do access checking if it is a templated function. */
27710 if (processing_template_decl)
27711 push_deferring_access_checks (dk_no_check);
27712
27713 /* #pragma omp declare reduction needs special parsing. */
27714 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27715 {
27716 parser->lexer->in_pragma = true;
27717 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27718 finish_function (/*inline_p=*/true);
27719 cp_check_omp_declare_reduction (member_function);
27720 }
27721 else
27722 /* Now, parse the body of the function. */
27723 cp_parser_function_definition_after_declarator (parser,
27724 /*inline_p=*/true);
27725
27726 if (processing_template_decl)
27727 pop_deferring_access_checks ();
27728
27729 /* Leave the scope of the containing function. */
27730 if (function_scope)
27731 pop_function_context ();
27732 cp_parser_pop_lexer (parser);
27733 }
27734
27735 /* Remove any template parameters from the symbol table. */
27736 maybe_end_member_template_processing ();
27737
27738 /* Restore the queue. */
27739 pop_unparsed_function_queues (parser);
27740 timevar_pop (TV_PARSE_INMETH);
27741 }
27742
27743 /* If DECL contains any default args, remember it on the unparsed
27744 functions queue. */
27745
27746 static void
27747 cp_parser_save_default_args (cp_parser* parser, tree decl)
27748 {
27749 tree probe;
27750
27751 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27752 probe;
27753 probe = TREE_CHAIN (probe))
27754 if (TREE_PURPOSE (probe))
27755 {
27756 cp_default_arg_entry entry = {current_class_type, decl};
27757 vec_safe_push (unparsed_funs_with_default_args, entry);
27758 break;
27759 }
27760 }
27761
27762 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27763 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27764 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27765 from the parameter-type-list. */
27766
27767 static tree
27768 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27769 tree default_arg, tree parmtype)
27770 {
27771 cp_token_cache *tokens;
27772 tree parsed_arg;
27773 bool dummy;
27774
27775 if (default_arg == error_mark_node)
27776 return error_mark_node;
27777
27778 /* Push the saved tokens for the default argument onto the parser's
27779 lexer stack. */
27780 tokens = DEFARG_TOKENS (default_arg);
27781 cp_parser_push_lexer_for_tokens (parser, tokens);
27782
27783 start_lambda_scope (decl);
27784
27785 /* Parse the default argument. */
27786 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27787 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27788 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27789
27790 finish_lambda_scope ();
27791
27792 if (parsed_arg == error_mark_node)
27793 cp_parser_skip_to_end_of_statement (parser);
27794
27795 if (!processing_template_decl)
27796 {
27797 /* In a non-template class, check conversions now. In a template,
27798 we'll wait and instantiate these as needed. */
27799 if (TREE_CODE (decl) == PARM_DECL)
27800 parsed_arg = check_default_argument (parmtype, parsed_arg,
27801 tf_warning_or_error);
27802 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27803 parsed_arg = error_mark_node;
27804 else
27805 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27806 }
27807
27808 /* If the token stream has not been completely used up, then
27809 there was extra junk after the end of the default
27810 argument. */
27811 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27812 {
27813 if (TREE_CODE (decl) == PARM_DECL)
27814 cp_parser_error (parser, "expected %<,%>");
27815 else
27816 cp_parser_error (parser, "expected %<;%>");
27817 }
27818
27819 /* Revert to the main lexer. */
27820 cp_parser_pop_lexer (parser);
27821
27822 return parsed_arg;
27823 }
27824
27825 /* FIELD is a non-static data member with an initializer which we saved for
27826 later; parse it now. */
27827
27828 static void
27829 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27830 {
27831 tree def;
27832
27833 maybe_begin_member_template_processing (field);
27834
27835 push_unparsed_function_queues (parser);
27836 def = cp_parser_late_parse_one_default_arg (parser, field,
27837 DECL_INITIAL (field),
27838 NULL_TREE);
27839 pop_unparsed_function_queues (parser);
27840
27841 maybe_end_member_template_processing ();
27842
27843 DECL_INITIAL (field) = def;
27844 }
27845
27846 /* FN is a FUNCTION_DECL which may contains a parameter with an
27847 unparsed DEFAULT_ARG. Parse the default args now. This function
27848 assumes that the current scope is the scope in which the default
27849 argument should be processed. */
27850
27851 static void
27852 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27853 {
27854 bool saved_local_variables_forbidden_p;
27855 tree parm, parmdecl;
27856
27857 /* While we're parsing the default args, we might (due to the
27858 statement expression extension) encounter more classes. We want
27859 to handle them right away, but we don't want them getting mixed
27860 up with default args that are currently in the queue. */
27861 push_unparsed_function_queues (parser);
27862
27863 /* Local variable names (and the `this' keyword) may not appear
27864 in a default argument. */
27865 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27866 parser->local_variables_forbidden_p = true;
27867
27868 push_defarg_context (fn);
27869
27870 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27871 parmdecl = DECL_ARGUMENTS (fn);
27872 parm && parm != void_list_node;
27873 parm = TREE_CHAIN (parm),
27874 parmdecl = DECL_CHAIN (parmdecl))
27875 {
27876 tree default_arg = TREE_PURPOSE (parm);
27877 tree parsed_arg;
27878 vec<tree, va_gc> *insts;
27879 tree copy;
27880 unsigned ix;
27881
27882 if (!default_arg)
27883 continue;
27884
27885 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27886 /* This can happen for a friend declaration for a function
27887 already declared with default arguments. */
27888 continue;
27889
27890 parsed_arg
27891 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27892 default_arg,
27893 TREE_VALUE (parm));
27894 TREE_PURPOSE (parm) = parsed_arg;
27895
27896 /* Update any instantiations we've already created. */
27897 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27898 vec_safe_iterate (insts, ix, &copy); ix++)
27899 TREE_PURPOSE (copy) = parsed_arg;
27900 }
27901
27902 pop_defarg_context ();
27903
27904 /* Make sure no default arg is missing. */
27905 check_default_args (fn);
27906
27907 /* Restore the state of local_variables_forbidden_p. */
27908 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27909
27910 /* Restore the queue. */
27911 pop_unparsed_function_queues (parser);
27912 }
27913
27914 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27915
27916 sizeof ... ( identifier )
27917
27918 where the 'sizeof' token has already been consumed. */
27919
27920 static tree
27921 cp_parser_sizeof_pack (cp_parser *parser)
27922 {
27923 /* Consume the `...'. */
27924 cp_lexer_consume_token (parser->lexer);
27925 maybe_warn_variadic_templates ();
27926
27927 matching_parens parens;
27928 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27929 if (paren)
27930 parens.consume_open (parser);
27931 else
27932 permerror (cp_lexer_peek_token (parser->lexer)->location,
27933 "%<sizeof...%> argument must be surrounded by parentheses");
27934
27935 cp_token *token = cp_lexer_peek_token (parser->lexer);
27936 tree name = cp_parser_identifier (parser);
27937 if (name == error_mark_node)
27938 return error_mark_node;
27939 /* The name is not qualified. */
27940 parser->scope = NULL_TREE;
27941 parser->qualifying_scope = NULL_TREE;
27942 parser->object_scope = NULL_TREE;
27943 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27944 if (expr == error_mark_node)
27945 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27946 token->location);
27947 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27948 expr = TREE_TYPE (expr);
27949 else if (TREE_CODE (expr) == CONST_DECL)
27950 expr = DECL_INITIAL (expr);
27951 expr = make_pack_expansion (expr);
27952 PACK_EXPANSION_SIZEOF_P (expr) = true;
27953
27954 if (paren)
27955 parens.require_close (parser);
27956
27957 return expr;
27958 }
27959
27960 /* Parse the operand of `sizeof' (or a similar operator). Returns
27961 either a TYPE or an expression, depending on the form of the
27962 input. The KEYWORD indicates which kind of expression we have
27963 encountered. */
27964
27965 static tree
27966 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27967 {
27968 tree expr = NULL_TREE;
27969 const char *saved_message;
27970 char *tmp;
27971 bool saved_integral_constant_expression_p;
27972 bool saved_non_integral_constant_expression_p;
27973
27974 /* If it's a `...', then we are computing the length of a parameter
27975 pack. */
27976 if (keyword == RID_SIZEOF
27977 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27978 return cp_parser_sizeof_pack (parser);
27979
27980 /* Types cannot be defined in a `sizeof' expression. Save away the
27981 old message. */
27982 saved_message = parser->type_definition_forbidden_message;
27983 /* And create the new one. */
27984 tmp = concat ("types may not be defined in %<",
27985 IDENTIFIER_POINTER (ridpointers[keyword]),
27986 "%> expressions", NULL);
27987 parser->type_definition_forbidden_message = tmp;
27988
27989 /* The restrictions on constant-expressions do not apply inside
27990 sizeof expressions. */
27991 saved_integral_constant_expression_p
27992 = parser->integral_constant_expression_p;
27993 saved_non_integral_constant_expression_p
27994 = parser->non_integral_constant_expression_p;
27995 parser->integral_constant_expression_p = false;
27996
27997 /* Do not actually evaluate the expression. */
27998 ++cp_unevaluated_operand;
27999 ++c_inhibit_evaluation_warnings;
28000 /* If it's a `(', then we might be looking at the type-id
28001 construction. */
28002 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28003 {
28004 tree type = NULL_TREE;
28005
28006 /* We can't be sure yet whether we're looking at a type-id or an
28007 expression. */
28008 cp_parser_parse_tentatively (parser);
28009
28010 matching_parens parens;
28011 parens.consume_open (parser);
28012
28013 /* Note: as a GNU Extension, compound literals are considered
28014 postfix-expressions as they are in C99, so they are valid
28015 arguments to sizeof. See comment in cp_parser_cast_expression
28016 for details. */
28017 if (cp_parser_compound_literal_p (parser))
28018 cp_parser_simulate_error (parser);
28019 else
28020 {
28021 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28022 parser->in_type_id_in_expr_p = true;
28023 /* Look for the type-id. */
28024 type = cp_parser_type_id (parser);
28025 /* Look for the closing `)'. */
28026 parens.require_close (parser);
28027 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28028 }
28029
28030 /* If all went well, then we're done. */
28031 if (cp_parser_parse_definitely (parser))
28032 {
28033 cp_decl_specifier_seq decl_specs;
28034
28035 /* Build a trivial decl-specifier-seq. */
28036 clear_decl_specs (&decl_specs);
28037 decl_specs.type = type;
28038
28039 /* Call grokdeclarator to figure out what type this is. */
28040 expr = grokdeclarator (NULL,
28041 &decl_specs,
28042 TYPENAME,
28043 /*initialized=*/0,
28044 /*attrlist=*/NULL);
28045 }
28046 }
28047
28048 /* If the type-id production did not work out, then we must be
28049 looking at the unary-expression production. */
28050 if (!expr)
28051 expr = cp_parser_unary_expression (parser);
28052
28053 /* Go back to evaluating expressions. */
28054 --cp_unevaluated_operand;
28055 --c_inhibit_evaluation_warnings;
28056
28057 /* Free the message we created. */
28058 free (tmp);
28059 /* And restore the old one. */
28060 parser->type_definition_forbidden_message = saved_message;
28061 parser->integral_constant_expression_p
28062 = saved_integral_constant_expression_p;
28063 parser->non_integral_constant_expression_p
28064 = saved_non_integral_constant_expression_p;
28065
28066 return expr;
28067 }
28068
28069 /* If the current declaration has no declarator, return true. */
28070
28071 static bool
28072 cp_parser_declares_only_class_p (cp_parser *parser)
28073 {
28074 /* If the next token is a `;' or a `,' then there is no
28075 declarator. */
28076 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28077 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28078 }
28079
28080 /* Update the DECL_SPECS to reflect the storage class indicated by
28081 KEYWORD. */
28082
28083 static void
28084 cp_parser_set_storage_class (cp_parser *parser,
28085 cp_decl_specifier_seq *decl_specs,
28086 enum rid keyword,
28087 cp_token *token)
28088 {
28089 cp_storage_class storage_class;
28090
28091 if (parser->in_unbraced_linkage_specification_p)
28092 {
28093 error_at (token->location, "invalid use of %qD in linkage specification",
28094 ridpointers[keyword]);
28095 return;
28096 }
28097 else if (decl_specs->storage_class != sc_none)
28098 {
28099 decl_specs->conflicting_specifiers_p = true;
28100 return;
28101 }
28102
28103 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28104 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28105 && decl_specs->gnu_thread_keyword_p)
28106 {
28107 pedwarn (decl_specs->locations[ds_thread], 0,
28108 "%<__thread%> before %qD", ridpointers[keyword]);
28109 }
28110
28111 switch (keyword)
28112 {
28113 case RID_AUTO:
28114 storage_class = sc_auto;
28115 break;
28116 case RID_REGISTER:
28117 storage_class = sc_register;
28118 break;
28119 case RID_STATIC:
28120 storage_class = sc_static;
28121 break;
28122 case RID_EXTERN:
28123 storage_class = sc_extern;
28124 break;
28125 case RID_MUTABLE:
28126 storage_class = sc_mutable;
28127 break;
28128 default:
28129 gcc_unreachable ();
28130 }
28131 decl_specs->storage_class = storage_class;
28132 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28133
28134 /* A storage class specifier cannot be applied alongside a typedef
28135 specifier. If there is a typedef specifier present then set
28136 conflicting_specifiers_p which will trigger an error later
28137 on in grokdeclarator. */
28138 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28139 decl_specs->conflicting_specifiers_p = true;
28140 }
28141
28142 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28143 is true, the type is a class or enum definition. */
28144
28145 static void
28146 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28147 tree type_spec,
28148 cp_token *token,
28149 bool type_definition_p)
28150 {
28151 decl_specs->any_specifiers_p = true;
28152
28153 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28154 (with, for example, in "typedef int wchar_t;") we remember that
28155 this is what happened. In system headers, we ignore these
28156 declarations so that G++ can work with system headers that are not
28157 C++-safe. */
28158 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28159 && !type_definition_p
28160 && (type_spec == boolean_type_node
28161 || type_spec == char16_type_node
28162 || type_spec == char32_type_node
28163 || type_spec == wchar_type_node)
28164 && (decl_specs->type
28165 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28166 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28167 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28168 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28169 {
28170 decl_specs->redefined_builtin_type = type_spec;
28171 set_and_check_decl_spec_loc (decl_specs,
28172 ds_redefined_builtin_type_spec,
28173 token);
28174 if (!decl_specs->type)
28175 {
28176 decl_specs->type = type_spec;
28177 decl_specs->type_definition_p = false;
28178 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28179 }
28180 }
28181 else if (decl_specs->type)
28182 decl_specs->multiple_types_p = true;
28183 else
28184 {
28185 decl_specs->type = type_spec;
28186 decl_specs->type_definition_p = type_definition_p;
28187 decl_specs->redefined_builtin_type = NULL_TREE;
28188 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28189 }
28190 }
28191
28192 /* True iff TOKEN is the GNU keyword __thread. */
28193
28194 static bool
28195 token_is__thread (cp_token *token)
28196 {
28197 gcc_assert (token->keyword == RID_THREAD);
28198 return id_equal (token->u.value, "__thread");
28199 }
28200
28201 /* Set the location for a declarator specifier and check if it is
28202 duplicated.
28203
28204 DECL_SPECS is the sequence of declarator specifiers onto which to
28205 set the location.
28206
28207 DS is the single declarator specifier to set which location is to
28208 be set onto the existing sequence of declarators.
28209
28210 LOCATION is the location for the declarator specifier to
28211 consider. */
28212
28213 static void
28214 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28215 cp_decl_spec ds, cp_token *token)
28216 {
28217 gcc_assert (ds < ds_last);
28218
28219 if (decl_specs == NULL)
28220 return;
28221
28222 source_location location = token->location;
28223
28224 if (decl_specs->locations[ds] == 0)
28225 {
28226 decl_specs->locations[ds] = location;
28227 if (ds == ds_thread)
28228 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28229 }
28230 else
28231 {
28232 if (ds == ds_long)
28233 {
28234 if (decl_specs->locations[ds_long_long] != 0)
28235 error_at (location,
28236 "%<long long long%> is too long for GCC");
28237 else
28238 {
28239 decl_specs->locations[ds_long_long] = location;
28240 pedwarn_cxx98 (location,
28241 OPT_Wlong_long,
28242 "ISO C++ 1998 does not support %<long long%>");
28243 }
28244 }
28245 else if (ds == ds_thread)
28246 {
28247 bool gnu = token_is__thread (token);
28248 if (gnu != decl_specs->gnu_thread_keyword_p)
28249 error_at (location,
28250 "both %<__thread%> and %<thread_local%> specified");
28251 else
28252 {
28253 gcc_rich_location richloc (location);
28254 richloc.add_fixit_remove ();
28255 error_at (&richloc, "duplicate %qD", token->u.value);
28256 }
28257 }
28258 else
28259 {
28260 static const char *const decl_spec_names[] = {
28261 "signed",
28262 "unsigned",
28263 "short",
28264 "long",
28265 "const",
28266 "volatile",
28267 "restrict",
28268 "inline",
28269 "virtual",
28270 "explicit",
28271 "friend",
28272 "typedef",
28273 "using",
28274 "constexpr",
28275 "__complex"
28276 };
28277 gcc_rich_location richloc (location);
28278 richloc.add_fixit_remove ();
28279 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28280 }
28281 }
28282 }
28283
28284 /* Return true iff the declarator specifier DS is present in the
28285 sequence of declarator specifiers DECL_SPECS. */
28286
28287 bool
28288 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28289 cp_decl_spec ds)
28290 {
28291 gcc_assert (ds < ds_last);
28292
28293 if (decl_specs == NULL)
28294 return false;
28295
28296 return decl_specs->locations[ds] != 0;
28297 }
28298
28299 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28300 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28301
28302 static bool
28303 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28304 {
28305 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28306 }
28307
28308 /* Issue an error message indicating that TOKEN_DESC was expected.
28309 If KEYWORD is true, it indicated this function is called by
28310 cp_parser_require_keword and the required token can only be
28311 a indicated keyword.
28312
28313 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28314 within any error as the location of an "opening" token matching
28315 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28316 RT_CLOSE_PAREN). */
28317
28318 static void
28319 cp_parser_required_error (cp_parser *parser,
28320 required_token token_desc,
28321 bool keyword,
28322 location_t matching_location)
28323 {
28324 if (cp_parser_simulate_error (parser))
28325 return;
28326
28327 const char *gmsgid = NULL;
28328 switch (token_desc)
28329 {
28330 case RT_NEW:
28331 gmsgid = G_("expected %<new%>");
28332 break;
28333 case RT_DELETE:
28334 gmsgid = G_("expected %<delete%>");
28335 break;
28336 case RT_RETURN:
28337 gmsgid = G_("expected %<return%>");
28338 break;
28339 case RT_WHILE:
28340 gmsgid = G_("expected %<while%>");
28341 break;
28342 case RT_EXTERN:
28343 gmsgid = G_("expected %<extern%>");
28344 break;
28345 case RT_STATIC_ASSERT:
28346 gmsgid = G_("expected %<static_assert%>");
28347 break;
28348 case RT_DECLTYPE:
28349 gmsgid = G_("expected %<decltype%>");
28350 break;
28351 case RT_OPERATOR:
28352 gmsgid = G_("expected %<operator%>");
28353 break;
28354 case RT_CLASS:
28355 gmsgid = G_("expected %<class%>");
28356 break;
28357 case RT_TEMPLATE:
28358 gmsgid = G_("expected %<template%>");
28359 break;
28360 case RT_NAMESPACE:
28361 gmsgid = G_("expected %<namespace%>");
28362 break;
28363 case RT_USING:
28364 gmsgid = G_("expected %<using%>");
28365 break;
28366 case RT_ASM:
28367 gmsgid = G_("expected %<asm%>");
28368 break;
28369 case RT_TRY:
28370 gmsgid = G_("expected %<try%>");
28371 break;
28372 case RT_CATCH:
28373 gmsgid = G_("expected %<catch%>");
28374 break;
28375 case RT_THROW:
28376 gmsgid = G_("expected %<throw%>");
28377 break;
28378 case RT_LABEL:
28379 gmsgid = G_("expected %<__label__%>");
28380 break;
28381 case RT_AT_TRY:
28382 gmsgid = G_("expected %<@try%>");
28383 break;
28384 case RT_AT_SYNCHRONIZED:
28385 gmsgid = G_("expected %<@synchronized%>");
28386 break;
28387 case RT_AT_THROW:
28388 gmsgid = G_("expected %<@throw%>");
28389 break;
28390 case RT_TRANSACTION_ATOMIC:
28391 gmsgid = G_("expected %<__transaction_atomic%>");
28392 break;
28393 case RT_TRANSACTION_RELAXED:
28394 gmsgid = G_("expected %<__transaction_relaxed%>");
28395 break;
28396 default:
28397 break;
28398 }
28399
28400 if (!gmsgid && !keyword)
28401 {
28402 switch (token_desc)
28403 {
28404 case RT_SEMICOLON:
28405 gmsgid = G_("expected %<;%>");
28406 break;
28407 case RT_OPEN_PAREN:
28408 gmsgid = G_("expected %<(%>");
28409 break;
28410 case RT_CLOSE_BRACE:
28411 gmsgid = G_("expected %<}%>");
28412 break;
28413 case RT_OPEN_BRACE:
28414 gmsgid = G_("expected %<{%>");
28415 break;
28416 case RT_CLOSE_SQUARE:
28417 gmsgid = G_("expected %<]%>");
28418 break;
28419 case RT_OPEN_SQUARE:
28420 gmsgid = G_("expected %<[%>");
28421 break;
28422 case RT_COMMA:
28423 gmsgid = G_("expected %<,%>");
28424 break;
28425 case RT_SCOPE:
28426 gmsgid = G_("expected %<::%>");
28427 break;
28428 case RT_LESS:
28429 gmsgid = G_("expected %<<%>");
28430 break;
28431 case RT_GREATER:
28432 gmsgid = G_("expected %<>%>");
28433 break;
28434 case RT_EQ:
28435 gmsgid = G_("expected %<=%>");
28436 break;
28437 case RT_ELLIPSIS:
28438 gmsgid = G_("expected %<...%>");
28439 break;
28440 case RT_MULT:
28441 gmsgid = G_("expected %<*%>");
28442 break;
28443 case RT_COMPL:
28444 gmsgid = G_("expected %<~%>");
28445 break;
28446 case RT_COLON:
28447 gmsgid = G_("expected %<:%>");
28448 break;
28449 case RT_COLON_SCOPE:
28450 gmsgid = G_("expected %<:%> or %<::%>");
28451 break;
28452 case RT_CLOSE_PAREN:
28453 gmsgid = G_("expected %<)%>");
28454 break;
28455 case RT_COMMA_CLOSE_PAREN:
28456 gmsgid = G_("expected %<,%> or %<)%>");
28457 break;
28458 case RT_PRAGMA_EOL:
28459 gmsgid = G_("expected end of line");
28460 break;
28461 case RT_NAME:
28462 gmsgid = G_("expected identifier");
28463 break;
28464 case RT_SELECT:
28465 gmsgid = G_("expected selection-statement");
28466 break;
28467 case RT_ITERATION:
28468 gmsgid = G_("expected iteration-statement");
28469 break;
28470 case RT_JUMP:
28471 gmsgid = G_("expected jump-statement");
28472 break;
28473 case RT_CLASS_KEY:
28474 gmsgid = G_("expected class-key");
28475 break;
28476 case RT_CLASS_TYPENAME_TEMPLATE:
28477 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28478 break;
28479 default:
28480 gcc_unreachable ();
28481 }
28482 }
28483
28484 if (gmsgid)
28485 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28486 }
28487
28488
28489 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28490 issue an error message indicating that TOKEN_DESC was expected.
28491
28492 Returns the token consumed, if the token had the appropriate type.
28493 Otherwise, returns NULL.
28494
28495 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28496 within any error as the location of an "opening" token matching
28497 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28498 RT_CLOSE_PAREN). */
28499
28500 static cp_token *
28501 cp_parser_require (cp_parser* parser,
28502 enum cpp_ttype type,
28503 required_token token_desc,
28504 location_t matching_location)
28505 {
28506 if (cp_lexer_next_token_is (parser->lexer, type))
28507 return cp_lexer_consume_token (parser->lexer);
28508 else
28509 {
28510 /* Output the MESSAGE -- unless we're parsing tentatively. */
28511 if (!cp_parser_simulate_error (parser))
28512 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28513 matching_location);
28514 return NULL;
28515 }
28516 }
28517
28518 /* An error message is produced if the next token is not '>'.
28519 All further tokens are skipped until the desired token is
28520 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28521
28522 static void
28523 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28524 {
28525 /* Current level of '< ... >'. */
28526 unsigned level = 0;
28527 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28528 unsigned nesting_depth = 0;
28529
28530 /* Are we ready, yet? If not, issue error message. */
28531 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28532 return;
28533
28534 /* Skip tokens until the desired token is found. */
28535 while (true)
28536 {
28537 /* Peek at the next token. */
28538 switch (cp_lexer_peek_token (parser->lexer)->type)
28539 {
28540 case CPP_LESS:
28541 if (!nesting_depth)
28542 ++level;
28543 break;
28544
28545 case CPP_RSHIFT:
28546 if (cxx_dialect == cxx98)
28547 /* C++0x views the `>>' operator as two `>' tokens, but
28548 C++98 does not. */
28549 break;
28550 else if (!nesting_depth && level-- == 0)
28551 {
28552 /* We've hit a `>>' where the first `>' closes the
28553 template argument list, and the second `>' is
28554 spurious. Just consume the `>>' and stop; we've
28555 already produced at least one error. */
28556 cp_lexer_consume_token (parser->lexer);
28557 return;
28558 }
28559 /* Fall through for C++0x, so we handle the second `>' in
28560 the `>>'. */
28561 gcc_fallthrough ();
28562
28563 case CPP_GREATER:
28564 if (!nesting_depth && level-- == 0)
28565 {
28566 /* We've reached the token we want, consume it and stop. */
28567 cp_lexer_consume_token (parser->lexer);
28568 return;
28569 }
28570 break;
28571
28572 case CPP_OPEN_PAREN:
28573 case CPP_OPEN_SQUARE:
28574 ++nesting_depth;
28575 break;
28576
28577 case CPP_CLOSE_PAREN:
28578 case CPP_CLOSE_SQUARE:
28579 if (nesting_depth-- == 0)
28580 return;
28581 break;
28582
28583 case CPP_EOF:
28584 case CPP_PRAGMA_EOL:
28585 case CPP_SEMICOLON:
28586 case CPP_OPEN_BRACE:
28587 case CPP_CLOSE_BRACE:
28588 /* The '>' was probably forgotten, don't look further. */
28589 return;
28590
28591 default:
28592 break;
28593 }
28594
28595 /* Consume this token. */
28596 cp_lexer_consume_token (parser->lexer);
28597 }
28598 }
28599
28600 /* If the next token is the indicated keyword, consume it. Otherwise,
28601 issue an error message indicating that TOKEN_DESC was expected.
28602
28603 Returns the token consumed, if the token had the appropriate type.
28604 Otherwise, returns NULL. */
28605
28606 static cp_token *
28607 cp_parser_require_keyword (cp_parser* parser,
28608 enum rid keyword,
28609 required_token token_desc)
28610 {
28611 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28612
28613 if (token && token->keyword != keyword)
28614 {
28615 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28616 UNKNOWN_LOCATION);
28617 return NULL;
28618 }
28619
28620 return token;
28621 }
28622
28623 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28624 function-definition. */
28625
28626 static bool
28627 cp_parser_token_starts_function_definition_p (cp_token* token)
28628 {
28629 return (/* An ordinary function-body begins with an `{'. */
28630 token->type == CPP_OPEN_BRACE
28631 /* A ctor-initializer begins with a `:'. */
28632 || token->type == CPP_COLON
28633 /* A function-try-block begins with `try'. */
28634 || token->keyword == RID_TRY
28635 /* A function-transaction-block begins with `__transaction_atomic'
28636 or `__transaction_relaxed'. */
28637 || token->keyword == RID_TRANSACTION_ATOMIC
28638 || token->keyword == RID_TRANSACTION_RELAXED
28639 /* The named return value extension begins with `return'. */
28640 || token->keyword == RID_RETURN);
28641 }
28642
28643 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28644 definition. */
28645
28646 static bool
28647 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28648 {
28649 cp_token *token;
28650
28651 token = cp_lexer_peek_token (parser->lexer);
28652 return (token->type == CPP_OPEN_BRACE
28653 || (token->type == CPP_COLON
28654 && !parser->colon_doesnt_start_class_def_p));
28655 }
28656
28657 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28658 C++0x) ending a template-argument. */
28659
28660 static bool
28661 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28662 {
28663 cp_token *token;
28664
28665 token = cp_lexer_peek_token (parser->lexer);
28666 return (token->type == CPP_COMMA
28667 || token->type == CPP_GREATER
28668 || token->type == CPP_ELLIPSIS
28669 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28670 }
28671
28672 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28673 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28674
28675 static bool
28676 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28677 size_t n)
28678 {
28679 cp_token *token;
28680
28681 token = cp_lexer_peek_nth_token (parser->lexer, n);
28682 if (token->type == CPP_LESS)
28683 return true;
28684 /* Check for the sequence `<::' in the original code. It would be lexed as
28685 `[:', where `[' is a digraph, and there is no whitespace before
28686 `:'. */
28687 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28688 {
28689 cp_token *token2;
28690 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28691 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28692 return true;
28693 }
28694 return false;
28695 }
28696
28697 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28698 or none_type otherwise. */
28699
28700 static enum tag_types
28701 cp_parser_token_is_class_key (cp_token* token)
28702 {
28703 switch (token->keyword)
28704 {
28705 case RID_CLASS:
28706 return class_type;
28707 case RID_STRUCT:
28708 return record_type;
28709 case RID_UNION:
28710 return union_type;
28711
28712 default:
28713 return none_type;
28714 }
28715 }
28716
28717 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28718 or none_type otherwise or if the token is null. */
28719
28720 static enum tag_types
28721 cp_parser_token_is_type_parameter_key (cp_token* token)
28722 {
28723 if (!token)
28724 return none_type;
28725
28726 switch (token->keyword)
28727 {
28728 case RID_CLASS:
28729 return class_type;
28730 case RID_TYPENAME:
28731 return typename_type;
28732
28733 default:
28734 return none_type;
28735 }
28736 }
28737
28738 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28739
28740 static void
28741 cp_parser_check_class_key (enum tag_types class_key, tree type)
28742 {
28743 if (type == error_mark_node)
28744 return;
28745 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28746 {
28747 if (permerror (input_location, "%qs tag used in naming %q#T",
28748 class_key == union_type ? "union"
28749 : class_key == record_type ? "struct" : "class",
28750 type))
28751 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28752 "%q#T was previously declared here", type);
28753 }
28754 }
28755
28756 /* Issue an error message if DECL is redeclared with different
28757 access than its original declaration [class.access.spec/3].
28758 This applies to nested classes, nested class templates and
28759 enumerations [class.mem/1]. */
28760
28761 static void
28762 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28763 {
28764 if (!decl
28765 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28766 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28767 return;
28768
28769 if ((TREE_PRIVATE (decl)
28770 != (current_access_specifier == access_private_node))
28771 || (TREE_PROTECTED (decl)
28772 != (current_access_specifier == access_protected_node)))
28773 error_at (location, "%qD redeclared with different access", decl);
28774 }
28775
28776 /* Look for the `template' keyword, as a syntactic disambiguator.
28777 Return TRUE iff it is present, in which case it will be
28778 consumed. */
28779
28780 static bool
28781 cp_parser_optional_template_keyword (cp_parser *parser)
28782 {
28783 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28784 {
28785 /* In C++98 the `template' keyword can only be used within templates;
28786 outside templates the parser can always figure out what is a
28787 template and what is not. In C++11, per the resolution of DR 468,
28788 `template' is allowed in cases where it is not strictly necessary. */
28789 if (!processing_template_decl
28790 && pedantic && cxx_dialect == cxx98)
28791 {
28792 cp_token *token = cp_lexer_peek_token (parser->lexer);
28793 pedwarn (token->location, OPT_Wpedantic,
28794 "in C++98 %<template%> (as a disambiguator) is only "
28795 "allowed within templates");
28796 /* If this part of the token stream is rescanned, the same
28797 error message would be generated. So, we purge the token
28798 from the stream. */
28799 cp_lexer_purge_token (parser->lexer);
28800 return false;
28801 }
28802 else
28803 {
28804 /* Consume the `template' keyword. */
28805 cp_lexer_consume_token (parser->lexer);
28806 return true;
28807 }
28808 }
28809 return false;
28810 }
28811
28812 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28813 set PARSER->SCOPE, and perform other related actions. */
28814
28815 static void
28816 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28817 {
28818 struct tree_check *check_value;
28819
28820 /* Get the stored value. */
28821 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28822 /* Set the scope from the stored value. */
28823 parser->scope = saved_checks_value (check_value);
28824 parser->qualifying_scope = check_value->qualifying_scope;
28825 parser->object_scope = NULL_TREE;
28826 }
28827
28828 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28829 encounter the end of a block before what we were looking for. */
28830
28831 static bool
28832 cp_parser_cache_group (cp_parser *parser,
28833 enum cpp_ttype end,
28834 unsigned depth)
28835 {
28836 while (true)
28837 {
28838 cp_token *token = cp_lexer_peek_token (parser->lexer);
28839
28840 /* Abort a parenthesized expression if we encounter a semicolon. */
28841 if ((end == CPP_CLOSE_PAREN || depth == 0)
28842 && token->type == CPP_SEMICOLON)
28843 return true;
28844 /* If we've reached the end of the file, stop. */
28845 if (token->type == CPP_EOF
28846 || (end != CPP_PRAGMA_EOL
28847 && token->type == CPP_PRAGMA_EOL))
28848 return true;
28849 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28850 /* We've hit the end of an enclosing block, so there's been some
28851 kind of syntax error. */
28852 return true;
28853
28854 /* Consume the token. */
28855 cp_lexer_consume_token (parser->lexer);
28856 /* See if it starts a new group. */
28857 if (token->type == CPP_OPEN_BRACE)
28858 {
28859 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28860 /* In theory this should probably check end == '}', but
28861 cp_parser_save_member_function_body needs it to exit
28862 after either '}' or ')' when called with ')'. */
28863 if (depth == 0)
28864 return false;
28865 }
28866 else if (token->type == CPP_OPEN_PAREN)
28867 {
28868 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28869 if (depth == 0 && end == CPP_CLOSE_PAREN)
28870 return false;
28871 }
28872 else if (token->type == CPP_PRAGMA)
28873 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28874 else if (token->type == end)
28875 return false;
28876 }
28877 }
28878
28879 /* Like above, for caching a default argument or NSDMI. Both of these are
28880 terminated by a non-nested comma, but it can be unclear whether or not a
28881 comma is nested in a template argument list unless we do more parsing.
28882 In order to handle this ambiguity, when we encounter a ',' after a '<'
28883 we try to parse what follows as a parameter-declaration-list (in the
28884 case of a default argument) or a member-declarator (in the case of an
28885 NSDMI). If that succeeds, then we stop caching. */
28886
28887 static tree
28888 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28889 {
28890 unsigned depth = 0;
28891 int maybe_template_id = 0;
28892 cp_token *first_token;
28893 cp_token *token;
28894 tree default_argument;
28895
28896 /* Add tokens until we have processed the entire default
28897 argument. We add the range [first_token, token). */
28898 first_token = cp_lexer_peek_token (parser->lexer);
28899 if (first_token->type == CPP_OPEN_BRACE)
28900 {
28901 /* For list-initialization, this is straightforward. */
28902 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28903 token = cp_lexer_peek_token (parser->lexer);
28904 }
28905 else while (true)
28906 {
28907 bool done = false;
28908
28909 /* Peek at the next token. */
28910 token = cp_lexer_peek_token (parser->lexer);
28911 /* What we do depends on what token we have. */
28912 switch (token->type)
28913 {
28914 /* In valid code, a default argument must be
28915 immediately followed by a `,' `)', or `...'. */
28916 case CPP_COMMA:
28917 if (depth == 0 && maybe_template_id)
28918 {
28919 /* If we've seen a '<', we might be in a
28920 template-argument-list. Until Core issue 325 is
28921 resolved, we don't know how this situation ought
28922 to be handled, so try to DTRT. We check whether
28923 what comes after the comma is a valid parameter
28924 declaration list. If it is, then the comma ends
28925 the default argument; otherwise the default
28926 argument continues. */
28927 bool error = false;
28928 cp_token *peek;
28929
28930 /* Set ITALP so cp_parser_parameter_declaration_list
28931 doesn't decide to commit to this parse. */
28932 bool saved_italp = parser->in_template_argument_list_p;
28933 parser->in_template_argument_list_p = true;
28934
28935 cp_parser_parse_tentatively (parser);
28936
28937 if (nsdmi)
28938 {
28939 /* Parse declarators until we reach a non-comma or
28940 somthing that cannot be an initializer.
28941 Just checking whether we're looking at a single
28942 declarator is insufficient. Consider:
28943 int var = tuple<T,U>::x;
28944 The template parameter 'U' looks exactly like a
28945 declarator. */
28946 do
28947 {
28948 int ctor_dtor_or_conv_p;
28949 cp_lexer_consume_token (parser->lexer);
28950 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28951 &ctor_dtor_or_conv_p,
28952 /*parenthesized_p=*/NULL,
28953 /*member_p=*/true,
28954 /*friend_p=*/false);
28955 peek = cp_lexer_peek_token (parser->lexer);
28956 if (cp_parser_error_occurred (parser))
28957 break;
28958 }
28959 while (peek->type == CPP_COMMA);
28960 /* If we met an '=' or ';' then the original comma
28961 was the end of the NSDMI. Otherwise assume
28962 we're still in the NSDMI. */
28963 error = (peek->type != CPP_EQ
28964 && peek->type != CPP_SEMICOLON);
28965 }
28966 else
28967 {
28968 cp_lexer_consume_token (parser->lexer);
28969 begin_scope (sk_function_parms, NULL_TREE);
28970 cp_parser_parameter_declaration_list (parser, &error);
28971 pop_bindings_and_leave_scope ();
28972 }
28973 if (!cp_parser_error_occurred (parser) && !error)
28974 done = true;
28975 cp_parser_abort_tentative_parse (parser);
28976
28977 parser->in_template_argument_list_p = saved_italp;
28978 break;
28979 }
28980 /* FALLTHRU */
28981 case CPP_CLOSE_PAREN:
28982 case CPP_ELLIPSIS:
28983 /* If we run into a non-nested `;', `}', or `]',
28984 then the code is invalid -- but the default
28985 argument is certainly over. */
28986 case CPP_SEMICOLON:
28987 case CPP_CLOSE_BRACE:
28988 case CPP_CLOSE_SQUARE:
28989 if (depth == 0
28990 /* Handle correctly int n = sizeof ... ( p ); */
28991 && token->type != CPP_ELLIPSIS)
28992 done = true;
28993 /* Update DEPTH, if necessary. */
28994 else if (token->type == CPP_CLOSE_PAREN
28995 || token->type == CPP_CLOSE_BRACE
28996 || token->type == CPP_CLOSE_SQUARE)
28997 --depth;
28998 break;
28999
29000 case CPP_OPEN_PAREN:
29001 case CPP_OPEN_SQUARE:
29002 case CPP_OPEN_BRACE:
29003 ++depth;
29004 break;
29005
29006 case CPP_LESS:
29007 if (depth == 0)
29008 /* This might be the comparison operator, or it might
29009 start a template argument list. */
29010 ++maybe_template_id;
29011 break;
29012
29013 case CPP_RSHIFT:
29014 if (cxx_dialect == cxx98)
29015 break;
29016 /* Fall through for C++0x, which treats the `>>'
29017 operator like two `>' tokens in certain
29018 cases. */
29019 gcc_fallthrough ();
29020
29021 case CPP_GREATER:
29022 if (depth == 0)
29023 {
29024 /* This might be an operator, or it might close a
29025 template argument list. But if a previous '<'
29026 started a template argument list, this will have
29027 closed it, so we can't be in one anymore. */
29028 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29029 if (maybe_template_id < 0)
29030 maybe_template_id = 0;
29031 }
29032 break;
29033
29034 /* If we run out of tokens, issue an error message. */
29035 case CPP_EOF:
29036 case CPP_PRAGMA_EOL:
29037 error_at (token->location, "file ends in default argument");
29038 return error_mark_node;
29039
29040 case CPP_NAME:
29041 case CPP_SCOPE:
29042 /* In these cases, we should look for template-ids.
29043 For example, if the default argument is
29044 `X<int, double>()', we need to do name lookup to
29045 figure out whether or not `X' is a template; if
29046 so, the `,' does not end the default argument.
29047
29048 That is not yet done. */
29049 break;
29050
29051 default:
29052 break;
29053 }
29054
29055 /* If we've reached the end, stop. */
29056 if (done)
29057 break;
29058
29059 /* Add the token to the token block. */
29060 token = cp_lexer_consume_token (parser->lexer);
29061 }
29062
29063 /* Create a DEFAULT_ARG to represent the unparsed default
29064 argument. */
29065 default_argument = make_node (DEFAULT_ARG);
29066 DEFARG_TOKENS (default_argument)
29067 = cp_token_cache_new (first_token, token);
29068 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29069
29070 return default_argument;
29071 }
29072
29073 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29074
29075 location_t
29076 defarg_location (tree default_argument)
29077 {
29078 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29079 location_t start = tokens->first->location;
29080 location_t end = tokens->last->location;
29081 return make_location (start, start, end);
29082 }
29083
29084 /* Begin parsing tentatively. We always save tokens while parsing
29085 tentatively so that if the tentative parsing fails we can restore the
29086 tokens. */
29087
29088 static void
29089 cp_parser_parse_tentatively (cp_parser* parser)
29090 {
29091 /* Enter a new parsing context. */
29092 parser->context = cp_parser_context_new (parser->context);
29093 /* Begin saving tokens. */
29094 cp_lexer_save_tokens (parser->lexer);
29095 /* In order to avoid repetitive access control error messages,
29096 access checks are queued up until we are no longer parsing
29097 tentatively. */
29098 push_deferring_access_checks (dk_deferred);
29099 }
29100
29101 /* Commit to the currently active tentative parse. */
29102
29103 static void
29104 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29105 {
29106 cp_parser_context *context;
29107 cp_lexer *lexer;
29108
29109 /* Mark all of the levels as committed. */
29110 lexer = parser->lexer;
29111 for (context = parser->context; context->next; context = context->next)
29112 {
29113 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29114 break;
29115 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29116 while (!cp_lexer_saving_tokens (lexer))
29117 lexer = lexer->next;
29118 cp_lexer_commit_tokens (lexer);
29119 }
29120 }
29121
29122 /* Commit to the topmost currently active tentative parse.
29123
29124 Note that this function shouldn't be called when there are
29125 irreversible side-effects while in a tentative state. For
29126 example, we shouldn't create a permanent entry in the symbol
29127 table, or issue an error message that might not apply if the
29128 tentative parse is aborted. */
29129
29130 static void
29131 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29132 {
29133 cp_parser_context *context = parser->context;
29134 cp_lexer *lexer = parser->lexer;
29135
29136 if (context)
29137 {
29138 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29139 return;
29140 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29141
29142 while (!cp_lexer_saving_tokens (lexer))
29143 lexer = lexer->next;
29144 cp_lexer_commit_tokens (lexer);
29145 }
29146 }
29147
29148 /* Abort the currently active tentative parse. All consumed tokens
29149 will be rolled back, and no diagnostics will be issued. */
29150
29151 static void
29152 cp_parser_abort_tentative_parse (cp_parser* parser)
29153 {
29154 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29155 || errorcount > 0);
29156 cp_parser_simulate_error (parser);
29157 /* Now, pretend that we want to see if the construct was
29158 successfully parsed. */
29159 cp_parser_parse_definitely (parser);
29160 }
29161
29162 /* Stop parsing tentatively. If a parse error has occurred, restore the
29163 token stream. Otherwise, commit to the tokens we have consumed.
29164 Returns true if no error occurred; false otherwise. */
29165
29166 static bool
29167 cp_parser_parse_definitely (cp_parser* parser)
29168 {
29169 bool error_occurred;
29170 cp_parser_context *context;
29171
29172 /* Remember whether or not an error occurred, since we are about to
29173 destroy that information. */
29174 error_occurred = cp_parser_error_occurred (parser);
29175 /* Remove the topmost context from the stack. */
29176 context = parser->context;
29177 parser->context = context->next;
29178 /* If no parse errors occurred, commit to the tentative parse. */
29179 if (!error_occurred)
29180 {
29181 /* Commit to the tokens read tentatively, unless that was
29182 already done. */
29183 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29184 cp_lexer_commit_tokens (parser->lexer);
29185
29186 pop_to_parent_deferring_access_checks ();
29187 }
29188 /* Otherwise, if errors occurred, roll back our state so that things
29189 are just as they were before we began the tentative parse. */
29190 else
29191 {
29192 cp_lexer_rollback_tokens (parser->lexer);
29193 pop_deferring_access_checks ();
29194 }
29195 /* Add the context to the front of the free list. */
29196 context->next = cp_parser_context_free_list;
29197 cp_parser_context_free_list = context;
29198
29199 return !error_occurred;
29200 }
29201
29202 /* Returns true if we are parsing tentatively and are not committed to
29203 this tentative parse. */
29204
29205 static bool
29206 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29207 {
29208 return (cp_parser_parsing_tentatively (parser)
29209 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29210 }
29211
29212 /* Returns nonzero iff an error has occurred during the most recent
29213 tentative parse. */
29214
29215 static bool
29216 cp_parser_error_occurred (cp_parser* parser)
29217 {
29218 return (cp_parser_parsing_tentatively (parser)
29219 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29220 }
29221
29222 /* Returns nonzero if GNU extensions are allowed. */
29223
29224 static bool
29225 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29226 {
29227 return parser->allow_gnu_extensions_p;
29228 }
29229 \f
29230 /* Objective-C++ Productions */
29231
29232
29233 /* Parse an Objective-C expression, which feeds into a primary-expression
29234 above.
29235
29236 objc-expression:
29237 objc-message-expression
29238 objc-string-literal
29239 objc-encode-expression
29240 objc-protocol-expression
29241 objc-selector-expression
29242
29243 Returns a tree representation of the expression. */
29244
29245 static cp_expr
29246 cp_parser_objc_expression (cp_parser* parser)
29247 {
29248 /* Try to figure out what kind of declaration is present. */
29249 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29250
29251 switch (kwd->type)
29252 {
29253 case CPP_OPEN_SQUARE:
29254 return cp_parser_objc_message_expression (parser);
29255
29256 case CPP_OBJC_STRING:
29257 kwd = cp_lexer_consume_token (parser->lexer);
29258 return objc_build_string_object (kwd->u.value);
29259
29260 case CPP_KEYWORD:
29261 switch (kwd->keyword)
29262 {
29263 case RID_AT_ENCODE:
29264 return cp_parser_objc_encode_expression (parser);
29265
29266 case RID_AT_PROTOCOL:
29267 return cp_parser_objc_protocol_expression (parser);
29268
29269 case RID_AT_SELECTOR:
29270 return cp_parser_objc_selector_expression (parser);
29271
29272 default:
29273 break;
29274 }
29275 default:
29276 error_at (kwd->location,
29277 "misplaced %<@%D%> Objective-C++ construct",
29278 kwd->u.value);
29279 cp_parser_skip_to_end_of_block_or_statement (parser);
29280 }
29281
29282 return error_mark_node;
29283 }
29284
29285 /* Parse an Objective-C message expression.
29286
29287 objc-message-expression:
29288 [ objc-message-receiver objc-message-args ]
29289
29290 Returns a representation of an Objective-C message. */
29291
29292 static tree
29293 cp_parser_objc_message_expression (cp_parser* parser)
29294 {
29295 tree receiver, messageargs;
29296
29297 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29298 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29299 receiver = cp_parser_objc_message_receiver (parser);
29300 messageargs = cp_parser_objc_message_args (parser);
29301 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29302 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29303
29304 tree result = objc_build_message_expr (receiver, messageargs);
29305
29306 /* Construct a location e.g.
29307 [self func1:5]
29308 ^~~~~~~~~~~~~~
29309 ranging from the '[' to the ']', with the caret at the start. */
29310 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29311 protected_set_expr_location (result, combined_loc);
29312
29313 return result;
29314 }
29315
29316 /* Parse an objc-message-receiver.
29317
29318 objc-message-receiver:
29319 expression
29320 simple-type-specifier
29321
29322 Returns a representation of the type or expression. */
29323
29324 static tree
29325 cp_parser_objc_message_receiver (cp_parser* parser)
29326 {
29327 tree rcv;
29328
29329 /* An Objective-C message receiver may be either (1) a type
29330 or (2) an expression. */
29331 cp_parser_parse_tentatively (parser);
29332 rcv = cp_parser_expression (parser);
29333
29334 /* If that worked out, fine. */
29335 if (cp_parser_parse_definitely (parser))
29336 return rcv;
29337
29338 cp_parser_parse_tentatively (parser);
29339 rcv = cp_parser_simple_type_specifier (parser,
29340 /*decl_specs=*/NULL,
29341 CP_PARSER_FLAGS_NONE);
29342
29343 if (cp_parser_parse_definitely (parser))
29344 return objc_get_class_reference (rcv);
29345
29346 cp_parser_error (parser, "objective-c++ message receiver expected");
29347 return error_mark_node;
29348 }
29349
29350 /* Parse the arguments and selectors comprising an Objective-C message.
29351
29352 objc-message-args:
29353 objc-selector
29354 objc-selector-args
29355 objc-selector-args , objc-comma-args
29356
29357 objc-selector-args:
29358 objc-selector [opt] : assignment-expression
29359 objc-selector-args objc-selector [opt] : assignment-expression
29360
29361 objc-comma-args:
29362 assignment-expression
29363 objc-comma-args , assignment-expression
29364
29365 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29366 selector arguments and TREE_VALUE containing a list of comma
29367 arguments. */
29368
29369 static tree
29370 cp_parser_objc_message_args (cp_parser* parser)
29371 {
29372 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29373 bool maybe_unary_selector_p = true;
29374 cp_token *token = cp_lexer_peek_token (parser->lexer);
29375
29376 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29377 {
29378 tree selector = NULL_TREE, arg;
29379
29380 if (token->type != CPP_COLON)
29381 selector = cp_parser_objc_selector (parser);
29382
29383 /* Detect if we have a unary selector. */
29384 if (maybe_unary_selector_p
29385 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29386 return build_tree_list (selector, NULL_TREE);
29387
29388 maybe_unary_selector_p = false;
29389 cp_parser_require (parser, CPP_COLON, RT_COLON);
29390 arg = cp_parser_assignment_expression (parser);
29391
29392 sel_args
29393 = chainon (sel_args,
29394 build_tree_list (selector, arg));
29395
29396 token = cp_lexer_peek_token (parser->lexer);
29397 }
29398
29399 /* Handle non-selector arguments, if any. */
29400 while (token->type == CPP_COMMA)
29401 {
29402 tree arg;
29403
29404 cp_lexer_consume_token (parser->lexer);
29405 arg = cp_parser_assignment_expression (parser);
29406
29407 addl_args
29408 = chainon (addl_args,
29409 build_tree_list (NULL_TREE, arg));
29410
29411 token = cp_lexer_peek_token (parser->lexer);
29412 }
29413
29414 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29415 {
29416 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29417 return build_tree_list (error_mark_node, error_mark_node);
29418 }
29419
29420 return build_tree_list (sel_args, addl_args);
29421 }
29422
29423 /* Parse an Objective-C encode expression.
29424
29425 objc-encode-expression:
29426 @encode objc-typename
29427
29428 Returns an encoded representation of the type argument. */
29429
29430 static cp_expr
29431 cp_parser_objc_encode_expression (cp_parser* parser)
29432 {
29433 tree type;
29434 cp_token *token;
29435 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29436
29437 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29438 matching_parens parens;
29439 parens.require_open (parser);
29440 token = cp_lexer_peek_token (parser->lexer);
29441 type = complete_type (cp_parser_type_id (parser));
29442 parens.require_close (parser);
29443
29444 if (!type)
29445 {
29446 error_at (token->location,
29447 "%<@encode%> must specify a type as an argument");
29448 return error_mark_node;
29449 }
29450
29451 /* This happens if we find @encode(T) (where T is a template
29452 typename or something dependent on a template typename) when
29453 parsing a template. In that case, we can't compile it
29454 immediately, but we rather create an AT_ENCODE_EXPR which will
29455 need to be instantiated when the template is used.
29456 */
29457 if (dependent_type_p (type))
29458 {
29459 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29460 TREE_READONLY (value) = 1;
29461 return value;
29462 }
29463
29464
29465 /* Build a location of the form:
29466 @encode(int)
29467 ^~~~~~~~~~~~
29468 with caret==start at the @ token, finishing at the close paren. */
29469 location_t combined_loc
29470 = make_location (start_loc, start_loc,
29471 cp_lexer_previous_token (parser->lexer)->location);
29472
29473 return cp_expr (objc_build_encode_expr (type), combined_loc);
29474 }
29475
29476 /* Parse an Objective-C @defs expression. */
29477
29478 static tree
29479 cp_parser_objc_defs_expression (cp_parser *parser)
29480 {
29481 tree name;
29482
29483 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29484 matching_parens parens;
29485 parens.require_open (parser);
29486 name = cp_parser_identifier (parser);
29487 parens.require_close (parser);
29488
29489 return objc_get_class_ivars (name);
29490 }
29491
29492 /* Parse an Objective-C protocol expression.
29493
29494 objc-protocol-expression:
29495 @protocol ( identifier )
29496
29497 Returns a representation of the protocol expression. */
29498
29499 static tree
29500 cp_parser_objc_protocol_expression (cp_parser* parser)
29501 {
29502 tree proto;
29503 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29504
29505 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29506 matching_parens parens;
29507 parens.require_open (parser);
29508 proto = cp_parser_identifier (parser);
29509 parens.require_close (parser);
29510
29511 /* Build a location of the form:
29512 @protocol(prot)
29513 ^~~~~~~~~~~~~~~
29514 with caret==start at the @ token, finishing at the close paren. */
29515 location_t combined_loc
29516 = make_location (start_loc, start_loc,
29517 cp_lexer_previous_token (parser->lexer)->location);
29518 tree result = objc_build_protocol_expr (proto);
29519 protected_set_expr_location (result, combined_loc);
29520 return result;
29521 }
29522
29523 /* Parse an Objective-C selector expression.
29524
29525 objc-selector-expression:
29526 @selector ( objc-method-signature )
29527
29528 objc-method-signature:
29529 objc-selector
29530 objc-selector-seq
29531
29532 objc-selector-seq:
29533 objc-selector :
29534 objc-selector-seq objc-selector :
29535
29536 Returns a representation of the method selector. */
29537
29538 static tree
29539 cp_parser_objc_selector_expression (cp_parser* parser)
29540 {
29541 tree sel_seq = NULL_TREE;
29542 bool maybe_unary_selector_p = true;
29543 cp_token *token;
29544 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29545
29546 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29547 matching_parens parens;
29548 parens.require_open (parser);
29549 token = cp_lexer_peek_token (parser->lexer);
29550
29551 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29552 || token->type == CPP_SCOPE)
29553 {
29554 tree selector = NULL_TREE;
29555
29556 if (token->type != CPP_COLON
29557 || token->type == CPP_SCOPE)
29558 selector = cp_parser_objc_selector (parser);
29559
29560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29561 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29562 {
29563 /* Detect if we have a unary selector. */
29564 if (maybe_unary_selector_p)
29565 {
29566 sel_seq = selector;
29567 goto finish_selector;
29568 }
29569 else
29570 {
29571 cp_parser_error (parser, "expected %<:%>");
29572 }
29573 }
29574 maybe_unary_selector_p = false;
29575 token = cp_lexer_consume_token (parser->lexer);
29576
29577 if (token->type == CPP_SCOPE)
29578 {
29579 sel_seq
29580 = chainon (sel_seq,
29581 build_tree_list (selector, NULL_TREE));
29582 sel_seq
29583 = chainon (sel_seq,
29584 build_tree_list (NULL_TREE, NULL_TREE));
29585 }
29586 else
29587 sel_seq
29588 = chainon (sel_seq,
29589 build_tree_list (selector, NULL_TREE));
29590
29591 token = cp_lexer_peek_token (parser->lexer);
29592 }
29593
29594 finish_selector:
29595 parens.require_close (parser);
29596
29597
29598 /* Build a location of the form:
29599 @selector(func)
29600 ^~~~~~~~~~~~~~~
29601 with caret==start at the @ token, finishing at the close paren. */
29602 location_t combined_loc
29603 = make_location (loc, loc,
29604 cp_lexer_previous_token (parser->lexer)->location);
29605 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29606 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29607 protected_set_expr_location (result, combined_loc);
29608 return result;
29609 }
29610
29611 /* Parse a list of identifiers.
29612
29613 objc-identifier-list:
29614 identifier
29615 objc-identifier-list , identifier
29616
29617 Returns a TREE_LIST of identifier nodes. */
29618
29619 static tree
29620 cp_parser_objc_identifier_list (cp_parser* parser)
29621 {
29622 tree identifier;
29623 tree list;
29624 cp_token *sep;
29625
29626 identifier = cp_parser_identifier (parser);
29627 if (identifier == error_mark_node)
29628 return error_mark_node;
29629
29630 list = build_tree_list (NULL_TREE, identifier);
29631 sep = cp_lexer_peek_token (parser->lexer);
29632
29633 while (sep->type == CPP_COMMA)
29634 {
29635 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29636 identifier = cp_parser_identifier (parser);
29637 if (identifier == error_mark_node)
29638 return list;
29639
29640 list = chainon (list, build_tree_list (NULL_TREE,
29641 identifier));
29642 sep = cp_lexer_peek_token (parser->lexer);
29643 }
29644
29645 return list;
29646 }
29647
29648 /* Parse an Objective-C alias declaration.
29649
29650 objc-alias-declaration:
29651 @compatibility_alias identifier identifier ;
29652
29653 This function registers the alias mapping with the Objective-C front end.
29654 It returns nothing. */
29655
29656 static void
29657 cp_parser_objc_alias_declaration (cp_parser* parser)
29658 {
29659 tree alias, orig;
29660
29661 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29662 alias = cp_parser_identifier (parser);
29663 orig = cp_parser_identifier (parser);
29664 objc_declare_alias (alias, orig);
29665 cp_parser_consume_semicolon_at_end_of_statement (parser);
29666 }
29667
29668 /* Parse an Objective-C class forward-declaration.
29669
29670 objc-class-declaration:
29671 @class objc-identifier-list ;
29672
29673 The function registers the forward declarations with the Objective-C
29674 front end. It returns nothing. */
29675
29676 static void
29677 cp_parser_objc_class_declaration (cp_parser* parser)
29678 {
29679 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29680 while (true)
29681 {
29682 tree id;
29683
29684 id = cp_parser_identifier (parser);
29685 if (id == error_mark_node)
29686 break;
29687
29688 objc_declare_class (id);
29689
29690 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29691 cp_lexer_consume_token (parser->lexer);
29692 else
29693 break;
29694 }
29695 cp_parser_consume_semicolon_at_end_of_statement (parser);
29696 }
29697
29698 /* Parse a list of Objective-C protocol references.
29699
29700 objc-protocol-refs-opt:
29701 objc-protocol-refs [opt]
29702
29703 objc-protocol-refs:
29704 < objc-identifier-list >
29705
29706 Returns a TREE_LIST of identifiers, if any. */
29707
29708 static tree
29709 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29710 {
29711 tree protorefs = NULL_TREE;
29712
29713 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29714 {
29715 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29716 protorefs = cp_parser_objc_identifier_list (parser);
29717 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29718 }
29719
29720 return protorefs;
29721 }
29722
29723 /* Parse a Objective-C visibility specification. */
29724
29725 static void
29726 cp_parser_objc_visibility_spec (cp_parser* parser)
29727 {
29728 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29729
29730 switch (vis->keyword)
29731 {
29732 case RID_AT_PRIVATE:
29733 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29734 break;
29735 case RID_AT_PROTECTED:
29736 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29737 break;
29738 case RID_AT_PUBLIC:
29739 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29740 break;
29741 case RID_AT_PACKAGE:
29742 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29743 break;
29744 default:
29745 return;
29746 }
29747
29748 /* Eat '@private'/'@protected'/'@public'. */
29749 cp_lexer_consume_token (parser->lexer);
29750 }
29751
29752 /* Parse an Objective-C method type. Return 'true' if it is a class
29753 (+) method, and 'false' if it is an instance (-) method. */
29754
29755 static inline bool
29756 cp_parser_objc_method_type (cp_parser* parser)
29757 {
29758 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29759 return true;
29760 else
29761 return false;
29762 }
29763
29764 /* Parse an Objective-C protocol qualifier. */
29765
29766 static tree
29767 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29768 {
29769 tree quals = NULL_TREE, node;
29770 cp_token *token = cp_lexer_peek_token (parser->lexer);
29771
29772 node = token->u.value;
29773
29774 while (node && identifier_p (node)
29775 && (node == ridpointers [(int) RID_IN]
29776 || node == ridpointers [(int) RID_OUT]
29777 || node == ridpointers [(int) RID_INOUT]
29778 || node == ridpointers [(int) RID_BYCOPY]
29779 || node == ridpointers [(int) RID_BYREF]
29780 || node == ridpointers [(int) RID_ONEWAY]))
29781 {
29782 quals = tree_cons (NULL_TREE, node, quals);
29783 cp_lexer_consume_token (parser->lexer);
29784 token = cp_lexer_peek_token (parser->lexer);
29785 node = token->u.value;
29786 }
29787
29788 return quals;
29789 }
29790
29791 /* Parse an Objective-C typename. */
29792
29793 static tree
29794 cp_parser_objc_typename (cp_parser* parser)
29795 {
29796 tree type_name = NULL_TREE;
29797
29798 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29799 {
29800 tree proto_quals, cp_type = NULL_TREE;
29801
29802 matching_parens parens;
29803 parens.consume_open (parser); /* Eat '('. */
29804 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29805
29806 /* An ObjC type name may consist of just protocol qualifiers, in which
29807 case the type shall default to 'id'. */
29808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29809 {
29810 cp_type = cp_parser_type_id (parser);
29811
29812 /* If the type could not be parsed, an error has already
29813 been produced. For error recovery, behave as if it had
29814 not been specified, which will use the default type
29815 'id'. */
29816 if (cp_type == error_mark_node)
29817 {
29818 cp_type = NULL_TREE;
29819 /* We need to skip to the closing parenthesis as
29820 cp_parser_type_id() does not seem to do it for
29821 us. */
29822 cp_parser_skip_to_closing_parenthesis (parser,
29823 /*recovering=*/true,
29824 /*or_comma=*/false,
29825 /*consume_paren=*/false);
29826 }
29827 }
29828
29829 parens.require_close (parser);
29830 type_name = build_tree_list (proto_quals, cp_type);
29831 }
29832
29833 return type_name;
29834 }
29835
29836 /* Check to see if TYPE refers to an Objective-C selector name. */
29837
29838 static bool
29839 cp_parser_objc_selector_p (enum cpp_ttype type)
29840 {
29841 return (type == CPP_NAME || type == CPP_KEYWORD
29842 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29843 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29844 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29845 || type == CPP_XOR || type == CPP_XOR_EQ);
29846 }
29847
29848 /* Parse an Objective-C selector. */
29849
29850 static tree
29851 cp_parser_objc_selector (cp_parser* parser)
29852 {
29853 cp_token *token = cp_lexer_consume_token (parser->lexer);
29854
29855 if (!cp_parser_objc_selector_p (token->type))
29856 {
29857 error_at (token->location, "invalid Objective-C++ selector name");
29858 return error_mark_node;
29859 }
29860
29861 /* C++ operator names are allowed to appear in ObjC selectors. */
29862 switch (token->type)
29863 {
29864 case CPP_AND_AND: return get_identifier ("and");
29865 case CPP_AND_EQ: return get_identifier ("and_eq");
29866 case CPP_AND: return get_identifier ("bitand");
29867 case CPP_OR: return get_identifier ("bitor");
29868 case CPP_COMPL: return get_identifier ("compl");
29869 case CPP_NOT: return get_identifier ("not");
29870 case CPP_NOT_EQ: return get_identifier ("not_eq");
29871 case CPP_OR_OR: return get_identifier ("or");
29872 case CPP_OR_EQ: return get_identifier ("or_eq");
29873 case CPP_XOR: return get_identifier ("xor");
29874 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29875 default: return token->u.value;
29876 }
29877 }
29878
29879 /* Parse an Objective-C params list. */
29880
29881 static tree
29882 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29883 {
29884 tree params = NULL_TREE;
29885 bool maybe_unary_selector_p = true;
29886 cp_token *token = cp_lexer_peek_token (parser->lexer);
29887
29888 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29889 {
29890 tree selector = NULL_TREE, type_name, identifier;
29891 tree parm_attr = NULL_TREE;
29892
29893 if (token->keyword == RID_ATTRIBUTE)
29894 break;
29895
29896 if (token->type != CPP_COLON)
29897 selector = cp_parser_objc_selector (parser);
29898
29899 /* Detect if we have a unary selector. */
29900 if (maybe_unary_selector_p
29901 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29902 {
29903 params = selector; /* Might be followed by attributes. */
29904 break;
29905 }
29906
29907 maybe_unary_selector_p = false;
29908 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29909 {
29910 /* Something went quite wrong. There should be a colon
29911 here, but there is not. Stop parsing parameters. */
29912 break;
29913 }
29914 type_name = cp_parser_objc_typename (parser);
29915 /* New ObjC allows attributes on parameters too. */
29916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29917 parm_attr = cp_parser_attributes_opt (parser);
29918 identifier = cp_parser_identifier (parser);
29919
29920 params
29921 = chainon (params,
29922 objc_build_keyword_decl (selector,
29923 type_name,
29924 identifier,
29925 parm_attr));
29926
29927 token = cp_lexer_peek_token (parser->lexer);
29928 }
29929
29930 if (params == NULL_TREE)
29931 {
29932 cp_parser_error (parser, "objective-c++ method declaration is expected");
29933 return error_mark_node;
29934 }
29935
29936 /* We allow tail attributes for the method. */
29937 if (token->keyword == RID_ATTRIBUTE)
29938 {
29939 *attributes = cp_parser_attributes_opt (parser);
29940 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29941 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29942 return params;
29943 cp_parser_error (parser,
29944 "method attributes must be specified at the end");
29945 return error_mark_node;
29946 }
29947
29948 if (params == NULL_TREE)
29949 {
29950 cp_parser_error (parser, "objective-c++ method declaration is expected");
29951 return error_mark_node;
29952 }
29953 return params;
29954 }
29955
29956 /* Parse the non-keyword Objective-C params. */
29957
29958 static tree
29959 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29960 tree* attributes)
29961 {
29962 tree params = make_node (TREE_LIST);
29963 cp_token *token = cp_lexer_peek_token (parser->lexer);
29964 *ellipsisp = false; /* Initially, assume no ellipsis. */
29965
29966 while (token->type == CPP_COMMA)
29967 {
29968 cp_parameter_declarator *parmdecl;
29969 tree parm;
29970
29971 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29972 token = cp_lexer_peek_token (parser->lexer);
29973
29974 if (token->type == CPP_ELLIPSIS)
29975 {
29976 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29977 *ellipsisp = true;
29978 token = cp_lexer_peek_token (parser->lexer);
29979 break;
29980 }
29981
29982 /* TODO: parse attributes for tail parameters. */
29983 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29984 parm = grokdeclarator (parmdecl->declarator,
29985 &parmdecl->decl_specifiers,
29986 PARM, /*initialized=*/0,
29987 /*attrlist=*/NULL);
29988
29989 chainon (params, build_tree_list (NULL_TREE, parm));
29990 token = cp_lexer_peek_token (parser->lexer);
29991 }
29992
29993 /* We allow tail attributes for the method. */
29994 if (token->keyword == RID_ATTRIBUTE)
29995 {
29996 if (*attributes == NULL_TREE)
29997 {
29998 *attributes = cp_parser_attributes_opt (parser);
29999 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30000 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30001 return params;
30002 }
30003 else
30004 /* We have an error, but parse the attributes, so that we can
30005 carry on. */
30006 *attributes = cp_parser_attributes_opt (parser);
30007
30008 cp_parser_error (parser,
30009 "method attributes must be specified at the end");
30010 return error_mark_node;
30011 }
30012
30013 return params;
30014 }
30015
30016 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30017
30018 static void
30019 cp_parser_objc_interstitial_code (cp_parser* parser)
30020 {
30021 cp_token *token = cp_lexer_peek_token (parser->lexer);
30022
30023 /* If the next token is `extern' and the following token is a string
30024 literal, then we have a linkage specification. */
30025 if (token->keyword == RID_EXTERN
30026 && cp_parser_is_pure_string_literal
30027 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30028 cp_parser_linkage_specification (parser);
30029 /* Handle #pragma, if any. */
30030 else if (token->type == CPP_PRAGMA)
30031 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30032 /* Allow stray semicolons. */
30033 else if (token->type == CPP_SEMICOLON)
30034 cp_lexer_consume_token (parser->lexer);
30035 /* Mark methods as optional or required, when building protocols. */
30036 else if (token->keyword == RID_AT_OPTIONAL)
30037 {
30038 cp_lexer_consume_token (parser->lexer);
30039 objc_set_method_opt (true);
30040 }
30041 else if (token->keyword == RID_AT_REQUIRED)
30042 {
30043 cp_lexer_consume_token (parser->lexer);
30044 objc_set_method_opt (false);
30045 }
30046 else if (token->keyword == RID_NAMESPACE)
30047 cp_parser_namespace_definition (parser);
30048 /* Other stray characters must generate errors. */
30049 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30050 {
30051 cp_lexer_consume_token (parser->lexer);
30052 error ("stray %qs between Objective-C++ methods",
30053 token->type == CPP_OPEN_BRACE ? "{" : "}");
30054 }
30055 /* Finally, try to parse a block-declaration, or a function-definition. */
30056 else
30057 cp_parser_block_declaration (parser, /*statement_p=*/false);
30058 }
30059
30060 /* Parse a method signature. */
30061
30062 static tree
30063 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30064 {
30065 tree rettype, kwdparms, optparms;
30066 bool ellipsis = false;
30067 bool is_class_method;
30068
30069 is_class_method = cp_parser_objc_method_type (parser);
30070 rettype = cp_parser_objc_typename (parser);
30071 *attributes = NULL_TREE;
30072 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30073 if (kwdparms == error_mark_node)
30074 return error_mark_node;
30075 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30076 if (optparms == error_mark_node)
30077 return error_mark_node;
30078
30079 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30080 }
30081
30082 static bool
30083 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30084 {
30085 tree tattr;
30086 cp_lexer_save_tokens (parser->lexer);
30087 tattr = cp_parser_attributes_opt (parser);
30088 gcc_assert (tattr) ;
30089
30090 /* If the attributes are followed by a method introducer, this is not allowed.
30091 Dump the attributes and flag the situation. */
30092 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30093 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30094 return true;
30095
30096 /* Otherwise, the attributes introduce some interstitial code, possibly so
30097 rewind to allow that check. */
30098 cp_lexer_rollback_tokens (parser->lexer);
30099 return false;
30100 }
30101
30102 /* Parse an Objective-C method prototype list. */
30103
30104 static void
30105 cp_parser_objc_method_prototype_list (cp_parser* parser)
30106 {
30107 cp_token *token = cp_lexer_peek_token (parser->lexer);
30108
30109 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30110 {
30111 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30112 {
30113 tree attributes, sig;
30114 bool is_class_method;
30115 if (token->type == CPP_PLUS)
30116 is_class_method = true;
30117 else
30118 is_class_method = false;
30119 sig = cp_parser_objc_method_signature (parser, &attributes);
30120 if (sig == error_mark_node)
30121 {
30122 cp_parser_skip_to_end_of_block_or_statement (parser);
30123 token = cp_lexer_peek_token (parser->lexer);
30124 continue;
30125 }
30126 objc_add_method_declaration (is_class_method, sig, attributes);
30127 cp_parser_consume_semicolon_at_end_of_statement (parser);
30128 }
30129 else if (token->keyword == RID_AT_PROPERTY)
30130 cp_parser_objc_at_property_declaration (parser);
30131 else if (token->keyword == RID_ATTRIBUTE
30132 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30133 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30134 OPT_Wattributes,
30135 "prefix attributes are ignored for methods");
30136 else
30137 /* Allow for interspersed non-ObjC++ code. */
30138 cp_parser_objc_interstitial_code (parser);
30139
30140 token = cp_lexer_peek_token (parser->lexer);
30141 }
30142
30143 if (token->type != CPP_EOF)
30144 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30145 else
30146 cp_parser_error (parser, "expected %<@end%>");
30147
30148 objc_finish_interface ();
30149 }
30150
30151 /* Parse an Objective-C method definition list. */
30152
30153 static void
30154 cp_parser_objc_method_definition_list (cp_parser* parser)
30155 {
30156 cp_token *token = cp_lexer_peek_token (parser->lexer);
30157
30158 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30159 {
30160 tree meth;
30161
30162 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30163 {
30164 cp_token *ptk;
30165 tree sig, attribute;
30166 bool is_class_method;
30167 if (token->type == CPP_PLUS)
30168 is_class_method = true;
30169 else
30170 is_class_method = false;
30171 push_deferring_access_checks (dk_deferred);
30172 sig = cp_parser_objc_method_signature (parser, &attribute);
30173 if (sig == error_mark_node)
30174 {
30175 cp_parser_skip_to_end_of_block_or_statement (parser);
30176 token = cp_lexer_peek_token (parser->lexer);
30177 continue;
30178 }
30179 objc_start_method_definition (is_class_method, sig, attribute,
30180 NULL_TREE);
30181
30182 /* For historical reasons, we accept an optional semicolon. */
30183 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30184 cp_lexer_consume_token (parser->lexer);
30185
30186 ptk = cp_lexer_peek_token (parser->lexer);
30187 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30188 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30189 {
30190 perform_deferred_access_checks (tf_warning_or_error);
30191 stop_deferring_access_checks ();
30192 meth = cp_parser_function_definition_after_declarator (parser,
30193 false);
30194 pop_deferring_access_checks ();
30195 objc_finish_method_definition (meth);
30196 }
30197 }
30198 /* The following case will be removed once @synthesize is
30199 completely implemented. */
30200 else if (token->keyword == RID_AT_PROPERTY)
30201 cp_parser_objc_at_property_declaration (parser);
30202 else if (token->keyword == RID_AT_SYNTHESIZE)
30203 cp_parser_objc_at_synthesize_declaration (parser);
30204 else if (token->keyword == RID_AT_DYNAMIC)
30205 cp_parser_objc_at_dynamic_declaration (parser);
30206 else if (token->keyword == RID_ATTRIBUTE
30207 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30208 warning_at (token->location, OPT_Wattributes,
30209 "prefix attributes are ignored for methods");
30210 else
30211 /* Allow for interspersed non-ObjC++ code. */
30212 cp_parser_objc_interstitial_code (parser);
30213
30214 token = cp_lexer_peek_token (parser->lexer);
30215 }
30216
30217 if (token->type != CPP_EOF)
30218 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30219 else
30220 cp_parser_error (parser, "expected %<@end%>");
30221
30222 objc_finish_implementation ();
30223 }
30224
30225 /* Parse Objective-C ivars. */
30226
30227 static void
30228 cp_parser_objc_class_ivars (cp_parser* parser)
30229 {
30230 cp_token *token = cp_lexer_peek_token (parser->lexer);
30231
30232 if (token->type != CPP_OPEN_BRACE)
30233 return; /* No ivars specified. */
30234
30235 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30236 token = cp_lexer_peek_token (parser->lexer);
30237
30238 while (token->type != CPP_CLOSE_BRACE
30239 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30240 {
30241 cp_decl_specifier_seq declspecs;
30242 int decl_class_or_enum_p;
30243 tree prefix_attributes;
30244
30245 cp_parser_objc_visibility_spec (parser);
30246
30247 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30248 break;
30249
30250 cp_parser_decl_specifier_seq (parser,
30251 CP_PARSER_FLAGS_OPTIONAL,
30252 &declspecs,
30253 &decl_class_or_enum_p);
30254
30255 /* auto, register, static, extern, mutable. */
30256 if (declspecs.storage_class != sc_none)
30257 {
30258 cp_parser_error (parser, "invalid type for instance variable");
30259 declspecs.storage_class = sc_none;
30260 }
30261
30262 /* thread_local. */
30263 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30264 {
30265 cp_parser_error (parser, "invalid type for instance variable");
30266 declspecs.locations[ds_thread] = 0;
30267 }
30268
30269 /* typedef. */
30270 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30271 {
30272 cp_parser_error (parser, "invalid type for instance variable");
30273 declspecs.locations[ds_typedef] = 0;
30274 }
30275
30276 prefix_attributes = declspecs.attributes;
30277 declspecs.attributes = NULL_TREE;
30278
30279 /* Keep going until we hit the `;' at the end of the
30280 declaration. */
30281 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30282 {
30283 tree width = NULL_TREE, attributes, first_attribute, decl;
30284 cp_declarator *declarator = NULL;
30285 int ctor_dtor_or_conv_p;
30286
30287 /* Check for a (possibly unnamed) bitfield declaration. */
30288 token = cp_lexer_peek_token (parser->lexer);
30289 if (token->type == CPP_COLON)
30290 goto eat_colon;
30291
30292 if (token->type == CPP_NAME
30293 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30294 == CPP_COLON))
30295 {
30296 /* Get the name of the bitfield. */
30297 declarator = make_id_declarator (NULL_TREE,
30298 cp_parser_identifier (parser),
30299 sfk_none);
30300
30301 eat_colon:
30302 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30303 /* Get the width of the bitfield. */
30304 width
30305 = cp_parser_constant_expression (parser);
30306 }
30307 else
30308 {
30309 /* Parse the declarator. */
30310 declarator
30311 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30312 &ctor_dtor_or_conv_p,
30313 /*parenthesized_p=*/NULL,
30314 /*member_p=*/false,
30315 /*friend_p=*/false);
30316 }
30317
30318 /* Look for attributes that apply to the ivar. */
30319 attributes = cp_parser_attributes_opt (parser);
30320 /* Remember which attributes are prefix attributes and
30321 which are not. */
30322 first_attribute = attributes;
30323 /* Combine the attributes. */
30324 attributes = chainon (prefix_attributes, attributes);
30325
30326 if (width)
30327 /* Create the bitfield declaration. */
30328 decl = grokbitfield (declarator, &declspecs,
30329 width, NULL_TREE, attributes);
30330 else
30331 decl = grokfield (declarator, &declspecs,
30332 NULL_TREE, /*init_const_expr_p=*/false,
30333 NULL_TREE, attributes);
30334
30335 /* Add the instance variable. */
30336 if (decl != error_mark_node && decl != NULL_TREE)
30337 objc_add_instance_variable (decl);
30338
30339 /* Reset PREFIX_ATTRIBUTES. */
30340 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30341 attributes = TREE_CHAIN (attributes);
30342 if (attributes)
30343 TREE_CHAIN (attributes) = NULL_TREE;
30344
30345 token = cp_lexer_peek_token (parser->lexer);
30346
30347 if (token->type == CPP_COMMA)
30348 {
30349 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30350 continue;
30351 }
30352 break;
30353 }
30354
30355 cp_parser_consume_semicolon_at_end_of_statement (parser);
30356 token = cp_lexer_peek_token (parser->lexer);
30357 }
30358
30359 if (token->keyword == RID_AT_END)
30360 cp_parser_error (parser, "expected %<}%>");
30361
30362 /* Do not consume the RID_AT_END, so it will be read again as terminating
30363 the @interface of @implementation. */
30364 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30365 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30366
30367 /* For historical reasons, we accept an optional semicolon. */
30368 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30369 cp_lexer_consume_token (parser->lexer);
30370 }
30371
30372 /* Parse an Objective-C protocol declaration. */
30373
30374 static void
30375 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30376 {
30377 tree proto, protorefs;
30378 cp_token *tok;
30379
30380 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30382 {
30383 tok = cp_lexer_peek_token (parser->lexer);
30384 error_at (tok->location, "identifier expected after %<@protocol%>");
30385 cp_parser_consume_semicolon_at_end_of_statement (parser);
30386 return;
30387 }
30388
30389 /* See if we have a forward declaration or a definition. */
30390 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30391
30392 /* Try a forward declaration first. */
30393 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30394 {
30395 while (true)
30396 {
30397 tree id;
30398
30399 id = cp_parser_identifier (parser);
30400 if (id == error_mark_node)
30401 break;
30402
30403 objc_declare_protocol (id, attributes);
30404
30405 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30406 cp_lexer_consume_token (parser->lexer);
30407 else
30408 break;
30409 }
30410 cp_parser_consume_semicolon_at_end_of_statement (parser);
30411 }
30412
30413 /* Ok, we got a full-fledged definition (or at least should). */
30414 else
30415 {
30416 proto = cp_parser_identifier (parser);
30417 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30418 objc_start_protocol (proto, protorefs, attributes);
30419 cp_parser_objc_method_prototype_list (parser);
30420 }
30421 }
30422
30423 /* Parse an Objective-C superclass or category. */
30424
30425 static void
30426 cp_parser_objc_superclass_or_category (cp_parser *parser,
30427 bool iface_p,
30428 tree *super,
30429 tree *categ, bool *is_class_extension)
30430 {
30431 cp_token *next = cp_lexer_peek_token (parser->lexer);
30432
30433 *super = *categ = NULL_TREE;
30434 *is_class_extension = false;
30435 if (next->type == CPP_COLON)
30436 {
30437 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30438 *super = cp_parser_identifier (parser);
30439 }
30440 else if (next->type == CPP_OPEN_PAREN)
30441 {
30442 matching_parens parens;
30443 parens.consume_open (parser); /* Eat '('. */
30444
30445 /* If there is no category name, and this is an @interface, we
30446 have a class extension. */
30447 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30448 {
30449 *categ = NULL_TREE;
30450 *is_class_extension = true;
30451 }
30452 else
30453 *categ = cp_parser_identifier (parser);
30454
30455 parens.require_close (parser);
30456 }
30457 }
30458
30459 /* Parse an Objective-C class interface. */
30460
30461 static void
30462 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30463 {
30464 tree name, super, categ, protos;
30465 bool is_class_extension;
30466
30467 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30468 name = cp_parser_identifier (parser);
30469 if (name == error_mark_node)
30470 {
30471 /* It's hard to recover because even if valid @interface stuff
30472 is to follow, we can't compile it (or validate it) if we
30473 don't even know which class it refers to. Let's assume this
30474 was a stray '@interface' token in the stream and skip it.
30475 */
30476 return;
30477 }
30478 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30479 &is_class_extension);
30480 protos = cp_parser_objc_protocol_refs_opt (parser);
30481
30482 /* We have either a class or a category on our hands. */
30483 if (categ || is_class_extension)
30484 objc_start_category_interface (name, categ, protos, attributes);
30485 else
30486 {
30487 objc_start_class_interface (name, super, protos, attributes);
30488 /* Handle instance variable declarations, if any. */
30489 cp_parser_objc_class_ivars (parser);
30490 objc_continue_interface ();
30491 }
30492
30493 cp_parser_objc_method_prototype_list (parser);
30494 }
30495
30496 /* Parse an Objective-C class implementation. */
30497
30498 static void
30499 cp_parser_objc_class_implementation (cp_parser* parser)
30500 {
30501 tree name, super, categ;
30502 bool is_class_extension;
30503
30504 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30505 name = cp_parser_identifier (parser);
30506 if (name == error_mark_node)
30507 {
30508 /* It's hard to recover because even if valid @implementation
30509 stuff is to follow, we can't compile it (or validate it) if
30510 we don't even know which class it refers to. Let's assume
30511 this was a stray '@implementation' token in the stream and
30512 skip it.
30513 */
30514 return;
30515 }
30516 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30517 &is_class_extension);
30518
30519 /* We have either a class or a category on our hands. */
30520 if (categ)
30521 objc_start_category_implementation (name, categ);
30522 else
30523 {
30524 objc_start_class_implementation (name, super);
30525 /* Handle instance variable declarations, if any. */
30526 cp_parser_objc_class_ivars (parser);
30527 objc_continue_implementation ();
30528 }
30529
30530 cp_parser_objc_method_definition_list (parser);
30531 }
30532
30533 /* Consume the @end token and finish off the implementation. */
30534
30535 static void
30536 cp_parser_objc_end_implementation (cp_parser* parser)
30537 {
30538 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30539 objc_finish_implementation ();
30540 }
30541
30542 /* Parse an Objective-C declaration. */
30543
30544 static void
30545 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30546 {
30547 /* Try to figure out what kind of declaration is present. */
30548 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30549
30550 if (attributes)
30551 switch (kwd->keyword)
30552 {
30553 case RID_AT_ALIAS:
30554 case RID_AT_CLASS:
30555 case RID_AT_END:
30556 error_at (kwd->location, "attributes may not be specified before"
30557 " the %<@%D%> Objective-C++ keyword",
30558 kwd->u.value);
30559 attributes = NULL;
30560 break;
30561 case RID_AT_IMPLEMENTATION:
30562 warning_at (kwd->location, OPT_Wattributes,
30563 "prefix attributes are ignored before %<@%D%>",
30564 kwd->u.value);
30565 attributes = NULL;
30566 default:
30567 break;
30568 }
30569
30570 switch (kwd->keyword)
30571 {
30572 case RID_AT_ALIAS:
30573 cp_parser_objc_alias_declaration (parser);
30574 break;
30575 case RID_AT_CLASS:
30576 cp_parser_objc_class_declaration (parser);
30577 break;
30578 case RID_AT_PROTOCOL:
30579 cp_parser_objc_protocol_declaration (parser, attributes);
30580 break;
30581 case RID_AT_INTERFACE:
30582 cp_parser_objc_class_interface (parser, attributes);
30583 break;
30584 case RID_AT_IMPLEMENTATION:
30585 cp_parser_objc_class_implementation (parser);
30586 break;
30587 case RID_AT_END:
30588 cp_parser_objc_end_implementation (parser);
30589 break;
30590 default:
30591 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30592 kwd->u.value);
30593 cp_parser_skip_to_end_of_block_or_statement (parser);
30594 }
30595 }
30596
30597 /* Parse an Objective-C try-catch-finally statement.
30598
30599 objc-try-catch-finally-stmt:
30600 @try compound-statement objc-catch-clause-seq [opt]
30601 objc-finally-clause [opt]
30602
30603 objc-catch-clause-seq:
30604 objc-catch-clause objc-catch-clause-seq [opt]
30605
30606 objc-catch-clause:
30607 @catch ( objc-exception-declaration ) compound-statement
30608
30609 objc-finally-clause:
30610 @finally compound-statement
30611
30612 objc-exception-declaration:
30613 parameter-declaration
30614 '...'
30615
30616 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30617
30618 Returns NULL_TREE.
30619
30620 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30621 for C. Keep them in sync. */
30622
30623 static tree
30624 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30625 {
30626 location_t location;
30627 tree stmt;
30628
30629 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30630 location = cp_lexer_peek_token (parser->lexer)->location;
30631 objc_maybe_warn_exceptions (location);
30632 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30633 node, lest it get absorbed into the surrounding block. */
30634 stmt = push_stmt_list ();
30635 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30636 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30637
30638 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30639 {
30640 cp_parameter_declarator *parm;
30641 tree parameter_declaration = error_mark_node;
30642 bool seen_open_paren = false;
30643 matching_parens parens;
30644
30645 cp_lexer_consume_token (parser->lexer);
30646 if (parens.require_open (parser))
30647 seen_open_paren = true;
30648 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30649 {
30650 /* We have "@catch (...)" (where the '...' are literally
30651 what is in the code). Skip the '...'.
30652 parameter_declaration is set to NULL_TREE, and
30653 objc_being_catch_clauses() knows that that means
30654 '...'. */
30655 cp_lexer_consume_token (parser->lexer);
30656 parameter_declaration = NULL_TREE;
30657 }
30658 else
30659 {
30660 /* We have "@catch (NSException *exception)" or something
30661 like that. Parse the parameter declaration. */
30662 parm = cp_parser_parameter_declaration (parser, false, NULL);
30663 if (parm == NULL)
30664 parameter_declaration = error_mark_node;
30665 else
30666 parameter_declaration = grokdeclarator (parm->declarator,
30667 &parm->decl_specifiers,
30668 PARM, /*initialized=*/0,
30669 /*attrlist=*/NULL);
30670 }
30671 if (seen_open_paren)
30672 parens.require_close (parser);
30673 else
30674 {
30675 /* If there was no open parenthesis, we are recovering from
30676 an error, and we are trying to figure out what mistake
30677 the user has made. */
30678
30679 /* If there is an immediate closing parenthesis, the user
30680 probably forgot the opening one (ie, they typed "@catch
30681 NSException *e)". Parse the closing parenthesis and keep
30682 going. */
30683 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30684 cp_lexer_consume_token (parser->lexer);
30685
30686 /* If these is no immediate closing parenthesis, the user
30687 probably doesn't know that parenthesis are required at
30688 all (ie, they typed "@catch NSException *e"). So, just
30689 forget about the closing parenthesis and keep going. */
30690 }
30691 objc_begin_catch_clause (parameter_declaration);
30692 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30693 objc_finish_catch_clause ();
30694 }
30695 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30696 {
30697 cp_lexer_consume_token (parser->lexer);
30698 location = cp_lexer_peek_token (parser->lexer)->location;
30699 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30700 node, lest it get absorbed into the surrounding block. */
30701 stmt = push_stmt_list ();
30702 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30703 objc_build_finally_clause (location, pop_stmt_list (stmt));
30704 }
30705
30706 return objc_finish_try_stmt ();
30707 }
30708
30709 /* Parse an Objective-C synchronized statement.
30710
30711 objc-synchronized-stmt:
30712 @synchronized ( expression ) compound-statement
30713
30714 Returns NULL_TREE. */
30715
30716 static tree
30717 cp_parser_objc_synchronized_statement (cp_parser *parser)
30718 {
30719 location_t location;
30720 tree lock, stmt;
30721
30722 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30723
30724 location = cp_lexer_peek_token (parser->lexer)->location;
30725 objc_maybe_warn_exceptions (location);
30726 matching_parens parens;
30727 parens.require_open (parser);
30728 lock = cp_parser_expression (parser);
30729 parens.require_close (parser);
30730
30731 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30732 node, lest it get absorbed into the surrounding block. */
30733 stmt = push_stmt_list ();
30734 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30735
30736 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30737 }
30738
30739 /* Parse an Objective-C throw statement.
30740
30741 objc-throw-stmt:
30742 @throw assignment-expression [opt] ;
30743
30744 Returns a constructed '@throw' statement. */
30745
30746 static tree
30747 cp_parser_objc_throw_statement (cp_parser *parser)
30748 {
30749 tree expr = NULL_TREE;
30750 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30751
30752 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30753
30754 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30755 expr = cp_parser_expression (parser);
30756
30757 cp_parser_consume_semicolon_at_end_of_statement (parser);
30758
30759 return objc_build_throw_stmt (loc, expr);
30760 }
30761
30762 /* Parse an Objective-C statement. */
30763
30764 static tree
30765 cp_parser_objc_statement (cp_parser * parser)
30766 {
30767 /* Try to figure out what kind of declaration is present. */
30768 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30769
30770 switch (kwd->keyword)
30771 {
30772 case RID_AT_TRY:
30773 return cp_parser_objc_try_catch_finally_statement (parser);
30774 case RID_AT_SYNCHRONIZED:
30775 return cp_parser_objc_synchronized_statement (parser);
30776 case RID_AT_THROW:
30777 return cp_parser_objc_throw_statement (parser);
30778 default:
30779 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30780 kwd->u.value);
30781 cp_parser_skip_to_end_of_block_or_statement (parser);
30782 }
30783
30784 return error_mark_node;
30785 }
30786
30787 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30788 look ahead to see if an objc keyword follows the attributes. This
30789 is to detect the use of prefix attributes on ObjC @interface and
30790 @protocol. */
30791
30792 static bool
30793 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30794 {
30795 cp_lexer_save_tokens (parser->lexer);
30796 *attrib = cp_parser_attributes_opt (parser);
30797 gcc_assert (*attrib);
30798 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30799 {
30800 cp_lexer_commit_tokens (parser->lexer);
30801 return true;
30802 }
30803 cp_lexer_rollback_tokens (parser->lexer);
30804 return false;
30805 }
30806
30807 /* This routine is a minimal replacement for
30808 c_parser_struct_declaration () used when parsing the list of
30809 types/names or ObjC++ properties. For example, when parsing the
30810 code
30811
30812 @property (readonly) int a, b, c;
30813
30814 this function is responsible for parsing "int a, int b, int c" and
30815 returning the declarations as CHAIN of DECLs.
30816
30817 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30818 similar parsing. */
30819 static tree
30820 cp_parser_objc_struct_declaration (cp_parser *parser)
30821 {
30822 tree decls = NULL_TREE;
30823 cp_decl_specifier_seq declspecs;
30824 int decl_class_or_enum_p;
30825 tree prefix_attributes;
30826
30827 cp_parser_decl_specifier_seq (parser,
30828 CP_PARSER_FLAGS_NONE,
30829 &declspecs,
30830 &decl_class_or_enum_p);
30831
30832 if (declspecs.type == error_mark_node)
30833 return error_mark_node;
30834
30835 /* auto, register, static, extern, mutable. */
30836 if (declspecs.storage_class != sc_none)
30837 {
30838 cp_parser_error (parser, "invalid type for property");
30839 declspecs.storage_class = sc_none;
30840 }
30841
30842 /* thread_local. */
30843 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30844 {
30845 cp_parser_error (parser, "invalid type for property");
30846 declspecs.locations[ds_thread] = 0;
30847 }
30848
30849 /* typedef. */
30850 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30851 {
30852 cp_parser_error (parser, "invalid type for property");
30853 declspecs.locations[ds_typedef] = 0;
30854 }
30855
30856 prefix_attributes = declspecs.attributes;
30857 declspecs.attributes = NULL_TREE;
30858
30859 /* Keep going until we hit the `;' at the end of the declaration. */
30860 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30861 {
30862 tree attributes, first_attribute, decl;
30863 cp_declarator *declarator;
30864 cp_token *token;
30865
30866 /* Parse the declarator. */
30867 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30868 NULL, NULL, false, false);
30869
30870 /* Look for attributes that apply to the ivar. */
30871 attributes = cp_parser_attributes_opt (parser);
30872 /* Remember which attributes are prefix attributes and
30873 which are not. */
30874 first_attribute = attributes;
30875 /* Combine the attributes. */
30876 attributes = chainon (prefix_attributes, attributes);
30877
30878 decl = grokfield (declarator, &declspecs,
30879 NULL_TREE, /*init_const_expr_p=*/false,
30880 NULL_TREE, attributes);
30881
30882 if (decl == error_mark_node || decl == NULL_TREE)
30883 return error_mark_node;
30884
30885 /* Reset PREFIX_ATTRIBUTES. */
30886 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30887 attributes = TREE_CHAIN (attributes);
30888 if (attributes)
30889 TREE_CHAIN (attributes) = NULL_TREE;
30890
30891 DECL_CHAIN (decl) = decls;
30892 decls = decl;
30893
30894 token = cp_lexer_peek_token (parser->lexer);
30895 if (token->type == CPP_COMMA)
30896 {
30897 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30898 continue;
30899 }
30900 else
30901 break;
30902 }
30903 return decls;
30904 }
30905
30906 /* Parse an Objective-C @property declaration. The syntax is:
30907
30908 objc-property-declaration:
30909 '@property' objc-property-attributes[opt] struct-declaration ;
30910
30911 objc-property-attributes:
30912 '(' objc-property-attribute-list ')'
30913
30914 objc-property-attribute-list:
30915 objc-property-attribute
30916 objc-property-attribute-list, objc-property-attribute
30917
30918 objc-property-attribute
30919 'getter' = identifier
30920 'setter' = identifier
30921 'readonly'
30922 'readwrite'
30923 'assign'
30924 'retain'
30925 'copy'
30926 'nonatomic'
30927
30928 For example:
30929 @property NSString *name;
30930 @property (readonly) id object;
30931 @property (retain, nonatomic, getter=getTheName) id name;
30932 @property int a, b, c;
30933
30934 PS: This function is identical to
30935 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30936 static void
30937 cp_parser_objc_at_property_declaration (cp_parser *parser)
30938 {
30939 /* The following variables hold the attributes of the properties as
30940 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30941 seen. When we see an attribute, we set them to 'true' (if they
30942 are boolean properties) or to the identifier (if they have an
30943 argument, ie, for getter and setter). Note that here we only
30944 parse the list of attributes, check the syntax and accumulate the
30945 attributes that we find. objc_add_property_declaration() will
30946 then process the information. */
30947 bool property_assign = false;
30948 bool property_copy = false;
30949 tree property_getter_ident = NULL_TREE;
30950 bool property_nonatomic = false;
30951 bool property_readonly = false;
30952 bool property_readwrite = false;
30953 bool property_retain = false;
30954 tree property_setter_ident = NULL_TREE;
30955
30956 /* 'properties' is the list of properties that we read. Usually a
30957 single one, but maybe more (eg, in "@property int a, b, c;" there
30958 are three). */
30959 tree properties;
30960 location_t loc;
30961
30962 loc = cp_lexer_peek_token (parser->lexer)->location;
30963
30964 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30965
30966 /* Parse the optional attribute list... */
30967 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30968 {
30969 /* Eat the '('. */
30970 matching_parens parens;
30971 parens.consume_open (parser);
30972
30973 while (true)
30974 {
30975 bool syntax_error = false;
30976 cp_token *token = cp_lexer_peek_token (parser->lexer);
30977 enum rid keyword;
30978
30979 if (token->type != CPP_NAME)
30980 {
30981 cp_parser_error (parser, "expected identifier");
30982 break;
30983 }
30984 keyword = C_RID_CODE (token->u.value);
30985 cp_lexer_consume_token (parser->lexer);
30986 switch (keyword)
30987 {
30988 case RID_ASSIGN: property_assign = true; break;
30989 case RID_COPY: property_copy = true; break;
30990 case RID_NONATOMIC: property_nonatomic = true; break;
30991 case RID_READONLY: property_readonly = true; break;
30992 case RID_READWRITE: property_readwrite = true; break;
30993 case RID_RETAIN: property_retain = true; break;
30994
30995 case RID_GETTER:
30996 case RID_SETTER:
30997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30998 {
30999 if (keyword == RID_GETTER)
31000 cp_parser_error (parser,
31001 "missing %<=%> (after %<getter%> attribute)");
31002 else
31003 cp_parser_error (parser,
31004 "missing %<=%> (after %<setter%> attribute)");
31005 syntax_error = true;
31006 break;
31007 }
31008 cp_lexer_consume_token (parser->lexer); /* eat the = */
31009 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31010 {
31011 cp_parser_error (parser, "expected identifier");
31012 syntax_error = true;
31013 break;
31014 }
31015 if (keyword == RID_SETTER)
31016 {
31017 if (property_setter_ident != NULL_TREE)
31018 {
31019 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31020 cp_lexer_consume_token (parser->lexer);
31021 }
31022 else
31023 property_setter_ident = cp_parser_objc_selector (parser);
31024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31025 cp_parser_error (parser, "setter name must terminate with %<:%>");
31026 else
31027 cp_lexer_consume_token (parser->lexer);
31028 }
31029 else
31030 {
31031 if (property_getter_ident != NULL_TREE)
31032 {
31033 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31034 cp_lexer_consume_token (parser->lexer);
31035 }
31036 else
31037 property_getter_ident = cp_parser_objc_selector (parser);
31038 }
31039 break;
31040 default:
31041 cp_parser_error (parser, "unknown property attribute");
31042 syntax_error = true;
31043 break;
31044 }
31045
31046 if (syntax_error)
31047 break;
31048
31049 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31050 cp_lexer_consume_token (parser->lexer);
31051 else
31052 break;
31053 }
31054
31055 /* FIXME: "@property (setter, assign);" will generate a spurious
31056 "error: expected ‘)’ before ‘,’ token". This is because
31057 cp_parser_require, unlike the C counterpart, will produce an
31058 error even if we are in error recovery. */
31059 if (!parens.require_close (parser))
31060 {
31061 cp_parser_skip_to_closing_parenthesis (parser,
31062 /*recovering=*/true,
31063 /*or_comma=*/false,
31064 /*consume_paren=*/true);
31065 }
31066 }
31067
31068 /* ... and the property declaration(s). */
31069 properties = cp_parser_objc_struct_declaration (parser);
31070
31071 if (properties == error_mark_node)
31072 {
31073 cp_parser_skip_to_end_of_statement (parser);
31074 /* If the next token is now a `;', consume it. */
31075 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31076 cp_lexer_consume_token (parser->lexer);
31077 return;
31078 }
31079
31080 if (properties == NULL_TREE)
31081 cp_parser_error (parser, "expected identifier");
31082 else
31083 {
31084 /* Comma-separated properties are chained together in
31085 reverse order; add them one by one. */
31086 properties = nreverse (properties);
31087
31088 for (; properties; properties = TREE_CHAIN (properties))
31089 objc_add_property_declaration (loc, copy_node (properties),
31090 property_readonly, property_readwrite,
31091 property_assign, property_retain,
31092 property_copy, property_nonatomic,
31093 property_getter_ident, property_setter_ident);
31094 }
31095
31096 cp_parser_consume_semicolon_at_end_of_statement (parser);
31097 }
31098
31099 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31100
31101 objc-synthesize-declaration:
31102 @synthesize objc-synthesize-identifier-list ;
31103
31104 objc-synthesize-identifier-list:
31105 objc-synthesize-identifier
31106 objc-synthesize-identifier-list, objc-synthesize-identifier
31107
31108 objc-synthesize-identifier
31109 identifier
31110 identifier = identifier
31111
31112 For example:
31113 @synthesize MyProperty;
31114 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31115
31116 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31117 for C. Keep them in sync.
31118 */
31119 static void
31120 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31121 {
31122 tree list = NULL_TREE;
31123 location_t loc;
31124 loc = cp_lexer_peek_token (parser->lexer)->location;
31125
31126 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31127 while (true)
31128 {
31129 tree property, ivar;
31130 property = cp_parser_identifier (parser);
31131 if (property == error_mark_node)
31132 {
31133 cp_parser_consume_semicolon_at_end_of_statement (parser);
31134 return;
31135 }
31136 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31137 {
31138 cp_lexer_consume_token (parser->lexer);
31139 ivar = cp_parser_identifier (parser);
31140 if (ivar == error_mark_node)
31141 {
31142 cp_parser_consume_semicolon_at_end_of_statement (parser);
31143 return;
31144 }
31145 }
31146 else
31147 ivar = NULL_TREE;
31148 list = chainon (list, build_tree_list (ivar, property));
31149 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31150 cp_lexer_consume_token (parser->lexer);
31151 else
31152 break;
31153 }
31154 cp_parser_consume_semicolon_at_end_of_statement (parser);
31155 objc_add_synthesize_declaration (loc, list);
31156 }
31157
31158 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31159
31160 objc-dynamic-declaration:
31161 @dynamic identifier-list ;
31162
31163 For example:
31164 @dynamic MyProperty;
31165 @dynamic MyProperty, AnotherProperty;
31166
31167 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31168 for C. Keep them in sync.
31169 */
31170 static void
31171 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31172 {
31173 tree list = NULL_TREE;
31174 location_t loc;
31175 loc = cp_lexer_peek_token (parser->lexer)->location;
31176
31177 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31178 while (true)
31179 {
31180 tree property;
31181 property = cp_parser_identifier (parser);
31182 if (property == error_mark_node)
31183 {
31184 cp_parser_consume_semicolon_at_end_of_statement (parser);
31185 return;
31186 }
31187 list = chainon (list, build_tree_list (NULL, property));
31188 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31189 cp_lexer_consume_token (parser->lexer);
31190 else
31191 break;
31192 }
31193 cp_parser_consume_semicolon_at_end_of_statement (parser);
31194 objc_add_dynamic_declaration (loc, list);
31195 }
31196
31197 \f
31198 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31199
31200 /* Returns name of the next clause.
31201 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31202 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31203 returned and the token is consumed. */
31204
31205 static pragma_omp_clause
31206 cp_parser_omp_clause_name (cp_parser *parser)
31207 {
31208 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31209
31210 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31211 result = PRAGMA_OACC_CLAUSE_AUTO;
31212 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31213 result = PRAGMA_OMP_CLAUSE_IF;
31214 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31215 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31216 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31217 result = PRAGMA_OACC_CLAUSE_DELETE;
31218 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31219 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31220 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31221 result = PRAGMA_OMP_CLAUSE_FOR;
31222 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31223 {
31224 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31225 const char *p = IDENTIFIER_POINTER (id);
31226
31227 switch (p[0])
31228 {
31229 case 'a':
31230 if (!strcmp ("aligned", p))
31231 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31232 else if (!strcmp ("async", p))
31233 result = PRAGMA_OACC_CLAUSE_ASYNC;
31234 break;
31235 case 'c':
31236 if (!strcmp ("collapse", p))
31237 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31238 else if (!strcmp ("copy", p))
31239 result = PRAGMA_OACC_CLAUSE_COPY;
31240 else if (!strcmp ("copyin", p))
31241 result = PRAGMA_OMP_CLAUSE_COPYIN;
31242 else if (!strcmp ("copyout", p))
31243 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31244 else if (!strcmp ("copyprivate", p))
31245 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31246 else if (!strcmp ("create", p))
31247 result = PRAGMA_OACC_CLAUSE_CREATE;
31248 break;
31249 case 'd':
31250 if (!strcmp ("defaultmap", p))
31251 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31252 else if (!strcmp ("depend", p))
31253 result = PRAGMA_OMP_CLAUSE_DEPEND;
31254 else if (!strcmp ("device", p))
31255 result = PRAGMA_OMP_CLAUSE_DEVICE;
31256 else if (!strcmp ("deviceptr", p))
31257 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31258 else if (!strcmp ("device_resident", p))
31259 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31260 else if (!strcmp ("dist_schedule", p))
31261 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31262 break;
31263 case 'f':
31264 if (!strcmp ("final", p))
31265 result = PRAGMA_OMP_CLAUSE_FINAL;
31266 else if (!strcmp ("firstprivate", p))
31267 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31268 else if (!strcmp ("from", p))
31269 result = PRAGMA_OMP_CLAUSE_FROM;
31270 break;
31271 case 'g':
31272 if (!strcmp ("gang", p))
31273 result = PRAGMA_OACC_CLAUSE_GANG;
31274 else if (!strcmp ("grainsize", p))
31275 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31276 break;
31277 case 'h':
31278 if (!strcmp ("hint", p))
31279 result = PRAGMA_OMP_CLAUSE_HINT;
31280 else if (!strcmp ("host", p))
31281 result = PRAGMA_OACC_CLAUSE_HOST;
31282 break;
31283 case 'i':
31284 if (!strcmp ("inbranch", p))
31285 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31286 else if (!strcmp ("independent", p))
31287 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31288 else if (!strcmp ("is_device_ptr", p))
31289 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31290 break;
31291 case 'l':
31292 if (!strcmp ("lastprivate", p))
31293 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31294 else if (!strcmp ("linear", p))
31295 result = PRAGMA_OMP_CLAUSE_LINEAR;
31296 else if (!strcmp ("link", p))
31297 result = PRAGMA_OMP_CLAUSE_LINK;
31298 break;
31299 case 'm':
31300 if (!strcmp ("map", p))
31301 result = PRAGMA_OMP_CLAUSE_MAP;
31302 else if (!strcmp ("mergeable", p))
31303 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31304 else if (flag_cilkplus && !strcmp ("mask", p))
31305 result = PRAGMA_CILK_CLAUSE_MASK;
31306 break;
31307 case 'n':
31308 if (!strcmp ("nogroup", p))
31309 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31310 else if (!strcmp ("notinbranch", p))
31311 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31312 else if (!strcmp ("nowait", p))
31313 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31314 else if (flag_cilkplus && !strcmp ("nomask", p))
31315 result = PRAGMA_CILK_CLAUSE_NOMASK;
31316 else if (!strcmp ("num_gangs", p))
31317 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31318 else if (!strcmp ("num_tasks", p))
31319 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31320 else if (!strcmp ("num_teams", p))
31321 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31322 else if (!strcmp ("num_threads", p))
31323 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31324 else if (!strcmp ("num_workers", p))
31325 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31326 break;
31327 case 'o':
31328 if (!strcmp ("ordered", p))
31329 result = PRAGMA_OMP_CLAUSE_ORDERED;
31330 break;
31331 case 'p':
31332 if (!strcmp ("parallel", p))
31333 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31334 else if (!strcmp ("present", p))
31335 result = PRAGMA_OACC_CLAUSE_PRESENT;
31336 else if (!strcmp ("present_or_copy", p)
31337 || !strcmp ("pcopy", p))
31338 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31339 else if (!strcmp ("present_or_copyin", p)
31340 || !strcmp ("pcopyin", p))
31341 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31342 else if (!strcmp ("present_or_copyout", p)
31343 || !strcmp ("pcopyout", p))
31344 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31345 else if (!strcmp ("present_or_create", p)
31346 || !strcmp ("pcreate", p))
31347 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31348 else if (!strcmp ("priority", p))
31349 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31350 else if (!strcmp ("proc_bind", p))
31351 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31352 break;
31353 case 'r':
31354 if (!strcmp ("reduction", p))
31355 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31356 break;
31357 case 's':
31358 if (!strcmp ("safelen", p))
31359 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31360 else if (!strcmp ("schedule", p))
31361 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31362 else if (!strcmp ("sections", p))
31363 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31364 else if (!strcmp ("self", p))
31365 result = PRAGMA_OACC_CLAUSE_SELF;
31366 else if (!strcmp ("seq", p))
31367 result = PRAGMA_OACC_CLAUSE_SEQ;
31368 else if (!strcmp ("shared", p))
31369 result = PRAGMA_OMP_CLAUSE_SHARED;
31370 else if (!strcmp ("simd", p))
31371 result = PRAGMA_OMP_CLAUSE_SIMD;
31372 else if (!strcmp ("simdlen", p))
31373 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31374 break;
31375 case 't':
31376 if (!strcmp ("taskgroup", p))
31377 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31378 else if (!strcmp ("thread_limit", p))
31379 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31380 else if (!strcmp ("threads", p))
31381 result = PRAGMA_OMP_CLAUSE_THREADS;
31382 else if (!strcmp ("tile", p))
31383 result = PRAGMA_OACC_CLAUSE_TILE;
31384 else if (!strcmp ("to", p))
31385 result = PRAGMA_OMP_CLAUSE_TO;
31386 break;
31387 case 'u':
31388 if (!strcmp ("uniform", p))
31389 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31390 else if (!strcmp ("untied", p))
31391 result = PRAGMA_OMP_CLAUSE_UNTIED;
31392 else if (!strcmp ("use_device", p))
31393 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31394 else if (!strcmp ("use_device_ptr", p))
31395 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31396 break;
31397 case 'v':
31398 if (!strcmp ("vector", p))
31399 result = PRAGMA_OACC_CLAUSE_VECTOR;
31400 else if (!strcmp ("vector_length", p))
31401 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31402 else if (flag_cilkplus && !strcmp ("vectorlength", p))
31403 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31404 break;
31405 case 'w':
31406 if (!strcmp ("wait", p))
31407 result = PRAGMA_OACC_CLAUSE_WAIT;
31408 else if (!strcmp ("worker", p))
31409 result = PRAGMA_OACC_CLAUSE_WORKER;
31410 break;
31411 }
31412 }
31413
31414 if (result != PRAGMA_OMP_CLAUSE_NONE)
31415 cp_lexer_consume_token (parser->lexer);
31416
31417 return result;
31418 }
31419
31420 /* Validate that a clause of the given type does not already exist. */
31421
31422 static void
31423 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31424 const char *name, location_t location)
31425 {
31426 tree c;
31427
31428 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31429 if (OMP_CLAUSE_CODE (c) == code)
31430 {
31431 error_at (location, "too many %qs clauses", name);
31432 break;
31433 }
31434 }
31435
31436 /* OpenMP 2.5:
31437 variable-list:
31438 identifier
31439 variable-list , identifier
31440
31441 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31442 colon). An opening parenthesis will have been consumed by the caller.
31443
31444 If KIND is nonzero, create the appropriate node and install the decl
31445 in OMP_CLAUSE_DECL and add the node to the head of the list.
31446
31447 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31448 return the list created.
31449
31450 COLON can be NULL if only closing parenthesis should end the list,
31451 or pointer to bool which will receive false if the list is terminated
31452 by closing parenthesis or true if the list is terminated by colon. */
31453
31454 static tree
31455 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31456 tree list, bool *colon)
31457 {
31458 cp_token *token;
31459 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31460 if (colon)
31461 {
31462 parser->colon_corrects_to_scope_p = false;
31463 *colon = false;
31464 }
31465 while (1)
31466 {
31467 tree name, decl;
31468
31469 token = cp_lexer_peek_token (parser->lexer);
31470 if (kind != 0
31471 && current_class_ptr
31472 && cp_parser_is_keyword (token, RID_THIS))
31473 {
31474 decl = finish_this_expr ();
31475 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31476 || CONVERT_EXPR_P (decl))
31477 decl = TREE_OPERAND (decl, 0);
31478 cp_lexer_consume_token (parser->lexer);
31479 }
31480 else
31481 {
31482 name = cp_parser_id_expression (parser, /*template_p=*/false,
31483 /*check_dependency_p=*/true,
31484 /*template_p=*/NULL,
31485 /*declarator_p=*/false,
31486 /*optional_p=*/false);
31487 if (name == error_mark_node)
31488 goto skip_comma;
31489
31490 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31491 if (decl == error_mark_node)
31492 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31493 token->location);
31494 }
31495 if (decl == error_mark_node)
31496 ;
31497 else if (kind != 0)
31498 {
31499 switch (kind)
31500 {
31501 case OMP_CLAUSE__CACHE_:
31502 /* The OpenACC cache directive explicitly only allows "array
31503 elements or subarrays". */
31504 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31505 {
31506 error_at (token->location, "expected %<[%>");
31507 decl = error_mark_node;
31508 break;
31509 }
31510 /* FALLTHROUGH. */
31511 case OMP_CLAUSE_MAP:
31512 case OMP_CLAUSE_FROM:
31513 case OMP_CLAUSE_TO:
31514 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31515 {
31516 location_t loc
31517 = cp_lexer_peek_token (parser->lexer)->location;
31518 cp_id_kind idk = CP_ID_KIND_NONE;
31519 cp_lexer_consume_token (parser->lexer);
31520 decl = convert_from_reference (decl);
31521 decl
31522 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31523 decl, false,
31524 &idk, loc);
31525 }
31526 /* FALLTHROUGH. */
31527 case OMP_CLAUSE_DEPEND:
31528 case OMP_CLAUSE_REDUCTION:
31529 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31530 {
31531 tree low_bound = NULL_TREE, length = NULL_TREE;
31532
31533 parser->colon_corrects_to_scope_p = false;
31534 cp_lexer_consume_token (parser->lexer);
31535 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31536 low_bound = cp_parser_expression (parser);
31537 if (!colon)
31538 parser->colon_corrects_to_scope_p
31539 = saved_colon_corrects_to_scope_p;
31540 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31541 length = integer_one_node;
31542 else
31543 {
31544 /* Look for `:'. */
31545 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31546 goto skip_comma;
31547 if (!cp_lexer_next_token_is (parser->lexer,
31548 CPP_CLOSE_SQUARE))
31549 length = cp_parser_expression (parser);
31550 }
31551 /* Look for the closing `]'. */
31552 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31553 RT_CLOSE_SQUARE))
31554 goto skip_comma;
31555
31556 decl = tree_cons (low_bound, length, decl);
31557 }
31558 break;
31559 default:
31560 break;
31561 }
31562
31563 tree u = build_omp_clause (token->location, kind);
31564 OMP_CLAUSE_DECL (u) = decl;
31565 OMP_CLAUSE_CHAIN (u) = list;
31566 list = u;
31567 }
31568 else
31569 list = tree_cons (decl, NULL_TREE, list);
31570
31571 get_comma:
31572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31573 break;
31574 cp_lexer_consume_token (parser->lexer);
31575 }
31576
31577 if (colon)
31578 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31579
31580 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31581 {
31582 *colon = true;
31583 cp_parser_require (parser, CPP_COLON, RT_COLON);
31584 return list;
31585 }
31586
31587 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31588 {
31589 int ending;
31590
31591 /* Try to resync to an unnested comma. Copied from
31592 cp_parser_parenthesized_expression_list. */
31593 skip_comma:
31594 if (colon)
31595 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31596 ending = cp_parser_skip_to_closing_parenthesis (parser,
31597 /*recovering=*/true,
31598 /*or_comma=*/true,
31599 /*consume_paren=*/true);
31600 if (ending < 0)
31601 goto get_comma;
31602 }
31603
31604 return list;
31605 }
31606
31607 /* Similarly, but expect leading and trailing parenthesis. This is a very
31608 common case for omp clauses. */
31609
31610 static tree
31611 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31612 {
31613 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31614 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31615 return list;
31616 }
31617
31618 /* OpenACC 2.0:
31619 copy ( variable-list )
31620 copyin ( variable-list )
31621 copyout ( variable-list )
31622 create ( variable-list )
31623 delete ( variable-list )
31624 present ( variable-list )
31625 present_or_copy ( variable-list )
31626 pcopy ( variable-list )
31627 present_or_copyin ( variable-list )
31628 pcopyin ( variable-list )
31629 present_or_copyout ( variable-list )
31630 pcopyout ( variable-list )
31631 present_or_create ( variable-list )
31632 pcreate ( variable-list ) */
31633
31634 static tree
31635 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31636 tree list)
31637 {
31638 enum gomp_map_kind kind;
31639 switch (c_kind)
31640 {
31641 case PRAGMA_OACC_CLAUSE_COPY:
31642 kind = GOMP_MAP_FORCE_TOFROM;
31643 break;
31644 case PRAGMA_OACC_CLAUSE_COPYIN:
31645 kind = GOMP_MAP_FORCE_TO;
31646 break;
31647 case PRAGMA_OACC_CLAUSE_COPYOUT:
31648 kind = GOMP_MAP_FORCE_FROM;
31649 break;
31650 case PRAGMA_OACC_CLAUSE_CREATE:
31651 kind = GOMP_MAP_FORCE_ALLOC;
31652 break;
31653 case PRAGMA_OACC_CLAUSE_DELETE:
31654 kind = GOMP_MAP_DELETE;
31655 break;
31656 case PRAGMA_OACC_CLAUSE_DEVICE:
31657 kind = GOMP_MAP_FORCE_TO;
31658 break;
31659 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31660 kind = GOMP_MAP_DEVICE_RESIDENT;
31661 break;
31662 case PRAGMA_OACC_CLAUSE_HOST:
31663 case PRAGMA_OACC_CLAUSE_SELF:
31664 kind = GOMP_MAP_FORCE_FROM;
31665 break;
31666 case PRAGMA_OACC_CLAUSE_LINK:
31667 kind = GOMP_MAP_LINK;
31668 break;
31669 case PRAGMA_OACC_CLAUSE_PRESENT:
31670 kind = GOMP_MAP_FORCE_PRESENT;
31671 break;
31672 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31673 kind = GOMP_MAP_TOFROM;
31674 break;
31675 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31676 kind = GOMP_MAP_TO;
31677 break;
31678 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31679 kind = GOMP_MAP_FROM;
31680 break;
31681 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31682 kind = GOMP_MAP_ALLOC;
31683 break;
31684 default:
31685 gcc_unreachable ();
31686 }
31687 tree nl, c;
31688 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31689
31690 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31691 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31692
31693 return nl;
31694 }
31695
31696 /* OpenACC 2.0:
31697 deviceptr ( variable-list ) */
31698
31699 static tree
31700 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31701 {
31702 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31703 tree vars, t;
31704
31705 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31706 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31707 variable-list must only allow for pointer variables. */
31708 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31709 for (t = vars; t; t = TREE_CHAIN (t))
31710 {
31711 tree v = TREE_PURPOSE (t);
31712 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31713 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31714 OMP_CLAUSE_DECL (u) = v;
31715 OMP_CLAUSE_CHAIN (u) = list;
31716 list = u;
31717 }
31718
31719 return list;
31720 }
31721
31722 /* OpenACC 2.0:
31723 auto
31724 independent
31725 nohost
31726 seq */
31727
31728 static tree
31729 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31730 enum omp_clause_code code,
31731 tree list, location_t location)
31732 {
31733 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31734 tree c = build_omp_clause (location, code);
31735 OMP_CLAUSE_CHAIN (c) = list;
31736 return c;
31737 }
31738
31739 /* OpenACC:
31740 num_gangs ( expression )
31741 num_workers ( expression )
31742 vector_length ( expression ) */
31743
31744 static tree
31745 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31746 const char *str, tree list)
31747 {
31748 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31749
31750 matching_parens parens;
31751 if (!parens.require_open (parser))
31752 return list;
31753
31754 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31755
31756 if (t == error_mark_node
31757 || !parens.require_close (parser))
31758 {
31759 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31760 /*or_comma=*/false,
31761 /*consume_paren=*/true);
31762 return list;
31763 }
31764
31765 check_no_duplicate_clause (list, code, str, loc);
31766
31767 tree c = build_omp_clause (loc, code);
31768 OMP_CLAUSE_OPERAND (c, 0) = t;
31769 OMP_CLAUSE_CHAIN (c) = list;
31770 return c;
31771 }
31772
31773 /* OpenACC:
31774
31775 gang [( gang-arg-list )]
31776 worker [( [num:] int-expr )]
31777 vector [( [length:] int-expr )]
31778
31779 where gang-arg is one of:
31780
31781 [num:] int-expr
31782 static: size-expr
31783
31784 and size-expr may be:
31785
31786 *
31787 int-expr
31788 */
31789
31790 static tree
31791 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31792 const char *str, tree list)
31793 {
31794 const char *id = "num";
31795 cp_lexer *lexer = parser->lexer;
31796 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31797 location_t loc = cp_lexer_peek_token (lexer)->location;
31798
31799 if (kind == OMP_CLAUSE_VECTOR)
31800 id = "length";
31801
31802 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31803 {
31804 matching_parens parens;
31805 parens.consume_open (parser);
31806
31807 do
31808 {
31809 cp_token *next = cp_lexer_peek_token (lexer);
31810 int idx = 0;
31811
31812 /* Gang static argument. */
31813 if (kind == OMP_CLAUSE_GANG
31814 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31815 {
31816 cp_lexer_consume_token (lexer);
31817
31818 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31819 goto cleanup_error;
31820
31821 idx = 1;
31822 if (ops[idx] != NULL)
31823 {
31824 cp_parser_error (parser, "too many %<static%> arguments");
31825 goto cleanup_error;
31826 }
31827
31828 /* Check for the '*' argument. */
31829 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31830 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31831 || cp_lexer_nth_token_is (parser->lexer, 2,
31832 CPP_CLOSE_PAREN)))
31833 {
31834 cp_lexer_consume_token (lexer);
31835 ops[idx] = integer_minus_one_node;
31836
31837 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31838 {
31839 cp_lexer_consume_token (lexer);
31840 continue;
31841 }
31842 else break;
31843 }
31844 }
31845 /* Worker num: argument and vector length: arguments. */
31846 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31847 && id_equal (next->u.value, id)
31848 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31849 {
31850 cp_lexer_consume_token (lexer); /* id */
31851 cp_lexer_consume_token (lexer); /* ':' */
31852 }
31853
31854 /* Now collect the actual argument. */
31855 if (ops[idx] != NULL_TREE)
31856 {
31857 cp_parser_error (parser, "unexpected argument");
31858 goto cleanup_error;
31859 }
31860
31861 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31862 false);
31863 if (expr == error_mark_node)
31864 goto cleanup_error;
31865
31866 mark_exp_read (expr);
31867 ops[idx] = expr;
31868
31869 if (kind == OMP_CLAUSE_GANG
31870 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31871 {
31872 cp_lexer_consume_token (lexer);
31873 continue;
31874 }
31875 break;
31876 }
31877 while (1);
31878
31879 if (!parens.require_close (parser))
31880 goto cleanup_error;
31881 }
31882
31883 check_no_duplicate_clause (list, kind, str, loc);
31884
31885 c = build_omp_clause (loc, kind);
31886
31887 if (ops[1])
31888 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31889
31890 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31891 OMP_CLAUSE_CHAIN (c) = list;
31892
31893 return c;
31894
31895 cleanup_error:
31896 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31897 return list;
31898 }
31899
31900 /* OpenACC 2.0:
31901 tile ( size-expr-list ) */
31902
31903 static tree
31904 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31905 {
31906 tree c, expr = error_mark_node;
31907 tree tile = NULL_TREE;
31908
31909 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31910 so, but the spec authors never considered such a case and have
31911 differing opinions on what it might mean, including 'not
31912 allowed'.) */
31913 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31914 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31915 clause_loc);
31916
31917 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31918 return list;
31919
31920 do
31921 {
31922 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31923 return list;
31924
31925 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31926 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31927 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31928 {
31929 cp_lexer_consume_token (parser->lexer);
31930 expr = integer_zero_node;
31931 }
31932 else
31933 expr = cp_parser_constant_expression (parser);
31934
31935 tile = tree_cons (NULL_TREE, expr, tile);
31936 }
31937 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31938
31939 /* Consume the trailing ')'. */
31940 cp_lexer_consume_token (parser->lexer);
31941
31942 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31943 tile = nreverse (tile);
31944 OMP_CLAUSE_TILE_LIST (c) = tile;
31945 OMP_CLAUSE_CHAIN (c) = list;
31946 return c;
31947 }
31948
31949 /* OpenACC 2.0
31950 Parse wait clause or directive parameters. */
31951
31952 static tree
31953 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31954 {
31955 vec<tree, va_gc> *args;
31956 tree t, args_tree;
31957
31958 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31959 /*cast_p=*/false,
31960 /*allow_expansion_p=*/true,
31961 /*non_constant_p=*/NULL);
31962
31963 if (args == NULL || args->length () == 0)
31964 {
31965 cp_parser_error (parser, "expected integer expression before ')'");
31966 if (args != NULL)
31967 release_tree_vector (args);
31968 return list;
31969 }
31970
31971 args_tree = build_tree_list_vec (args);
31972
31973 release_tree_vector (args);
31974
31975 for (t = args_tree; t; t = TREE_CHAIN (t))
31976 {
31977 tree targ = TREE_VALUE (t);
31978
31979 if (targ != error_mark_node)
31980 {
31981 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31982 error ("%<wait%> expression must be integral");
31983 else
31984 {
31985 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31986
31987 targ = mark_rvalue_use (targ);
31988 OMP_CLAUSE_DECL (c) = targ;
31989 OMP_CLAUSE_CHAIN (c) = list;
31990 list = c;
31991 }
31992 }
31993 }
31994
31995 return list;
31996 }
31997
31998 /* OpenACC:
31999 wait ( int-expr-list ) */
32000
32001 static tree
32002 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32003 {
32004 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32005
32006 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32007 return list;
32008
32009 list = cp_parser_oacc_wait_list (parser, location, list);
32010
32011 return list;
32012 }
32013
32014 /* OpenMP 3.0:
32015 collapse ( constant-expression ) */
32016
32017 static tree
32018 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32019 {
32020 tree c, num;
32021 location_t loc;
32022 HOST_WIDE_INT n;
32023
32024 loc = cp_lexer_peek_token (parser->lexer)->location;
32025 matching_parens parens;
32026 if (!parens.require_open (parser))
32027 return list;
32028
32029 num = cp_parser_constant_expression (parser);
32030
32031 if (!parens.require_close (parser))
32032 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32033 /*or_comma=*/false,
32034 /*consume_paren=*/true);
32035
32036 if (num == error_mark_node)
32037 return list;
32038 num = fold_non_dependent_expr (num);
32039 if (!tree_fits_shwi_p (num)
32040 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32041 || (n = tree_to_shwi (num)) <= 0
32042 || (int) n != n)
32043 {
32044 error_at (loc, "collapse argument needs positive constant integer expression");
32045 return list;
32046 }
32047
32048 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32049 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32050 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32051 OMP_CLAUSE_CHAIN (c) = list;
32052 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32053
32054 return c;
32055 }
32056
32057 /* OpenMP 2.5:
32058 default ( none | shared )
32059
32060 OpenACC:
32061 default ( none | present ) */
32062
32063 static tree
32064 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32065 location_t location, bool is_oacc)
32066 {
32067 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32068 tree c;
32069
32070 matching_parens parens;
32071 if (!parens.require_open (parser))
32072 return list;
32073 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32074 {
32075 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32076 const char *p = IDENTIFIER_POINTER (id);
32077
32078 switch (p[0])
32079 {
32080 case 'n':
32081 if (strcmp ("none", p) != 0)
32082 goto invalid_kind;
32083 kind = OMP_CLAUSE_DEFAULT_NONE;
32084 break;
32085
32086 case 'p':
32087 if (strcmp ("present", p) != 0 || !is_oacc)
32088 goto invalid_kind;
32089 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32090 break;
32091
32092 case 's':
32093 if (strcmp ("shared", p) != 0 || is_oacc)
32094 goto invalid_kind;
32095 kind = OMP_CLAUSE_DEFAULT_SHARED;
32096 break;
32097
32098 default:
32099 goto invalid_kind;
32100 }
32101
32102 cp_lexer_consume_token (parser->lexer);
32103 }
32104 else
32105 {
32106 invalid_kind:
32107 if (is_oacc)
32108 cp_parser_error (parser, "expected %<none%> or %<present%>");
32109 else
32110 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32111 }
32112
32113 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32114 || !parens.require_close (parser))
32115 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32116 /*or_comma=*/false,
32117 /*consume_paren=*/true);
32118
32119 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32120 return list;
32121
32122 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32123 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32124 OMP_CLAUSE_CHAIN (c) = list;
32125 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32126
32127 return c;
32128 }
32129
32130 /* OpenMP 3.1:
32131 final ( expression ) */
32132
32133 static tree
32134 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32135 {
32136 tree t, c;
32137
32138 matching_parens parens;
32139 if (!parens.require_open (parser))
32140 return list;
32141
32142 t = cp_parser_condition (parser);
32143
32144 if (t == error_mark_node
32145 || !parens.require_close (parser))
32146 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32147 /*or_comma=*/false,
32148 /*consume_paren=*/true);
32149
32150 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32151
32152 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32153 OMP_CLAUSE_FINAL_EXPR (c) = t;
32154 OMP_CLAUSE_CHAIN (c) = list;
32155
32156 return c;
32157 }
32158
32159 /* OpenMP 2.5:
32160 if ( expression )
32161
32162 OpenMP 4.5:
32163 if ( directive-name-modifier : expression )
32164
32165 directive-name-modifier:
32166 parallel | task | taskloop | target data | target | target update
32167 | target enter data | target exit data */
32168
32169 static tree
32170 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32171 bool is_omp)
32172 {
32173 tree t, c;
32174 enum tree_code if_modifier = ERROR_MARK;
32175
32176 matching_parens parens;
32177 if (!parens.require_open (parser))
32178 return list;
32179
32180 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32181 {
32182 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32183 const char *p = IDENTIFIER_POINTER (id);
32184 int n = 2;
32185
32186 if (strcmp ("parallel", p) == 0)
32187 if_modifier = OMP_PARALLEL;
32188 else if (strcmp ("task", p) == 0)
32189 if_modifier = OMP_TASK;
32190 else if (strcmp ("taskloop", p) == 0)
32191 if_modifier = OMP_TASKLOOP;
32192 else if (strcmp ("target", p) == 0)
32193 {
32194 if_modifier = OMP_TARGET;
32195 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32196 {
32197 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32198 p = IDENTIFIER_POINTER (id);
32199 if (strcmp ("data", p) == 0)
32200 if_modifier = OMP_TARGET_DATA;
32201 else if (strcmp ("update", p) == 0)
32202 if_modifier = OMP_TARGET_UPDATE;
32203 else if (strcmp ("enter", p) == 0)
32204 if_modifier = OMP_TARGET_ENTER_DATA;
32205 else if (strcmp ("exit", p) == 0)
32206 if_modifier = OMP_TARGET_EXIT_DATA;
32207 if (if_modifier != OMP_TARGET)
32208 n = 3;
32209 else
32210 {
32211 location_t loc
32212 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32213 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32214 "or %<exit%>");
32215 if_modifier = ERROR_MARK;
32216 }
32217 if (if_modifier == OMP_TARGET_ENTER_DATA
32218 || if_modifier == OMP_TARGET_EXIT_DATA)
32219 {
32220 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32221 {
32222 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32223 p = IDENTIFIER_POINTER (id);
32224 if (strcmp ("data", p) == 0)
32225 n = 4;
32226 }
32227 if (n != 4)
32228 {
32229 location_t loc
32230 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32231 error_at (loc, "expected %<data%>");
32232 if_modifier = ERROR_MARK;
32233 }
32234 }
32235 }
32236 }
32237 if (if_modifier != ERROR_MARK)
32238 {
32239 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32240 {
32241 while (n-- > 0)
32242 cp_lexer_consume_token (parser->lexer);
32243 }
32244 else
32245 {
32246 if (n > 2)
32247 {
32248 location_t loc
32249 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32250 error_at (loc, "expected %<:%>");
32251 }
32252 if_modifier = ERROR_MARK;
32253 }
32254 }
32255 }
32256
32257 t = cp_parser_condition (parser);
32258
32259 if (t == error_mark_node
32260 || !parens.require_close (parser))
32261 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32262 /*or_comma=*/false,
32263 /*consume_paren=*/true);
32264
32265 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32266 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32267 {
32268 if (if_modifier != ERROR_MARK
32269 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32270 {
32271 const char *p = NULL;
32272 switch (if_modifier)
32273 {
32274 case OMP_PARALLEL: p = "parallel"; break;
32275 case OMP_TASK: p = "task"; break;
32276 case OMP_TASKLOOP: p = "taskloop"; break;
32277 case OMP_TARGET_DATA: p = "target data"; break;
32278 case OMP_TARGET: p = "target"; break;
32279 case OMP_TARGET_UPDATE: p = "target update"; break;
32280 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32281 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32282 default: gcc_unreachable ();
32283 }
32284 error_at (location, "too many %<if%> clauses with %qs modifier",
32285 p);
32286 return list;
32287 }
32288 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32289 {
32290 if (!is_omp)
32291 error_at (location, "too many %<if%> clauses");
32292 else
32293 error_at (location, "too many %<if%> clauses without modifier");
32294 return list;
32295 }
32296 else if (if_modifier == ERROR_MARK
32297 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32298 {
32299 error_at (location, "if any %<if%> clause has modifier, then all "
32300 "%<if%> clauses have to use modifier");
32301 return list;
32302 }
32303 }
32304
32305 c = build_omp_clause (location, OMP_CLAUSE_IF);
32306 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32307 OMP_CLAUSE_IF_EXPR (c) = t;
32308 OMP_CLAUSE_CHAIN (c) = list;
32309
32310 return c;
32311 }
32312
32313 /* OpenMP 3.1:
32314 mergeable */
32315
32316 static tree
32317 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32318 tree list, location_t location)
32319 {
32320 tree c;
32321
32322 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32323 location);
32324
32325 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32326 OMP_CLAUSE_CHAIN (c) = list;
32327 return c;
32328 }
32329
32330 /* OpenMP 2.5:
32331 nowait */
32332
32333 static tree
32334 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32335 tree list, location_t location)
32336 {
32337 tree c;
32338
32339 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32340
32341 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32342 OMP_CLAUSE_CHAIN (c) = list;
32343 return c;
32344 }
32345
32346 /* OpenMP 2.5:
32347 num_threads ( expression ) */
32348
32349 static tree
32350 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32351 location_t location)
32352 {
32353 tree t, c;
32354
32355 matching_parens parens;
32356 if (!parens.require_open (parser))
32357 return list;
32358
32359 t = cp_parser_expression (parser);
32360
32361 if (t == error_mark_node
32362 || !parens.require_close (parser))
32363 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32364 /*or_comma=*/false,
32365 /*consume_paren=*/true);
32366
32367 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32368 "num_threads", location);
32369
32370 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32371 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32372 OMP_CLAUSE_CHAIN (c) = list;
32373
32374 return c;
32375 }
32376
32377 /* OpenMP 4.5:
32378 num_tasks ( expression ) */
32379
32380 static tree
32381 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32382 location_t location)
32383 {
32384 tree t, c;
32385
32386 matching_parens parens;
32387 if (!parens.require_open (parser))
32388 return list;
32389
32390 t = cp_parser_expression (parser);
32391
32392 if (t == error_mark_node
32393 || !parens.require_close (parser))
32394 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32395 /*or_comma=*/false,
32396 /*consume_paren=*/true);
32397
32398 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32399 "num_tasks", location);
32400
32401 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32402 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32403 OMP_CLAUSE_CHAIN (c) = list;
32404
32405 return c;
32406 }
32407
32408 /* OpenMP 4.5:
32409 grainsize ( expression ) */
32410
32411 static tree
32412 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32413 location_t location)
32414 {
32415 tree t, c;
32416
32417 matching_parens parens;
32418 if (!parens.require_open (parser))
32419 return list;
32420
32421 t = cp_parser_expression (parser);
32422
32423 if (t == error_mark_node
32424 || !parens.require_close (parser))
32425 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32426 /*or_comma=*/false,
32427 /*consume_paren=*/true);
32428
32429 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32430 "grainsize", location);
32431
32432 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32433 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32434 OMP_CLAUSE_CHAIN (c) = list;
32435
32436 return c;
32437 }
32438
32439 /* OpenMP 4.5:
32440 priority ( expression ) */
32441
32442 static tree
32443 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32444 location_t location)
32445 {
32446 tree t, c;
32447
32448 matching_parens parens;
32449 if (!parens.require_open (parser))
32450 return list;
32451
32452 t = cp_parser_expression (parser);
32453
32454 if (t == error_mark_node
32455 || !parens.require_close (parser))
32456 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32457 /*or_comma=*/false,
32458 /*consume_paren=*/true);
32459
32460 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32461 "priority", location);
32462
32463 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32464 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32465 OMP_CLAUSE_CHAIN (c) = list;
32466
32467 return c;
32468 }
32469
32470 /* OpenMP 4.5:
32471 hint ( expression ) */
32472
32473 static tree
32474 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32475 location_t location)
32476 {
32477 tree t, c;
32478
32479 matching_parens parens;
32480 if (!parens.require_open (parser))
32481 return list;
32482
32483 t = cp_parser_expression (parser);
32484
32485 if (t == error_mark_node
32486 || !parens.require_close (parser))
32487 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32488 /*or_comma=*/false,
32489 /*consume_paren=*/true);
32490
32491 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32492
32493 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32494 OMP_CLAUSE_HINT_EXPR (c) = t;
32495 OMP_CLAUSE_CHAIN (c) = list;
32496
32497 return c;
32498 }
32499
32500 /* OpenMP 4.5:
32501 defaultmap ( tofrom : scalar ) */
32502
32503 static tree
32504 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32505 location_t location)
32506 {
32507 tree c, id;
32508 const char *p;
32509
32510 matching_parens parens;
32511 if (!parens.require_open (parser))
32512 return list;
32513
32514 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32515 {
32516 cp_parser_error (parser, "expected %<tofrom%>");
32517 goto out_err;
32518 }
32519 id = cp_lexer_peek_token (parser->lexer)->u.value;
32520 p = IDENTIFIER_POINTER (id);
32521 if (strcmp (p, "tofrom") != 0)
32522 {
32523 cp_parser_error (parser, "expected %<tofrom%>");
32524 goto out_err;
32525 }
32526 cp_lexer_consume_token (parser->lexer);
32527 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32528 goto out_err;
32529
32530 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32531 {
32532 cp_parser_error (parser, "expected %<scalar%>");
32533 goto out_err;
32534 }
32535 id = cp_lexer_peek_token (parser->lexer)->u.value;
32536 p = IDENTIFIER_POINTER (id);
32537 if (strcmp (p, "scalar") != 0)
32538 {
32539 cp_parser_error (parser, "expected %<scalar%>");
32540 goto out_err;
32541 }
32542 cp_lexer_consume_token (parser->lexer);
32543 if (!parens.require_close (parser))
32544 goto out_err;
32545
32546 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32547 location);
32548
32549 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32550 OMP_CLAUSE_CHAIN (c) = list;
32551 return c;
32552
32553 out_err:
32554 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32555 /*or_comma=*/false,
32556 /*consume_paren=*/true);
32557 return list;
32558 }
32559
32560 /* OpenMP 2.5:
32561 ordered
32562
32563 OpenMP 4.5:
32564 ordered ( constant-expression ) */
32565
32566 static tree
32567 cp_parser_omp_clause_ordered (cp_parser *parser,
32568 tree list, location_t location)
32569 {
32570 tree c, num = NULL_TREE;
32571 HOST_WIDE_INT n;
32572
32573 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32574 "ordered", location);
32575
32576 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32577 {
32578 matching_parens parens;
32579 parens.consume_open (parser);
32580
32581 num = cp_parser_constant_expression (parser);
32582
32583 if (!parens.require_close (parser))
32584 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32585 /*or_comma=*/false,
32586 /*consume_paren=*/true);
32587
32588 if (num == error_mark_node)
32589 return list;
32590 num = fold_non_dependent_expr (num);
32591 if (!tree_fits_shwi_p (num)
32592 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32593 || (n = tree_to_shwi (num)) <= 0
32594 || (int) n != n)
32595 {
32596 error_at (location,
32597 "ordered argument needs positive constant integer "
32598 "expression");
32599 return list;
32600 }
32601 }
32602
32603 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32604 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32605 OMP_CLAUSE_CHAIN (c) = list;
32606 return c;
32607 }
32608
32609 /* OpenMP 2.5:
32610 reduction ( reduction-operator : variable-list )
32611
32612 reduction-operator:
32613 One of: + * - & ^ | && ||
32614
32615 OpenMP 3.1:
32616
32617 reduction-operator:
32618 One of: + * - & ^ | && || min max
32619
32620 OpenMP 4.0:
32621
32622 reduction-operator:
32623 One of: + * - & ^ | && ||
32624 id-expression */
32625
32626 static tree
32627 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32628 {
32629 enum tree_code code = ERROR_MARK;
32630 tree nlist, c, id = NULL_TREE;
32631
32632 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32633 return list;
32634
32635 switch (cp_lexer_peek_token (parser->lexer)->type)
32636 {
32637 case CPP_PLUS: code = PLUS_EXPR; break;
32638 case CPP_MULT: code = MULT_EXPR; break;
32639 case CPP_MINUS: code = MINUS_EXPR; break;
32640 case CPP_AND: code = BIT_AND_EXPR; break;
32641 case CPP_XOR: code = BIT_XOR_EXPR; break;
32642 case CPP_OR: code = BIT_IOR_EXPR; break;
32643 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32644 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32645 default: break;
32646 }
32647
32648 if (code != ERROR_MARK)
32649 cp_lexer_consume_token (parser->lexer);
32650 else
32651 {
32652 bool saved_colon_corrects_to_scope_p;
32653 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32654 parser->colon_corrects_to_scope_p = false;
32655 id = cp_parser_id_expression (parser, /*template_p=*/false,
32656 /*check_dependency_p=*/true,
32657 /*template_p=*/NULL,
32658 /*declarator_p=*/false,
32659 /*optional_p=*/false);
32660 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32661 if (identifier_p (id))
32662 {
32663 const char *p = IDENTIFIER_POINTER (id);
32664
32665 if (strcmp (p, "min") == 0)
32666 code = MIN_EXPR;
32667 else if (strcmp (p, "max") == 0)
32668 code = MAX_EXPR;
32669 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32670 code = PLUS_EXPR;
32671 else if (id == ovl_op_identifier (false, MULT_EXPR))
32672 code = MULT_EXPR;
32673 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32674 code = MINUS_EXPR;
32675 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32676 code = BIT_AND_EXPR;
32677 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32678 code = BIT_IOR_EXPR;
32679 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32680 code = BIT_XOR_EXPR;
32681 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32682 code = TRUTH_ANDIF_EXPR;
32683 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32684 code = TRUTH_ORIF_EXPR;
32685 id = omp_reduction_id (code, id, NULL_TREE);
32686 tree scope = parser->scope;
32687 if (scope)
32688 id = build_qualified_name (NULL_TREE, scope, id, false);
32689 parser->scope = NULL_TREE;
32690 parser->qualifying_scope = NULL_TREE;
32691 parser->object_scope = NULL_TREE;
32692 }
32693 else
32694 {
32695 error ("invalid reduction-identifier");
32696 resync_fail:
32697 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32698 /*or_comma=*/false,
32699 /*consume_paren=*/true);
32700 return list;
32701 }
32702 }
32703
32704 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32705 goto resync_fail;
32706
32707 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32708 NULL);
32709 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32710 {
32711 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32712 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32713 }
32714
32715 return nlist;
32716 }
32717
32718 /* OpenMP 2.5:
32719 schedule ( schedule-kind )
32720 schedule ( schedule-kind , expression )
32721
32722 schedule-kind:
32723 static | dynamic | guided | runtime | auto
32724
32725 OpenMP 4.5:
32726 schedule ( schedule-modifier : schedule-kind )
32727 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32728
32729 schedule-modifier:
32730 simd
32731 monotonic
32732 nonmonotonic */
32733
32734 static tree
32735 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32736 {
32737 tree c, t;
32738 int modifiers = 0, nmodifiers = 0;
32739
32740 matching_parens parens;
32741 if (!parens.require_open (parser))
32742 return list;
32743
32744 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32745
32746 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32747 {
32748 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32749 const char *p = IDENTIFIER_POINTER (id);
32750 if (strcmp ("simd", p) == 0)
32751 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32752 else if (strcmp ("monotonic", p) == 0)
32753 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32754 else if (strcmp ("nonmonotonic", p) == 0)
32755 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32756 else
32757 break;
32758 cp_lexer_consume_token (parser->lexer);
32759 if (nmodifiers++ == 0
32760 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32761 cp_lexer_consume_token (parser->lexer);
32762 else
32763 {
32764 cp_parser_require (parser, CPP_COLON, RT_COLON);
32765 break;
32766 }
32767 }
32768
32769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32770 {
32771 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32772 const char *p = IDENTIFIER_POINTER (id);
32773
32774 switch (p[0])
32775 {
32776 case 'd':
32777 if (strcmp ("dynamic", p) != 0)
32778 goto invalid_kind;
32779 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32780 break;
32781
32782 case 'g':
32783 if (strcmp ("guided", p) != 0)
32784 goto invalid_kind;
32785 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32786 break;
32787
32788 case 'r':
32789 if (strcmp ("runtime", p) != 0)
32790 goto invalid_kind;
32791 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32792 break;
32793
32794 default:
32795 goto invalid_kind;
32796 }
32797 }
32798 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32799 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32800 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32801 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32802 else
32803 goto invalid_kind;
32804 cp_lexer_consume_token (parser->lexer);
32805
32806 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32807 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32808 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32809 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32810 {
32811 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32812 "specified");
32813 modifiers = 0;
32814 }
32815
32816 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32817 {
32818 cp_token *token;
32819 cp_lexer_consume_token (parser->lexer);
32820
32821 token = cp_lexer_peek_token (parser->lexer);
32822 t = cp_parser_assignment_expression (parser);
32823
32824 if (t == error_mark_node)
32825 goto resync_fail;
32826 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32827 error_at (token->location, "schedule %<runtime%> does not take "
32828 "a %<chunk_size%> parameter");
32829 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32830 error_at (token->location, "schedule %<auto%> does not take "
32831 "a %<chunk_size%> parameter");
32832 else
32833 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32834
32835 if (!parens.require_close (parser))
32836 goto resync_fail;
32837 }
32838 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32839 goto resync_fail;
32840
32841 OMP_CLAUSE_SCHEDULE_KIND (c)
32842 = (enum omp_clause_schedule_kind)
32843 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32844
32845 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32846 OMP_CLAUSE_CHAIN (c) = list;
32847 return c;
32848
32849 invalid_kind:
32850 cp_parser_error (parser, "invalid schedule kind");
32851 resync_fail:
32852 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32853 /*or_comma=*/false,
32854 /*consume_paren=*/true);
32855 return list;
32856 }
32857
32858 /* OpenMP 3.0:
32859 untied */
32860
32861 static tree
32862 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32863 tree list, location_t location)
32864 {
32865 tree c;
32866
32867 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32868
32869 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32870 OMP_CLAUSE_CHAIN (c) = list;
32871 return c;
32872 }
32873
32874 /* OpenMP 4.0:
32875 inbranch
32876 notinbranch */
32877
32878 static tree
32879 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32880 tree list, location_t location)
32881 {
32882 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32883 tree c = build_omp_clause (location, code);
32884 OMP_CLAUSE_CHAIN (c) = list;
32885 return c;
32886 }
32887
32888 /* OpenMP 4.0:
32889 parallel
32890 for
32891 sections
32892 taskgroup */
32893
32894 static tree
32895 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32896 enum omp_clause_code code,
32897 tree list, location_t location)
32898 {
32899 tree c = build_omp_clause (location, code);
32900 OMP_CLAUSE_CHAIN (c) = list;
32901 return c;
32902 }
32903
32904 /* OpenMP 4.5:
32905 nogroup */
32906
32907 static tree
32908 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32909 tree list, location_t location)
32910 {
32911 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32912 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32913 OMP_CLAUSE_CHAIN (c) = list;
32914 return c;
32915 }
32916
32917 /* OpenMP 4.5:
32918 simd
32919 threads */
32920
32921 static tree
32922 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32923 enum omp_clause_code code,
32924 tree list, location_t location)
32925 {
32926 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32927 tree c = build_omp_clause (location, code);
32928 OMP_CLAUSE_CHAIN (c) = list;
32929 return c;
32930 }
32931
32932 /* OpenMP 4.0:
32933 num_teams ( expression ) */
32934
32935 static tree
32936 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32937 location_t location)
32938 {
32939 tree t, c;
32940
32941 matching_parens parens;
32942 if (!parens.require_open (parser))
32943 return list;
32944
32945 t = cp_parser_expression (parser);
32946
32947 if (t == error_mark_node
32948 || !parens.require_close (parser))
32949 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32950 /*or_comma=*/false,
32951 /*consume_paren=*/true);
32952
32953 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32954 "num_teams", location);
32955
32956 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32957 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32958 OMP_CLAUSE_CHAIN (c) = list;
32959
32960 return c;
32961 }
32962
32963 /* OpenMP 4.0:
32964 thread_limit ( expression ) */
32965
32966 static tree
32967 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32968 location_t location)
32969 {
32970 tree t, c;
32971
32972 matching_parens parens;
32973 if (!parens.require_open (parser))
32974 return list;
32975
32976 t = cp_parser_expression (parser);
32977
32978 if (t == error_mark_node
32979 || !parens.require_close (parser))
32980 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32981 /*or_comma=*/false,
32982 /*consume_paren=*/true);
32983
32984 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32985 "thread_limit", location);
32986
32987 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32988 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32989 OMP_CLAUSE_CHAIN (c) = list;
32990
32991 return c;
32992 }
32993
32994 /* OpenMP 4.0:
32995 aligned ( variable-list )
32996 aligned ( variable-list : constant-expression ) */
32997
32998 static tree
32999 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33000 {
33001 tree nlist, c, alignment = NULL_TREE;
33002 bool colon;
33003
33004 matching_parens parens;
33005 if (!parens.require_open (parser))
33006 return list;
33007
33008 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33009 &colon);
33010
33011 if (colon)
33012 {
33013 alignment = cp_parser_constant_expression (parser);
33014
33015 if (!parens.require_close (parser))
33016 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33017 /*or_comma=*/false,
33018 /*consume_paren=*/true);
33019
33020 if (alignment == error_mark_node)
33021 alignment = NULL_TREE;
33022 }
33023
33024 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33025 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33026
33027 return nlist;
33028 }
33029
33030 /* OpenMP 4.0:
33031 linear ( variable-list )
33032 linear ( variable-list : expression )
33033
33034 OpenMP 4.5:
33035 linear ( modifier ( variable-list ) )
33036 linear ( modifier ( variable-list ) : expression ) */
33037
33038 static tree
33039 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33040 bool is_cilk_simd_fn, bool declare_simd)
33041 {
33042 tree nlist, c, step = integer_one_node;
33043 bool colon;
33044 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33045
33046 matching_parens parens;
33047 if (!parens.require_open (parser))
33048 return list;
33049
33050 if (!is_cilk_simd_fn
33051 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33052 {
33053 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33054 const char *p = IDENTIFIER_POINTER (id);
33055
33056 if (strcmp ("ref", p) == 0)
33057 kind = OMP_CLAUSE_LINEAR_REF;
33058 else if (strcmp ("val", p) == 0)
33059 kind = OMP_CLAUSE_LINEAR_VAL;
33060 else if (strcmp ("uval", p) == 0)
33061 kind = OMP_CLAUSE_LINEAR_UVAL;
33062 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33063 cp_lexer_consume_token (parser->lexer);
33064 else
33065 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33066 }
33067
33068 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33069 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33070 &colon);
33071 else
33072 {
33073 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33074 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33075 if (colon)
33076 cp_parser_require (parser, CPP_COLON, RT_COLON);
33077 else if (!parens.require_close (parser))
33078 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33079 /*or_comma=*/false,
33080 /*consume_paren=*/true);
33081 }
33082
33083 if (colon)
33084 {
33085 step = NULL_TREE;
33086 if (declare_simd
33087 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33088 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33089 {
33090 cp_token *token = cp_lexer_peek_token (parser->lexer);
33091 cp_parser_parse_tentatively (parser);
33092 step = cp_parser_id_expression (parser, /*template_p=*/false,
33093 /*check_dependency_p=*/true,
33094 /*template_p=*/NULL,
33095 /*declarator_p=*/false,
33096 /*optional_p=*/false);
33097 if (step != error_mark_node)
33098 step = cp_parser_lookup_name_simple (parser, step, token->location);
33099 if (step == error_mark_node)
33100 {
33101 step = NULL_TREE;
33102 cp_parser_abort_tentative_parse (parser);
33103 }
33104 else if (!cp_parser_parse_definitely (parser))
33105 step = NULL_TREE;
33106 }
33107 if (!step)
33108 step = cp_parser_expression (parser);
33109
33110 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
33111 {
33112 sorry ("using parameters for %<linear%> step is not supported yet");
33113 step = integer_one_node;
33114 }
33115 if (!parens.require_close (parser))
33116 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33117 /*or_comma=*/false,
33118 /*consume_paren=*/true);
33119
33120 if (step == error_mark_node)
33121 return list;
33122 }
33123
33124 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33125 {
33126 OMP_CLAUSE_LINEAR_STEP (c) = step;
33127 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33128 }
33129
33130 return nlist;
33131 }
33132
33133 /* OpenMP 4.0:
33134 safelen ( constant-expression ) */
33135
33136 static tree
33137 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33138 location_t location)
33139 {
33140 tree t, c;
33141
33142 matching_parens parens;
33143 if (!parens.require_open (parser))
33144 return list;
33145
33146 t = cp_parser_constant_expression (parser);
33147
33148 if (t == error_mark_node
33149 || !parens.require_close (parser))
33150 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33151 /*or_comma=*/false,
33152 /*consume_paren=*/true);
33153
33154 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33155
33156 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33157 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33158 OMP_CLAUSE_CHAIN (c) = list;
33159
33160 return c;
33161 }
33162
33163 /* OpenMP 4.0:
33164 simdlen ( constant-expression ) */
33165
33166 static tree
33167 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33168 location_t location)
33169 {
33170 tree t, c;
33171
33172 matching_parens parens;
33173 if (!parens.require_open (parser))
33174 return list;
33175
33176 t = cp_parser_constant_expression (parser);
33177
33178 if (t == error_mark_node
33179 || !parens.require_close (parser))
33180 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33181 /*or_comma=*/false,
33182 /*consume_paren=*/true);
33183
33184 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33185
33186 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33187 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33188 OMP_CLAUSE_CHAIN (c) = list;
33189
33190 return c;
33191 }
33192
33193 /* OpenMP 4.5:
33194 vec:
33195 identifier [+/- integer]
33196 vec , identifier [+/- integer]
33197 */
33198
33199 static tree
33200 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33201 tree list)
33202 {
33203 tree vec = NULL;
33204
33205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33206 {
33207 cp_parser_error (parser, "expected identifier");
33208 return list;
33209 }
33210
33211 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33212 {
33213 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33214 tree t, identifier = cp_parser_identifier (parser);
33215 tree addend = NULL;
33216
33217 if (identifier == error_mark_node)
33218 t = error_mark_node;
33219 else
33220 {
33221 t = cp_parser_lookup_name_simple
33222 (parser, identifier,
33223 cp_lexer_peek_token (parser->lexer)->location);
33224 if (t == error_mark_node)
33225 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33226 id_loc);
33227 }
33228
33229 bool neg = false;
33230 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33231 neg = true;
33232 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33233 {
33234 addend = integer_zero_node;
33235 goto add_to_vector;
33236 }
33237 cp_lexer_consume_token (parser->lexer);
33238
33239 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33240 {
33241 cp_parser_error (parser, "expected integer");
33242 return list;
33243 }
33244
33245 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33246 if (TREE_CODE (addend) != INTEGER_CST)
33247 {
33248 cp_parser_error (parser, "expected integer");
33249 return list;
33250 }
33251 cp_lexer_consume_token (parser->lexer);
33252
33253 add_to_vector:
33254 if (t != error_mark_node)
33255 {
33256 vec = tree_cons (addend, t, vec);
33257 if (neg)
33258 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33259 }
33260
33261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33262 break;
33263
33264 cp_lexer_consume_token (parser->lexer);
33265 }
33266
33267 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33268 {
33269 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33270 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33271 OMP_CLAUSE_DECL (u) = nreverse (vec);
33272 OMP_CLAUSE_CHAIN (u) = list;
33273 return u;
33274 }
33275 return list;
33276 }
33277
33278 /* OpenMP 4.0:
33279 depend ( depend-kind : variable-list )
33280
33281 depend-kind:
33282 in | out | inout
33283
33284 OpenMP 4.5:
33285 depend ( source )
33286
33287 depend ( sink : vec ) */
33288
33289 static tree
33290 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33291 {
33292 tree nlist, c;
33293 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33294
33295 matching_parens parens;
33296 if (!parens.require_open (parser))
33297 return list;
33298
33299 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33300 {
33301 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33302 const char *p = IDENTIFIER_POINTER (id);
33303
33304 if (strcmp ("in", p) == 0)
33305 kind = OMP_CLAUSE_DEPEND_IN;
33306 else if (strcmp ("inout", p) == 0)
33307 kind = OMP_CLAUSE_DEPEND_INOUT;
33308 else if (strcmp ("out", p) == 0)
33309 kind = OMP_CLAUSE_DEPEND_OUT;
33310 else if (strcmp ("source", p) == 0)
33311 kind = OMP_CLAUSE_DEPEND_SOURCE;
33312 else if (strcmp ("sink", p) == 0)
33313 kind = OMP_CLAUSE_DEPEND_SINK;
33314 else
33315 goto invalid_kind;
33316 }
33317 else
33318 goto invalid_kind;
33319
33320 cp_lexer_consume_token (parser->lexer);
33321
33322 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33323 {
33324 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33325 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33326 OMP_CLAUSE_DECL (c) = NULL_TREE;
33327 OMP_CLAUSE_CHAIN (c) = list;
33328 if (!parens.require_close (parser))
33329 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33330 /*or_comma=*/false,
33331 /*consume_paren=*/true);
33332 return c;
33333 }
33334
33335 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33336 goto resync_fail;
33337
33338 if (kind == OMP_CLAUSE_DEPEND_SINK)
33339 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33340 else
33341 {
33342 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33343 list, NULL);
33344
33345 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33346 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33347 }
33348 return nlist;
33349
33350 invalid_kind:
33351 cp_parser_error (parser, "invalid depend kind");
33352 resync_fail:
33353 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33354 /*or_comma=*/false,
33355 /*consume_paren=*/true);
33356 return list;
33357 }
33358
33359 /* OpenMP 4.0:
33360 map ( map-kind : variable-list )
33361 map ( variable-list )
33362
33363 map-kind:
33364 alloc | to | from | tofrom
33365
33366 OpenMP 4.5:
33367 map-kind:
33368 alloc | to | from | tofrom | release | delete
33369
33370 map ( always [,] map-kind: variable-list ) */
33371
33372 static tree
33373 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33374 {
33375 tree nlist, c;
33376 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33377 bool always = false;
33378
33379 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33380 return list;
33381
33382 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33383 {
33384 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33385 const char *p = IDENTIFIER_POINTER (id);
33386
33387 if (strcmp ("always", p) == 0)
33388 {
33389 int nth = 2;
33390 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33391 nth++;
33392 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33393 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33394 == RID_DELETE))
33395 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33396 == CPP_COLON))
33397 {
33398 always = true;
33399 cp_lexer_consume_token (parser->lexer);
33400 if (nth == 3)
33401 cp_lexer_consume_token (parser->lexer);
33402 }
33403 }
33404 }
33405
33406 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33407 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33408 {
33409 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33410 const char *p = IDENTIFIER_POINTER (id);
33411
33412 if (strcmp ("alloc", p) == 0)
33413 kind = GOMP_MAP_ALLOC;
33414 else if (strcmp ("to", p) == 0)
33415 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33416 else if (strcmp ("from", p) == 0)
33417 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33418 else if (strcmp ("tofrom", p) == 0)
33419 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33420 else if (strcmp ("release", p) == 0)
33421 kind = GOMP_MAP_RELEASE;
33422 else
33423 {
33424 cp_parser_error (parser, "invalid map kind");
33425 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33426 /*or_comma=*/false,
33427 /*consume_paren=*/true);
33428 return list;
33429 }
33430 cp_lexer_consume_token (parser->lexer);
33431 cp_lexer_consume_token (parser->lexer);
33432 }
33433 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33434 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33435 {
33436 kind = GOMP_MAP_DELETE;
33437 cp_lexer_consume_token (parser->lexer);
33438 cp_lexer_consume_token (parser->lexer);
33439 }
33440
33441 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33442 NULL);
33443
33444 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33445 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33446
33447 return nlist;
33448 }
33449
33450 /* OpenMP 4.0:
33451 device ( expression ) */
33452
33453 static tree
33454 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33455 location_t location)
33456 {
33457 tree t, c;
33458
33459 matching_parens parens;
33460 if (!parens.require_open (parser))
33461 return list;
33462
33463 t = cp_parser_expression (parser);
33464
33465 if (t == error_mark_node
33466 || !parens.require_close (parser))
33467 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33468 /*or_comma=*/false,
33469 /*consume_paren=*/true);
33470
33471 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33472 "device", location);
33473
33474 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33475 OMP_CLAUSE_DEVICE_ID (c) = t;
33476 OMP_CLAUSE_CHAIN (c) = list;
33477
33478 return c;
33479 }
33480
33481 /* OpenMP 4.0:
33482 dist_schedule ( static )
33483 dist_schedule ( static , expression ) */
33484
33485 static tree
33486 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33487 location_t location)
33488 {
33489 tree c, t;
33490
33491 matching_parens parens;
33492 if (!parens.require_open (parser))
33493 return list;
33494
33495 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33496
33497 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33498 goto invalid_kind;
33499 cp_lexer_consume_token (parser->lexer);
33500
33501 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33502 {
33503 cp_lexer_consume_token (parser->lexer);
33504
33505 t = cp_parser_assignment_expression (parser);
33506
33507 if (t == error_mark_node)
33508 goto resync_fail;
33509 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33510
33511 if (!parens.require_close (parser))
33512 goto resync_fail;
33513 }
33514 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33515 goto resync_fail;
33516
33517 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33518 location);
33519 OMP_CLAUSE_CHAIN (c) = list;
33520 return c;
33521
33522 invalid_kind:
33523 cp_parser_error (parser, "invalid dist_schedule kind");
33524 resync_fail:
33525 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33526 /*or_comma=*/false,
33527 /*consume_paren=*/true);
33528 return list;
33529 }
33530
33531 /* OpenMP 4.0:
33532 proc_bind ( proc-bind-kind )
33533
33534 proc-bind-kind:
33535 master | close | spread */
33536
33537 static tree
33538 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33539 location_t location)
33540 {
33541 tree c;
33542 enum omp_clause_proc_bind_kind kind;
33543
33544 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33545 return list;
33546
33547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33548 {
33549 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33550 const char *p = IDENTIFIER_POINTER (id);
33551
33552 if (strcmp ("master", p) == 0)
33553 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33554 else if (strcmp ("close", p) == 0)
33555 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33556 else if (strcmp ("spread", p) == 0)
33557 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33558 else
33559 goto invalid_kind;
33560 }
33561 else
33562 goto invalid_kind;
33563
33564 cp_lexer_consume_token (parser->lexer);
33565 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33566 goto resync_fail;
33567
33568 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33569 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33570 location);
33571 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33572 OMP_CLAUSE_CHAIN (c) = list;
33573 return c;
33574
33575 invalid_kind:
33576 cp_parser_error (parser, "invalid depend kind");
33577 resync_fail:
33578 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33579 /*or_comma=*/false,
33580 /*consume_paren=*/true);
33581 return list;
33582 }
33583
33584 /* OpenACC:
33585 async [( int-expr )] */
33586
33587 static tree
33588 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33589 {
33590 tree c, t;
33591 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33592
33593 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33594
33595 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33596 {
33597 matching_parens parens;
33598 parens.consume_open (parser);
33599
33600 t = cp_parser_expression (parser);
33601 if (t == error_mark_node
33602 || !parens.require_close (parser))
33603 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33604 /*or_comma=*/false,
33605 /*consume_paren=*/true);
33606 }
33607
33608 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33609
33610 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33611 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33612 OMP_CLAUSE_CHAIN (c) = list;
33613 list = c;
33614
33615 return list;
33616 }
33617
33618 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33619 is a bitmask in MASK. Return the list of clauses found. */
33620
33621 static tree
33622 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33623 const char *where, cp_token *pragma_tok,
33624 bool finish_p = true)
33625 {
33626 tree clauses = NULL;
33627 bool first = true;
33628
33629 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33630 {
33631 location_t here;
33632 pragma_omp_clause c_kind;
33633 omp_clause_code code;
33634 const char *c_name;
33635 tree prev = clauses;
33636
33637 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33638 cp_lexer_consume_token (parser->lexer);
33639
33640 here = cp_lexer_peek_token (parser->lexer)->location;
33641 c_kind = cp_parser_omp_clause_name (parser);
33642
33643 switch (c_kind)
33644 {
33645 case PRAGMA_OACC_CLAUSE_ASYNC:
33646 clauses = cp_parser_oacc_clause_async (parser, clauses);
33647 c_name = "async";
33648 break;
33649 case PRAGMA_OACC_CLAUSE_AUTO:
33650 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33651 clauses, here);
33652 c_name = "auto";
33653 break;
33654 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33655 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33656 c_name = "collapse";
33657 break;
33658 case PRAGMA_OACC_CLAUSE_COPY:
33659 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33660 c_name = "copy";
33661 break;
33662 case PRAGMA_OACC_CLAUSE_COPYIN:
33663 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33664 c_name = "copyin";
33665 break;
33666 case PRAGMA_OACC_CLAUSE_COPYOUT:
33667 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33668 c_name = "copyout";
33669 break;
33670 case PRAGMA_OACC_CLAUSE_CREATE:
33671 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33672 c_name = "create";
33673 break;
33674 case PRAGMA_OACC_CLAUSE_DELETE:
33675 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33676 c_name = "delete";
33677 break;
33678 case PRAGMA_OMP_CLAUSE_DEFAULT:
33679 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33680 c_name = "default";
33681 break;
33682 case PRAGMA_OACC_CLAUSE_DEVICE:
33683 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33684 c_name = "device";
33685 break;
33686 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33687 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33688 c_name = "deviceptr";
33689 break;
33690 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33691 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33692 c_name = "device_resident";
33693 break;
33694 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33695 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33696 clauses);
33697 c_name = "firstprivate";
33698 break;
33699 case PRAGMA_OACC_CLAUSE_GANG:
33700 c_name = "gang";
33701 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33702 c_name, clauses);
33703 break;
33704 case PRAGMA_OACC_CLAUSE_HOST:
33705 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33706 c_name = "host";
33707 break;
33708 case PRAGMA_OACC_CLAUSE_IF:
33709 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33710 c_name = "if";
33711 break;
33712 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33713 clauses = cp_parser_oacc_simple_clause (parser,
33714 OMP_CLAUSE_INDEPENDENT,
33715 clauses, here);
33716 c_name = "independent";
33717 break;
33718 case PRAGMA_OACC_CLAUSE_LINK:
33719 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33720 c_name = "link";
33721 break;
33722 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33723 code = OMP_CLAUSE_NUM_GANGS;
33724 c_name = "num_gangs";
33725 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33726 clauses);
33727 break;
33728 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33729 c_name = "num_workers";
33730 code = OMP_CLAUSE_NUM_WORKERS;
33731 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33732 clauses);
33733 break;
33734 case PRAGMA_OACC_CLAUSE_PRESENT:
33735 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33736 c_name = "present";
33737 break;
33738 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33739 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33740 c_name = "present_or_copy";
33741 break;
33742 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33743 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33744 c_name = "present_or_copyin";
33745 break;
33746 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33747 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33748 c_name = "present_or_copyout";
33749 break;
33750 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33751 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33752 c_name = "present_or_create";
33753 break;
33754 case PRAGMA_OACC_CLAUSE_PRIVATE:
33755 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33756 clauses);
33757 c_name = "private";
33758 break;
33759 case PRAGMA_OACC_CLAUSE_REDUCTION:
33760 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33761 c_name = "reduction";
33762 break;
33763 case PRAGMA_OACC_CLAUSE_SELF:
33764 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33765 c_name = "self";
33766 break;
33767 case PRAGMA_OACC_CLAUSE_SEQ:
33768 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33769 clauses, here);
33770 c_name = "seq";
33771 break;
33772 case PRAGMA_OACC_CLAUSE_TILE:
33773 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33774 c_name = "tile";
33775 break;
33776 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33777 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33778 clauses);
33779 c_name = "use_device";
33780 break;
33781 case PRAGMA_OACC_CLAUSE_VECTOR:
33782 c_name = "vector";
33783 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33784 c_name, clauses);
33785 break;
33786 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33787 c_name = "vector_length";
33788 code = OMP_CLAUSE_VECTOR_LENGTH;
33789 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33790 clauses);
33791 break;
33792 case PRAGMA_OACC_CLAUSE_WAIT:
33793 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33794 c_name = "wait";
33795 break;
33796 case PRAGMA_OACC_CLAUSE_WORKER:
33797 c_name = "worker";
33798 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33799 c_name, clauses);
33800 break;
33801 default:
33802 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33803 goto saw_error;
33804 }
33805
33806 first = false;
33807
33808 if (((mask >> c_kind) & 1) == 0)
33809 {
33810 /* Remove the invalid clause(s) from the list to avoid
33811 confusing the rest of the compiler. */
33812 clauses = prev;
33813 error_at (here, "%qs is not valid for %qs", c_name, where);
33814 }
33815 }
33816
33817 saw_error:
33818 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33819
33820 if (finish_p)
33821 return finish_omp_clauses (clauses, C_ORT_ACC);
33822
33823 return clauses;
33824 }
33825
33826 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33827 is a bitmask in MASK. Return the list of clauses found; the result
33828 of clause default goes in *pdefault. */
33829
33830 static tree
33831 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33832 const char *where, cp_token *pragma_tok,
33833 bool finish_p = true)
33834 {
33835 tree clauses = NULL;
33836 bool first = true;
33837 cp_token *token = NULL;
33838
33839 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33840 {
33841 pragma_omp_clause c_kind;
33842 const char *c_name;
33843 tree prev = clauses;
33844
33845 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33846 cp_lexer_consume_token (parser->lexer);
33847
33848 token = cp_lexer_peek_token (parser->lexer);
33849 c_kind = cp_parser_omp_clause_name (parser);
33850
33851 switch (c_kind)
33852 {
33853 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33854 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33855 token->location);
33856 c_name = "collapse";
33857 break;
33858 case PRAGMA_OMP_CLAUSE_COPYIN:
33859 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33860 c_name = "copyin";
33861 break;
33862 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33863 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33864 clauses);
33865 c_name = "copyprivate";
33866 break;
33867 case PRAGMA_OMP_CLAUSE_DEFAULT:
33868 clauses = cp_parser_omp_clause_default (parser, clauses,
33869 token->location, false);
33870 c_name = "default";
33871 break;
33872 case PRAGMA_OMP_CLAUSE_FINAL:
33873 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33874 c_name = "final";
33875 break;
33876 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33877 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33878 clauses);
33879 c_name = "firstprivate";
33880 break;
33881 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33882 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33883 token->location);
33884 c_name = "grainsize";
33885 break;
33886 case PRAGMA_OMP_CLAUSE_HINT:
33887 clauses = cp_parser_omp_clause_hint (parser, clauses,
33888 token->location);
33889 c_name = "hint";
33890 break;
33891 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33892 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33893 token->location);
33894 c_name = "defaultmap";
33895 break;
33896 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33897 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33898 clauses);
33899 c_name = "use_device_ptr";
33900 break;
33901 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33902 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33903 clauses);
33904 c_name = "is_device_ptr";
33905 break;
33906 case PRAGMA_OMP_CLAUSE_IF:
33907 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33908 true);
33909 c_name = "if";
33910 break;
33911 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33912 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33913 clauses);
33914 c_name = "lastprivate";
33915 break;
33916 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33917 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33918 token->location);
33919 c_name = "mergeable";
33920 break;
33921 case PRAGMA_OMP_CLAUSE_NOWAIT:
33922 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33923 c_name = "nowait";
33924 break;
33925 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33926 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33927 token->location);
33928 c_name = "num_tasks";
33929 break;
33930 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33931 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33932 token->location);
33933 c_name = "num_threads";
33934 break;
33935 case PRAGMA_OMP_CLAUSE_ORDERED:
33936 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33937 token->location);
33938 c_name = "ordered";
33939 break;
33940 case PRAGMA_OMP_CLAUSE_PRIORITY:
33941 clauses = cp_parser_omp_clause_priority (parser, clauses,
33942 token->location);
33943 c_name = "priority";
33944 break;
33945 case PRAGMA_OMP_CLAUSE_PRIVATE:
33946 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33947 clauses);
33948 c_name = "private";
33949 break;
33950 case PRAGMA_OMP_CLAUSE_REDUCTION:
33951 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33952 c_name = "reduction";
33953 break;
33954 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33955 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33956 token->location);
33957 c_name = "schedule";
33958 break;
33959 case PRAGMA_OMP_CLAUSE_SHARED:
33960 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33961 clauses);
33962 c_name = "shared";
33963 break;
33964 case PRAGMA_OMP_CLAUSE_UNTIED:
33965 clauses = cp_parser_omp_clause_untied (parser, clauses,
33966 token->location);
33967 c_name = "untied";
33968 break;
33969 case PRAGMA_OMP_CLAUSE_INBRANCH:
33970 case PRAGMA_CILK_CLAUSE_MASK:
33971 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33972 clauses, token->location);
33973 c_name = "inbranch";
33974 break;
33975 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33976 case PRAGMA_CILK_CLAUSE_NOMASK:
33977 clauses = cp_parser_omp_clause_branch (parser,
33978 OMP_CLAUSE_NOTINBRANCH,
33979 clauses, token->location);
33980 c_name = "notinbranch";
33981 break;
33982 case PRAGMA_OMP_CLAUSE_PARALLEL:
33983 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33984 clauses, token->location);
33985 c_name = "parallel";
33986 if (!first)
33987 {
33988 clause_not_first:
33989 error_at (token->location, "%qs must be the first clause of %qs",
33990 c_name, where);
33991 clauses = prev;
33992 }
33993 break;
33994 case PRAGMA_OMP_CLAUSE_FOR:
33995 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33996 clauses, token->location);
33997 c_name = "for";
33998 if (!first)
33999 goto clause_not_first;
34000 break;
34001 case PRAGMA_OMP_CLAUSE_SECTIONS:
34002 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34003 clauses, token->location);
34004 c_name = "sections";
34005 if (!first)
34006 goto clause_not_first;
34007 break;
34008 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34009 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34010 clauses, token->location);
34011 c_name = "taskgroup";
34012 if (!first)
34013 goto clause_not_first;
34014 break;
34015 case PRAGMA_OMP_CLAUSE_LINK:
34016 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34017 c_name = "to";
34018 break;
34019 case PRAGMA_OMP_CLAUSE_TO:
34020 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34021 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34022 clauses);
34023 else
34024 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34025 c_name = "to";
34026 break;
34027 case PRAGMA_OMP_CLAUSE_FROM:
34028 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34029 c_name = "from";
34030 break;
34031 case PRAGMA_OMP_CLAUSE_UNIFORM:
34032 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34033 clauses);
34034 c_name = "uniform";
34035 break;
34036 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34037 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34038 token->location);
34039 c_name = "num_teams";
34040 break;
34041 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34042 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34043 token->location);
34044 c_name = "thread_limit";
34045 break;
34046 case PRAGMA_OMP_CLAUSE_ALIGNED:
34047 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34048 c_name = "aligned";
34049 break;
34050 case PRAGMA_OMP_CLAUSE_LINEAR:
34051 {
34052 bool cilk_simd_fn = false, declare_simd = false;
34053 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
34054 cilk_simd_fn = true;
34055 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34056 declare_simd = true;
34057 clauses = cp_parser_omp_clause_linear (parser, clauses,
34058 cilk_simd_fn, declare_simd);
34059 }
34060 c_name = "linear";
34061 break;
34062 case PRAGMA_OMP_CLAUSE_DEPEND:
34063 clauses = cp_parser_omp_clause_depend (parser, clauses,
34064 token->location);
34065 c_name = "depend";
34066 break;
34067 case PRAGMA_OMP_CLAUSE_MAP:
34068 clauses = cp_parser_omp_clause_map (parser, clauses);
34069 c_name = "map";
34070 break;
34071 case PRAGMA_OMP_CLAUSE_DEVICE:
34072 clauses = cp_parser_omp_clause_device (parser, clauses,
34073 token->location);
34074 c_name = "device";
34075 break;
34076 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34077 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34078 token->location);
34079 c_name = "dist_schedule";
34080 break;
34081 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34082 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34083 token->location);
34084 c_name = "proc_bind";
34085 break;
34086 case PRAGMA_OMP_CLAUSE_SAFELEN:
34087 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34088 token->location);
34089 c_name = "safelen";
34090 break;
34091 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34092 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34093 token->location);
34094 c_name = "simdlen";
34095 break;
34096 case PRAGMA_OMP_CLAUSE_NOGROUP:
34097 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34098 token->location);
34099 c_name = "nogroup";
34100 break;
34101 case PRAGMA_OMP_CLAUSE_THREADS:
34102 clauses
34103 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34104 clauses, token->location);
34105 c_name = "threads";
34106 break;
34107 case PRAGMA_OMP_CLAUSE_SIMD:
34108 clauses
34109 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34110 clauses, token->location);
34111 c_name = "simd";
34112 break;
34113 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
34114 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
34115 c_name = "simdlen";
34116 break;
34117 default:
34118 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34119 goto saw_error;
34120 }
34121
34122 first = false;
34123
34124 if (((mask >> c_kind) & 1) == 0)
34125 {
34126 /* Remove the invalid clause(s) from the list to avoid
34127 confusing the rest of the compiler. */
34128 clauses = prev;
34129 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34130 }
34131 }
34132 saw_error:
34133 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
34134 no reason to skip to the end. */
34135 if (!(flag_cilkplus && pragma_tok == NULL))
34136 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34137 if (finish_p)
34138 {
34139 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34140 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34141 else
34142 return finish_omp_clauses (clauses, C_ORT_OMP);
34143 }
34144 return clauses;
34145 }
34146
34147 /* OpenMP 2.5:
34148 structured-block:
34149 statement
34150
34151 In practice, we're also interested in adding the statement to an
34152 outer node. So it is convenient if we work around the fact that
34153 cp_parser_statement calls add_stmt. */
34154
34155 static unsigned
34156 cp_parser_begin_omp_structured_block (cp_parser *parser)
34157 {
34158 unsigned save = parser->in_statement;
34159
34160 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34161 This preserves the "not within loop or switch" style error messages
34162 for nonsense cases like
34163 void foo() {
34164 #pragma omp single
34165 break;
34166 }
34167 */
34168 if (parser->in_statement)
34169 parser->in_statement = IN_OMP_BLOCK;
34170
34171 return save;
34172 }
34173
34174 static void
34175 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34176 {
34177 parser->in_statement = save;
34178 }
34179
34180 static tree
34181 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34182 {
34183 tree stmt = begin_omp_structured_block ();
34184 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34185
34186 cp_parser_statement (parser, NULL_TREE, false, if_p);
34187
34188 cp_parser_end_omp_structured_block (parser, save);
34189 return finish_omp_structured_block (stmt);
34190 }
34191
34192 /* OpenMP 2.5:
34193 # pragma omp atomic new-line
34194 expression-stmt
34195
34196 expression-stmt:
34197 x binop= expr | x++ | ++x | x-- | --x
34198 binop:
34199 +, *, -, /, &, ^, |, <<, >>
34200
34201 where x is an lvalue expression with scalar type.
34202
34203 OpenMP 3.1:
34204 # pragma omp atomic new-line
34205 update-stmt
34206
34207 # pragma omp atomic read new-line
34208 read-stmt
34209
34210 # pragma omp atomic write new-line
34211 write-stmt
34212
34213 # pragma omp atomic update new-line
34214 update-stmt
34215
34216 # pragma omp atomic capture new-line
34217 capture-stmt
34218
34219 # pragma omp atomic capture new-line
34220 capture-block
34221
34222 read-stmt:
34223 v = x
34224 write-stmt:
34225 x = expr
34226 update-stmt:
34227 expression-stmt | x = x binop expr
34228 capture-stmt:
34229 v = expression-stmt
34230 capture-block:
34231 { v = x; update-stmt; } | { update-stmt; v = x; }
34232
34233 OpenMP 4.0:
34234 update-stmt:
34235 expression-stmt | x = x binop expr | x = expr binop x
34236 capture-stmt:
34237 v = update-stmt
34238 capture-block:
34239 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34240
34241 where x and v are lvalue expressions with scalar type. */
34242
34243 static void
34244 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34245 {
34246 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34247 tree rhs1 = NULL_TREE, orig_lhs;
34248 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34249 bool structured_block = false;
34250 bool seq_cst = false;
34251
34252 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34253 {
34254 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34255 const char *p = IDENTIFIER_POINTER (id);
34256
34257 if (!strcmp (p, "seq_cst"))
34258 {
34259 seq_cst = true;
34260 cp_lexer_consume_token (parser->lexer);
34261 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34262 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34263 cp_lexer_consume_token (parser->lexer);
34264 }
34265 }
34266 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34267 {
34268 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34269 const char *p = IDENTIFIER_POINTER (id);
34270
34271 if (!strcmp (p, "read"))
34272 code = OMP_ATOMIC_READ;
34273 else if (!strcmp (p, "write"))
34274 code = NOP_EXPR;
34275 else if (!strcmp (p, "update"))
34276 code = OMP_ATOMIC;
34277 else if (!strcmp (p, "capture"))
34278 code = OMP_ATOMIC_CAPTURE_NEW;
34279 else
34280 p = NULL;
34281 if (p)
34282 cp_lexer_consume_token (parser->lexer);
34283 }
34284 if (!seq_cst)
34285 {
34286 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34287 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34288 cp_lexer_consume_token (parser->lexer);
34289
34290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34291 {
34292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34293 const char *p = IDENTIFIER_POINTER (id);
34294
34295 if (!strcmp (p, "seq_cst"))
34296 {
34297 seq_cst = true;
34298 cp_lexer_consume_token (parser->lexer);
34299 }
34300 }
34301 }
34302 cp_parser_require_pragma_eol (parser, pragma_tok);
34303
34304 switch (code)
34305 {
34306 case OMP_ATOMIC_READ:
34307 case NOP_EXPR: /* atomic write */
34308 v = cp_parser_unary_expression (parser);
34309 if (v == error_mark_node)
34310 goto saw_error;
34311 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34312 goto saw_error;
34313 if (code == NOP_EXPR)
34314 lhs = cp_parser_expression (parser);
34315 else
34316 lhs = cp_parser_unary_expression (parser);
34317 if (lhs == error_mark_node)
34318 goto saw_error;
34319 if (code == NOP_EXPR)
34320 {
34321 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34322 opcode. */
34323 code = OMP_ATOMIC;
34324 rhs = lhs;
34325 lhs = v;
34326 v = NULL_TREE;
34327 }
34328 goto done;
34329 case OMP_ATOMIC_CAPTURE_NEW:
34330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34331 {
34332 cp_lexer_consume_token (parser->lexer);
34333 structured_block = true;
34334 }
34335 else
34336 {
34337 v = cp_parser_unary_expression (parser);
34338 if (v == error_mark_node)
34339 goto saw_error;
34340 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34341 goto saw_error;
34342 }
34343 default:
34344 break;
34345 }
34346
34347 restart:
34348 lhs = cp_parser_unary_expression (parser);
34349 orig_lhs = lhs;
34350 switch (TREE_CODE (lhs))
34351 {
34352 case ERROR_MARK:
34353 goto saw_error;
34354
34355 case POSTINCREMENT_EXPR:
34356 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34357 code = OMP_ATOMIC_CAPTURE_OLD;
34358 /* FALLTHROUGH */
34359 case PREINCREMENT_EXPR:
34360 lhs = TREE_OPERAND (lhs, 0);
34361 opcode = PLUS_EXPR;
34362 rhs = integer_one_node;
34363 break;
34364
34365 case POSTDECREMENT_EXPR:
34366 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34367 code = OMP_ATOMIC_CAPTURE_OLD;
34368 /* FALLTHROUGH */
34369 case PREDECREMENT_EXPR:
34370 lhs = TREE_OPERAND (lhs, 0);
34371 opcode = MINUS_EXPR;
34372 rhs = integer_one_node;
34373 break;
34374
34375 case COMPOUND_EXPR:
34376 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34377 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34378 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34379 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34380 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34381 (TREE_OPERAND (lhs, 1), 0), 0)))
34382 == BOOLEAN_TYPE)
34383 /* Undo effects of boolean_increment for post {in,de}crement. */
34384 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34385 /* FALLTHRU */
34386 case MODIFY_EXPR:
34387 if (TREE_CODE (lhs) == MODIFY_EXPR
34388 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34389 {
34390 /* Undo effects of boolean_increment. */
34391 if (integer_onep (TREE_OPERAND (lhs, 1)))
34392 {
34393 /* This is pre or post increment. */
34394 rhs = TREE_OPERAND (lhs, 1);
34395 lhs = TREE_OPERAND (lhs, 0);
34396 opcode = NOP_EXPR;
34397 if (code == OMP_ATOMIC_CAPTURE_NEW
34398 && !structured_block
34399 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34400 code = OMP_ATOMIC_CAPTURE_OLD;
34401 break;
34402 }
34403 }
34404 /* FALLTHRU */
34405 default:
34406 switch (cp_lexer_peek_token (parser->lexer)->type)
34407 {
34408 case CPP_MULT_EQ:
34409 opcode = MULT_EXPR;
34410 break;
34411 case CPP_DIV_EQ:
34412 opcode = TRUNC_DIV_EXPR;
34413 break;
34414 case CPP_PLUS_EQ:
34415 opcode = PLUS_EXPR;
34416 break;
34417 case CPP_MINUS_EQ:
34418 opcode = MINUS_EXPR;
34419 break;
34420 case CPP_LSHIFT_EQ:
34421 opcode = LSHIFT_EXPR;
34422 break;
34423 case CPP_RSHIFT_EQ:
34424 opcode = RSHIFT_EXPR;
34425 break;
34426 case CPP_AND_EQ:
34427 opcode = BIT_AND_EXPR;
34428 break;
34429 case CPP_OR_EQ:
34430 opcode = BIT_IOR_EXPR;
34431 break;
34432 case CPP_XOR_EQ:
34433 opcode = BIT_XOR_EXPR;
34434 break;
34435 case CPP_EQ:
34436 enum cp_parser_prec oprec;
34437 cp_token *token;
34438 cp_lexer_consume_token (parser->lexer);
34439 cp_parser_parse_tentatively (parser);
34440 rhs1 = cp_parser_simple_cast_expression (parser);
34441 if (rhs1 == error_mark_node)
34442 {
34443 cp_parser_abort_tentative_parse (parser);
34444 cp_parser_simple_cast_expression (parser);
34445 goto saw_error;
34446 }
34447 token = cp_lexer_peek_token (parser->lexer);
34448 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34449 {
34450 cp_parser_abort_tentative_parse (parser);
34451 cp_parser_parse_tentatively (parser);
34452 rhs = cp_parser_binary_expression (parser, false, true,
34453 PREC_NOT_OPERATOR, NULL);
34454 if (rhs == error_mark_node)
34455 {
34456 cp_parser_abort_tentative_parse (parser);
34457 cp_parser_binary_expression (parser, false, true,
34458 PREC_NOT_OPERATOR, NULL);
34459 goto saw_error;
34460 }
34461 switch (TREE_CODE (rhs))
34462 {
34463 case MULT_EXPR:
34464 case TRUNC_DIV_EXPR:
34465 case RDIV_EXPR:
34466 case PLUS_EXPR:
34467 case MINUS_EXPR:
34468 case LSHIFT_EXPR:
34469 case RSHIFT_EXPR:
34470 case BIT_AND_EXPR:
34471 case BIT_IOR_EXPR:
34472 case BIT_XOR_EXPR:
34473 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34474 {
34475 if (cp_parser_parse_definitely (parser))
34476 {
34477 opcode = TREE_CODE (rhs);
34478 rhs1 = TREE_OPERAND (rhs, 0);
34479 rhs = TREE_OPERAND (rhs, 1);
34480 goto stmt_done;
34481 }
34482 else
34483 goto saw_error;
34484 }
34485 break;
34486 default:
34487 break;
34488 }
34489 cp_parser_abort_tentative_parse (parser);
34490 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34491 {
34492 rhs = cp_parser_expression (parser);
34493 if (rhs == error_mark_node)
34494 goto saw_error;
34495 opcode = NOP_EXPR;
34496 rhs1 = NULL_TREE;
34497 goto stmt_done;
34498 }
34499 cp_parser_error (parser,
34500 "invalid form of %<#pragma omp atomic%>");
34501 goto saw_error;
34502 }
34503 if (!cp_parser_parse_definitely (parser))
34504 goto saw_error;
34505 switch (token->type)
34506 {
34507 case CPP_SEMICOLON:
34508 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34509 {
34510 code = OMP_ATOMIC_CAPTURE_OLD;
34511 v = lhs;
34512 lhs = NULL_TREE;
34513 lhs1 = rhs1;
34514 rhs1 = NULL_TREE;
34515 cp_lexer_consume_token (parser->lexer);
34516 goto restart;
34517 }
34518 else if (structured_block)
34519 {
34520 opcode = NOP_EXPR;
34521 rhs = rhs1;
34522 rhs1 = NULL_TREE;
34523 goto stmt_done;
34524 }
34525 cp_parser_error (parser,
34526 "invalid form of %<#pragma omp atomic%>");
34527 goto saw_error;
34528 case CPP_MULT:
34529 opcode = MULT_EXPR;
34530 break;
34531 case CPP_DIV:
34532 opcode = TRUNC_DIV_EXPR;
34533 break;
34534 case CPP_PLUS:
34535 opcode = PLUS_EXPR;
34536 break;
34537 case CPP_MINUS:
34538 opcode = MINUS_EXPR;
34539 break;
34540 case CPP_LSHIFT:
34541 opcode = LSHIFT_EXPR;
34542 break;
34543 case CPP_RSHIFT:
34544 opcode = RSHIFT_EXPR;
34545 break;
34546 case CPP_AND:
34547 opcode = BIT_AND_EXPR;
34548 break;
34549 case CPP_OR:
34550 opcode = BIT_IOR_EXPR;
34551 break;
34552 case CPP_XOR:
34553 opcode = BIT_XOR_EXPR;
34554 break;
34555 default:
34556 cp_parser_error (parser,
34557 "invalid operator for %<#pragma omp atomic%>");
34558 goto saw_error;
34559 }
34560 oprec = TOKEN_PRECEDENCE (token);
34561 gcc_assert (oprec != PREC_NOT_OPERATOR);
34562 if (commutative_tree_code (opcode))
34563 oprec = (enum cp_parser_prec) (oprec - 1);
34564 cp_lexer_consume_token (parser->lexer);
34565 rhs = cp_parser_binary_expression (parser, false, false,
34566 oprec, NULL);
34567 if (rhs == error_mark_node)
34568 goto saw_error;
34569 goto stmt_done;
34570 /* FALLTHROUGH */
34571 default:
34572 cp_parser_error (parser,
34573 "invalid operator for %<#pragma omp atomic%>");
34574 goto saw_error;
34575 }
34576 cp_lexer_consume_token (parser->lexer);
34577
34578 rhs = cp_parser_expression (parser);
34579 if (rhs == error_mark_node)
34580 goto saw_error;
34581 break;
34582 }
34583 stmt_done:
34584 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34585 {
34586 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34587 goto saw_error;
34588 v = cp_parser_unary_expression (parser);
34589 if (v == error_mark_node)
34590 goto saw_error;
34591 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34592 goto saw_error;
34593 lhs1 = cp_parser_unary_expression (parser);
34594 if (lhs1 == error_mark_node)
34595 goto saw_error;
34596 }
34597 if (structured_block)
34598 {
34599 cp_parser_consume_semicolon_at_end_of_statement (parser);
34600 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34601 }
34602 done:
34603 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34604 if (!structured_block)
34605 cp_parser_consume_semicolon_at_end_of_statement (parser);
34606 return;
34607
34608 saw_error:
34609 cp_parser_skip_to_end_of_block_or_statement (parser);
34610 if (structured_block)
34611 {
34612 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34613 cp_lexer_consume_token (parser->lexer);
34614 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34615 {
34616 cp_parser_skip_to_end_of_block_or_statement (parser);
34617 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34618 cp_lexer_consume_token (parser->lexer);
34619 }
34620 }
34621 }
34622
34623
34624 /* OpenMP 2.5:
34625 # pragma omp barrier new-line */
34626
34627 static void
34628 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34629 {
34630 cp_parser_require_pragma_eol (parser, pragma_tok);
34631 finish_omp_barrier ();
34632 }
34633
34634 /* OpenMP 2.5:
34635 # pragma omp critical [(name)] new-line
34636 structured-block
34637
34638 OpenMP 4.5:
34639 # pragma omp critical [(name) [hint(expression)]] new-line
34640 structured-block */
34641
34642 #define OMP_CRITICAL_CLAUSE_MASK \
34643 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34644
34645 static tree
34646 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34647 {
34648 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34649
34650 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34651 {
34652 matching_parens parens;
34653 parens.consume_open (parser);
34654
34655 name = cp_parser_identifier (parser);
34656
34657 if (name == error_mark_node
34658 || !parens.require_close (parser))
34659 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34660 /*or_comma=*/false,
34661 /*consume_paren=*/true);
34662 if (name == error_mark_node)
34663 name = NULL;
34664
34665 clauses = cp_parser_omp_all_clauses (parser,
34666 OMP_CRITICAL_CLAUSE_MASK,
34667 "#pragma omp critical", pragma_tok);
34668 }
34669 else
34670 cp_parser_require_pragma_eol (parser, pragma_tok);
34671
34672 stmt = cp_parser_omp_structured_block (parser, if_p);
34673 return c_finish_omp_critical (input_location, stmt, name, clauses);
34674 }
34675
34676 /* OpenMP 2.5:
34677 # pragma omp flush flush-vars[opt] new-line
34678
34679 flush-vars:
34680 ( variable-list ) */
34681
34682 static void
34683 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34684 {
34685 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34686 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34687 cp_parser_require_pragma_eol (parser, pragma_tok);
34688
34689 finish_omp_flush ();
34690 }
34691
34692 /* Helper function, to parse omp for increment expression. */
34693
34694 static tree
34695 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34696 {
34697 tree cond = cp_parser_binary_expression (parser, false, true,
34698 PREC_NOT_OPERATOR, NULL);
34699 if (cond == error_mark_node
34700 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34701 {
34702 cp_parser_skip_to_end_of_statement (parser);
34703 return error_mark_node;
34704 }
34705
34706 switch (TREE_CODE (cond))
34707 {
34708 case GT_EXPR:
34709 case GE_EXPR:
34710 case LT_EXPR:
34711 case LE_EXPR:
34712 break;
34713 case NE_EXPR:
34714 if (code == CILK_SIMD || code == CILK_FOR)
34715 break;
34716 /* Fall through: OpenMP disallows NE_EXPR. */
34717 gcc_fallthrough ();
34718 default:
34719 return error_mark_node;
34720 }
34721
34722 /* If decl is an iterator, preserve LHS and RHS of the relational
34723 expr until finish_omp_for. */
34724 if (decl
34725 && (type_dependent_expression_p (decl)
34726 || CLASS_TYPE_P (TREE_TYPE (decl))))
34727 return cond;
34728
34729 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34730 TREE_CODE (cond),
34731 TREE_OPERAND (cond, 0), ERROR_MARK,
34732 TREE_OPERAND (cond, 1), ERROR_MARK,
34733 /*overload=*/NULL, tf_warning_or_error);
34734 }
34735
34736 /* Helper function, to parse omp for increment expression. */
34737
34738 static tree
34739 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34740 {
34741 cp_token *token = cp_lexer_peek_token (parser->lexer);
34742 enum tree_code op;
34743 tree lhs, rhs;
34744 cp_id_kind idk;
34745 bool decl_first;
34746
34747 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34748 {
34749 op = (token->type == CPP_PLUS_PLUS
34750 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34751 cp_lexer_consume_token (parser->lexer);
34752 lhs = cp_parser_simple_cast_expression (parser);
34753 if (lhs != decl
34754 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34755 return error_mark_node;
34756 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34757 }
34758
34759 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34760 if (lhs != decl
34761 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34762 return error_mark_node;
34763
34764 token = cp_lexer_peek_token (parser->lexer);
34765 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34766 {
34767 op = (token->type == CPP_PLUS_PLUS
34768 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34769 cp_lexer_consume_token (parser->lexer);
34770 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34771 }
34772
34773 op = cp_parser_assignment_operator_opt (parser);
34774 if (op == ERROR_MARK)
34775 return error_mark_node;
34776
34777 if (op != NOP_EXPR)
34778 {
34779 rhs = cp_parser_assignment_expression (parser);
34780 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34781 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34782 }
34783
34784 lhs = cp_parser_binary_expression (parser, false, false,
34785 PREC_ADDITIVE_EXPRESSION, NULL);
34786 token = cp_lexer_peek_token (parser->lexer);
34787 decl_first = (lhs == decl
34788 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34789 if (decl_first)
34790 lhs = NULL_TREE;
34791 if (token->type != CPP_PLUS
34792 && token->type != CPP_MINUS)
34793 return error_mark_node;
34794
34795 do
34796 {
34797 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34798 cp_lexer_consume_token (parser->lexer);
34799 rhs = cp_parser_binary_expression (parser, false, false,
34800 PREC_ADDITIVE_EXPRESSION, NULL);
34801 token = cp_lexer_peek_token (parser->lexer);
34802 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34803 {
34804 if (lhs == NULL_TREE)
34805 {
34806 if (op == PLUS_EXPR)
34807 lhs = rhs;
34808 else
34809 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34810 tf_warning_or_error);
34811 }
34812 else
34813 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34814 ERROR_MARK, NULL, tf_warning_or_error);
34815 }
34816 }
34817 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34818
34819 if (!decl_first)
34820 {
34821 if ((rhs != decl
34822 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34823 || op == MINUS_EXPR)
34824 return error_mark_node;
34825 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34826 }
34827 else
34828 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34829
34830 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34831 }
34832
34833 /* Parse the initialization statement of either an OpenMP for loop or
34834 a Cilk Plus for loop.
34835
34836 Return true if the resulting construct should have an
34837 OMP_CLAUSE_PRIVATE added to it. */
34838
34839 static tree
34840 cp_parser_omp_for_loop_init (cp_parser *parser,
34841 enum tree_code code,
34842 tree &this_pre_body,
34843 vec<tree, va_gc> *for_block,
34844 tree &init,
34845 tree &orig_init,
34846 tree &decl,
34847 tree &real_decl)
34848 {
34849 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34850 return NULL_TREE;
34851
34852 tree add_private_clause = NULL_TREE;
34853
34854 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34855
34856 init-expr:
34857 var = lb
34858 integer-type var = lb
34859 random-access-iterator-type var = lb
34860 pointer-type var = lb
34861 */
34862 cp_decl_specifier_seq type_specifiers;
34863
34864 /* First, try to parse as an initialized declaration. See
34865 cp_parser_condition, from whence the bulk of this is copied. */
34866
34867 cp_parser_parse_tentatively (parser);
34868 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34869 /*is_trailing_return=*/false,
34870 &type_specifiers);
34871 if (cp_parser_parse_definitely (parser))
34872 {
34873 /* If parsing a type specifier seq succeeded, then this
34874 MUST be a initialized declaration. */
34875 tree asm_specification, attributes;
34876 cp_declarator *declarator;
34877
34878 declarator = cp_parser_declarator (parser,
34879 CP_PARSER_DECLARATOR_NAMED,
34880 /*ctor_dtor_or_conv_p=*/NULL,
34881 /*parenthesized_p=*/NULL,
34882 /*member_p=*/false,
34883 /*friend_p=*/false);
34884 attributes = cp_parser_attributes_opt (parser);
34885 asm_specification = cp_parser_asm_specification_opt (parser);
34886
34887 if (declarator == cp_error_declarator)
34888 cp_parser_skip_to_end_of_statement (parser);
34889
34890 else
34891 {
34892 tree pushed_scope, auto_node;
34893
34894 decl = start_decl (declarator, &type_specifiers,
34895 SD_INITIALIZED, attributes,
34896 /*prefix_attributes=*/NULL_TREE,
34897 &pushed_scope);
34898
34899 auto_node = type_uses_auto (TREE_TYPE (decl));
34900 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34901 {
34902 if (cp_lexer_next_token_is (parser->lexer,
34903 CPP_OPEN_PAREN))
34904 {
34905 if (code != CILK_SIMD && code != CILK_FOR)
34906 error ("parenthesized initialization is not allowed in "
34907 "OpenMP %<for%> loop");
34908 else
34909 error ("parenthesized initialization is "
34910 "not allowed in for-loop");
34911 }
34912 else
34913 /* Trigger an error. */
34914 cp_parser_require (parser, CPP_EQ, RT_EQ);
34915
34916 init = error_mark_node;
34917 cp_parser_skip_to_end_of_statement (parser);
34918 }
34919 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34920 || type_dependent_expression_p (decl)
34921 || auto_node)
34922 {
34923 bool is_direct_init, is_non_constant_init;
34924
34925 init = cp_parser_initializer (parser,
34926 &is_direct_init,
34927 &is_non_constant_init);
34928
34929 if (auto_node)
34930 {
34931 TREE_TYPE (decl)
34932 = do_auto_deduction (TREE_TYPE (decl), init,
34933 auto_node);
34934
34935 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34936 && !type_dependent_expression_p (decl))
34937 goto non_class;
34938 }
34939
34940 cp_finish_decl (decl, init, !is_non_constant_init,
34941 asm_specification,
34942 LOOKUP_ONLYCONVERTING);
34943 orig_init = init;
34944 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34945 {
34946 vec_safe_push (for_block, this_pre_body);
34947 init = NULL_TREE;
34948 }
34949 else
34950 {
34951 init = pop_stmt_list (this_pre_body);
34952 if (init && TREE_CODE (init) == STATEMENT_LIST)
34953 {
34954 tree_stmt_iterator i = tsi_start (init);
34955 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34956 while (!tsi_end_p (i))
34957 {
34958 tree t = tsi_stmt (i);
34959 if (TREE_CODE (t) == DECL_EXPR
34960 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34961 {
34962 tsi_delink (&i);
34963 vec_safe_push (for_block, t);
34964 continue;
34965 }
34966 break;
34967 }
34968 if (tsi_one_before_end_p (i))
34969 {
34970 tree t = tsi_stmt (i);
34971 tsi_delink (&i);
34972 free_stmt_list (init);
34973 init = t;
34974 }
34975 }
34976 }
34977 this_pre_body = NULL_TREE;
34978 }
34979 else
34980 {
34981 /* Consume '='. */
34982 cp_lexer_consume_token (parser->lexer);
34983 init = cp_parser_assignment_expression (parser);
34984
34985 non_class:
34986 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34987 init = error_mark_node;
34988 else
34989 cp_finish_decl (decl, NULL_TREE,
34990 /*init_const_expr_p=*/false,
34991 asm_specification,
34992 LOOKUP_ONLYCONVERTING);
34993 }
34994
34995 if (pushed_scope)
34996 pop_scope (pushed_scope);
34997 }
34998 }
34999 else
35000 {
35001 cp_id_kind idk;
35002 /* If parsing a type specifier sequence failed, then
35003 this MUST be a simple expression. */
35004 if (code == CILK_FOR)
35005 error ("%<_Cilk_for%> allows expression instead of declaration only "
35006 "in C, not in C++");
35007 cp_parser_parse_tentatively (parser);
35008 decl = cp_parser_primary_expression (parser, false, false,
35009 false, &idk);
35010 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35011 if (!cp_parser_error_occurred (parser)
35012 && decl
35013 && (TREE_CODE (decl) == COMPONENT_REF
35014 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35015 {
35016 cp_parser_abort_tentative_parse (parser);
35017 cp_parser_parse_tentatively (parser);
35018 cp_token *token = cp_lexer_peek_token (parser->lexer);
35019 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35020 /*check_dependency_p=*/true,
35021 /*template_p=*/NULL,
35022 /*declarator_p=*/false,
35023 /*optional_p=*/false);
35024 if (name != error_mark_node
35025 && last_tok == cp_lexer_peek_token (parser->lexer))
35026 {
35027 decl = cp_parser_lookup_name_simple (parser, name,
35028 token->location);
35029 if (TREE_CODE (decl) == FIELD_DECL)
35030 add_private_clause = omp_privatize_field (decl, false);
35031 }
35032 cp_parser_abort_tentative_parse (parser);
35033 cp_parser_parse_tentatively (parser);
35034 decl = cp_parser_primary_expression (parser, false, false,
35035 false, &idk);
35036 }
35037 if (!cp_parser_error_occurred (parser)
35038 && decl
35039 && DECL_P (decl)
35040 && CLASS_TYPE_P (TREE_TYPE (decl)))
35041 {
35042 tree rhs;
35043
35044 cp_parser_parse_definitely (parser);
35045 cp_parser_require (parser, CPP_EQ, RT_EQ);
35046 rhs = cp_parser_assignment_expression (parser);
35047 orig_init = rhs;
35048 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35049 decl, NOP_EXPR,
35050 rhs,
35051 tf_warning_or_error));
35052 if (!add_private_clause)
35053 add_private_clause = decl;
35054 }
35055 else
35056 {
35057 decl = NULL;
35058 cp_parser_abort_tentative_parse (parser);
35059 init = cp_parser_expression (parser);
35060 if (init)
35061 {
35062 if (TREE_CODE (init) == MODIFY_EXPR
35063 || TREE_CODE (init) == MODOP_EXPR)
35064 real_decl = TREE_OPERAND (init, 0);
35065 }
35066 }
35067 }
35068 return add_private_clause;
35069 }
35070
35071 /* Parse the restricted form of the for statement allowed by OpenMP. */
35072
35073 static tree
35074 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
35075 tree *cclauses, bool *if_p)
35076 {
35077 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
35078 tree real_decl, initv, condv, incrv, declv;
35079 tree this_pre_body, cl, ordered_cl = NULL_TREE;
35080 location_t loc_first;
35081 bool collapse_err = false;
35082 int i, collapse = 1, ordered = 0, count, nbraces = 0;
35083 vec<tree, va_gc> *for_block = make_tree_vector ();
35084 auto_vec<tree, 4> orig_inits;
35085 bool tiling = false;
35086
35087 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
35088 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
35089 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
35090 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35091 {
35092 tiling = true;
35093 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35094 }
35095 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35096 && OMP_CLAUSE_ORDERED_EXPR (cl))
35097 {
35098 ordered_cl = cl;
35099 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35100 }
35101
35102 if (ordered && ordered < collapse)
35103 {
35104 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35105 "%<ordered%> clause parameter is less than %<collapse%>");
35106 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35107 = build_int_cst (NULL_TREE, collapse);
35108 ordered = collapse;
35109 }
35110 if (ordered)
35111 {
35112 for (tree *pc = &clauses; *pc; )
35113 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35114 {
35115 error_at (OMP_CLAUSE_LOCATION (*pc),
35116 "%<linear%> clause may not be specified together "
35117 "with %<ordered%> clause with a parameter");
35118 *pc = OMP_CLAUSE_CHAIN (*pc);
35119 }
35120 else
35121 pc = &OMP_CLAUSE_CHAIN (*pc);
35122 }
35123
35124 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35125 count = ordered ? ordered : collapse;
35126
35127 declv = make_tree_vec (count);
35128 initv = make_tree_vec (count);
35129 condv = make_tree_vec (count);
35130 incrv = make_tree_vec (count);
35131
35132 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35133
35134 for (i = 0; i < count; i++)
35135 {
35136 int bracecount = 0;
35137 tree add_private_clause = NULL_TREE;
35138 location_t loc;
35139
35140 if (code != CILK_FOR
35141 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35142 {
35143 if (!collapse_err)
35144 cp_parser_error (parser, "for statement expected");
35145 return NULL;
35146 }
35147 if (code == CILK_FOR
35148 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
35149 {
35150 if (!collapse_err)
35151 cp_parser_error (parser, "_Cilk_for statement expected");
35152 return NULL;
35153 }
35154 loc = cp_lexer_consume_token (parser->lexer)->location;
35155
35156 matching_parens parens;
35157 if (!parens.require_open (parser))
35158 return NULL;
35159
35160 init = orig_init = decl = real_decl = NULL;
35161 this_pre_body = push_stmt_list ();
35162
35163 add_private_clause
35164 = cp_parser_omp_for_loop_init (parser, code,
35165 this_pre_body, for_block,
35166 init, orig_init, decl, real_decl);
35167
35168 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35169 if (this_pre_body)
35170 {
35171 this_pre_body = pop_stmt_list (this_pre_body);
35172 if (pre_body)
35173 {
35174 tree t = pre_body;
35175 pre_body = push_stmt_list ();
35176 add_stmt (t);
35177 add_stmt (this_pre_body);
35178 pre_body = pop_stmt_list (pre_body);
35179 }
35180 else
35181 pre_body = this_pre_body;
35182 }
35183
35184 if (decl)
35185 real_decl = decl;
35186 if (cclauses != NULL
35187 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35188 && real_decl != NULL_TREE)
35189 {
35190 tree *c;
35191 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35192 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35193 && OMP_CLAUSE_DECL (*c) == real_decl)
35194 {
35195 error_at (loc, "iteration variable %qD"
35196 " should not be firstprivate", real_decl);
35197 *c = OMP_CLAUSE_CHAIN (*c);
35198 }
35199 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35200 && OMP_CLAUSE_DECL (*c) == real_decl)
35201 {
35202 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35203 tree l = *c;
35204 *c = OMP_CLAUSE_CHAIN (*c);
35205 if (code == OMP_SIMD)
35206 {
35207 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35208 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35209 }
35210 else
35211 {
35212 OMP_CLAUSE_CHAIN (l) = clauses;
35213 clauses = l;
35214 }
35215 add_private_clause = NULL_TREE;
35216 }
35217 else
35218 {
35219 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35220 && OMP_CLAUSE_DECL (*c) == real_decl)
35221 add_private_clause = NULL_TREE;
35222 c = &OMP_CLAUSE_CHAIN (*c);
35223 }
35224 }
35225
35226 if (add_private_clause)
35227 {
35228 tree c;
35229 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35230 {
35231 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35232 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35233 && OMP_CLAUSE_DECL (c) == decl)
35234 break;
35235 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35236 && OMP_CLAUSE_DECL (c) == decl)
35237 error_at (loc, "iteration variable %qD "
35238 "should not be firstprivate",
35239 decl);
35240 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35241 && OMP_CLAUSE_DECL (c) == decl)
35242 error_at (loc, "iteration variable %qD should not be reduction",
35243 decl);
35244 }
35245 if (c == NULL)
35246 {
35247 if (code != OMP_SIMD)
35248 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35249 else if (collapse == 1)
35250 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35251 else
35252 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35253 OMP_CLAUSE_DECL (c) = add_private_clause;
35254 c = finish_omp_clauses (c, C_ORT_OMP);
35255 if (c)
35256 {
35257 OMP_CLAUSE_CHAIN (c) = clauses;
35258 clauses = c;
35259 /* For linear, signal that we need to fill up
35260 the so far unknown linear step. */
35261 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35262 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35263 }
35264 }
35265 }
35266
35267 cond = NULL;
35268 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35269 cond = cp_parser_omp_for_cond (parser, decl, code);
35270 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35271
35272 incr = NULL;
35273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35274 {
35275 /* If decl is an iterator, preserve the operator on decl
35276 until finish_omp_for. */
35277 if (real_decl
35278 && ((processing_template_decl
35279 && (TREE_TYPE (real_decl) == NULL_TREE
35280 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35281 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35282 incr = cp_parser_omp_for_incr (parser, real_decl);
35283 else
35284 incr = cp_parser_expression (parser);
35285 if (!EXPR_HAS_LOCATION (incr))
35286 protected_set_expr_location (incr, input_location);
35287 }
35288
35289 if (!parens.require_close (parser))
35290 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35291 /*or_comma=*/false,
35292 /*consume_paren=*/true);
35293
35294 TREE_VEC_ELT (declv, i) = decl;
35295 TREE_VEC_ELT (initv, i) = init;
35296 TREE_VEC_ELT (condv, i) = cond;
35297 TREE_VEC_ELT (incrv, i) = incr;
35298 if (orig_init)
35299 {
35300 orig_inits.safe_grow_cleared (i + 1);
35301 orig_inits[i] = orig_init;
35302 }
35303
35304 if (i == count - 1)
35305 break;
35306
35307 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35308 in between the collapsed for loops to be still considered perfectly
35309 nested. Hopefully the final version clarifies this.
35310 For now handle (multiple) {'s and empty statements. */
35311 cp_parser_parse_tentatively (parser);
35312 for (;;)
35313 {
35314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35315 break;
35316 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35317 {
35318 cp_lexer_consume_token (parser->lexer);
35319 bracecount++;
35320 }
35321 else if (bracecount
35322 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35323 cp_lexer_consume_token (parser->lexer);
35324 else
35325 {
35326 loc = cp_lexer_peek_token (parser->lexer)->location;
35327 error_at (loc, "not enough for loops to collapse");
35328 collapse_err = true;
35329 cp_parser_abort_tentative_parse (parser);
35330 declv = NULL_TREE;
35331 break;
35332 }
35333 }
35334
35335 if (declv)
35336 {
35337 cp_parser_parse_definitely (parser);
35338 nbraces += bracecount;
35339 }
35340 }
35341
35342 if (nbraces)
35343 if_p = NULL;
35344
35345 /* Note that we saved the original contents of this flag when we entered
35346 the structured block, and so we don't need to re-save it here. */
35347 if (code == CILK_SIMD || code == CILK_FOR)
35348 parser->in_statement = IN_CILK_SIMD_FOR;
35349 else
35350 parser->in_statement = IN_OMP_FOR;
35351
35352 /* Note that the grammar doesn't call for a structured block here,
35353 though the loop as a whole is a structured block. */
35354 body = push_stmt_list ();
35355 cp_parser_statement (parser, NULL_TREE, false, if_p);
35356 body = pop_stmt_list (body);
35357
35358 if (declv == NULL_TREE)
35359 ret = NULL_TREE;
35360 else
35361 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35362 body, pre_body, &orig_inits, clauses);
35363
35364 while (nbraces)
35365 {
35366 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35367 {
35368 cp_lexer_consume_token (parser->lexer);
35369 nbraces--;
35370 }
35371 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35372 cp_lexer_consume_token (parser->lexer);
35373 else
35374 {
35375 if (!collapse_err)
35376 {
35377 error_at (cp_lexer_peek_token (parser->lexer)->location,
35378 "collapsed loops not perfectly nested");
35379 }
35380 collapse_err = true;
35381 cp_parser_statement_seq_opt (parser, NULL);
35382 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35383 break;
35384 }
35385 }
35386
35387 while (!for_block->is_empty ())
35388 {
35389 tree t = for_block->pop ();
35390 if (TREE_CODE (t) == STATEMENT_LIST)
35391 add_stmt (pop_stmt_list (t));
35392 else
35393 add_stmt (t);
35394 }
35395 release_tree_vector (for_block);
35396
35397 return ret;
35398 }
35399
35400 /* Helper function for OpenMP parsing, split clauses and call
35401 finish_omp_clauses on each of the set of clauses afterwards. */
35402
35403 static void
35404 cp_omp_split_clauses (location_t loc, enum tree_code code,
35405 omp_clause_mask mask, tree clauses, tree *cclauses)
35406 {
35407 int i;
35408 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35409 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35410 if (cclauses[i])
35411 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35412 }
35413
35414 /* OpenMP 4.0:
35415 #pragma omp simd simd-clause[optseq] new-line
35416 for-loop */
35417
35418 #define OMP_SIMD_CLAUSE_MASK \
35419 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35427
35428 static tree
35429 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35430 char *p_name, omp_clause_mask mask, tree *cclauses,
35431 bool *if_p)
35432 {
35433 tree clauses, sb, ret;
35434 unsigned int save;
35435 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35436
35437 strcat (p_name, " simd");
35438 mask |= OMP_SIMD_CLAUSE_MASK;
35439
35440 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35441 cclauses == NULL);
35442 if (cclauses)
35443 {
35444 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35445 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35446 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35447 OMP_CLAUSE_ORDERED);
35448 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35449 {
35450 error_at (OMP_CLAUSE_LOCATION (c),
35451 "%<ordered%> clause with parameter may not be specified "
35452 "on %qs construct", p_name);
35453 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35454 }
35455 }
35456
35457 sb = begin_omp_structured_block ();
35458 save = cp_parser_begin_omp_structured_block (parser);
35459
35460 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35461
35462 cp_parser_end_omp_structured_block (parser, save);
35463 add_stmt (finish_omp_structured_block (sb));
35464
35465 return ret;
35466 }
35467
35468 /* OpenMP 2.5:
35469 #pragma omp for for-clause[optseq] new-line
35470 for-loop
35471
35472 OpenMP 4.0:
35473 #pragma omp for simd for-simd-clause[optseq] new-line
35474 for-loop */
35475
35476 #define OMP_FOR_CLAUSE_MASK \
35477 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35486
35487 static tree
35488 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35489 char *p_name, omp_clause_mask mask, tree *cclauses,
35490 bool *if_p)
35491 {
35492 tree clauses, sb, ret;
35493 unsigned int save;
35494 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35495
35496 strcat (p_name, " for");
35497 mask |= OMP_FOR_CLAUSE_MASK;
35498 /* parallel for{, simd} disallows nowait clause, but for
35499 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35500 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35501 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35502 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35503 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35504 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35505
35506 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35507 {
35508 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35509 const char *p = IDENTIFIER_POINTER (id);
35510
35511 if (strcmp (p, "simd") == 0)
35512 {
35513 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35514 if (cclauses == NULL)
35515 cclauses = cclauses_buf;
35516
35517 cp_lexer_consume_token (parser->lexer);
35518 if (!flag_openmp) /* flag_openmp_simd */
35519 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35520 cclauses, if_p);
35521 sb = begin_omp_structured_block ();
35522 save = cp_parser_begin_omp_structured_block (parser);
35523 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35524 cclauses, if_p);
35525 cp_parser_end_omp_structured_block (parser, save);
35526 tree body = finish_omp_structured_block (sb);
35527 if (ret == NULL)
35528 return ret;
35529 ret = make_node (OMP_FOR);
35530 TREE_TYPE (ret) = void_type_node;
35531 OMP_FOR_BODY (ret) = body;
35532 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35533 SET_EXPR_LOCATION (ret, loc);
35534 add_stmt (ret);
35535 return ret;
35536 }
35537 }
35538 if (!flag_openmp) /* flag_openmp_simd */
35539 {
35540 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35541 return NULL_TREE;
35542 }
35543
35544 /* Composite distribute parallel for disallows linear clause. */
35545 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35546 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35547
35548 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35549 cclauses == NULL);
35550 if (cclauses)
35551 {
35552 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35553 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35554 }
35555
35556 sb = begin_omp_structured_block ();
35557 save = cp_parser_begin_omp_structured_block (parser);
35558
35559 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35560
35561 cp_parser_end_omp_structured_block (parser, save);
35562 add_stmt (finish_omp_structured_block (sb));
35563
35564 return ret;
35565 }
35566
35567 /* OpenMP 2.5:
35568 # pragma omp master new-line
35569 structured-block */
35570
35571 static tree
35572 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35573 {
35574 cp_parser_require_pragma_eol (parser, pragma_tok);
35575 return c_finish_omp_master (input_location,
35576 cp_parser_omp_structured_block (parser, if_p));
35577 }
35578
35579 /* OpenMP 2.5:
35580 # pragma omp ordered new-line
35581 structured-block
35582
35583 OpenMP 4.5:
35584 # pragma omp ordered ordered-clauses new-line
35585 structured-block */
35586
35587 #define OMP_ORDERED_CLAUSE_MASK \
35588 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35590
35591 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35592 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35593
35594 static bool
35595 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35596 enum pragma_context context, bool *if_p)
35597 {
35598 location_t loc = pragma_tok->location;
35599
35600 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35601 {
35602 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35603 const char *p = IDENTIFIER_POINTER (id);
35604
35605 if (strcmp (p, "depend") == 0)
35606 {
35607 if (!flag_openmp) /* flag_openmp_simd */
35608 {
35609 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35610 return false;
35611 }
35612 if (context == pragma_stmt)
35613 {
35614 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35615 "%<depend%> clause may only be used in compound "
35616 "statements");
35617 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35618 return false;
35619 }
35620 tree clauses
35621 = cp_parser_omp_all_clauses (parser,
35622 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35623 "#pragma omp ordered", pragma_tok);
35624 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35625 return false;
35626 }
35627 }
35628
35629 tree clauses
35630 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35631 "#pragma omp ordered", pragma_tok);
35632
35633 if (!flag_openmp /* flag_openmp_simd */
35634 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35635 return false;
35636
35637 c_finish_omp_ordered (loc, clauses,
35638 cp_parser_omp_structured_block (parser, if_p));
35639 return true;
35640 }
35641
35642 /* OpenMP 2.5:
35643
35644 section-scope:
35645 { section-sequence }
35646
35647 section-sequence:
35648 section-directive[opt] structured-block
35649 section-sequence section-directive structured-block */
35650
35651 static tree
35652 cp_parser_omp_sections_scope (cp_parser *parser)
35653 {
35654 tree stmt, substmt;
35655 bool error_suppress = false;
35656 cp_token *tok;
35657
35658 matching_braces braces;
35659 if (!braces.require_open (parser))
35660 return NULL_TREE;
35661
35662 stmt = push_stmt_list ();
35663
35664 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35665 != PRAGMA_OMP_SECTION)
35666 {
35667 substmt = cp_parser_omp_structured_block (parser, NULL);
35668 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35669 add_stmt (substmt);
35670 }
35671
35672 while (1)
35673 {
35674 tok = cp_lexer_peek_token (parser->lexer);
35675 if (tok->type == CPP_CLOSE_BRACE)
35676 break;
35677 if (tok->type == CPP_EOF)
35678 break;
35679
35680 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35681 {
35682 cp_lexer_consume_token (parser->lexer);
35683 cp_parser_require_pragma_eol (parser, tok);
35684 error_suppress = false;
35685 }
35686 else if (!error_suppress)
35687 {
35688 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35689 error_suppress = true;
35690 }
35691
35692 substmt = cp_parser_omp_structured_block (parser, NULL);
35693 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35694 add_stmt (substmt);
35695 }
35696 braces.require_close (parser);
35697
35698 substmt = pop_stmt_list (stmt);
35699
35700 stmt = make_node (OMP_SECTIONS);
35701 TREE_TYPE (stmt) = void_type_node;
35702 OMP_SECTIONS_BODY (stmt) = substmt;
35703
35704 add_stmt (stmt);
35705 return stmt;
35706 }
35707
35708 /* OpenMP 2.5:
35709 # pragma omp sections sections-clause[optseq] newline
35710 sections-scope */
35711
35712 #define OMP_SECTIONS_CLAUSE_MASK \
35713 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35718
35719 static tree
35720 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35721 char *p_name, omp_clause_mask mask, tree *cclauses)
35722 {
35723 tree clauses, ret;
35724 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35725
35726 strcat (p_name, " sections");
35727 mask |= OMP_SECTIONS_CLAUSE_MASK;
35728 if (cclauses)
35729 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35730
35731 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35732 cclauses == NULL);
35733 if (cclauses)
35734 {
35735 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35736 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35737 }
35738
35739 ret = cp_parser_omp_sections_scope (parser);
35740 if (ret)
35741 OMP_SECTIONS_CLAUSES (ret) = clauses;
35742
35743 return ret;
35744 }
35745
35746 /* OpenMP 2.5:
35747 # pragma omp parallel parallel-clause[optseq] new-line
35748 structured-block
35749 # pragma omp parallel for parallel-for-clause[optseq] new-line
35750 structured-block
35751 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35752 structured-block
35753
35754 OpenMP 4.0:
35755 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35756 structured-block */
35757
35758 #define OMP_PARALLEL_CLAUSE_MASK \
35759 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35768
35769 static tree
35770 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35771 char *p_name, omp_clause_mask mask, tree *cclauses,
35772 bool *if_p)
35773 {
35774 tree stmt, clauses, block;
35775 unsigned int save;
35776 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35777
35778 strcat (p_name, " parallel");
35779 mask |= OMP_PARALLEL_CLAUSE_MASK;
35780 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35781 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35782 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35783 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35784
35785 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35786 {
35787 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35788 if (cclauses == NULL)
35789 cclauses = cclauses_buf;
35790
35791 cp_lexer_consume_token (parser->lexer);
35792 if (!flag_openmp) /* flag_openmp_simd */
35793 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35794 if_p);
35795 block = begin_omp_parallel ();
35796 save = cp_parser_begin_omp_structured_block (parser);
35797 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35798 if_p);
35799 cp_parser_end_omp_structured_block (parser, save);
35800 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35801 block);
35802 if (ret == NULL_TREE)
35803 return ret;
35804 OMP_PARALLEL_COMBINED (stmt) = 1;
35805 return stmt;
35806 }
35807 /* When combined with distribute, parallel has to be followed by for.
35808 #pragma omp target parallel is allowed though. */
35809 else if (cclauses
35810 && (mask & (OMP_CLAUSE_MASK_1
35811 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35812 {
35813 error_at (loc, "expected %<for%> after %qs", p_name);
35814 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35815 return NULL_TREE;
35816 }
35817 else if (!flag_openmp) /* flag_openmp_simd */
35818 {
35819 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35820 return NULL_TREE;
35821 }
35822 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35823 {
35824 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35825 const char *p = IDENTIFIER_POINTER (id);
35826 if (strcmp (p, "sections") == 0)
35827 {
35828 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35829 cclauses = cclauses_buf;
35830
35831 cp_lexer_consume_token (parser->lexer);
35832 block = begin_omp_parallel ();
35833 save = cp_parser_begin_omp_structured_block (parser);
35834 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35835 cp_parser_end_omp_structured_block (parser, save);
35836 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35837 block);
35838 OMP_PARALLEL_COMBINED (stmt) = 1;
35839 return stmt;
35840 }
35841 }
35842
35843 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35844 cclauses == NULL);
35845 if (cclauses)
35846 {
35847 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35848 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35849 }
35850
35851 block = begin_omp_parallel ();
35852 save = cp_parser_begin_omp_structured_block (parser);
35853 cp_parser_statement (parser, NULL_TREE, false, if_p);
35854 cp_parser_end_omp_structured_block (parser, save);
35855 stmt = finish_omp_parallel (clauses, block);
35856 return stmt;
35857 }
35858
35859 /* OpenMP 2.5:
35860 # pragma omp single single-clause[optseq] new-line
35861 structured-block */
35862
35863 #define OMP_SINGLE_CLAUSE_MASK \
35864 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35868
35869 static tree
35870 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35871 {
35872 tree stmt = make_node (OMP_SINGLE);
35873 TREE_TYPE (stmt) = void_type_node;
35874
35875 OMP_SINGLE_CLAUSES (stmt)
35876 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35877 "#pragma omp single", pragma_tok);
35878 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35879
35880 return add_stmt (stmt);
35881 }
35882
35883 /* OpenMP 3.0:
35884 # pragma omp task task-clause[optseq] new-line
35885 structured-block */
35886
35887 #define OMP_TASK_CLAUSE_MASK \
35888 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35898
35899 static tree
35900 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35901 {
35902 tree clauses, block;
35903 unsigned int save;
35904
35905 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35906 "#pragma omp task", pragma_tok);
35907 block = begin_omp_task ();
35908 save = cp_parser_begin_omp_structured_block (parser);
35909 cp_parser_statement (parser, NULL_TREE, false, if_p);
35910 cp_parser_end_omp_structured_block (parser, save);
35911 return finish_omp_task (clauses, block);
35912 }
35913
35914 /* OpenMP 3.0:
35915 # pragma omp taskwait new-line */
35916
35917 static void
35918 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35919 {
35920 cp_parser_require_pragma_eol (parser, pragma_tok);
35921 finish_omp_taskwait ();
35922 }
35923
35924 /* OpenMP 3.1:
35925 # pragma omp taskyield new-line */
35926
35927 static void
35928 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35929 {
35930 cp_parser_require_pragma_eol (parser, pragma_tok);
35931 finish_omp_taskyield ();
35932 }
35933
35934 /* OpenMP 4.0:
35935 # pragma omp taskgroup new-line
35936 structured-block */
35937
35938 static tree
35939 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35940 {
35941 cp_parser_require_pragma_eol (parser, pragma_tok);
35942 return c_finish_omp_taskgroup (input_location,
35943 cp_parser_omp_structured_block (parser,
35944 if_p));
35945 }
35946
35947
35948 /* OpenMP 2.5:
35949 # pragma omp threadprivate (variable-list) */
35950
35951 static void
35952 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35953 {
35954 tree vars;
35955
35956 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35957 cp_parser_require_pragma_eol (parser, pragma_tok);
35958
35959 finish_omp_threadprivate (vars);
35960 }
35961
35962 /* OpenMP 4.0:
35963 # pragma omp cancel cancel-clause[optseq] new-line */
35964
35965 #define OMP_CANCEL_CLAUSE_MASK \
35966 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35971
35972 static void
35973 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35974 {
35975 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35976 "#pragma omp cancel", pragma_tok);
35977 finish_omp_cancel (clauses);
35978 }
35979
35980 /* OpenMP 4.0:
35981 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35982
35983 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35984 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35988
35989 static void
35990 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35991 enum pragma_context context)
35992 {
35993 tree clauses;
35994 bool point_seen = false;
35995
35996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35997 {
35998 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35999 const char *p = IDENTIFIER_POINTER (id);
36000
36001 if (strcmp (p, "point") == 0)
36002 {
36003 cp_lexer_consume_token (parser->lexer);
36004 point_seen = true;
36005 }
36006 }
36007 if (!point_seen)
36008 {
36009 cp_parser_error (parser, "expected %<point%>");
36010 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36011 return;
36012 }
36013
36014 if (context != pragma_compound)
36015 {
36016 if (context == pragma_stmt)
36017 error_at (pragma_tok->location,
36018 "%<#pragma %s%> may only be used in compound statements",
36019 "omp cancellation point");
36020 else
36021 cp_parser_error (parser, "expected declaration specifiers");
36022 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36023 return;
36024 }
36025
36026 clauses = cp_parser_omp_all_clauses (parser,
36027 OMP_CANCELLATION_POINT_CLAUSE_MASK,
36028 "#pragma omp cancellation point",
36029 pragma_tok);
36030 finish_omp_cancellation_point (clauses);
36031 }
36032
36033 /* OpenMP 4.0:
36034 #pragma omp distribute distribute-clause[optseq] new-line
36035 for-loop */
36036
36037 #define OMP_DISTRIBUTE_CLAUSE_MASK \
36038 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
36042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36043
36044 static tree
36045 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
36046 char *p_name, omp_clause_mask mask, tree *cclauses,
36047 bool *if_p)
36048 {
36049 tree clauses, sb, ret;
36050 unsigned int save;
36051 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36052
36053 strcat (p_name, " distribute");
36054 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
36055
36056 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36057 {
36058 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36059 const char *p = IDENTIFIER_POINTER (id);
36060 bool simd = false;
36061 bool parallel = false;
36062
36063 if (strcmp (p, "simd") == 0)
36064 simd = true;
36065 else
36066 parallel = strcmp (p, "parallel") == 0;
36067 if (parallel || simd)
36068 {
36069 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36070 if (cclauses == NULL)
36071 cclauses = cclauses_buf;
36072 cp_lexer_consume_token (parser->lexer);
36073 if (!flag_openmp) /* flag_openmp_simd */
36074 {
36075 if (simd)
36076 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36077 cclauses, if_p);
36078 else
36079 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36080 cclauses, if_p);
36081 }
36082 sb = begin_omp_structured_block ();
36083 save = cp_parser_begin_omp_structured_block (parser);
36084 if (simd)
36085 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36086 cclauses, if_p);
36087 else
36088 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
36089 cclauses, if_p);
36090 cp_parser_end_omp_structured_block (parser, save);
36091 tree body = finish_omp_structured_block (sb);
36092 if (ret == NULL)
36093 return ret;
36094 ret = make_node (OMP_DISTRIBUTE);
36095 TREE_TYPE (ret) = void_type_node;
36096 OMP_FOR_BODY (ret) = body;
36097 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36098 SET_EXPR_LOCATION (ret, loc);
36099 add_stmt (ret);
36100 return ret;
36101 }
36102 }
36103 if (!flag_openmp) /* flag_openmp_simd */
36104 {
36105 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36106 return NULL_TREE;
36107 }
36108
36109 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36110 cclauses == NULL);
36111 if (cclauses)
36112 {
36113 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36114 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36115 }
36116
36117 sb = begin_omp_structured_block ();
36118 save = cp_parser_begin_omp_structured_block (parser);
36119
36120 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36121
36122 cp_parser_end_omp_structured_block (parser, save);
36123 add_stmt (finish_omp_structured_block (sb));
36124
36125 return ret;
36126 }
36127
36128 /* OpenMP 4.0:
36129 # pragma omp teams teams-clause[optseq] new-line
36130 structured-block */
36131
36132 #define OMP_TEAMS_CLAUSE_MASK \
36133 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36140
36141 static tree
36142 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36143 char *p_name, omp_clause_mask mask, tree *cclauses,
36144 bool *if_p)
36145 {
36146 tree clauses, sb, ret;
36147 unsigned int save;
36148 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36149
36150 strcat (p_name, " teams");
36151 mask |= OMP_TEAMS_CLAUSE_MASK;
36152
36153 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36154 {
36155 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36156 const char *p = IDENTIFIER_POINTER (id);
36157 if (strcmp (p, "distribute") == 0)
36158 {
36159 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36160 if (cclauses == NULL)
36161 cclauses = cclauses_buf;
36162
36163 cp_lexer_consume_token (parser->lexer);
36164 if (!flag_openmp) /* flag_openmp_simd */
36165 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36166 cclauses, if_p);
36167 sb = begin_omp_structured_block ();
36168 save = cp_parser_begin_omp_structured_block (parser);
36169 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36170 cclauses, if_p);
36171 cp_parser_end_omp_structured_block (parser, save);
36172 tree body = finish_omp_structured_block (sb);
36173 if (ret == NULL)
36174 return ret;
36175 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36176 ret = make_node (OMP_TEAMS);
36177 TREE_TYPE (ret) = void_type_node;
36178 OMP_TEAMS_CLAUSES (ret) = clauses;
36179 OMP_TEAMS_BODY (ret) = body;
36180 OMP_TEAMS_COMBINED (ret) = 1;
36181 SET_EXPR_LOCATION (ret, loc);
36182 return add_stmt (ret);
36183 }
36184 }
36185 if (!flag_openmp) /* flag_openmp_simd */
36186 {
36187 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36188 return NULL_TREE;
36189 }
36190
36191 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36192 cclauses == NULL);
36193 if (cclauses)
36194 {
36195 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36196 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36197 }
36198
36199 tree stmt = make_node (OMP_TEAMS);
36200 TREE_TYPE (stmt) = void_type_node;
36201 OMP_TEAMS_CLAUSES (stmt) = clauses;
36202 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36203 SET_EXPR_LOCATION (stmt, loc);
36204
36205 return add_stmt (stmt);
36206 }
36207
36208 /* OpenMP 4.0:
36209 # pragma omp target data target-data-clause[optseq] new-line
36210 structured-block */
36211
36212 #define OMP_TARGET_DATA_CLAUSE_MASK \
36213 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36217
36218 static tree
36219 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36220 {
36221 tree clauses
36222 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36223 "#pragma omp target data", pragma_tok);
36224 int map_seen = 0;
36225 for (tree *pc = &clauses; *pc;)
36226 {
36227 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36228 switch (OMP_CLAUSE_MAP_KIND (*pc))
36229 {
36230 case GOMP_MAP_TO:
36231 case GOMP_MAP_ALWAYS_TO:
36232 case GOMP_MAP_FROM:
36233 case GOMP_MAP_ALWAYS_FROM:
36234 case GOMP_MAP_TOFROM:
36235 case GOMP_MAP_ALWAYS_TOFROM:
36236 case GOMP_MAP_ALLOC:
36237 map_seen = 3;
36238 break;
36239 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36240 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36241 case GOMP_MAP_ALWAYS_POINTER:
36242 break;
36243 default:
36244 map_seen |= 1;
36245 error_at (OMP_CLAUSE_LOCATION (*pc),
36246 "%<#pragma omp target data%> with map-type other "
36247 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36248 "on %<map%> clause");
36249 *pc = OMP_CLAUSE_CHAIN (*pc);
36250 continue;
36251 }
36252 pc = &OMP_CLAUSE_CHAIN (*pc);
36253 }
36254
36255 if (map_seen != 3)
36256 {
36257 if (map_seen == 0)
36258 error_at (pragma_tok->location,
36259 "%<#pragma omp target data%> must contain at least "
36260 "one %<map%> clause");
36261 return NULL_TREE;
36262 }
36263
36264 tree stmt = make_node (OMP_TARGET_DATA);
36265 TREE_TYPE (stmt) = void_type_node;
36266 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36267
36268 keep_next_level (true);
36269 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36270
36271 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36272 return add_stmt (stmt);
36273 }
36274
36275 /* OpenMP 4.5:
36276 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36277 structured-block */
36278
36279 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36280 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36285
36286 static tree
36287 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36288 enum pragma_context context)
36289 {
36290 bool data_seen = false;
36291 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36292 {
36293 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36294 const char *p = IDENTIFIER_POINTER (id);
36295
36296 if (strcmp (p, "data") == 0)
36297 {
36298 cp_lexer_consume_token (parser->lexer);
36299 data_seen = true;
36300 }
36301 }
36302 if (!data_seen)
36303 {
36304 cp_parser_error (parser, "expected %<data%>");
36305 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36306 return NULL_TREE;
36307 }
36308
36309 if (context == pragma_stmt)
36310 {
36311 error_at (pragma_tok->location,
36312 "%<#pragma %s%> may only be used in compound statements",
36313 "omp target enter data");
36314 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36315 return NULL_TREE;
36316 }
36317
36318 tree clauses
36319 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36320 "#pragma omp target enter data", pragma_tok);
36321 int map_seen = 0;
36322 for (tree *pc = &clauses; *pc;)
36323 {
36324 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36325 switch (OMP_CLAUSE_MAP_KIND (*pc))
36326 {
36327 case GOMP_MAP_TO:
36328 case GOMP_MAP_ALWAYS_TO:
36329 case GOMP_MAP_ALLOC:
36330 map_seen = 3;
36331 break;
36332 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36333 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36334 case GOMP_MAP_ALWAYS_POINTER:
36335 break;
36336 default:
36337 map_seen |= 1;
36338 error_at (OMP_CLAUSE_LOCATION (*pc),
36339 "%<#pragma omp target enter data%> with map-type other "
36340 "than %<to%> or %<alloc%> on %<map%> clause");
36341 *pc = OMP_CLAUSE_CHAIN (*pc);
36342 continue;
36343 }
36344 pc = &OMP_CLAUSE_CHAIN (*pc);
36345 }
36346
36347 if (map_seen != 3)
36348 {
36349 if (map_seen == 0)
36350 error_at (pragma_tok->location,
36351 "%<#pragma omp target enter data%> must contain at least "
36352 "one %<map%> clause");
36353 return NULL_TREE;
36354 }
36355
36356 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36357 TREE_TYPE (stmt) = void_type_node;
36358 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36359 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36360 return add_stmt (stmt);
36361 }
36362
36363 /* OpenMP 4.5:
36364 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36365 structured-block */
36366
36367 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36373
36374 static tree
36375 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36376 enum pragma_context context)
36377 {
36378 bool data_seen = false;
36379 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36380 {
36381 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36382 const char *p = IDENTIFIER_POINTER (id);
36383
36384 if (strcmp (p, "data") == 0)
36385 {
36386 cp_lexer_consume_token (parser->lexer);
36387 data_seen = true;
36388 }
36389 }
36390 if (!data_seen)
36391 {
36392 cp_parser_error (parser, "expected %<data%>");
36393 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36394 return NULL_TREE;
36395 }
36396
36397 if (context == pragma_stmt)
36398 {
36399 error_at (pragma_tok->location,
36400 "%<#pragma %s%> may only be used in compound statements",
36401 "omp target exit data");
36402 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36403 return NULL_TREE;
36404 }
36405
36406 tree clauses
36407 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36408 "#pragma omp target exit data", pragma_tok);
36409 int map_seen = 0;
36410 for (tree *pc = &clauses; *pc;)
36411 {
36412 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36413 switch (OMP_CLAUSE_MAP_KIND (*pc))
36414 {
36415 case GOMP_MAP_FROM:
36416 case GOMP_MAP_ALWAYS_FROM:
36417 case GOMP_MAP_RELEASE:
36418 case GOMP_MAP_DELETE:
36419 map_seen = 3;
36420 break;
36421 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36422 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36423 case GOMP_MAP_ALWAYS_POINTER:
36424 break;
36425 default:
36426 map_seen |= 1;
36427 error_at (OMP_CLAUSE_LOCATION (*pc),
36428 "%<#pragma omp target exit data%> with map-type other "
36429 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36430 " clause");
36431 *pc = OMP_CLAUSE_CHAIN (*pc);
36432 continue;
36433 }
36434 pc = &OMP_CLAUSE_CHAIN (*pc);
36435 }
36436
36437 if (map_seen != 3)
36438 {
36439 if (map_seen == 0)
36440 error_at (pragma_tok->location,
36441 "%<#pragma omp target exit data%> must contain at least "
36442 "one %<map%> clause");
36443 return NULL_TREE;
36444 }
36445
36446 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36447 TREE_TYPE (stmt) = void_type_node;
36448 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36449 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36450 return add_stmt (stmt);
36451 }
36452
36453 /* OpenMP 4.0:
36454 # pragma omp target update target-update-clause[optseq] new-line */
36455
36456 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36457 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36463
36464 static bool
36465 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36466 enum pragma_context context)
36467 {
36468 if (context == pragma_stmt)
36469 {
36470 error_at (pragma_tok->location,
36471 "%<#pragma %s%> may only be used in compound statements",
36472 "omp target update");
36473 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36474 return false;
36475 }
36476
36477 tree clauses
36478 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36479 "#pragma omp target update", pragma_tok);
36480 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36481 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36482 {
36483 error_at (pragma_tok->location,
36484 "%<#pragma omp target update%> must contain at least one "
36485 "%<from%> or %<to%> clauses");
36486 return false;
36487 }
36488
36489 tree stmt = make_node (OMP_TARGET_UPDATE);
36490 TREE_TYPE (stmt) = void_type_node;
36491 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36492 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36493 add_stmt (stmt);
36494 return false;
36495 }
36496
36497 /* OpenMP 4.0:
36498 # pragma omp target target-clause[optseq] new-line
36499 structured-block */
36500
36501 #define OMP_TARGET_CLAUSE_MASK \
36502 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36511
36512 static bool
36513 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36514 enum pragma_context context, bool *if_p)
36515 {
36516 tree *pc = NULL, stmt;
36517
36518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36519 {
36520 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36521 const char *p = IDENTIFIER_POINTER (id);
36522 enum tree_code ccode = ERROR_MARK;
36523
36524 if (strcmp (p, "teams") == 0)
36525 ccode = OMP_TEAMS;
36526 else if (strcmp (p, "parallel") == 0)
36527 ccode = OMP_PARALLEL;
36528 else if (strcmp (p, "simd") == 0)
36529 ccode = OMP_SIMD;
36530 if (ccode != ERROR_MARK)
36531 {
36532 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36533 char p_name[sizeof ("#pragma omp target teams distribute "
36534 "parallel for simd")];
36535
36536 cp_lexer_consume_token (parser->lexer);
36537 strcpy (p_name, "#pragma omp target");
36538 if (!flag_openmp) /* flag_openmp_simd */
36539 {
36540 tree stmt;
36541 switch (ccode)
36542 {
36543 case OMP_TEAMS:
36544 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36545 OMP_TARGET_CLAUSE_MASK,
36546 cclauses, if_p);
36547 break;
36548 case OMP_PARALLEL:
36549 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36550 OMP_TARGET_CLAUSE_MASK,
36551 cclauses, if_p);
36552 break;
36553 case OMP_SIMD:
36554 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36555 OMP_TARGET_CLAUSE_MASK,
36556 cclauses, if_p);
36557 break;
36558 default:
36559 gcc_unreachable ();
36560 }
36561 return stmt != NULL_TREE;
36562 }
36563 keep_next_level (true);
36564 tree sb = begin_omp_structured_block (), ret;
36565 unsigned save = cp_parser_begin_omp_structured_block (parser);
36566 switch (ccode)
36567 {
36568 case OMP_TEAMS:
36569 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36570 OMP_TARGET_CLAUSE_MASK, cclauses,
36571 if_p);
36572 break;
36573 case OMP_PARALLEL:
36574 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36575 OMP_TARGET_CLAUSE_MASK, cclauses,
36576 if_p);
36577 break;
36578 case OMP_SIMD:
36579 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36580 OMP_TARGET_CLAUSE_MASK, cclauses,
36581 if_p);
36582 break;
36583 default:
36584 gcc_unreachable ();
36585 }
36586 cp_parser_end_omp_structured_block (parser, save);
36587 tree body = finish_omp_structured_block (sb);
36588 if (ret == NULL_TREE)
36589 return false;
36590 if (ccode == OMP_TEAMS && !processing_template_decl)
36591 {
36592 /* For combined target teams, ensure the num_teams and
36593 thread_limit clause expressions are evaluated on the host,
36594 before entering the target construct. */
36595 tree c;
36596 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36597 c; c = OMP_CLAUSE_CHAIN (c))
36598 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36599 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36600 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36601 {
36602 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36603 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36604 if (expr == error_mark_node)
36605 continue;
36606 tree tmp = TARGET_EXPR_SLOT (expr);
36607 add_stmt (expr);
36608 OMP_CLAUSE_OPERAND (c, 0) = expr;
36609 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36610 OMP_CLAUSE_FIRSTPRIVATE);
36611 OMP_CLAUSE_DECL (tc) = tmp;
36612 OMP_CLAUSE_CHAIN (tc)
36613 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36614 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36615 }
36616 }
36617 tree stmt = make_node (OMP_TARGET);
36618 TREE_TYPE (stmt) = void_type_node;
36619 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36620 OMP_TARGET_BODY (stmt) = body;
36621 OMP_TARGET_COMBINED (stmt) = 1;
36622 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36623 add_stmt (stmt);
36624 pc = &OMP_TARGET_CLAUSES (stmt);
36625 goto check_clauses;
36626 }
36627 else if (!flag_openmp) /* flag_openmp_simd */
36628 {
36629 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36630 return false;
36631 }
36632 else if (strcmp (p, "data") == 0)
36633 {
36634 cp_lexer_consume_token (parser->lexer);
36635 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36636 return true;
36637 }
36638 else if (strcmp (p, "enter") == 0)
36639 {
36640 cp_lexer_consume_token (parser->lexer);
36641 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36642 return false;
36643 }
36644 else if (strcmp (p, "exit") == 0)
36645 {
36646 cp_lexer_consume_token (parser->lexer);
36647 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36648 return false;
36649 }
36650 else if (strcmp (p, "update") == 0)
36651 {
36652 cp_lexer_consume_token (parser->lexer);
36653 return cp_parser_omp_target_update (parser, pragma_tok, context);
36654 }
36655 }
36656 if (!flag_openmp) /* flag_openmp_simd */
36657 {
36658 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36659 return false;
36660 }
36661
36662 stmt = make_node (OMP_TARGET);
36663 TREE_TYPE (stmt) = void_type_node;
36664
36665 OMP_TARGET_CLAUSES (stmt)
36666 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36667 "#pragma omp target", pragma_tok);
36668 pc = &OMP_TARGET_CLAUSES (stmt);
36669 keep_next_level (true);
36670 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36671
36672 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36673 add_stmt (stmt);
36674
36675 check_clauses:
36676 while (*pc)
36677 {
36678 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36679 switch (OMP_CLAUSE_MAP_KIND (*pc))
36680 {
36681 case GOMP_MAP_TO:
36682 case GOMP_MAP_ALWAYS_TO:
36683 case GOMP_MAP_FROM:
36684 case GOMP_MAP_ALWAYS_FROM:
36685 case GOMP_MAP_TOFROM:
36686 case GOMP_MAP_ALWAYS_TOFROM:
36687 case GOMP_MAP_ALLOC:
36688 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36689 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36690 case GOMP_MAP_ALWAYS_POINTER:
36691 break;
36692 default:
36693 error_at (OMP_CLAUSE_LOCATION (*pc),
36694 "%<#pragma omp target%> with map-type other "
36695 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36696 "on %<map%> clause");
36697 *pc = OMP_CLAUSE_CHAIN (*pc);
36698 continue;
36699 }
36700 pc = &OMP_CLAUSE_CHAIN (*pc);
36701 }
36702 return true;
36703 }
36704
36705 /* OpenACC 2.0:
36706 # pragma acc cache (variable-list) new-line
36707 */
36708
36709 static tree
36710 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36711 {
36712 tree stmt, clauses;
36713
36714 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36715 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36716
36717 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36718
36719 stmt = make_node (OACC_CACHE);
36720 TREE_TYPE (stmt) = void_type_node;
36721 OACC_CACHE_CLAUSES (stmt) = clauses;
36722 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36723 add_stmt (stmt);
36724
36725 return stmt;
36726 }
36727
36728 /* OpenACC 2.0:
36729 # pragma acc data oacc-data-clause[optseq] new-line
36730 structured-block */
36731
36732 #define OACC_DATA_CLAUSE_MASK \
36733 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36744
36745 static tree
36746 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36747 {
36748 tree stmt, clauses, block;
36749 unsigned int save;
36750
36751 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36752 "#pragma acc data", pragma_tok);
36753
36754 block = begin_omp_parallel ();
36755 save = cp_parser_begin_omp_structured_block (parser);
36756 cp_parser_statement (parser, NULL_TREE, false, if_p);
36757 cp_parser_end_omp_structured_block (parser, save);
36758 stmt = finish_oacc_data (clauses, block);
36759 return stmt;
36760 }
36761
36762 /* OpenACC 2.0:
36763 # pragma acc host_data <clauses> new-line
36764 structured-block */
36765
36766 #define OACC_HOST_DATA_CLAUSE_MASK \
36767 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36768
36769 static tree
36770 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36771 {
36772 tree stmt, clauses, block;
36773 unsigned int save;
36774
36775 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36776 "#pragma acc host_data", pragma_tok);
36777
36778 block = begin_omp_parallel ();
36779 save = cp_parser_begin_omp_structured_block (parser);
36780 cp_parser_statement (parser, NULL_TREE, false, if_p);
36781 cp_parser_end_omp_structured_block (parser, save);
36782 stmt = finish_oacc_host_data (clauses, block);
36783 return stmt;
36784 }
36785
36786 /* OpenACC 2.0:
36787 # pragma acc declare oacc-data-clause[optseq] new-line
36788 */
36789
36790 #define OACC_DECLARE_CLAUSE_MASK \
36791 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36803
36804 static tree
36805 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36806 {
36807 tree clauses, stmt;
36808 bool error = false;
36809
36810 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36811 "#pragma acc declare", pragma_tok, true);
36812
36813
36814 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36815 {
36816 error_at (pragma_tok->location,
36817 "no valid clauses specified in %<#pragma acc declare%>");
36818 return NULL_TREE;
36819 }
36820
36821 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36822 {
36823 location_t loc = OMP_CLAUSE_LOCATION (t);
36824 tree decl = OMP_CLAUSE_DECL (t);
36825 if (!DECL_P (decl))
36826 {
36827 error_at (loc, "array section in %<#pragma acc declare%>");
36828 error = true;
36829 continue;
36830 }
36831 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36832 switch (OMP_CLAUSE_MAP_KIND (t))
36833 {
36834 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36835 case GOMP_MAP_FORCE_ALLOC:
36836 case GOMP_MAP_FORCE_TO:
36837 case GOMP_MAP_FORCE_DEVICEPTR:
36838 case GOMP_MAP_DEVICE_RESIDENT:
36839 break;
36840
36841 case GOMP_MAP_LINK:
36842 if (!global_bindings_p ()
36843 && (TREE_STATIC (decl)
36844 || !DECL_EXTERNAL (decl)))
36845 {
36846 error_at (loc,
36847 "%qD must be a global variable in "
36848 "%<#pragma acc declare link%>",
36849 decl);
36850 error = true;
36851 continue;
36852 }
36853 break;
36854
36855 default:
36856 if (global_bindings_p ())
36857 {
36858 error_at (loc, "invalid OpenACC clause at file scope");
36859 error = true;
36860 continue;
36861 }
36862 if (DECL_EXTERNAL (decl))
36863 {
36864 error_at (loc,
36865 "invalid use of %<extern%> variable %qD "
36866 "in %<#pragma acc declare%>", decl);
36867 error = true;
36868 continue;
36869 }
36870 else if (TREE_PUBLIC (decl))
36871 {
36872 error_at (loc,
36873 "invalid use of %<global%> variable %qD "
36874 "in %<#pragma acc declare%>", decl);
36875 error = true;
36876 continue;
36877 }
36878 break;
36879 }
36880
36881 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36882 || lookup_attribute ("omp declare target link",
36883 DECL_ATTRIBUTES (decl)))
36884 {
36885 error_at (loc, "variable %qD used more than once with "
36886 "%<#pragma acc declare%>", decl);
36887 error = true;
36888 continue;
36889 }
36890
36891 if (!error)
36892 {
36893 tree id;
36894
36895 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36896 id = get_identifier ("omp declare target link");
36897 else
36898 id = get_identifier ("omp declare target");
36899
36900 DECL_ATTRIBUTES (decl)
36901 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36902 if (global_bindings_p ())
36903 {
36904 symtab_node *node = symtab_node::get (decl);
36905 if (node != NULL)
36906 {
36907 node->offloadable = 1;
36908 if (ENABLE_OFFLOADING)
36909 {
36910 g->have_offload = true;
36911 if (is_a <varpool_node *> (node))
36912 vec_safe_push (offload_vars, decl);
36913 }
36914 }
36915 }
36916 }
36917 }
36918
36919 if (error || global_bindings_p ())
36920 return NULL_TREE;
36921
36922 stmt = make_node (OACC_DECLARE);
36923 TREE_TYPE (stmt) = void_type_node;
36924 OACC_DECLARE_CLAUSES (stmt) = clauses;
36925 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36926
36927 add_stmt (stmt);
36928
36929 return NULL_TREE;
36930 }
36931
36932 /* OpenACC 2.0:
36933 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36934
36935 or
36936
36937 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36938
36939 LOC is the location of the #pragma token.
36940 */
36941
36942 #define OACC_ENTER_DATA_CLAUSE_MASK \
36943 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36950
36951 #define OACC_EXIT_DATA_CLAUSE_MASK \
36952 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36953 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36957
36958 static tree
36959 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36960 bool enter)
36961 {
36962 location_t loc = pragma_tok->location;
36963 tree stmt, clauses;
36964 const char *p = "";
36965
36966 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36967 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36968
36969 if (strcmp (p, "data") != 0)
36970 {
36971 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36972 enter ? "enter" : "exit");
36973 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36974 return NULL_TREE;
36975 }
36976
36977 cp_lexer_consume_token (parser->lexer);
36978
36979 if (enter)
36980 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36981 "#pragma acc enter data", pragma_tok);
36982 else
36983 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36984 "#pragma acc exit data", pragma_tok);
36985
36986 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36987 {
36988 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36989 enter ? "enter" : "exit");
36990 return NULL_TREE;
36991 }
36992
36993 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36994 TREE_TYPE (stmt) = void_type_node;
36995 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36996 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36997 add_stmt (stmt);
36998 return stmt;
36999 }
37000
37001 /* OpenACC 2.0:
37002 # pragma acc loop oacc-loop-clause[optseq] new-line
37003 structured-block */
37004
37005 #define OACC_LOOP_CLAUSE_MASK \
37006 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
37013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
37014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
37016
37017 static tree
37018 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
37019 omp_clause_mask mask, tree *cclauses, bool *if_p)
37020 {
37021 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
37022
37023 strcat (p_name, " loop");
37024 mask |= OACC_LOOP_CLAUSE_MASK;
37025
37026 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
37027 cclauses == NULL);
37028 if (cclauses)
37029 {
37030 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
37031 if (*cclauses)
37032 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
37033 if (clauses)
37034 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
37035 }
37036
37037 tree block = begin_omp_structured_block ();
37038 int save = cp_parser_begin_omp_structured_block (parser);
37039 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
37040 cp_parser_end_omp_structured_block (parser, save);
37041 add_stmt (finish_omp_structured_block (block));
37042
37043 return stmt;
37044 }
37045
37046 /* OpenACC 2.0:
37047 # pragma acc kernels oacc-kernels-clause[optseq] new-line
37048 structured-block
37049
37050 or
37051
37052 # pragma acc parallel oacc-parallel-clause[optseq] new-line
37053 structured-block
37054 */
37055
37056 #define OACC_KERNELS_CLAUSE_MASK \
37057 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37074
37075 #define OACC_PARALLEL_CLAUSE_MASK \
37076 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
37078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
37079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
37080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
37081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
37082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
37083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
37084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
37086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
37087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
37088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
37089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
37090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
37091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37096
37097 static tree
37098 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37099 char *p_name, bool *if_p)
37100 {
37101 omp_clause_mask mask;
37102 enum tree_code code;
37103 switch (cp_parser_pragma_kind (pragma_tok))
37104 {
37105 case PRAGMA_OACC_KERNELS:
37106 strcat (p_name, " kernels");
37107 mask = OACC_KERNELS_CLAUSE_MASK;
37108 code = OACC_KERNELS;
37109 break;
37110 case PRAGMA_OACC_PARALLEL:
37111 strcat (p_name, " parallel");
37112 mask = OACC_PARALLEL_CLAUSE_MASK;
37113 code = OACC_PARALLEL;
37114 break;
37115 default:
37116 gcc_unreachable ();
37117 }
37118
37119 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37120 {
37121 const char *p
37122 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37123 if (strcmp (p, "loop") == 0)
37124 {
37125 cp_lexer_consume_token (parser->lexer);
37126 tree block = begin_omp_parallel ();
37127 tree clauses;
37128 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37129 if_p);
37130 return finish_omp_construct (code, block, clauses);
37131 }
37132 }
37133
37134 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37135
37136 tree block = begin_omp_parallel ();
37137 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37138 cp_parser_statement (parser, NULL_TREE, false, if_p);
37139 cp_parser_end_omp_structured_block (parser, save);
37140 return finish_omp_construct (code, block, clauses);
37141 }
37142
37143 /* OpenACC 2.0:
37144 # pragma acc update oacc-update-clause[optseq] new-line
37145 */
37146
37147 #define OACC_UPDATE_CLAUSE_MASK \
37148 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37154
37155 static tree
37156 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37157 {
37158 tree stmt, clauses;
37159
37160 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37161 "#pragma acc update", pragma_tok);
37162
37163 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37164 {
37165 error_at (pragma_tok->location,
37166 "%<#pragma acc update%> must contain at least one "
37167 "%<device%> or %<host%> or %<self%> clause");
37168 return NULL_TREE;
37169 }
37170
37171 stmt = make_node (OACC_UPDATE);
37172 TREE_TYPE (stmt) = void_type_node;
37173 OACC_UPDATE_CLAUSES (stmt) = clauses;
37174 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37175 add_stmt (stmt);
37176 return stmt;
37177 }
37178
37179 /* OpenACC 2.0:
37180 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37181
37182 LOC is the location of the #pragma token.
37183 */
37184
37185 #define OACC_WAIT_CLAUSE_MASK \
37186 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37187
37188 static tree
37189 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37190 {
37191 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37192 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37193
37194 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37195 list = cp_parser_oacc_wait_list (parser, loc, list);
37196
37197 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37198 "#pragma acc wait", pragma_tok);
37199
37200 stmt = c_finish_oacc_wait (loc, list, clauses);
37201 stmt = finish_expr_stmt (stmt);
37202
37203 return stmt;
37204 }
37205
37206 /* OpenMP 4.0:
37207 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37208
37209 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37210 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37216
37217 static void
37218 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37219 enum pragma_context context)
37220 {
37221 bool first_p = parser->omp_declare_simd == NULL;
37222 cp_omp_declare_simd_data data;
37223 if (first_p)
37224 {
37225 data.error_seen = false;
37226 data.fndecl_seen = false;
37227 data.tokens = vNULL;
37228 data.clauses = NULL_TREE;
37229 /* It is safe to take the address of a local variable; it will only be
37230 used while this scope is live. */
37231 parser->omp_declare_simd = &data;
37232 }
37233
37234 /* Store away all pragma tokens. */
37235 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37236 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37237 cp_lexer_consume_token (parser->lexer);
37238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37239 parser->omp_declare_simd->error_seen = true;
37240 cp_parser_require_pragma_eol (parser, pragma_tok);
37241 struct cp_token_cache *cp
37242 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37243 parser->omp_declare_simd->tokens.safe_push (cp);
37244
37245 if (first_p)
37246 {
37247 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37248 cp_parser_pragma (parser, context, NULL);
37249 switch (context)
37250 {
37251 case pragma_external:
37252 cp_parser_declaration (parser);
37253 break;
37254 case pragma_member:
37255 cp_parser_member_declaration (parser);
37256 break;
37257 case pragma_objc_icode:
37258 cp_parser_block_declaration (parser, /*statement_p=*/false);
37259 break;
37260 default:
37261 cp_parser_declaration_statement (parser);
37262 break;
37263 }
37264 if (parser->omp_declare_simd
37265 && !parser->omp_declare_simd->error_seen
37266 && !parser->omp_declare_simd->fndecl_seen)
37267 error_at (pragma_tok->location,
37268 "%<#pragma omp declare simd%> not immediately followed by "
37269 "function declaration or definition");
37270 data.tokens.release ();
37271 parser->omp_declare_simd = NULL;
37272 }
37273 }
37274
37275 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
37276 This function is modelled similar to the late parsing of omp declare
37277 simd. */
37278
37279 static tree
37280 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
37281 {
37282 struct cp_token_cache *ce;
37283 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
37284 int ii = 0;
37285
37286 if (parser->omp_declare_simd != NULL
37287 || lookup_attribute ("simd", attrs))
37288 {
37289 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
37290 "used in the same function marked as a Cilk Plus SIMD-enabled "
37291 "function");
37292 parser->cilk_simd_fn_info->tokens.release ();
37293 XDELETE (parser->cilk_simd_fn_info);
37294 parser->cilk_simd_fn_info = NULL;
37295 return attrs;
37296 }
37297 if (!info->error_seen && info->fndecl_seen)
37298 {
37299 error ("vector attribute not immediately followed by a single function"
37300 " declaration or definition");
37301 info->error_seen = true;
37302 }
37303 if (info->error_seen)
37304 return attrs;
37305
37306 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
37307 {
37308 tree c, cl;
37309
37310 cp_parser_push_lexer_for_tokens (parser, ce);
37311 parser->lexer->in_pragma = true;
37312 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
37313 "SIMD-enabled functions attribute",
37314 NULL);
37315 cp_parser_pop_lexer (parser);
37316 if (cl)
37317 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37318
37319 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
37320 TREE_CHAIN (c) = attrs;
37321 attrs = c;
37322
37323 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37324 TREE_CHAIN (c) = attrs;
37325 if (processing_template_decl)
37326 ATTR_IS_DEPENDENT (c) = 1;
37327 attrs = c;
37328 }
37329 info->fndecl_seen = true;
37330 parser->cilk_simd_fn_info->tokens.release ();
37331 XDELETE (parser->cilk_simd_fn_info);
37332 parser->cilk_simd_fn_info = NULL;
37333 return attrs;
37334 }
37335
37336 /* Finalize #pragma omp declare simd clauses after direct declarator has
37337 been parsed, and put that into "omp declare simd" attribute. */
37338
37339 static tree
37340 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37341 {
37342 struct cp_token_cache *ce;
37343 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37344 int i;
37345
37346 if (!data->error_seen && data->fndecl_seen)
37347 {
37348 error ("%<#pragma omp declare simd%> not immediately followed by "
37349 "a single function declaration or definition");
37350 data->error_seen = true;
37351 }
37352 if (data->error_seen)
37353 return attrs;
37354
37355 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37356 {
37357 tree c, cl;
37358
37359 cp_parser_push_lexer_for_tokens (parser, ce);
37360 parser->lexer->in_pragma = true;
37361 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37362 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37363 cp_lexer_consume_token (parser->lexer);
37364 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37365 "#pragma omp declare simd", pragma_tok);
37366 cp_parser_pop_lexer (parser);
37367 if (cl)
37368 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37369 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37370 TREE_CHAIN (c) = attrs;
37371 if (processing_template_decl)
37372 ATTR_IS_DEPENDENT (c) = 1;
37373 attrs = c;
37374 }
37375
37376 data->fndecl_seen = true;
37377 return attrs;
37378 }
37379
37380
37381 /* OpenMP 4.0:
37382 # pragma omp declare target new-line
37383 declarations and definitions
37384 # pragma omp end declare target new-line
37385
37386 OpenMP 4.5:
37387 # pragma omp declare target ( extended-list ) new-line
37388
37389 # pragma omp declare target declare-target-clauses[seq] new-line */
37390
37391 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37392 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37394
37395 static void
37396 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37397 {
37398 tree clauses = NULL_TREE;
37399 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37400 clauses
37401 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37402 "#pragma omp declare target", pragma_tok);
37403 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37404 {
37405 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37406 clauses);
37407 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37408 cp_parser_require_pragma_eol (parser, pragma_tok);
37409 }
37410 else
37411 {
37412 cp_parser_require_pragma_eol (parser, pragma_tok);
37413 scope_chain->omp_declare_target_attribute++;
37414 return;
37415 }
37416 if (scope_chain->omp_declare_target_attribute)
37417 error_at (pragma_tok->location,
37418 "%<#pragma omp declare target%> with clauses in between "
37419 "%<#pragma omp declare target%> without clauses and "
37420 "%<#pragma omp end declare target%>");
37421 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37422 {
37423 tree t = OMP_CLAUSE_DECL (c), id;
37424 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37425 tree at2 = lookup_attribute ("omp declare target link",
37426 DECL_ATTRIBUTES (t));
37427 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37428 {
37429 id = get_identifier ("omp declare target link");
37430 std::swap (at1, at2);
37431 }
37432 else
37433 id = get_identifier ("omp declare target");
37434 if (at2)
37435 {
37436 error_at (OMP_CLAUSE_LOCATION (c),
37437 "%qD specified both in declare target %<link%> and %<to%>"
37438 " clauses", t);
37439 continue;
37440 }
37441 if (!at1)
37442 {
37443 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37444 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37445 continue;
37446
37447 symtab_node *node = symtab_node::get (t);
37448 if (node != NULL)
37449 {
37450 node->offloadable = 1;
37451 if (ENABLE_OFFLOADING)
37452 {
37453 g->have_offload = true;
37454 if (is_a <varpool_node *> (node))
37455 vec_safe_push (offload_vars, t);
37456 }
37457 }
37458 }
37459 }
37460 }
37461
37462 static void
37463 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37464 {
37465 const char *p = "";
37466 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37467 {
37468 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37469 p = IDENTIFIER_POINTER (id);
37470 }
37471 if (strcmp (p, "declare") == 0)
37472 {
37473 cp_lexer_consume_token (parser->lexer);
37474 p = "";
37475 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37476 {
37477 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37478 p = IDENTIFIER_POINTER (id);
37479 }
37480 if (strcmp (p, "target") == 0)
37481 cp_lexer_consume_token (parser->lexer);
37482 else
37483 {
37484 cp_parser_error (parser, "expected %<target%>");
37485 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37486 return;
37487 }
37488 }
37489 else
37490 {
37491 cp_parser_error (parser, "expected %<declare%>");
37492 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37493 return;
37494 }
37495 cp_parser_require_pragma_eol (parser, pragma_tok);
37496 if (!scope_chain->omp_declare_target_attribute)
37497 error_at (pragma_tok->location,
37498 "%<#pragma omp end declare target%> without corresponding "
37499 "%<#pragma omp declare target%>");
37500 else
37501 scope_chain->omp_declare_target_attribute--;
37502 }
37503
37504 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37505 expression and optional initializer clause of
37506 #pragma omp declare reduction. We store the expression(s) as
37507 either 3, 6 or 7 special statements inside of the artificial function's
37508 body. The first two statements are DECL_EXPRs for the artificial
37509 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37510 expression that uses those variables.
37511 If there was any INITIALIZER clause, this is followed by further statements,
37512 the fourth and fifth statements are DECL_EXPRs for the artificial
37513 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37514 constructor variant (first token after open paren is not omp_priv),
37515 then the sixth statement is a statement with the function call expression
37516 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37517 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37518 to initialize the OMP_PRIV artificial variable and there is seventh
37519 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37520
37521 static bool
37522 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37523 {
37524 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37525 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37526 type = TREE_TYPE (type);
37527 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37528 DECL_ARTIFICIAL (omp_out) = 1;
37529 pushdecl (omp_out);
37530 add_decl_expr (omp_out);
37531 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37532 DECL_ARTIFICIAL (omp_in) = 1;
37533 pushdecl (omp_in);
37534 add_decl_expr (omp_in);
37535 tree combiner;
37536 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37537
37538 keep_next_level (true);
37539 tree block = begin_omp_structured_block ();
37540 combiner = cp_parser_expression (parser);
37541 finish_expr_stmt (combiner);
37542 block = finish_omp_structured_block (block);
37543 add_stmt (block);
37544
37545 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37546 return false;
37547
37548 const char *p = "";
37549 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37550 {
37551 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37552 p = IDENTIFIER_POINTER (id);
37553 }
37554
37555 if (strcmp (p, "initializer") == 0)
37556 {
37557 cp_lexer_consume_token (parser->lexer);
37558 matching_parens parens;
37559 if (!parens.require_open (parser))
37560 return false;
37561
37562 p = "";
37563 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37564 {
37565 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37566 p = IDENTIFIER_POINTER (id);
37567 }
37568
37569 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37570 DECL_ARTIFICIAL (omp_priv) = 1;
37571 pushdecl (omp_priv);
37572 add_decl_expr (omp_priv);
37573 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37574 DECL_ARTIFICIAL (omp_orig) = 1;
37575 pushdecl (omp_orig);
37576 add_decl_expr (omp_orig);
37577
37578 keep_next_level (true);
37579 block = begin_omp_structured_block ();
37580
37581 bool ctor = false;
37582 if (strcmp (p, "omp_priv") == 0)
37583 {
37584 bool is_direct_init, is_non_constant_init;
37585 ctor = true;
37586 cp_lexer_consume_token (parser->lexer);
37587 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37588 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37589 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37590 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37591 == CPP_CLOSE_PAREN
37592 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37593 == CPP_CLOSE_PAREN))
37594 {
37595 finish_omp_structured_block (block);
37596 error ("invalid initializer clause");
37597 return false;
37598 }
37599 initializer = cp_parser_initializer (parser, &is_direct_init,
37600 &is_non_constant_init);
37601 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37602 NULL_TREE, LOOKUP_ONLYCONVERTING);
37603 }
37604 else
37605 {
37606 cp_parser_parse_tentatively (parser);
37607 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37608 /*check_dependency_p=*/true,
37609 /*template_p=*/NULL,
37610 /*declarator_p=*/false,
37611 /*optional_p=*/false);
37612 vec<tree, va_gc> *args;
37613 if (fn_name == error_mark_node
37614 || cp_parser_error_occurred (parser)
37615 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37616 || ((args = cp_parser_parenthesized_expression_list
37617 (parser, non_attr, /*cast_p=*/false,
37618 /*allow_expansion_p=*/true,
37619 /*non_constant_p=*/NULL)),
37620 cp_parser_error_occurred (parser)))
37621 {
37622 finish_omp_structured_block (block);
37623 cp_parser_abort_tentative_parse (parser);
37624 cp_parser_error (parser, "expected id-expression (arguments)");
37625 return false;
37626 }
37627 unsigned int i;
37628 tree arg;
37629 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37630 if (arg == omp_priv
37631 || (TREE_CODE (arg) == ADDR_EXPR
37632 && TREE_OPERAND (arg, 0) == omp_priv))
37633 break;
37634 cp_parser_abort_tentative_parse (parser);
37635 if (arg == NULL_TREE)
37636 error ("one of the initializer call arguments should be %<omp_priv%>"
37637 " or %<&omp_priv%>");
37638 initializer = cp_parser_postfix_expression (parser, false, false, false,
37639 false, NULL);
37640 finish_expr_stmt (initializer);
37641 }
37642
37643 block = finish_omp_structured_block (block);
37644 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37645 add_stmt (block);
37646
37647 if (ctor)
37648 add_decl_expr (omp_orig);
37649
37650 if (!parens.require_close (parser))
37651 return false;
37652 }
37653
37654 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37655 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37656 UNKNOWN_LOCATION);
37657
37658 return true;
37659 }
37660
37661 /* OpenMP 4.0
37662 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37663 initializer-clause[opt] new-line
37664
37665 initializer-clause:
37666 initializer (omp_priv initializer)
37667 initializer (function-name (argument-list)) */
37668
37669 static void
37670 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37671 enum pragma_context)
37672 {
37673 auto_vec<tree> types;
37674 enum tree_code reduc_code = ERROR_MARK;
37675 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37676 unsigned int i;
37677 cp_token *first_token;
37678 cp_token_cache *cp;
37679 int errs;
37680 void *p;
37681
37682 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37683 p = obstack_alloc (&declarator_obstack, 0);
37684
37685 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37686 goto fail;
37687
37688 switch (cp_lexer_peek_token (parser->lexer)->type)
37689 {
37690 case CPP_PLUS:
37691 reduc_code = PLUS_EXPR;
37692 break;
37693 case CPP_MULT:
37694 reduc_code = MULT_EXPR;
37695 break;
37696 case CPP_MINUS:
37697 reduc_code = MINUS_EXPR;
37698 break;
37699 case CPP_AND:
37700 reduc_code = BIT_AND_EXPR;
37701 break;
37702 case CPP_XOR:
37703 reduc_code = BIT_XOR_EXPR;
37704 break;
37705 case CPP_OR:
37706 reduc_code = BIT_IOR_EXPR;
37707 break;
37708 case CPP_AND_AND:
37709 reduc_code = TRUTH_ANDIF_EXPR;
37710 break;
37711 case CPP_OR_OR:
37712 reduc_code = TRUTH_ORIF_EXPR;
37713 break;
37714 case CPP_NAME:
37715 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37716 break;
37717 default:
37718 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37719 "%<|%>, %<&&%>, %<||%> or identifier");
37720 goto fail;
37721 }
37722
37723 if (reduc_code != ERROR_MARK)
37724 cp_lexer_consume_token (parser->lexer);
37725
37726 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37727 if (reduc_id == error_mark_node)
37728 goto fail;
37729
37730 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37731 goto fail;
37732
37733 /* Types may not be defined in declare reduction type list. */
37734 const char *saved_message;
37735 saved_message = parser->type_definition_forbidden_message;
37736 parser->type_definition_forbidden_message
37737 = G_("types may not be defined in declare reduction type list");
37738 bool saved_colon_corrects_to_scope_p;
37739 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37740 parser->colon_corrects_to_scope_p = false;
37741 bool saved_colon_doesnt_start_class_def_p;
37742 saved_colon_doesnt_start_class_def_p
37743 = parser->colon_doesnt_start_class_def_p;
37744 parser->colon_doesnt_start_class_def_p = true;
37745
37746 while (true)
37747 {
37748 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37749 type = cp_parser_type_id (parser);
37750 if (type == error_mark_node)
37751 ;
37752 else if (ARITHMETIC_TYPE_P (type)
37753 && (orig_reduc_id == NULL_TREE
37754 || (TREE_CODE (type) != COMPLEX_TYPE
37755 && (id_equal (orig_reduc_id, "min")
37756 || id_equal (orig_reduc_id, "max")))))
37757 error_at (loc, "predeclared arithmetic type %qT in "
37758 "%<#pragma omp declare reduction%>", type);
37759 else if (TREE_CODE (type) == FUNCTION_TYPE
37760 || TREE_CODE (type) == METHOD_TYPE
37761 || TREE_CODE (type) == ARRAY_TYPE)
37762 error_at (loc, "function or array type %qT in "
37763 "%<#pragma omp declare reduction%>", type);
37764 else if (TREE_CODE (type) == REFERENCE_TYPE)
37765 error_at (loc, "reference type %qT in "
37766 "%<#pragma omp declare reduction%>", type);
37767 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37768 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37769 "%<#pragma omp declare reduction%>", type);
37770 else
37771 types.safe_push (type);
37772
37773 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37774 cp_lexer_consume_token (parser->lexer);
37775 else
37776 break;
37777 }
37778
37779 /* Restore the saved message. */
37780 parser->type_definition_forbidden_message = saved_message;
37781 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37782 parser->colon_doesnt_start_class_def_p
37783 = saved_colon_doesnt_start_class_def_p;
37784
37785 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37786 || types.is_empty ())
37787 {
37788 fail:
37789 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37790 goto done;
37791 }
37792
37793 first_token = cp_lexer_peek_token (parser->lexer);
37794 cp = NULL;
37795 errs = errorcount;
37796 FOR_EACH_VEC_ELT (types, i, type)
37797 {
37798 tree fntype
37799 = build_function_type_list (void_type_node,
37800 cp_build_reference_type (type, false),
37801 NULL_TREE);
37802 tree this_reduc_id = reduc_id;
37803 if (!dependent_type_p (type))
37804 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37805 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37806 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37807 DECL_ARTIFICIAL (fndecl) = 1;
37808 DECL_EXTERNAL (fndecl) = 1;
37809 DECL_DECLARED_INLINE_P (fndecl) = 1;
37810 DECL_IGNORED_P (fndecl) = 1;
37811 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37812 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37813 DECL_ATTRIBUTES (fndecl)
37814 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37815 DECL_ATTRIBUTES (fndecl));
37816 if (processing_template_decl)
37817 fndecl = push_template_decl (fndecl);
37818 bool block_scope = false;
37819 tree block = NULL_TREE;
37820 if (current_function_decl)
37821 {
37822 block_scope = true;
37823 DECL_CONTEXT (fndecl) = global_namespace;
37824 if (!processing_template_decl)
37825 pushdecl (fndecl);
37826 }
37827 else if (current_class_type)
37828 {
37829 if (cp == NULL)
37830 {
37831 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37832 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37833 cp_lexer_consume_token (parser->lexer);
37834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37835 goto fail;
37836 cp = cp_token_cache_new (first_token,
37837 cp_lexer_peek_nth_token (parser->lexer,
37838 2));
37839 }
37840 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37841 finish_member_declaration (fndecl);
37842 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37843 DECL_PENDING_INLINE_P (fndecl) = 1;
37844 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37845 continue;
37846 }
37847 else
37848 {
37849 DECL_CONTEXT (fndecl) = current_namespace;
37850 pushdecl (fndecl);
37851 }
37852 if (!block_scope)
37853 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37854 else
37855 block = begin_omp_structured_block ();
37856 if (cp)
37857 {
37858 cp_parser_push_lexer_for_tokens (parser, cp);
37859 parser->lexer->in_pragma = true;
37860 }
37861 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37862 {
37863 if (!block_scope)
37864 finish_function (/*inline_p=*/false);
37865 else
37866 DECL_CONTEXT (fndecl) = current_function_decl;
37867 if (cp)
37868 cp_parser_pop_lexer (parser);
37869 goto fail;
37870 }
37871 if (cp)
37872 cp_parser_pop_lexer (parser);
37873 if (!block_scope)
37874 finish_function (/*inline_p=*/false);
37875 else
37876 {
37877 DECL_CONTEXT (fndecl) = current_function_decl;
37878 block = finish_omp_structured_block (block);
37879 if (TREE_CODE (block) == BIND_EXPR)
37880 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37881 else if (TREE_CODE (block) == STATEMENT_LIST)
37882 DECL_SAVED_TREE (fndecl) = block;
37883 if (processing_template_decl)
37884 add_decl_expr (fndecl);
37885 }
37886 cp_check_omp_declare_reduction (fndecl);
37887 if (cp == NULL && types.length () > 1)
37888 cp = cp_token_cache_new (first_token,
37889 cp_lexer_peek_nth_token (parser->lexer, 2));
37890 if (errs != errorcount)
37891 break;
37892 }
37893
37894 cp_parser_require_pragma_eol (parser, pragma_tok);
37895
37896 done:
37897 /* Free any declarators allocated. */
37898 obstack_free (&declarator_obstack, p);
37899 }
37900
37901 /* OpenMP 4.0
37902 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37903 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37904 initializer-clause[opt] new-line
37905 #pragma omp declare target new-line */
37906
37907 static bool
37908 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37909 enum pragma_context context)
37910 {
37911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37912 {
37913 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37914 const char *p = IDENTIFIER_POINTER (id);
37915
37916 if (strcmp (p, "simd") == 0)
37917 {
37918 cp_lexer_consume_token (parser->lexer);
37919 cp_parser_omp_declare_simd (parser, pragma_tok,
37920 context);
37921 return true;
37922 }
37923 cp_ensure_no_omp_declare_simd (parser);
37924 if (strcmp (p, "reduction") == 0)
37925 {
37926 cp_lexer_consume_token (parser->lexer);
37927 cp_parser_omp_declare_reduction (parser, pragma_tok,
37928 context);
37929 return false;
37930 }
37931 if (!flag_openmp) /* flag_openmp_simd */
37932 {
37933 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37934 return false;
37935 }
37936 if (strcmp (p, "target") == 0)
37937 {
37938 cp_lexer_consume_token (parser->lexer);
37939 cp_parser_omp_declare_target (parser, pragma_tok);
37940 return false;
37941 }
37942 }
37943 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37944 "or %<target%>");
37945 cp_parser_require_pragma_eol (parser, pragma_tok);
37946 return false;
37947 }
37948
37949 /* OpenMP 4.5:
37950 #pragma omp taskloop taskloop-clause[optseq] new-line
37951 for-loop
37952
37953 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37954 for-loop */
37955
37956 #define OMP_TASKLOOP_CLAUSE_MASK \
37957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37971
37972 static tree
37973 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37974 char *p_name, omp_clause_mask mask, tree *cclauses,
37975 bool *if_p)
37976 {
37977 tree clauses, sb, ret;
37978 unsigned int save;
37979 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37980
37981 strcat (p_name, " taskloop");
37982 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37983
37984 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37985 {
37986 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37987 const char *p = IDENTIFIER_POINTER (id);
37988
37989 if (strcmp (p, "simd") == 0)
37990 {
37991 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37992 if (cclauses == NULL)
37993 cclauses = cclauses_buf;
37994
37995 cp_lexer_consume_token (parser->lexer);
37996 if (!flag_openmp) /* flag_openmp_simd */
37997 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37998 cclauses, if_p);
37999 sb = begin_omp_structured_block ();
38000 save = cp_parser_begin_omp_structured_block (parser);
38001 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
38002 cclauses, if_p);
38003 cp_parser_end_omp_structured_block (parser, save);
38004 tree body = finish_omp_structured_block (sb);
38005 if (ret == NULL)
38006 return ret;
38007 ret = make_node (OMP_TASKLOOP);
38008 TREE_TYPE (ret) = void_type_node;
38009 OMP_FOR_BODY (ret) = body;
38010 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38011 SET_EXPR_LOCATION (ret, loc);
38012 add_stmt (ret);
38013 return ret;
38014 }
38015 }
38016 if (!flag_openmp) /* flag_openmp_simd */
38017 {
38018 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38019 return NULL_TREE;
38020 }
38021
38022 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38023 cclauses == NULL);
38024 if (cclauses)
38025 {
38026 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
38027 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
38028 }
38029
38030 sb = begin_omp_structured_block ();
38031 save = cp_parser_begin_omp_structured_block (parser);
38032
38033 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
38034 if_p);
38035
38036 cp_parser_end_omp_structured_block (parser, save);
38037 add_stmt (finish_omp_structured_block (sb));
38038
38039 return ret;
38040 }
38041
38042
38043 /* OpenACC 2.0:
38044 # pragma acc routine oacc-routine-clause[optseq] new-line
38045 function-definition
38046
38047 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
38048 */
38049
38050 #define OACC_ROUTINE_CLAUSE_MASK \
38051 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
38055
38056
38057 /* Parse the OpenACC routine pragma. This has an optional '( name )'
38058 component, which must resolve to a declared namespace-scope
38059 function. The clauses are either processed directly (for a named
38060 function), or defered until the immediatley following declaration
38061 is parsed. */
38062
38063 static void
38064 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
38065 enum pragma_context context)
38066 {
38067 gcc_checking_assert (context == pragma_external);
38068 /* The checking for "another pragma following this one" in the "no optional
38069 '( name )'" case makes sure that we dont re-enter. */
38070 gcc_checking_assert (parser->oacc_routine == NULL);
38071
38072 cp_oacc_routine_data data;
38073 data.error_seen = false;
38074 data.fndecl_seen = false;
38075 data.tokens = vNULL;
38076 data.clauses = NULL_TREE;
38077 data.loc = pragma_tok->location;
38078 /* It is safe to take the address of a local variable; it will only be
38079 used while this scope is live. */
38080 parser->oacc_routine = &data;
38081
38082 /* Look for optional '( name )'. */
38083 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38084 {
38085 matching_parens parens;
38086 parens.consume_open (parser); /* '(' */
38087
38088 /* We parse the name as an id-expression. If it resolves to
38089 anything other than a non-overloaded function at namespace
38090 scope, it's an error. */
38091 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
38092 tree name = cp_parser_id_expression (parser,
38093 /*template_keyword_p=*/false,
38094 /*check_dependency_p=*/false,
38095 /*template_p=*/NULL,
38096 /*declarator_p=*/false,
38097 /*optional_p=*/false);
38098 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
38099 if (name != error_mark_node && decl == error_mark_node)
38100 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38101
38102 if (decl == error_mark_node
38103 || !parens.require_close (parser))
38104 {
38105 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38106 parser->oacc_routine = NULL;
38107 return;
38108 }
38109
38110 data.clauses
38111 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38112 "#pragma acc routine",
38113 cp_lexer_peek_token (parser->lexer));
38114
38115 if (decl && is_overloaded_fn (decl)
38116 && (TREE_CODE (decl) != FUNCTION_DECL
38117 || DECL_FUNCTION_TEMPLATE_P (decl)))
38118 {
38119 error_at (name_loc,
38120 "%<#pragma acc routine%> names a set of overloads");
38121 parser->oacc_routine = NULL;
38122 return;
38123 }
38124
38125 /* Perhaps we should use the same rule as declarations in different
38126 namespaces? */
38127 if (!DECL_NAMESPACE_SCOPE_P (decl))
38128 {
38129 error_at (name_loc,
38130 "%qD does not refer to a namespace scope function", decl);
38131 parser->oacc_routine = NULL;
38132 return;
38133 }
38134
38135 if (TREE_CODE (decl) != FUNCTION_DECL)
38136 {
38137 error_at (name_loc, "%qD does not refer to a function", decl);
38138 parser->oacc_routine = NULL;
38139 return;
38140 }
38141
38142 cp_finalize_oacc_routine (parser, decl, false);
38143 parser->oacc_routine = NULL;
38144 }
38145 else /* No optional '( name )'. */
38146 {
38147 /* Store away all pragma tokens. */
38148 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38149 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38150 cp_lexer_consume_token (parser->lexer);
38151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38152 parser->oacc_routine->error_seen = true;
38153 cp_parser_require_pragma_eol (parser, pragma_tok);
38154 struct cp_token_cache *cp
38155 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38156 parser->oacc_routine->tokens.safe_push (cp);
38157
38158 /* Emit a helpful diagnostic if there's another pragma following this
38159 one. */
38160 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38161 {
38162 cp_ensure_no_oacc_routine (parser);
38163 data.tokens.release ();
38164 /* ..., and then just keep going. */
38165 return;
38166 }
38167
38168 /* We only have to consider the pragma_external case here. */
38169 cp_parser_declaration (parser);
38170 if (parser->oacc_routine
38171 && !parser->oacc_routine->fndecl_seen)
38172 cp_ensure_no_oacc_routine (parser);
38173 else
38174 parser->oacc_routine = NULL;
38175 data.tokens.release ();
38176 }
38177 }
38178
38179 /* Finalize #pragma acc routine clauses after direct declarator has
38180 been parsed. */
38181
38182 static tree
38183 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38184 {
38185 struct cp_token_cache *ce;
38186 cp_oacc_routine_data *data = parser->oacc_routine;
38187
38188 if (!data->error_seen && data->fndecl_seen)
38189 {
38190 error_at (data->loc,
38191 "%<#pragma acc routine%> not immediately followed by "
38192 "a single function declaration or definition");
38193 data->error_seen = true;
38194 }
38195 if (data->error_seen)
38196 return attrs;
38197
38198 gcc_checking_assert (data->tokens.length () == 1);
38199 ce = data->tokens[0];
38200
38201 cp_parser_push_lexer_for_tokens (parser, ce);
38202 parser->lexer->in_pragma = true;
38203 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38204
38205 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38206 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38207 parser->oacc_routine->clauses
38208 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38209 "#pragma acc routine", pragma_tok);
38210 cp_parser_pop_lexer (parser);
38211 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38212 fndecl_seen. */
38213
38214 return attrs;
38215 }
38216
38217 /* Apply any saved OpenACC routine clauses to a just-parsed
38218 declaration. */
38219
38220 static void
38221 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38222 {
38223 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38224 {
38225 /* Keep going if we're in error reporting mode. */
38226 if (parser->oacc_routine->error_seen
38227 || fndecl == error_mark_node)
38228 return;
38229
38230 if (parser->oacc_routine->fndecl_seen)
38231 {
38232 error_at (parser->oacc_routine->loc,
38233 "%<#pragma acc routine%> not immediately followed by"
38234 " a single function declaration or definition");
38235 parser->oacc_routine = NULL;
38236 return;
38237 }
38238 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38239 {
38240 cp_ensure_no_oacc_routine (parser);
38241 return;
38242 }
38243
38244 if (oacc_get_fn_attrib (fndecl))
38245 {
38246 error_at (parser->oacc_routine->loc,
38247 "%<#pragma acc routine%> already applied to %qD", fndecl);
38248 parser->oacc_routine = NULL;
38249 return;
38250 }
38251
38252 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38253 {
38254 error_at (parser->oacc_routine->loc,
38255 TREE_USED (fndecl)
38256 ? G_("%<#pragma acc routine%> must be applied before use")
38257 : G_("%<#pragma acc routine%> must be applied before "
38258 "definition"));
38259 parser->oacc_routine = NULL;
38260 return;
38261 }
38262
38263 /* Process the routine's dimension clauses. */
38264 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38265 oacc_replace_fn_attrib (fndecl, dims);
38266
38267 /* Add an "omp declare target" attribute. */
38268 DECL_ATTRIBUTES (fndecl)
38269 = tree_cons (get_identifier ("omp declare target"),
38270 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38271
38272 /* Don't unset parser->oacc_routine here: we may still need it to
38273 diagnose wrong usage. But, remember that we've used this "#pragma acc
38274 routine". */
38275 parser->oacc_routine->fndecl_seen = true;
38276 }
38277 }
38278
38279 /* Main entry point to OpenMP statement pragmas. */
38280
38281 static void
38282 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38283 {
38284 tree stmt;
38285 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38286 omp_clause_mask mask (0);
38287
38288 switch (cp_parser_pragma_kind (pragma_tok))
38289 {
38290 case PRAGMA_OACC_ATOMIC:
38291 cp_parser_omp_atomic (parser, pragma_tok);
38292 return;
38293 case PRAGMA_OACC_CACHE:
38294 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38295 break;
38296 case PRAGMA_OACC_DATA:
38297 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38298 break;
38299 case PRAGMA_OACC_ENTER_DATA:
38300 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38301 break;
38302 case PRAGMA_OACC_EXIT_DATA:
38303 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38304 break;
38305 case PRAGMA_OACC_HOST_DATA:
38306 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38307 break;
38308 case PRAGMA_OACC_KERNELS:
38309 case PRAGMA_OACC_PARALLEL:
38310 strcpy (p_name, "#pragma acc");
38311 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38312 if_p);
38313 break;
38314 case PRAGMA_OACC_LOOP:
38315 strcpy (p_name, "#pragma acc");
38316 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38317 if_p);
38318 break;
38319 case PRAGMA_OACC_UPDATE:
38320 stmt = cp_parser_oacc_update (parser, pragma_tok);
38321 break;
38322 case PRAGMA_OACC_WAIT:
38323 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38324 break;
38325 case PRAGMA_OMP_ATOMIC:
38326 cp_parser_omp_atomic (parser, pragma_tok);
38327 return;
38328 case PRAGMA_OMP_CRITICAL:
38329 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38330 break;
38331 case PRAGMA_OMP_DISTRIBUTE:
38332 strcpy (p_name, "#pragma omp");
38333 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38334 if_p);
38335 break;
38336 case PRAGMA_OMP_FOR:
38337 strcpy (p_name, "#pragma omp");
38338 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38339 if_p);
38340 break;
38341 case PRAGMA_OMP_MASTER:
38342 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38343 break;
38344 case PRAGMA_OMP_PARALLEL:
38345 strcpy (p_name, "#pragma omp");
38346 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38347 if_p);
38348 break;
38349 case PRAGMA_OMP_SECTIONS:
38350 strcpy (p_name, "#pragma omp");
38351 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38352 break;
38353 case PRAGMA_OMP_SIMD:
38354 strcpy (p_name, "#pragma omp");
38355 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38356 if_p);
38357 break;
38358 case PRAGMA_OMP_SINGLE:
38359 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38360 break;
38361 case PRAGMA_OMP_TASK:
38362 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38363 break;
38364 case PRAGMA_OMP_TASKGROUP:
38365 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38366 break;
38367 case PRAGMA_OMP_TASKLOOP:
38368 strcpy (p_name, "#pragma omp");
38369 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38370 if_p);
38371 break;
38372 case PRAGMA_OMP_TEAMS:
38373 strcpy (p_name, "#pragma omp");
38374 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38375 if_p);
38376 break;
38377 default:
38378 gcc_unreachable ();
38379 }
38380
38381 protected_set_expr_location (stmt, pragma_tok->location);
38382 }
38383 \f
38384 /* Transactional Memory parsing routines. */
38385
38386 /* Parse a transaction attribute.
38387
38388 txn-attribute:
38389 attribute
38390 [ [ identifier ] ]
38391
38392 We use this instead of cp_parser_attributes_opt for transactions to avoid
38393 the pedwarn in C++98 mode. */
38394
38395 static tree
38396 cp_parser_txn_attribute_opt (cp_parser *parser)
38397 {
38398 cp_token *token;
38399 tree attr_name, attr = NULL;
38400
38401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38402 return cp_parser_attributes_opt (parser);
38403
38404 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38405 return NULL_TREE;
38406 cp_lexer_consume_token (parser->lexer);
38407 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38408 goto error1;
38409
38410 token = cp_lexer_peek_token (parser->lexer);
38411 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38412 {
38413 token = cp_lexer_consume_token (parser->lexer);
38414
38415 attr_name = (token->type == CPP_KEYWORD
38416 /* For keywords, use the canonical spelling,
38417 not the parsed identifier. */
38418 ? ridpointers[(int) token->keyword]
38419 : token->u.value);
38420 attr = build_tree_list (attr_name, NULL_TREE);
38421 }
38422 else
38423 cp_parser_error (parser, "expected identifier");
38424
38425 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38426 error1:
38427 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38428 return attr;
38429 }
38430
38431 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38432
38433 transaction-statement:
38434 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38435 compound-statement
38436 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38437 */
38438
38439 static tree
38440 cp_parser_transaction (cp_parser *parser, cp_token *token)
38441 {
38442 unsigned char old_in = parser->in_transaction;
38443 unsigned char this_in = 1, new_in;
38444 enum rid keyword = token->keyword;
38445 tree stmt, attrs, noex;
38446
38447 cp_lexer_consume_token (parser->lexer);
38448
38449 if (keyword == RID_TRANSACTION_RELAXED
38450 || keyword == RID_SYNCHRONIZED)
38451 this_in |= TM_STMT_ATTR_RELAXED;
38452 else
38453 {
38454 attrs = cp_parser_txn_attribute_opt (parser);
38455 if (attrs)
38456 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38457 }
38458
38459 /* Parse a noexcept specification. */
38460 if (keyword == RID_ATOMIC_NOEXCEPT)
38461 noex = boolean_true_node;
38462 else if (keyword == RID_ATOMIC_CANCEL)
38463 {
38464 /* cancel-and-throw is unimplemented. */
38465 sorry ("atomic_cancel");
38466 noex = NULL_TREE;
38467 }
38468 else
38469 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38470
38471 /* Keep track if we're in the lexical scope of an outer transaction. */
38472 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38473
38474 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38475
38476 parser->in_transaction = new_in;
38477 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38478 parser->in_transaction = old_in;
38479
38480 finish_transaction_stmt (stmt, NULL, this_in, noex);
38481
38482 return stmt;
38483 }
38484
38485 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38486
38487 transaction-expression:
38488 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38489 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38490 */
38491
38492 static tree
38493 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38494 {
38495 unsigned char old_in = parser->in_transaction;
38496 unsigned char this_in = 1;
38497 cp_token *token;
38498 tree expr, noex;
38499 bool noex_expr;
38500 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38501
38502 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38503 || keyword == RID_TRANSACTION_RELAXED);
38504
38505 if (!flag_tm)
38506 error_at (loc,
38507 keyword == RID_TRANSACTION_RELAXED
38508 ? G_("%<__transaction_relaxed%> without transactional memory "
38509 "support enabled")
38510 : G_("%<__transaction_atomic%> without transactional memory "
38511 "support enabled"));
38512
38513 token = cp_parser_require_keyword (parser, keyword,
38514 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38515 : RT_TRANSACTION_RELAXED));
38516 gcc_assert (token != NULL);
38517
38518 if (keyword == RID_TRANSACTION_RELAXED)
38519 this_in |= TM_STMT_ATTR_RELAXED;
38520
38521 /* Set this early. This might mean that we allow transaction_cancel in
38522 an expression that we find out later actually has to be a constexpr.
38523 However, we expect that cxx_constant_value will be able to deal with
38524 this; also, if the noexcept has no constexpr, then what we parse next
38525 really is a transaction's body. */
38526 parser->in_transaction = this_in;
38527
38528 /* Parse a noexcept specification. */
38529 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38530 true);
38531
38532 if (!noex || !noex_expr
38533 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38534 {
38535 matching_parens parens;
38536 parens.require_open (parser);
38537
38538 expr = cp_parser_expression (parser);
38539 expr = finish_parenthesized_expr (expr);
38540
38541 parens.require_close (parser);
38542 }
38543 else
38544 {
38545 /* The only expression that is available got parsed for the noexcept
38546 already. noexcept is true then. */
38547 expr = noex;
38548 noex = boolean_true_node;
38549 }
38550
38551 expr = build_transaction_expr (token->location, expr, this_in, noex);
38552 parser->in_transaction = old_in;
38553
38554 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38555 return error_mark_node;
38556
38557 return (flag_tm ? expr : error_mark_node);
38558 }
38559
38560 /* Parse a function-transaction-block.
38561
38562 function-transaction-block:
38563 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38564 function-body
38565 __transaction_atomic txn-attribute[opt] function-try-block
38566 __transaction_relaxed ctor-initializer[opt] function-body
38567 __transaction_relaxed function-try-block
38568 */
38569
38570 static void
38571 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38572 {
38573 unsigned char old_in = parser->in_transaction;
38574 unsigned char new_in = 1;
38575 tree compound_stmt, stmt, attrs;
38576 cp_token *token;
38577
38578 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38579 || keyword == RID_TRANSACTION_RELAXED);
38580 token = cp_parser_require_keyword (parser, keyword,
38581 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38582 : RT_TRANSACTION_RELAXED));
38583 gcc_assert (token != NULL);
38584
38585 if (keyword == RID_TRANSACTION_RELAXED)
38586 new_in |= TM_STMT_ATTR_RELAXED;
38587 else
38588 {
38589 attrs = cp_parser_txn_attribute_opt (parser);
38590 if (attrs)
38591 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38592 }
38593
38594 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38595
38596 parser->in_transaction = new_in;
38597
38598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38599 cp_parser_function_try_block (parser);
38600 else
38601 cp_parser_ctor_initializer_opt_and_function_body
38602 (parser, /*in_function_try_block=*/false);
38603
38604 parser->in_transaction = old_in;
38605
38606 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38607 }
38608
38609 /* Parse a __transaction_cancel statement.
38610
38611 cancel-statement:
38612 __transaction_cancel txn-attribute[opt] ;
38613 __transaction_cancel txn-attribute[opt] throw-expression ;
38614
38615 ??? Cancel and throw is not yet implemented. */
38616
38617 static tree
38618 cp_parser_transaction_cancel (cp_parser *parser)
38619 {
38620 cp_token *token;
38621 bool is_outer = false;
38622 tree stmt, attrs;
38623
38624 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38625 RT_TRANSACTION_CANCEL);
38626 gcc_assert (token != NULL);
38627
38628 attrs = cp_parser_txn_attribute_opt (parser);
38629 if (attrs)
38630 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38631
38632 /* ??? Parse cancel-and-throw here. */
38633
38634 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38635
38636 if (!flag_tm)
38637 {
38638 error_at (token->location, "%<__transaction_cancel%> without "
38639 "transactional memory support enabled");
38640 return error_mark_node;
38641 }
38642 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38643 {
38644 error_at (token->location, "%<__transaction_cancel%> within a "
38645 "%<__transaction_relaxed%>");
38646 return error_mark_node;
38647 }
38648 else if (is_outer)
38649 {
38650 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38651 && !is_tm_may_cancel_outer (current_function_decl))
38652 {
38653 error_at (token->location, "outer %<__transaction_cancel%> not "
38654 "within outer %<__transaction_atomic%>");
38655 error_at (token->location,
38656 " or a %<transaction_may_cancel_outer%> function");
38657 return error_mark_node;
38658 }
38659 }
38660 else if (parser->in_transaction == 0)
38661 {
38662 error_at (token->location, "%<__transaction_cancel%> not within "
38663 "%<__transaction_atomic%>");
38664 return error_mark_node;
38665 }
38666
38667 stmt = build_tm_abort_call (token->location, is_outer);
38668 add_stmt (stmt);
38669
38670 return stmt;
38671 }
38672 \f
38673 /* The parser. */
38674
38675 static GTY (()) cp_parser *the_parser;
38676
38677 \f
38678 /* Special handling for the first token or line in the file. The first
38679 thing in the file might be #pragma GCC pch_preprocess, which loads a
38680 PCH file, which is a GC collection point. So we need to handle this
38681 first pragma without benefit of an existing lexer structure.
38682
38683 Always returns one token to the caller in *FIRST_TOKEN. This is
38684 either the true first token of the file, or the first token after
38685 the initial pragma. */
38686
38687 static void
38688 cp_parser_initial_pragma (cp_token *first_token)
38689 {
38690 tree name = NULL;
38691
38692 cp_lexer_get_preprocessor_token (NULL, first_token);
38693 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38694 return;
38695
38696 cp_lexer_get_preprocessor_token (NULL, first_token);
38697 if (first_token->type == CPP_STRING)
38698 {
38699 name = first_token->u.value;
38700
38701 cp_lexer_get_preprocessor_token (NULL, first_token);
38702 if (first_token->type != CPP_PRAGMA_EOL)
38703 error_at (first_token->location,
38704 "junk at end of %<#pragma GCC pch_preprocess%>");
38705 }
38706 else
38707 error_at (first_token->location, "expected string literal");
38708
38709 /* Skip to the end of the pragma. */
38710 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38711 cp_lexer_get_preprocessor_token (NULL, first_token);
38712
38713 /* Now actually load the PCH file. */
38714 if (name)
38715 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38716
38717 /* Read one more token to return to our caller. We have to do this
38718 after reading the PCH file in, since its pointers have to be
38719 live. */
38720 cp_lexer_get_preprocessor_token (NULL, first_token);
38721 }
38722
38723 /* Parses the grainsize pragma for the _Cilk_for statement.
38724 Syntax:
38725 #pragma cilk grainsize = <VALUE>. */
38726
38727 static void
38728 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38729 {
38730 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38731 {
38732 tree exp = cp_parser_binary_expression (parser, false, false,
38733 PREC_NOT_OPERATOR, NULL);
38734 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38735 if (!exp || exp == error_mark_node)
38736 {
38737 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38738 return;
38739 }
38740
38741 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38743 cp_parser_cilk_for (parser, exp, if_p);
38744 else
38745 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38746 "%<#pragma cilk grainsize%> is not followed by "
38747 "%<_Cilk_for%>");
38748 return;
38749 }
38750 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38751 }
38752
38753 /* Normal parsing of a pragma token. Here we can (and must) use the
38754 regular lexer. */
38755
38756 static bool
38757 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38758 {
38759 cp_token *pragma_tok;
38760 unsigned int id;
38761 tree stmt;
38762 bool ret;
38763
38764 pragma_tok = cp_lexer_consume_token (parser->lexer);
38765 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38766 parser->lexer->in_pragma = true;
38767
38768 id = cp_parser_pragma_kind (pragma_tok);
38769 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38770 cp_ensure_no_omp_declare_simd (parser);
38771 switch (id)
38772 {
38773 case PRAGMA_GCC_PCH_PREPROCESS:
38774 error_at (pragma_tok->location,
38775 "%<#pragma GCC pch_preprocess%> must be first");
38776 break;
38777
38778 case PRAGMA_OMP_BARRIER:
38779 switch (context)
38780 {
38781 case pragma_compound:
38782 cp_parser_omp_barrier (parser, pragma_tok);
38783 return false;
38784 case pragma_stmt:
38785 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38786 "used in compound statements", "omp barrier");
38787 break;
38788 default:
38789 goto bad_stmt;
38790 }
38791 break;
38792
38793 case PRAGMA_OMP_FLUSH:
38794 switch (context)
38795 {
38796 case pragma_compound:
38797 cp_parser_omp_flush (parser, pragma_tok);
38798 return false;
38799 case pragma_stmt:
38800 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38801 "used in compound statements", "omp flush");
38802 break;
38803 default:
38804 goto bad_stmt;
38805 }
38806 break;
38807
38808 case PRAGMA_OMP_TASKWAIT:
38809 switch (context)
38810 {
38811 case pragma_compound:
38812 cp_parser_omp_taskwait (parser, pragma_tok);
38813 return false;
38814 case pragma_stmt:
38815 error_at (pragma_tok->location,
38816 "%<#pragma %s%> may only be used in compound statements",
38817 "omp taskwait");
38818 break;
38819 default:
38820 goto bad_stmt;
38821 }
38822 break;
38823
38824 case PRAGMA_OMP_TASKYIELD:
38825 switch (context)
38826 {
38827 case pragma_compound:
38828 cp_parser_omp_taskyield (parser, pragma_tok);
38829 return false;
38830 case pragma_stmt:
38831 error_at (pragma_tok->location,
38832 "%<#pragma %s%> may only be used in compound statements",
38833 "omp taskyield");
38834 break;
38835 default:
38836 goto bad_stmt;
38837 }
38838 break;
38839
38840 case PRAGMA_OMP_CANCEL:
38841 switch (context)
38842 {
38843 case pragma_compound:
38844 cp_parser_omp_cancel (parser, pragma_tok);
38845 return false;
38846 case pragma_stmt:
38847 error_at (pragma_tok->location,
38848 "%<#pragma %s%> may only be used in compound statements",
38849 "omp cancel");
38850 break;
38851 default:
38852 goto bad_stmt;
38853 }
38854 break;
38855
38856 case PRAGMA_OMP_CANCELLATION_POINT:
38857 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38858 return false;
38859
38860 case PRAGMA_OMP_THREADPRIVATE:
38861 cp_parser_omp_threadprivate (parser, pragma_tok);
38862 return false;
38863
38864 case PRAGMA_OMP_DECLARE:
38865 return cp_parser_omp_declare (parser, pragma_tok, context);
38866
38867 case PRAGMA_OACC_DECLARE:
38868 cp_parser_oacc_declare (parser, pragma_tok);
38869 return false;
38870
38871 case PRAGMA_OACC_ENTER_DATA:
38872 if (context == pragma_stmt)
38873 {
38874 error_at (pragma_tok->location,
38875 "%<#pragma %s%> may only be used in compound statements",
38876 "acc enter data");
38877 break;
38878 }
38879 else if (context != pragma_compound)
38880 goto bad_stmt;
38881 cp_parser_omp_construct (parser, pragma_tok, if_p);
38882 return true;
38883
38884 case PRAGMA_OACC_EXIT_DATA:
38885 if (context == pragma_stmt)
38886 {
38887 error_at (pragma_tok->location,
38888 "%<#pragma %s%> may only be used in compound statements",
38889 "acc exit data");
38890 break;
38891 }
38892 else if (context != pragma_compound)
38893 goto bad_stmt;
38894 cp_parser_omp_construct (parser, pragma_tok, if_p);
38895 return true;
38896
38897 case PRAGMA_OACC_ROUTINE:
38898 if (context != pragma_external)
38899 {
38900 error_at (pragma_tok->location,
38901 "%<#pragma acc routine%> must be at file scope");
38902 break;
38903 }
38904 cp_parser_oacc_routine (parser, pragma_tok, context);
38905 return false;
38906
38907 case PRAGMA_OACC_UPDATE:
38908 if (context == pragma_stmt)
38909 {
38910 error_at (pragma_tok->location,
38911 "%<#pragma %s%> may only be used in compound statements",
38912 "acc update");
38913 break;
38914 }
38915 else if (context != pragma_compound)
38916 goto bad_stmt;
38917 cp_parser_omp_construct (parser, pragma_tok, if_p);
38918 return true;
38919
38920 case PRAGMA_OACC_WAIT:
38921 if (context == pragma_stmt)
38922 {
38923 error_at (pragma_tok->location,
38924 "%<#pragma %s%> may only be used in compound statements",
38925 "acc wait");
38926 break;
38927 }
38928 else if (context != pragma_compound)
38929 goto bad_stmt;
38930 cp_parser_omp_construct (parser, pragma_tok, if_p);
38931 return true;
38932
38933 case PRAGMA_OACC_ATOMIC:
38934 case PRAGMA_OACC_CACHE:
38935 case PRAGMA_OACC_DATA:
38936 case PRAGMA_OACC_HOST_DATA:
38937 case PRAGMA_OACC_KERNELS:
38938 case PRAGMA_OACC_PARALLEL:
38939 case PRAGMA_OACC_LOOP:
38940 case PRAGMA_OMP_ATOMIC:
38941 case PRAGMA_OMP_CRITICAL:
38942 case PRAGMA_OMP_DISTRIBUTE:
38943 case PRAGMA_OMP_FOR:
38944 case PRAGMA_OMP_MASTER:
38945 case PRAGMA_OMP_PARALLEL:
38946 case PRAGMA_OMP_SECTIONS:
38947 case PRAGMA_OMP_SIMD:
38948 case PRAGMA_OMP_SINGLE:
38949 case PRAGMA_OMP_TASK:
38950 case PRAGMA_OMP_TASKGROUP:
38951 case PRAGMA_OMP_TASKLOOP:
38952 case PRAGMA_OMP_TEAMS:
38953 if (context != pragma_stmt && context != pragma_compound)
38954 goto bad_stmt;
38955 stmt = push_omp_privatization_clauses (false);
38956 cp_parser_omp_construct (parser, pragma_tok, if_p);
38957 pop_omp_privatization_clauses (stmt);
38958 return true;
38959
38960 case PRAGMA_OMP_ORDERED:
38961 if (context != pragma_stmt && context != pragma_compound)
38962 goto bad_stmt;
38963 stmt = push_omp_privatization_clauses (false);
38964 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38965 pop_omp_privatization_clauses (stmt);
38966 return ret;
38967
38968 case PRAGMA_OMP_TARGET:
38969 if (context != pragma_stmt && context != pragma_compound)
38970 goto bad_stmt;
38971 stmt = push_omp_privatization_clauses (false);
38972 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38973 pop_omp_privatization_clauses (stmt);
38974 return ret;
38975
38976 case PRAGMA_OMP_END_DECLARE_TARGET:
38977 cp_parser_omp_end_declare_target (parser, pragma_tok);
38978 return false;
38979
38980 case PRAGMA_OMP_SECTION:
38981 error_at (pragma_tok->location,
38982 "%<#pragma omp section%> may only be used in "
38983 "%<#pragma omp sections%> construct");
38984 break;
38985
38986 case PRAGMA_IVDEP:
38987 {
38988 if (context == pragma_external)
38989 {
38990 error_at (pragma_tok->location,
38991 "%<#pragma GCC ivdep%> must be inside a function");
38992 break;
38993 }
38994 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38995 cp_token *tok;
38996 tok = cp_lexer_peek_token (the_parser->lexer);
38997 if (tok->type != CPP_KEYWORD
38998 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38999 && tok->keyword != RID_DO))
39000 {
39001 cp_parser_error (parser, "for, while or do statement expected");
39002 return false;
39003 }
39004 cp_parser_iteration_statement (parser, if_p, true);
39005 return true;
39006 }
39007
39008 case PRAGMA_CILK_SIMD:
39009 if (context == pragma_external)
39010 {
39011 error_at (pragma_tok->location,
39012 "%<#pragma simd%> must be inside a function");
39013 break;
39014 }
39015 stmt = push_omp_privatization_clauses (false);
39016 cp_parser_cilk_simd (parser, pragma_tok, if_p);
39017 pop_omp_privatization_clauses (stmt);
39018 return true;
39019
39020 case PRAGMA_CILK_GRAINSIZE:
39021 if (context == pragma_external)
39022 {
39023 error_at (pragma_tok->location,
39024 "%<#pragma cilk grainsize%> must be inside a function");
39025 break;
39026 }
39027
39028 /* Ignore the pragma if Cilk Plus is not enabled. */
39029 if (flag_cilkplus)
39030 {
39031 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
39032 return true;
39033 }
39034 else
39035 {
39036 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
39037 "%<#pragma cilk grainsize%>");
39038 break;
39039 }
39040
39041 default:
39042 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
39043 c_invoke_pragma_handler (id);
39044 break;
39045
39046 bad_stmt:
39047 cp_parser_error (parser, "expected declaration specifiers");
39048 break;
39049 }
39050
39051 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39052 return false;
39053 }
39054
39055 /* The interface the pragma parsers have to the lexer. */
39056
39057 enum cpp_ttype
39058 pragma_lex (tree *value, location_t *loc)
39059 {
39060 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
39061 enum cpp_ttype ret = tok->type;
39062
39063 *value = tok->u.value;
39064 if (loc)
39065 *loc = tok->location;
39066
39067 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
39068 ret = CPP_EOF;
39069 else if (ret == CPP_STRING)
39070 *value = cp_parser_string_literal (the_parser, false, false);
39071 else
39072 {
39073 if (ret == CPP_KEYWORD)
39074 ret = CPP_NAME;
39075 cp_lexer_consume_token (the_parser->lexer);
39076 }
39077
39078 return ret;
39079 }
39080
39081 \f
39082 /* External interface. */
39083
39084 /* Parse one entire translation unit. */
39085
39086 void
39087 c_parse_file (void)
39088 {
39089 static bool already_called = false;
39090
39091 if (already_called)
39092 fatal_error (input_location,
39093 "inter-module optimizations not implemented for C++");
39094 already_called = true;
39095
39096 the_parser = cp_parser_new ();
39097 push_deferring_access_checks (flag_access_control
39098 ? dk_no_deferred : dk_no_check);
39099 cp_parser_translation_unit (the_parser);
39100 the_parser = NULL;
39101 }
39102
39103 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
39104 vectorlength clause:
39105 Syntax:
39106 vectorlength ( constant-expression ) */
39107
39108 static tree
39109 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
39110 bool is_simd_fn)
39111 {
39112 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39113 tree expr;
39114 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
39115 safelen clause. Thus, vectorlength is represented as OMP 4.0
39116 safelen. For SIMD-enabled function it is represented by OMP 4.0
39117 simdlen. */
39118 if (!is_simd_fn)
39119 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
39120 loc);
39121 else
39122 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
39123 loc);
39124
39125 matching_parens parens;
39126 if (!parens.require_open (parser))
39127 return error_mark_node;
39128
39129 expr = cp_parser_constant_expression (parser);
39130 expr = maybe_constant_value (expr);
39131
39132 /* If expr == error_mark_node, then don't emit any errors nor
39133 create a clause. if any of the above functions returns
39134 error mark node then they would have emitted an error message. */
39135 if (expr == error_mark_node)
39136 ;
39137 else if (!TREE_TYPE (expr)
39138 || !TREE_CONSTANT (expr)
39139 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
39140 error_at (loc, "vectorlength must be an integer constant");
39141 else if (TREE_CONSTANT (expr)
39142 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
39143 error_at (loc, "vectorlength must be a power of 2");
39144 else
39145 {
39146 tree c;
39147 if (!is_simd_fn)
39148 {
39149 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
39150 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
39151 OMP_CLAUSE_CHAIN (c) = clauses;
39152 clauses = c;
39153 }
39154 else
39155 {
39156 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
39157 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
39158 OMP_CLAUSE_CHAIN (c) = clauses;
39159 clauses = c;
39160 }
39161 }
39162
39163 if (!parens.require_close (parser))
39164 return error_mark_node;
39165 return clauses;
39166 }
39167
39168 /* Handles the Cilk Plus #pragma simd linear clause.
39169 Syntax:
39170 linear ( simd-linear-variable-list )
39171
39172 simd-linear-variable-list:
39173 simd-linear-variable
39174 simd-linear-variable-list , simd-linear-variable
39175
39176 simd-linear-variable:
39177 id-expression
39178 id-expression : simd-linear-step
39179
39180 simd-linear-step:
39181 conditional-expression */
39182
39183 static tree
39184 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
39185 {
39186 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39187
39188 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39189 return clauses;
39190 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39191 {
39192 cp_parser_error (parser, "expected identifier");
39193 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
39194 return error_mark_node;
39195 }
39196
39197 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39198 parser->colon_corrects_to_scope_p = false;
39199 while (1)
39200 {
39201 cp_token *token = cp_lexer_peek_token (parser->lexer);
39202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39203 {
39204 cp_parser_error (parser, "expected variable-name");
39205 clauses = error_mark_node;
39206 break;
39207 }
39208
39209 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
39210 false, false);
39211 tree decl = cp_parser_lookup_name_simple (parser, var_name,
39212 token->location);
39213 if (decl == error_mark_node)
39214 {
39215 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
39216 token->location);
39217 clauses = error_mark_node;
39218 }
39219 else
39220 {
39221 tree e = NULL_TREE;
39222 tree step_size = integer_one_node;
39223
39224 /* If present, parse the linear step. Otherwise, assume the default
39225 value of 1. */
39226 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
39227 {
39228 cp_lexer_consume_token (parser->lexer);
39229
39230 e = cp_parser_assignment_expression (parser);
39231 e = maybe_constant_value (e);
39232
39233 if (e == error_mark_node)
39234 {
39235 /* If an error has occurred, then the whole pragma is
39236 considered ill-formed. Thus, no reason to keep
39237 parsing. */
39238 clauses = error_mark_node;
39239 break;
39240 }
39241 else if (type_dependent_expression_p (e)
39242 || value_dependent_expression_p (e)
39243 || (TREE_TYPE (e)
39244 && INTEGRAL_TYPE_P (TREE_TYPE (e))
39245 && (TREE_CONSTANT (e)
39246 || DECL_P (e))))
39247 step_size = e;
39248 else
39249 cp_parser_error (parser,
39250 "step size must be an integer constant "
39251 "expression or an integer variable");
39252 }
39253
39254 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
39255 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
39256 OMP_CLAUSE_DECL (l) = decl;
39257 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
39258 OMP_CLAUSE_CHAIN (l) = clauses;
39259 clauses = l;
39260 }
39261 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39262 cp_lexer_consume_token (parser->lexer);
39263 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39264 break;
39265 else
39266 {
39267 error_at (cp_lexer_peek_token (parser->lexer)->location,
39268 "expected %<,%> or %<)%> after %qE", decl);
39269 clauses = error_mark_node;
39270 break;
39271 }
39272 }
39273 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39274 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
39275 return clauses;
39276 }
39277
39278 /* Returns the name of the next clause. If the clause is not
39279 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
39280 token is not consumed. Otherwise, the appropriate enum from the
39281 pragma_simd_clause is returned and the token is consumed. */
39282
39283 static pragma_omp_clause
39284 cp_parser_cilk_simd_clause_name (cp_parser *parser)
39285 {
39286 pragma_omp_clause clause_type;
39287 cp_token *token = cp_lexer_peek_token (parser->lexer);
39288
39289 if (token->keyword == RID_PRIVATE)
39290 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
39291 else if (!token->u.value || token->type != CPP_NAME)
39292 return PRAGMA_CILK_CLAUSE_NONE;
39293 else if (id_equal (token->u.value, "vectorlength"))
39294 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
39295 else if (id_equal (token->u.value, "linear"))
39296 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
39297 else if (id_equal (token->u.value, "firstprivate"))
39298 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
39299 else if (id_equal (token->u.value, "lastprivate"))
39300 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
39301 else if (id_equal (token->u.value, "reduction"))
39302 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
39303 else
39304 return PRAGMA_CILK_CLAUSE_NONE;
39305
39306 cp_lexer_consume_token (parser->lexer);
39307 return clause_type;
39308 }
39309
39310 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
39311
39312 static tree
39313 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
39314 {
39315 tree clauses = NULL_TREE;
39316
39317 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39318 && clauses != error_mark_node)
39319 {
39320 pragma_omp_clause c_kind;
39321 c_kind = cp_parser_cilk_simd_clause_name (parser);
39322 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
39323 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
39324 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
39325 clauses = cp_parser_cilk_simd_linear (parser, clauses);
39326 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
39327 /* Use the OpenMP 4.0 equivalent function. */
39328 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
39329 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
39330 /* Use the OpenMP 4.0 equivalent function. */
39331 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39332 clauses);
39333 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
39334 /* Use the OMP 4.0 equivalent function. */
39335 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
39336 clauses);
39337 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
39338 /* Use the OMP 4.0 equivalent function. */
39339 clauses = cp_parser_omp_clause_reduction (parser, clauses);
39340 else
39341 {
39342 clauses = error_mark_node;
39343 cp_parser_error (parser, "expected %<#pragma simd%> clause");
39344 break;
39345 }
39346 }
39347
39348 cp_parser_skip_to_pragma_eol (parser, pragma_token);
39349
39350 if (clauses == error_mark_node)
39351 return error_mark_node;
39352 else
39353 return finish_omp_clauses (clauses, C_ORT_CILK);
39354 }
39355
39356 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
39357
39358 static void
39359 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
39360 {
39361 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
39362
39363 if (clauses == error_mark_node)
39364 return;
39365
39366 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
39367 {
39368 error_at (cp_lexer_peek_token (parser->lexer)->location,
39369 "for statement expected");
39370 return;
39371 }
39372
39373 tree sb = begin_omp_structured_block ();
39374 int save = cp_parser_begin_omp_structured_block (parser);
39375 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
39376 if (ret)
39377 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
39378 cp_parser_end_omp_structured_block (parser, save);
39379 add_stmt (finish_omp_structured_block (sb));
39380 }
39381
39382 /* Main entry-point for parsing Cilk Plus _Cilk_for
39383 loops. The return value is error_mark_node
39384 when errors happen and CILK_FOR tree on success. */
39385
39386 static tree
39387 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
39388 {
39389 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
39390 gcc_unreachable ();
39391
39392 tree sb = begin_omp_structured_block ();
39393 int save = cp_parser_begin_omp_structured_block (parser);
39394
39395 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
39396 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
39397 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
39398 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
39399
39400 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
39401 if (ret)
39402 cpp_validate_cilk_plus_loop (ret);
39403 else
39404 ret = error_mark_node;
39405
39406 cp_parser_end_omp_structured_block (parser, save);
39407 add_stmt (finish_omp_structured_block (sb));
39408 return ret;
39409 }
39410
39411 /* Create an identifier for a generic parameter type (a synthesized
39412 template parameter implied by `auto' or a concept identifier). */
39413
39414 static GTY(()) int generic_parm_count;
39415 static tree
39416 make_generic_type_name ()
39417 {
39418 char buf[32];
39419 sprintf (buf, "auto:%d", ++generic_parm_count);
39420 return get_identifier (buf);
39421 }
39422
39423 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39424 (creating a new template parameter list if necessary). Returns the newly
39425 created template type parm. */
39426
39427 static tree
39428 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39429 {
39430 gcc_assert (current_binding_level->kind == sk_function_parms);
39431
39432 /* Before committing to modifying any scope, if we're in an
39433 implicit template scope, and we're trying to synthesize a
39434 constrained parameter, try to find a previous parameter with
39435 the same name. This is the same-type rule for abbreviated
39436 function templates.
39437
39438 NOTE: We can generate implicit parameters when tentatively
39439 parsing a nested name specifier, only to reject that parse
39440 later. However, matching the same template-id as part of a
39441 direct-declarator should generate an identical template
39442 parameter, so this rule will merge them. */
39443 if (parser->implicit_template_scope && constr)
39444 {
39445 tree t = parser->implicit_template_parms;
39446 while (t)
39447 {
39448 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39449 {
39450 tree d = TREE_VALUE (t);
39451 if (TREE_CODE (d) == PARM_DECL)
39452 /* Return the TEMPLATE_PARM_INDEX. */
39453 d = DECL_INITIAL (d);
39454 return d;
39455 }
39456 t = TREE_CHAIN (t);
39457 }
39458 }
39459
39460 /* We are either continuing a function template that already contains implicit
39461 template parameters, creating a new fully-implicit function template, or
39462 extending an existing explicit function template with implicit template
39463 parameters. */
39464
39465 cp_binding_level *const entry_scope = current_binding_level;
39466
39467 bool become_template = false;
39468 cp_binding_level *parent_scope = 0;
39469
39470 if (parser->implicit_template_scope)
39471 {
39472 gcc_assert (parser->implicit_template_parms);
39473
39474 current_binding_level = parser->implicit_template_scope;
39475 }
39476 else
39477 {
39478 /* Roll back to the existing template parameter scope (in the case of
39479 extending an explicit function template) or introduce a new template
39480 parameter scope ahead of the function parameter scope (or class scope
39481 in the case of out-of-line member definitions). The function scope is
39482 added back after template parameter synthesis below. */
39483
39484 cp_binding_level *scope = entry_scope;
39485
39486 while (scope->kind == sk_function_parms)
39487 {
39488 parent_scope = scope;
39489 scope = scope->level_chain;
39490 }
39491 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39492 {
39493 /* If not defining a class, then any class scope is a scope level in
39494 an out-of-line member definition. In this case simply wind back
39495 beyond the first such scope to inject the template parameter list.
39496 Otherwise wind back to the class being defined. The latter can
39497 occur in class member friend declarations such as:
39498
39499 class A {
39500 void foo (auto);
39501 };
39502 class B {
39503 friend void A::foo (auto);
39504 };
39505
39506 The template parameter list synthesized for the friend declaration
39507 must be injected in the scope of 'B'. This can also occur in
39508 erroneous cases such as:
39509
39510 struct A {
39511 struct B {
39512 void foo (auto);
39513 };
39514 void B::foo (auto) {}
39515 };
39516
39517 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39518 but, nevertheless, the template parameter list synthesized for the
39519 declarator should be injected into the scope of 'A' as if the
39520 ill-formed template was specified explicitly. */
39521
39522 while (scope->kind == sk_class && !scope->defining_class_p)
39523 {
39524 parent_scope = scope;
39525 scope = scope->level_chain;
39526 }
39527 }
39528
39529 current_binding_level = scope;
39530
39531 if (scope->kind != sk_template_parms
39532 || !function_being_declared_is_template_p (parser))
39533 {
39534 /* Introduce a new template parameter list for implicit template
39535 parameters. */
39536
39537 become_template = true;
39538
39539 parser->implicit_template_scope
39540 = begin_scope (sk_template_parms, NULL);
39541
39542 ++processing_template_decl;
39543
39544 parser->fully_implicit_function_template_p = true;
39545 ++parser->num_template_parameter_lists;
39546 }
39547 else
39548 {
39549 /* Synthesize implicit template parameters at the end of the explicit
39550 template parameter list. */
39551
39552 gcc_assert (current_template_parms);
39553
39554 parser->implicit_template_scope = scope;
39555
39556 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39557 parser->implicit_template_parms
39558 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39559 }
39560 }
39561
39562 /* Synthesize a new template parameter and track the current template
39563 parameter chain with implicit_template_parms. */
39564
39565 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39566 tree synth_id = make_generic_type_name ();
39567 tree synth_tmpl_parm;
39568 bool non_type = false;
39569
39570 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39571 synth_tmpl_parm
39572 = finish_template_type_parm (class_type_node, synth_id);
39573 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39574 synth_tmpl_parm
39575 = finish_constrained_template_template_parm (proto, synth_id);
39576 else
39577 {
39578 synth_tmpl_parm = copy_decl (proto);
39579 DECL_NAME (synth_tmpl_parm) = synth_id;
39580 non_type = true;
39581 }
39582
39583 // Attach the constraint to the parm before processing.
39584 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39585 TREE_TYPE (node) = constr;
39586 tree new_parm
39587 = process_template_parm (parser->implicit_template_parms,
39588 input_location,
39589 node,
39590 /*non_type=*/non_type,
39591 /*param_pack=*/false);
39592
39593 // Chain the new parameter to the list of implicit parameters.
39594 if (parser->implicit_template_parms)
39595 parser->implicit_template_parms
39596 = TREE_CHAIN (parser->implicit_template_parms);
39597 else
39598 parser->implicit_template_parms = new_parm;
39599
39600 tree new_decl = get_local_decls ();
39601 if (non_type)
39602 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39603 new_decl = DECL_INITIAL (new_decl);
39604
39605 /* If creating a fully implicit function template, start the new implicit
39606 template parameter list with this synthesized type, otherwise grow the
39607 current template parameter list. */
39608
39609 if (become_template)
39610 {
39611 parent_scope->level_chain = current_binding_level;
39612
39613 tree new_parms = make_tree_vec (1);
39614 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39615 current_template_parms = tree_cons (size_int (processing_template_decl),
39616 new_parms, current_template_parms);
39617 }
39618 else
39619 {
39620 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39621 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39622 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39623 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39624 }
39625
39626 // If the new parameter was constrained, we need to add that to the
39627 // constraints in the template parameter list.
39628 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39629 {
39630 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39631 reqs = conjoin_constraints (reqs, req);
39632 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39633 }
39634
39635 current_binding_level = entry_scope;
39636
39637 return new_decl;
39638 }
39639
39640 /* Finish the declaration of a fully implicit function template. Such a
39641 template has no explicit template parameter list so has not been through the
39642 normal template head and tail processing. synthesize_implicit_template_parm
39643 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39644 provided if the declaration is a class member such that its template
39645 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39646 form is returned. Otherwise NULL_TREE is returned. */
39647
39648 static tree
39649 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39650 {
39651 gcc_assert (parser->fully_implicit_function_template_p);
39652
39653 if (member_decl_opt && member_decl_opt != error_mark_node
39654 && DECL_VIRTUAL_P (member_decl_opt))
39655 {
39656 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39657 "implicit templates may not be %<virtual%>");
39658 DECL_VIRTUAL_P (member_decl_opt) = false;
39659 }
39660
39661 if (member_decl_opt)
39662 member_decl_opt = finish_member_template_decl (member_decl_opt);
39663 end_template_decl ();
39664
39665 parser->fully_implicit_function_template_p = false;
39666 --parser->num_template_parameter_lists;
39667
39668 return member_decl_opt;
39669 }
39670
39671 /* Helper function for diagnostics that have complained about things
39672 being used with 'extern "C"' linkage.
39673
39674 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39675
39676 void
39677 maybe_show_extern_c_location (void)
39678 {
39679 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39680 inform (the_parser->innermost_linkage_specification_location,
39681 "%<extern \"C\"%> linkage started here");
39682 }
39683
39684 #include "gt-cp-parser.h"