]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
re PR c++/81197 (ICE with structured binding and lifetime-extended temporaries)
[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 "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "c-family/name-hint.h"
47
48 \f
49 /* The lexer. */
50
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
53
54 static cp_token eof_token =
55 {
56 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
57 };
58
59 /* The various kinds of non integral constant we encounter. */
60 enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116 };
117
118 /* The various kinds of errors about name-lookup failing. */
119 enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128 };
129
130 /* The various kinds of required token */
131 enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
153
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
175
176 RT_SELECT, /* selection-statement */
177 RT_ITERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 };
185
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
188
189 class type_id_in_expr_sentinel
190 {
191 cp_parser *parser;
192 bool saved;
193 public:
194 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
195 : parser (parser),
196 saved (parser->in_type_id_in_expr_p)
197 { parser->in_type_id_in_expr_p = set; }
198 ~type_id_in_expr_sentinel ()
199 { parser->in_type_id_in_expr_p = saved; }
200 };
201
202 /* Prototypes. */
203
204 static cp_lexer *cp_lexer_new_main
205 (void);
206 static cp_lexer *cp_lexer_new_from_tokens
207 (cp_token_cache *tokens);
208 static void cp_lexer_destroy
209 (cp_lexer *);
210 static int cp_lexer_saving_tokens
211 (const cp_lexer *);
212 static cp_token *cp_lexer_token_at
213 (cp_lexer *, cp_token_position);
214 static void cp_lexer_get_preprocessor_token
215 (cp_lexer *, cp_token *);
216 static inline cp_token *cp_lexer_peek_token
217 (cp_lexer *);
218 static cp_token *cp_lexer_peek_nth_token
219 (cp_lexer *, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer *, enum cpp_ttype);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer *, enum cpp_ttype);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer *, enum rid);
226 static cp_token *cp_lexer_consume_token
227 (cp_lexer *);
228 static void cp_lexer_purge_token
229 (cp_lexer *);
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer *, cp_token_position);
232 static void cp_lexer_save_tokens
233 (cp_lexer *);
234 static void cp_lexer_commit_tokens
235 (cp_lexer *);
236 static void cp_lexer_rollback_tokens
237 (cp_lexer *);
238 static void cp_lexer_print_token
239 (FILE *, cp_token *);
240 static inline bool cp_lexer_debugging_p
241 (cp_lexer *);
242 static void cp_lexer_start_debugging
243 (cp_lexer *) ATTRIBUTE_UNUSED;
244 static void cp_lexer_stop_debugging
245 (cp_lexer *) ATTRIBUTE_UNUSED;
246
247 static cp_token_cache *cp_token_cache_new
248 (cp_token *, cp_token *);
249
250 static void cp_parser_initial_pragma
251 (cp_token *);
252
253 static bool cp_parser_omp_declare_reduction_exprs
254 (tree, cp_parser *);
255 static void cp_finalize_oacc_routine
256 (cp_parser *, tree, bool);
257
258 /* Manifest constants. */
259 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
260 #define CP_SAVED_TOKEN_STACK 5
261
262 /* Variables. */
263
264 /* The stream to which debugging output should be written. */
265 static FILE *cp_lexer_debug_stream;
266
267 /* Nonzero if we are parsing an unevaluated operand: an operand to
268 sizeof, typeof, or alignof. */
269 int cp_unevaluated_operand;
270
271 /* Dump up to NUM tokens in BUFFER to FILE starting with token
272 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
273 first token in BUFFER. If NUM is 0, dump all the tokens. If
274 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
275 highlighted by surrounding it in [[ ]]. */
276
277 static void
278 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
279 cp_token *start_token, unsigned num,
280 cp_token *curr_token)
281 {
282 unsigned i, nprinted;
283 cp_token *token;
284 bool do_print;
285
286 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
287
288 if (buffer == NULL)
289 return;
290
291 if (num == 0)
292 num = buffer->length ();
293
294 if (start_token == NULL)
295 start_token = buffer->address ();
296
297 if (start_token > buffer->address ())
298 {
299 cp_lexer_print_token (file, &(*buffer)[0]);
300 fprintf (file, " ... ");
301 }
302
303 do_print = false;
304 nprinted = 0;
305 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
306 {
307 if (token == start_token)
308 do_print = true;
309
310 if (!do_print)
311 continue;
312
313 nprinted++;
314 if (token == curr_token)
315 fprintf (file, "[[");
316
317 cp_lexer_print_token (file, token);
318
319 if (token == curr_token)
320 fprintf (file, "]]");
321
322 switch (token->type)
323 {
324 case CPP_SEMICOLON:
325 case CPP_OPEN_BRACE:
326 case CPP_CLOSE_BRACE:
327 case CPP_EOF:
328 fputc ('\n', file);
329 break;
330
331 default:
332 fputc (' ', file);
333 }
334 }
335
336 if (i == num && i < buffer->length ())
337 {
338 fprintf (file, " ... ");
339 cp_lexer_print_token (file, &buffer->last ());
340 }
341
342 fprintf (file, "\n");
343 }
344
345
346 /* Dump all tokens in BUFFER to stderr. */
347
348 void
349 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
350 {
351 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
352 }
353
354 DEBUG_FUNCTION void
355 debug (vec<cp_token, va_gc> &ref)
356 {
357 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
358 }
359
360 DEBUG_FUNCTION void
361 debug (vec<cp_token, va_gc> *ptr)
362 {
363 if (ptr)
364 debug (*ptr);
365 else
366 fprintf (stderr, "<nil>\n");
367 }
368
369
370 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
371 description for T. */
372
373 static void
374 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
375 {
376 if (t)
377 {
378 fprintf (file, "%s: ", desc);
379 print_node_brief (file, "", t, 0);
380 }
381 }
382
383
384 /* Dump parser context C to FILE. */
385
386 static void
387 cp_debug_print_context (FILE *file, cp_parser_context *c)
388 {
389 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
390 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
391 print_node_brief (file, "", c->object_type, 0);
392 fprintf (file, "}\n");
393 }
394
395
396 /* Print the stack of parsing contexts to FILE starting with FIRST. */
397
398 static void
399 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
400 {
401 unsigned i;
402 cp_parser_context *c;
403
404 fprintf (file, "Parsing context stack:\n");
405 for (i = 0, c = first; c; c = c->next, i++)
406 {
407 fprintf (file, "\t#%u: ", i);
408 cp_debug_print_context (file, c);
409 }
410 }
411
412
413 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
414
415 static void
416 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
417 {
418 if (flag)
419 fprintf (file, "%s: true\n", desc);
420 }
421
422
423 /* Print an unparsed function entry UF to FILE. */
424
425 static void
426 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
427 {
428 unsigned i;
429 cp_default_arg_entry *default_arg_fn;
430 tree fn;
431
432 fprintf (file, "\tFunctions with default args:\n");
433 for (i = 0;
434 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
435 i++)
436 {
437 fprintf (file, "\t\tClass type: ");
438 print_node_brief (file, "", default_arg_fn->class_type, 0);
439 fprintf (file, "\t\tDeclaration: ");
440 print_node_brief (file, "", default_arg_fn->decl, 0);
441 fprintf (file, "\n");
442 }
443
444 fprintf (file, "\n\tFunctions with definitions that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
447 {
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
450 }
451 fprintf (file, "\n");
452
453 fprintf (file, "\n\tNon-static data members with initializers that require "
454 "post-processing\n\t\t");
455 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
456 {
457 print_node_brief (file, "", fn, 0);
458 fprintf (file, " ");
459 }
460 fprintf (file, "\n");
461 }
462
463
464 /* Print the stack of unparsed member functions S to FILE. */
465
466 static void
467 cp_debug_print_unparsed_queues (FILE *file,
468 vec<cp_unparsed_functions_entry, va_gc> *s)
469 {
470 unsigned i;
471 cp_unparsed_functions_entry *uf;
472
473 fprintf (file, "Unparsed functions\n");
474 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
475 {
476 fprintf (file, "#%u:\n", i);
477 cp_debug_print_unparsed_function (file, uf);
478 }
479 }
480
481
482 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
483 the given PARSER. If FILE is NULL, the output is printed on stderr. */
484
485 static void
486 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
487 {
488 cp_token *next_token, *first_token, *start_token;
489
490 if (file == NULL)
491 file = stderr;
492
493 next_token = parser->lexer->next_token;
494 first_token = parser->lexer->buffer->address ();
495 start_token = (next_token > first_token + window_size / 2)
496 ? next_token - window_size / 2
497 : first_token;
498 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
499 next_token);
500 }
501
502
503 /* Dump debugging information for the given PARSER. If FILE is NULL,
504 the output is printed on stderr. */
505
506 void
507 cp_debug_parser (FILE *file, cp_parser *parser)
508 {
509 const size_t window_size = 20;
510 cp_token *token;
511 expanded_location eloc;
512
513 if (file == NULL)
514 file = stderr;
515
516 fprintf (file, "Parser state\n\n");
517 fprintf (file, "Number of tokens: %u\n",
518 vec_safe_length (parser->lexer->buffer));
519 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
520 cp_debug_print_tree_if_set (file, "Object scope",
521 parser->object_scope);
522 cp_debug_print_tree_if_set (file, "Qualifying scope",
523 parser->qualifying_scope);
524 cp_debug_print_context_stack (file, parser->context);
525 cp_debug_print_flag (file, "Allow GNU extensions",
526 parser->allow_gnu_extensions_p);
527 cp_debug_print_flag (file, "'>' token is greater-than",
528 parser->greater_than_is_operator_p);
529 cp_debug_print_flag (file, "Default args allowed in current "
530 "parameter list", parser->default_arg_ok_p);
531 cp_debug_print_flag (file, "Parsing integral constant-expression",
532 parser->integral_constant_expression_p);
533 cp_debug_print_flag (file, "Allow non-constant expression in current "
534 "constant-expression",
535 parser->allow_non_integral_constant_expression_p);
536 cp_debug_print_flag (file, "Seen non-constant expression",
537 parser->non_integral_constant_expression_p);
538 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
539 "current context",
540 parser->local_variables_forbidden_p);
541 cp_debug_print_flag (file, "In unbraced linkage specification",
542 parser->in_unbraced_linkage_specification_p);
543 cp_debug_print_flag (file, "Parsing a declarator",
544 parser->in_declarator_p);
545 cp_debug_print_flag (file, "In template argument list",
546 parser->in_template_argument_list_p);
547 cp_debug_print_flag (file, "Parsing an iteration statement",
548 parser->in_statement & IN_ITERATION_STMT);
549 cp_debug_print_flag (file, "Parsing a switch statement",
550 parser->in_statement & IN_SWITCH_STMT);
551 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
552 parser->in_statement & IN_OMP_BLOCK);
553 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
554 parser->in_statement & IN_OMP_FOR);
555 cp_debug_print_flag (file, "Parsing an if statement",
556 parser->in_statement & IN_IF_STMT);
557 cp_debug_print_flag (file, "Parsing a type-id in an expression "
558 "context", parser->in_type_id_in_expr_p);
559 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
560 parser->implicit_extern_c);
561 cp_debug_print_flag (file, "String expressions should be translated "
562 "to execution character set",
563 parser->translate_strings_p);
564 cp_debug_print_flag (file, "Parsing function body outside of a "
565 "local class", parser->in_function_body);
566 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
567 parser->colon_corrects_to_scope_p);
568 cp_debug_print_flag (file, "Colon doesn't start a class definition",
569 parser->colon_doesnt_start_class_def_p);
570 if (parser->type_definition_forbidden_message)
571 fprintf (file, "Error message for forbidden type definitions: %s\n",
572 parser->type_definition_forbidden_message);
573 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
574 fprintf (file, "Number of class definitions in progress: %u\n",
575 parser->num_classes_being_defined);
576 fprintf (file, "Number of template parameter lists for the current "
577 "declaration: %u\n", parser->num_template_parameter_lists);
578 cp_debug_parser_tokens (file, parser, window_size);
579 token = parser->lexer->next_token;
580 fprintf (file, "Next token to parse:\n");
581 fprintf (file, "\tToken: ");
582 cp_lexer_print_token (file, token);
583 eloc = expand_location (token->location);
584 fprintf (file, "\n\tFile: %s\n", eloc.file);
585 fprintf (file, "\tLine: %d\n", eloc.line);
586 fprintf (file, "\tColumn: %d\n", eloc.column);
587 }
588
589 DEBUG_FUNCTION void
590 debug (cp_parser &ref)
591 {
592 cp_debug_parser (stderr, &ref);
593 }
594
595 DEBUG_FUNCTION void
596 debug (cp_parser *ptr)
597 {
598 if (ptr)
599 debug (*ptr);
600 else
601 fprintf (stderr, "<nil>\n");
602 }
603
604 /* Allocate memory for a new lexer object and return it. */
605
606 static cp_lexer *
607 cp_lexer_alloc (void)
608 {
609 cp_lexer *lexer;
610
611 c_common_no_more_pch ();
612
613 /* Allocate the memory. */
614 lexer = ggc_cleared_alloc<cp_lexer> ();
615
616 /* Initially we are not debugging. */
617 lexer->debugging_p = false;
618
619 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
620
621 /* Create the buffer. */
622 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
623
624 return lexer;
625 }
626
627
628 /* Create a new main C++ lexer, the lexer that gets tokens from the
629 preprocessor. */
630
631 static cp_lexer *
632 cp_lexer_new_main (void)
633 {
634 cp_lexer *lexer;
635 cp_token token;
636
637 /* It's possible that parsing the first pragma will load a PCH file,
638 which is a GC collection point. So we have to do that before
639 allocating any memory. */
640 cp_parser_initial_pragma (&token);
641
642 lexer = cp_lexer_alloc ();
643
644 /* Put the first token in the buffer. */
645 lexer->buffer->quick_push (token);
646
647 /* Get the remaining tokens from the preprocessor. */
648 while (token.type != CPP_EOF)
649 {
650 cp_lexer_get_preprocessor_token (lexer, &token);
651 vec_safe_push (lexer->buffer, token);
652 }
653
654 lexer->last_token = lexer->buffer->address ()
655 + lexer->buffer->length ()
656 - 1;
657 lexer->next_token = lexer->buffer->length ()
658 ? lexer->buffer->address ()
659 : &eof_token;
660
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
663 done_lexing = true;
664
665 gcc_assert (!lexer->next_token->purged_p);
666 return lexer;
667 }
668
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
671
672 static cp_lexer *
673 cp_lexer_new_from_tokens (cp_token_cache *cache)
674 {
675 cp_token *first = cache->first;
676 cp_token *last = cache->last;
677 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
678
679 /* We do not own the buffer. */
680 lexer->buffer = NULL;
681 lexer->next_token = first == last ? &eof_token : first;
682 lexer->last_token = last;
683
684 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
685
686 /* Initially we are not debugging. */
687 lexer->debugging_p = false;
688
689 gcc_assert (!lexer->next_token->purged_p);
690 return lexer;
691 }
692
693 /* Frees all resources associated with LEXER. */
694
695 static void
696 cp_lexer_destroy (cp_lexer *lexer)
697 {
698 vec_free (lexer->buffer);
699 lexer->saved_tokens.release ();
700 ggc_free (lexer);
701 }
702
703 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
704 be used. The point of this flag is to help the compiler to fold away calls
705 to cp_lexer_debugging_p within this source file at compile time, when the
706 lexer is not being debugged. */
707
708 #define LEXER_DEBUGGING_ENABLED_P false
709
710 /* Returns nonzero if debugging information should be output. */
711
712 static inline bool
713 cp_lexer_debugging_p (cp_lexer *lexer)
714 {
715 if (!LEXER_DEBUGGING_ENABLED_P)
716 return false;
717
718 return lexer->debugging_p;
719 }
720
721
722 static inline cp_token_position
723 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
724 {
725 gcc_assert (!previous_p || lexer->next_token != &eof_token);
726
727 return lexer->next_token - previous_p;
728 }
729
730 static inline cp_token *
731 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
732 {
733 return pos;
734 }
735
736 static inline void
737 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
738 {
739 lexer->next_token = cp_lexer_token_at (lexer, pos);
740 }
741
742 static inline cp_token_position
743 cp_lexer_previous_token_position (cp_lexer *lexer)
744 {
745 if (lexer->next_token == &eof_token)
746 return lexer->last_token - 1;
747 else
748 return cp_lexer_token_position (lexer, true);
749 }
750
751 static inline cp_token *
752 cp_lexer_previous_token (cp_lexer *lexer)
753 {
754 cp_token_position tp = cp_lexer_previous_token_position (lexer);
755
756 /* Skip past purged tokens. */
757 while (tp->purged_p)
758 {
759 gcc_assert (tp != vec_safe_address (lexer->buffer));
760 tp--;
761 }
762
763 return cp_lexer_token_at (lexer, tp);
764 }
765
766 /* nonzero if we are presently saving tokens. */
767
768 static inline int
769 cp_lexer_saving_tokens (const cp_lexer* lexer)
770 {
771 return lexer->saved_tokens.length () != 0;
772 }
773
774 /* Store the next token from the preprocessor in *TOKEN. Return true
775 if we reach EOF. If LEXER is NULL, assume we are handling an
776 initial #pragma pch_preprocess, and thus want the lexer to return
777 processed strings. */
778
779 static void
780 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
781 {
782 static int is_extern_c = 0;
783
784 /* Get a new token from the preprocessor. */
785 token->type
786 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
787 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
788 token->keyword = RID_MAX;
789 token->purged_p = false;
790 token->error_reported = false;
791
792 /* On some systems, some header files are surrounded by an
793 implicit extern "C" block. Set a flag in the token if it
794 comes from such a header. */
795 is_extern_c += pending_lang_change;
796 pending_lang_change = 0;
797 token->implicit_extern_c = is_extern_c > 0;
798
799 /* Check to see if this token is a keyword. */
800 if (token->type == CPP_NAME)
801 {
802 if (IDENTIFIER_KEYWORD_P (token->u.value))
803 {
804 /* Mark this token as a keyword. */
805 token->type = CPP_KEYWORD;
806 /* Record which keyword. */
807 token->keyword = C_RID_CODE (token->u.value);
808 }
809 else
810 {
811 if (warn_cxx11_compat
812 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
813 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
814 {
815 /* Warn about the C++0x keyword (but still treat it as
816 an identifier). */
817 warning (OPT_Wc__11_compat,
818 "identifier %qE is a keyword in C++11",
819 token->u.value);
820
821 /* Clear out the C_RID_CODE so we don't warn about this
822 particular identifier-turned-keyword again. */
823 C_SET_RID_CODE (token->u.value, RID_MAX);
824 }
825
826 token->keyword = RID_MAX;
827 }
828 }
829 else if (token->type == CPP_AT_NAME)
830 {
831 /* This only happens in Objective-C++; it must be a keyword. */
832 token->type = CPP_KEYWORD;
833 switch (C_RID_CODE (token->u.value))
834 {
835 /* Replace 'class' with '@class', 'private' with '@private',
836 etc. This prevents confusion with the C++ keyword
837 'class', and makes the tokens consistent with other
838 Objective-C 'AT' keywords. For example '@class' is
839 reported as RID_AT_CLASS which is consistent with
840 '@synchronized', which is reported as
841 RID_AT_SYNCHRONIZED.
842 */
843 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
844 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
845 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
846 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
847 case RID_THROW: token->keyword = RID_AT_THROW; break;
848 case RID_TRY: token->keyword = RID_AT_TRY; break;
849 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
850 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
851 default: token->keyword = C_RID_CODE (token->u.value);
852 }
853 }
854 }
855
856 /* Update the globals input_location and the input file stack from TOKEN. */
857 static inline void
858 cp_lexer_set_source_position_from_token (cp_token *token)
859 {
860 if (token->type != CPP_EOF)
861 {
862 input_location = token->location;
863 }
864 }
865
866 /* Update the globals input_location and the input file stack from LEXER. */
867 static inline void
868 cp_lexer_set_source_position (cp_lexer *lexer)
869 {
870 cp_token *token = cp_lexer_peek_token (lexer);
871 cp_lexer_set_source_position_from_token (token);
872 }
873
874 /* Return a pointer to the next token in the token stream, but do not
875 consume it. */
876
877 static inline cp_token *
878 cp_lexer_peek_token (cp_lexer *lexer)
879 {
880 if (cp_lexer_debugging_p (lexer))
881 {
882 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
883 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
884 putc ('\n', cp_lexer_debug_stream);
885 }
886 return lexer->next_token;
887 }
888
889 /* Return true if the next token has the indicated TYPE. */
890
891 static inline bool
892 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
893 {
894 return cp_lexer_peek_token (lexer)->type == type;
895 }
896
897 /* Return true if the next token does not have the indicated TYPE. */
898
899 static inline bool
900 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
901 {
902 return !cp_lexer_next_token_is (lexer, type);
903 }
904
905 /* Return true if the next token is the indicated KEYWORD. */
906
907 static inline bool
908 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
909 {
910 return cp_lexer_peek_token (lexer)->keyword == keyword;
911 }
912
913 static inline bool
914 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
915 {
916 return cp_lexer_peek_nth_token (lexer, n)->type == type;
917 }
918
919 static inline bool
920 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
921 {
922 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
923 }
924
925 /* Return true if the next token is not the indicated KEYWORD. */
926
927 static inline bool
928 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
929 {
930 return cp_lexer_peek_token (lexer)->keyword != keyword;
931 }
932
933 /* Return true if KEYWORD can start a decl-specifier. */
934
935 bool
936 cp_keyword_starts_decl_specifier_p (enum rid keyword)
937 {
938 switch (keyword)
939 {
940 /* auto specifier: storage-class-specifier in C++,
941 simple-type-specifier in C++0x. */
942 case RID_AUTO:
943 /* Storage classes. */
944 case RID_REGISTER:
945 case RID_STATIC:
946 case RID_EXTERN:
947 case RID_MUTABLE:
948 case RID_THREAD:
949 /* Elaborated type specifiers. */
950 case RID_ENUM:
951 case RID_CLASS:
952 case RID_STRUCT:
953 case RID_UNION:
954 case RID_TYPENAME:
955 /* Simple type specifiers. */
956 case RID_CHAR:
957 case RID_CHAR16:
958 case RID_CHAR32:
959 case RID_WCHAR:
960 case RID_BOOL:
961 case RID_SHORT:
962 case RID_INT:
963 case RID_LONG:
964 case RID_SIGNED:
965 case RID_UNSIGNED:
966 case RID_FLOAT:
967 case RID_DOUBLE:
968 case RID_VOID:
969 /* GNU extensions. */
970 case RID_ATTRIBUTE:
971 case RID_TYPEOF:
972 /* C++0x extensions. */
973 case RID_DECLTYPE:
974 case RID_UNDERLYING_TYPE:
975 case RID_CONSTEXPR:
976 return true;
977
978 default:
979 if (keyword >= RID_FIRST_INT_N
980 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
981 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
982 return true;
983 return false;
984 }
985 }
986
987 /* Return true if the next token is a keyword for a decl-specifier. */
988
989 static bool
990 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
991 {
992 cp_token *token;
993
994 token = cp_lexer_peek_token (lexer);
995 return cp_keyword_starts_decl_specifier_p (token->keyword);
996 }
997
998 /* Returns TRUE iff the token T begins a decltype type. */
999
1000 static bool
1001 token_is_decltype (cp_token *t)
1002 {
1003 return (t->keyword == RID_DECLTYPE
1004 || t->type == CPP_DECLTYPE);
1005 }
1006
1007 /* Returns TRUE iff the next token begins a decltype type. */
1008
1009 static bool
1010 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1011 {
1012 cp_token *t = cp_lexer_peek_token (lexer);
1013 return token_is_decltype (t);
1014 }
1015
1016 /* Called when processing a token with tree_check_value; perform or defer the
1017 associated checks and return the value. */
1018
1019 static tree
1020 saved_checks_value (struct tree_check *check_value)
1021 {
1022 /* Perform any access checks that were deferred. */
1023 vec<deferred_access_check, va_gc> *checks;
1024 deferred_access_check *chk;
1025 checks = check_value->checks;
1026 if (checks)
1027 {
1028 int i;
1029 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1030 perform_or_defer_access_check (chk->binfo,
1031 chk->decl,
1032 chk->diag_decl, tf_warning_or_error);
1033 }
1034 /* Return the stored value. */
1035 return check_value->value;
1036 }
1037
1038 /* Return a pointer to the Nth token in the token stream. If N is 1,
1039 then this is precisely equivalent to cp_lexer_peek_token (except
1040 that it is not inline). One would like to disallow that case, but
1041 there is one case (cp_parser_nth_token_starts_template_id) where
1042 the caller passes a variable for N and it might be 1. */
1043
1044 static cp_token *
1045 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1046 {
1047 cp_token *token;
1048
1049 /* N is 1-based, not zero-based. */
1050 gcc_assert (n > 0);
1051
1052 if (cp_lexer_debugging_p (lexer))
1053 fprintf (cp_lexer_debug_stream,
1054 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1055
1056 --n;
1057 token = lexer->next_token;
1058 gcc_assert (!n || token != &eof_token);
1059 while (n != 0)
1060 {
1061 ++token;
1062 if (token == lexer->last_token)
1063 {
1064 token = &eof_token;
1065 break;
1066 }
1067
1068 if (!token->purged_p)
1069 --n;
1070 }
1071
1072 if (cp_lexer_debugging_p (lexer))
1073 {
1074 cp_lexer_print_token (cp_lexer_debug_stream, token);
1075 putc ('\n', cp_lexer_debug_stream);
1076 }
1077
1078 return token;
1079 }
1080
1081 /* Return the next token, and advance the lexer's next_token pointer
1082 to point to the next non-purged token. */
1083
1084 static cp_token *
1085 cp_lexer_consume_token (cp_lexer* lexer)
1086 {
1087 cp_token *token = lexer->next_token;
1088
1089 gcc_assert (token != &eof_token);
1090 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1091
1092 do
1093 {
1094 lexer->next_token++;
1095 if (lexer->next_token == lexer->last_token)
1096 {
1097 lexer->next_token = &eof_token;
1098 break;
1099 }
1100
1101 }
1102 while (lexer->next_token->purged_p);
1103
1104 cp_lexer_set_source_position_from_token (token);
1105
1106 /* Provide debugging output. */
1107 if (cp_lexer_debugging_p (lexer))
1108 {
1109 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1110 cp_lexer_print_token (cp_lexer_debug_stream, token);
1111 putc ('\n', cp_lexer_debug_stream);
1112 }
1113
1114 return token;
1115 }
1116
1117 /* Permanently remove the next token from the token stream, and
1118 advance the next_token pointer to refer to the next non-purged
1119 token. */
1120
1121 static void
1122 cp_lexer_purge_token (cp_lexer *lexer)
1123 {
1124 cp_token *tok = lexer->next_token;
1125
1126 gcc_assert (tok != &eof_token);
1127 tok->purged_p = true;
1128 tok->location = UNKNOWN_LOCATION;
1129 tok->u.value = NULL_TREE;
1130 tok->keyword = RID_MAX;
1131
1132 do
1133 {
1134 tok++;
1135 if (tok == lexer->last_token)
1136 {
1137 tok = &eof_token;
1138 break;
1139 }
1140 }
1141 while (tok->purged_p);
1142 lexer->next_token = tok;
1143 }
1144
1145 /* Permanently remove all tokens after TOK, up to, but not
1146 including, the token that will be returned next by
1147 cp_lexer_peek_token. */
1148
1149 static void
1150 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1151 {
1152 cp_token *peek = lexer->next_token;
1153
1154 if (peek == &eof_token)
1155 peek = lexer->last_token;
1156
1157 gcc_assert (tok < peek);
1158
1159 for ( tok += 1; tok != peek; tok += 1)
1160 {
1161 tok->purged_p = true;
1162 tok->location = UNKNOWN_LOCATION;
1163 tok->u.value = NULL_TREE;
1164 tok->keyword = RID_MAX;
1165 }
1166 }
1167
1168 /* Begin saving tokens. All tokens consumed after this point will be
1169 preserved. */
1170
1171 static void
1172 cp_lexer_save_tokens (cp_lexer* lexer)
1173 {
1174 /* Provide debugging output. */
1175 if (cp_lexer_debugging_p (lexer))
1176 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1177
1178 lexer->saved_tokens.safe_push (lexer->next_token);
1179 }
1180
1181 /* Commit to the portion of the token stream most recently saved. */
1182
1183 static void
1184 cp_lexer_commit_tokens (cp_lexer* lexer)
1185 {
1186 /* Provide debugging output. */
1187 if (cp_lexer_debugging_p (lexer))
1188 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1189
1190 lexer->saved_tokens.pop ();
1191 }
1192
1193 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1194 to the token stream. Stop saving tokens. */
1195
1196 static void
1197 cp_lexer_rollback_tokens (cp_lexer* lexer)
1198 {
1199 /* Provide debugging output. */
1200 if (cp_lexer_debugging_p (lexer))
1201 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1202
1203 lexer->next_token = lexer->saved_tokens.pop ();
1204 }
1205
1206 /* RAII wrapper around the above functions, with sanity checking. Creating
1207 a variable saves tokens, which are committed when the variable is
1208 destroyed unless they are explicitly rolled back by calling the rollback
1209 member function. */
1210
1211 struct saved_token_sentinel
1212 {
1213 cp_lexer *lexer;
1214 unsigned len;
1215 bool commit;
1216 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1217 {
1218 len = lexer->saved_tokens.length ();
1219 cp_lexer_save_tokens (lexer);
1220 }
1221 void rollback ()
1222 {
1223 cp_lexer_rollback_tokens (lexer);
1224 commit = false;
1225 }
1226 ~saved_token_sentinel()
1227 {
1228 if (commit)
1229 cp_lexer_commit_tokens (lexer);
1230 gcc_assert (lexer->saved_tokens.length () == len);
1231 }
1232 };
1233
1234 /* Print a representation of the TOKEN on the STREAM. */
1235
1236 static void
1237 cp_lexer_print_token (FILE * stream, cp_token *token)
1238 {
1239 /* We don't use cpp_type2name here because the parser defines
1240 a few tokens of its own. */
1241 static const char *const token_names[] = {
1242 /* cpplib-defined token types */
1243 #define OP(e, s) #e,
1244 #define TK(e, s) #e,
1245 TTYPE_TABLE
1246 #undef OP
1247 #undef TK
1248 /* C++ parser token types - see "Manifest constants", above. */
1249 "KEYWORD",
1250 "TEMPLATE_ID",
1251 "NESTED_NAME_SPECIFIER",
1252 };
1253
1254 /* For some tokens, print the associated data. */
1255 switch (token->type)
1256 {
1257 case CPP_KEYWORD:
1258 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1259 For example, `struct' is mapped to an INTEGER_CST. */
1260 if (!identifier_p (token->u.value))
1261 break;
1262 /* fall through */
1263 case CPP_NAME:
1264 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1265 break;
1266
1267 case CPP_STRING:
1268 case CPP_STRING16:
1269 case CPP_STRING32:
1270 case CPP_WSTRING:
1271 case CPP_UTF8STRING:
1272 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1273 break;
1274
1275 case CPP_NUMBER:
1276 print_generic_expr (stream, token->u.value);
1277 break;
1278
1279 default:
1280 /* If we have a name for the token, print it out. Otherwise, we
1281 simply give the numeric code. */
1282 if (token->type < ARRAY_SIZE(token_names))
1283 fputs (token_names[token->type], stream);
1284 else
1285 fprintf (stream, "[%d]", token->type);
1286 break;
1287 }
1288 }
1289
1290 DEBUG_FUNCTION void
1291 debug (cp_token &ref)
1292 {
1293 cp_lexer_print_token (stderr, &ref);
1294 fprintf (stderr, "\n");
1295 }
1296
1297 DEBUG_FUNCTION void
1298 debug (cp_token *ptr)
1299 {
1300 if (ptr)
1301 debug (*ptr);
1302 else
1303 fprintf (stderr, "<nil>\n");
1304 }
1305
1306
1307 /* Start emitting debugging information. */
1308
1309 static void
1310 cp_lexer_start_debugging (cp_lexer* lexer)
1311 {
1312 if (!LEXER_DEBUGGING_ENABLED_P)
1313 fatal_error (input_location,
1314 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1315
1316 lexer->debugging_p = true;
1317 cp_lexer_debug_stream = stderr;
1318 }
1319
1320 /* Stop emitting debugging information. */
1321
1322 static void
1323 cp_lexer_stop_debugging (cp_lexer* lexer)
1324 {
1325 if (!LEXER_DEBUGGING_ENABLED_P)
1326 fatal_error (input_location,
1327 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1328
1329 lexer->debugging_p = false;
1330 cp_lexer_debug_stream = NULL;
1331 }
1332
1333 /* Create a new cp_token_cache, representing a range of tokens. */
1334
1335 static cp_token_cache *
1336 cp_token_cache_new (cp_token *first, cp_token *last)
1337 {
1338 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1339 cache->first = first;
1340 cache->last = last;
1341 return cache;
1342 }
1343
1344 /* Diagnose if #pragma omp declare simd isn't followed immediately
1345 by function declaration or definition. */
1346
1347 static inline void
1348 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1349 {
1350 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1351 {
1352 error ("%<#pragma omp declare simd%> not immediately followed by "
1353 "function declaration or definition");
1354 parser->omp_declare_simd = NULL;
1355 }
1356 }
1357
1358 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1359 and put that into "omp declare simd" attribute. */
1360
1361 static inline void
1362 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1363 {
1364 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1365 {
1366 if (fndecl == error_mark_node)
1367 {
1368 parser->omp_declare_simd = NULL;
1369 return;
1370 }
1371 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1372 {
1373 cp_ensure_no_omp_declare_simd (parser);
1374 return;
1375 }
1376 }
1377 }
1378
1379 /* Diagnose if #pragma acc routine isn't followed immediately by function
1380 declaration or definition. */
1381
1382 static inline void
1383 cp_ensure_no_oacc_routine (cp_parser *parser)
1384 {
1385 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1386 {
1387 error_at (parser->oacc_routine->loc,
1388 "%<#pragma acc routine%> not immediately followed by "
1389 "function declaration or definition");
1390 parser->oacc_routine = NULL;
1391 }
1392 }
1393 \f
1394 /* Decl-specifiers. */
1395
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1397
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1400 {
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1402 }
1403
1404 /* Declarators. */
1405
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1410
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1421
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1424
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1428
1429 /* Alloc BYTES from the declarator memory pool. */
1430
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1433 {
1434 return obstack_alloc (&declarator_obstack, bytes);
1435 }
1436
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1439
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1442 {
1443 cp_declarator *declarator;
1444
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->parenthesized = UNKNOWN_LOCATION;
1448 declarator->attributes = NULL_TREE;
1449 declarator->std_attributes = NULL_TREE;
1450 declarator->declarator = NULL;
1451 declarator->parameter_pack_p = false;
1452 declarator->id_loc = UNKNOWN_LOCATION;
1453
1454 return declarator;
1455 }
1456
1457 /* Make a declarator for a generalized identifier. If
1458 QUALIFYING_SCOPE is non-NULL, the identifier is
1459 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1460 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1461 is, if any. */
1462
1463 static cp_declarator *
1464 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1465 special_function_kind sfk)
1466 {
1467 cp_declarator *declarator;
1468
1469 /* It is valid to write:
1470
1471 class C { void f(); };
1472 typedef C D;
1473 void D::f();
1474
1475 The standard is not clear about whether `typedef const C D' is
1476 legal; as of 2002-09-15 the committee is considering that
1477 question. EDG 3.0 allows that syntax. Therefore, we do as
1478 well. */
1479 if (qualifying_scope && TYPE_P (qualifying_scope))
1480 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1481
1482 gcc_assert (identifier_p (unqualified_name)
1483 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1484 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1485
1486 declarator = make_declarator (cdk_id);
1487 declarator->u.id.qualifying_scope = qualifying_scope;
1488 declarator->u.id.unqualified_name = unqualified_name;
1489 declarator->u.id.sfk = sfk;
1490
1491 return declarator;
1492 }
1493
1494 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1495 of modifiers such as const or volatile to apply to the pointer
1496 type, represented as identifiers. ATTRIBUTES represent the attributes that
1497 appertain to the pointer or reference. */
1498
1499 cp_declarator *
1500 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1501 tree attributes)
1502 {
1503 cp_declarator *declarator;
1504
1505 declarator = make_declarator (cdk_pointer);
1506 declarator->declarator = target;
1507 declarator->u.pointer.qualifiers = cv_qualifiers;
1508 declarator->u.pointer.class_type = NULL_TREE;
1509 if (target)
1510 {
1511 declarator->id_loc = target->id_loc;
1512 declarator->parameter_pack_p = target->parameter_pack_p;
1513 target->parameter_pack_p = false;
1514 }
1515 else
1516 declarator->parameter_pack_p = false;
1517
1518 declarator->std_attributes = attributes;
1519
1520 return declarator;
1521 }
1522
1523 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1524 represent the attributes that appertain to the pointer or
1525 reference. */
1526
1527 cp_declarator *
1528 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1529 bool rvalue_ref, tree attributes)
1530 {
1531 cp_declarator *declarator;
1532
1533 declarator = make_declarator (cdk_reference);
1534 declarator->declarator = target;
1535 declarator->u.reference.qualifiers = cv_qualifiers;
1536 declarator->u.reference.rvalue_ref = rvalue_ref;
1537 if (target)
1538 {
1539 declarator->id_loc = target->id_loc;
1540 declarator->parameter_pack_p = target->parameter_pack_p;
1541 target->parameter_pack_p = false;
1542 }
1543 else
1544 declarator->parameter_pack_p = false;
1545
1546 declarator->std_attributes = attributes;
1547
1548 return declarator;
1549 }
1550
1551 /* Like make_pointer_declarator -- but for a pointer to a non-static
1552 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1553 appertain to the pointer or reference. */
1554
1555 cp_declarator *
1556 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1557 cp_declarator *pointee,
1558 tree attributes)
1559 {
1560 cp_declarator *declarator;
1561
1562 declarator = make_declarator (cdk_ptrmem);
1563 declarator->declarator = pointee;
1564 declarator->u.pointer.qualifiers = cv_qualifiers;
1565 declarator->u.pointer.class_type = class_type;
1566
1567 if (pointee)
1568 {
1569 declarator->parameter_pack_p = pointee->parameter_pack_p;
1570 pointee->parameter_pack_p = false;
1571 }
1572 else
1573 declarator->parameter_pack_p = false;
1574
1575 declarator->std_attributes = attributes;
1576
1577 return declarator;
1578 }
1579
1580 /* Make a declarator for the function given by TARGET, with the
1581 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1582 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1583 indicates what exceptions can be thrown. */
1584
1585 cp_declarator *
1586 make_call_declarator (cp_declarator *target,
1587 tree parms,
1588 cp_cv_quals cv_qualifiers,
1589 cp_virt_specifiers virt_specifiers,
1590 cp_ref_qualifier ref_qualifier,
1591 tree tx_qualifier,
1592 tree exception_specification,
1593 tree late_return_type,
1594 tree requires_clause)
1595 {
1596 cp_declarator *declarator;
1597
1598 declarator = make_declarator (cdk_function);
1599 declarator->declarator = target;
1600 declarator->u.function.parameters = parms;
1601 declarator->u.function.qualifiers = cv_qualifiers;
1602 declarator->u.function.virt_specifiers = virt_specifiers;
1603 declarator->u.function.ref_qualifier = ref_qualifier;
1604 declarator->u.function.tx_qualifier = tx_qualifier;
1605 declarator->u.function.exception_specification = exception_specification;
1606 declarator->u.function.late_return_type = late_return_type;
1607 declarator->u.function.requires_clause = requires_clause;
1608 if (target)
1609 {
1610 declarator->id_loc = target->id_loc;
1611 declarator->parameter_pack_p = target->parameter_pack_p;
1612 target->parameter_pack_p = false;
1613 }
1614 else
1615 declarator->parameter_pack_p = false;
1616
1617 return declarator;
1618 }
1619
1620 /* Make a declarator for an array of BOUNDS elements, each of which is
1621 defined by ELEMENT. */
1622
1623 cp_declarator *
1624 make_array_declarator (cp_declarator *element, tree bounds)
1625 {
1626 cp_declarator *declarator;
1627
1628 declarator = make_declarator (cdk_array);
1629 declarator->declarator = element;
1630 declarator->u.array.bounds = bounds;
1631 if (element)
1632 {
1633 declarator->id_loc = element->id_loc;
1634 declarator->parameter_pack_p = element->parameter_pack_p;
1635 element->parameter_pack_p = false;
1636 }
1637 else
1638 declarator->parameter_pack_p = false;
1639
1640 return declarator;
1641 }
1642
1643 /* Determine whether the declarator we've seen so far can be a
1644 parameter pack, when followed by an ellipsis. */
1645 static bool
1646 declarator_can_be_parameter_pack (cp_declarator *declarator)
1647 {
1648 if (declarator && declarator->parameter_pack_p)
1649 /* We already saw an ellipsis. */
1650 return false;
1651
1652 /* Search for a declarator name, or any other declarator that goes
1653 after the point where the ellipsis could appear in a parameter
1654 pack. If we find any of these, then this declarator can not be
1655 made into a parameter pack. */
1656 bool found = false;
1657 while (declarator && !found)
1658 {
1659 switch ((int)declarator->kind)
1660 {
1661 case cdk_id:
1662 case cdk_array:
1663 case cdk_decomp:
1664 found = true;
1665 break;
1666
1667 case cdk_error:
1668 return true;
1669
1670 default:
1671 declarator = declarator->declarator;
1672 break;
1673 }
1674 }
1675
1676 return !found;
1677 }
1678
1679 cp_parameter_declarator *no_parameters;
1680
1681 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1682 DECLARATOR and DEFAULT_ARGUMENT. */
1683
1684 cp_parameter_declarator *
1685 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1686 cp_declarator *declarator,
1687 tree default_argument,
1688 location_t loc,
1689 bool template_parameter_pack_p = false)
1690 {
1691 cp_parameter_declarator *parameter;
1692
1693 parameter = ((cp_parameter_declarator *)
1694 alloc_declarator (sizeof (cp_parameter_declarator)));
1695 parameter->next = NULL;
1696 if (decl_specifiers)
1697 parameter->decl_specifiers = *decl_specifiers;
1698 else
1699 clear_decl_specs (&parameter->decl_specifiers);
1700 parameter->declarator = declarator;
1701 parameter->default_argument = default_argument;
1702 parameter->template_parameter_pack_p = template_parameter_pack_p;
1703 parameter->loc = loc;
1704
1705 return parameter;
1706 }
1707
1708 /* Returns true iff DECLARATOR is a declaration for a function. */
1709
1710 static bool
1711 function_declarator_p (const cp_declarator *declarator)
1712 {
1713 while (declarator)
1714 {
1715 if (declarator->kind == cdk_function
1716 && declarator->declarator->kind == cdk_id)
1717 return true;
1718 if (declarator->kind == cdk_id
1719 || declarator->kind == cdk_decomp
1720 || declarator->kind == cdk_error)
1721 return false;
1722 declarator = declarator->declarator;
1723 }
1724 return false;
1725 }
1726
1727 /* The parser. */
1728
1729 /* Overview
1730 --------
1731
1732 A cp_parser parses the token stream as specified by the C++
1733 grammar. Its job is purely parsing, not semantic analysis. For
1734 example, the parser breaks the token stream into declarators,
1735 expressions, statements, and other similar syntactic constructs.
1736 It does not check that the types of the expressions on either side
1737 of an assignment-statement are compatible, or that a function is
1738 not declared with a parameter of type `void'.
1739
1740 The parser invokes routines elsewhere in the compiler to perform
1741 semantic analysis and to build up the abstract syntax tree for the
1742 code processed.
1743
1744 The parser (and the template instantiation code, which is, in a
1745 way, a close relative of parsing) are the only parts of the
1746 compiler that should be calling push_scope and pop_scope, or
1747 related functions. The parser (and template instantiation code)
1748 keeps track of what scope is presently active; everything else
1749 should simply honor that. (The code that generates static
1750 initializers may also need to set the scope, in order to check
1751 access control correctly when emitting the initializers.)
1752
1753 Methodology
1754 -----------
1755
1756 The parser is of the standard recursive-descent variety. Upcoming
1757 tokens in the token stream are examined in order to determine which
1758 production to use when parsing a non-terminal. Some C++ constructs
1759 require arbitrary look ahead to disambiguate. For example, it is
1760 impossible, in the general case, to tell whether a statement is an
1761 expression or declaration without scanning the entire statement.
1762 Therefore, the parser is capable of "parsing tentatively." When the
1763 parser is not sure what construct comes next, it enters this mode.
1764 Then, while we attempt to parse the construct, the parser queues up
1765 error messages, rather than issuing them immediately, and saves the
1766 tokens it consumes. If the construct is parsed successfully, the
1767 parser "commits", i.e., it issues any queued error messages and
1768 the tokens that were being preserved are permanently discarded.
1769 If, however, the construct is not parsed successfully, the parser
1770 rolls back its state completely so that it can resume parsing using
1771 a different alternative.
1772
1773 Future Improvements
1774 -------------------
1775
1776 The performance of the parser could probably be improved substantially.
1777 We could often eliminate the need to parse tentatively by looking ahead
1778 a little bit. In some places, this approach might not entirely eliminate
1779 the need to parse tentatively, but it might still speed up the average
1780 case. */
1781
1782 /* Flags that are passed to some parsing functions. These values can
1783 be bitwise-ored together. */
1784
1785 enum
1786 {
1787 /* No flags. */
1788 CP_PARSER_FLAGS_NONE = 0x0,
1789 /* The construct is optional. If it is not present, then no error
1790 should be issued. */
1791 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1792 /* When parsing a type-specifier, treat user-defined type-names
1793 as non-type identifiers. */
1794 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1795 /* When parsing a type-specifier, do not try to parse a class-specifier
1796 or enum-specifier. */
1797 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1798 /* When parsing a decl-specifier-seq, only allow type-specifier or
1799 constexpr. */
1800 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1801 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1802 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1803 };
1804
1805 /* This type is used for parameters and variables which hold
1806 combinations of the above flags. */
1807 typedef int cp_parser_flags;
1808
1809 /* The different kinds of declarators we want to parse. */
1810
1811 enum cp_parser_declarator_kind
1812 {
1813 /* We want an abstract declarator. */
1814 CP_PARSER_DECLARATOR_ABSTRACT,
1815 /* We want a named declarator. */
1816 CP_PARSER_DECLARATOR_NAMED,
1817 /* We don't mind, but the name must be an unqualified-id. */
1818 CP_PARSER_DECLARATOR_EITHER
1819 };
1820
1821 /* The precedence values used to parse binary expressions. The minimum value
1822 of PREC must be 1, because zero is reserved to quickly discriminate
1823 binary operators from other tokens. */
1824
1825 enum cp_parser_prec
1826 {
1827 PREC_NOT_OPERATOR,
1828 PREC_LOGICAL_OR_EXPRESSION,
1829 PREC_LOGICAL_AND_EXPRESSION,
1830 PREC_INCLUSIVE_OR_EXPRESSION,
1831 PREC_EXCLUSIVE_OR_EXPRESSION,
1832 PREC_AND_EXPRESSION,
1833 PREC_EQUALITY_EXPRESSION,
1834 PREC_RELATIONAL_EXPRESSION,
1835 PREC_SHIFT_EXPRESSION,
1836 PREC_ADDITIVE_EXPRESSION,
1837 PREC_MULTIPLICATIVE_EXPRESSION,
1838 PREC_PM_EXPRESSION,
1839 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1840 };
1841
1842 /* A mapping from a token type to a corresponding tree node type, with a
1843 precedence value. */
1844
1845 struct cp_parser_binary_operations_map_node
1846 {
1847 /* The token type. */
1848 enum cpp_ttype token_type;
1849 /* The corresponding tree code. */
1850 enum tree_code tree_type;
1851 /* The precedence of this operator. */
1852 enum cp_parser_prec prec;
1853 };
1854
1855 struct cp_parser_expression_stack_entry
1856 {
1857 /* Left hand side of the binary operation we are currently
1858 parsing. */
1859 cp_expr lhs;
1860 /* Original tree code for left hand side, if it was a binary
1861 expression itself (used for -Wparentheses). */
1862 enum tree_code lhs_type;
1863 /* Tree code for the binary operation we are parsing. */
1864 enum tree_code tree_type;
1865 /* Precedence of the binary operation we are parsing. */
1866 enum cp_parser_prec prec;
1867 /* Location of the binary operation we are parsing. */
1868 location_t loc;
1869 };
1870
1871 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1872 entries because precedence levels on the stack are monotonically
1873 increasing. */
1874 typedef struct cp_parser_expression_stack_entry
1875 cp_parser_expression_stack[NUM_PREC_VALUES];
1876
1877 /* Prototypes. */
1878
1879 /* Constructors and destructors. */
1880
1881 static cp_parser_context *cp_parser_context_new
1882 (cp_parser_context *);
1883
1884 /* Class variables. */
1885
1886 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1887
1888 /* The operator-precedence table used by cp_parser_binary_expression.
1889 Transformed into an associative array (binops_by_token) by
1890 cp_parser_new. */
1891
1892 static const cp_parser_binary_operations_map_node binops[] = {
1893 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1894 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1895
1896 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1898 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1899
1900 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1901 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1902
1903 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1904 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1905
1906 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1908 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1909 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1910
1911 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1912 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1913
1914 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1915
1916 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1917
1918 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1919
1920 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1921
1922 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1923 };
1924
1925 /* The same as binops, but initialized by cp_parser_new so that
1926 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1927 for speed. */
1928 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1929
1930 /* Constructors and destructors. */
1931
1932 /* Construct a new context. The context below this one on the stack
1933 is given by NEXT. */
1934
1935 static cp_parser_context *
1936 cp_parser_context_new (cp_parser_context* next)
1937 {
1938 cp_parser_context *context;
1939
1940 /* Allocate the storage. */
1941 if (cp_parser_context_free_list != NULL)
1942 {
1943 /* Pull the first entry from the free list. */
1944 context = cp_parser_context_free_list;
1945 cp_parser_context_free_list = context->next;
1946 memset (context, 0, sizeof (*context));
1947 }
1948 else
1949 context = ggc_cleared_alloc<cp_parser_context> ();
1950
1951 /* No errors have occurred yet in this context. */
1952 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1953 /* If this is not the bottommost context, copy information that we
1954 need from the previous context. */
1955 if (next)
1956 {
1957 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1958 expression, then we are parsing one in this context, too. */
1959 context->object_type = next->object_type;
1960 /* Thread the stack. */
1961 context->next = next;
1962 }
1963
1964 return context;
1965 }
1966
1967 /* Managing the unparsed function queues. */
1968
1969 #define unparsed_funs_with_default_args \
1970 parser->unparsed_queues->last ().funs_with_default_args
1971 #define unparsed_funs_with_definitions \
1972 parser->unparsed_queues->last ().funs_with_definitions
1973 #define unparsed_nsdmis \
1974 parser->unparsed_queues->last ().nsdmis
1975 #define unparsed_classes \
1976 parser->unparsed_queues->last ().classes
1977
1978 static void
1979 push_unparsed_function_queues (cp_parser *parser)
1980 {
1981 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1982 vec_safe_push (parser->unparsed_queues, e);
1983 }
1984
1985 static void
1986 pop_unparsed_function_queues (cp_parser *parser)
1987 {
1988 release_tree_vector (unparsed_funs_with_definitions);
1989 parser->unparsed_queues->pop ();
1990 }
1991
1992 /* Prototypes. */
1993
1994 /* Constructors and destructors. */
1995
1996 static cp_parser *cp_parser_new
1997 (void);
1998
1999 /* Routines to parse various constructs.
2000
2001 Those that return `tree' will return the error_mark_node (rather
2002 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2003 Sometimes, they will return an ordinary node if error-recovery was
2004 attempted, even though a parse error occurred. So, to check
2005 whether or not a parse error occurred, you should always use
2006 cp_parser_error_occurred. If the construct is optional (indicated
2007 either by an `_opt' in the name of the function that does the
2008 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2009 the construct is not present. */
2010
2011 /* Lexical conventions [gram.lex] */
2012
2013 static cp_expr cp_parser_identifier
2014 (cp_parser *);
2015 static cp_expr cp_parser_string_literal
2016 (cp_parser *, bool, bool, bool);
2017 static cp_expr cp_parser_userdef_char_literal
2018 (cp_parser *);
2019 static tree cp_parser_userdef_string_literal
2020 (tree);
2021 static cp_expr cp_parser_userdef_numeric_literal
2022 (cp_parser *);
2023
2024 /* Basic concepts [gram.basic] */
2025
2026 static bool cp_parser_translation_unit
2027 (cp_parser *);
2028
2029 /* Expressions [gram.expr] */
2030
2031 static cp_expr cp_parser_primary_expression
2032 (cp_parser *, bool, bool, bool, cp_id_kind *);
2033 static cp_expr cp_parser_id_expression
2034 (cp_parser *, bool, bool, bool *, bool, bool);
2035 static cp_expr cp_parser_unqualified_id
2036 (cp_parser *, bool, bool, bool, bool);
2037 static tree cp_parser_nested_name_specifier_opt
2038 (cp_parser *, bool, bool, bool, bool, bool = false);
2039 static tree cp_parser_nested_name_specifier
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_qualifying_entity
2042 (cp_parser *, bool, bool, bool, bool, bool);
2043 static cp_expr cp_parser_postfix_expression
2044 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2045 static tree cp_parser_postfix_open_square_expression
2046 (cp_parser *, tree, bool, bool);
2047 static tree cp_parser_postfix_dot_deref_expression
2048 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2049 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2050 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2051 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2052 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2053 static void cp_parser_pseudo_destructor_name
2054 (cp_parser *, tree, tree *, tree *);
2055 static cp_expr cp_parser_unary_expression
2056 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2057 static enum tree_code cp_parser_unary_operator
2058 (cp_token *);
2059 static tree cp_parser_new_expression
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_placement
2062 (cp_parser *);
2063 static tree cp_parser_new_type_id
2064 (cp_parser *, tree *);
2065 static cp_declarator *cp_parser_new_declarator_opt
2066 (cp_parser *);
2067 static cp_declarator *cp_parser_direct_new_declarator
2068 (cp_parser *);
2069 static vec<tree, va_gc> *cp_parser_new_initializer
2070 (cp_parser *);
2071 static tree cp_parser_delete_expression
2072 (cp_parser *);
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser *, bool, bool, bool, cp_id_kind *);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser *, cp_expr);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2082 (cp_parser *);
2083 static cp_expr cp_parser_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser *, bool = false, bool * = NULL, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2088 (cp_parser *);
2089 static cp_expr cp_parser_lambda_expression
2090 (cp_parser *);
2091 static void cp_parser_lambda_introducer
2092 (cp_parser *, tree);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser *, tree);
2095 static void cp_parser_lambda_body
2096 (cp_parser *, tree);
2097
2098 /* Statements [gram.stmt.stmt] */
2099
2100 static void cp_parser_statement
2101 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser *, tree);
2104 static tree cp_parser_expression_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_compound_statement
2107 (cp_parser *, tree, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser *, tree);
2110 static tree cp_parser_selection_statement
2111 (cp_parser *, bool *, vec<tree> *);
2112 static tree cp_parser_condition
2113 (cp_parser *);
2114 static tree cp_parser_iteration_statement
2115 (cp_parser *, bool *, bool);
2116 static bool cp_parser_init_statement
2117 (cp_parser *, tree *decl);
2118 static tree cp_parser_for
2119 (cp_parser *, bool);
2120 static tree cp_parser_c_for
2121 (cp_parser *, tree, tree, bool);
2122 static tree cp_parser_range_for
2123 (cp_parser *, tree, tree, tree, bool);
2124 static void do_range_for_auto_deduction
2125 (tree, tree);
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree, tree *, tree *);
2128 static tree cp_parser_range_for_member_function
2129 (tree, tree);
2130 static tree cp_parser_jump_statement
2131 (cp_parser *);
2132 static void cp_parser_declaration_statement
2133 (cp_parser *);
2134
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &);
2139
2140 /* Declarations [gram.dcl.dcl] */
2141
2142 static void cp_parser_declaration_seq_opt
2143 (cp_parser *);
2144 static void cp_parser_declaration
2145 (cp_parser *);
2146 static void cp_parser_block_declaration
2147 (cp_parser *, bool);
2148 static void cp_parser_simple_declaration
2149 (cp_parser *, bool, tree *);
2150 static void cp_parser_decl_specifier_seq
2151 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2152 static tree cp_parser_storage_class_specifier_opt
2153 (cp_parser *);
2154 static tree cp_parser_function_specifier_opt
2155 (cp_parser *, cp_decl_specifier_seq *);
2156 static tree cp_parser_type_specifier
2157 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2158 int *, bool *);
2159 static tree cp_parser_simple_type_specifier
2160 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2161 static tree cp_parser_type_name
2162 (cp_parser *, bool);
2163 static tree cp_parser_type_name
2164 (cp_parser *);
2165 static tree cp_parser_nonclass_name
2166 (cp_parser* parser);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser *, bool, bool);
2169 static tree cp_parser_enum_specifier
2170 (cp_parser *);
2171 static void cp_parser_enumerator_list
2172 (cp_parser *, tree);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser *, tree);
2175 static tree cp_parser_namespace_name
2176 (cp_parser *);
2177 static void cp_parser_namespace_definition
2178 (cp_parser *);
2179 static void cp_parser_namespace_body
2180 (cp_parser *);
2181 static tree cp_parser_qualified_namespace_specifier
2182 (cp_parser *);
2183 static void cp_parser_namespace_alias_definition
2184 (cp_parser *);
2185 static bool cp_parser_using_declaration
2186 (cp_parser *, bool);
2187 static void cp_parser_using_directive
2188 (cp_parser *);
2189 static tree cp_parser_alias_declaration
2190 (cp_parser *);
2191 static void cp_parser_asm_definition
2192 (cp_parser *);
2193 static void cp_parser_linkage_specification
2194 (cp_parser *);
2195 static void cp_parser_static_assert
2196 (cp_parser *, bool);
2197 static tree cp_parser_decltype
2198 (cp_parser *);
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2201
2202 /* Declarators [gram.dcl.decl] */
2203
2204 static tree cp_parser_init_declarator
2205 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2206 bool, bool, int, bool *, tree *, location_t *, tree *);
2207 static cp_declarator *cp_parser_declarator
2208 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2209 static cp_declarator *cp_parser_direct_declarator
2210 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2211 static enum tree_code cp_parser_ptr_operator
2212 (cp_parser *, tree *, cp_cv_quals *, tree *);
2213 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2214 (cp_parser *);
2215 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2216 (cp_parser *);
2217 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2218 (cp_parser *);
2219 static tree cp_parser_tx_qualifier_opt
2220 (cp_parser *);
2221 static tree cp_parser_late_return_type_opt
2222 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2223 static tree cp_parser_declarator_id
2224 (cp_parser *, bool);
2225 static tree cp_parser_type_id
2226 (cp_parser *);
2227 static tree cp_parser_template_type_arg
2228 (cp_parser *);
2229 static tree cp_parser_trailing_type_id (cp_parser *);
2230 static tree cp_parser_type_id_1
2231 (cp_parser *, bool, bool);
2232 static void cp_parser_type_specifier_seq
2233 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2234 static tree cp_parser_parameter_declaration_clause
2235 (cp_parser *);
2236 static tree cp_parser_parameter_declaration_list
2237 (cp_parser *, bool *);
2238 static cp_parameter_declarator *cp_parser_parameter_declaration
2239 (cp_parser *, bool, bool *);
2240 static tree cp_parser_default_argument
2241 (cp_parser *, bool);
2242 static void cp_parser_function_body
2243 (cp_parser *, bool);
2244 static tree cp_parser_initializer
2245 (cp_parser *, bool *, bool *);
2246 static cp_expr cp_parser_initializer_clause
2247 (cp_parser *, bool *);
2248 static cp_expr cp_parser_braced_list
2249 (cp_parser*, bool*);
2250 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2251 (cp_parser *, bool *);
2252
2253 static void cp_parser_ctor_initializer_opt_and_function_body
2254 (cp_parser *, bool);
2255
2256 static tree cp_parser_late_parsing_omp_declare_simd
2257 (cp_parser *, tree);
2258
2259 static tree cp_parser_late_parsing_oacc_routine
2260 (cp_parser *, tree);
2261
2262 static tree synthesize_implicit_template_parm
2263 (cp_parser *, tree);
2264 static tree finish_fully_implicit_template
2265 (cp_parser *, tree);
2266
2267 /* Classes [gram.class] */
2268
2269 static tree cp_parser_class_name
2270 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2271 static tree cp_parser_class_specifier
2272 (cp_parser *);
2273 static tree cp_parser_class_head
2274 (cp_parser *, bool *);
2275 static enum tag_types cp_parser_class_key
2276 (cp_parser *);
2277 static void cp_parser_type_parameter_key
2278 (cp_parser* parser);
2279 static void cp_parser_member_specification_opt
2280 (cp_parser *);
2281 static void cp_parser_member_declaration
2282 (cp_parser *);
2283 static tree cp_parser_pure_specifier
2284 (cp_parser *);
2285 static tree cp_parser_constant_initializer
2286 (cp_parser *);
2287
2288 /* Derived classes [gram.class.derived] */
2289
2290 static tree cp_parser_base_clause
2291 (cp_parser *);
2292 static tree cp_parser_base_specifier
2293 (cp_parser *);
2294
2295 /* Special member functions [gram.special] */
2296
2297 static tree cp_parser_conversion_function_id
2298 (cp_parser *);
2299 static tree cp_parser_conversion_type_id
2300 (cp_parser *);
2301 static cp_declarator *cp_parser_conversion_declarator_opt
2302 (cp_parser *);
2303 static void cp_parser_ctor_initializer_opt
2304 (cp_parser *);
2305 static void cp_parser_mem_initializer_list
2306 (cp_parser *);
2307 static tree cp_parser_mem_initializer
2308 (cp_parser *);
2309 static tree cp_parser_mem_initializer_id
2310 (cp_parser *);
2311
2312 /* Overloading [gram.over] */
2313
2314 static cp_expr cp_parser_operator_function_id
2315 (cp_parser *);
2316 static cp_expr cp_parser_operator
2317 (cp_parser *);
2318
2319 /* Templates [gram.temp] */
2320
2321 static void cp_parser_template_declaration
2322 (cp_parser *, bool);
2323 static tree cp_parser_template_parameter_list
2324 (cp_parser *);
2325 static tree cp_parser_template_parameter
2326 (cp_parser *, bool *, bool *);
2327 static tree cp_parser_type_parameter
2328 (cp_parser *, bool *);
2329 static tree cp_parser_template_id
2330 (cp_parser *, bool, bool, enum tag_types, bool);
2331 static tree cp_parser_template_name
2332 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2333 static tree cp_parser_template_argument_list
2334 (cp_parser *);
2335 static tree cp_parser_template_argument
2336 (cp_parser *);
2337 static void cp_parser_explicit_instantiation
2338 (cp_parser *);
2339 static void cp_parser_explicit_specialization
2340 (cp_parser *);
2341
2342 /* Exception handling [gram.exception] */
2343
2344 static tree cp_parser_try_block
2345 (cp_parser *);
2346 static void cp_parser_function_try_block
2347 (cp_parser *);
2348 static void cp_parser_handler_seq
2349 (cp_parser *);
2350 static void cp_parser_handler
2351 (cp_parser *);
2352 static tree cp_parser_exception_declaration
2353 (cp_parser *);
2354 static tree cp_parser_throw_expression
2355 (cp_parser *);
2356 static tree cp_parser_exception_specification_opt
2357 (cp_parser *);
2358 static tree cp_parser_type_id_list
2359 (cp_parser *);
2360
2361 /* GNU Extensions */
2362
2363 static tree cp_parser_asm_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_asm_operand_list
2366 (cp_parser *);
2367 static tree cp_parser_asm_clobber_list
2368 (cp_parser *);
2369 static tree cp_parser_asm_label_list
2370 (cp_parser *);
2371 static bool cp_next_tokens_can_be_attribute_p
2372 (cp_parser *);
2373 static bool cp_next_tokens_can_be_gnu_attribute_p
2374 (cp_parser *);
2375 static bool cp_next_tokens_can_be_std_attribute_p
2376 (cp_parser *);
2377 static bool cp_nth_tokens_can_be_std_attribute_p
2378 (cp_parser *, size_t);
2379 static bool cp_nth_tokens_can_be_gnu_attribute_p
2380 (cp_parser *, size_t);
2381 static bool cp_nth_tokens_can_be_attribute_p
2382 (cp_parser *, size_t);
2383 static tree cp_parser_attributes_opt
2384 (cp_parser *);
2385 static tree cp_parser_gnu_attributes_opt
2386 (cp_parser *);
2387 static tree cp_parser_gnu_attribute_list
2388 (cp_parser *);
2389 static tree cp_parser_std_attribute
2390 (cp_parser *, tree);
2391 static tree cp_parser_std_attribute_spec
2392 (cp_parser *);
2393 static tree cp_parser_std_attribute_spec_seq
2394 (cp_parser *);
2395 static bool cp_parser_extension_opt
2396 (cp_parser *, int *);
2397 static void cp_parser_label_declaration
2398 (cp_parser *);
2399
2400 /* Concept Extensions */
2401
2402 static tree cp_parser_requires_clause
2403 (cp_parser *);
2404 static tree cp_parser_requires_clause_opt
2405 (cp_parser *);
2406 static tree cp_parser_requires_expression
2407 (cp_parser *);
2408 static tree cp_parser_requirement_parameter_list
2409 (cp_parser *);
2410 static tree cp_parser_requirement_body
2411 (cp_parser *);
2412 static tree cp_parser_requirement_list
2413 (cp_parser *);
2414 static tree cp_parser_requirement
2415 (cp_parser *);
2416 static tree cp_parser_simple_requirement
2417 (cp_parser *);
2418 static tree cp_parser_compound_requirement
2419 (cp_parser *);
2420 static tree cp_parser_type_requirement
2421 (cp_parser *);
2422 static tree cp_parser_nested_requirement
2423 (cp_parser *);
2424
2425 /* Transactional Memory Extensions */
2426
2427 static tree cp_parser_transaction
2428 (cp_parser *, cp_token *);
2429 static tree cp_parser_transaction_expression
2430 (cp_parser *, enum rid);
2431 static void cp_parser_function_transaction
2432 (cp_parser *, enum rid);
2433 static tree cp_parser_transaction_cancel
2434 (cp_parser *);
2435
2436 enum pragma_context {
2437 pragma_external,
2438 pragma_member,
2439 pragma_objc_icode,
2440 pragma_stmt,
2441 pragma_compound
2442 };
2443 static bool cp_parser_pragma
2444 (cp_parser *, enum pragma_context, bool *);
2445
2446 /* Objective-C++ Productions */
2447
2448 static tree cp_parser_objc_message_receiver
2449 (cp_parser *);
2450 static tree cp_parser_objc_message_args
2451 (cp_parser *);
2452 static tree cp_parser_objc_message_expression
2453 (cp_parser *);
2454 static cp_expr cp_parser_objc_encode_expression
2455 (cp_parser *);
2456 static tree cp_parser_objc_defs_expression
2457 (cp_parser *);
2458 static tree cp_parser_objc_protocol_expression
2459 (cp_parser *);
2460 static tree cp_parser_objc_selector_expression
2461 (cp_parser *);
2462 static cp_expr cp_parser_objc_expression
2463 (cp_parser *);
2464 static bool cp_parser_objc_selector_p
2465 (enum cpp_ttype);
2466 static tree cp_parser_objc_selector
2467 (cp_parser *);
2468 static tree cp_parser_objc_protocol_refs_opt
2469 (cp_parser *);
2470 static void cp_parser_objc_declaration
2471 (cp_parser *, tree);
2472 static tree cp_parser_objc_statement
2473 (cp_parser *);
2474 static bool cp_parser_objc_valid_prefix_attributes
2475 (cp_parser *, tree *);
2476 static void cp_parser_objc_at_property_declaration
2477 (cp_parser *) ;
2478 static void cp_parser_objc_at_synthesize_declaration
2479 (cp_parser *) ;
2480 static void cp_parser_objc_at_dynamic_declaration
2481 (cp_parser *) ;
2482 static tree cp_parser_objc_struct_declaration
2483 (cp_parser *) ;
2484
2485 /* Utility Routines */
2486
2487 static cp_expr cp_parser_lookup_name
2488 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2489 static tree cp_parser_lookup_name_simple
2490 (cp_parser *, tree, location_t);
2491 static tree cp_parser_maybe_treat_template_as_class
2492 (tree, bool);
2493 static bool cp_parser_check_declarator_template_parameters
2494 (cp_parser *, cp_declarator *, location_t);
2495 static bool cp_parser_check_template_parameters
2496 (cp_parser *, unsigned, location_t, cp_declarator *);
2497 static cp_expr cp_parser_simple_cast_expression
2498 (cp_parser *);
2499 static tree cp_parser_global_scope_opt
2500 (cp_parser *, bool);
2501 static bool cp_parser_constructor_declarator_p
2502 (cp_parser *, bool);
2503 static tree cp_parser_function_definition_from_specifiers_and_declarator
2504 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2505 static tree cp_parser_function_definition_after_declarator
2506 (cp_parser *, bool);
2507 static bool cp_parser_template_declaration_after_export
2508 (cp_parser *, bool);
2509 static void cp_parser_perform_template_parameter_access_checks
2510 (vec<deferred_access_check, va_gc> *);
2511 static tree cp_parser_single_declaration
2512 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2513 static cp_expr cp_parser_functional_cast
2514 (cp_parser *, tree);
2515 static tree cp_parser_save_member_function_body
2516 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2517 static tree cp_parser_save_nsdmi
2518 (cp_parser *);
2519 static tree cp_parser_enclosed_template_argument_list
2520 (cp_parser *);
2521 static void cp_parser_save_default_args
2522 (cp_parser *, tree);
2523 static void cp_parser_late_parsing_for_member
2524 (cp_parser *, tree);
2525 static tree cp_parser_late_parse_one_default_arg
2526 (cp_parser *, tree, tree, tree);
2527 static void cp_parser_late_parsing_nsdmi
2528 (cp_parser *, tree);
2529 static void cp_parser_late_parsing_default_args
2530 (cp_parser *, tree);
2531 static tree cp_parser_sizeof_operand
2532 (cp_parser *, enum rid);
2533 static cp_expr cp_parser_trait_expr
2534 (cp_parser *, enum rid);
2535 static bool cp_parser_declares_only_class_p
2536 (cp_parser *);
2537 static void cp_parser_set_storage_class
2538 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2539 static void cp_parser_set_decl_spec_type
2540 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2541 static void set_and_check_decl_spec_loc
2542 (cp_decl_specifier_seq *decl_specs,
2543 cp_decl_spec ds, cp_token *);
2544 static bool cp_parser_friend_p
2545 (const cp_decl_specifier_seq *);
2546 static void cp_parser_required_error
2547 (cp_parser *, required_token, bool, location_t);
2548 static cp_token *cp_parser_require
2549 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2550 static cp_token *cp_parser_require_keyword
2551 (cp_parser *, enum rid, required_token);
2552 static bool cp_parser_token_starts_function_definition_p
2553 (cp_token *);
2554 static bool cp_parser_next_token_starts_class_definition_p
2555 (cp_parser *);
2556 static bool cp_parser_next_token_ends_template_argument_p
2557 (cp_parser *);
2558 static bool cp_parser_nth_token_starts_template_argument_list_p
2559 (cp_parser *, size_t);
2560 static enum tag_types cp_parser_token_is_class_key
2561 (cp_token *);
2562 static enum tag_types cp_parser_token_is_type_parameter_key
2563 (cp_token *);
2564 static void cp_parser_check_class_key
2565 (enum tag_types, tree type);
2566 static void cp_parser_check_access_in_redeclaration
2567 (tree type, location_t location);
2568 static bool cp_parser_optional_template_keyword
2569 (cp_parser *);
2570 static void cp_parser_pre_parsed_nested_name_specifier
2571 (cp_parser *);
2572 static bool cp_parser_cache_group
2573 (cp_parser *, enum cpp_ttype, unsigned);
2574 static tree cp_parser_cache_defarg
2575 (cp_parser *parser, bool nsdmi);
2576 static void cp_parser_parse_tentatively
2577 (cp_parser *);
2578 static void cp_parser_commit_to_tentative_parse
2579 (cp_parser *);
2580 static void cp_parser_commit_to_topmost_tentative_parse
2581 (cp_parser *);
2582 static void cp_parser_abort_tentative_parse
2583 (cp_parser *);
2584 static bool cp_parser_parse_definitely
2585 (cp_parser *);
2586 static inline bool cp_parser_parsing_tentatively
2587 (cp_parser *);
2588 static bool cp_parser_uncommitted_to_tentative_parse_p
2589 (cp_parser *);
2590 static void cp_parser_error
2591 (cp_parser *, const char *);
2592 static void cp_parser_name_lookup_error
2593 (cp_parser *, tree, tree, name_lookup_error, location_t);
2594 static bool cp_parser_simulate_error
2595 (cp_parser *);
2596 static bool cp_parser_check_type_definition
2597 (cp_parser *);
2598 static void cp_parser_check_for_definition_in_return_type
2599 (cp_declarator *, tree, location_t type_location);
2600 static void cp_parser_check_for_invalid_template_id
2601 (cp_parser *, tree, enum tag_types, location_t location);
2602 static bool cp_parser_non_integral_constant_expression
2603 (cp_parser *, non_integral_constant);
2604 static void cp_parser_diagnose_invalid_type_name
2605 (cp_parser *, tree, location_t);
2606 static bool cp_parser_parse_and_diagnose_invalid_type_name
2607 (cp_parser *);
2608 static int cp_parser_skip_to_closing_parenthesis
2609 (cp_parser *, bool, bool, bool);
2610 static void cp_parser_skip_to_end_of_statement
2611 (cp_parser *);
2612 static void cp_parser_consume_semicolon_at_end_of_statement
2613 (cp_parser *);
2614 static void cp_parser_skip_to_end_of_block_or_statement
2615 (cp_parser *);
2616 static bool cp_parser_skip_to_closing_brace
2617 (cp_parser *);
2618 static void cp_parser_skip_to_end_of_template_parameter_list
2619 (cp_parser *);
2620 static void cp_parser_skip_to_pragma_eol
2621 (cp_parser*, cp_token *);
2622 static bool cp_parser_error_occurred
2623 (cp_parser *);
2624 static bool cp_parser_allow_gnu_extensions_p
2625 (cp_parser *);
2626 static bool cp_parser_is_pure_string_literal
2627 (cp_token *);
2628 static bool cp_parser_is_string_literal
2629 (cp_token *);
2630 static bool cp_parser_is_keyword
2631 (cp_token *, enum rid);
2632 static tree cp_parser_make_typename_type
2633 (cp_parser *, tree, location_t location);
2634 static cp_declarator * cp_parser_make_indirect_declarator
2635 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2636 static bool cp_parser_compound_literal_p
2637 (cp_parser *);
2638 static bool cp_parser_array_designator_p
2639 (cp_parser *);
2640 static bool cp_parser_init_statement_p
2641 (cp_parser *);
2642 static bool cp_parser_skip_to_closing_square_bracket
2643 (cp_parser *);
2644
2645 /* Concept-related syntactic transformations */
2646
2647 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2648 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2649
2650 // -------------------------------------------------------------------------- //
2651 // Unevaluated Operand Guard
2652 //
2653 // Implementation of an RAII helper for unevaluated operand parsing.
2654 cp_unevaluated::cp_unevaluated ()
2655 {
2656 ++cp_unevaluated_operand;
2657 ++c_inhibit_evaluation_warnings;
2658 }
2659
2660 cp_unevaluated::~cp_unevaluated ()
2661 {
2662 --c_inhibit_evaluation_warnings;
2663 --cp_unevaluated_operand;
2664 }
2665
2666 // -------------------------------------------------------------------------- //
2667 // Tentative Parsing
2668
2669 /* Returns nonzero if we are parsing tentatively. */
2670
2671 static inline bool
2672 cp_parser_parsing_tentatively (cp_parser* parser)
2673 {
2674 return parser->context->next != NULL;
2675 }
2676
2677 /* Returns nonzero if TOKEN is a string literal. */
2678
2679 static bool
2680 cp_parser_is_pure_string_literal (cp_token* token)
2681 {
2682 return (token->type == CPP_STRING ||
2683 token->type == CPP_STRING16 ||
2684 token->type == CPP_STRING32 ||
2685 token->type == CPP_WSTRING ||
2686 token->type == CPP_UTF8STRING);
2687 }
2688
2689 /* Returns nonzero if TOKEN is a string literal
2690 of a user-defined string literal. */
2691
2692 static bool
2693 cp_parser_is_string_literal (cp_token* token)
2694 {
2695 return (cp_parser_is_pure_string_literal (token) ||
2696 token->type == CPP_STRING_USERDEF ||
2697 token->type == CPP_STRING16_USERDEF ||
2698 token->type == CPP_STRING32_USERDEF ||
2699 token->type == CPP_WSTRING_USERDEF ||
2700 token->type == CPP_UTF8STRING_USERDEF);
2701 }
2702
2703 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2704
2705 static bool
2706 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2707 {
2708 return token->keyword == keyword;
2709 }
2710
2711 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2712 PRAGMA_NONE. */
2713
2714 static enum pragma_kind
2715 cp_parser_pragma_kind (cp_token *token)
2716 {
2717 if (token->type != CPP_PRAGMA)
2718 return PRAGMA_NONE;
2719 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2720 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2721 }
2722
2723 /* Helper function for cp_parser_error.
2724 Having peeked a token of kind TOK1_KIND that might signify
2725 a conflict marker, peek successor tokens to determine
2726 if we actually do have a conflict marker.
2727 Specifically, we consider a run of 7 '<', '=' or '>' characters
2728 at the start of a line as a conflict marker.
2729 These come through the lexer as three pairs and a single,
2730 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2731 If it returns true, *OUT_LOC is written to with the location/range
2732 of the marker. */
2733
2734 static bool
2735 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2736 location_t *out_loc)
2737 {
2738 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2739 if (token2->type != tok1_kind)
2740 return false;
2741 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2742 if (token3->type != tok1_kind)
2743 return false;
2744 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2745 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2746 return false;
2747
2748 /* It must be at the start of the line. */
2749 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2750 if (LOCATION_COLUMN (start_loc) != 1)
2751 return false;
2752
2753 /* We have a conflict marker. Construct a location of the form:
2754 <<<<<<<
2755 ^~~~~~~
2756 with start == caret, finishing at the end of the marker. */
2757 location_t finish_loc = get_finish (token4->location);
2758 *out_loc = make_location (start_loc, start_loc, finish_loc);
2759
2760 return true;
2761 }
2762
2763 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2764 RT_CLOSE_PAREN. */
2765
2766 static const char *
2767 get_matching_symbol (required_token token_desc)
2768 {
2769 switch (token_desc)
2770 {
2771 default:
2772 gcc_unreachable ();
2773 return "";
2774 case RT_CLOSE_BRACE:
2775 return "{";
2776 case RT_CLOSE_PAREN:
2777 return "(";
2778 }
2779 }
2780
2781 /* Attempt to convert TOKEN_DESC from a required_token to an
2782 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2783
2784 static enum cpp_ttype
2785 get_required_cpp_ttype (required_token token_desc)
2786 {
2787 switch (token_desc)
2788 {
2789 case RT_SEMICOLON:
2790 return CPP_SEMICOLON;
2791 case RT_OPEN_PAREN:
2792 return CPP_OPEN_PAREN;
2793 case RT_CLOSE_BRACE:
2794 return CPP_CLOSE_BRACE;
2795 case RT_OPEN_BRACE:
2796 return CPP_OPEN_BRACE;
2797 case RT_CLOSE_SQUARE:
2798 return CPP_CLOSE_SQUARE;
2799 case RT_OPEN_SQUARE:
2800 return CPP_OPEN_SQUARE;
2801 case RT_COMMA:
2802 return CPP_COMMA;
2803 case RT_COLON:
2804 return CPP_COLON;
2805 case RT_CLOSE_PAREN:
2806 return CPP_CLOSE_PAREN;
2807
2808 default:
2809 /* Use CPP_EOF as a "no completions possible" code. */
2810 return CPP_EOF;
2811 }
2812 }
2813
2814
2815 /* Subroutine of cp_parser_error and cp_parser_required_error.
2816
2817 Issue a diagnostic of the form
2818 FILE:LINE: MESSAGE before TOKEN
2819 where TOKEN is the next token in the input stream. MESSAGE
2820 (specified by the caller) is usually of the form "expected
2821 OTHER-TOKEN".
2822
2823 This bypasses the check for tentative passing, and potentially
2824 adds material needed by cp_parser_required_error.
2825
2826 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2827 suggesting insertion of the missing token.
2828
2829 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2830 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2831 location. */
2832
2833 static void
2834 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2835 required_token missing_token_desc,
2836 location_t matching_location)
2837 {
2838 cp_token *token = cp_lexer_peek_token (parser->lexer);
2839 /* This diagnostic makes more sense if it is tagged to the line
2840 of the token we just peeked at. */
2841 cp_lexer_set_source_position_from_token (token);
2842
2843 if (token->type == CPP_PRAGMA)
2844 {
2845 error_at (token->location,
2846 "%<#pragma%> is not allowed here");
2847 cp_parser_skip_to_pragma_eol (parser, token);
2848 return;
2849 }
2850
2851 /* If this is actually a conflict marker, report it as such. */
2852 if (token->type == CPP_LSHIFT
2853 || token->type == CPP_RSHIFT
2854 || token->type == CPP_EQ_EQ)
2855 {
2856 location_t loc;
2857 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2858 {
2859 error_at (loc, "version control conflict marker in file");
2860 return;
2861 }
2862 }
2863
2864 gcc_rich_location richloc (input_location);
2865
2866 bool added_matching_location = false;
2867
2868 if (missing_token_desc != RT_NONE)
2869 {
2870 /* Potentially supply a fix-it hint, suggesting to add the
2871 missing token immediately after the *previous* token.
2872 This may move the primary location within richloc. */
2873 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2874 location_t prev_token_loc
2875 = cp_lexer_previous_token (parser->lexer)->location;
2876 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2877
2878 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2879 Attempt to consolidate diagnostics by printing it as a
2880 secondary range within the main diagnostic. */
2881 if (matching_location != UNKNOWN_LOCATION)
2882 added_matching_location
2883 = richloc.add_location_if_nearby (matching_location);
2884 }
2885
2886 /* Actually emit the error. */
2887 c_parse_error (gmsgid,
2888 /* Because c_parser_error does not understand
2889 CPP_KEYWORD, keywords are treated like
2890 identifiers. */
2891 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2892 token->u.value, token->flags, &richloc);
2893
2894 if (missing_token_desc != RT_NONE)
2895 {
2896 /* If we weren't able to consolidate matching_location, then
2897 print it as a secondary diagnostic. */
2898 if (matching_location != UNKNOWN_LOCATION
2899 && !added_matching_location)
2900 inform (matching_location, "to match this %qs",
2901 get_matching_symbol (missing_token_desc));
2902 }
2903 }
2904
2905 /* If not parsing tentatively, issue a diagnostic of the form
2906 FILE:LINE: MESSAGE before TOKEN
2907 where TOKEN is the next token in the input stream. MESSAGE
2908 (specified by the caller) is usually of the form "expected
2909 OTHER-TOKEN". */
2910
2911 static void
2912 cp_parser_error (cp_parser* parser, const char* gmsgid)
2913 {
2914 if (!cp_parser_simulate_error (parser))
2915 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2916 }
2917
2918 /* Issue an error about name-lookup failing. NAME is the
2919 IDENTIFIER_NODE DECL is the result of
2920 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2921 the thing that we hoped to find. */
2922
2923 static void
2924 cp_parser_name_lookup_error (cp_parser* parser,
2925 tree name,
2926 tree decl,
2927 name_lookup_error desired,
2928 location_t location)
2929 {
2930 /* If name lookup completely failed, tell the user that NAME was not
2931 declared. */
2932 if (decl == error_mark_node)
2933 {
2934 if (parser->scope && parser->scope != global_namespace)
2935 error_at (location, "%<%E::%E%> has not been declared",
2936 parser->scope, name);
2937 else if (parser->scope == global_namespace)
2938 error_at (location, "%<::%E%> has not been declared", name);
2939 else if (parser->object_scope
2940 && !CLASS_TYPE_P (parser->object_scope))
2941 error_at (location, "request for member %qE in non-class type %qT",
2942 name, parser->object_scope);
2943 else if (parser->object_scope)
2944 error_at (location, "%<%T::%E%> has not been declared",
2945 parser->object_scope, name);
2946 else
2947 error_at (location, "%qE has not been declared", name);
2948 }
2949 else if (parser->scope && parser->scope != global_namespace)
2950 {
2951 switch (desired)
2952 {
2953 case NLE_TYPE:
2954 error_at (location, "%<%E::%E%> is not a type",
2955 parser->scope, name);
2956 break;
2957 case NLE_CXX98:
2958 error_at (location, "%<%E::%E%> is not a class or namespace",
2959 parser->scope, name);
2960 break;
2961 case NLE_NOT_CXX98:
2962 error_at (location,
2963 "%<%E::%E%> is not a class, namespace, or enumeration",
2964 parser->scope, name);
2965 break;
2966 default:
2967 gcc_unreachable ();
2968
2969 }
2970 }
2971 else if (parser->scope == global_namespace)
2972 {
2973 switch (desired)
2974 {
2975 case NLE_TYPE:
2976 error_at (location, "%<::%E%> is not a type", name);
2977 break;
2978 case NLE_CXX98:
2979 error_at (location, "%<::%E%> is not a class or namespace", name);
2980 break;
2981 case NLE_NOT_CXX98:
2982 error_at (location,
2983 "%<::%E%> is not a class, namespace, or enumeration",
2984 name);
2985 break;
2986 default:
2987 gcc_unreachable ();
2988 }
2989 }
2990 else
2991 {
2992 switch (desired)
2993 {
2994 case NLE_TYPE:
2995 error_at (location, "%qE is not a type", name);
2996 break;
2997 case NLE_CXX98:
2998 error_at (location, "%qE is not a class or namespace", name);
2999 break;
3000 case NLE_NOT_CXX98:
3001 error_at (location,
3002 "%qE is not a class, namespace, or enumeration", name);
3003 break;
3004 default:
3005 gcc_unreachable ();
3006 }
3007 }
3008 }
3009
3010 /* If we are parsing tentatively, remember that an error has occurred
3011 during this tentative parse. Returns true if the error was
3012 simulated; false if a message should be issued by the caller. */
3013
3014 static bool
3015 cp_parser_simulate_error (cp_parser* parser)
3016 {
3017 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3018 {
3019 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3020 return true;
3021 }
3022 return false;
3023 }
3024
3025 /* This function is called when a type is defined. If type
3026 definitions are forbidden at this point, an error message is
3027 issued. */
3028
3029 static bool
3030 cp_parser_check_type_definition (cp_parser* parser)
3031 {
3032 /* If types are forbidden here, issue a message. */
3033 if (parser->type_definition_forbidden_message)
3034 {
3035 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3036 in the message need to be interpreted. */
3037 error (parser->type_definition_forbidden_message);
3038 return false;
3039 }
3040 return true;
3041 }
3042
3043 /* This function is called when the DECLARATOR is processed. The TYPE
3044 was a type defined in the decl-specifiers. If it is invalid to
3045 define a type in the decl-specifiers for DECLARATOR, an error is
3046 issued. TYPE_LOCATION is the location of TYPE and is used
3047 for error reporting. */
3048
3049 static void
3050 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3051 tree type, location_t type_location)
3052 {
3053 /* [dcl.fct] forbids type definitions in return types.
3054 Unfortunately, it's not easy to know whether or not we are
3055 processing a return type until after the fact. */
3056 while (declarator
3057 && (declarator->kind == cdk_pointer
3058 || declarator->kind == cdk_reference
3059 || declarator->kind == cdk_ptrmem))
3060 declarator = declarator->declarator;
3061 if (declarator
3062 && declarator->kind == cdk_function)
3063 {
3064 error_at (type_location,
3065 "new types may not be defined in a return type");
3066 inform (type_location,
3067 "(perhaps a semicolon is missing after the definition of %qT)",
3068 type);
3069 }
3070 }
3071
3072 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3073 "<" in any valid C++ program. If the next token is indeed "<",
3074 issue a message warning the user about what appears to be an
3075 invalid attempt to form a template-id. LOCATION is the location
3076 of the type-specifier (TYPE) */
3077
3078 static void
3079 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3080 tree type,
3081 enum tag_types tag_type,
3082 location_t location)
3083 {
3084 cp_token_position start = 0;
3085
3086 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3087 {
3088 if (TREE_CODE (type) == TYPE_DECL)
3089 type = TREE_TYPE (type);
3090 if (TYPE_P (type) && !template_placeholder_p (type))
3091 error_at (location, "%qT is not a template", type);
3092 else if (identifier_p (type))
3093 {
3094 if (tag_type != none_type)
3095 error_at (location, "%qE is not a class template", type);
3096 else
3097 error_at (location, "%qE is not a template", type);
3098 }
3099 else
3100 error_at (location, "invalid template-id");
3101 /* Remember the location of the invalid "<". */
3102 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3103 start = cp_lexer_token_position (parser->lexer, true);
3104 /* Consume the "<". */
3105 cp_lexer_consume_token (parser->lexer);
3106 /* Parse the template arguments. */
3107 cp_parser_enclosed_template_argument_list (parser);
3108 /* Permanently remove the invalid template arguments so that
3109 this error message is not issued again. */
3110 if (start)
3111 cp_lexer_purge_tokens_after (parser->lexer, start);
3112 }
3113 }
3114
3115 /* If parsing an integral constant-expression, issue an error message
3116 about the fact that THING appeared and return true. Otherwise,
3117 return false. In either case, set
3118 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3119
3120 static bool
3121 cp_parser_non_integral_constant_expression (cp_parser *parser,
3122 non_integral_constant thing)
3123 {
3124 parser->non_integral_constant_expression_p = true;
3125 if (parser->integral_constant_expression_p)
3126 {
3127 if (!parser->allow_non_integral_constant_expression_p)
3128 {
3129 const char *msg = NULL;
3130 switch (thing)
3131 {
3132 case NIC_FLOAT:
3133 pedwarn (input_location, OPT_Wpedantic,
3134 "ISO C++ forbids using a floating-point literal "
3135 "in a constant-expression");
3136 return true;
3137 case NIC_CAST:
3138 error ("a cast to a type other than an integral or "
3139 "enumeration type cannot appear in a "
3140 "constant-expression");
3141 return true;
3142 case NIC_TYPEID:
3143 error ("%<typeid%> operator "
3144 "cannot appear in a constant-expression");
3145 return true;
3146 case NIC_NCC:
3147 error ("non-constant compound literals "
3148 "cannot appear in a constant-expression");
3149 return true;
3150 case NIC_FUNC_CALL:
3151 error ("a function call "
3152 "cannot appear in a constant-expression");
3153 return true;
3154 case NIC_INC:
3155 error ("an increment "
3156 "cannot appear in a constant-expression");
3157 return true;
3158 case NIC_DEC:
3159 error ("an decrement "
3160 "cannot appear in a constant-expression");
3161 return true;
3162 case NIC_ARRAY_REF:
3163 error ("an array reference "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_ADDR_LABEL:
3167 error ("the address of a label "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_OVERLOADED:
3171 error ("calls to overloaded operators "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_ASSIGNMENT:
3175 error ("an assignment cannot appear in a constant-expression");
3176 return true;
3177 case NIC_COMMA:
3178 error ("a comma operator "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_CONSTRUCTOR:
3182 error ("a call to a constructor "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_TRANSACTION:
3186 error ("a transaction expression "
3187 "cannot appear in a constant-expression");
3188 return true;
3189 case NIC_THIS:
3190 msg = "this";
3191 break;
3192 case NIC_FUNC_NAME:
3193 msg = "__FUNCTION__";
3194 break;
3195 case NIC_PRETTY_FUNC:
3196 msg = "__PRETTY_FUNCTION__";
3197 break;
3198 case NIC_C99_FUNC:
3199 msg = "__func__";
3200 break;
3201 case NIC_VA_ARG:
3202 msg = "va_arg";
3203 break;
3204 case NIC_ARROW:
3205 msg = "->";
3206 break;
3207 case NIC_POINT:
3208 msg = ".";
3209 break;
3210 case NIC_STAR:
3211 msg = "*";
3212 break;
3213 case NIC_ADDR:
3214 msg = "&";
3215 break;
3216 case NIC_PREINCREMENT:
3217 msg = "++";
3218 break;
3219 case NIC_PREDECREMENT:
3220 msg = "--";
3221 break;
3222 case NIC_NEW:
3223 msg = "new";
3224 break;
3225 case NIC_DEL:
3226 msg = "delete";
3227 break;
3228 default:
3229 gcc_unreachable ();
3230 }
3231 if (msg)
3232 error ("%qs cannot appear in a constant-expression", msg);
3233 return true;
3234 }
3235 }
3236 return false;
3237 }
3238
3239 /* Emit a diagnostic for an invalid type name. This function commits
3240 to the current active tentative parse, if any. (Otherwise, the
3241 problematic construct might be encountered again later, resulting
3242 in duplicate error messages.) LOCATION is the location of ID. */
3243
3244 static void
3245 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3246 location_t location)
3247 {
3248 tree decl, ambiguous_decls;
3249 cp_parser_commit_to_tentative_parse (parser);
3250 /* Try to lookup the identifier. */
3251 decl = cp_parser_lookup_name (parser, id, none_type,
3252 /*is_template=*/false,
3253 /*is_namespace=*/false,
3254 /*check_dependency=*/true,
3255 &ambiguous_decls, location);
3256 if (ambiguous_decls)
3257 /* If the lookup was ambiguous, an error will already have
3258 been issued. */
3259 return;
3260 /* If the lookup found a template-name, it means that the user forgot
3261 to specify an argument list. Emit a useful error message. */
3262 if (DECL_TYPE_TEMPLATE_P (decl))
3263 {
3264 error_at (location,
3265 "invalid use of template-name %qE without an argument list",
3266 decl);
3267 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3268 inform (location, "class template argument deduction is only available "
3269 "with -std=c++17 or -std=gnu++17");
3270 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3271 }
3272 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3273 error_at (location, "invalid use of destructor %qD as a type", id);
3274 else if (TREE_CODE (decl) == TYPE_DECL)
3275 /* Something like 'unsigned A a;' */
3276 error_at (location, "invalid combination of multiple type-specifiers");
3277 else if (!parser->scope)
3278 {
3279 /* Issue an error message. */
3280 name_hint hint;
3281 if (TREE_CODE (id) == IDENTIFIER_NODE)
3282 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3283 if (hint)
3284 {
3285 gcc_rich_location richloc (location);
3286 richloc.add_fixit_replace (hint.suggestion ());
3287 error_at (&richloc,
3288 "%qE does not name a type; did you mean %qs?",
3289 id, hint.suggestion ());
3290 }
3291 else
3292 error_at (location, "%qE does not name a type", id);
3293 /* If we're in a template class, it's possible that the user was
3294 referring to a type from a base class. For example:
3295
3296 template <typename T> struct A { typedef T X; };
3297 template <typename T> struct B : public A<T> { X x; };
3298
3299 The user should have said "typename A<T>::X". */
3300 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3301 inform (location, "C++11 %<constexpr%> only available with "
3302 "-std=c++11 or -std=gnu++11");
3303 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3304 inform (location, "C++11 %<noexcept%> only available with "
3305 "-std=c++11 or -std=gnu++11");
3306 else if (cxx_dialect < cxx11
3307 && TREE_CODE (id) == IDENTIFIER_NODE
3308 && id_equal (id, "thread_local"))
3309 inform (location, "C++11 %<thread_local%> only available with "
3310 "-std=c++11 or -std=gnu++11");
3311 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3312 inform (location, "%<concept%> only available with -fconcepts");
3313 else if (processing_template_decl && current_class_type
3314 && TYPE_BINFO (current_class_type))
3315 {
3316 tree b;
3317
3318 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3319 b;
3320 b = TREE_CHAIN (b))
3321 {
3322 tree base_type = BINFO_TYPE (b);
3323 if (CLASS_TYPE_P (base_type)
3324 && dependent_type_p (base_type))
3325 {
3326 tree field;
3327 /* Go from a particular instantiation of the
3328 template (which will have an empty TYPE_FIELDs),
3329 to the main version. */
3330 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3331 for (field = TYPE_FIELDS (base_type);
3332 field;
3333 field = DECL_CHAIN (field))
3334 if (TREE_CODE (field) == TYPE_DECL
3335 && DECL_NAME (field) == id)
3336 {
3337 inform (location,
3338 "(perhaps %<typename %T::%E%> was intended)",
3339 BINFO_TYPE (b), id);
3340 break;
3341 }
3342 if (field)
3343 break;
3344 }
3345 }
3346 }
3347 }
3348 /* Here we diagnose qualified-ids where the scope is actually correct,
3349 but the identifier does not resolve to a valid type name. */
3350 else if (parser->scope != error_mark_node)
3351 {
3352 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3353 {
3354 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3355 error_at (location_of (id),
3356 "%qE in namespace %qE does not name a template type",
3357 id, parser->scope);
3358 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3359 error_at (location_of (id),
3360 "%qE in namespace %qE does not name a template type",
3361 TREE_OPERAND (id, 0), parser->scope);
3362 else
3363 error_at (location_of (id),
3364 "%qE in namespace %qE does not name a type",
3365 id, parser->scope);
3366 if (DECL_P (decl))
3367 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3368 else if (decl == error_mark_node)
3369 suggest_alternative_in_explicit_scope (location, id,
3370 parser->scope);
3371 }
3372 else if (CLASS_TYPE_P (parser->scope)
3373 && constructor_name_p (id, parser->scope))
3374 {
3375 /* A<T>::A<T>() */
3376 error_at (location, "%<%T::%E%> names the constructor, not"
3377 " the type", parser->scope, id);
3378 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3379 error_at (location, "and %qT has no template constructors",
3380 parser->scope);
3381 }
3382 else if (TYPE_P (parser->scope)
3383 && dependent_scope_p (parser->scope))
3384 {
3385 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3386 error_at (location,
3387 "need %<typename%> before %<%T::%D::%E%> because "
3388 "%<%T::%D%> is a dependent scope",
3389 TYPE_CONTEXT (parser->scope),
3390 TYPENAME_TYPE_FULLNAME (parser->scope),
3391 id,
3392 TYPE_CONTEXT (parser->scope),
3393 TYPENAME_TYPE_FULLNAME (parser->scope));
3394 else
3395 error_at (location, "need %<typename%> before %<%T::%E%> because "
3396 "%qT is a dependent scope",
3397 parser->scope, id, parser->scope);
3398 }
3399 else if (TYPE_P (parser->scope))
3400 {
3401 if (!COMPLETE_TYPE_P (parser->scope))
3402 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3403 parser->scope);
3404 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3405 error_at (location_of (id),
3406 "%qE in %q#T does not name a template type",
3407 id, parser->scope);
3408 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3409 error_at (location_of (id),
3410 "%qE in %q#T does not name a template type",
3411 TREE_OPERAND (id, 0), parser->scope);
3412 else
3413 error_at (location_of (id),
3414 "%qE in %q#T does not name a type",
3415 id, parser->scope);
3416 if (DECL_P (decl))
3417 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3418 }
3419 else
3420 gcc_unreachable ();
3421 }
3422 }
3423
3424 /* Check for a common situation where a type-name should be present,
3425 but is not, and issue a sensible error message. Returns true if an
3426 invalid type-name was detected.
3427
3428 The situation handled by this function are variable declarations of the
3429 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3430 Usually, `ID' should name a type, but if we got here it means that it
3431 does not. We try to emit the best possible error message depending on
3432 how exactly the id-expression looks like. */
3433
3434 static bool
3435 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3436 {
3437 tree id;
3438 cp_token *token = cp_lexer_peek_token (parser->lexer);
3439
3440 /* Avoid duplicate error about ambiguous lookup. */
3441 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3442 {
3443 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3444 if (next->type == CPP_NAME && next->error_reported)
3445 goto out;
3446 }
3447
3448 cp_parser_parse_tentatively (parser);
3449 id = cp_parser_id_expression (parser,
3450 /*template_keyword_p=*/false,
3451 /*check_dependency_p=*/true,
3452 /*template_p=*/NULL,
3453 /*declarator_p=*/true,
3454 /*optional_p=*/false);
3455 /* If the next token is a (, this is a function with no explicit return
3456 type, i.e. constructor, destructor or conversion op. */
3457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3458 || TREE_CODE (id) == TYPE_DECL)
3459 {
3460 cp_parser_abort_tentative_parse (parser);
3461 return false;
3462 }
3463 if (!cp_parser_parse_definitely (parser))
3464 return false;
3465
3466 /* Emit a diagnostic for the invalid type. */
3467 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3468 out:
3469 /* If we aren't in the middle of a declarator (i.e. in a
3470 parameter-declaration-clause), skip to the end of the declaration;
3471 there's no point in trying to process it. */
3472 if (!parser->in_declarator_p)
3473 cp_parser_skip_to_end_of_block_or_statement (parser);
3474 return true;
3475 }
3476
3477 /* Consume tokens up to, and including, the next non-nested closing `)'.
3478 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3479 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3480 found an unnested token of that type. */
3481
3482 static int
3483 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3484 bool recovering,
3485 cpp_ttype or_ttype,
3486 bool consume_paren)
3487 {
3488 unsigned paren_depth = 0;
3489 unsigned brace_depth = 0;
3490 unsigned square_depth = 0;
3491
3492 if (recovering && or_ttype == CPP_EOF
3493 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3494 return 0;
3495
3496 while (true)
3497 {
3498 cp_token * token = cp_lexer_peek_token (parser->lexer);
3499
3500 /* Have we found what we're looking for before the closing paren? */
3501 if (token->type == or_ttype && or_ttype != CPP_EOF
3502 && !brace_depth && !paren_depth && !square_depth)
3503 return -1;
3504
3505 switch (token->type)
3506 {
3507 case CPP_EOF:
3508 case CPP_PRAGMA_EOL:
3509 /* If we've run out of tokens, then there is no closing `)'. */
3510 return 0;
3511
3512 /* This is good for lambda expression capture-lists. */
3513 case CPP_OPEN_SQUARE:
3514 ++square_depth;
3515 break;
3516 case CPP_CLOSE_SQUARE:
3517 if (!square_depth--)
3518 return 0;
3519 break;
3520
3521 case CPP_SEMICOLON:
3522 /* This matches the processing in skip_to_end_of_statement. */
3523 if (!brace_depth)
3524 return 0;
3525 break;
3526
3527 case CPP_OPEN_BRACE:
3528 ++brace_depth;
3529 break;
3530 case CPP_CLOSE_BRACE:
3531 if (!brace_depth--)
3532 return 0;
3533 break;
3534
3535 case CPP_OPEN_PAREN:
3536 if (!brace_depth)
3537 ++paren_depth;
3538 break;
3539
3540 case CPP_CLOSE_PAREN:
3541 if (!brace_depth && !paren_depth--)
3542 {
3543 if (consume_paren)
3544 cp_lexer_consume_token (parser->lexer);
3545 return 1;
3546 }
3547 break;
3548
3549 default:
3550 break;
3551 }
3552
3553 /* Consume the token. */
3554 cp_lexer_consume_token (parser->lexer);
3555 }
3556 }
3557
3558 /* Consume tokens up to, and including, the next non-nested closing `)'.
3559 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3560 are doing error recovery. Returns -1 if OR_COMMA is true and we
3561 found an unnested token of that type. */
3562
3563 static int
3564 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3565 bool recovering,
3566 bool or_comma,
3567 bool consume_paren)
3568 {
3569 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3570 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3571 ttype, consume_paren);
3572 }
3573
3574 /* Consume tokens until we reach the end of the current statement.
3575 Normally, that will be just before consuming a `;'. However, if a
3576 non-nested `}' comes first, then we stop before consuming that. */
3577
3578 static void
3579 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3580 {
3581 unsigned nesting_depth = 0;
3582
3583 /* Unwind generic function template scope if necessary. */
3584 if (parser->fully_implicit_function_template_p)
3585 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3586
3587 while (true)
3588 {
3589 cp_token *token = cp_lexer_peek_token (parser->lexer);
3590
3591 switch (token->type)
3592 {
3593 case CPP_EOF:
3594 case CPP_PRAGMA_EOL:
3595 /* If we've run out of tokens, stop. */
3596 return;
3597
3598 case CPP_SEMICOLON:
3599 /* If the next token is a `;', we have reached the end of the
3600 statement. */
3601 if (!nesting_depth)
3602 return;
3603 break;
3604
3605 case CPP_CLOSE_BRACE:
3606 /* If this is a non-nested '}', stop before consuming it.
3607 That way, when confronted with something like:
3608
3609 { 3 + }
3610
3611 we stop before consuming the closing '}', even though we
3612 have not yet reached a `;'. */
3613 if (nesting_depth == 0)
3614 return;
3615
3616 /* If it is the closing '}' for a block that we have
3617 scanned, stop -- but only after consuming the token.
3618 That way given:
3619
3620 void f g () { ... }
3621 typedef int I;
3622
3623 we will stop after the body of the erroneously declared
3624 function, but before consuming the following `typedef'
3625 declaration. */
3626 if (--nesting_depth == 0)
3627 {
3628 cp_lexer_consume_token (parser->lexer);
3629 return;
3630 }
3631 break;
3632
3633 case CPP_OPEN_BRACE:
3634 ++nesting_depth;
3635 break;
3636
3637 default:
3638 break;
3639 }
3640
3641 /* Consume the token. */
3642 cp_lexer_consume_token (parser->lexer);
3643 }
3644 }
3645
3646 /* This function is called at the end of a statement or declaration.
3647 If the next token is a semicolon, it is consumed; otherwise, error
3648 recovery is attempted. */
3649
3650 static void
3651 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3652 {
3653 /* Look for the trailing `;'. */
3654 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3655 {
3656 /* If there is additional (erroneous) input, skip to the end of
3657 the statement. */
3658 cp_parser_skip_to_end_of_statement (parser);
3659 /* If the next token is now a `;', consume it. */
3660 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3661 cp_lexer_consume_token (parser->lexer);
3662 }
3663 }
3664
3665 /* Skip tokens until we have consumed an entire block, or until we
3666 have consumed a non-nested `;'. */
3667
3668 static void
3669 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3670 {
3671 int nesting_depth = 0;
3672
3673 /* Unwind generic function template scope if necessary. */
3674 if (parser->fully_implicit_function_template_p)
3675 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3676
3677 while (nesting_depth >= 0)
3678 {
3679 cp_token *token = cp_lexer_peek_token (parser->lexer);
3680
3681 switch (token->type)
3682 {
3683 case CPP_EOF:
3684 case CPP_PRAGMA_EOL:
3685 /* If we've run out of tokens, stop. */
3686 return;
3687
3688 case CPP_SEMICOLON:
3689 /* Stop if this is an unnested ';'. */
3690 if (!nesting_depth)
3691 nesting_depth = -1;
3692 break;
3693
3694 case CPP_CLOSE_BRACE:
3695 /* Stop if this is an unnested '}', or closes the outermost
3696 nesting level. */
3697 nesting_depth--;
3698 if (nesting_depth < 0)
3699 return;
3700 if (!nesting_depth)
3701 nesting_depth = -1;
3702 break;
3703
3704 case CPP_OPEN_BRACE:
3705 /* Nest. */
3706 nesting_depth++;
3707 break;
3708
3709 default:
3710 break;
3711 }
3712
3713 /* Consume the token. */
3714 cp_lexer_consume_token (parser->lexer);
3715 }
3716 }
3717
3718 /* Skip tokens until a non-nested closing curly brace is the next
3719 token, or there are no more tokens. Return true in the first case,
3720 false otherwise. */
3721
3722 static bool
3723 cp_parser_skip_to_closing_brace (cp_parser *parser)
3724 {
3725 unsigned nesting_depth = 0;
3726
3727 while (true)
3728 {
3729 cp_token *token = cp_lexer_peek_token (parser->lexer);
3730
3731 switch (token->type)
3732 {
3733 case CPP_EOF:
3734 case CPP_PRAGMA_EOL:
3735 /* If we've run out of tokens, stop. */
3736 return false;
3737
3738 case CPP_CLOSE_BRACE:
3739 /* If the next token is a non-nested `}', then we have reached
3740 the end of the current block. */
3741 if (nesting_depth-- == 0)
3742 return true;
3743 break;
3744
3745 case CPP_OPEN_BRACE:
3746 /* If it the next token is a `{', then we are entering a new
3747 block. Consume the entire block. */
3748 ++nesting_depth;
3749 break;
3750
3751 default:
3752 break;
3753 }
3754
3755 /* Consume the token. */
3756 cp_lexer_consume_token (parser->lexer);
3757 }
3758 }
3759
3760 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3761 parameter is the PRAGMA token, allowing us to purge the entire pragma
3762 sequence. */
3763
3764 static void
3765 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3766 {
3767 cp_token *token;
3768
3769 parser->lexer->in_pragma = false;
3770
3771 do
3772 token = cp_lexer_consume_token (parser->lexer);
3773 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3774
3775 /* Ensure that the pragma is not parsed again. */
3776 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3777 }
3778
3779 /* Require pragma end of line, resyncing with it as necessary. The
3780 arguments are as for cp_parser_skip_to_pragma_eol. */
3781
3782 static void
3783 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3784 {
3785 parser->lexer->in_pragma = false;
3786 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3787 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3788 }
3789
3790 /* This is a simple wrapper around make_typename_type. When the id is
3791 an unresolved identifier node, we can provide a superior diagnostic
3792 using cp_parser_diagnose_invalid_type_name. */
3793
3794 static tree
3795 cp_parser_make_typename_type (cp_parser *parser, tree id,
3796 location_t id_location)
3797 {
3798 tree result;
3799 if (identifier_p (id))
3800 {
3801 result = make_typename_type (parser->scope, id, typename_type,
3802 /*complain=*/tf_none);
3803 if (result == error_mark_node)
3804 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3805 return result;
3806 }
3807 return make_typename_type (parser->scope, id, typename_type, tf_error);
3808 }
3809
3810 /* This is a wrapper around the
3811 make_{pointer,ptrmem,reference}_declarator functions that decides
3812 which one to call based on the CODE and CLASS_TYPE arguments. The
3813 CODE argument should be one of the values returned by
3814 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3815 appertain to the pointer or reference. */
3816
3817 static cp_declarator *
3818 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3819 cp_cv_quals cv_qualifiers,
3820 cp_declarator *target,
3821 tree attributes)
3822 {
3823 if (code == ERROR_MARK)
3824 return cp_error_declarator;
3825
3826 if (code == INDIRECT_REF)
3827 if (class_type == NULL_TREE)
3828 return make_pointer_declarator (cv_qualifiers, target, attributes);
3829 else
3830 return make_ptrmem_declarator (cv_qualifiers, class_type,
3831 target, attributes);
3832 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3833 return make_reference_declarator (cv_qualifiers, target,
3834 false, attributes);
3835 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3836 return make_reference_declarator (cv_qualifiers, target,
3837 true, attributes);
3838 gcc_unreachable ();
3839 }
3840
3841 /* Create a new C++ parser. */
3842
3843 static cp_parser *
3844 cp_parser_new (void)
3845 {
3846 cp_parser *parser;
3847 cp_lexer *lexer;
3848 unsigned i;
3849
3850 /* cp_lexer_new_main is called before doing GC allocation because
3851 cp_lexer_new_main might load a PCH file. */
3852 lexer = cp_lexer_new_main ();
3853
3854 /* Initialize the binops_by_token so that we can get the tree
3855 directly from the token. */
3856 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3857 binops_by_token[binops[i].token_type] = binops[i];
3858
3859 parser = ggc_cleared_alloc<cp_parser> ();
3860 parser->lexer = lexer;
3861 parser->context = cp_parser_context_new (NULL);
3862
3863 /* For now, we always accept GNU extensions. */
3864 parser->allow_gnu_extensions_p = 1;
3865
3866 /* The `>' token is a greater-than operator, not the end of a
3867 template-id. */
3868 parser->greater_than_is_operator_p = true;
3869
3870 parser->default_arg_ok_p = true;
3871
3872 /* We are not parsing a constant-expression. */
3873 parser->integral_constant_expression_p = false;
3874 parser->allow_non_integral_constant_expression_p = false;
3875 parser->non_integral_constant_expression_p = false;
3876
3877 /* Local variable names are not forbidden. */
3878 parser->local_variables_forbidden_p = false;
3879
3880 /* We are not processing an `extern "C"' declaration. */
3881 parser->in_unbraced_linkage_specification_p = false;
3882
3883 /* We are not processing a declarator. */
3884 parser->in_declarator_p = false;
3885
3886 /* We are not processing a template-argument-list. */
3887 parser->in_template_argument_list_p = false;
3888
3889 /* We are not in an iteration statement. */
3890 parser->in_statement = 0;
3891
3892 /* We are not in a switch statement. */
3893 parser->in_switch_statement_p = false;
3894
3895 /* We are not parsing a type-id inside an expression. */
3896 parser->in_type_id_in_expr_p = false;
3897
3898 /* Declarations aren't implicitly extern "C". */
3899 parser->implicit_extern_c = false;
3900
3901 /* String literals should be translated to the execution character set. */
3902 parser->translate_strings_p = true;
3903
3904 /* We are not parsing a function body. */
3905 parser->in_function_body = false;
3906
3907 /* We can correct until told otherwise. */
3908 parser->colon_corrects_to_scope_p = true;
3909
3910 /* The unparsed function queue is empty. */
3911 push_unparsed_function_queues (parser);
3912
3913 /* There are no classes being defined. */
3914 parser->num_classes_being_defined = 0;
3915
3916 /* No template parameters apply. */
3917 parser->num_template_parameter_lists = 0;
3918
3919 /* Special parsing data structures. */
3920 parser->omp_declare_simd = NULL;
3921 parser->oacc_routine = NULL;
3922
3923 /* Not declaring an implicit function template. */
3924 parser->auto_is_implicit_function_template_parm_p = false;
3925 parser->fully_implicit_function_template_p = false;
3926 parser->implicit_template_parms = 0;
3927 parser->implicit_template_scope = 0;
3928
3929 /* Allow constrained-type-specifiers. */
3930 parser->prevent_constrained_type_specifiers = 0;
3931
3932 /* We haven't yet seen an 'extern "C"'. */
3933 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3934
3935 return parser;
3936 }
3937
3938 /* Create a cp_lexer structure which will emit the tokens in CACHE
3939 and push it onto the parser's lexer stack. This is used for delayed
3940 parsing of in-class method bodies and default arguments, and should
3941 not be confused with tentative parsing. */
3942 static void
3943 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3944 {
3945 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3946 lexer->next = parser->lexer;
3947 parser->lexer = lexer;
3948
3949 /* Move the current source position to that of the first token in the
3950 new lexer. */
3951 cp_lexer_set_source_position_from_token (lexer->next_token);
3952 }
3953
3954 /* Pop the top lexer off the parser stack. This is never used for the
3955 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3956 static void
3957 cp_parser_pop_lexer (cp_parser *parser)
3958 {
3959 cp_lexer *lexer = parser->lexer;
3960 parser->lexer = lexer->next;
3961 cp_lexer_destroy (lexer);
3962
3963 /* Put the current source position back where it was before this
3964 lexer was pushed. */
3965 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3966 }
3967
3968 /* Lexical conventions [gram.lex] */
3969
3970 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3971 identifier. */
3972
3973 static cp_expr
3974 cp_parser_identifier (cp_parser* parser)
3975 {
3976 cp_token *token;
3977
3978 /* Look for the identifier. */
3979 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3980 /* Return the value. */
3981 if (token)
3982 return cp_expr (token->u.value, token->location);
3983 else
3984 return error_mark_node;
3985 }
3986
3987 /* Parse a sequence of adjacent string constants. Returns a
3988 TREE_STRING representing the combined, nul-terminated string
3989 constant. If TRANSLATE is true, translate the string to the
3990 execution character set. If WIDE_OK is true, a wide string is
3991 invalid here.
3992
3993 C++98 [lex.string] says that if a narrow string literal token is
3994 adjacent to a wide string literal token, the behavior is undefined.
3995 However, C99 6.4.5p4 says that this results in a wide string literal.
3996 We follow C99 here, for consistency with the C front end.
3997
3998 This code is largely lifted from lex_string() in c-lex.c.
3999
4000 FUTURE: ObjC++ will need to handle @-strings here. */
4001 static cp_expr
4002 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4003 bool lookup_udlit = true)
4004 {
4005 tree value;
4006 size_t count;
4007 struct obstack str_ob;
4008 cpp_string str, istr, *strs;
4009 cp_token *tok;
4010 enum cpp_ttype type, curr_type;
4011 int have_suffix_p = 0;
4012 tree string_tree;
4013 tree suffix_id = NULL_TREE;
4014 bool curr_tok_is_userdef_p = false;
4015
4016 tok = cp_lexer_peek_token (parser->lexer);
4017 if (!cp_parser_is_string_literal (tok))
4018 {
4019 cp_parser_error (parser, "expected string-literal");
4020 return error_mark_node;
4021 }
4022
4023 location_t loc = tok->location;
4024
4025 if (cpp_userdef_string_p (tok->type))
4026 {
4027 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4028 curr_type = cpp_userdef_string_remove_type (tok->type);
4029 curr_tok_is_userdef_p = true;
4030 }
4031 else
4032 {
4033 string_tree = tok->u.value;
4034 curr_type = tok->type;
4035 }
4036 type = curr_type;
4037
4038 /* Try to avoid the overhead of creating and destroying an obstack
4039 for the common case of just one string. */
4040 if (!cp_parser_is_string_literal
4041 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4042 {
4043 cp_lexer_consume_token (parser->lexer);
4044
4045 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4046 str.len = TREE_STRING_LENGTH (string_tree);
4047 count = 1;
4048
4049 if (curr_tok_is_userdef_p)
4050 {
4051 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4052 have_suffix_p = 1;
4053 curr_type = cpp_userdef_string_remove_type (tok->type);
4054 }
4055 else
4056 curr_type = tok->type;
4057
4058 strs = &str;
4059 }
4060 else
4061 {
4062 location_t last_tok_loc = tok->location;
4063 gcc_obstack_init (&str_ob);
4064 count = 0;
4065
4066 do
4067 {
4068 cp_lexer_consume_token (parser->lexer);
4069 count++;
4070 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4071 str.len = TREE_STRING_LENGTH (string_tree);
4072
4073 if (curr_tok_is_userdef_p)
4074 {
4075 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4076 if (have_suffix_p == 0)
4077 {
4078 suffix_id = curr_suffix_id;
4079 have_suffix_p = 1;
4080 }
4081 else if (have_suffix_p == 1
4082 && curr_suffix_id != suffix_id)
4083 {
4084 error ("inconsistent user-defined literal suffixes"
4085 " %qD and %qD in string literal",
4086 suffix_id, curr_suffix_id);
4087 have_suffix_p = -1;
4088 }
4089 curr_type = cpp_userdef_string_remove_type (tok->type);
4090 }
4091 else
4092 curr_type = tok->type;
4093
4094 if (type != curr_type)
4095 {
4096 if (type == CPP_STRING)
4097 type = curr_type;
4098 else if (curr_type != CPP_STRING)
4099 {
4100 rich_location rich_loc (line_table, tok->location);
4101 rich_loc.add_range (last_tok_loc, false);
4102 error_at (&rich_loc,
4103 "unsupported non-standard concatenation "
4104 "of string literals");
4105 }
4106 }
4107
4108 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4109
4110 last_tok_loc = tok->location;
4111
4112 tok = cp_lexer_peek_token (parser->lexer);
4113 if (cpp_userdef_string_p (tok->type))
4114 {
4115 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4116 curr_type = cpp_userdef_string_remove_type (tok->type);
4117 curr_tok_is_userdef_p = true;
4118 }
4119 else
4120 {
4121 string_tree = tok->u.value;
4122 curr_type = tok->type;
4123 curr_tok_is_userdef_p = false;
4124 }
4125 }
4126 while (cp_parser_is_string_literal (tok));
4127
4128 /* A string literal built by concatenation has its caret=start at
4129 the start of the initial string, and its finish at the finish of
4130 the final string literal. */
4131 loc = make_location (loc, loc, get_finish (last_tok_loc));
4132
4133 strs = (cpp_string *) obstack_finish (&str_ob);
4134 }
4135
4136 if (type != CPP_STRING && !wide_ok)
4137 {
4138 cp_parser_error (parser, "a wide string is invalid in this context");
4139 type = CPP_STRING;
4140 }
4141
4142 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4143 (parse_in, strs, count, &istr, type))
4144 {
4145 value = build_string (istr.len, (const char *)istr.text);
4146 free (CONST_CAST (unsigned char *, istr.text));
4147
4148 switch (type)
4149 {
4150 default:
4151 case CPP_STRING:
4152 case CPP_UTF8STRING:
4153 TREE_TYPE (value) = char_array_type_node;
4154 break;
4155 case CPP_STRING16:
4156 TREE_TYPE (value) = char16_array_type_node;
4157 break;
4158 case CPP_STRING32:
4159 TREE_TYPE (value) = char32_array_type_node;
4160 break;
4161 case CPP_WSTRING:
4162 TREE_TYPE (value) = wchar_array_type_node;
4163 break;
4164 }
4165
4166 value = fix_string_type (value);
4167
4168 if (have_suffix_p)
4169 {
4170 tree literal = build_userdef_literal (suffix_id, value,
4171 OT_NONE, NULL_TREE);
4172 if (lookup_udlit)
4173 value = cp_parser_userdef_string_literal (literal);
4174 else
4175 value = literal;
4176 }
4177 }
4178 else
4179 /* cpp_interpret_string has issued an error. */
4180 value = error_mark_node;
4181
4182 if (count > 1)
4183 obstack_free (&str_ob, 0);
4184
4185 return cp_expr (value, loc);
4186 }
4187
4188 /* Look up a literal operator with the name and the exact arguments. */
4189
4190 static tree
4191 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4192 {
4193 tree decl;
4194 decl = lookup_name (name);
4195 if (!decl || !is_overloaded_fn (decl))
4196 return error_mark_node;
4197
4198 for (lkp_iterator iter (decl); iter; ++iter)
4199 {
4200 unsigned int ix;
4201 bool found = true;
4202 tree fn = *iter;
4203 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4204 if (parmtypes != NULL_TREE)
4205 {
4206 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4207 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4208 {
4209 tree tparm = TREE_VALUE (parmtypes);
4210 tree targ = TREE_TYPE ((*args)[ix]);
4211 bool ptr = TYPE_PTR_P (tparm);
4212 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4213 if ((ptr || arr || !same_type_p (tparm, targ))
4214 && (!ptr || !arr
4215 || !same_type_p (TREE_TYPE (tparm),
4216 TREE_TYPE (targ))))
4217 found = false;
4218 }
4219 if (found
4220 && ix == vec_safe_length (args)
4221 /* May be this should be sufficient_parms_p instead,
4222 depending on how exactly should user-defined literals
4223 work in presence of default arguments on the literal
4224 operator parameters. */
4225 && parmtypes == void_list_node)
4226 return decl;
4227 }
4228 }
4229
4230 return error_mark_node;
4231 }
4232
4233 /* Parse a user-defined char constant. Returns a call to a user-defined
4234 literal operator taking the character as an argument. */
4235
4236 static cp_expr
4237 cp_parser_userdef_char_literal (cp_parser *parser)
4238 {
4239 cp_token *token = cp_lexer_consume_token (parser->lexer);
4240 tree literal = token->u.value;
4241 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4242 tree value = USERDEF_LITERAL_VALUE (literal);
4243 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4244 tree decl, result;
4245
4246 /* Build up a call to the user-defined operator */
4247 /* Lookup the name we got back from the id-expression. */
4248 vec<tree, va_gc> *args = make_tree_vector ();
4249 vec_safe_push (args, value);
4250 decl = lookup_literal_operator (name, args);
4251 if (!decl || decl == error_mark_node)
4252 {
4253 error ("unable to find character literal operator %qD with %qT argument",
4254 name, TREE_TYPE (value));
4255 release_tree_vector (args);
4256 return error_mark_node;
4257 }
4258 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4259 release_tree_vector (args);
4260 return result;
4261 }
4262
4263 /* A subroutine of cp_parser_userdef_numeric_literal to
4264 create a char... template parameter pack from a string node. */
4265
4266 static tree
4267 make_char_string_pack (tree value)
4268 {
4269 tree charvec;
4270 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4271 const char *str = TREE_STRING_POINTER (value);
4272 int i, len = TREE_STRING_LENGTH (value) - 1;
4273 tree argvec = make_tree_vec (1);
4274
4275 /* Fill in CHARVEC with all of the parameters. */
4276 charvec = make_tree_vec (len);
4277 for (i = 0; i < len; ++i)
4278 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4279
4280 /* Build the argument packs. */
4281 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4282
4283 TREE_VEC_ELT (argvec, 0) = argpack;
4284
4285 return argvec;
4286 }
4287
4288 /* A subroutine of cp_parser_userdef_numeric_literal to
4289 create a char... template parameter pack from a string node. */
4290
4291 static tree
4292 make_string_pack (tree value)
4293 {
4294 tree charvec;
4295 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4296 const unsigned char *str
4297 = (const unsigned char *) TREE_STRING_POINTER (value);
4298 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4299 int len = TREE_STRING_LENGTH (value) / sz - 1;
4300 tree argvec = make_tree_vec (2);
4301
4302 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4303 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4304
4305 /* First template parm is character type. */
4306 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4307
4308 /* Fill in CHARVEC with all of the parameters. */
4309 charvec = make_tree_vec (len);
4310 for (int i = 0; i < len; ++i)
4311 TREE_VEC_ELT (charvec, i)
4312 = double_int_to_tree (str_char_type_node,
4313 double_int::from_buffer (str + i * sz, sz));
4314
4315 /* Build the argument packs. */
4316 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4317
4318 TREE_VEC_ELT (argvec, 1) = argpack;
4319
4320 return argvec;
4321 }
4322
4323 /* Parse a user-defined numeric constant. returns a call to a user-defined
4324 literal operator. */
4325
4326 static cp_expr
4327 cp_parser_userdef_numeric_literal (cp_parser *parser)
4328 {
4329 cp_token *token = cp_lexer_consume_token (parser->lexer);
4330 tree literal = token->u.value;
4331 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4332 tree value = USERDEF_LITERAL_VALUE (literal);
4333 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4334 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4335 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4336 tree decl, result;
4337 vec<tree, va_gc> *args;
4338
4339 /* Look for a literal operator taking the exact type of numeric argument
4340 as the literal value. */
4341 args = make_tree_vector ();
4342 vec_safe_push (args, value);
4343 decl = lookup_literal_operator (name, args);
4344 if (decl && decl != error_mark_node)
4345 {
4346 result = finish_call_expr (decl, &args, false, true,
4347 tf_warning_or_error);
4348
4349 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4350 {
4351 warning_at (token->location, OPT_Woverflow,
4352 "integer literal exceeds range of %qT type",
4353 long_long_unsigned_type_node);
4354 }
4355 else
4356 {
4357 if (overflow > 0)
4358 warning_at (token->location, OPT_Woverflow,
4359 "floating literal exceeds range of %qT type",
4360 long_double_type_node);
4361 else if (overflow < 0)
4362 warning_at (token->location, OPT_Woverflow,
4363 "floating literal truncated to zero");
4364 }
4365
4366 release_tree_vector (args);
4367 return result;
4368 }
4369 release_tree_vector (args);
4370
4371 /* If the numeric argument didn't work, look for a raw literal
4372 operator taking a const char* argument consisting of the number
4373 in string format. */
4374 args = make_tree_vector ();
4375 vec_safe_push (args, num_string);
4376 decl = lookup_literal_operator (name, args);
4377 if (decl && decl != error_mark_node)
4378 {
4379 result = finish_call_expr (decl, &args, false, true,
4380 tf_warning_or_error);
4381 release_tree_vector (args);
4382 return result;
4383 }
4384 release_tree_vector (args);
4385
4386 /* If the raw literal didn't work, look for a non-type template
4387 function with parameter pack char.... Call the function with
4388 template parameter characters representing the number. */
4389 args = make_tree_vector ();
4390 decl = lookup_literal_operator (name, args);
4391 if (decl && decl != error_mark_node)
4392 {
4393 tree tmpl_args = make_char_string_pack (num_string);
4394 decl = lookup_template_function (decl, tmpl_args);
4395 result = finish_call_expr (decl, &args, false, true,
4396 tf_warning_or_error);
4397 release_tree_vector (args);
4398 return result;
4399 }
4400
4401 release_tree_vector (args);
4402
4403 /* In C++14 the standard library defines complex number suffixes that
4404 conflict with GNU extensions. Prefer them if <complex> is #included. */
4405 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4406 bool i14 = (cxx_dialect > cxx11
4407 && (id_equal (suffix_id, "i")
4408 || id_equal (suffix_id, "if")
4409 || id_equal (suffix_id, "il")));
4410 diagnostic_t kind = DK_ERROR;
4411 int opt = 0;
4412
4413 if (i14 && ext)
4414 {
4415 tree cxlit = lookup_qualified_name (std_node,
4416 get_identifier ("complex_literals"),
4417 0, false, false);
4418 if (cxlit == error_mark_node)
4419 {
4420 /* No <complex>, so pedwarn and use GNU semantics. */
4421 kind = DK_PEDWARN;
4422 opt = OPT_Wpedantic;
4423 }
4424 }
4425
4426 bool complained
4427 = emit_diagnostic (kind, input_location, opt,
4428 "unable to find numeric literal operator %qD", name);
4429
4430 if (!complained)
4431 /* Don't inform either. */;
4432 else if (i14)
4433 {
4434 inform (token->location, "add %<using namespace std::complex_literals%> "
4435 "(from <complex>) to enable the C++14 user-defined literal "
4436 "suffixes");
4437 if (ext)
4438 inform (token->location, "or use %<j%> instead of %<i%> for the "
4439 "GNU built-in suffix");
4440 }
4441 else if (!ext)
4442 inform (token->location, "use -fext-numeric-literals "
4443 "to enable more built-in suffixes");
4444
4445 if (kind == DK_ERROR)
4446 value = error_mark_node;
4447 else
4448 {
4449 /* Use the built-in semantics. */
4450 tree type;
4451 if (id_equal (suffix_id, "i"))
4452 {
4453 if (TREE_CODE (value) == INTEGER_CST)
4454 type = integer_type_node;
4455 else
4456 type = double_type_node;
4457 }
4458 else if (id_equal (suffix_id, "if"))
4459 type = float_type_node;
4460 else /* if (id_equal (suffix_id, "il")) */
4461 type = long_double_type_node;
4462
4463 value = build_complex (build_complex_type (type),
4464 fold_convert (type, integer_zero_node),
4465 fold_convert (type, value));
4466 }
4467
4468 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4469 /* Avoid repeated diagnostics. */
4470 token->u.value = value;
4471 return value;
4472 }
4473
4474 /* Parse a user-defined string constant. Returns a call to a user-defined
4475 literal operator taking a character pointer and the length of the string
4476 as arguments. */
4477
4478 static tree
4479 cp_parser_userdef_string_literal (tree literal)
4480 {
4481 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4482 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4483 tree value = USERDEF_LITERAL_VALUE (literal);
4484 int len = TREE_STRING_LENGTH (value)
4485 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4486 tree decl, result;
4487 vec<tree, va_gc> *args;
4488
4489 /* Build up a call to the user-defined operator. */
4490 /* Lookup the name we got back from the id-expression. */
4491 args = make_tree_vector ();
4492 vec_safe_push (args, value);
4493 vec_safe_push (args, build_int_cst (size_type_node, len));
4494 decl = lookup_literal_operator (name, args);
4495
4496 if (decl && decl != error_mark_node)
4497 {
4498 result = finish_call_expr (decl, &args, false, true,
4499 tf_warning_or_error);
4500 release_tree_vector (args);
4501 return result;
4502 }
4503 release_tree_vector (args);
4504
4505 /* Look for a template function with typename parameter CharT
4506 and parameter pack CharT... Call the function with
4507 template parameter characters representing the string. */
4508 args = make_tree_vector ();
4509 decl = lookup_literal_operator (name, args);
4510 if (decl && decl != error_mark_node)
4511 {
4512 tree tmpl_args = make_string_pack (value);
4513 decl = lookup_template_function (decl, tmpl_args);
4514 result = finish_call_expr (decl, &args, false, true,
4515 tf_warning_or_error);
4516 release_tree_vector (args);
4517 return result;
4518 }
4519 release_tree_vector (args);
4520
4521 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4522 name, TREE_TYPE (value), size_type_node);
4523 return error_mark_node;
4524 }
4525
4526
4527 /* Basic concepts [gram.basic] */
4528
4529 /* Parse a translation-unit.
4530
4531 translation-unit:
4532 declaration-seq [opt]
4533
4534 Returns TRUE if all went well. */
4535
4536 static bool
4537 cp_parser_translation_unit (cp_parser* parser)
4538 {
4539 /* The address of the first non-permanent object on the declarator
4540 obstack. */
4541 static void *declarator_obstack_base;
4542
4543 bool success;
4544
4545 /* Create the declarator obstack, if necessary. */
4546 if (!cp_error_declarator)
4547 {
4548 gcc_obstack_init (&declarator_obstack);
4549 /* Create the error declarator. */
4550 cp_error_declarator = make_declarator (cdk_error);
4551 /* Create the empty parameter list. */
4552 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4553 UNKNOWN_LOCATION);
4554 /* Remember where the base of the declarator obstack lies. */
4555 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4556 }
4557
4558 cp_parser_declaration_seq_opt (parser);
4559
4560 /* If there are no tokens left then all went well. */
4561 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4562 {
4563 /* Get rid of the token array; we don't need it any more. */
4564 cp_lexer_destroy (parser->lexer);
4565 parser->lexer = NULL;
4566
4567 /* This file might have been a context that's implicitly extern
4568 "C". If so, pop the lang context. (Only relevant for PCH.) */
4569 if (parser->implicit_extern_c)
4570 {
4571 pop_lang_context ();
4572 parser->implicit_extern_c = false;
4573 }
4574
4575 /* Finish up. */
4576 finish_translation_unit ();
4577
4578 success = true;
4579 }
4580 else
4581 {
4582 cp_parser_error (parser, "expected declaration");
4583 success = false;
4584 }
4585
4586 /* Make sure the declarator obstack was fully cleaned up. */
4587 gcc_assert (obstack_next_free (&declarator_obstack)
4588 == declarator_obstack_base);
4589
4590 /* All went well. */
4591 return success;
4592 }
4593
4594 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4595 decltype context. */
4596
4597 static inline tsubst_flags_t
4598 complain_flags (bool decltype_p)
4599 {
4600 tsubst_flags_t complain = tf_warning_or_error;
4601 if (decltype_p)
4602 complain |= tf_decltype;
4603 return complain;
4604 }
4605
4606 /* We're about to parse a collection of statements. If we're currently
4607 parsing tentatively, set up a firewall so that any nested
4608 cp_parser_commit_to_tentative_parse won't affect the current context. */
4609
4610 static cp_token_position
4611 cp_parser_start_tentative_firewall (cp_parser *parser)
4612 {
4613 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4614 return 0;
4615
4616 cp_parser_parse_tentatively (parser);
4617 cp_parser_commit_to_topmost_tentative_parse (parser);
4618 return cp_lexer_token_position (parser->lexer, false);
4619 }
4620
4621 /* We've finished parsing the collection of statements. Wrap up the
4622 firewall and replace the relevant tokens with the parsed form. */
4623
4624 static void
4625 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4626 tree expr)
4627 {
4628 if (!start)
4629 return;
4630
4631 /* Finish the firewall level. */
4632 cp_parser_parse_definitely (parser);
4633 /* And remember the result of the parse for when we try again. */
4634 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4635 token->type = CPP_PREPARSED_EXPR;
4636 token->u.value = expr;
4637 token->keyword = RID_MAX;
4638 cp_lexer_purge_tokens_after (parser->lexer, start);
4639 }
4640
4641 /* Like the above functions, but let the user modify the tokens. Used by
4642 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4643 later parses, so it makes sense to localize the effects of
4644 cp_parser_commit_to_tentative_parse. */
4645
4646 struct tentative_firewall
4647 {
4648 cp_parser *parser;
4649 bool set;
4650
4651 tentative_firewall (cp_parser *p): parser(p)
4652 {
4653 /* If we're currently parsing tentatively, start a committed level as a
4654 firewall and then an inner tentative parse. */
4655 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4656 {
4657 cp_parser_parse_tentatively (parser);
4658 cp_parser_commit_to_topmost_tentative_parse (parser);
4659 cp_parser_parse_tentatively (parser);
4660 }
4661 }
4662
4663 ~tentative_firewall()
4664 {
4665 if (set)
4666 {
4667 /* Finish the inner tentative parse and the firewall, propagating any
4668 uncommitted error state to the outer tentative parse. */
4669 bool err = cp_parser_error_occurred (parser);
4670 cp_parser_parse_definitely (parser);
4671 cp_parser_parse_definitely (parser);
4672 if (err)
4673 cp_parser_simulate_error (parser);
4674 }
4675 }
4676 };
4677
4678 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4679 This class is for tracking such a matching pair of symbols.
4680 In particular, it tracks the location of the first token,
4681 so that if the second token is missing, we can highlight the
4682 location of the first token when notifying the user about the
4683 problem. */
4684
4685 template <typename traits_t>
4686 class token_pair
4687 {
4688 public:
4689 /* token_pair's ctor. */
4690 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4691
4692 /* If the next token is the opening symbol for this pair, consume it and
4693 return true.
4694 Otherwise, issue an error and return false.
4695 In either case, record the location of the opening token. */
4696
4697 bool require_open (cp_parser *parser)
4698 {
4699 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4700 return cp_parser_require (parser, traits_t::open_token_type,
4701 traits_t::required_token_open);
4702 }
4703
4704 /* Consume the next token from PARSER, recording its location as
4705 that of the opening token within the pair. */
4706
4707 cp_token * consume_open (cp_parser *parser)
4708 {
4709 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4710 gcc_assert (tok->type == traits_t::open_token_type);
4711 m_open_loc = tok->location;
4712 return tok;
4713 }
4714
4715 /* If the next token is the closing symbol for this pair, consume it
4716 and return it.
4717 Otherwise, issue an error, highlighting the location of the
4718 corresponding opening token, and return NULL. */
4719
4720 cp_token *require_close (cp_parser *parser) const
4721 {
4722 return cp_parser_require (parser, traits_t::close_token_type,
4723 traits_t::required_token_close,
4724 m_open_loc);
4725 }
4726
4727 private:
4728 location_t m_open_loc;
4729 };
4730
4731 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4732
4733 struct matching_paren_traits
4734 {
4735 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4736 static const enum required_token required_token_open = RT_OPEN_PAREN;
4737 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4738 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4739 };
4740
4741 /* "matching_parens" is a token_pair<T> class for tracking matching
4742 pairs of parentheses. */
4743
4744 typedef token_pair<matching_paren_traits> matching_parens;
4745
4746 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4747
4748 struct matching_brace_traits
4749 {
4750 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4751 static const enum required_token required_token_open = RT_OPEN_BRACE;
4752 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4753 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4754 };
4755
4756 /* "matching_braces" is a token_pair<T> class for tracking matching
4757 pairs of braces. */
4758
4759 typedef token_pair<matching_brace_traits> matching_braces;
4760
4761
4762 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4763 enclosing parentheses. */
4764
4765 static cp_expr
4766 cp_parser_statement_expr (cp_parser *parser)
4767 {
4768 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4769
4770 /* Consume the '('. */
4771 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4772 matching_parens parens;
4773 parens.consume_open (parser);
4774 /* Start the statement-expression. */
4775 tree expr = begin_stmt_expr ();
4776 /* Parse the compound-statement. */
4777 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4778 /* Finish up. */
4779 expr = finish_stmt_expr (expr, false);
4780 /* Consume the ')'. */
4781 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4782 if (!parens.require_close (parser))
4783 cp_parser_skip_to_end_of_statement (parser);
4784
4785 cp_parser_end_tentative_firewall (parser, start, expr);
4786 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4787 return cp_expr (expr, combined_loc);
4788 }
4789
4790 /* Expressions [gram.expr] */
4791
4792 /* Parse a fold-operator.
4793
4794 fold-operator:
4795 - * / % ^ & | = < > << >>
4796 = -= *= /= %= ^= &= |= <<= >>=
4797 == != <= >= && || , .* ->*
4798
4799 This returns the tree code corresponding to the matched operator
4800 as an int. When the current token matches a compound assignment
4801 opertor, the resulting tree code is the negative value of the
4802 non-assignment operator. */
4803
4804 static int
4805 cp_parser_fold_operator (cp_token *token)
4806 {
4807 switch (token->type)
4808 {
4809 case CPP_PLUS: return PLUS_EXPR;
4810 case CPP_MINUS: return MINUS_EXPR;
4811 case CPP_MULT: return MULT_EXPR;
4812 case CPP_DIV: return TRUNC_DIV_EXPR;
4813 case CPP_MOD: return TRUNC_MOD_EXPR;
4814 case CPP_XOR: return BIT_XOR_EXPR;
4815 case CPP_AND: return BIT_AND_EXPR;
4816 case CPP_OR: return BIT_IOR_EXPR;
4817 case CPP_LSHIFT: return LSHIFT_EXPR;
4818 case CPP_RSHIFT: return RSHIFT_EXPR;
4819
4820 case CPP_EQ: return -NOP_EXPR;
4821 case CPP_PLUS_EQ: return -PLUS_EXPR;
4822 case CPP_MINUS_EQ: return -MINUS_EXPR;
4823 case CPP_MULT_EQ: return -MULT_EXPR;
4824 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4825 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4826 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4827 case CPP_AND_EQ: return -BIT_AND_EXPR;
4828 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4829 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4830 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4831
4832 case CPP_EQ_EQ: return EQ_EXPR;
4833 case CPP_NOT_EQ: return NE_EXPR;
4834 case CPP_LESS: return LT_EXPR;
4835 case CPP_GREATER: return GT_EXPR;
4836 case CPP_LESS_EQ: return LE_EXPR;
4837 case CPP_GREATER_EQ: return GE_EXPR;
4838
4839 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4840 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4841
4842 case CPP_COMMA: return COMPOUND_EXPR;
4843
4844 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4845 case CPP_DEREF_STAR: return MEMBER_REF;
4846
4847 default: return ERROR_MARK;
4848 }
4849 }
4850
4851 /* Returns true if CODE indicates a binary expression, which is not allowed in
4852 the LHS of a fold-expression. More codes will need to be added to use this
4853 function in other contexts. */
4854
4855 static bool
4856 is_binary_op (tree_code code)
4857 {
4858 switch (code)
4859 {
4860 case PLUS_EXPR:
4861 case POINTER_PLUS_EXPR:
4862 case MINUS_EXPR:
4863 case MULT_EXPR:
4864 case TRUNC_DIV_EXPR:
4865 case TRUNC_MOD_EXPR:
4866 case BIT_XOR_EXPR:
4867 case BIT_AND_EXPR:
4868 case BIT_IOR_EXPR:
4869 case LSHIFT_EXPR:
4870 case RSHIFT_EXPR:
4871
4872 case MODOP_EXPR:
4873
4874 case EQ_EXPR:
4875 case NE_EXPR:
4876 case LE_EXPR:
4877 case GE_EXPR:
4878 case LT_EXPR:
4879 case GT_EXPR:
4880
4881 case TRUTH_ANDIF_EXPR:
4882 case TRUTH_ORIF_EXPR:
4883
4884 case COMPOUND_EXPR:
4885
4886 case DOTSTAR_EXPR:
4887 case MEMBER_REF:
4888 return true;
4889
4890 default:
4891 return false;
4892 }
4893 }
4894
4895 /* If the next token is a suitable fold operator, consume it and return as
4896 the function above. */
4897
4898 static int
4899 cp_parser_fold_operator (cp_parser *parser)
4900 {
4901 cp_token* token = cp_lexer_peek_token (parser->lexer);
4902 int code = cp_parser_fold_operator (token);
4903 if (code != ERROR_MARK)
4904 cp_lexer_consume_token (parser->lexer);
4905 return code;
4906 }
4907
4908 /* Parse a fold-expression.
4909
4910 fold-expression:
4911 ( ... folding-operator cast-expression)
4912 ( cast-expression folding-operator ... )
4913 ( cast-expression folding operator ... folding-operator cast-expression)
4914
4915 Note that the '(' and ')' are matched in primary expression. */
4916
4917 static cp_expr
4918 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4919 {
4920 cp_id_kind pidk;
4921
4922 // Left fold.
4923 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4924 {
4925 cp_lexer_consume_token (parser->lexer);
4926 int op = cp_parser_fold_operator (parser);
4927 if (op == ERROR_MARK)
4928 {
4929 cp_parser_error (parser, "expected binary operator");
4930 return error_mark_node;
4931 }
4932
4933 tree expr = cp_parser_cast_expression (parser, false, false,
4934 false, &pidk);
4935 if (expr == error_mark_node)
4936 return error_mark_node;
4937 return finish_left_unary_fold_expr (expr, op);
4938 }
4939
4940 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4941 int op = cp_parser_fold_operator (parser);
4942 if (op == ERROR_MARK)
4943 {
4944 cp_parser_error (parser, "expected binary operator");
4945 return error_mark_node;
4946 }
4947
4948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4949 {
4950 cp_parser_error (parser, "expected ...");
4951 return error_mark_node;
4952 }
4953 cp_lexer_consume_token (parser->lexer);
4954
4955 /* The operands of a fold-expression are cast-expressions, so binary or
4956 conditional expressions are not allowed. We check this here to avoid
4957 tentative parsing. */
4958 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4959 /* OK, the expression was parenthesized. */;
4960 else if (is_binary_op (TREE_CODE (expr1)))
4961 error_at (location_of (expr1),
4962 "binary expression in operand of fold-expression");
4963 else if (TREE_CODE (expr1) == COND_EXPR)
4964 error_at (location_of (expr1),
4965 "conditional expression in operand of fold-expression");
4966
4967 // Right fold.
4968 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4969 return finish_right_unary_fold_expr (expr1, op);
4970
4971 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4972 {
4973 cp_parser_error (parser, "mismatched operator in fold-expression");
4974 return error_mark_node;
4975 }
4976 cp_lexer_consume_token (parser->lexer);
4977
4978 // Binary left or right fold.
4979 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4980 if (expr2 == error_mark_node)
4981 return error_mark_node;
4982 return finish_binary_fold_expr (expr1, expr2, op);
4983 }
4984
4985 /* Parse a primary-expression.
4986
4987 primary-expression:
4988 literal
4989 this
4990 ( expression )
4991 id-expression
4992 lambda-expression (C++11)
4993
4994 GNU Extensions:
4995
4996 primary-expression:
4997 ( compound-statement )
4998 __builtin_va_arg ( assignment-expression , type-id )
4999 __builtin_offsetof ( type-id , offsetof-expression )
5000
5001 C++ Extensions:
5002 __has_nothrow_assign ( type-id )
5003 __has_nothrow_constructor ( type-id )
5004 __has_nothrow_copy ( type-id )
5005 __has_trivial_assign ( type-id )
5006 __has_trivial_constructor ( type-id )
5007 __has_trivial_copy ( type-id )
5008 __has_trivial_destructor ( type-id )
5009 __has_virtual_destructor ( type-id )
5010 __is_abstract ( type-id )
5011 __is_base_of ( type-id , type-id )
5012 __is_class ( type-id )
5013 __is_empty ( type-id )
5014 __is_enum ( type-id )
5015 __is_final ( type-id )
5016 __is_literal_type ( type-id )
5017 __is_pod ( type-id )
5018 __is_polymorphic ( type-id )
5019 __is_std_layout ( type-id )
5020 __is_trivial ( type-id )
5021 __is_union ( type-id )
5022
5023 Objective-C++ Extension:
5024
5025 primary-expression:
5026 objc-expression
5027
5028 literal:
5029 __null
5030
5031 ADDRESS_P is true iff this expression was immediately preceded by
5032 "&" and therefore might denote a pointer-to-member. CAST_P is true
5033 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5034 true iff this expression is a template argument.
5035
5036 Returns a representation of the expression. Upon return, *IDK
5037 indicates what kind of id-expression (if any) was present. */
5038
5039 static cp_expr
5040 cp_parser_primary_expression (cp_parser *parser,
5041 bool address_p,
5042 bool cast_p,
5043 bool template_arg_p,
5044 bool decltype_p,
5045 cp_id_kind *idk)
5046 {
5047 cp_token *token = NULL;
5048
5049 /* Assume the primary expression is not an id-expression. */
5050 *idk = CP_ID_KIND_NONE;
5051
5052 /* Peek at the next token. */
5053 token = cp_lexer_peek_token (parser->lexer);
5054 switch ((int) token->type)
5055 {
5056 /* literal:
5057 integer-literal
5058 character-literal
5059 floating-literal
5060 string-literal
5061 boolean-literal
5062 pointer-literal
5063 user-defined-literal */
5064 case CPP_CHAR:
5065 case CPP_CHAR16:
5066 case CPP_CHAR32:
5067 case CPP_WCHAR:
5068 case CPP_UTF8CHAR:
5069 case CPP_NUMBER:
5070 case CPP_PREPARSED_EXPR:
5071 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5072 return cp_parser_userdef_numeric_literal (parser);
5073 token = cp_lexer_consume_token (parser->lexer);
5074 if (TREE_CODE (token->u.value) == FIXED_CST)
5075 {
5076 error_at (token->location,
5077 "fixed-point types not supported in C++");
5078 return error_mark_node;
5079 }
5080 /* Floating-point literals are only allowed in an integral
5081 constant expression if they are cast to an integral or
5082 enumeration type. */
5083 if (TREE_CODE (token->u.value) == REAL_CST
5084 && parser->integral_constant_expression_p
5085 && pedantic)
5086 {
5087 /* CAST_P will be set even in invalid code like "int(2.7 +
5088 ...)". Therefore, we have to check that the next token
5089 is sure to end the cast. */
5090 if (cast_p)
5091 {
5092 cp_token *next_token;
5093
5094 next_token = cp_lexer_peek_token (parser->lexer);
5095 if (/* The comma at the end of an
5096 enumerator-definition. */
5097 next_token->type != CPP_COMMA
5098 /* The curly brace at the end of an enum-specifier. */
5099 && next_token->type != CPP_CLOSE_BRACE
5100 /* The end of a statement. */
5101 && next_token->type != CPP_SEMICOLON
5102 /* The end of the cast-expression. */
5103 && next_token->type != CPP_CLOSE_PAREN
5104 /* The end of an array bound. */
5105 && next_token->type != CPP_CLOSE_SQUARE
5106 /* The closing ">" in a template-argument-list. */
5107 && (next_token->type != CPP_GREATER
5108 || parser->greater_than_is_operator_p)
5109 /* C++0x only: A ">>" treated like two ">" tokens,
5110 in a template-argument-list. */
5111 && (next_token->type != CPP_RSHIFT
5112 || (cxx_dialect == cxx98)
5113 || parser->greater_than_is_operator_p))
5114 cast_p = false;
5115 }
5116
5117 /* If we are within a cast, then the constraint that the
5118 cast is to an integral or enumeration type will be
5119 checked at that point. If we are not within a cast, then
5120 this code is invalid. */
5121 if (!cast_p)
5122 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5123 }
5124 return cp_expr (token->u.value, token->location);
5125
5126 case CPP_CHAR_USERDEF:
5127 case CPP_CHAR16_USERDEF:
5128 case CPP_CHAR32_USERDEF:
5129 case CPP_WCHAR_USERDEF:
5130 case CPP_UTF8CHAR_USERDEF:
5131 return cp_parser_userdef_char_literal (parser);
5132
5133 case CPP_STRING:
5134 case CPP_STRING16:
5135 case CPP_STRING32:
5136 case CPP_WSTRING:
5137 case CPP_UTF8STRING:
5138 case CPP_STRING_USERDEF:
5139 case CPP_STRING16_USERDEF:
5140 case CPP_STRING32_USERDEF:
5141 case CPP_WSTRING_USERDEF:
5142 case CPP_UTF8STRING_USERDEF:
5143 /* ??? Should wide strings be allowed when parser->translate_strings_p
5144 is false (i.e. in attributes)? If not, we can kill the third
5145 argument to cp_parser_string_literal. */
5146 return cp_parser_string_literal (parser,
5147 parser->translate_strings_p,
5148 true);
5149
5150 case CPP_OPEN_PAREN:
5151 /* If we see `( { ' then we are looking at the beginning of
5152 a GNU statement-expression. */
5153 if (cp_parser_allow_gnu_extensions_p (parser)
5154 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5155 {
5156 /* Statement-expressions are not allowed by the standard. */
5157 pedwarn (token->location, OPT_Wpedantic,
5158 "ISO C++ forbids braced-groups within expressions");
5159
5160 /* And they're not allowed outside of a function-body; you
5161 cannot, for example, write:
5162
5163 int i = ({ int j = 3; j + 1; });
5164
5165 at class or namespace scope. */
5166 if (!parser->in_function_body
5167 || parser->in_template_argument_list_p)
5168 {
5169 error_at (token->location,
5170 "statement-expressions are not allowed outside "
5171 "functions nor in template-argument lists");
5172 cp_parser_skip_to_end_of_block_or_statement (parser);
5173 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5174 cp_lexer_consume_token (parser->lexer);
5175 return error_mark_node;
5176 }
5177 else
5178 return cp_parser_statement_expr (parser);
5179 }
5180 /* Otherwise it's a normal parenthesized expression. */
5181 {
5182 cp_expr expr;
5183 bool saved_greater_than_is_operator_p;
5184
5185 location_t open_paren_loc = token->location;
5186
5187 /* Consume the `('. */
5188 matching_parens parens;
5189 parens.consume_open (parser);
5190 /* Within a parenthesized expression, a `>' token is always
5191 the greater-than operator. */
5192 saved_greater_than_is_operator_p
5193 = parser->greater_than_is_operator_p;
5194 parser->greater_than_is_operator_p = true;
5195
5196 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5197 /* Left fold expression. */
5198 expr = NULL_TREE;
5199 else
5200 /* Parse the parenthesized expression. */
5201 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5202
5203 token = cp_lexer_peek_token (parser->lexer);
5204 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5205 {
5206 expr = cp_parser_fold_expression (parser, expr);
5207 if (expr != error_mark_node
5208 && cxx_dialect < cxx17
5209 && !in_system_header_at (input_location))
5210 pedwarn (input_location, 0, "fold-expressions only available "
5211 "with -std=c++17 or -std=gnu++17");
5212 }
5213 else
5214 /* Let the front end know that this expression was
5215 enclosed in parentheses. This matters in case, for
5216 example, the expression is of the form `A::B', since
5217 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5218 not. */
5219 expr = finish_parenthesized_expr (expr);
5220
5221 /* DR 705: Wrapping an unqualified name in parentheses
5222 suppresses arg-dependent lookup. We want to pass back
5223 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5224 (c++/37862), but none of the others. */
5225 if (*idk != CP_ID_KIND_QUALIFIED)
5226 *idk = CP_ID_KIND_NONE;
5227
5228 /* The `>' token might be the end of a template-id or
5229 template-parameter-list now. */
5230 parser->greater_than_is_operator_p
5231 = saved_greater_than_is_operator_p;
5232
5233 /* Consume the `)'. */
5234 token = cp_lexer_peek_token (parser->lexer);
5235 location_t close_paren_loc = token->location;
5236 expr.set_range (open_paren_loc, close_paren_loc);
5237 if (!parens.require_close (parser)
5238 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5239 cp_parser_skip_to_end_of_statement (parser);
5240
5241 return expr;
5242 }
5243
5244 case CPP_OPEN_SQUARE:
5245 {
5246 if (c_dialect_objc ())
5247 {
5248 /* We might have an Objective-C++ message. */
5249 cp_parser_parse_tentatively (parser);
5250 tree msg = cp_parser_objc_message_expression (parser);
5251 /* If that works out, we're done ... */
5252 if (cp_parser_parse_definitely (parser))
5253 return msg;
5254 /* ... else, fall though to see if it's a lambda. */
5255 }
5256 cp_expr lam = cp_parser_lambda_expression (parser);
5257 /* Don't warn about a failed tentative parse. */
5258 if (cp_parser_error_occurred (parser))
5259 return error_mark_node;
5260 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5261 return lam;
5262 }
5263
5264 case CPP_OBJC_STRING:
5265 if (c_dialect_objc ())
5266 /* We have an Objective-C++ string literal. */
5267 return cp_parser_objc_expression (parser);
5268 cp_parser_error (parser, "expected primary-expression");
5269 return error_mark_node;
5270
5271 case CPP_KEYWORD:
5272 switch (token->keyword)
5273 {
5274 /* These two are the boolean literals. */
5275 case RID_TRUE:
5276 cp_lexer_consume_token (parser->lexer);
5277 return cp_expr (boolean_true_node, token->location);
5278 case RID_FALSE:
5279 cp_lexer_consume_token (parser->lexer);
5280 return cp_expr (boolean_false_node, token->location);
5281
5282 /* The `__null' literal. */
5283 case RID_NULL:
5284 cp_lexer_consume_token (parser->lexer);
5285 return cp_expr (null_node, token->location);
5286
5287 /* The `nullptr' literal. */
5288 case RID_NULLPTR:
5289 cp_lexer_consume_token (parser->lexer);
5290 return cp_expr (nullptr_node, token->location);
5291
5292 /* Recognize the `this' keyword. */
5293 case RID_THIS:
5294 cp_lexer_consume_token (parser->lexer);
5295 if (parser->local_variables_forbidden_p)
5296 {
5297 error_at (token->location,
5298 "%<this%> may not be used in this context");
5299 return error_mark_node;
5300 }
5301 /* Pointers cannot appear in constant-expressions. */
5302 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5303 return error_mark_node;
5304 return cp_expr (finish_this_expr (), token->location);
5305
5306 /* The `operator' keyword can be the beginning of an
5307 id-expression. */
5308 case RID_OPERATOR:
5309 goto id_expression;
5310
5311 case RID_FUNCTION_NAME:
5312 case RID_PRETTY_FUNCTION_NAME:
5313 case RID_C99_FUNCTION_NAME:
5314 {
5315 non_integral_constant name;
5316
5317 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5318 __func__ are the names of variables -- but they are
5319 treated specially. Therefore, they are handled here,
5320 rather than relying on the generic id-expression logic
5321 below. Grammatically, these names are id-expressions.
5322
5323 Consume the token. */
5324 token = cp_lexer_consume_token (parser->lexer);
5325
5326 switch (token->keyword)
5327 {
5328 case RID_FUNCTION_NAME:
5329 name = NIC_FUNC_NAME;
5330 break;
5331 case RID_PRETTY_FUNCTION_NAME:
5332 name = NIC_PRETTY_FUNC;
5333 break;
5334 case RID_C99_FUNCTION_NAME:
5335 name = NIC_C99_FUNC;
5336 break;
5337 default:
5338 gcc_unreachable ();
5339 }
5340
5341 if (cp_parser_non_integral_constant_expression (parser, name))
5342 return error_mark_node;
5343
5344 /* Look up the name. */
5345 return finish_fname (token->u.value);
5346 }
5347
5348 case RID_VA_ARG:
5349 {
5350 tree expression;
5351 tree type;
5352 source_location type_location;
5353 location_t start_loc
5354 = cp_lexer_peek_token (parser->lexer)->location;
5355 /* The `__builtin_va_arg' construct is used to handle
5356 `va_arg'. Consume the `__builtin_va_arg' token. */
5357 cp_lexer_consume_token (parser->lexer);
5358 /* Look for the opening `('. */
5359 matching_parens parens;
5360 parens.require_open (parser);
5361 /* Now, parse the assignment-expression. */
5362 expression = cp_parser_assignment_expression (parser);
5363 /* Look for the `,'. */
5364 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5365 type_location = cp_lexer_peek_token (parser->lexer)->location;
5366 /* Parse the type-id. */
5367 {
5368 type_id_in_expr_sentinel s (parser);
5369 type = cp_parser_type_id (parser);
5370 }
5371 /* Look for the closing `)'. */
5372 location_t finish_loc
5373 = cp_lexer_peek_token (parser->lexer)->location;
5374 parens.require_close (parser);
5375 /* Using `va_arg' in a constant-expression is not
5376 allowed. */
5377 if (cp_parser_non_integral_constant_expression (parser,
5378 NIC_VA_ARG))
5379 return error_mark_node;
5380 /* Construct a location of the form:
5381 __builtin_va_arg (v, int)
5382 ~~~~~~~~~~~~~~~~~~~~~^~~~
5383 with the caret at the type, ranging from the start of the
5384 "__builtin_va_arg" token to the close paren. */
5385 location_t combined_loc
5386 = make_location (type_location, start_loc, finish_loc);
5387 return build_x_va_arg (combined_loc, expression, type);
5388 }
5389
5390 case RID_OFFSETOF:
5391 return cp_parser_builtin_offsetof (parser);
5392
5393 case RID_HAS_NOTHROW_ASSIGN:
5394 case RID_HAS_NOTHROW_CONSTRUCTOR:
5395 case RID_HAS_NOTHROW_COPY:
5396 case RID_HAS_TRIVIAL_ASSIGN:
5397 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5398 case RID_HAS_TRIVIAL_COPY:
5399 case RID_HAS_TRIVIAL_DESTRUCTOR:
5400 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5401 case RID_HAS_VIRTUAL_DESTRUCTOR:
5402 case RID_IS_ABSTRACT:
5403 case RID_IS_AGGREGATE:
5404 case RID_IS_BASE_OF:
5405 case RID_IS_CLASS:
5406 case RID_IS_EMPTY:
5407 case RID_IS_ENUM:
5408 case RID_IS_FINAL:
5409 case RID_IS_LITERAL_TYPE:
5410 case RID_IS_POD:
5411 case RID_IS_POLYMORPHIC:
5412 case RID_IS_SAME_AS:
5413 case RID_IS_STD_LAYOUT:
5414 case RID_IS_TRIVIAL:
5415 case RID_IS_TRIVIALLY_ASSIGNABLE:
5416 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5417 case RID_IS_TRIVIALLY_COPYABLE:
5418 case RID_IS_UNION:
5419 case RID_IS_ASSIGNABLE:
5420 case RID_IS_CONSTRUCTIBLE:
5421 return cp_parser_trait_expr (parser, token->keyword);
5422
5423 // C++ concepts
5424 case RID_REQUIRES:
5425 return cp_parser_requires_expression (parser);
5426
5427 /* Objective-C++ expressions. */
5428 case RID_AT_ENCODE:
5429 case RID_AT_PROTOCOL:
5430 case RID_AT_SELECTOR:
5431 return cp_parser_objc_expression (parser);
5432
5433 case RID_TEMPLATE:
5434 if (parser->in_function_body
5435 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5436 == CPP_LESS))
5437 {
5438 error_at (token->location,
5439 "a template declaration cannot appear at block scope");
5440 cp_parser_skip_to_end_of_block_or_statement (parser);
5441 return error_mark_node;
5442 }
5443 /* FALLTHRU */
5444 default:
5445 cp_parser_error (parser, "expected primary-expression");
5446 return error_mark_node;
5447 }
5448
5449 /* An id-expression can start with either an identifier, a
5450 `::' as the beginning of a qualified-id, or the "operator"
5451 keyword. */
5452 case CPP_NAME:
5453 case CPP_SCOPE:
5454 case CPP_TEMPLATE_ID:
5455 case CPP_NESTED_NAME_SPECIFIER:
5456 {
5457 id_expression:
5458 cp_expr id_expression;
5459 cp_expr decl;
5460 const char *error_msg;
5461 bool template_p;
5462 bool done;
5463 cp_token *id_expr_token;
5464
5465 /* Parse the id-expression. */
5466 id_expression
5467 = cp_parser_id_expression (parser,
5468 /*template_keyword_p=*/false,
5469 /*check_dependency_p=*/true,
5470 &template_p,
5471 /*declarator_p=*/false,
5472 /*optional_p=*/false);
5473 if (id_expression == error_mark_node)
5474 return error_mark_node;
5475 id_expr_token = token;
5476 token = cp_lexer_peek_token (parser->lexer);
5477 done = (token->type != CPP_OPEN_SQUARE
5478 && token->type != CPP_OPEN_PAREN
5479 && token->type != CPP_DOT
5480 && token->type != CPP_DEREF
5481 && token->type != CPP_PLUS_PLUS
5482 && token->type != CPP_MINUS_MINUS);
5483 /* If we have a template-id, then no further lookup is
5484 required. If the template-id was for a template-class, we
5485 will sometimes have a TYPE_DECL at this point. */
5486 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5487 || TREE_CODE (id_expression) == TYPE_DECL)
5488 decl = id_expression;
5489 /* Look up the name. */
5490 else
5491 {
5492 tree ambiguous_decls;
5493
5494 /* If we already know that this lookup is ambiguous, then
5495 we've already issued an error message; there's no reason
5496 to check again. */
5497 if (id_expr_token->type == CPP_NAME
5498 && id_expr_token->error_reported)
5499 {
5500 cp_parser_simulate_error (parser);
5501 return error_mark_node;
5502 }
5503
5504 decl = cp_parser_lookup_name (parser, id_expression,
5505 none_type,
5506 template_p,
5507 /*is_namespace=*/false,
5508 /*check_dependency=*/true,
5509 &ambiguous_decls,
5510 id_expr_token->location);
5511 /* If the lookup was ambiguous, an error will already have
5512 been issued. */
5513 if (ambiguous_decls)
5514 return error_mark_node;
5515
5516 /* In Objective-C++, we may have an Objective-C 2.0
5517 dot-syntax for classes here. */
5518 if (c_dialect_objc ()
5519 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5520 && TREE_CODE (decl) == TYPE_DECL
5521 && objc_is_class_name (decl))
5522 {
5523 tree component;
5524 cp_lexer_consume_token (parser->lexer);
5525 component = cp_parser_identifier (parser);
5526 if (component == error_mark_node)
5527 return error_mark_node;
5528
5529 tree result = objc_build_class_component_ref (id_expression,
5530 component);
5531 /* Build a location of the form:
5532 expr.component
5533 ~~~~~^~~~~~~~~
5534 with caret at the start of the component name (at
5535 input_location), ranging from the start of the id_expression
5536 to the end of the component name. */
5537 location_t combined_loc
5538 = make_location (input_location, id_expression.get_start (),
5539 get_finish (input_location));
5540 protected_set_expr_location (result, combined_loc);
5541 return result;
5542 }
5543
5544 /* In Objective-C++, an instance variable (ivar) may be preferred
5545 to whatever cp_parser_lookup_name() found.
5546 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5547 rest of c-family, we have to do a little extra work to preserve
5548 any location information in cp_expr "decl". Given that
5549 objc_lookup_ivar is implemented in "c-family" and "objc", we
5550 have a trip through the pure "tree" type, rather than cp_expr.
5551 Naively copying it back to "decl" would implicitly give the
5552 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5553 store an EXPR_LOCATION. Hence we only update "decl" (and
5554 hence its location_t) if we get back a different tree node. */
5555 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5556 id_expression);
5557 if (decl_tree != decl.get_value ())
5558 decl = cp_expr (decl_tree);
5559
5560 /* If name lookup gives us a SCOPE_REF, then the
5561 qualifying scope was dependent. */
5562 if (TREE_CODE (decl) == SCOPE_REF)
5563 {
5564 /* At this point, we do not know if DECL is a valid
5565 integral constant expression. We assume that it is
5566 in fact such an expression, so that code like:
5567
5568 template <int N> struct A {
5569 int a[B<N>::i];
5570 };
5571
5572 is accepted. At template-instantiation time, we
5573 will check that B<N>::i is actually a constant. */
5574 return decl;
5575 }
5576 /* Check to see if DECL is a local variable in a context
5577 where that is forbidden. */
5578 if (parser->local_variables_forbidden_p
5579 && local_variable_p (decl))
5580 {
5581 /* It might be that we only found DECL because we are
5582 trying to be generous with pre-ISO scoping rules.
5583 For example, consider:
5584
5585 int i;
5586 void g() {
5587 for (int i = 0; i < 10; ++i) {}
5588 extern void f(int j = i);
5589 }
5590
5591 Here, name look up will originally find the out
5592 of scope `i'. We need to issue a warning message,
5593 but then use the global `i'. */
5594 decl = check_for_out_of_scope_variable (decl);
5595 if (local_variable_p (decl))
5596 {
5597 error_at (id_expr_token->location,
5598 "local variable %qD may not appear in this context",
5599 decl.get_value ());
5600 return error_mark_node;
5601 }
5602 }
5603 }
5604
5605 decl = (finish_id_expression
5606 (id_expression, decl, parser->scope,
5607 idk,
5608 parser->integral_constant_expression_p,
5609 parser->allow_non_integral_constant_expression_p,
5610 &parser->non_integral_constant_expression_p,
5611 template_p, done, address_p,
5612 template_arg_p,
5613 &error_msg,
5614 id_expression.get_location ()));
5615 if (error_msg)
5616 cp_parser_error (parser, error_msg);
5617 decl.set_location (id_expr_token->location);
5618 return decl;
5619 }
5620
5621 /* Anything else is an error. */
5622 default:
5623 cp_parser_error (parser, "expected primary-expression");
5624 return error_mark_node;
5625 }
5626 }
5627
5628 static inline cp_expr
5629 cp_parser_primary_expression (cp_parser *parser,
5630 bool address_p,
5631 bool cast_p,
5632 bool template_arg_p,
5633 cp_id_kind *idk)
5634 {
5635 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5636 /*decltype*/false, idk);
5637 }
5638
5639 /* Parse an id-expression.
5640
5641 id-expression:
5642 unqualified-id
5643 qualified-id
5644
5645 qualified-id:
5646 :: [opt] nested-name-specifier template [opt] unqualified-id
5647 :: identifier
5648 :: operator-function-id
5649 :: template-id
5650
5651 Return a representation of the unqualified portion of the
5652 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5653 a `::' or nested-name-specifier.
5654
5655 Often, if the id-expression was a qualified-id, the caller will
5656 want to make a SCOPE_REF to represent the qualified-id. This
5657 function does not do this in order to avoid wastefully creating
5658 SCOPE_REFs when they are not required.
5659
5660 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5661 `template' keyword.
5662
5663 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5664 uninstantiated templates.
5665
5666 If *TEMPLATE_P is non-NULL, it is set to true iff the
5667 `template' keyword is used to explicitly indicate that the entity
5668 named is a template.
5669
5670 If DECLARATOR_P is true, the id-expression is appearing as part of
5671 a declarator, rather than as part of an expression. */
5672
5673 static cp_expr
5674 cp_parser_id_expression (cp_parser *parser,
5675 bool template_keyword_p,
5676 bool check_dependency_p,
5677 bool *template_p,
5678 bool declarator_p,
5679 bool optional_p)
5680 {
5681 bool global_scope_p;
5682 bool nested_name_specifier_p;
5683
5684 /* Assume the `template' keyword was not used. */
5685 if (template_p)
5686 *template_p = template_keyword_p;
5687
5688 /* Look for the optional `::' operator. */
5689 global_scope_p
5690 = (!template_keyword_p
5691 && (cp_parser_global_scope_opt (parser,
5692 /*current_scope_valid_p=*/false)
5693 != NULL_TREE));
5694
5695 /* Look for the optional nested-name-specifier. */
5696 nested_name_specifier_p
5697 = (cp_parser_nested_name_specifier_opt (parser,
5698 /*typename_keyword_p=*/false,
5699 check_dependency_p,
5700 /*type_p=*/false,
5701 declarator_p,
5702 template_keyword_p)
5703 != NULL_TREE);
5704
5705 /* If there is a nested-name-specifier, then we are looking at
5706 the first qualified-id production. */
5707 if (nested_name_specifier_p)
5708 {
5709 tree saved_scope;
5710 tree saved_object_scope;
5711 tree saved_qualifying_scope;
5712 cp_expr unqualified_id;
5713 bool is_template;
5714
5715 /* See if the next token is the `template' keyword. */
5716 if (!template_p)
5717 template_p = &is_template;
5718 *template_p = cp_parser_optional_template_keyword (parser);
5719 /* Name lookup we do during the processing of the
5720 unqualified-id might obliterate SCOPE. */
5721 saved_scope = parser->scope;
5722 saved_object_scope = parser->object_scope;
5723 saved_qualifying_scope = parser->qualifying_scope;
5724 /* Process the final unqualified-id. */
5725 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5726 check_dependency_p,
5727 declarator_p,
5728 /*optional_p=*/false);
5729 /* Restore the SAVED_SCOPE for our caller. */
5730 parser->scope = saved_scope;
5731 parser->object_scope = saved_object_scope;
5732 parser->qualifying_scope = saved_qualifying_scope;
5733
5734 return unqualified_id;
5735 }
5736 /* Otherwise, if we are in global scope, then we are looking at one
5737 of the other qualified-id productions. */
5738 else if (global_scope_p)
5739 {
5740 cp_token *token;
5741 tree id;
5742
5743 /* Peek at the next token. */
5744 token = cp_lexer_peek_token (parser->lexer);
5745
5746 /* If it's an identifier, and the next token is not a "<", then
5747 we can avoid the template-id case. This is an optimization
5748 for this common case. */
5749 if (token->type == CPP_NAME
5750 && !cp_parser_nth_token_starts_template_argument_list_p
5751 (parser, 2))
5752 return cp_parser_identifier (parser);
5753
5754 cp_parser_parse_tentatively (parser);
5755 /* Try a template-id. */
5756 id = cp_parser_template_id (parser,
5757 /*template_keyword_p=*/false,
5758 /*check_dependency_p=*/true,
5759 none_type,
5760 declarator_p);
5761 /* If that worked, we're done. */
5762 if (cp_parser_parse_definitely (parser))
5763 return id;
5764
5765 /* Peek at the next token. (Changes in the token buffer may
5766 have invalidated the pointer obtained above.) */
5767 token = cp_lexer_peek_token (parser->lexer);
5768
5769 switch (token->type)
5770 {
5771 case CPP_NAME:
5772 return cp_parser_identifier (parser);
5773
5774 case CPP_KEYWORD:
5775 if (token->keyword == RID_OPERATOR)
5776 return cp_parser_operator_function_id (parser);
5777 /* Fall through. */
5778
5779 default:
5780 cp_parser_error (parser, "expected id-expression");
5781 return error_mark_node;
5782 }
5783 }
5784 else
5785 return cp_parser_unqualified_id (parser, template_keyword_p,
5786 /*check_dependency_p=*/true,
5787 declarator_p,
5788 optional_p);
5789 }
5790
5791 /* Parse an unqualified-id.
5792
5793 unqualified-id:
5794 identifier
5795 operator-function-id
5796 conversion-function-id
5797 ~ class-name
5798 template-id
5799
5800 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5801 keyword, in a construct like `A::template ...'.
5802
5803 Returns a representation of unqualified-id. For the `identifier'
5804 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5805 production a BIT_NOT_EXPR is returned; the operand of the
5806 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5807 other productions, see the documentation accompanying the
5808 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5809 names are looked up in uninstantiated templates. If DECLARATOR_P
5810 is true, the unqualified-id is appearing as part of a declarator,
5811 rather than as part of an expression. */
5812
5813 static cp_expr
5814 cp_parser_unqualified_id (cp_parser* parser,
5815 bool template_keyword_p,
5816 bool check_dependency_p,
5817 bool declarator_p,
5818 bool optional_p)
5819 {
5820 cp_token *token;
5821
5822 /* Peek at the next token. */
5823 token = cp_lexer_peek_token (parser->lexer);
5824
5825 switch ((int) token->type)
5826 {
5827 case CPP_NAME:
5828 {
5829 tree id;
5830
5831 /* We don't know yet whether or not this will be a
5832 template-id. */
5833 cp_parser_parse_tentatively (parser);
5834 /* Try a template-id. */
5835 id = cp_parser_template_id (parser, template_keyword_p,
5836 check_dependency_p,
5837 none_type,
5838 declarator_p);
5839 /* If it worked, we're done. */
5840 if (cp_parser_parse_definitely (parser))
5841 return id;
5842 /* Otherwise, it's an ordinary identifier. */
5843 return cp_parser_identifier (parser);
5844 }
5845
5846 case CPP_TEMPLATE_ID:
5847 return cp_parser_template_id (parser, template_keyword_p,
5848 check_dependency_p,
5849 none_type,
5850 declarator_p);
5851
5852 case CPP_COMPL:
5853 {
5854 tree type_decl;
5855 tree qualifying_scope;
5856 tree object_scope;
5857 tree scope;
5858 bool done;
5859
5860 /* Consume the `~' token. */
5861 cp_lexer_consume_token (parser->lexer);
5862 /* Parse the class-name. The standard, as written, seems to
5863 say that:
5864
5865 template <typename T> struct S { ~S (); };
5866 template <typename T> S<T>::~S() {}
5867
5868 is invalid, since `~' must be followed by a class-name, but
5869 `S<T>' is dependent, and so not known to be a class.
5870 That's not right; we need to look in uninstantiated
5871 templates. A further complication arises from:
5872
5873 template <typename T> void f(T t) {
5874 t.T::~T();
5875 }
5876
5877 Here, it is not possible to look up `T' in the scope of `T'
5878 itself. We must look in both the current scope, and the
5879 scope of the containing complete expression.
5880
5881 Yet another issue is:
5882
5883 struct S {
5884 int S;
5885 ~S();
5886 };
5887
5888 S::~S() {}
5889
5890 The standard does not seem to say that the `S' in `~S'
5891 should refer to the type `S' and not the data member
5892 `S::S'. */
5893
5894 /* DR 244 says that we look up the name after the "~" in the
5895 same scope as we looked up the qualifying name. That idea
5896 isn't fully worked out; it's more complicated than that. */
5897 scope = parser->scope;
5898 object_scope = parser->object_scope;
5899 qualifying_scope = parser->qualifying_scope;
5900
5901 /* Check for invalid scopes. */
5902 if (scope == error_mark_node)
5903 {
5904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5905 cp_lexer_consume_token (parser->lexer);
5906 return error_mark_node;
5907 }
5908 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5909 {
5910 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5911 error_at (token->location,
5912 "scope %qT before %<~%> is not a class-name",
5913 scope);
5914 cp_parser_simulate_error (parser);
5915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5916 cp_lexer_consume_token (parser->lexer);
5917 return error_mark_node;
5918 }
5919 gcc_assert (!scope || TYPE_P (scope));
5920
5921 /* If the name is of the form "X::~X" it's OK even if X is a
5922 typedef. */
5923 token = cp_lexer_peek_token (parser->lexer);
5924 if (scope
5925 && token->type == CPP_NAME
5926 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5927 != CPP_LESS)
5928 && (token->u.value == TYPE_IDENTIFIER (scope)
5929 || (CLASS_TYPE_P (scope)
5930 && constructor_name_p (token->u.value, scope))))
5931 {
5932 cp_lexer_consume_token (parser->lexer);
5933 return build_nt (BIT_NOT_EXPR, scope);
5934 }
5935
5936 /* ~auto means the destructor of whatever the object is. */
5937 if (cp_parser_is_keyword (token, RID_AUTO))
5938 {
5939 if (cxx_dialect < cxx14)
5940 pedwarn (input_location, 0,
5941 "%<~auto%> only available with "
5942 "-std=c++14 or -std=gnu++14");
5943 cp_lexer_consume_token (parser->lexer);
5944 return build_nt (BIT_NOT_EXPR, make_auto ());
5945 }
5946
5947 /* If there was an explicit qualification (S::~T), first look
5948 in the scope given by the qualification (i.e., S).
5949
5950 Note: in the calls to cp_parser_class_name below we pass
5951 typename_type so that lookup finds the injected-class-name
5952 rather than the constructor. */
5953 done = false;
5954 type_decl = NULL_TREE;
5955 if (scope)
5956 {
5957 cp_parser_parse_tentatively (parser);
5958 type_decl = cp_parser_class_name (parser,
5959 /*typename_keyword_p=*/false,
5960 /*template_keyword_p=*/false,
5961 typename_type,
5962 /*check_dependency=*/false,
5963 /*class_head_p=*/false,
5964 declarator_p);
5965 if (cp_parser_parse_definitely (parser))
5966 done = true;
5967 }
5968 /* In "N::S::~S", look in "N" as well. */
5969 if (!done && scope && qualifying_scope)
5970 {
5971 cp_parser_parse_tentatively (parser);
5972 parser->scope = qualifying_scope;
5973 parser->object_scope = NULL_TREE;
5974 parser->qualifying_scope = NULL_TREE;
5975 type_decl
5976 = cp_parser_class_name (parser,
5977 /*typename_keyword_p=*/false,
5978 /*template_keyword_p=*/false,
5979 typename_type,
5980 /*check_dependency=*/false,
5981 /*class_head_p=*/false,
5982 declarator_p);
5983 if (cp_parser_parse_definitely (parser))
5984 done = true;
5985 }
5986 /* In "p->S::~T", look in the scope given by "*p" as well. */
5987 else if (!done && object_scope)
5988 {
5989 cp_parser_parse_tentatively (parser);
5990 parser->scope = object_scope;
5991 parser->object_scope = NULL_TREE;
5992 parser->qualifying_scope = NULL_TREE;
5993 type_decl
5994 = cp_parser_class_name (parser,
5995 /*typename_keyword_p=*/false,
5996 /*template_keyword_p=*/false,
5997 typename_type,
5998 /*check_dependency=*/false,
5999 /*class_head_p=*/false,
6000 declarator_p);
6001 if (cp_parser_parse_definitely (parser))
6002 done = true;
6003 }
6004 /* Look in the surrounding context. */
6005 if (!done)
6006 {
6007 parser->scope = NULL_TREE;
6008 parser->object_scope = NULL_TREE;
6009 parser->qualifying_scope = NULL_TREE;
6010 if (processing_template_decl)
6011 cp_parser_parse_tentatively (parser);
6012 type_decl
6013 = cp_parser_class_name (parser,
6014 /*typename_keyword_p=*/false,
6015 /*template_keyword_p=*/false,
6016 typename_type,
6017 /*check_dependency=*/false,
6018 /*class_head_p=*/false,
6019 declarator_p);
6020 if (processing_template_decl
6021 && ! cp_parser_parse_definitely (parser))
6022 {
6023 /* We couldn't find a type with this name. If we're parsing
6024 tentatively, fail and try something else. */
6025 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6026 {
6027 cp_parser_simulate_error (parser);
6028 return error_mark_node;
6029 }
6030 /* Otherwise, accept it and check for a match at instantiation
6031 time. */
6032 type_decl = cp_parser_identifier (parser);
6033 if (type_decl != error_mark_node)
6034 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6035 return type_decl;
6036 }
6037 }
6038 /* If an error occurred, assume that the name of the
6039 destructor is the same as the name of the qualifying
6040 class. That allows us to keep parsing after running
6041 into ill-formed destructor names. */
6042 if (type_decl == error_mark_node && scope)
6043 return build_nt (BIT_NOT_EXPR, scope);
6044 else if (type_decl == error_mark_node)
6045 return error_mark_node;
6046
6047 /* Check that destructor name and scope match. */
6048 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6049 {
6050 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6051 error_at (token->location,
6052 "declaration of %<~%T%> as member of %qT",
6053 type_decl, scope);
6054 cp_parser_simulate_error (parser);
6055 return error_mark_node;
6056 }
6057
6058 /* [class.dtor]
6059
6060 A typedef-name that names a class shall not be used as the
6061 identifier in the declarator for a destructor declaration. */
6062 if (declarator_p
6063 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6064 && !DECL_SELF_REFERENCE_P (type_decl)
6065 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6066 error_at (token->location,
6067 "typedef-name %qD used as destructor declarator",
6068 type_decl);
6069
6070 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6071 }
6072
6073 case CPP_KEYWORD:
6074 if (token->keyword == RID_OPERATOR)
6075 {
6076 cp_expr id;
6077
6078 /* This could be a template-id, so we try that first. */
6079 cp_parser_parse_tentatively (parser);
6080 /* Try a template-id. */
6081 id = cp_parser_template_id (parser, template_keyword_p,
6082 /*check_dependency_p=*/true,
6083 none_type,
6084 declarator_p);
6085 /* If that worked, we're done. */
6086 if (cp_parser_parse_definitely (parser))
6087 return id;
6088 /* We still don't know whether we're looking at an
6089 operator-function-id or a conversion-function-id. */
6090 cp_parser_parse_tentatively (parser);
6091 /* Try an operator-function-id. */
6092 id = cp_parser_operator_function_id (parser);
6093 /* If that didn't work, try a conversion-function-id. */
6094 if (!cp_parser_parse_definitely (parser))
6095 id = cp_parser_conversion_function_id (parser);
6096 else if (UDLIT_OPER_P (id))
6097 {
6098 /* 17.6.3.3.5 */
6099 const char *name = UDLIT_OP_SUFFIX (id);
6100 if (name[0] != '_' && !in_system_header_at (input_location)
6101 && declarator_p)
6102 warning (OPT_Wliteral_suffix,
6103 "literal operator suffixes not preceded by %<_%>"
6104 " are reserved for future standardization");
6105 }
6106
6107 return id;
6108 }
6109 /* Fall through. */
6110
6111 default:
6112 if (optional_p)
6113 return NULL_TREE;
6114 cp_parser_error (parser, "expected unqualified-id");
6115 return error_mark_node;
6116 }
6117 }
6118
6119 /* Parse an (optional) nested-name-specifier.
6120
6121 nested-name-specifier: [C++98]
6122 class-or-namespace-name :: nested-name-specifier [opt]
6123 class-or-namespace-name :: template nested-name-specifier [opt]
6124
6125 nested-name-specifier: [C++0x]
6126 type-name ::
6127 namespace-name ::
6128 nested-name-specifier identifier ::
6129 nested-name-specifier template [opt] simple-template-id ::
6130
6131 PARSER->SCOPE should be set appropriately before this function is
6132 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6133 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6134 in name lookups.
6135
6136 Sets PARSER->SCOPE to the class (TYPE) or namespace
6137 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6138 it unchanged if there is no nested-name-specifier. Returns the new
6139 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6140
6141 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6142 part of a declaration and/or decl-specifier. */
6143
6144 static tree
6145 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6146 bool typename_keyword_p,
6147 bool check_dependency_p,
6148 bool type_p,
6149 bool is_declaration,
6150 bool template_keyword_p /* = false */)
6151 {
6152 bool success = false;
6153 cp_token_position start = 0;
6154 cp_token *token;
6155
6156 /* Remember where the nested-name-specifier starts. */
6157 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6158 {
6159 start = cp_lexer_token_position (parser->lexer, false);
6160 push_deferring_access_checks (dk_deferred);
6161 }
6162
6163 while (true)
6164 {
6165 tree new_scope;
6166 tree old_scope;
6167 tree saved_qualifying_scope;
6168
6169 /* Spot cases that cannot be the beginning of a
6170 nested-name-specifier. */
6171 token = cp_lexer_peek_token (parser->lexer);
6172
6173 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6174 the already parsed nested-name-specifier. */
6175 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6176 {
6177 /* Grab the nested-name-specifier and continue the loop. */
6178 cp_parser_pre_parsed_nested_name_specifier (parser);
6179 /* If we originally encountered this nested-name-specifier
6180 with IS_DECLARATION set to false, we will not have
6181 resolved TYPENAME_TYPEs, so we must do so here. */
6182 if (is_declaration
6183 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6184 {
6185 new_scope = resolve_typename_type (parser->scope,
6186 /*only_current_p=*/false);
6187 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6188 parser->scope = new_scope;
6189 }
6190 success = true;
6191 continue;
6192 }
6193
6194 /* Spot cases that cannot be the beginning of a
6195 nested-name-specifier. On the second and subsequent times
6196 through the loop, we look for the `template' keyword. */
6197 if (success && token->keyword == RID_TEMPLATE)
6198 ;
6199 /* A template-id can start a nested-name-specifier. */
6200 else if (token->type == CPP_TEMPLATE_ID)
6201 ;
6202 /* DR 743: decltype can be used in a nested-name-specifier. */
6203 else if (token_is_decltype (token))
6204 ;
6205 else
6206 {
6207 /* If the next token is not an identifier, then it is
6208 definitely not a type-name or namespace-name. */
6209 if (token->type != CPP_NAME)
6210 break;
6211 /* If the following token is neither a `<' (to begin a
6212 template-id), nor a `::', then we are not looking at a
6213 nested-name-specifier. */
6214 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6215
6216 if (token->type == CPP_COLON
6217 && parser->colon_corrects_to_scope_p
6218 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6219 {
6220 gcc_rich_location richloc (token->location);
6221 richloc.add_fixit_replace ("::");
6222 error_at (&richloc,
6223 "found %<:%> in nested-name-specifier, "
6224 "expected %<::%>");
6225 token->type = CPP_SCOPE;
6226 }
6227
6228 if (token->type != CPP_SCOPE
6229 && !cp_parser_nth_token_starts_template_argument_list_p
6230 (parser, 2))
6231 break;
6232 }
6233
6234 /* The nested-name-specifier is optional, so we parse
6235 tentatively. */
6236 cp_parser_parse_tentatively (parser);
6237
6238 /* Look for the optional `template' keyword, if this isn't the
6239 first time through the loop. */
6240 if (success)
6241 template_keyword_p = cp_parser_optional_template_keyword (parser);
6242
6243 /* Save the old scope since the name lookup we are about to do
6244 might destroy it. */
6245 old_scope = parser->scope;
6246 saved_qualifying_scope = parser->qualifying_scope;
6247 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6248 look up names in "X<T>::I" in order to determine that "Y" is
6249 a template. So, if we have a typename at this point, we make
6250 an effort to look through it. */
6251 if (is_declaration
6252 && !typename_keyword_p
6253 && parser->scope
6254 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6255 parser->scope = resolve_typename_type (parser->scope,
6256 /*only_current_p=*/false);
6257 /* Parse the qualifying entity. */
6258 new_scope
6259 = cp_parser_qualifying_entity (parser,
6260 typename_keyword_p,
6261 template_keyword_p,
6262 check_dependency_p,
6263 type_p,
6264 is_declaration);
6265 /* Look for the `::' token. */
6266 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6267
6268 /* If we found what we wanted, we keep going; otherwise, we're
6269 done. */
6270 if (!cp_parser_parse_definitely (parser))
6271 {
6272 bool error_p = false;
6273
6274 /* Restore the OLD_SCOPE since it was valid before the
6275 failed attempt at finding the last
6276 class-or-namespace-name. */
6277 parser->scope = old_scope;
6278 parser->qualifying_scope = saved_qualifying_scope;
6279
6280 /* If the next token is a decltype, and the one after that is a
6281 `::', then the decltype has failed to resolve to a class or
6282 enumeration type. Give this error even when parsing
6283 tentatively since it can't possibly be valid--and we're going
6284 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6285 won't get another chance.*/
6286 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6287 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6288 == CPP_SCOPE))
6289 {
6290 token = cp_lexer_consume_token (parser->lexer);
6291 error_at (token->location, "decltype evaluates to %qT, "
6292 "which is not a class or enumeration type",
6293 token->u.tree_check_value->value);
6294 parser->scope = error_mark_node;
6295 error_p = true;
6296 /* As below. */
6297 success = true;
6298 cp_lexer_consume_token (parser->lexer);
6299 }
6300
6301 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6302 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6303 {
6304 /* If we have a non-type template-id followed by ::, it can't
6305 possibly be valid. */
6306 token = cp_lexer_peek_token (parser->lexer);
6307 tree tid = token->u.tree_check_value->value;
6308 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6309 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6310 {
6311 tree tmpl = NULL_TREE;
6312 if (is_overloaded_fn (tid))
6313 {
6314 tree fns = get_fns (tid);
6315 if (OVL_SINGLE_P (fns))
6316 tmpl = OVL_FIRST (fns);
6317 error_at (token->location, "function template-id %qD "
6318 "in nested-name-specifier", tid);
6319 }
6320 else
6321 {
6322 /* Variable template. */
6323 tmpl = TREE_OPERAND (tid, 0);
6324 gcc_assert (variable_template_p (tmpl));
6325 error_at (token->location, "variable template-id %qD "
6326 "in nested-name-specifier", tid);
6327 }
6328 if (tmpl)
6329 inform (DECL_SOURCE_LOCATION (tmpl),
6330 "%qD declared here", tmpl);
6331
6332 parser->scope = error_mark_node;
6333 error_p = true;
6334 /* As below. */
6335 success = true;
6336 cp_lexer_consume_token (parser->lexer);
6337 cp_lexer_consume_token (parser->lexer);
6338 }
6339 }
6340
6341 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6342 break;
6343 /* If the next token is an identifier, and the one after
6344 that is a `::', then any valid interpretation would have
6345 found a class-or-namespace-name. */
6346 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6347 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6348 == CPP_SCOPE)
6349 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6350 != CPP_COMPL))
6351 {
6352 token = cp_lexer_consume_token (parser->lexer);
6353 if (!error_p)
6354 {
6355 if (!token->error_reported)
6356 {
6357 tree decl;
6358 tree ambiguous_decls;
6359
6360 decl = cp_parser_lookup_name (parser, token->u.value,
6361 none_type,
6362 /*is_template=*/false,
6363 /*is_namespace=*/false,
6364 /*check_dependency=*/true,
6365 &ambiguous_decls,
6366 token->location);
6367 if (TREE_CODE (decl) == TEMPLATE_DECL)
6368 error_at (token->location,
6369 "%qD used without template parameters",
6370 decl);
6371 else if (ambiguous_decls)
6372 {
6373 // cp_parser_lookup_name has the same diagnostic,
6374 // thus make sure to emit it at most once.
6375 if (cp_parser_uncommitted_to_tentative_parse_p
6376 (parser))
6377 {
6378 error_at (token->location,
6379 "reference to %qD is ambiguous",
6380 token->u.value);
6381 print_candidates (ambiguous_decls);
6382 }
6383 decl = error_mark_node;
6384 }
6385 else
6386 {
6387 if (cxx_dialect != cxx98)
6388 cp_parser_name_lookup_error
6389 (parser, token->u.value, decl, NLE_NOT_CXX98,
6390 token->location);
6391 else
6392 cp_parser_name_lookup_error
6393 (parser, token->u.value, decl, NLE_CXX98,
6394 token->location);
6395 }
6396 }
6397 parser->scope = error_mark_node;
6398 error_p = true;
6399 /* Treat this as a successful nested-name-specifier
6400 due to:
6401
6402 [basic.lookup.qual]
6403
6404 If the name found is not a class-name (clause
6405 _class_) or namespace-name (_namespace.def_), the
6406 program is ill-formed. */
6407 success = true;
6408 }
6409 cp_lexer_consume_token (parser->lexer);
6410 }
6411 break;
6412 }
6413 /* We've found one valid nested-name-specifier. */
6414 success = true;
6415 /* Name lookup always gives us a DECL. */
6416 if (TREE_CODE (new_scope) == TYPE_DECL)
6417 new_scope = TREE_TYPE (new_scope);
6418 /* Uses of "template" must be followed by actual templates. */
6419 if (template_keyword_p
6420 && !(CLASS_TYPE_P (new_scope)
6421 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6422 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6423 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6424 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6425 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6426 == TEMPLATE_ID_EXPR)))
6427 permerror (input_location, TYPE_P (new_scope)
6428 ? G_("%qT is not a template")
6429 : G_("%qD is not a template"),
6430 new_scope);
6431 /* If it is a class scope, try to complete it; we are about to
6432 be looking up names inside the class. */
6433 if (TYPE_P (new_scope)
6434 /* Since checking types for dependency can be expensive,
6435 avoid doing it if the type is already complete. */
6436 && !COMPLETE_TYPE_P (new_scope)
6437 /* Do not try to complete dependent types. */
6438 && !dependent_type_p (new_scope))
6439 {
6440 new_scope = complete_type (new_scope);
6441 /* If it is a typedef to current class, use the current
6442 class instead, as the typedef won't have any names inside
6443 it yet. */
6444 if (!COMPLETE_TYPE_P (new_scope)
6445 && currently_open_class (new_scope))
6446 new_scope = TYPE_MAIN_VARIANT (new_scope);
6447 }
6448 /* Make sure we look in the right scope the next time through
6449 the loop. */
6450 parser->scope = new_scope;
6451 }
6452
6453 /* If parsing tentatively, replace the sequence of tokens that makes
6454 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6455 token. That way, should we re-parse the token stream, we will
6456 not have to repeat the effort required to do the parse, nor will
6457 we issue duplicate error messages. */
6458 if (success && start)
6459 {
6460 cp_token *token;
6461
6462 token = cp_lexer_token_at (parser->lexer, start);
6463 /* Reset the contents of the START token. */
6464 token->type = CPP_NESTED_NAME_SPECIFIER;
6465 /* Retrieve any deferred checks. Do not pop this access checks yet
6466 so the memory will not be reclaimed during token replacing below. */
6467 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6468 token->u.tree_check_value->value = parser->scope;
6469 token->u.tree_check_value->checks = get_deferred_access_checks ();
6470 token->u.tree_check_value->qualifying_scope =
6471 parser->qualifying_scope;
6472 token->keyword = RID_MAX;
6473
6474 /* Purge all subsequent tokens. */
6475 cp_lexer_purge_tokens_after (parser->lexer, start);
6476 }
6477
6478 if (start)
6479 pop_to_parent_deferring_access_checks ();
6480
6481 return success ? parser->scope : NULL_TREE;
6482 }
6483
6484 /* Parse a nested-name-specifier. See
6485 cp_parser_nested_name_specifier_opt for details. This function
6486 behaves identically, except that it will an issue an error if no
6487 nested-name-specifier is present. */
6488
6489 static tree
6490 cp_parser_nested_name_specifier (cp_parser *parser,
6491 bool typename_keyword_p,
6492 bool check_dependency_p,
6493 bool type_p,
6494 bool is_declaration)
6495 {
6496 tree scope;
6497
6498 /* Look for the nested-name-specifier. */
6499 scope = cp_parser_nested_name_specifier_opt (parser,
6500 typename_keyword_p,
6501 check_dependency_p,
6502 type_p,
6503 is_declaration);
6504 /* If it was not present, issue an error message. */
6505 if (!scope)
6506 {
6507 cp_parser_error (parser, "expected nested-name-specifier");
6508 parser->scope = NULL_TREE;
6509 }
6510
6511 return scope;
6512 }
6513
6514 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6515 this is either a class-name or a namespace-name (which corresponds
6516 to the class-or-namespace-name production in the grammar). For
6517 C++0x, it can also be a type-name that refers to an enumeration
6518 type or a simple-template-id.
6519
6520 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6521 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6522 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6523 TYPE_P is TRUE iff the next name should be taken as a class-name,
6524 even the same name is declared to be another entity in the same
6525 scope.
6526
6527 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6528 specified by the class-or-namespace-name. If neither is found the
6529 ERROR_MARK_NODE is returned. */
6530
6531 static tree
6532 cp_parser_qualifying_entity (cp_parser *parser,
6533 bool typename_keyword_p,
6534 bool template_keyword_p,
6535 bool check_dependency_p,
6536 bool type_p,
6537 bool is_declaration)
6538 {
6539 tree saved_scope;
6540 tree saved_qualifying_scope;
6541 tree saved_object_scope;
6542 tree scope;
6543 bool only_class_p;
6544 bool successful_parse_p;
6545
6546 /* DR 743: decltype can appear in a nested-name-specifier. */
6547 if (cp_lexer_next_token_is_decltype (parser->lexer))
6548 {
6549 scope = cp_parser_decltype (parser);
6550 if (TREE_CODE (scope) != ENUMERAL_TYPE
6551 && !MAYBE_CLASS_TYPE_P (scope))
6552 {
6553 cp_parser_simulate_error (parser);
6554 return error_mark_node;
6555 }
6556 if (TYPE_NAME (scope))
6557 scope = TYPE_NAME (scope);
6558 return scope;
6559 }
6560
6561 /* Before we try to parse the class-name, we must save away the
6562 current PARSER->SCOPE since cp_parser_class_name will destroy
6563 it. */
6564 saved_scope = parser->scope;
6565 saved_qualifying_scope = parser->qualifying_scope;
6566 saved_object_scope = parser->object_scope;
6567 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6568 there is no need to look for a namespace-name. */
6569 only_class_p = template_keyword_p
6570 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6571 if (!only_class_p)
6572 cp_parser_parse_tentatively (parser);
6573 scope = cp_parser_class_name (parser,
6574 typename_keyword_p,
6575 template_keyword_p,
6576 type_p ? class_type : none_type,
6577 check_dependency_p,
6578 /*class_head_p=*/false,
6579 is_declaration,
6580 /*enum_ok=*/cxx_dialect > cxx98);
6581 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6582 /* If that didn't work, try for a namespace-name. */
6583 if (!only_class_p && !successful_parse_p)
6584 {
6585 /* Restore the saved scope. */
6586 parser->scope = saved_scope;
6587 parser->qualifying_scope = saved_qualifying_scope;
6588 parser->object_scope = saved_object_scope;
6589 /* If we are not looking at an identifier followed by the scope
6590 resolution operator, then this is not part of a
6591 nested-name-specifier. (Note that this function is only used
6592 to parse the components of a nested-name-specifier.) */
6593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6594 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6595 return error_mark_node;
6596 scope = cp_parser_namespace_name (parser);
6597 }
6598
6599 return scope;
6600 }
6601
6602 /* Return true if we are looking at a compound-literal, false otherwise. */
6603
6604 static bool
6605 cp_parser_compound_literal_p (cp_parser *parser)
6606 {
6607 cp_lexer_save_tokens (parser->lexer);
6608
6609 /* Skip tokens until the next token is a closing parenthesis.
6610 If we find the closing `)', and the next token is a `{', then
6611 we are looking at a compound-literal. */
6612 bool compound_literal_p
6613 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6614 /*consume_paren=*/true)
6615 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6616
6617 /* Roll back the tokens we skipped. */
6618 cp_lexer_rollback_tokens (parser->lexer);
6619
6620 return compound_literal_p;
6621 }
6622
6623 /* Parse a postfix-expression.
6624
6625 postfix-expression:
6626 primary-expression
6627 postfix-expression [ expression ]
6628 postfix-expression ( expression-list [opt] )
6629 simple-type-specifier ( expression-list [opt] )
6630 typename :: [opt] nested-name-specifier identifier
6631 ( expression-list [opt] )
6632 typename :: [opt] nested-name-specifier template [opt] template-id
6633 ( expression-list [opt] )
6634 postfix-expression . template [opt] id-expression
6635 postfix-expression -> template [opt] id-expression
6636 postfix-expression . pseudo-destructor-name
6637 postfix-expression -> pseudo-destructor-name
6638 postfix-expression ++
6639 postfix-expression --
6640 dynamic_cast < type-id > ( expression )
6641 static_cast < type-id > ( expression )
6642 reinterpret_cast < type-id > ( expression )
6643 const_cast < type-id > ( expression )
6644 typeid ( expression )
6645 typeid ( type-id )
6646
6647 GNU Extension:
6648
6649 postfix-expression:
6650 ( type-id ) { initializer-list , [opt] }
6651
6652 This extension is a GNU version of the C99 compound-literal
6653 construct. (The C99 grammar uses `type-name' instead of `type-id',
6654 but they are essentially the same concept.)
6655
6656 If ADDRESS_P is true, the postfix expression is the operand of the
6657 `&' operator. CAST_P is true if this expression is the target of a
6658 cast.
6659
6660 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6661 class member access expressions [expr.ref].
6662
6663 Returns a representation of the expression. */
6664
6665 static cp_expr
6666 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6667 bool member_access_only_p, bool decltype_p,
6668 cp_id_kind * pidk_return)
6669 {
6670 cp_token *token;
6671 location_t loc;
6672 enum rid keyword;
6673 cp_id_kind idk = CP_ID_KIND_NONE;
6674 cp_expr postfix_expression = NULL_TREE;
6675 bool is_member_access = false;
6676
6677 /* Peek at the next token. */
6678 token = cp_lexer_peek_token (parser->lexer);
6679 loc = token->location;
6680 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6681
6682 /* Some of the productions are determined by keywords. */
6683 keyword = token->keyword;
6684 switch (keyword)
6685 {
6686 case RID_DYNCAST:
6687 case RID_STATCAST:
6688 case RID_REINTCAST:
6689 case RID_CONSTCAST:
6690 {
6691 tree type;
6692 cp_expr expression;
6693 const char *saved_message;
6694 bool saved_in_type_id_in_expr_p;
6695
6696 /* All of these can be handled in the same way from the point
6697 of view of parsing. Begin by consuming the token
6698 identifying the cast. */
6699 cp_lexer_consume_token (parser->lexer);
6700
6701 /* New types cannot be defined in the cast. */
6702 saved_message = parser->type_definition_forbidden_message;
6703 parser->type_definition_forbidden_message
6704 = G_("types may not be defined in casts");
6705
6706 /* Look for the opening `<'. */
6707 cp_parser_require (parser, CPP_LESS, RT_LESS);
6708 /* Parse the type to which we are casting. */
6709 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6710 parser->in_type_id_in_expr_p = true;
6711 type = cp_parser_type_id (parser);
6712 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6713 /* Look for the closing `>'. */
6714 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6715 /* Restore the old message. */
6716 parser->type_definition_forbidden_message = saved_message;
6717
6718 bool saved_greater_than_is_operator_p
6719 = parser->greater_than_is_operator_p;
6720 parser->greater_than_is_operator_p = true;
6721
6722 /* And the expression which is being cast. */
6723 matching_parens parens;
6724 parens.require_open (parser);
6725 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6726 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6727 RT_CLOSE_PAREN);
6728 location_t end_loc = close_paren ?
6729 close_paren->location : UNKNOWN_LOCATION;
6730
6731 parser->greater_than_is_operator_p
6732 = saved_greater_than_is_operator_p;
6733
6734 /* Only type conversions to integral or enumeration types
6735 can be used in constant-expressions. */
6736 if (!cast_valid_in_integral_constant_expression_p (type)
6737 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6738 {
6739 postfix_expression = error_mark_node;
6740 break;
6741 }
6742
6743 switch (keyword)
6744 {
6745 case RID_DYNCAST:
6746 postfix_expression
6747 = build_dynamic_cast (type, expression, tf_warning_or_error);
6748 break;
6749 case RID_STATCAST:
6750 postfix_expression
6751 = build_static_cast (type, expression, tf_warning_or_error);
6752 break;
6753 case RID_REINTCAST:
6754 postfix_expression
6755 = build_reinterpret_cast (type, expression,
6756 tf_warning_or_error);
6757 break;
6758 case RID_CONSTCAST:
6759 postfix_expression
6760 = build_const_cast (type, expression, tf_warning_or_error);
6761 break;
6762 default:
6763 gcc_unreachable ();
6764 }
6765
6766 /* Construct a location e.g. :
6767 reinterpret_cast <int *> (expr)
6768 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6769 ranging from the start of the "*_cast" token to the final closing
6770 paren, with the caret at the start. */
6771 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6772 postfix_expression.set_location (cp_cast_loc);
6773 }
6774 break;
6775
6776 case RID_TYPEID:
6777 {
6778 tree type;
6779 const char *saved_message;
6780 bool saved_in_type_id_in_expr_p;
6781
6782 /* Consume the `typeid' token. */
6783 cp_lexer_consume_token (parser->lexer);
6784 /* Look for the `(' token. */
6785 matching_parens parens;
6786 parens.require_open (parser);
6787 /* Types cannot be defined in a `typeid' expression. */
6788 saved_message = parser->type_definition_forbidden_message;
6789 parser->type_definition_forbidden_message
6790 = G_("types may not be defined in a %<typeid%> expression");
6791 /* We can't be sure yet whether we're looking at a type-id or an
6792 expression. */
6793 cp_parser_parse_tentatively (parser);
6794 /* Try a type-id first. */
6795 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6796 parser->in_type_id_in_expr_p = true;
6797 type = cp_parser_type_id (parser);
6798 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6799 /* Look for the `)' token. Otherwise, we can't be sure that
6800 we're not looking at an expression: consider `typeid (int
6801 (3))', for example. */
6802 cp_token *close_paren = parens.require_close (parser);
6803 /* If all went well, simply lookup the type-id. */
6804 if (cp_parser_parse_definitely (parser))
6805 postfix_expression = get_typeid (type, tf_warning_or_error);
6806 /* Otherwise, fall back to the expression variant. */
6807 else
6808 {
6809 tree expression;
6810
6811 /* Look for an expression. */
6812 expression = cp_parser_expression (parser, & idk);
6813 /* Compute its typeid. */
6814 postfix_expression = build_typeid (expression, tf_warning_or_error);
6815 /* Look for the `)' token. */
6816 close_paren = parens.require_close (parser);
6817 }
6818 /* Restore the saved message. */
6819 parser->type_definition_forbidden_message = saved_message;
6820 /* `typeid' may not appear in an integral constant expression. */
6821 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6822 postfix_expression = error_mark_node;
6823
6824 /* Construct a location e.g. :
6825 typeid (expr)
6826 ^~~~~~~~~~~~~
6827 ranging from the start of the "typeid" token to the final closing
6828 paren, with the caret at the start. */
6829 if (close_paren)
6830 {
6831 location_t typeid_loc
6832 = make_location (start_loc, start_loc, close_paren->location);
6833 postfix_expression.set_location (typeid_loc);
6834 }
6835 }
6836 break;
6837
6838 case RID_TYPENAME:
6839 {
6840 tree type;
6841 /* The syntax permitted here is the same permitted for an
6842 elaborated-type-specifier. */
6843 ++parser->prevent_constrained_type_specifiers;
6844 type = cp_parser_elaborated_type_specifier (parser,
6845 /*is_friend=*/false,
6846 /*is_declaration=*/false);
6847 --parser->prevent_constrained_type_specifiers;
6848 postfix_expression = cp_parser_functional_cast (parser, type);
6849 }
6850 break;
6851
6852 case RID_ADDRESSOF:
6853 case RID_BUILTIN_SHUFFLE:
6854 case RID_BUILTIN_LAUNDER:
6855 {
6856 vec<tree, va_gc> *vec;
6857 unsigned int i;
6858 tree p;
6859
6860 cp_lexer_consume_token (parser->lexer);
6861 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6862 /*cast_p=*/false, /*allow_expansion_p=*/true,
6863 /*non_constant_p=*/NULL);
6864 if (vec == NULL)
6865 {
6866 postfix_expression = error_mark_node;
6867 break;
6868 }
6869
6870 FOR_EACH_VEC_ELT (*vec, i, p)
6871 mark_exp_read (p);
6872
6873 switch (keyword)
6874 {
6875 case RID_ADDRESSOF:
6876 if (vec->length () == 1)
6877 postfix_expression
6878 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6879 else
6880 {
6881 error_at (loc, "wrong number of arguments to "
6882 "%<__builtin_addressof%>");
6883 postfix_expression = error_mark_node;
6884 }
6885 break;
6886
6887 case RID_BUILTIN_LAUNDER:
6888 if (vec->length () == 1)
6889 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6890 tf_warning_or_error);
6891 else
6892 {
6893 error_at (loc, "wrong number of arguments to "
6894 "%<__builtin_launder%>");
6895 postfix_expression = error_mark_node;
6896 }
6897 break;
6898
6899 case RID_BUILTIN_SHUFFLE:
6900 if (vec->length () == 2)
6901 postfix_expression
6902 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6903 (*vec)[1], tf_warning_or_error);
6904 else if (vec->length () == 3)
6905 postfix_expression
6906 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6907 (*vec)[2], tf_warning_or_error);
6908 else
6909 {
6910 error_at (loc, "wrong number of arguments to "
6911 "%<__builtin_shuffle%>");
6912 postfix_expression = error_mark_node;
6913 }
6914 break;
6915
6916 default:
6917 gcc_unreachable ();
6918 }
6919 break;
6920 }
6921
6922 default:
6923 {
6924 tree type;
6925
6926 /* If the next thing is a simple-type-specifier, we may be
6927 looking at a functional cast. We could also be looking at
6928 an id-expression. So, we try the functional cast, and if
6929 that doesn't work we fall back to the primary-expression. */
6930 cp_parser_parse_tentatively (parser);
6931 /* Look for the simple-type-specifier. */
6932 ++parser->prevent_constrained_type_specifiers;
6933 type = cp_parser_simple_type_specifier (parser,
6934 /*decl_specs=*/NULL,
6935 CP_PARSER_FLAGS_NONE);
6936 --parser->prevent_constrained_type_specifiers;
6937 /* Parse the cast itself. */
6938 if (!cp_parser_error_occurred (parser))
6939 postfix_expression
6940 = cp_parser_functional_cast (parser, type);
6941 /* If that worked, we're done. */
6942 if (cp_parser_parse_definitely (parser))
6943 break;
6944
6945 /* If the functional-cast didn't work out, try a
6946 compound-literal. */
6947 if (cp_parser_allow_gnu_extensions_p (parser)
6948 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6949 {
6950 cp_expr initializer = NULL_TREE;
6951
6952 cp_parser_parse_tentatively (parser);
6953
6954 matching_parens parens;
6955 parens.consume_open (parser);
6956
6957 /* Avoid calling cp_parser_type_id pointlessly, see comment
6958 in cp_parser_cast_expression about c++/29234. */
6959 if (!cp_parser_compound_literal_p (parser))
6960 cp_parser_simulate_error (parser);
6961 else
6962 {
6963 /* Parse the type. */
6964 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6965 parser->in_type_id_in_expr_p = true;
6966 type = cp_parser_type_id (parser);
6967 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6968 parens.require_close (parser);
6969 }
6970
6971 /* If things aren't going well, there's no need to
6972 keep going. */
6973 if (!cp_parser_error_occurred (parser))
6974 {
6975 bool non_constant_p;
6976 /* Parse the brace-enclosed initializer list. */
6977 initializer = cp_parser_braced_list (parser,
6978 &non_constant_p);
6979 }
6980 /* If that worked, we're definitely looking at a
6981 compound-literal expression. */
6982 if (cp_parser_parse_definitely (parser))
6983 {
6984 /* Warn the user that a compound literal is not
6985 allowed in standard C++. */
6986 pedwarn (input_location, OPT_Wpedantic,
6987 "ISO C++ forbids compound-literals");
6988 /* For simplicity, we disallow compound literals in
6989 constant-expressions. We could
6990 allow compound literals of integer type, whose
6991 initializer was a constant, in constant
6992 expressions. Permitting that usage, as a further
6993 extension, would not change the meaning of any
6994 currently accepted programs. (Of course, as
6995 compound literals are not part of ISO C++, the
6996 standard has nothing to say.) */
6997 if (cp_parser_non_integral_constant_expression (parser,
6998 NIC_NCC))
6999 {
7000 postfix_expression = error_mark_node;
7001 break;
7002 }
7003 /* Form the representation of the compound-literal. */
7004 postfix_expression
7005 = finish_compound_literal (type, initializer,
7006 tf_warning_or_error, fcl_c99);
7007 postfix_expression.set_location (initializer.get_location ());
7008 break;
7009 }
7010 }
7011
7012 /* It must be a primary-expression. */
7013 postfix_expression
7014 = cp_parser_primary_expression (parser, address_p, cast_p,
7015 /*template_arg_p=*/false,
7016 decltype_p,
7017 &idk);
7018 }
7019 break;
7020 }
7021
7022 /* Note that we don't need to worry about calling build_cplus_new on a
7023 class-valued CALL_EXPR in decltype when it isn't the end of the
7024 postfix-expression; unary_complex_lvalue will take care of that for
7025 all these cases. */
7026
7027 /* Keep looping until the postfix-expression is complete. */
7028 while (true)
7029 {
7030 if (idk == CP_ID_KIND_UNQUALIFIED
7031 && identifier_p (postfix_expression)
7032 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7033 /* It is not a Koenig lookup function call. */
7034 postfix_expression
7035 = unqualified_name_lookup_error (postfix_expression);
7036
7037 /* Peek at the next token. */
7038 token = cp_lexer_peek_token (parser->lexer);
7039
7040 switch (token->type)
7041 {
7042 case CPP_OPEN_SQUARE:
7043 if (cp_next_tokens_can_be_std_attribute_p (parser))
7044 {
7045 cp_parser_error (parser,
7046 "two consecutive %<[%> shall "
7047 "only introduce an attribute");
7048 return error_mark_node;
7049 }
7050 postfix_expression
7051 = cp_parser_postfix_open_square_expression (parser,
7052 postfix_expression,
7053 false,
7054 decltype_p);
7055 postfix_expression.set_range (start_loc,
7056 postfix_expression.get_location ());
7057
7058 idk = CP_ID_KIND_NONE;
7059 is_member_access = false;
7060 break;
7061
7062 case CPP_OPEN_PAREN:
7063 /* postfix-expression ( expression-list [opt] ) */
7064 {
7065 bool koenig_p;
7066 bool is_builtin_constant_p;
7067 bool saved_integral_constant_expression_p = false;
7068 bool saved_non_integral_constant_expression_p = false;
7069 tsubst_flags_t complain = complain_flags (decltype_p);
7070 vec<tree, va_gc> *args;
7071 location_t close_paren_loc = UNKNOWN_LOCATION;
7072
7073 is_member_access = false;
7074
7075 is_builtin_constant_p
7076 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7077 if (is_builtin_constant_p)
7078 {
7079 /* The whole point of __builtin_constant_p is to allow
7080 non-constant expressions to appear as arguments. */
7081 saved_integral_constant_expression_p
7082 = parser->integral_constant_expression_p;
7083 saved_non_integral_constant_expression_p
7084 = parser->non_integral_constant_expression_p;
7085 parser->integral_constant_expression_p = false;
7086 }
7087 args = (cp_parser_parenthesized_expression_list
7088 (parser, non_attr,
7089 /*cast_p=*/false, /*allow_expansion_p=*/true,
7090 /*non_constant_p=*/NULL,
7091 /*close_paren_loc=*/&close_paren_loc));
7092 if (is_builtin_constant_p)
7093 {
7094 parser->integral_constant_expression_p
7095 = saved_integral_constant_expression_p;
7096 parser->non_integral_constant_expression_p
7097 = saved_non_integral_constant_expression_p;
7098 }
7099
7100 if (args == NULL)
7101 {
7102 postfix_expression = error_mark_node;
7103 break;
7104 }
7105
7106 /* Function calls are not permitted in
7107 constant-expressions. */
7108 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7109 && cp_parser_non_integral_constant_expression (parser,
7110 NIC_FUNC_CALL))
7111 {
7112 postfix_expression = error_mark_node;
7113 release_tree_vector (args);
7114 break;
7115 }
7116
7117 koenig_p = false;
7118 if (idk == CP_ID_KIND_UNQUALIFIED
7119 || idk == CP_ID_KIND_TEMPLATE_ID)
7120 {
7121 if (identifier_p (postfix_expression))
7122 {
7123 if (!args->is_empty ())
7124 {
7125 koenig_p = true;
7126 if (!any_type_dependent_arguments_p (args))
7127 postfix_expression
7128 = perform_koenig_lookup (postfix_expression, args,
7129 complain);
7130 }
7131 else
7132 postfix_expression
7133 = unqualified_fn_lookup_error (postfix_expression);
7134 }
7135 /* We do not perform argument-dependent lookup if
7136 normal lookup finds a non-function, in accordance
7137 with the expected resolution of DR 218. */
7138 else if (!args->is_empty ()
7139 && is_overloaded_fn (postfix_expression))
7140 {
7141 tree fn = get_first_fn (postfix_expression);
7142 fn = STRIP_TEMPLATE (fn);
7143
7144 /* Do not do argument dependent lookup if regular
7145 lookup finds a member function or a block-scope
7146 function declaration. [basic.lookup.argdep]/3 */
7147 if (!DECL_FUNCTION_MEMBER_P (fn)
7148 && !DECL_LOCAL_FUNCTION_P (fn))
7149 {
7150 koenig_p = true;
7151 if (!any_type_dependent_arguments_p (args))
7152 postfix_expression
7153 = perform_koenig_lookup (postfix_expression, args,
7154 complain);
7155 }
7156 }
7157 }
7158
7159 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7160 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7161 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7162 && vec_safe_length (args) == 3)
7163 {
7164 tree arg0 = (*args)[0];
7165 tree arg1 = (*args)[1];
7166 tree arg2 = (*args)[2];
7167 int literal_mask = ((!!integer_zerop (arg1) << 1)
7168 | (!!integer_zerop (arg2) << 2));
7169 if (TREE_CODE (arg2) == CONST_DECL)
7170 arg2 = DECL_INITIAL (arg2);
7171 warn_for_memset (input_location, arg0, arg2, literal_mask);
7172 }
7173
7174 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7175 {
7176 tree instance = TREE_OPERAND (postfix_expression, 0);
7177 tree fn = TREE_OPERAND (postfix_expression, 1);
7178
7179 if (processing_template_decl
7180 && (type_dependent_object_expression_p (instance)
7181 || (!BASELINK_P (fn)
7182 && TREE_CODE (fn) != FIELD_DECL)
7183 || type_dependent_expression_p (fn)
7184 || any_type_dependent_arguments_p (args)))
7185 {
7186 maybe_generic_this_capture (instance, fn);
7187 postfix_expression
7188 = build_min_nt_call_vec (postfix_expression, args);
7189 release_tree_vector (args);
7190 break;
7191 }
7192
7193 if (BASELINK_P (fn))
7194 {
7195 postfix_expression
7196 = (build_new_method_call
7197 (instance, fn, &args, NULL_TREE,
7198 (idk == CP_ID_KIND_QUALIFIED
7199 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7200 : LOOKUP_NORMAL),
7201 /*fn_p=*/NULL,
7202 complain));
7203 }
7204 else
7205 postfix_expression
7206 = finish_call_expr (postfix_expression, &args,
7207 /*disallow_virtual=*/false,
7208 /*koenig_p=*/false,
7209 complain);
7210 }
7211 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7212 || TREE_CODE (postfix_expression) == MEMBER_REF
7213 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7214 postfix_expression = (build_offset_ref_call_from_tree
7215 (postfix_expression, &args,
7216 complain));
7217 else if (idk == CP_ID_KIND_QUALIFIED)
7218 /* A call to a static class member, or a namespace-scope
7219 function. */
7220 postfix_expression
7221 = finish_call_expr (postfix_expression, &args,
7222 /*disallow_virtual=*/true,
7223 koenig_p,
7224 complain);
7225 else
7226 /* All other function calls. */
7227 postfix_expression
7228 = finish_call_expr (postfix_expression, &args,
7229 /*disallow_virtual=*/false,
7230 koenig_p,
7231 complain);
7232
7233 if (close_paren_loc != UNKNOWN_LOCATION)
7234 {
7235 location_t combined_loc = make_location (token->location,
7236 start_loc,
7237 close_paren_loc);
7238 postfix_expression.set_location (combined_loc);
7239 }
7240
7241 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7242 idk = CP_ID_KIND_NONE;
7243
7244 release_tree_vector (args);
7245 }
7246 break;
7247
7248 case CPP_DOT:
7249 case CPP_DEREF:
7250 /* postfix-expression . template [opt] id-expression
7251 postfix-expression . pseudo-destructor-name
7252 postfix-expression -> template [opt] id-expression
7253 postfix-expression -> pseudo-destructor-name */
7254
7255 /* Consume the `.' or `->' operator. */
7256 cp_lexer_consume_token (parser->lexer);
7257
7258 postfix_expression
7259 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7260 postfix_expression,
7261 false, &idk, loc);
7262
7263 is_member_access = true;
7264 break;
7265
7266 case CPP_PLUS_PLUS:
7267 /* postfix-expression ++ */
7268 /* Consume the `++' token. */
7269 cp_lexer_consume_token (parser->lexer);
7270 /* Generate a representation for the complete expression. */
7271 postfix_expression
7272 = finish_increment_expr (postfix_expression,
7273 POSTINCREMENT_EXPR);
7274 /* Increments may not appear in constant-expressions. */
7275 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7276 postfix_expression = error_mark_node;
7277 idk = CP_ID_KIND_NONE;
7278 is_member_access = false;
7279 break;
7280
7281 case CPP_MINUS_MINUS:
7282 /* postfix-expression -- */
7283 /* Consume the `--' token. */
7284 cp_lexer_consume_token (parser->lexer);
7285 /* Generate a representation for the complete expression. */
7286 postfix_expression
7287 = finish_increment_expr (postfix_expression,
7288 POSTDECREMENT_EXPR);
7289 /* Decrements may not appear in constant-expressions. */
7290 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7291 postfix_expression = error_mark_node;
7292 idk = CP_ID_KIND_NONE;
7293 is_member_access = false;
7294 break;
7295
7296 default:
7297 if (pidk_return != NULL)
7298 * pidk_return = idk;
7299 if (member_access_only_p)
7300 return is_member_access
7301 ? postfix_expression
7302 : cp_expr (error_mark_node);
7303 else
7304 return postfix_expression;
7305 }
7306 }
7307
7308 /* We should never get here. */
7309 gcc_unreachable ();
7310 return error_mark_node;
7311 }
7312
7313 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7314 by cp_parser_builtin_offsetof. We're looking for
7315
7316 postfix-expression [ expression ]
7317 postfix-expression [ braced-init-list ] (C++11)
7318
7319 FOR_OFFSETOF is set if we're being called in that context, which
7320 changes how we deal with integer constant expressions. */
7321
7322 static tree
7323 cp_parser_postfix_open_square_expression (cp_parser *parser,
7324 tree postfix_expression,
7325 bool for_offsetof,
7326 bool decltype_p)
7327 {
7328 tree index = NULL_TREE;
7329 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7330 bool saved_greater_than_is_operator_p;
7331
7332 /* Consume the `[' token. */
7333 cp_lexer_consume_token (parser->lexer);
7334
7335 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7336 parser->greater_than_is_operator_p = true;
7337
7338 /* Parse the index expression. */
7339 /* ??? For offsetof, there is a question of what to allow here. If
7340 offsetof is not being used in an integral constant expression context,
7341 then we *could* get the right answer by computing the value at runtime.
7342 If we are in an integral constant expression context, then we might
7343 could accept any constant expression; hard to say without analysis.
7344 Rather than open the barn door too wide right away, allow only integer
7345 constant expressions here. */
7346 if (for_offsetof)
7347 index = cp_parser_constant_expression (parser);
7348 else
7349 {
7350 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7351 {
7352 bool expr_nonconst_p;
7353 cp_lexer_set_source_position (parser->lexer);
7354 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7355 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7356 }
7357 else
7358 index = cp_parser_expression (parser);
7359 }
7360
7361 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7362
7363 /* Look for the closing `]'. */
7364 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7365
7366 /* Build the ARRAY_REF. */
7367 postfix_expression = grok_array_decl (loc, postfix_expression,
7368 index, decltype_p);
7369
7370 /* When not doing offsetof, array references are not permitted in
7371 constant-expressions. */
7372 if (!for_offsetof
7373 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7374 postfix_expression = error_mark_node;
7375
7376 return postfix_expression;
7377 }
7378
7379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7380 by cp_parser_builtin_offsetof. We're looking for
7381
7382 postfix-expression . template [opt] id-expression
7383 postfix-expression . pseudo-destructor-name
7384 postfix-expression -> template [opt] id-expression
7385 postfix-expression -> pseudo-destructor-name
7386
7387 FOR_OFFSETOF is set if we're being called in that context. That sorta
7388 limits what of the above we'll actually accept, but nevermind.
7389 TOKEN_TYPE is the "." or "->" token, which will already have been
7390 removed from the stream. */
7391
7392 static tree
7393 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7394 enum cpp_ttype token_type,
7395 cp_expr postfix_expression,
7396 bool for_offsetof, cp_id_kind *idk,
7397 location_t location)
7398 {
7399 tree name;
7400 bool dependent_p;
7401 bool pseudo_destructor_p;
7402 tree scope = NULL_TREE;
7403 location_t start_loc = postfix_expression.get_start ();
7404
7405 /* If this is a `->' operator, dereference the pointer. */
7406 if (token_type == CPP_DEREF)
7407 postfix_expression = build_x_arrow (location, postfix_expression,
7408 tf_warning_or_error);
7409 /* Check to see whether or not the expression is type-dependent and
7410 not the current instantiation. */
7411 dependent_p = type_dependent_object_expression_p (postfix_expression);
7412 /* The identifier following the `->' or `.' is not qualified. */
7413 parser->scope = NULL_TREE;
7414 parser->qualifying_scope = NULL_TREE;
7415 parser->object_scope = NULL_TREE;
7416 *idk = CP_ID_KIND_NONE;
7417
7418 /* Enter the scope corresponding to the type of the object
7419 given by the POSTFIX_EXPRESSION. */
7420 if (!dependent_p)
7421 {
7422 scope = TREE_TYPE (postfix_expression);
7423 /* According to the standard, no expression should ever have
7424 reference type. Unfortunately, we do not currently match
7425 the standard in this respect in that our internal representation
7426 of an expression may have reference type even when the standard
7427 says it does not. Therefore, we have to manually obtain the
7428 underlying type here. */
7429 scope = non_reference (scope);
7430 /* The type of the POSTFIX_EXPRESSION must be complete. */
7431 /* Unlike the object expression in other contexts, *this is not
7432 required to be of complete type for purposes of class member
7433 access (5.2.5) outside the member function body. */
7434 if (postfix_expression != current_class_ref
7435 && scope != error_mark_node
7436 && !(processing_template_decl
7437 && current_class_type
7438 && (same_type_ignoring_top_level_qualifiers_p
7439 (scope, current_class_type))))
7440 {
7441 scope = complete_type (scope);
7442 if (!COMPLETE_TYPE_P (scope)
7443 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7444 && EXPR_P (postfix_expression))
7445 {
7446 /* In a template, be permissive by treating an object expression
7447 of incomplete type as dependent (after a pedwarn). */
7448 diagnostic_t kind = (processing_template_decl
7449 && MAYBE_CLASS_TYPE_P (scope)
7450 ? DK_PEDWARN
7451 : DK_ERROR);
7452 cxx_incomplete_type_diagnostic
7453 (location_of (postfix_expression),
7454 postfix_expression, scope, kind);
7455 if (!MAYBE_CLASS_TYPE_P (scope))
7456 return error_mark_node;
7457 if (processing_template_decl)
7458 {
7459 dependent_p = true;
7460 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7461 }
7462 }
7463 }
7464
7465 if (!dependent_p)
7466 {
7467 /* Let the name lookup machinery know that we are processing a
7468 class member access expression. */
7469 parser->context->object_type = scope;
7470 /* If something went wrong, we want to be able to discern that case,
7471 as opposed to the case where there was no SCOPE due to the type
7472 of expression being dependent. */
7473 if (!scope)
7474 scope = error_mark_node;
7475 /* If the SCOPE was erroneous, make the various semantic analysis
7476 functions exit quickly -- and without issuing additional error
7477 messages. */
7478 if (scope == error_mark_node)
7479 postfix_expression = error_mark_node;
7480 }
7481 }
7482
7483 if (dependent_p)
7484 /* Tell cp_parser_lookup_name that there was an object, even though it's
7485 type-dependent. */
7486 parser->context->object_type = unknown_type_node;
7487
7488 /* Assume this expression is not a pseudo-destructor access. */
7489 pseudo_destructor_p = false;
7490
7491 /* If the SCOPE is a scalar type, then, if this is a valid program,
7492 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7493 is type dependent, it can be pseudo-destructor-name or something else.
7494 Try to parse it as pseudo-destructor-name first. */
7495 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7496 {
7497 tree s;
7498 tree type;
7499
7500 cp_parser_parse_tentatively (parser);
7501 /* Parse the pseudo-destructor-name. */
7502 s = NULL_TREE;
7503 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7504 &s, &type);
7505 if (dependent_p
7506 && (cp_parser_error_occurred (parser)
7507 || !SCALAR_TYPE_P (type)))
7508 cp_parser_abort_tentative_parse (parser);
7509 else if (cp_parser_parse_definitely (parser))
7510 {
7511 pseudo_destructor_p = true;
7512 postfix_expression
7513 = finish_pseudo_destructor_expr (postfix_expression,
7514 s, type, location);
7515 }
7516 }
7517
7518 if (!pseudo_destructor_p)
7519 {
7520 /* If the SCOPE is not a scalar type, we are looking at an
7521 ordinary class member access expression, rather than a
7522 pseudo-destructor-name. */
7523 bool template_p;
7524 cp_token *token = cp_lexer_peek_token (parser->lexer);
7525 /* Parse the id-expression. */
7526 name = (cp_parser_id_expression
7527 (parser,
7528 cp_parser_optional_template_keyword (parser),
7529 /*check_dependency_p=*/true,
7530 &template_p,
7531 /*declarator_p=*/false,
7532 /*optional_p=*/false));
7533 /* In general, build a SCOPE_REF if the member name is qualified.
7534 However, if the name was not dependent and has already been
7535 resolved; there is no need to build the SCOPE_REF. For example;
7536
7537 struct X { void f(); };
7538 template <typename T> void f(T* t) { t->X::f(); }
7539
7540 Even though "t" is dependent, "X::f" is not and has been resolved
7541 to a BASELINK; there is no need to include scope information. */
7542
7543 /* But we do need to remember that there was an explicit scope for
7544 virtual function calls. */
7545 if (parser->scope)
7546 *idk = CP_ID_KIND_QUALIFIED;
7547
7548 /* If the name is a template-id that names a type, we will get a
7549 TYPE_DECL here. That is invalid code. */
7550 if (TREE_CODE (name) == TYPE_DECL)
7551 {
7552 error_at (token->location, "invalid use of %qD", name);
7553 postfix_expression = error_mark_node;
7554 }
7555 else
7556 {
7557 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7558 {
7559 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7560 {
7561 error_at (token->location, "%<%D::%D%> is not a class member",
7562 parser->scope, name);
7563 postfix_expression = error_mark_node;
7564 }
7565 else
7566 name = build_qualified_name (/*type=*/NULL_TREE,
7567 parser->scope,
7568 name,
7569 template_p);
7570 parser->scope = NULL_TREE;
7571 parser->qualifying_scope = NULL_TREE;
7572 parser->object_scope = NULL_TREE;
7573 }
7574 if (parser->scope && name && BASELINK_P (name))
7575 adjust_result_of_qualified_name_lookup
7576 (name, parser->scope, scope);
7577 postfix_expression
7578 = finish_class_member_access_expr (postfix_expression, name,
7579 template_p,
7580 tf_warning_or_error);
7581 /* Build a location e.g.:
7582 ptr->access_expr
7583 ~~~^~~~~~~~~~~~~
7584 where the caret is at the deref token, ranging from
7585 the start of postfix_expression to the end of the access expr. */
7586 location_t end_loc
7587 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7588 location_t combined_loc
7589 = make_location (input_location, start_loc, end_loc);
7590 protected_set_expr_location (postfix_expression, combined_loc);
7591 }
7592 }
7593
7594 /* We no longer need to look up names in the scope of the object on
7595 the left-hand side of the `.' or `->' operator. */
7596 parser->context->object_type = NULL_TREE;
7597
7598 /* Outside of offsetof, these operators may not appear in
7599 constant-expressions. */
7600 if (!for_offsetof
7601 && (cp_parser_non_integral_constant_expression
7602 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7603 postfix_expression = error_mark_node;
7604
7605 return postfix_expression;
7606 }
7607
7608 /* Parse a parenthesized expression-list.
7609
7610 expression-list:
7611 assignment-expression
7612 expression-list, assignment-expression
7613
7614 attribute-list:
7615 expression-list
7616 identifier
7617 identifier, expression-list
7618
7619 CAST_P is true if this expression is the target of a cast.
7620
7621 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7622 argument pack.
7623
7624 Returns a vector of trees. Each element is a representation of an
7625 assignment-expression. NULL is returned if the ( and or ) are
7626 missing. An empty, but allocated, vector is returned on no
7627 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7628 if we are parsing an attribute list for an attribute that wants a
7629 plain identifier argument, normal_attr for an attribute that wants
7630 an expression, or non_attr if we aren't parsing an attribute list. If
7631 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7632 not all of the expressions in the list were constant.
7633 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7634 will be written to with the location of the closing parenthesis. If
7635 an error occurs, it may or may not be written to. */
7636
7637 static vec<tree, va_gc> *
7638 cp_parser_parenthesized_expression_list (cp_parser* parser,
7639 int is_attribute_list,
7640 bool cast_p,
7641 bool allow_expansion_p,
7642 bool *non_constant_p,
7643 location_t *close_paren_loc)
7644 {
7645 vec<tree, va_gc> *expression_list;
7646 bool fold_expr_p = is_attribute_list != non_attr;
7647 tree identifier = NULL_TREE;
7648 bool saved_greater_than_is_operator_p;
7649
7650 /* Assume all the expressions will be constant. */
7651 if (non_constant_p)
7652 *non_constant_p = false;
7653
7654 matching_parens parens;
7655 if (!parens.require_open (parser))
7656 return NULL;
7657
7658 expression_list = make_tree_vector ();
7659
7660 /* Within a parenthesized expression, a `>' token is always
7661 the greater-than operator. */
7662 saved_greater_than_is_operator_p
7663 = parser->greater_than_is_operator_p;
7664 parser->greater_than_is_operator_p = true;
7665
7666 /* Consume expressions until there are no more. */
7667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7668 while (true)
7669 {
7670 tree expr;
7671
7672 /* At the beginning of attribute lists, check to see if the
7673 next token is an identifier. */
7674 if (is_attribute_list == id_attr
7675 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7676 {
7677 cp_token *token;
7678
7679 /* Consume the identifier. */
7680 token = cp_lexer_consume_token (parser->lexer);
7681 /* Save the identifier. */
7682 identifier = token->u.value;
7683 }
7684 else
7685 {
7686 bool expr_non_constant_p;
7687
7688 /* Parse the next assignment-expression. */
7689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7690 {
7691 /* A braced-init-list. */
7692 cp_lexer_set_source_position (parser->lexer);
7693 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7694 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7695 if (non_constant_p && expr_non_constant_p)
7696 *non_constant_p = true;
7697 }
7698 else if (non_constant_p)
7699 {
7700 expr = (cp_parser_constant_expression
7701 (parser, /*allow_non_constant_p=*/true,
7702 &expr_non_constant_p));
7703 if (expr_non_constant_p)
7704 *non_constant_p = true;
7705 }
7706 else
7707 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7708 cast_p);
7709
7710 if (fold_expr_p)
7711 expr = instantiate_non_dependent_expr (expr);
7712
7713 /* If we have an ellipsis, then this is an expression
7714 expansion. */
7715 if (allow_expansion_p
7716 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7717 {
7718 /* Consume the `...'. */
7719 cp_lexer_consume_token (parser->lexer);
7720
7721 /* Build the argument pack. */
7722 expr = make_pack_expansion (expr);
7723 }
7724
7725 /* Add it to the list. We add error_mark_node
7726 expressions to the list, so that we can still tell if
7727 the correct form for a parenthesized expression-list
7728 is found. That gives better errors. */
7729 vec_safe_push (expression_list, expr);
7730
7731 if (expr == error_mark_node)
7732 goto skip_comma;
7733 }
7734
7735 /* After the first item, attribute lists look the same as
7736 expression lists. */
7737 is_attribute_list = non_attr;
7738
7739 get_comma:;
7740 /* If the next token isn't a `,', then we are done. */
7741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7742 break;
7743
7744 /* Otherwise, consume the `,' and keep going. */
7745 cp_lexer_consume_token (parser->lexer);
7746 }
7747
7748 if (close_paren_loc)
7749 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7750
7751 if (!parens.require_close (parser))
7752 {
7753 int ending;
7754
7755 skip_comma:;
7756 /* We try and resync to an unnested comma, as that will give the
7757 user better diagnostics. */
7758 ending = cp_parser_skip_to_closing_parenthesis (parser,
7759 /*recovering=*/true,
7760 /*or_comma=*/true,
7761 /*consume_paren=*/true);
7762 if (ending < 0)
7763 goto get_comma;
7764 if (!ending)
7765 {
7766 parser->greater_than_is_operator_p
7767 = saved_greater_than_is_operator_p;
7768 return NULL;
7769 }
7770 }
7771
7772 parser->greater_than_is_operator_p
7773 = saved_greater_than_is_operator_p;
7774
7775 if (identifier)
7776 vec_safe_insert (expression_list, 0, identifier);
7777
7778 return expression_list;
7779 }
7780
7781 /* Parse a pseudo-destructor-name.
7782
7783 pseudo-destructor-name:
7784 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7785 :: [opt] nested-name-specifier template template-id :: ~ type-name
7786 :: [opt] nested-name-specifier [opt] ~ type-name
7787
7788 If either of the first two productions is used, sets *SCOPE to the
7789 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7790 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7791 or ERROR_MARK_NODE if the parse fails. */
7792
7793 static void
7794 cp_parser_pseudo_destructor_name (cp_parser* parser,
7795 tree object,
7796 tree* scope,
7797 tree* type)
7798 {
7799 bool nested_name_specifier_p;
7800
7801 /* Handle ~auto. */
7802 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7803 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7804 && !type_dependent_expression_p (object))
7805 {
7806 if (cxx_dialect < cxx14)
7807 pedwarn (input_location, 0,
7808 "%<~auto%> only available with "
7809 "-std=c++14 or -std=gnu++14");
7810 cp_lexer_consume_token (parser->lexer);
7811 cp_lexer_consume_token (parser->lexer);
7812 *scope = NULL_TREE;
7813 *type = TREE_TYPE (object);
7814 return;
7815 }
7816
7817 /* Assume that things will not work out. */
7818 *type = error_mark_node;
7819
7820 /* Look for the optional `::' operator. */
7821 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7822 /* Look for the optional nested-name-specifier. */
7823 nested_name_specifier_p
7824 = (cp_parser_nested_name_specifier_opt (parser,
7825 /*typename_keyword_p=*/false,
7826 /*check_dependency_p=*/true,
7827 /*type_p=*/false,
7828 /*is_declaration=*/false)
7829 != NULL_TREE);
7830 /* Now, if we saw a nested-name-specifier, we might be doing the
7831 second production. */
7832 if (nested_name_specifier_p
7833 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7834 {
7835 /* Consume the `template' keyword. */
7836 cp_lexer_consume_token (parser->lexer);
7837 /* Parse the template-id. */
7838 cp_parser_template_id (parser,
7839 /*template_keyword_p=*/true,
7840 /*check_dependency_p=*/false,
7841 class_type,
7842 /*is_declaration=*/true);
7843 /* Look for the `::' token. */
7844 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7845 }
7846 /* If the next token is not a `~', then there might be some
7847 additional qualification. */
7848 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7849 {
7850 /* At this point, we're looking for "type-name :: ~". The type-name
7851 must not be a class-name, since this is a pseudo-destructor. So,
7852 it must be either an enum-name, or a typedef-name -- both of which
7853 are just identifiers. So, we peek ahead to check that the "::"
7854 and "~" tokens are present; if they are not, then we can avoid
7855 calling type_name. */
7856 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7857 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7858 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7859 {
7860 cp_parser_error (parser, "non-scalar type");
7861 return;
7862 }
7863
7864 /* Look for the type-name. */
7865 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7866 if (*scope == error_mark_node)
7867 return;
7868
7869 /* Look for the `::' token. */
7870 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7871 }
7872 else
7873 *scope = NULL_TREE;
7874
7875 /* Look for the `~'. */
7876 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7877
7878 /* Once we see the ~, this has to be a pseudo-destructor. */
7879 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7880 cp_parser_commit_to_topmost_tentative_parse (parser);
7881
7882 /* Look for the type-name again. We are not responsible for
7883 checking that it matches the first type-name. */
7884 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7885 }
7886
7887 /* Parse a unary-expression.
7888
7889 unary-expression:
7890 postfix-expression
7891 ++ cast-expression
7892 -- cast-expression
7893 unary-operator cast-expression
7894 sizeof unary-expression
7895 sizeof ( type-id )
7896 alignof ( type-id ) [C++0x]
7897 new-expression
7898 delete-expression
7899
7900 GNU Extensions:
7901
7902 unary-expression:
7903 __extension__ cast-expression
7904 __alignof__ unary-expression
7905 __alignof__ ( type-id )
7906 alignof unary-expression [C++0x]
7907 __real__ cast-expression
7908 __imag__ cast-expression
7909 && identifier
7910 sizeof ( type-id ) { initializer-list , [opt] }
7911 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7912 __alignof__ ( type-id ) { initializer-list , [opt] }
7913
7914 ADDRESS_P is true iff the unary-expression is appearing as the
7915 operand of the `&' operator. CAST_P is true if this expression is
7916 the target of a cast.
7917
7918 Returns a representation of the expression. */
7919
7920 static cp_expr
7921 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7922 bool address_p, bool cast_p, bool decltype_p)
7923 {
7924 cp_token *token;
7925 enum tree_code unary_operator;
7926
7927 /* Peek at the next token. */
7928 token = cp_lexer_peek_token (parser->lexer);
7929 /* Some keywords give away the kind of expression. */
7930 if (token->type == CPP_KEYWORD)
7931 {
7932 enum rid keyword = token->keyword;
7933
7934 switch (keyword)
7935 {
7936 case RID_ALIGNOF:
7937 case RID_SIZEOF:
7938 {
7939 tree operand, ret;
7940 enum tree_code op;
7941 location_t start_loc = token->location;
7942
7943 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7944 /* Consume the token. */
7945 cp_lexer_consume_token (parser->lexer);
7946 /* Parse the operand. */
7947 operand = cp_parser_sizeof_operand (parser, keyword);
7948
7949 if (TYPE_P (operand))
7950 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7951 else
7952 {
7953 /* ISO C++ defines alignof only with types, not with
7954 expressions. So pedwarn if alignof is used with a non-
7955 type expression. However, __alignof__ is ok. */
7956 if (id_equal (token->u.value, "alignof"))
7957 pedwarn (token->location, OPT_Wpedantic,
7958 "ISO C++ does not allow %<alignof%> "
7959 "with a non-type");
7960
7961 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7962 }
7963 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7964 SIZEOF_EXPR with the original operand. */
7965 if (op == SIZEOF_EXPR && ret != error_mark_node)
7966 {
7967 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7968 {
7969 if (!processing_template_decl && TYPE_P (operand))
7970 {
7971 ret = build_min (SIZEOF_EXPR, size_type_node,
7972 build1 (NOP_EXPR, operand,
7973 error_mark_node));
7974 SIZEOF_EXPR_TYPE_P (ret) = 1;
7975 }
7976 else
7977 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7978 TREE_SIDE_EFFECTS (ret) = 0;
7979 TREE_READONLY (ret) = 1;
7980 }
7981 }
7982
7983 /* Construct a location e.g. :
7984 alignof (expr)
7985 ^~~~~~~~~~~~~~
7986 with start == caret at the start of the "alignof"/"sizeof"
7987 token, with the endpoint at the final closing paren. */
7988 location_t finish_loc
7989 = cp_lexer_previous_token (parser->lexer)->location;
7990 location_t compound_loc
7991 = make_location (start_loc, start_loc, finish_loc);
7992
7993 cp_expr ret_expr (ret);
7994 ret_expr.set_location (compound_loc);
7995 return ret_expr;
7996 }
7997
7998 case RID_NEW:
7999 return cp_parser_new_expression (parser);
8000
8001 case RID_DELETE:
8002 return cp_parser_delete_expression (parser);
8003
8004 case RID_EXTENSION:
8005 {
8006 /* The saved value of the PEDANTIC flag. */
8007 int saved_pedantic;
8008 tree expr;
8009
8010 /* Save away the PEDANTIC flag. */
8011 cp_parser_extension_opt (parser, &saved_pedantic);
8012 /* Parse the cast-expression. */
8013 expr = cp_parser_simple_cast_expression (parser);
8014 /* Restore the PEDANTIC flag. */
8015 pedantic = saved_pedantic;
8016
8017 return expr;
8018 }
8019
8020 case RID_REALPART:
8021 case RID_IMAGPART:
8022 {
8023 tree expression;
8024
8025 /* Consume the `__real__' or `__imag__' token. */
8026 cp_lexer_consume_token (parser->lexer);
8027 /* Parse the cast-expression. */
8028 expression = cp_parser_simple_cast_expression (parser);
8029 /* Create the complete representation. */
8030 return build_x_unary_op (token->location,
8031 (keyword == RID_REALPART
8032 ? REALPART_EXPR : IMAGPART_EXPR),
8033 expression,
8034 tf_warning_or_error);
8035 }
8036 break;
8037
8038 case RID_TRANSACTION_ATOMIC:
8039 case RID_TRANSACTION_RELAXED:
8040 return cp_parser_transaction_expression (parser, keyword);
8041
8042 case RID_NOEXCEPT:
8043 {
8044 tree expr;
8045 const char *saved_message;
8046 bool saved_integral_constant_expression_p;
8047 bool saved_non_integral_constant_expression_p;
8048 bool saved_greater_than_is_operator_p;
8049
8050 location_t start_loc = token->location;
8051
8052 cp_lexer_consume_token (parser->lexer);
8053 matching_parens parens;
8054 parens.require_open (parser);
8055
8056 saved_message = parser->type_definition_forbidden_message;
8057 parser->type_definition_forbidden_message
8058 = G_("types may not be defined in %<noexcept%> expressions");
8059
8060 saved_integral_constant_expression_p
8061 = parser->integral_constant_expression_p;
8062 saved_non_integral_constant_expression_p
8063 = parser->non_integral_constant_expression_p;
8064 parser->integral_constant_expression_p = false;
8065
8066 saved_greater_than_is_operator_p
8067 = parser->greater_than_is_operator_p;
8068 parser->greater_than_is_operator_p = true;
8069
8070 ++cp_unevaluated_operand;
8071 ++c_inhibit_evaluation_warnings;
8072 ++cp_noexcept_operand;
8073 expr = cp_parser_expression (parser);
8074 --cp_noexcept_operand;
8075 --c_inhibit_evaluation_warnings;
8076 --cp_unevaluated_operand;
8077
8078 parser->greater_than_is_operator_p
8079 = saved_greater_than_is_operator_p;
8080
8081 parser->integral_constant_expression_p
8082 = saved_integral_constant_expression_p;
8083 parser->non_integral_constant_expression_p
8084 = saved_non_integral_constant_expression_p;
8085
8086 parser->type_definition_forbidden_message = saved_message;
8087
8088 location_t finish_loc
8089 = cp_lexer_peek_token (parser->lexer)->location;
8090 parens.require_close (parser);
8091
8092 /* Construct a location of the form:
8093 noexcept (expr)
8094 ^~~~~~~~~~~~~~~
8095 with start == caret, finishing at the close-paren. */
8096 location_t noexcept_loc
8097 = make_location (start_loc, start_loc, finish_loc);
8098
8099 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8100 noexcept_loc);
8101 }
8102
8103 default:
8104 break;
8105 }
8106 }
8107
8108 /* Look for the `:: new' and `:: delete', which also signal the
8109 beginning of a new-expression, or delete-expression,
8110 respectively. If the next token is `::', then it might be one of
8111 these. */
8112 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8113 {
8114 enum rid keyword;
8115
8116 /* See if the token after the `::' is one of the keywords in
8117 which we're interested. */
8118 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8119 /* If it's `new', we have a new-expression. */
8120 if (keyword == RID_NEW)
8121 return cp_parser_new_expression (parser);
8122 /* Similarly, for `delete'. */
8123 else if (keyword == RID_DELETE)
8124 return cp_parser_delete_expression (parser);
8125 }
8126
8127 /* Look for a unary operator. */
8128 unary_operator = cp_parser_unary_operator (token);
8129 /* The `++' and `--' operators can be handled similarly, even though
8130 they are not technically unary-operators in the grammar. */
8131 if (unary_operator == ERROR_MARK)
8132 {
8133 if (token->type == CPP_PLUS_PLUS)
8134 unary_operator = PREINCREMENT_EXPR;
8135 else if (token->type == CPP_MINUS_MINUS)
8136 unary_operator = PREDECREMENT_EXPR;
8137 /* Handle the GNU address-of-label extension. */
8138 else if (cp_parser_allow_gnu_extensions_p (parser)
8139 && token->type == CPP_AND_AND)
8140 {
8141 tree identifier;
8142 tree expression;
8143 location_t start_loc = token->location;
8144
8145 /* Consume the '&&' token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 /* Look for the identifier. */
8148 location_t finish_loc
8149 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8150 identifier = cp_parser_identifier (parser);
8151 /* Construct a location of the form:
8152 &&label
8153 ^~~~~~~
8154 with caret==start at the "&&", finish at the end of the label. */
8155 location_t combined_loc
8156 = make_location (start_loc, start_loc, finish_loc);
8157 /* Create an expression representing the address. */
8158 expression = finish_label_address_expr (identifier, combined_loc);
8159 if (cp_parser_non_integral_constant_expression (parser,
8160 NIC_ADDR_LABEL))
8161 expression = error_mark_node;
8162 return expression;
8163 }
8164 }
8165 if (unary_operator != ERROR_MARK)
8166 {
8167 cp_expr cast_expression;
8168 cp_expr expression = error_mark_node;
8169 non_integral_constant non_constant_p = NIC_NONE;
8170 location_t loc = token->location;
8171 tsubst_flags_t complain = complain_flags (decltype_p);
8172
8173 /* Consume the operator token. */
8174 token = cp_lexer_consume_token (parser->lexer);
8175 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8176
8177 /* Parse the cast-expression. */
8178 cast_expression
8179 = cp_parser_cast_expression (parser,
8180 unary_operator == ADDR_EXPR,
8181 /*cast_p=*/false,
8182 /*decltype*/false,
8183 pidk);
8184
8185 /* Make a location:
8186 OP_TOKEN CAST_EXPRESSION
8187 ^~~~~~~~~~~~~~~~~~~~~~~~~
8188 with start==caret at the operator token, and
8189 extending to the end of the cast_expression. */
8190 loc = make_location (loc, loc, cast_expression.get_finish ());
8191
8192 /* Now, build an appropriate representation. */
8193 switch (unary_operator)
8194 {
8195 case INDIRECT_REF:
8196 non_constant_p = NIC_STAR;
8197 expression = build_x_indirect_ref (loc, cast_expression,
8198 RO_UNARY_STAR,
8199 complain);
8200 /* TODO: build_x_indirect_ref does not always honor the
8201 location, so ensure it is set. */
8202 expression.set_location (loc);
8203 break;
8204
8205 case ADDR_EXPR:
8206 non_constant_p = NIC_ADDR;
8207 /* Fall through. */
8208 case BIT_NOT_EXPR:
8209 expression = build_x_unary_op (loc, unary_operator,
8210 cast_expression,
8211 complain);
8212 /* TODO: build_x_unary_op does not always honor the location,
8213 so ensure it is set. */
8214 expression.set_location (loc);
8215 break;
8216
8217 case PREINCREMENT_EXPR:
8218 case PREDECREMENT_EXPR:
8219 non_constant_p = unary_operator == PREINCREMENT_EXPR
8220 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8221 /* Fall through. */
8222 case NEGATE_EXPR:
8223 /* Immediately fold negation of a constant, unless the constant is 0
8224 (since -0 == 0) or it would overflow. */
8225 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8226 && CONSTANT_CLASS_P (cast_expression)
8227 && !integer_zerop (cast_expression)
8228 && !TREE_OVERFLOW (cast_expression))
8229 {
8230 tree folded = fold_build1 (unary_operator,
8231 TREE_TYPE (cast_expression),
8232 cast_expression);
8233 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8234 {
8235 expression = cp_expr (folded, loc);
8236 break;
8237 }
8238 }
8239 /* Fall through. */
8240 case UNARY_PLUS_EXPR:
8241 case TRUTH_NOT_EXPR:
8242 expression = finish_unary_op_expr (loc, unary_operator,
8243 cast_expression, complain);
8244 break;
8245
8246 default:
8247 gcc_unreachable ();
8248 }
8249
8250 if (non_constant_p != NIC_NONE
8251 && cp_parser_non_integral_constant_expression (parser,
8252 non_constant_p))
8253 expression = error_mark_node;
8254
8255 return expression;
8256 }
8257
8258 return cp_parser_postfix_expression (parser, address_p, cast_p,
8259 /*member_access_only_p=*/false,
8260 decltype_p,
8261 pidk);
8262 }
8263
8264 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8265 unary-operator, the corresponding tree code is returned. */
8266
8267 static enum tree_code
8268 cp_parser_unary_operator (cp_token* token)
8269 {
8270 switch (token->type)
8271 {
8272 case CPP_MULT:
8273 return INDIRECT_REF;
8274
8275 case CPP_AND:
8276 return ADDR_EXPR;
8277
8278 case CPP_PLUS:
8279 return UNARY_PLUS_EXPR;
8280
8281 case CPP_MINUS:
8282 return NEGATE_EXPR;
8283
8284 case CPP_NOT:
8285 return TRUTH_NOT_EXPR;
8286
8287 case CPP_COMPL:
8288 return BIT_NOT_EXPR;
8289
8290 default:
8291 return ERROR_MARK;
8292 }
8293 }
8294
8295 /* Parse a new-expression.
8296
8297 new-expression:
8298 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8299 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8300
8301 Returns a representation of the expression. */
8302
8303 static tree
8304 cp_parser_new_expression (cp_parser* parser)
8305 {
8306 bool global_scope_p;
8307 vec<tree, va_gc> *placement;
8308 tree type;
8309 vec<tree, va_gc> *initializer;
8310 tree nelts = NULL_TREE;
8311 tree ret;
8312
8313 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8314
8315 /* Look for the optional `::' operator. */
8316 global_scope_p
8317 = (cp_parser_global_scope_opt (parser,
8318 /*current_scope_valid_p=*/false)
8319 != NULL_TREE);
8320 /* Look for the `new' operator. */
8321 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8322 /* There's no easy way to tell a new-placement from the
8323 `( type-id )' construct. */
8324 cp_parser_parse_tentatively (parser);
8325 /* Look for a new-placement. */
8326 placement = cp_parser_new_placement (parser);
8327 /* If that didn't work out, there's no new-placement. */
8328 if (!cp_parser_parse_definitely (parser))
8329 {
8330 if (placement != NULL)
8331 release_tree_vector (placement);
8332 placement = NULL;
8333 }
8334
8335 /* If the next token is a `(', then we have a parenthesized
8336 type-id. */
8337 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8338 {
8339 cp_token *token;
8340 const char *saved_message = parser->type_definition_forbidden_message;
8341
8342 /* Consume the `('. */
8343 matching_parens parens;
8344 parens.consume_open (parser);
8345
8346 /* Parse the type-id. */
8347 parser->type_definition_forbidden_message
8348 = G_("types may not be defined in a new-expression");
8349 {
8350 type_id_in_expr_sentinel s (parser);
8351 type = cp_parser_type_id (parser);
8352 }
8353 parser->type_definition_forbidden_message = saved_message;
8354
8355 /* Look for the closing `)'. */
8356 parens.require_close (parser);
8357 token = cp_lexer_peek_token (parser->lexer);
8358 /* There should not be a direct-new-declarator in this production,
8359 but GCC used to allowed this, so we check and emit a sensible error
8360 message for this case. */
8361 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8362 {
8363 error_at (token->location,
8364 "array bound forbidden after parenthesized type-id");
8365 inform (token->location,
8366 "try removing the parentheses around the type-id");
8367 cp_parser_direct_new_declarator (parser);
8368 }
8369 }
8370 /* Otherwise, there must be a new-type-id. */
8371 else
8372 type = cp_parser_new_type_id (parser, &nelts);
8373
8374 /* If the next token is a `(' or '{', then we have a new-initializer. */
8375 cp_token *token = cp_lexer_peek_token (parser->lexer);
8376 if (token->type == CPP_OPEN_PAREN
8377 || token->type == CPP_OPEN_BRACE)
8378 initializer = cp_parser_new_initializer (parser);
8379 else
8380 initializer = NULL;
8381
8382 /* A new-expression may not appear in an integral constant
8383 expression. */
8384 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8385 ret = error_mark_node;
8386 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8387 of a new-type-id or type-id of a new-expression, the new-expression shall
8388 contain a new-initializer of the form ( assignment-expression )".
8389 Additionally, consistently with the spirit of DR 1467, we want to accept
8390 'new auto { 2 }' too. */
8391 else if ((ret = type_uses_auto (type))
8392 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8393 && (vec_safe_length (initializer) != 1
8394 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8395 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8396 {
8397 error_at (token->location,
8398 "initialization of new-expression for type %<auto%> "
8399 "requires exactly one element");
8400 ret = error_mark_node;
8401 }
8402 else
8403 {
8404 /* Construct a location e.g.:
8405 ptr = new int[100]
8406 ^~~~~~~~~~~~
8407 with caret == start at the start of the "new" token, and the end
8408 at the end of the final token we consumed. */
8409 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8410 location_t end_loc = get_finish (end_tok->location);
8411 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8412
8413 /* Create a representation of the new-expression. */
8414 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8415 tf_warning_or_error);
8416 protected_set_expr_location (ret, combined_loc);
8417 }
8418
8419 if (placement != NULL)
8420 release_tree_vector (placement);
8421 if (initializer != NULL)
8422 release_tree_vector (initializer);
8423
8424 return ret;
8425 }
8426
8427 /* Parse a new-placement.
8428
8429 new-placement:
8430 ( expression-list )
8431
8432 Returns the same representation as for an expression-list. */
8433
8434 static vec<tree, va_gc> *
8435 cp_parser_new_placement (cp_parser* parser)
8436 {
8437 vec<tree, va_gc> *expression_list;
8438
8439 /* Parse the expression-list. */
8440 expression_list = (cp_parser_parenthesized_expression_list
8441 (parser, non_attr, /*cast_p=*/false,
8442 /*allow_expansion_p=*/true,
8443 /*non_constant_p=*/NULL));
8444
8445 if (expression_list && expression_list->is_empty ())
8446 error ("expected expression-list or type-id");
8447
8448 return expression_list;
8449 }
8450
8451 /* Parse a new-type-id.
8452
8453 new-type-id:
8454 type-specifier-seq new-declarator [opt]
8455
8456 Returns the TYPE allocated. If the new-type-id indicates an array
8457 type, *NELTS is set to the number of elements in the last array
8458 bound; the TYPE will not include the last array bound. */
8459
8460 static tree
8461 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8462 {
8463 cp_decl_specifier_seq type_specifier_seq;
8464 cp_declarator *new_declarator;
8465 cp_declarator *declarator;
8466 cp_declarator *outer_declarator;
8467 const char *saved_message;
8468
8469 /* The type-specifier sequence must not contain type definitions.
8470 (It cannot contain declarations of new types either, but if they
8471 are not definitions we will catch that because they are not
8472 complete.) */
8473 saved_message = parser->type_definition_forbidden_message;
8474 parser->type_definition_forbidden_message
8475 = G_("types may not be defined in a new-type-id");
8476 /* Parse the type-specifier-seq. */
8477 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8478 /*is_trailing_return=*/false,
8479 &type_specifier_seq);
8480 /* Restore the old message. */
8481 parser->type_definition_forbidden_message = saved_message;
8482
8483 if (type_specifier_seq.type == error_mark_node)
8484 return error_mark_node;
8485
8486 /* Parse the new-declarator. */
8487 new_declarator = cp_parser_new_declarator_opt (parser);
8488
8489 /* Determine the number of elements in the last array dimension, if
8490 any. */
8491 *nelts = NULL_TREE;
8492 /* Skip down to the last array dimension. */
8493 declarator = new_declarator;
8494 outer_declarator = NULL;
8495 while (declarator && (declarator->kind == cdk_pointer
8496 || declarator->kind == cdk_ptrmem))
8497 {
8498 outer_declarator = declarator;
8499 declarator = declarator->declarator;
8500 }
8501 while (declarator
8502 && declarator->kind == cdk_array
8503 && declarator->declarator
8504 && declarator->declarator->kind == cdk_array)
8505 {
8506 outer_declarator = declarator;
8507 declarator = declarator->declarator;
8508 }
8509
8510 if (declarator && declarator->kind == cdk_array)
8511 {
8512 *nelts = declarator->u.array.bounds;
8513 if (*nelts == error_mark_node)
8514 *nelts = integer_one_node;
8515
8516 if (outer_declarator)
8517 outer_declarator->declarator = declarator->declarator;
8518 else
8519 new_declarator = NULL;
8520 }
8521
8522 return groktypename (&type_specifier_seq, new_declarator, false);
8523 }
8524
8525 /* Parse an (optional) new-declarator.
8526
8527 new-declarator:
8528 ptr-operator new-declarator [opt]
8529 direct-new-declarator
8530
8531 Returns the declarator. */
8532
8533 static cp_declarator *
8534 cp_parser_new_declarator_opt (cp_parser* parser)
8535 {
8536 enum tree_code code;
8537 tree type, std_attributes = NULL_TREE;
8538 cp_cv_quals cv_quals;
8539
8540 /* We don't know if there's a ptr-operator next, or not. */
8541 cp_parser_parse_tentatively (parser);
8542 /* Look for a ptr-operator. */
8543 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8544 /* If that worked, look for more new-declarators. */
8545 if (cp_parser_parse_definitely (parser))
8546 {
8547 cp_declarator *declarator;
8548
8549 /* Parse another optional declarator. */
8550 declarator = cp_parser_new_declarator_opt (parser);
8551
8552 declarator = cp_parser_make_indirect_declarator
8553 (code, type, cv_quals, declarator, std_attributes);
8554
8555 return declarator;
8556 }
8557
8558 /* If the next token is a `[', there is a direct-new-declarator. */
8559 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8560 return cp_parser_direct_new_declarator (parser);
8561
8562 return NULL;
8563 }
8564
8565 /* Parse a direct-new-declarator.
8566
8567 direct-new-declarator:
8568 [ expression ]
8569 direct-new-declarator [constant-expression]
8570
8571 */
8572
8573 static cp_declarator *
8574 cp_parser_direct_new_declarator (cp_parser* parser)
8575 {
8576 cp_declarator *declarator = NULL;
8577
8578 while (true)
8579 {
8580 tree expression;
8581 cp_token *token;
8582
8583 /* Look for the opening `['. */
8584 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8585
8586 token = cp_lexer_peek_token (parser->lexer);
8587 expression = cp_parser_expression (parser);
8588 /* The standard requires that the expression have integral
8589 type. DR 74 adds enumeration types. We believe that the
8590 real intent is that these expressions be handled like the
8591 expression in a `switch' condition, which also allows
8592 classes with a single conversion to integral or
8593 enumeration type. */
8594 if (!processing_template_decl)
8595 {
8596 expression
8597 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8598 expression,
8599 /*complain=*/true);
8600 if (!expression)
8601 {
8602 error_at (token->location,
8603 "expression in new-declarator must have integral "
8604 "or enumeration type");
8605 expression = error_mark_node;
8606 }
8607 }
8608
8609 /* Look for the closing `]'. */
8610 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8611
8612 /* Add this bound to the declarator. */
8613 declarator = make_array_declarator (declarator, expression);
8614
8615 /* If the next token is not a `[', then there are no more
8616 bounds. */
8617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8618 break;
8619 }
8620
8621 return declarator;
8622 }
8623
8624 /* Parse a new-initializer.
8625
8626 new-initializer:
8627 ( expression-list [opt] )
8628 braced-init-list
8629
8630 Returns a representation of the expression-list. */
8631
8632 static vec<tree, va_gc> *
8633 cp_parser_new_initializer (cp_parser* parser)
8634 {
8635 vec<tree, va_gc> *expression_list;
8636
8637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8638 {
8639 tree t;
8640 bool expr_non_constant_p;
8641 cp_lexer_set_source_position (parser->lexer);
8642 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8643 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8644 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8645 expression_list = make_tree_vector_single (t);
8646 }
8647 else
8648 expression_list = (cp_parser_parenthesized_expression_list
8649 (parser, non_attr, /*cast_p=*/false,
8650 /*allow_expansion_p=*/true,
8651 /*non_constant_p=*/NULL));
8652
8653 return expression_list;
8654 }
8655
8656 /* Parse a delete-expression.
8657
8658 delete-expression:
8659 :: [opt] delete cast-expression
8660 :: [opt] delete [ ] cast-expression
8661
8662 Returns a representation of the expression. */
8663
8664 static tree
8665 cp_parser_delete_expression (cp_parser* parser)
8666 {
8667 bool global_scope_p;
8668 bool array_p;
8669 tree expression;
8670
8671 /* Look for the optional `::' operator. */
8672 global_scope_p
8673 = (cp_parser_global_scope_opt (parser,
8674 /*current_scope_valid_p=*/false)
8675 != NULL_TREE);
8676 /* Look for the `delete' keyword. */
8677 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8678 /* See if the array syntax is in use. */
8679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8680 {
8681 /* Consume the `[' token. */
8682 cp_lexer_consume_token (parser->lexer);
8683 /* Look for the `]' token. */
8684 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8685 /* Remember that this is the `[]' construct. */
8686 array_p = true;
8687 }
8688 else
8689 array_p = false;
8690
8691 /* Parse the cast-expression. */
8692 expression = cp_parser_simple_cast_expression (parser);
8693
8694 /* A delete-expression may not appear in an integral constant
8695 expression. */
8696 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8697 return error_mark_node;
8698
8699 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8700 tf_warning_or_error);
8701 }
8702
8703 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8704 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8705 0 otherwise. */
8706
8707 static int
8708 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8709 {
8710 cp_token *token = cp_lexer_peek_token (parser->lexer);
8711 switch (token->type)
8712 {
8713 case CPP_COMMA:
8714 case CPP_SEMICOLON:
8715 case CPP_QUERY:
8716 case CPP_COLON:
8717 case CPP_CLOSE_SQUARE:
8718 case CPP_CLOSE_PAREN:
8719 case CPP_CLOSE_BRACE:
8720 case CPP_OPEN_BRACE:
8721 case CPP_DOT:
8722 case CPP_DOT_STAR:
8723 case CPP_DEREF:
8724 case CPP_DEREF_STAR:
8725 case CPP_DIV:
8726 case CPP_MOD:
8727 case CPP_LSHIFT:
8728 case CPP_RSHIFT:
8729 case CPP_LESS:
8730 case CPP_GREATER:
8731 case CPP_LESS_EQ:
8732 case CPP_GREATER_EQ:
8733 case CPP_EQ_EQ:
8734 case CPP_NOT_EQ:
8735 case CPP_EQ:
8736 case CPP_MULT_EQ:
8737 case CPP_DIV_EQ:
8738 case CPP_MOD_EQ:
8739 case CPP_PLUS_EQ:
8740 case CPP_MINUS_EQ:
8741 case CPP_RSHIFT_EQ:
8742 case CPP_LSHIFT_EQ:
8743 case CPP_AND_EQ:
8744 case CPP_XOR_EQ:
8745 case CPP_OR_EQ:
8746 case CPP_XOR:
8747 case CPP_OR:
8748 case CPP_OR_OR:
8749 case CPP_EOF:
8750 case CPP_ELLIPSIS:
8751 return 0;
8752
8753 case CPP_OPEN_PAREN:
8754 /* In ((type ()) () the last () isn't a valid cast-expression,
8755 so the whole must be parsed as postfix-expression. */
8756 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8757 != CPP_CLOSE_PAREN;
8758
8759 case CPP_OPEN_SQUARE:
8760 /* '[' may start a primary-expression in obj-c++ and in C++11,
8761 as a lambda-expression, eg, '(void)[]{}'. */
8762 if (cxx_dialect >= cxx11)
8763 return -1;
8764 return c_dialect_objc ();
8765
8766 case CPP_PLUS_PLUS:
8767 case CPP_MINUS_MINUS:
8768 /* '++' and '--' may or may not start a cast-expression:
8769
8770 struct T { void operator++(int); };
8771 void f() { (T())++; }
8772
8773 vs
8774
8775 int a;
8776 (int)++a; */
8777 return -1;
8778
8779 default:
8780 return 1;
8781 }
8782 }
8783
8784 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8785 in the order: const_cast, static_cast, reinterpret_cast.
8786
8787 Don't suggest dynamic_cast.
8788
8789 Return the first legal cast kind found, or NULL otherwise. */
8790
8791 static const char *
8792 get_cast_suggestion (tree dst_type, tree orig_expr)
8793 {
8794 tree trial;
8795
8796 /* Reuse the parser logic by attempting to build the various kinds of
8797 cast, with "complain" disabled.
8798 Identify the first such cast that is valid. */
8799
8800 /* Don't attempt to run such logic within template processing. */
8801 if (processing_template_decl)
8802 return NULL;
8803
8804 /* First try const_cast. */
8805 trial = build_const_cast (dst_type, orig_expr, tf_none);
8806 if (trial != error_mark_node)
8807 return "const_cast";
8808
8809 /* If that fails, try static_cast. */
8810 trial = build_static_cast (dst_type, orig_expr, tf_none);
8811 if (trial != error_mark_node)
8812 return "static_cast";
8813
8814 /* Finally, try reinterpret_cast. */
8815 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8816 if (trial != error_mark_node)
8817 return "reinterpret_cast";
8818
8819 /* No such cast possible. */
8820 return NULL;
8821 }
8822
8823 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8824 suggesting how to convert a C-style cast of the form:
8825
8826 (DST_TYPE)ORIG_EXPR
8827
8828 to a C++-style cast.
8829
8830 The primary range of RICHLOC is asssumed to be that of the original
8831 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8832 of the parens in the C-style cast. */
8833
8834 static void
8835 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8836 location_t close_paren_loc, tree orig_expr,
8837 tree dst_type)
8838 {
8839 /* This function is non-trivial, so bail out now if the warning isn't
8840 going to be emitted. */
8841 if (!warn_old_style_cast)
8842 return;
8843
8844 /* Try to find a legal C++ cast, trying them in order:
8845 const_cast, static_cast, reinterpret_cast. */
8846 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8847 if (!cast_suggestion)
8848 return;
8849
8850 /* Replace the open paren with "CAST_SUGGESTION<". */
8851 pretty_printer pp;
8852 pp_printf (&pp, "%s<", cast_suggestion);
8853 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8854
8855 /* Replace the close paren with "> (". */
8856 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8857
8858 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8859 rich_loc->add_fixit_insert_after (")");
8860 }
8861
8862
8863 /* Parse a cast-expression.
8864
8865 cast-expression:
8866 unary-expression
8867 ( type-id ) cast-expression
8868
8869 ADDRESS_P is true iff the unary-expression is appearing as the
8870 operand of the `&' operator. CAST_P is true if this expression is
8871 the target of a cast.
8872
8873 Returns a representation of the expression. */
8874
8875 static cp_expr
8876 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8877 bool decltype_p, cp_id_kind * pidk)
8878 {
8879 /* If it's a `(', then we might be looking at a cast. */
8880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8881 {
8882 tree type = NULL_TREE;
8883 cp_expr expr (NULL_TREE);
8884 int cast_expression = 0;
8885 const char *saved_message;
8886
8887 /* There's no way to know yet whether or not this is a cast.
8888 For example, `(int (3))' is a unary-expression, while `(int)
8889 3' is a cast. So, we resort to parsing tentatively. */
8890 cp_parser_parse_tentatively (parser);
8891 /* Types may not be defined in a cast. */
8892 saved_message = parser->type_definition_forbidden_message;
8893 parser->type_definition_forbidden_message
8894 = G_("types may not be defined in casts");
8895 /* Consume the `('. */
8896 matching_parens parens;
8897 cp_token *open_paren = parens.consume_open (parser);
8898 location_t open_paren_loc = open_paren->location;
8899 location_t close_paren_loc = UNKNOWN_LOCATION;
8900
8901 /* A very tricky bit is that `(struct S) { 3 }' is a
8902 compound-literal (which we permit in C++ as an extension).
8903 But, that construct is not a cast-expression -- it is a
8904 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8905 is legal; if the compound-literal were a cast-expression,
8906 you'd need an extra set of parentheses.) But, if we parse
8907 the type-id, and it happens to be a class-specifier, then we
8908 will commit to the parse at that point, because we cannot
8909 undo the action that is done when creating a new class. So,
8910 then we cannot back up and do a postfix-expression.
8911
8912 Another tricky case is the following (c++/29234):
8913
8914 struct S { void operator () (); };
8915
8916 void foo ()
8917 {
8918 ( S()() );
8919 }
8920
8921 As a type-id we parse the parenthesized S()() as a function
8922 returning a function, groktypename complains and we cannot
8923 back up in this case either.
8924
8925 Therefore, we scan ahead to the closing `)', and check to see
8926 if the tokens after the `)' can start a cast-expression. Otherwise
8927 we are dealing with an unary-expression, a postfix-expression
8928 or something else.
8929
8930 Yet another tricky case, in C++11, is the following (c++/54891):
8931
8932 (void)[]{};
8933
8934 The issue is that usually, besides the case of lambda-expressions,
8935 the parenthesized type-id cannot be followed by '[', and, eg, we
8936 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8937 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8938 we don't commit, we try a cast-expression, then an unary-expression.
8939
8940 Save tokens so that we can put them back. */
8941 cp_lexer_save_tokens (parser->lexer);
8942
8943 /* We may be looking at a cast-expression. */
8944 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8945 /*consume_paren=*/true))
8946 cast_expression
8947 = cp_parser_tokens_start_cast_expression (parser);
8948
8949 /* Roll back the tokens we skipped. */
8950 cp_lexer_rollback_tokens (parser->lexer);
8951 /* If we aren't looking at a cast-expression, simulate an error so
8952 that the call to cp_parser_error_occurred below returns true. */
8953 if (!cast_expression)
8954 cp_parser_simulate_error (parser);
8955 else
8956 {
8957 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8958 parser->in_type_id_in_expr_p = true;
8959 /* Look for the type-id. */
8960 type = cp_parser_type_id (parser);
8961 /* Look for the closing `)'. */
8962 cp_token *close_paren = parens.require_close (parser);
8963 if (close_paren)
8964 close_paren_loc = close_paren->location;
8965 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8966 }
8967
8968 /* Restore the saved message. */
8969 parser->type_definition_forbidden_message = saved_message;
8970
8971 /* At this point this can only be either a cast or a
8972 parenthesized ctor such as `(T ())' that looks like a cast to
8973 function returning T. */
8974 if (!cp_parser_error_occurred (parser))
8975 {
8976 /* Only commit if the cast-expression doesn't start with
8977 '++', '--', or '[' in C++11. */
8978 if (cast_expression > 0)
8979 cp_parser_commit_to_topmost_tentative_parse (parser);
8980
8981 expr = cp_parser_cast_expression (parser,
8982 /*address_p=*/false,
8983 /*cast_p=*/true,
8984 /*decltype_p=*/false,
8985 pidk);
8986
8987 if (cp_parser_parse_definitely (parser))
8988 {
8989 /* Warn about old-style casts, if so requested. */
8990 if (warn_old_style_cast
8991 && !in_system_header_at (input_location)
8992 && !VOID_TYPE_P (type)
8993 && current_lang_name != lang_name_c)
8994 {
8995 gcc_rich_location rich_loc (input_location);
8996 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
8997 expr, type);
8998 warning_at (&rich_loc, OPT_Wold_style_cast,
8999 "use of old-style cast to %q#T", type);
9000 }
9001
9002 /* Only type conversions to integral or enumeration types
9003 can be used in constant-expressions. */
9004 if (!cast_valid_in_integral_constant_expression_p (type)
9005 && cp_parser_non_integral_constant_expression (parser,
9006 NIC_CAST))
9007 return error_mark_node;
9008
9009 /* Perform the cast. */
9010 /* Make a location:
9011 (TYPE) EXPR
9012 ^~~~~~~~~~~
9013 with start==caret at the open paren, extending to the
9014 end of "expr". */
9015 location_t cast_loc = make_location (open_paren_loc,
9016 open_paren_loc,
9017 expr.get_finish ());
9018 expr = build_c_cast (cast_loc, type, expr);
9019 return expr;
9020 }
9021 }
9022 else
9023 cp_parser_abort_tentative_parse (parser);
9024 }
9025
9026 /* If we get here, then it's not a cast, so it must be a
9027 unary-expression. */
9028 return cp_parser_unary_expression (parser, pidk, address_p,
9029 cast_p, decltype_p);
9030 }
9031
9032 /* Parse a binary expression of the general form:
9033
9034 pm-expression:
9035 cast-expression
9036 pm-expression .* cast-expression
9037 pm-expression ->* cast-expression
9038
9039 multiplicative-expression:
9040 pm-expression
9041 multiplicative-expression * pm-expression
9042 multiplicative-expression / pm-expression
9043 multiplicative-expression % pm-expression
9044
9045 additive-expression:
9046 multiplicative-expression
9047 additive-expression + multiplicative-expression
9048 additive-expression - multiplicative-expression
9049
9050 shift-expression:
9051 additive-expression
9052 shift-expression << additive-expression
9053 shift-expression >> additive-expression
9054
9055 relational-expression:
9056 shift-expression
9057 relational-expression < shift-expression
9058 relational-expression > shift-expression
9059 relational-expression <= shift-expression
9060 relational-expression >= shift-expression
9061
9062 GNU Extension:
9063
9064 relational-expression:
9065 relational-expression <? shift-expression
9066 relational-expression >? shift-expression
9067
9068 equality-expression:
9069 relational-expression
9070 equality-expression == relational-expression
9071 equality-expression != relational-expression
9072
9073 and-expression:
9074 equality-expression
9075 and-expression & equality-expression
9076
9077 exclusive-or-expression:
9078 and-expression
9079 exclusive-or-expression ^ and-expression
9080
9081 inclusive-or-expression:
9082 exclusive-or-expression
9083 inclusive-or-expression | exclusive-or-expression
9084
9085 logical-and-expression:
9086 inclusive-or-expression
9087 logical-and-expression && inclusive-or-expression
9088
9089 logical-or-expression:
9090 logical-and-expression
9091 logical-or-expression || logical-and-expression
9092
9093 All these are implemented with a single function like:
9094
9095 binary-expression:
9096 simple-cast-expression
9097 binary-expression <token> binary-expression
9098
9099 CAST_P is true if this expression is the target of a cast.
9100
9101 The binops_by_token map is used to get the tree codes for each <token> type.
9102 binary-expressions are associated according to a precedence table. */
9103
9104 #define TOKEN_PRECEDENCE(token) \
9105 (((token->type == CPP_GREATER \
9106 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9107 && !parser->greater_than_is_operator_p) \
9108 ? PREC_NOT_OPERATOR \
9109 : binops_by_token[token->type].prec)
9110
9111 static cp_expr
9112 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9113 bool no_toplevel_fold_p,
9114 bool decltype_p,
9115 enum cp_parser_prec prec,
9116 cp_id_kind * pidk)
9117 {
9118 cp_parser_expression_stack stack;
9119 cp_parser_expression_stack_entry *sp = &stack[0];
9120 cp_parser_expression_stack_entry current;
9121 cp_expr rhs;
9122 cp_token *token;
9123 enum tree_code rhs_type;
9124 enum cp_parser_prec new_prec, lookahead_prec;
9125 tree overload;
9126
9127 /* Parse the first expression. */
9128 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9129 ? TRUTH_NOT_EXPR : ERROR_MARK);
9130 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9131 cast_p, decltype_p, pidk);
9132 current.prec = prec;
9133
9134 if (cp_parser_error_occurred (parser))
9135 return error_mark_node;
9136
9137 for (;;)
9138 {
9139 /* Get an operator token. */
9140 token = cp_lexer_peek_token (parser->lexer);
9141
9142 if (warn_cxx11_compat
9143 && token->type == CPP_RSHIFT
9144 && !parser->greater_than_is_operator_p)
9145 {
9146 if (warning_at (token->location, OPT_Wc__11_compat,
9147 "%<>>%> operator is treated"
9148 " as two right angle brackets in C++11"))
9149 inform (token->location,
9150 "suggest parentheses around %<>>%> expression");
9151 }
9152
9153 new_prec = TOKEN_PRECEDENCE (token);
9154 if (new_prec != PREC_NOT_OPERATOR
9155 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9156 /* This is a fold-expression; handle it later. */
9157 new_prec = PREC_NOT_OPERATOR;
9158
9159 /* Popping an entry off the stack means we completed a subexpression:
9160 - either we found a token which is not an operator (`>' where it is not
9161 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9162 will happen repeatedly;
9163 - or, we found an operator which has lower priority. This is the case
9164 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9165 parsing `3 * 4'. */
9166 if (new_prec <= current.prec)
9167 {
9168 if (sp == stack)
9169 break;
9170 else
9171 goto pop;
9172 }
9173
9174 get_rhs:
9175 current.tree_type = binops_by_token[token->type].tree_type;
9176 current.loc = token->location;
9177
9178 /* We used the operator token. */
9179 cp_lexer_consume_token (parser->lexer);
9180
9181 /* For "false && x" or "true || x", x will never be executed;
9182 disable warnings while evaluating it. */
9183 if (current.tree_type == TRUTH_ANDIF_EXPR)
9184 c_inhibit_evaluation_warnings +=
9185 cp_fully_fold (current.lhs) == truthvalue_false_node;
9186 else if (current.tree_type == TRUTH_ORIF_EXPR)
9187 c_inhibit_evaluation_warnings +=
9188 cp_fully_fold (current.lhs) == truthvalue_true_node;
9189
9190 /* Extract another operand. It may be the RHS of this expression
9191 or the LHS of a new, higher priority expression. */
9192 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9193 ? TRUTH_NOT_EXPR : ERROR_MARK);
9194 rhs = cp_parser_simple_cast_expression (parser);
9195
9196 /* Get another operator token. Look up its precedence to avoid
9197 building a useless (immediately popped) stack entry for common
9198 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9199 token = cp_lexer_peek_token (parser->lexer);
9200 lookahead_prec = TOKEN_PRECEDENCE (token);
9201 if (lookahead_prec != PREC_NOT_OPERATOR
9202 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9203 lookahead_prec = PREC_NOT_OPERATOR;
9204 if (lookahead_prec > new_prec)
9205 {
9206 /* ... and prepare to parse the RHS of the new, higher priority
9207 expression. Since precedence levels on the stack are
9208 monotonically increasing, we do not have to care about
9209 stack overflows. */
9210 *sp = current;
9211 ++sp;
9212 current.lhs = rhs;
9213 current.lhs_type = rhs_type;
9214 current.prec = new_prec;
9215 new_prec = lookahead_prec;
9216 goto get_rhs;
9217
9218 pop:
9219 lookahead_prec = new_prec;
9220 /* If the stack is not empty, we have parsed into LHS the right side
9221 (`4' in the example above) of an expression we had suspended.
9222 We can use the information on the stack to recover the LHS (`3')
9223 from the stack together with the tree code (`MULT_EXPR'), and
9224 the precedence of the higher level subexpression
9225 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9226 which will be used to actually build the additive expression. */
9227 rhs = current.lhs;
9228 rhs_type = current.lhs_type;
9229 --sp;
9230 current = *sp;
9231 }
9232
9233 /* Undo the disabling of warnings done above. */
9234 if (current.tree_type == TRUTH_ANDIF_EXPR)
9235 c_inhibit_evaluation_warnings -=
9236 cp_fully_fold (current.lhs) == truthvalue_false_node;
9237 else if (current.tree_type == TRUTH_ORIF_EXPR)
9238 c_inhibit_evaluation_warnings -=
9239 cp_fully_fold (current.lhs) == truthvalue_true_node;
9240
9241 if (warn_logical_not_paren
9242 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9243 && current.lhs_type == TRUTH_NOT_EXPR
9244 /* Avoid warning for !!x == y. */
9245 && (TREE_CODE (current.lhs) != NE_EXPR
9246 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9247 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9248 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9249 /* Avoid warning for !b == y where b is boolean. */
9250 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9251 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9252 != BOOLEAN_TYPE))))
9253 /* Avoid warning for !!b == y where b is boolean. */
9254 && (!DECL_P (current.lhs)
9255 || TREE_TYPE (current.lhs) == NULL_TREE
9256 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9257 warn_logical_not_parentheses (current.loc, current.tree_type,
9258 current.lhs, maybe_constant_value (rhs));
9259
9260 overload = NULL;
9261
9262 location_t combined_loc = make_location (current.loc,
9263 current.lhs.get_start (),
9264 rhs.get_finish ());
9265
9266 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9267 ERROR_MARK for everything that is not a binary expression.
9268 This makes warn_about_parentheses miss some warnings that
9269 involve unary operators. For unary expressions we should
9270 pass the correct tree_code unless the unary expression was
9271 surrounded by parentheses.
9272 */
9273 if (no_toplevel_fold_p
9274 && lookahead_prec <= current.prec
9275 && sp == stack)
9276 current.lhs = build2_loc (combined_loc,
9277 current.tree_type,
9278 TREE_CODE_CLASS (current.tree_type)
9279 == tcc_comparison
9280 ? boolean_type_node : TREE_TYPE (current.lhs),
9281 current.lhs, rhs);
9282 else
9283 {
9284 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9285 current.lhs, current.lhs_type,
9286 rhs, rhs_type, &overload,
9287 complain_flags (decltype_p));
9288 /* TODO: build_x_binary_op doesn't always honor the location. */
9289 current.lhs.set_location (combined_loc);
9290 }
9291 current.lhs_type = current.tree_type;
9292
9293 /* If the binary operator required the use of an overloaded operator,
9294 then this expression cannot be an integral constant-expression.
9295 An overloaded operator can be used even if both operands are
9296 otherwise permissible in an integral constant-expression if at
9297 least one of the operands is of enumeration type. */
9298
9299 if (overload
9300 && cp_parser_non_integral_constant_expression (parser,
9301 NIC_OVERLOADED))
9302 return error_mark_node;
9303 }
9304
9305 return current.lhs;
9306 }
9307
9308 static cp_expr
9309 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9310 bool no_toplevel_fold_p,
9311 enum cp_parser_prec prec,
9312 cp_id_kind * pidk)
9313 {
9314 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9315 /*decltype*/false, prec, pidk);
9316 }
9317
9318 /* Parse the `? expression : assignment-expression' part of a
9319 conditional-expression. The LOGICAL_OR_EXPR is the
9320 logical-or-expression that started the conditional-expression.
9321 Returns a representation of the entire conditional-expression.
9322
9323 This routine is used by cp_parser_assignment_expression.
9324
9325 ? expression : assignment-expression
9326
9327 GNU Extensions:
9328
9329 ? : assignment-expression */
9330
9331 static tree
9332 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9333 {
9334 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9335 cp_expr assignment_expr;
9336 struct cp_token *token;
9337 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9338
9339 /* Consume the `?' token. */
9340 cp_lexer_consume_token (parser->lexer);
9341 token = cp_lexer_peek_token (parser->lexer);
9342 if (cp_parser_allow_gnu_extensions_p (parser)
9343 && token->type == CPP_COLON)
9344 {
9345 pedwarn (token->location, OPT_Wpedantic,
9346 "ISO C++ does not allow ?: with omitted middle operand");
9347 /* Implicit true clause. */
9348 expr = NULL_TREE;
9349 c_inhibit_evaluation_warnings +=
9350 folded_logical_or_expr == truthvalue_true_node;
9351 warn_for_omitted_condop (token->location, logical_or_expr);
9352 }
9353 else
9354 {
9355 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9356 parser->colon_corrects_to_scope_p = false;
9357 /* Parse the expression. */
9358 c_inhibit_evaluation_warnings +=
9359 folded_logical_or_expr == truthvalue_false_node;
9360 expr = cp_parser_expression (parser);
9361 c_inhibit_evaluation_warnings +=
9362 ((folded_logical_or_expr == truthvalue_true_node)
9363 - (folded_logical_or_expr == truthvalue_false_node));
9364 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9365 }
9366
9367 /* The next token should be a `:'. */
9368 cp_parser_require (parser, CPP_COLON, RT_COLON);
9369 /* Parse the assignment-expression. */
9370 assignment_expr = cp_parser_assignment_expression (parser);
9371 c_inhibit_evaluation_warnings -=
9372 folded_logical_or_expr == truthvalue_true_node;
9373
9374 /* Make a location:
9375 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9376 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9377 with the caret at the "?", ranging from the start of
9378 the logical_or_expr to the end of the assignment_expr. */
9379 loc = make_location (loc,
9380 logical_or_expr.get_start (),
9381 assignment_expr.get_finish ());
9382
9383 /* Build the conditional-expression. */
9384 return build_x_conditional_expr (loc, logical_or_expr,
9385 expr,
9386 assignment_expr,
9387 tf_warning_or_error);
9388 }
9389
9390 /* Parse an assignment-expression.
9391
9392 assignment-expression:
9393 conditional-expression
9394 logical-or-expression assignment-operator assignment_expression
9395 throw-expression
9396
9397 CAST_P is true if this expression is the target of a cast.
9398 DECLTYPE_P is true if this expression is the operand of decltype.
9399
9400 Returns a representation for the expression. */
9401
9402 static cp_expr
9403 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9404 bool cast_p, bool decltype_p)
9405 {
9406 cp_expr expr;
9407
9408 /* If the next token is the `throw' keyword, then we're looking at
9409 a throw-expression. */
9410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9411 expr = cp_parser_throw_expression (parser);
9412 /* Otherwise, it must be that we are looking at a
9413 logical-or-expression. */
9414 else
9415 {
9416 /* Parse the binary expressions (logical-or-expression). */
9417 expr = cp_parser_binary_expression (parser, cast_p, false,
9418 decltype_p,
9419 PREC_NOT_OPERATOR, pidk);
9420 /* If the next token is a `?' then we're actually looking at a
9421 conditional-expression. */
9422 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9423 return cp_parser_question_colon_clause (parser, expr);
9424 else
9425 {
9426 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9427
9428 /* If it's an assignment-operator, we're using the second
9429 production. */
9430 enum tree_code assignment_operator
9431 = cp_parser_assignment_operator_opt (parser);
9432 if (assignment_operator != ERROR_MARK)
9433 {
9434 bool non_constant_p;
9435
9436 /* Parse the right-hand side of the assignment. */
9437 cp_expr rhs = cp_parser_initializer_clause (parser,
9438 &non_constant_p);
9439
9440 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9441 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9442
9443 /* An assignment may not appear in a
9444 constant-expression. */
9445 if (cp_parser_non_integral_constant_expression (parser,
9446 NIC_ASSIGNMENT))
9447 return error_mark_node;
9448 /* Build the assignment expression. Its default
9449 location:
9450 LHS = RHS
9451 ~~~~^~~~~
9452 is the location of the '=' token as the
9453 caret, ranging from the start of the lhs to the
9454 end of the rhs. */
9455 loc = make_location (loc,
9456 expr.get_start (),
9457 rhs.get_finish ());
9458 expr = build_x_modify_expr (loc, expr,
9459 assignment_operator,
9460 rhs,
9461 complain_flags (decltype_p));
9462 /* TODO: build_x_modify_expr doesn't honor the location,
9463 so we must set it here. */
9464 expr.set_location (loc);
9465 }
9466 }
9467 }
9468
9469 return expr;
9470 }
9471
9472 /* Parse an (optional) assignment-operator.
9473
9474 assignment-operator: one of
9475 = *= /= %= += -= >>= <<= &= ^= |=
9476
9477 GNU Extension:
9478
9479 assignment-operator: one of
9480 <?= >?=
9481
9482 If the next token is an assignment operator, the corresponding tree
9483 code is returned, and the token is consumed. For example, for
9484 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9485 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9486 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9487 operator, ERROR_MARK is returned. */
9488
9489 static enum tree_code
9490 cp_parser_assignment_operator_opt (cp_parser* parser)
9491 {
9492 enum tree_code op;
9493 cp_token *token;
9494
9495 /* Peek at the next token. */
9496 token = cp_lexer_peek_token (parser->lexer);
9497
9498 switch (token->type)
9499 {
9500 case CPP_EQ:
9501 op = NOP_EXPR;
9502 break;
9503
9504 case CPP_MULT_EQ:
9505 op = MULT_EXPR;
9506 break;
9507
9508 case CPP_DIV_EQ:
9509 op = TRUNC_DIV_EXPR;
9510 break;
9511
9512 case CPP_MOD_EQ:
9513 op = TRUNC_MOD_EXPR;
9514 break;
9515
9516 case CPP_PLUS_EQ:
9517 op = PLUS_EXPR;
9518 break;
9519
9520 case CPP_MINUS_EQ:
9521 op = MINUS_EXPR;
9522 break;
9523
9524 case CPP_RSHIFT_EQ:
9525 op = RSHIFT_EXPR;
9526 break;
9527
9528 case CPP_LSHIFT_EQ:
9529 op = LSHIFT_EXPR;
9530 break;
9531
9532 case CPP_AND_EQ:
9533 op = BIT_AND_EXPR;
9534 break;
9535
9536 case CPP_XOR_EQ:
9537 op = BIT_XOR_EXPR;
9538 break;
9539
9540 case CPP_OR_EQ:
9541 op = BIT_IOR_EXPR;
9542 break;
9543
9544 default:
9545 /* Nothing else is an assignment operator. */
9546 op = ERROR_MARK;
9547 }
9548
9549 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9550 if (op != ERROR_MARK
9551 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9552 op = ERROR_MARK;
9553
9554 /* If it was an assignment operator, consume it. */
9555 if (op != ERROR_MARK)
9556 cp_lexer_consume_token (parser->lexer);
9557
9558 return op;
9559 }
9560
9561 /* Parse an expression.
9562
9563 expression:
9564 assignment-expression
9565 expression , assignment-expression
9566
9567 CAST_P is true if this expression is the target of a cast.
9568 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9569 except possibly parenthesized or on the RHS of a comma (N3276).
9570
9571 Returns a representation of the expression. */
9572
9573 static cp_expr
9574 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9575 bool cast_p, bool decltype_p)
9576 {
9577 cp_expr expression = NULL_TREE;
9578 location_t loc = UNKNOWN_LOCATION;
9579
9580 while (true)
9581 {
9582 cp_expr assignment_expression;
9583
9584 /* Parse the next assignment-expression. */
9585 assignment_expression
9586 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9587
9588 /* We don't create a temporary for a call that is the immediate operand
9589 of decltype or on the RHS of a comma. But when we see a comma, we
9590 need to create a temporary for a call on the LHS. */
9591 if (decltype_p && !processing_template_decl
9592 && TREE_CODE (assignment_expression) == CALL_EXPR
9593 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9594 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9595 assignment_expression
9596 = build_cplus_new (TREE_TYPE (assignment_expression),
9597 assignment_expression, tf_warning_or_error);
9598
9599 /* If this is the first assignment-expression, we can just
9600 save it away. */
9601 if (!expression)
9602 expression = assignment_expression;
9603 else
9604 {
9605 /* Create a location with caret at the comma, ranging
9606 from the start of the LHS to the end of the RHS. */
9607 loc = make_location (loc,
9608 expression.get_start (),
9609 assignment_expression.get_finish ());
9610 expression = build_x_compound_expr (loc, expression,
9611 assignment_expression,
9612 complain_flags (decltype_p));
9613 expression.set_location (loc);
9614 }
9615 /* If the next token is not a comma, or we're in a fold-expression, then
9616 we are done with the expression. */
9617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9618 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9619 break;
9620 /* Consume the `,'. */
9621 loc = cp_lexer_peek_token (parser->lexer)->location;
9622 cp_lexer_consume_token (parser->lexer);
9623 /* A comma operator cannot appear in a constant-expression. */
9624 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9625 expression = error_mark_node;
9626 }
9627
9628 return expression;
9629 }
9630
9631 /* Parse a constant-expression.
9632
9633 constant-expression:
9634 conditional-expression
9635
9636 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9637 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9638 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9639 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9640 only parse a conditional-expression, otherwise parse an
9641 assignment-expression. See below for rationale. */
9642
9643 static cp_expr
9644 cp_parser_constant_expression (cp_parser* parser,
9645 bool allow_non_constant_p,
9646 bool *non_constant_p,
9647 bool strict_p)
9648 {
9649 bool saved_integral_constant_expression_p;
9650 bool saved_allow_non_integral_constant_expression_p;
9651 bool saved_non_integral_constant_expression_p;
9652 cp_expr expression;
9653
9654 /* It might seem that we could simply parse the
9655 conditional-expression, and then check to see if it were
9656 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9657 one that the compiler can figure out is constant, possibly after
9658 doing some simplifications or optimizations. The standard has a
9659 precise definition of constant-expression, and we must honor
9660 that, even though it is somewhat more restrictive.
9661
9662 For example:
9663
9664 int i[(2, 3)];
9665
9666 is not a legal declaration, because `(2, 3)' is not a
9667 constant-expression. The `,' operator is forbidden in a
9668 constant-expression. However, GCC's constant-folding machinery
9669 will fold this operation to an INTEGER_CST for `3'. */
9670
9671 /* Save the old settings. */
9672 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9673 saved_allow_non_integral_constant_expression_p
9674 = parser->allow_non_integral_constant_expression_p;
9675 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9676 /* We are now parsing a constant-expression. */
9677 parser->integral_constant_expression_p = true;
9678 parser->allow_non_integral_constant_expression_p
9679 = (allow_non_constant_p || cxx_dialect >= cxx11);
9680 parser->non_integral_constant_expression_p = false;
9681 /* Although the grammar says "conditional-expression", when not STRICT_P,
9682 we parse an "assignment-expression", which also permits
9683 "throw-expression" and the use of assignment operators. In the case
9684 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9685 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9686 actually essential that we look for an assignment-expression.
9687 For example, cp_parser_initializer_clauses uses this function to
9688 determine whether a particular assignment-expression is in fact
9689 constant. */
9690 if (strict_p)
9691 {
9692 /* Parse the binary expressions (logical-or-expression). */
9693 expression = cp_parser_binary_expression (parser, false, false, false,
9694 PREC_NOT_OPERATOR, NULL);
9695 /* If the next token is a `?' then we're actually looking at
9696 a conditional-expression; otherwise we're done. */
9697 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9698 expression = cp_parser_question_colon_clause (parser, expression);
9699 }
9700 else
9701 expression = cp_parser_assignment_expression (parser);
9702 /* Restore the old settings. */
9703 parser->integral_constant_expression_p
9704 = saved_integral_constant_expression_p;
9705 parser->allow_non_integral_constant_expression_p
9706 = saved_allow_non_integral_constant_expression_p;
9707 if (cxx_dialect >= cxx11)
9708 {
9709 /* Require an rvalue constant expression here; that's what our
9710 callers expect. Reference constant expressions are handled
9711 separately in e.g. cp_parser_template_argument. */
9712 tree decay = expression;
9713 if (TREE_TYPE (expression)
9714 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9715 decay = build_address (expression);
9716 bool is_const = potential_rvalue_constant_expression (decay);
9717 parser->non_integral_constant_expression_p = !is_const;
9718 if (!is_const && !allow_non_constant_p)
9719 require_potential_rvalue_constant_expression (decay);
9720 }
9721 if (allow_non_constant_p)
9722 *non_constant_p = parser->non_integral_constant_expression_p;
9723 parser->non_integral_constant_expression_p
9724 = saved_non_integral_constant_expression_p;
9725
9726 return expression;
9727 }
9728
9729 /* Parse __builtin_offsetof.
9730
9731 offsetof-expression:
9732 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9733
9734 offsetof-member-designator:
9735 id-expression
9736 | offsetof-member-designator "." id-expression
9737 | offsetof-member-designator "[" expression "]"
9738 | offsetof-member-designator "->" id-expression */
9739
9740 static cp_expr
9741 cp_parser_builtin_offsetof (cp_parser *parser)
9742 {
9743 int save_ice_p, save_non_ice_p;
9744 tree type;
9745 cp_expr expr;
9746 cp_id_kind dummy;
9747 cp_token *token;
9748 location_t finish_loc;
9749
9750 /* We're about to accept non-integral-constant things, but will
9751 definitely yield an integral constant expression. Save and
9752 restore these values around our local parsing. */
9753 save_ice_p = parser->integral_constant_expression_p;
9754 save_non_ice_p = parser->non_integral_constant_expression_p;
9755
9756 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9757
9758 /* Consume the "__builtin_offsetof" token. */
9759 cp_lexer_consume_token (parser->lexer);
9760 /* Consume the opening `('. */
9761 matching_parens parens;
9762 parens.require_open (parser);
9763 /* Parse the type-id. */
9764 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9765 type = cp_parser_type_id (parser);
9766 /* Look for the `,'. */
9767 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9768 token = cp_lexer_peek_token (parser->lexer);
9769
9770 /* Build the (type *)null that begins the traditional offsetof macro. */
9771 tree object_ptr
9772 = build_static_cast (build_pointer_type (type), null_pointer_node,
9773 tf_warning_or_error);
9774
9775 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9776 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9777 true, &dummy, token->location);
9778 while (true)
9779 {
9780 token = cp_lexer_peek_token (parser->lexer);
9781 switch (token->type)
9782 {
9783 case CPP_OPEN_SQUARE:
9784 /* offsetof-member-designator "[" expression "]" */
9785 expr = cp_parser_postfix_open_square_expression (parser, expr,
9786 true, false);
9787 break;
9788
9789 case CPP_DEREF:
9790 /* offsetof-member-designator "->" identifier */
9791 expr = grok_array_decl (token->location, expr,
9792 integer_zero_node, false);
9793 /* FALLTHRU */
9794
9795 case CPP_DOT:
9796 /* offsetof-member-designator "." identifier */
9797 cp_lexer_consume_token (parser->lexer);
9798 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9799 expr, true, &dummy,
9800 token->location);
9801 break;
9802
9803 case CPP_CLOSE_PAREN:
9804 /* Consume the ")" token. */
9805 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9806 cp_lexer_consume_token (parser->lexer);
9807 goto success;
9808
9809 default:
9810 /* Error. We know the following require will fail, but
9811 that gives the proper error message. */
9812 parens.require_close (parser);
9813 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9814 expr = error_mark_node;
9815 goto failure;
9816 }
9817 }
9818
9819 success:
9820 /* Make a location of the form:
9821 __builtin_offsetof (struct s, f)
9822 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9823 with caret at the type-id, ranging from the start of the
9824 "_builtin_offsetof" token to the close paren. */
9825 loc = make_location (loc, start_loc, finish_loc);
9826 /* The result will be an INTEGER_CST, so we need to explicitly
9827 preserve the location. */
9828 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9829
9830 failure:
9831 parser->integral_constant_expression_p = save_ice_p;
9832 parser->non_integral_constant_expression_p = save_non_ice_p;
9833
9834 return expr;
9835 }
9836
9837 /* Parse a trait expression.
9838
9839 Returns a representation of the expression, the underlying type
9840 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9841
9842 static cp_expr
9843 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9844 {
9845 cp_trait_kind kind;
9846 tree type1, type2 = NULL_TREE;
9847 bool binary = false;
9848 bool variadic = false;
9849
9850 switch (keyword)
9851 {
9852 case RID_HAS_NOTHROW_ASSIGN:
9853 kind = CPTK_HAS_NOTHROW_ASSIGN;
9854 break;
9855 case RID_HAS_NOTHROW_CONSTRUCTOR:
9856 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9857 break;
9858 case RID_HAS_NOTHROW_COPY:
9859 kind = CPTK_HAS_NOTHROW_COPY;
9860 break;
9861 case RID_HAS_TRIVIAL_ASSIGN:
9862 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9863 break;
9864 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9865 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9866 break;
9867 case RID_HAS_TRIVIAL_COPY:
9868 kind = CPTK_HAS_TRIVIAL_COPY;
9869 break;
9870 case RID_HAS_TRIVIAL_DESTRUCTOR:
9871 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9872 break;
9873 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9874 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9875 break;
9876 case RID_HAS_VIRTUAL_DESTRUCTOR:
9877 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9878 break;
9879 case RID_IS_ABSTRACT:
9880 kind = CPTK_IS_ABSTRACT;
9881 break;
9882 case RID_IS_AGGREGATE:
9883 kind = CPTK_IS_AGGREGATE;
9884 break;
9885 case RID_IS_BASE_OF:
9886 kind = CPTK_IS_BASE_OF;
9887 binary = true;
9888 break;
9889 case RID_IS_CLASS:
9890 kind = CPTK_IS_CLASS;
9891 break;
9892 case RID_IS_EMPTY:
9893 kind = CPTK_IS_EMPTY;
9894 break;
9895 case RID_IS_ENUM:
9896 kind = CPTK_IS_ENUM;
9897 break;
9898 case RID_IS_FINAL:
9899 kind = CPTK_IS_FINAL;
9900 break;
9901 case RID_IS_LITERAL_TYPE:
9902 kind = CPTK_IS_LITERAL_TYPE;
9903 break;
9904 case RID_IS_POD:
9905 kind = CPTK_IS_POD;
9906 break;
9907 case RID_IS_POLYMORPHIC:
9908 kind = CPTK_IS_POLYMORPHIC;
9909 break;
9910 case RID_IS_SAME_AS:
9911 kind = CPTK_IS_SAME_AS;
9912 binary = true;
9913 break;
9914 case RID_IS_STD_LAYOUT:
9915 kind = CPTK_IS_STD_LAYOUT;
9916 break;
9917 case RID_IS_TRIVIAL:
9918 kind = CPTK_IS_TRIVIAL;
9919 break;
9920 case RID_IS_TRIVIALLY_ASSIGNABLE:
9921 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9922 binary = true;
9923 break;
9924 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9925 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9926 variadic = true;
9927 break;
9928 case RID_IS_TRIVIALLY_COPYABLE:
9929 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9930 break;
9931 case RID_IS_UNION:
9932 kind = CPTK_IS_UNION;
9933 break;
9934 case RID_UNDERLYING_TYPE:
9935 kind = CPTK_UNDERLYING_TYPE;
9936 break;
9937 case RID_BASES:
9938 kind = CPTK_BASES;
9939 break;
9940 case RID_DIRECT_BASES:
9941 kind = CPTK_DIRECT_BASES;
9942 break;
9943 case RID_IS_ASSIGNABLE:
9944 kind = CPTK_IS_ASSIGNABLE;
9945 binary = true;
9946 break;
9947 case RID_IS_CONSTRUCTIBLE:
9948 kind = CPTK_IS_CONSTRUCTIBLE;
9949 variadic = true;
9950 break;
9951 default:
9952 gcc_unreachable ();
9953 }
9954
9955 /* Get location of initial token. */
9956 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9957
9958 /* Consume the token. */
9959 cp_lexer_consume_token (parser->lexer);
9960
9961 matching_parens parens;
9962 parens.require_open (parser);
9963
9964 {
9965 type_id_in_expr_sentinel s (parser);
9966 type1 = cp_parser_type_id (parser);
9967 }
9968
9969 if (type1 == error_mark_node)
9970 return error_mark_node;
9971
9972 if (binary)
9973 {
9974 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9975
9976 {
9977 type_id_in_expr_sentinel s (parser);
9978 type2 = cp_parser_type_id (parser);
9979 }
9980
9981 if (type2 == error_mark_node)
9982 return error_mark_node;
9983 }
9984 else if (variadic)
9985 {
9986 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9987 {
9988 cp_lexer_consume_token (parser->lexer);
9989 tree elt = cp_parser_type_id (parser);
9990 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9991 {
9992 cp_lexer_consume_token (parser->lexer);
9993 elt = make_pack_expansion (elt);
9994 }
9995 if (elt == error_mark_node)
9996 return error_mark_node;
9997 type2 = tree_cons (NULL_TREE, elt, type2);
9998 }
9999 }
10000
10001 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10002 parens.require_close (parser);
10003
10004 /* Construct a location of the form:
10005 __is_trivially_copyable(_Tp)
10006 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10007 with start == caret, finishing at the close-paren. */
10008 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10009
10010 /* Complete the trait expression, which may mean either processing
10011 the trait expr now or saving it for template instantiation. */
10012 switch (kind)
10013 {
10014 case CPTK_UNDERLYING_TYPE:
10015 return cp_expr (finish_underlying_type (type1), trait_loc);
10016 case CPTK_BASES:
10017 return cp_expr (finish_bases (type1, false), trait_loc);
10018 case CPTK_DIRECT_BASES:
10019 return cp_expr (finish_bases (type1, true), trait_loc);
10020 default:
10021 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10022 }
10023 }
10024
10025 /* Parse a lambda expression.
10026
10027 lambda-expression:
10028 lambda-introducer lambda-declarator [opt] compound-statement
10029
10030 Returns a representation of the expression. */
10031
10032 static cp_expr
10033 cp_parser_lambda_expression (cp_parser* parser)
10034 {
10035 tree lambda_expr = build_lambda_expr ();
10036 tree type;
10037 bool ok = true;
10038 cp_token *token = cp_lexer_peek_token (parser->lexer);
10039 cp_token_position start = 0;
10040
10041 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10042
10043 if (cp_unevaluated_operand)
10044 {
10045 if (!token->error_reported)
10046 {
10047 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10048 "lambda-expression in unevaluated context");
10049 token->error_reported = true;
10050 }
10051 ok = false;
10052 }
10053 else if (parser->in_template_argument_list_p)
10054 {
10055 if (!token->error_reported)
10056 {
10057 error_at (token->location, "lambda-expression in template-argument");
10058 token->error_reported = true;
10059 }
10060 ok = false;
10061 }
10062
10063 /* We may be in the middle of deferred access check. Disable
10064 it now. */
10065 push_deferring_access_checks (dk_no_deferred);
10066
10067 cp_parser_lambda_introducer (parser, lambda_expr);
10068
10069 type = begin_lambda_type (lambda_expr);
10070 if (type == error_mark_node)
10071 return error_mark_node;
10072
10073 record_lambda_scope (lambda_expr);
10074
10075 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10076 determine_visibility (TYPE_NAME (type));
10077
10078 /* Now that we've started the type, add the capture fields for any
10079 explicit captures. */
10080 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10081
10082 {
10083 /* Inside the class, surrounding template-parameter-lists do not apply. */
10084 unsigned int saved_num_template_parameter_lists
10085 = parser->num_template_parameter_lists;
10086 unsigned char in_statement = parser->in_statement;
10087 bool in_switch_statement_p = parser->in_switch_statement_p;
10088 bool fully_implicit_function_template_p
10089 = parser->fully_implicit_function_template_p;
10090 tree implicit_template_parms = parser->implicit_template_parms;
10091 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10092 bool auto_is_implicit_function_template_parm_p
10093 = parser->auto_is_implicit_function_template_parm_p;
10094
10095 parser->num_template_parameter_lists = 0;
10096 parser->in_statement = 0;
10097 parser->in_switch_statement_p = false;
10098 parser->fully_implicit_function_template_p = false;
10099 parser->implicit_template_parms = 0;
10100 parser->implicit_template_scope = 0;
10101 parser->auto_is_implicit_function_template_parm_p = false;
10102
10103 /* By virtue of defining a local class, a lambda expression has access to
10104 the private variables of enclosing classes. */
10105
10106 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10107
10108 if (ok && cp_parser_error_occurred (parser))
10109 ok = false;
10110
10111 if (ok)
10112 {
10113 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10114 && cp_parser_start_tentative_firewall (parser))
10115 start = token;
10116 cp_parser_lambda_body (parser, lambda_expr);
10117 }
10118 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10119 {
10120 if (cp_parser_skip_to_closing_brace (parser))
10121 cp_lexer_consume_token (parser->lexer);
10122 }
10123
10124 /* The capture list was built up in reverse order; fix that now. */
10125 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10126 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10127
10128 if (ok)
10129 maybe_add_lambda_conv_op (type);
10130
10131 type = finish_struct (type, /*attributes=*/NULL_TREE);
10132
10133 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10134 parser->in_statement = in_statement;
10135 parser->in_switch_statement_p = in_switch_statement_p;
10136 parser->fully_implicit_function_template_p
10137 = fully_implicit_function_template_p;
10138 parser->implicit_template_parms = implicit_template_parms;
10139 parser->implicit_template_scope = implicit_template_scope;
10140 parser->auto_is_implicit_function_template_parm_p
10141 = auto_is_implicit_function_template_parm_p;
10142 }
10143
10144 /* This field is only used during parsing of the lambda. */
10145 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10146
10147 /* This lambda shouldn't have any proxies left at this point. */
10148 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10149 /* And now that we're done, push proxies for an enclosing lambda. */
10150 insert_pending_capture_proxies ();
10151
10152 if (ok)
10153 lambda_expr = build_lambda_object (lambda_expr);
10154 else
10155 lambda_expr = error_mark_node;
10156
10157 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10158
10159 pop_deferring_access_checks ();
10160
10161 return lambda_expr;
10162 }
10163
10164 /* Parse the beginning of a lambda expression.
10165
10166 lambda-introducer:
10167 [ lambda-capture [opt] ]
10168
10169 LAMBDA_EXPR is the current representation of the lambda expression. */
10170
10171 static void
10172 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10173 {
10174 /* Need commas after the first capture. */
10175 bool first = true;
10176
10177 /* Eat the leading `['. */
10178 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10179
10180 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10181 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10182 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10183 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10184 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10185 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10186
10187 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10188 {
10189 cp_lexer_consume_token (parser->lexer);
10190 first = false;
10191 }
10192
10193 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10194 {
10195 cp_token* capture_token;
10196 tree capture_id;
10197 tree capture_init_expr;
10198 cp_id_kind idk = CP_ID_KIND_NONE;
10199 bool explicit_init_p = false;
10200
10201 enum capture_kind_type
10202 {
10203 BY_COPY,
10204 BY_REFERENCE
10205 };
10206 enum capture_kind_type capture_kind = BY_COPY;
10207
10208 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10209 {
10210 error ("expected end of capture-list");
10211 return;
10212 }
10213
10214 if (first)
10215 first = false;
10216 else
10217 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10218
10219 /* Possibly capture `this'. */
10220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10221 {
10222 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10223 if (cxx_dialect < cxx2a
10224 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10225 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10226 "with by-copy capture default");
10227 cp_lexer_consume_token (parser->lexer);
10228 add_capture (lambda_expr,
10229 /*id=*/this_identifier,
10230 /*initializer=*/finish_this_expr (),
10231 /*by_reference_p=*/true,
10232 explicit_init_p);
10233 continue;
10234 }
10235
10236 /* Possibly capture `*this'. */
10237 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10238 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10239 {
10240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10241 if (cxx_dialect < cxx17)
10242 pedwarn (loc, 0, "%<*this%> capture only available with "
10243 "-std=c++17 or -std=gnu++17");
10244 cp_lexer_consume_token (parser->lexer);
10245 cp_lexer_consume_token (parser->lexer);
10246 add_capture (lambda_expr,
10247 /*id=*/this_identifier,
10248 /*initializer=*/finish_this_expr (),
10249 /*by_reference_p=*/false,
10250 explicit_init_p);
10251 continue;
10252 }
10253
10254 /* Remember whether we want to capture as a reference or not. */
10255 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10256 {
10257 capture_kind = BY_REFERENCE;
10258 cp_lexer_consume_token (parser->lexer);
10259 }
10260
10261 /* Get the identifier. */
10262 capture_token = cp_lexer_peek_token (parser->lexer);
10263 capture_id = cp_parser_identifier (parser);
10264
10265 if (capture_id == error_mark_node)
10266 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10267 delimiters, but I modified this to stop on unnested ']' as well. It
10268 was already changed to stop on unnested '}', so the
10269 "closing_parenthesis" name is no more misleading with my change. */
10270 {
10271 cp_parser_skip_to_closing_parenthesis (parser,
10272 /*recovering=*/true,
10273 /*or_comma=*/true,
10274 /*consume_paren=*/true);
10275 break;
10276 }
10277
10278 /* Find the initializer for this capture. */
10279 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10280 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10281 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10282 {
10283 bool direct, non_constant;
10284 /* An explicit initializer exists. */
10285 if (cxx_dialect < cxx14)
10286 pedwarn (input_location, 0,
10287 "lambda capture initializers "
10288 "only available with -std=c++14 or -std=gnu++14");
10289 capture_init_expr = cp_parser_initializer (parser, &direct,
10290 &non_constant);
10291 explicit_init_p = true;
10292 if (capture_init_expr == NULL_TREE)
10293 {
10294 error ("empty initializer for lambda init-capture");
10295 capture_init_expr = error_mark_node;
10296 }
10297 }
10298 else
10299 {
10300 const char* error_msg;
10301
10302 /* Turn the identifier into an id-expression. */
10303 capture_init_expr
10304 = cp_parser_lookup_name_simple (parser, capture_id,
10305 capture_token->location);
10306
10307 if (capture_init_expr == error_mark_node)
10308 {
10309 unqualified_name_lookup_error (capture_id);
10310 continue;
10311 }
10312 else if (DECL_P (capture_init_expr)
10313 && (!VAR_P (capture_init_expr)
10314 && TREE_CODE (capture_init_expr) != PARM_DECL))
10315 {
10316 error_at (capture_token->location,
10317 "capture of non-variable %qD ",
10318 capture_init_expr);
10319 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10320 "%q#D declared here", capture_init_expr);
10321 continue;
10322 }
10323 if (VAR_P (capture_init_expr)
10324 && decl_storage_duration (capture_init_expr) != dk_auto)
10325 {
10326 if (pedwarn (capture_token->location, 0, "capture of variable "
10327 "%qD with non-automatic storage duration",
10328 capture_init_expr))
10329 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10330 "%q#D declared here", capture_init_expr);
10331 continue;
10332 }
10333
10334 capture_init_expr
10335 = finish_id_expression
10336 (capture_id,
10337 capture_init_expr,
10338 parser->scope,
10339 &idk,
10340 /*integral_constant_expression_p=*/false,
10341 /*allow_non_integral_constant_expression_p=*/false,
10342 /*non_integral_constant_expression_p=*/NULL,
10343 /*template_p=*/false,
10344 /*done=*/true,
10345 /*address_p=*/false,
10346 /*template_arg_p=*/false,
10347 &error_msg,
10348 capture_token->location);
10349
10350 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10351 {
10352 cp_lexer_consume_token (parser->lexer);
10353 capture_init_expr = make_pack_expansion (capture_init_expr);
10354 }
10355 else
10356 check_for_bare_parameter_packs (capture_init_expr);
10357 }
10358
10359 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10360 && !explicit_init_p)
10361 {
10362 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10363 && capture_kind == BY_COPY)
10364 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10365 "of %qD redundant with by-copy capture default",
10366 capture_id);
10367 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10368 && capture_kind == BY_REFERENCE)
10369 pedwarn (capture_token->location, 0, "explicit by-reference "
10370 "capture of %qD redundant with by-reference capture "
10371 "default", capture_id);
10372 }
10373
10374 add_capture (lambda_expr,
10375 capture_id,
10376 capture_init_expr,
10377 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10378 explicit_init_p);
10379 }
10380
10381 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10382 }
10383
10384 /* Parse the (optional) middle of a lambda expression.
10385
10386 lambda-declarator:
10387 < template-parameter-list [opt] >
10388 ( parameter-declaration-clause [opt] )
10389 attribute-specifier [opt]
10390 decl-specifier-seq [opt]
10391 exception-specification [opt]
10392 lambda-return-type-clause [opt]
10393
10394 LAMBDA_EXPR is the current representation of the lambda expression. */
10395
10396 static bool
10397 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10398 {
10399 /* 5.1.1.4 of the standard says:
10400 If a lambda-expression does not include a lambda-declarator, it is as if
10401 the lambda-declarator were ().
10402 This means an empty parameter list, no attributes, and no exception
10403 specification. */
10404 tree param_list = void_list_node;
10405 tree attributes = NULL_TREE;
10406 tree exception_spec = NULL_TREE;
10407 tree template_param_list = NULL_TREE;
10408 tree tx_qual = NULL_TREE;
10409 tree return_type = NULL_TREE;
10410 cp_decl_specifier_seq lambda_specs;
10411 clear_decl_specs (&lambda_specs);
10412
10413 /* The template-parameter-list is optional, but must begin with
10414 an opening angle if present. */
10415 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10416 {
10417 if (cxx_dialect < cxx14)
10418 pedwarn (parser->lexer->next_token->location, 0,
10419 "lambda templates are only available with "
10420 "-std=c++14 or -std=gnu++14");
10421 else if (cxx_dialect < cxx2a)
10422 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10423 "lambda templates are only available with "
10424 "-std=c++2a or -std=gnu++2a");
10425
10426 cp_lexer_consume_token (parser->lexer);
10427
10428 template_param_list = cp_parser_template_parameter_list (parser);
10429
10430 cp_parser_skip_to_end_of_template_parameter_list (parser);
10431
10432 /* We just processed one more parameter list. */
10433 ++parser->num_template_parameter_lists;
10434 }
10435
10436 /* The parameter-declaration-clause is optional (unless
10437 template-parameter-list was given), but must begin with an
10438 opening parenthesis if present. */
10439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10440 {
10441 matching_parens parens;
10442 parens.consume_open (parser);
10443
10444 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10445
10446 /* Parse parameters. */
10447 param_list = cp_parser_parameter_declaration_clause (parser);
10448
10449 /* Default arguments shall not be specified in the
10450 parameter-declaration-clause of a lambda-declarator. */
10451 if (cxx_dialect < cxx14)
10452 for (tree t = param_list; t; t = TREE_CHAIN (t))
10453 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10454 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10455 "default argument specified for lambda parameter");
10456
10457 parens.require_close (parser);
10458
10459 attributes = cp_parser_attributes_opt (parser);
10460
10461 /* In the decl-specifier-seq of the lambda-declarator, each
10462 decl-specifier shall either be mutable or constexpr. */
10463 int declares_class_or_enum;
10464 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10465 cp_parser_decl_specifier_seq (parser,
10466 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10467 &lambda_specs, &declares_class_or_enum);
10468 if (lambda_specs.storage_class == sc_mutable)
10469 {
10470 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10471 if (lambda_specs.conflicting_specifiers_p)
10472 error_at (lambda_specs.locations[ds_storage_class],
10473 "duplicate %<mutable%>");
10474 }
10475
10476 tx_qual = cp_parser_tx_qualifier_opt (parser);
10477
10478 /* Parse optional exception specification. */
10479 exception_spec = cp_parser_exception_specification_opt (parser);
10480
10481 /* Parse optional trailing return type. */
10482 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10483 {
10484 cp_lexer_consume_token (parser->lexer);
10485 return_type = cp_parser_trailing_type_id (parser);
10486 }
10487
10488 /* The function parameters must be in scope all the way until after the
10489 trailing-return-type in case of decltype. */
10490 pop_bindings_and_leave_scope ();
10491 }
10492 else if (template_param_list != NULL_TREE) // generate diagnostic
10493 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10494
10495 /* Create the function call operator.
10496
10497 Messing with declarators like this is no uglier than building up the
10498 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10499 other code. */
10500 {
10501 cp_decl_specifier_seq return_type_specs;
10502 cp_declarator* declarator;
10503 tree fco;
10504 int quals;
10505 void *p;
10506
10507 clear_decl_specs (&return_type_specs);
10508 if (return_type)
10509 return_type_specs.type = return_type;
10510 else
10511 /* Maybe we will deduce the return type later. */
10512 return_type_specs.type = make_auto ();
10513
10514 if (lambda_specs.locations[ds_constexpr])
10515 {
10516 if (cxx_dialect >= cxx17)
10517 return_type_specs.locations[ds_constexpr]
10518 = lambda_specs.locations[ds_constexpr];
10519 else
10520 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10521 "lambda only available with -std=c++17 or -std=gnu++17");
10522 }
10523
10524 p = obstack_alloc (&declarator_obstack, 0);
10525
10526 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10527
10528 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10529 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10530 declarator = make_call_declarator (declarator, param_list, quals,
10531 VIRT_SPEC_UNSPECIFIED,
10532 REF_QUAL_NONE,
10533 tx_qual,
10534 exception_spec,
10535 /*late_return_type=*/NULL_TREE,
10536 /*requires_clause*/NULL_TREE);
10537 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10538
10539 fco = grokmethod (&return_type_specs,
10540 declarator,
10541 attributes);
10542 if (fco != error_mark_node)
10543 {
10544 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10545 DECL_ARTIFICIAL (fco) = 1;
10546 /* Give the object parameter a different name. */
10547 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10548 if (return_type)
10549 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10550 }
10551 if (template_param_list)
10552 {
10553 fco = finish_member_template_decl (fco);
10554 finish_template_decl (template_param_list);
10555 --parser->num_template_parameter_lists;
10556 }
10557 else if (parser->fully_implicit_function_template_p)
10558 fco = finish_fully_implicit_template (parser, fco);
10559
10560 finish_member_declaration (fco);
10561
10562 obstack_free (&declarator_obstack, p);
10563
10564 return (fco != error_mark_node);
10565 }
10566 }
10567
10568 /* Parse the body of a lambda expression, which is simply
10569
10570 compound-statement
10571
10572 but which requires special handling.
10573 LAMBDA_EXPR is the current representation of the lambda expression. */
10574
10575 static void
10576 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10577 {
10578 bool nested = (current_function_decl != NULL_TREE);
10579 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10580 bool in_function_body = parser->in_function_body;
10581 if (nested)
10582 push_function_context ();
10583 else
10584 /* Still increment function_depth so that we don't GC in the
10585 middle of an expression. */
10586 ++function_depth;
10587 vec<tree> omp_privatization_save;
10588 save_omp_privatization_clauses (omp_privatization_save);
10589 /* Clear this in case we're in the middle of a default argument. */
10590 parser->local_variables_forbidden_p = false;
10591 parser->in_function_body = true;
10592
10593 /* Finish the function call operator
10594 - class_specifier
10595 + late_parsing_for_member
10596 + function_definition_after_declarator
10597 + ctor_initializer_opt_and_function_body */
10598 {
10599 local_specialization_stack s (lss_copy);
10600
10601 tree fco = lambda_function (lambda_expr);
10602 tree body = start_lambda_function (fco, lambda_expr);
10603 bool done = false;
10604 tree compound_stmt;
10605
10606 matching_braces braces;
10607 if (!braces.require_open (parser))
10608 goto out;
10609
10610 compound_stmt = begin_compound_stmt (0);
10611
10612 /* 5.1.1.4 of the standard says:
10613 If a lambda-expression does not include a trailing-return-type, it
10614 is as if the trailing-return-type denotes the following type:
10615 * if the compound-statement is of the form
10616 { return attribute-specifier [opt] expression ; }
10617 the type of the returned expression after lvalue-to-rvalue
10618 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10619 (_conv.array_ 4.2), and function-to-pointer conversion
10620 (_conv.func_ 4.3);
10621 * otherwise, void. */
10622
10623 /* In a lambda that has neither a lambda-return-type-clause
10624 nor a deducible form, errors should be reported for return statements
10625 in the body. Since we used void as the placeholder return type, parsing
10626 the body as usual will give such desired behavior. */
10627 if (is_auto (TREE_TYPE (TREE_TYPE (fco)))
10628 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10629 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10630 {
10631 tree expr = NULL_TREE;
10632 cp_id_kind idk = CP_ID_KIND_NONE;
10633
10634 /* Parse tentatively in case there's more after the initial return
10635 statement. */
10636 cp_parser_parse_tentatively (parser);
10637
10638 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10639
10640 expr = cp_parser_expression (parser, &idk);
10641
10642 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10643 braces.require_close (parser);
10644
10645 if (cp_parser_parse_definitely (parser))
10646 {
10647 if (!processing_template_decl)
10648 {
10649 tree type = lambda_return_type (expr);
10650 apply_deduced_return_type (fco, type);
10651 if (type == error_mark_node)
10652 expr = error_mark_node;
10653 }
10654
10655 /* Will get error here if type not deduced yet. */
10656 finish_return_stmt (expr);
10657
10658 done = true;
10659 }
10660 }
10661
10662 if (!done)
10663 {
10664 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10665 cp_parser_label_declaration (parser);
10666 cp_parser_statement_seq_opt (parser, NULL_TREE);
10667 braces.require_close (parser);
10668 }
10669
10670 finish_compound_stmt (compound_stmt);
10671
10672 out:
10673 finish_lambda_function (body);
10674 }
10675
10676 restore_omp_privatization_clauses (omp_privatization_save);
10677 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10678 parser->in_function_body = in_function_body;
10679 if (nested)
10680 pop_function_context();
10681 else
10682 --function_depth;
10683 }
10684
10685 /* Statements [gram.stmt.stmt] */
10686
10687 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10688
10689 static void
10690 add_debug_begin_stmt (location_t loc)
10691 {
10692 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10693 return;
10694
10695 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10696 SET_EXPR_LOCATION (stmt, loc);
10697 add_stmt (stmt);
10698 }
10699
10700 /* Parse a statement.
10701
10702 statement:
10703 labeled-statement
10704 expression-statement
10705 compound-statement
10706 selection-statement
10707 iteration-statement
10708 jump-statement
10709 declaration-statement
10710 try-block
10711
10712 C++11:
10713
10714 statement:
10715 labeled-statement
10716 attribute-specifier-seq (opt) expression-statement
10717 attribute-specifier-seq (opt) compound-statement
10718 attribute-specifier-seq (opt) selection-statement
10719 attribute-specifier-seq (opt) iteration-statement
10720 attribute-specifier-seq (opt) jump-statement
10721 declaration-statement
10722 attribute-specifier-seq (opt) try-block
10723
10724 init-statement:
10725 expression-statement
10726 simple-declaration
10727
10728 TM Extension:
10729
10730 statement:
10731 atomic-statement
10732
10733 IN_COMPOUND is true when the statement is nested inside a
10734 cp_parser_compound_statement; this matters for certain pragmas.
10735
10736 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10737 is a (possibly labeled) if statement which is not enclosed in braces
10738 and has an else clause. This is used to implement -Wparentheses.
10739
10740 CHAIN is a vector of if-else-if conditions. */
10741
10742 static void
10743 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10744 bool in_compound, bool *if_p, vec<tree> *chain,
10745 location_t *loc_after_labels)
10746 {
10747 tree statement, std_attrs = NULL_TREE;
10748 cp_token *token;
10749 location_t statement_location, attrs_location;
10750
10751 restart:
10752 if (if_p != NULL)
10753 *if_p = false;
10754 /* There is no statement yet. */
10755 statement = NULL_TREE;
10756
10757 saved_token_sentinel saved_tokens (parser->lexer);
10758 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10759 if (c_dialect_objc ())
10760 /* In obj-c++, seeing '[[' might be the either the beginning of
10761 c++11 attributes, or a nested objc-message-expression. So
10762 let's parse the c++11 attributes tentatively. */
10763 cp_parser_parse_tentatively (parser);
10764 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10765 if (c_dialect_objc ())
10766 {
10767 if (!cp_parser_parse_definitely (parser))
10768 std_attrs = NULL_TREE;
10769 }
10770
10771 /* Peek at the next token. */
10772 token = cp_lexer_peek_token (parser->lexer);
10773 /* Remember the location of the first token in the statement. */
10774 statement_location = token->location;
10775 add_debug_begin_stmt (statement_location);
10776 /* If this is a keyword, then that will often determine what kind of
10777 statement we have. */
10778 if (token->type == CPP_KEYWORD)
10779 {
10780 enum rid keyword = token->keyword;
10781
10782 switch (keyword)
10783 {
10784 case RID_CASE:
10785 case RID_DEFAULT:
10786 /* Looks like a labeled-statement with a case label.
10787 Parse the label, and then use tail recursion to parse
10788 the statement. */
10789 cp_parser_label_for_labeled_statement (parser, std_attrs);
10790 in_compound = false;
10791 goto restart;
10792
10793 case RID_IF:
10794 case RID_SWITCH:
10795 statement = cp_parser_selection_statement (parser, if_p, chain);
10796 break;
10797
10798 case RID_WHILE:
10799 case RID_DO:
10800 case RID_FOR:
10801 statement = cp_parser_iteration_statement (parser, if_p, false);
10802 break;
10803
10804 case RID_BREAK:
10805 case RID_CONTINUE:
10806 case RID_RETURN:
10807 case RID_GOTO:
10808 statement = cp_parser_jump_statement (parser);
10809 break;
10810
10811 /* Objective-C++ exception-handling constructs. */
10812 case RID_AT_TRY:
10813 case RID_AT_CATCH:
10814 case RID_AT_FINALLY:
10815 case RID_AT_SYNCHRONIZED:
10816 case RID_AT_THROW:
10817 statement = cp_parser_objc_statement (parser);
10818 break;
10819
10820 case RID_TRY:
10821 statement = cp_parser_try_block (parser);
10822 break;
10823
10824 case RID_NAMESPACE:
10825 /* This must be a namespace alias definition. */
10826 cp_parser_declaration_statement (parser);
10827 return;
10828
10829 case RID_TRANSACTION_ATOMIC:
10830 case RID_TRANSACTION_RELAXED:
10831 case RID_SYNCHRONIZED:
10832 case RID_ATOMIC_NOEXCEPT:
10833 case RID_ATOMIC_CANCEL:
10834 statement = cp_parser_transaction (parser, token);
10835 break;
10836 case RID_TRANSACTION_CANCEL:
10837 statement = cp_parser_transaction_cancel (parser);
10838 break;
10839
10840 default:
10841 /* It might be a keyword like `int' that can start a
10842 declaration-statement. */
10843 break;
10844 }
10845 }
10846 else if (token->type == CPP_NAME)
10847 {
10848 /* If the next token is a `:', then we are looking at a
10849 labeled-statement. */
10850 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10851 if (token->type == CPP_COLON)
10852 {
10853 /* Looks like a labeled-statement with an ordinary label.
10854 Parse the label, and then use tail recursion to parse
10855 the statement. */
10856
10857 cp_parser_label_for_labeled_statement (parser, std_attrs);
10858 in_compound = false;
10859 goto restart;
10860 }
10861 }
10862 /* Anything that starts with a `{' must be a compound-statement. */
10863 else if (token->type == CPP_OPEN_BRACE)
10864 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10865 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10866 a statement all its own. */
10867 else if (token->type == CPP_PRAGMA)
10868 {
10869 /* Only certain OpenMP pragmas are attached to statements, and thus
10870 are considered statements themselves. All others are not. In
10871 the context of a compound, accept the pragma as a "statement" and
10872 return so that we can check for a close brace. Otherwise we
10873 require a real statement and must go back and read one. */
10874 if (in_compound)
10875 cp_parser_pragma (parser, pragma_compound, if_p);
10876 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10877 goto restart;
10878 return;
10879 }
10880 else if (token->type == CPP_EOF)
10881 {
10882 cp_parser_error (parser, "expected statement");
10883 return;
10884 }
10885
10886 /* Everything else must be a declaration-statement or an
10887 expression-statement. Try for the declaration-statement
10888 first, unless we are looking at a `;', in which case we know that
10889 we have an expression-statement. */
10890 if (!statement)
10891 {
10892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10893 {
10894 if (std_attrs != NULL_TREE)
10895 {
10896 /* Attributes should be parsed as part of the the
10897 declaration, so let's un-parse them. */
10898 saved_tokens.rollback();
10899 std_attrs = NULL_TREE;
10900 }
10901
10902 cp_parser_parse_tentatively (parser);
10903 /* Try to parse the declaration-statement. */
10904 cp_parser_declaration_statement (parser);
10905 /* If that worked, we're done. */
10906 if (cp_parser_parse_definitely (parser))
10907 return;
10908 }
10909 /* All preceding labels have been parsed at this point. */
10910 if (loc_after_labels != NULL)
10911 *loc_after_labels = statement_location;
10912
10913 /* Look for an expression-statement instead. */
10914 statement = cp_parser_expression_statement (parser, in_statement_expr);
10915
10916 /* Handle [[fallthrough]];. */
10917 if (attribute_fallthrough_p (std_attrs))
10918 {
10919 /* The next token after the fallthrough attribute is ';'. */
10920 if (statement == NULL_TREE)
10921 {
10922 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10923 statement = build_call_expr_internal_loc (statement_location,
10924 IFN_FALLTHROUGH,
10925 void_type_node, 0);
10926 finish_expr_stmt (statement);
10927 }
10928 else
10929 warning_at (statement_location, OPT_Wattributes,
10930 "%<fallthrough%> attribute not followed by %<;%>");
10931 std_attrs = NULL_TREE;
10932 }
10933 }
10934
10935 /* Set the line number for the statement. */
10936 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10937 SET_EXPR_LOCATION (statement, statement_location);
10938
10939 /* Allow "[[fallthrough]];", but warn otherwise. */
10940 if (std_attrs != NULL_TREE)
10941 warning_at (attrs_location,
10942 OPT_Wattributes,
10943 "attributes at the beginning of statement are ignored");
10944 }
10945
10946 /* Parse the label for a labeled-statement, i.e.
10947
10948 identifier :
10949 case constant-expression :
10950 default :
10951
10952 GNU Extension:
10953 case constant-expression ... constant-expression : statement
10954
10955 When a label is parsed without errors, the label is added to the
10956 parse tree by the finish_* functions, so this function doesn't
10957 have to return the label. */
10958
10959 static void
10960 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10961 {
10962 cp_token *token;
10963 tree label = NULL_TREE;
10964 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10965
10966 /* The next token should be an identifier. */
10967 token = cp_lexer_peek_token (parser->lexer);
10968 if (token->type != CPP_NAME
10969 && token->type != CPP_KEYWORD)
10970 {
10971 cp_parser_error (parser, "expected labeled-statement");
10972 return;
10973 }
10974
10975 /* Remember whether this case or a user-defined label is allowed to fall
10976 through to. */
10977 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10978
10979 parser->colon_corrects_to_scope_p = false;
10980 switch (token->keyword)
10981 {
10982 case RID_CASE:
10983 {
10984 tree expr, expr_hi;
10985 cp_token *ellipsis;
10986
10987 /* Consume the `case' token. */
10988 cp_lexer_consume_token (parser->lexer);
10989 /* Parse the constant-expression. */
10990 expr = cp_parser_constant_expression (parser);
10991 if (check_for_bare_parameter_packs (expr))
10992 expr = error_mark_node;
10993
10994 ellipsis = cp_lexer_peek_token (parser->lexer);
10995 if (ellipsis->type == CPP_ELLIPSIS)
10996 {
10997 /* Consume the `...' token. */
10998 cp_lexer_consume_token (parser->lexer);
10999 expr_hi = cp_parser_constant_expression (parser);
11000 if (check_for_bare_parameter_packs (expr_hi))
11001 expr_hi = error_mark_node;
11002
11003 /* We don't need to emit warnings here, as the common code
11004 will do this for us. */
11005 }
11006 else
11007 expr_hi = NULL_TREE;
11008
11009 if (parser->in_switch_statement_p)
11010 {
11011 tree l = finish_case_label (token->location, expr, expr_hi);
11012 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11013 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11014 }
11015 else
11016 error_at (token->location,
11017 "case label %qE not within a switch statement",
11018 expr);
11019 }
11020 break;
11021
11022 case RID_DEFAULT:
11023 /* Consume the `default' token. */
11024 cp_lexer_consume_token (parser->lexer);
11025
11026 if (parser->in_switch_statement_p)
11027 {
11028 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11029 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11030 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11031 }
11032 else
11033 error_at (token->location, "case label not within a switch statement");
11034 break;
11035
11036 default:
11037 /* Anything else must be an ordinary label. */
11038 label = finish_label_stmt (cp_parser_identifier (parser));
11039 if (label && TREE_CODE (label) == LABEL_DECL)
11040 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11041 break;
11042 }
11043
11044 /* Require the `:' token. */
11045 cp_parser_require (parser, CPP_COLON, RT_COLON);
11046
11047 /* An ordinary label may optionally be followed by attributes.
11048 However, this is only permitted if the attributes are then
11049 followed by a semicolon. This is because, for backward
11050 compatibility, when parsing
11051 lab: __attribute__ ((unused)) int i;
11052 we want the attribute to attach to "i", not "lab". */
11053 if (label != NULL_TREE
11054 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11055 {
11056 tree attrs;
11057 cp_parser_parse_tentatively (parser);
11058 attrs = cp_parser_gnu_attributes_opt (parser);
11059 if (attrs == NULL_TREE
11060 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11061 cp_parser_abort_tentative_parse (parser);
11062 else if (!cp_parser_parse_definitely (parser))
11063 ;
11064 else
11065 attributes = chainon (attributes, attrs);
11066 }
11067
11068 if (attributes != NULL_TREE)
11069 cplus_decl_attributes (&label, attributes, 0);
11070
11071 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11072 }
11073
11074 /* Parse an expression-statement.
11075
11076 expression-statement:
11077 expression [opt] ;
11078
11079 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11080 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11081 indicates whether this expression-statement is part of an
11082 expression statement. */
11083
11084 static tree
11085 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11086 {
11087 tree statement = NULL_TREE;
11088 cp_token *token = cp_lexer_peek_token (parser->lexer);
11089 location_t loc = token->location;
11090
11091 /* There might be attribute fallthrough. */
11092 tree attr = cp_parser_gnu_attributes_opt (parser);
11093
11094 /* If the next token is a ';', then there is no expression
11095 statement. */
11096 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11097 {
11098 statement = cp_parser_expression (parser);
11099 if (statement == error_mark_node
11100 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11101 {
11102 cp_parser_skip_to_end_of_block_or_statement (parser);
11103 return error_mark_node;
11104 }
11105 }
11106
11107 /* Handle [[fallthrough]];. */
11108 if (attribute_fallthrough_p (attr))
11109 {
11110 /* The next token after the fallthrough attribute is ';'. */
11111 if (statement == NULL_TREE)
11112 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11113 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11114 void_type_node, 0);
11115 else
11116 warning_at (loc, OPT_Wattributes,
11117 "%<fallthrough%> attribute not followed by %<;%>");
11118 attr = NULL_TREE;
11119 }
11120
11121 /* Allow "[[fallthrough]];", but warn otherwise. */
11122 if (attr != NULL_TREE)
11123 warning_at (loc, OPT_Wattributes,
11124 "attributes at the beginning of statement are ignored");
11125
11126 /* Give a helpful message for "A<T>::type t;" and the like. */
11127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11128 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11129 {
11130 if (TREE_CODE (statement) == SCOPE_REF)
11131 error_at (token->location, "need %<typename%> before %qE because "
11132 "%qT is a dependent scope",
11133 statement, TREE_OPERAND (statement, 0));
11134 else if (is_overloaded_fn (statement)
11135 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11136 {
11137 /* A::A a; */
11138 tree fn = get_first_fn (statement);
11139 error_at (token->location,
11140 "%<%T::%D%> names the constructor, not the type",
11141 DECL_CONTEXT (fn), DECL_NAME (fn));
11142 }
11143 }
11144
11145 /* Consume the final `;'. */
11146 cp_parser_consume_semicolon_at_end_of_statement (parser);
11147
11148 if (in_statement_expr
11149 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11150 /* This is the final expression statement of a statement
11151 expression. */
11152 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11153 else if (statement)
11154 statement = finish_expr_stmt (statement);
11155
11156 return statement;
11157 }
11158
11159 /* Parse a compound-statement.
11160
11161 compound-statement:
11162 { statement-seq [opt] }
11163
11164 GNU extension:
11165
11166 compound-statement:
11167 { label-declaration-seq [opt] statement-seq [opt] }
11168
11169 label-declaration-seq:
11170 label-declaration
11171 label-declaration-seq label-declaration
11172
11173 Returns a tree representing the statement. */
11174
11175 static tree
11176 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11177 int bcs_flags, bool function_body)
11178 {
11179 tree compound_stmt;
11180 matching_braces braces;
11181
11182 /* Consume the `{'. */
11183 if (!braces.require_open (parser))
11184 return error_mark_node;
11185 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11186 && !function_body && cxx_dialect < cxx14)
11187 pedwarn (input_location, OPT_Wpedantic,
11188 "compound-statement in %<constexpr%> function");
11189 /* Begin the compound-statement. */
11190 compound_stmt = begin_compound_stmt (bcs_flags);
11191 /* If the next keyword is `__label__' we have a label declaration. */
11192 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11193 cp_parser_label_declaration (parser);
11194 /* Parse an (optional) statement-seq. */
11195 cp_parser_statement_seq_opt (parser, in_statement_expr);
11196 /* Finish the compound-statement. */
11197 finish_compound_stmt (compound_stmt);
11198 /* Consume the `}'. */
11199 braces.require_close (parser);
11200
11201 return compound_stmt;
11202 }
11203
11204 /* Parse an (optional) statement-seq.
11205
11206 statement-seq:
11207 statement
11208 statement-seq [opt] statement */
11209
11210 static void
11211 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11212 {
11213 /* Scan statements until there aren't any more. */
11214 while (true)
11215 {
11216 cp_token *token = cp_lexer_peek_token (parser->lexer);
11217
11218 /* If we are looking at a `}', then we have run out of
11219 statements; the same is true if we have reached the end
11220 of file, or have stumbled upon a stray '@end'. */
11221 if (token->type == CPP_CLOSE_BRACE
11222 || token->type == CPP_EOF
11223 || token->type == CPP_PRAGMA_EOL
11224 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11225 break;
11226
11227 /* If we are in a compound statement and find 'else' then
11228 something went wrong. */
11229 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11230 {
11231 if (parser->in_statement & IN_IF_STMT)
11232 break;
11233 else
11234 {
11235 token = cp_lexer_consume_token (parser->lexer);
11236 error_at (token->location, "%<else%> without a previous %<if%>");
11237 }
11238 }
11239
11240 /* Parse the statement. */
11241 cp_parser_statement (parser, in_statement_expr, true, NULL);
11242 }
11243 }
11244
11245 /* Return true if we're looking at (init; cond), false otherwise. */
11246
11247 static bool
11248 cp_parser_init_statement_p (cp_parser *parser)
11249 {
11250 /* Save tokens so that we can put them back. */
11251 cp_lexer_save_tokens (parser->lexer);
11252
11253 /* Look for ';' that is not nested in () or {}. */
11254 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11255 /*recovering=*/false,
11256 CPP_SEMICOLON,
11257 /*consume_paren=*/false);
11258
11259 /* Roll back the tokens we skipped. */
11260 cp_lexer_rollback_tokens (parser->lexer);
11261
11262 return ret == -1;
11263 }
11264
11265 /* Parse a selection-statement.
11266
11267 selection-statement:
11268 if ( init-statement [opt] condition ) statement
11269 if ( init-statement [opt] condition ) statement else statement
11270 switch ( init-statement [opt] condition ) statement
11271
11272 Returns the new IF_STMT or SWITCH_STMT.
11273
11274 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11275 is a (possibly labeled) if statement which is not enclosed in
11276 braces and has an else clause. This is used to implement
11277 -Wparentheses.
11278
11279 CHAIN is a vector of if-else-if conditions. This is used to implement
11280 -Wduplicated-cond. */
11281
11282 static tree
11283 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11284 vec<tree> *chain)
11285 {
11286 cp_token *token;
11287 enum rid keyword;
11288 token_indent_info guard_tinfo;
11289
11290 if (if_p != NULL)
11291 *if_p = false;
11292
11293 /* Peek at the next token. */
11294 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11295 guard_tinfo = get_token_indent_info (token);
11296
11297 /* See what kind of keyword it is. */
11298 keyword = token->keyword;
11299 switch (keyword)
11300 {
11301 case RID_IF:
11302 case RID_SWITCH:
11303 {
11304 tree statement;
11305 tree condition;
11306
11307 bool cx = false;
11308 if (keyword == RID_IF
11309 && cp_lexer_next_token_is_keyword (parser->lexer,
11310 RID_CONSTEXPR))
11311 {
11312 cx = true;
11313 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11314 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11315 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11316 "with -std=c++17 or -std=gnu++17");
11317 }
11318
11319 /* Look for the `('. */
11320 matching_parens parens;
11321 if (!parens.require_open (parser))
11322 {
11323 cp_parser_skip_to_end_of_statement (parser);
11324 return error_mark_node;
11325 }
11326
11327 /* Begin the selection-statement. */
11328 if (keyword == RID_IF)
11329 {
11330 statement = begin_if_stmt ();
11331 IF_STMT_CONSTEXPR_P (statement) = cx;
11332 }
11333 else
11334 statement = begin_switch_stmt ();
11335
11336 /* Parse the optional init-statement. */
11337 if (cp_parser_init_statement_p (parser))
11338 {
11339 tree decl;
11340 if (cxx_dialect < cxx17)
11341 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11342 "init-statement in selection statements only available "
11343 "with -std=c++17 or -std=gnu++17");
11344 cp_parser_init_statement (parser, &decl);
11345 }
11346
11347 /* Parse the condition. */
11348 condition = cp_parser_condition (parser);
11349 /* Look for the `)'. */
11350 if (!parens.require_close (parser))
11351 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11352 /*consume_paren=*/true);
11353
11354 if (keyword == RID_IF)
11355 {
11356 bool nested_if;
11357 unsigned char in_statement;
11358
11359 /* Add the condition. */
11360 condition = finish_if_stmt_cond (condition, statement);
11361
11362 if (warn_duplicated_cond)
11363 warn_duplicated_cond_add_or_warn (token->location, condition,
11364 &chain);
11365
11366 /* Parse the then-clause. */
11367 in_statement = parser->in_statement;
11368 parser->in_statement |= IN_IF_STMT;
11369
11370 /* Outside a template, the non-selected branch of a constexpr
11371 if is a 'discarded statement', i.e. unevaluated. */
11372 bool was_discarded = in_discarded_stmt;
11373 bool discard_then = (cx && !processing_template_decl
11374 && integer_zerop (condition));
11375 if (discard_then)
11376 {
11377 in_discarded_stmt = true;
11378 ++c_inhibit_evaluation_warnings;
11379 }
11380
11381 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11382 guard_tinfo);
11383
11384 parser->in_statement = in_statement;
11385
11386 finish_then_clause (statement);
11387
11388 if (discard_then)
11389 {
11390 THEN_CLAUSE (statement) = NULL_TREE;
11391 in_discarded_stmt = was_discarded;
11392 --c_inhibit_evaluation_warnings;
11393 }
11394
11395 /* If the next token is `else', parse the else-clause. */
11396 if (cp_lexer_next_token_is_keyword (parser->lexer,
11397 RID_ELSE))
11398 {
11399 bool discard_else = (cx && !processing_template_decl
11400 && integer_nonzerop (condition));
11401 if (discard_else)
11402 {
11403 in_discarded_stmt = true;
11404 ++c_inhibit_evaluation_warnings;
11405 }
11406
11407 guard_tinfo
11408 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11409 /* Consume the `else' keyword. */
11410 cp_lexer_consume_token (parser->lexer);
11411 if (warn_duplicated_cond)
11412 {
11413 if (cp_lexer_next_token_is_keyword (parser->lexer,
11414 RID_IF)
11415 && chain == NULL)
11416 {
11417 /* We've got "if (COND) else if (COND2)". Start
11418 the condition chain and add COND as the first
11419 element. */
11420 chain = new vec<tree> ();
11421 if (!CONSTANT_CLASS_P (condition)
11422 && !TREE_SIDE_EFFECTS (condition))
11423 {
11424 /* Wrap it in a NOP_EXPR so that we can set the
11425 location of the condition. */
11426 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11427 condition);
11428 SET_EXPR_LOCATION (e, token->location);
11429 chain->safe_push (e);
11430 }
11431 }
11432 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11433 RID_IF))
11434 {
11435 /* This is if-else without subsequent if. Zap the
11436 condition chain; we would have already warned at
11437 this point. */
11438 delete chain;
11439 chain = NULL;
11440 }
11441 }
11442 begin_else_clause (statement);
11443 /* Parse the else-clause. */
11444 cp_parser_implicitly_scoped_statement (parser, NULL,
11445 guard_tinfo, chain);
11446
11447 finish_else_clause (statement);
11448
11449 /* If we are currently parsing a then-clause, then
11450 IF_P will not be NULL. We set it to true to
11451 indicate that this if statement has an else clause.
11452 This may trigger the Wparentheses warning below
11453 when we get back up to the parent if statement. */
11454 if (if_p != NULL)
11455 *if_p = true;
11456
11457 if (discard_else)
11458 {
11459 ELSE_CLAUSE (statement) = NULL_TREE;
11460 in_discarded_stmt = was_discarded;
11461 --c_inhibit_evaluation_warnings;
11462 }
11463 }
11464 else
11465 {
11466 /* This if statement does not have an else clause. If
11467 NESTED_IF is true, then the then-clause has an if
11468 statement which does have an else clause. We warn
11469 about the potential ambiguity. */
11470 if (nested_if)
11471 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11472 "suggest explicit braces to avoid ambiguous"
11473 " %<else%>");
11474 if (warn_duplicated_cond)
11475 {
11476 /* We don't need the condition chain anymore. */
11477 delete chain;
11478 chain = NULL;
11479 }
11480 }
11481
11482 /* Now we're all done with the if-statement. */
11483 finish_if_stmt (statement);
11484 }
11485 else
11486 {
11487 bool in_switch_statement_p;
11488 unsigned char in_statement;
11489
11490 /* Add the condition. */
11491 finish_switch_cond (condition, statement);
11492
11493 /* Parse the body of the switch-statement. */
11494 in_switch_statement_p = parser->in_switch_statement_p;
11495 in_statement = parser->in_statement;
11496 parser->in_switch_statement_p = true;
11497 parser->in_statement |= IN_SWITCH_STMT;
11498 cp_parser_implicitly_scoped_statement (parser, if_p,
11499 guard_tinfo);
11500 parser->in_switch_statement_p = in_switch_statement_p;
11501 parser->in_statement = in_statement;
11502
11503 /* Now we're all done with the switch-statement. */
11504 finish_switch_stmt (statement);
11505 }
11506
11507 return statement;
11508 }
11509 break;
11510
11511 default:
11512 cp_parser_error (parser, "expected selection-statement");
11513 return error_mark_node;
11514 }
11515 }
11516
11517 /* Parse a condition.
11518
11519 condition:
11520 expression
11521 type-specifier-seq declarator = initializer-clause
11522 type-specifier-seq declarator braced-init-list
11523
11524 GNU Extension:
11525
11526 condition:
11527 type-specifier-seq declarator asm-specification [opt]
11528 attributes [opt] = assignment-expression
11529
11530 Returns the expression that should be tested. */
11531
11532 static tree
11533 cp_parser_condition (cp_parser* parser)
11534 {
11535 cp_decl_specifier_seq type_specifiers;
11536 const char *saved_message;
11537 int declares_class_or_enum;
11538
11539 /* Try the declaration first. */
11540 cp_parser_parse_tentatively (parser);
11541 /* New types are not allowed in the type-specifier-seq for a
11542 condition. */
11543 saved_message = parser->type_definition_forbidden_message;
11544 parser->type_definition_forbidden_message
11545 = G_("types may not be defined in conditions");
11546 /* Parse the type-specifier-seq. */
11547 cp_parser_decl_specifier_seq (parser,
11548 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11549 &type_specifiers,
11550 &declares_class_or_enum);
11551 /* Restore the saved message. */
11552 parser->type_definition_forbidden_message = saved_message;
11553 /* If all is well, we might be looking at a declaration. */
11554 if (!cp_parser_error_occurred (parser))
11555 {
11556 tree decl;
11557 tree asm_specification;
11558 tree attributes;
11559 cp_declarator *declarator;
11560 tree initializer = NULL_TREE;
11561
11562 /* Parse the declarator. */
11563 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11564 /*ctor_dtor_or_conv_p=*/NULL,
11565 /*parenthesized_p=*/NULL,
11566 /*member_p=*/false,
11567 /*friend_p=*/false);
11568 /* Parse the attributes. */
11569 attributes = cp_parser_attributes_opt (parser);
11570 /* Parse the asm-specification. */
11571 asm_specification = cp_parser_asm_specification_opt (parser);
11572 /* If the next token is not an `=' or '{', then we might still be
11573 looking at an expression. For example:
11574
11575 if (A(a).x)
11576
11577 looks like a decl-specifier-seq and a declarator -- but then
11578 there is no `=', so this is an expression. */
11579 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11580 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11581 cp_parser_simulate_error (parser);
11582
11583 /* If we did see an `=' or '{', then we are looking at a declaration
11584 for sure. */
11585 if (cp_parser_parse_definitely (parser))
11586 {
11587 tree pushed_scope;
11588 bool non_constant_p;
11589 int flags = LOOKUP_ONLYCONVERTING;
11590
11591 /* Create the declaration. */
11592 decl = start_decl (declarator, &type_specifiers,
11593 /*initialized_p=*/true,
11594 attributes, /*prefix_attributes=*/NULL_TREE,
11595 &pushed_scope);
11596
11597 /* Parse the initializer. */
11598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11599 {
11600 initializer = cp_parser_braced_list (parser, &non_constant_p);
11601 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11602 flags = 0;
11603 }
11604 else
11605 {
11606 /* Consume the `='. */
11607 cp_parser_require (parser, CPP_EQ, RT_EQ);
11608 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11609 }
11610 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11611 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11612
11613 /* Process the initializer. */
11614 cp_finish_decl (decl,
11615 initializer, !non_constant_p,
11616 asm_specification,
11617 flags);
11618
11619 if (pushed_scope)
11620 pop_scope (pushed_scope);
11621
11622 return convert_from_reference (decl);
11623 }
11624 }
11625 /* If we didn't even get past the declarator successfully, we are
11626 definitely not looking at a declaration. */
11627 else
11628 cp_parser_abort_tentative_parse (parser);
11629
11630 /* Otherwise, we are looking at an expression. */
11631 return cp_parser_expression (parser);
11632 }
11633
11634 /* Parses a for-statement or range-for-statement until the closing ')',
11635 not included. */
11636
11637 static tree
11638 cp_parser_for (cp_parser *parser, bool ivdep)
11639 {
11640 tree init, scope, decl;
11641 bool is_range_for;
11642
11643 /* Begin the for-statement. */
11644 scope = begin_for_scope (&init);
11645
11646 /* Parse the initialization. */
11647 is_range_for = cp_parser_init_statement (parser, &decl);
11648
11649 if (is_range_for)
11650 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11651 else
11652 return cp_parser_c_for (parser, scope, init, ivdep);
11653 }
11654
11655 static tree
11656 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11657 {
11658 /* Normal for loop */
11659 tree condition = NULL_TREE;
11660 tree expression = NULL_TREE;
11661 tree stmt;
11662
11663 stmt = begin_for_stmt (scope, init);
11664 /* The init-statement has already been parsed in
11665 cp_parser_init_statement, so no work is needed here. */
11666 finish_init_stmt (stmt);
11667
11668 /* If there's a condition, process it. */
11669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11670 condition = cp_parser_condition (parser);
11671 else if (ivdep)
11672 {
11673 cp_parser_error (parser, "missing loop condition in loop with "
11674 "%<GCC ivdep%> pragma");
11675 condition = error_mark_node;
11676 }
11677 finish_for_cond (condition, stmt, ivdep);
11678 /* Look for the `;'. */
11679 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11680
11681 /* If there's an expression, process it. */
11682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11683 expression = cp_parser_expression (parser);
11684 finish_for_expr (expression, stmt);
11685
11686 return stmt;
11687 }
11688
11689 /* Tries to parse a range-based for-statement:
11690
11691 range-based-for:
11692 decl-specifier-seq declarator : expression
11693
11694 The decl-specifier-seq declarator and the `:' are already parsed by
11695 cp_parser_init_statement. If processing_template_decl it returns a
11696 newly created RANGE_FOR_STMT; if not, it is converted to a
11697 regular FOR_STMT. */
11698
11699 static tree
11700 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11701 bool ivdep)
11702 {
11703 tree stmt, range_expr;
11704 auto_vec <cxx_binding *, 16> bindings;
11705 auto_vec <tree, 16> names;
11706 tree decomp_first_name = NULL_TREE;
11707 unsigned int decomp_cnt = 0;
11708
11709 /* Get the range declaration momentarily out of the way so that
11710 the range expression doesn't clash with it. */
11711 if (range_decl != error_mark_node)
11712 {
11713 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11714 {
11715 tree v = DECL_VALUE_EXPR (range_decl);
11716 /* For decomposition declaration get all of the corresponding
11717 declarations out of the way. */
11718 if (TREE_CODE (v) == ARRAY_REF
11719 && VAR_P (TREE_OPERAND (v, 0))
11720 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11721 {
11722 tree d = range_decl;
11723 range_decl = TREE_OPERAND (v, 0);
11724 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11725 decomp_first_name = d;
11726 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11727 {
11728 tree name = DECL_NAME (d);
11729 names.safe_push (name);
11730 bindings.safe_push (IDENTIFIER_BINDING (name));
11731 IDENTIFIER_BINDING (name)
11732 = IDENTIFIER_BINDING (name)->previous;
11733 }
11734 }
11735 }
11736 if (names.is_empty ())
11737 {
11738 tree name = DECL_NAME (range_decl);
11739 names.safe_push (name);
11740 bindings.safe_push (IDENTIFIER_BINDING (name));
11741 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11742 }
11743 }
11744
11745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11746 {
11747 bool expr_non_constant_p;
11748 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11749 }
11750 else
11751 range_expr = cp_parser_expression (parser);
11752
11753 /* Put the range declaration(s) back into scope. */
11754 for (unsigned int i = 0; i < names.length (); i++)
11755 {
11756 cxx_binding *binding = bindings[i];
11757 binding->previous = IDENTIFIER_BINDING (names[i]);
11758 IDENTIFIER_BINDING (names[i]) = binding;
11759 }
11760
11761 /* If in template, STMT is converted to a normal for-statement
11762 at instantiation. If not, it is done just ahead. */
11763 if (processing_template_decl)
11764 {
11765 if (check_for_bare_parameter_packs (range_expr))
11766 range_expr = error_mark_node;
11767 stmt = begin_range_for_stmt (scope, init);
11768 if (ivdep)
11769 RANGE_FOR_IVDEP (stmt) = 1;
11770 finish_range_for_decl (stmt, range_decl, range_expr);
11771 if (!type_dependent_expression_p (range_expr)
11772 /* do_auto_deduction doesn't mess with template init-lists. */
11773 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11774 do_range_for_auto_deduction (range_decl, range_expr);
11775 }
11776 else
11777 {
11778 stmt = begin_for_stmt (scope, init);
11779 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11780 decomp_first_name, decomp_cnt, ivdep);
11781 }
11782 return stmt;
11783 }
11784
11785 /* Subroutine of cp_convert_range_for: given the initializer expression,
11786 builds up the range temporary. */
11787
11788 static tree
11789 build_range_temp (tree range_expr)
11790 {
11791 tree range_type, range_temp;
11792
11793 /* Find out the type deduced by the declaration
11794 `auto &&__range = range_expr'. */
11795 range_type = cp_build_reference_type (make_auto (), true);
11796 range_type = do_auto_deduction (range_type, range_expr,
11797 type_uses_auto (range_type));
11798
11799 /* Create the __range variable. */
11800 range_temp = build_decl (input_location, VAR_DECL,
11801 get_identifier ("__for_range"), range_type);
11802 TREE_USED (range_temp) = 1;
11803 DECL_ARTIFICIAL (range_temp) = 1;
11804
11805 return range_temp;
11806 }
11807
11808 /* Used by cp_parser_range_for in template context: we aren't going to
11809 do a full conversion yet, but we still need to resolve auto in the
11810 type of the for-range-declaration if present. This is basically
11811 a shortcut version of cp_convert_range_for. */
11812
11813 static void
11814 do_range_for_auto_deduction (tree decl, tree range_expr)
11815 {
11816 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11817 if (auto_node)
11818 {
11819 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11820 range_temp = convert_from_reference (build_range_temp (range_expr));
11821 iter_type = (cp_parser_perform_range_for_lookup
11822 (range_temp, &begin_dummy, &end_dummy));
11823 if (iter_type)
11824 {
11825 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11826 iter_type);
11827 iter_decl = build_x_indirect_ref (input_location, iter_decl,
11828 RO_UNARY_STAR,
11829 tf_warning_or_error);
11830 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11831 iter_decl, auto_node);
11832 }
11833 }
11834 }
11835
11836 /* Converts a range-based for-statement into a normal
11837 for-statement, as per the definition.
11838
11839 for (RANGE_DECL : RANGE_EXPR)
11840 BLOCK
11841
11842 should be equivalent to:
11843
11844 {
11845 auto &&__range = RANGE_EXPR;
11846 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11847 __begin != __end;
11848 ++__begin)
11849 {
11850 RANGE_DECL = *__begin;
11851 BLOCK
11852 }
11853 }
11854
11855 If RANGE_EXPR is an array:
11856 BEGIN_EXPR = __range
11857 END_EXPR = __range + ARRAY_SIZE(__range)
11858 Else if RANGE_EXPR has a member 'begin' or 'end':
11859 BEGIN_EXPR = __range.begin()
11860 END_EXPR = __range.end()
11861 Else:
11862 BEGIN_EXPR = begin(__range)
11863 END_EXPR = end(__range);
11864
11865 If __range has a member 'begin' but not 'end', or vice versa, we must
11866 still use the second alternative (it will surely fail, however).
11867 When calling begin()/end() in the third alternative we must use
11868 argument dependent lookup, but always considering 'std' as an associated
11869 namespace. */
11870
11871 tree
11872 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11873 tree decomp_first_name, unsigned int decomp_cnt,
11874 bool ivdep)
11875 {
11876 tree begin, end;
11877 tree iter_type, begin_expr, end_expr;
11878 tree condition, expression;
11879
11880 range_expr = mark_lvalue_use (range_expr);
11881
11882 if (range_decl == error_mark_node || range_expr == error_mark_node)
11883 /* If an error happened previously do nothing or else a lot of
11884 unhelpful errors would be issued. */
11885 begin_expr = end_expr = iter_type = error_mark_node;
11886 else
11887 {
11888 tree range_temp;
11889
11890 if (VAR_P (range_expr)
11891 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11892 /* Can't bind a reference to an array of runtime bound. */
11893 range_temp = range_expr;
11894 else
11895 {
11896 range_temp = build_range_temp (range_expr);
11897 pushdecl (range_temp);
11898 cp_finish_decl (range_temp, range_expr,
11899 /*is_constant_init*/false, NULL_TREE,
11900 LOOKUP_ONLYCONVERTING);
11901 range_temp = convert_from_reference (range_temp);
11902 }
11903 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11904 &begin_expr, &end_expr);
11905 }
11906
11907 /* The new for initialization statement. */
11908 begin = build_decl (input_location, VAR_DECL,
11909 get_identifier ("__for_begin"), iter_type);
11910 TREE_USED (begin) = 1;
11911 DECL_ARTIFICIAL (begin) = 1;
11912 pushdecl (begin);
11913 cp_finish_decl (begin, begin_expr,
11914 /*is_constant_init*/false, NULL_TREE,
11915 LOOKUP_ONLYCONVERTING);
11916
11917 if (cxx_dialect >= cxx17)
11918 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11919 end = build_decl (input_location, VAR_DECL,
11920 get_identifier ("__for_end"), iter_type);
11921 TREE_USED (end) = 1;
11922 DECL_ARTIFICIAL (end) = 1;
11923 pushdecl (end);
11924 cp_finish_decl (end, end_expr,
11925 /*is_constant_init*/false, NULL_TREE,
11926 LOOKUP_ONLYCONVERTING);
11927
11928 finish_init_stmt (statement);
11929
11930 /* The new for condition. */
11931 condition = build_x_binary_op (input_location, NE_EXPR,
11932 begin, ERROR_MARK,
11933 end, ERROR_MARK,
11934 NULL, tf_warning_or_error);
11935 finish_for_cond (condition, statement, ivdep);
11936
11937 /* The new increment expression. */
11938 expression = finish_unary_op_expr (input_location,
11939 PREINCREMENT_EXPR, begin,
11940 tf_warning_or_error);
11941 finish_for_expr (expression, statement);
11942
11943 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11944 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11945
11946 /* The declaration is initialized with *__begin inside the loop body. */
11947 cp_finish_decl (range_decl,
11948 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
11949 tf_warning_or_error),
11950 /*is_constant_init*/false, NULL_TREE,
11951 LOOKUP_ONLYCONVERTING);
11952 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11953 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11954
11955 return statement;
11956 }
11957
11958 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11959 We need to solve both at the same time because the method used
11960 depends on the existence of members begin or end.
11961 Returns the type deduced for the iterator expression. */
11962
11963 static tree
11964 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11965 {
11966 if (error_operand_p (range))
11967 {
11968 *begin = *end = error_mark_node;
11969 return error_mark_node;
11970 }
11971
11972 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11973 {
11974 error ("range-based %<for%> expression of type %qT "
11975 "has incomplete type", TREE_TYPE (range));
11976 *begin = *end = error_mark_node;
11977 return error_mark_node;
11978 }
11979 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11980 {
11981 /* If RANGE is an array, we will use pointer arithmetic. */
11982 *begin = decay_conversion (range, tf_warning_or_error);
11983 *end = build_binary_op (input_location, PLUS_EXPR,
11984 range,
11985 array_type_nelts_top (TREE_TYPE (range)),
11986 false);
11987 return TREE_TYPE (*begin);
11988 }
11989 else
11990 {
11991 /* If it is not an array, we must do a bit of magic. */
11992 tree id_begin, id_end;
11993 tree member_begin, member_end;
11994
11995 *begin = *end = error_mark_node;
11996
11997 id_begin = get_identifier ("begin");
11998 id_end = get_identifier ("end");
11999 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12000 /*protect=*/2, /*want_type=*/false,
12001 tf_warning_or_error);
12002 member_end = lookup_member (TREE_TYPE (range), id_end,
12003 /*protect=*/2, /*want_type=*/false,
12004 tf_warning_or_error);
12005
12006 if (member_begin != NULL_TREE || member_end != NULL_TREE)
12007 {
12008 /* Use the member functions. */
12009 if (member_begin != NULL_TREE)
12010 *begin = cp_parser_range_for_member_function (range, id_begin);
12011 else
12012 error ("range-based %<for%> expression of type %qT has an "
12013 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12014
12015 if (member_end != NULL_TREE)
12016 *end = cp_parser_range_for_member_function (range, id_end);
12017 else
12018 error ("range-based %<for%> expression of type %qT has a "
12019 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12020 }
12021 else
12022 {
12023 /* Use global functions with ADL. */
12024 vec<tree, va_gc> *vec;
12025 vec = make_tree_vector ();
12026
12027 vec_safe_push (vec, range);
12028
12029 member_begin = perform_koenig_lookup (id_begin, vec,
12030 tf_warning_or_error);
12031 *begin = finish_call_expr (member_begin, &vec, false, true,
12032 tf_warning_or_error);
12033 member_end = perform_koenig_lookup (id_end, vec,
12034 tf_warning_or_error);
12035 *end = finish_call_expr (member_end, &vec, false, true,
12036 tf_warning_or_error);
12037
12038 release_tree_vector (vec);
12039 }
12040
12041 /* Last common checks. */
12042 if (*begin == error_mark_node || *end == error_mark_node)
12043 {
12044 /* If one of the expressions is an error do no more checks. */
12045 *begin = *end = error_mark_node;
12046 return error_mark_node;
12047 }
12048 else if (type_dependent_expression_p (*begin)
12049 || type_dependent_expression_p (*end))
12050 /* Can happen, when, eg, in a template context, Koenig lookup
12051 can't resolve begin/end (c++/58503). */
12052 return NULL_TREE;
12053 else
12054 {
12055 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12056 /* The unqualified type of the __begin and __end temporaries should
12057 be the same, as required by the multiple auto declaration. */
12058 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12059 {
12060 if (cxx_dialect >= cxx17
12061 && (build_x_binary_op (input_location, NE_EXPR,
12062 *begin, ERROR_MARK,
12063 *end, ERROR_MARK,
12064 NULL, tf_none)
12065 != error_mark_node))
12066 /* P0184R0 allows __begin and __end to have different types,
12067 but make sure they are comparable so we can give a better
12068 diagnostic. */;
12069 else
12070 error ("inconsistent begin/end types in range-based %<for%> "
12071 "statement: %qT and %qT",
12072 TREE_TYPE (*begin), TREE_TYPE (*end));
12073 }
12074 return iter_type;
12075 }
12076 }
12077 }
12078
12079 /* Helper function for cp_parser_perform_range_for_lookup.
12080 Builds a tree for RANGE.IDENTIFIER(). */
12081
12082 static tree
12083 cp_parser_range_for_member_function (tree range, tree identifier)
12084 {
12085 tree member, res;
12086 vec<tree, va_gc> *vec;
12087
12088 member = finish_class_member_access_expr (range, identifier,
12089 false, tf_warning_or_error);
12090 if (member == error_mark_node)
12091 return error_mark_node;
12092
12093 vec = make_tree_vector ();
12094 res = finish_call_expr (member, &vec,
12095 /*disallow_virtual=*/false,
12096 /*koenig_p=*/false,
12097 tf_warning_or_error);
12098 release_tree_vector (vec);
12099 return res;
12100 }
12101
12102 /* Parse an iteration-statement.
12103
12104 iteration-statement:
12105 while ( condition ) statement
12106 do statement while ( expression ) ;
12107 for ( init-statement condition [opt] ; expression [opt] )
12108 statement
12109
12110 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12111
12112 static tree
12113 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12114 {
12115 cp_token *token;
12116 enum rid keyword;
12117 tree statement;
12118 unsigned char in_statement;
12119 token_indent_info guard_tinfo;
12120
12121 /* Peek at the next token. */
12122 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12123 if (!token)
12124 return error_mark_node;
12125
12126 guard_tinfo = get_token_indent_info (token);
12127
12128 /* Remember whether or not we are already within an iteration
12129 statement. */
12130 in_statement = parser->in_statement;
12131
12132 /* See what kind of keyword it is. */
12133 keyword = token->keyword;
12134 switch (keyword)
12135 {
12136 case RID_WHILE:
12137 {
12138 tree condition;
12139
12140 /* Begin the while-statement. */
12141 statement = begin_while_stmt ();
12142 /* Look for the `('. */
12143 matching_parens parens;
12144 parens.require_open (parser);
12145 /* Parse the condition. */
12146 condition = cp_parser_condition (parser);
12147 finish_while_stmt_cond (condition, statement, ivdep);
12148 /* Look for the `)'. */
12149 parens.require_close (parser);
12150 /* Parse the dependent statement. */
12151 parser->in_statement = IN_ITERATION_STMT;
12152 bool prev = note_iteration_stmt_body_start ();
12153 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12154 note_iteration_stmt_body_end (prev);
12155 parser->in_statement = in_statement;
12156 /* We're done with the while-statement. */
12157 finish_while_stmt (statement);
12158 }
12159 break;
12160
12161 case RID_DO:
12162 {
12163 tree expression;
12164
12165 /* Begin the do-statement. */
12166 statement = begin_do_stmt ();
12167 /* Parse the body of the do-statement. */
12168 parser->in_statement = IN_ITERATION_STMT;
12169 bool prev = note_iteration_stmt_body_start ();
12170 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12171 note_iteration_stmt_body_end (prev);
12172 parser->in_statement = in_statement;
12173 finish_do_body (statement);
12174 /* Look for the `while' keyword. */
12175 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12176 /* Look for the `('. */
12177 matching_parens parens;
12178 parens.require_open (parser);
12179 /* Parse the expression. */
12180 expression = cp_parser_expression (parser);
12181 /* We're done with the do-statement. */
12182 finish_do_stmt (expression, statement, ivdep);
12183 /* Look for the `)'. */
12184 parens.require_close (parser);
12185 /* Look for the `;'. */
12186 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12187 }
12188 break;
12189
12190 case RID_FOR:
12191 {
12192 /* Look for the `('. */
12193 matching_parens parens;
12194 parens.require_open (parser);
12195
12196 statement = cp_parser_for (parser, ivdep);
12197
12198 /* Look for the `)'. */
12199 parens.require_close (parser);
12200
12201 /* Parse the body of the for-statement. */
12202 parser->in_statement = IN_ITERATION_STMT;
12203 bool prev = note_iteration_stmt_body_start ();
12204 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12205 note_iteration_stmt_body_end (prev);
12206 parser->in_statement = in_statement;
12207
12208 /* We're done with the for-statement. */
12209 finish_for_stmt (statement);
12210 }
12211 break;
12212
12213 default:
12214 cp_parser_error (parser, "expected iteration-statement");
12215 statement = error_mark_node;
12216 break;
12217 }
12218
12219 return statement;
12220 }
12221
12222 /* Parse a init-statement or the declarator of a range-based-for.
12223 Returns true if a range-based-for declaration is seen.
12224
12225 init-statement:
12226 expression-statement
12227 simple-declaration */
12228
12229 static bool
12230 cp_parser_init_statement (cp_parser* parser, tree *decl)
12231 {
12232 /* If the next token is a `;', then we have an empty
12233 expression-statement. Grammatically, this is also a
12234 simple-declaration, but an invalid one, because it does not
12235 declare anything. Therefore, if we did not handle this case
12236 specially, we would issue an error message about an invalid
12237 declaration. */
12238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12239 {
12240 bool is_range_for = false;
12241 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12242
12243 /* A colon is used in range-based for. */
12244 parser->colon_corrects_to_scope_p = false;
12245
12246 /* We're going to speculatively look for a declaration, falling back
12247 to an expression, if necessary. */
12248 cp_parser_parse_tentatively (parser);
12249 /* Parse the declaration. */
12250 cp_parser_simple_declaration (parser,
12251 /*function_definition_allowed_p=*/false,
12252 decl);
12253 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12254 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12255 {
12256 /* It is a range-for, consume the ':' */
12257 cp_lexer_consume_token (parser->lexer);
12258 is_range_for = true;
12259 if (cxx_dialect < cxx11)
12260 {
12261 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12262 "range-based %<for%> loops only available with "
12263 "-std=c++11 or -std=gnu++11");
12264 *decl = error_mark_node;
12265 }
12266 }
12267 else
12268 /* The ';' is not consumed yet because we told
12269 cp_parser_simple_declaration not to. */
12270 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12271
12272 if (cp_parser_parse_definitely (parser))
12273 return is_range_for;
12274 /* If the tentative parse failed, then we shall need to look for an
12275 expression-statement. */
12276 }
12277 /* If we are here, it is an expression-statement. */
12278 cp_parser_expression_statement (parser, NULL_TREE);
12279 return false;
12280 }
12281
12282 /* Parse a jump-statement.
12283
12284 jump-statement:
12285 break ;
12286 continue ;
12287 return expression [opt] ;
12288 return braced-init-list ;
12289 goto identifier ;
12290
12291 GNU extension:
12292
12293 jump-statement:
12294 goto * expression ;
12295
12296 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12297
12298 static tree
12299 cp_parser_jump_statement (cp_parser* parser)
12300 {
12301 tree statement = error_mark_node;
12302 cp_token *token;
12303 enum rid keyword;
12304 unsigned char in_statement;
12305
12306 /* Peek at the next token. */
12307 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12308 if (!token)
12309 return error_mark_node;
12310
12311 /* See what kind of keyword it is. */
12312 keyword = token->keyword;
12313 switch (keyword)
12314 {
12315 case RID_BREAK:
12316 in_statement = parser->in_statement & ~IN_IF_STMT;
12317 switch (in_statement)
12318 {
12319 case 0:
12320 error_at (token->location, "break statement not within loop or switch");
12321 break;
12322 default:
12323 gcc_assert ((in_statement & IN_SWITCH_STMT)
12324 || in_statement == IN_ITERATION_STMT);
12325 statement = finish_break_stmt ();
12326 if (in_statement == IN_ITERATION_STMT)
12327 break_maybe_infinite_loop ();
12328 break;
12329 case IN_OMP_BLOCK:
12330 error_at (token->location, "invalid exit from OpenMP structured block");
12331 break;
12332 case IN_OMP_FOR:
12333 error_at (token->location, "break statement used with OpenMP for loop");
12334 break;
12335 }
12336 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12337 break;
12338
12339 case RID_CONTINUE:
12340 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12341 {
12342 case 0:
12343 error_at (token->location, "continue statement not within a loop");
12344 break;
12345 /* Fall through. */
12346 case IN_ITERATION_STMT:
12347 case IN_OMP_FOR:
12348 statement = finish_continue_stmt ();
12349 break;
12350 case IN_OMP_BLOCK:
12351 error_at (token->location, "invalid exit from OpenMP structured block");
12352 break;
12353 default:
12354 gcc_unreachable ();
12355 }
12356 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12357 break;
12358
12359 case RID_RETURN:
12360 {
12361 tree expr;
12362 bool expr_non_constant_p;
12363
12364 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12365 {
12366 cp_lexer_set_source_position (parser->lexer);
12367 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12368 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12369 }
12370 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12371 expr = cp_parser_expression (parser);
12372 else
12373 /* If the next token is a `;', then there is no
12374 expression. */
12375 expr = NULL_TREE;
12376 /* Build the return-statement. */
12377 if (current_function_auto_return_pattern && in_discarded_stmt)
12378 /* Don't deduce from a discarded return statement. */;
12379 else
12380 statement = finish_return_stmt (expr);
12381 /* Look for the final `;'. */
12382 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12383 }
12384 break;
12385
12386 case RID_GOTO:
12387 if (parser->in_function_body
12388 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12389 {
12390 error ("%<goto%> in %<constexpr%> function");
12391 cp_function_chain->invalid_constexpr = true;
12392 }
12393
12394 /* Create the goto-statement. */
12395 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12396 {
12397 /* Issue a warning about this use of a GNU extension. */
12398 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12399 /* Consume the '*' token. */
12400 cp_lexer_consume_token (parser->lexer);
12401 /* Parse the dependent expression. */
12402 finish_goto_stmt (cp_parser_expression (parser));
12403 }
12404 else
12405 finish_goto_stmt (cp_parser_identifier (parser));
12406 /* Look for the final `;'. */
12407 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12408 break;
12409
12410 default:
12411 cp_parser_error (parser, "expected jump-statement");
12412 break;
12413 }
12414
12415 return statement;
12416 }
12417
12418 /* Parse a declaration-statement.
12419
12420 declaration-statement:
12421 block-declaration */
12422
12423 static void
12424 cp_parser_declaration_statement (cp_parser* parser)
12425 {
12426 void *p;
12427
12428 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12429 p = obstack_alloc (&declarator_obstack, 0);
12430
12431 /* Parse the block-declaration. */
12432 cp_parser_block_declaration (parser, /*statement_p=*/true);
12433
12434 /* Free any declarators allocated. */
12435 obstack_free (&declarator_obstack, p);
12436 }
12437
12438 /* Some dependent statements (like `if (cond) statement'), are
12439 implicitly in their own scope. In other words, if the statement is
12440 a single statement (as opposed to a compound-statement), it is
12441 none-the-less treated as if it were enclosed in braces. Any
12442 declarations appearing in the dependent statement are out of scope
12443 after control passes that point. This function parses a statement,
12444 but ensures that is in its own scope, even if it is not a
12445 compound-statement.
12446
12447 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12448 is a (possibly labeled) if statement which is not enclosed in
12449 braces and has an else clause. This is used to implement
12450 -Wparentheses.
12451
12452 CHAIN is a vector of if-else-if conditions. This is used to implement
12453 -Wduplicated-cond.
12454
12455 Returns the new statement. */
12456
12457 static tree
12458 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12459 const token_indent_info &guard_tinfo,
12460 vec<tree> *chain)
12461 {
12462 tree statement;
12463 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12464 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12465 token_indent_info body_tinfo
12466 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12467
12468 if (if_p != NULL)
12469 *if_p = false;
12470
12471 /* Mark if () ; with a special NOP_EXPR. */
12472 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12473 {
12474 cp_lexer_consume_token (parser->lexer);
12475 statement = add_stmt (build_empty_stmt (body_loc));
12476
12477 if (guard_tinfo.keyword == RID_IF
12478 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12479 warning_at (body_loc, OPT_Wempty_body,
12480 "suggest braces around empty body in an %<if%> statement");
12481 else if (guard_tinfo.keyword == RID_ELSE)
12482 warning_at (body_loc, OPT_Wempty_body,
12483 "suggest braces around empty body in an %<else%> statement");
12484 }
12485 /* if a compound is opened, we simply parse the statement directly. */
12486 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12487 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12488 /* If the token is not a `{', then we must take special action. */
12489 else
12490 {
12491 /* Create a compound-statement. */
12492 statement = begin_compound_stmt (0);
12493 /* Parse the dependent-statement. */
12494 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12495 &body_loc_after_labels);
12496 /* Finish the dummy compound-statement. */
12497 finish_compound_stmt (statement);
12498 }
12499
12500 token_indent_info next_tinfo
12501 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12502 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12503
12504 if (body_loc_after_labels != UNKNOWN_LOCATION
12505 && next_tinfo.type != CPP_SEMICOLON)
12506 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12507 guard_tinfo.location, guard_tinfo.keyword);
12508
12509 /* Return the statement. */
12510 return statement;
12511 }
12512
12513 /* For some dependent statements (like `while (cond) statement'), we
12514 have already created a scope. Therefore, even if the dependent
12515 statement is a compound-statement, we do not want to create another
12516 scope. */
12517
12518 static void
12519 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12520 const token_indent_info &guard_tinfo)
12521 {
12522 /* If the token is a `{', then we must take special action. */
12523 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12524 {
12525 token_indent_info body_tinfo
12526 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12527 location_t loc_after_labels = UNKNOWN_LOCATION;
12528
12529 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12530 &loc_after_labels);
12531 token_indent_info next_tinfo
12532 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12533 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12534
12535 if (loc_after_labels != UNKNOWN_LOCATION
12536 && next_tinfo.type != CPP_SEMICOLON)
12537 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12538 guard_tinfo.location,
12539 guard_tinfo.keyword);
12540 }
12541 else
12542 {
12543 /* Avoid calling cp_parser_compound_statement, so that we
12544 don't create a new scope. Do everything else by hand. */
12545 matching_braces braces;
12546 braces.require_open (parser);
12547 /* If the next keyword is `__label__' we have a label declaration. */
12548 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12549 cp_parser_label_declaration (parser);
12550 /* Parse an (optional) statement-seq. */
12551 cp_parser_statement_seq_opt (parser, NULL_TREE);
12552 braces.require_close (parser);
12553 }
12554 }
12555
12556 /* Declarations [gram.dcl.dcl] */
12557
12558 /* Parse an optional declaration-sequence.
12559
12560 declaration-seq:
12561 declaration
12562 declaration-seq declaration */
12563
12564 static void
12565 cp_parser_declaration_seq_opt (cp_parser* parser)
12566 {
12567 while (true)
12568 {
12569 cp_token *token;
12570
12571 token = cp_lexer_peek_token (parser->lexer);
12572
12573 if (token->type == CPP_CLOSE_BRACE
12574 || token->type == CPP_EOF
12575 || token->type == CPP_PRAGMA_EOL)
12576 break;
12577
12578 if (token->type == CPP_SEMICOLON)
12579 {
12580 /* A declaration consisting of a single semicolon is
12581 invalid. Allow it unless we're being pedantic. */
12582 cp_lexer_consume_token (parser->lexer);
12583 if (!in_system_header_at (input_location))
12584 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12585 continue;
12586 }
12587
12588 /* If we're entering or exiting a region that's implicitly
12589 extern "C", modify the lang context appropriately. */
12590 if (!parser->implicit_extern_c && token->implicit_extern_c)
12591 {
12592 push_lang_context (lang_name_c);
12593 parser->implicit_extern_c = true;
12594 }
12595 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12596 {
12597 pop_lang_context ();
12598 parser->implicit_extern_c = false;
12599 }
12600
12601 if (token->type == CPP_PRAGMA)
12602 {
12603 /* A top-level declaration can consist solely of a #pragma.
12604 A nested declaration cannot, so this is done here and not
12605 in cp_parser_declaration. (A #pragma at block scope is
12606 handled in cp_parser_statement.) */
12607 cp_parser_pragma (parser, pragma_external, NULL);
12608 continue;
12609 }
12610
12611 /* Parse the declaration itself. */
12612 cp_parser_declaration (parser);
12613 }
12614 }
12615
12616 /* Parse a declaration.
12617
12618 declaration:
12619 block-declaration
12620 function-definition
12621 template-declaration
12622 explicit-instantiation
12623 explicit-specialization
12624 linkage-specification
12625 namespace-definition
12626
12627 C++17:
12628 deduction-guide
12629
12630 GNU extension:
12631
12632 declaration:
12633 __extension__ declaration */
12634
12635 static void
12636 cp_parser_declaration (cp_parser* parser)
12637 {
12638 cp_token token1;
12639 cp_token token2;
12640 int saved_pedantic;
12641 void *p;
12642 tree attributes = NULL_TREE;
12643
12644 /* Check for the `__extension__' keyword. */
12645 if (cp_parser_extension_opt (parser, &saved_pedantic))
12646 {
12647 /* Parse the qualified declaration. */
12648 cp_parser_declaration (parser);
12649 /* Restore the PEDANTIC flag. */
12650 pedantic = saved_pedantic;
12651
12652 return;
12653 }
12654
12655 /* Try to figure out what kind of declaration is present. */
12656 token1 = *cp_lexer_peek_token (parser->lexer);
12657
12658 if (token1.type != CPP_EOF)
12659 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12660 else
12661 {
12662 token2.type = CPP_EOF;
12663 token2.keyword = RID_MAX;
12664 }
12665
12666 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12667 p = obstack_alloc (&declarator_obstack, 0);
12668
12669 /* If the next token is `extern' and the following token is a string
12670 literal, then we have a linkage specification. */
12671 if (token1.keyword == RID_EXTERN
12672 && cp_parser_is_pure_string_literal (&token2))
12673 cp_parser_linkage_specification (parser);
12674 /* If the next token is `template', then we have either a template
12675 declaration, an explicit instantiation, or an explicit
12676 specialization. */
12677 else if (token1.keyword == RID_TEMPLATE)
12678 {
12679 /* `template <>' indicates a template specialization. */
12680 if (token2.type == CPP_LESS
12681 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12682 cp_parser_explicit_specialization (parser);
12683 /* `template <' indicates a template declaration. */
12684 else if (token2.type == CPP_LESS)
12685 cp_parser_template_declaration (parser, /*member_p=*/false);
12686 /* Anything else must be an explicit instantiation. */
12687 else
12688 cp_parser_explicit_instantiation (parser);
12689 }
12690 /* If the next token is `export', then we have a template
12691 declaration. */
12692 else if (token1.keyword == RID_EXPORT)
12693 cp_parser_template_declaration (parser, /*member_p=*/false);
12694 /* If the next token is `extern', 'static' or 'inline' and the one
12695 after that is `template', we have a GNU extended explicit
12696 instantiation directive. */
12697 else if (cp_parser_allow_gnu_extensions_p (parser)
12698 && (token1.keyword == RID_EXTERN
12699 || token1.keyword == RID_STATIC
12700 || token1.keyword == RID_INLINE)
12701 && token2.keyword == RID_TEMPLATE)
12702 cp_parser_explicit_instantiation (parser);
12703 /* If the next token is `namespace', check for a named or unnamed
12704 namespace definition. */
12705 else if (token1.keyword == RID_NAMESPACE
12706 && (/* A named namespace definition. */
12707 (token2.type == CPP_NAME
12708 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12709 != CPP_EQ))
12710 || (token2.type == CPP_OPEN_SQUARE
12711 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12712 == CPP_OPEN_SQUARE)
12713 /* An unnamed namespace definition. */
12714 || token2.type == CPP_OPEN_BRACE
12715 || token2.keyword == RID_ATTRIBUTE))
12716 cp_parser_namespace_definition (parser);
12717 /* An inline (associated) namespace definition. */
12718 else if (token1.keyword == RID_INLINE
12719 && token2.keyword == RID_NAMESPACE)
12720 cp_parser_namespace_definition (parser);
12721 /* Objective-C++ declaration/definition. */
12722 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12723 cp_parser_objc_declaration (parser, NULL_TREE);
12724 else if (c_dialect_objc ()
12725 && token1.keyword == RID_ATTRIBUTE
12726 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12727 cp_parser_objc_declaration (parser, attributes);
12728 /* At this point we may have a template declared by a concept
12729 introduction. */
12730 else if (flag_concepts
12731 && cp_parser_template_declaration_after_export (parser,
12732 /*member_p=*/false))
12733 /* We did. */;
12734 else
12735 /* Try to parse a block-declaration, or a function-definition. */
12736 cp_parser_block_declaration (parser, /*statement_p=*/false);
12737
12738 /* Free any declarators allocated. */
12739 obstack_free (&declarator_obstack, p);
12740 }
12741
12742 /* Parse a block-declaration.
12743
12744 block-declaration:
12745 simple-declaration
12746 asm-definition
12747 namespace-alias-definition
12748 using-declaration
12749 using-directive
12750
12751 GNU Extension:
12752
12753 block-declaration:
12754 __extension__ block-declaration
12755
12756 C++0x Extension:
12757
12758 block-declaration:
12759 static_assert-declaration
12760
12761 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12762 part of a declaration-statement. */
12763
12764 static void
12765 cp_parser_block_declaration (cp_parser *parser,
12766 bool statement_p)
12767 {
12768 cp_token *token1;
12769 int saved_pedantic;
12770
12771 /* Check for the `__extension__' keyword. */
12772 if (cp_parser_extension_opt (parser, &saved_pedantic))
12773 {
12774 /* Parse the qualified declaration. */
12775 cp_parser_block_declaration (parser, statement_p);
12776 /* Restore the PEDANTIC flag. */
12777 pedantic = saved_pedantic;
12778
12779 return;
12780 }
12781
12782 /* Peek at the next token to figure out which kind of declaration is
12783 present. */
12784 token1 = cp_lexer_peek_token (parser->lexer);
12785
12786 /* If the next keyword is `asm', we have an asm-definition. */
12787 if (token1->keyword == RID_ASM)
12788 {
12789 if (statement_p)
12790 cp_parser_commit_to_tentative_parse (parser);
12791 cp_parser_asm_definition (parser);
12792 }
12793 /* If the next keyword is `namespace', we have a
12794 namespace-alias-definition. */
12795 else if (token1->keyword == RID_NAMESPACE)
12796 cp_parser_namespace_alias_definition (parser);
12797 /* If the next keyword is `using', we have a
12798 using-declaration, a using-directive, or an alias-declaration. */
12799 else if (token1->keyword == RID_USING)
12800 {
12801 cp_token *token2;
12802
12803 if (statement_p)
12804 cp_parser_commit_to_tentative_parse (parser);
12805 /* If the token after `using' is `namespace', then we have a
12806 using-directive. */
12807 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12808 if (token2->keyword == RID_NAMESPACE)
12809 cp_parser_using_directive (parser);
12810 /* If the second token after 'using' is '=', then we have an
12811 alias-declaration. */
12812 else if (cxx_dialect >= cxx11
12813 && token2->type == CPP_NAME
12814 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12815 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12816 cp_parser_alias_declaration (parser);
12817 /* Otherwise, it's a using-declaration. */
12818 else
12819 cp_parser_using_declaration (parser,
12820 /*access_declaration_p=*/false);
12821 }
12822 /* If the next keyword is `__label__' we have a misplaced label
12823 declaration. */
12824 else if (token1->keyword == RID_LABEL)
12825 {
12826 cp_lexer_consume_token (parser->lexer);
12827 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12828 cp_parser_skip_to_end_of_statement (parser);
12829 /* If the next token is now a `;', consume it. */
12830 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12831 cp_lexer_consume_token (parser->lexer);
12832 }
12833 /* If the next token is `static_assert' we have a static assertion. */
12834 else if (token1->keyword == RID_STATIC_ASSERT)
12835 cp_parser_static_assert (parser, /*member_p=*/false);
12836 /* Anything else must be a simple-declaration. */
12837 else
12838 cp_parser_simple_declaration (parser, !statement_p,
12839 /*maybe_range_for_decl*/NULL);
12840 }
12841
12842 /* Parse a simple-declaration.
12843
12844 simple-declaration:
12845 decl-specifier-seq [opt] init-declarator-list [opt] ;
12846 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12847 brace-or-equal-initializer ;
12848
12849 init-declarator-list:
12850 init-declarator
12851 init-declarator-list , init-declarator
12852
12853 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12854 function-definition as a simple-declaration.
12855
12856 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12857 parsed declaration if it is an uninitialized single declarator not followed
12858 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12859 if present, will not be consumed. */
12860
12861 static void
12862 cp_parser_simple_declaration (cp_parser* parser,
12863 bool function_definition_allowed_p,
12864 tree *maybe_range_for_decl)
12865 {
12866 cp_decl_specifier_seq decl_specifiers;
12867 int declares_class_or_enum;
12868 bool saw_declarator;
12869 location_t comma_loc = UNKNOWN_LOCATION;
12870 location_t init_loc = UNKNOWN_LOCATION;
12871
12872 if (maybe_range_for_decl)
12873 *maybe_range_for_decl = NULL_TREE;
12874
12875 /* Defer access checks until we know what is being declared; the
12876 checks for names appearing in the decl-specifier-seq should be
12877 done as if we were in the scope of the thing being declared. */
12878 push_deferring_access_checks (dk_deferred);
12879
12880 /* Parse the decl-specifier-seq. We have to keep track of whether
12881 or not the decl-specifier-seq declares a named class or
12882 enumeration type, since that is the only case in which the
12883 init-declarator-list is allowed to be empty.
12884
12885 [dcl.dcl]
12886
12887 In a simple-declaration, the optional init-declarator-list can be
12888 omitted only when declaring a class or enumeration, that is when
12889 the decl-specifier-seq contains either a class-specifier, an
12890 elaborated-type-specifier, or an enum-specifier. */
12891 cp_parser_decl_specifier_seq (parser,
12892 CP_PARSER_FLAGS_OPTIONAL,
12893 &decl_specifiers,
12894 &declares_class_or_enum);
12895 /* We no longer need to defer access checks. */
12896 stop_deferring_access_checks ();
12897
12898 /* In a block scope, a valid declaration must always have a
12899 decl-specifier-seq. By not trying to parse declarators, we can
12900 resolve the declaration/expression ambiguity more quickly. */
12901 if (!function_definition_allowed_p
12902 && !decl_specifiers.any_specifiers_p)
12903 {
12904 cp_parser_error (parser, "expected declaration");
12905 goto done;
12906 }
12907
12908 /* If the next two tokens are both identifiers, the code is
12909 erroneous. The usual cause of this situation is code like:
12910
12911 T t;
12912
12913 where "T" should name a type -- but does not. */
12914 if (!decl_specifiers.any_type_specifiers_p
12915 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12916 {
12917 /* If parsing tentatively, we should commit; we really are
12918 looking at a declaration. */
12919 cp_parser_commit_to_tentative_parse (parser);
12920 /* Give up. */
12921 goto done;
12922 }
12923
12924 /* If we have seen at least one decl-specifier, and the next token
12925 is not a parenthesis, then we must be looking at a declaration.
12926 (After "int (" we might be looking at a functional cast.) */
12927 if (decl_specifiers.any_specifiers_p
12928 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12929 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12930 && !cp_parser_error_occurred (parser))
12931 cp_parser_commit_to_tentative_parse (parser);
12932
12933 /* Look for C++17 decomposition declaration. */
12934 for (size_t n = 1; ; n++)
12935 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12936 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12937 continue;
12938 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12939 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12940 && decl_specifiers.any_specifiers_p)
12941 {
12942 tree decl
12943 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12944 maybe_range_for_decl,
12945 &init_loc);
12946
12947 /* The next token should be either a `,' or a `;'. */
12948 cp_token *token = cp_lexer_peek_token (parser->lexer);
12949 /* If it's a `;', we are done. */
12950 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12951 goto finish;
12952 /* Anything else is an error. */
12953 else
12954 {
12955 /* If we have already issued an error message we don't need
12956 to issue another one. */
12957 if ((decl != error_mark_node
12958 && DECL_INITIAL (decl) != error_mark_node)
12959 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12960 cp_parser_error (parser, "expected %<,%> or %<;%>");
12961 /* Skip tokens until we reach the end of the statement. */
12962 cp_parser_skip_to_end_of_statement (parser);
12963 /* If the next token is now a `;', consume it. */
12964 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12965 cp_lexer_consume_token (parser->lexer);
12966 goto done;
12967 }
12968 }
12969 else
12970 break;
12971
12972 tree last_type;
12973 bool auto_specifier_p;
12974 /* NULL_TREE if both variable and function declaration are allowed,
12975 error_mark_node if function declaration are not allowed and
12976 a FUNCTION_DECL that should be diagnosed if it is followed by
12977 variable declarations. */
12978 tree auto_function_declaration;
12979
12980 last_type = NULL_TREE;
12981 auto_specifier_p
12982 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12983 auto_function_declaration = NULL_TREE;
12984
12985 /* Keep going until we hit the `;' at the end of the simple
12986 declaration. */
12987 saw_declarator = false;
12988 while (cp_lexer_next_token_is_not (parser->lexer,
12989 CPP_SEMICOLON))
12990 {
12991 cp_token *token;
12992 bool function_definition_p;
12993 tree decl;
12994 tree auto_result = NULL_TREE;
12995
12996 if (saw_declarator)
12997 {
12998 /* If we are processing next declarator, comma is expected */
12999 token = cp_lexer_peek_token (parser->lexer);
13000 gcc_assert (token->type == CPP_COMMA);
13001 cp_lexer_consume_token (parser->lexer);
13002 if (maybe_range_for_decl)
13003 {
13004 *maybe_range_for_decl = error_mark_node;
13005 if (comma_loc == UNKNOWN_LOCATION)
13006 comma_loc = token->location;
13007 }
13008 }
13009 else
13010 saw_declarator = true;
13011
13012 /* Parse the init-declarator. */
13013 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13014 /*checks=*/NULL,
13015 function_definition_allowed_p,
13016 /*member_p=*/false,
13017 declares_class_or_enum,
13018 &function_definition_p,
13019 maybe_range_for_decl,
13020 &init_loc,
13021 &auto_result);
13022 /* If an error occurred while parsing tentatively, exit quickly.
13023 (That usually happens when in the body of a function; each
13024 statement is treated as a declaration-statement until proven
13025 otherwise.) */
13026 if (cp_parser_error_occurred (parser))
13027 goto done;
13028
13029 if (auto_specifier_p && cxx_dialect >= cxx14)
13030 {
13031 /* If the init-declarator-list contains more than one
13032 init-declarator, they shall all form declarations of
13033 variables. */
13034 if (auto_function_declaration == NULL_TREE)
13035 auto_function_declaration
13036 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13037 else if (TREE_CODE (decl) == FUNCTION_DECL
13038 || auto_function_declaration != error_mark_node)
13039 {
13040 error_at (decl_specifiers.locations[ds_type_spec],
13041 "non-variable %qD in declaration with more than one "
13042 "declarator with placeholder type",
13043 TREE_CODE (decl) == FUNCTION_DECL
13044 ? decl : auto_function_declaration);
13045 auto_function_declaration = error_mark_node;
13046 }
13047 }
13048
13049 if (auto_result
13050 && (!processing_template_decl || !type_uses_auto (auto_result)))
13051 {
13052 if (last_type
13053 && last_type != error_mark_node
13054 && !same_type_p (auto_result, last_type))
13055 {
13056 /* If the list of declarators contains more than one declarator,
13057 the type of each declared variable is determined as described
13058 above. If the type deduced for the template parameter U is not
13059 the same in each deduction, the program is ill-formed. */
13060 error_at (decl_specifiers.locations[ds_type_spec],
13061 "inconsistent deduction for %qT: %qT and then %qT",
13062 decl_specifiers.type, last_type, auto_result);
13063 last_type = error_mark_node;
13064 }
13065 else
13066 last_type = auto_result;
13067 }
13068
13069 /* Handle function definitions specially. */
13070 if (function_definition_p)
13071 {
13072 /* If the next token is a `,', then we are probably
13073 processing something like:
13074
13075 void f() {}, *p;
13076
13077 which is erroneous. */
13078 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13079 {
13080 cp_token *token = cp_lexer_peek_token (parser->lexer);
13081 error_at (token->location,
13082 "mixing"
13083 " declarations and function-definitions is forbidden");
13084 }
13085 /* Otherwise, we're done with the list of declarators. */
13086 else
13087 {
13088 pop_deferring_access_checks ();
13089 return;
13090 }
13091 }
13092 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13093 *maybe_range_for_decl = decl;
13094 /* The next token should be either a `,' or a `;'. */
13095 token = cp_lexer_peek_token (parser->lexer);
13096 /* If it's a `,', there are more declarators to come. */
13097 if (token->type == CPP_COMMA)
13098 /* will be consumed next time around */;
13099 /* If it's a `;', we are done. */
13100 else if (token->type == CPP_SEMICOLON)
13101 break;
13102 else if (maybe_range_for_decl)
13103 {
13104 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13105 permerror (decl_specifiers.locations[ds_type_spec],
13106 "types may not be defined in a for-range-declaration");
13107 break;
13108 }
13109 /* Anything else is an error. */
13110 else
13111 {
13112 /* If we have already issued an error message we don't need
13113 to issue another one. */
13114 if ((decl != error_mark_node
13115 && DECL_INITIAL (decl) != error_mark_node)
13116 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13117 cp_parser_error (parser, "expected %<,%> or %<;%>");
13118 /* Skip tokens until we reach the end of the statement. */
13119 cp_parser_skip_to_end_of_statement (parser);
13120 /* If the next token is now a `;', consume it. */
13121 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13122 cp_lexer_consume_token (parser->lexer);
13123 goto done;
13124 }
13125 /* After the first time around, a function-definition is not
13126 allowed -- even if it was OK at first. For example:
13127
13128 int i, f() {}
13129
13130 is not valid. */
13131 function_definition_allowed_p = false;
13132 }
13133
13134 /* Issue an error message if no declarators are present, and the
13135 decl-specifier-seq does not itself declare a class or
13136 enumeration: [dcl.dcl]/3. */
13137 if (!saw_declarator)
13138 {
13139 if (cp_parser_declares_only_class_p (parser))
13140 {
13141 if (!declares_class_or_enum
13142 && decl_specifiers.type
13143 && OVERLOAD_TYPE_P (decl_specifiers.type))
13144 /* Ensure an error is issued anyway when finish_decltype_type,
13145 called via cp_parser_decl_specifier_seq, returns a class or
13146 an enumeration (c++/51786). */
13147 decl_specifiers.type = NULL_TREE;
13148 shadow_tag (&decl_specifiers);
13149 }
13150 /* Perform any deferred access checks. */
13151 perform_deferred_access_checks (tf_warning_or_error);
13152 }
13153
13154 /* Consume the `;'. */
13155 finish:
13156 if (!maybe_range_for_decl)
13157 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13158 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13159 {
13160 if (init_loc != UNKNOWN_LOCATION)
13161 error_at (init_loc, "initializer in range-based %<for%> loop");
13162 if (comma_loc != UNKNOWN_LOCATION)
13163 error_at (comma_loc,
13164 "multiple declarations in range-based %<for%> loop");
13165 }
13166
13167 done:
13168 pop_deferring_access_checks ();
13169 }
13170
13171 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13172 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13173 initializer ; */
13174
13175 static tree
13176 cp_parser_decomposition_declaration (cp_parser *parser,
13177 cp_decl_specifier_seq *decl_specifiers,
13178 tree *maybe_range_for_decl,
13179 location_t *init_loc)
13180 {
13181 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13182 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13183 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13184
13185 /* Parse the identifier-list. */
13186 auto_vec<cp_expr, 10> v;
13187 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13188 while (true)
13189 {
13190 cp_expr e = cp_parser_identifier (parser);
13191 if (e.get_value () == error_mark_node)
13192 break;
13193 v.safe_push (e);
13194 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13195 break;
13196 cp_lexer_consume_token (parser->lexer);
13197 }
13198
13199 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13200 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13201 {
13202 end_loc = UNKNOWN_LOCATION;
13203 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13204 false);
13205 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13206 cp_lexer_consume_token (parser->lexer);
13207 else
13208 {
13209 cp_parser_skip_to_end_of_statement (parser);
13210 return error_mark_node;
13211 }
13212 }
13213
13214 if (cxx_dialect < cxx17)
13215 pedwarn (loc, 0, "structured bindings only available with "
13216 "-std=c++17 or -std=gnu++17");
13217
13218 tree pushed_scope;
13219 cp_declarator *declarator = make_declarator (cdk_decomp);
13220 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13221 declarator->id_loc = loc;
13222 if (ref_qual != REF_QUAL_NONE)
13223 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13224 ref_qual == REF_QUAL_RVALUE,
13225 NULL_TREE);
13226 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13227 NULL_TREE, decl_specifiers->attributes,
13228 &pushed_scope);
13229 tree orig_decl = decl;
13230
13231 unsigned int i;
13232 cp_expr e;
13233 cp_decl_specifier_seq decl_specs;
13234 clear_decl_specs (&decl_specs);
13235 decl_specs.type = make_auto ();
13236 tree prev = decl;
13237 FOR_EACH_VEC_ELT (v, i, e)
13238 {
13239 if (i == 0)
13240 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13241 else
13242 declarator->u.id.unqualified_name = e.get_value ();
13243 declarator->id_loc = e.get_location ();
13244 tree elt_pushed_scope;
13245 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13246 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13247 if (decl2 == error_mark_node)
13248 decl = error_mark_node;
13249 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13250 {
13251 /* Ensure we've diagnosed redeclaration if we aren't creating
13252 a new VAR_DECL. */
13253 gcc_assert (errorcount);
13254 decl = error_mark_node;
13255 }
13256 else
13257 prev = decl2;
13258 if (elt_pushed_scope)
13259 pop_scope (elt_pushed_scope);
13260 }
13261
13262 if (v.is_empty ())
13263 {
13264 error_at (loc, "empty structured binding declaration");
13265 decl = error_mark_node;
13266 }
13267
13268 if (maybe_range_for_decl == NULL
13269 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13270 {
13271 bool non_constant_p = false, is_direct_init = false;
13272 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13273 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13274 &non_constant_p);
13275 if (initializer == NULL_TREE
13276 || (TREE_CODE (initializer) == TREE_LIST
13277 && TREE_CHAIN (initializer))
13278 || (is_direct_init
13279 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13280 && CONSTRUCTOR_NELTS (initializer) != 1))
13281 {
13282 error_at (loc, "invalid initializer for structured binding "
13283 "declaration");
13284 initializer = error_mark_node;
13285 }
13286
13287 if (decl != error_mark_node)
13288 {
13289 cp_maybe_mangle_decomp (decl, prev, v.length ());
13290 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13291 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13292 cp_finish_decomp (decl, prev, v.length ());
13293 }
13294 }
13295 else if (decl != error_mark_node)
13296 {
13297 *maybe_range_for_decl = prev;
13298 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13299 the underlying DECL. */
13300 cp_finish_decomp (decl, prev, v.length ());
13301 }
13302
13303 if (pushed_scope)
13304 pop_scope (pushed_scope);
13305
13306 if (decl == error_mark_node && DECL_P (orig_decl))
13307 {
13308 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13309 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13310 }
13311
13312 return decl;
13313 }
13314
13315 /* Parse a decl-specifier-seq.
13316
13317 decl-specifier-seq:
13318 decl-specifier-seq [opt] decl-specifier
13319 decl-specifier attribute-specifier-seq [opt] (C++11)
13320
13321 decl-specifier:
13322 storage-class-specifier
13323 type-specifier
13324 function-specifier
13325 friend
13326 typedef
13327
13328 GNU Extension:
13329
13330 decl-specifier:
13331 attributes
13332
13333 Concepts Extension:
13334
13335 decl-specifier:
13336 concept
13337
13338 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13339
13340 The parser flags FLAGS is used to control type-specifier parsing.
13341
13342 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13343 flags:
13344
13345 1: one of the decl-specifiers is an elaborated-type-specifier
13346 (i.e., a type declaration)
13347 2: one of the decl-specifiers is an enum-specifier or a
13348 class-specifier (i.e., a type definition)
13349
13350 */
13351
13352 static void
13353 cp_parser_decl_specifier_seq (cp_parser* parser,
13354 cp_parser_flags flags,
13355 cp_decl_specifier_seq *decl_specs,
13356 int* declares_class_or_enum)
13357 {
13358 bool constructor_possible_p = !parser->in_declarator_p;
13359 bool found_decl_spec = false;
13360 cp_token *start_token = NULL;
13361 cp_decl_spec ds;
13362
13363 /* Clear DECL_SPECS. */
13364 clear_decl_specs (decl_specs);
13365
13366 /* Assume no class or enumeration type is declared. */
13367 *declares_class_or_enum = 0;
13368
13369 /* Keep reading specifiers until there are no more to read. */
13370 while (true)
13371 {
13372 bool constructor_p;
13373 cp_token *token;
13374 ds = ds_last;
13375
13376 /* Peek at the next token. */
13377 token = cp_lexer_peek_token (parser->lexer);
13378
13379 /* Save the first token of the decl spec list for error
13380 reporting. */
13381 if (!start_token)
13382 start_token = token;
13383 /* Handle attributes. */
13384 if (cp_next_tokens_can_be_attribute_p (parser))
13385 {
13386 /* Parse the attributes. */
13387 tree attrs = cp_parser_attributes_opt (parser);
13388
13389 /* In a sequence of declaration specifiers, c++11 attributes
13390 appertain to the type that precede them. In that case
13391 [dcl.spec]/1 says:
13392
13393 The attribute-specifier-seq affects the type only for
13394 the declaration it appears in, not other declarations
13395 involving the same type.
13396
13397 But for now let's force the user to position the
13398 attribute either at the beginning of the declaration or
13399 after the declarator-id, which would clearly mean that it
13400 applies to the declarator. */
13401 if (cxx11_attribute_p (attrs))
13402 {
13403 if (!found_decl_spec)
13404 /* The c++11 attribute is at the beginning of the
13405 declaration. It appertains to the entity being
13406 declared. */;
13407 else
13408 {
13409 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13410 {
13411 /* This is an attribute following a
13412 class-specifier. */
13413 if (decl_specs->type_definition_p)
13414 warn_misplaced_attr_for_class_type (token->location,
13415 decl_specs->type);
13416 attrs = NULL_TREE;
13417 }
13418 else
13419 {
13420 decl_specs->std_attributes
13421 = chainon (decl_specs->std_attributes,
13422 attrs);
13423 if (decl_specs->locations[ds_std_attribute] == 0)
13424 decl_specs->locations[ds_std_attribute] = token->location;
13425 }
13426 continue;
13427 }
13428 }
13429
13430 decl_specs->attributes
13431 = chainon (decl_specs->attributes,
13432 attrs);
13433 if (decl_specs->locations[ds_attribute] == 0)
13434 decl_specs->locations[ds_attribute] = token->location;
13435 continue;
13436 }
13437 /* Assume we will find a decl-specifier keyword. */
13438 found_decl_spec = true;
13439 /* If the next token is an appropriate keyword, we can simply
13440 add it to the list. */
13441 switch (token->keyword)
13442 {
13443 /* decl-specifier:
13444 friend
13445 constexpr */
13446 case RID_FRIEND:
13447 if (!at_class_scope_p ())
13448 {
13449 gcc_rich_location richloc (token->location);
13450 richloc.add_fixit_remove ();
13451 error_at (&richloc, "%<friend%> used outside of class");
13452 cp_lexer_purge_token (parser->lexer);
13453 }
13454 else
13455 {
13456 ds = ds_friend;
13457 /* Consume the token. */
13458 cp_lexer_consume_token (parser->lexer);
13459 }
13460 break;
13461
13462 case RID_CONSTEXPR:
13463 ds = ds_constexpr;
13464 cp_lexer_consume_token (parser->lexer);
13465 break;
13466
13467 case RID_CONCEPT:
13468 ds = ds_concept;
13469 cp_lexer_consume_token (parser->lexer);
13470 break;
13471
13472 /* function-specifier:
13473 inline
13474 virtual
13475 explicit */
13476 case RID_INLINE:
13477 case RID_VIRTUAL:
13478 case RID_EXPLICIT:
13479 cp_parser_function_specifier_opt (parser, decl_specs);
13480 break;
13481
13482 /* decl-specifier:
13483 typedef */
13484 case RID_TYPEDEF:
13485 ds = ds_typedef;
13486 /* Consume the token. */
13487 cp_lexer_consume_token (parser->lexer);
13488 /* A constructor declarator cannot appear in a typedef. */
13489 constructor_possible_p = false;
13490 /* The "typedef" keyword can only occur in a declaration; we
13491 may as well commit at this point. */
13492 cp_parser_commit_to_tentative_parse (parser);
13493
13494 if (decl_specs->storage_class != sc_none)
13495 decl_specs->conflicting_specifiers_p = true;
13496 break;
13497
13498 /* storage-class-specifier:
13499 auto
13500 register
13501 static
13502 extern
13503 mutable
13504
13505 GNU Extension:
13506 thread */
13507 case RID_AUTO:
13508 if (cxx_dialect == cxx98)
13509 {
13510 /* Consume the token. */
13511 cp_lexer_consume_token (parser->lexer);
13512
13513 /* Complain about `auto' as a storage specifier, if
13514 we're complaining about C++0x compatibility. */
13515 gcc_rich_location richloc (token->location);
13516 richloc.add_fixit_remove ();
13517 warning_at (&richloc, OPT_Wc__11_compat,
13518 "%<auto%> changes meaning in C++11; "
13519 "please remove it");
13520
13521 /* Set the storage class anyway. */
13522 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13523 token);
13524 }
13525 else
13526 /* C++0x auto type-specifier. */
13527 found_decl_spec = false;
13528 break;
13529
13530 case RID_REGISTER:
13531 case RID_STATIC:
13532 case RID_EXTERN:
13533 case RID_MUTABLE:
13534 /* Consume the token. */
13535 cp_lexer_consume_token (parser->lexer);
13536 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13537 token);
13538 break;
13539 case RID_THREAD:
13540 /* Consume the token. */
13541 ds = ds_thread;
13542 cp_lexer_consume_token (parser->lexer);
13543 break;
13544
13545 default:
13546 /* We did not yet find a decl-specifier yet. */
13547 found_decl_spec = false;
13548 break;
13549 }
13550
13551 if (found_decl_spec
13552 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13553 && token->keyword != RID_CONSTEXPR)
13554 error ("decl-specifier invalid in condition");
13555
13556 if (found_decl_spec
13557 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13558 && token->keyword != RID_MUTABLE
13559 && token->keyword != RID_CONSTEXPR)
13560 error_at (token->location, "%qD invalid in lambda",
13561 ridpointers[token->keyword]);
13562
13563 if (ds != ds_last)
13564 set_and_check_decl_spec_loc (decl_specs, ds, token);
13565
13566 /* Constructors are a special case. The `S' in `S()' is not a
13567 decl-specifier; it is the beginning of the declarator. */
13568 constructor_p
13569 = (!found_decl_spec
13570 && constructor_possible_p
13571 && (cp_parser_constructor_declarator_p
13572 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13573
13574 /* If we don't have a DECL_SPEC yet, then we must be looking at
13575 a type-specifier. */
13576 if (!found_decl_spec && !constructor_p)
13577 {
13578 int decl_spec_declares_class_or_enum;
13579 bool is_cv_qualifier;
13580 tree type_spec;
13581
13582 type_spec
13583 = cp_parser_type_specifier (parser, flags,
13584 decl_specs,
13585 /*is_declaration=*/true,
13586 &decl_spec_declares_class_or_enum,
13587 &is_cv_qualifier);
13588 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13589
13590 /* If this type-specifier referenced a user-defined type
13591 (a typedef, class-name, etc.), then we can't allow any
13592 more such type-specifiers henceforth.
13593
13594 [dcl.spec]
13595
13596 The longest sequence of decl-specifiers that could
13597 possibly be a type name is taken as the
13598 decl-specifier-seq of a declaration. The sequence shall
13599 be self-consistent as described below.
13600
13601 [dcl.type]
13602
13603 As a general rule, at most one type-specifier is allowed
13604 in the complete decl-specifier-seq of a declaration. The
13605 only exceptions are the following:
13606
13607 -- const or volatile can be combined with any other
13608 type-specifier.
13609
13610 -- signed or unsigned can be combined with char, long,
13611 short, or int.
13612
13613 -- ..
13614
13615 Example:
13616
13617 typedef char* Pc;
13618 void g (const int Pc);
13619
13620 Here, Pc is *not* part of the decl-specifier seq; it's
13621 the declarator. Therefore, once we see a type-specifier
13622 (other than a cv-qualifier), we forbid any additional
13623 user-defined types. We *do* still allow things like `int
13624 int' to be considered a decl-specifier-seq, and issue the
13625 error message later. */
13626 if (type_spec && !is_cv_qualifier)
13627 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13628 /* A constructor declarator cannot follow a type-specifier. */
13629 if (type_spec)
13630 {
13631 constructor_possible_p = false;
13632 found_decl_spec = true;
13633 if (!is_cv_qualifier)
13634 decl_specs->any_type_specifiers_p = true;
13635 }
13636 }
13637
13638 /* If we still do not have a DECL_SPEC, then there are no more
13639 decl-specifiers. */
13640 if (!found_decl_spec)
13641 break;
13642
13643 decl_specs->any_specifiers_p = true;
13644 /* After we see one decl-specifier, further decl-specifiers are
13645 always optional. */
13646 flags |= CP_PARSER_FLAGS_OPTIONAL;
13647 }
13648
13649 /* Don't allow a friend specifier with a class definition. */
13650 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13651 && (*declares_class_or_enum & 2))
13652 error_at (decl_specs->locations[ds_friend],
13653 "class definition may not be declared a friend");
13654 }
13655
13656 /* Parse an (optional) storage-class-specifier.
13657
13658 storage-class-specifier:
13659 auto
13660 register
13661 static
13662 extern
13663 mutable
13664
13665 GNU Extension:
13666
13667 storage-class-specifier:
13668 thread
13669
13670 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13671
13672 static tree
13673 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13674 {
13675 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13676 {
13677 case RID_AUTO:
13678 if (cxx_dialect != cxx98)
13679 return NULL_TREE;
13680 /* Fall through for C++98. */
13681 gcc_fallthrough ();
13682
13683 case RID_REGISTER:
13684 case RID_STATIC:
13685 case RID_EXTERN:
13686 case RID_MUTABLE:
13687 case RID_THREAD:
13688 /* Consume the token. */
13689 return cp_lexer_consume_token (parser->lexer)->u.value;
13690
13691 default:
13692 return NULL_TREE;
13693 }
13694 }
13695
13696 /* Parse an (optional) function-specifier.
13697
13698 function-specifier:
13699 inline
13700 virtual
13701 explicit
13702
13703 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13704 Updates DECL_SPECS, if it is non-NULL. */
13705
13706 static tree
13707 cp_parser_function_specifier_opt (cp_parser* parser,
13708 cp_decl_specifier_seq *decl_specs)
13709 {
13710 cp_token *token = cp_lexer_peek_token (parser->lexer);
13711 switch (token->keyword)
13712 {
13713 case RID_INLINE:
13714 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13715 break;
13716
13717 case RID_VIRTUAL:
13718 /* 14.5.2.3 [temp.mem]
13719
13720 A member function template shall not be virtual. */
13721 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13722 && current_class_type)
13723 error_at (token->location, "templates may not be %<virtual%>");
13724 else
13725 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13726 break;
13727
13728 case RID_EXPLICIT:
13729 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13730 break;
13731
13732 default:
13733 return NULL_TREE;
13734 }
13735
13736 /* Consume the token. */
13737 return cp_lexer_consume_token (parser->lexer)->u.value;
13738 }
13739
13740 /* Parse a linkage-specification.
13741
13742 linkage-specification:
13743 extern string-literal { declaration-seq [opt] }
13744 extern string-literal declaration */
13745
13746 static void
13747 cp_parser_linkage_specification (cp_parser* parser)
13748 {
13749 tree linkage;
13750
13751 /* Look for the `extern' keyword. */
13752 cp_token *extern_token
13753 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13754
13755 /* Look for the string-literal. */
13756 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13757 linkage = cp_parser_string_literal (parser, false, false);
13758
13759 /* Transform the literal into an identifier. If the literal is a
13760 wide-character string, or contains embedded NULs, then we can't
13761 handle it as the user wants. */
13762 if (strlen (TREE_STRING_POINTER (linkage))
13763 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13764 {
13765 cp_parser_error (parser, "invalid linkage-specification");
13766 /* Assume C++ linkage. */
13767 linkage = lang_name_cplusplus;
13768 }
13769 else
13770 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13771
13772 /* We're now using the new linkage. */
13773 push_lang_context (linkage);
13774
13775 /* Preserve the location of the the innermost linkage specification,
13776 tracking the locations of nested specifications via a local. */
13777 location_t saved_location
13778 = parser->innermost_linkage_specification_location;
13779 /* Construct a location ranging from the start of the "extern" to
13780 the end of the string-literal, with the caret at the start, e.g.:
13781 extern "C" {
13782 ^~~~~~~~~~
13783 */
13784 parser->innermost_linkage_specification_location
13785 = make_location (extern_token->location,
13786 extern_token->location,
13787 get_finish (string_token->location));
13788
13789 /* If the next token is a `{', then we're using the first
13790 production. */
13791 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13792 {
13793 cp_ensure_no_omp_declare_simd (parser);
13794 cp_ensure_no_oacc_routine (parser);
13795
13796 /* Consume the `{' token. */
13797 matching_braces braces;
13798 braces.consume_open (parser)->location;
13799 /* Parse the declarations. */
13800 cp_parser_declaration_seq_opt (parser);
13801 /* Look for the closing `}'. */
13802 braces.require_close (parser);
13803 }
13804 /* Otherwise, there's just one declaration. */
13805 else
13806 {
13807 bool saved_in_unbraced_linkage_specification_p;
13808
13809 saved_in_unbraced_linkage_specification_p
13810 = parser->in_unbraced_linkage_specification_p;
13811 parser->in_unbraced_linkage_specification_p = true;
13812 cp_parser_declaration (parser);
13813 parser->in_unbraced_linkage_specification_p
13814 = saved_in_unbraced_linkage_specification_p;
13815 }
13816
13817 /* We're done with the linkage-specification. */
13818 pop_lang_context ();
13819
13820 /* Restore location of parent linkage specification, if any. */
13821 parser->innermost_linkage_specification_location = saved_location;
13822 }
13823
13824 /* Parse a static_assert-declaration.
13825
13826 static_assert-declaration:
13827 static_assert ( constant-expression , string-literal ) ;
13828 static_assert ( constant-expression ) ; (C++17)
13829
13830 If MEMBER_P, this static_assert is a class member. */
13831
13832 static void
13833 cp_parser_static_assert(cp_parser *parser, bool member_p)
13834 {
13835 cp_expr condition;
13836 location_t token_loc;
13837 tree message;
13838 bool dummy;
13839
13840 /* Peek at the `static_assert' token so we can keep track of exactly
13841 where the static assertion started. */
13842 token_loc = cp_lexer_peek_token (parser->lexer)->location;
13843
13844 /* Look for the `static_assert' keyword. */
13845 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13846 RT_STATIC_ASSERT))
13847 return;
13848
13849 /* We know we are in a static assertion; commit to any tentative
13850 parse. */
13851 if (cp_parser_parsing_tentatively (parser))
13852 cp_parser_commit_to_tentative_parse (parser);
13853
13854 /* Parse the `(' starting the static assertion condition. */
13855 matching_parens parens;
13856 parens.require_open (parser);
13857
13858 /* Parse the constant-expression. Allow a non-constant expression
13859 here in order to give better diagnostics in finish_static_assert. */
13860 condition =
13861 cp_parser_constant_expression (parser,
13862 /*allow_non_constant_p=*/true,
13863 /*non_constant_p=*/&dummy);
13864
13865 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13866 {
13867 if (cxx_dialect < cxx17)
13868 pedwarn (input_location, OPT_Wpedantic,
13869 "static_assert without a message "
13870 "only available with -std=c++17 or -std=gnu++17");
13871 /* Eat the ')' */
13872 cp_lexer_consume_token (parser->lexer);
13873 message = build_string (1, "");
13874 TREE_TYPE (message) = char_array_type_node;
13875 fix_string_type (message);
13876 }
13877 else
13878 {
13879 /* Parse the separating `,'. */
13880 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13881
13882 /* Parse the string-literal message. */
13883 message = cp_parser_string_literal (parser,
13884 /*translate=*/false,
13885 /*wide_ok=*/true);
13886
13887 /* A `)' completes the static assertion. */
13888 if (!parens.require_close (parser))
13889 cp_parser_skip_to_closing_parenthesis (parser,
13890 /*recovering=*/true,
13891 /*or_comma=*/false,
13892 /*consume_paren=*/true);
13893 }
13894
13895 /* A semicolon terminates the declaration. */
13896 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13897
13898 /* Get the location for the static assertion. Use that of the
13899 condition if available, otherwise, use that of the "static_assert"
13900 token. */
13901 location_t assert_loc = condition.get_location ();
13902 if (assert_loc == UNKNOWN_LOCATION)
13903 assert_loc = token_loc;
13904
13905 /* Complete the static assertion, which may mean either processing
13906 the static assert now or saving it for template instantiation. */
13907 finish_static_assert (condition, message, assert_loc, member_p);
13908 }
13909
13910 /* Parse the expression in decltype ( expression ). */
13911
13912 static tree
13913 cp_parser_decltype_expr (cp_parser *parser,
13914 bool &id_expression_or_member_access_p)
13915 {
13916 cp_token *id_expr_start_token;
13917 tree expr;
13918
13919 /* Since we're going to preserve any side-effects from this parse, set up a
13920 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13921 in the expression. */
13922 tentative_firewall firewall (parser);
13923
13924 /* First, try parsing an id-expression. */
13925 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13926 cp_parser_parse_tentatively (parser);
13927 expr = cp_parser_id_expression (parser,
13928 /*template_keyword_p=*/false,
13929 /*check_dependency_p=*/true,
13930 /*template_p=*/NULL,
13931 /*declarator_p=*/false,
13932 /*optional_p=*/false);
13933
13934 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13935 {
13936 bool non_integral_constant_expression_p = false;
13937 tree id_expression = expr;
13938 cp_id_kind idk;
13939 const char *error_msg;
13940
13941 if (identifier_p (expr))
13942 /* Lookup the name we got back from the id-expression. */
13943 expr = cp_parser_lookup_name_simple (parser, expr,
13944 id_expr_start_token->location);
13945
13946 if (expr
13947 && expr != error_mark_node
13948 && TREE_CODE (expr) != TYPE_DECL
13949 && (TREE_CODE (expr) != BIT_NOT_EXPR
13950 || !TYPE_P (TREE_OPERAND (expr, 0)))
13951 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13952 {
13953 /* Complete lookup of the id-expression. */
13954 expr = (finish_id_expression
13955 (id_expression, expr, parser->scope, &idk,
13956 /*integral_constant_expression_p=*/false,
13957 /*allow_non_integral_constant_expression_p=*/true,
13958 &non_integral_constant_expression_p,
13959 /*template_p=*/false,
13960 /*done=*/true,
13961 /*address_p=*/false,
13962 /*template_arg_p=*/false,
13963 &error_msg,
13964 id_expr_start_token->location));
13965
13966 if (expr == error_mark_node)
13967 /* We found an id-expression, but it was something that we
13968 should not have found. This is an error, not something
13969 we can recover from, so note that we found an
13970 id-expression and we'll recover as gracefully as
13971 possible. */
13972 id_expression_or_member_access_p = true;
13973 }
13974
13975 if (expr
13976 && expr != error_mark_node
13977 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13978 /* We have an id-expression. */
13979 id_expression_or_member_access_p = true;
13980 }
13981
13982 if (!id_expression_or_member_access_p)
13983 {
13984 /* Abort the id-expression parse. */
13985 cp_parser_abort_tentative_parse (parser);
13986
13987 /* Parsing tentatively, again. */
13988 cp_parser_parse_tentatively (parser);
13989
13990 /* Parse a class member access. */
13991 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13992 /*cast_p=*/false, /*decltype*/true,
13993 /*member_access_only_p=*/true, NULL);
13994
13995 if (expr
13996 && expr != error_mark_node
13997 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13998 /* We have an id-expression. */
13999 id_expression_or_member_access_p = true;
14000 }
14001
14002 if (id_expression_or_member_access_p)
14003 /* We have parsed the complete id-expression or member access. */
14004 cp_parser_parse_definitely (parser);
14005 else
14006 {
14007 /* Abort our attempt to parse an id-expression or member access
14008 expression. */
14009 cp_parser_abort_tentative_parse (parser);
14010
14011 /* Parse a full expression. */
14012 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14013 /*decltype_p=*/true);
14014 }
14015
14016 return expr;
14017 }
14018
14019 /* Parse a `decltype' type. Returns the type.
14020
14021 simple-type-specifier:
14022 decltype ( expression )
14023 C++14 proposal:
14024 decltype ( auto ) */
14025
14026 static tree
14027 cp_parser_decltype (cp_parser *parser)
14028 {
14029 tree expr;
14030 bool id_expression_or_member_access_p = false;
14031 const char *saved_message;
14032 bool saved_integral_constant_expression_p;
14033 bool saved_non_integral_constant_expression_p;
14034 bool saved_greater_than_is_operator_p;
14035 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14036
14037 if (start_token->type == CPP_DECLTYPE)
14038 {
14039 /* Already parsed. */
14040 cp_lexer_consume_token (parser->lexer);
14041 return saved_checks_value (start_token->u.tree_check_value);
14042 }
14043
14044 /* Look for the `decltype' token. */
14045 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14046 return error_mark_node;
14047
14048 /* Parse the opening `('. */
14049 matching_parens parens;
14050 if (!parens.require_open (parser))
14051 return error_mark_node;
14052
14053 /* decltype (auto) */
14054 if (cxx_dialect >= cxx14
14055 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14056 {
14057 cp_lexer_consume_token (parser->lexer);
14058 if (!parens.require_close (parser))
14059 return error_mark_node;
14060 expr = make_decltype_auto ();
14061 AUTO_IS_DECLTYPE (expr) = true;
14062 goto rewrite;
14063 }
14064
14065 /* Types cannot be defined in a `decltype' expression. Save away the
14066 old message. */
14067 saved_message = parser->type_definition_forbidden_message;
14068
14069 /* And create the new one. */
14070 parser->type_definition_forbidden_message
14071 = G_("types may not be defined in %<decltype%> expressions");
14072
14073 /* The restrictions on constant-expressions do not apply inside
14074 decltype expressions. */
14075 saved_integral_constant_expression_p
14076 = parser->integral_constant_expression_p;
14077 saved_non_integral_constant_expression_p
14078 = parser->non_integral_constant_expression_p;
14079 parser->integral_constant_expression_p = false;
14080
14081 /* Within a parenthesized expression, a `>' token is always
14082 the greater-than operator. */
14083 saved_greater_than_is_operator_p
14084 = parser->greater_than_is_operator_p;
14085 parser->greater_than_is_operator_p = true;
14086
14087 /* Do not actually evaluate the expression. */
14088 ++cp_unevaluated_operand;
14089
14090 /* Do not warn about problems with the expression. */
14091 ++c_inhibit_evaluation_warnings;
14092
14093 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14094
14095 /* Go back to evaluating expressions. */
14096 --cp_unevaluated_operand;
14097 --c_inhibit_evaluation_warnings;
14098
14099 /* The `>' token might be the end of a template-id or
14100 template-parameter-list now. */
14101 parser->greater_than_is_operator_p
14102 = saved_greater_than_is_operator_p;
14103
14104 /* Restore the old message and the integral constant expression
14105 flags. */
14106 parser->type_definition_forbidden_message = saved_message;
14107 parser->integral_constant_expression_p
14108 = saved_integral_constant_expression_p;
14109 parser->non_integral_constant_expression_p
14110 = saved_non_integral_constant_expression_p;
14111
14112 /* Parse to the closing `)'. */
14113 if (!parens.require_close (parser))
14114 {
14115 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14116 /*consume_paren=*/true);
14117 return error_mark_node;
14118 }
14119
14120 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14121 tf_warning_or_error);
14122
14123 rewrite:
14124 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14125 it again. */
14126 start_token->type = CPP_DECLTYPE;
14127 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14128 start_token->u.tree_check_value->value = expr;
14129 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14130 start_token->keyword = RID_MAX;
14131 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14132
14133 return expr;
14134 }
14135
14136 /* Special member functions [gram.special] */
14137
14138 /* Parse a conversion-function-id.
14139
14140 conversion-function-id:
14141 operator conversion-type-id
14142
14143 Returns an IDENTIFIER_NODE representing the operator. */
14144
14145 static tree
14146 cp_parser_conversion_function_id (cp_parser* parser)
14147 {
14148 tree type;
14149 tree saved_scope;
14150 tree saved_qualifying_scope;
14151 tree saved_object_scope;
14152 tree pushed_scope = NULL_TREE;
14153
14154 /* Look for the `operator' token. */
14155 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14156 return error_mark_node;
14157 /* When we parse the conversion-type-id, the current scope will be
14158 reset. However, we need that information in able to look up the
14159 conversion function later, so we save it here. */
14160 saved_scope = parser->scope;
14161 saved_qualifying_scope = parser->qualifying_scope;
14162 saved_object_scope = parser->object_scope;
14163 /* We must enter the scope of the class so that the names of
14164 entities declared within the class are available in the
14165 conversion-type-id. For example, consider:
14166
14167 struct S {
14168 typedef int I;
14169 operator I();
14170 };
14171
14172 S::operator I() { ... }
14173
14174 In order to see that `I' is a type-name in the definition, we
14175 must be in the scope of `S'. */
14176 if (saved_scope)
14177 pushed_scope = push_scope (saved_scope);
14178 /* Parse the conversion-type-id. */
14179 type = cp_parser_conversion_type_id (parser);
14180 /* Leave the scope of the class, if any. */
14181 if (pushed_scope)
14182 pop_scope (pushed_scope);
14183 /* Restore the saved scope. */
14184 parser->scope = saved_scope;
14185 parser->qualifying_scope = saved_qualifying_scope;
14186 parser->object_scope = saved_object_scope;
14187 /* If the TYPE is invalid, indicate failure. */
14188 if (type == error_mark_node)
14189 return error_mark_node;
14190 return make_conv_op_name (type);
14191 }
14192
14193 /* Parse a conversion-type-id:
14194
14195 conversion-type-id:
14196 type-specifier-seq conversion-declarator [opt]
14197
14198 Returns the TYPE specified. */
14199
14200 static tree
14201 cp_parser_conversion_type_id (cp_parser* parser)
14202 {
14203 tree attributes;
14204 cp_decl_specifier_seq type_specifiers;
14205 cp_declarator *declarator;
14206 tree type_specified;
14207 const char *saved_message;
14208
14209 /* Parse the attributes. */
14210 attributes = cp_parser_attributes_opt (parser);
14211
14212 saved_message = parser->type_definition_forbidden_message;
14213 parser->type_definition_forbidden_message
14214 = G_("types may not be defined in a conversion-type-id");
14215
14216 /* Parse the type-specifiers. */
14217 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14218 /*is_trailing_return=*/false,
14219 &type_specifiers);
14220
14221 parser->type_definition_forbidden_message = saved_message;
14222
14223 /* If that didn't work, stop. */
14224 if (type_specifiers.type == error_mark_node)
14225 return error_mark_node;
14226 /* Parse the conversion-declarator. */
14227 declarator = cp_parser_conversion_declarator_opt (parser);
14228
14229 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14230 /*initialized=*/0, &attributes);
14231 if (attributes)
14232 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14233
14234 /* Don't give this error when parsing tentatively. This happens to
14235 work because we always parse this definitively once. */
14236 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14237 && type_uses_auto (type_specified))
14238 {
14239 if (cxx_dialect < cxx14)
14240 {
14241 error ("invalid use of %<auto%> in conversion operator");
14242 return error_mark_node;
14243 }
14244 else if (template_parm_scope_p ())
14245 warning (0, "use of %<auto%> in member template "
14246 "conversion operator can never be deduced");
14247 }
14248
14249 return type_specified;
14250 }
14251
14252 /* Parse an (optional) conversion-declarator.
14253
14254 conversion-declarator:
14255 ptr-operator conversion-declarator [opt]
14256
14257 */
14258
14259 static cp_declarator *
14260 cp_parser_conversion_declarator_opt (cp_parser* parser)
14261 {
14262 enum tree_code code;
14263 tree class_type, std_attributes = NULL_TREE;
14264 cp_cv_quals cv_quals;
14265
14266 /* We don't know if there's a ptr-operator next, or not. */
14267 cp_parser_parse_tentatively (parser);
14268 /* Try the ptr-operator. */
14269 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14270 &std_attributes);
14271 /* If it worked, look for more conversion-declarators. */
14272 if (cp_parser_parse_definitely (parser))
14273 {
14274 cp_declarator *declarator;
14275
14276 /* Parse another optional declarator. */
14277 declarator = cp_parser_conversion_declarator_opt (parser);
14278
14279 declarator = cp_parser_make_indirect_declarator
14280 (code, class_type, cv_quals, declarator, std_attributes);
14281
14282 return declarator;
14283 }
14284
14285 return NULL;
14286 }
14287
14288 /* Parse an (optional) ctor-initializer.
14289
14290 ctor-initializer:
14291 : mem-initializer-list */
14292
14293 static void
14294 cp_parser_ctor_initializer_opt (cp_parser* parser)
14295 {
14296 /* If the next token is not a `:', then there is no
14297 ctor-initializer. */
14298 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14299 {
14300 /* Do default initialization of any bases and members. */
14301 if (DECL_CONSTRUCTOR_P (current_function_decl))
14302 finish_mem_initializers (NULL_TREE);
14303 return;
14304 }
14305
14306 /* Consume the `:' token. */
14307 cp_lexer_consume_token (parser->lexer);
14308 /* And the mem-initializer-list. */
14309 cp_parser_mem_initializer_list (parser);
14310 }
14311
14312 /* Parse a mem-initializer-list.
14313
14314 mem-initializer-list:
14315 mem-initializer ... [opt]
14316 mem-initializer ... [opt] , mem-initializer-list */
14317
14318 static void
14319 cp_parser_mem_initializer_list (cp_parser* parser)
14320 {
14321 tree mem_initializer_list = NULL_TREE;
14322 tree target_ctor = error_mark_node;
14323 cp_token *token = cp_lexer_peek_token (parser->lexer);
14324
14325 /* Let the semantic analysis code know that we are starting the
14326 mem-initializer-list. */
14327 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14328 error_at (token->location,
14329 "only constructors take member initializers");
14330
14331 /* Loop through the list. */
14332 while (true)
14333 {
14334 tree mem_initializer;
14335
14336 token = cp_lexer_peek_token (parser->lexer);
14337 /* Parse the mem-initializer. */
14338 mem_initializer = cp_parser_mem_initializer (parser);
14339 /* If the next token is a `...', we're expanding member initializers. */
14340 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14341 {
14342 /* Consume the `...'. */
14343 cp_lexer_consume_token (parser->lexer);
14344
14345 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14346 can be expanded but members cannot. */
14347 if (mem_initializer != error_mark_node
14348 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14349 {
14350 error_at (token->location,
14351 "cannot expand initializer for member %qD",
14352 TREE_PURPOSE (mem_initializer));
14353 mem_initializer = error_mark_node;
14354 }
14355
14356 /* Construct the pack expansion type. */
14357 if (mem_initializer != error_mark_node)
14358 mem_initializer = make_pack_expansion (mem_initializer);
14359 }
14360 if (target_ctor != error_mark_node
14361 && mem_initializer != error_mark_node)
14362 {
14363 error ("mem-initializer for %qD follows constructor delegation",
14364 TREE_PURPOSE (mem_initializer));
14365 mem_initializer = error_mark_node;
14366 }
14367 /* Look for a target constructor. */
14368 if (mem_initializer != error_mark_node
14369 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14370 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14371 {
14372 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14373 if (mem_initializer_list)
14374 {
14375 error ("constructor delegation follows mem-initializer for %qD",
14376 TREE_PURPOSE (mem_initializer_list));
14377 mem_initializer = error_mark_node;
14378 }
14379 target_ctor = mem_initializer;
14380 }
14381 /* Add it to the list, unless it was erroneous. */
14382 if (mem_initializer != error_mark_node)
14383 {
14384 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14385 mem_initializer_list = mem_initializer;
14386 }
14387 /* If the next token is not a `,', we're done. */
14388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14389 break;
14390 /* Consume the `,' token. */
14391 cp_lexer_consume_token (parser->lexer);
14392 }
14393
14394 /* Perform semantic analysis. */
14395 if (DECL_CONSTRUCTOR_P (current_function_decl))
14396 finish_mem_initializers (mem_initializer_list);
14397 }
14398
14399 /* Parse a mem-initializer.
14400
14401 mem-initializer:
14402 mem-initializer-id ( expression-list [opt] )
14403 mem-initializer-id braced-init-list
14404
14405 GNU extension:
14406
14407 mem-initializer:
14408 ( expression-list [opt] )
14409
14410 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14411 class) or FIELD_DECL (for a non-static data member) to initialize;
14412 the TREE_VALUE is the expression-list. An empty initialization
14413 list is represented by void_list_node. */
14414
14415 static tree
14416 cp_parser_mem_initializer (cp_parser* parser)
14417 {
14418 tree mem_initializer_id;
14419 tree expression_list;
14420 tree member;
14421 cp_token *token = cp_lexer_peek_token (parser->lexer);
14422
14423 /* Find out what is being initialized. */
14424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14425 {
14426 permerror (token->location,
14427 "anachronistic old-style base class initializer");
14428 mem_initializer_id = NULL_TREE;
14429 }
14430 else
14431 {
14432 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14433 if (mem_initializer_id == error_mark_node)
14434 return mem_initializer_id;
14435 }
14436 member = expand_member_init (mem_initializer_id);
14437 if (member && !DECL_P (member))
14438 in_base_initializer = 1;
14439
14440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14441 {
14442 bool expr_non_constant_p;
14443 cp_lexer_set_source_position (parser->lexer);
14444 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14445 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14446 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14447 expression_list = build_tree_list (NULL_TREE, expression_list);
14448 }
14449 else
14450 {
14451 vec<tree, va_gc> *vec;
14452 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14453 /*cast_p=*/false,
14454 /*allow_expansion_p=*/true,
14455 /*non_constant_p=*/NULL);
14456 if (vec == NULL)
14457 return error_mark_node;
14458 expression_list = build_tree_list_vec (vec);
14459 release_tree_vector (vec);
14460 }
14461
14462 if (expression_list == error_mark_node)
14463 return error_mark_node;
14464 if (!expression_list)
14465 expression_list = void_type_node;
14466
14467 in_base_initializer = 0;
14468
14469 return member ? build_tree_list (member, expression_list) : error_mark_node;
14470 }
14471
14472 /* Parse a mem-initializer-id.
14473
14474 mem-initializer-id:
14475 :: [opt] nested-name-specifier [opt] class-name
14476 decltype-specifier (C++11)
14477 identifier
14478
14479 Returns a TYPE indicating the class to be initialized for the first
14480 production (and the second in C++11). Returns an IDENTIFIER_NODE
14481 indicating the data member to be initialized for the last production. */
14482
14483 static tree
14484 cp_parser_mem_initializer_id (cp_parser* parser)
14485 {
14486 bool global_scope_p;
14487 bool nested_name_specifier_p;
14488 bool template_p = false;
14489 tree id;
14490
14491 cp_token *token = cp_lexer_peek_token (parser->lexer);
14492
14493 /* `typename' is not allowed in this context ([temp.res]). */
14494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14495 {
14496 error_at (token->location,
14497 "keyword %<typename%> not allowed in this context (a qualified "
14498 "member initializer is implicitly a type)");
14499 cp_lexer_consume_token (parser->lexer);
14500 }
14501 /* Look for the optional `::' operator. */
14502 global_scope_p
14503 = (cp_parser_global_scope_opt (parser,
14504 /*current_scope_valid_p=*/false)
14505 != NULL_TREE);
14506 /* Look for the optional nested-name-specifier. The simplest way to
14507 implement:
14508
14509 [temp.res]
14510
14511 The keyword `typename' is not permitted in a base-specifier or
14512 mem-initializer; in these contexts a qualified name that
14513 depends on a template-parameter is implicitly assumed to be a
14514 type name.
14515
14516 is to assume that we have seen the `typename' keyword at this
14517 point. */
14518 nested_name_specifier_p
14519 = (cp_parser_nested_name_specifier_opt (parser,
14520 /*typename_keyword_p=*/true,
14521 /*check_dependency_p=*/true,
14522 /*type_p=*/true,
14523 /*is_declaration=*/true)
14524 != NULL_TREE);
14525 if (nested_name_specifier_p)
14526 template_p = cp_parser_optional_template_keyword (parser);
14527 /* If there is a `::' operator or a nested-name-specifier, then we
14528 are definitely looking for a class-name. */
14529 if (global_scope_p || nested_name_specifier_p)
14530 return cp_parser_class_name (parser,
14531 /*typename_keyword_p=*/true,
14532 /*template_keyword_p=*/template_p,
14533 typename_type,
14534 /*check_dependency_p=*/true,
14535 /*class_head_p=*/false,
14536 /*is_declaration=*/true);
14537 /* Otherwise, we could also be looking for an ordinary identifier. */
14538 cp_parser_parse_tentatively (parser);
14539 if (cp_lexer_next_token_is_decltype (parser->lexer))
14540 /* Try a decltype-specifier. */
14541 id = cp_parser_decltype (parser);
14542 else
14543 /* Otherwise, try a class-name. */
14544 id = cp_parser_class_name (parser,
14545 /*typename_keyword_p=*/true,
14546 /*template_keyword_p=*/false,
14547 none_type,
14548 /*check_dependency_p=*/true,
14549 /*class_head_p=*/false,
14550 /*is_declaration=*/true);
14551 /* If we found one, we're done. */
14552 if (cp_parser_parse_definitely (parser))
14553 return id;
14554 /* Otherwise, look for an ordinary identifier. */
14555 return cp_parser_identifier (parser);
14556 }
14557
14558 /* Overloading [gram.over] */
14559
14560 /* Parse an operator-function-id.
14561
14562 operator-function-id:
14563 operator operator
14564
14565 Returns an IDENTIFIER_NODE for the operator which is a
14566 human-readable spelling of the identifier, e.g., `operator +'. */
14567
14568 static cp_expr
14569 cp_parser_operator_function_id (cp_parser* parser)
14570 {
14571 /* Look for the `operator' keyword. */
14572 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14573 return error_mark_node;
14574 /* And then the name of the operator itself. */
14575 return cp_parser_operator (parser);
14576 }
14577
14578 /* Return an identifier node for a user-defined literal operator.
14579 The suffix identifier is chained to the operator name identifier. */
14580
14581 tree
14582 cp_literal_operator_id (const char* name)
14583 {
14584 tree identifier;
14585 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14586 + strlen (name) + 10);
14587 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14588 identifier = get_identifier (buffer);
14589
14590 return identifier;
14591 }
14592
14593 /* Parse an operator.
14594
14595 operator:
14596 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14597 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14598 || ++ -- , ->* -> () []
14599
14600 GNU Extensions:
14601
14602 operator:
14603 <? >? <?= >?=
14604
14605 Returns an IDENTIFIER_NODE for the operator which is a
14606 human-readable spelling of the identifier, e.g., `operator +'. */
14607
14608 static cp_expr
14609 cp_parser_operator (cp_parser* parser)
14610 {
14611 tree id = NULL_TREE;
14612 cp_token *token;
14613 bool utf8 = false;
14614
14615 /* Peek at the next token. */
14616 token = cp_lexer_peek_token (parser->lexer);
14617
14618 location_t start_loc = token->location;
14619
14620 /* Figure out which operator we have. */
14621 enum tree_code op = ERROR_MARK;
14622 bool assop = false;
14623 bool consumed = false;
14624 switch (token->type)
14625 {
14626 case CPP_KEYWORD:
14627 {
14628 /* The keyword should be either `new' or `delete'. */
14629 if (token->keyword == RID_NEW)
14630 op = NEW_EXPR;
14631 else if (token->keyword == RID_DELETE)
14632 op = DELETE_EXPR;
14633 else
14634 break;
14635
14636 /* Consume the `new' or `delete' token. */
14637 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14638
14639 /* Peek at the next token. */
14640 token = cp_lexer_peek_token (parser->lexer);
14641 /* If it's a `[' token then this is the array variant of the
14642 operator. */
14643 if (token->type == CPP_OPEN_SQUARE)
14644 {
14645 /* Consume the `[' token. */
14646 cp_lexer_consume_token (parser->lexer);
14647 /* Look for the `]' token. */
14648 if (cp_token *close_token
14649 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14650 end_loc = close_token->location;
14651 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14652 }
14653 start_loc = make_location (start_loc, start_loc, end_loc);
14654 consumed = true;
14655 break;
14656 }
14657
14658 case CPP_PLUS:
14659 op = PLUS_EXPR;
14660 break;
14661
14662 case CPP_MINUS:
14663 op = MINUS_EXPR;
14664 break;
14665
14666 case CPP_MULT:
14667 op = MULT_EXPR;
14668 break;
14669
14670 case CPP_DIV:
14671 op = TRUNC_DIV_EXPR;
14672 break;
14673
14674 case CPP_MOD:
14675 op = TRUNC_MOD_EXPR;
14676 break;
14677
14678 case CPP_XOR:
14679 op = BIT_XOR_EXPR;
14680 break;
14681
14682 case CPP_AND:
14683 op = BIT_AND_EXPR;
14684 break;
14685
14686 case CPP_OR:
14687 op = BIT_IOR_EXPR;
14688 break;
14689
14690 case CPP_COMPL:
14691 op = BIT_NOT_EXPR;
14692 break;
14693
14694 case CPP_NOT:
14695 op = TRUTH_NOT_EXPR;
14696 break;
14697
14698 case CPP_EQ:
14699 assop = true;
14700 op = NOP_EXPR;
14701 break;
14702
14703 case CPP_LESS:
14704 op = LT_EXPR;
14705 break;
14706
14707 case CPP_GREATER:
14708 op = GT_EXPR;
14709 break;
14710
14711 case CPP_PLUS_EQ:
14712 assop = true;
14713 op = PLUS_EXPR;
14714 break;
14715
14716 case CPP_MINUS_EQ:
14717 assop = true;
14718 op = MINUS_EXPR;
14719 break;
14720
14721 case CPP_MULT_EQ:
14722 assop = true;
14723 op = MULT_EXPR;
14724 break;
14725
14726 case CPP_DIV_EQ:
14727 assop = true;
14728 op = TRUNC_DIV_EXPR;
14729 break;
14730
14731 case CPP_MOD_EQ:
14732 assop = true;
14733 op = TRUNC_MOD_EXPR;
14734 break;
14735
14736 case CPP_XOR_EQ:
14737 assop = true;
14738 op = BIT_XOR_EXPR;
14739 break;
14740
14741 case CPP_AND_EQ:
14742 assop = true;
14743 op = BIT_AND_EXPR;
14744 break;
14745
14746 case CPP_OR_EQ:
14747 assop = true;
14748 op = BIT_IOR_EXPR;
14749 break;
14750
14751 case CPP_LSHIFT:
14752 op = LSHIFT_EXPR;
14753 break;
14754
14755 case CPP_RSHIFT:
14756 op = RSHIFT_EXPR;
14757 break;
14758
14759 case CPP_LSHIFT_EQ:
14760 assop = true;
14761 op = LSHIFT_EXPR;
14762 break;
14763
14764 case CPP_RSHIFT_EQ:
14765 assop = true;
14766 op = RSHIFT_EXPR;
14767 break;
14768
14769 case CPP_EQ_EQ:
14770 op = EQ_EXPR;
14771 break;
14772
14773 case CPP_NOT_EQ:
14774 op = NE_EXPR;
14775 break;
14776
14777 case CPP_LESS_EQ:
14778 op = LE_EXPR;
14779 break;
14780
14781 case CPP_GREATER_EQ:
14782 op = GE_EXPR;
14783 break;
14784
14785 case CPP_AND_AND:
14786 op = TRUTH_ANDIF_EXPR;
14787 break;
14788
14789 case CPP_OR_OR:
14790 op = TRUTH_ORIF_EXPR;
14791 break;
14792
14793 case CPP_PLUS_PLUS:
14794 op = POSTINCREMENT_EXPR;
14795 break;
14796
14797 case CPP_MINUS_MINUS:
14798 op = PREDECREMENT_EXPR;
14799 break;
14800
14801 case CPP_COMMA:
14802 op = COMPOUND_EXPR;
14803 break;
14804
14805 case CPP_DEREF_STAR:
14806 op = MEMBER_REF;
14807 break;
14808
14809 case CPP_DEREF:
14810 op = COMPONENT_REF;
14811 break;
14812
14813 case CPP_OPEN_PAREN:
14814 {
14815 /* Consume the `('. */
14816 matching_parens parens;
14817 parens.consume_open (parser);
14818 /* Look for the matching `)'. */
14819 parens.require_close (parser);
14820 op = CALL_EXPR;
14821 consumed = true;
14822 break;
14823 }
14824
14825 case CPP_OPEN_SQUARE:
14826 /* Consume the `['. */
14827 cp_lexer_consume_token (parser->lexer);
14828 /* Look for the matching `]'. */
14829 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14830 op = ARRAY_REF;
14831 consumed = true;
14832 break;
14833
14834 case CPP_UTF8STRING:
14835 case CPP_UTF8STRING_USERDEF:
14836 utf8 = true;
14837 /* FALLTHRU */
14838 case CPP_STRING:
14839 case CPP_WSTRING:
14840 case CPP_STRING16:
14841 case CPP_STRING32:
14842 case CPP_STRING_USERDEF:
14843 case CPP_WSTRING_USERDEF:
14844 case CPP_STRING16_USERDEF:
14845 case CPP_STRING32_USERDEF:
14846 {
14847 tree str, string_tree;
14848 int sz, len;
14849
14850 if (cxx_dialect == cxx98)
14851 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14852
14853 /* Consume the string. */
14854 str = cp_parser_string_literal (parser, /*translate=*/true,
14855 /*wide_ok=*/true, /*lookup_udlit=*/false);
14856 if (str == error_mark_node)
14857 return error_mark_node;
14858 else if (TREE_CODE (str) == USERDEF_LITERAL)
14859 {
14860 string_tree = USERDEF_LITERAL_VALUE (str);
14861 id = USERDEF_LITERAL_SUFFIX_ID (str);
14862 }
14863 else
14864 {
14865 string_tree = str;
14866 /* Look for the suffix identifier. */
14867 token = cp_lexer_peek_token (parser->lexer);
14868 if (token->type == CPP_NAME)
14869 id = cp_parser_identifier (parser);
14870 else if (token->type == CPP_KEYWORD)
14871 {
14872 error ("unexpected keyword;"
14873 " remove space between quotes and suffix identifier");
14874 return error_mark_node;
14875 }
14876 else
14877 {
14878 error ("expected suffix identifier");
14879 return error_mark_node;
14880 }
14881 }
14882 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14883 (TREE_TYPE (TREE_TYPE (string_tree))));
14884 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14885 if (len != 0)
14886 {
14887 error ("expected empty string after %<operator%> keyword");
14888 return error_mark_node;
14889 }
14890 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14891 != char_type_node)
14892 {
14893 error ("invalid encoding prefix in literal operator");
14894 return error_mark_node;
14895 }
14896 if (id != error_mark_node)
14897 {
14898 const char *name = IDENTIFIER_POINTER (id);
14899 id = cp_literal_operator_id (name);
14900 }
14901 return id;
14902 }
14903
14904 default:
14905 /* Anything else is an error. */
14906 break;
14907 }
14908
14909 /* If we have selected an identifier, we need to consume the
14910 operator token. */
14911 if (op != ERROR_MARK)
14912 {
14913 id = ovl_op_identifier (assop, op);
14914 if (!consumed)
14915 cp_lexer_consume_token (parser->lexer);
14916 }
14917 /* Otherwise, no valid operator name was present. */
14918 else
14919 {
14920 cp_parser_error (parser, "expected operator");
14921 id = error_mark_node;
14922 }
14923
14924 return cp_expr (id, start_loc);
14925 }
14926
14927 /* Parse a template-declaration.
14928
14929 template-declaration:
14930 export [opt] template < template-parameter-list > declaration
14931
14932 If MEMBER_P is TRUE, this template-declaration occurs within a
14933 class-specifier.
14934
14935 The grammar rule given by the standard isn't correct. What
14936 is really meant is:
14937
14938 template-declaration:
14939 export [opt] template-parameter-list-seq
14940 decl-specifier-seq [opt] init-declarator [opt] ;
14941 export [opt] template-parameter-list-seq
14942 function-definition
14943
14944 template-parameter-list-seq:
14945 template-parameter-list-seq [opt]
14946 template < template-parameter-list >
14947
14948 Concept Extensions:
14949
14950 template-parameter-list-seq:
14951 template < template-parameter-list > requires-clause [opt]
14952
14953 requires-clause:
14954 requires logical-or-expression */
14955
14956 static void
14957 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14958 {
14959 /* Check for `export'. */
14960 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14961 {
14962 /* Consume the `export' token. */
14963 cp_lexer_consume_token (parser->lexer);
14964 /* Warn that we do not support `export'. */
14965 warning (0, "keyword %<export%> not implemented, and will be ignored");
14966 }
14967
14968 cp_parser_template_declaration_after_export (parser, member_p);
14969 }
14970
14971 /* Parse a template-parameter-list.
14972
14973 template-parameter-list:
14974 template-parameter
14975 template-parameter-list , template-parameter
14976
14977 Returns a TREE_LIST. Each node represents a template parameter.
14978 The nodes are connected via their TREE_CHAINs. */
14979
14980 static tree
14981 cp_parser_template_parameter_list (cp_parser* parser)
14982 {
14983 tree parameter_list = NULL_TREE;
14984
14985 begin_template_parm_list ();
14986
14987 /* The loop below parses the template parms. We first need to know
14988 the total number of template parms to be able to compute proper
14989 canonical types of each dependent type. So after the loop, when
14990 we know the total number of template parms,
14991 end_template_parm_list computes the proper canonical types and
14992 fixes up the dependent types accordingly. */
14993 while (true)
14994 {
14995 tree parameter;
14996 bool is_non_type;
14997 bool is_parameter_pack;
14998 location_t parm_loc;
14999
15000 /* Parse the template-parameter. */
15001 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15002 parameter = cp_parser_template_parameter (parser,
15003 &is_non_type,
15004 &is_parameter_pack);
15005 /* Add it to the list. */
15006 if (parameter != error_mark_node)
15007 parameter_list = process_template_parm (parameter_list,
15008 parm_loc,
15009 parameter,
15010 is_non_type,
15011 is_parameter_pack);
15012 else
15013 {
15014 tree err_parm = build_tree_list (parameter, parameter);
15015 parameter_list = chainon (parameter_list, err_parm);
15016 }
15017
15018 /* If the next token is not a `,', we're done. */
15019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15020 break;
15021 /* Otherwise, consume the `,' token. */
15022 cp_lexer_consume_token (parser->lexer);
15023 }
15024
15025 return end_template_parm_list (parameter_list);
15026 }
15027
15028 /* Parse a introduction-list.
15029
15030 introduction-list:
15031 introduced-parameter
15032 introduction-list , introduced-parameter
15033
15034 introduced-parameter:
15035 ...[opt] identifier
15036
15037 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15038 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15039 WILDCARD_DECL will also have DECL_NAME set and token location in
15040 DECL_SOURCE_LOCATION. */
15041
15042 static tree
15043 cp_parser_introduction_list (cp_parser *parser)
15044 {
15045 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15046
15047 while (true)
15048 {
15049 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15050 if (is_pack)
15051 cp_lexer_consume_token (parser->lexer);
15052
15053 /* Build placeholder. */
15054 tree parm = build_nt (WILDCARD_DECL);
15055 DECL_SOURCE_LOCATION (parm)
15056 = cp_lexer_peek_token (parser->lexer)->location;
15057 DECL_NAME (parm) = cp_parser_identifier (parser);
15058 WILDCARD_PACK_P (parm) = is_pack;
15059 vec_safe_push (introduction_vec, parm);
15060
15061 /* If the next token is not a `,', we're done. */
15062 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15063 break;
15064 /* Otherwise, consume the `,' token. */
15065 cp_lexer_consume_token (parser->lexer);
15066 }
15067
15068 /* Convert the vec into a TREE_VEC. */
15069 tree introduction_list = make_tree_vec (introduction_vec->length ());
15070 unsigned int n;
15071 tree parm;
15072 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15073 TREE_VEC_ELT (introduction_list, n) = parm;
15074
15075 release_tree_vector (introduction_vec);
15076 return introduction_list;
15077 }
15078
15079 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15080 is an abstract declarator. */
15081
15082 static inline cp_declarator*
15083 get_id_declarator (cp_declarator *declarator)
15084 {
15085 cp_declarator *d = declarator;
15086 while (d && d->kind != cdk_id)
15087 d = d->declarator;
15088 return d;
15089 }
15090
15091 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15092 is an abstract declarator. */
15093
15094 static inline tree
15095 get_unqualified_id (cp_declarator *declarator)
15096 {
15097 declarator = get_id_declarator (declarator);
15098 if (declarator)
15099 return declarator->u.id.unqualified_name;
15100 else
15101 return NULL_TREE;
15102 }
15103
15104 /* Returns true if DECL represents a constrained-parameter. */
15105
15106 static inline bool
15107 is_constrained_parameter (tree decl)
15108 {
15109 return (decl
15110 && TREE_CODE (decl) == TYPE_DECL
15111 && CONSTRAINED_PARM_CONCEPT (decl)
15112 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15113 }
15114
15115 /* Returns true if PARM declares a constrained-parameter. */
15116
15117 static inline bool
15118 is_constrained_parameter (cp_parameter_declarator *parm)
15119 {
15120 return is_constrained_parameter (parm->decl_specifiers.type);
15121 }
15122
15123 /* Check that the type parameter is only a declarator-id, and that its
15124 type is not cv-qualified. */
15125
15126 bool
15127 cp_parser_check_constrained_type_parm (cp_parser *parser,
15128 cp_parameter_declarator *parm)
15129 {
15130 if (!parm->declarator)
15131 return true;
15132
15133 if (parm->declarator->kind != cdk_id)
15134 {
15135 cp_parser_error (parser, "invalid constrained type parameter");
15136 return false;
15137 }
15138
15139 /* Don't allow cv-qualified type parameters. */
15140 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15141 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15142 {
15143 cp_parser_error (parser, "cv-qualified type parameter");
15144 return false;
15145 }
15146
15147 return true;
15148 }
15149
15150 /* Finish parsing/processing a template type parameter and checking
15151 various restrictions. */
15152
15153 static inline tree
15154 cp_parser_constrained_type_template_parm (cp_parser *parser,
15155 tree id,
15156 cp_parameter_declarator* parmdecl)
15157 {
15158 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15159 return finish_template_type_parm (class_type_node, id);
15160 else
15161 return error_mark_node;
15162 }
15163
15164 static tree
15165 finish_constrained_template_template_parm (tree proto, tree id)
15166 {
15167 /* FIXME: This should probably be copied, and we may need to adjust
15168 the template parameter depths. */
15169 tree saved_parms = current_template_parms;
15170 begin_template_parm_list ();
15171 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15172 end_template_parm_list ();
15173
15174 tree parm = finish_template_template_parm (class_type_node, id);
15175 current_template_parms = saved_parms;
15176
15177 return parm;
15178 }
15179
15180 /* Finish parsing/processing a template template parameter by borrowing
15181 the template parameter list from the prototype parameter. */
15182
15183 static tree
15184 cp_parser_constrained_template_template_parm (cp_parser *parser,
15185 tree proto,
15186 tree id,
15187 cp_parameter_declarator *parmdecl)
15188 {
15189 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15190 return error_mark_node;
15191 return finish_constrained_template_template_parm (proto, id);
15192 }
15193
15194 /* Create a new non-type template parameter from the given PARM
15195 declarator. */
15196
15197 static tree
15198 constrained_non_type_template_parm (bool *is_non_type,
15199 cp_parameter_declarator *parm)
15200 {
15201 *is_non_type = true;
15202 cp_declarator *decl = parm->declarator;
15203 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15204 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15205 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15206 }
15207
15208 /* Build a constrained template parameter based on the PARMDECL
15209 declarator. The type of PARMDECL is the constrained type, which
15210 refers to the prototype template parameter that ultimately
15211 specifies the type of the declared parameter. */
15212
15213 static tree
15214 finish_constrained_parameter (cp_parser *parser,
15215 cp_parameter_declarator *parmdecl,
15216 bool *is_non_type,
15217 bool *is_parameter_pack)
15218 {
15219 tree decl = parmdecl->decl_specifiers.type;
15220 tree id = get_unqualified_id (parmdecl->declarator);
15221 tree def = parmdecl->default_argument;
15222 tree proto = DECL_INITIAL (decl);
15223
15224 /* A template parameter constrained by a variadic concept shall also
15225 be declared as a template parameter pack. */
15226 bool is_variadic = template_parameter_pack_p (proto);
15227 if (is_variadic && !*is_parameter_pack)
15228 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15229
15230 /* Build the parameter. Return an error if the declarator was invalid. */
15231 tree parm;
15232 if (TREE_CODE (proto) == TYPE_DECL)
15233 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15234 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15235 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15236 parmdecl);
15237 else
15238 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15239 if (parm == error_mark_node)
15240 return error_mark_node;
15241
15242 /* Finish the parameter decl and create a node attaching the
15243 default argument and constraint. */
15244 parm = build_tree_list (def, parm);
15245 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15246
15247 return parm;
15248 }
15249
15250 /* Returns true if the parsed type actually represents the declaration
15251 of a type template-parameter. */
15252
15253 static inline bool
15254 declares_constrained_type_template_parameter (tree type)
15255 {
15256 return (is_constrained_parameter (type)
15257 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15258 }
15259
15260
15261 /* Returns true if the parsed type actually represents the declaration of
15262 a template template-parameter. */
15263
15264 static bool
15265 declares_constrained_template_template_parameter (tree type)
15266 {
15267 return (is_constrained_parameter (type)
15268 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15269 }
15270
15271 /* Parse a default argument for a type template-parameter.
15272 Note that diagnostics are handled in cp_parser_template_parameter. */
15273
15274 static tree
15275 cp_parser_default_type_template_argument (cp_parser *parser)
15276 {
15277 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15278
15279 /* Consume the `=' token. */
15280 cp_lexer_consume_token (parser->lexer);
15281
15282 cp_token *token = cp_lexer_peek_token (parser->lexer);
15283
15284 /* Parse the default-argument. */
15285 push_deferring_access_checks (dk_no_deferred);
15286 tree default_argument = cp_parser_type_id (parser);
15287 pop_deferring_access_checks ();
15288
15289 if (flag_concepts && type_uses_auto (default_argument))
15290 {
15291 error_at (token->location,
15292 "invalid use of %<auto%> in default template argument");
15293 return error_mark_node;
15294 }
15295
15296 return default_argument;
15297 }
15298
15299 /* Parse a default argument for a template template-parameter. */
15300
15301 static tree
15302 cp_parser_default_template_template_argument (cp_parser *parser)
15303 {
15304 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15305
15306 bool is_template;
15307
15308 /* Consume the `='. */
15309 cp_lexer_consume_token (parser->lexer);
15310 /* Parse the id-expression. */
15311 push_deferring_access_checks (dk_no_deferred);
15312 /* save token before parsing the id-expression, for error
15313 reporting */
15314 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15315 tree default_argument
15316 = cp_parser_id_expression (parser,
15317 /*template_keyword_p=*/false,
15318 /*check_dependency_p=*/true,
15319 /*template_p=*/&is_template,
15320 /*declarator_p=*/false,
15321 /*optional_p=*/false);
15322 if (TREE_CODE (default_argument) == TYPE_DECL)
15323 /* If the id-expression was a template-id that refers to
15324 a template-class, we already have the declaration here,
15325 so no further lookup is needed. */
15326 ;
15327 else
15328 /* Look up the name. */
15329 default_argument
15330 = cp_parser_lookup_name (parser, default_argument,
15331 none_type,
15332 /*is_template=*/is_template,
15333 /*is_namespace=*/false,
15334 /*check_dependency=*/true,
15335 /*ambiguous_decls=*/NULL,
15336 token->location);
15337 /* See if the default argument is valid. */
15338 default_argument = check_template_template_default_arg (default_argument);
15339 pop_deferring_access_checks ();
15340 return default_argument;
15341 }
15342
15343 /* Parse a template-parameter.
15344
15345 template-parameter:
15346 type-parameter
15347 parameter-declaration
15348
15349 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15350 the parameter. The TREE_PURPOSE is the default value, if any.
15351 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15352 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15353 set to true iff this parameter is a parameter pack. */
15354
15355 static tree
15356 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15357 bool *is_parameter_pack)
15358 {
15359 cp_token *token;
15360 cp_parameter_declarator *parameter_declarator;
15361 tree parm;
15362
15363 /* Assume it is a type parameter or a template parameter. */
15364 *is_non_type = false;
15365 /* Assume it not a parameter pack. */
15366 *is_parameter_pack = false;
15367 /* Peek at the next token. */
15368 token = cp_lexer_peek_token (parser->lexer);
15369 /* If it is `template', we have a type-parameter. */
15370 if (token->keyword == RID_TEMPLATE)
15371 return cp_parser_type_parameter (parser, is_parameter_pack);
15372 /* If it is `class' or `typename' we do not know yet whether it is a
15373 type parameter or a non-type parameter. Consider:
15374
15375 template <typename T, typename T::X X> ...
15376
15377 or:
15378
15379 template <class C, class D*> ...
15380
15381 Here, the first parameter is a type parameter, and the second is
15382 a non-type parameter. We can tell by looking at the token after
15383 the identifier -- if it is a `,', `=', or `>' then we have a type
15384 parameter. */
15385 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15386 {
15387 /* Peek at the token after `class' or `typename'. */
15388 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15389 /* If it's an ellipsis, we have a template type parameter
15390 pack. */
15391 if (token->type == CPP_ELLIPSIS)
15392 return cp_parser_type_parameter (parser, is_parameter_pack);
15393 /* If it's an identifier, skip it. */
15394 if (token->type == CPP_NAME)
15395 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15396 /* Now, see if the token looks like the end of a template
15397 parameter. */
15398 if (token->type == CPP_COMMA
15399 || token->type == CPP_EQ
15400 || token->type == CPP_GREATER)
15401 return cp_parser_type_parameter (parser, is_parameter_pack);
15402 }
15403
15404 /* Otherwise, it is a non-type parameter or a constrained parameter.
15405
15406 [temp.param]
15407
15408 When parsing a default template-argument for a non-type
15409 template-parameter, the first non-nested `>' is taken as the end
15410 of the template parameter-list rather than a greater-than
15411 operator. */
15412 parameter_declarator
15413 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15414 /*parenthesized_p=*/NULL);
15415
15416 if (!parameter_declarator)
15417 return error_mark_node;
15418
15419 /* If the parameter declaration is marked as a parameter pack, set
15420 *IS_PARAMETER_PACK to notify the caller. */
15421 if (parameter_declarator->template_parameter_pack_p)
15422 *is_parameter_pack = true;
15423
15424 if (parameter_declarator->default_argument)
15425 {
15426 /* Can happen in some cases of erroneous input (c++/34892). */
15427 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15428 /* Consume the `...' for better error recovery. */
15429 cp_lexer_consume_token (parser->lexer);
15430 }
15431
15432 // The parameter may have been constrained.
15433 if (is_constrained_parameter (parameter_declarator))
15434 return finish_constrained_parameter (parser,
15435 parameter_declarator,
15436 is_non_type,
15437 is_parameter_pack);
15438
15439 // Now we're sure that the parameter is a non-type parameter.
15440 *is_non_type = true;
15441
15442 parm = grokdeclarator (parameter_declarator->declarator,
15443 &parameter_declarator->decl_specifiers,
15444 TPARM, /*initialized=*/0,
15445 /*attrlist=*/NULL);
15446 if (parm == error_mark_node)
15447 return error_mark_node;
15448
15449 return build_tree_list (parameter_declarator->default_argument, parm);
15450 }
15451
15452 /* Parse a type-parameter.
15453
15454 type-parameter:
15455 class identifier [opt]
15456 class identifier [opt] = type-id
15457 typename identifier [opt]
15458 typename identifier [opt] = type-id
15459 template < template-parameter-list > class identifier [opt]
15460 template < template-parameter-list > class identifier [opt]
15461 = id-expression
15462
15463 GNU Extension (variadic templates):
15464
15465 type-parameter:
15466 class ... identifier [opt]
15467 typename ... identifier [opt]
15468
15469 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15470 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15471 the declaration of the parameter.
15472
15473 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15474
15475 static tree
15476 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15477 {
15478 cp_token *token;
15479 tree parameter;
15480
15481 /* Look for a keyword to tell us what kind of parameter this is. */
15482 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15483 if (!token)
15484 return error_mark_node;
15485
15486 switch (token->keyword)
15487 {
15488 case RID_CLASS:
15489 case RID_TYPENAME:
15490 {
15491 tree identifier;
15492 tree default_argument;
15493
15494 /* If the next token is an ellipsis, we have a template
15495 argument pack. */
15496 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15497 {
15498 /* Consume the `...' token. */
15499 cp_lexer_consume_token (parser->lexer);
15500 maybe_warn_variadic_templates ();
15501
15502 *is_parameter_pack = true;
15503 }
15504
15505 /* If the next token is an identifier, then it names the
15506 parameter. */
15507 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15508 identifier = cp_parser_identifier (parser);
15509 else
15510 identifier = NULL_TREE;
15511
15512 /* Create the parameter. */
15513 parameter = finish_template_type_parm (class_type_node, identifier);
15514
15515 /* If the next token is an `=', we have a default argument. */
15516 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15517 {
15518 default_argument
15519 = cp_parser_default_type_template_argument (parser);
15520
15521 /* Template parameter packs cannot have default
15522 arguments. */
15523 if (*is_parameter_pack)
15524 {
15525 if (identifier)
15526 error_at (token->location,
15527 "template parameter pack %qD cannot have a "
15528 "default argument", identifier);
15529 else
15530 error_at (token->location,
15531 "template parameter packs cannot have "
15532 "default arguments");
15533 default_argument = NULL_TREE;
15534 }
15535 else if (check_for_bare_parameter_packs (default_argument))
15536 default_argument = error_mark_node;
15537 }
15538 else
15539 default_argument = NULL_TREE;
15540
15541 /* Create the combined representation of the parameter and the
15542 default argument. */
15543 parameter = build_tree_list (default_argument, parameter);
15544 }
15545 break;
15546
15547 case RID_TEMPLATE:
15548 {
15549 tree identifier;
15550 tree default_argument;
15551
15552 /* Look for the `<'. */
15553 cp_parser_require (parser, CPP_LESS, RT_LESS);
15554 /* Parse the template-parameter-list. */
15555 cp_parser_template_parameter_list (parser);
15556 /* Look for the `>'. */
15557 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15558
15559 // If template requirements are present, parse them.
15560 if (flag_concepts)
15561 {
15562 tree reqs = get_shorthand_constraints (current_template_parms);
15563 if (tree r = cp_parser_requires_clause_opt (parser))
15564 reqs = conjoin_constraints (reqs, normalize_expression (r));
15565 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15566 }
15567
15568 /* Look for the `class' or 'typename' keywords. */
15569 cp_parser_type_parameter_key (parser);
15570 /* If the next token is an ellipsis, we have a template
15571 argument pack. */
15572 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15573 {
15574 /* Consume the `...' token. */
15575 cp_lexer_consume_token (parser->lexer);
15576 maybe_warn_variadic_templates ();
15577
15578 *is_parameter_pack = true;
15579 }
15580 /* If the next token is an `=', then there is a
15581 default-argument. If the next token is a `>', we are at
15582 the end of the parameter-list. If the next token is a `,',
15583 then we are at the end of this parameter. */
15584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15585 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15586 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15587 {
15588 identifier = cp_parser_identifier (parser);
15589 /* Treat invalid names as if the parameter were nameless. */
15590 if (identifier == error_mark_node)
15591 identifier = NULL_TREE;
15592 }
15593 else
15594 identifier = NULL_TREE;
15595
15596 /* Create the template parameter. */
15597 parameter = finish_template_template_parm (class_type_node,
15598 identifier);
15599
15600 /* If the next token is an `=', then there is a
15601 default-argument. */
15602 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15603 {
15604 default_argument
15605 = cp_parser_default_template_template_argument (parser);
15606
15607 /* Template parameter packs cannot have default
15608 arguments. */
15609 if (*is_parameter_pack)
15610 {
15611 if (identifier)
15612 error_at (token->location,
15613 "template parameter pack %qD cannot "
15614 "have a default argument",
15615 identifier);
15616 else
15617 error_at (token->location, "template parameter packs cannot "
15618 "have default arguments");
15619 default_argument = NULL_TREE;
15620 }
15621 }
15622 else
15623 default_argument = NULL_TREE;
15624
15625 /* Create the combined representation of the parameter and the
15626 default argument. */
15627 parameter = build_tree_list (default_argument, parameter);
15628 }
15629 break;
15630
15631 default:
15632 gcc_unreachable ();
15633 break;
15634 }
15635
15636 return parameter;
15637 }
15638
15639 /* Parse a template-id.
15640
15641 template-id:
15642 template-name < template-argument-list [opt] >
15643
15644 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15645 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15646 returned. Otherwise, if the template-name names a function, or set
15647 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15648 names a class, returns a TYPE_DECL for the specialization.
15649
15650 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15651 uninstantiated templates. */
15652
15653 static tree
15654 cp_parser_template_id (cp_parser *parser,
15655 bool template_keyword_p,
15656 bool check_dependency_p,
15657 enum tag_types tag_type,
15658 bool is_declaration)
15659 {
15660 tree templ;
15661 tree arguments;
15662 tree template_id;
15663 cp_token_position start_of_id = 0;
15664 cp_token *next_token = NULL, *next_token_2 = NULL;
15665 bool is_identifier;
15666
15667 /* If the next token corresponds to a template-id, there is no need
15668 to reparse it. */
15669 cp_token *token = cp_lexer_peek_token (parser->lexer);
15670 if (token->type == CPP_TEMPLATE_ID)
15671 {
15672 cp_lexer_consume_token (parser->lexer);
15673 return saved_checks_value (token->u.tree_check_value);
15674 }
15675
15676 /* Avoid performing name lookup if there is no possibility of
15677 finding a template-id. */
15678 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15679 || (token->type == CPP_NAME
15680 && !cp_parser_nth_token_starts_template_argument_list_p
15681 (parser, 2)))
15682 {
15683 cp_parser_error (parser, "expected template-id");
15684 return error_mark_node;
15685 }
15686
15687 /* Remember where the template-id starts. */
15688 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15689 start_of_id = cp_lexer_token_position (parser->lexer, false);
15690
15691 push_deferring_access_checks (dk_deferred);
15692
15693 /* Parse the template-name. */
15694 is_identifier = false;
15695 templ = cp_parser_template_name (parser, template_keyword_p,
15696 check_dependency_p,
15697 is_declaration,
15698 tag_type,
15699 &is_identifier);
15700 if (templ == error_mark_node || is_identifier)
15701 {
15702 pop_deferring_access_checks ();
15703 return templ;
15704 }
15705
15706 /* Since we're going to preserve any side-effects from this parse, set up a
15707 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15708 in the template arguments. */
15709 tentative_firewall firewall (parser);
15710
15711 /* If we find the sequence `[:' after a template-name, it's probably
15712 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15713 parse correctly the argument list. */
15714 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15715 == CPP_OPEN_SQUARE)
15716 && next_token->flags & DIGRAPH
15717 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15718 == CPP_COLON)
15719 && !(next_token_2->flags & PREV_WHITE))
15720 {
15721 cp_parser_parse_tentatively (parser);
15722 /* Change `:' into `::'. */
15723 next_token_2->type = CPP_SCOPE;
15724 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15725 CPP_LESS. */
15726 cp_lexer_consume_token (parser->lexer);
15727
15728 /* Parse the arguments. */
15729 arguments = cp_parser_enclosed_template_argument_list (parser);
15730 if (!cp_parser_parse_definitely (parser))
15731 {
15732 /* If we couldn't parse an argument list, then we revert our changes
15733 and return simply an error. Maybe this is not a template-id
15734 after all. */
15735 next_token_2->type = CPP_COLON;
15736 cp_parser_error (parser, "expected %<<%>");
15737 pop_deferring_access_checks ();
15738 return error_mark_node;
15739 }
15740 /* Otherwise, emit an error about the invalid digraph, but continue
15741 parsing because we got our argument list. */
15742 if (permerror (next_token->location,
15743 "%<<::%> cannot begin a template-argument list"))
15744 {
15745 static bool hint = false;
15746 inform (next_token->location,
15747 "%<<:%> is an alternate spelling for %<[%>."
15748 " Insert whitespace between %<<%> and %<::%>");
15749 if (!hint && !flag_permissive)
15750 {
15751 inform (next_token->location, "(if you use %<-fpermissive%> "
15752 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15753 "accept your code)");
15754 hint = true;
15755 }
15756 }
15757 }
15758 else
15759 {
15760 /* Look for the `<' that starts the template-argument-list. */
15761 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15762 {
15763 pop_deferring_access_checks ();
15764 return error_mark_node;
15765 }
15766 /* Parse the arguments. */
15767 arguments = cp_parser_enclosed_template_argument_list (parser);
15768 }
15769
15770 /* Set the location to be of the form:
15771 template-name < template-argument-list [opt] >
15772 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15773 with caret == start at the start of the template-name,
15774 ranging until the closing '>'. */
15775 location_t finish_loc
15776 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15777 location_t combined_loc
15778 = make_location (token->location, token->location, finish_loc);
15779
15780 /* Build a representation of the specialization. */
15781 if (identifier_p (templ))
15782 template_id = build_min_nt_loc (combined_loc,
15783 TEMPLATE_ID_EXPR,
15784 templ, arguments);
15785 else if (DECL_TYPE_TEMPLATE_P (templ)
15786 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15787 {
15788 bool entering_scope;
15789 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15790 template (rather than some instantiation thereof) only if
15791 is not nested within some other construct. For example, in
15792 "template <typename T> void f(T) { A<T>::", A<T> is just an
15793 instantiation of A. */
15794 entering_scope = (template_parm_scope_p ()
15795 && cp_lexer_next_token_is (parser->lexer,
15796 CPP_SCOPE));
15797 template_id
15798 = finish_template_type (templ, arguments, entering_scope);
15799 }
15800 /* A template-like identifier may be a partial concept id. */
15801 else if (flag_concepts
15802 && (template_id = (cp_parser_maybe_partial_concept_id
15803 (parser, templ, arguments))))
15804 return template_id;
15805 else if (variable_template_p (templ))
15806 {
15807 template_id = lookup_template_variable (templ, arguments);
15808 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15809 SET_EXPR_LOCATION (template_id, combined_loc);
15810 }
15811 else
15812 {
15813 /* If it's not a class-template or a template-template, it should be
15814 a function-template. */
15815 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15816 || TREE_CODE (templ) == OVERLOAD
15817 || BASELINK_P (templ)));
15818
15819 template_id = lookup_template_function (templ, arguments);
15820 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15821 SET_EXPR_LOCATION (template_id, combined_loc);
15822 }
15823
15824 /* If parsing tentatively, replace the sequence of tokens that makes
15825 up the template-id with a CPP_TEMPLATE_ID token. That way,
15826 should we re-parse the token stream, we will not have to repeat
15827 the effort required to do the parse, nor will we issue duplicate
15828 error messages about problems during instantiation of the
15829 template. */
15830 if (start_of_id
15831 /* Don't do this if we had a parse error in a declarator; re-parsing
15832 might succeed if a name changes meaning (60361). */
15833 && !(cp_parser_error_occurred (parser)
15834 && cp_parser_parsing_tentatively (parser)
15835 && parser->in_declarator_p))
15836 {
15837 /* Reset the contents of the START_OF_ID token. */
15838 token->type = CPP_TEMPLATE_ID;
15839 token->location = combined_loc;
15840
15841 /* We must mark the lookup as kept, so we don't throw it away on
15842 the first parse. */
15843 if (is_overloaded_fn (template_id))
15844 lookup_keep (get_fns (template_id), true);
15845
15846 /* Retrieve any deferred checks. Do not pop this access checks yet
15847 so the memory will not be reclaimed during token replacing below. */
15848 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15849 token->u.tree_check_value->value = template_id;
15850 token->u.tree_check_value->checks = get_deferred_access_checks ();
15851 token->keyword = RID_MAX;
15852
15853 /* Purge all subsequent tokens. */
15854 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15855
15856 /* ??? Can we actually assume that, if template_id ==
15857 error_mark_node, we will have issued a diagnostic to the
15858 user, as opposed to simply marking the tentative parse as
15859 failed? */
15860 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15861 error_at (token->location, "parse error in template argument list");
15862 }
15863
15864 pop_to_parent_deferring_access_checks ();
15865 return template_id;
15866 }
15867
15868 /* Parse a template-name.
15869
15870 template-name:
15871 identifier
15872
15873 The standard should actually say:
15874
15875 template-name:
15876 identifier
15877 operator-function-id
15878
15879 A defect report has been filed about this issue.
15880
15881 A conversion-function-id cannot be a template name because they cannot
15882 be part of a template-id. In fact, looking at this code:
15883
15884 a.operator K<int>()
15885
15886 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15887 It is impossible to call a templated conversion-function-id with an
15888 explicit argument list, since the only allowed template parameter is
15889 the type to which it is converting.
15890
15891 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15892 `template' keyword, in a construction like:
15893
15894 T::template f<3>()
15895
15896 In that case `f' is taken to be a template-name, even though there
15897 is no way of knowing for sure.
15898
15899 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15900 name refers to a set of overloaded functions, at least one of which
15901 is a template, or an IDENTIFIER_NODE with the name of the template,
15902 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15903 names are looked up inside uninstantiated templates. */
15904
15905 static tree
15906 cp_parser_template_name (cp_parser* parser,
15907 bool template_keyword_p,
15908 bool check_dependency_p,
15909 bool is_declaration,
15910 enum tag_types tag_type,
15911 bool *is_identifier)
15912 {
15913 tree identifier;
15914 tree decl;
15915 cp_token *token = cp_lexer_peek_token (parser->lexer);
15916
15917 /* If the next token is `operator', then we have either an
15918 operator-function-id or a conversion-function-id. */
15919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15920 {
15921 /* We don't know whether we're looking at an
15922 operator-function-id or a conversion-function-id. */
15923 cp_parser_parse_tentatively (parser);
15924 /* Try an operator-function-id. */
15925 identifier = cp_parser_operator_function_id (parser);
15926 /* If that didn't work, try a conversion-function-id. */
15927 if (!cp_parser_parse_definitely (parser))
15928 {
15929 cp_parser_error (parser, "expected template-name");
15930 return error_mark_node;
15931 }
15932 }
15933 /* Look for the identifier. */
15934 else
15935 identifier = cp_parser_identifier (parser);
15936
15937 /* If we didn't find an identifier, we don't have a template-id. */
15938 if (identifier == error_mark_node)
15939 return error_mark_node;
15940
15941 /* If the name immediately followed the `template' keyword, then it
15942 is a template-name. However, if the next token is not `<', then
15943 we do not treat it as a template-name, since it is not being used
15944 as part of a template-id. This enables us to handle constructs
15945 like:
15946
15947 template <typename T> struct S { S(); };
15948 template <typename T> S<T>::S();
15949
15950 correctly. We would treat `S' as a template -- if it were `S<T>'
15951 -- but we do not if there is no `<'. */
15952
15953 if (processing_template_decl
15954 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15955 {
15956 /* In a declaration, in a dependent context, we pretend that the
15957 "template" keyword was present in order to improve error
15958 recovery. For example, given:
15959
15960 template <typename T> void f(T::X<int>);
15961
15962 we want to treat "X<int>" as a template-id. */
15963 if (is_declaration
15964 && !template_keyword_p
15965 && parser->scope && TYPE_P (parser->scope)
15966 && check_dependency_p
15967 && dependent_scope_p (parser->scope)
15968 /* Do not do this for dtors (or ctors), since they never
15969 need the template keyword before their name. */
15970 && !constructor_name_p (identifier, parser->scope))
15971 {
15972 cp_token_position start = 0;
15973
15974 /* Explain what went wrong. */
15975 error_at (token->location, "non-template %qD used as template",
15976 identifier);
15977 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15978 parser->scope, identifier);
15979 /* If parsing tentatively, find the location of the "<" token. */
15980 if (cp_parser_simulate_error (parser))
15981 start = cp_lexer_token_position (parser->lexer, true);
15982 /* Parse the template arguments so that we can issue error
15983 messages about them. */
15984 cp_lexer_consume_token (parser->lexer);
15985 cp_parser_enclosed_template_argument_list (parser);
15986 /* Skip tokens until we find a good place from which to
15987 continue parsing. */
15988 cp_parser_skip_to_closing_parenthesis (parser,
15989 /*recovering=*/true,
15990 /*or_comma=*/true,
15991 /*consume_paren=*/false);
15992 /* If parsing tentatively, permanently remove the
15993 template argument list. That will prevent duplicate
15994 error messages from being issued about the missing
15995 "template" keyword. */
15996 if (start)
15997 cp_lexer_purge_tokens_after (parser->lexer, start);
15998 if (is_identifier)
15999 *is_identifier = true;
16000 parser->context->object_type = NULL_TREE;
16001 return identifier;
16002 }
16003
16004 /* If the "template" keyword is present, then there is generally
16005 no point in doing name-lookup, so we just return IDENTIFIER.
16006 But, if the qualifying scope is non-dependent then we can
16007 (and must) do name-lookup normally. */
16008 if (template_keyword_p)
16009 {
16010 tree scope = (parser->scope ? parser->scope
16011 : parser->context->object_type);
16012 if (scope && TYPE_P (scope)
16013 && (!CLASS_TYPE_P (scope)
16014 || (check_dependency_p && dependent_type_p (scope))))
16015 {
16016 /* We're optimizing away the call to cp_parser_lookup_name, but
16017 we still need to do this. */
16018 parser->context->object_type = NULL_TREE;
16019 return identifier;
16020 }
16021 }
16022 }
16023
16024 /* Look up the name. */
16025 decl = cp_parser_lookup_name (parser, identifier,
16026 tag_type,
16027 /*is_template=*/true,
16028 /*is_namespace=*/false,
16029 check_dependency_p,
16030 /*ambiguous_decls=*/NULL,
16031 token->location);
16032
16033 decl = strip_using_decl (decl);
16034
16035 /* If DECL is a template, then the name was a template-name. */
16036 if (TREE_CODE (decl) == TEMPLATE_DECL)
16037 {
16038 if (TREE_DEPRECATED (decl)
16039 && deprecated_state != DEPRECATED_SUPPRESS)
16040 warn_deprecated_use (decl, NULL_TREE);
16041 }
16042 else
16043 {
16044 /* The standard does not explicitly indicate whether a name that
16045 names a set of overloaded declarations, some of which are
16046 templates, is a template-name. However, such a name should
16047 be a template-name; otherwise, there is no way to form a
16048 template-id for the overloaded templates. */
16049 bool found = false;
16050
16051 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16052 !found && iter; ++iter)
16053 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16054 found = true;
16055
16056 if (!found)
16057 {
16058 /* The name does not name a template. */
16059 cp_parser_error (parser, "expected template-name");
16060 return error_mark_node;
16061 }
16062 }
16063
16064 /* If DECL is dependent, and refers to a function, then just return
16065 its name; we will look it up again during template instantiation. */
16066 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16067 {
16068 tree scope = ovl_scope (decl);
16069 if (TYPE_P (scope) && dependent_type_p (scope))
16070 return identifier;
16071 }
16072
16073 return decl;
16074 }
16075
16076 /* Parse a template-argument-list.
16077
16078 template-argument-list:
16079 template-argument ... [opt]
16080 template-argument-list , template-argument ... [opt]
16081
16082 Returns a TREE_VEC containing the arguments. */
16083
16084 static tree
16085 cp_parser_template_argument_list (cp_parser* parser)
16086 {
16087 tree fixed_args[10];
16088 unsigned n_args = 0;
16089 unsigned alloced = 10;
16090 tree *arg_ary = fixed_args;
16091 tree vec;
16092 bool saved_in_template_argument_list_p;
16093 bool saved_ice_p;
16094 bool saved_non_ice_p;
16095
16096 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16097 parser->in_template_argument_list_p = true;
16098 /* Even if the template-id appears in an integral
16099 constant-expression, the contents of the argument list do
16100 not. */
16101 saved_ice_p = parser->integral_constant_expression_p;
16102 parser->integral_constant_expression_p = false;
16103 saved_non_ice_p = parser->non_integral_constant_expression_p;
16104 parser->non_integral_constant_expression_p = false;
16105
16106 /* Parse the arguments. */
16107 do
16108 {
16109 tree argument;
16110
16111 if (n_args)
16112 /* Consume the comma. */
16113 cp_lexer_consume_token (parser->lexer);
16114
16115 /* Parse the template-argument. */
16116 argument = cp_parser_template_argument (parser);
16117
16118 /* If the next token is an ellipsis, we're expanding a template
16119 argument pack. */
16120 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16121 {
16122 if (argument == error_mark_node)
16123 {
16124 cp_token *token = cp_lexer_peek_token (parser->lexer);
16125 error_at (token->location,
16126 "expected parameter pack before %<...%>");
16127 }
16128 /* Consume the `...' token. */
16129 cp_lexer_consume_token (parser->lexer);
16130
16131 /* Make the argument into a TYPE_PACK_EXPANSION or
16132 EXPR_PACK_EXPANSION. */
16133 argument = make_pack_expansion (argument);
16134 }
16135
16136 if (n_args == alloced)
16137 {
16138 alloced *= 2;
16139
16140 if (arg_ary == fixed_args)
16141 {
16142 arg_ary = XNEWVEC (tree, alloced);
16143 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16144 }
16145 else
16146 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16147 }
16148 arg_ary[n_args++] = argument;
16149 }
16150 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16151
16152 vec = make_tree_vec (n_args);
16153
16154 while (n_args--)
16155 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16156
16157 if (arg_ary != fixed_args)
16158 free (arg_ary);
16159 parser->non_integral_constant_expression_p = saved_non_ice_p;
16160 parser->integral_constant_expression_p = saved_ice_p;
16161 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16162 if (CHECKING_P)
16163 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16164 return vec;
16165 }
16166
16167 /* Parse a template-argument.
16168
16169 template-argument:
16170 assignment-expression
16171 type-id
16172 id-expression
16173
16174 The representation is that of an assignment-expression, type-id, or
16175 id-expression -- except that the qualified id-expression is
16176 evaluated, so that the value returned is either a DECL or an
16177 OVERLOAD.
16178
16179 Although the standard says "assignment-expression", it forbids
16180 throw-expressions or assignments in the template argument.
16181 Therefore, we use "conditional-expression" instead. */
16182
16183 static tree
16184 cp_parser_template_argument (cp_parser* parser)
16185 {
16186 tree argument;
16187 bool template_p;
16188 bool address_p;
16189 bool maybe_type_id = false;
16190 cp_token *token = NULL, *argument_start_token = NULL;
16191 location_t loc = 0;
16192 cp_id_kind idk;
16193
16194 /* There's really no way to know what we're looking at, so we just
16195 try each alternative in order.
16196
16197 [temp.arg]
16198
16199 In a template-argument, an ambiguity between a type-id and an
16200 expression is resolved to a type-id, regardless of the form of
16201 the corresponding template-parameter.
16202
16203 Therefore, we try a type-id first. */
16204 cp_parser_parse_tentatively (parser);
16205 argument = cp_parser_template_type_arg (parser);
16206 /* If there was no error parsing the type-id but the next token is a
16207 '>>', our behavior depends on which dialect of C++ we're
16208 parsing. In C++98, we probably found a typo for '> >'. But there
16209 are type-id which are also valid expressions. For instance:
16210
16211 struct X { int operator >> (int); };
16212 template <int V> struct Foo {};
16213 Foo<X () >> 5> r;
16214
16215 Here 'X()' is a valid type-id of a function type, but the user just
16216 wanted to write the expression "X() >> 5". Thus, we remember that we
16217 found a valid type-id, but we still try to parse the argument as an
16218 expression to see what happens.
16219
16220 In C++0x, the '>>' will be considered two separate '>'
16221 tokens. */
16222 if (!cp_parser_error_occurred (parser)
16223 && cxx_dialect == cxx98
16224 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16225 {
16226 maybe_type_id = true;
16227 cp_parser_abort_tentative_parse (parser);
16228 }
16229 else
16230 {
16231 /* If the next token isn't a `,' or a `>', then this argument wasn't
16232 really finished. This means that the argument is not a valid
16233 type-id. */
16234 if (!cp_parser_next_token_ends_template_argument_p (parser))
16235 cp_parser_error (parser, "expected template-argument");
16236 /* If that worked, we're done. */
16237 if (cp_parser_parse_definitely (parser))
16238 return argument;
16239 }
16240 /* We're still not sure what the argument will be. */
16241 cp_parser_parse_tentatively (parser);
16242 /* Try a template. */
16243 argument_start_token = cp_lexer_peek_token (parser->lexer);
16244 argument = cp_parser_id_expression (parser,
16245 /*template_keyword_p=*/false,
16246 /*check_dependency_p=*/true,
16247 &template_p,
16248 /*declarator_p=*/false,
16249 /*optional_p=*/false);
16250 /* If the next token isn't a `,' or a `>', then this argument wasn't
16251 really finished. */
16252 if (!cp_parser_next_token_ends_template_argument_p (parser))
16253 cp_parser_error (parser, "expected template-argument");
16254 if (!cp_parser_error_occurred (parser))
16255 {
16256 /* Figure out what is being referred to. If the id-expression
16257 was for a class template specialization, then we will have a
16258 TYPE_DECL at this point. There is no need to do name lookup
16259 at this point in that case. */
16260 if (TREE_CODE (argument) != TYPE_DECL)
16261 argument = cp_parser_lookup_name (parser, argument,
16262 none_type,
16263 /*is_template=*/template_p,
16264 /*is_namespace=*/false,
16265 /*check_dependency=*/true,
16266 /*ambiguous_decls=*/NULL,
16267 argument_start_token->location);
16268 /* Handle a constrained-type-specifier for a non-type template
16269 parameter. */
16270 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16271 argument = decl;
16272 else if (TREE_CODE (argument) != TEMPLATE_DECL
16273 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16274 cp_parser_error (parser, "expected template-name");
16275 }
16276 if (cp_parser_parse_definitely (parser))
16277 {
16278 if (TREE_DEPRECATED (argument))
16279 warn_deprecated_use (argument, NULL_TREE);
16280 return argument;
16281 }
16282 /* It must be a non-type argument. In C++17 any constant-expression is
16283 allowed. */
16284 if (cxx_dialect > cxx14)
16285 goto general_expr;
16286
16287 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16288
16289 -- an integral constant-expression of integral or enumeration
16290 type; or
16291
16292 -- the name of a non-type template-parameter; or
16293
16294 -- the name of an object or function with external linkage...
16295
16296 -- the address of an object or function with external linkage...
16297
16298 -- a pointer to member... */
16299 /* Look for a non-type template parameter. */
16300 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16301 {
16302 cp_parser_parse_tentatively (parser);
16303 argument = cp_parser_primary_expression (parser,
16304 /*address_p=*/false,
16305 /*cast_p=*/false,
16306 /*template_arg_p=*/true,
16307 &idk);
16308 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16309 || !cp_parser_next_token_ends_template_argument_p (parser))
16310 cp_parser_simulate_error (parser);
16311 if (cp_parser_parse_definitely (parser))
16312 return argument;
16313 }
16314
16315 /* If the next token is "&", the argument must be the address of an
16316 object or function with external linkage. */
16317 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16318 if (address_p)
16319 {
16320 loc = cp_lexer_peek_token (parser->lexer)->location;
16321 cp_lexer_consume_token (parser->lexer);
16322 }
16323 /* See if we might have an id-expression. */
16324 token = cp_lexer_peek_token (parser->lexer);
16325 if (token->type == CPP_NAME
16326 || token->keyword == RID_OPERATOR
16327 || token->type == CPP_SCOPE
16328 || token->type == CPP_TEMPLATE_ID
16329 || token->type == CPP_NESTED_NAME_SPECIFIER)
16330 {
16331 cp_parser_parse_tentatively (parser);
16332 argument = cp_parser_primary_expression (parser,
16333 address_p,
16334 /*cast_p=*/false,
16335 /*template_arg_p=*/true,
16336 &idk);
16337 if (cp_parser_error_occurred (parser)
16338 || !cp_parser_next_token_ends_template_argument_p (parser))
16339 cp_parser_abort_tentative_parse (parser);
16340 else
16341 {
16342 tree probe;
16343
16344 if (INDIRECT_REF_P (argument))
16345 {
16346 /* Strip the dereference temporarily. */
16347 gcc_assert (REFERENCE_REF_P (argument));
16348 argument = TREE_OPERAND (argument, 0);
16349 }
16350
16351 /* If we're in a template, we represent a qualified-id referring
16352 to a static data member as a SCOPE_REF even if the scope isn't
16353 dependent so that we can check access control later. */
16354 probe = argument;
16355 if (TREE_CODE (probe) == SCOPE_REF)
16356 probe = TREE_OPERAND (probe, 1);
16357 if (VAR_P (probe))
16358 {
16359 /* A variable without external linkage might still be a
16360 valid constant-expression, so no error is issued here
16361 if the external-linkage check fails. */
16362 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16363 cp_parser_simulate_error (parser);
16364 }
16365 else if (is_overloaded_fn (argument))
16366 /* All overloaded functions are allowed; if the external
16367 linkage test does not pass, an error will be issued
16368 later. */
16369 ;
16370 else if (address_p
16371 && (TREE_CODE (argument) == OFFSET_REF
16372 || TREE_CODE (argument) == SCOPE_REF))
16373 /* A pointer-to-member. */
16374 ;
16375 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16376 ;
16377 else
16378 cp_parser_simulate_error (parser);
16379
16380 if (cp_parser_parse_definitely (parser))
16381 {
16382 if (address_p)
16383 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16384 tf_warning_or_error);
16385 else
16386 argument = convert_from_reference (argument);
16387 return argument;
16388 }
16389 }
16390 }
16391 /* If the argument started with "&", there are no other valid
16392 alternatives at this point. */
16393 if (address_p)
16394 {
16395 cp_parser_error (parser, "invalid non-type template argument");
16396 return error_mark_node;
16397 }
16398
16399 general_expr:
16400 /* If the argument wasn't successfully parsed as a type-id followed
16401 by '>>', the argument can only be a constant expression now.
16402 Otherwise, we try parsing the constant-expression tentatively,
16403 because the argument could really be a type-id. */
16404 if (maybe_type_id)
16405 cp_parser_parse_tentatively (parser);
16406
16407 if (cxx_dialect <= cxx14)
16408 argument = cp_parser_constant_expression (parser);
16409 else
16410 {
16411 /* With C++17 generalized non-type template arguments we need to handle
16412 lvalue constant expressions, too. */
16413 argument = cp_parser_assignment_expression (parser);
16414 require_potential_constant_expression (argument);
16415 }
16416
16417 if (!maybe_type_id)
16418 return argument;
16419 if (!cp_parser_next_token_ends_template_argument_p (parser))
16420 cp_parser_error (parser, "expected template-argument");
16421 if (cp_parser_parse_definitely (parser))
16422 return argument;
16423 /* We did our best to parse the argument as a non type-id, but that
16424 was the only alternative that matched (albeit with a '>' after
16425 it). We can assume it's just a typo from the user, and a
16426 diagnostic will then be issued. */
16427 return cp_parser_template_type_arg (parser);
16428 }
16429
16430 /* Parse an explicit-instantiation.
16431
16432 explicit-instantiation:
16433 template declaration
16434
16435 Although the standard says `declaration', what it really means is:
16436
16437 explicit-instantiation:
16438 template decl-specifier-seq [opt] declarator [opt] ;
16439
16440 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16441 supposed to be allowed. A defect report has been filed about this
16442 issue.
16443
16444 GNU Extension:
16445
16446 explicit-instantiation:
16447 storage-class-specifier template
16448 decl-specifier-seq [opt] declarator [opt] ;
16449 function-specifier template
16450 decl-specifier-seq [opt] declarator [opt] ; */
16451
16452 static void
16453 cp_parser_explicit_instantiation (cp_parser* parser)
16454 {
16455 int declares_class_or_enum;
16456 cp_decl_specifier_seq decl_specifiers;
16457 tree extension_specifier = NULL_TREE;
16458
16459 timevar_push (TV_TEMPLATE_INST);
16460
16461 /* Look for an (optional) storage-class-specifier or
16462 function-specifier. */
16463 if (cp_parser_allow_gnu_extensions_p (parser))
16464 {
16465 extension_specifier
16466 = cp_parser_storage_class_specifier_opt (parser);
16467 if (!extension_specifier)
16468 extension_specifier
16469 = cp_parser_function_specifier_opt (parser,
16470 /*decl_specs=*/NULL);
16471 }
16472
16473 /* Look for the `template' keyword. */
16474 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16475 /* Let the front end know that we are processing an explicit
16476 instantiation. */
16477 begin_explicit_instantiation ();
16478 /* [temp.explicit] says that we are supposed to ignore access
16479 control while processing explicit instantiation directives. */
16480 push_deferring_access_checks (dk_no_check);
16481 /* Parse a decl-specifier-seq. */
16482 cp_parser_decl_specifier_seq (parser,
16483 CP_PARSER_FLAGS_OPTIONAL,
16484 &decl_specifiers,
16485 &declares_class_or_enum);
16486 /* If there was exactly one decl-specifier, and it declared a class,
16487 and there's no declarator, then we have an explicit type
16488 instantiation. */
16489 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16490 {
16491 tree type;
16492
16493 type = check_tag_decl (&decl_specifiers,
16494 /*explicit_type_instantiation_p=*/true);
16495 /* Turn access control back on for names used during
16496 template instantiation. */
16497 pop_deferring_access_checks ();
16498 if (type)
16499 do_type_instantiation (type, extension_specifier,
16500 /*complain=*/tf_error);
16501 }
16502 else
16503 {
16504 cp_declarator *declarator;
16505 tree decl;
16506
16507 /* Parse the declarator. */
16508 declarator
16509 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16510 /*ctor_dtor_or_conv_p=*/NULL,
16511 /*parenthesized_p=*/NULL,
16512 /*member_p=*/false,
16513 /*friend_p=*/false);
16514 if (declares_class_or_enum & 2)
16515 cp_parser_check_for_definition_in_return_type (declarator,
16516 decl_specifiers.type,
16517 decl_specifiers.locations[ds_type_spec]);
16518 if (declarator != cp_error_declarator)
16519 {
16520 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16521 permerror (decl_specifiers.locations[ds_inline],
16522 "explicit instantiation shall not use"
16523 " %<inline%> specifier");
16524 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16525 permerror (decl_specifiers.locations[ds_constexpr],
16526 "explicit instantiation shall not use"
16527 " %<constexpr%> specifier");
16528
16529 decl = grokdeclarator (declarator, &decl_specifiers,
16530 NORMAL, 0, &decl_specifiers.attributes);
16531 /* Turn access control back on for names used during
16532 template instantiation. */
16533 pop_deferring_access_checks ();
16534 /* Do the explicit instantiation. */
16535 do_decl_instantiation (decl, extension_specifier);
16536 }
16537 else
16538 {
16539 pop_deferring_access_checks ();
16540 /* Skip the body of the explicit instantiation. */
16541 cp_parser_skip_to_end_of_statement (parser);
16542 }
16543 }
16544 /* We're done with the instantiation. */
16545 end_explicit_instantiation ();
16546
16547 cp_parser_consume_semicolon_at_end_of_statement (parser);
16548
16549 timevar_pop (TV_TEMPLATE_INST);
16550 }
16551
16552 /* Parse an explicit-specialization.
16553
16554 explicit-specialization:
16555 template < > declaration
16556
16557 Although the standard says `declaration', what it really means is:
16558
16559 explicit-specialization:
16560 template <> decl-specifier [opt] init-declarator [opt] ;
16561 template <> function-definition
16562 template <> explicit-specialization
16563 template <> template-declaration */
16564
16565 static void
16566 cp_parser_explicit_specialization (cp_parser* parser)
16567 {
16568 bool need_lang_pop;
16569 cp_token *token = cp_lexer_peek_token (parser->lexer);
16570
16571 /* Look for the `template' keyword. */
16572 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16573 /* Look for the `<'. */
16574 cp_parser_require (parser, CPP_LESS, RT_LESS);
16575 /* Look for the `>'. */
16576 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16577 /* We have processed another parameter list. */
16578 ++parser->num_template_parameter_lists;
16579 /* [temp]
16580
16581 A template ... explicit specialization ... shall not have C
16582 linkage. */
16583 if (current_lang_name == lang_name_c)
16584 {
16585 error_at (token->location, "template specialization with C linkage");
16586 maybe_show_extern_c_location ();
16587 /* Give it C++ linkage to avoid confusing other parts of the
16588 front end. */
16589 push_lang_context (lang_name_cplusplus);
16590 need_lang_pop = true;
16591 }
16592 else
16593 need_lang_pop = false;
16594 /* Let the front end know that we are beginning a specialization. */
16595 if (!begin_specialization ())
16596 {
16597 end_specialization ();
16598 return;
16599 }
16600
16601 /* If the next keyword is `template', we need to figure out whether
16602 or not we're looking a template-declaration. */
16603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16604 {
16605 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16606 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16607 cp_parser_template_declaration_after_export (parser,
16608 /*member_p=*/false);
16609 else
16610 cp_parser_explicit_specialization (parser);
16611 }
16612 else
16613 /* Parse the dependent declaration. */
16614 cp_parser_single_declaration (parser,
16615 /*checks=*/NULL,
16616 /*member_p=*/false,
16617 /*explicit_specialization_p=*/true,
16618 /*friend_p=*/NULL);
16619 /* We're done with the specialization. */
16620 end_specialization ();
16621 /* For the erroneous case of a template with C linkage, we pushed an
16622 implicit C++ linkage scope; exit that scope now. */
16623 if (need_lang_pop)
16624 pop_lang_context ();
16625 /* We're done with this parameter list. */
16626 --parser->num_template_parameter_lists;
16627 }
16628
16629 /* Parse a type-specifier.
16630
16631 type-specifier:
16632 simple-type-specifier
16633 class-specifier
16634 enum-specifier
16635 elaborated-type-specifier
16636 cv-qualifier
16637
16638 GNU Extension:
16639
16640 type-specifier:
16641 __complex__
16642
16643 Returns a representation of the type-specifier. For a
16644 class-specifier, enum-specifier, or elaborated-type-specifier, a
16645 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16646
16647 The parser flags FLAGS is used to control type-specifier parsing.
16648
16649 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16650 in a decl-specifier-seq.
16651
16652 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16653 class-specifier, enum-specifier, or elaborated-type-specifier, then
16654 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16655 if a type is declared; 2 if it is defined. Otherwise, it is set to
16656 zero.
16657
16658 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16659 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16660 is set to FALSE. */
16661
16662 static tree
16663 cp_parser_type_specifier (cp_parser* parser,
16664 cp_parser_flags flags,
16665 cp_decl_specifier_seq *decl_specs,
16666 bool is_declaration,
16667 int* declares_class_or_enum,
16668 bool* is_cv_qualifier)
16669 {
16670 tree type_spec = NULL_TREE;
16671 cp_token *token;
16672 enum rid keyword;
16673 cp_decl_spec ds = ds_last;
16674
16675 /* Assume this type-specifier does not declare a new type. */
16676 if (declares_class_or_enum)
16677 *declares_class_or_enum = 0;
16678 /* And that it does not specify a cv-qualifier. */
16679 if (is_cv_qualifier)
16680 *is_cv_qualifier = false;
16681 /* Peek at the next token. */
16682 token = cp_lexer_peek_token (parser->lexer);
16683
16684 /* If we're looking at a keyword, we can use that to guide the
16685 production we choose. */
16686 keyword = token->keyword;
16687 switch (keyword)
16688 {
16689 case RID_ENUM:
16690 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16691 goto elaborated_type_specifier;
16692
16693 /* Look for the enum-specifier. */
16694 type_spec = cp_parser_enum_specifier (parser);
16695 /* If that worked, we're done. */
16696 if (type_spec)
16697 {
16698 if (declares_class_or_enum)
16699 *declares_class_or_enum = 2;
16700 if (decl_specs)
16701 cp_parser_set_decl_spec_type (decl_specs,
16702 type_spec,
16703 token,
16704 /*type_definition_p=*/true);
16705 return type_spec;
16706 }
16707 else
16708 goto elaborated_type_specifier;
16709
16710 /* Any of these indicate either a class-specifier, or an
16711 elaborated-type-specifier. */
16712 case RID_CLASS:
16713 case RID_STRUCT:
16714 case RID_UNION:
16715 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16716 goto elaborated_type_specifier;
16717
16718 /* Parse tentatively so that we can back up if we don't find a
16719 class-specifier. */
16720 cp_parser_parse_tentatively (parser);
16721 /* Look for the class-specifier. */
16722 type_spec = cp_parser_class_specifier (parser);
16723 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16724 /* If that worked, we're done. */
16725 if (cp_parser_parse_definitely (parser))
16726 {
16727 if (declares_class_or_enum)
16728 *declares_class_or_enum = 2;
16729 if (decl_specs)
16730 cp_parser_set_decl_spec_type (decl_specs,
16731 type_spec,
16732 token,
16733 /*type_definition_p=*/true);
16734 return type_spec;
16735 }
16736
16737 /* Fall through. */
16738 elaborated_type_specifier:
16739 /* We're declaring (not defining) a class or enum. */
16740 if (declares_class_or_enum)
16741 *declares_class_or_enum = 1;
16742
16743 /* Fall through. */
16744 case RID_TYPENAME:
16745 /* Look for an elaborated-type-specifier. */
16746 type_spec
16747 = (cp_parser_elaborated_type_specifier
16748 (parser,
16749 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16750 is_declaration));
16751 if (decl_specs)
16752 cp_parser_set_decl_spec_type (decl_specs,
16753 type_spec,
16754 token,
16755 /*type_definition_p=*/false);
16756 return type_spec;
16757
16758 case RID_CONST:
16759 ds = ds_const;
16760 if (is_cv_qualifier)
16761 *is_cv_qualifier = true;
16762 break;
16763
16764 case RID_VOLATILE:
16765 ds = ds_volatile;
16766 if (is_cv_qualifier)
16767 *is_cv_qualifier = true;
16768 break;
16769
16770 case RID_RESTRICT:
16771 ds = ds_restrict;
16772 if (is_cv_qualifier)
16773 *is_cv_qualifier = true;
16774 break;
16775
16776 case RID_COMPLEX:
16777 /* The `__complex__' keyword is a GNU extension. */
16778 ds = ds_complex;
16779 break;
16780
16781 default:
16782 break;
16783 }
16784
16785 /* Handle simple keywords. */
16786 if (ds != ds_last)
16787 {
16788 if (decl_specs)
16789 {
16790 set_and_check_decl_spec_loc (decl_specs, ds, token);
16791 decl_specs->any_specifiers_p = true;
16792 }
16793 return cp_lexer_consume_token (parser->lexer)->u.value;
16794 }
16795
16796 /* If we do not already have a type-specifier, assume we are looking
16797 at a simple-type-specifier. */
16798 type_spec = cp_parser_simple_type_specifier (parser,
16799 decl_specs,
16800 flags);
16801
16802 /* If we didn't find a type-specifier, and a type-specifier was not
16803 optional in this context, issue an error message. */
16804 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16805 {
16806 cp_parser_error (parser, "expected type specifier");
16807 return error_mark_node;
16808 }
16809
16810 return type_spec;
16811 }
16812
16813 /* Parse a simple-type-specifier.
16814
16815 simple-type-specifier:
16816 :: [opt] nested-name-specifier [opt] type-name
16817 :: [opt] nested-name-specifier template template-id
16818 char
16819 wchar_t
16820 bool
16821 short
16822 int
16823 long
16824 signed
16825 unsigned
16826 float
16827 double
16828 void
16829
16830 C++11 Extension:
16831
16832 simple-type-specifier:
16833 auto
16834 decltype ( expression )
16835 char16_t
16836 char32_t
16837 __underlying_type ( type-id )
16838
16839 C++17 extension:
16840
16841 nested-name-specifier(opt) template-name
16842
16843 GNU Extension:
16844
16845 simple-type-specifier:
16846 __int128
16847 __typeof__ unary-expression
16848 __typeof__ ( type-id )
16849 __typeof__ ( type-id ) { initializer-list , [opt] }
16850
16851 Concepts Extension:
16852
16853 simple-type-specifier:
16854 constrained-type-specifier
16855
16856 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16857 appropriately updated. */
16858
16859 static tree
16860 cp_parser_simple_type_specifier (cp_parser* parser,
16861 cp_decl_specifier_seq *decl_specs,
16862 cp_parser_flags flags)
16863 {
16864 tree type = NULL_TREE;
16865 cp_token *token;
16866 int idx;
16867
16868 /* Peek at the next token. */
16869 token = cp_lexer_peek_token (parser->lexer);
16870
16871 /* If we're looking at a keyword, things are easy. */
16872 switch (token->keyword)
16873 {
16874 case RID_CHAR:
16875 if (decl_specs)
16876 decl_specs->explicit_char_p = true;
16877 type = char_type_node;
16878 break;
16879 case RID_CHAR16:
16880 type = char16_type_node;
16881 break;
16882 case RID_CHAR32:
16883 type = char32_type_node;
16884 break;
16885 case RID_WCHAR:
16886 type = wchar_type_node;
16887 break;
16888 case RID_BOOL:
16889 type = boolean_type_node;
16890 break;
16891 case RID_SHORT:
16892 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16893 type = short_integer_type_node;
16894 break;
16895 case RID_INT:
16896 if (decl_specs)
16897 decl_specs->explicit_int_p = true;
16898 type = integer_type_node;
16899 break;
16900 case RID_INT_N_0:
16901 case RID_INT_N_1:
16902 case RID_INT_N_2:
16903 case RID_INT_N_3:
16904 idx = token->keyword - RID_INT_N_0;
16905 if (! int_n_enabled_p [idx])
16906 break;
16907 if (decl_specs)
16908 {
16909 decl_specs->explicit_intN_p = true;
16910 decl_specs->int_n_idx = idx;
16911 }
16912 type = int_n_trees [idx].signed_type;
16913 break;
16914 case RID_LONG:
16915 if (decl_specs)
16916 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16917 type = long_integer_type_node;
16918 break;
16919 case RID_SIGNED:
16920 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16921 type = integer_type_node;
16922 break;
16923 case RID_UNSIGNED:
16924 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16925 type = unsigned_type_node;
16926 break;
16927 case RID_FLOAT:
16928 type = float_type_node;
16929 break;
16930 case RID_DOUBLE:
16931 type = double_type_node;
16932 break;
16933 case RID_VOID:
16934 type = void_type_node;
16935 break;
16936
16937 case RID_AUTO:
16938 maybe_warn_cpp0x (CPP0X_AUTO);
16939 if (parser->auto_is_implicit_function_template_parm_p)
16940 {
16941 /* The 'auto' might be the placeholder return type for a function decl
16942 with trailing return type. */
16943 bool have_trailing_return_fn_decl = false;
16944
16945 cp_parser_parse_tentatively (parser);
16946 cp_lexer_consume_token (parser->lexer);
16947 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16948 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16949 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16950 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16951 {
16952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16953 {
16954 cp_lexer_consume_token (parser->lexer);
16955 cp_parser_skip_to_closing_parenthesis (parser,
16956 /*recovering*/false,
16957 /*or_comma*/false,
16958 /*consume_paren*/true);
16959 continue;
16960 }
16961
16962 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16963 {
16964 have_trailing_return_fn_decl = true;
16965 break;
16966 }
16967
16968 cp_lexer_consume_token (parser->lexer);
16969 }
16970 cp_parser_abort_tentative_parse (parser);
16971
16972 if (have_trailing_return_fn_decl)
16973 {
16974 type = make_auto ();
16975 break;
16976 }
16977
16978 if (cxx_dialect >= cxx14)
16979 {
16980 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16981 type = TREE_TYPE (type);
16982 }
16983 else
16984 type = error_mark_node;
16985
16986 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16987 {
16988 if (cxx_dialect < cxx14)
16989 error_at (token->location,
16990 "use of %<auto%> in lambda parameter declaration "
16991 "only available with "
16992 "-std=c++14 or -std=gnu++14");
16993 }
16994 else if (cxx_dialect < cxx14)
16995 error_at (token->location,
16996 "use of %<auto%> in parameter declaration "
16997 "only available with "
16998 "-std=c++14 or -std=gnu++14");
16999 else if (!flag_concepts)
17000 pedwarn (token->location, OPT_Wpedantic,
17001 "ISO C++ forbids use of %<auto%> in parameter "
17002 "declaration");
17003 }
17004 else
17005 type = make_auto ();
17006 break;
17007
17008 case RID_DECLTYPE:
17009 /* Since DR 743, decltype can either be a simple-type-specifier by
17010 itself or begin a nested-name-specifier. Parsing it will replace
17011 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17012 handling below decide what to do. */
17013 cp_parser_decltype (parser);
17014 cp_lexer_set_token_position (parser->lexer, token);
17015 break;
17016
17017 case RID_TYPEOF:
17018 /* Consume the `typeof' token. */
17019 cp_lexer_consume_token (parser->lexer);
17020 /* Parse the operand to `typeof'. */
17021 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17022 /* If it is not already a TYPE, take its type. */
17023 if (!TYPE_P (type))
17024 type = finish_typeof (type);
17025
17026 if (decl_specs)
17027 cp_parser_set_decl_spec_type (decl_specs, type,
17028 token,
17029 /*type_definition_p=*/false);
17030
17031 return type;
17032
17033 case RID_UNDERLYING_TYPE:
17034 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17035 if (decl_specs)
17036 cp_parser_set_decl_spec_type (decl_specs, type,
17037 token,
17038 /*type_definition_p=*/false);
17039
17040 return type;
17041
17042 case RID_BASES:
17043 case RID_DIRECT_BASES:
17044 type = cp_parser_trait_expr (parser, token->keyword);
17045 if (decl_specs)
17046 cp_parser_set_decl_spec_type (decl_specs, type,
17047 token,
17048 /*type_definition_p=*/false);
17049 return type;
17050 default:
17051 break;
17052 }
17053
17054 /* If token is an already-parsed decltype not followed by ::,
17055 it's a simple-type-specifier. */
17056 if (token->type == CPP_DECLTYPE
17057 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17058 {
17059 type = saved_checks_value (token->u.tree_check_value);
17060 if (decl_specs)
17061 {
17062 cp_parser_set_decl_spec_type (decl_specs, type,
17063 token,
17064 /*type_definition_p=*/false);
17065 /* Remember that we are handling a decltype in order to
17066 implement the resolution of DR 1510 when the argument
17067 isn't instantiation dependent. */
17068 decl_specs->decltype_p = true;
17069 }
17070 cp_lexer_consume_token (parser->lexer);
17071 return type;
17072 }
17073
17074 /* If the type-specifier was for a built-in type, we're done. */
17075 if (type)
17076 {
17077 /* Record the type. */
17078 if (decl_specs
17079 && (token->keyword != RID_SIGNED
17080 && token->keyword != RID_UNSIGNED
17081 && token->keyword != RID_SHORT
17082 && token->keyword != RID_LONG))
17083 cp_parser_set_decl_spec_type (decl_specs,
17084 type,
17085 token,
17086 /*type_definition_p=*/false);
17087 if (decl_specs)
17088 decl_specs->any_specifiers_p = true;
17089
17090 /* Consume the token. */
17091 cp_lexer_consume_token (parser->lexer);
17092
17093 if (type == error_mark_node)
17094 return error_mark_node;
17095
17096 /* There is no valid C++ program where a non-template type is
17097 followed by a "<". That usually indicates that the user thought
17098 that the type was a template. */
17099 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17100 token->location);
17101
17102 return TYPE_NAME (type);
17103 }
17104
17105 /* The type-specifier must be a user-defined type. */
17106 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17107 {
17108 bool qualified_p;
17109 bool global_p;
17110
17111 /* Don't gobble tokens or issue error messages if this is an
17112 optional type-specifier. */
17113 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17114 cp_parser_parse_tentatively (parser);
17115
17116 token = cp_lexer_peek_token (parser->lexer);
17117
17118 /* Look for the optional `::' operator. */
17119 global_p
17120 = (cp_parser_global_scope_opt (parser,
17121 /*current_scope_valid_p=*/false)
17122 != NULL_TREE);
17123 /* Look for the nested-name specifier. */
17124 qualified_p
17125 = (cp_parser_nested_name_specifier_opt (parser,
17126 /*typename_keyword_p=*/false,
17127 /*check_dependency_p=*/true,
17128 /*type_p=*/false,
17129 /*is_declaration=*/false)
17130 != NULL_TREE);
17131 /* If we have seen a nested-name-specifier, and the next token
17132 is `template', then we are using the template-id production. */
17133 if (parser->scope
17134 && cp_parser_optional_template_keyword (parser))
17135 {
17136 /* Look for the template-id. */
17137 type = cp_parser_template_id (parser,
17138 /*template_keyword_p=*/true,
17139 /*check_dependency_p=*/true,
17140 none_type,
17141 /*is_declaration=*/false);
17142 /* If the template-id did not name a type, we are out of
17143 luck. */
17144 if (TREE_CODE (type) != TYPE_DECL)
17145 {
17146 cp_parser_error (parser, "expected template-id for type");
17147 type = NULL_TREE;
17148 }
17149 }
17150 /* Otherwise, look for a type-name. */
17151 else
17152 type = cp_parser_type_name (parser);
17153 /* Keep track of all name-lookups performed in class scopes. */
17154 if (type
17155 && !global_p
17156 && !qualified_p
17157 && TREE_CODE (type) == TYPE_DECL
17158 && identifier_p (DECL_NAME (type)))
17159 maybe_note_name_used_in_class (DECL_NAME (type), type);
17160 /* If it didn't work out, we don't have a TYPE. */
17161 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17162 && !cp_parser_parse_definitely (parser))
17163 type = NULL_TREE;
17164 if (!type && cxx_dialect >= cxx17)
17165 {
17166 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17167 cp_parser_parse_tentatively (parser);
17168
17169 cp_parser_global_scope_opt (parser,
17170 /*current_scope_valid_p=*/false);
17171 cp_parser_nested_name_specifier_opt (parser,
17172 /*typename_keyword_p=*/false,
17173 /*check_dependency_p=*/true,
17174 /*type_p=*/false,
17175 /*is_declaration=*/false);
17176 tree name = cp_parser_identifier (parser);
17177 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17178 && parser->scope != error_mark_node)
17179 {
17180 tree tmpl = cp_parser_lookup_name (parser, name,
17181 none_type,
17182 /*is_template=*/false,
17183 /*is_namespace=*/false,
17184 /*check_dependency=*/true,
17185 /*ambiguous_decls=*/NULL,
17186 token->location);
17187 if (tmpl && tmpl != error_mark_node
17188 && (DECL_CLASS_TEMPLATE_P (tmpl)
17189 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17190 type = make_template_placeholder (tmpl);
17191 else
17192 {
17193 type = error_mark_node;
17194 if (!cp_parser_simulate_error (parser))
17195 cp_parser_name_lookup_error (parser, name, tmpl,
17196 NLE_TYPE, token->location);
17197 }
17198 }
17199 else
17200 type = error_mark_node;
17201
17202 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17203 && !cp_parser_parse_definitely (parser))
17204 type = NULL_TREE;
17205 }
17206 if (type && decl_specs)
17207 cp_parser_set_decl_spec_type (decl_specs, type,
17208 token,
17209 /*type_definition_p=*/false);
17210 }
17211
17212 /* If we didn't get a type-name, issue an error message. */
17213 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17214 {
17215 cp_parser_error (parser, "expected type-name");
17216 return error_mark_node;
17217 }
17218
17219 if (type && type != error_mark_node)
17220 {
17221 /* See if TYPE is an Objective-C type, and if so, parse and
17222 accept any protocol references following it. Do this before
17223 the cp_parser_check_for_invalid_template_id() call, because
17224 Objective-C types can be followed by '<...>' which would
17225 enclose protocol names rather than template arguments, and so
17226 everything is fine. */
17227 if (c_dialect_objc () && !parser->scope
17228 && (objc_is_id (type) || objc_is_class_name (type)))
17229 {
17230 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17231 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17232
17233 /* Clobber the "unqualified" type previously entered into
17234 DECL_SPECS with the new, improved protocol-qualified version. */
17235 if (decl_specs)
17236 decl_specs->type = qual_type;
17237
17238 return qual_type;
17239 }
17240
17241 /* There is no valid C++ program where a non-template type is
17242 followed by a "<". That usually indicates that the user
17243 thought that the type was a template. */
17244 cp_parser_check_for_invalid_template_id (parser, type,
17245 none_type,
17246 token->location);
17247 }
17248
17249 return type;
17250 }
17251
17252 /* Parse a type-name.
17253
17254 type-name:
17255 class-name
17256 enum-name
17257 typedef-name
17258 simple-template-id [in c++0x]
17259
17260 enum-name:
17261 identifier
17262
17263 typedef-name:
17264 identifier
17265
17266 Concepts:
17267
17268 type-name:
17269 concept-name
17270 partial-concept-id
17271
17272 concept-name:
17273 identifier
17274
17275 Returns a TYPE_DECL for the type. */
17276
17277 static tree
17278 cp_parser_type_name (cp_parser* parser)
17279 {
17280 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17281 }
17282
17283 /* See above. */
17284 static tree
17285 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17286 {
17287 tree type_decl;
17288
17289 /* We can't know yet whether it is a class-name or not. */
17290 cp_parser_parse_tentatively (parser);
17291 /* Try a class-name. */
17292 type_decl = cp_parser_class_name (parser,
17293 typename_keyword_p,
17294 /*template_keyword_p=*/false,
17295 none_type,
17296 /*check_dependency_p=*/true,
17297 /*class_head_p=*/false,
17298 /*is_declaration=*/false);
17299 /* If it's not a class-name, keep looking. */
17300 if (!cp_parser_parse_definitely (parser))
17301 {
17302 if (cxx_dialect < cxx11)
17303 /* It must be a typedef-name or an enum-name. */
17304 return cp_parser_nonclass_name (parser);
17305
17306 cp_parser_parse_tentatively (parser);
17307 /* It is either a simple-template-id representing an
17308 instantiation of an alias template... */
17309 type_decl = cp_parser_template_id (parser,
17310 /*template_keyword_p=*/false,
17311 /*check_dependency_p=*/true,
17312 none_type,
17313 /*is_declaration=*/false);
17314 /* Note that this must be an instantiation of an alias template
17315 because [temp.names]/6 says:
17316
17317 A template-id that names an alias template specialization
17318 is a type-name.
17319
17320 Whereas [temp.names]/7 says:
17321
17322 A simple-template-id that names a class template
17323 specialization is a class-name.
17324
17325 With concepts, this could also be a partial-concept-id that
17326 declares a non-type template parameter. */
17327 if (type_decl != NULL_TREE
17328 && TREE_CODE (type_decl) == TYPE_DECL
17329 && TYPE_DECL_ALIAS_P (type_decl))
17330 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17331 else if (is_constrained_parameter (type_decl))
17332 /* Don't do anything. */ ;
17333 else
17334 cp_parser_simulate_error (parser);
17335
17336 if (!cp_parser_parse_definitely (parser))
17337 /* ... Or a typedef-name or an enum-name. */
17338 return cp_parser_nonclass_name (parser);
17339 }
17340
17341 return type_decl;
17342 }
17343
17344 /* Check if DECL and ARGS can form a constrained-type-specifier.
17345 If ARGS is non-null, we try to form a concept check of the
17346 form DECL<?, ARGS> where ? is a wildcard that matches any
17347 kind of template argument. If ARGS is NULL, then we try to
17348 form a concept check of the form DECL<?>. */
17349
17350 static tree
17351 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17352 tree decl, tree args)
17353 {
17354 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17355
17356 /* If we a constrained-type-specifier cannot be deduced. */
17357 if (parser->prevent_constrained_type_specifiers)
17358 return NULL_TREE;
17359
17360 /* A constrained type specifier can only be found in an
17361 overload set or as a reference to a template declaration.
17362
17363 FIXME: This might be masking a bug. It's possible that
17364 that the deduction below is causing template specializations
17365 to be formed with the wildcard as an argument. */
17366 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17367 return NULL_TREE;
17368
17369 /* Try to build a call expression that evaluates the
17370 concept. This can fail if the overload set refers
17371 only to non-templates. */
17372 tree placeholder = build_nt (WILDCARD_DECL);
17373 tree check = build_concept_check (decl, placeholder, args);
17374 if (check == error_mark_node)
17375 return NULL_TREE;
17376
17377 /* Deduce the checked constraint and the prototype parameter.
17378
17379 FIXME: In certain cases, failure to deduce should be a
17380 diagnosable error. */
17381 tree conc;
17382 tree proto;
17383 if (!deduce_constrained_parameter (check, conc, proto))
17384 return NULL_TREE;
17385
17386 /* In template parameter scope, this results in a constrained
17387 parameter. Return a descriptor of that parm. */
17388 if (processing_template_parmlist)
17389 return build_constrained_parameter (conc, proto, args);
17390
17391 /* In a parameter-declaration-clause, constrained-type
17392 specifiers result in invented template parameters. */
17393 if (parser->auto_is_implicit_function_template_parm_p)
17394 {
17395 tree x = build_constrained_parameter (conc, proto, args);
17396 return synthesize_implicit_template_parm (parser, x);
17397 }
17398 else
17399 {
17400 /* Otherwise, we're in a context where the constrained
17401 type name is deduced and the constraint applies
17402 after deduction. */
17403 return make_constrained_auto (conc, args);
17404 }
17405
17406 return NULL_TREE;
17407 }
17408
17409 /* If DECL refers to a concept, return a TYPE_DECL representing
17410 the result of using the constrained type specifier in the
17411 current context. DECL refers to a concept if
17412
17413 - it is an overload set containing a function concept taking a single
17414 type argument, or
17415
17416 - it is a variable concept taking a single type argument. */
17417
17418 static tree
17419 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17420 {
17421 if (flag_concepts
17422 && (TREE_CODE (decl) == OVERLOAD
17423 || BASELINK_P (decl)
17424 || variable_concept_p (decl)))
17425 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17426 else
17427 return NULL_TREE;
17428 }
17429
17430 /* Check if DECL and ARGS form a partial-concept-id. If so,
17431 assign ID to the resulting constrained placeholder.
17432
17433 Returns true if the partial-concept-id designates a placeholder
17434 and false otherwise. Note that *id is set to NULL_TREE in
17435 this case. */
17436
17437 static tree
17438 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17439 {
17440 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17441 }
17442
17443 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17444 or a concept-name.
17445
17446 enum-name:
17447 identifier
17448
17449 typedef-name:
17450 identifier
17451
17452 concept-name:
17453 identifier
17454
17455 Returns a TYPE_DECL for the type. */
17456
17457 static tree
17458 cp_parser_nonclass_name (cp_parser* parser)
17459 {
17460 tree type_decl;
17461 tree identifier;
17462
17463 cp_token *token = cp_lexer_peek_token (parser->lexer);
17464 identifier = cp_parser_identifier (parser);
17465 if (identifier == error_mark_node)
17466 return error_mark_node;
17467
17468 /* Look up the type-name. */
17469 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17470
17471 type_decl = strip_using_decl (type_decl);
17472
17473 /* If we found an overload set, then it may refer to a concept-name. */
17474 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17475 type_decl = decl;
17476
17477 if (TREE_CODE (type_decl) != TYPE_DECL
17478 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17479 {
17480 /* See if this is an Objective-C type. */
17481 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17482 tree type = objc_get_protocol_qualified_type (identifier, protos);
17483 if (type)
17484 type_decl = TYPE_NAME (type);
17485 }
17486
17487 /* Issue an error if we did not find a type-name. */
17488 if (TREE_CODE (type_decl) != TYPE_DECL
17489 /* In Objective-C, we have the complication that class names are
17490 normally type names and start declarations (eg, the
17491 "NSObject" in "NSObject *object;"), but can be used in an
17492 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17493 is an expression. So, a classname followed by a dot is not a
17494 valid type-name. */
17495 || (objc_is_class_name (TREE_TYPE (type_decl))
17496 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17497 {
17498 if (!cp_parser_simulate_error (parser))
17499 cp_parser_name_lookup_error (parser, identifier, type_decl,
17500 NLE_TYPE, token->location);
17501 return error_mark_node;
17502 }
17503 /* Remember that the name was used in the definition of the
17504 current class so that we can check later to see if the
17505 meaning would have been different after the class was
17506 entirely defined. */
17507 else if (type_decl != error_mark_node
17508 && !parser->scope)
17509 maybe_note_name_used_in_class (identifier, type_decl);
17510
17511 return type_decl;
17512 }
17513
17514 /* Parse an elaborated-type-specifier. Note that the grammar given
17515 here incorporates the resolution to DR68.
17516
17517 elaborated-type-specifier:
17518 class-key :: [opt] nested-name-specifier [opt] identifier
17519 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17520 enum-key :: [opt] nested-name-specifier [opt] identifier
17521 typename :: [opt] nested-name-specifier identifier
17522 typename :: [opt] nested-name-specifier template [opt]
17523 template-id
17524
17525 GNU extension:
17526
17527 elaborated-type-specifier:
17528 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17529 class-key attributes :: [opt] nested-name-specifier [opt]
17530 template [opt] template-id
17531 enum attributes :: [opt] nested-name-specifier [opt] identifier
17532
17533 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17534 declared `friend'. If IS_DECLARATION is TRUE, then this
17535 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17536 something is being declared.
17537
17538 Returns the TYPE specified. */
17539
17540 static tree
17541 cp_parser_elaborated_type_specifier (cp_parser* parser,
17542 bool is_friend,
17543 bool is_declaration)
17544 {
17545 enum tag_types tag_type;
17546 tree identifier;
17547 tree type = NULL_TREE;
17548 tree attributes = NULL_TREE;
17549 tree globalscope;
17550 cp_token *token = NULL;
17551
17552 /* See if we're looking at the `enum' keyword. */
17553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17554 {
17555 /* Consume the `enum' token. */
17556 cp_lexer_consume_token (parser->lexer);
17557 /* Remember that it's an enumeration type. */
17558 tag_type = enum_type;
17559 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17560 enums) is used here. */
17561 cp_token *token = cp_lexer_peek_token (parser->lexer);
17562 if (cp_parser_is_keyword (token, RID_CLASS)
17563 || cp_parser_is_keyword (token, RID_STRUCT))
17564 {
17565 gcc_rich_location richloc (token->location);
17566 richloc.add_range (input_location, false);
17567 richloc.add_fixit_remove ();
17568 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17569 "a scoped enum must not use the %qD keyword",
17570 token->u.value);
17571 /* Consume the `struct' or `class' and parse it anyway. */
17572 cp_lexer_consume_token (parser->lexer);
17573 }
17574 /* Parse the attributes. */
17575 attributes = cp_parser_attributes_opt (parser);
17576 }
17577 /* Or, it might be `typename'. */
17578 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17579 RID_TYPENAME))
17580 {
17581 /* Consume the `typename' token. */
17582 cp_lexer_consume_token (parser->lexer);
17583 /* Remember that it's a `typename' type. */
17584 tag_type = typename_type;
17585 }
17586 /* Otherwise it must be a class-key. */
17587 else
17588 {
17589 tag_type = cp_parser_class_key (parser);
17590 if (tag_type == none_type)
17591 return error_mark_node;
17592 /* Parse the attributes. */
17593 attributes = cp_parser_attributes_opt (parser);
17594 }
17595
17596 /* Look for the `::' operator. */
17597 globalscope = cp_parser_global_scope_opt (parser,
17598 /*current_scope_valid_p=*/false);
17599 /* Look for the nested-name-specifier. */
17600 tree nested_name_specifier;
17601 if (tag_type == typename_type && !globalscope)
17602 {
17603 nested_name_specifier
17604 = cp_parser_nested_name_specifier (parser,
17605 /*typename_keyword_p=*/true,
17606 /*check_dependency_p=*/true,
17607 /*type_p=*/true,
17608 is_declaration);
17609 if (!nested_name_specifier)
17610 return error_mark_node;
17611 }
17612 else
17613 /* Even though `typename' is not present, the proposed resolution
17614 to Core Issue 180 says that in `class A<T>::B', `B' should be
17615 considered a type-name, even if `A<T>' is dependent. */
17616 nested_name_specifier
17617 = cp_parser_nested_name_specifier_opt (parser,
17618 /*typename_keyword_p=*/true,
17619 /*check_dependency_p=*/true,
17620 /*type_p=*/true,
17621 is_declaration);
17622 /* For everything but enumeration types, consider a template-id.
17623 For an enumeration type, consider only a plain identifier. */
17624 if (tag_type != enum_type)
17625 {
17626 bool template_p = false;
17627 tree decl;
17628
17629 /* Allow the `template' keyword. */
17630 template_p = cp_parser_optional_template_keyword (parser);
17631 /* If we didn't see `template', we don't know if there's a
17632 template-id or not. */
17633 if (!template_p)
17634 cp_parser_parse_tentatively (parser);
17635 /* Parse the template-id. */
17636 token = cp_lexer_peek_token (parser->lexer);
17637 decl = cp_parser_template_id (parser, template_p,
17638 /*check_dependency_p=*/true,
17639 tag_type,
17640 is_declaration);
17641 /* If we didn't find a template-id, look for an ordinary
17642 identifier. */
17643 if (!template_p && !cp_parser_parse_definitely (parser))
17644 ;
17645 /* We can get here when cp_parser_template_id, called by
17646 cp_parser_class_name with tag_type == none_type, succeeds
17647 and caches a BASELINK. Then, when called again here,
17648 instead of failing and returning an error_mark_node
17649 returns it (see template/typename17.C in C++11).
17650 ??? Could we diagnose this earlier? */
17651 else if (tag_type == typename_type && BASELINK_P (decl))
17652 {
17653 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17654 type = error_mark_node;
17655 }
17656 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17657 in effect, then we must assume that, upon instantiation, the
17658 template will correspond to a class. */
17659 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17660 && tag_type == typename_type)
17661 type = make_typename_type (parser->scope, decl,
17662 typename_type,
17663 /*complain=*/tf_error);
17664 /* If the `typename' keyword is in effect and DECL is not a type
17665 decl, then type is non existent. */
17666 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17667 ;
17668 else if (TREE_CODE (decl) == TYPE_DECL)
17669 {
17670 type = check_elaborated_type_specifier (tag_type, decl,
17671 /*allow_template_p=*/true);
17672
17673 /* If the next token is a semicolon, this must be a specialization,
17674 instantiation, or friend declaration. Check the scope while we
17675 still know whether or not we had a nested-name-specifier. */
17676 if (type != error_mark_node
17677 && !nested_name_specifier && !is_friend
17678 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17679 check_unqualified_spec_or_inst (type, token->location);
17680 }
17681 else if (decl == error_mark_node)
17682 type = error_mark_node;
17683 }
17684
17685 if (!type)
17686 {
17687 token = cp_lexer_peek_token (parser->lexer);
17688 identifier = cp_parser_identifier (parser);
17689
17690 if (identifier == error_mark_node)
17691 {
17692 parser->scope = NULL_TREE;
17693 return error_mark_node;
17694 }
17695
17696 /* For a `typename', we needn't call xref_tag. */
17697 if (tag_type == typename_type
17698 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17699 return cp_parser_make_typename_type (parser, identifier,
17700 token->location);
17701
17702 /* Template parameter lists apply only if we are not within a
17703 function parameter list. */
17704 bool template_parm_lists_apply
17705 = parser->num_template_parameter_lists;
17706 if (template_parm_lists_apply)
17707 for (cp_binding_level *s = current_binding_level;
17708 s && s->kind != sk_template_parms;
17709 s = s->level_chain)
17710 if (s->kind == sk_function_parms)
17711 template_parm_lists_apply = false;
17712
17713 /* Look up a qualified name in the usual way. */
17714 if (parser->scope)
17715 {
17716 tree decl;
17717 tree ambiguous_decls;
17718
17719 decl = cp_parser_lookup_name (parser, identifier,
17720 tag_type,
17721 /*is_template=*/false,
17722 /*is_namespace=*/false,
17723 /*check_dependency=*/true,
17724 &ambiguous_decls,
17725 token->location);
17726
17727 /* If the lookup was ambiguous, an error will already have been
17728 issued. */
17729 if (ambiguous_decls)
17730 return error_mark_node;
17731
17732 /* If we are parsing friend declaration, DECL may be a
17733 TEMPLATE_DECL tree node here. However, we need to check
17734 whether this TEMPLATE_DECL results in valid code. Consider
17735 the following example:
17736
17737 namespace N {
17738 template <class T> class C {};
17739 }
17740 class X {
17741 template <class T> friend class N::C; // #1, valid code
17742 };
17743 template <class T> class Y {
17744 friend class N::C; // #2, invalid code
17745 };
17746
17747 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17748 name lookup of `N::C'. We see that friend declaration must
17749 be template for the code to be valid. Note that
17750 processing_template_decl does not work here since it is
17751 always 1 for the above two cases. */
17752
17753 decl = (cp_parser_maybe_treat_template_as_class
17754 (decl, /*tag_name_p=*/is_friend
17755 && template_parm_lists_apply));
17756
17757 if (TREE_CODE (decl) != TYPE_DECL)
17758 {
17759 cp_parser_diagnose_invalid_type_name (parser,
17760 identifier,
17761 token->location);
17762 return error_mark_node;
17763 }
17764
17765 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17766 {
17767 bool allow_template = (template_parm_lists_apply
17768 || DECL_SELF_REFERENCE_P (decl));
17769 type = check_elaborated_type_specifier (tag_type, decl,
17770 allow_template);
17771
17772 if (type == error_mark_node)
17773 return error_mark_node;
17774 }
17775
17776 /* Forward declarations of nested types, such as
17777
17778 class C1::C2;
17779 class C1::C2::C3;
17780
17781 are invalid unless all components preceding the final '::'
17782 are complete. If all enclosing types are complete, these
17783 declarations become merely pointless.
17784
17785 Invalid forward declarations of nested types are errors
17786 caught elsewhere in parsing. Those that are pointless arrive
17787 here. */
17788
17789 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17790 && !is_friend && !processing_explicit_instantiation)
17791 warning (0, "declaration %qD does not declare anything", decl);
17792
17793 type = TREE_TYPE (decl);
17794 }
17795 else
17796 {
17797 /* An elaborated-type-specifier sometimes introduces a new type and
17798 sometimes names an existing type. Normally, the rule is that it
17799 introduces a new type only if there is not an existing type of
17800 the same name already in scope. For example, given:
17801
17802 struct S {};
17803 void f() { struct S s; }
17804
17805 the `struct S' in the body of `f' is the same `struct S' as in
17806 the global scope; the existing definition is used. However, if
17807 there were no global declaration, this would introduce a new
17808 local class named `S'.
17809
17810 An exception to this rule applies to the following code:
17811
17812 namespace N { struct S; }
17813
17814 Here, the elaborated-type-specifier names a new type
17815 unconditionally; even if there is already an `S' in the
17816 containing scope this declaration names a new type.
17817 This exception only applies if the elaborated-type-specifier
17818 forms the complete declaration:
17819
17820 [class.name]
17821
17822 A declaration consisting solely of `class-key identifier ;' is
17823 either a redeclaration of the name in the current scope or a
17824 forward declaration of the identifier as a class name. It
17825 introduces the name into the current scope.
17826
17827 We are in this situation precisely when the next token is a `;'.
17828
17829 An exception to the exception is that a `friend' declaration does
17830 *not* name a new type; i.e., given:
17831
17832 struct S { friend struct T; };
17833
17834 `T' is not a new type in the scope of `S'.
17835
17836 Also, `new struct S' or `sizeof (struct S)' never results in the
17837 definition of a new type; a new type can only be declared in a
17838 declaration context. */
17839
17840 tag_scope ts;
17841 bool template_p;
17842
17843 if (is_friend)
17844 /* Friends have special name lookup rules. */
17845 ts = ts_within_enclosing_non_class;
17846 else if (is_declaration
17847 && cp_lexer_next_token_is (parser->lexer,
17848 CPP_SEMICOLON))
17849 /* This is a `class-key identifier ;' */
17850 ts = ts_current;
17851 else
17852 ts = ts_global;
17853
17854 template_p =
17855 (template_parm_lists_apply
17856 && (cp_parser_next_token_starts_class_definition_p (parser)
17857 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17858 /* An unqualified name was used to reference this type, so
17859 there were no qualifying templates. */
17860 if (template_parm_lists_apply
17861 && !cp_parser_check_template_parameters (parser,
17862 /*num_templates=*/0,
17863 token->location,
17864 /*declarator=*/NULL))
17865 return error_mark_node;
17866 type = xref_tag (tag_type, identifier, ts, template_p);
17867 }
17868 }
17869
17870 if (type == error_mark_node)
17871 return error_mark_node;
17872
17873 /* Allow attributes on forward declarations of classes. */
17874 if (attributes)
17875 {
17876 if (TREE_CODE (type) == TYPENAME_TYPE)
17877 warning (OPT_Wattributes,
17878 "attributes ignored on uninstantiated type");
17879 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17880 && ! processing_explicit_instantiation)
17881 warning (OPT_Wattributes,
17882 "attributes ignored on template instantiation");
17883 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17884 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17885 else
17886 warning (OPT_Wattributes,
17887 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17888 }
17889
17890 if (tag_type != enum_type)
17891 {
17892 /* Indicate whether this class was declared as a `class' or as a
17893 `struct'. */
17894 if (CLASS_TYPE_P (type))
17895 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17896 cp_parser_check_class_key (tag_type, type);
17897 }
17898
17899 /* A "<" cannot follow an elaborated type specifier. If that
17900 happens, the user was probably trying to form a template-id. */
17901 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17902 token->location);
17903
17904 return type;
17905 }
17906
17907 /* Parse an enum-specifier.
17908
17909 enum-specifier:
17910 enum-head { enumerator-list [opt] }
17911 enum-head { enumerator-list , } [C++0x]
17912
17913 enum-head:
17914 enum-key identifier [opt] enum-base [opt]
17915 enum-key nested-name-specifier identifier enum-base [opt]
17916
17917 enum-key:
17918 enum
17919 enum class [C++0x]
17920 enum struct [C++0x]
17921
17922 enum-base: [C++0x]
17923 : type-specifier-seq
17924
17925 opaque-enum-specifier:
17926 enum-key identifier enum-base [opt] ;
17927
17928 GNU Extensions:
17929 enum-key attributes[opt] identifier [opt] enum-base [opt]
17930 { enumerator-list [opt] }attributes[opt]
17931 enum-key attributes[opt] identifier [opt] enum-base [opt]
17932 { enumerator-list, }attributes[opt] [C++0x]
17933
17934 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17935 if the token stream isn't an enum-specifier after all. */
17936
17937 static tree
17938 cp_parser_enum_specifier (cp_parser* parser)
17939 {
17940 tree identifier;
17941 tree type = NULL_TREE;
17942 tree prev_scope;
17943 tree nested_name_specifier = NULL_TREE;
17944 tree attributes;
17945 bool scoped_enum_p = false;
17946 bool has_underlying_type = false;
17947 bool nested_being_defined = false;
17948 bool new_value_list = false;
17949 bool is_new_type = false;
17950 bool is_unnamed = false;
17951 tree underlying_type = NULL_TREE;
17952 cp_token *type_start_token = NULL;
17953 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17954
17955 parser->colon_corrects_to_scope_p = false;
17956
17957 /* Parse tentatively so that we can back up if we don't find a
17958 enum-specifier. */
17959 cp_parser_parse_tentatively (parser);
17960
17961 /* Caller guarantees that the current token is 'enum', an identifier
17962 possibly follows, and the token after that is an opening brace.
17963 If we don't have an identifier, fabricate an anonymous name for
17964 the enumeration being defined. */
17965 cp_lexer_consume_token (parser->lexer);
17966
17967 /* Parse the "class" or "struct", which indicates a scoped
17968 enumeration type in C++0x. */
17969 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17970 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17971 {
17972 if (cxx_dialect < cxx11)
17973 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17974
17975 /* Consume the `struct' or `class' token. */
17976 cp_lexer_consume_token (parser->lexer);
17977
17978 scoped_enum_p = true;
17979 }
17980
17981 attributes = cp_parser_attributes_opt (parser);
17982
17983 /* Clear the qualification. */
17984 parser->scope = NULL_TREE;
17985 parser->qualifying_scope = NULL_TREE;
17986 parser->object_scope = NULL_TREE;
17987
17988 /* Figure out in what scope the declaration is being placed. */
17989 prev_scope = current_scope ();
17990
17991 type_start_token = cp_lexer_peek_token (parser->lexer);
17992
17993 push_deferring_access_checks (dk_no_check);
17994 nested_name_specifier
17995 = cp_parser_nested_name_specifier_opt (parser,
17996 /*typename_keyword_p=*/true,
17997 /*check_dependency_p=*/false,
17998 /*type_p=*/false,
17999 /*is_declaration=*/false);
18000
18001 if (nested_name_specifier)
18002 {
18003 tree name;
18004
18005 identifier = cp_parser_identifier (parser);
18006 name = cp_parser_lookup_name (parser, identifier,
18007 enum_type,
18008 /*is_template=*/false,
18009 /*is_namespace=*/false,
18010 /*check_dependency=*/true,
18011 /*ambiguous_decls=*/NULL,
18012 input_location);
18013 if (name && name != error_mark_node)
18014 {
18015 type = TREE_TYPE (name);
18016 if (TREE_CODE (type) == TYPENAME_TYPE)
18017 {
18018 /* Are template enums allowed in ISO? */
18019 if (template_parm_scope_p ())
18020 pedwarn (type_start_token->location, OPT_Wpedantic,
18021 "%qD is an enumeration template", name);
18022 /* ignore a typename reference, for it will be solved by name
18023 in start_enum. */
18024 type = NULL_TREE;
18025 }
18026 }
18027 else if (nested_name_specifier == error_mark_node)
18028 /* We already issued an error. */;
18029 else
18030 {
18031 error_at (type_start_token->location,
18032 "%qD does not name an enumeration in %qT",
18033 identifier, nested_name_specifier);
18034 nested_name_specifier = error_mark_node;
18035 }
18036 }
18037 else
18038 {
18039 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18040 identifier = cp_parser_identifier (parser);
18041 else
18042 {
18043 identifier = make_anon_name ();
18044 is_unnamed = true;
18045 if (scoped_enum_p)
18046 error_at (type_start_token->location,
18047 "unnamed scoped enum is not allowed");
18048 }
18049 }
18050 pop_deferring_access_checks ();
18051
18052 /* Check for the `:' that denotes a specified underlying type in C++0x.
18053 Note that a ':' could also indicate a bitfield width, however. */
18054 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18055 {
18056 cp_decl_specifier_seq type_specifiers;
18057
18058 /* Consume the `:'. */
18059 cp_lexer_consume_token (parser->lexer);
18060
18061 /* Parse the type-specifier-seq. */
18062 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18063 /*is_trailing_return=*/false,
18064 &type_specifiers);
18065
18066 /* At this point this is surely not elaborated type specifier. */
18067 if (!cp_parser_parse_definitely (parser))
18068 return NULL_TREE;
18069
18070 if (cxx_dialect < cxx11)
18071 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18072
18073 has_underlying_type = true;
18074
18075 /* If that didn't work, stop. */
18076 if (type_specifiers.type != error_mark_node)
18077 {
18078 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18079 /*initialized=*/0, NULL);
18080 if (underlying_type == error_mark_node
18081 || check_for_bare_parameter_packs (underlying_type))
18082 underlying_type = NULL_TREE;
18083 }
18084 }
18085
18086 /* Look for the `{' but don't consume it yet. */
18087 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18088 {
18089 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18090 {
18091 cp_parser_error (parser, "expected %<{%>");
18092 if (has_underlying_type)
18093 {
18094 type = NULL_TREE;
18095 goto out;
18096 }
18097 }
18098 /* An opaque-enum-specifier must have a ';' here. */
18099 if ((scoped_enum_p || underlying_type)
18100 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18101 {
18102 cp_parser_error (parser, "expected %<;%> or %<{%>");
18103 if (has_underlying_type)
18104 {
18105 type = NULL_TREE;
18106 goto out;
18107 }
18108 }
18109 }
18110
18111 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18112 return NULL_TREE;
18113
18114 if (nested_name_specifier)
18115 {
18116 if (CLASS_TYPE_P (nested_name_specifier))
18117 {
18118 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18119 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18120 push_scope (nested_name_specifier);
18121 }
18122 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18123 {
18124 push_nested_namespace (nested_name_specifier);
18125 }
18126 }
18127
18128 /* Issue an error message if type-definitions are forbidden here. */
18129 if (!cp_parser_check_type_definition (parser))
18130 type = error_mark_node;
18131 else
18132 /* Create the new type. We do this before consuming the opening
18133 brace so the enum will be recorded as being on the line of its
18134 tag (or the 'enum' keyword, if there is no tag). */
18135 type = start_enum (identifier, type, underlying_type,
18136 attributes, scoped_enum_p, &is_new_type);
18137
18138 /* If the next token is not '{' it is an opaque-enum-specifier or an
18139 elaborated-type-specifier. */
18140 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18141 {
18142 timevar_push (TV_PARSE_ENUM);
18143 if (nested_name_specifier
18144 && nested_name_specifier != error_mark_node)
18145 {
18146 /* The following catches invalid code such as:
18147 enum class S<int>::E { A, B, C }; */
18148 if (!processing_specialization
18149 && CLASS_TYPE_P (nested_name_specifier)
18150 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18151 error_at (type_start_token->location, "cannot add an enumerator "
18152 "list to a template instantiation");
18153
18154 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18155 {
18156 error_at (type_start_token->location,
18157 "%<%T::%E%> has not been declared",
18158 TYPE_CONTEXT (nested_name_specifier),
18159 nested_name_specifier);
18160 type = error_mark_node;
18161 }
18162 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18163 && !CLASS_TYPE_P (nested_name_specifier))
18164 {
18165 error_at (type_start_token->location, "nested name specifier "
18166 "%qT for enum declaration does not name a class "
18167 "or namespace", nested_name_specifier);
18168 type = error_mark_node;
18169 }
18170 /* If that scope does not contain the scope in which the
18171 class was originally declared, the program is invalid. */
18172 else if (prev_scope && !is_ancestor (prev_scope,
18173 nested_name_specifier))
18174 {
18175 if (at_namespace_scope_p ())
18176 error_at (type_start_token->location,
18177 "declaration of %qD in namespace %qD which does not "
18178 "enclose %qD",
18179 type, prev_scope, nested_name_specifier);
18180 else
18181 error_at (type_start_token->location,
18182 "declaration of %qD in %qD which does not "
18183 "enclose %qD",
18184 type, prev_scope, nested_name_specifier);
18185 type = error_mark_node;
18186 }
18187 /* If that scope is the scope where the declaration is being placed
18188 the program is invalid. */
18189 else if (CLASS_TYPE_P (nested_name_specifier)
18190 && CLASS_TYPE_P (prev_scope)
18191 && same_type_p (nested_name_specifier, prev_scope))
18192 {
18193 permerror (type_start_token->location,
18194 "extra qualification not allowed");
18195 nested_name_specifier = NULL_TREE;
18196 }
18197 }
18198
18199 if (scoped_enum_p)
18200 begin_scope (sk_scoped_enum, type);
18201
18202 /* Consume the opening brace. */
18203 matching_braces braces;
18204 braces.consume_open (parser);
18205
18206 if (type == error_mark_node)
18207 ; /* Nothing to add */
18208 else if (OPAQUE_ENUM_P (type)
18209 || (cxx_dialect > cxx98 && processing_specialization))
18210 {
18211 new_value_list = true;
18212 SET_OPAQUE_ENUM_P (type, false);
18213 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18214 }
18215 else
18216 {
18217 error_at (type_start_token->location,
18218 "multiple definition of %q#T", type);
18219 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18220 "previous definition here");
18221 type = error_mark_node;
18222 }
18223
18224 if (type == error_mark_node)
18225 cp_parser_skip_to_end_of_block_or_statement (parser);
18226 /* If the next token is not '}', then there are some enumerators. */
18227 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18228 {
18229 if (is_unnamed && !scoped_enum_p)
18230 pedwarn (type_start_token->location, OPT_Wpedantic,
18231 "ISO C++ forbids empty unnamed enum");
18232 }
18233 else
18234 cp_parser_enumerator_list (parser, type);
18235
18236 /* Consume the final '}'. */
18237 braces.require_close (parser);
18238
18239 if (scoped_enum_p)
18240 finish_scope ();
18241 timevar_pop (TV_PARSE_ENUM);
18242 }
18243 else
18244 {
18245 /* If a ';' follows, then it is an opaque-enum-specifier
18246 and additional restrictions apply. */
18247 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18248 {
18249 if (is_unnamed)
18250 error_at (type_start_token->location,
18251 "opaque-enum-specifier without name");
18252 else if (nested_name_specifier)
18253 error_at (type_start_token->location,
18254 "opaque-enum-specifier must use a simple identifier");
18255 }
18256 }
18257
18258 /* Look for trailing attributes to apply to this enumeration, and
18259 apply them if appropriate. */
18260 if (cp_parser_allow_gnu_extensions_p (parser))
18261 {
18262 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18263 cplus_decl_attributes (&type,
18264 trailing_attr,
18265 (int) ATTR_FLAG_TYPE_IN_PLACE);
18266 }
18267
18268 /* Finish up the enumeration. */
18269 if (type != error_mark_node)
18270 {
18271 if (new_value_list)
18272 finish_enum_value_list (type);
18273 if (is_new_type)
18274 finish_enum (type);
18275 }
18276
18277 if (nested_name_specifier)
18278 {
18279 if (CLASS_TYPE_P (nested_name_specifier))
18280 {
18281 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18282 pop_scope (nested_name_specifier);
18283 }
18284 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18285 {
18286 pop_nested_namespace (nested_name_specifier);
18287 }
18288 }
18289 out:
18290 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18291 return type;
18292 }
18293
18294 /* Parse an enumerator-list. The enumerators all have the indicated
18295 TYPE.
18296
18297 enumerator-list:
18298 enumerator-definition
18299 enumerator-list , enumerator-definition */
18300
18301 static void
18302 cp_parser_enumerator_list (cp_parser* parser, tree type)
18303 {
18304 while (true)
18305 {
18306 /* Parse an enumerator-definition. */
18307 cp_parser_enumerator_definition (parser, type);
18308
18309 /* If the next token is not a ',', we've reached the end of
18310 the list. */
18311 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18312 break;
18313 /* Otherwise, consume the `,' and keep going. */
18314 cp_lexer_consume_token (parser->lexer);
18315 /* If the next token is a `}', there is a trailing comma. */
18316 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18317 {
18318 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18319 pedwarn (input_location, OPT_Wpedantic,
18320 "comma at end of enumerator list");
18321 break;
18322 }
18323 }
18324 }
18325
18326 /* Parse an enumerator-definition. The enumerator has the indicated
18327 TYPE.
18328
18329 enumerator-definition:
18330 enumerator
18331 enumerator = constant-expression
18332
18333 enumerator:
18334 identifier
18335
18336 GNU Extensions:
18337
18338 enumerator-definition:
18339 enumerator attributes [opt]
18340 enumerator attributes [opt] = constant-expression */
18341
18342 static void
18343 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18344 {
18345 tree identifier;
18346 tree value;
18347 location_t loc;
18348
18349 /* Save the input location because we are interested in the location
18350 of the identifier and not the location of the explicit value. */
18351 loc = cp_lexer_peek_token (parser->lexer)->location;
18352
18353 /* Look for the identifier. */
18354 identifier = cp_parser_identifier (parser);
18355 if (identifier == error_mark_node)
18356 return;
18357
18358 /* Parse any specified attributes. */
18359 tree attrs = cp_parser_attributes_opt (parser);
18360
18361 /* If the next token is an '=', then there is an explicit value. */
18362 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18363 {
18364 /* Consume the `=' token. */
18365 cp_lexer_consume_token (parser->lexer);
18366 /* Parse the value. */
18367 value = cp_parser_constant_expression (parser);
18368 }
18369 else
18370 value = NULL_TREE;
18371
18372 /* If we are processing a template, make sure the initializer of the
18373 enumerator doesn't contain any bare template parameter pack. */
18374 if (check_for_bare_parameter_packs (value))
18375 value = error_mark_node;
18376
18377 /* Create the enumerator. */
18378 build_enumerator (identifier, value, type, attrs, loc);
18379 }
18380
18381 /* Parse a namespace-name.
18382
18383 namespace-name:
18384 original-namespace-name
18385 namespace-alias
18386
18387 Returns the NAMESPACE_DECL for the namespace. */
18388
18389 static tree
18390 cp_parser_namespace_name (cp_parser* parser)
18391 {
18392 tree identifier;
18393 tree namespace_decl;
18394
18395 cp_token *token = cp_lexer_peek_token (parser->lexer);
18396
18397 /* Get the name of the namespace. */
18398 identifier = cp_parser_identifier (parser);
18399 if (identifier == error_mark_node)
18400 return error_mark_node;
18401
18402 /* Look up the identifier in the currently active scope. Look only
18403 for namespaces, due to:
18404
18405 [basic.lookup.udir]
18406
18407 When looking up a namespace-name in a using-directive or alias
18408 definition, only namespace names are considered.
18409
18410 And:
18411
18412 [basic.lookup.qual]
18413
18414 During the lookup of a name preceding the :: scope resolution
18415 operator, object, function, and enumerator names are ignored.
18416
18417 (Note that cp_parser_qualifying_entity only calls this
18418 function if the token after the name is the scope resolution
18419 operator.) */
18420 namespace_decl = cp_parser_lookup_name (parser, identifier,
18421 none_type,
18422 /*is_template=*/false,
18423 /*is_namespace=*/true,
18424 /*check_dependency=*/true,
18425 /*ambiguous_decls=*/NULL,
18426 token->location);
18427 /* If it's not a namespace, issue an error. */
18428 if (namespace_decl == error_mark_node
18429 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18430 {
18431 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18432 {
18433 error_at (token->location, "%qD is not a namespace-name", identifier);
18434 if (namespace_decl == error_mark_node
18435 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18436 suggest_alternative_in_explicit_scope (token->location, identifier,
18437 parser->scope);
18438 }
18439 cp_parser_error (parser, "expected namespace-name");
18440 namespace_decl = error_mark_node;
18441 }
18442
18443 return namespace_decl;
18444 }
18445
18446 /* Parse a namespace-definition.
18447
18448 namespace-definition:
18449 named-namespace-definition
18450 unnamed-namespace-definition
18451
18452 named-namespace-definition:
18453 original-namespace-definition
18454 extension-namespace-definition
18455
18456 original-namespace-definition:
18457 namespace identifier { namespace-body }
18458
18459 extension-namespace-definition:
18460 namespace original-namespace-name { namespace-body }
18461
18462 unnamed-namespace-definition:
18463 namespace { namespace-body } */
18464
18465 static void
18466 cp_parser_namespace_definition (cp_parser* parser)
18467 {
18468 tree identifier;
18469 int nested_definition_count = 0;
18470
18471 cp_ensure_no_omp_declare_simd (parser);
18472 cp_ensure_no_oacc_routine (parser);
18473
18474 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18475
18476 if (is_inline)
18477 {
18478 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18479 cp_lexer_consume_token (parser->lexer);
18480 }
18481
18482 /* Look for the `namespace' keyword. */
18483 cp_token* token
18484 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18485
18486 /* Parse any specified attributes before the identifier. */
18487 tree attribs = cp_parser_attributes_opt (parser);
18488
18489 for (;;)
18490 {
18491 identifier = NULL_TREE;
18492
18493 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18494 {
18495 identifier = cp_parser_identifier (parser);
18496
18497 /* Parse any attributes specified after the identifier. */
18498 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18499 }
18500
18501 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18502 break;
18503
18504 if (!nested_definition_count && cxx_dialect < cxx17)
18505 pedwarn (input_location, OPT_Wpedantic,
18506 "nested namespace definitions only available with "
18507 "-std=c++17 or -std=gnu++17");
18508
18509 /* Nested namespace names can create new namespaces (unlike
18510 other qualified-ids). */
18511 if (int count = identifier ? push_namespace (identifier) : 0)
18512 nested_definition_count += count;
18513 else
18514 cp_parser_error (parser, "nested namespace name required");
18515 cp_lexer_consume_token (parser->lexer);
18516 }
18517
18518 if (nested_definition_count && !identifier)
18519 cp_parser_error (parser, "namespace name required");
18520
18521 if (nested_definition_count && attribs)
18522 error_at (token->location,
18523 "a nested namespace definition cannot have attributes");
18524 if (nested_definition_count && is_inline)
18525 error_at (token->location,
18526 "a nested namespace definition cannot be inline");
18527
18528 /* Start the namespace. */
18529 nested_definition_count += push_namespace (identifier, is_inline);
18530
18531 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18532
18533 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18534
18535 /* Look for the `{' to validate starting the namespace. */
18536 matching_braces braces;
18537 if (braces.require_open (parser))
18538 {
18539 /* Parse the body of the namespace. */
18540 cp_parser_namespace_body (parser);
18541
18542 /* Look for the final `}'. */
18543 braces.require_close (parser);
18544 }
18545
18546 if (has_visibility)
18547 pop_visibility (1);
18548
18549 /* Pop the nested namespace definitions. */
18550 while (nested_definition_count--)
18551 pop_namespace ();
18552 }
18553
18554 /* Parse a namespace-body.
18555
18556 namespace-body:
18557 declaration-seq [opt] */
18558
18559 static void
18560 cp_parser_namespace_body (cp_parser* parser)
18561 {
18562 cp_parser_declaration_seq_opt (parser);
18563 }
18564
18565 /* Parse a namespace-alias-definition.
18566
18567 namespace-alias-definition:
18568 namespace identifier = qualified-namespace-specifier ; */
18569
18570 static void
18571 cp_parser_namespace_alias_definition (cp_parser* parser)
18572 {
18573 tree identifier;
18574 tree namespace_specifier;
18575
18576 cp_token *token = cp_lexer_peek_token (parser->lexer);
18577
18578 /* Look for the `namespace' keyword. */
18579 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18580 /* Look for the identifier. */
18581 identifier = cp_parser_identifier (parser);
18582 if (identifier == error_mark_node)
18583 return;
18584 /* Look for the `=' token. */
18585 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18586 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18587 {
18588 error_at (token->location, "%<namespace%> definition is not allowed here");
18589 /* Skip the definition. */
18590 cp_lexer_consume_token (parser->lexer);
18591 if (cp_parser_skip_to_closing_brace (parser))
18592 cp_lexer_consume_token (parser->lexer);
18593 return;
18594 }
18595 cp_parser_require (parser, CPP_EQ, RT_EQ);
18596 /* Look for the qualified-namespace-specifier. */
18597 namespace_specifier
18598 = cp_parser_qualified_namespace_specifier (parser);
18599 /* Look for the `;' token. */
18600 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18601
18602 /* Register the alias in the symbol table. */
18603 do_namespace_alias (identifier, namespace_specifier);
18604 }
18605
18606 /* Parse a qualified-namespace-specifier.
18607
18608 qualified-namespace-specifier:
18609 :: [opt] nested-name-specifier [opt] namespace-name
18610
18611 Returns a NAMESPACE_DECL corresponding to the specified
18612 namespace. */
18613
18614 static tree
18615 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18616 {
18617 /* Look for the optional `::'. */
18618 cp_parser_global_scope_opt (parser,
18619 /*current_scope_valid_p=*/false);
18620
18621 /* Look for the optional nested-name-specifier. */
18622 cp_parser_nested_name_specifier_opt (parser,
18623 /*typename_keyword_p=*/false,
18624 /*check_dependency_p=*/true,
18625 /*type_p=*/false,
18626 /*is_declaration=*/true);
18627
18628 return cp_parser_namespace_name (parser);
18629 }
18630
18631 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18632 access declaration.
18633
18634 using-declaration:
18635 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18636 using :: unqualified-id ;
18637
18638 access-declaration:
18639 qualified-id ;
18640
18641 */
18642
18643 static bool
18644 cp_parser_using_declaration (cp_parser* parser,
18645 bool access_declaration_p)
18646 {
18647 cp_token *token;
18648 bool typename_p = false;
18649 bool global_scope_p;
18650 tree decl;
18651 tree identifier;
18652 tree qscope;
18653 int oldcount = errorcount;
18654 cp_token *diag_token = NULL;
18655
18656 if (access_declaration_p)
18657 {
18658 diag_token = cp_lexer_peek_token (parser->lexer);
18659 cp_parser_parse_tentatively (parser);
18660 }
18661 else
18662 {
18663 /* Look for the `using' keyword. */
18664 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18665
18666 again:
18667 /* Peek at the next token. */
18668 token = cp_lexer_peek_token (parser->lexer);
18669 /* See if it's `typename'. */
18670 if (token->keyword == RID_TYPENAME)
18671 {
18672 /* Remember that we've seen it. */
18673 typename_p = true;
18674 /* Consume the `typename' token. */
18675 cp_lexer_consume_token (parser->lexer);
18676 }
18677 }
18678
18679 /* Look for the optional global scope qualification. */
18680 global_scope_p
18681 = (cp_parser_global_scope_opt (parser,
18682 /*current_scope_valid_p=*/false)
18683 != NULL_TREE);
18684
18685 /* If we saw `typename', or didn't see `::', then there must be a
18686 nested-name-specifier present. */
18687 if (typename_p || !global_scope_p)
18688 {
18689 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18690 /*check_dependency_p=*/true,
18691 /*type_p=*/false,
18692 /*is_declaration=*/true);
18693 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18694 {
18695 cp_parser_skip_to_end_of_block_or_statement (parser);
18696 return false;
18697 }
18698 }
18699 /* Otherwise, we could be in either of the two productions. In that
18700 case, treat the nested-name-specifier as optional. */
18701 else
18702 qscope = cp_parser_nested_name_specifier_opt (parser,
18703 /*typename_keyword_p=*/false,
18704 /*check_dependency_p=*/true,
18705 /*type_p=*/false,
18706 /*is_declaration=*/true);
18707 if (!qscope)
18708 qscope = global_namespace;
18709 else if (UNSCOPED_ENUM_P (qscope))
18710 qscope = CP_TYPE_CONTEXT (qscope);
18711
18712 if (access_declaration_p && cp_parser_error_occurred (parser))
18713 /* Something has already gone wrong; there's no need to parse
18714 further. Since an error has occurred, the return value of
18715 cp_parser_parse_definitely will be false, as required. */
18716 return cp_parser_parse_definitely (parser);
18717
18718 token = cp_lexer_peek_token (parser->lexer);
18719 /* Parse the unqualified-id. */
18720 identifier = cp_parser_unqualified_id (parser,
18721 /*template_keyword_p=*/false,
18722 /*check_dependency_p=*/true,
18723 /*declarator_p=*/true,
18724 /*optional_p=*/false);
18725
18726 if (access_declaration_p)
18727 {
18728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18729 cp_parser_simulate_error (parser);
18730 if (!cp_parser_parse_definitely (parser))
18731 return false;
18732 }
18733 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18734 {
18735 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18736 if (cxx_dialect < cxx17
18737 && !in_system_header_at (ell->location))
18738 pedwarn (ell->location, 0,
18739 "pack expansion in using-declaration only available "
18740 "with -std=c++17 or -std=gnu++17");
18741 qscope = make_pack_expansion (qscope);
18742 }
18743
18744 /* The function we call to handle a using-declaration is different
18745 depending on what scope we are in. */
18746 if (qscope == error_mark_node || identifier == error_mark_node)
18747 ;
18748 else if (!identifier_p (identifier)
18749 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18750 /* [namespace.udecl]
18751
18752 A using declaration shall not name a template-id. */
18753 error_at (token->location,
18754 "a template-id may not appear in a using-declaration");
18755 else
18756 {
18757 if (at_class_scope_p ())
18758 {
18759 /* Create the USING_DECL. */
18760 decl = do_class_using_decl (qscope, identifier);
18761
18762 if (decl && typename_p)
18763 USING_DECL_TYPENAME_P (decl) = 1;
18764
18765 if (check_for_bare_parameter_packs (decl))
18766 {
18767 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18768 return false;
18769 }
18770 else
18771 /* Add it to the list of members in this class. */
18772 finish_member_declaration (decl);
18773 }
18774 else
18775 {
18776 decl = cp_parser_lookup_name_simple (parser,
18777 identifier,
18778 token->location);
18779 if (decl == error_mark_node)
18780 cp_parser_name_lookup_error (parser, identifier,
18781 decl, NLE_NULL,
18782 token->location);
18783 else if (check_for_bare_parameter_packs (decl))
18784 {
18785 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18786 return false;
18787 }
18788 else if (!at_namespace_scope_p ())
18789 finish_local_using_decl (decl, qscope, identifier);
18790 else
18791 finish_namespace_using_decl (decl, qscope, identifier);
18792 }
18793 }
18794
18795 if (!access_declaration_p
18796 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18797 {
18798 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18799 if (cxx_dialect < cxx17)
18800 pedwarn (comma->location, 0,
18801 "comma-separated list in using-declaration only available "
18802 "with -std=c++17 or -std=gnu++17");
18803 goto again;
18804 }
18805
18806 /* Look for the final `;'. */
18807 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18808
18809 if (access_declaration_p && errorcount == oldcount)
18810 warning_at (diag_token->location, OPT_Wdeprecated,
18811 "access declarations are deprecated "
18812 "in favour of using-declarations; "
18813 "suggestion: add the %<using%> keyword");
18814
18815 return true;
18816 }
18817
18818 /* Parse an alias-declaration.
18819
18820 alias-declaration:
18821 using identifier attribute-specifier-seq [opt] = type-id */
18822
18823 static tree
18824 cp_parser_alias_declaration (cp_parser* parser)
18825 {
18826 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18827 location_t id_location;
18828 cp_declarator *declarator;
18829 cp_decl_specifier_seq decl_specs;
18830 bool member_p;
18831 const char *saved_message = NULL;
18832
18833 /* Look for the `using' keyword. */
18834 cp_token *using_token
18835 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18836 if (using_token == NULL)
18837 return error_mark_node;
18838
18839 id_location = cp_lexer_peek_token (parser->lexer)->location;
18840 id = cp_parser_identifier (parser);
18841 if (id == error_mark_node)
18842 return error_mark_node;
18843
18844 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18845 attributes = cp_parser_attributes_opt (parser);
18846 if (attributes == error_mark_node)
18847 return error_mark_node;
18848
18849 cp_parser_require (parser, CPP_EQ, RT_EQ);
18850
18851 if (cp_parser_error_occurred (parser))
18852 return error_mark_node;
18853
18854 cp_parser_commit_to_tentative_parse (parser);
18855
18856 /* Now we are going to parse the type-id of the declaration. */
18857
18858 /*
18859 [dcl.type]/3 says:
18860
18861 "A type-specifier-seq shall not define a class or enumeration
18862 unless it appears in the type-id of an alias-declaration (7.1.3) that
18863 is not the declaration of a template-declaration."
18864
18865 In other words, if we currently are in an alias template, the
18866 type-id should not define a type.
18867
18868 So let's set parser->type_definition_forbidden_message in that
18869 case; cp_parser_check_type_definition (called by
18870 cp_parser_class_specifier) will then emit an error if a type is
18871 defined in the type-id. */
18872 if (parser->num_template_parameter_lists)
18873 {
18874 saved_message = parser->type_definition_forbidden_message;
18875 parser->type_definition_forbidden_message =
18876 G_("types may not be defined in alias template declarations");
18877 }
18878
18879 type = cp_parser_type_id (parser);
18880
18881 /* Restore the error message if need be. */
18882 if (parser->num_template_parameter_lists)
18883 parser->type_definition_forbidden_message = saved_message;
18884
18885 if (type == error_mark_node
18886 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18887 {
18888 cp_parser_skip_to_end_of_block_or_statement (parser);
18889 return error_mark_node;
18890 }
18891
18892 /* A typedef-name can also be introduced by an alias-declaration. The
18893 identifier following the using keyword becomes a typedef-name. It has
18894 the same semantics as if it were introduced by the typedef
18895 specifier. In particular, it does not define a new type and it shall
18896 not appear in the type-id. */
18897
18898 clear_decl_specs (&decl_specs);
18899 decl_specs.type = type;
18900 if (attributes != NULL_TREE)
18901 {
18902 decl_specs.attributes = attributes;
18903 set_and_check_decl_spec_loc (&decl_specs,
18904 ds_attribute,
18905 attrs_token);
18906 }
18907 set_and_check_decl_spec_loc (&decl_specs,
18908 ds_typedef,
18909 using_token);
18910 set_and_check_decl_spec_loc (&decl_specs,
18911 ds_alias,
18912 using_token);
18913
18914 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18915 declarator->id_loc = id_location;
18916
18917 member_p = at_class_scope_p ();
18918 if (member_p)
18919 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18920 NULL_TREE, attributes);
18921 else
18922 decl = start_decl (declarator, &decl_specs, 0,
18923 attributes, NULL_TREE, &pushed_scope);
18924 if (decl == error_mark_node)
18925 return decl;
18926
18927 // Attach constraints to the alias declaration.
18928 if (flag_concepts && current_template_parms)
18929 {
18930 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18931 tree constr = build_constraints (reqs, NULL_TREE);
18932 set_constraints (decl, constr);
18933 }
18934
18935 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18936
18937 if (pushed_scope)
18938 pop_scope (pushed_scope);
18939
18940 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18941 added into the symbol table; otherwise, return the TYPE_DECL. */
18942 if (DECL_LANG_SPECIFIC (decl)
18943 && DECL_TEMPLATE_INFO (decl)
18944 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18945 {
18946 decl = DECL_TI_TEMPLATE (decl);
18947 if (member_p)
18948 check_member_template (decl);
18949 }
18950
18951 return decl;
18952 }
18953
18954 /* Parse a using-directive.
18955
18956 using-directive:
18957 using namespace :: [opt] nested-name-specifier [opt]
18958 namespace-name ; */
18959
18960 static void
18961 cp_parser_using_directive (cp_parser* parser)
18962 {
18963 tree namespace_decl;
18964 tree attribs;
18965
18966 /* Look for the `using' keyword. */
18967 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18968 /* And the `namespace' keyword. */
18969 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18970 /* Look for the optional `::' operator. */
18971 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18972 /* And the optional nested-name-specifier. */
18973 cp_parser_nested_name_specifier_opt (parser,
18974 /*typename_keyword_p=*/false,
18975 /*check_dependency_p=*/true,
18976 /*type_p=*/false,
18977 /*is_declaration=*/true);
18978 /* Get the namespace being used. */
18979 namespace_decl = cp_parser_namespace_name (parser);
18980 /* And any specified attributes. */
18981 attribs = cp_parser_attributes_opt (parser);
18982
18983 /* Update the symbol table. */
18984 if (namespace_bindings_p ())
18985 finish_namespace_using_directive (namespace_decl, attribs);
18986 else
18987 finish_local_using_directive (namespace_decl, attribs);
18988
18989 /* Look for the final `;'. */
18990 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18991 }
18992
18993 /* Parse an asm-definition.
18994
18995 asm-definition:
18996 asm ( string-literal ) ;
18997
18998 GNU Extension:
18999
19000 asm-definition:
19001 asm volatile [opt] ( string-literal ) ;
19002 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19003 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19004 : asm-operand-list [opt] ) ;
19005 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19006 : asm-operand-list [opt]
19007 : asm-clobber-list [opt] ) ;
19008 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19009 : asm-clobber-list [opt]
19010 : asm-goto-list ) ; */
19011
19012 static void
19013 cp_parser_asm_definition (cp_parser* parser)
19014 {
19015 tree string;
19016 tree outputs = NULL_TREE;
19017 tree inputs = NULL_TREE;
19018 tree clobbers = NULL_TREE;
19019 tree labels = NULL_TREE;
19020 tree asm_stmt;
19021 bool volatile_p = false;
19022 bool extended_p = false;
19023 bool invalid_inputs_p = false;
19024 bool invalid_outputs_p = false;
19025 bool goto_p = false;
19026 required_token missing = RT_NONE;
19027
19028 /* Look for the `asm' keyword. */
19029 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19030
19031 if (parser->in_function_body
19032 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19033 {
19034 error ("%<asm%> in %<constexpr%> function");
19035 cp_function_chain->invalid_constexpr = true;
19036 }
19037
19038 /* See if the next token is `volatile'. */
19039 if (cp_parser_allow_gnu_extensions_p (parser)
19040 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19041 {
19042 /* Remember that we saw the `volatile' keyword. */
19043 volatile_p = true;
19044 /* Consume the token. */
19045 cp_lexer_consume_token (parser->lexer);
19046 }
19047 if (cp_parser_allow_gnu_extensions_p (parser)
19048 && parser->in_function_body
19049 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19050 {
19051 /* Remember that we saw the `goto' keyword. */
19052 goto_p = true;
19053 /* Consume the token. */
19054 cp_lexer_consume_token (parser->lexer);
19055 }
19056 /* Look for the opening `('. */
19057 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19058 return;
19059 /* Look for the string. */
19060 string = cp_parser_string_literal (parser, false, false);
19061 if (string == error_mark_node)
19062 {
19063 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19064 /*consume_paren=*/true);
19065 return;
19066 }
19067
19068 /* If we're allowing GNU extensions, check for the extended assembly
19069 syntax. Unfortunately, the `:' tokens need not be separated by
19070 a space in C, and so, for compatibility, we tolerate that here
19071 too. Doing that means that we have to treat the `::' operator as
19072 two `:' tokens. */
19073 if (cp_parser_allow_gnu_extensions_p (parser)
19074 && parser->in_function_body
19075 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19076 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19077 {
19078 bool inputs_p = false;
19079 bool clobbers_p = false;
19080 bool labels_p = false;
19081
19082 /* The extended syntax was used. */
19083 extended_p = true;
19084
19085 /* Look for outputs. */
19086 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19087 {
19088 /* Consume the `:'. */
19089 cp_lexer_consume_token (parser->lexer);
19090 /* Parse the output-operands. */
19091 if (cp_lexer_next_token_is_not (parser->lexer,
19092 CPP_COLON)
19093 && cp_lexer_next_token_is_not (parser->lexer,
19094 CPP_SCOPE)
19095 && cp_lexer_next_token_is_not (parser->lexer,
19096 CPP_CLOSE_PAREN)
19097 && !goto_p)
19098 {
19099 outputs = cp_parser_asm_operand_list (parser);
19100 if (outputs == error_mark_node)
19101 invalid_outputs_p = true;
19102 }
19103 }
19104 /* If the next token is `::', there are no outputs, and the
19105 next token is the beginning of the inputs. */
19106 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19107 /* The inputs are coming next. */
19108 inputs_p = true;
19109
19110 /* Look for inputs. */
19111 if (inputs_p
19112 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19113 {
19114 /* Consume the `:' or `::'. */
19115 cp_lexer_consume_token (parser->lexer);
19116 /* Parse the output-operands. */
19117 if (cp_lexer_next_token_is_not (parser->lexer,
19118 CPP_COLON)
19119 && cp_lexer_next_token_is_not (parser->lexer,
19120 CPP_SCOPE)
19121 && cp_lexer_next_token_is_not (parser->lexer,
19122 CPP_CLOSE_PAREN))
19123 {
19124 inputs = cp_parser_asm_operand_list (parser);
19125 if (inputs == error_mark_node)
19126 invalid_inputs_p = true;
19127 }
19128 }
19129 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19130 /* The clobbers are coming next. */
19131 clobbers_p = true;
19132
19133 /* Look for clobbers. */
19134 if (clobbers_p
19135 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19136 {
19137 clobbers_p = true;
19138 /* Consume the `:' or `::'. */
19139 cp_lexer_consume_token (parser->lexer);
19140 /* Parse the clobbers. */
19141 if (cp_lexer_next_token_is_not (parser->lexer,
19142 CPP_COLON)
19143 && cp_lexer_next_token_is_not (parser->lexer,
19144 CPP_CLOSE_PAREN))
19145 clobbers = cp_parser_asm_clobber_list (parser);
19146 }
19147 else if (goto_p
19148 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19149 /* The labels are coming next. */
19150 labels_p = true;
19151
19152 /* Look for labels. */
19153 if (labels_p
19154 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19155 {
19156 labels_p = true;
19157 /* Consume the `:' or `::'. */
19158 cp_lexer_consume_token (parser->lexer);
19159 /* Parse the labels. */
19160 labels = cp_parser_asm_label_list (parser);
19161 }
19162
19163 if (goto_p && !labels_p)
19164 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19165 }
19166 else if (goto_p)
19167 missing = RT_COLON_SCOPE;
19168
19169 /* Look for the closing `)'. */
19170 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19171 missing ? missing : RT_CLOSE_PAREN))
19172 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19173 /*consume_paren=*/true);
19174 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19175
19176 if (!invalid_inputs_p && !invalid_outputs_p)
19177 {
19178 /* Create the ASM_EXPR. */
19179 if (parser->in_function_body)
19180 {
19181 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19182 inputs, clobbers, labels);
19183 /* If the extended syntax was not used, mark the ASM_EXPR. */
19184 if (!extended_p)
19185 {
19186 tree temp = asm_stmt;
19187 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19188 temp = TREE_OPERAND (temp, 0);
19189
19190 ASM_INPUT_P (temp) = 1;
19191 }
19192 }
19193 else
19194 symtab->finalize_toplevel_asm (string);
19195 }
19196 }
19197
19198 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19199 type that comes from the decl-specifier-seq. */
19200
19201 static tree
19202 strip_declarator_types (tree type, cp_declarator *declarator)
19203 {
19204 for (cp_declarator *d = declarator; d;)
19205 switch (d->kind)
19206 {
19207 case cdk_id:
19208 case cdk_decomp:
19209 case cdk_error:
19210 d = NULL;
19211 break;
19212
19213 default:
19214 if (TYPE_PTRMEMFUNC_P (type))
19215 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19216 type = TREE_TYPE (type);
19217 d = d->declarator;
19218 break;
19219 }
19220
19221 return type;
19222 }
19223
19224 /* Declarators [gram.dcl.decl] */
19225
19226 /* Parse an init-declarator.
19227
19228 init-declarator:
19229 declarator initializer [opt]
19230
19231 GNU Extension:
19232
19233 init-declarator:
19234 declarator asm-specification [opt] attributes [opt] initializer [opt]
19235
19236 function-definition:
19237 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19238 function-body
19239 decl-specifier-seq [opt] declarator function-try-block
19240
19241 GNU Extension:
19242
19243 function-definition:
19244 __extension__ function-definition
19245
19246 TM Extension:
19247
19248 function-definition:
19249 decl-specifier-seq [opt] declarator function-transaction-block
19250
19251 The DECL_SPECIFIERS apply to this declarator. Returns a
19252 representation of the entity declared. If MEMBER_P is TRUE, then
19253 this declarator appears in a class scope. The new DECL created by
19254 this declarator is returned.
19255
19256 The CHECKS are access checks that should be performed once we know
19257 what entity is being declared (and, therefore, what classes have
19258 befriended it).
19259
19260 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19261 for a function-definition here as well. If the declarator is a
19262 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19263 be TRUE upon return. By that point, the function-definition will
19264 have been completely parsed.
19265
19266 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19267 is FALSE.
19268
19269 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19270 parsed declaration if it is an uninitialized single declarator not followed
19271 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19272 if present, will not be consumed. If returned, this declarator will be
19273 created with SD_INITIALIZED but will not call cp_finish_decl.
19274
19275 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19276 and there is an initializer, the pointed location_t is set to the
19277 location of the '=' or `(', or '{' in C++11 token introducing the
19278 initializer. */
19279
19280 static tree
19281 cp_parser_init_declarator (cp_parser* parser,
19282 cp_decl_specifier_seq *decl_specifiers,
19283 vec<deferred_access_check, va_gc> *checks,
19284 bool function_definition_allowed_p,
19285 bool member_p,
19286 int declares_class_or_enum,
19287 bool* function_definition_p,
19288 tree* maybe_range_for_decl,
19289 location_t* init_loc,
19290 tree* auto_result)
19291 {
19292 cp_token *token = NULL, *asm_spec_start_token = NULL,
19293 *attributes_start_token = NULL;
19294 cp_declarator *declarator;
19295 tree prefix_attributes;
19296 tree attributes = NULL;
19297 tree asm_specification;
19298 tree initializer;
19299 tree decl = NULL_TREE;
19300 tree scope;
19301 int is_initialized;
19302 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19303 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19304 "(...)". */
19305 enum cpp_ttype initialization_kind;
19306 bool is_direct_init = false;
19307 bool is_non_constant_init;
19308 int ctor_dtor_or_conv_p;
19309 bool friend_p = cp_parser_friend_p (decl_specifiers);
19310 tree pushed_scope = NULL_TREE;
19311 bool range_for_decl_p = false;
19312 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19313 location_t tmp_init_loc = UNKNOWN_LOCATION;
19314
19315 /* Gather the attributes that were provided with the
19316 decl-specifiers. */
19317 prefix_attributes = decl_specifiers->attributes;
19318
19319 /* Assume that this is not the declarator for a function
19320 definition. */
19321 if (function_definition_p)
19322 *function_definition_p = false;
19323
19324 /* Default arguments are only permitted for function parameters. */
19325 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19326 parser->default_arg_ok_p = false;
19327
19328 /* Defer access checks while parsing the declarator; we cannot know
19329 what names are accessible until we know what is being
19330 declared. */
19331 resume_deferring_access_checks ();
19332
19333 token = cp_lexer_peek_token (parser->lexer);
19334
19335 /* Parse the declarator. */
19336 declarator
19337 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19338 &ctor_dtor_or_conv_p,
19339 /*parenthesized_p=*/NULL,
19340 member_p, friend_p);
19341 /* Gather up the deferred checks. */
19342 stop_deferring_access_checks ();
19343
19344 parser->default_arg_ok_p = saved_default_arg_ok_p;
19345
19346 /* If the DECLARATOR was erroneous, there's no need to go
19347 further. */
19348 if (declarator == cp_error_declarator)
19349 return error_mark_node;
19350
19351 /* Check that the number of template-parameter-lists is OK. */
19352 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19353 token->location))
19354 return error_mark_node;
19355
19356 if (declares_class_or_enum & 2)
19357 cp_parser_check_for_definition_in_return_type (declarator,
19358 decl_specifiers->type,
19359 decl_specifiers->locations[ds_type_spec]);
19360
19361 /* Figure out what scope the entity declared by the DECLARATOR is
19362 located in. `grokdeclarator' sometimes changes the scope, so
19363 we compute it now. */
19364 scope = get_scope_of_declarator (declarator);
19365
19366 /* Perform any lookups in the declared type which were thought to be
19367 dependent, but are not in the scope of the declarator. */
19368 decl_specifiers->type
19369 = maybe_update_decl_type (decl_specifiers->type, scope);
19370
19371 /* If we're allowing GNU extensions, look for an
19372 asm-specification. */
19373 if (cp_parser_allow_gnu_extensions_p (parser))
19374 {
19375 /* Look for an asm-specification. */
19376 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19377 asm_specification = cp_parser_asm_specification_opt (parser);
19378 }
19379 else
19380 asm_specification = NULL_TREE;
19381
19382 /* Look for attributes. */
19383 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19384 attributes = cp_parser_attributes_opt (parser);
19385
19386 /* Peek at the next token. */
19387 token = cp_lexer_peek_token (parser->lexer);
19388
19389 bool bogus_implicit_tmpl = false;
19390
19391 if (function_declarator_p (declarator))
19392 {
19393 /* Handle C++17 deduction guides. */
19394 if (!decl_specifiers->type
19395 && ctor_dtor_or_conv_p <= 0
19396 && cxx_dialect >= cxx17)
19397 {
19398 cp_declarator *id = get_id_declarator (declarator);
19399 tree name = id->u.id.unqualified_name;
19400 parser->scope = id->u.id.qualifying_scope;
19401 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19402 if (tmpl
19403 && (DECL_CLASS_TEMPLATE_P (tmpl)
19404 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19405 {
19406 id->u.id.unqualified_name = dguide_name (tmpl);
19407 id->u.id.sfk = sfk_deduction_guide;
19408 ctor_dtor_or_conv_p = 1;
19409 }
19410 }
19411
19412 /* Check to see if the token indicates the start of a
19413 function-definition. */
19414 if (cp_parser_token_starts_function_definition_p (token))
19415 {
19416 if (!function_definition_allowed_p)
19417 {
19418 /* If a function-definition should not appear here, issue an
19419 error message. */
19420 cp_parser_error (parser,
19421 "a function-definition is not allowed here");
19422 return error_mark_node;
19423 }
19424
19425 location_t func_brace_location
19426 = cp_lexer_peek_token (parser->lexer)->location;
19427
19428 /* Neither attributes nor an asm-specification are allowed
19429 on a function-definition. */
19430 if (asm_specification)
19431 error_at (asm_spec_start_token->location,
19432 "an asm-specification is not allowed "
19433 "on a function-definition");
19434 if (attributes)
19435 error_at (attributes_start_token->location,
19436 "attributes are not allowed "
19437 "on a function-definition");
19438 /* This is a function-definition. */
19439 *function_definition_p = true;
19440
19441 /* Parse the function definition. */
19442 if (member_p)
19443 decl = cp_parser_save_member_function_body (parser,
19444 decl_specifiers,
19445 declarator,
19446 prefix_attributes);
19447 else
19448 decl =
19449 (cp_parser_function_definition_from_specifiers_and_declarator
19450 (parser, decl_specifiers, prefix_attributes, declarator));
19451
19452 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19453 {
19454 /* This is where the prologue starts... */
19455 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19456 = func_brace_location;
19457 }
19458
19459 return decl;
19460 }
19461 }
19462 else if (parser->fully_implicit_function_template_p)
19463 {
19464 /* A non-template declaration involving a function parameter list
19465 containing an implicit template parameter will be made into a
19466 template. If the resulting declaration is not going to be an
19467 actual function then finish the template scope here to prevent it.
19468 An error message will be issued once we have a decl to talk about.
19469
19470 FIXME probably we should do type deduction rather than create an
19471 implicit template, but the standard currently doesn't allow it. */
19472 bogus_implicit_tmpl = true;
19473 finish_fully_implicit_template (parser, NULL_TREE);
19474 }
19475
19476 /* [dcl.dcl]
19477
19478 Only in function declarations for constructors, destructors, type
19479 conversions, and deduction guides can the decl-specifier-seq be omitted.
19480
19481 We explicitly postpone this check past the point where we handle
19482 function-definitions because we tolerate function-definitions
19483 that are missing their return types in some modes. */
19484 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19485 {
19486 cp_parser_error (parser,
19487 "expected constructor, destructor, or type conversion");
19488 return error_mark_node;
19489 }
19490
19491 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19492 if (token->type == CPP_EQ
19493 || token->type == CPP_OPEN_PAREN
19494 || token->type == CPP_OPEN_BRACE)
19495 {
19496 is_initialized = SD_INITIALIZED;
19497 initialization_kind = token->type;
19498 if (maybe_range_for_decl)
19499 *maybe_range_for_decl = error_mark_node;
19500 tmp_init_loc = token->location;
19501 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19502 *init_loc = tmp_init_loc;
19503
19504 if (token->type == CPP_EQ
19505 && function_declarator_p (declarator))
19506 {
19507 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19508 if (t2->keyword == RID_DEFAULT)
19509 is_initialized = SD_DEFAULTED;
19510 else if (t2->keyword == RID_DELETE)
19511 is_initialized = SD_DELETED;
19512 }
19513 }
19514 else
19515 {
19516 /* If the init-declarator isn't initialized and isn't followed by a
19517 `,' or `;', it's not a valid init-declarator. */
19518 if (token->type != CPP_COMMA
19519 && token->type != CPP_SEMICOLON)
19520 {
19521 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19522 range_for_decl_p = true;
19523 else
19524 {
19525 if (!maybe_range_for_decl)
19526 cp_parser_error (parser, "expected initializer");
19527 return error_mark_node;
19528 }
19529 }
19530 is_initialized = SD_UNINITIALIZED;
19531 initialization_kind = CPP_EOF;
19532 }
19533
19534 /* Because start_decl has side-effects, we should only call it if we
19535 know we're going ahead. By this point, we know that we cannot
19536 possibly be looking at any other construct. */
19537 cp_parser_commit_to_tentative_parse (parser);
19538
19539 /* Enter the newly declared entry in the symbol table. If we're
19540 processing a declaration in a class-specifier, we wait until
19541 after processing the initializer. */
19542 if (!member_p)
19543 {
19544 if (parser->in_unbraced_linkage_specification_p)
19545 decl_specifiers->storage_class = sc_extern;
19546 decl = start_decl (declarator, decl_specifiers,
19547 range_for_decl_p? SD_INITIALIZED : is_initialized,
19548 attributes, prefix_attributes, &pushed_scope);
19549 cp_finalize_omp_declare_simd (parser, decl);
19550 cp_finalize_oacc_routine (parser, decl, false);
19551 /* Adjust location of decl if declarator->id_loc is more appropriate:
19552 set, and decl wasn't merged with another decl, in which case its
19553 location would be different from input_location, and more accurate. */
19554 if (DECL_P (decl)
19555 && declarator->id_loc != UNKNOWN_LOCATION
19556 && DECL_SOURCE_LOCATION (decl) == input_location)
19557 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19558 }
19559 else if (scope)
19560 /* Enter the SCOPE. That way unqualified names appearing in the
19561 initializer will be looked up in SCOPE. */
19562 pushed_scope = push_scope (scope);
19563
19564 /* Perform deferred access control checks, now that we know in which
19565 SCOPE the declared entity resides. */
19566 if (!member_p && decl)
19567 {
19568 tree saved_current_function_decl = NULL_TREE;
19569
19570 /* If the entity being declared is a function, pretend that we
19571 are in its scope. If it is a `friend', it may have access to
19572 things that would not otherwise be accessible. */
19573 if (TREE_CODE (decl) == FUNCTION_DECL)
19574 {
19575 saved_current_function_decl = current_function_decl;
19576 current_function_decl = decl;
19577 }
19578
19579 /* Perform access checks for template parameters. */
19580 cp_parser_perform_template_parameter_access_checks (checks);
19581
19582 /* Perform the access control checks for the declarator and the
19583 decl-specifiers. */
19584 perform_deferred_access_checks (tf_warning_or_error);
19585
19586 /* Restore the saved value. */
19587 if (TREE_CODE (decl) == FUNCTION_DECL)
19588 current_function_decl = saved_current_function_decl;
19589 }
19590
19591 /* Parse the initializer. */
19592 initializer = NULL_TREE;
19593 is_direct_init = false;
19594 is_non_constant_init = true;
19595 if (is_initialized)
19596 {
19597 if (function_declarator_p (declarator))
19598 {
19599 if (initialization_kind == CPP_EQ)
19600 initializer = cp_parser_pure_specifier (parser);
19601 else
19602 {
19603 /* If the declaration was erroneous, we don't really
19604 know what the user intended, so just silently
19605 consume the initializer. */
19606 if (decl != error_mark_node)
19607 error_at (tmp_init_loc, "initializer provided for function");
19608 cp_parser_skip_to_closing_parenthesis (parser,
19609 /*recovering=*/true,
19610 /*or_comma=*/false,
19611 /*consume_paren=*/true);
19612 }
19613 }
19614 else
19615 {
19616 /* We want to record the extra mangling scope for in-class
19617 initializers of class members and initializers of static data
19618 member templates. The former involves deferring
19619 parsing of the initializer until end of class as with default
19620 arguments. So right here we only handle the latter. */
19621 if (!member_p && processing_template_decl)
19622 start_lambda_scope (decl);
19623 initializer = cp_parser_initializer (parser,
19624 &is_direct_init,
19625 &is_non_constant_init);
19626 if (!member_p && processing_template_decl)
19627 finish_lambda_scope ();
19628 if (initializer == error_mark_node)
19629 cp_parser_skip_to_end_of_statement (parser);
19630 }
19631 }
19632
19633 /* The old parser allows attributes to appear after a parenthesized
19634 initializer. Mark Mitchell proposed removing this functionality
19635 on the GCC mailing lists on 2002-08-13. This parser accepts the
19636 attributes -- but ignores them. */
19637 if (cp_parser_allow_gnu_extensions_p (parser)
19638 && initialization_kind == CPP_OPEN_PAREN)
19639 if (cp_parser_attributes_opt (parser))
19640 warning (OPT_Wattributes,
19641 "attributes after parenthesized initializer ignored");
19642
19643 /* And now complain about a non-function implicit template. */
19644 if (bogus_implicit_tmpl && decl != error_mark_node)
19645 error_at (DECL_SOURCE_LOCATION (decl),
19646 "non-function %qD declared as implicit template", decl);
19647
19648 /* For an in-class declaration, use `grokfield' to create the
19649 declaration. */
19650 if (member_p)
19651 {
19652 if (pushed_scope)
19653 {
19654 pop_scope (pushed_scope);
19655 pushed_scope = NULL_TREE;
19656 }
19657 decl = grokfield (declarator, decl_specifiers,
19658 initializer, !is_non_constant_init,
19659 /*asmspec=*/NULL_TREE,
19660 chainon (attributes, prefix_attributes));
19661 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19662 cp_parser_save_default_args (parser, decl);
19663 cp_finalize_omp_declare_simd (parser, decl);
19664 cp_finalize_oacc_routine (parser, decl, false);
19665 }
19666
19667 /* Finish processing the declaration. But, skip member
19668 declarations. */
19669 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19670 {
19671 cp_finish_decl (decl,
19672 initializer, !is_non_constant_init,
19673 asm_specification,
19674 /* If the initializer is in parentheses, then this is
19675 a direct-initialization, which means that an
19676 `explicit' constructor is OK. Otherwise, an
19677 `explicit' constructor cannot be used. */
19678 ((is_direct_init || !is_initialized)
19679 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19680 }
19681 else if ((cxx_dialect != cxx98) && friend_p
19682 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19683 /* Core issue #226 (C++0x only): A default template-argument
19684 shall not be specified in a friend class template
19685 declaration. */
19686 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19687 /*is_partial=*/false, /*is_friend_decl=*/1);
19688
19689 if (!friend_p && pushed_scope)
19690 pop_scope (pushed_scope);
19691
19692 if (function_declarator_p (declarator)
19693 && parser->fully_implicit_function_template_p)
19694 {
19695 if (member_p)
19696 decl = finish_fully_implicit_template (parser, decl);
19697 else
19698 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19699 }
19700
19701 if (auto_result && is_initialized && decl_specifiers->type
19702 && type_uses_auto (decl_specifiers->type))
19703 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19704
19705 return decl;
19706 }
19707
19708 /* Parse a declarator.
19709
19710 declarator:
19711 direct-declarator
19712 ptr-operator declarator
19713
19714 abstract-declarator:
19715 ptr-operator abstract-declarator [opt]
19716 direct-abstract-declarator
19717
19718 GNU Extensions:
19719
19720 declarator:
19721 attributes [opt] direct-declarator
19722 attributes [opt] ptr-operator declarator
19723
19724 abstract-declarator:
19725 attributes [opt] ptr-operator abstract-declarator [opt]
19726 attributes [opt] direct-abstract-declarator
19727
19728 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19729 detect constructors, destructors, deduction guides, or conversion operators.
19730 It is set to -1 if the declarator is a name, and +1 if it is a
19731 function. Otherwise it is set to zero. Usually you just want to
19732 test for >0, but internally the negative value is used.
19733
19734 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19735 a decl-specifier-seq unless it declares a constructor, destructor,
19736 or conversion. It might seem that we could check this condition in
19737 semantic analysis, rather than parsing, but that makes it difficult
19738 to handle something like `f()'. We want to notice that there are
19739 no decl-specifiers, and therefore realize that this is an
19740 expression, not a declaration.)
19741
19742 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19743 the declarator is a direct-declarator of the form "(...)".
19744
19745 MEMBER_P is true iff this declarator is a member-declarator.
19746
19747 FRIEND_P is true iff this declarator is a friend. */
19748
19749 static cp_declarator *
19750 cp_parser_declarator (cp_parser* parser,
19751 cp_parser_declarator_kind dcl_kind,
19752 int* ctor_dtor_or_conv_p,
19753 bool* parenthesized_p,
19754 bool member_p, bool friend_p)
19755 {
19756 cp_declarator *declarator;
19757 enum tree_code code;
19758 cp_cv_quals cv_quals;
19759 tree class_type;
19760 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19761
19762 /* Assume this is not a constructor, destructor, or type-conversion
19763 operator. */
19764 if (ctor_dtor_or_conv_p)
19765 *ctor_dtor_or_conv_p = 0;
19766
19767 if (cp_parser_allow_gnu_extensions_p (parser))
19768 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19769
19770 /* Check for the ptr-operator production. */
19771 cp_parser_parse_tentatively (parser);
19772 /* Parse the ptr-operator. */
19773 code = cp_parser_ptr_operator (parser,
19774 &class_type,
19775 &cv_quals,
19776 &std_attributes);
19777
19778 /* If that worked, then we have a ptr-operator. */
19779 if (cp_parser_parse_definitely (parser))
19780 {
19781 /* If a ptr-operator was found, then this declarator was not
19782 parenthesized. */
19783 if (parenthesized_p)
19784 *parenthesized_p = true;
19785 /* The dependent declarator is optional if we are parsing an
19786 abstract-declarator. */
19787 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19788 cp_parser_parse_tentatively (parser);
19789
19790 /* Parse the dependent declarator. */
19791 declarator = cp_parser_declarator (parser, dcl_kind,
19792 /*ctor_dtor_or_conv_p=*/NULL,
19793 /*parenthesized_p=*/NULL,
19794 /*member_p=*/false,
19795 friend_p);
19796
19797 /* If we are parsing an abstract-declarator, we must handle the
19798 case where the dependent declarator is absent. */
19799 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19800 && !cp_parser_parse_definitely (parser))
19801 declarator = NULL;
19802
19803 declarator = cp_parser_make_indirect_declarator
19804 (code, class_type, cv_quals, declarator, std_attributes);
19805 }
19806 /* Everything else is a direct-declarator. */
19807 else
19808 {
19809 if (parenthesized_p)
19810 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19811 CPP_OPEN_PAREN);
19812 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19813 ctor_dtor_or_conv_p,
19814 member_p, friend_p);
19815 }
19816
19817 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19818 declarator->attributes = gnu_attributes;
19819 return declarator;
19820 }
19821
19822 /* Parse a direct-declarator or direct-abstract-declarator.
19823
19824 direct-declarator:
19825 declarator-id
19826 direct-declarator ( parameter-declaration-clause )
19827 cv-qualifier-seq [opt]
19828 ref-qualifier [opt]
19829 exception-specification [opt]
19830 direct-declarator [ constant-expression [opt] ]
19831 ( declarator )
19832
19833 direct-abstract-declarator:
19834 direct-abstract-declarator [opt]
19835 ( parameter-declaration-clause )
19836 cv-qualifier-seq [opt]
19837 ref-qualifier [opt]
19838 exception-specification [opt]
19839 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19840 ( abstract-declarator )
19841
19842 Returns a representation of the declarator. DCL_KIND is
19843 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19844 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19845 we are parsing a direct-declarator. It is
19846 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19847 of ambiguity we prefer an abstract declarator, as per
19848 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19849 as for cp_parser_declarator. */
19850
19851 static cp_declarator *
19852 cp_parser_direct_declarator (cp_parser* parser,
19853 cp_parser_declarator_kind dcl_kind,
19854 int* ctor_dtor_or_conv_p,
19855 bool member_p, bool friend_p)
19856 {
19857 cp_token *token;
19858 cp_declarator *declarator = NULL;
19859 tree scope = NULL_TREE;
19860 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19861 bool saved_in_declarator_p = parser->in_declarator_p;
19862 bool first = true;
19863 tree pushed_scope = NULL_TREE;
19864 cp_token *open_paren = NULL, *close_paren = NULL;
19865
19866 while (true)
19867 {
19868 /* Peek at the next token. */
19869 token = cp_lexer_peek_token (parser->lexer);
19870 if (token->type == CPP_OPEN_PAREN)
19871 {
19872 /* This is either a parameter-declaration-clause, or a
19873 parenthesized declarator. When we know we are parsing a
19874 named declarator, it must be a parenthesized declarator
19875 if FIRST is true. For instance, `(int)' is a
19876 parameter-declaration-clause, with an omitted
19877 direct-abstract-declarator. But `((*))', is a
19878 parenthesized abstract declarator. Finally, when T is a
19879 template parameter `(T)' is a
19880 parameter-declaration-clause, and not a parenthesized
19881 named declarator.
19882
19883 We first try and parse a parameter-declaration-clause,
19884 and then try a nested declarator (if FIRST is true).
19885
19886 It is not an error for it not to be a
19887 parameter-declaration-clause, even when FIRST is
19888 false. Consider,
19889
19890 int i (int);
19891 int i (3);
19892
19893 The first is the declaration of a function while the
19894 second is the definition of a variable, including its
19895 initializer.
19896
19897 Having seen only the parenthesis, we cannot know which of
19898 these two alternatives should be selected. Even more
19899 complex are examples like:
19900
19901 int i (int (a));
19902 int i (int (3));
19903
19904 The former is a function-declaration; the latter is a
19905 variable initialization.
19906
19907 Thus again, we try a parameter-declaration-clause, and if
19908 that fails, we back out and return. */
19909
19910 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19911 {
19912 tree params;
19913 bool is_declarator = false;
19914
19915 open_paren = NULL;
19916
19917 /* In a member-declarator, the only valid interpretation
19918 of a parenthesis is the start of a
19919 parameter-declaration-clause. (It is invalid to
19920 initialize a static data member with a parenthesized
19921 initializer; only the "=" form of initialization is
19922 permitted.) */
19923 if (!member_p)
19924 cp_parser_parse_tentatively (parser);
19925
19926 /* Consume the `('. */
19927 matching_parens parens;
19928 parens.consume_open (parser);
19929 if (first)
19930 {
19931 /* If this is going to be an abstract declarator, we're
19932 in a declarator and we can't have default args. */
19933 parser->default_arg_ok_p = false;
19934 parser->in_declarator_p = true;
19935 }
19936
19937 begin_scope (sk_function_parms, NULL_TREE);
19938
19939 /* Parse the parameter-declaration-clause. */
19940 params = cp_parser_parameter_declaration_clause (parser);
19941
19942 /* Consume the `)'. */
19943 parens.require_close (parser);
19944
19945 /* If all went well, parse the cv-qualifier-seq,
19946 ref-qualifier and the exception-specification. */
19947 if (member_p || cp_parser_parse_definitely (parser))
19948 {
19949 cp_cv_quals cv_quals;
19950 cp_virt_specifiers virt_specifiers;
19951 cp_ref_qualifier ref_qual;
19952 tree exception_specification;
19953 tree late_return;
19954 tree attrs;
19955 bool memfn = (member_p || (pushed_scope
19956 && CLASS_TYPE_P (pushed_scope)));
19957
19958 is_declarator = true;
19959
19960 if (ctor_dtor_or_conv_p)
19961 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19962 first = false;
19963
19964 /* Parse the cv-qualifier-seq. */
19965 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19966 /* Parse the ref-qualifier. */
19967 ref_qual = cp_parser_ref_qualifier_opt (parser);
19968 /* Parse the tx-qualifier. */
19969 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19970 /* And the exception-specification. */
19971 exception_specification
19972 = cp_parser_exception_specification_opt (parser);
19973
19974 attrs = cp_parser_std_attribute_spec_seq (parser);
19975
19976 /* In here, we handle cases where attribute is used after
19977 the function declaration. For example:
19978 void func (int x) __attribute__((vector(..))); */
19979 tree gnu_attrs = NULL_TREE;
19980 tree requires_clause = NULL_TREE;
19981 late_return = (cp_parser_late_return_type_opt
19982 (parser, declarator, requires_clause,
19983 memfn ? cv_quals : -1));
19984
19985 /* Parse the virt-specifier-seq. */
19986 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19987
19988 /* Create the function-declarator. */
19989 declarator = make_call_declarator (declarator,
19990 params,
19991 cv_quals,
19992 virt_specifiers,
19993 ref_qual,
19994 tx_qual,
19995 exception_specification,
19996 late_return,
19997 requires_clause);
19998 declarator->std_attributes = attrs;
19999 declarator->attributes = gnu_attrs;
20000 /* Any subsequent parameter lists are to do with
20001 return type, so are not those of the declared
20002 function. */
20003 parser->default_arg_ok_p = false;
20004 }
20005
20006 /* Remove the function parms from scope. */
20007 pop_bindings_and_leave_scope ();
20008
20009 if (is_declarator)
20010 /* Repeat the main loop. */
20011 continue;
20012 }
20013
20014 /* If this is the first, we can try a parenthesized
20015 declarator. */
20016 if (first)
20017 {
20018 bool saved_in_type_id_in_expr_p;
20019
20020 parser->default_arg_ok_p = saved_default_arg_ok_p;
20021 parser->in_declarator_p = saved_in_declarator_p;
20022
20023 open_paren = token;
20024 /* Consume the `('. */
20025 matching_parens parens;
20026 parens.consume_open (parser);
20027 /* Parse the nested declarator. */
20028 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20029 parser->in_type_id_in_expr_p = true;
20030 declarator
20031 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20032 /*parenthesized_p=*/NULL,
20033 member_p, friend_p);
20034 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20035 first = false;
20036 /* Expect a `)'. */
20037 close_paren = cp_lexer_peek_token (parser->lexer);
20038 if (!parens.require_close (parser))
20039 declarator = cp_error_declarator;
20040 if (declarator == cp_error_declarator)
20041 break;
20042
20043 goto handle_declarator;
20044 }
20045 /* Otherwise, we must be done. */
20046 else
20047 break;
20048 }
20049 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20050 && token->type == CPP_OPEN_SQUARE
20051 && !cp_next_tokens_can_be_attribute_p (parser))
20052 {
20053 /* Parse an array-declarator. */
20054 tree bounds, attrs;
20055
20056 if (ctor_dtor_or_conv_p)
20057 *ctor_dtor_or_conv_p = 0;
20058
20059 open_paren = NULL;
20060 first = false;
20061 parser->default_arg_ok_p = false;
20062 parser->in_declarator_p = true;
20063 /* Consume the `['. */
20064 cp_lexer_consume_token (parser->lexer);
20065 /* Peek at the next token. */
20066 token = cp_lexer_peek_token (parser->lexer);
20067 /* If the next token is `]', then there is no
20068 constant-expression. */
20069 if (token->type != CPP_CLOSE_SQUARE)
20070 {
20071 bool non_constant_p;
20072 bounds
20073 = cp_parser_constant_expression (parser,
20074 /*allow_non_constant=*/true,
20075 &non_constant_p);
20076 if (!non_constant_p)
20077 /* OK */;
20078 else if (error_operand_p (bounds))
20079 /* Already gave an error. */;
20080 else if (!parser->in_function_body
20081 || current_binding_level->kind == sk_function_parms)
20082 {
20083 /* Normally, the array bound must be an integral constant
20084 expression. However, as an extension, we allow VLAs
20085 in function scopes as long as they aren't part of a
20086 parameter declaration. */
20087 cp_parser_error (parser,
20088 "array bound is not an integer constant");
20089 bounds = error_mark_node;
20090 }
20091 else if (processing_template_decl
20092 && !type_dependent_expression_p (bounds))
20093 {
20094 /* Remember this wasn't a constant-expression. */
20095 bounds = build_nop (TREE_TYPE (bounds), bounds);
20096 TREE_SIDE_EFFECTS (bounds) = 1;
20097 }
20098 }
20099 else
20100 bounds = NULL_TREE;
20101 /* Look for the closing `]'. */
20102 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20103 {
20104 declarator = cp_error_declarator;
20105 break;
20106 }
20107
20108 attrs = cp_parser_std_attribute_spec_seq (parser);
20109 declarator = make_array_declarator (declarator, bounds);
20110 declarator->std_attributes = attrs;
20111 }
20112 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20113 {
20114 {
20115 tree qualifying_scope;
20116 tree unqualified_name;
20117 tree attrs;
20118 special_function_kind sfk;
20119 bool abstract_ok;
20120 bool pack_expansion_p = false;
20121 cp_token *declarator_id_start_token;
20122
20123 /* Parse a declarator-id */
20124 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20125 if (abstract_ok)
20126 {
20127 cp_parser_parse_tentatively (parser);
20128
20129 /* If we see an ellipsis, we should be looking at a
20130 parameter pack. */
20131 if (token->type == CPP_ELLIPSIS)
20132 {
20133 /* Consume the `...' */
20134 cp_lexer_consume_token (parser->lexer);
20135
20136 pack_expansion_p = true;
20137 }
20138 }
20139
20140 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20141 unqualified_name
20142 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20143 qualifying_scope = parser->scope;
20144 if (abstract_ok)
20145 {
20146 bool okay = false;
20147
20148 if (!unqualified_name && pack_expansion_p)
20149 {
20150 /* Check whether an error occurred. */
20151 okay = !cp_parser_error_occurred (parser);
20152
20153 /* We already consumed the ellipsis to mark a
20154 parameter pack, but we have no way to report it,
20155 so abort the tentative parse. We will be exiting
20156 immediately anyway. */
20157 cp_parser_abort_tentative_parse (parser);
20158 }
20159 else
20160 okay = cp_parser_parse_definitely (parser);
20161
20162 if (!okay)
20163 unqualified_name = error_mark_node;
20164 else if (unqualified_name
20165 && (qualifying_scope
20166 || (!identifier_p (unqualified_name))))
20167 {
20168 cp_parser_error (parser, "expected unqualified-id");
20169 unqualified_name = error_mark_node;
20170 }
20171 }
20172
20173 if (!unqualified_name)
20174 return NULL;
20175 if (unqualified_name == error_mark_node)
20176 {
20177 declarator = cp_error_declarator;
20178 pack_expansion_p = false;
20179 declarator->parameter_pack_p = false;
20180 break;
20181 }
20182
20183 attrs = cp_parser_std_attribute_spec_seq (parser);
20184
20185 if (qualifying_scope && at_namespace_scope_p ()
20186 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20187 {
20188 /* In the declaration of a member of a template class
20189 outside of the class itself, the SCOPE will sometimes
20190 be a TYPENAME_TYPE. For example, given:
20191
20192 template <typename T>
20193 int S<T>::R::i = 3;
20194
20195 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20196 this context, we must resolve S<T>::R to an ordinary
20197 type, rather than a typename type.
20198
20199 The reason we normally avoid resolving TYPENAME_TYPEs
20200 is that a specialization of `S' might render
20201 `S<T>::R' not a type. However, if `S' is
20202 specialized, then this `i' will not be used, so there
20203 is no harm in resolving the types here. */
20204 tree type;
20205
20206 /* Resolve the TYPENAME_TYPE. */
20207 type = resolve_typename_type (qualifying_scope,
20208 /*only_current_p=*/false);
20209 /* If that failed, the declarator is invalid. */
20210 if (TREE_CODE (type) == TYPENAME_TYPE)
20211 {
20212 if (typedef_variant_p (type))
20213 error_at (declarator_id_start_token->location,
20214 "cannot define member of dependent typedef "
20215 "%qT", type);
20216 else
20217 error_at (declarator_id_start_token->location,
20218 "%<%T::%E%> is not a type",
20219 TYPE_CONTEXT (qualifying_scope),
20220 TYPE_IDENTIFIER (qualifying_scope));
20221 }
20222 qualifying_scope = type;
20223 }
20224
20225 sfk = sfk_none;
20226
20227 if (unqualified_name)
20228 {
20229 tree class_type;
20230
20231 if (qualifying_scope
20232 && CLASS_TYPE_P (qualifying_scope))
20233 class_type = qualifying_scope;
20234 else
20235 class_type = current_class_type;
20236
20237 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20238 {
20239 tree name_type = TREE_TYPE (unqualified_name);
20240
20241 if (!class_type || !same_type_p (name_type, class_type))
20242 {
20243 /* We do not attempt to print the declarator
20244 here because we do not have enough
20245 information about its original syntactic
20246 form. */
20247 cp_parser_error (parser, "invalid declarator");
20248 declarator = cp_error_declarator;
20249 break;
20250 }
20251 else if (qualifying_scope
20252 && CLASSTYPE_USE_TEMPLATE (name_type))
20253 {
20254 error_at (declarator_id_start_token->location,
20255 "invalid use of constructor as a template");
20256 inform (declarator_id_start_token->location,
20257 "use %<%T::%D%> instead of %<%T::%D%> to "
20258 "name the constructor in a qualified name",
20259 class_type,
20260 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20261 class_type, name_type);
20262 declarator = cp_error_declarator;
20263 break;
20264 }
20265 unqualified_name = constructor_name (class_type);
20266 }
20267
20268 if (class_type)
20269 {
20270 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20271 sfk = sfk_destructor;
20272 else if (identifier_p (unqualified_name)
20273 && IDENTIFIER_CONV_OP_P (unqualified_name))
20274 sfk = sfk_conversion;
20275 else if (/* There's no way to declare a constructor
20276 for an unnamed type, even if the type
20277 got a name for linkage purposes. */
20278 !TYPE_WAS_UNNAMED (class_type)
20279 /* Handle correctly (c++/19200):
20280
20281 struct S {
20282 struct T{};
20283 friend void S(T);
20284 };
20285
20286 and also:
20287
20288 namespace N {
20289 void S();
20290 }
20291
20292 struct S {
20293 friend void N::S();
20294 }; */
20295 && (!friend_p || class_type == qualifying_scope)
20296 && constructor_name_p (unqualified_name,
20297 class_type))
20298 sfk = sfk_constructor;
20299 else if (is_overloaded_fn (unqualified_name)
20300 && DECL_CONSTRUCTOR_P (get_first_fn
20301 (unqualified_name)))
20302 sfk = sfk_constructor;
20303
20304 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20305 *ctor_dtor_or_conv_p = -1;
20306 }
20307 }
20308 declarator = make_id_declarator (qualifying_scope,
20309 unqualified_name,
20310 sfk);
20311 declarator->std_attributes = attrs;
20312 declarator->id_loc = token->location;
20313 declarator->parameter_pack_p = pack_expansion_p;
20314
20315 if (pack_expansion_p)
20316 maybe_warn_variadic_templates ();
20317 }
20318
20319 handle_declarator:;
20320 scope = get_scope_of_declarator (declarator);
20321 if (scope)
20322 {
20323 /* Any names that appear after the declarator-id for a
20324 member are looked up in the containing scope. */
20325 if (at_function_scope_p ())
20326 {
20327 /* But declarations with qualified-ids can't appear in a
20328 function. */
20329 cp_parser_error (parser, "qualified-id in declaration");
20330 declarator = cp_error_declarator;
20331 break;
20332 }
20333 pushed_scope = push_scope (scope);
20334 }
20335 parser->in_declarator_p = true;
20336 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20337 || (declarator && declarator->kind == cdk_id))
20338 /* Default args are only allowed on function
20339 declarations. */
20340 parser->default_arg_ok_p = saved_default_arg_ok_p;
20341 else
20342 parser->default_arg_ok_p = false;
20343
20344 first = false;
20345 }
20346 /* We're done. */
20347 else
20348 break;
20349 }
20350
20351 /* For an abstract declarator, we might wind up with nothing at this
20352 point. That's an error; the declarator is not optional. */
20353 if (!declarator)
20354 cp_parser_error (parser, "expected declarator");
20355 else if (open_paren)
20356 {
20357 /* Record overly parenthesized declarator so we can give a
20358 diagnostic about confusing decl/expr disambiguation. */
20359 if (declarator->kind == cdk_array)
20360 {
20361 /* If the open and close parens are on different lines, this
20362 is probably a formatting thing, so ignore. */
20363 expanded_location open = expand_location (open_paren->location);
20364 expanded_location close = expand_location (close_paren->location);
20365 if (open.line != close.line || open.file != close.file)
20366 open_paren = NULL;
20367 }
20368 if (open_paren)
20369 declarator->parenthesized = open_paren->location;
20370 }
20371
20372 /* If we entered a scope, we must exit it now. */
20373 if (pushed_scope)
20374 pop_scope (pushed_scope);
20375
20376 parser->default_arg_ok_p = saved_default_arg_ok_p;
20377 parser->in_declarator_p = saved_in_declarator_p;
20378
20379 return declarator;
20380 }
20381
20382 /* Parse a ptr-operator.
20383
20384 ptr-operator:
20385 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20386 * cv-qualifier-seq [opt]
20387 &
20388 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20389 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20390
20391 GNU Extension:
20392
20393 ptr-operator:
20394 & cv-qualifier-seq [opt]
20395
20396 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20397 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20398 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20399 filled in with the TYPE containing the member. *CV_QUALS is
20400 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20401 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20402 Note that the tree codes returned by this function have nothing
20403 to do with the types of trees that will be eventually be created
20404 to represent the pointer or reference type being parsed. They are
20405 just constants with suggestive names. */
20406 static enum tree_code
20407 cp_parser_ptr_operator (cp_parser* parser,
20408 tree* type,
20409 cp_cv_quals *cv_quals,
20410 tree *attributes)
20411 {
20412 enum tree_code code = ERROR_MARK;
20413 cp_token *token;
20414 tree attrs = NULL_TREE;
20415
20416 /* Assume that it's not a pointer-to-member. */
20417 *type = NULL_TREE;
20418 /* And that there are no cv-qualifiers. */
20419 *cv_quals = TYPE_UNQUALIFIED;
20420
20421 /* Peek at the next token. */
20422 token = cp_lexer_peek_token (parser->lexer);
20423
20424 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20425 if (token->type == CPP_MULT)
20426 code = INDIRECT_REF;
20427 else if (token->type == CPP_AND)
20428 code = ADDR_EXPR;
20429 else if ((cxx_dialect != cxx98) &&
20430 token->type == CPP_AND_AND) /* C++0x only */
20431 code = NON_LVALUE_EXPR;
20432
20433 if (code != ERROR_MARK)
20434 {
20435 /* Consume the `*', `&' or `&&'. */
20436 cp_lexer_consume_token (parser->lexer);
20437
20438 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20439 `&', if we are allowing GNU extensions. (The only qualifier
20440 that can legally appear after `&' is `restrict', but that is
20441 enforced during semantic analysis. */
20442 if (code == INDIRECT_REF
20443 || cp_parser_allow_gnu_extensions_p (parser))
20444 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20445
20446 attrs = cp_parser_std_attribute_spec_seq (parser);
20447 if (attributes != NULL)
20448 *attributes = attrs;
20449 }
20450 else
20451 {
20452 /* Try the pointer-to-member case. */
20453 cp_parser_parse_tentatively (parser);
20454 /* Look for the optional `::' operator. */
20455 cp_parser_global_scope_opt (parser,
20456 /*current_scope_valid_p=*/false);
20457 /* Look for the nested-name specifier. */
20458 token = cp_lexer_peek_token (parser->lexer);
20459 cp_parser_nested_name_specifier (parser,
20460 /*typename_keyword_p=*/false,
20461 /*check_dependency_p=*/true,
20462 /*type_p=*/false,
20463 /*is_declaration=*/false);
20464 /* If we found it, and the next token is a `*', then we are
20465 indeed looking at a pointer-to-member operator. */
20466 if (!cp_parser_error_occurred (parser)
20467 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20468 {
20469 /* Indicate that the `*' operator was used. */
20470 code = INDIRECT_REF;
20471
20472 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20473 error_at (token->location, "%qD is a namespace", parser->scope);
20474 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20475 error_at (token->location, "cannot form pointer to member of "
20476 "non-class %q#T", parser->scope);
20477 else
20478 {
20479 /* The type of which the member is a member is given by the
20480 current SCOPE. */
20481 *type = parser->scope;
20482 /* The next name will not be qualified. */
20483 parser->scope = NULL_TREE;
20484 parser->qualifying_scope = NULL_TREE;
20485 parser->object_scope = NULL_TREE;
20486 /* Look for optional c++11 attributes. */
20487 attrs = cp_parser_std_attribute_spec_seq (parser);
20488 if (attributes != NULL)
20489 *attributes = attrs;
20490 /* Look for the optional cv-qualifier-seq. */
20491 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20492 }
20493 }
20494 /* If that didn't work we don't have a ptr-operator. */
20495 if (!cp_parser_parse_definitely (parser))
20496 cp_parser_error (parser, "expected ptr-operator");
20497 }
20498
20499 return code;
20500 }
20501
20502 /* Parse an (optional) cv-qualifier-seq.
20503
20504 cv-qualifier-seq:
20505 cv-qualifier cv-qualifier-seq [opt]
20506
20507 cv-qualifier:
20508 const
20509 volatile
20510
20511 GNU Extension:
20512
20513 cv-qualifier:
20514 __restrict__
20515
20516 Returns a bitmask representing the cv-qualifiers. */
20517
20518 static cp_cv_quals
20519 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20520 {
20521 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20522
20523 while (true)
20524 {
20525 cp_token *token;
20526 cp_cv_quals cv_qualifier;
20527
20528 /* Peek at the next token. */
20529 token = cp_lexer_peek_token (parser->lexer);
20530 /* See if it's a cv-qualifier. */
20531 switch (token->keyword)
20532 {
20533 case RID_CONST:
20534 cv_qualifier = TYPE_QUAL_CONST;
20535 break;
20536
20537 case RID_VOLATILE:
20538 cv_qualifier = TYPE_QUAL_VOLATILE;
20539 break;
20540
20541 case RID_RESTRICT:
20542 cv_qualifier = TYPE_QUAL_RESTRICT;
20543 break;
20544
20545 default:
20546 cv_qualifier = TYPE_UNQUALIFIED;
20547 break;
20548 }
20549
20550 if (!cv_qualifier)
20551 break;
20552
20553 if (cv_quals & cv_qualifier)
20554 {
20555 gcc_rich_location richloc (token->location);
20556 richloc.add_fixit_remove ();
20557 error_at (&richloc, "duplicate cv-qualifier");
20558 cp_lexer_purge_token (parser->lexer);
20559 }
20560 else
20561 {
20562 cp_lexer_consume_token (parser->lexer);
20563 cv_quals |= cv_qualifier;
20564 }
20565 }
20566
20567 return cv_quals;
20568 }
20569
20570 /* Parse an (optional) ref-qualifier
20571
20572 ref-qualifier:
20573 &
20574 &&
20575
20576 Returns cp_ref_qualifier representing ref-qualifier. */
20577
20578 static cp_ref_qualifier
20579 cp_parser_ref_qualifier_opt (cp_parser* parser)
20580 {
20581 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20582
20583 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20584 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20585 return ref_qual;
20586
20587 while (true)
20588 {
20589 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20590 cp_token *token = cp_lexer_peek_token (parser->lexer);
20591
20592 switch (token->type)
20593 {
20594 case CPP_AND:
20595 curr_ref_qual = REF_QUAL_LVALUE;
20596 break;
20597
20598 case CPP_AND_AND:
20599 curr_ref_qual = REF_QUAL_RVALUE;
20600 break;
20601
20602 default:
20603 curr_ref_qual = REF_QUAL_NONE;
20604 break;
20605 }
20606
20607 if (!curr_ref_qual)
20608 break;
20609 else if (ref_qual)
20610 {
20611 error_at (token->location, "multiple ref-qualifiers");
20612 cp_lexer_purge_token (parser->lexer);
20613 }
20614 else
20615 {
20616 ref_qual = curr_ref_qual;
20617 cp_lexer_consume_token (parser->lexer);
20618 }
20619 }
20620
20621 return ref_qual;
20622 }
20623
20624 /* Parse an optional tx-qualifier.
20625
20626 tx-qualifier:
20627 transaction_safe
20628 transaction_safe_dynamic */
20629
20630 static tree
20631 cp_parser_tx_qualifier_opt (cp_parser *parser)
20632 {
20633 cp_token *token = cp_lexer_peek_token (parser->lexer);
20634 if (token->type == CPP_NAME)
20635 {
20636 tree name = token->u.value;
20637 const char *p = IDENTIFIER_POINTER (name);
20638 const int len = strlen ("transaction_safe");
20639 if (!strncmp (p, "transaction_safe", len))
20640 {
20641 p += len;
20642 if (*p == '\0'
20643 || !strcmp (p, "_dynamic"))
20644 {
20645 cp_lexer_consume_token (parser->lexer);
20646 if (!flag_tm)
20647 {
20648 error ("%qE requires %<-fgnu-tm%>", name);
20649 return NULL_TREE;
20650 }
20651 else
20652 return name;
20653 }
20654 }
20655 }
20656 return NULL_TREE;
20657 }
20658
20659 /* Parse an (optional) virt-specifier-seq.
20660
20661 virt-specifier-seq:
20662 virt-specifier virt-specifier-seq [opt]
20663
20664 virt-specifier:
20665 override
20666 final
20667
20668 Returns a bitmask representing the virt-specifiers. */
20669
20670 static cp_virt_specifiers
20671 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20672 {
20673 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20674
20675 while (true)
20676 {
20677 cp_token *token;
20678 cp_virt_specifiers virt_specifier;
20679
20680 /* Peek at the next token. */
20681 token = cp_lexer_peek_token (parser->lexer);
20682 /* See if it's a virt-specifier-qualifier. */
20683 if (token->type != CPP_NAME)
20684 break;
20685 if (id_equal (token->u.value, "override"))
20686 {
20687 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20688 virt_specifier = VIRT_SPEC_OVERRIDE;
20689 }
20690 else if (id_equal (token->u.value, "final"))
20691 {
20692 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20693 virt_specifier = VIRT_SPEC_FINAL;
20694 }
20695 else if (id_equal (token->u.value, "__final"))
20696 {
20697 virt_specifier = VIRT_SPEC_FINAL;
20698 }
20699 else
20700 break;
20701
20702 if (virt_specifiers & virt_specifier)
20703 {
20704 gcc_rich_location richloc (token->location);
20705 richloc.add_fixit_remove ();
20706 error_at (&richloc, "duplicate virt-specifier");
20707 cp_lexer_purge_token (parser->lexer);
20708 }
20709 else
20710 {
20711 cp_lexer_consume_token (parser->lexer);
20712 virt_specifiers |= virt_specifier;
20713 }
20714 }
20715 return virt_specifiers;
20716 }
20717
20718 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20719 is in scope even though it isn't real. */
20720
20721 void
20722 inject_this_parameter (tree ctype, cp_cv_quals quals)
20723 {
20724 tree this_parm;
20725
20726 if (current_class_ptr)
20727 {
20728 /* We don't clear this between NSDMIs. Is it already what we want? */
20729 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20730 if (DECL_P (current_class_ptr)
20731 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20732 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20733 && cp_type_quals (type) == quals)
20734 return;
20735 }
20736
20737 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20738 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20739 current_class_ptr = NULL_TREE;
20740 current_class_ref
20741 = cp_build_fold_indirect_ref (this_parm);
20742 current_class_ptr = this_parm;
20743 }
20744
20745 /* Return true iff our current scope is a non-static data member
20746 initializer. */
20747
20748 bool
20749 parsing_nsdmi (void)
20750 {
20751 /* We recognize NSDMI context by the context-less 'this' pointer set up
20752 by the function above. */
20753 if (current_class_ptr
20754 && TREE_CODE (current_class_ptr) == PARM_DECL
20755 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20756 return true;
20757 return false;
20758 }
20759
20760 /* Parse a late-specified return type, if any. This is not a separate
20761 non-terminal, but part of a function declarator, which looks like
20762
20763 -> trailing-type-specifier-seq abstract-declarator(opt)
20764
20765 Returns the type indicated by the type-id.
20766
20767 In addition to this, parse any queued up #pragma omp declare simd
20768 clauses, and #pragma acc routine clauses.
20769
20770 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20771 function. */
20772
20773 static tree
20774 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20775 tree& requires_clause, cp_cv_quals quals)
20776 {
20777 cp_token *token;
20778 tree type = NULL_TREE;
20779 bool declare_simd_p = (parser->omp_declare_simd
20780 && declarator
20781 && declarator->kind == cdk_id);
20782
20783 bool oacc_routine_p = (parser->oacc_routine
20784 && declarator
20785 && declarator->kind == cdk_id);
20786
20787 /* Peek at the next token. */
20788 token = cp_lexer_peek_token (parser->lexer);
20789 /* A late-specified return type is indicated by an initial '->'. */
20790 if (token->type != CPP_DEREF
20791 && token->keyword != RID_REQUIRES
20792 && !(token->type == CPP_NAME
20793 && token->u.value == ridpointers[RID_REQUIRES])
20794 && !(declare_simd_p || oacc_routine_p))
20795 return NULL_TREE;
20796
20797 tree save_ccp = current_class_ptr;
20798 tree save_ccr = current_class_ref;
20799 if (quals >= 0)
20800 {
20801 /* DR 1207: 'this' is in scope in the trailing return type. */
20802 inject_this_parameter (current_class_type, quals);
20803 }
20804
20805 if (token->type == CPP_DEREF)
20806 {
20807 /* Consume the ->. */
20808 cp_lexer_consume_token (parser->lexer);
20809
20810 type = cp_parser_trailing_type_id (parser);
20811 }
20812
20813 /* Function declarations may be followed by a trailing
20814 requires-clause. */
20815 requires_clause = cp_parser_requires_clause_opt (parser);
20816
20817 if (declare_simd_p)
20818 declarator->attributes
20819 = cp_parser_late_parsing_omp_declare_simd (parser,
20820 declarator->attributes);
20821 if (oacc_routine_p)
20822 declarator->attributes
20823 = cp_parser_late_parsing_oacc_routine (parser,
20824 declarator->attributes);
20825
20826 if (quals >= 0)
20827 {
20828 current_class_ptr = save_ccp;
20829 current_class_ref = save_ccr;
20830 }
20831
20832 return type;
20833 }
20834
20835 /* Parse a declarator-id.
20836
20837 declarator-id:
20838 id-expression
20839 :: [opt] nested-name-specifier [opt] type-name
20840
20841 In the `id-expression' case, the value returned is as for
20842 cp_parser_id_expression if the id-expression was an unqualified-id.
20843 If the id-expression was a qualified-id, then a SCOPE_REF is
20844 returned. The first operand is the scope (either a NAMESPACE_DECL
20845 or TREE_TYPE), but the second is still just a representation of an
20846 unqualified-id. */
20847
20848 static tree
20849 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20850 {
20851 tree id;
20852 /* The expression must be an id-expression. Assume that qualified
20853 names are the names of types so that:
20854
20855 template <class T>
20856 int S<T>::R::i = 3;
20857
20858 will work; we must treat `S<T>::R' as the name of a type.
20859 Similarly, assume that qualified names are templates, where
20860 required, so that:
20861
20862 template <class T>
20863 int S<T>::R<T>::i = 3;
20864
20865 will work, too. */
20866 id = cp_parser_id_expression (parser,
20867 /*template_keyword_p=*/false,
20868 /*check_dependency_p=*/false,
20869 /*template_p=*/NULL,
20870 /*declarator_p=*/true,
20871 optional_p);
20872 if (id && BASELINK_P (id))
20873 id = BASELINK_FUNCTIONS (id);
20874 return id;
20875 }
20876
20877 /* Parse a type-id.
20878
20879 type-id:
20880 type-specifier-seq abstract-declarator [opt]
20881
20882 Returns the TYPE specified. */
20883
20884 static tree
20885 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20886 bool is_trailing_return)
20887 {
20888 cp_decl_specifier_seq type_specifier_seq;
20889 cp_declarator *abstract_declarator;
20890
20891 /* Parse the type-specifier-seq. */
20892 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20893 is_trailing_return,
20894 &type_specifier_seq);
20895 if (is_template_arg && type_specifier_seq.type
20896 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20897 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20898 /* A bare template name as a template argument is a template template
20899 argument, not a placeholder, so fail parsing it as a type argument. */
20900 {
20901 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20902 cp_parser_simulate_error (parser);
20903 return error_mark_node;
20904 }
20905 if (type_specifier_seq.type == error_mark_node)
20906 return error_mark_node;
20907
20908 /* There might or might not be an abstract declarator. */
20909 cp_parser_parse_tentatively (parser);
20910 /* Look for the declarator. */
20911 abstract_declarator
20912 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20913 /*parenthesized_p=*/NULL,
20914 /*member_p=*/false,
20915 /*friend_p=*/false);
20916 /* Check to see if there really was a declarator. */
20917 if (!cp_parser_parse_definitely (parser))
20918 abstract_declarator = NULL;
20919
20920 if (type_specifier_seq.type
20921 /* The concepts TS allows 'auto' as a type-id. */
20922 && (!flag_concepts || parser->in_type_id_in_expr_p)
20923 /* None of the valid uses of 'auto' in C++14 involve the type-id
20924 nonterminal, but it is valid in a trailing-return-type. */
20925 && !(cxx_dialect >= cxx14 && is_trailing_return))
20926 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20927 {
20928 /* A type-id with type 'auto' is only ok if the abstract declarator
20929 is a function declarator with a late-specified return type.
20930
20931 A type-id with 'auto' is also valid in a trailing-return-type
20932 in a compound-requirement. */
20933 if (abstract_declarator
20934 && abstract_declarator->kind == cdk_function
20935 && abstract_declarator->u.function.late_return_type)
20936 /* OK */;
20937 else if (parser->in_result_type_constraint_p)
20938 /* OK */;
20939 else
20940 {
20941 location_t loc = type_specifier_seq.locations[ds_type_spec];
20942 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20943 {
20944 error_at (loc, "missing template arguments after %qT",
20945 auto_node);
20946 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20947 tmpl);
20948 }
20949 else
20950 error_at (loc, "invalid use of %qT", auto_node);
20951 return error_mark_node;
20952 }
20953 }
20954
20955 return groktypename (&type_specifier_seq, abstract_declarator,
20956 is_template_arg);
20957 }
20958
20959 static tree
20960 cp_parser_type_id (cp_parser *parser)
20961 {
20962 return cp_parser_type_id_1 (parser, false, false);
20963 }
20964
20965 static tree
20966 cp_parser_template_type_arg (cp_parser *parser)
20967 {
20968 tree r;
20969 const char *saved_message = parser->type_definition_forbidden_message;
20970 parser->type_definition_forbidden_message
20971 = G_("types may not be defined in template arguments");
20972 r = cp_parser_type_id_1 (parser, true, false);
20973 parser->type_definition_forbidden_message = saved_message;
20974 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20975 {
20976 error ("invalid use of %<auto%> in template argument");
20977 r = error_mark_node;
20978 }
20979 return r;
20980 }
20981
20982 static tree
20983 cp_parser_trailing_type_id (cp_parser *parser)
20984 {
20985 return cp_parser_type_id_1 (parser, false, true);
20986 }
20987
20988 /* Parse a type-specifier-seq.
20989
20990 type-specifier-seq:
20991 type-specifier type-specifier-seq [opt]
20992
20993 GNU extension:
20994
20995 type-specifier-seq:
20996 attributes type-specifier-seq [opt]
20997
20998 If IS_DECLARATION is true, we are at the start of a "condition" or
20999 exception-declaration, so we might be followed by a declarator-id.
21000
21001 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21002 i.e. we've just seen "->".
21003
21004 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21005
21006 static void
21007 cp_parser_type_specifier_seq (cp_parser* parser,
21008 bool is_declaration,
21009 bool is_trailing_return,
21010 cp_decl_specifier_seq *type_specifier_seq)
21011 {
21012 bool seen_type_specifier = false;
21013 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21014 cp_token *start_token = NULL;
21015
21016 /* Clear the TYPE_SPECIFIER_SEQ. */
21017 clear_decl_specs (type_specifier_seq);
21018
21019 /* In the context of a trailing return type, enum E { } is an
21020 elaborated-type-specifier followed by a function-body, not an
21021 enum-specifier. */
21022 if (is_trailing_return)
21023 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21024
21025 /* Parse the type-specifiers and attributes. */
21026 while (true)
21027 {
21028 tree type_specifier;
21029 bool is_cv_qualifier;
21030
21031 /* Check for attributes first. */
21032 if (cp_next_tokens_can_be_attribute_p (parser))
21033 {
21034 type_specifier_seq->attributes =
21035 chainon (type_specifier_seq->attributes,
21036 cp_parser_attributes_opt (parser));
21037 continue;
21038 }
21039
21040 /* record the token of the beginning of the type specifier seq,
21041 for error reporting purposes*/
21042 if (!start_token)
21043 start_token = cp_lexer_peek_token (parser->lexer);
21044
21045 /* Look for the type-specifier. */
21046 type_specifier = cp_parser_type_specifier (parser,
21047 flags,
21048 type_specifier_seq,
21049 /*is_declaration=*/false,
21050 NULL,
21051 &is_cv_qualifier);
21052 if (!type_specifier)
21053 {
21054 /* If the first type-specifier could not be found, this is not a
21055 type-specifier-seq at all. */
21056 if (!seen_type_specifier)
21057 {
21058 /* Set in_declarator_p to avoid skipping to the semicolon. */
21059 int in_decl = parser->in_declarator_p;
21060 parser->in_declarator_p = true;
21061
21062 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21063 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21064 cp_parser_error (parser, "expected type-specifier");
21065
21066 parser->in_declarator_p = in_decl;
21067
21068 type_specifier_seq->type = error_mark_node;
21069 return;
21070 }
21071 /* If subsequent type-specifiers could not be found, the
21072 type-specifier-seq is complete. */
21073 break;
21074 }
21075
21076 seen_type_specifier = true;
21077 /* The standard says that a condition can be:
21078
21079 type-specifier-seq declarator = assignment-expression
21080
21081 However, given:
21082
21083 struct S {};
21084 if (int S = ...)
21085
21086 we should treat the "S" as a declarator, not as a
21087 type-specifier. The standard doesn't say that explicitly for
21088 type-specifier-seq, but it does say that for
21089 decl-specifier-seq in an ordinary declaration. Perhaps it
21090 would be clearer just to allow a decl-specifier-seq here, and
21091 then add a semantic restriction that if any decl-specifiers
21092 that are not type-specifiers appear, the program is invalid. */
21093 if (is_declaration && !is_cv_qualifier)
21094 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21095 }
21096 }
21097
21098 /* Return whether the function currently being declared has an associated
21099 template parameter list. */
21100
21101 static bool
21102 function_being_declared_is_template_p (cp_parser* parser)
21103 {
21104 if (!current_template_parms || processing_template_parmlist)
21105 return false;
21106
21107 if (parser->implicit_template_scope)
21108 return true;
21109
21110 if (at_class_scope_p ()
21111 && TYPE_BEING_DEFINED (current_class_type))
21112 return parser->num_template_parameter_lists != 0;
21113
21114 return ((int) parser->num_template_parameter_lists > template_class_depth
21115 (current_class_type));
21116 }
21117
21118 /* Parse a parameter-declaration-clause.
21119
21120 parameter-declaration-clause:
21121 parameter-declaration-list [opt] ... [opt]
21122 parameter-declaration-list , ...
21123
21124 Returns a representation for the parameter declarations. A return
21125 value of NULL indicates a parameter-declaration-clause consisting
21126 only of an ellipsis. */
21127
21128 static tree
21129 cp_parser_parameter_declaration_clause (cp_parser* parser)
21130 {
21131 tree parameters;
21132 cp_token *token;
21133 bool ellipsis_p;
21134 bool is_error;
21135
21136 struct cleanup {
21137 cp_parser* parser;
21138 int auto_is_implicit_function_template_parm_p;
21139 ~cleanup() {
21140 parser->auto_is_implicit_function_template_parm_p
21141 = auto_is_implicit_function_template_parm_p;
21142 }
21143 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21144
21145 (void) cleanup;
21146
21147 if (!processing_specialization
21148 && !processing_template_parmlist
21149 && !processing_explicit_instantiation)
21150 if (!current_function_decl
21151 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21152 parser->auto_is_implicit_function_template_parm_p = true;
21153
21154 /* Peek at the next token. */
21155 token = cp_lexer_peek_token (parser->lexer);
21156 /* Check for trivial parameter-declaration-clauses. */
21157 if (token->type == CPP_ELLIPSIS)
21158 {
21159 /* Consume the `...' token. */
21160 cp_lexer_consume_token (parser->lexer);
21161 return NULL_TREE;
21162 }
21163 else if (token->type == CPP_CLOSE_PAREN)
21164 /* There are no parameters. */
21165 {
21166 #ifndef NO_IMPLICIT_EXTERN_C
21167 if (in_system_header_at (input_location)
21168 && current_class_type == NULL
21169 && current_lang_name == lang_name_c)
21170 return NULL_TREE;
21171 else
21172 #endif
21173 return void_list_node;
21174 }
21175 /* Check for `(void)', too, which is a special case. */
21176 else if (token->keyword == RID_VOID
21177 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21178 == CPP_CLOSE_PAREN))
21179 {
21180 /* Consume the `void' token. */
21181 cp_lexer_consume_token (parser->lexer);
21182 /* There are no parameters. */
21183 return void_list_node;
21184 }
21185
21186 /* Parse the parameter-declaration-list. */
21187 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21188 /* If a parse error occurred while parsing the
21189 parameter-declaration-list, then the entire
21190 parameter-declaration-clause is erroneous. */
21191 if (is_error)
21192 return NULL;
21193
21194 /* Peek at the next token. */
21195 token = cp_lexer_peek_token (parser->lexer);
21196 /* If it's a `,', the clause should terminate with an ellipsis. */
21197 if (token->type == CPP_COMMA)
21198 {
21199 /* Consume the `,'. */
21200 cp_lexer_consume_token (parser->lexer);
21201 /* Expect an ellipsis. */
21202 ellipsis_p
21203 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21204 }
21205 /* It might also be `...' if the optional trailing `,' was
21206 omitted. */
21207 else if (token->type == CPP_ELLIPSIS)
21208 {
21209 /* Consume the `...' token. */
21210 cp_lexer_consume_token (parser->lexer);
21211 /* And remember that we saw it. */
21212 ellipsis_p = true;
21213 }
21214 else
21215 ellipsis_p = false;
21216
21217 /* Finish the parameter list. */
21218 if (!ellipsis_p)
21219 parameters = chainon (parameters, void_list_node);
21220
21221 return parameters;
21222 }
21223
21224 /* Parse a parameter-declaration-list.
21225
21226 parameter-declaration-list:
21227 parameter-declaration
21228 parameter-declaration-list , parameter-declaration
21229
21230 Returns a representation of the parameter-declaration-list, as for
21231 cp_parser_parameter_declaration_clause. However, the
21232 `void_list_node' is never appended to the list. Upon return,
21233 *IS_ERROR will be true iff an error occurred. */
21234
21235 static tree
21236 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21237 {
21238 tree parameters = NULL_TREE;
21239 tree *tail = &parameters;
21240 bool saved_in_unbraced_linkage_specification_p;
21241 int index = 0;
21242
21243 /* Assume all will go well. */
21244 *is_error = false;
21245 /* The special considerations that apply to a function within an
21246 unbraced linkage specifications do not apply to the parameters
21247 to the function. */
21248 saved_in_unbraced_linkage_specification_p
21249 = parser->in_unbraced_linkage_specification_p;
21250 parser->in_unbraced_linkage_specification_p = false;
21251
21252 /* Look for more parameters. */
21253 while (true)
21254 {
21255 cp_parameter_declarator *parameter;
21256 tree decl = error_mark_node;
21257 bool parenthesized_p = false;
21258 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21259 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21260 (current_template_parms)) : 0);
21261
21262 /* Parse the parameter. */
21263 parameter
21264 = cp_parser_parameter_declaration (parser,
21265 /*template_parm_p=*/false,
21266 &parenthesized_p);
21267
21268 /* We don't know yet if the enclosing context is deprecated, so wait
21269 and warn in grokparms if appropriate. */
21270 deprecated_state = DEPRECATED_SUPPRESS;
21271
21272 if (parameter)
21273 {
21274 /* If a function parameter pack was specified and an implicit template
21275 parameter was introduced during cp_parser_parameter_declaration,
21276 change any implicit parameters introduced into packs. */
21277 if (parser->implicit_template_parms
21278 && parameter->declarator
21279 && parameter->declarator->parameter_pack_p)
21280 {
21281 int latest_template_parm_idx = TREE_VEC_LENGTH
21282 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21283
21284 if (latest_template_parm_idx != template_parm_idx)
21285 parameter->decl_specifiers.type = convert_generic_types_to_packs
21286 (parameter->decl_specifiers.type,
21287 template_parm_idx, latest_template_parm_idx);
21288 }
21289
21290 decl = grokdeclarator (parameter->declarator,
21291 &parameter->decl_specifiers,
21292 PARM,
21293 parameter->default_argument != NULL_TREE,
21294 &parameter->decl_specifiers.attributes);
21295 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21296 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21297 }
21298
21299 deprecated_state = DEPRECATED_NORMAL;
21300
21301 /* If a parse error occurred parsing the parameter declaration,
21302 then the entire parameter-declaration-list is erroneous. */
21303 if (decl == error_mark_node)
21304 {
21305 *is_error = true;
21306 parameters = error_mark_node;
21307 break;
21308 }
21309
21310 if (parameter->decl_specifiers.attributes)
21311 cplus_decl_attributes (&decl,
21312 parameter->decl_specifiers.attributes,
21313 0);
21314 if (DECL_NAME (decl))
21315 decl = pushdecl (decl);
21316
21317 if (decl != error_mark_node)
21318 {
21319 retrofit_lang_decl (decl);
21320 DECL_PARM_INDEX (decl) = ++index;
21321 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21322 }
21323
21324 /* Add the new parameter to the list. */
21325 *tail = build_tree_list (parameter->default_argument, decl);
21326 tail = &TREE_CHAIN (*tail);
21327
21328 /* Peek at the next token. */
21329 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21330 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21331 /* These are for Objective-C++ */
21332 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21333 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21334 /* The parameter-declaration-list is complete. */
21335 break;
21336 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21337 {
21338 cp_token *token;
21339
21340 /* Peek at the next token. */
21341 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21342 /* If it's an ellipsis, then the list is complete. */
21343 if (token->type == CPP_ELLIPSIS)
21344 break;
21345 /* Otherwise, there must be more parameters. Consume the
21346 `,'. */
21347 cp_lexer_consume_token (parser->lexer);
21348 /* When parsing something like:
21349
21350 int i(float f, double d)
21351
21352 we can tell after seeing the declaration for "f" that we
21353 are not looking at an initialization of a variable "i",
21354 but rather at the declaration of a function "i".
21355
21356 Due to the fact that the parsing of template arguments
21357 (as specified to a template-id) requires backtracking we
21358 cannot use this technique when inside a template argument
21359 list. */
21360 if (!parser->in_template_argument_list_p
21361 && !parser->in_type_id_in_expr_p
21362 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21363 /* However, a parameter-declaration of the form
21364 "float(f)" (which is a valid declaration of a
21365 parameter "f") can also be interpreted as an
21366 expression (the conversion of "f" to "float"). */
21367 && !parenthesized_p)
21368 cp_parser_commit_to_tentative_parse (parser);
21369 }
21370 else
21371 {
21372 cp_parser_error (parser, "expected %<,%> or %<...%>");
21373 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21374 cp_parser_skip_to_closing_parenthesis (parser,
21375 /*recovering=*/true,
21376 /*or_comma=*/false,
21377 /*consume_paren=*/false);
21378 break;
21379 }
21380 }
21381
21382 parser->in_unbraced_linkage_specification_p
21383 = saved_in_unbraced_linkage_specification_p;
21384
21385 /* Reset implicit_template_scope if we are about to leave the function
21386 parameter list that introduced it. Note that for out-of-line member
21387 definitions, there will be one or more class scopes before we get to
21388 the template parameter scope. */
21389
21390 if (cp_binding_level *its = parser->implicit_template_scope)
21391 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21392 {
21393 while (maybe_its->kind == sk_class)
21394 maybe_its = maybe_its->level_chain;
21395 if (maybe_its == its)
21396 {
21397 parser->implicit_template_parms = 0;
21398 parser->implicit_template_scope = 0;
21399 }
21400 }
21401
21402 return parameters;
21403 }
21404
21405 /* Parse a parameter declaration.
21406
21407 parameter-declaration:
21408 decl-specifier-seq ... [opt] declarator
21409 decl-specifier-seq declarator = assignment-expression
21410 decl-specifier-seq ... [opt] abstract-declarator [opt]
21411 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21412
21413 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21414 declares a template parameter. (In that case, a non-nested `>'
21415 token encountered during the parsing of the assignment-expression
21416 is not interpreted as a greater-than operator.)
21417
21418 Returns a representation of the parameter, or NULL if an error
21419 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21420 true iff the declarator is of the form "(p)". */
21421
21422 static cp_parameter_declarator *
21423 cp_parser_parameter_declaration (cp_parser *parser,
21424 bool template_parm_p,
21425 bool *parenthesized_p)
21426 {
21427 int declares_class_or_enum;
21428 cp_decl_specifier_seq decl_specifiers;
21429 cp_declarator *declarator;
21430 tree default_argument;
21431 cp_token *token = NULL, *declarator_token_start = NULL;
21432 const char *saved_message;
21433 bool template_parameter_pack_p = false;
21434
21435 /* In a template parameter, `>' is not an operator.
21436
21437 [temp.param]
21438
21439 When parsing a default template-argument for a non-type
21440 template-parameter, the first non-nested `>' is taken as the end
21441 of the template parameter-list rather than a greater-than
21442 operator. */
21443
21444 /* Type definitions may not appear in parameter types. */
21445 saved_message = parser->type_definition_forbidden_message;
21446 parser->type_definition_forbidden_message
21447 = G_("types may not be defined in parameter types");
21448
21449 /* Parse the declaration-specifiers. */
21450 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21451 cp_parser_decl_specifier_seq (parser,
21452 CP_PARSER_FLAGS_NONE,
21453 &decl_specifiers,
21454 &declares_class_or_enum);
21455
21456 /* Complain about missing 'typename' or other invalid type names. */
21457 if (!decl_specifiers.any_type_specifiers_p
21458 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21459 decl_specifiers.type = error_mark_node;
21460
21461 /* If an error occurred, there's no reason to attempt to parse the
21462 rest of the declaration. */
21463 if (cp_parser_error_occurred (parser))
21464 {
21465 parser->type_definition_forbidden_message = saved_message;
21466 return NULL;
21467 }
21468
21469 /* Peek at the next token. */
21470 token = cp_lexer_peek_token (parser->lexer);
21471
21472 /* If the next token is a `)', `,', `=', `>', or `...', then there
21473 is no declarator. However, when variadic templates are enabled,
21474 there may be a declarator following `...'. */
21475 if (token->type == CPP_CLOSE_PAREN
21476 || token->type == CPP_COMMA
21477 || token->type == CPP_EQ
21478 || token->type == CPP_GREATER)
21479 {
21480 declarator = NULL;
21481 if (parenthesized_p)
21482 *parenthesized_p = false;
21483 }
21484 /* Otherwise, there should be a declarator. */
21485 else
21486 {
21487 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21488 parser->default_arg_ok_p = false;
21489
21490 /* After seeing a decl-specifier-seq, if the next token is not a
21491 "(", there is no possibility that the code is a valid
21492 expression. Therefore, if parsing tentatively, we commit at
21493 this point. */
21494 if (!parser->in_template_argument_list_p
21495 /* In an expression context, having seen:
21496
21497 (int((char ...
21498
21499 we cannot be sure whether we are looking at a
21500 function-type (taking a "char" as a parameter) or a cast
21501 of some object of type "char" to "int". */
21502 && !parser->in_type_id_in_expr_p
21503 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21504 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21505 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21506 cp_parser_commit_to_tentative_parse (parser);
21507 /* Parse the declarator. */
21508 declarator_token_start = token;
21509 declarator = cp_parser_declarator (parser,
21510 CP_PARSER_DECLARATOR_EITHER,
21511 /*ctor_dtor_or_conv_p=*/NULL,
21512 parenthesized_p,
21513 /*member_p=*/false,
21514 /*friend_p=*/false);
21515 parser->default_arg_ok_p = saved_default_arg_ok_p;
21516 /* After the declarator, allow more attributes. */
21517 decl_specifiers.attributes
21518 = chainon (decl_specifiers.attributes,
21519 cp_parser_attributes_opt (parser));
21520
21521 /* If the declarator is a template parameter pack, remember that and
21522 clear the flag in the declarator itself so we don't get errors
21523 from grokdeclarator. */
21524 if (template_parm_p && declarator && declarator->parameter_pack_p)
21525 {
21526 declarator->parameter_pack_p = false;
21527 template_parameter_pack_p = true;
21528 }
21529 }
21530
21531 /* If the next token is an ellipsis, and we have not seen a declarator
21532 name, and if either the type of the declarator contains parameter
21533 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21534 for, eg, abbreviated integral type names), then we actually have a
21535 parameter pack expansion expression. Otherwise, leave the ellipsis
21536 for a C-style variadic function. */
21537 token = cp_lexer_peek_token (parser->lexer);
21538 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21539 {
21540 tree type = decl_specifiers.type;
21541
21542 if (type && DECL_P (type))
21543 type = TREE_TYPE (type);
21544
21545 if (((type
21546 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21547 && (template_parm_p || uses_parameter_packs (type)))
21548 || (!type && template_parm_p))
21549 && declarator_can_be_parameter_pack (declarator))
21550 {
21551 /* Consume the `...'. */
21552 cp_lexer_consume_token (parser->lexer);
21553 maybe_warn_variadic_templates ();
21554
21555 /* Build a pack expansion type */
21556 if (template_parm_p)
21557 template_parameter_pack_p = true;
21558 else if (declarator)
21559 declarator->parameter_pack_p = true;
21560 else
21561 decl_specifiers.type = make_pack_expansion (type);
21562 }
21563 }
21564
21565 /* The restriction on defining new types applies only to the type
21566 of the parameter, not to the default argument. */
21567 parser->type_definition_forbidden_message = saved_message;
21568
21569 /* If the next token is `=', then process a default argument. */
21570 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21571 {
21572 tree type = decl_specifiers.type;
21573 token = cp_lexer_peek_token (parser->lexer);
21574 /* If we are defining a class, then the tokens that make up the
21575 default argument must be saved and processed later. */
21576 if (!template_parm_p && at_class_scope_p ()
21577 && TYPE_BEING_DEFINED (current_class_type)
21578 && !LAMBDA_TYPE_P (current_class_type))
21579 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21580
21581 // A constrained-type-specifier may declare a type template-parameter.
21582 else if (declares_constrained_type_template_parameter (type))
21583 default_argument
21584 = cp_parser_default_type_template_argument (parser);
21585
21586 // A constrained-type-specifier may declare a template-template-parameter.
21587 else if (declares_constrained_template_template_parameter (type))
21588 default_argument
21589 = cp_parser_default_template_template_argument (parser);
21590
21591 /* Outside of a class definition, we can just parse the
21592 assignment-expression. */
21593 else
21594 default_argument
21595 = cp_parser_default_argument (parser, template_parm_p);
21596
21597 if (!parser->default_arg_ok_p)
21598 {
21599 permerror (token->location,
21600 "default arguments are only "
21601 "permitted for function parameters");
21602 }
21603 else if ((declarator && declarator->parameter_pack_p)
21604 || template_parameter_pack_p
21605 || (decl_specifiers.type
21606 && PACK_EXPANSION_P (decl_specifiers.type)))
21607 {
21608 /* Find the name of the parameter pack. */
21609 cp_declarator *id_declarator = declarator;
21610 while (id_declarator && id_declarator->kind != cdk_id)
21611 id_declarator = id_declarator->declarator;
21612
21613 if (id_declarator && id_declarator->kind == cdk_id)
21614 error_at (declarator_token_start->location,
21615 template_parm_p
21616 ? G_("template parameter pack %qD "
21617 "cannot have a default argument")
21618 : G_("parameter pack %qD cannot have "
21619 "a default argument"),
21620 id_declarator->u.id.unqualified_name);
21621 else
21622 error_at (declarator_token_start->location,
21623 template_parm_p
21624 ? G_("template parameter pack cannot have "
21625 "a default argument")
21626 : G_("parameter pack cannot have a "
21627 "default argument"));
21628
21629 default_argument = NULL_TREE;
21630 }
21631 }
21632 else
21633 default_argument = NULL_TREE;
21634
21635 /* Generate a location for the parameter, ranging from the start of the
21636 initial token to the end of the final token (using input_location for
21637 the latter, set up by cp_lexer_set_source_position_from_token when
21638 consuming tokens).
21639
21640 If we have a identifier, then use it for the caret location, e.g.
21641
21642 extern int callee (int one, int (*two)(int, int), float three);
21643 ~~~~~~^~~~~~~~~~~~~~
21644
21645 otherwise, reuse the start location for the caret location e.g.:
21646
21647 extern int callee (int one, int (*)(int, int), float three);
21648 ^~~~~~~~~~~~~~~~~
21649
21650 */
21651 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21652 ? declarator->id_loc
21653 : decl_spec_token_start->location);
21654 location_t param_loc = make_location (caret_loc,
21655 decl_spec_token_start->location,
21656 input_location);
21657
21658 return make_parameter_declarator (&decl_specifiers,
21659 declarator,
21660 default_argument,
21661 param_loc,
21662 template_parameter_pack_p);
21663 }
21664
21665 /* Parse a default argument and return it.
21666
21667 TEMPLATE_PARM_P is true if this is a default argument for a
21668 non-type template parameter. */
21669 static tree
21670 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21671 {
21672 tree default_argument = NULL_TREE;
21673 bool saved_greater_than_is_operator_p;
21674 bool saved_local_variables_forbidden_p;
21675 bool non_constant_p, is_direct_init;
21676
21677 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21678 set correctly. */
21679 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21680 parser->greater_than_is_operator_p = !template_parm_p;
21681 /* Local variable names (and the `this' keyword) may not
21682 appear in a default argument. */
21683 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21684 parser->local_variables_forbidden_p = true;
21685 /* Parse the assignment-expression. */
21686 if (template_parm_p)
21687 push_deferring_access_checks (dk_no_deferred);
21688 tree saved_class_ptr = NULL_TREE;
21689 tree saved_class_ref = NULL_TREE;
21690 /* The "this" pointer is not valid in a default argument. */
21691 if (cfun)
21692 {
21693 saved_class_ptr = current_class_ptr;
21694 cp_function_chain->x_current_class_ptr = NULL_TREE;
21695 saved_class_ref = current_class_ref;
21696 cp_function_chain->x_current_class_ref = NULL_TREE;
21697 }
21698 default_argument
21699 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21700 /* Restore the "this" pointer. */
21701 if (cfun)
21702 {
21703 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21704 cp_function_chain->x_current_class_ref = saved_class_ref;
21705 }
21706 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21707 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21708 if (template_parm_p)
21709 pop_deferring_access_checks ();
21710 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21711 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21712
21713 return default_argument;
21714 }
21715
21716 /* Parse a function-body.
21717
21718 function-body:
21719 compound_statement */
21720
21721 static void
21722 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21723 {
21724 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21725 ? BCS_TRY_BLOCK : BCS_NORMAL),
21726 true);
21727 }
21728
21729 /* Parse a ctor-initializer-opt followed by a function-body. Return
21730 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21731 is true we are parsing a function-try-block. */
21732
21733 static void
21734 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21735 bool in_function_try_block)
21736 {
21737 tree body, list;
21738 const bool check_body_p =
21739 DECL_CONSTRUCTOR_P (current_function_decl)
21740 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21741 tree last = NULL;
21742
21743 /* Begin the function body. */
21744 body = begin_function_body ();
21745 /* Parse the optional ctor-initializer. */
21746 cp_parser_ctor_initializer_opt (parser);
21747
21748 /* If we're parsing a constexpr constructor definition, we need
21749 to check that the constructor body is indeed empty. However,
21750 before we get to cp_parser_function_body lot of junk has been
21751 generated, so we can't just check that we have an empty block.
21752 Rather we take a snapshot of the outermost block, and check whether
21753 cp_parser_function_body changed its state. */
21754 if (check_body_p)
21755 {
21756 list = cur_stmt_list;
21757 if (STATEMENT_LIST_TAIL (list))
21758 last = STATEMENT_LIST_TAIL (list)->stmt;
21759 }
21760 /* Parse the function-body. */
21761 cp_parser_function_body (parser, in_function_try_block);
21762 if (check_body_p)
21763 check_constexpr_ctor_body (last, list, /*complain=*/true);
21764 /* Finish the function body. */
21765 finish_function_body (body);
21766 }
21767
21768 /* Parse an initializer.
21769
21770 initializer:
21771 = initializer-clause
21772 ( expression-list )
21773
21774 Returns an expression representing the initializer. If no
21775 initializer is present, NULL_TREE is returned.
21776
21777 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21778 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21779 set to TRUE if there is no initializer present. If there is an
21780 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21781 is set to true; otherwise it is set to false. */
21782
21783 static tree
21784 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21785 bool* non_constant_p)
21786 {
21787 cp_token *token;
21788 tree init;
21789
21790 /* Peek at the next token. */
21791 token = cp_lexer_peek_token (parser->lexer);
21792
21793 /* Let our caller know whether or not this initializer was
21794 parenthesized. */
21795 *is_direct_init = (token->type != CPP_EQ);
21796 /* Assume that the initializer is constant. */
21797 *non_constant_p = false;
21798
21799 if (token->type == CPP_EQ)
21800 {
21801 /* Consume the `='. */
21802 cp_lexer_consume_token (parser->lexer);
21803 /* Parse the initializer-clause. */
21804 init = cp_parser_initializer_clause (parser, non_constant_p);
21805 }
21806 else if (token->type == CPP_OPEN_PAREN)
21807 {
21808 vec<tree, va_gc> *vec;
21809 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21810 /*cast_p=*/false,
21811 /*allow_expansion_p=*/true,
21812 non_constant_p);
21813 if (vec == NULL)
21814 return error_mark_node;
21815 init = build_tree_list_vec (vec);
21816 release_tree_vector (vec);
21817 }
21818 else if (token->type == CPP_OPEN_BRACE)
21819 {
21820 cp_lexer_set_source_position (parser->lexer);
21821 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21822 init = cp_parser_braced_list (parser, non_constant_p);
21823 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21824 }
21825 else
21826 {
21827 /* Anything else is an error. */
21828 cp_parser_error (parser, "expected initializer");
21829 init = error_mark_node;
21830 }
21831
21832 if (check_for_bare_parameter_packs (init))
21833 init = error_mark_node;
21834
21835 return init;
21836 }
21837
21838 /* Parse an initializer-clause.
21839
21840 initializer-clause:
21841 assignment-expression
21842 braced-init-list
21843
21844 Returns an expression representing the initializer.
21845
21846 If the `assignment-expression' production is used the value
21847 returned is simply a representation for the expression.
21848
21849 Otherwise, calls cp_parser_braced_list. */
21850
21851 static cp_expr
21852 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21853 {
21854 cp_expr initializer;
21855
21856 /* Assume the expression is constant. */
21857 *non_constant_p = false;
21858
21859 /* If it is not a `{', then we are looking at an
21860 assignment-expression. */
21861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21862 {
21863 initializer
21864 = cp_parser_constant_expression (parser,
21865 /*allow_non_constant_p=*/true,
21866 non_constant_p);
21867 }
21868 else
21869 initializer = cp_parser_braced_list (parser, non_constant_p);
21870
21871 return initializer;
21872 }
21873
21874 /* Parse a brace-enclosed initializer list.
21875
21876 braced-init-list:
21877 { initializer-list , [opt] }
21878 { designated-initializer-list , [opt] }
21879 { }
21880
21881 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21882 the elements of the initializer-list (or NULL, if the last
21883 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21884 NULL_TREE. There is no way to detect whether or not the optional
21885 trailing `,' was provided. NON_CONSTANT_P is as for
21886 cp_parser_initializer. */
21887
21888 static cp_expr
21889 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21890 {
21891 tree initializer;
21892 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21893
21894 /* Consume the `{' token. */
21895 matching_braces braces;
21896 braces.consume_open (parser);
21897 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21898 initializer = make_node (CONSTRUCTOR);
21899 /* If it's not a `}', then there is a non-trivial initializer. */
21900 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21901 {
21902 /* Parse the initializer list. */
21903 CONSTRUCTOR_ELTS (initializer)
21904 = cp_parser_initializer_list (parser, non_constant_p);
21905 /* A trailing `,' token is allowed. */
21906 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21907 cp_lexer_consume_token (parser->lexer);
21908 }
21909 else
21910 *non_constant_p = false;
21911 /* Now, there should be a trailing `}'. */
21912 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21913 braces.require_close (parser);
21914 TREE_TYPE (initializer) = init_list_type_node;
21915
21916 cp_expr result (initializer);
21917 /* Build a location of the form:
21918 { ... }
21919 ^~~~~~~
21920 with caret==start at the open brace, finish at the close brace. */
21921 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21922 result.set_location (combined_loc);
21923 return result;
21924 }
21925
21926 /* Consume tokens up to, and including, the next non-nested closing `]'.
21927 Returns true iff we found a closing `]'. */
21928
21929 static bool
21930 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21931 {
21932 unsigned square_depth = 0;
21933
21934 while (true)
21935 {
21936 cp_token * token = cp_lexer_peek_token (parser->lexer);
21937
21938 switch (token->type)
21939 {
21940 case CPP_EOF:
21941 case CPP_PRAGMA_EOL:
21942 /* If we've run out of tokens, then there is no closing `]'. */
21943 return false;
21944
21945 case CPP_OPEN_SQUARE:
21946 ++square_depth;
21947 break;
21948
21949 case CPP_CLOSE_SQUARE:
21950 if (!square_depth--)
21951 {
21952 cp_lexer_consume_token (parser->lexer);
21953 return true;
21954 }
21955 break;
21956
21957 default:
21958 break;
21959 }
21960
21961 /* Consume the token. */
21962 cp_lexer_consume_token (parser->lexer);
21963 }
21964 }
21965
21966 /* Return true if we are looking at an array-designator, false otherwise. */
21967
21968 static bool
21969 cp_parser_array_designator_p (cp_parser *parser)
21970 {
21971 /* Consume the `['. */
21972 cp_lexer_consume_token (parser->lexer);
21973
21974 cp_lexer_save_tokens (parser->lexer);
21975
21976 /* Skip tokens until the next token is a closing square bracket.
21977 If we find the closing `]', and the next token is a `=', then
21978 we are looking at an array designator. */
21979 bool array_designator_p
21980 = (cp_parser_skip_to_closing_square_bracket (parser)
21981 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21982
21983 /* Roll back the tokens we skipped. */
21984 cp_lexer_rollback_tokens (parser->lexer);
21985
21986 return array_designator_p;
21987 }
21988
21989 /* Parse an initializer-list.
21990
21991 initializer-list:
21992 initializer-clause ... [opt]
21993 initializer-list , initializer-clause ... [opt]
21994
21995 C++2A Extension:
21996
21997 designated-initializer-list:
21998 designated-initializer-clause
21999 designated-initializer-list , designated-initializer-clause
22000
22001 designated-initializer-clause:
22002 designator brace-or-equal-initializer
22003
22004 designator:
22005 . identifier
22006
22007 GNU Extension:
22008
22009 initializer-list:
22010 designation initializer-clause ...[opt]
22011 initializer-list , designation initializer-clause ...[opt]
22012
22013 designation:
22014 . identifier =
22015 identifier :
22016 [ constant-expression ] =
22017
22018 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22019 for the initializer. If the INDEX of the elt is non-NULL, it is the
22020 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22021 as for cp_parser_initializer. */
22022
22023 static vec<constructor_elt, va_gc> *
22024 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22025 {
22026 vec<constructor_elt, va_gc> *v = NULL;
22027 bool first_p = true;
22028 tree first_designator = NULL_TREE;
22029
22030 /* Assume all of the expressions are constant. */
22031 *non_constant_p = false;
22032
22033 /* Parse the rest of the list. */
22034 while (true)
22035 {
22036 cp_token *token;
22037 tree designator;
22038 tree initializer;
22039 bool clause_non_constant_p;
22040 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22041
22042 /* Handle the C++2A syntax, '. id ='. */
22043 if ((cxx_dialect >= cxx2a
22044 || cp_parser_allow_gnu_extensions_p (parser))
22045 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22046 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22047 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22048 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22049 == CPP_OPEN_BRACE)))
22050 {
22051 if (cxx_dialect < cxx2a)
22052 pedwarn (loc, OPT_Wpedantic,
22053 "C++ designated initializers only available with "
22054 "-std=c++2a or -std=gnu++2a");
22055 /* Consume the `.'. */
22056 cp_lexer_consume_token (parser->lexer);
22057 /* Consume the identifier. */
22058 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22059 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22060 /* Consume the `='. */
22061 cp_lexer_consume_token (parser->lexer);
22062 }
22063 /* Also, if the next token is an identifier and the following one is a
22064 colon, we are looking at the GNU designated-initializer
22065 syntax. */
22066 else if (cp_parser_allow_gnu_extensions_p (parser)
22067 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22068 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22069 == CPP_COLON))
22070 {
22071 /* Warn the user that they are using an extension. */
22072 pedwarn (loc, OPT_Wpedantic,
22073 "ISO C++ does not allow GNU designated initializers");
22074 /* Consume the identifier. */
22075 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22076 /* Consume the `:'. */
22077 cp_lexer_consume_token (parser->lexer);
22078 }
22079 /* Also handle C99 array designators, '[ const ] ='. */
22080 else if (cp_parser_allow_gnu_extensions_p (parser)
22081 && !c_dialect_objc ()
22082 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22083 {
22084 /* In C++11, [ could start a lambda-introducer. */
22085 bool non_const = false;
22086
22087 cp_parser_parse_tentatively (parser);
22088
22089 if (!cp_parser_array_designator_p (parser))
22090 {
22091 cp_parser_simulate_error (parser);
22092 designator = NULL_TREE;
22093 }
22094 else
22095 {
22096 designator = cp_parser_constant_expression (parser, true,
22097 &non_const);
22098 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22099 cp_parser_require (parser, CPP_EQ, RT_EQ);
22100 }
22101
22102 if (!cp_parser_parse_definitely (parser))
22103 designator = NULL_TREE;
22104 else if (non_const)
22105 require_potential_rvalue_constant_expression (designator);
22106 if (designator)
22107 /* Warn the user that they are using an extension. */
22108 pedwarn (loc, OPT_Wpedantic,
22109 "ISO C++ does not allow C99 designated initializers");
22110 }
22111 else
22112 designator = NULL_TREE;
22113
22114 if (first_p)
22115 {
22116 first_designator = designator;
22117 first_p = false;
22118 }
22119 else if (cxx_dialect >= cxx2a
22120 && first_designator != error_mark_node
22121 && (!first_designator != !designator))
22122 {
22123 error_at (loc, "either all initializer clauses should be designated "
22124 "or none of them should be");
22125 first_designator = error_mark_node;
22126 }
22127 else if (cxx_dialect < cxx2a && !first_designator)
22128 first_designator = designator;
22129
22130 /* Parse the initializer. */
22131 initializer = cp_parser_initializer_clause (parser,
22132 &clause_non_constant_p);
22133 /* If any clause is non-constant, so is the entire initializer. */
22134 if (clause_non_constant_p)
22135 *non_constant_p = true;
22136
22137 /* If we have an ellipsis, this is an initializer pack
22138 expansion. */
22139 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22140 {
22141 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22142
22143 /* Consume the `...'. */
22144 cp_lexer_consume_token (parser->lexer);
22145
22146 if (designator && cxx_dialect >= cxx2a)
22147 error_at (loc,
22148 "%<...%> not allowed in designated initializer list");
22149
22150 /* Turn the initializer into an initializer expansion. */
22151 initializer = make_pack_expansion (initializer);
22152 }
22153
22154 /* Add it to the vector. */
22155 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22156
22157 /* If the next token is not a comma, we have reached the end of
22158 the list. */
22159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22160 break;
22161
22162 /* Peek at the next token. */
22163 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22164 /* If the next token is a `}', then we're still done. An
22165 initializer-clause can have a trailing `,' after the
22166 initializer-list and before the closing `}'. */
22167 if (token->type == CPP_CLOSE_BRACE)
22168 break;
22169
22170 /* Consume the `,' token. */
22171 cp_lexer_consume_token (parser->lexer);
22172 }
22173
22174 /* The same identifier shall not appear in multiple designators
22175 of a designated-initializer-list. */
22176 if (first_designator)
22177 {
22178 unsigned int i;
22179 tree designator, val;
22180 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22181 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22182 {
22183 if (IDENTIFIER_MARKED (designator))
22184 {
22185 error_at (EXPR_LOC_OR_LOC (val, input_location),
22186 "%<.%s%> designator used multiple times in "
22187 "the same initializer list",
22188 IDENTIFIER_POINTER (designator));
22189 (*v)[i].index = NULL_TREE;
22190 }
22191 else
22192 IDENTIFIER_MARKED (designator) = 1;
22193 }
22194 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22195 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22196 IDENTIFIER_MARKED (designator) = 0;
22197 }
22198
22199 return v;
22200 }
22201
22202 /* Classes [gram.class] */
22203
22204 /* Parse a class-name.
22205
22206 class-name:
22207 identifier
22208 template-id
22209
22210 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22211 to indicate that names looked up in dependent types should be
22212 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22213 keyword has been used to indicate that the name that appears next
22214 is a template. TAG_TYPE indicates the explicit tag given before
22215 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22216 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22217 is the class being defined in a class-head. If ENUM_OK is TRUE,
22218 enum-names are also accepted.
22219
22220 Returns the TYPE_DECL representing the class. */
22221
22222 static tree
22223 cp_parser_class_name (cp_parser *parser,
22224 bool typename_keyword_p,
22225 bool template_keyword_p,
22226 enum tag_types tag_type,
22227 bool check_dependency_p,
22228 bool class_head_p,
22229 bool is_declaration,
22230 bool enum_ok)
22231 {
22232 tree decl;
22233 tree scope;
22234 bool typename_p;
22235 cp_token *token;
22236 tree identifier = NULL_TREE;
22237
22238 /* All class-names start with an identifier. */
22239 token = cp_lexer_peek_token (parser->lexer);
22240 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22241 {
22242 cp_parser_error (parser, "expected class-name");
22243 return error_mark_node;
22244 }
22245
22246 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22247 to a template-id, so we save it here. */
22248 scope = parser->scope;
22249 if (scope == error_mark_node)
22250 return error_mark_node;
22251
22252 /* Any name names a type if we're following the `typename' keyword
22253 in a qualified name where the enclosing scope is type-dependent. */
22254 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22255 && dependent_type_p (scope));
22256 /* Handle the common case (an identifier, but not a template-id)
22257 efficiently. */
22258 if (token->type == CPP_NAME
22259 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22260 {
22261 cp_token *identifier_token;
22262 bool ambiguous_p;
22263
22264 /* Look for the identifier. */
22265 identifier_token = cp_lexer_peek_token (parser->lexer);
22266 ambiguous_p = identifier_token->error_reported;
22267 identifier = cp_parser_identifier (parser);
22268 /* If the next token isn't an identifier, we are certainly not
22269 looking at a class-name. */
22270 if (identifier == error_mark_node)
22271 decl = error_mark_node;
22272 /* If we know this is a type-name, there's no need to look it
22273 up. */
22274 else if (typename_p)
22275 decl = identifier;
22276 else
22277 {
22278 tree ambiguous_decls;
22279 /* If we already know that this lookup is ambiguous, then
22280 we've already issued an error message; there's no reason
22281 to check again. */
22282 if (ambiguous_p)
22283 {
22284 cp_parser_simulate_error (parser);
22285 return error_mark_node;
22286 }
22287 /* If the next token is a `::', then the name must be a type
22288 name.
22289
22290 [basic.lookup.qual]
22291
22292 During the lookup for a name preceding the :: scope
22293 resolution operator, object, function, and enumerator
22294 names are ignored. */
22295 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22296 tag_type = scope_type;
22297 /* Look up the name. */
22298 decl = cp_parser_lookup_name (parser, identifier,
22299 tag_type,
22300 /*is_template=*/false,
22301 /*is_namespace=*/false,
22302 check_dependency_p,
22303 &ambiguous_decls,
22304 identifier_token->location);
22305 if (ambiguous_decls)
22306 {
22307 if (cp_parser_parsing_tentatively (parser))
22308 cp_parser_simulate_error (parser);
22309 return error_mark_node;
22310 }
22311 }
22312 }
22313 else
22314 {
22315 /* Try a template-id. */
22316 decl = cp_parser_template_id (parser, template_keyword_p,
22317 check_dependency_p,
22318 tag_type,
22319 is_declaration);
22320 if (decl == error_mark_node)
22321 return error_mark_node;
22322 }
22323
22324 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22325
22326 /* If this is a typename, create a TYPENAME_TYPE. */
22327 if (typename_p && decl != error_mark_node)
22328 {
22329 decl = make_typename_type (scope, decl, typename_type,
22330 /*complain=*/tf_error);
22331 if (decl != error_mark_node)
22332 decl = TYPE_NAME (decl);
22333 }
22334
22335 decl = strip_using_decl (decl);
22336
22337 /* Check to see that it is really the name of a class. */
22338 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22339 && identifier_p (TREE_OPERAND (decl, 0))
22340 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22341 /* Situations like this:
22342
22343 template <typename T> struct A {
22344 typename T::template X<int>::I i;
22345 };
22346
22347 are problematic. Is `T::template X<int>' a class-name? The
22348 standard does not seem to be definitive, but there is no other
22349 valid interpretation of the following `::'. Therefore, those
22350 names are considered class-names. */
22351 {
22352 decl = make_typename_type (scope, decl, tag_type, tf_error);
22353 if (decl != error_mark_node)
22354 decl = TYPE_NAME (decl);
22355 }
22356 else if (TREE_CODE (decl) != TYPE_DECL
22357 || TREE_TYPE (decl) == error_mark_node
22358 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22359 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22360 /* In Objective-C 2.0, a classname followed by '.' starts a
22361 dot-syntax expression, and it's not a type-name. */
22362 || (c_dialect_objc ()
22363 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22364 && objc_is_class_name (decl)))
22365 decl = error_mark_node;
22366
22367 if (decl == error_mark_node)
22368 cp_parser_error (parser, "expected class-name");
22369 else if (identifier && !parser->scope)
22370 maybe_note_name_used_in_class (identifier, decl);
22371
22372 return decl;
22373 }
22374
22375 /* Parse a class-specifier.
22376
22377 class-specifier:
22378 class-head { member-specification [opt] }
22379
22380 Returns the TREE_TYPE representing the class. */
22381
22382 static tree
22383 cp_parser_class_specifier_1 (cp_parser* parser)
22384 {
22385 tree type;
22386 tree attributes = NULL_TREE;
22387 bool nested_name_specifier_p;
22388 unsigned saved_num_template_parameter_lists;
22389 bool saved_in_function_body;
22390 unsigned char in_statement;
22391 bool in_switch_statement_p;
22392 bool saved_in_unbraced_linkage_specification_p;
22393 tree old_scope = NULL_TREE;
22394 tree scope = NULL_TREE;
22395 cp_token *closing_brace;
22396
22397 push_deferring_access_checks (dk_no_deferred);
22398
22399 /* Parse the class-head. */
22400 type = cp_parser_class_head (parser,
22401 &nested_name_specifier_p);
22402 /* If the class-head was a semantic disaster, skip the entire body
22403 of the class. */
22404 if (!type)
22405 {
22406 cp_parser_skip_to_end_of_block_or_statement (parser);
22407 pop_deferring_access_checks ();
22408 return error_mark_node;
22409 }
22410
22411 /* Look for the `{'. */
22412 matching_braces braces;
22413 if (!braces.require_open (parser))
22414 {
22415 pop_deferring_access_checks ();
22416 return error_mark_node;
22417 }
22418
22419 cp_ensure_no_omp_declare_simd (parser);
22420 cp_ensure_no_oacc_routine (parser);
22421
22422 /* Issue an error message if type-definitions are forbidden here. */
22423 cp_parser_check_type_definition (parser);
22424 /* Remember that we are defining one more class. */
22425 ++parser->num_classes_being_defined;
22426 /* Inside the class, surrounding template-parameter-lists do not
22427 apply. */
22428 saved_num_template_parameter_lists
22429 = parser->num_template_parameter_lists;
22430 parser->num_template_parameter_lists = 0;
22431 /* We are not in a function body. */
22432 saved_in_function_body = parser->in_function_body;
22433 parser->in_function_body = false;
22434 /* Or in a loop. */
22435 in_statement = parser->in_statement;
22436 parser->in_statement = 0;
22437 /* Or in a switch. */
22438 in_switch_statement_p = parser->in_switch_statement_p;
22439 parser->in_switch_statement_p = false;
22440 /* We are not immediately inside an extern "lang" block. */
22441 saved_in_unbraced_linkage_specification_p
22442 = parser->in_unbraced_linkage_specification_p;
22443 parser->in_unbraced_linkage_specification_p = false;
22444
22445 // Associate constraints with the type.
22446 if (flag_concepts)
22447 type = associate_classtype_constraints (type);
22448
22449 /* Start the class. */
22450 if (nested_name_specifier_p)
22451 {
22452 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22453 old_scope = push_inner_scope (scope);
22454 }
22455 type = begin_class_definition (type);
22456
22457 if (type == error_mark_node)
22458 /* If the type is erroneous, skip the entire body of the class. */
22459 cp_parser_skip_to_closing_brace (parser);
22460 else
22461 /* Parse the member-specification. */
22462 cp_parser_member_specification_opt (parser);
22463
22464 /* Look for the trailing `}'. */
22465 closing_brace = braces.require_close (parser);
22466 /* Look for trailing attributes to apply to this class. */
22467 if (cp_parser_allow_gnu_extensions_p (parser))
22468 attributes = cp_parser_gnu_attributes_opt (parser);
22469 if (type != error_mark_node)
22470 type = finish_struct (type, attributes);
22471 if (nested_name_specifier_p)
22472 pop_inner_scope (old_scope, scope);
22473
22474 /* We've finished a type definition. Check for the common syntax
22475 error of forgetting a semicolon after the definition. We need to
22476 be careful, as we can't just check for not-a-semicolon and be done
22477 with it; the user might have typed:
22478
22479 class X { } c = ...;
22480 class X { } *p = ...;
22481
22482 and so forth. Instead, enumerate all the possible tokens that
22483 might follow this production; if we don't see one of them, then
22484 complain and silently insert the semicolon. */
22485 {
22486 cp_token *token = cp_lexer_peek_token (parser->lexer);
22487 bool want_semicolon = true;
22488
22489 if (cp_next_tokens_can_be_std_attribute_p (parser))
22490 /* Don't try to parse c++11 attributes here. As per the
22491 grammar, that should be a task for
22492 cp_parser_decl_specifier_seq. */
22493 want_semicolon = false;
22494
22495 switch (token->type)
22496 {
22497 case CPP_NAME:
22498 case CPP_SEMICOLON:
22499 case CPP_MULT:
22500 case CPP_AND:
22501 case CPP_OPEN_PAREN:
22502 case CPP_CLOSE_PAREN:
22503 case CPP_COMMA:
22504 want_semicolon = false;
22505 break;
22506
22507 /* While it's legal for type qualifiers and storage class
22508 specifiers to follow type definitions in the grammar, only
22509 compiler testsuites contain code like that. Assume that if
22510 we see such code, then what we're really seeing is a case
22511 like:
22512
22513 class X { }
22514 const <type> var = ...;
22515
22516 or
22517
22518 class Y { }
22519 static <type> func (...) ...
22520
22521 i.e. the qualifier or specifier applies to the next
22522 declaration. To do so, however, we need to look ahead one
22523 more token to see if *that* token is a type specifier.
22524
22525 This code could be improved to handle:
22526
22527 class Z { }
22528 static const <type> var = ...; */
22529 case CPP_KEYWORD:
22530 if (keyword_is_decl_specifier (token->keyword))
22531 {
22532 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22533
22534 /* Handling user-defined types here would be nice, but very
22535 tricky. */
22536 want_semicolon
22537 = (lookahead->type == CPP_KEYWORD
22538 && keyword_begins_type_specifier (lookahead->keyword));
22539 }
22540 break;
22541 default:
22542 break;
22543 }
22544
22545 /* If we don't have a type, then something is very wrong and we
22546 shouldn't try to do anything clever. Likewise for not seeing the
22547 closing brace. */
22548 if (closing_brace && TYPE_P (type) && want_semicolon)
22549 {
22550 /* Locate the closing brace. */
22551 cp_token_position prev
22552 = cp_lexer_previous_token_position (parser->lexer);
22553 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22554 location_t loc = prev_token->location;
22555
22556 /* We want to suggest insertion of a ';' immediately *after* the
22557 closing brace, so, if we can, offset the location by 1 column. */
22558 location_t next_loc = loc;
22559 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22560 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22561
22562 rich_location richloc (line_table, next_loc);
22563
22564 /* If we successfully offset the location, suggest the fix-it. */
22565 if (next_loc != loc)
22566 richloc.add_fixit_insert_before (next_loc, ";");
22567
22568 if (CLASSTYPE_DECLARED_CLASS (type))
22569 error_at (&richloc,
22570 "expected %<;%> after class definition");
22571 else if (TREE_CODE (type) == RECORD_TYPE)
22572 error_at (&richloc,
22573 "expected %<;%> after struct definition");
22574 else if (TREE_CODE (type) == UNION_TYPE)
22575 error_at (&richloc,
22576 "expected %<;%> after union definition");
22577 else
22578 gcc_unreachable ();
22579
22580 /* Unget one token and smash it to look as though we encountered
22581 a semicolon in the input stream. */
22582 cp_lexer_set_token_position (parser->lexer, prev);
22583 token = cp_lexer_peek_token (parser->lexer);
22584 token->type = CPP_SEMICOLON;
22585 token->keyword = RID_MAX;
22586 }
22587 }
22588
22589 /* If this class is not itself within the scope of another class,
22590 then we need to parse the bodies of all of the queued function
22591 definitions. Note that the queued functions defined in a class
22592 are not always processed immediately following the
22593 class-specifier for that class. Consider:
22594
22595 struct A {
22596 struct B { void f() { sizeof (A); } };
22597 };
22598
22599 If `f' were processed before the processing of `A' were
22600 completed, there would be no way to compute the size of `A'.
22601 Note that the nesting we are interested in here is lexical --
22602 not the semantic nesting given by TYPE_CONTEXT. In particular,
22603 for:
22604
22605 struct A { struct B; };
22606 struct A::B { void f() { } };
22607
22608 there is no need to delay the parsing of `A::B::f'. */
22609 if (--parser->num_classes_being_defined == 0)
22610 {
22611 tree decl;
22612 tree class_type = NULL_TREE;
22613 tree pushed_scope = NULL_TREE;
22614 unsigned ix;
22615 cp_default_arg_entry *e;
22616 tree save_ccp, save_ccr;
22617
22618 /* In a first pass, parse default arguments to the functions.
22619 Then, in a second pass, parse the bodies of the functions.
22620 This two-phased approach handles cases like:
22621
22622 struct S {
22623 void f() { g(); }
22624 void g(int i = 3);
22625 };
22626
22627 */
22628 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22629 {
22630 decl = e->decl;
22631 /* If there are default arguments that have not yet been processed,
22632 take care of them now. */
22633 if (class_type != e->class_type)
22634 {
22635 if (pushed_scope)
22636 pop_scope (pushed_scope);
22637 class_type = e->class_type;
22638 pushed_scope = push_scope (class_type);
22639 }
22640 /* Make sure that any template parameters are in scope. */
22641 maybe_begin_member_template_processing (decl);
22642 /* Parse the default argument expressions. */
22643 cp_parser_late_parsing_default_args (parser, decl);
22644 /* Remove any template parameters from the symbol table. */
22645 maybe_end_member_template_processing ();
22646 }
22647 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22648 /* Now parse any NSDMIs. */
22649 save_ccp = current_class_ptr;
22650 save_ccr = current_class_ref;
22651 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22652 {
22653 if (class_type != DECL_CONTEXT (decl))
22654 {
22655 if (pushed_scope)
22656 pop_scope (pushed_scope);
22657 class_type = DECL_CONTEXT (decl);
22658 pushed_scope = push_scope (class_type);
22659 }
22660 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22661 cp_parser_late_parsing_nsdmi (parser, decl);
22662 }
22663 vec_safe_truncate (unparsed_nsdmis, 0);
22664 current_class_ptr = save_ccp;
22665 current_class_ref = save_ccr;
22666 if (pushed_scope)
22667 pop_scope (pushed_scope);
22668
22669 /* Now do some post-NSDMI bookkeeping. */
22670 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22671 after_nsdmi_defaulted_late_checks (class_type);
22672 vec_safe_truncate (unparsed_classes, 0);
22673 after_nsdmi_defaulted_late_checks (type);
22674
22675 /* Now parse the body of the functions. */
22676 if (flag_openmp)
22677 {
22678 /* OpenMP UDRs need to be parsed before all other functions. */
22679 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22680 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22681 cp_parser_late_parsing_for_member (parser, decl);
22682 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22683 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22684 cp_parser_late_parsing_for_member (parser, decl);
22685 }
22686 else
22687 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22688 cp_parser_late_parsing_for_member (parser, decl);
22689 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22690 }
22691 else
22692 vec_safe_push (unparsed_classes, type);
22693
22694 /* Put back any saved access checks. */
22695 pop_deferring_access_checks ();
22696
22697 /* Restore saved state. */
22698 parser->in_switch_statement_p = in_switch_statement_p;
22699 parser->in_statement = in_statement;
22700 parser->in_function_body = saved_in_function_body;
22701 parser->num_template_parameter_lists
22702 = saved_num_template_parameter_lists;
22703 parser->in_unbraced_linkage_specification_p
22704 = saved_in_unbraced_linkage_specification_p;
22705
22706 return type;
22707 }
22708
22709 static tree
22710 cp_parser_class_specifier (cp_parser* parser)
22711 {
22712 tree ret;
22713 timevar_push (TV_PARSE_STRUCT);
22714 ret = cp_parser_class_specifier_1 (parser);
22715 timevar_pop (TV_PARSE_STRUCT);
22716 return ret;
22717 }
22718
22719 /* Parse a class-head.
22720
22721 class-head:
22722 class-key identifier [opt] base-clause [opt]
22723 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22724 class-key nested-name-specifier [opt] template-id
22725 base-clause [opt]
22726
22727 class-virt-specifier:
22728 final
22729
22730 GNU Extensions:
22731 class-key attributes identifier [opt] base-clause [opt]
22732 class-key attributes nested-name-specifier identifier base-clause [opt]
22733 class-key attributes nested-name-specifier [opt] template-id
22734 base-clause [opt]
22735
22736 Upon return BASES is initialized to the list of base classes (or
22737 NULL, if there are none) in the same form returned by
22738 cp_parser_base_clause.
22739
22740 Returns the TYPE of the indicated class. Sets
22741 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22742 involving a nested-name-specifier was used, and FALSE otherwise.
22743
22744 Returns error_mark_node if this is not a class-head.
22745
22746 Returns NULL_TREE if the class-head is syntactically valid, but
22747 semantically invalid in a way that means we should skip the entire
22748 body of the class. */
22749
22750 static tree
22751 cp_parser_class_head (cp_parser* parser,
22752 bool* nested_name_specifier_p)
22753 {
22754 tree nested_name_specifier;
22755 enum tag_types class_key;
22756 tree id = NULL_TREE;
22757 tree type = NULL_TREE;
22758 tree attributes;
22759 tree bases;
22760 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22761 bool template_id_p = false;
22762 bool qualified_p = false;
22763 bool invalid_nested_name_p = false;
22764 bool invalid_explicit_specialization_p = false;
22765 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22766 tree pushed_scope = NULL_TREE;
22767 unsigned num_templates;
22768 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22769 /* Assume no nested-name-specifier will be present. */
22770 *nested_name_specifier_p = false;
22771 /* Assume no template parameter lists will be used in defining the
22772 type. */
22773 num_templates = 0;
22774 parser->colon_corrects_to_scope_p = false;
22775
22776 /* Look for the class-key. */
22777 class_key = cp_parser_class_key (parser);
22778 if (class_key == none_type)
22779 return error_mark_node;
22780
22781 location_t class_head_start_location = input_location;
22782
22783 /* Parse the attributes. */
22784 attributes = cp_parser_attributes_opt (parser);
22785
22786 /* If the next token is `::', that is invalid -- but sometimes
22787 people do try to write:
22788
22789 struct ::S {};
22790
22791 Handle this gracefully by accepting the extra qualifier, and then
22792 issuing an error about it later if this really is a
22793 class-head. If it turns out just to be an elaborated type
22794 specifier, remain silent. */
22795 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22796 qualified_p = true;
22797
22798 push_deferring_access_checks (dk_no_check);
22799
22800 /* Determine the name of the class. Begin by looking for an
22801 optional nested-name-specifier. */
22802 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22803 nested_name_specifier
22804 = cp_parser_nested_name_specifier_opt (parser,
22805 /*typename_keyword_p=*/false,
22806 /*check_dependency_p=*/false,
22807 /*type_p=*/true,
22808 /*is_declaration=*/false);
22809 /* If there was a nested-name-specifier, then there *must* be an
22810 identifier. */
22811
22812 cp_token *bad_template_keyword = NULL;
22813
22814 if (nested_name_specifier)
22815 {
22816 type_start_token = cp_lexer_peek_token (parser->lexer);
22817 /* Although the grammar says `identifier', it really means
22818 `class-name' or `template-name'. You are only allowed to
22819 define a class that has already been declared with this
22820 syntax.
22821
22822 The proposed resolution for Core Issue 180 says that wherever
22823 you see `class T::X' you should treat `X' as a type-name.
22824
22825 It is OK to define an inaccessible class; for example:
22826
22827 class A { class B; };
22828 class A::B {};
22829
22830 We do not know if we will see a class-name, or a
22831 template-name. We look for a class-name first, in case the
22832 class-name is a template-id; if we looked for the
22833 template-name first we would stop after the template-name. */
22834 cp_parser_parse_tentatively (parser);
22835 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22836 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22837 type = cp_parser_class_name (parser,
22838 /*typename_keyword_p=*/false,
22839 /*template_keyword_p=*/false,
22840 class_type,
22841 /*check_dependency_p=*/false,
22842 /*class_head_p=*/true,
22843 /*is_declaration=*/false);
22844 /* If that didn't work, ignore the nested-name-specifier. */
22845 if (!cp_parser_parse_definitely (parser))
22846 {
22847 invalid_nested_name_p = true;
22848 type_start_token = cp_lexer_peek_token (parser->lexer);
22849 id = cp_parser_identifier (parser);
22850 if (id == error_mark_node)
22851 id = NULL_TREE;
22852 }
22853 /* If we could not find a corresponding TYPE, treat this
22854 declaration like an unqualified declaration. */
22855 if (type == error_mark_node)
22856 nested_name_specifier = NULL_TREE;
22857 /* Otherwise, count the number of templates used in TYPE and its
22858 containing scopes. */
22859 else
22860 {
22861 tree scope;
22862
22863 for (scope = TREE_TYPE (type);
22864 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22865 scope = get_containing_scope (scope))
22866 if (TYPE_P (scope)
22867 && CLASS_TYPE_P (scope)
22868 && CLASSTYPE_TEMPLATE_INFO (scope)
22869 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22870 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22871 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22872 ++num_templates;
22873 }
22874 }
22875 /* Otherwise, the identifier is optional. */
22876 else
22877 {
22878 /* We don't know whether what comes next is a template-id,
22879 an identifier, or nothing at all. */
22880 cp_parser_parse_tentatively (parser);
22881 /* Check for a template-id. */
22882 type_start_token = cp_lexer_peek_token (parser->lexer);
22883 id = cp_parser_template_id (parser,
22884 /*template_keyword_p=*/false,
22885 /*check_dependency_p=*/true,
22886 class_key,
22887 /*is_declaration=*/true);
22888 /* If that didn't work, it could still be an identifier. */
22889 if (!cp_parser_parse_definitely (parser))
22890 {
22891 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22892 {
22893 type_start_token = cp_lexer_peek_token (parser->lexer);
22894 id = cp_parser_identifier (parser);
22895 }
22896 else
22897 id = NULL_TREE;
22898 }
22899 else
22900 {
22901 template_id_p = true;
22902 ++num_templates;
22903 }
22904 }
22905
22906 pop_deferring_access_checks ();
22907
22908 if (id)
22909 {
22910 cp_parser_check_for_invalid_template_id (parser, id,
22911 class_key,
22912 type_start_token->location);
22913 }
22914 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22915
22916 /* If it's not a `:' or a `{' then we can't really be looking at a
22917 class-head, since a class-head only appears as part of a
22918 class-specifier. We have to detect this situation before calling
22919 xref_tag, since that has irreversible side-effects. */
22920 if (!cp_parser_next_token_starts_class_definition_p (parser))
22921 {
22922 cp_parser_error (parser, "expected %<{%> or %<:%>");
22923 type = error_mark_node;
22924 goto out;
22925 }
22926
22927 /* At this point, we're going ahead with the class-specifier, even
22928 if some other problem occurs. */
22929 cp_parser_commit_to_tentative_parse (parser);
22930 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22931 {
22932 cp_parser_error (parser,
22933 "cannot specify %<override%> for a class");
22934 type = error_mark_node;
22935 goto out;
22936 }
22937 /* Issue the error about the overly-qualified name now. */
22938 if (qualified_p)
22939 {
22940 cp_parser_error (parser,
22941 "global qualification of class name is invalid");
22942 type = error_mark_node;
22943 goto out;
22944 }
22945 else if (invalid_nested_name_p)
22946 {
22947 cp_parser_error (parser,
22948 "qualified name does not name a class");
22949 type = error_mark_node;
22950 goto out;
22951 }
22952 else if (nested_name_specifier)
22953 {
22954 tree scope;
22955
22956 if (bad_template_keyword)
22957 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22958 keyword template shall not appear at the top level. */
22959 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22960 "keyword %<template%> not allowed in class-head-name");
22961
22962 /* Reject typedef-names in class heads. */
22963 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22964 {
22965 error_at (type_start_token->location,
22966 "invalid class name in declaration of %qD",
22967 type);
22968 type = NULL_TREE;
22969 goto done;
22970 }
22971
22972 /* Figure out in what scope the declaration is being placed. */
22973 scope = current_scope ();
22974 /* If that scope does not contain the scope in which the
22975 class was originally declared, the program is invalid. */
22976 if (scope && !is_ancestor (scope, nested_name_specifier))
22977 {
22978 if (at_namespace_scope_p ())
22979 error_at (type_start_token->location,
22980 "declaration of %qD in namespace %qD which does not "
22981 "enclose %qD",
22982 type, scope, nested_name_specifier);
22983 else
22984 error_at (type_start_token->location,
22985 "declaration of %qD in %qD which does not enclose %qD",
22986 type, scope, nested_name_specifier);
22987 type = NULL_TREE;
22988 goto done;
22989 }
22990 /* [dcl.meaning]
22991
22992 A declarator-id shall not be qualified except for the
22993 definition of a ... nested class outside of its class
22994 ... [or] the definition or explicit instantiation of a
22995 class member of a namespace outside of its namespace. */
22996 if (scope == nested_name_specifier)
22997 {
22998 permerror (nested_name_specifier_token_start->location,
22999 "extra qualification not allowed");
23000 nested_name_specifier = NULL_TREE;
23001 num_templates = 0;
23002 }
23003 }
23004 /* An explicit-specialization must be preceded by "template <>". If
23005 it is not, try to recover gracefully. */
23006 if (at_namespace_scope_p ()
23007 && parser->num_template_parameter_lists == 0
23008 && !processing_template_parmlist
23009 && template_id_p)
23010 {
23011 /* Build a location of this form:
23012 struct typename <ARGS>
23013 ^~~~~~~~~~~~~~~~~~~~~~
23014 with caret==start at the start token, and
23015 finishing at the end of the type. */
23016 location_t reported_loc
23017 = make_location (class_head_start_location,
23018 class_head_start_location,
23019 get_finish (type_start_token->location));
23020 rich_location richloc (line_table, reported_loc);
23021 richloc.add_fixit_insert_before (class_head_start_location,
23022 "template <> ");
23023 error_at (&richloc,
23024 "an explicit specialization must be preceded by"
23025 " %<template <>%>");
23026 invalid_explicit_specialization_p = true;
23027 /* Take the same action that would have been taken by
23028 cp_parser_explicit_specialization. */
23029 ++parser->num_template_parameter_lists;
23030 begin_specialization ();
23031 }
23032 /* There must be no "return" statements between this point and the
23033 end of this function; set "type "to the correct return value and
23034 use "goto done;" to return. */
23035 /* Make sure that the right number of template parameters were
23036 present. */
23037 if (!cp_parser_check_template_parameters (parser, num_templates,
23038 type_start_token->location,
23039 /*declarator=*/NULL))
23040 {
23041 /* If something went wrong, there is no point in even trying to
23042 process the class-definition. */
23043 type = NULL_TREE;
23044 goto done;
23045 }
23046
23047 /* Look up the type. */
23048 if (template_id_p)
23049 {
23050 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23051 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23052 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23053 {
23054 error_at (type_start_token->location,
23055 "function template %qD redeclared as a class template", id);
23056 type = error_mark_node;
23057 }
23058 else
23059 {
23060 type = TREE_TYPE (id);
23061 type = maybe_process_partial_specialization (type);
23062
23063 /* Check the scope while we still know whether or not we had a
23064 nested-name-specifier. */
23065 if (type != error_mark_node)
23066 check_unqualified_spec_or_inst (type, type_start_token->location);
23067 }
23068 if (nested_name_specifier)
23069 pushed_scope = push_scope (nested_name_specifier);
23070 }
23071 else if (nested_name_specifier)
23072 {
23073 tree class_type;
23074
23075 /* Given:
23076
23077 template <typename T> struct S { struct T };
23078 template <typename T> struct S<T>::T { };
23079
23080 we will get a TYPENAME_TYPE when processing the definition of
23081 `S::T'. We need to resolve it to the actual type before we
23082 try to define it. */
23083 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23084 {
23085 class_type = resolve_typename_type (TREE_TYPE (type),
23086 /*only_current_p=*/false);
23087 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23088 type = TYPE_NAME (class_type);
23089 else
23090 {
23091 cp_parser_error (parser, "could not resolve typename type");
23092 type = error_mark_node;
23093 }
23094 }
23095
23096 if (maybe_process_partial_specialization (TREE_TYPE (type))
23097 == error_mark_node)
23098 {
23099 type = NULL_TREE;
23100 goto done;
23101 }
23102
23103 class_type = current_class_type;
23104 /* Enter the scope indicated by the nested-name-specifier. */
23105 pushed_scope = push_scope (nested_name_specifier);
23106 /* Get the canonical version of this type. */
23107 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23108 /* Call push_template_decl if it seems like we should be defining a
23109 template either from the template headers or the type we're
23110 defining, so that we diagnose both extra and missing headers. */
23111 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23112 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23113 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23114 {
23115 type = push_template_decl (type);
23116 if (type == error_mark_node)
23117 {
23118 type = NULL_TREE;
23119 goto done;
23120 }
23121 }
23122
23123 type = TREE_TYPE (type);
23124 *nested_name_specifier_p = true;
23125 }
23126 else /* The name is not a nested name. */
23127 {
23128 /* If the class was unnamed, create a dummy name. */
23129 if (!id)
23130 id = make_anon_name ();
23131 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23132 ? ts_within_enclosing_non_class
23133 : ts_current);
23134 type = xref_tag (class_key, id, tag_scope,
23135 parser->num_template_parameter_lists);
23136 }
23137
23138 /* Indicate whether this class was declared as a `class' or as a
23139 `struct'. */
23140 if (TREE_CODE (type) == RECORD_TYPE)
23141 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23142 cp_parser_check_class_key (class_key, type);
23143
23144 /* If this type was already complete, and we see another definition,
23145 that's an error. */
23146 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23147 {
23148 error_at (type_start_token->location, "redefinition of %q#T",
23149 type);
23150 inform (location_of (type), "previous definition of %q#T",
23151 type);
23152 type = NULL_TREE;
23153 goto done;
23154 }
23155 else if (type == error_mark_node)
23156 type = NULL_TREE;
23157
23158 if (type)
23159 {
23160 /* Apply attributes now, before any use of the class as a template
23161 argument in its base list. */
23162 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23163 fixup_attribute_variants (type);
23164 }
23165
23166 /* We will have entered the scope containing the class; the names of
23167 base classes should be looked up in that context. For example:
23168
23169 struct A { struct B {}; struct C; };
23170 struct A::C : B {};
23171
23172 is valid. */
23173
23174 /* Get the list of base-classes, if there is one. */
23175 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23176 {
23177 /* PR59482: enter the class scope so that base-specifiers are looked
23178 up correctly. */
23179 if (type)
23180 pushclass (type);
23181 bases = cp_parser_base_clause (parser);
23182 /* PR59482: get out of the previously pushed class scope so that the
23183 subsequent pops pop the right thing. */
23184 if (type)
23185 popclass ();
23186 }
23187 else
23188 bases = NULL_TREE;
23189
23190 /* If we're really defining a class, process the base classes.
23191 If they're invalid, fail. */
23192 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23193 xref_basetypes (type, bases);
23194
23195 done:
23196 /* Leave the scope given by the nested-name-specifier. We will
23197 enter the class scope itself while processing the members. */
23198 if (pushed_scope)
23199 pop_scope (pushed_scope);
23200
23201 if (invalid_explicit_specialization_p)
23202 {
23203 end_specialization ();
23204 --parser->num_template_parameter_lists;
23205 }
23206
23207 if (type)
23208 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23209 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23210 CLASSTYPE_FINAL (type) = 1;
23211 out:
23212 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23213 return type;
23214 }
23215
23216 /* Parse a class-key.
23217
23218 class-key:
23219 class
23220 struct
23221 union
23222
23223 Returns the kind of class-key specified, or none_type to indicate
23224 error. */
23225
23226 static enum tag_types
23227 cp_parser_class_key (cp_parser* parser)
23228 {
23229 cp_token *token;
23230 enum tag_types tag_type;
23231
23232 /* Look for the class-key. */
23233 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23234 if (!token)
23235 return none_type;
23236
23237 /* Check to see if the TOKEN is a class-key. */
23238 tag_type = cp_parser_token_is_class_key (token);
23239 if (!tag_type)
23240 cp_parser_error (parser, "expected class-key");
23241 return tag_type;
23242 }
23243
23244 /* Parse a type-parameter-key.
23245
23246 type-parameter-key:
23247 class
23248 typename
23249 */
23250
23251 static void
23252 cp_parser_type_parameter_key (cp_parser* parser)
23253 {
23254 /* Look for the type-parameter-key. */
23255 enum tag_types tag_type = none_type;
23256 cp_token *token = cp_lexer_peek_token (parser->lexer);
23257 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23258 {
23259 cp_lexer_consume_token (parser->lexer);
23260 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23261 /* typename is not allowed in a template template parameter
23262 by the standard until C++17. */
23263 pedwarn (token->location, OPT_Wpedantic,
23264 "ISO C++ forbids typename key in template template parameter;"
23265 " use -std=c++17 or -std=gnu++17");
23266 }
23267 else
23268 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23269
23270 return;
23271 }
23272
23273 /* Parse an (optional) member-specification.
23274
23275 member-specification:
23276 member-declaration member-specification [opt]
23277 access-specifier : member-specification [opt] */
23278
23279 static void
23280 cp_parser_member_specification_opt (cp_parser* parser)
23281 {
23282 while (true)
23283 {
23284 cp_token *token;
23285 enum rid keyword;
23286
23287 /* Peek at the next token. */
23288 token = cp_lexer_peek_token (parser->lexer);
23289 /* If it's a `}', or EOF then we've seen all the members. */
23290 if (token->type == CPP_CLOSE_BRACE
23291 || token->type == CPP_EOF
23292 || token->type == CPP_PRAGMA_EOL)
23293 break;
23294
23295 /* See if this token is a keyword. */
23296 keyword = token->keyword;
23297 switch (keyword)
23298 {
23299 case RID_PUBLIC:
23300 case RID_PROTECTED:
23301 case RID_PRIVATE:
23302 /* Consume the access-specifier. */
23303 cp_lexer_consume_token (parser->lexer);
23304 /* Remember which access-specifier is active. */
23305 current_access_specifier = token->u.value;
23306 /* Look for the `:'. */
23307 cp_parser_require (parser, CPP_COLON, RT_COLON);
23308 break;
23309
23310 default:
23311 /* Accept #pragmas at class scope. */
23312 if (token->type == CPP_PRAGMA)
23313 {
23314 cp_parser_pragma (parser, pragma_member, NULL);
23315 break;
23316 }
23317
23318 /* Otherwise, the next construction must be a
23319 member-declaration. */
23320 cp_parser_member_declaration (parser);
23321 }
23322 }
23323 }
23324
23325 /* Parse a member-declaration.
23326
23327 member-declaration:
23328 decl-specifier-seq [opt] member-declarator-list [opt] ;
23329 function-definition ; [opt]
23330 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23331 using-declaration
23332 template-declaration
23333 alias-declaration
23334
23335 member-declarator-list:
23336 member-declarator
23337 member-declarator-list , member-declarator
23338
23339 member-declarator:
23340 declarator pure-specifier [opt]
23341 declarator constant-initializer [opt]
23342 identifier [opt] : constant-expression
23343
23344 GNU Extensions:
23345
23346 member-declaration:
23347 __extension__ member-declaration
23348
23349 member-declarator:
23350 declarator attributes [opt] pure-specifier [opt]
23351 declarator attributes [opt] constant-initializer [opt]
23352 identifier [opt] attributes [opt] : constant-expression
23353
23354 C++0x Extensions:
23355
23356 member-declaration:
23357 static_assert-declaration */
23358
23359 static void
23360 cp_parser_member_declaration (cp_parser* parser)
23361 {
23362 cp_decl_specifier_seq decl_specifiers;
23363 tree prefix_attributes;
23364 tree decl;
23365 int declares_class_or_enum;
23366 bool friend_p;
23367 cp_token *token = NULL;
23368 cp_token *decl_spec_token_start = NULL;
23369 cp_token *initializer_token_start = NULL;
23370 int saved_pedantic;
23371 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23372
23373 /* Check for the `__extension__' keyword. */
23374 if (cp_parser_extension_opt (parser, &saved_pedantic))
23375 {
23376 /* Recurse. */
23377 cp_parser_member_declaration (parser);
23378 /* Restore the old value of the PEDANTIC flag. */
23379 pedantic = saved_pedantic;
23380
23381 return;
23382 }
23383
23384 /* Check for a template-declaration. */
23385 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23386 {
23387 /* An explicit specialization here is an error condition, and we
23388 expect the specialization handler to detect and report this. */
23389 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23390 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23391 cp_parser_explicit_specialization (parser);
23392 else
23393 cp_parser_template_declaration (parser, /*member_p=*/true);
23394
23395 return;
23396 }
23397 /* Check for a template introduction. */
23398 else if (cp_parser_template_declaration_after_export (parser, true))
23399 return;
23400
23401 /* Check for a using-declaration. */
23402 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23403 {
23404 if (cxx_dialect < cxx11)
23405 {
23406 /* Parse the using-declaration. */
23407 cp_parser_using_declaration (parser,
23408 /*access_declaration_p=*/false);
23409 return;
23410 }
23411 else
23412 {
23413 tree decl;
23414 bool alias_decl_expected;
23415 cp_parser_parse_tentatively (parser);
23416 decl = cp_parser_alias_declaration (parser);
23417 /* Note that if we actually see the '=' token after the
23418 identifier, cp_parser_alias_declaration commits the
23419 tentative parse. In that case, we really expect an
23420 alias-declaration. Otherwise, we expect a using
23421 declaration. */
23422 alias_decl_expected =
23423 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23424 cp_parser_parse_definitely (parser);
23425
23426 if (alias_decl_expected)
23427 finish_member_declaration (decl);
23428 else
23429 cp_parser_using_declaration (parser,
23430 /*access_declaration_p=*/false);
23431 return;
23432 }
23433 }
23434
23435 /* Check for @defs. */
23436 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23437 {
23438 tree ivar, member;
23439 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23440 ivar = ivar_chains;
23441 while (ivar)
23442 {
23443 member = ivar;
23444 ivar = TREE_CHAIN (member);
23445 TREE_CHAIN (member) = NULL_TREE;
23446 finish_member_declaration (member);
23447 }
23448 return;
23449 }
23450
23451 /* If the next token is `static_assert' we have a static assertion. */
23452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23453 {
23454 cp_parser_static_assert (parser, /*member_p=*/true);
23455 return;
23456 }
23457
23458 parser->colon_corrects_to_scope_p = false;
23459
23460 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23461 goto out;
23462
23463 /* Parse the decl-specifier-seq. */
23464 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23465 cp_parser_decl_specifier_seq (parser,
23466 CP_PARSER_FLAGS_OPTIONAL,
23467 &decl_specifiers,
23468 &declares_class_or_enum);
23469 /* Check for an invalid type-name. */
23470 if (!decl_specifiers.any_type_specifiers_p
23471 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23472 goto out;
23473 /* If there is no declarator, then the decl-specifier-seq should
23474 specify a type. */
23475 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23476 {
23477 /* If there was no decl-specifier-seq, and the next token is a
23478 `;', then we have something like:
23479
23480 struct S { ; };
23481
23482 [class.mem]
23483
23484 Each member-declaration shall declare at least one member
23485 name of the class. */
23486 if (!decl_specifiers.any_specifiers_p)
23487 {
23488 cp_token *token = cp_lexer_peek_token (parser->lexer);
23489 if (!in_system_header_at (token->location))
23490 {
23491 gcc_rich_location richloc (token->location);
23492 richloc.add_fixit_remove ();
23493 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23494 }
23495 }
23496 else
23497 {
23498 tree type;
23499
23500 /* See if this declaration is a friend. */
23501 friend_p = cp_parser_friend_p (&decl_specifiers);
23502 /* If there were decl-specifiers, check to see if there was
23503 a class-declaration. */
23504 type = check_tag_decl (&decl_specifiers,
23505 /*explicit_type_instantiation_p=*/false);
23506 /* Nested classes have already been added to the class, but
23507 a `friend' needs to be explicitly registered. */
23508 if (friend_p)
23509 {
23510 /* If the `friend' keyword was present, the friend must
23511 be introduced with a class-key. */
23512 if (!declares_class_or_enum && cxx_dialect < cxx11)
23513 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23514 "in C++03 a class-key must be used "
23515 "when declaring a friend");
23516 /* In this case:
23517
23518 template <typename T> struct A {
23519 friend struct A<T>::B;
23520 };
23521
23522 A<T>::B will be represented by a TYPENAME_TYPE, and
23523 therefore not recognized by check_tag_decl. */
23524 if (!type)
23525 {
23526 type = decl_specifiers.type;
23527 if (type && TREE_CODE (type) == TYPE_DECL)
23528 type = TREE_TYPE (type);
23529 }
23530 if (!type || !TYPE_P (type))
23531 error_at (decl_spec_token_start->location,
23532 "friend declaration does not name a class or "
23533 "function");
23534 else
23535 make_friend_class (current_class_type, type,
23536 /*complain=*/true);
23537 }
23538 /* If there is no TYPE, an error message will already have
23539 been issued. */
23540 else if (!type || type == error_mark_node)
23541 ;
23542 /* An anonymous aggregate has to be handled specially; such
23543 a declaration really declares a data member (with a
23544 particular type), as opposed to a nested class. */
23545 else if (ANON_AGGR_TYPE_P (type))
23546 {
23547 /* C++11 9.5/6. */
23548 if (decl_specifiers.storage_class != sc_none)
23549 error_at (decl_spec_token_start->location,
23550 "a storage class on an anonymous aggregate "
23551 "in class scope is not allowed");
23552
23553 /* Remove constructors and such from TYPE, now that we
23554 know it is an anonymous aggregate. */
23555 fixup_anonymous_aggr (type);
23556 /* And make the corresponding data member. */
23557 decl = build_decl (decl_spec_token_start->location,
23558 FIELD_DECL, NULL_TREE, type);
23559 /* Add it to the class. */
23560 finish_member_declaration (decl);
23561 }
23562 else
23563 cp_parser_check_access_in_redeclaration
23564 (TYPE_NAME (type),
23565 decl_spec_token_start->location);
23566 }
23567 }
23568 else
23569 {
23570 bool assume_semicolon = false;
23571
23572 /* Clear attributes from the decl_specifiers but keep them
23573 around as prefix attributes that apply them to the entity
23574 being declared. */
23575 prefix_attributes = decl_specifiers.attributes;
23576 decl_specifiers.attributes = NULL_TREE;
23577
23578 /* See if these declarations will be friends. */
23579 friend_p = cp_parser_friend_p (&decl_specifiers);
23580
23581 /* Keep going until we hit the `;' at the end of the
23582 declaration. */
23583 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23584 {
23585 tree attributes = NULL_TREE;
23586 tree first_attribute;
23587 tree initializer;
23588 bool is_bitfld = false;
23589 bool named_bitfld = false;
23590
23591 /* Peek at the next token. */
23592 token = cp_lexer_peek_token (parser->lexer);
23593
23594 /* The following code wants to know early if it is a bit-field
23595 or some other declaration. Attributes can appear before
23596 the `:' token, but are hopefully rare enough that the
23597 simplicity of the tentative lookup pays off. */
23598 if (cp_next_tokens_can_be_attribute_p (parser)
23599 || (token->type == CPP_NAME
23600 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23601 && (named_bitfld = true)))
23602 {
23603 cp_parser_parse_tentatively (parser);
23604 if (named_bitfld)
23605 cp_lexer_consume_token (parser->lexer);
23606 cp_parser_attributes_opt (parser);
23607 token = cp_lexer_peek_token (parser->lexer);
23608 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23609 cp_parser_abort_tentative_parse (parser);
23610 }
23611
23612 /* Check for a bitfield declaration. */
23613 if (is_bitfld
23614 || token->type == CPP_COLON
23615 || (token->type == CPP_NAME
23616 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23617 && (named_bitfld = true)))
23618 {
23619 tree identifier;
23620 tree width;
23621 tree late_attributes = NULL_TREE;
23622
23623 if (named_bitfld)
23624 identifier = cp_parser_identifier (parser);
23625 else
23626 identifier = NULL_TREE;
23627
23628 /* Look for attributes that apply to the bitfield. */
23629 attributes = cp_parser_attributes_opt (parser);
23630
23631 /* Consume the `:' token. */
23632 cp_lexer_consume_token (parser->lexer);
23633
23634 /* Get the width of the bitfield. */
23635 width = cp_parser_constant_expression (parser, false, NULL,
23636 cxx_dialect >= cxx11);
23637
23638 /* In C++2A and as extension for C++11 and above we allow
23639 default member initializers for bit-fields. */
23640 initializer = NULL_TREE;
23641 if (cxx_dialect >= cxx11
23642 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23643 || cp_lexer_next_token_is (parser->lexer,
23644 CPP_OPEN_BRACE)))
23645 {
23646 location_t loc
23647 = cp_lexer_peek_token (parser->lexer)->location;
23648 if (cxx_dialect < cxx2a
23649 && !in_system_header_at (loc)
23650 && identifier != NULL_TREE)
23651 pedwarn (loc, 0,
23652 "default member initializers for bit-fields "
23653 "only available with -std=c++2a or "
23654 "-std=gnu++2a");
23655
23656 initializer = cp_parser_save_nsdmi (parser);
23657 if (identifier == NULL_TREE)
23658 {
23659 error_at (loc, "default member initializer for "
23660 "unnamed bit-field");
23661 initializer = NULL_TREE;
23662 }
23663 }
23664 else
23665 {
23666 /* Look for attributes that apply to the bitfield after
23667 the `:' token and width. This is where GCC used to
23668 parse attributes in the past, pedwarn if there is
23669 a std attribute. */
23670 if (cp_next_tokens_can_be_std_attribute_p (parser))
23671 pedwarn (input_location, OPT_Wpedantic,
23672 "ISO C++ allows bit-field attributes only "
23673 "before the %<:%> token");
23674
23675 late_attributes = cp_parser_attributes_opt (parser);
23676 }
23677
23678 attributes = chainon (attributes, late_attributes);
23679
23680 /* Remember which attributes are prefix attributes and
23681 which are not. */
23682 first_attribute = attributes;
23683 /* Combine the attributes. */
23684 attributes = chainon (prefix_attributes, attributes);
23685
23686 /* Create the bitfield declaration. */
23687 decl = grokbitfield (identifier
23688 ? make_id_declarator (NULL_TREE,
23689 identifier,
23690 sfk_none)
23691 : NULL,
23692 &decl_specifiers,
23693 width, initializer,
23694 attributes);
23695 }
23696 else
23697 {
23698 cp_declarator *declarator;
23699 tree asm_specification;
23700 int ctor_dtor_or_conv_p;
23701
23702 /* Parse the declarator. */
23703 declarator
23704 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23705 &ctor_dtor_or_conv_p,
23706 /*parenthesized_p=*/NULL,
23707 /*member_p=*/true,
23708 friend_p);
23709
23710 /* If something went wrong parsing the declarator, make sure
23711 that we at least consume some tokens. */
23712 if (declarator == cp_error_declarator)
23713 {
23714 /* Skip to the end of the statement. */
23715 cp_parser_skip_to_end_of_statement (parser);
23716 /* If the next token is not a semicolon, that is
23717 probably because we just skipped over the body of
23718 a function. So, we consume a semicolon if
23719 present, but do not issue an error message if it
23720 is not present. */
23721 if (cp_lexer_next_token_is (parser->lexer,
23722 CPP_SEMICOLON))
23723 cp_lexer_consume_token (parser->lexer);
23724 goto out;
23725 }
23726
23727 if (declares_class_or_enum & 2)
23728 cp_parser_check_for_definition_in_return_type
23729 (declarator, decl_specifiers.type,
23730 decl_specifiers.locations[ds_type_spec]);
23731
23732 /* Look for an asm-specification. */
23733 asm_specification = cp_parser_asm_specification_opt (parser);
23734 /* Look for attributes that apply to the declaration. */
23735 attributes = cp_parser_attributes_opt (parser);
23736 /* Remember which attributes are prefix attributes and
23737 which are not. */
23738 first_attribute = attributes;
23739 /* Combine the attributes. */
23740 attributes = chainon (prefix_attributes, attributes);
23741
23742 /* If it's an `=', then we have a constant-initializer or a
23743 pure-specifier. It is not correct to parse the
23744 initializer before registering the member declaration
23745 since the member declaration should be in scope while
23746 its initializer is processed. However, the rest of the
23747 front end does not yet provide an interface that allows
23748 us to handle this correctly. */
23749 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23750 {
23751 /* In [class.mem]:
23752
23753 A pure-specifier shall be used only in the declaration of
23754 a virtual function.
23755
23756 A member-declarator can contain a constant-initializer
23757 only if it declares a static member of integral or
23758 enumeration type.
23759
23760 Therefore, if the DECLARATOR is for a function, we look
23761 for a pure-specifier; otherwise, we look for a
23762 constant-initializer. When we call `grokfield', it will
23763 perform more stringent semantics checks. */
23764 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23765 if (function_declarator_p (declarator)
23766 || (decl_specifiers.type
23767 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23768 && declarator->kind == cdk_id
23769 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23770 == FUNCTION_TYPE)))
23771 initializer = cp_parser_pure_specifier (parser);
23772 else if (decl_specifiers.storage_class != sc_static)
23773 initializer = cp_parser_save_nsdmi (parser);
23774 else if (cxx_dialect >= cxx11)
23775 {
23776 bool nonconst;
23777 /* Don't require a constant rvalue in C++11, since we
23778 might want a reference constant. We'll enforce
23779 constancy later. */
23780 cp_lexer_consume_token (parser->lexer);
23781 /* Parse the initializer. */
23782 initializer = cp_parser_initializer_clause (parser,
23783 &nonconst);
23784 }
23785 else
23786 /* Parse the initializer. */
23787 initializer = cp_parser_constant_initializer (parser);
23788 }
23789 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23790 && !function_declarator_p (declarator))
23791 {
23792 bool x;
23793 if (decl_specifiers.storage_class != sc_static)
23794 initializer = cp_parser_save_nsdmi (parser);
23795 else
23796 initializer = cp_parser_initializer (parser, &x, &x);
23797 }
23798 /* Otherwise, there is no initializer. */
23799 else
23800 initializer = NULL_TREE;
23801
23802 /* See if we are probably looking at a function
23803 definition. We are certainly not looking at a
23804 member-declarator. Calling `grokfield' has
23805 side-effects, so we must not do it unless we are sure
23806 that we are looking at a member-declarator. */
23807 if (cp_parser_token_starts_function_definition_p
23808 (cp_lexer_peek_token (parser->lexer)))
23809 {
23810 /* The grammar does not allow a pure-specifier to be
23811 used when a member function is defined. (It is
23812 possible that this fact is an oversight in the
23813 standard, since a pure function may be defined
23814 outside of the class-specifier. */
23815 if (initializer && initializer_token_start)
23816 error_at (initializer_token_start->location,
23817 "pure-specifier on function-definition");
23818 decl = cp_parser_save_member_function_body (parser,
23819 &decl_specifiers,
23820 declarator,
23821 attributes);
23822 if (parser->fully_implicit_function_template_p)
23823 decl = finish_fully_implicit_template (parser, decl);
23824 /* If the member was not a friend, declare it here. */
23825 if (!friend_p)
23826 finish_member_declaration (decl);
23827 /* Peek at the next token. */
23828 token = cp_lexer_peek_token (parser->lexer);
23829 /* If the next token is a semicolon, consume it. */
23830 if (token->type == CPP_SEMICOLON)
23831 {
23832 location_t semicolon_loc
23833 = cp_lexer_consume_token (parser->lexer)->location;
23834 gcc_rich_location richloc (semicolon_loc);
23835 richloc.add_fixit_remove ();
23836 warning_at (&richloc, OPT_Wextra_semi,
23837 "extra %<;%> after in-class "
23838 "function definition");
23839 }
23840 goto out;
23841 }
23842 else
23843 if (declarator->kind == cdk_function)
23844 declarator->id_loc = token->location;
23845 /* Create the declaration. */
23846 decl = grokfield (declarator, &decl_specifiers,
23847 initializer, /*init_const_expr_p=*/true,
23848 asm_specification, attributes);
23849 if (parser->fully_implicit_function_template_p)
23850 {
23851 if (friend_p)
23852 finish_fully_implicit_template (parser, 0);
23853 else
23854 decl = finish_fully_implicit_template (parser, decl);
23855 }
23856 }
23857
23858 cp_finalize_omp_declare_simd (parser, decl);
23859 cp_finalize_oacc_routine (parser, decl, false);
23860
23861 /* Reset PREFIX_ATTRIBUTES. */
23862 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23863 attributes = TREE_CHAIN (attributes);
23864 if (attributes)
23865 TREE_CHAIN (attributes) = NULL_TREE;
23866
23867 /* If there is any qualification still in effect, clear it
23868 now; we will be starting fresh with the next declarator. */
23869 parser->scope = NULL_TREE;
23870 parser->qualifying_scope = NULL_TREE;
23871 parser->object_scope = NULL_TREE;
23872 /* If it's a `,', then there are more declarators. */
23873 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23874 {
23875 cp_lexer_consume_token (parser->lexer);
23876 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23877 {
23878 cp_token *token = cp_lexer_previous_token (parser->lexer);
23879 gcc_rich_location richloc (token->location);
23880 richloc.add_fixit_remove ();
23881 error_at (&richloc, "stray %<,%> at end of "
23882 "member declaration");
23883 }
23884 }
23885 /* If the next token isn't a `;', then we have a parse error. */
23886 else if (cp_lexer_next_token_is_not (parser->lexer,
23887 CPP_SEMICOLON))
23888 {
23889 /* The next token might be a ways away from where the
23890 actual semicolon is missing. Find the previous token
23891 and use that for our error position. */
23892 cp_token *token = cp_lexer_previous_token (parser->lexer);
23893 gcc_rich_location richloc (token->location);
23894 richloc.add_fixit_insert_after (";");
23895 error_at (&richloc, "expected %<;%> at end of "
23896 "member declaration");
23897
23898 /* Assume that the user meant to provide a semicolon. If
23899 we were to cp_parser_skip_to_end_of_statement, we might
23900 skip to a semicolon inside a member function definition
23901 and issue nonsensical error messages. */
23902 assume_semicolon = true;
23903 }
23904
23905 if (decl)
23906 {
23907 /* Add DECL to the list of members. */
23908 if (!friend_p
23909 /* Explicitly include, eg, NSDMIs, for better error
23910 recovery (c++/58650). */
23911 || !DECL_DECLARES_FUNCTION_P (decl))
23912 finish_member_declaration (decl);
23913
23914 if (TREE_CODE (decl) == FUNCTION_DECL)
23915 cp_parser_save_default_args (parser, decl);
23916 else if (TREE_CODE (decl) == FIELD_DECL
23917 && DECL_INITIAL (decl))
23918 /* Add DECL to the queue of NSDMI to be parsed later. */
23919 vec_safe_push (unparsed_nsdmis, decl);
23920 }
23921
23922 if (assume_semicolon)
23923 goto out;
23924 }
23925 }
23926
23927 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23928 out:
23929 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23930 }
23931
23932 /* Parse a pure-specifier.
23933
23934 pure-specifier:
23935 = 0
23936
23937 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23938 Otherwise, ERROR_MARK_NODE is returned. */
23939
23940 static tree
23941 cp_parser_pure_specifier (cp_parser* parser)
23942 {
23943 cp_token *token;
23944
23945 /* Look for the `=' token. */
23946 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23947 return error_mark_node;
23948 /* Look for the `0' token. */
23949 token = cp_lexer_peek_token (parser->lexer);
23950
23951 if (token->type == CPP_EOF
23952 || token->type == CPP_PRAGMA_EOL)
23953 return error_mark_node;
23954
23955 cp_lexer_consume_token (parser->lexer);
23956
23957 /* Accept = default or = delete in c++0x mode. */
23958 if (token->keyword == RID_DEFAULT
23959 || token->keyword == RID_DELETE)
23960 {
23961 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23962 return token->u.value;
23963 }
23964
23965 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23966 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23967 {
23968 cp_parser_error (parser,
23969 "invalid pure specifier (only %<= 0%> is allowed)");
23970 cp_parser_skip_to_end_of_statement (parser);
23971 return error_mark_node;
23972 }
23973 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23974 {
23975 error_at (token->location, "templates may not be %<virtual%>");
23976 return error_mark_node;
23977 }
23978
23979 return integer_zero_node;
23980 }
23981
23982 /* Parse a constant-initializer.
23983
23984 constant-initializer:
23985 = constant-expression
23986
23987 Returns a representation of the constant-expression. */
23988
23989 static tree
23990 cp_parser_constant_initializer (cp_parser* parser)
23991 {
23992 /* Look for the `=' token. */
23993 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23994 return error_mark_node;
23995
23996 /* It is invalid to write:
23997
23998 struct S { static const int i = { 7 }; };
23999
24000 */
24001 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24002 {
24003 cp_parser_error (parser,
24004 "a brace-enclosed initializer is not allowed here");
24005 /* Consume the opening brace. */
24006 matching_braces braces;
24007 braces.consume_open (parser);
24008 /* Skip the initializer. */
24009 cp_parser_skip_to_closing_brace (parser);
24010 /* Look for the trailing `}'. */
24011 braces.require_close (parser);
24012
24013 return error_mark_node;
24014 }
24015
24016 return cp_parser_constant_expression (parser);
24017 }
24018
24019 /* Derived classes [gram.class.derived] */
24020
24021 /* Parse a base-clause.
24022
24023 base-clause:
24024 : base-specifier-list
24025
24026 base-specifier-list:
24027 base-specifier ... [opt]
24028 base-specifier-list , base-specifier ... [opt]
24029
24030 Returns a TREE_LIST representing the base-classes, in the order in
24031 which they were declared. The representation of each node is as
24032 described by cp_parser_base_specifier.
24033
24034 In the case that no bases are specified, this function will return
24035 NULL_TREE, not ERROR_MARK_NODE. */
24036
24037 static tree
24038 cp_parser_base_clause (cp_parser* parser)
24039 {
24040 tree bases = NULL_TREE;
24041
24042 /* Look for the `:' that begins the list. */
24043 cp_parser_require (parser, CPP_COLON, RT_COLON);
24044
24045 /* Scan the base-specifier-list. */
24046 while (true)
24047 {
24048 cp_token *token;
24049 tree base;
24050 bool pack_expansion_p = false;
24051
24052 /* Look for the base-specifier. */
24053 base = cp_parser_base_specifier (parser);
24054 /* Look for the (optional) ellipsis. */
24055 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24056 {
24057 /* Consume the `...'. */
24058 cp_lexer_consume_token (parser->lexer);
24059
24060 pack_expansion_p = true;
24061 }
24062
24063 /* Add BASE to the front of the list. */
24064 if (base && base != error_mark_node)
24065 {
24066 if (pack_expansion_p)
24067 /* Make this a pack expansion type. */
24068 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24069
24070 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24071 {
24072 TREE_CHAIN (base) = bases;
24073 bases = base;
24074 }
24075 }
24076 /* Peek at the next token. */
24077 token = cp_lexer_peek_token (parser->lexer);
24078 /* If it's not a comma, then the list is complete. */
24079 if (token->type != CPP_COMMA)
24080 break;
24081 /* Consume the `,'. */
24082 cp_lexer_consume_token (parser->lexer);
24083 }
24084
24085 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24086 base class had a qualified name. However, the next name that
24087 appears is certainly not qualified. */
24088 parser->scope = NULL_TREE;
24089 parser->qualifying_scope = NULL_TREE;
24090 parser->object_scope = NULL_TREE;
24091
24092 return nreverse (bases);
24093 }
24094
24095 /* Parse a base-specifier.
24096
24097 base-specifier:
24098 :: [opt] nested-name-specifier [opt] class-name
24099 virtual access-specifier [opt] :: [opt] nested-name-specifier
24100 [opt] class-name
24101 access-specifier virtual [opt] :: [opt] nested-name-specifier
24102 [opt] class-name
24103
24104 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24105 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24106 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24107 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24108
24109 static tree
24110 cp_parser_base_specifier (cp_parser* parser)
24111 {
24112 cp_token *token;
24113 bool done = false;
24114 bool virtual_p = false;
24115 bool duplicate_virtual_error_issued_p = false;
24116 bool duplicate_access_error_issued_p = false;
24117 bool class_scope_p, template_p;
24118 tree access = access_default_node;
24119 tree type;
24120
24121 /* Process the optional `virtual' and `access-specifier'. */
24122 while (!done)
24123 {
24124 /* Peek at the next token. */
24125 token = cp_lexer_peek_token (parser->lexer);
24126 /* Process `virtual'. */
24127 switch (token->keyword)
24128 {
24129 case RID_VIRTUAL:
24130 /* If `virtual' appears more than once, issue an error. */
24131 if (virtual_p && !duplicate_virtual_error_issued_p)
24132 {
24133 cp_parser_error (parser,
24134 "%<virtual%> specified more than once in base-specifier");
24135 duplicate_virtual_error_issued_p = true;
24136 }
24137
24138 virtual_p = true;
24139
24140 /* Consume the `virtual' token. */
24141 cp_lexer_consume_token (parser->lexer);
24142
24143 break;
24144
24145 case RID_PUBLIC:
24146 case RID_PROTECTED:
24147 case RID_PRIVATE:
24148 /* If more than one access specifier appears, issue an
24149 error. */
24150 if (access != access_default_node
24151 && !duplicate_access_error_issued_p)
24152 {
24153 cp_parser_error (parser,
24154 "more than one access specifier in base-specifier");
24155 duplicate_access_error_issued_p = true;
24156 }
24157
24158 access = ridpointers[(int) token->keyword];
24159
24160 /* Consume the access-specifier. */
24161 cp_lexer_consume_token (parser->lexer);
24162
24163 break;
24164
24165 default:
24166 done = true;
24167 break;
24168 }
24169 }
24170 /* It is not uncommon to see programs mechanically, erroneously, use
24171 the 'typename' keyword to denote (dependent) qualified types
24172 as base classes. */
24173 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24174 {
24175 token = cp_lexer_peek_token (parser->lexer);
24176 if (!processing_template_decl)
24177 error_at (token->location,
24178 "keyword %<typename%> not allowed outside of templates");
24179 else
24180 error_at (token->location,
24181 "keyword %<typename%> not allowed in this context "
24182 "(the base class is implicitly a type)");
24183 cp_lexer_consume_token (parser->lexer);
24184 }
24185
24186 /* Look for the optional `::' operator. */
24187 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24188 /* Look for the nested-name-specifier. The simplest way to
24189 implement:
24190
24191 [temp.res]
24192
24193 The keyword `typename' is not permitted in a base-specifier or
24194 mem-initializer; in these contexts a qualified name that
24195 depends on a template-parameter is implicitly assumed to be a
24196 type name.
24197
24198 is to pretend that we have seen the `typename' keyword at this
24199 point. */
24200 cp_parser_nested_name_specifier_opt (parser,
24201 /*typename_keyword_p=*/true,
24202 /*check_dependency_p=*/true,
24203 /*type_p=*/true,
24204 /*is_declaration=*/true);
24205 /* If the base class is given by a qualified name, assume that names
24206 we see are type names or templates, as appropriate. */
24207 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24208 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24209
24210 if (!parser->scope
24211 && cp_lexer_next_token_is_decltype (parser->lexer))
24212 /* DR 950 allows decltype as a base-specifier. */
24213 type = cp_parser_decltype (parser);
24214 else
24215 {
24216 /* Otherwise, look for the class-name. */
24217 type = cp_parser_class_name (parser,
24218 class_scope_p,
24219 template_p,
24220 typename_type,
24221 /*check_dependency_p=*/true,
24222 /*class_head_p=*/false,
24223 /*is_declaration=*/true);
24224 type = TREE_TYPE (type);
24225 }
24226
24227 if (type == error_mark_node)
24228 return error_mark_node;
24229
24230 return finish_base_specifier (type, access, virtual_p);
24231 }
24232
24233 /* Exception handling [gram.exception] */
24234
24235 /* Parse an (optional) noexcept-specification.
24236
24237 noexcept-specification:
24238 noexcept ( constant-expression ) [opt]
24239
24240 If no noexcept-specification is present, returns NULL_TREE.
24241 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24242 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24243 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24244 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24245 in which case a boolean condition is returned instead. */
24246
24247 static tree
24248 cp_parser_noexcept_specification_opt (cp_parser* parser,
24249 bool require_constexpr,
24250 bool* consumed_expr,
24251 bool return_cond)
24252 {
24253 cp_token *token;
24254 const char *saved_message;
24255
24256 /* Peek at the next token. */
24257 token = cp_lexer_peek_token (parser->lexer);
24258
24259 /* Is it a noexcept-specification? */
24260 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24261 {
24262 tree expr;
24263 cp_lexer_consume_token (parser->lexer);
24264
24265 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24266 {
24267 matching_parens parens;
24268 parens.consume_open (parser);
24269
24270 if (require_constexpr)
24271 {
24272 /* Types may not be defined in an exception-specification. */
24273 saved_message = parser->type_definition_forbidden_message;
24274 parser->type_definition_forbidden_message
24275 = G_("types may not be defined in an exception-specification");
24276
24277 expr = cp_parser_constant_expression (parser);
24278
24279 /* Restore the saved message. */
24280 parser->type_definition_forbidden_message = saved_message;
24281 }
24282 else
24283 {
24284 expr = cp_parser_expression (parser);
24285 *consumed_expr = true;
24286 }
24287
24288 parens.require_close (parser);
24289 }
24290 else
24291 {
24292 expr = boolean_true_node;
24293 if (!require_constexpr)
24294 *consumed_expr = false;
24295 }
24296
24297 /* We cannot build a noexcept-spec right away because this will check
24298 that expr is a constexpr. */
24299 if (!return_cond)
24300 return build_noexcept_spec (expr, tf_warning_or_error);
24301 else
24302 return expr;
24303 }
24304 else
24305 return NULL_TREE;
24306 }
24307
24308 /* Parse an (optional) exception-specification.
24309
24310 exception-specification:
24311 throw ( type-id-list [opt] )
24312
24313 Returns a TREE_LIST representing the exception-specification. The
24314 TREE_VALUE of each node is a type. */
24315
24316 static tree
24317 cp_parser_exception_specification_opt (cp_parser* parser)
24318 {
24319 cp_token *token;
24320 tree type_id_list;
24321 const char *saved_message;
24322
24323 /* Peek at the next token. */
24324 token = cp_lexer_peek_token (parser->lexer);
24325
24326 /* Is it a noexcept-specification? */
24327 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24328 false);
24329 if (type_id_list != NULL_TREE)
24330 return type_id_list;
24331
24332 /* If it's not `throw', then there's no exception-specification. */
24333 if (!cp_parser_is_keyword (token, RID_THROW))
24334 return NULL_TREE;
24335
24336 location_t loc = token->location;
24337
24338 /* Consume the `throw'. */
24339 cp_lexer_consume_token (parser->lexer);
24340
24341 /* Look for the `('. */
24342 matching_parens parens;
24343 parens.require_open (parser);
24344
24345 /* Peek at the next token. */
24346 token = cp_lexer_peek_token (parser->lexer);
24347 /* If it's not a `)', then there is a type-id-list. */
24348 if (token->type != CPP_CLOSE_PAREN)
24349 {
24350 /* Types may not be defined in an exception-specification. */
24351 saved_message = parser->type_definition_forbidden_message;
24352 parser->type_definition_forbidden_message
24353 = G_("types may not be defined in an exception-specification");
24354 /* Parse the type-id-list. */
24355 type_id_list = cp_parser_type_id_list (parser);
24356 /* Restore the saved message. */
24357 parser->type_definition_forbidden_message = saved_message;
24358
24359 if (cxx_dialect >= cxx17)
24360 {
24361 error_at (loc, "ISO C++17 does not allow dynamic exception "
24362 "specifications");
24363 type_id_list = NULL_TREE;
24364 }
24365 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24366 warning_at (loc, OPT_Wdeprecated,
24367 "dynamic exception specifications are deprecated in "
24368 "C++11");
24369 }
24370 /* In C++17, throw() is equivalent to noexcept (true). throw()
24371 is deprecated in C++11 and above as well, but is still widely used,
24372 so don't warn about it yet. */
24373 else if (cxx_dialect >= cxx17)
24374 type_id_list = noexcept_true_spec;
24375 else
24376 type_id_list = empty_except_spec;
24377
24378 /* Look for the `)'. */
24379 parens.require_close (parser);
24380
24381 return type_id_list;
24382 }
24383
24384 /* Parse an (optional) type-id-list.
24385
24386 type-id-list:
24387 type-id ... [opt]
24388 type-id-list , type-id ... [opt]
24389
24390 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24391 in the order that the types were presented. */
24392
24393 static tree
24394 cp_parser_type_id_list (cp_parser* parser)
24395 {
24396 tree types = NULL_TREE;
24397
24398 while (true)
24399 {
24400 cp_token *token;
24401 tree type;
24402
24403 token = cp_lexer_peek_token (parser->lexer);
24404
24405 /* Get the next type-id. */
24406 type = cp_parser_type_id (parser);
24407 /* Check for invalid 'auto'. */
24408 if (flag_concepts && type_uses_auto (type))
24409 {
24410 error_at (token->location,
24411 "invalid use of %<auto%> in exception-specification");
24412 type = error_mark_node;
24413 }
24414 /* Parse the optional ellipsis. */
24415 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24416 {
24417 /* Consume the `...'. */
24418 cp_lexer_consume_token (parser->lexer);
24419
24420 /* Turn the type into a pack expansion expression. */
24421 type = make_pack_expansion (type);
24422 }
24423 /* Add it to the list. */
24424 types = add_exception_specifier (types, type, /*complain=*/1);
24425 /* Peek at the next token. */
24426 token = cp_lexer_peek_token (parser->lexer);
24427 /* If it is not a `,', we are done. */
24428 if (token->type != CPP_COMMA)
24429 break;
24430 /* Consume the `,'. */
24431 cp_lexer_consume_token (parser->lexer);
24432 }
24433
24434 return nreverse (types);
24435 }
24436
24437 /* Parse a try-block.
24438
24439 try-block:
24440 try compound-statement handler-seq */
24441
24442 static tree
24443 cp_parser_try_block (cp_parser* parser)
24444 {
24445 tree try_block;
24446
24447 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24448 if (parser->in_function_body
24449 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24450 error ("%<try%> in %<constexpr%> function");
24451
24452 try_block = begin_try_block ();
24453 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24454 finish_try_block (try_block);
24455 cp_parser_handler_seq (parser);
24456 finish_handler_sequence (try_block);
24457
24458 return try_block;
24459 }
24460
24461 /* Parse a function-try-block.
24462
24463 function-try-block:
24464 try ctor-initializer [opt] function-body handler-seq */
24465
24466 static void
24467 cp_parser_function_try_block (cp_parser* parser)
24468 {
24469 tree compound_stmt;
24470 tree try_block;
24471
24472 /* Look for the `try' keyword. */
24473 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24474 return;
24475 /* Let the rest of the front end know where we are. */
24476 try_block = begin_function_try_block (&compound_stmt);
24477 /* Parse the function-body. */
24478 cp_parser_ctor_initializer_opt_and_function_body
24479 (parser, /*in_function_try_block=*/true);
24480 /* We're done with the `try' part. */
24481 finish_function_try_block (try_block);
24482 /* Parse the handlers. */
24483 cp_parser_handler_seq (parser);
24484 /* We're done with the handlers. */
24485 finish_function_handler_sequence (try_block, compound_stmt);
24486 }
24487
24488 /* Parse a handler-seq.
24489
24490 handler-seq:
24491 handler handler-seq [opt] */
24492
24493 static void
24494 cp_parser_handler_seq (cp_parser* parser)
24495 {
24496 while (true)
24497 {
24498 cp_token *token;
24499
24500 /* Parse the handler. */
24501 cp_parser_handler (parser);
24502 /* Peek at the next token. */
24503 token = cp_lexer_peek_token (parser->lexer);
24504 /* If it's not `catch' then there are no more handlers. */
24505 if (!cp_parser_is_keyword (token, RID_CATCH))
24506 break;
24507 }
24508 }
24509
24510 /* Parse a handler.
24511
24512 handler:
24513 catch ( exception-declaration ) compound-statement */
24514
24515 static void
24516 cp_parser_handler (cp_parser* parser)
24517 {
24518 tree handler;
24519 tree declaration;
24520
24521 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24522 handler = begin_handler ();
24523 matching_parens parens;
24524 parens.require_open (parser);
24525 declaration = cp_parser_exception_declaration (parser);
24526 finish_handler_parms (declaration, handler);
24527 parens.require_close (parser);
24528 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24529 finish_handler (handler);
24530 }
24531
24532 /* Parse an exception-declaration.
24533
24534 exception-declaration:
24535 type-specifier-seq declarator
24536 type-specifier-seq abstract-declarator
24537 type-specifier-seq
24538 ...
24539
24540 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24541 ellipsis variant is used. */
24542
24543 static tree
24544 cp_parser_exception_declaration (cp_parser* parser)
24545 {
24546 cp_decl_specifier_seq type_specifiers;
24547 cp_declarator *declarator;
24548 const char *saved_message;
24549
24550 /* If it's an ellipsis, it's easy to handle. */
24551 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24552 {
24553 /* Consume the `...' token. */
24554 cp_lexer_consume_token (parser->lexer);
24555 return NULL_TREE;
24556 }
24557
24558 /* Types may not be defined in exception-declarations. */
24559 saved_message = parser->type_definition_forbidden_message;
24560 parser->type_definition_forbidden_message
24561 = G_("types may not be defined in exception-declarations");
24562
24563 /* Parse the type-specifier-seq. */
24564 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24565 /*is_trailing_return=*/false,
24566 &type_specifiers);
24567 /* If it's a `)', then there is no declarator. */
24568 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24569 declarator = NULL;
24570 else
24571 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24572 /*ctor_dtor_or_conv_p=*/NULL,
24573 /*parenthesized_p=*/NULL,
24574 /*member_p=*/false,
24575 /*friend_p=*/false);
24576
24577 /* Restore the saved message. */
24578 parser->type_definition_forbidden_message = saved_message;
24579
24580 if (!type_specifiers.any_specifiers_p)
24581 return error_mark_node;
24582
24583 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24584 }
24585
24586 /* Parse a throw-expression.
24587
24588 throw-expression:
24589 throw assignment-expression [opt]
24590
24591 Returns a THROW_EXPR representing the throw-expression. */
24592
24593 static tree
24594 cp_parser_throw_expression (cp_parser* parser)
24595 {
24596 tree expression;
24597 cp_token* token;
24598
24599 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24600 token = cp_lexer_peek_token (parser->lexer);
24601 /* Figure out whether or not there is an assignment-expression
24602 following the "throw" keyword. */
24603 if (token->type == CPP_COMMA
24604 || token->type == CPP_SEMICOLON
24605 || token->type == CPP_CLOSE_PAREN
24606 || token->type == CPP_CLOSE_SQUARE
24607 || token->type == CPP_CLOSE_BRACE
24608 || token->type == CPP_COLON)
24609 expression = NULL_TREE;
24610 else
24611 expression = cp_parser_assignment_expression (parser);
24612
24613 return build_throw (expression);
24614 }
24615
24616 /* GNU Extensions */
24617
24618 /* Parse an (optional) asm-specification.
24619
24620 asm-specification:
24621 asm ( string-literal )
24622
24623 If the asm-specification is present, returns a STRING_CST
24624 corresponding to the string-literal. Otherwise, returns
24625 NULL_TREE. */
24626
24627 static tree
24628 cp_parser_asm_specification_opt (cp_parser* parser)
24629 {
24630 cp_token *token;
24631 tree asm_specification;
24632
24633 /* Peek at the next token. */
24634 token = cp_lexer_peek_token (parser->lexer);
24635 /* If the next token isn't the `asm' keyword, then there's no
24636 asm-specification. */
24637 if (!cp_parser_is_keyword (token, RID_ASM))
24638 return NULL_TREE;
24639
24640 /* Consume the `asm' token. */
24641 cp_lexer_consume_token (parser->lexer);
24642 /* Look for the `('. */
24643 matching_parens parens;
24644 parens.require_open (parser);
24645
24646 /* Look for the string-literal. */
24647 asm_specification = cp_parser_string_literal (parser, false, false);
24648
24649 /* Look for the `)'. */
24650 parens.require_close (parser);
24651
24652 return asm_specification;
24653 }
24654
24655 /* Parse an asm-operand-list.
24656
24657 asm-operand-list:
24658 asm-operand
24659 asm-operand-list , asm-operand
24660
24661 asm-operand:
24662 string-literal ( expression )
24663 [ string-literal ] string-literal ( expression )
24664
24665 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24666 each node is the expression. The TREE_PURPOSE is itself a
24667 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24668 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24669 is a STRING_CST for the string literal before the parenthesis. Returns
24670 ERROR_MARK_NODE if any of the operands are invalid. */
24671
24672 static tree
24673 cp_parser_asm_operand_list (cp_parser* parser)
24674 {
24675 tree asm_operands = NULL_TREE;
24676 bool invalid_operands = false;
24677
24678 while (true)
24679 {
24680 tree string_literal;
24681 tree expression;
24682 tree name;
24683
24684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24685 {
24686 /* Consume the `[' token. */
24687 cp_lexer_consume_token (parser->lexer);
24688 /* Read the operand name. */
24689 name = cp_parser_identifier (parser);
24690 if (name != error_mark_node)
24691 name = build_string (IDENTIFIER_LENGTH (name),
24692 IDENTIFIER_POINTER (name));
24693 /* Look for the closing `]'. */
24694 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24695 }
24696 else
24697 name = NULL_TREE;
24698 /* Look for the string-literal. */
24699 string_literal = cp_parser_string_literal (parser, false, false);
24700
24701 /* Look for the `('. */
24702 matching_parens parens;
24703 parens.require_open (parser);
24704 /* Parse the expression. */
24705 expression = cp_parser_expression (parser);
24706 /* Look for the `)'. */
24707 parens.require_close (parser);
24708
24709 if (name == error_mark_node
24710 || string_literal == error_mark_node
24711 || expression == error_mark_node)
24712 invalid_operands = true;
24713
24714 /* Add this operand to the list. */
24715 asm_operands = tree_cons (build_tree_list (name, string_literal),
24716 expression,
24717 asm_operands);
24718 /* If the next token is not a `,', there are no more
24719 operands. */
24720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24721 break;
24722 /* Consume the `,'. */
24723 cp_lexer_consume_token (parser->lexer);
24724 }
24725
24726 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24727 }
24728
24729 /* Parse an asm-clobber-list.
24730
24731 asm-clobber-list:
24732 string-literal
24733 asm-clobber-list , string-literal
24734
24735 Returns a TREE_LIST, indicating the clobbers in the order that they
24736 appeared. The TREE_VALUE of each node is a STRING_CST. */
24737
24738 static tree
24739 cp_parser_asm_clobber_list (cp_parser* parser)
24740 {
24741 tree clobbers = NULL_TREE;
24742
24743 while (true)
24744 {
24745 tree string_literal;
24746
24747 /* Look for the string literal. */
24748 string_literal = cp_parser_string_literal (parser, false, false);
24749 /* Add it to the list. */
24750 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24751 /* If the next token is not a `,', then the list is
24752 complete. */
24753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24754 break;
24755 /* Consume the `,' token. */
24756 cp_lexer_consume_token (parser->lexer);
24757 }
24758
24759 return clobbers;
24760 }
24761
24762 /* Parse an asm-label-list.
24763
24764 asm-label-list:
24765 identifier
24766 asm-label-list , identifier
24767
24768 Returns a TREE_LIST, indicating the labels in the order that they
24769 appeared. The TREE_VALUE of each node is a label. */
24770
24771 static tree
24772 cp_parser_asm_label_list (cp_parser* parser)
24773 {
24774 tree labels = NULL_TREE;
24775
24776 while (true)
24777 {
24778 tree identifier, label, name;
24779
24780 /* Look for the identifier. */
24781 identifier = cp_parser_identifier (parser);
24782 if (!error_operand_p (identifier))
24783 {
24784 label = lookup_label (identifier);
24785 if (TREE_CODE (label) == LABEL_DECL)
24786 {
24787 TREE_USED (label) = 1;
24788 check_goto (label);
24789 name = build_string (IDENTIFIER_LENGTH (identifier),
24790 IDENTIFIER_POINTER (identifier));
24791 labels = tree_cons (name, label, labels);
24792 }
24793 }
24794 /* If the next token is not a `,', then the list is
24795 complete. */
24796 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24797 break;
24798 /* Consume the `,' token. */
24799 cp_lexer_consume_token (parser->lexer);
24800 }
24801
24802 return nreverse (labels);
24803 }
24804
24805 /* Return TRUE iff the next tokens in the stream are possibly the
24806 beginning of a GNU extension attribute. */
24807
24808 static bool
24809 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24810 {
24811 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24812 }
24813
24814 /* Return TRUE iff the next tokens in the stream are possibly the
24815 beginning of a standard C++-11 attribute specifier. */
24816
24817 static bool
24818 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24819 {
24820 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24821 }
24822
24823 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24824 beginning of a standard C++-11 attribute specifier. */
24825
24826 static bool
24827 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24828 {
24829 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24830
24831 return (cxx_dialect >= cxx11
24832 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24833 || (token->type == CPP_OPEN_SQUARE
24834 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24835 && token->type == CPP_OPEN_SQUARE)));
24836 }
24837
24838 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24839 beginning of a GNU extension attribute. */
24840
24841 static bool
24842 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24843 {
24844 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24845
24846 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24847 }
24848
24849 /* Return true iff the next tokens can be the beginning of either a
24850 GNU attribute list, or a standard C++11 attribute sequence. */
24851
24852 static bool
24853 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24854 {
24855 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24856 || cp_next_tokens_can_be_std_attribute_p (parser));
24857 }
24858
24859 /* Return true iff the next Nth tokens can be the beginning of either
24860 a GNU attribute list, or a standard C++11 attribute sequence. */
24861
24862 static bool
24863 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24864 {
24865 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24866 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24867 }
24868
24869 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24870 of GNU attributes, or return NULL. */
24871
24872 static tree
24873 cp_parser_attributes_opt (cp_parser *parser)
24874 {
24875 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24876 return cp_parser_gnu_attributes_opt (parser);
24877 return cp_parser_std_attribute_spec_seq (parser);
24878 }
24879
24880 /* Parse an (optional) series of attributes.
24881
24882 attributes:
24883 attributes attribute
24884
24885 attribute:
24886 __attribute__ (( attribute-list [opt] ))
24887
24888 The return value is as for cp_parser_gnu_attribute_list. */
24889
24890 static tree
24891 cp_parser_gnu_attributes_opt (cp_parser* parser)
24892 {
24893 tree attributes = NULL_TREE;
24894
24895 while (true)
24896 {
24897 cp_token *token;
24898 tree attribute_list;
24899 bool ok = true;
24900
24901 /* Peek at the next token. */
24902 token = cp_lexer_peek_token (parser->lexer);
24903 /* If it's not `__attribute__', then we're done. */
24904 if (token->keyword != RID_ATTRIBUTE)
24905 break;
24906
24907 /* Consume the `__attribute__' keyword. */
24908 cp_lexer_consume_token (parser->lexer);
24909 /* Look for the two `(' tokens. */
24910 matching_parens outer_parens;
24911 outer_parens.require_open (parser);
24912 matching_parens inner_parens;
24913 inner_parens.require_open (parser);
24914
24915 /* Peek at the next token. */
24916 token = cp_lexer_peek_token (parser->lexer);
24917 if (token->type != CPP_CLOSE_PAREN)
24918 /* Parse the attribute-list. */
24919 attribute_list = cp_parser_gnu_attribute_list (parser);
24920 else
24921 /* If the next token is a `)', then there is no attribute
24922 list. */
24923 attribute_list = NULL;
24924
24925 /* Look for the two `)' tokens. */
24926 if (!inner_parens.require_close (parser))
24927 ok = false;
24928 if (!outer_parens.require_close (parser))
24929 ok = false;
24930 if (!ok)
24931 cp_parser_skip_to_end_of_statement (parser);
24932
24933 /* Add these new attributes to the list. */
24934 attributes = chainon (attributes, attribute_list);
24935 }
24936
24937 return attributes;
24938 }
24939
24940 /* Parse a GNU attribute-list.
24941
24942 attribute-list:
24943 attribute
24944 attribute-list , attribute
24945
24946 attribute:
24947 identifier
24948 identifier ( identifier )
24949 identifier ( identifier , expression-list )
24950 identifier ( expression-list )
24951
24952 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24953 to an attribute. The TREE_PURPOSE of each node is the identifier
24954 indicating which attribute is in use. The TREE_VALUE represents
24955 the arguments, if any. */
24956
24957 static tree
24958 cp_parser_gnu_attribute_list (cp_parser* parser)
24959 {
24960 tree attribute_list = NULL_TREE;
24961 bool save_translate_strings_p = parser->translate_strings_p;
24962
24963 parser->translate_strings_p = false;
24964 while (true)
24965 {
24966 cp_token *token;
24967 tree identifier;
24968 tree attribute;
24969
24970 /* Look for the identifier. We also allow keywords here; for
24971 example `__attribute__ ((const))' is legal. */
24972 token = cp_lexer_peek_token (parser->lexer);
24973 if (token->type == CPP_NAME
24974 || token->type == CPP_KEYWORD)
24975 {
24976 tree arguments = NULL_TREE;
24977
24978 /* Consume the token, but save it since we need it for the
24979 SIMD enabled function parsing. */
24980 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24981
24982 /* Save away the identifier that indicates which attribute
24983 this is. */
24984 identifier = (token->type == CPP_KEYWORD)
24985 /* For keywords, use the canonical spelling, not the
24986 parsed identifier. */
24987 ? ridpointers[(int) token->keyword]
24988 : id_token->u.value;
24989
24990 identifier = canonicalize_attr_name (identifier);
24991 attribute = build_tree_list (identifier, NULL_TREE);
24992
24993 /* Peek at the next token. */
24994 token = cp_lexer_peek_token (parser->lexer);
24995 /* If it's an `(', then parse the attribute arguments. */
24996 if (token->type == CPP_OPEN_PAREN)
24997 {
24998 vec<tree, va_gc> *vec;
24999 int attr_flag = (attribute_takes_identifier_p (identifier)
25000 ? id_attr : normal_attr);
25001 vec = cp_parser_parenthesized_expression_list
25002 (parser, attr_flag, /*cast_p=*/false,
25003 /*allow_expansion_p=*/false,
25004 /*non_constant_p=*/NULL);
25005 if (vec == NULL)
25006 arguments = error_mark_node;
25007 else
25008 {
25009 arguments = build_tree_list_vec (vec);
25010 release_tree_vector (vec);
25011 }
25012 /* Save the arguments away. */
25013 TREE_VALUE (attribute) = arguments;
25014 }
25015
25016 if (arguments != error_mark_node)
25017 {
25018 /* Add this attribute to the list. */
25019 TREE_CHAIN (attribute) = attribute_list;
25020 attribute_list = attribute;
25021 }
25022
25023 token = cp_lexer_peek_token (parser->lexer);
25024 }
25025 /* Now, look for more attributes. If the next token isn't a
25026 `,', we're done. */
25027 if (token->type != CPP_COMMA)
25028 break;
25029
25030 /* Consume the comma and keep going. */
25031 cp_lexer_consume_token (parser->lexer);
25032 }
25033 parser->translate_strings_p = save_translate_strings_p;
25034
25035 /* We built up the list in reverse order. */
25036 return nreverse (attribute_list);
25037 }
25038
25039 /* Parse a standard C++11 attribute.
25040
25041 The returned representation is a TREE_LIST which TREE_PURPOSE is
25042 the scoped name of the attribute, and the TREE_VALUE is its
25043 arguments list.
25044
25045 Note that the scoped name of the attribute is itself a TREE_LIST
25046 which TREE_PURPOSE is the namespace of the attribute, and
25047 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25048 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25049 and which TREE_PURPOSE is directly the attribute name.
25050
25051 Clients of the attribute code should use get_attribute_namespace
25052 and get_attribute_name to get the actual namespace and name of
25053 attributes, regardless of their being GNU or C++11 attributes.
25054
25055 attribute:
25056 attribute-token attribute-argument-clause [opt]
25057
25058 attribute-token:
25059 identifier
25060 attribute-scoped-token
25061
25062 attribute-scoped-token:
25063 attribute-namespace :: identifier
25064
25065 attribute-namespace:
25066 identifier
25067
25068 attribute-argument-clause:
25069 ( balanced-token-seq )
25070
25071 balanced-token-seq:
25072 balanced-token [opt]
25073 balanced-token-seq balanced-token
25074
25075 balanced-token:
25076 ( balanced-token-seq )
25077 [ balanced-token-seq ]
25078 { balanced-token-seq }. */
25079
25080 static tree
25081 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25082 {
25083 tree attribute, attr_id = NULL_TREE, arguments;
25084 cp_token *token;
25085
25086 /* First, parse name of the attribute, a.k.a attribute-token. */
25087
25088 token = cp_lexer_peek_token (parser->lexer);
25089 if (token->type == CPP_NAME)
25090 attr_id = token->u.value;
25091 else if (token->type == CPP_KEYWORD)
25092 attr_id = ridpointers[(int) token->keyword];
25093 else if (token->flags & NAMED_OP)
25094 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25095
25096 if (attr_id == NULL_TREE)
25097 return NULL_TREE;
25098
25099 cp_lexer_consume_token (parser->lexer);
25100
25101 token = cp_lexer_peek_token (parser->lexer);
25102 if (token->type == CPP_SCOPE)
25103 {
25104 /* We are seeing a scoped attribute token. */
25105
25106 cp_lexer_consume_token (parser->lexer);
25107 if (attr_ns)
25108 error_at (token->location, "attribute using prefix used together "
25109 "with scoped attribute token");
25110 attr_ns = attr_id;
25111
25112 token = cp_lexer_consume_token (parser->lexer);
25113 if (token->type == CPP_NAME)
25114 attr_id = token->u.value;
25115 else if (token->type == CPP_KEYWORD)
25116 attr_id = ridpointers[(int) token->keyword];
25117 else if (token->flags & NAMED_OP)
25118 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25119 else
25120 {
25121 error_at (token->location,
25122 "expected an identifier for the attribute name");
25123 return error_mark_node;
25124 }
25125
25126 attr_id = canonicalize_attr_name (attr_id);
25127 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25128 NULL_TREE);
25129 token = cp_lexer_peek_token (parser->lexer);
25130 }
25131 else if (attr_ns)
25132 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25133 NULL_TREE);
25134 else
25135 {
25136 attr_id = canonicalize_attr_name (attr_id);
25137 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25138 NULL_TREE);
25139 /* C++11 noreturn attribute is equivalent to GNU's. */
25140 if (is_attribute_p ("noreturn", attr_id))
25141 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25142 /* C++14 deprecated attribute is equivalent to GNU's. */
25143 else if (is_attribute_p ("deprecated", attr_id))
25144 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25145 /* C++17 fallthrough attribute is equivalent to GNU's. */
25146 else if (is_attribute_p ("fallthrough", attr_id))
25147 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25148 /* Transactional Memory TS optimize_for_synchronized attribute is
25149 equivalent to GNU transaction_callable. */
25150 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25151 TREE_PURPOSE (attribute)
25152 = get_identifier ("transaction_callable");
25153 /* Transactional Memory attributes are GNU attributes. */
25154 else if (tm_attr_to_mask (attr_id))
25155 TREE_PURPOSE (attribute) = attr_id;
25156 }
25157
25158 /* Now parse the optional argument clause of the attribute. */
25159
25160 if (token->type != CPP_OPEN_PAREN)
25161 return attribute;
25162
25163 {
25164 vec<tree, va_gc> *vec;
25165 int attr_flag = normal_attr;
25166
25167 if (attr_ns == get_identifier ("gnu")
25168 && attribute_takes_identifier_p (attr_id))
25169 /* A GNU attribute that takes an identifier in parameter. */
25170 attr_flag = id_attr;
25171
25172 vec = cp_parser_parenthesized_expression_list
25173 (parser, attr_flag, /*cast_p=*/false,
25174 /*allow_expansion_p=*/true,
25175 /*non_constant_p=*/NULL);
25176 if (vec == NULL)
25177 arguments = error_mark_node;
25178 else
25179 {
25180 arguments = build_tree_list_vec (vec);
25181 release_tree_vector (vec);
25182 }
25183
25184 if (arguments == error_mark_node)
25185 attribute = error_mark_node;
25186 else
25187 TREE_VALUE (attribute) = arguments;
25188 }
25189
25190 return attribute;
25191 }
25192
25193 /* Check that the attribute ATTRIBUTE appears at most once in the
25194 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25195 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25196 isn't implemented yet in GCC. */
25197
25198 static void
25199 cp_parser_check_std_attribute (tree attributes, tree attribute)
25200 {
25201 if (attributes)
25202 {
25203 tree name = get_attribute_name (attribute);
25204 if (is_attribute_p ("noreturn", name)
25205 && lookup_attribute ("noreturn", attributes))
25206 error ("attribute %<noreturn%> can appear at most once "
25207 "in an attribute-list");
25208 else if (is_attribute_p ("deprecated", name)
25209 && lookup_attribute ("deprecated", attributes))
25210 error ("attribute %<deprecated%> can appear at most once "
25211 "in an attribute-list");
25212 }
25213 }
25214
25215 /* Parse a list of standard C++-11 attributes.
25216
25217 attribute-list:
25218 attribute [opt]
25219 attribute-list , attribute[opt]
25220 attribute ...
25221 attribute-list , attribute ...
25222 */
25223
25224 static tree
25225 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25226 {
25227 tree attributes = NULL_TREE, attribute = NULL_TREE;
25228 cp_token *token = NULL;
25229
25230 while (true)
25231 {
25232 attribute = cp_parser_std_attribute (parser, attr_ns);
25233 if (attribute == error_mark_node)
25234 break;
25235 if (attribute != NULL_TREE)
25236 {
25237 cp_parser_check_std_attribute (attributes, attribute);
25238 TREE_CHAIN (attribute) = attributes;
25239 attributes = attribute;
25240 }
25241 token = cp_lexer_peek_token (parser->lexer);
25242 if (token->type == CPP_ELLIPSIS)
25243 {
25244 cp_lexer_consume_token (parser->lexer);
25245 if (attribute == NULL_TREE)
25246 error_at (token->location,
25247 "expected attribute before %<...%>");
25248 else
25249 {
25250 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25251 if (pack == error_mark_node)
25252 return error_mark_node;
25253 TREE_VALUE (attribute) = pack;
25254 }
25255 token = cp_lexer_peek_token (parser->lexer);
25256 }
25257 if (token->type != CPP_COMMA)
25258 break;
25259 cp_lexer_consume_token (parser->lexer);
25260 }
25261 attributes = nreverse (attributes);
25262 return attributes;
25263 }
25264
25265 /* Parse a standard C++-11 attribute specifier.
25266
25267 attribute-specifier:
25268 [ [ attribute-using-prefix [opt] attribute-list ] ]
25269 alignment-specifier
25270
25271 attribute-using-prefix:
25272 using attribute-namespace :
25273
25274 alignment-specifier:
25275 alignas ( type-id ... [opt] )
25276 alignas ( alignment-expression ... [opt] ). */
25277
25278 static tree
25279 cp_parser_std_attribute_spec (cp_parser *parser)
25280 {
25281 tree attributes = NULL_TREE;
25282 cp_token *token = cp_lexer_peek_token (parser->lexer);
25283
25284 if (token->type == CPP_OPEN_SQUARE
25285 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25286 {
25287 tree attr_ns = NULL_TREE;
25288
25289 cp_lexer_consume_token (parser->lexer);
25290 cp_lexer_consume_token (parser->lexer);
25291
25292 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25293 {
25294 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25295 if (token->type == CPP_NAME)
25296 attr_ns = token->u.value;
25297 else if (token->type == CPP_KEYWORD)
25298 attr_ns = ridpointers[(int) token->keyword];
25299 else if (token->flags & NAMED_OP)
25300 attr_ns = get_identifier (cpp_type2name (token->type,
25301 token->flags));
25302 if (attr_ns
25303 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25304 {
25305 if (cxx_dialect < cxx17
25306 && !in_system_header_at (input_location))
25307 pedwarn (input_location, 0,
25308 "attribute using prefix only available "
25309 "with -std=c++17 or -std=gnu++17");
25310
25311 cp_lexer_consume_token (parser->lexer);
25312 cp_lexer_consume_token (parser->lexer);
25313 cp_lexer_consume_token (parser->lexer);
25314 }
25315 else
25316 attr_ns = NULL_TREE;
25317 }
25318
25319 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25320
25321 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25322 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25323 cp_parser_skip_to_end_of_statement (parser);
25324 else
25325 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25326 when we are sure that we have actually parsed them. */
25327 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25328 }
25329 else
25330 {
25331 tree alignas_expr;
25332
25333 /* Look for an alignment-specifier. */
25334
25335 token = cp_lexer_peek_token (parser->lexer);
25336
25337 if (token->type != CPP_KEYWORD
25338 || token->keyword != RID_ALIGNAS)
25339 return NULL_TREE;
25340
25341 cp_lexer_consume_token (parser->lexer);
25342 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25343
25344 matching_parens parens;
25345 if (!parens.require_open (parser))
25346 {
25347 cp_parser_error (parser, "expected %<(%>");
25348 return error_mark_node;
25349 }
25350
25351 cp_parser_parse_tentatively (parser);
25352 alignas_expr = cp_parser_type_id (parser);
25353
25354 if (!cp_parser_parse_definitely (parser))
25355 {
25356 alignas_expr = cp_parser_assignment_expression (parser);
25357 if (alignas_expr == error_mark_node)
25358 cp_parser_skip_to_end_of_statement (parser);
25359 if (alignas_expr == NULL_TREE
25360 || alignas_expr == error_mark_node)
25361 return alignas_expr;
25362 }
25363
25364 alignas_expr = cxx_alignas_expr (alignas_expr);
25365 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25366
25367 /* Handle alignas (pack...). */
25368 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25369 {
25370 cp_lexer_consume_token (parser->lexer);
25371 alignas_expr = make_pack_expansion (alignas_expr);
25372 }
25373
25374 /* Something went wrong, so don't build the attribute. */
25375 if (alignas_expr == error_mark_node)
25376 return error_mark_node;
25377
25378 if (!parens.require_close (parser))
25379 {
25380 cp_parser_error (parser, "expected %<)%>");
25381 return error_mark_node;
25382 }
25383
25384 /* Build the C++-11 representation of an 'aligned'
25385 attribute. */
25386 attributes =
25387 build_tree_list (build_tree_list (get_identifier ("gnu"),
25388 get_identifier ("aligned")),
25389 alignas_expr);
25390 }
25391
25392 return attributes;
25393 }
25394
25395 /* Parse a standard C++-11 attribute-specifier-seq.
25396
25397 attribute-specifier-seq:
25398 attribute-specifier-seq [opt] attribute-specifier
25399 */
25400
25401 static tree
25402 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25403 {
25404 tree attr_specs = NULL_TREE;
25405 tree attr_last = NULL_TREE;
25406
25407 while (true)
25408 {
25409 tree attr_spec = cp_parser_std_attribute_spec (parser);
25410 if (attr_spec == NULL_TREE)
25411 break;
25412 if (attr_spec == error_mark_node)
25413 return error_mark_node;
25414
25415 if (attr_last)
25416 TREE_CHAIN (attr_last) = attr_spec;
25417 else
25418 attr_specs = attr_last = attr_spec;
25419 attr_last = tree_last (attr_last);
25420 }
25421
25422 return attr_specs;
25423 }
25424
25425 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25426 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25427 current value of the PEDANTIC flag, regardless of whether or not
25428 the `__extension__' keyword is present. The caller is responsible
25429 for restoring the value of the PEDANTIC flag. */
25430
25431 static bool
25432 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25433 {
25434 /* Save the old value of the PEDANTIC flag. */
25435 *saved_pedantic = pedantic;
25436
25437 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25438 {
25439 /* Consume the `__extension__' token. */
25440 cp_lexer_consume_token (parser->lexer);
25441 /* We're not being pedantic while the `__extension__' keyword is
25442 in effect. */
25443 pedantic = 0;
25444
25445 return true;
25446 }
25447
25448 return false;
25449 }
25450
25451 /* Parse a label declaration.
25452
25453 label-declaration:
25454 __label__ label-declarator-seq ;
25455
25456 label-declarator-seq:
25457 identifier , label-declarator-seq
25458 identifier */
25459
25460 static void
25461 cp_parser_label_declaration (cp_parser* parser)
25462 {
25463 /* Look for the `__label__' keyword. */
25464 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25465
25466 while (true)
25467 {
25468 tree identifier;
25469
25470 /* Look for an identifier. */
25471 identifier = cp_parser_identifier (parser);
25472 /* If we failed, stop. */
25473 if (identifier == error_mark_node)
25474 break;
25475 /* Declare it as a label. */
25476 finish_label_decl (identifier);
25477 /* If the next token is a `;', stop. */
25478 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25479 break;
25480 /* Look for the `,' separating the label declarations. */
25481 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25482 }
25483
25484 /* Look for the final `;'. */
25485 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25486 }
25487
25488 // -------------------------------------------------------------------------- //
25489 // Requires Clause
25490
25491 // Parse a requires clause.
25492 //
25493 // requires-clause:
25494 // 'requires' logical-or-expression
25495 //
25496 // The required logical-or-expression must be a constant expression. Note
25497 // that we don't check that the expression is constepxr here. We defer until
25498 // we analyze constraints and then, we only check atomic constraints.
25499 static tree
25500 cp_parser_requires_clause (cp_parser *parser)
25501 {
25502 // Parse the requires clause so that it is not automatically folded.
25503 ++processing_template_decl;
25504 tree expr = cp_parser_binary_expression (parser, false, false,
25505 PREC_NOT_OPERATOR, NULL);
25506 if (check_for_bare_parameter_packs (expr))
25507 expr = error_mark_node;
25508 --processing_template_decl;
25509 return expr;
25510 }
25511
25512 // Optionally parse a requires clause:
25513 static tree
25514 cp_parser_requires_clause_opt (cp_parser *parser)
25515 {
25516 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25517 if (tok->keyword != RID_REQUIRES)
25518 {
25519 if (!flag_concepts && tok->type == CPP_NAME
25520 && tok->u.value == ridpointers[RID_REQUIRES])
25521 {
25522 error_at (cp_lexer_peek_token (parser->lexer)->location,
25523 "%<requires%> only available with -fconcepts");
25524 /* Parse and discard the requires-clause. */
25525 cp_lexer_consume_token (parser->lexer);
25526 cp_parser_requires_clause (parser);
25527 }
25528 return NULL_TREE;
25529 }
25530 cp_lexer_consume_token (parser->lexer);
25531 return cp_parser_requires_clause (parser);
25532 }
25533
25534
25535 /*---------------------------------------------------------------------------
25536 Requires expressions
25537 ---------------------------------------------------------------------------*/
25538
25539 /* Parse a requires expression
25540
25541 requirement-expression:
25542 'requires' requirement-parameter-list [opt] requirement-body */
25543 static tree
25544 cp_parser_requires_expression (cp_parser *parser)
25545 {
25546 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25547 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25548
25549 /* A requires-expression shall appear only within a concept
25550 definition or a requires-clause.
25551
25552 TODO: Implement this diagnostic correctly. */
25553 if (!processing_template_decl)
25554 {
25555 error_at (loc, "a requires expression cannot appear outside a template");
25556 cp_parser_skip_to_end_of_statement (parser);
25557 return error_mark_node;
25558 }
25559
25560 tree parms, reqs;
25561 {
25562 /* Local parameters are delared as variables within the scope
25563 of the expression. They are not visible past the end of
25564 the expression. Expressions within the requires-expression
25565 are unevaluated. */
25566 struct scope_sentinel
25567 {
25568 scope_sentinel ()
25569 {
25570 ++cp_unevaluated_operand;
25571 begin_scope (sk_block, NULL_TREE);
25572 }
25573
25574 ~scope_sentinel ()
25575 {
25576 pop_bindings_and_leave_scope ();
25577 --cp_unevaluated_operand;
25578 }
25579 } s;
25580
25581 /* Parse the optional parameter list. */
25582 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25583 {
25584 parms = cp_parser_requirement_parameter_list (parser);
25585 if (parms == error_mark_node)
25586 return error_mark_node;
25587 }
25588 else
25589 parms = NULL_TREE;
25590
25591 /* Parse the requirement body. */
25592 reqs = cp_parser_requirement_body (parser);
25593 if (reqs == error_mark_node)
25594 return error_mark_node;
25595 }
25596
25597 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25598 the parm chain. */
25599 grokparms (parms, &parms);
25600 return finish_requires_expr (parms, reqs);
25601 }
25602
25603 /* Parse a parameterized requirement.
25604
25605 requirement-parameter-list:
25606 '(' parameter-declaration-clause ')' */
25607 static tree
25608 cp_parser_requirement_parameter_list (cp_parser *parser)
25609 {
25610 matching_parens parens;
25611 if (!parens.require_open (parser))
25612 return error_mark_node;
25613
25614 tree parms = cp_parser_parameter_declaration_clause (parser);
25615
25616 if (!parens.require_close (parser))
25617 return error_mark_node;
25618
25619 return parms;
25620 }
25621
25622 /* Parse the body of a requirement.
25623
25624 requirement-body:
25625 '{' requirement-list '}' */
25626 static tree
25627 cp_parser_requirement_body (cp_parser *parser)
25628 {
25629 matching_braces braces;
25630 if (!braces.require_open (parser))
25631 return error_mark_node;
25632
25633 tree reqs = cp_parser_requirement_list (parser);
25634
25635 if (!braces.require_close (parser))
25636 return error_mark_node;
25637
25638 return reqs;
25639 }
25640
25641 /* Parse a list of requirements.
25642
25643 requirement-list:
25644 requirement
25645 requirement-list ';' requirement[opt] */
25646 static tree
25647 cp_parser_requirement_list (cp_parser *parser)
25648 {
25649 tree result = NULL_TREE;
25650 while (true)
25651 {
25652 tree req = cp_parser_requirement (parser);
25653 if (req == error_mark_node)
25654 return error_mark_node;
25655
25656 result = tree_cons (NULL_TREE, req, result);
25657
25658 /* If we see a semi-colon, consume it. */
25659 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25660 cp_lexer_consume_token (parser->lexer);
25661
25662 /* Stop processing at the end of the list. */
25663 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25664 break;
25665 }
25666
25667 /* Reverse the order of requirements so they are analyzed in
25668 declaration order. */
25669 return nreverse (result);
25670 }
25671
25672 /* Parse a syntactic requirement or type requirement.
25673
25674 requirement:
25675 simple-requirement
25676 compound-requirement
25677 type-requirement
25678 nested-requirement */
25679 static tree
25680 cp_parser_requirement (cp_parser *parser)
25681 {
25682 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25683 return cp_parser_compound_requirement (parser);
25684 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25685 return cp_parser_type_requirement (parser);
25686 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25687 return cp_parser_nested_requirement (parser);
25688 else
25689 return cp_parser_simple_requirement (parser);
25690 }
25691
25692 /* Parse a simple requirement.
25693
25694 simple-requirement:
25695 expression ';' */
25696 static tree
25697 cp_parser_simple_requirement (cp_parser *parser)
25698 {
25699 tree expr = cp_parser_expression (parser, NULL, false, false);
25700 if (!expr || expr == error_mark_node)
25701 return error_mark_node;
25702
25703 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25704 return error_mark_node;
25705
25706 return finish_simple_requirement (expr);
25707 }
25708
25709 /* Parse a type requirement
25710
25711 type-requirement
25712 nested-name-specifier [opt] required-type-name ';'
25713
25714 required-type-name:
25715 type-name
25716 'template' [opt] simple-template-id */
25717 static tree
25718 cp_parser_type_requirement (cp_parser *parser)
25719 {
25720 cp_lexer_consume_token (parser->lexer);
25721
25722 // Save the scope before parsing name specifiers.
25723 tree saved_scope = parser->scope;
25724 tree saved_object_scope = parser->object_scope;
25725 tree saved_qualifying_scope = parser->qualifying_scope;
25726 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25727 cp_parser_nested_name_specifier_opt (parser,
25728 /*typename_keyword_p=*/true,
25729 /*check_dependency_p=*/false,
25730 /*type_p=*/true,
25731 /*is_declaration=*/false);
25732
25733 tree type;
25734 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25735 {
25736 cp_lexer_consume_token (parser->lexer);
25737 type = cp_parser_template_id (parser,
25738 /*template_keyword_p=*/true,
25739 /*check_dependency=*/false,
25740 /*tag_type=*/none_type,
25741 /*is_declaration=*/false);
25742 type = make_typename_type (parser->scope, type, typename_type,
25743 /*complain=*/tf_error);
25744 }
25745 else
25746 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25747
25748 if (TREE_CODE (type) == TYPE_DECL)
25749 type = TREE_TYPE (type);
25750
25751 parser->scope = saved_scope;
25752 parser->object_scope = saved_object_scope;
25753 parser->qualifying_scope = saved_qualifying_scope;
25754
25755 if (type == error_mark_node)
25756 cp_parser_skip_to_end_of_statement (parser);
25757
25758 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25759 return error_mark_node;
25760 if (type == error_mark_node)
25761 return error_mark_node;
25762
25763 return finish_type_requirement (type);
25764 }
25765
25766 /* Parse a compound requirement
25767
25768 compound-requirement:
25769 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25770 static tree
25771 cp_parser_compound_requirement (cp_parser *parser)
25772 {
25773 /* Parse an expression enclosed in '{ }'s. */
25774 matching_braces braces;
25775 if (!braces.require_open (parser))
25776 return error_mark_node;
25777
25778 tree expr = cp_parser_expression (parser, NULL, false, false);
25779 if (!expr || expr == error_mark_node)
25780 return error_mark_node;
25781
25782 if (!braces.require_close (parser))
25783 return error_mark_node;
25784
25785 /* Parse the optional noexcept. */
25786 bool noexcept_p = false;
25787 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25788 {
25789 cp_lexer_consume_token (parser->lexer);
25790 noexcept_p = true;
25791 }
25792
25793 /* Parse the optional trailing return type. */
25794 tree type = NULL_TREE;
25795 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25796 {
25797 cp_lexer_consume_token (parser->lexer);
25798 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25799 parser->in_result_type_constraint_p = true;
25800 type = cp_parser_trailing_type_id (parser);
25801 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25802 if (type == error_mark_node)
25803 return error_mark_node;
25804 }
25805
25806 return finish_compound_requirement (expr, type, noexcept_p);
25807 }
25808
25809 /* Parse a nested requirement. This is the same as a requires clause.
25810
25811 nested-requirement:
25812 requires-clause */
25813 static tree
25814 cp_parser_nested_requirement (cp_parser *parser)
25815 {
25816 cp_lexer_consume_token (parser->lexer);
25817 tree req = cp_parser_requires_clause (parser);
25818 if (req == error_mark_node)
25819 return error_mark_node;
25820 return finish_nested_requirement (req);
25821 }
25822
25823 /* Support Functions */
25824
25825 /* Return the appropriate prefer_type argument for lookup_name_real based on
25826 tag_type and template_mem_access. */
25827
25828 static inline int
25829 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25830 {
25831 /* DR 141: When looking in the current enclosing context for a template-name
25832 after -> or ., only consider class templates. */
25833 if (template_mem_access)
25834 return 2;
25835 switch (tag_type)
25836 {
25837 case none_type: return 0; // No preference.
25838 case scope_type: return 1; // Type or namespace.
25839 default: return 2; // Type only.
25840 }
25841 }
25842
25843 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25844 NAME should have one of the representations used for an
25845 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25846 is returned. If PARSER->SCOPE is a dependent type, then a
25847 SCOPE_REF is returned.
25848
25849 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25850 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25851 was formed. Abstractly, such entities should not be passed to this
25852 function, because they do not need to be looked up, but it is
25853 simpler to check for this special case here, rather than at the
25854 call-sites.
25855
25856 In cases not explicitly covered above, this function returns a
25857 DECL, OVERLOAD, or baselink representing the result of the lookup.
25858 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25859 is returned.
25860
25861 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25862 (e.g., "struct") that was used. In that case bindings that do not
25863 refer to types are ignored.
25864
25865 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25866 ignored.
25867
25868 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25869 are ignored.
25870
25871 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25872 types.
25873
25874 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25875 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25876 NULL_TREE otherwise. */
25877
25878 static cp_expr
25879 cp_parser_lookup_name (cp_parser *parser, tree name,
25880 enum tag_types tag_type,
25881 bool is_template,
25882 bool is_namespace,
25883 bool check_dependency,
25884 tree *ambiguous_decls,
25885 location_t name_location)
25886 {
25887 tree decl;
25888 tree object_type = parser->context->object_type;
25889
25890 /* Assume that the lookup will be unambiguous. */
25891 if (ambiguous_decls)
25892 *ambiguous_decls = NULL_TREE;
25893
25894 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25895 no longer valid. Note that if we are parsing tentatively, and
25896 the parse fails, OBJECT_TYPE will be automatically restored. */
25897 parser->context->object_type = NULL_TREE;
25898
25899 if (name == error_mark_node)
25900 return error_mark_node;
25901
25902 /* A template-id has already been resolved; there is no lookup to
25903 do. */
25904 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25905 return name;
25906 if (BASELINK_P (name))
25907 {
25908 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25909 == TEMPLATE_ID_EXPR);
25910 return name;
25911 }
25912
25913 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25914 it should already have been checked to make sure that the name
25915 used matches the type being destroyed. */
25916 if (TREE_CODE (name) == BIT_NOT_EXPR)
25917 {
25918 tree type;
25919
25920 /* Figure out to which type this destructor applies. */
25921 if (parser->scope)
25922 type = parser->scope;
25923 else if (object_type)
25924 type = object_type;
25925 else
25926 type = current_class_type;
25927 /* If that's not a class type, there is no destructor. */
25928 if (!type || !CLASS_TYPE_P (type))
25929 return error_mark_node;
25930
25931 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25932 lazily_declare_fn (sfk_destructor, type);
25933
25934 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25935 return dtor;
25936
25937 return error_mark_node;
25938 }
25939
25940 /* By this point, the NAME should be an ordinary identifier. If
25941 the id-expression was a qualified name, the qualifying scope is
25942 stored in PARSER->SCOPE at this point. */
25943 gcc_assert (identifier_p (name));
25944
25945 /* Perform the lookup. */
25946 if (parser->scope)
25947 {
25948 bool dependent_p;
25949
25950 if (parser->scope == error_mark_node)
25951 return error_mark_node;
25952
25953 /* If the SCOPE is dependent, the lookup must be deferred until
25954 the template is instantiated -- unless we are explicitly
25955 looking up names in uninstantiated templates. Even then, we
25956 cannot look up the name if the scope is not a class type; it
25957 might, for example, be a template type parameter. */
25958 dependent_p = (TYPE_P (parser->scope)
25959 && dependent_scope_p (parser->scope));
25960 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25961 && dependent_p)
25962 /* Defer lookup. */
25963 decl = error_mark_node;
25964 else
25965 {
25966 tree pushed_scope = NULL_TREE;
25967
25968 /* If PARSER->SCOPE is a dependent type, then it must be a
25969 class type, and we must not be checking dependencies;
25970 otherwise, we would have processed this lookup above. So
25971 that PARSER->SCOPE is not considered a dependent base by
25972 lookup_member, we must enter the scope here. */
25973 if (dependent_p)
25974 pushed_scope = push_scope (parser->scope);
25975
25976 /* If the PARSER->SCOPE is a template specialization, it
25977 may be instantiated during name lookup. In that case,
25978 errors may be issued. Even if we rollback the current
25979 tentative parse, those errors are valid. */
25980 decl = lookup_qualified_name (parser->scope, name,
25981 prefer_type_arg (tag_type),
25982 /*complain=*/true);
25983
25984 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25985 lookup result and the nested-name-specifier nominates a class C:
25986 * if the name specified after the nested-name-specifier, when
25987 looked up in C, is the injected-class-name of C (Clause 9), or
25988 * if the name specified after the nested-name-specifier is the
25989 same as the identifier or the simple-template-id's template-
25990 name in the last component of the nested-name-specifier,
25991 the name is instead considered to name the constructor of
25992 class C. [ Note: for example, the constructor is not an
25993 acceptable lookup result in an elaborated-type-specifier so
25994 the constructor would not be used in place of the
25995 injected-class-name. --end note ] Such a constructor name
25996 shall be used only in the declarator-id of a declaration that
25997 names a constructor or in a using-declaration. */
25998 if (tag_type == none_type
25999 && DECL_SELF_REFERENCE_P (decl)
26000 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26001 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26002 prefer_type_arg (tag_type),
26003 /*complain=*/true);
26004
26005 /* If we have a single function from a using decl, pull it out. */
26006 if (TREE_CODE (decl) == OVERLOAD
26007 && !really_overloaded_fn (decl))
26008 decl = OVL_FUNCTION (decl);
26009
26010 if (pushed_scope)
26011 pop_scope (pushed_scope);
26012 }
26013
26014 /* If the scope is a dependent type and either we deferred lookup or
26015 we did lookup but didn't find the name, rememeber the name. */
26016 if (decl == error_mark_node && TYPE_P (parser->scope)
26017 && dependent_type_p (parser->scope))
26018 {
26019 if (tag_type)
26020 {
26021 tree type;
26022
26023 /* The resolution to Core Issue 180 says that `struct
26024 A::B' should be considered a type-name, even if `A'
26025 is dependent. */
26026 type = make_typename_type (parser->scope, name, tag_type,
26027 /*complain=*/tf_error);
26028 if (type != error_mark_node)
26029 decl = TYPE_NAME (type);
26030 }
26031 else if (is_template
26032 && (cp_parser_next_token_ends_template_argument_p (parser)
26033 || cp_lexer_next_token_is (parser->lexer,
26034 CPP_CLOSE_PAREN)))
26035 decl = make_unbound_class_template (parser->scope,
26036 name, NULL_TREE,
26037 /*complain=*/tf_error);
26038 else
26039 decl = build_qualified_name (/*type=*/NULL_TREE,
26040 parser->scope, name,
26041 is_template);
26042 }
26043 parser->qualifying_scope = parser->scope;
26044 parser->object_scope = NULL_TREE;
26045 }
26046 else if (object_type)
26047 {
26048 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26049 OBJECT_TYPE is not a class. */
26050 if (CLASS_TYPE_P (object_type))
26051 /* If the OBJECT_TYPE is a template specialization, it may
26052 be instantiated during name lookup. In that case, errors
26053 may be issued. Even if we rollback the current tentative
26054 parse, those errors are valid. */
26055 decl = lookup_member (object_type,
26056 name,
26057 /*protect=*/0,
26058 prefer_type_arg (tag_type),
26059 tf_warning_or_error);
26060 else
26061 decl = NULL_TREE;
26062
26063 if (!decl)
26064 /* Look it up in the enclosing context. DR 141: When looking for a
26065 template-name after -> or ., only consider class templates. */
26066 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26067 /*nonclass=*/0,
26068 /*block_p=*/true, is_namespace, 0);
26069 if (object_type == unknown_type_node)
26070 /* The object is type-dependent, so we can't look anything up; we used
26071 this to get the DR 141 behavior. */
26072 object_type = NULL_TREE;
26073 parser->object_scope = object_type;
26074 parser->qualifying_scope = NULL_TREE;
26075 }
26076 else
26077 {
26078 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26079 /*nonclass=*/0,
26080 /*block_p=*/true, is_namespace, 0);
26081 parser->qualifying_scope = NULL_TREE;
26082 parser->object_scope = NULL_TREE;
26083 }
26084
26085 /* If the lookup failed, let our caller know. */
26086 if (!decl || decl == error_mark_node)
26087 return error_mark_node;
26088
26089 /* Pull out the template from an injected-class-name (or multiple). */
26090 if (is_template)
26091 decl = maybe_get_template_decl_from_type_decl (decl);
26092
26093 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26094 if (TREE_CODE (decl) == TREE_LIST)
26095 {
26096 if (ambiguous_decls)
26097 *ambiguous_decls = decl;
26098 /* The error message we have to print is too complicated for
26099 cp_parser_error, so we incorporate its actions directly. */
26100 if (!cp_parser_simulate_error (parser))
26101 {
26102 error_at (name_location, "reference to %qD is ambiguous",
26103 name);
26104 print_candidates (decl);
26105 }
26106 return error_mark_node;
26107 }
26108
26109 gcc_assert (DECL_P (decl)
26110 || TREE_CODE (decl) == OVERLOAD
26111 || TREE_CODE (decl) == SCOPE_REF
26112 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26113 || BASELINK_P (decl));
26114
26115 /* If we have resolved the name of a member declaration, check to
26116 see if the declaration is accessible. When the name resolves to
26117 set of overloaded functions, accessibility is checked when
26118 overload resolution is done.
26119
26120 During an explicit instantiation, access is not checked at all,
26121 as per [temp.explicit]. */
26122 if (DECL_P (decl))
26123 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26124
26125 maybe_record_typedef_use (decl);
26126
26127 return cp_expr (decl, name_location);
26128 }
26129
26130 /* Like cp_parser_lookup_name, but for use in the typical case where
26131 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26132 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26133
26134 static tree
26135 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26136 {
26137 return cp_parser_lookup_name (parser, name,
26138 none_type,
26139 /*is_template=*/false,
26140 /*is_namespace=*/false,
26141 /*check_dependency=*/true,
26142 /*ambiguous_decls=*/NULL,
26143 location);
26144 }
26145
26146 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26147 the current context, return the TYPE_DECL. If TAG_NAME_P is
26148 true, the DECL indicates the class being defined in a class-head,
26149 or declared in an elaborated-type-specifier.
26150
26151 Otherwise, return DECL. */
26152
26153 static tree
26154 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26155 {
26156 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26157 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26158
26159 struct A {
26160 template <typename T> struct B;
26161 };
26162
26163 template <typename T> struct A::B {};
26164
26165 Similarly, in an elaborated-type-specifier:
26166
26167 namespace N { struct X{}; }
26168
26169 struct A {
26170 template <typename T> friend struct N::X;
26171 };
26172
26173 However, if the DECL refers to a class type, and we are in
26174 the scope of the class, then the name lookup automatically
26175 finds the TYPE_DECL created by build_self_reference rather
26176 than a TEMPLATE_DECL. For example, in:
26177
26178 template <class T> struct S {
26179 S s;
26180 };
26181
26182 there is no need to handle such case. */
26183
26184 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26185 return DECL_TEMPLATE_RESULT (decl);
26186
26187 return decl;
26188 }
26189
26190 /* If too many, or too few, template-parameter lists apply to the
26191 declarator, issue an error message. Returns TRUE if all went well,
26192 and FALSE otherwise. */
26193
26194 static bool
26195 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26196 cp_declarator *declarator,
26197 location_t declarator_location)
26198 {
26199 switch (declarator->kind)
26200 {
26201 case cdk_id:
26202 {
26203 unsigned num_templates = 0;
26204 tree scope = declarator->u.id.qualifying_scope;
26205
26206 if (scope)
26207 num_templates = num_template_headers_for_class (scope);
26208 else if (TREE_CODE (declarator->u.id.unqualified_name)
26209 == TEMPLATE_ID_EXPR)
26210 /* If the DECLARATOR has the form `X<y>' then it uses one
26211 additional level of template parameters. */
26212 ++num_templates;
26213
26214 return cp_parser_check_template_parameters
26215 (parser, num_templates, declarator_location, declarator);
26216 }
26217
26218 case cdk_function:
26219 case cdk_array:
26220 case cdk_pointer:
26221 case cdk_reference:
26222 case cdk_ptrmem:
26223 return (cp_parser_check_declarator_template_parameters
26224 (parser, declarator->declarator, declarator_location));
26225
26226 case cdk_decomp:
26227 case cdk_error:
26228 return true;
26229
26230 default:
26231 gcc_unreachable ();
26232 }
26233 return false;
26234 }
26235
26236 /* NUM_TEMPLATES were used in the current declaration. If that is
26237 invalid, return FALSE and issue an error messages. Otherwise,
26238 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26239 declarator and we can print more accurate diagnostics. */
26240
26241 static bool
26242 cp_parser_check_template_parameters (cp_parser* parser,
26243 unsigned num_templates,
26244 location_t location,
26245 cp_declarator *declarator)
26246 {
26247 /* If there are the same number of template classes and parameter
26248 lists, that's OK. */
26249 if (parser->num_template_parameter_lists == num_templates)
26250 return true;
26251 /* If there are more, but only one more, then we are referring to a
26252 member template. That's OK too. */
26253 if (parser->num_template_parameter_lists == num_templates + 1)
26254 return true;
26255 /* If there are more template classes than parameter lists, we have
26256 something like:
26257
26258 template <class T> void S<T>::R<T>::f (); */
26259 if (parser->num_template_parameter_lists < num_templates)
26260 {
26261 if (declarator && !current_function_decl)
26262 error_at (location, "specializing member %<%T::%E%> "
26263 "requires %<template<>%> syntax",
26264 declarator->u.id.qualifying_scope,
26265 declarator->u.id.unqualified_name);
26266 else if (declarator)
26267 error_at (location, "invalid declaration of %<%T::%E%>",
26268 declarator->u.id.qualifying_scope,
26269 declarator->u.id.unqualified_name);
26270 else
26271 error_at (location, "too few template-parameter-lists");
26272 return false;
26273 }
26274 /* Otherwise, there are too many template parameter lists. We have
26275 something like:
26276
26277 template <class T> template <class U> void S::f(); */
26278 error_at (location, "too many template-parameter-lists");
26279 return false;
26280 }
26281
26282 /* Parse an optional `::' token indicating that the following name is
26283 from the global namespace. If so, PARSER->SCOPE is set to the
26284 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26285 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26286 Returns the new value of PARSER->SCOPE, if the `::' token is
26287 present, and NULL_TREE otherwise. */
26288
26289 static tree
26290 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26291 {
26292 cp_token *token;
26293
26294 /* Peek at the next token. */
26295 token = cp_lexer_peek_token (parser->lexer);
26296 /* If we're looking at a `::' token then we're starting from the
26297 global namespace, not our current location. */
26298 if (token->type == CPP_SCOPE)
26299 {
26300 /* Consume the `::' token. */
26301 cp_lexer_consume_token (parser->lexer);
26302 /* Set the SCOPE so that we know where to start the lookup. */
26303 parser->scope = global_namespace;
26304 parser->qualifying_scope = global_namespace;
26305 parser->object_scope = NULL_TREE;
26306
26307 return parser->scope;
26308 }
26309 else if (!current_scope_valid_p)
26310 {
26311 parser->scope = NULL_TREE;
26312 parser->qualifying_scope = NULL_TREE;
26313 parser->object_scope = NULL_TREE;
26314 }
26315
26316 return NULL_TREE;
26317 }
26318
26319 /* Returns TRUE if the upcoming token sequence is the start of a
26320 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26321 declarator is preceded by the `friend' specifier. */
26322
26323 static bool
26324 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26325 {
26326 bool constructor_p;
26327 bool outside_class_specifier_p;
26328 tree nested_name_specifier;
26329 cp_token *next_token;
26330
26331 /* The common case is that this is not a constructor declarator, so
26332 try to avoid doing lots of work if at all possible. It's not
26333 valid declare a constructor at function scope. */
26334 if (parser->in_function_body)
26335 return false;
26336 /* And only certain tokens can begin a constructor declarator. */
26337 next_token = cp_lexer_peek_token (parser->lexer);
26338 if (next_token->type != CPP_NAME
26339 && next_token->type != CPP_SCOPE
26340 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26341 && next_token->type != CPP_TEMPLATE_ID)
26342 return false;
26343
26344 /* Parse tentatively; we are going to roll back all of the tokens
26345 consumed here. */
26346 cp_parser_parse_tentatively (parser);
26347 /* Assume that we are looking at a constructor declarator. */
26348 constructor_p = true;
26349
26350 /* Look for the optional `::' operator. */
26351 cp_parser_global_scope_opt (parser,
26352 /*current_scope_valid_p=*/false);
26353 /* Look for the nested-name-specifier. */
26354 nested_name_specifier
26355 = (cp_parser_nested_name_specifier_opt (parser,
26356 /*typename_keyword_p=*/false,
26357 /*check_dependency_p=*/false,
26358 /*type_p=*/false,
26359 /*is_declaration=*/false));
26360
26361 outside_class_specifier_p = (!at_class_scope_p ()
26362 || !TYPE_BEING_DEFINED (current_class_type)
26363 || friend_p);
26364
26365 /* Outside of a class-specifier, there must be a
26366 nested-name-specifier. Except in C++17 mode, where we
26367 might be declaring a guiding declaration. */
26368 if (!nested_name_specifier && outside_class_specifier_p
26369 && cxx_dialect < cxx17)
26370 constructor_p = false;
26371 else if (nested_name_specifier == error_mark_node)
26372 constructor_p = false;
26373
26374 /* If we have a class scope, this is easy; DR 147 says that S::S always
26375 names the constructor, and no other qualified name could. */
26376 if (constructor_p && nested_name_specifier
26377 && CLASS_TYPE_P (nested_name_specifier))
26378 {
26379 tree id = cp_parser_unqualified_id (parser,
26380 /*template_keyword_p=*/false,
26381 /*check_dependency_p=*/false,
26382 /*declarator_p=*/true,
26383 /*optional_p=*/false);
26384 if (is_overloaded_fn (id))
26385 id = DECL_NAME (get_first_fn (id));
26386 if (!constructor_name_p (id, nested_name_specifier))
26387 constructor_p = false;
26388 }
26389 /* If we still think that this might be a constructor-declarator,
26390 look for a class-name. */
26391 else if (constructor_p)
26392 {
26393 /* If we have:
26394
26395 template <typename T> struct S {
26396 S();
26397 };
26398
26399 we must recognize that the nested `S' names a class. */
26400 if (cxx_dialect >= cxx17)
26401 cp_parser_parse_tentatively (parser);
26402
26403 tree type_decl;
26404 type_decl = cp_parser_class_name (parser,
26405 /*typename_keyword_p=*/false,
26406 /*template_keyword_p=*/false,
26407 none_type,
26408 /*check_dependency_p=*/false,
26409 /*class_head_p=*/false,
26410 /*is_declaration=*/false);
26411
26412 if (cxx_dialect >= cxx17
26413 && !cp_parser_parse_definitely (parser))
26414 {
26415 type_decl = NULL_TREE;
26416 tree tmpl = cp_parser_template_name (parser,
26417 /*template_keyword*/false,
26418 /*check_dependency_p*/false,
26419 /*is_declaration*/false,
26420 none_type,
26421 /*is_identifier*/NULL);
26422 if (DECL_CLASS_TEMPLATE_P (tmpl)
26423 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26424 /* It's a deduction guide, return true. */;
26425 else
26426 cp_parser_simulate_error (parser);
26427 }
26428
26429 /* If there was no class-name, then this is not a constructor.
26430 Otherwise, if we are in a class-specifier and we aren't
26431 handling a friend declaration, check that its type matches
26432 current_class_type (c++/38313). Note: error_mark_node
26433 is left alone for error recovery purposes. */
26434 constructor_p = (!cp_parser_error_occurred (parser)
26435 && (outside_class_specifier_p
26436 || type_decl == NULL_TREE
26437 || type_decl == error_mark_node
26438 || same_type_p (current_class_type,
26439 TREE_TYPE (type_decl))));
26440
26441 /* If we're still considering a constructor, we have to see a `(',
26442 to begin the parameter-declaration-clause, followed by either a
26443 `)', an `...', or a decl-specifier. We need to check for a
26444 type-specifier to avoid being fooled into thinking that:
26445
26446 S (f) (int);
26447
26448 is a constructor. (It is actually a function named `f' that
26449 takes one parameter (of type `int') and returns a value of type
26450 `S'. */
26451 if (constructor_p
26452 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26453 constructor_p = false;
26454
26455 if (constructor_p
26456 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26457 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26458 /* A parameter declaration begins with a decl-specifier,
26459 which is either the "attribute" keyword, a storage class
26460 specifier, or (usually) a type-specifier. */
26461 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26462 {
26463 tree type;
26464 tree pushed_scope = NULL_TREE;
26465 unsigned saved_num_template_parameter_lists;
26466
26467 /* Names appearing in the type-specifier should be looked up
26468 in the scope of the class. */
26469 if (current_class_type)
26470 type = NULL_TREE;
26471 else if (type_decl)
26472 {
26473 type = TREE_TYPE (type_decl);
26474 if (TREE_CODE (type) == TYPENAME_TYPE)
26475 {
26476 type = resolve_typename_type (type,
26477 /*only_current_p=*/false);
26478 if (TREE_CODE (type) == TYPENAME_TYPE)
26479 {
26480 cp_parser_abort_tentative_parse (parser);
26481 return false;
26482 }
26483 }
26484 pushed_scope = push_scope (type);
26485 }
26486
26487 /* Inside the constructor parameter list, surrounding
26488 template-parameter-lists do not apply. */
26489 saved_num_template_parameter_lists
26490 = parser->num_template_parameter_lists;
26491 parser->num_template_parameter_lists = 0;
26492
26493 /* Look for the type-specifier. */
26494 cp_parser_type_specifier (parser,
26495 CP_PARSER_FLAGS_NONE,
26496 /*decl_specs=*/NULL,
26497 /*is_declarator=*/true,
26498 /*declares_class_or_enum=*/NULL,
26499 /*is_cv_qualifier=*/NULL);
26500
26501 parser->num_template_parameter_lists
26502 = saved_num_template_parameter_lists;
26503
26504 /* Leave the scope of the class. */
26505 if (pushed_scope)
26506 pop_scope (pushed_scope);
26507
26508 constructor_p = !cp_parser_error_occurred (parser);
26509 }
26510 }
26511
26512 /* We did not really want to consume any tokens. */
26513 cp_parser_abort_tentative_parse (parser);
26514
26515 return constructor_p;
26516 }
26517
26518 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26519 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26520 they must be performed once we are in the scope of the function.
26521
26522 Returns the function defined. */
26523
26524 static tree
26525 cp_parser_function_definition_from_specifiers_and_declarator
26526 (cp_parser* parser,
26527 cp_decl_specifier_seq *decl_specifiers,
26528 tree attributes,
26529 const cp_declarator *declarator)
26530 {
26531 tree fn;
26532 bool success_p;
26533
26534 /* Begin the function-definition. */
26535 success_p = start_function (decl_specifiers, declarator, attributes);
26536
26537 /* The things we're about to see are not directly qualified by any
26538 template headers we've seen thus far. */
26539 reset_specialization ();
26540
26541 /* If there were names looked up in the decl-specifier-seq that we
26542 did not check, check them now. We must wait until we are in the
26543 scope of the function to perform the checks, since the function
26544 might be a friend. */
26545 perform_deferred_access_checks (tf_warning_or_error);
26546
26547 if (success_p)
26548 {
26549 cp_finalize_omp_declare_simd (parser, current_function_decl);
26550 parser->omp_declare_simd = NULL;
26551 cp_finalize_oacc_routine (parser, current_function_decl, true);
26552 parser->oacc_routine = NULL;
26553 }
26554
26555 if (!success_p)
26556 {
26557 /* Skip the entire function. */
26558 cp_parser_skip_to_end_of_block_or_statement (parser);
26559 fn = error_mark_node;
26560 }
26561 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26562 {
26563 /* Seen already, skip it. An error message has already been output. */
26564 cp_parser_skip_to_end_of_block_or_statement (parser);
26565 fn = current_function_decl;
26566 current_function_decl = NULL_TREE;
26567 /* If this is a function from a class, pop the nested class. */
26568 if (current_class_name)
26569 pop_nested_class ();
26570 }
26571 else
26572 {
26573 timevar_id_t tv;
26574 if (DECL_DECLARED_INLINE_P (current_function_decl))
26575 tv = TV_PARSE_INLINE;
26576 else
26577 tv = TV_PARSE_FUNC;
26578 timevar_push (tv);
26579 fn = cp_parser_function_definition_after_declarator (parser,
26580 /*inline_p=*/false);
26581 timevar_pop (tv);
26582 }
26583
26584 return fn;
26585 }
26586
26587 /* Parse the part of a function-definition that follows the
26588 declarator. INLINE_P is TRUE iff this function is an inline
26589 function defined within a class-specifier.
26590
26591 Returns the function defined. */
26592
26593 static tree
26594 cp_parser_function_definition_after_declarator (cp_parser* parser,
26595 bool inline_p)
26596 {
26597 tree fn;
26598 bool saved_in_unbraced_linkage_specification_p;
26599 bool saved_in_function_body;
26600 unsigned saved_num_template_parameter_lists;
26601 cp_token *token;
26602 bool fully_implicit_function_template_p
26603 = parser->fully_implicit_function_template_p;
26604 parser->fully_implicit_function_template_p = false;
26605 tree implicit_template_parms
26606 = parser->implicit_template_parms;
26607 parser->implicit_template_parms = 0;
26608 cp_binding_level* implicit_template_scope
26609 = parser->implicit_template_scope;
26610 parser->implicit_template_scope = 0;
26611
26612 saved_in_function_body = parser->in_function_body;
26613 parser->in_function_body = true;
26614 /* If the next token is `return', then the code may be trying to
26615 make use of the "named return value" extension that G++ used to
26616 support. */
26617 token = cp_lexer_peek_token (parser->lexer);
26618 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26619 {
26620 /* Consume the `return' keyword. */
26621 cp_lexer_consume_token (parser->lexer);
26622 /* Look for the identifier that indicates what value is to be
26623 returned. */
26624 cp_parser_identifier (parser);
26625 /* Issue an error message. */
26626 error_at (token->location,
26627 "named return values are no longer supported");
26628 /* Skip tokens until we reach the start of the function body. */
26629 while (true)
26630 {
26631 cp_token *token = cp_lexer_peek_token (parser->lexer);
26632 if (token->type == CPP_OPEN_BRACE
26633 || token->type == CPP_EOF
26634 || token->type == CPP_PRAGMA_EOL)
26635 break;
26636 cp_lexer_consume_token (parser->lexer);
26637 }
26638 }
26639 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26640 anything declared inside `f'. */
26641 saved_in_unbraced_linkage_specification_p
26642 = parser->in_unbraced_linkage_specification_p;
26643 parser->in_unbraced_linkage_specification_p = false;
26644 /* Inside the function, surrounding template-parameter-lists do not
26645 apply. */
26646 saved_num_template_parameter_lists
26647 = parser->num_template_parameter_lists;
26648 parser->num_template_parameter_lists = 0;
26649
26650 /* If the next token is `try', `__transaction_atomic', or
26651 `__transaction_relaxed`, then we are looking at either function-try-block
26652 or function-transaction-block. Note that all of these include the
26653 function-body. */
26654 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26655 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26656 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26657 RID_TRANSACTION_RELAXED))
26658 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26659 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26660 cp_parser_function_try_block (parser);
26661 else
26662 cp_parser_ctor_initializer_opt_and_function_body
26663 (parser, /*in_function_try_block=*/false);
26664
26665 /* Finish the function. */
26666 fn = finish_function (inline_p);
26667 /* Generate code for it, if necessary. */
26668 expand_or_defer_fn (fn);
26669 /* Restore the saved values. */
26670 parser->in_unbraced_linkage_specification_p
26671 = saved_in_unbraced_linkage_specification_p;
26672 parser->num_template_parameter_lists
26673 = saved_num_template_parameter_lists;
26674 parser->in_function_body = saved_in_function_body;
26675
26676 parser->fully_implicit_function_template_p
26677 = fully_implicit_function_template_p;
26678 parser->implicit_template_parms
26679 = implicit_template_parms;
26680 parser->implicit_template_scope
26681 = implicit_template_scope;
26682
26683 if (parser->fully_implicit_function_template_p)
26684 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26685
26686 return fn;
26687 }
26688
26689 /* Parse a template-declaration body (following argument list). */
26690
26691 static void
26692 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26693 tree parameter_list,
26694 bool member_p)
26695 {
26696 tree decl = NULL_TREE;
26697 bool friend_p = false;
26698
26699 /* We just processed one more parameter list. */
26700 ++parser->num_template_parameter_lists;
26701
26702 /* Get the deferred access checks from the parameter list. These
26703 will be checked once we know what is being declared, as for a
26704 member template the checks must be performed in the scope of the
26705 class containing the member. */
26706 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26707
26708 /* Tentatively parse for a new template parameter list, which can either be
26709 the template keyword or a template introduction. */
26710 if (cp_parser_template_declaration_after_export (parser, member_p))
26711 /* OK */;
26712 else if (cxx_dialect >= cxx11
26713 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26714 decl = cp_parser_alias_declaration (parser);
26715 else
26716 {
26717 /* There are no access checks when parsing a template, as we do not
26718 know if a specialization will be a friend. */
26719 push_deferring_access_checks (dk_no_check);
26720 cp_token *token = cp_lexer_peek_token (parser->lexer);
26721 decl = cp_parser_single_declaration (parser,
26722 checks,
26723 member_p,
26724 /*explicit_specialization_p=*/false,
26725 &friend_p);
26726 pop_deferring_access_checks ();
26727
26728 /* If this is a member template declaration, let the front
26729 end know. */
26730 if (member_p && !friend_p && decl)
26731 {
26732 if (TREE_CODE (decl) == TYPE_DECL)
26733 cp_parser_check_access_in_redeclaration (decl, token->location);
26734
26735 decl = finish_member_template_decl (decl);
26736 }
26737 else if (friend_p && decl
26738 && DECL_DECLARES_TYPE_P (decl))
26739 make_friend_class (current_class_type, TREE_TYPE (decl),
26740 /*complain=*/true);
26741 }
26742 /* We are done with the current parameter list. */
26743 --parser->num_template_parameter_lists;
26744
26745 pop_deferring_access_checks ();
26746
26747 /* Finish up. */
26748 finish_template_decl (parameter_list);
26749
26750 /* Check the template arguments for a literal operator template. */
26751 if (decl
26752 && DECL_DECLARES_FUNCTION_P (decl)
26753 && UDLIT_OPER_P (DECL_NAME (decl)))
26754 {
26755 bool ok = true;
26756 if (parameter_list == NULL_TREE)
26757 ok = false;
26758 else
26759 {
26760 int num_parms = TREE_VEC_LENGTH (parameter_list);
26761 if (num_parms == 1)
26762 {
26763 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26764 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26765 if (TREE_TYPE (parm) != char_type_node
26766 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26767 ok = false;
26768 }
26769 else if (num_parms == 2 && cxx_dialect >= cxx14)
26770 {
26771 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26772 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26773 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26774 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26775 if (parm == error_mark_node
26776 || TREE_TYPE (parm) != TREE_TYPE (type)
26777 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26778 ok = false;
26779 }
26780 else
26781 ok = false;
26782 }
26783 if (!ok)
26784 {
26785 if (cxx_dialect >= cxx14)
26786 error ("literal operator template %qD has invalid parameter list."
26787 " Expected non-type template argument pack <char...>"
26788 " or <typename CharT, CharT...>",
26789 decl);
26790 else
26791 error ("literal operator template %qD has invalid parameter list."
26792 " Expected non-type template argument pack <char...>",
26793 decl);
26794 }
26795 }
26796
26797 /* Register member declarations. */
26798 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26799 finish_member_declaration (decl);
26800 /* If DECL is a function template, we must return to parse it later.
26801 (Even though there is no definition, there might be default
26802 arguments that need handling.) */
26803 if (member_p && decl
26804 && DECL_DECLARES_FUNCTION_P (decl))
26805 vec_safe_push (unparsed_funs_with_definitions, decl);
26806 }
26807
26808 /* Parse a template introduction header for a template-declaration. Returns
26809 false if tentative parse fails. */
26810
26811 static bool
26812 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26813 {
26814 cp_parser_parse_tentatively (parser);
26815
26816 tree saved_scope = parser->scope;
26817 tree saved_object_scope = parser->object_scope;
26818 tree saved_qualifying_scope = parser->qualifying_scope;
26819
26820 /* Look for the optional `::' operator. */
26821 cp_parser_global_scope_opt (parser,
26822 /*current_scope_valid_p=*/false);
26823 /* Look for the nested-name-specifier. */
26824 cp_parser_nested_name_specifier_opt (parser,
26825 /*typename_keyword_p=*/false,
26826 /*check_dependency_p=*/true,
26827 /*type_p=*/false,
26828 /*is_declaration=*/false);
26829
26830 cp_token *token = cp_lexer_peek_token (parser->lexer);
26831 tree concept_name = cp_parser_identifier (parser);
26832
26833 /* Look up the concept for which we will be matching
26834 template parameters. */
26835 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26836 token->location);
26837 parser->scope = saved_scope;
26838 parser->object_scope = saved_object_scope;
26839 parser->qualifying_scope = saved_qualifying_scope;
26840
26841 if (concept_name == error_mark_node)
26842 cp_parser_simulate_error (parser);
26843
26844 /* Look for opening brace for introduction. */
26845 matching_braces braces;
26846 braces.require_open (parser);
26847
26848 if (!cp_parser_parse_definitely (parser))
26849 return false;
26850
26851 push_deferring_access_checks (dk_deferred);
26852
26853 /* Build vector of placeholder parameters and grab
26854 matching identifiers. */
26855 tree introduction_list = cp_parser_introduction_list (parser);
26856
26857 /* The introduction-list shall not be empty. */
26858 int nargs = TREE_VEC_LENGTH (introduction_list);
26859 if (nargs == 0)
26860 {
26861 error ("empty introduction-list");
26862 return true;
26863 }
26864
26865 /* Look for closing brace for introduction. */
26866 if (!braces.require_close (parser))
26867 return true;
26868
26869 if (tmpl_decl == error_mark_node)
26870 {
26871 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26872 token->location);
26873 return true;
26874 }
26875
26876 /* Build and associate the constraint. */
26877 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26878 if (parms && parms != error_mark_node)
26879 {
26880 cp_parser_template_declaration_after_parameters (parser, parms,
26881 member_p);
26882 return true;
26883 }
26884
26885 error_at (token->location, "no matching concept for template-introduction");
26886 return true;
26887 }
26888
26889 /* Parse a normal template-declaration following the template keyword. */
26890
26891 static void
26892 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26893 {
26894 tree parameter_list;
26895 bool need_lang_pop;
26896 location_t location = input_location;
26897
26898 /* Look for the `<' token. */
26899 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26900 return;
26901 if (at_class_scope_p () && current_function_decl)
26902 {
26903 /* 14.5.2.2 [temp.mem]
26904
26905 A local class shall not have member templates. */
26906 error_at (location,
26907 "invalid declaration of member template in local class");
26908 cp_parser_skip_to_end_of_block_or_statement (parser);
26909 return;
26910 }
26911 /* [temp]
26912
26913 A template ... shall not have C linkage. */
26914 if (current_lang_name == lang_name_c)
26915 {
26916 error_at (location, "template with C linkage");
26917 maybe_show_extern_c_location ();
26918 /* Give it C++ linkage to avoid confusing other parts of the
26919 front end. */
26920 push_lang_context (lang_name_cplusplus);
26921 need_lang_pop = true;
26922 }
26923 else
26924 need_lang_pop = false;
26925
26926 /* We cannot perform access checks on the template parameter
26927 declarations until we know what is being declared, just as we
26928 cannot check the decl-specifier list. */
26929 push_deferring_access_checks (dk_deferred);
26930
26931 /* If the next token is `>', then we have an invalid
26932 specialization. Rather than complain about an invalid template
26933 parameter, issue an error message here. */
26934 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26935 {
26936 cp_parser_error (parser, "invalid explicit specialization");
26937 begin_specialization ();
26938 parameter_list = NULL_TREE;
26939 }
26940 else
26941 {
26942 /* Parse the template parameters. */
26943 parameter_list = cp_parser_template_parameter_list (parser);
26944 }
26945
26946 /* Look for the `>'. */
26947 cp_parser_skip_to_end_of_template_parameter_list (parser);
26948
26949 /* Manage template requirements */
26950 if (flag_concepts)
26951 {
26952 tree reqs = get_shorthand_constraints (current_template_parms);
26953 if (tree r = cp_parser_requires_clause_opt (parser))
26954 reqs = conjoin_constraints (reqs, normalize_expression (r));
26955 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26956 }
26957
26958 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26959 member_p);
26960
26961 /* For the erroneous case of a template with C linkage, we pushed an
26962 implicit C++ linkage scope; exit that scope now. */
26963 if (need_lang_pop)
26964 pop_lang_context ();
26965 }
26966
26967 /* Parse a template-declaration, assuming that the `export' (and
26968 `extern') keywords, if present, has already been scanned. MEMBER_P
26969 is as for cp_parser_template_declaration. */
26970
26971 static bool
26972 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26973 {
26974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26975 {
26976 cp_lexer_consume_token (parser->lexer);
26977 cp_parser_explicit_template_declaration (parser, member_p);
26978 return true;
26979 }
26980 else if (flag_concepts)
26981 return cp_parser_template_introduction (parser, member_p);
26982
26983 return false;
26984 }
26985
26986 /* Perform the deferred access checks from a template-parameter-list.
26987 CHECKS is a TREE_LIST of access checks, as returned by
26988 get_deferred_access_checks. */
26989
26990 static void
26991 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26992 {
26993 ++processing_template_parmlist;
26994 perform_access_checks (checks, tf_warning_or_error);
26995 --processing_template_parmlist;
26996 }
26997
26998 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26999 `function-definition' sequence that follows a template header.
27000 If MEMBER_P is true, this declaration appears in a class scope.
27001
27002 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27003 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27004
27005 static tree
27006 cp_parser_single_declaration (cp_parser* parser,
27007 vec<deferred_access_check, va_gc> *checks,
27008 bool member_p,
27009 bool explicit_specialization_p,
27010 bool* friend_p)
27011 {
27012 int declares_class_or_enum;
27013 tree decl = NULL_TREE;
27014 cp_decl_specifier_seq decl_specifiers;
27015 bool function_definition_p = false;
27016 cp_token *decl_spec_token_start;
27017
27018 /* This function is only used when processing a template
27019 declaration. */
27020 gcc_assert (innermost_scope_kind () == sk_template_parms
27021 || innermost_scope_kind () == sk_template_spec);
27022
27023 /* Defer access checks until we know what is being declared. */
27024 push_deferring_access_checks (dk_deferred);
27025
27026 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27027 alternative. */
27028 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27029 cp_parser_decl_specifier_seq (parser,
27030 CP_PARSER_FLAGS_OPTIONAL,
27031 &decl_specifiers,
27032 &declares_class_or_enum);
27033 if (friend_p)
27034 *friend_p = cp_parser_friend_p (&decl_specifiers);
27035
27036 /* There are no template typedefs. */
27037 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27038 {
27039 error_at (decl_spec_token_start->location,
27040 "template declaration of %<typedef%>");
27041 decl = error_mark_node;
27042 }
27043
27044 /* Gather up the access checks that occurred the
27045 decl-specifier-seq. */
27046 stop_deferring_access_checks ();
27047
27048 /* Check for the declaration of a template class. */
27049 if (declares_class_or_enum)
27050 {
27051 if (cp_parser_declares_only_class_p (parser)
27052 || (declares_class_or_enum & 2))
27053 {
27054 // If this is a declaration, but not a definition, associate
27055 // any constraints with the type declaration. Constraints
27056 // are associated with definitions in cp_parser_class_specifier.
27057 if (declares_class_or_enum == 1)
27058 associate_classtype_constraints (decl_specifiers.type);
27059
27060 decl = shadow_tag (&decl_specifiers);
27061
27062 /* In this case:
27063
27064 struct C {
27065 friend template <typename T> struct A<T>::B;
27066 };
27067
27068 A<T>::B will be represented by a TYPENAME_TYPE, and
27069 therefore not recognized by shadow_tag. */
27070 if (friend_p && *friend_p
27071 && !decl
27072 && decl_specifiers.type
27073 && TYPE_P (decl_specifiers.type))
27074 decl = decl_specifiers.type;
27075
27076 if (decl && decl != error_mark_node)
27077 decl = TYPE_NAME (decl);
27078 else
27079 decl = error_mark_node;
27080
27081 /* Perform access checks for template parameters. */
27082 cp_parser_perform_template_parameter_access_checks (checks);
27083
27084 /* Give a helpful diagnostic for
27085 template <class T> struct A { } a;
27086 if we aren't already recovering from an error. */
27087 if (!cp_parser_declares_only_class_p (parser)
27088 && !seen_error ())
27089 {
27090 error_at (cp_lexer_peek_token (parser->lexer)->location,
27091 "a class template declaration must not declare "
27092 "anything else");
27093 cp_parser_skip_to_end_of_block_or_statement (parser);
27094 goto out;
27095 }
27096 }
27097 }
27098
27099 /* Complain about missing 'typename' or other invalid type names. */
27100 if (!decl_specifiers.any_type_specifiers_p
27101 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27102 {
27103 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27104 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27105 the rest of this declaration. */
27106 decl = error_mark_node;
27107 goto out;
27108 }
27109
27110 /* If it's not a template class, try for a template function. If
27111 the next token is a `;', then this declaration does not declare
27112 anything. But, if there were errors in the decl-specifiers, then
27113 the error might well have come from an attempted class-specifier.
27114 In that case, there's no need to warn about a missing declarator. */
27115 if (!decl
27116 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27117 || decl_specifiers.type != error_mark_node))
27118 {
27119 decl = cp_parser_init_declarator (parser,
27120 &decl_specifiers,
27121 checks,
27122 /*function_definition_allowed_p=*/true,
27123 member_p,
27124 declares_class_or_enum,
27125 &function_definition_p,
27126 NULL, NULL, NULL);
27127
27128 /* 7.1.1-1 [dcl.stc]
27129
27130 A storage-class-specifier shall not be specified in an explicit
27131 specialization... */
27132 if (decl
27133 && explicit_specialization_p
27134 && decl_specifiers.storage_class != sc_none)
27135 {
27136 error_at (decl_spec_token_start->location,
27137 "explicit template specialization cannot have a storage class");
27138 decl = error_mark_node;
27139 }
27140
27141 if (decl && VAR_P (decl))
27142 check_template_variable (decl);
27143 }
27144
27145 /* Look for a trailing `;' after the declaration. */
27146 if (!function_definition_p
27147 && (decl == error_mark_node
27148 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27149 cp_parser_skip_to_end_of_block_or_statement (parser);
27150
27151 out:
27152 pop_deferring_access_checks ();
27153
27154 /* Clear any current qualification; whatever comes next is the start
27155 of something new. */
27156 parser->scope = NULL_TREE;
27157 parser->qualifying_scope = NULL_TREE;
27158 parser->object_scope = NULL_TREE;
27159
27160 return decl;
27161 }
27162
27163 /* Parse a cast-expression that is not the operand of a unary "&". */
27164
27165 static cp_expr
27166 cp_parser_simple_cast_expression (cp_parser *parser)
27167 {
27168 return cp_parser_cast_expression (parser, /*address_p=*/false,
27169 /*cast_p=*/false, /*decltype*/false, NULL);
27170 }
27171
27172 /* Parse a functional cast to TYPE. Returns an expression
27173 representing the cast. */
27174
27175 static cp_expr
27176 cp_parser_functional_cast (cp_parser* parser, tree type)
27177 {
27178 vec<tree, va_gc> *vec;
27179 tree expression_list;
27180 cp_expr cast;
27181 bool nonconst_p;
27182
27183 location_t start_loc = input_location;
27184
27185 if (!type)
27186 type = error_mark_node;
27187
27188 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27189 {
27190 cp_lexer_set_source_position (parser->lexer);
27191 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27192 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27193 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27194 if (TREE_CODE (type) == TYPE_DECL)
27195 type = TREE_TYPE (type);
27196
27197 cast = finish_compound_literal (type, expression_list,
27198 tf_warning_or_error, fcl_functional);
27199 /* Create a location of the form:
27200 type_name{i, f}
27201 ^~~~~~~~~~~~~~~
27202 with caret == start at the start of the type name,
27203 finishing at the closing brace. */
27204 location_t finish_loc
27205 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27206 location_t combined_loc = make_location (start_loc, start_loc,
27207 finish_loc);
27208 cast.set_location (combined_loc);
27209 return cast;
27210 }
27211
27212
27213 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27214 /*cast_p=*/true,
27215 /*allow_expansion_p=*/true,
27216 /*non_constant_p=*/NULL);
27217 if (vec == NULL)
27218 expression_list = error_mark_node;
27219 else
27220 {
27221 expression_list = build_tree_list_vec (vec);
27222 release_tree_vector (vec);
27223 }
27224
27225 cast = build_functional_cast (type, expression_list,
27226 tf_warning_or_error);
27227 /* [expr.const]/1: In an integral constant expression "only type
27228 conversions to integral or enumeration type can be used". */
27229 if (TREE_CODE (type) == TYPE_DECL)
27230 type = TREE_TYPE (type);
27231 if (cast != error_mark_node
27232 && !cast_valid_in_integral_constant_expression_p (type)
27233 && cp_parser_non_integral_constant_expression (parser,
27234 NIC_CONSTRUCTOR))
27235 return error_mark_node;
27236
27237 /* Create a location of the form:
27238 float(i)
27239 ^~~~~~~~
27240 with caret == start at the start of the type name,
27241 finishing at the closing paren. */
27242 location_t finish_loc
27243 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27244 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27245 cast.set_location (combined_loc);
27246 return cast;
27247 }
27248
27249 /* Save the tokens that make up the body of a member function defined
27250 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27251 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27252 specifiers applied to the declaration. Returns the FUNCTION_DECL
27253 for the member function. */
27254
27255 static tree
27256 cp_parser_save_member_function_body (cp_parser* parser,
27257 cp_decl_specifier_seq *decl_specifiers,
27258 cp_declarator *declarator,
27259 tree attributes)
27260 {
27261 cp_token *first;
27262 cp_token *last;
27263 tree fn;
27264 bool function_try_block = false;
27265
27266 /* Create the FUNCTION_DECL. */
27267 fn = grokmethod (decl_specifiers, declarator, attributes);
27268 cp_finalize_omp_declare_simd (parser, fn);
27269 cp_finalize_oacc_routine (parser, fn, true);
27270 /* If something went badly wrong, bail out now. */
27271 if (fn == error_mark_node)
27272 {
27273 /* If there's a function-body, skip it. */
27274 if (cp_parser_token_starts_function_definition_p
27275 (cp_lexer_peek_token (parser->lexer)))
27276 cp_parser_skip_to_end_of_block_or_statement (parser);
27277 return error_mark_node;
27278 }
27279
27280 /* Remember it, if there default args to post process. */
27281 cp_parser_save_default_args (parser, fn);
27282
27283 /* Save away the tokens that make up the body of the
27284 function. */
27285 first = parser->lexer->next_token;
27286
27287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27288 cp_lexer_consume_token (parser->lexer);
27289 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27290 RID_TRANSACTION_ATOMIC))
27291 {
27292 cp_lexer_consume_token (parser->lexer);
27293 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27294 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27295 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27296 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27297 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27298 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27299 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27300 {
27301 cp_lexer_consume_token (parser->lexer);
27302 cp_lexer_consume_token (parser->lexer);
27303 cp_lexer_consume_token (parser->lexer);
27304 cp_lexer_consume_token (parser->lexer);
27305 cp_lexer_consume_token (parser->lexer);
27306 }
27307 else
27308 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27309 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27310 {
27311 cp_lexer_consume_token (parser->lexer);
27312 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27313 break;
27314 }
27315 }
27316
27317 /* Handle function try blocks. */
27318 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27319 {
27320 cp_lexer_consume_token (parser->lexer);
27321 function_try_block = true;
27322 }
27323 /* We can have braced-init-list mem-initializers before the fn body. */
27324 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27325 {
27326 cp_lexer_consume_token (parser->lexer);
27327 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27328 {
27329 /* cache_group will stop after an un-nested { } pair, too. */
27330 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27331 break;
27332
27333 /* variadic mem-inits have ... after the ')'. */
27334 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27335 cp_lexer_consume_token (parser->lexer);
27336 }
27337 }
27338 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27339 /* Handle function try blocks. */
27340 if (function_try_block)
27341 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27342 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27343 last = parser->lexer->next_token;
27344
27345 /* Save away the inline definition; we will process it when the
27346 class is complete. */
27347 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27348 DECL_PENDING_INLINE_P (fn) = 1;
27349
27350 /* We need to know that this was defined in the class, so that
27351 friend templates are handled correctly. */
27352 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27353
27354 /* Add FN to the queue of functions to be parsed later. */
27355 vec_safe_push (unparsed_funs_with_definitions, fn);
27356
27357 return fn;
27358 }
27359
27360 /* Save the tokens that make up the in-class initializer for a non-static
27361 data member. Returns a DEFAULT_ARG. */
27362
27363 static tree
27364 cp_parser_save_nsdmi (cp_parser* parser)
27365 {
27366 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27367 }
27368
27369 /* Parse a template-argument-list, as well as the trailing ">" (but
27370 not the opening "<"). See cp_parser_template_argument_list for the
27371 return value. */
27372
27373 static tree
27374 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27375 {
27376 tree arguments;
27377 tree saved_scope;
27378 tree saved_qualifying_scope;
27379 tree saved_object_scope;
27380 bool saved_greater_than_is_operator_p;
27381 int saved_unevaluated_operand;
27382 int saved_inhibit_evaluation_warnings;
27383
27384 /* [temp.names]
27385
27386 When parsing a template-id, the first non-nested `>' is taken as
27387 the end of the template-argument-list rather than a greater-than
27388 operator. */
27389 saved_greater_than_is_operator_p
27390 = parser->greater_than_is_operator_p;
27391 parser->greater_than_is_operator_p = false;
27392 /* Parsing the argument list may modify SCOPE, so we save it
27393 here. */
27394 saved_scope = parser->scope;
27395 saved_qualifying_scope = parser->qualifying_scope;
27396 saved_object_scope = parser->object_scope;
27397 /* We need to evaluate the template arguments, even though this
27398 template-id may be nested within a "sizeof". */
27399 saved_unevaluated_operand = cp_unevaluated_operand;
27400 cp_unevaluated_operand = 0;
27401 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27402 c_inhibit_evaluation_warnings = 0;
27403 /* Parse the template-argument-list itself. */
27404 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27405 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27406 arguments = NULL_TREE;
27407 else
27408 arguments = cp_parser_template_argument_list (parser);
27409 /* Look for the `>' that ends the template-argument-list. If we find
27410 a '>>' instead, it's probably just a typo. */
27411 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27412 {
27413 if (cxx_dialect != cxx98)
27414 {
27415 /* In C++0x, a `>>' in a template argument list or cast
27416 expression is considered to be two separate `>'
27417 tokens. So, change the current token to a `>', but don't
27418 consume it: it will be consumed later when the outer
27419 template argument list (or cast expression) is parsed.
27420 Note that this replacement of `>' for `>>' is necessary
27421 even if we are parsing tentatively: in the tentative
27422 case, after calling
27423 cp_parser_enclosed_template_argument_list we will always
27424 throw away all of the template arguments and the first
27425 closing `>', either because the template argument list
27426 was erroneous or because we are replacing those tokens
27427 with a CPP_TEMPLATE_ID token. The second `>' (which will
27428 not have been thrown away) is needed either to close an
27429 outer template argument list or to complete a new-style
27430 cast. */
27431 cp_token *token = cp_lexer_peek_token (parser->lexer);
27432 token->type = CPP_GREATER;
27433 }
27434 else if (!saved_greater_than_is_operator_p)
27435 {
27436 /* If we're in a nested template argument list, the '>>' has
27437 to be a typo for '> >'. We emit the error message, but we
27438 continue parsing and we push a '>' as next token, so that
27439 the argument list will be parsed correctly. Note that the
27440 global source location is still on the token before the
27441 '>>', so we need to say explicitly where we want it. */
27442 cp_token *token = cp_lexer_peek_token (parser->lexer);
27443 gcc_rich_location richloc (token->location);
27444 richloc.add_fixit_replace ("> >");
27445 error_at (&richloc, "%<>>%> should be %<> >%> "
27446 "within a nested template argument list");
27447
27448 token->type = CPP_GREATER;
27449 }
27450 else
27451 {
27452 /* If this is not a nested template argument list, the '>>'
27453 is a typo for '>'. Emit an error message and continue.
27454 Same deal about the token location, but here we can get it
27455 right by consuming the '>>' before issuing the diagnostic. */
27456 cp_token *token = cp_lexer_consume_token (parser->lexer);
27457 error_at (token->location,
27458 "spurious %<>>%>, use %<>%> to terminate "
27459 "a template argument list");
27460 }
27461 }
27462 else
27463 cp_parser_skip_to_end_of_template_parameter_list (parser);
27464 /* The `>' token might be a greater-than operator again now. */
27465 parser->greater_than_is_operator_p
27466 = saved_greater_than_is_operator_p;
27467 /* Restore the SAVED_SCOPE. */
27468 parser->scope = saved_scope;
27469 parser->qualifying_scope = saved_qualifying_scope;
27470 parser->object_scope = saved_object_scope;
27471 cp_unevaluated_operand = saved_unevaluated_operand;
27472 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27473
27474 return arguments;
27475 }
27476
27477 /* MEMBER_FUNCTION is a member function, or a friend. If default
27478 arguments, or the body of the function have not yet been parsed,
27479 parse them now. */
27480
27481 static void
27482 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27483 {
27484 timevar_push (TV_PARSE_INMETH);
27485 /* If this member is a template, get the underlying
27486 FUNCTION_DECL. */
27487 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27488 member_function = DECL_TEMPLATE_RESULT (member_function);
27489
27490 /* There should not be any class definitions in progress at this
27491 point; the bodies of members are only parsed outside of all class
27492 definitions. */
27493 gcc_assert (parser->num_classes_being_defined == 0);
27494 /* While we're parsing the member functions we might encounter more
27495 classes. We want to handle them right away, but we don't want
27496 them getting mixed up with functions that are currently in the
27497 queue. */
27498 push_unparsed_function_queues (parser);
27499
27500 /* Make sure that any template parameters are in scope. */
27501 maybe_begin_member_template_processing (member_function);
27502
27503 /* If the body of the function has not yet been parsed, parse it
27504 now. */
27505 if (DECL_PENDING_INLINE_P (member_function))
27506 {
27507 tree function_scope;
27508 cp_token_cache *tokens;
27509
27510 /* The function is no longer pending; we are processing it. */
27511 tokens = DECL_PENDING_INLINE_INFO (member_function);
27512 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27513 DECL_PENDING_INLINE_P (member_function) = 0;
27514
27515 /* If this is a local class, enter the scope of the containing
27516 function. */
27517 function_scope = current_function_decl;
27518 if (function_scope)
27519 push_function_context ();
27520
27521 /* Push the body of the function onto the lexer stack. */
27522 cp_parser_push_lexer_for_tokens (parser, tokens);
27523
27524 /* Let the front end know that we going to be defining this
27525 function. */
27526 start_preparsed_function (member_function, NULL_TREE,
27527 SF_PRE_PARSED | SF_INCLASS_INLINE);
27528
27529 /* Don't do access checking if it is a templated function. */
27530 if (processing_template_decl)
27531 push_deferring_access_checks (dk_no_check);
27532
27533 /* #pragma omp declare reduction needs special parsing. */
27534 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27535 {
27536 parser->lexer->in_pragma = true;
27537 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27538 finish_function (/*inline_p=*/true);
27539 cp_check_omp_declare_reduction (member_function);
27540 }
27541 else
27542 /* Now, parse the body of the function. */
27543 cp_parser_function_definition_after_declarator (parser,
27544 /*inline_p=*/true);
27545
27546 if (processing_template_decl)
27547 pop_deferring_access_checks ();
27548
27549 /* Leave the scope of the containing function. */
27550 if (function_scope)
27551 pop_function_context ();
27552 cp_parser_pop_lexer (parser);
27553 }
27554
27555 /* Remove any template parameters from the symbol table. */
27556 maybe_end_member_template_processing ();
27557
27558 /* Restore the queue. */
27559 pop_unparsed_function_queues (parser);
27560 timevar_pop (TV_PARSE_INMETH);
27561 }
27562
27563 /* If DECL contains any default args, remember it on the unparsed
27564 functions queue. */
27565
27566 static void
27567 cp_parser_save_default_args (cp_parser* parser, tree decl)
27568 {
27569 tree probe;
27570
27571 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27572 probe;
27573 probe = TREE_CHAIN (probe))
27574 if (TREE_PURPOSE (probe))
27575 {
27576 cp_default_arg_entry entry = {current_class_type, decl};
27577 vec_safe_push (unparsed_funs_with_default_args, entry);
27578 break;
27579 }
27580 }
27581
27582 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27583 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27584 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27585 from the parameter-type-list. */
27586
27587 static tree
27588 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27589 tree default_arg, tree parmtype)
27590 {
27591 cp_token_cache *tokens;
27592 tree parsed_arg;
27593 bool dummy;
27594
27595 if (default_arg == error_mark_node)
27596 return error_mark_node;
27597
27598 /* Push the saved tokens for the default argument onto the parser's
27599 lexer stack. */
27600 tokens = DEFARG_TOKENS (default_arg);
27601 cp_parser_push_lexer_for_tokens (parser, tokens);
27602
27603 start_lambda_scope (decl);
27604
27605 /* Parse the default argument. */
27606 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27607 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27608 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27609
27610 finish_lambda_scope ();
27611
27612 if (parsed_arg == error_mark_node)
27613 cp_parser_skip_to_end_of_statement (parser);
27614
27615 if (!processing_template_decl)
27616 {
27617 /* In a non-template class, check conversions now. In a template,
27618 we'll wait and instantiate these as needed. */
27619 if (TREE_CODE (decl) == PARM_DECL)
27620 parsed_arg = check_default_argument (parmtype, parsed_arg,
27621 tf_warning_or_error);
27622 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27623 parsed_arg = error_mark_node;
27624 else
27625 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27626 }
27627
27628 /* If the token stream has not been completely used up, then
27629 there was extra junk after the end of the default
27630 argument. */
27631 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27632 {
27633 if (TREE_CODE (decl) == PARM_DECL)
27634 cp_parser_error (parser, "expected %<,%>");
27635 else
27636 cp_parser_error (parser, "expected %<;%>");
27637 }
27638
27639 /* Revert to the main lexer. */
27640 cp_parser_pop_lexer (parser);
27641
27642 return parsed_arg;
27643 }
27644
27645 /* FIELD is a non-static data member with an initializer which we saved for
27646 later; parse it now. */
27647
27648 static void
27649 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27650 {
27651 tree def;
27652
27653 maybe_begin_member_template_processing (field);
27654
27655 push_unparsed_function_queues (parser);
27656 def = cp_parser_late_parse_one_default_arg (parser, field,
27657 DECL_INITIAL (field),
27658 NULL_TREE);
27659 pop_unparsed_function_queues (parser);
27660
27661 maybe_end_member_template_processing ();
27662
27663 DECL_INITIAL (field) = def;
27664 }
27665
27666 /* FN is a FUNCTION_DECL which may contains a parameter with an
27667 unparsed DEFAULT_ARG. Parse the default args now. This function
27668 assumes that the current scope is the scope in which the default
27669 argument should be processed. */
27670
27671 static void
27672 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27673 {
27674 bool saved_local_variables_forbidden_p;
27675 tree parm, parmdecl;
27676
27677 /* While we're parsing the default args, we might (due to the
27678 statement expression extension) encounter more classes. We want
27679 to handle them right away, but we don't want them getting mixed
27680 up with default args that are currently in the queue. */
27681 push_unparsed_function_queues (parser);
27682
27683 /* Local variable names (and the `this' keyword) may not appear
27684 in a default argument. */
27685 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27686 parser->local_variables_forbidden_p = true;
27687
27688 push_defarg_context (fn);
27689
27690 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27691 parmdecl = DECL_ARGUMENTS (fn);
27692 parm && parm != void_list_node;
27693 parm = TREE_CHAIN (parm),
27694 parmdecl = DECL_CHAIN (parmdecl))
27695 {
27696 tree default_arg = TREE_PURPOSE (parm);
27697 tree parsed_arg;
27698 vec<tree, va_gc> *insts;
27699 tree copy;
27700 unsigned ix;
27701
27702 if (!default_arg)
27703 continue;
27704
27705 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27706 /* This can happen for a friend declaration for a function
27707 already declared with default arguments. */
27708 continue;
27709
27710 parsed_arg
27711 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27712 default_arg,
27713 TREE_VALUE (parm));
27714 TREE_PURPOSE (parm) = parsed_arg;
27715
27716 /* Update any instantiations we've already created. */
27717 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27718 vec_safe_iterate (insts, ix, &copy); ix++)
27719 TREE_PURPOSE (copy) = parsed_arg;
27720 }
27721
27722 pop_defarg_context ();
27723
27724 /* Make sure no default arg is missing. */
27725 check_default_args (fn);
27726
27727 /* Restore the state of local_variables_forbidden_p. */
27728 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27729
27730 /* Restore the queue. */
27731 pop_unparsed_function_queues (parser);
27732 }
27733
27734 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27735
27736 sizeof ... ( identifier )
27737
27738 where the 'sizeof' token has already been consumed. */
27739
27740 static tree
27741 cp_parser_sizeof_pack (cp_parser *parser)
27742 {
27743 /* Consume the `...'. */
27744 cp_lexer_consume_token (parser->lexer);
27745 maybe_warn_variadic_templates ();
27746
27747 matching_parens parens;
27748 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27749 if (paren)
27750 parens.consume_open (parser);
27751 else
27752 permerror (cp_lexer_peek_token (parser->lexer)->location,
27753 "%<sizeof...%> argument must be surrounded by parentheses");
27754
27755 cp_token *token = cp_lexer_peek_token (parser->lexer);
27756 tree name = cp_parser_identifier (parser);
27757 if (name == error_mark_node)
27758 return error_mark_node;
27759 /* The name is not qualified. */
27760 parser->scope = NULL_TREE;
27761 parser->qualifying_scope = NULL_TREE;
27762 parser->object_scope = NULL_TREE;
27763 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27764 if (expr == error_mark_node)
27765 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27766 token->location);
27767 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27768 expr = TREE_TYPE (expr);
27769 else if (TREE_CODE (expr) == CONST_DECL)
27770 expr = DECL_INITIAL (expr);
27771 expr = make_pack_expansion (expr);
27772 PACK_EXPANSION_SIZEOF_P (expr) = true;
27773
27774 if (paren)
27775 parens.require_close (parser);
27776
27777 return expr;
27778 }
27779
27780 /* Parse the operand of `sizeof' (or a similar operator). Returns
27781 either a TYPE or an expression, depending on the form of the
27782 input. The KEYWORD indicates which kind of expression we have
27783 encountered. */
27784
27785 static tree
27786 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27787 {
27788 tree expr = NULL_TREE;
27789 const char *saved_message;
27790 char *tmp;
27791 bool saved_integral_constant_expression_p;
27792 bool saved_non_integral_constant_expression_p;
27793
27794 /* If it's a `...', then we are computing the length of a parameter
27795 pack. */
27796 if (keyword == RID_SIZEOF
27797 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27798 return cp_parser_sizeof_pack (parser);
27799
27800 /* Types cannot be defined in a `sizeof' expression. Save away the
27801 old message. */
27802 saved_message = parser->type_definition_forbidden_message;
27803 /* And create the new one. */
27804 tmp = concat ("types may not be defined in %<",
27805 IDENTIFIER_POINTER (ridpointers[keyword]),
27806 "%> expressions", NULL);
27807 parser->type_definition_forbidden_message = tmp;
27808
27809 /* The restrictions on constant-expressions do not apply inside
27810 sizeof expressions. */
27811 saved_integral_constant_expression_p
27812 = parser->integral_constant_expression_p;
27813 saved_non_integral_constant_expression_p
27814 = parser->non_integral_constant_expression_p;
27815 parser->integral_constant_expression_p = false;
27816
27817 /* Do not actually evaluate the expression. */
27818 ++cp_unevaluated_operand;
27819 ++c_inhibit_evaluation_warnings;
27820 /* If it's a `(', then we might be looking at the type-id
27821 construction. */
27822 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27823 {
27824 tree type = NULL_TREE;
27825
27826 /* We can't be sure yet whether we're looking at a type-id or an
27827 expression. */
27828 cp_parser_parse_tentatively (parser);
27829
27830 matching_parens parens;
27831 parens.consume_open (parser);
27832
27833 /* Note: as a GNU Extension, compound literals are considered
27834 postfix-expressions as they are in C99, so they are valid
27835 arguments to sizeof. See comment in cp_parser_cast_expression
27836 for details. */
27837 if (cp_parser_compound_literal_p (parser))
27838 cp_parser_simulate_error (parser);
27839 else
27840 {
27841 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27842 parser->in_type_id_in_expr_p = true;
27843 /* Look for the type-id. */
27844 type = cp_parser_type_id (parser);
27845 /* Look for the closing `)'. */
27846 parens.require_close (parser);
27847 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27848 }
27849
27850 /* If all went well, then we're done. */
27851 if (cp_parser_parse_definitely (parser))
27852 {
27853 cp_decl_specifier_seq decl_specs;
27854
27855 /* Build a trivial decl-specifier-seq. */
27856 clear_decl_specs (&decl_specs);
27857 decl_specs.type = type;
27858
27859 /* Call grokdeclarator to figure out what type this is. */
27860 expr = grokdeclarator (NULL,
27861 &decl_specs,
27862 TYPENAME,
27863 /*initialized=*/0,
27864 /*attrlist=*/NULL);
27865 }
27866 }
27867
27868 /* If the type-id production did not work out, then we must be
27869 looking at the unary-expression production. */
27870 if (!expr)
27871 expr = cp_parser_unary_expression (parser);
27872
27873 /* Go back to evaluating expressions. */
27874 --cp_unevaluated_operand;
27875 --c_inhibit_evaluation_warnings;
27876
27877 /* Free the message we created. */
27878 free (tmp);
27879 /* And restore the old one. */
27880 parser->type_definition_forbidden_message = saved_message;
27881 parser->integral_constant_expression_p
27882 = saved_integral_constant_expression_p;
27883 parser->non_integral_constant_expression_p
27884 = saved_non_integral_constant_expression_p;
27885
27886 return expr;
27887 }
27888
27889 /* If the current declaration has no declarator, return true. */
27890
27891 static bool
27892 cp_parser_declares_only_class_p (cp_parser *parser)
27893 {
27894 /* If the next token is a `;' or a `,' then there is no
27895 declarator. */
27896 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27897 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27898 }
27899
27900 /* Update the DECL_SPECS to reflect the storage class indicated by
27901 KEYWORD. */
27902
27903 static void
27904 cp_parser_set_storage_class (cp_parser *parser,
27905 cp_decl_specifier_seq *decl_specs,
27906 enum rid keyword,
27907 cp_token *token)
27908 {
27909 cp_storage_class storage_class;
27910
27911 if (parser->in_unbraced_linkage_specification_p)
27912 {
27913 error_at (token->location, "invalid use of %qD in linkage specification",
27914 ridpointers[keyword]);
27915 return;
27916 }
27917 else if (decl_specs->storage_class != sc_none)
27918 {
27919 decl_specs->conflicting_specifiers_p = true;
27920 return;
27921 }
27922
27923 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27924 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27925 && decl_specs->gnu_thread_keyword_p)
27926 {
27927 pedwarn (decl_specs->locations[ds_thread], 0,
27928 "%<__thread%> before %qD", ridpointers[keyword]);
27929 }
27930
27931 switch (keyword)
27932 {
27933 case RID_AUTO:
27934 storage_class = sc_auto;
27935 break;
27936 case RID_REGISTER:
27937 storage_class = sc_register;
27938 break;
27939 case RID_STATIC:
27940 storage_class = sc_static;
27941 break;
27942 case RID_EXTERN:
27943 storage_class = sc_extern;
27944 break;
27945 case RID_MUTABLE:
27946 storage_class = sc_mutable;
27947 break;
27948 default:
27949 gcc_unreachable ();
27950 }
27951 decl_specs->storage_class = storage_class;
27952 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27953
27954 /* A storage class specifier cannot be applied alongside a typedef
27955 specifier. If there is a typedef specifier present then set
27956 conflicting_specifiers_p which will trigger an error later
27957 on in grokdeclarator. */
27958 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27959 decl_specs->conflicting_specifiers_p = true;
27960 }
27961
27962 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27963 is true, the type is a class or enum definition. */
27964
27965 static void
27966 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27967 tree type_spec,
27968 cp_token *token,
27969 bool type_definition_p)
27970 {
27971 decl_specs->any_specifiers_p = true;
27972
27973 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27974 (with, for example, in "typedef int wchar_t;") we remember that
27975 this is what happened. In system headers, we ignore these
27976 declarations so that G++ can work with system headers that are not
27977 C++-safe. */
27978 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27979 && !type_definition_p
27980 && (type_spec == boolean_type_node
27981 || type_spec == char16_type_node
27982 || type_spec == char32_type_node
27983 || type_spec == wchar_type_node)
27984 && (decl_specs->type
27985 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27986 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27987 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27988 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27989 {
27990 decl_specs->redefined_builtin_type = type_spec;
27991 set_and_check_decl_spec_loc (decl_specs,
27992 ds_redefined_builtin_type_spec,
27993 token);
27994 if (!decl_specs->type)
27995 {
27996 decl_specs->type = type_spec;
27997 decl_specs->type_definition_p = false;
27998 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27999 }
28000 }
28001 else if (decl_specs->type)
28002 decl_specs->multiple_types_p = true;
28003 else
28004 {
28005 decl_specs->type = type_spec;
28006 decl_specs->type_definition_p = type_definition_p;
28007 decl_specs->redefined_builtin_type = NULL_TREE;
28008 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28009 }
28010 }
28011
28012 /* True iff TOKEN is the GNU keyword __thread. */
28013
28014 static bool
28015 token_is__thread (cp_token *token)
28016 {
28017 gcc_assert (token->keyword == RID_THREAD);
28018 return id_equal (token->u.value, "__thread");
28019 }
28020
28021 /* Set the location for a declarator specifier and check if it is
28022 duplicated.
28023
28024 DECL_SPECS is the sequence of declarator specifiers onto which to
28025 set the location.
28026
28027 DS is the single declarator specifier to set which location is to
28028 be set onto the existing sequence of declarators.
28029
28030 LOCATION is the location for the declarator specifier to
28031 consider. */
28032
28033 static void
28034 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28035 cp_decl_spec ds, cp_token *token)
28036 {
28037 gcc_assert (ds < ds_last);
28038
28039 if (decl_specs == NULL)
28040 return;
28041
28042 source_location location = token->location;
28043
28044 if (decl_specs->locations[ds] == 0)
28045 {
28046 decl_specs->locations[ds] = location;
28047 if (ds == ds_thread)
28048 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28049 }
28050 else
28051 {
28052 if (ds == ds_long)
28053 {
28054 if (decl_specs->locations[ds_long_long] != 0)
28055 error_at (location,
28056 "%<long long long%> is too long for GCC");
28057 else
28058 {
28059 decl_specs->locations[ds_long_long] = location;
28060 pedwarn_cxx98 (location,
28061 OPT_Wlong_long,
28062 "ISO C++ 1998 does not support %<long long%>");
28063 }
28064 }
28065 else if (ds == ds_thread)
28066 {
28067 bool gnu = token_is__thread (token);
28068 if (gnu != decl_specs->gnu_thread_keyword_p)
28069 error_at (location,
28070 "both %<__thread%> and %<thread_local%> specified");
28071 else
28072 {
28073 gcc_rich_location richloc (location);
28074 richloc.add_fixit_remove ();
28075 error_at (&richloc, "duplicate %qD", token->u.value);
28076 }
28077 }
28078 else
28079 {
28080 static const char *const decl_spec_names[] = {
28081 "signed",
28082 "unsigned",
28083 "short",
28084 "long",
28085 "const",
28086 "volatile",
28087 "restrict",
28088 "inline",
28089 "virtual",
28090 "explicit",
28091 "friend",
28092 "typedef",
28093 "using",
28094 "constexpr",
28095 "__complex"
28096 };
28097 gcc_rich_location richloc (location);
28098 richloc.add_fixit_remove ();
28099 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28100 }
28101 }
28102 }
28103
28104 /* Return true iff the declarator specifier DS is present in the
28105 sequence of declarator specifiers DECL_SPECS. */
28106
28107 bool
28108 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28109 cp_decl_spec ds)
28110 {
28111 gcc_assert (ds < ds_last);
28112
28113 if (decl_specs == NULL)
28114 return false;
28115
28116 return decl_specs->locations[ds] != 0;
28117 }
28118
28119 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28120 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28121
28122 static bool
28123 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28124 {
28125 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28126 }
28127
28128 /* Issue an error message indicating that TOKEN_DESC was expected.
28129 If KEYWORD is true, it indicated this function is called by
28130 cp_parser_require_keword and the required token can only be
28131 a indicated keyword.
28132
28133 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28134 within any error as the location of an "opening" token matching
28135 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28136 RT_CLOSE_PAREN). */
28137
28138 static void
28139 cp_parser_required_error (cp_parser *parser,
28140 required_token token_desc,
28141 bool keyword,
28142 location_t matching_location)
28143 {
28144 if (cp_parser_simulate_error (parser))
28145 return;
28146
28147 const char *gmsgid = NULL;
28148 switch (token_desc)
28149 {
28150 case RT_NEW:
28151 gmsgid = G_("expected %<new%>");
28152 break;
28153 case RT_DELETE:
28154 gmsgid = G_("expected %<delete%>");
28155 break;
28156 case RT_RETURN:
28157 gmsgid = G_("expected %<return%>");
28158 break;
28159 case RT_WHILE:
28160 gmsgid = G_("expected %<while%>");
28161 break;
28162 case RT_EXTERN:
28163 gmsgid = G_("expected %<extern%>");
28164 break;
28165 case RT_STATIC_ASSERT:
28166 gmsgid = G_("expected %<static_assert%>");
28167 break;
28168 case RT_DECLTYPE:
28169 gmsgid = G_("expected %<decltype%>");
28170 break;
28171 case RT_OPERATOR:
28172 gmsgid = G_("expected %<operator%>");
28173 break;
28174 case RT_CLASS:
28175 gmsgid = G_("expected %<class%>");
28176 break;
28177 case RT_TEMPLATE:
28178 gmsgid = G_("expected %<template%>");
28179 break;
28180 case RT_NAMESPACE:
28181 gmsgid = G_("expected %<namespace%>");
28182 break;
28183 case RT_USING:
28184 gmsgid = G_("expected %<using%>");
28185 break;
28186 case RT_ASM:
28187 gmsgid = G_("expected %<asm%>");
28188 break;
28189 case RT_TRY:
28190 gmsgid = G_("expected %<try%>");
28191 break;
28192 case RT_CATCH:
28193 gmsgid = G_("expected %<catch%>");
28194 break;
28195 case RT_THROW:
28196 gmsgid = G_("expected %<throw%>");
28197 break;
28198 case RT_LABEL:
28199 gmsgid = G_("expected %<__label__%>");
28200 break;
28201 case RT_AT_TRY:
28202 gmsgid = G_("expected %<@try%>");
28203 break;
28204 case RT_AT_SYNCHRONIZED:
28205 gmsgid = G_("expected %<@synchronized%>");
28206 break;
28207 case RT_AT_THROW:
28208 gmsgid = G_("expected %<@throw%>");
28209 break;
28210 case RT_TRANSACTION_ATOMIC:
28211 gmsgid = G_("expected %<__transaction_atomic%>");
28212 break;
28213 case RT_TRANSACTION_RELAXED:
28214 gmsgid = G_("expected %<__transaction_relaxed%>");
28215 break;
28216 default:
28217 break;
28218 }
28219
28220 if (!gmsgid && !keyword)
28221 {
28222 switch (token_desc)
28223 {
28224 case RT_SEMICOLON:
28225 gmsgid = G_("expected %<;%>");
28226 break;
28227 case RT_OPEN_PAREN:
28228 gmsgid = G_("expected %<(%>");
28229 break;
28230 case RT_CLOSE_BRACE:
28231 gmsgid = G_("expected %<}%>");
28232 break;
28233 case RT_OPEN_BRACE:
28234 gmsgid = G_("expected %<{%>");
28235 break;
28236 case RT_CLOSE_SQUARE:
28237 gmsgid = G_("expected %<]%>");
28238 break;
28239 case RT_OPEN_SQUARE:
28240 gmsgid = G_("expected %<[%>");
28241 break;
28242 case RT_COMMA:
28243 gmsgid = G_("expected %<,%>");
28244 break;
28245 case RT_SCOPE:
28246 gmsgid = G_("expected %<::%>");
28247 break;
28248 case RT_LESS:
28249 gmsgid = G_("expected %<<%>");
28250 break;
28251 case RT_GREATER:
28252 gmsgid = G_("expected %<>%>");
28253 break;
28254 case RT_EQ:
28255 gmsgid = G_("expected %<=%>");
28256 break;
28257 case RT_ELLIPSIS:
28258 gmsgid = G_("expected %<...%>");
28259 break;
28260 case RT_MULT:
28261 gmsgid = G_("expected %<*%>");
28262 break;
28263 case RT_COMPL:
28264 gmsgid = G_("expected %<~%>");
28265 break;
28266 case RT_COLON:
28267 gmsgid = G_("expected %<:%>");
28268 break;
28269 case RT_COLON_SCOPE:
28270 gmsgid = G_("expected %<:%> or %<::%>");
28271 break;
28272 case RT_CLOSE_PAREN:
28273 gmsgid = G_("expected %<)%>");
28274 break;
28275 case RT_COMMA_CLOSE_PAREN:
28276 gmsgid = G_("expected %<,%> or %<)%>");
28277 break;
28278 case RT_PRAGMA_EOL:
28279 gmsgid = G_("expected end of line");
28280 break;
28281 case RT_NAME:
28282 gmsgid = G_("expected identifier");
28283 break;
28284 case RT_SELECT:
28285 gmsgid = G_("expected selection-statement");
28286 break;
28287 case RT_ITERATION:
28288 gmsgid = G_("expected iteration-statement");
28289 break;
28290 case RT_JUMP:
28291 gmsgid = G_("expected jump-statement");
28292 break;
28293 case RT_CLASS_KEY:
28294 gmsgid = G_("expected class-key");
28295 break;
28296 case RT_CLASS_TYPENAME_TEMPLATE:
28297 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28298 break;
28299 default:
28300 gcc_unreachable ();
28301 }
28302 }
28303
28304 if (gmsgid)
28305 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28306 }
28307
28308
28309 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28310 issue an error message indicating that TOKEN_DESC was expected.
28311
28312 Returns the token consumed, if the token had the appropriate type.
28313 Otherwise, returns NULL.
28314
28315 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28316 within any error as the location of an "opening" token matching
28317 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28318 RT_CLOSE_PAREN). */
28319
28320 static cp_token *
28321 cp_parser_require (cp_parser* parser,
28322 enum cpp_ttype type,
28323 required_token token_desc,
28324 location_t matching_location)
28325 {
28326 if (cp_lexer_next_token_is (parser->lexer, type))
28327 return cp_lexer_consume_token (parser->lexer);
28328 else
28329 {
28330 /* Output the MESSAGE -- unless we're parsing tentatively. */
28331 if (!cp_parser_simulate_error (parser))
28332 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28333 matching_location);
28334 return NULL;
28335 }
28336 }
28337
28338 /* An error message is produced if the next token is not '>'.
28339 All further tokens are skipped until the desired token is
28340 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28341
28342 static void
28343 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28344 {
28345 /* Current level of '< ... >'. */
28346 unsigned level = 0;
28347 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28348 unsigned nesting_depth = 0;
28349
28350 /* Are we ready, yet? If not, issue error message. */
28351 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28352 return;
28353
28354 /* Skip tokens until the desired token is found. */
28355 while (true)
28356 {
28357 /* Peek at the next token. */
28358 switch (cp_lexer_peek_token (parser->lexer)->type)
28359 {
28360 case CPP_LESS:
28361 if (!nesting_depth)
28362 ++level;
28363 break;
28364
28365 case CPP_RSHIFT:
28366 if (cxx_dialect == cxx98)
28367 /* C++0x views the `>>' operator as two `>' tokens, but
28368 C++98 does not. */
28369 break;
28370 else if (!nesting_depth && level-- == 0)
28371 {
28372 /* We've hit a `>>' where the first `>' closes the
28373 template argument list, and the second `>' is
28374 spurious. Just consume the `>>' and stop; we've
28375 already produced at least one error. */
28376 cp_lexer_consume_token (parser->lexer);
28377 return;
28378 }
28379 /* Fall through for C++0x, so we handle the second `>' in
28380 the `>>'. */
28381 gcc_fallthrough ();
28382
28383 case CPP_GREATER:
28384 if (!nesting_depth && level-- == 0)
28385 {
28386 /* We've reached the token we want, consume it and stop. */
28387 cp_lexer_consume_token (parser->lexer);
28388 return;
28389 }
28390 break;
28391
28392 case CPP_OPEN_PAREN:
28393 case CPP_OPEN_SQUARE:
28394 ++nesting_depth;
28395 break;
28396
28397 case CPP_CLOSE_PAREN:
28398 case CPP_CLOSE_SQUARE:
28399 if (nesting_depth-- == 0)
28400 return;
28401 break;
28402
28403 case CPP_EOF:
28404 case CPP_PRAGMA_EOL:
28405 case CPP_SEMICOLON:
28406 case CPP_OPEN_BRACE:
28407 case CPP_CLOSE_BRACE:
28408 /* The '>' was probably forgotten, don't look further. */
28409 return;
28410
28411 default:
28412 break;
28413 }
28414
28415 /* Consume this token. */
28416 cp_lexer_consume_token (parser->lexer);
28417 }
28418 }
28419
28420 /* If the next token is the indicated keyword, consume it. Otherwise,
28421 issue an error message indicating that TOKEN_DESC was expected.
28422
28423 Returns the token consumed, if the token had the appropriate type.
28424 Otherwise, returns NULL. */
28425
28426 static cp_token *
28427 cp_parser_require_keyword (cp_parser* parser,
28428 enum rid keyword,
28429 required_token token_desc)
28430 {
28431 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28432
28433 if (token && token->keyword != keyword)
28434 {
28435 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28436 UNKNOWN_LOCATION);
28437 return NULL;
28438 }
28439
28440 return token;
28441 }
28442
28443 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28444 function-definition. */
28445
28446 static bool
28447 cp_parser_token_starts_function_definition_p (cp_token* token)
28448 {
28449 return (/* An ordinary function-body begins with an `{'. */
28450 token->type == CPP_OPEN_BRACE
28451 /* A ctor-initializer begins with a `:'. */
28452 || token->type == CPP_COLON
28453 /* A function-try-block begins with `try'. */
28454 || token->keyword == RID_TRY
28455 /* A function-transaction-block begins with `__transaction_atomic'
28456 or `__transaction_relaxed'. */
28457 || token->keyword == RID_TRANSACTION_ATOMIC
28458 || token->keyword == RID_TRANSACTION_RELAXED
28459 /* The named return value extension begins with `return'. */
28460 || token->keyword == RID_RETURN);
28461 }
28462
28463 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28464 definition. */
28465
28466 static bool
28467 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28468 {
28469 cp_token *token;
28470
28471 token = cp_lexer_peek_token (parser->lexer);
28472 return (token->type == CPP_OPEN_BRACE
28473 || (token->type == CPP_COLON
28474 && !parser->colon_doesnt_start_class_def_p));
28475 }
28476
28477 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28478 C++0x) ending a template-argument. */
28479
28480 static bool
28481 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28482 {
28483 cp_token *token;
28484
28485 token = cp_lexer_peek_token (parser->lexer);
28486 return (token->type == CPP_COMMA
28487 || token->type == CPP_GREATER
28488 || token->type == CPP_ELLIPSIS
28489 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28490 }
28491
28492 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28493 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28494
28495 static bool
28496 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28497 size_t n)
28498 {
28499 cp_token *token;
28500
28501 token = cp_lexer_peek_nth_token (parser->lexer, n);
28502 if (token->type == CPP_LESS)
28503 return true;
28504 /* Check for the sequence `<::' in the original code. It would be lexed as
28505 `[:', where `[' is a digraph, and there is no whitespace before
28506 `:'. */
28507 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28508 {
28509 cp_token *token2;
28510 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28511 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28512 return true;
28513 }
28514 return false;
28515 }
28516
28517 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28518 or none_type otherwise. */
28519
28520 static enum tag_types
28521 cp_parser_token_is_class_key (cp_token* token)
28522 {
28523 switch (token->keyword)
28524 {
28525 case RID_CLASS:
28526 return class_type;
28527 case RID_STRUCT:
28528 return record_type;
28529 case RID_UNION:
28530 return union_type;
28531
28532 default:
28533 return none_type;
28534 }
28535 }
28536
28537 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28538 or none_type otherwise or if the token is null. */
28539
28540 static enum tag_types
28541 cp_parser_token_is_type_parameter_key (cp_token* token)
28542 {
28543 if (!token)
28544 return none_type;
28545
28546 switch (token->keyword)
28547 {
28548 case RID_CLASS:
28549 return class_type;
28550 case RID_TYPENAME:
28551 return typename_type;
28552
28553 default:
28554 return none_type;
28555 }
28556 }
28557
28558 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28559
28560 static void
28561 cp_parser_check_class_key (enum tag_types class_key, tree type)
28562 {
28563 if (type == error_mark_node)
28564 return;
28565 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28566 {
28567 if (permerror (input_location, "%qs tag used in naming %q#T",
28568 class_key == union_type ? "union"
28569 : class_key == record_type ? "struct" : "class",
28570 type))
28571 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28572 "%q#T was previously declared here", type);
28573 }
28574 }
28575
28576 /* Issue an error message if DECL is redeclared with different
28577 access than its original declaration [class.access.spec/3].
28578 This applies to nested classes, nested class templates and
28579 enumerations [class.mem/1]. */
28580
28581 static void
28582 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28583 {
28584 if (!decl
28585 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28586 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28587 return;
28588
28589 if ((TREE_PRIVATE (decl)
28590 != (current_access_specifier == access_private_node))
28591 || (TREE_PROTECTED (decl)
28592 != (current_access_specifier == access_protected_node)))
28593 error_at (location, "%qD redeclared with different access", decl);
28594 }
28595
28596 /* Look for the `template' keyword, as a syntactic disambiguator.
28597 Return TRUE iff it is present, in which case it will be
28598 consumed. */
28599
28600 static bool
28601 cp_parser_optional_template_keyword (cp_parser *parser)
28602 {
28603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28604 {
28605 /* In C++98 the `template' keyword can only be used within templates;
28606 outside templates the parser can always figure out what is a
28607 template and what is not. In C++11, per the resolution of DR 468,
28608 `template' is allowed in cases where it is not strictly necessary. */
28609 if (!processing_template_decl
28610 && pedantic && cxx_dialect == cxx98)
28611 {
28612 cp_token *token = cp_lexer_peek_token (parser->lexer);
28613 pedwarn (token->location, OPT_Wpedantic,
28614 "in C++98 %<template%> (as a disambiguator) is only "
28615 "allowed within templates");
28616 /* If this part of the token stream is rescanned, the same
28617 error message would be generated. So, we purge the token
28618 from the stream. */
28619 cp_lexer_purge_token (parser->lexer);
28620 return false;
28621 }
28622 else
28623 {
28624 /* Consume the `template' keyword. */
28625 cp_lexer_consume_token (parser->lexer);
28626 return true;
28627 }
28628 }
28629 return false;
28630 }
28631
28632 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28633 set PARSER->SCOPE, and perform other related actions. */
28634
28635 static void
28636 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28637 {
28638 struct tree_check *check_value;
28639
28640 /* Get the stored value. */
28641 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28642 /* Set the scope from the stored value. */
28643 parser->scope = saved_checks_value (check_value);
28644 parser->qualifying_scope = check_value->qualifying_scope;
28645 parser->object_scope = NULL_TREE;
28646 }
28647
28648 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28649 encounter the end of a block before what we were looking for. */
28650
28651 static bool
28652 cp_parser_cache_group (cp_parser *parser,
28653 enum cpp_ttype end,
28654 unsigned depth)
28655 {
28656 while (true)
28657 {
28658 cp_token *token = cp_lexer_peek_token (parser->lexer);
28659
28660 /* Abort a parenthesized expression if we encounter a semicolon. */
28661 if ((end == CPP_CLOSE_PAREN || depth == 0)
28662 && token->type == CPP_SEMICOLON)
28663 return true;
28664 /* If we've reached the end of the file, stop. */
28665 if (token->type == CPP_EOF
28666 || (end != CPP_PRAGMA_EOL
28667 && token->type == CPP_PRAGMA_EOL))
28668 return true;
28669 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28670 /* We've hit the end of an enclosing block, so there's been some
28671 kind of syntax error. */
28672 return true;
28673
28674 /* Consume the token. */
28675 cp_lexer_consume_token (parser->lexer);
28676 /* See if it starts a new group. */
28677 if (token->type == CPP_OPEN_BRACE)
28678 {
28679 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28680 /* In theory this should probably check end == '}', but
28681 cp_parser_save_member_function_body needs it to exit
28682 after either '}' or ')' when called with ')'. */
28683 if (depth == 0)
28684 return false;
28685 }
28686 else if (token->type == CPP_OPEN_PAREN)
28687 {
28688 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28689 if (depth == 0 && end == CPP_CLOSE_PAREN)
28690 return false;
28691 }
28692 else if (token->type == CPP_PRAGMA)
28693 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28694 else if (token->type == end)
28695 return false;
28696 }
28697 }
28698
28699 /* Like above, for caching a default argument or NSDMI. Both of these are
28700 terminated by a non-nested comma, but it can be unclear whether or not a
28701 comma is nested in a template argument list unless we do more parsing.
28702 In order to handle this ambiguity, when we encounter a ',' after a '<'
28703 we try to parse what follows as a parameter-declaration-list (in the
28704 case of a default argument) or a member-declarator (in the case of an
28705 NSDMI). If that succeeds, then we stop caching. */
28706
28707 static tree
28708 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28709 {
28710 unsigned depth = 0;
28711 int maybe_template_id = 0;
28712 cp_token *first_token;
28713 cp_token *token;
28714 tree default_argument;
28715
28716 /* Add tokens until we have processed the entire default
28717 argument. We add the range [first_token, token). */
28718 first_token = cp_lexer_peek_token (parser->lexer);
28719 if (first_token->type == CPP_OPEN_BRACE)
28720 {
28721 /* For list-initialization, this is straightforward. */
28722 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28723 token = cp_lexer_peek_token (parser->lexer);
28724 }
28725 else while (true)
28726 {
28727 bool done = false;
28728
28729 /* Peek at the next token. */
28730 token = cp_lexer_peek_token (parser->lexer);
28731 /* What we do depends on what token we have. */
28732 switch (token->type)
28733 {
28734 /* In valid code, a default argument must be
28735 immediately followed by a `,' `)', or `...'. */
28736 case CPP_COMMA:
28737 if (depth == 0 && maybe_template_id)
28738 {
28739 /* If we've seen a '<', we might be in a
28740 template-argument-list. Until Core issue 325 is
28741 resolved, we don't know how this situation ought
28742 to be handled, so try to DTRT. We check whether
28743 what comes after the comma is a valid parameter
28744 declaration list. If it is, then the comma ends
28745 the default argument; otherwise the default
28746 argument continues. */
28747 bool error = false;
28748 cp_token *peek;
28749
28750 /* Set ITALP so cp_parser_parameter_declaration_list
28751 doesn't decide to commit to this parse. */
28752 bool saved_italp = parser->in_template_argument_list_p;
28753 parser->in_template_argument_list_p = true;
28754
28755 cp_parser_parse_tentatively (parser);
28756
28757 if (nsdmi)
28758 {
28759 /* Parse declarators until we reach a non-comma or
28760 somthing that cannot be an initializer.
28761 Just checking whether we're looking at a single
28762 declarator is insufficient. Consider:
28763 int var = tuple<T,U>::x;
28764 The template parameter 'U' looks exactly like a
28765 declarator. */
28766 do
28767 {
28768 int ctor_dtor_or_conv_p;
28769 cp_lexer_consume_token (parser->lexer);
28770 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28771 &ctor_dtor_or_conv_p,
28772 /*parenthesized_p=*/NULL,
28773 /*member_p=*/true,
28774 /*friend_p=*/false);
28775 peek = cp_lexer_peek_token (parser->lexer);
28776 if (cp_parser_error_occurred (parser))
28777 break;
28778 }
28779 while (peek->type == CPP_COMMA);
28780 /* If we met an '=' or ';' then the original comma
28781 was the end of the NSDMI. Otherwise assume
28782 we're still in the NSDMI. */
28783 error = (peek->type != CPP_EQ
28784 && peek->type != CPP_SEMICOLON);
28785 }
28786 else
28787 {
28788 cp_lexer_consume_token (parser->lexer);
28789 begin_scope (sk_function_parms, NULL_TREE);
28790 cp_parser_parameter_declaration_list (parser, &error);
28791 pop_bindings_and_leave_scope ();
28792 }
28793 if (!cp_parser_error_occurred (parser) && !error)
28794 done = true;
28795 cp_parser_abort_tentative_parse (parser);
28796
28797 parser->in_template_argument_list_p = saved_italp;
28798 break;
28799 }
28800 /* FALLTHRU */
28801 case CPP_CLOSE_PAREN:
28802 case CPP_ELLIPSIS:
28803 /* If we run into a non-nested `;', `}', or `]',
28804 then the code is invalid -- but the default
28805 argument is certainly over. */
28806 case CPP_SEMICOLON:
28807 case CPP_CLOSE_BRACE:
28808 case CPP_CLOSE_SQUARE:
28809 if (depth == 0
28810 /* Handle correctly int n = sizeof ... ( p ); */
28811 && token->type != CPP_ELLIPSIS)
28812 done = true;
28813 /* Update DEPTH, if necessary. */
28814 else if (token->type == CPP_CLOSE_PAREN
28815 || token->type == CPP_CLOSE_BRACE
28816 || token->type == CPP_CLOSE_SQUARE)
28817 --depth;
28818 break;
28819
28820 case CPP_OPEN_PAREN:
28821 case CPP_OPEN_SQUARE:
28822 case CPP_OPEN_BRACE:
28823 ++depth;
28824 break;
28825
28826 case CPP_LESS:
28827 if (depth == 0)
28828 /* This might be the comparison operator, or it might
28829 start a template argument list. */
28830 ++maybe_template_id;
28831 break;
28832
28833 case CPP_RSHIFT:
28834 if (cxx_dialect == cxx98)
28835 break;
28836 /* Fall through for C++0x, which treats the `>>'
28837 operator like two `>' tokens in certain
28838 cases. */
28839 gcc_fallthrough ();
28840
28841 case CPP_GREATER:
28842 if (depth == 0)
28843 {
28844 /* This might be an operator, or it might close a
28845 template argument list. But if a previous '<'
28846 started a template argument list, this will have
28847 closed it, so we can't be in one anymore. */
28848 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28849 if (maybe_template_id < 0)
28850 maybe_template_id = 0;
28851 }
28852 break;
28853
28854 /* If we run out of tokens, issue an error message. */
28855 case CPP_EOF:
28856 case CPP_PRAGMA_EOL:
28857 error_at (token->location, "file ends in default argument");
28858 return error_mark_node;
28859
28860 case CPP_NAME:
28861 case CPP_SCOPE:
28862 /* In these cases, we should look for template-ids.
28863 For example, if the default argument is
28864 `X<int, double>()', we need to do name lookup to
28865 figure out whether or not `X' is a template; if
28866 so, the `,' does not end the default argument.
28867
28868 That is not yet done. */
28869 break;
28870
28871 default:
28872 break;
28873 }
28874
28875 /* If we've reached the end, stop. */
28876 if (done)
28877 break;
28878
28879 /* Add the token to the token block. */
28880 token = cp_lexer_consume_token (parser->lexer);
28881 }
28882
28883 /* Create a DEFAULT_ARG to represent the unparsed default
28884 argument. */
28885 default_argument = make_node (DEFAULT_ARG);
28886 DEFARG_TOKENS (default_argument)
28887 = cp_token_cache_new (first_token, token);
28888 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28889
28890 return default_argument;
28891 }
28892
28893 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28894
28895 location_t
28896 defarg_location (tree default_argument)
28897 {
28898 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
28899 location_t start = tokens->first->location;
28900 location_t end = tokens->last->location;
28901 return make_location (start, start, end);
28902 }
28903
28904 /* Begin parsing tentatively. We always save tokens while parsing
28905 tentatively so that if the tentative parsing fails we can restore the
28906 tokens. */
28907
28908 static void
28909 cp_parser_parse_tentatively (cp_parser* parser)
28910 {
28911 /* Enter a new parsing context. */
28912 parser->context = cp_parser_context_new (parser->context);
28913 /* Begin saving tokens. */
28914 cp_lexer_save_tokens (parser->lexer);
28915 /* In order to avoid repetitive access control error messages,
28916 access checks are queued up until we are no longer parsing
28917 tentatively. */
28918 push_deferring_access_checks (dk_deferred);
28919 }
28920
28921 /* Commit to the currently active tentative parse. */
28922
28923 static void
28924 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28925 {
28926 cp_parser_context *context;
28927 cp_lexer *lexer;
28928
28929 /* Mark all of the levels as committed. */
28930 lexer = parser->lexer;
28931 for (context = parser->context; context->next; context = context->next)
28932 {
28933 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28934 break;
28935 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28936 while (!cp_lexer_saving_tokens (lexer))
28937 lexer = lexer->next;
28938 cp_lexer_commit_tokens (lexer);
28939 }
28940 }
28941
28942 /* Commit to the topmost currently active tentative parse.
28943
28944 Note that this function shouldn't be called when there are
28945 irreversible side-effects while in a tentative state. For
28946 example, we shouldn't create a permanent entry in the symbol
28947 table, or issue an error message that might not apply if the
28948 tentative parse is aborted. */
28949
28950 static void
28951 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28952 {
28953 cp_parser_context *context = parser->context;
28954 cp_lexer *lexer = parser->lexer;
28955
28956 if (context)
28957 {
28958 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28959 return;
28960 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28961
28962 while (!cp_lexer_saving_tokens (lexer))
28963 lexer = lexer->next;
28964 cp_lexer_commit_tokens (lexer);
28965 }
28966 }
28967
28968 /* Abort the currently active tentative parse. All consumed tokens
28969 will be rolled back, and no diagnostics will be issued. */
28970
28971 static void
28972 cp_parser_abort_tentative_parse (cp_parser* parser)
28973 {
28974 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28975 || errorcount > 0);
28976 cp_parser_simulate_error (parser);
28977 /* Now, pretend that we want to see if the construct was
28978 successfully parsed. */
28979 cp_parser_parse_definitely (parser);
28980 }
28981
28982 /* Stop parsing tentatively. If a parse error has occurred, restore the
28983 token stream. Otherwise, commit to the tokens we have consumed.
28984 Returns true if no error occurred; false otherwise. */
28985
28986 static bool
28987 cp_parser_parse_definitely (cp_parser* parser)
28988 {
28989 bool error_occurred;
28990 cp_parser_context *context;
28991
28992 /* Remember whether or not an error occurred, since we are about to
28993 destroy that information. */
28994 error_occurred = cp_parser_error_occurred (parser);
28995 /* Remove the topmost context from the stack. */
28996 context = parser->context;
28997 parser->context = context->next;
28998 /* If no parse errors occurred, commit to the tentative parse. */
28999 if (!error_occurred)
29000 {
29001 /* Commit to the tokens read tentatively, unless that was
29002 already done. */
29003 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29004 cp_lexer_commit_tokens (parser->lexer);
29005
29006 pop_to_parent_deferring_access_checks ();
29007 }
29008 /* Otherwise, if errors occurred, roll back our state so that things
29009 are just as they were before we began the tentative parse. */
29010 else
29011 {
29012 cp_lexer_rollback_tokens (parser->lexer);
29013 pop_deferring_access_checks ();
29014 }
29015 /* Add the context to the front of the free list. */
29016 context->next = cp_parser_context_free_list;
29017 cp_parser_context_free_list = context;
29018
29019 return !error_occurred;
29020 }
29021
29022 /* Returns true if we are parsing tentatively and are not committed to
29023 this tentative parse. */
29024
29025 static bool
29026 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29027 {
29028 return (cp_parser_parsing_tentatively (parser)
29029 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29030 }
29031
29032 /* Returns nonzero iff an error has occurred during the most recent
29033 tentative parse. */
29034
29035 static bool
29036 cp_parser_error_occurred (cp_parser* parser)
29037 {
29038 return (cp_parser_parsing_tentatively (parser)
29039 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29040 }
29041
29042 /* Returns nonzero if GNU extensions are allowed. */
29043
29044 static bool
29045 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29046 {
29047 return parser->allow_gnu_extensions_p;
29048 }
29049 \f
29050 /* Objective-C++ Productions */
29051
29052
29053 /* Parse an Objective-C expression, which feeds into a primary-expression
29054 above.
29055
29056 objc-expression:
29057 objc-message-expression
29058 objc-string-literal
29059 objc-encode-expression
29060 objc-protocol-expression
29061 objc-selector-expression
29062
29063 Returns a tree representation of the expression. */
29064
29065 static cp_expr
29066 cp_parser_objc_expression (cp_parser* parser)
29067 {
29068 /* Try to figure out what kind of declaration is present. */
29069 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29070
29071 switch (kwd->type)
29072 {
29073 case CPP_OPEN_SQUARE:
29074 return cp_parser_objc_message_expression (parser);
29075
29076 case CPP_OBJC_STRING:
29077 kwd = cp_lexer_consume_token (parser->lexer);
29078 return objc_build_string_object (kwd->u.value);
29079
29080 case CPP_KEYWORD:
29081 switch (kwd->keyword)
29082 {
29083 case RID_AT_ENCODE:
29084 return cp_parser_objc_encode_expression (parser);
29085
29086 case RID_AT_PROTOCOL:
29087 return cp_parser_objc_protocol_expression (parser);
29088
29089 case RID_AT_SELECTOR:
29090 return cp_parser_objc_selector_expression (parser);
29091
29092 default:
29093 break;
29094 }
29095 /* FALLTHRU */
29096 default:
29097 error_at (kwd->location,
29098 "misplaced %<@%D%> Objective-C++ construct",
29099 kwd->u.value);
29100 cp_parser_skip_to_end_of_block_or_statement (parser);
29101 }
29102
29103 return error_mark_node;
29104 }
29105
29106 /* Parse an Objective-C message expression.
29107
29108 objc-message-expression:
29109 [ objc-message-receiver objc-message-args ]
29110
29111 Returns a representation of an Objective-C message. */
29112
29113 static tree
29114 cp_parser_objc_message_expression (cp_parser* parser)
29115 {
29116 tree receiver, messageargs;
29117
29118 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29119 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29120 receiver = cp_parser_objc_message_receiver (parser);
29121 messageargs = cp_parser_objc_message_args (parser);
29122 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29123 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29124
29125 tree result = objc_build_message_expr (receiver, messageargs);
29126
29127 /* Construct a location e.g.
29128 [self func1:5]
29129 ^~~~~~~~~~~~~~
29130 ranging from the '[' to the ']', with the caret at the start. */
29131 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29132 protected_set_expr_location (result, combined_loc);
29133
29134 return result;
29135 }
29136
29137 /* Parse an objc-message-receiver.
29138
29139 objc-message-receiver:
29140 expression
29141 simple-type-specifier
29142
29143 Returns a representation of the type or expression. */
29144
29145 static tree
29146 cp_parser_objc_message_receiver (cp_parser* parser)
29147 {
29148 tree rcv;
29149
29150 /* An Objective-C message receiver may be either (1) a type
29151 or (2) an expression. */
29152 cp_parser_parse_tentatively (parser);
29153 rcv = cp_parser_expression (parser);
29154
29155 /* If that worked out, fine. */
29156 if (cp_parser_parse_definitely (parser))
29157 return rcv;
29158
29159 cp_parser_parse_tentatively (parser);
29160 rcv = cp_parser_simple_type_specifier (parser,
29161 /*decl_specs=*/NULL,
29162 CP_PARSER_FLAGS_NONE);
29163
29164 if (cp_parser_parse_definitely (parser))
29165 return objc_get_class_reference (rcv);
29166
29167 cp_parser_error (parser, "objective-c++ message receiver expected");
29168 return error_mark_node;
29169 }
29170
29171 /* Parse the arguments and selectors comprising an Objective-C message.
29172
29173 objc-message-args:
29174 objc-selector
29175 objc-selector-args
29176 objc-selector-args , objc-comma-args
29177
29178 objc-selector-args:
29179 objc-selector [opt] : assignment-expression
29180 objc-selector-args objc-selector [opt] : assignment-expression
29181
29182 objc-comma-args:
29183 assignment-expression
29184 objc-comma-args , assignment-expression
29185
29186 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29187 selector arguments and TREE_VALUE containing a list of comma
29188 arguments. */
29189
29190 static tree
29191 cp_parser_objc_message_args (cp_parser* parser)
29192 {
29193 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29194 bool maybe_unary_selector_p = true;
29195 cp_token *token = cp_lexer_peek_token (parser->lexer);
29196
29197 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29198 {
29199 tree selector = NULL_TREE, arg;
29200
29201 if (token->type != CPP_COLON)
29202 selector = cp_parser_objc_selector (parser);
29203
29204 /* Detect if we have a unary selector. */
29205 if (maybe_unary_selector_p
29206 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29207 return build_tree_list (selector, NULL_TREE);
29208
29209 maybe_unary_selector_p = false;
29210 cp_parser_require (parser, CPP_COLON, RT_COLON);
29211 arg = cp_parser_assignment_expression (parser);
29212
29213 sel_args
29214 = chainon (sel_args,
29215 build_tree_list (selector, arg));
29216
29217 token = cp_lexer_peek_token (parser->lexer);
29218 }
29219
29220 /* Handle non-selector arguments, if any. */
29221 while (token->type == CPP_COMMA)
29222 {
29223 tree arg;
29224
29225 cp_lexer_consume_token (parser->lexer);
29226 arg = cp_parser_assignment_expression (parser);
29227
29228 addl_args
29229 = chainon (addl_args,
29230 build_tree_list (NULL_TREE, arg));
29231
29232 token = cp_lexer_peek_token (parser->lexer);
29233 }
29234
29235 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29236 {
29237 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29238 return build_tree_list (error_mark_node, error_mark_node);
29239 }
29240
29241 return build_tree_list (sel_args, addl_args);
29242 }
29243
29244 /* Parse an Objective-C encode expression.
29245
29246 objc-encode-expression:
29247 @encode objc-typename
29248
29249 Returns an encoded representation of the type argument. */
29250
29251 static cp_expr
29252 cp_parser_objc_encode_expression (cp_parser* parser)
29253 {
29254 tree type;
29255 cp_token *token;
29256 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29257
29258 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29259 matching_parens parens;
29260 parens.require_open (parser);
29261 token = cp_lexer_peek_token (parser->lexer);
29262 type = complete_type (cp_parser_type_id (parser));
29263 parens.require_close (parser);
29264
29265 if (!type)
29266 {
29267 error_at (token->location,
29268 "%<@encode%> must specify a type as an argument");
29269 return error_mark_node;
29270 }
29271
29272 /* This happens if we find @encode(T) (where T is a template
29273 typename or something dependent on a template typename) when
29274 parsing a template. In that case, we can't compile it
29275 immediately, but we rather create an AT_ENCODE_EXPR which will
29276 need to be instantiated when the template is used.
29277 */
29278 if (dependent_type_p (type))
29279 {
29280 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29281 TREE_READONLY (value) = 1;
29282 return value;
29283 }
29284
29285
29286 /* Build a location of the form:
29287 @encode(int)
29288 ^~~~~~~~~~~~
29289 with caret==start at the @ token, finishing at the close paren. */
29290 location_t combined_loc
29291 = make_location (start_loc, start_loc,
29292 cp_lexer_previous_token (parser->lexer)->location);
29293
29294 return cp_expr (objc_build_encode_expr (type), combined_loc);
29295 }
29296
29297 /* Parse an Objective-C @defs expression. */
29298
29299 static tree
29300 cp_parser_objc_defs_expression (cp_parser *parser)
29301 {
29302 tree name;
29303
29304 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29305 matching_parens parens;
29306 parens.require_open (parser);
29307 name = cp_parser_identifier (parser);
29308 parens.require_close (parser);
29309
29310 return objc_get_class_ivars (name);
29311 }
29312
29313 /* Parse an Objective-C protocol expression.
29314
29315 objc-protocol-expression:
29316 @protocol ( identifier )
29317
29318 Returns a representation of the protocol expression. */
29319
29320 static tree
29321 cp_parser_objc_protocol_expression (cp_parser* parser)
29322 {
29323 tree proto;
29324 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29325
29326 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29327 matching_parens parens;
29328 parens.require_open (parser);
29329 proto = cp_parser_identifier (parser);
29330 parens.require_close (parser);
29331
29332 /* Build a location of the form:
29333 @protocol(prot)
29334 ^~~~~~~~~~~~~~~
29335 with caret==start at the @ token, finishing at the close paren. */
29336 location_t combined_loc
29337 = make_location (start_loc, start_loc,
29338 cp_lexer_previous_token (parser->lexer)->location);
29339 tree result = objc_build_protocol_expr (proto);
29340 protected_set_expr_location (result, combined_loc);
29341 return result;
29342 }
29343
29344 /* Parse an Objective-C selector expression.
29345
29346 objc-selector-expression:
29347 @selector ( objc-method-signature )
29348
29349 objc-method-signature:
29350 objc-selector
29351 objc-selector-seq
29352
29353 objc-selector-seq:
29354 objc-selector :
29355 objc-selector-seq objc-selector :
29356
29357 Returns a representation of the method selector. */
29358
29359 static tree
29360 cp_parser_objc_selector_expression (cp_parser* parser)
29361 {
29362 tree sel_seq = NULL_TREE;
29363 bool maybe_unary_selector_p = true;
29364 cp_token *token;
29365 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29366
29367 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29368 matching_parens parens;
29369 parens.require_open (parser);
29370 token = cp_lexer_peek_token (parser->lexer);
29371
29372 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29373 || token->type == CPP_SCOPE)
29374 {
29375 tree selector = NULL_TREE;
29376
29377 if (token->type != CPP_COLON
29378 || token->type == CPP_SCOPE)
29379 selector = cp_parser_objc_selector (parser);
29380
29381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29382 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29383 {
29384 /* Detect if we have a unary selector. */
29385 if (maybe_unary_selector_p)
29386 {
29387 sel_seq = selector;
29388 goto finish_selector;
29389 }
29390 else
29391 {
29392 cp_parser_error (parser, "expected %<:%>");
29393 }
29394 }
29395 maybe_unary_selector_p = false;
29396 token = cp_lexer_consume_token (parser->lexer);
29397
29398 if (token->type == CPP_SCOPE)
29399 {
29400 sel_seq
29401 = chainon (sel_seq,
29402 build_tree_list (selector, NULL_TREE));
29403 sel_seq
29404 = chainon (sel_seq,
29405 build_tree_list (NULL_TREE, NULL_TREE));
29406 }
29407 else
29408 sel_seq
29409 = chainon (sel_seq,
29410 build_tree_list (selector, NULL_TREE));
29411
29412 token = cp_lexer_peek_token (parser->lexer);
29413 }
29414
29415 finish_selector:
29416 parens.require_close (parser);
29417
29418
29419 /* Build a location of the form:
29420 @selector(func)
29421 ^~~~~~~~~~~~~~~
29422 with caret==start at the @ token, finishing at the close paren. */
29423 location_t combined_loc
29424 = make_location (loc, loc,
29425 cp_lexer_previous_token (parser->lexer)->location);
29426 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29427 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29428 protected_set_expr_location (result, combined_loc);
29429 return result;
29430 }
29431
29432 /* Parse a list of identifiers.
29433
29434 objc-identifier-list:
29435 identifier
29436 objc-identifier-list , identifier
29437
29438 Returns a TREE_LIST of identifier nodes. */
29439
29440 static tree
29441 cp_parser_objc_identifier_list (cp_parser* parser)
29442 {
29443 tree identifier;
29444 tree list;
29445 cp_token *sep;
29446
29447 identifier = cp_parser_identifier (parser);
29448 if (identifier == error_mark_node)
29449 return error_mark_node;
29450
29451 list = build_tree_list (NULL_TREE, identifier);
29452 sep = cp_lexer_peek_token (parser->lexer);
29453
29454 while (sep->type == CPP_COMMA)
29455 {
29456 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29457 identifier = cp_parser_identifier (parser);
29458 if (identifier == error_mark_node)
29459 return list;
29460
29461 list = chainon (list, build_tree_list (NULL_TREE,
29462 identifier));
29463 sep = cp_lexer_peek_token (parser->lexer);
29464 }
29465
29466 return list;
29467 }
29468
29469 /* Parse an Objective-C alias declaration.
29470
29471 objc-alias-declaration:
29472 @compatibility_alias identifier identifier ;
29473
29474 This function registers the alias mapping with the Objective-C front end.
29475 It returns nothing. */
29476
29477 static void
29478 cp_parser_objc_alias_declaration (cp_parser* parser)
29479 {
29480 tree alias, orig;
29481
29482 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29483 alias = cp_parser_identifier (parser);
29484 orig = cp_parser_identifier (parser);
29485 objc_declare_alias (alias, orig);
29486 cp_parser_consume_semicolon_at_end_of_statement (parser);
29487 }
29488
29489 /* Parse an Objective-C class forward-declaration.
29490
29491 objc-class-declaration:
29492 @class objc-identifier-list ;
29493
29494 The function registers the forward declarations with the Objective-C
29495 front end. It returns nothing. */
29496
29497 static void
29498 cp_parser_objc_class_declaration (cp_parser* parser)
29499 {
29500 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29501 while (true)
29502 {
29503 tree id;
29504
29505 id = cp_parser_identifier (parser);
29506 if (id == error_mark_node)
29507 break;
29508
29509 objc_declare_class (id);
29510
29511 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29512 cp_lexer_consume_token (parser->lexer);
29513 else
29514 break;
29515 }
29516 cp_parser_consume_semicolon_at_end_of_statement (parser);
29517 }
29518
29519 /* Parse a list of Objective-C protocol references.
29520
29521 objc-protocol-refs-opt:
29522 objc-protocol-refs [opt]
29523
29524 objc-protocol-refs:
29525 < objc-identifier-list >
29526
29527 Returns a TREE_LIST of identifiers, if any. */
29528
29529 static tree
29530 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29531 {
29532 tree protorefs = NULL_TREE;
29533
29534 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29535 {
29536 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29537 protorefs = cp_parser_objc_identifier_list (parser);
29538 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29539 }
29540
29541 return protorefs;
29542 }
29543
29544 /* Parse a Objective-C visibility specification. */
29545
29546 static void
29547 cp_parser_objc_visibility_spec (cp_parser* parser)
29548 {
29549 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29550
29551 switch (vis->keyword)
29552 {
29553 case RID_AT_PRIVATE:
29554 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29555 break;
29556 case RID_AT_PROTECTED:
29557 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29558 break;
29559 case RID_AT_PUBLIC:
29560 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29561 break;
29562 case RID_AT_PACKAGE:
29563 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29564 break;
29565 default:
29566 return;
29567 }
29568
29569 /* Eat '@private'/'@protected'/'@public'. */
29570 cp_lexer_consume_token (parser->lexer);
29571 }
29572
29573 /* Parse an Objective-C method type. Return 'true' if it is a class
29574 (+) method, and 'false' if it is an instance (-) method. */
29575
29576 static inline bool
29577 cp_parser_objc_method_type (cp_parser* parser)
29578 {
29579 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29580 return true;
29581 else
29582 return false;
29583 }
29584
29585 /* Parse an Objective-C protocol qualifier. */
29586
29587 static tree
29588 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29589 {
29590 tree quals = NULL_TREE, node;
29591 cp_token *token = cp_lexer_peek_token (parser->lexer);
29592
29593 node = token->u.value;
29594
29595 while (node && identifier_p (node)
29596 && (node == ridpointers [(int) RID_IN]
29597 || node == ridpointers [(int) RID_OUT]
29598 || node == ridpointers [(int) RID_INOUT]
29599 || node == ridpointers [(int) RID_BYCOPY]
29600 || node == ridpointers [(int) RID_BYREF]
29601 || node == ridpointers [(int) RID_ONEWAY]))
29602 {
29603 quals = tree_cons (NULL_TREE, node, quals);
29604 cp_lexer_consume_token (parser->lexer);
29605 token = cp_lexer_peek_token (parser->lexer);
29606 node = token->u.value;
29607 }
29608
29609 return quals;
29610 }
29611
29612 /* Parse an Objective-C typename. */
29613
29614 static tree
29615 cp_parser_objc_typename (cp_parser* parser)
29616 {
29617 tree type_name = NULL_TREE;
29618
29619 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29620 {
29621 tree proto_quals, cp_type = NULL_TREE;
29622
29623 matching_parens parens;
29624 parens.consume_open (parser); /* Eat '('. */
29625 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29626
29627 /* An ObjC type name may consist of just protocol qualifiers, in which
29628 case the type shall default to 'id'. */
29629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29630 {
29631 cp_type = cp_parser_type_id (parser);
29632
29633 /* If the type could not be parsed, an error has already
29634 been produced. For error recovery, behave as if it had
29635 not been specified, which will use the default type
29636 'id'. */
29637 if (cp_type == error_mark_node)
29638 {
29639 cp_type = NULL_TREE;
29640 /* We need to skip to the closing parenthesis as
29641 cp_parser_type_id() does not seem to do it for
29642 us. */
29643 cp_parser_skip_to_closing_parenthesis (parser,
29644 /*recovering=*/true,
29645 /*or_comma=*/false,
29646 /*consume_paren=*/false);
29647 }
29648 }
29649
29650 parens.require_close (parser);
29651 type_name = build_tree_list (proto_quals, cp_type);
29652 }
29653
29654 return type_name;
29655 }
29656
29657 /* Check to see if TYPE refers to an Objective-C selector name. */
29658
29659 static bool
29660 cp_parser_objc_selector_p (enum cpp_ttype type)
29661 {
29662 return (type == CPP_NAME || type == CPP_KEYWORD
29663 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29664 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29665 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29666 || type == CPP_XOR || type == CPP_XOR_EQ);
29667 }
29668
29669 /* Parse an Objective-C selector. */
29670
29671 static tree
29672 cp_parser_objc_selector (cp_parser* parser)
29673 {
29674 cp_token *token = cp_lexer_consume_token (parser->lexer);
29675
29676 if (!cp_parser_objc_selector_p (token->type))
29677 {
29678 error_at (token->location, "invalid Objective-C++ selector name");
29679 return error_mark_node;
29680 }
29681
29682 /* C++ operator names are allowed to appear in ObjC selectors. */
29683 switch (token->type)
29684 {
29685 case CPP_AND_AND: return get_identifier ("and");
29686 case CPP_AND_EQ: return get_identifier ("and_eq");
29687 case CPP_AND: return get_identifier ("bitand");
29688 case CPP_OR: return get_identifier ("bitor");
29689 case CPP_COMPL: return get_identifier ("compl");
29690 case CPP_NOT: return get_identifier ("not");
29691 case CPP_NOT_EQ: return get_identifier ("not_eq");
29692 case CPP_OR_OR: return get_identifier ("or");
29693 case CPP_OR_EQ: return get_identifier ("or_eq");
29694 case CPP_XOR: return get_identifier ("xor");
29695 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29696 default: return token->u.value;
29697 }
29698 }
29699
29700 /* Parse an Objective-C params list. */
29701
29702 static tree
29703 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29704 {
29705 tree params = NULL_TREE;
29706 bool maybe_unary_selector_p = true;
29707 cp_token *token = cp_lexer_peek_token (parser->lexer);
29708
29709 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29710 {
29711 tree selector = NULL_TREE, type_name, identifier;
29712 tree parm_attr = NULL_TREE;
29713
29714 if (token->keyword == RID_ATTRIBUTE)
29715 break;
29716
29717 if (token->type != CPP_COLON)
29718 selector = cp_parser_objc_selector (parser);
29719
29720 /* Detect if we have a unary selector. */
29721 if (maybe_unary_selector_p
29722 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29723 {
29724 params = selector; /* Might be followed by attributes. */
29725 break;
29726 }
29727
29728 maybe_unary_selector_p = false;
29729 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29730 {
29731 /* Something went quite wrong. There should be a colon
29732 here, but there is not. Stop parsing parameters. */
29733 break;
29734 }
29735 type_name = cp_parser_objc_typename (parser);
29736 /* New ObjC allows attributes on parameters too. */
29737 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29738 parm_attr = cp_parser_attributes_opt (parser);
29739 identifier = cp_parser_identifier (parser);
29740
29741 params
29742 = chainon (params,
29743 objc_build_keyword_decl (selector,
29744 type_name,
29745 identifier,
29746 parm_attr));
29747
29748 token = cp_lexer_peek_token (parser->lexer);
29749 }
29750
29751 if (params == NULL_TREE)
29752 {
29753 cp_parser_error (parser, "objective-c++ method declaration is expected");
29754 return error_mark_node;
29755 }
29756
29757 /* We allow tail attributes for the method. */
29758 if (token->keyword == RID_ATTRIBUTE)
29759 {
29760 *attributes = cp_parser_attributes_opt (parser);
29761 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29762 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29763 return params;
29764 cp_parser_error (parser,
29765 "method attributes must be specified at the end");
29766 return error_mark_node;
29767 }
29768
29769 if (params == NULL_TREE)
29770 {
29771 cp_parser_error (parser, "objective-c++ method declaration is expected");
29772 return error_mark_node;
29773 }
29774 return params;
29775 }
29776
29777 /* Parse the non-keyword Objective-C params. */
29778
29779 static tree
29780 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29781 tree* attributes)
29782 {
29783 tree params = make_node (TREE_LIST);
29784 cp_token *token = cp_lexer_peek_token (parser->lexer);
29785 *ellipsisp = false; /* Initially, assume no ellipsis. */
29786
29787 while (token->type == CPP_COMMA)
29788 {
29789 cp_parameter_declarator *parmdecl;
29790 tree parm;
29791
29792 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29793 token = cp_lexer_peek_token (parser->lexer);
29794
29795 if (token->type == CPP_ELLIPSIS)
29796 {
29797 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29798 *ellipsisp = true;
29799 token = cp_lexer_peek_token (parser->lexer);
29800 break;
29801 }
29802
29803 /* TODO: parse attributes for tail parameters. */
29804 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29805 parm = grokdeclarator (parmdecl->declarator,
29806 &parmdecl->decl_specifiers,
29807 PARM, /*initialized=*/0,
29808 /*attrlist=*/NULL);
29809
29810 chainon (params, build_tree_list (NULL_TREE, parm));
29811 token = cp_lexer_peek_token (parser->lexer);
29812 }
29813
29814 /* We allow tail attributes for the method. */
29815 if (token->keyword == RID_ATTRIBUTE)
29816 {
29817 if (*attributes == NULL_TREE)
29818 {
29819 *attributes = cp_parser_attributes_opt (parser);
29820 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29821 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29822 return params;
29823 }
29824 else
29825 /* We have an error, but parse the attributes, so that we can
29826 carry on. */
29827 *attributes = cp_parser_attributes_opt (parser);
29828
29829 cp_parser_error (parser,
29830 "method attributes must be specified at the end");
29831 return error_mark_node;
29832 }
29833
29834 return params;
29835 }
29836
29837 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29838
29839 static void
29840 cp_parser_objc_interstitial_code (cp_parser* parser)
29841 {
29842 cp_token *token = cp_lexer_peek_token (parser->lexer);
29843
29844 /* If the next token is `extern' and the following token is a string
29845 literal, then we have a linkage specification. */
29846 if (token->keyword == RID_EXTERN
29847 && cp_parser_is_pure_string_literal
29848 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29849 cp_parser_linkage_specification (parser);
29850 /* Handle #pragma, if any. */
29851 else if (token->type == CPP_PRAGMA)
29852 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29853 /* Allow stray semicolons. */
29854 else if (token->type == CPP_SEMICOLON)
29855 cp_lexer_consume_token (parser->lexer);
29856 /* Mark methods as optional or required, when building protocols. */
29857 else if (token->keyword == RID_AT_OPTIONAL)
29858 {
29859 cp_lexer_consume_token (parser->lexer);
29860 objc_set_method_opt (true);
29861 }
29862 else if (token->keyword == RID_AT_REQUIRED)
29863 {
29864 cp_lexer_consume_token (parser->lexer);
29865 objc_set_method_opt (false);
29866 }
29867 else if (token->keyword == RID_NAMESPACE)
29868 cp_parser_namespace_definition (parser);
29869 /* Other stray characters must generate errors. */
29870 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29871 {
29872 cp_lexer_consume_token (parser->lexer);
29873 error ("stray %qs between Objective-C++ methods",
29874 token->type == CPP_OPEN_BRACE ? "{" : "}");
29875 }
29876 /* Finally, try to parse a block-declaration, or a function-definition. */
29877 else
29878 cp_parser_block_declaration (parser, /*statement_p=*/false);
29879 }
29880
29881 /* Parse a method signature. */
29882
29883 static tree
29884 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29885 {
29886 tree rettype, kwdparms, optparms;
29887 bool ellipsis = false;
29888 bool is_class_method;
29889
29890 is_class_method = cp_parser_objc_method_type (parser);
29891 rettype = cp_parser_objc_typename (parser);
29892 *attributes = NULL_TREE;
29893 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29894 if (kwdparms == error_mark_node)
29895 return error_mark_node;
29896 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29897 if (optparms == error_mark_node)
29898 return error_mark_node;
29899
29900 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29901 }
29902
29903 static bool
29904 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29905 {
29906 tree tattr;
29907 cp_lexer_save_tokens (parser->lexer);
29908 tattr = cp_parser_attributes_opt (parser);
29909 gcc_assert (tattr) ;
29910
29911 /* If the attributes are followed by a method introducer, this is not allowed.
29912 Dump the attributes and flag the situation. */
29913 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29914 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29915 return true;
29916
29917 /* Otherwise, the attributes introduce some interstitial code, possibly so
29918 rewind to allow that check. */
29919 cp_lexer_rollback_tokens (parser->lexer);
29920 return false;
29921 }
29922
29923 /* Parse an Objective-C method prototype list. */
29924
29925 static void
29926 cp_parser_objc_method_prototype_list (cp_parser* parser)
29927 {
29928 cp_token *token = cp_lexer_peek_token (parser->lexer);
29929
29930 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29931 {
29932 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29933 {
29934 tree attributes, sig;
29935 bool is_class_method;
29936 if (token->type == CPP_PLUS)
29937 is_class_method = true;
29938 else
29939 is_class_method = false;
29940 sig = cp_parser_objc_method_signature (parser, &attributes);
29941 if (sig == error_mark_node)
29942 {
29943 cp_parser_skip_to_end_of_block_or_statement (parser);
29944 token = cp_lexer_peek_token (parser->lexer);
29945 continue;
29946 }
29947 objc_add_method_declaration (is_class_method, sig, attributes);
29948 cp_parser_consume_semicolon_at_end_of_statement (parser);
29949 }
29950 else if (token->keyword == RID_AT_PROPERTY)
29951 cp_parser_objc_at_property_declaration (parser);
29952 else if (token->keyword == RID_ATTRIBUTE
29953 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29954 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29955 OPT_Wattributes,
29956 "prefix attributes are ignored for methods");
29957 else
29958 /* Allow for interspersed non-ObjC++ code. */
29959 cp_parser_objc_interstitial_code (parser);
29960
29961 token = cp_lexer_peek_token (parser->lexer);
29962 }
29963
29964 if (token->type != CPP_EOF)
29965 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29966 else
29967 cp_parser_error (parser, "expected %<@end%>");
29968
29969 objc_finish_interface ();
29970 }
29971
29972 /* Parse an Objective-C method definition list. */
29973
29974 static void
29975 cp_parser_objc_method_definition_list (cp_parser* parser)
29976 {
29977 cp_token *token = cp_lexer_peek_token (parser->lexer);
29978
29979 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29980 {
29981 tree meth;
29982
29983 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29984 {
29985 cp_token *ptk;
29986 tree sig, attribute;
29987 bool is_class_method;
29988 if (token->type == CPP_PLUS)
29989 is_class_method = true;
29990 else
29991 is_class_method = false;
29992 push_deferring_access_checks (dk_deferred);
29993 sig = cp_parser_objc_method_signature (parser, &attribute);
29994 if (sig == error_mark_node)
29995 {
29996 cp_parser_skip_to_end_of_block_or_statement (parser);
29997 token = cp_lexer_peek_token (parser->lexer);
29998 continue;
29999 }
30000 objc_start_method_definition (is_class_method, sig, attribute,
30001 NULL_TREE);
30002
30003 /* For historical reasons, we accept an optional semicolon. */
30004 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30005 cp_lexer_consume_token (parser->lexer);
30006
30007 ptk = cp_lexer_peek_token (parser->lexer);
30008 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30009 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30010 {
30011 perform_deferred_access_checks (tf_warning_or_error);
30012 stop_deferring_access_checks ();
30013 meth = cp_parser_function_definition_after_declarator (parser,
30014 false);
30015 pop_deferring_access_checks ();
30016 objc_finish_method_definition (meth);
30017 }
30018 }
30019 /* The following case will be removed once @synthesize is
30020 completely implemented. */
30021 else if (token->keyword == RID_AT_PROPERTY)
30022 cp_parser_objc_at_property_declaration (parser);
30023 else if (token->keyword == RID_AT_SYNTHESIZE)
30024 cp_parser_objc_at_synthesize_declaration (parser);
30025 else if (token->keyword == RID_AT_DYNAMIC)
30026 cp_parser_objc_at_dynamic_declaration (parser);
30027 else if (token->keyword == RID_ATTRIBUTE
30028 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30029 warning_at (token->location, OPT_Wattributes,
30030 "prefix attributes are ignored for methods");
30031 else
30032 /* Allow for interspersed non-ObjC++ code. */
30033 cp_parser_objc_interstitial_code (parser);
30034
30035 token = cp_lexer_peek_token (parser->lexer);
30036 }
30037
30038 if (token->type != CPP_EOF)
30039 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30040 else
30041 cp_parser_error (parser, "expected %<@end%>");
30042
30043 objc_finish_implementation ();
30044 }
30045
30046 /* Parse Objective-C ivars. */
30047
30048 static void
30049 cp_parser_objc_class_ivars (cp_parser* parser)
30050 {
30051 cp_token *token = cp_lexer_peek_token (parser->lexer);
30052
30053 if (token->type != CPP_OPEN_BRACE)
30054 return; /* No ivars specified. */
30055
30056 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30057 token = cp_lexer_peek_token (parser->lexer);
30058
30059 while (token->type != CPP_CLOSE_BRACE
30060 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30061 {
30062 cp_decl_specifier_seq declspecs;
30063 int decl_class_or_enum_p;
30064 tree prefix_attributes;
30065
30066 cp_parser_objc_visibility_spec (parser);
30067
30068 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30069 break;
30070
30071 cp_parser_decl_specifier_seq (parser,
30072 CP_PARSER_FLAGS_OPTIONAL,
30073 &declspecs,
30074 &decl_class_or_enum_p);
30075
30076 /* auto, register, static, extern, mutable. */
30077 if (declspecs.storage_class != sc_none)
30078 {
30079 cp_parser_error (parser, "invalid type for instance variable");
30080 declspecs.storage_class = sc_none;
30081 }
30082
30083 /* thread_local. */
30084 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30085 {
30086 cp_parser_error (parser, "invalid type for instance variable");
30087 declspecs.locations[ds_thread] = 0;
30088 }
30089
30090 /* typedef. */
30091 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30092 {
30093 cp_parser_error (parser, "invalid type for instance variable");
30094 declspecs.locations[ds_typedef] = 0;
30095 }
30096
30097 prefix_attributes = declspecs.attributes;
30098 declspecs.attributes = NULL_TREE;
30099
30100 /* Keep going until we hit the `;' at the end of the
30101 declaration. */
30102 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30103 {
30104 tree width = NULL_TREE, attributes, first_attribute, decl;
30105 cp_declarator *declarator = NULL;
30106 int ctor_dtor_or_conv_p;
30107
30108 /* Check for a (possibly unnamed) bitfield declaration. */
30109 token = cp_lexer_peek_token (parser->lexer);
30110 if (token->type == CPP_COLON)
30111 goto eat_colon;
30112
30113 if (token->type == CPP_NAME
30114 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30115 == CPP_COLON))
30116 {
30117 /* Get the name of the bitfield. */
30118 declarator = make_id_declarator (NULL_TREE,
30119 cp_parser_identifier (parser),
30120 sfk_none);
30121
30122 eat_colon:
30123 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30124 /* Get the width of the bitfield. */
30125 width
30126 = cp_parser_constant_expression (parser);
30127 }
30128 else
30129 {
30130 /* Parse the declarator. */
30131 declarator
30132 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30133 &ctor_dtor_or_conv_p,
30134 /*parenthesized_p=*/NULL,
30135 /*member_p=*/false,
30136 /*friend_p=*/false);
30137 }
30138
30139 /* Look for attributes that apply to the ivar. */
30140 attributes = cp_parser_attributes_opt (parser);
30141 /* Remember which attributes are prefix attributes and
30142 which are not. */
30143 first_attribute = attributes;
30144 /* Combine the attributes. */
30145 attributes = chainon (prefix_attributes, attributes);
30146
30147 if (width)
30148 /* Create the bitfield declaration. */
30149 decl = grokbitfield (declarator, &declspecs,
30150 width, NULL_TREE, attributes);
30151 else
30152 decl = grokfield (declarator, &declspecs,
30153 NULL_TREE, /*init_const_expr_p=*/false,
30154 NULL_TREE, attributes);
30155
30156 /* Add the instance variable. */
30157 if (decl != error_mark_node && decl != NULL_TREE)
30158 objc_add_instance_variable (decl);
30159
30160 /* Reset PREFIX_ATTRIBUTES. */
30161 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30162 attributes = TREE_CHAIN (attributes);
30163 if (attributes)
30164 TREE_CHAIN (attributes) = NULL_TREE;
30165
30166 token = cp_lexer_peek_token (parser->lexer);
30167
30168 if (token->type == CPP_COMMA)
30169 {
30170 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30171 continue;
30172 }
30173 break;
30174 }
30175
30176 cp_parser_consume_semicolon_at_end_of_statement (parser);
30177 token = cp_lexer_peek_token (parser->lexer);
30178 }
30179
30180 if (token->keyword == RID_AT_END)
30181 cp_parser_error (parser, "expected %<}%>");
30182
30183 /* Do not consume the RID_AT_END, so it will be read again as terminating
30184 the @interface of @implementation. */
30185 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30186 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30187
30188 /* For historical reasons, we accept an optional semicolon. */
30189 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30190 cp_lexer_consume_token (parser->lexer);
30191 }
30192
30193 /* Parse an Objective-C protocol declaration. */
30194
30195 static void
30196 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30197 {
30198 tree proto, protorefs;
30199 cp_token *tok;
30200
30201 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30203 {
30204 tok = cp_lexer_peek_token (parser->lexer);
30205 error_at (tok->location, "identifier expected after %<@protocol%>");
30206 cp_parser_consume_semicolon_at_end_of_statement (parser);
30207 return;
30208 }
30209
30210 /* See if we have a forward declaration or a definition. */
30211 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30212
30213 /* Try a forward declaration first. */
30214 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30215 {
30216 while (true)
30217 {
30218 tree id;
30219
30220 id = cp_parser_identifier (parser);
30221 if (id == error_mark_node)
30222 break;
30223
30224 objc_declare_protocol (id, attributes);
30225
30226 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30227 cp_lexer_consume_token (parser->lexer);
30228 else
30229 break;
30230 }
30231 cp_parser_consume_semicolon_at_end_of_statement (parser);
30232 }
30233
30234 /* Ok, we got a full-fledged definition (or at least should). */
30235 else
30236 {
30237 proto = cp_parser_identifier (parser);
30238 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30239 objc_start_protocol (proto, protorefs, attributes);
30240 cp_parser_objc_method_prototype_list (parser);
30241 }
30242 }
30243
30244 /* Parse an Objective-C superclass or category. */
30245
30246 static void
30247 cp_parser_objc_superclass_or_category (cp_parser *parser,
30248 bool iface_p,
30249 tree *super,
30250 tree *categ, bool *is_class_extension)
30251 {
30252 cp_token *next = cp_lexer_peek_token (parser->lexer);
30253
30254 *super = *categ = NULL_TREE;
30255 *is_class_extension = false;
30256 if (next->type == CPP_COLON)
30257 {
30258 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30259 *super = cp_parser_identifier (parser);
30260 }
30261 else if (next->type == CPP_OPEN_PAREN)
30262 {
30263 matching_parens parens;
30264 parens.consume_open (parser); /* Eat '('. */
30265
30266 /* If there is no category name, and this is an @interface, we
30267 have a class extension. */
30268 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30269 {
30270 *categ = NULL_TREE;
30271 *is_class_extension = true;
30272 }
30273 else
30274 *categ = cp_parser_identifier (parser);
30275
30276 parens.require_close (parser);
30277 }
30278 }
30279
30280 /* Parse an Objective-C class interface. */
30281
30282 static void
30283 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30284 {
30285 tree name, super, categ, protos;
30286 bool is_class_extension;
30287
30288 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30289 name = cp_parser_identifier (parser);
30290 if (name == error_mark_node)
30291 {
30292 /* It's hard to recover because even if valid @interface stuff
30293 is to follow, we can't compile it (or validate it) if we
30294 don't even know which class it refers to. Let's assume this
30295 was a stray '@interface' token in the stream and skip it.
30296 */
30297 return;
30298 }
30299 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30300 &is_class_extension);
30301 protos = cp_parser_objc_protocol_refs_opt (parser);
30302
30303 /* We have either a class or a category on our hands. */
30304 if (categ || is_class_extension)
30305 objc_start_category_interface (name, categ, protos, attributes);
30306 else
30307 {
30308 objc_start_class_interface (name, super, protos, attributes);
30309 /* Handle instance variable declarations, if any. */
30310 cp_parser_objc_class_ivars (parser);
30311 objc_continue_interface ();
30312 }
30313
30314 cp_parser_objc_method_prototype_list (parser);
30315 }
30316
30317 /* Parse an Objective-C class implementation. */
30318
30319 static void
30320 cp_parser_objc_class_implementation (cp_parser* parser)
30321 {
30322 tree name, super, categ;
30323 bool is_class_extension;
30324
30325 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30326 name = cp_parser_identifier (parser);
30327 if (name == error_mark_node)
30328 {
30329 /* It's hard to recover because even if valid @implementation
30330 stuff is to follow, we can't compile it (or validate it) if
30331 we don't even know which class it refers to. Let's assume
30332 this was a stray '@implementation' token in the stream and
30333 skip it.
30334 */
30335 return;
30336 }
30337 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30338 &is_class_extension);
30339
30340 /* We have either a class or a category on our hands. */
30341 if (categ)
30342 objc_start_category_implementation (name, categ);
30343 else
30344 {
30345 objc_start_class_implementation (name, super);
30346 /* Handle instance variable declarations, if any. */
30347 cp_parser_objc_class_ivars (parser);
30348 objc_continue_implementation ();
30349 }
30350
30351 cp_parser_objc_method_definition_list (parser);
30352 }
30353
30354 /* Consume the @end token and finish off the implementation. */
30355
30356 static void
30357 cp_parser_objc_end_implementation (cp_parser* parser)
30358 {
30359 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30360 objc_finish_implementation ();
30361 }
30362
30363 /* Parse an Objective-C declaration. */
30364
30365 static void
30366 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30367 {
30368 /* Try to figure out what kind of declaration is present. */
30369 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30370
30371 if (attributes)
30372 switch (kwd->keyword)
30373 {
30374 case RID_AT_ALIAS:
30375 case RID_AT_CLASS:
30376 case RID_AT_END:
30377 error_at (kwd->location, "attributes may not be specified before"
30378 " the %<@%D%> Objective-C++ keyword",
30379 kwd->u.value);
30380 attributes = NULL;
30381 break;
30382 case RID_AT_IMPLEMENTATION:
30383 warning_at (kwd->location, OPT_Wattributes,
30384 "prefix attributes are ignored before %<@%D%>",
30385 kwd->u.value);
30386 attributes = NULL;
30387 default:
30388 break;
30389 }
30390
30391 switch (kwd->keyword)
30392 {
30393 case RID_AT_ALIAS:
30394 cp_parser_objc_alias_declaration (parser);
30395 break;
30396 case RID_AT_CLASS:
30397 cp_parser_objc_class_declaration (parser);
30398 break;
30399 case RID_AT_PROTOCOL:
30400 cp_parser_objc_protocol_declaration (parser, attributes);
30401 break;
30402 case RID_AT_INTERFACE:
30403 cp_parser_objc_class_interface (parser, attributes);
30404 break;
30405 case RID_AT_IMPLEMENTATION:
30406 cp_parser_objc_class_implementation (parser);
30407 break;
30408 case RID_AT_END:
30409 cp_parser_objc_end_implementation (parser);
30410 break;
30411 default:
30412 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30413 kwd->u.value);
30414 cp_parser_skip_to_end_of_block_or_statement (parser);
30415 }
30416 }
30417
30418 /* Parse an Objective-C try-catch-finally statement.
30419
30420 objc-try-catch-finally-stmt:
30421 @try compound-statement objc-catch-clause-seq [opt]
30422 objc-finally-clause [opt]
30423
30424 objc-catch-clause-seq:
30425 objc-catch-clause objc-catch-clause-seq [opt]
30426
30427 objc-catch-clause:
30428 @catch ( objc-exception-declaration ) compound-statement
30429
30430 objc-finally-clause:
30431 @finally compound-statement
30432
30433 objc-exception-declaration:
30434 parameter-declaration
30435 '...'
30436
30437 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30438
30439 Returns NULL_TREE.
30440
30441 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30442 for C. Keep them in sync. */
30443
30444 static tree
30445 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30446 {
30447 location_t location;
30448 tree stmt;
30449
30450 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30451 location = cp_lexer_peek_token (parser->lexer)->location;
30452 objc_maybe_warn_exceptions (location);
30453 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30454 node, lest it get absorbed into the surrounding block. */
30455 stmt = push_stmt_list ();
30456 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30457 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30458
30459 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30460 {
30461 cp_parameter_declarator *parm;
30462 tree parameter_declaration = error_mark_node;
30463 bool seen_open_paren = false;
30464 matching_parens parens;
30465
30466 cp_lexer_consume_token (parser->lexer);
30467 if (parens.require_open (parser))
30468 seen_open_paren = true;
30469 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30470 {
30471 /* We have "@catch (...)" (where the '...' are literally
30472 what is in the code). Skip the '...'.
30473 parameter_declaration is set to NULL_TREE, and
30474 objc_being_catch_clauses() knows that that means
30475 '...'. */
30476 cp_lexer_consume_token (parser->lexer);
30477 parameter_declaration = NULL_TREE;
30478 }
30479 else
30480 {
30481 /* We have "@catch (NSException *exception)" or something
30482 like that. Parse the parameter declaration. */
30483 parm = cp_parser_parameter_declaration (parser, false, NULL);
30484 if (parm == NULL)
30485 parameter_declaration = error_mark_node;
30486 else
30487 parameter_declaration = grokdeclarator (parm->declarator,
30488 &parm->decl_specifiers,
30489 PARM, /*initialized=*/0,
30490 /*attrlist=*/NULL);
30491 }
30492 if (seen_open_paren)
30493 parens.require_close (parser);
30494 else
30495 {
30496 /* If there was no open parenthesis, we are recovering from
30497 an error, and we are trying to figure out what mistake
30498 the user has made. */
30499
30500 /* If there is an immediate closing parenthesis, the user
30501 probably forgot the opening one (ie, they typed "@catch
30502 NSException *e)". Parse the closing parenthesis and keep
30503 going. */
30504 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30505 cp_lexer_consume_token (parser->lexer);
30506
30507 /* If these is no immediate closing parenthesis, the user
30508 probably doesn't know that parenthesis are required at
30509 all (ie, they typed "@catch NSException *e"). So, just
30510 forget about the closing parenthesis and keep going. */
30511 }
30512 objc_begin_catch_clause (parameter_declaration);
30513 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30514 objc_finish_catch_clause ();
30515 }
30516 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30517 {
30518 cp_lexer_consume_token (parser->lexer);
30519 location = cp_lexer_peek_token (parser->lexer)->location;
30520 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30521 node, lest it get absorbed into the surrounding block. */
30522 stmt = push_stmt_list ();
30523 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30524 objc_build_finally_clause (location, pop_stmt_list (stmt));
30525 }
30526
30527 return objc_finish_try_stmt ();
30528 }
30529
30530 /* Parse an Objective-C synchronized statement.
30531
30532 objc-synchronized-stmt:
30533 @synchronized ( expression ) compound-statement
30534
30535 Returns NULL_TREE. */
30536
30537 static tree
30538 cp_parser_objc_synchronized_statement (cp_parser *parser)
30539 {
30540 location_t location;
30541 tree lock, stmt;
30542
30543 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30544
30545 location = cp_lexer_peek_token (parser->lexer)->location;
30546 objc_maybe_warn_exceptions (location);
30547 matching_parens parens;
30548 parens.require_open (parser);
30549 lock = cp_parser_expression (parser);
30550 parens.require_close (parser);
30551
30552 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30553 node, lest it get absorbed into the surrounding block. */
30554 stmt = push_stmt_list ();
30555 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30556
30557 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30558 }
30559
30560 /* Parse an Objective-C throw statement.
30561
30562 objc-throw-stmt:
30563 @throw assignment-expression [opt] ;
30564
30565 Returns a constructed '@throw' statement. */
30566
30567 static tree
30568 cp_parser_objc_throw_statement (cp_parser *parser)
30569 {
30570 tree expr = NULL_TREE;
30571 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30572
30573 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30574
30575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30576 expr = cp_parser_expression (parser);
30577
30578 cp_parser_consume_semicolon_at_end_of_statement (parser);
30579
30580 return objc_build_throw_stmt (loc, expr);
30581 }
30582
30583 /* Parse an Objective-C statement. */
30584
30585 static tree
30586 cp_parser_objc_statement (cp_parser * parser)
30587 {
30588 /* Try to figure out what kind of declaration is present. */
30589 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30590
30591 switch (kwd->keyword)
30592 {
30593 case RID_AT_TRY:
30594 return cp_parser_objc_try_catch_finally_statement (parser);
30595 case RID_AT_SYNCHRONIZED:
30596 return cp_parser_objc_synchronized_statement (parser);
30597 case RID_AT_THROW:
30598 return cp_parser_objc_throw_statement (parser);
30599 default:
30600 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30601 kwd->u.value);
30602 cp_parser_skip_to_end_of_block_or_statement (parser);
30603 }
30604
30605 return error_mark_node;
30606 }
30607
30608 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30609 look ahead to see if an objc keyword follows the attributes. This
30610 is to detect the use of prefix attributes on ObjC @interface and
30611 @protocol. */
30612
30613 static bool
30614 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30615 {
30616 cp_lexer_save_tokens (parser->lexer);
30617 *attrib = cp_parser_attributes_opt (parser);
30618 gcc_assert (*attrib);
30619 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30620 {
30621 cp_lexer_commit_tokens (parser->lexer);
30622 return true;
30623 }
30624 cp_lexer_rollback_tokens (parser->lexer);
30625 return false;
30626 }
30627
30628 /* This routine is a minimal replacement for
30629 c_parser_struct_declaration () used when parsing the list of
30630 types/names or ObjC++ properties. For example, when parsing the
30631 code
30632
30633 @property (readonly) int a, b, c;
30634
30635 this function is responsible for parsing "int a, int b, int c" and
30636 returning the declarations as CHAIN of DECLs.
30637
30638 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30639 similar parsing. */
30640 static tree
30641 cp_parser_objc_struct_declaration (cp_parser *parser)
30642 {
30643 tree decls = NULL_TREE;
30644 cp_decl_specifier_seq declspecs;
30645 int decl_class_or_enum_p;
30646 tree prefix_attributes;
30647
30648 cp_parser_decl_specifier_seq (parser,
30649 CP_PARSER_FLAGS_NONE,
30650 &declspecs,
30651 &decl_class_or_enum_p);
30652
30653 if (declspecs.type == error_mark_node)
30654 return error_mark_node;
30655
30656 /* auto, register, static, extern, mutable. */
30657 if (declspecs.storage_class != sc_none)
30658 {
30659 cp_parser_error (parser, "invalid type for property");
30660 declspecs.storage_class = sc_none;
30661 }
30662
30663 /* thread_local. */
30664 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30665 {
30666 cp_parser_error (parser, "invalid type for property");
30667 declspecs.locations[ds_thread] = 0;
30668 }
30669
30670 /* typedef. */
30671 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30672 {
30673 cp_parser_error (parser, "invalid type for property");
30674 declspecs.locations[ds_typedef] = 0;
30675 }
30676
30677 prefix_attributes = declspecs.attributes;
30678 declspecs.attributes = NULL_TREE;
30679
30680 /* Keep going until we hit the `;' at the end of the declaration. */
30681 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30682 {
30683 tree attributes, first_attribute, decl;
30684 cp_declarator *declarator;
30685 cp_token *token;
30686
30687 /* Parse the declarator. */
30688 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30689 NULL, NULL, false, false);
30690
30691 /* Look for attributes that apply to the ivar. */
30692 attributes = cp_parser_attributes_opt (parser);
30693 /* Remember which attributes are prefix attributes and
30694 which are not. */
30695 first_attribute = attributes;
30696 /* Combine the attributes. */
30697 attributes = chainon (prefix_attributes, attributes);
30698
30699 decl = grokfield (declarator, &declspecs,
30700 NULL_TREE, /*init_const_expr_p=*/false,
30701 NULL_TREE, attributes);
30702
30703 if (decl == error_mark_node || decl == NULL_TREE)
30704 return error_mark_node;
30705
30706 /* Reset PREFIX_ATTRIBUTES. */
30707 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30708 attributes = TREE_CHAIN (attributes);
30709 if (attributes)
30710 TREE_CHAIN (attributes) = NULL_TREE;
30711
30712 DECL_CHAIN (decl) = decls;
30713 decls = decl;
30714
30715 token = cp_lexer_peek_token (parser->lexer);
30716 if (token->type == CPP_COMMA)
30717 {
30718 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30719 continue;
30720 }
30721 else
30722 break;
30723 }
30724 return decls;
30725 }
30726
30727 /* Parse an Objective-C @property declaration. The syntax is:
30728
30729 objc-property-declaration:
30730 '@property' objc-property-attributes[opt] struct-declaration ;
30731
30732 objc-property-attributes:
30733 '(' objc-property-attribute-list ')'
30734
30735 objc-property-attribute-list:
30736 objc-property-attribute
30737 objc-property-attribute-list, objc-property-attribute
30738
30739 objc-property-attribute
30740 'getter' = identifier
30741 'setter' = identifier
30742 'readonly'
30743 'readwrite'
30744 'assign'
30745 'retain'
30746 'copy'
30747 'nonatomic'
30748
30749 For example:
30750 @property NSString *name;
30751 @property (readonly) id object;
30752 @property (retain, nonatomic, getter=getTheName) id name;
30753 @property int a, b, c;
30754
30755 PS: This function is identical to
30756 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30757 static void
30758 cp_parser_objc_at_property_declaration (cp_parser *parser)
30759 {
30760 /* The following variables hold the attributes of the properties as
30761 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30762 seen. When we see an attribute, we set them to 'true' (if they
30763 are boolean properties) or to the identifier (if they have an
30764 argument, ie, for getter and setter). Note that here we only
30765 parse the list of attributes, check the syntax and accumulate the
30766 attributes that we find. objc_add_property_declaration() will
30767 then process the information. */
30768 bool property_assign = false;
30769 bool property_copy = false;
30770 tree property_getter_ident = NULL_TREE;
30771 bool property_nonatomic = false;
30772 bool property_readonly = false;
30773 bool property_readwrite = false;
30774 bool property_retain = false;
30775 tree property_setter_ident = NULL_TREE;
30776
30777 /* 'properties' is the list of properties that we read. Usually a
30778 single one, but maybe more (eg, in "@property int a, b, c;" there
30779 are three). */
30780 tree properties;
30781 location_t loc;
30782
30783 loc = cp_lexer_peek_token (parser->lexer)->location;
30784
30785 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30786
30787 /* Parse the optional attribute list... */
30788 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30789 {
30790 /* Eat the '('. */
30791 matching_parens parens;
30792 parens.consume_open (parser);
30793
30794 while (true)
30795 {
30796 bool syntax_error = false;
30797 cp_token *token = cp_lexer_peek_token (parser->lexer);
30798 enum rid keyword;
30799
30800 if (token->type != CPP_NAME)
30801 {
30802 cp_parser_error (parser, "expected identifier");
30803 break;
30804 }
30805 keyword = C_RID_CODE (token->u.value);
30806 cp_lexer_consume_token (parser->lexer);
30807 switch (keyword)
30808 {
30809 case RID_ASSIGN: property_assign = true; break;
30810 case RID_COPY: property_copy = true; break;
30811 case RID_NONATOMIC: property_nonatomic = true; break;
30812 case RID_READONLY: property_readonly = true; break;
30813 case RID_READWRITE: property_readwrite = true; break;
30814 case RID_RETAIN: property_retain = true; break;
30815
30816 case RID_GETTER:
30817 case RID_SETTER:
30818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30819 {
30820 if (keyword == RID_GETTER)
30821 cp_parser_error (parser,
30822 "missing %<=%> (after %<getter%> attribute)");
30823 else
30824 cp_parser_error (parser,
30825 "missing %<=%> (after %<setter%> attribute)");
30826 syntax_error = true;
30827 break;
30828 }
30829 cp_lexer_consume_token (parser->lexer); /* eat the = */
30830 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30831 {
30832 cp_parser_error (parser, "expected identifier");
30833 syntax_error = true;
30834 break;
30835 }
30836 if (keyword == RID_SETTER)
30837 {
30838 if (property_setter_ident != NULL_TREE)
30839 {
30840 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30841 cp_lexer_consume_token (parser->lexer);
30842 }
30843 else
30844 property_setter_ident = cp_parser_objc_selector (parser);
30845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30846 cp_parser_error (parser, "setter name must terminate with %<:%>");
30847 else
30848 cp_lexer_consume_token (parser->lexer);
30849 }
30850 else
30851 {
30852 if (property_getter_ident != NULL_TREE)
30853 {
30854 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30855 cp_lexer_consume_token (parser->lexer);
30856 }
30857 else
30858 property_getter_ident = cp_parser_objc_selector (parser);
30859 }
30860 break;
30861 default:
30862 cp_parser_error (parser, "unknown property attribute");
30863 syntax_error = true;
30864 break;
30865 }
30866
30867 if (syntax_error)
30868 break;
30869
30870 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30871 cp_lexer_consume_token (parser->lexer);
30872 else
30873 break;
30874 }
30875
30876 /* FIXME: "@property (setter, assign);" will generate a spurious
30877 "error: expected ‘)’ before ‘,’ token". This is because
30878 cp_parser_require, unlike the C counterpart, will produce an
30879 error even if we are in error recovery. */
30880 if (!parens.require_close (parser))
30881 {
30882 cp_parser_skip_to_closing_parenthesis (parser,
30883 /*recovering=*/true,
30884 /*or_comma=*/false,
30885 /*consume_paren=*/true);
30886 }
30887 }
30888
30889 /* ... and the property declaration(s). */
30890 properties = cp_parser_objc_struct_declaration (parser);
30891
30892 if (properties == error_mark_node)
30893 {
30894 cp_parser_skip_to_end_of_statement (parser);
30895 /* If the next token is now a `;', consume it. */
30896 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30897 cp_lexer_consume_token (parser->lexer);
30898 return;
30899 }
30900
30901 if (properties == NULL_TREE)
30902 cp_parser_error (parser, "expected identifier");
30903 else
30904 {
30905 /* Comma-separated properties are chained together in
30906 reverse order; add them one by one. */
30907 properties = nreverse (properties);
30908
30909 for (; properties; properties = TREE_CHAIN (properties))
30910 objc_add_property_declaration (loc, copy_node (properties),
30911 property_readonly, property_readwrite,
30912 property_assign, property_retain,
30913 property_copy, property_nonatomic,
30914 property_getter_ident, property_setter_ident);
30915 }
30916
30917 cp_parser_consume_semicolon_at_end_of_statement (parser);
30918 }
30919
30920 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30921
30922 objc-synthesize-declaration:
30923 @synthesize objc-synthesize-identifier-list ;
30924
30925 objc-synthesize-identifier-list:
30926 objc-synthesize-identifier
30927 objc-synthesize-identifier-list, objc-synthesize-identifier
30928
30929 objc-synthesize-identifier
30930 identifier
30931 identifier = identifier
30932
30933 For example:
30934 @synthesize MyProperty;
30935 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30936
30937 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30938 for C. Keep them in sync.
30939 */
30940 static void
30941 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30942 {
30943 tree list = NULL_TREE;
30944 location_t loc;
30945 loc = cp_lexer_peek_token (parser->lexer)->location;
30946
30947 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30948 while (true)
30949 {
30950 tree property, ivar;
30951 property = cp_parser_identifier (parser);
30952 if (property == error_mark_node)
30953 {
30954 cp_parser_consume_semicolon_at_end_of_statement (parser);
30955 return;
30956 }
30957 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30958 {
30959 cp_lexer_consume_token (parser->lexer);
30960 ivar = cp_parser_identifier (parser);
30961 if (ivar == error_mark_node)
30962 {
30963 cp_parser_consume_semicolon_at_end_of_statement (parser);
30964 return;
30965 }
30966 }
30967 else
30968 ivar = NULL_TREE;
30969 list = chainon (list, build_tree_list (ivar, property));
30970 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30971 cp_lexer_consume_token (parser->lexer);
30972 else
30973 break;
30974 }
30975 cp_parser_consume_semicolon_at_end_of_statement (parser);
30976 objc_add_synthesize_declaration (loc, list);
30977 }
30978
30979 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30980
30981 objc-dynamic-declaration:
30982 @dynamic identifier-list ;
30983
30984 For example:
30985 @dynamic MyProperty;
30986 @dynamic MyProperty, AnotherProperty;
30987
30988 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30989 for C. Keep them in sync.
30990 */
30991 static void
30992 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30993 {
30994 tree list = NULL_TREE;
30995 location_t loc;
30996 loc = cp_lexer_peek_token (parser->lexer)->location;
30997
30998 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30999 while (true)
31000 {
31001 tree property;
31002 property = cp_parser_identifier (parser);
31003 if (property == error_mark_node)
31004 {
31005 cp_parser_consume_semicolon_at_end_of_statement (parser);
31006 return;
31007 }
31008 list = chainon (list, build_tree_list (NULL, property));
31009 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31010 cp_lexer_consume_token (parser->lexer);
31011 else
31012 break;
31013 }
31014 cp_parser_consume_semicolon_at_end_of_statement (parser);
31015 objc_add_dynamic_declaration (loc, list);
31016 }
31017
31018 \f
31019 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31020
31021 /* Returns name of the next clause.
31022 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31023 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31024 returned and the token is consumed. */
31025
31026 static pragma_omp_clause
31027 cp_parser_omp_clause_name (cp_parser *parser)
31028 {
31029 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31030
31031 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31032 result = PRAGMA_OACC_CLAUSE_AUTO;
31033 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31034 result = PRAGMA_OMP_CLAUSE_IF;
31035 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31036 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31037 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31038 result = PRAGMA_OACC_CLAUSE_DELETE;
31039 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31040 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31041 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31042 result = PRAGMA_OMP_CLAUSE_FOR;
31043 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31044 {
31045 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31046 const char *p = IDENTIFIER_POINTER (id);
31047
31048 switch (p[0])
31049 {
31050 case 'a':
31051 if (!strcmp ("aligned", p))
31052 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31053 else if (!strcmp ("async", p))
31054 result = PRAGMA_OACC_CLAUSE_ASYNC;
31055 break;
31056 case 'c':
31057 if (!strcmp ("collapse", p))
31058 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31059 else if (!strcmp ("copy", p))
31060 result = PRAGMA_OACC_CLAUSE_COPY;
31061 else if (!strcmp ("copyin", p))
31062 result = PRAGMA_OMP_CLAUSE_COPYIN;
31063 else if (!strcmp ("copyout", p))
31064 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31065 else if (!strcmp ("copyprivate", p))
31066 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31067 else if (!strcmp ("create", p))
31068 result = PRAGMA_OACC_CLAUSE_CREATE;
31069 break;
31070 case 'd':
31071 if (!strcmp ("defaultmap", p))
31072 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31073 else if (!strcmp ("depend", p))
31074 result = PRAGMA_OMP_CLAUSE_DEPEND;
31075 else if (!strcmp ("device", p))
31076 result = PRAGMA_OMP_CLAUSE_DEVICE;
31077 else if (!strcmp ("deviceptr", p))
31078 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31079 else if (!strcmp ("device_resident", p))
31080 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31081 else if (!strcmp ("dist_schedule", p))
31082 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31083 break;
31084 case 'f':
31085 if (!strcmp ("final", p))
31086 result = PRAGMA_OMP_CLAUSE_FINAL;
31087 else if (!strcmp ("firstprivate", p))
31088 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31089 else if (!strcmp ("from", p))
31090 result = PRAGMA_OMP_CLAUSE_FROM;
31091 break;
31092 case 'g':
31093 if (!strcmp ("gang", p))
31094 result = PRAGMA_OACC_CLAUSE_GANG;
31095 else if (!strcmp ("grainsize", p))
31096 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31097 break;
31098 case 'h':
31099 if (!strcmp ("hint", p))
31100 result = PRAGMA_OMP_CLAUSE_HINT;
31101 else if (!strcmp ("host", p))
31102 result = PRAGMA_OACC_CLAUSE_HOST;
31103 break;
31104 case 'i':
31105 if (!strcmp ("inbranch", p))
31106 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31107 else if (!strcmp ("independent", p))
31108 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31109 else if (!strcmp ("is_device_ptr", p))
31110 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31111 break;
31112 case 'l':
31113 if (!strcmp ("lastprivate", p))
31114 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31115 else if (!strcmp ("linear", p))
31116 result = PRAGMA_OMP_CLAUSE_LINEAR;
31117 else if (!strcmp ("link", p))
31118 result = PRAGMA_OMP_CLAUSE_LINK;
31119 break;
31120 case 'm':
31121 if (!strcmp ("map", p))
31122 result = PRAGMA_OMP_CLAUSE_MAP;
31123 else if (!strcmp ("mergeable", p))
31124 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31125 break;
31126 case 'n':
31127 if (!strcmp ("nogroup", p))
31128 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31129 else if (!strcmp ("notinbranch", p))
31130 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31131 else if (!strcmp ("nowait", p))
31132 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31133 else if (!strcmp ("num_gangs", p))
31134 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31135 else if (!strcmp ("num_tasks", p))
31136 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31137 else if (!strcmp ("num_teams", p))
31138 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31139 else if (!strcmp ("num_threads", p))
31140 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31141 else if (!strcmp ("num_workers", p))
31142 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31143 break;
31144 case 'o':
31145 if (!strcmp ("ordered", p))
31146 result = PRAGMA_OMP_CLAUSE_ORDERED;
31147 break;
31148 case 'p':
31149 if (!strcmp ("parallel", p))
31150 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31151 else if (!strcmp ("present", p))
31152 result = PRAGMA_OACC_CLAUSE_PRESENT;
31153 else if (!strcmp ("present_or_copy", p)
31154 || !strcmp ("pcopy", p))
31155 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31156 else if (!strcmp ("present_or_copyin", p)
31157 || !strcmp ("pcopyin", p))
31158 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31159 else if (!strcmp ("present_or_copyout", p)
31160 || !strcmp ("pcopyout", p))
31161 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31162 else if (!strcmp ("present_or_create", p)
31163 || !strcmp ("pcreate", p))
31164 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31165 else if (!strcmp ("priority", p))
31166 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31167 else if (!strcmp ("proc_bind", p))
31168 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31169 break;
31170 case 'r':
31171 if (!strcmp ("reduction", p))
31172 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31173 break;
31174 case 's':
31175 if (!strcmp ("safelen", p))
31176 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31177 else if (!strcmp ("schedule", p))
31178 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31179 else if (!strcmp ("sections", p))
31180 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31181 else if (!strcmp ("self", p))
31182 result = PRAGMA_OACC_CLAUSE_SELF;
31183 else if (!strcmp ("seq", p))
31184 result = PRAGMA_OACC_CLAUSE_SEQ;
31185 else if (!strcmp ("shared", p))
31186 result = PRAGMA_OMP_CLAUSE_SHARED;
31187 else if (!strcmp ("simd", p))
31188 result = PRAGMA_OMP_CLAUSE_SIMD;
31189 else if (!strcmp ("simdlen", p))
31190 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31191 break;
31192 case 't':
31193 if (!strcmp ("taskgroup", p))
31194 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31195 else if (!strcmp ("thread_limit", p))
31196 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31197 else if (!strcmp ("threads", p))
31198 result = PRAGMA_OMP_CLAUSE_THREADS;
31199 else if (!strcmp ("tile", p))
31200 result = PRAGMA_OACC_CLAUSE_TILE;
31201 else if (!strcmp ("to", p))
31202 result = PRAGMA_OMP_CLAUSE_TO;
31203 break;
31204 case 'u':
31205 if (!strcmp ("uniform", p))
31206 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31207 else if (!strcmp ("untied", p))
31208 result = PRAGMA_OMP_CLAUSE_UNTIED;
31209 else if (!strcmp ("use_device", p))
31210 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31211 else if (!strcmp ("use_device_ptr", p))
31212 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31213 break;
31214 case 'v':
31215 if (!strcmp ("vector", p))
31216 result = PRAGMA_OACC_CLAUSE_VECTOR;
31217 else if (!strcmp ("vector_length", p))
31218 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31219 break;
31220 case 'w':
31221 if (!strcmp ("wait", p))
31222 result = PRAGMA_OACC_CLAUSE_WAIT;
31223 else if (!strcmp ("worker", p))
31224 result = PRAGMA_OACC_CLAUSE_WORKER;
31225 break;
31226 }
31227 }
31228
31229 if (result != PRAGMA_OMP_CLAUSE_NONE)
31230 cp_lexer_consume_token (parser->lexer);
31231
31232 return result;
31233 }
31234
31235 /* Validate that a clause of the given type does not already exist. */
31236
31237 static void
31238 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31239 const char *name, location_t location)
31240 {
31241 tree c;
31242
31243 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31244 if (OMP_CLAUSE_CODE (c) == code)
31245 {
31246 error_at (location, "too many %qs clauses", name);
31247 break;
31248 }
31249 }
31250
31251 /* OpenMP 2.5:
31252 variable-list:
31253 identifier
31254 variable-list , identifier
31255
31256 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31257 colon). An opening parenthesis will have been consumed by the caller.
31258
31259 If KIND is nonzero, create the appropriate node and install the decl
31260 in OMP_CLAUSE_DECL and add the node to the head of the list.
31261
31262 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31263 return the list created.
31264
31265 COLON can be NULL if only closing parenthesis should end the list,
31266 or pointer to bool which will receive false if the list is terminated
31267 by closing parenthesis or true if the list is terminated by colon. */
31268
31269 static tree
31270 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31271 tree list, bool *colon)
31272 {
31273 cp_token *token;
31274 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31275 if (colon)
31276 {
31277 parser->colon_corrects_to_scope_p = false;
31278 *colon = false;
31279 }
31280 while (1)
31281 {
31282 tree name, decl;
31283
31284 token = cp_lexer_peek_token (parser->lexer);
31285 if (kind != 0
31286 && current_class_ptr
31287 && cp_parser_is_keyword (token, RID_THIS))
31288 {
31289 decl = finish_this_expr ();
31290 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31291 || CONVERT_EXPR_P (decl))
31292 decl = TREE_OPERAND (decl, 0);
31293 cp_lexer_consume_token (parser->lexer);
31294 }
31295 else
31296 {
31297 name = cp_parser_id_expression (parser, /*template_p=*/false,
31298 /*check_dependency_p=*/true,
31299 /*template_p=*/NULL,
31300 /*declarator_p=*/false,
31301 /*optional_p=*/false);
31302 if (name == error_mark_node)
31303 goto skip_comma;
31304
31305 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31306 if (decl == error_mark_node)
31307 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31308 token->location);
31309 }
31310 if (decl == error_mark_node)
31311 ;
31312 else if (kind != 0)
31313 {
31314 switch (kind)
31315 {
31316 case OMP_CLAUSE__CACHE_:
31317 /* The OpenACC cache directive explicitly only allows "array
31318 elements or subarrays". */
31319 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31320 {
31321 error_at (token->location, "expected %<[%>");
31322 decl = error_mark_node;
31323 break;
31324 }
31325 /* FALLTHROUGH. */
31326 case OMP_CLAUSE_MAP:
31327 case OMP_CLAUSE_FROM:
31328 case OMP_CLAUSE_TO:
31329 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31330 {
31331 location_t loc
31332 = cp_lexer_peek_token (parser->lexer)->location;
31333 cp_id_kind idk = CP_ID_KIND_NONE;
31334 cp_lexer_consume_token (parser->lexer);
31335 decl = convert_from_reference (decl);
31336 decl
31337 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31338 decl, false,
31339 &idk, loc);
31340 }
31341 /* FALLTHROUGH. */
31342 case OMP_CLAUSE_DEPEND:
31343 case OMP_CLAUSE_REDUCTION:
31344 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31345 {
31346 tree low_bound = NULL_TREE, length = NULL_TREE;
31347
31348 parser->colon_corrects_to_scope_p = false;
31349 cp_lexer_consume_token (parser->lexer);
31350 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31351 low_bound = cp_parser_expression (parser);
31352 if (!colon)
31353 parser->colon_corrects_to_scope_p
31354 = saved_colon_corrects_to_scope_p;
31355 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31356 length = integer_one_node;
31357 else
31358 {
31359 /* Look for `:'. */
31360 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31361 goto skip_comma;
31362 if (!cp_lexer_next_token_is (parser->lexer,
31363 CPP_CLOSE_SQUARE))
31364 length = cp_parser_expression (parser);
31365 }
31366 /* Look for the closing `]'. */
31367 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31368 RT_CLOSE_SQUARE))
31369 goto skip_comma;
31370
31371 decl = tree_cons (low_bound, length, decl);
31372 }
31373 break;
31374 default:
31375 break;
31376 }
31377
31378 tree u = build_omp_clause (token->location, kind);
31379 OMP_CLAUSE_DECL (u) = decl;
31380 OMP_CLAUSE_CHAIN (u) = list;
31381 list = u;
31382 }
31383 else
31384 list = tree_cons (decl, NULL_TREE, list);
31385
31386 get_comma:
31387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31388 break;
31389 cp_lexer_consume_token (parser->lexer);
31390 }
31391
31392 if (colon)
31393 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31394
31395 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31396 {
31397 *colon = true;
31398 cp_parser_require (parser, CPP_COLON, RT_COLON);
31399 return list;
31400 }
31401
31402 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31403 {
31404 int ending;
31405
31406 /* Try to resync to an unnested comma. Copied from
31407 cp_parser_parenthesized_expression_list. */
31408 skip_comma:
31409 if (colon)
31410 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31411 ending = cp_parser_skip_to_closing_parenthesis (parser,
31412 /*recovering=*/true,
31413 /*or_comma=*/true,
31414 /*consume_paren=*/true);
31415 if (ending < 0)
31416 goto get_comma;
31417 }
31418
31419 return list;
31420 }
31421
31422 /* Similarly, but expect leading and trailing parenthesis. This is a very
31423 common case for omp clauses. */
31424
31425 static tree
31426 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31427 {
31428 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31429 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31430 return list;
31431 }
31432
31433 /* OpenACC 2.0:
31434 copy ( variable-list )
31435 copyin ( variable-list )
31436 copyout ( variable-list )
31437 create ( variable-list )
31438 delete ( variable-list )
31439 present ( variable-list )
31440 present_or_copy ( variable-list )
31441 pcopy ( variable-list )
31442 present_or_copyin ( variable-list )
31443 pcopyin ( variable-list )
31444 present_or_copyout ( variable-list )
31445 pcopyout ( variable-list )
31446 present_or_create ( variable-list )
31447 pcreate ( variable-list ) */
31448
31449 static tree
31450 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31451 tree list)
31452 {
31453 enum gomp_map_kind kind;
31454 switch (c_kind)
31455 {
31456 case PRAGMA_OACC_CLAUSE_COPY:
31457 kind = GOMP_MAP_FORCE_TOFROM;
31458 break;
31459 case PRAGMA_OACC_CLAUSE_COPYIN:
31460 kind = GOMP_MAP_FORCE_TO;
31461 break;
31462 case PRAGMA_OACC_CLAUSE_COPYOUT:
31463 kind = GOMP_MAP_FORCE_FROM;
31464 break;
31465 case PRAGMA_OACC_CLAUSE_CREATE:
31466 kind = GOMP_MAP_FORCE_ALLOC;
31467 break;
31468 case PRAGMA_OACC_CLAUSE_DELETE:
31469 kind = GOMP_MAP_DELETE;
31470 break;
31471 case PRAGMA_OACC_CLAUSE_DEVICE:
31472 kind = GOMP_MAP_FORCE_TO;
31473 break;
31474 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31475 kind = GOMP_MAP_DEVICE_RESIDENT;
31476 break;
31477 case PRAGMA_OACC_CLAUSE_HOST:
31478 case PRAGMA_OACC_CLAUSE_SELF:
31479 kind = GOMP_MAP_FORCE_FROM;
31480 break;
31481 case PRAGMA_OACC_CLAUSE_LINK:
31482 kind = GOMP_MAP_LINK;
31483 break;
31484 case PRAGMA_OACC_CLAUSE_PRESENT:
31485 kind = GOMP_MAP_FORCE_PRESENT;
31486 break;
31487 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31488 kind = GOMP_MAP_TOFROM;
31489 break;
31490 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31491 kind = GOMP_MAP_TO;
31492 break;
31493 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31494 kind = GOMP_MAP_FROM;
31495 break;
31496 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31497 kind = GOMP_MAP_ALLOC;
31498 break;
31499 default:
31500 gcc_unreachable ();
31501 }
31502 tree nl, c;
31503 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31504
31505 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31506 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31507
31508 return nl;
31509 }
31510
31511 /* OpenACC 2.0:
31512 deviceptr ( variable-list ) */
31513
31514 static tree
31515 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31516 {
31517 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31518 tree vars, t;
31519
31520 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31521 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31522 variable-list must only allow for pointer variables. */
31523 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31524 for (t = vars; t; t = TREE_CHAIN (t))
31525 {
31526 tree v = TREE_PURPOSE (t);
31527 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31528 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31529 OMP_CLAUSE_DECL (u) = v;
31530 OMP_CLAUSE_CHAIN (u) = list;
31531 list = u;
31532 }
31533
31534 return list;
31535 }
31536
31537 /* OpenACC 2.0:
31538 auto
31539 independent
31540 nohost
31541 seq */
31542
31543 static tree
31544 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31545 enum omp_clause_code code,
31546 tree list, location_t location)
31547 {
31548 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31549 tree c = build_omp_clause (location, code);
31550 OMP_CLAUSE_CHAIN (c) = list;
31551 return c;
31552 }
31553
31554 /* OpenACC:
31555 num_gangs ( expression )
31556 num_workers ( expression )
31557 vector_length ( expression ) */
31558
31559 static tree
31560 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31561 const char *str, tree list)
31562 {
31563 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31564
31565 matching_parens parens;
31566 if (!parens.require_open (parser))
31567 return list;
31568
31569 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31570
31571 if (t == error_mark_node
31572 || !parens.require_close (parser))
31573 {
31574 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31575 /*or_comma=*/false,
31576 /*consume_paren=*/true);
31577 return list;
31578 }
31579
31580 check_no_duplicate_clause (list, code, str, loc);
31581
31582 tree c = build_omp_clause (loc, code);
31583 OMP_CLAUSE_OPERAND (c, 0) = t;
31584 OMP_CLAUSE_CHAIN (c) = list;
31585 return c;
31586 }
31587
31588 /* OpenACC:
31589
31590 gang [( gang-arg-list )]
31591 worker [( [num:] int-expr )]
31592 vector [( [length:] int-expr )]
31593
31594 where gang-arg is one of:
31595
31596 [num:] int-expr
31597 static: size-expr
31598
31599 and size-expr may be:
31600
31601 *
31602 int-expr
31603 */
31604
31605 static tree
31606 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31607 const char *str, tree list)
31608 {
31609 const char *id = "num";
31610 cp_lexer *lexer = parser->lexer;
31611 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31612 location_t loc = cp_lexer_peek_token (lexer)->location;
31613
31614 if (kind == OMP_CLAUSE_VECTOR)
31615 id = "length";
31616
31617 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31618 {
31619 matching_parens parens;
31620 parens.consume_open (parser);
31621
31622 do
31623 {
31624 cp_token *next = cp_lexer_peek_token (lexer);
31625 int idx = 0;
31626
31627 /* Gang static argument. */
31628 if (kind == OMP_CLAUSE_GANG
31629 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31630 {
31631 cp_lexer_consume_token (lexer);
31632
31633 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31634 goto cleanup_error;
31635
31636 idx = 1;
31637 if (ops[idx] != NULL)
31638 {
31639 cp_parser_error (parser, "too many %<static%> arguments");
31640 goto cleanup_error;
31641 }
31642
31643 /* Check for the '*' argument. */
31644 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31645 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31646 || cp_lexer_nth_token_is (parser->lexer, 2,
31647 CPP_CLOSE_PAREN)))
31648 {
31649 cp_lexer_consume_token (lexer);
31650 ops[idx] = integer_minus_one_node;
31651
31652 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31653 {
31654 cp_lexer_consume_token (lexer);
31655 continue;
31656 }
31657 else break;
31658 }
31659 }
31660 /* Worker num: argument and vector length: arguments. */
31661 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31662 && id_equal (next->u.value, id)
31663 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31664 {
31665 cp_lexer_consume_token (lexer); /* id */
31666 cp_lexer_consume_token (lexer); /* ':' */
31667 }
31668
31669 /* Now collect the actual argument. */
31670 if (ops[idx] != NULL_TREE)
31671 {
31672 cp_parser_error (parser, "unexpected argument");
31673 goto cleanup_error;
31674 }
31675
31676 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31677 false);
31678 if (expr == error_mark_node)
31679 goto cleanup_error;
31680
31681 mark_exp_read (expr);
31682 ops[idx] = expr;
31683
31684 if (kind == OMP_CLAUSE_GANG
31685 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31686 {
31687 cp_lexer_consume_token (lexer);
31688 continue;
31689 }
31690 break;
31691 }
31692 while (1);
31693
31694 if (!parens.require_close (parser))
31695 goto cleanup_error;
31696 }
31697
31698 check_no_duplicate_clause (list, kind, str, loc);
31699
31700 c = build_omp_clause (loc, kind);
31701
31702 if (ops[1])
31703 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31704
31705 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31706 OMP_CLAUSE_CHAIN (c) = list;
31707
31708 return c;
31709
31710 cleanup_error:
31711 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31712 return list;
31713 }
31714
31715 /* OpenACC 2.0:
31716 tile ( size-expr-list ) */
31717
31718 static tree
31719 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31720 {
31721 tree c, expr = error_mark_node;
31722 tree tile = NULL_TREE;
31723
31724 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31725 so, but the spec authors never considered such a case and have
31726 differing opinions on what it might mean, including 'not
31727 allowed'.) */
31728 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31729 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31730 clause_loc);
31731
31732 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31733 return list;
31734
31735 do
31736 {
31737 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31738 return list;
31739
31740 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31741 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31742 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31743 {
31744 cp_lexer_consume_token (parser->lexer);
31745 expr = integer_zero_node;
31746 }
31747 else
31748 expr = cp_parser_constant_expression (parser);
31749
31750 tile = tree_cons (NULL_TREE, expr, tile);
31751 }
31752 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31753
31754 /* Consume the trailing ')'. */
31755 cp_lexer_consume_token (parser->lexer);
31756
31757 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31758 tile = nreverse (tile);
31759 OMP_CLAUSE_TILE_LIST (c) = tile;
31760 OMP_CLAUSE_CHAIN (c) = list;
31761 return c;
31762 }
31763
31764 /* OpenACC 2.0
31765 Parse wait clause or directive parameters. */
31766
31767 static tree
31768 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31769 {
31770 vec<tree, va_gc> *args;
31771 tree t, args_tree;
31772
31773 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31774 /*cast_p=*/false,
31775 /*allow_expansion_p=*/true,
31776 /*non_constant_p=*/NULL);
31777
31778 if (args == NULL || args->length () == 0)
31779 {
31780 cp_parser_error (parser, "expected integer expression before ')'");
31781 if (args != NULL)
31782 release_tree_vector (args);
31783 return list;
31784 }
31785
31786 args_tree = build_tree_list_vec (args);
31787
31788 release_tree_vector (args);
31789
31790 for (t = args_tree; t; t = TREE_CHAIN (t))
31791 {
31792 tree targ = TREE_VALUE (t);
31793
31794 if (targ != error_mark_node)
31795 {
31796 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31797 error ("%<wait%> expression must be integral");
31798 else
31799 {
31800 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31801
31802 targ = mark_rvalue_use (targ);
31803 OMP_CLAUSE_DECL (c) = targ;
31804 OMP_CLAUSE_CHAIN (c) = list;
31805 list = c;
31806 }
31807 }
31808 }
31809
31810 return list;
31811 }
31812
31813 /* OpenACC:
31814 wait ( int-expr-list ) */
31815
31816 static tree
31817 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31818 {
31819 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31820
31821 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31822 return list;
31823
31824 list = cp_parser_oacc_wait_list (parser, location, list);
31825
31826 return list;
31827 }
31828
31829 /* OpenMP 3.0:
31830 collapse ( constant-expression ) */
31831
31832 static tree
31833 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31834 {
31835 tree c, num;
31836 location_t loc;
31837 HOST_WIDE_INT n;
31838
31839 loc = cp_lexer_peek_token (parser->lexer)->location;
31840 matching_parens parens;
31841 if (!parens.require_open (parser))
31842 return list;
31843
31844 num = cp_parser_constant_expression (parser);
31845
31846 if (!parens.require_close (parser))
31847 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31848 /*or_comma=*/false,
31849 /*consume_paren=*/true);
31850
31851 if (num == error_mark_node)
31852 return list;
31853 num = fold_non_dependent_expr (num);
31854 if (!tree_fits_shwi_p (num)
31855 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31856 || (n = tree_to_shwi (num)) <= 0
31857 || (int) n != n)
31858 {
31859 error_at (loc, "collapse argument needs positive constant integer expression");
31860 return list;
31861 }
31862
31863 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31864 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31865 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31866 OMP_CLAUSE_CHAIN (c) = list;
31867 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31868
31869 return c;
31870 }
31871
31872 /* OpenMP 2.5:
31873 default ( none | shared )
31874
31875 OpenACC:
31876 default ( none | present ) */
31877
31878 static tree
31879 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31880 location_t location, bool is_oacc)
31881 {
31882 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31883 tree c;
31884
31885 matching_parens parens;
31886 if (!parens.require_open (parser))
31887 return list;
31888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31889 {
31890 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31891 const char *p = IDENTIFIER_POINTER (id);
31892
31893 switch (p[0])
31894 {
31895 case 'n':
31896 if (strcmp ("none", p) != 0)
31897 goto invalid_kind;
31898 kind = OMP_CLAUSE_DEFAULT_NONE;
31899 break;
31900
31901 case 'p':
31902 if (strcmp ("present", p) != 0 || !is_oacc)
31903 goto invalid_kind;
31904 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31905 break;
31906
31907 case 's':
31908 if (strcmp ("shared", p) != 0 || is_oacc)
31909 goto invalid_kind;
31910 kind = OMP_CLAUSE_DEFAULT_SHARED;
31911 break;
31912
31913 default:
31914 goto invalid_kind;
31915 }
31916
31917 cp_lexer_consume_token (parser->lexer);
31918 }
31919 else
31920 {
31921 invalid_kind:
31922 if (is_oacc)
31923 cp_parser_error (parser, "expected %<none%> or %<present%>");
31924 else
31925 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31926 }
31927
31928 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31929 || !parens.require_close (parser))
31930 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31931 /*or_comma=*/false,
31932 /*consume_paren=*/true);
31933
31934 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31935 return list;
31936
31937 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31938 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31939 OMP_CLAUSE_CHAIN (c) = list;
31940 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31941
31942 return c;
31943 }
31944
31945 /* OpenMP 3.1:
31946 final ( expression ) */
31947
31948 static tree
31949 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31950 {
31951 tree t, c;
31952
31953 matching_parens parens;
31954 if (!parens.require_open (parser))
31955 return list;
31956
31957 t = cp_parser_condition (parser);
31958
31959 if (t == error_mark_node
31960 || !parens.require_close (parser))
31961 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31962 /*or_comma=*/false,
31963 /*consume_paren=*/true);
31964
31965 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31966
31967 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31968 OMP_CLAUSE_FINAL_EXPR (c) = t;
31969 OMP_CLAUSE_CHAIN (c) = list;
31970
31971 return c;
31972 }
31973
31974 /* OpenMP 2.5:
31975 if ( expression )
31976
31977 OpenMP 4.5:
31978 if ( directive-name-modifier : expression )
31979
31980 directive-name-modifier:
31981 parallel | task | taskloop | target data | target | target update
31982 | target enter data | target exit data */
31983
31984 static tree
31985 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31986 bool is_omp)
31987 {
31988 tree t, c;
31989 enum tree_code if_modifier = ERROR_MARK;
31990
31991 matching_parens parens;
31992 if (!parens.require_open (parser))
31993 return list;
31994
31995 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31996 {
31997 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31998 const char *p = IDENTIFIER_POINTER (id);
31999 int n = 2;
32000
32001 if (strcmp ("parallel", p) == 0)
32002 if_modifier = OMP_PARALLEL;
32003 else if (strcmp ("task", p) == 0)
32004 if_modifier = OMP_TASK;
32005 else if (strcmp ("taskloop", p) == 0)
32006 if_modifier = OMP_TASKLOOP;
32007 else if (strcmp ("target", p) == 0)
32008 {
32009 if_modifier = OMP_TARGET;
32010 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32011 {
32012 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32013 p = IDENTIFIER_POINTER (id);
32014 if (strcmp ("data", p) == 0)
32015 if_modifier = OMP_TARGET_DATA;
32016 else if (strcmp ("update", p) == 0)
32017 if_modifier = OMP_TARGET_UPDATE;
32018 else if (strcmp ("enter", p) == 0)
32019 if_modifier = OMP_TARGET_ENTER_DATA;
32020 else if (strcmp ("exit", p) == 0)
32021 if_modifier = OMP_TARGET_EXIT_DATA;
32022 if (if_modifier != OMP_TARGET)
32023 n = 3;
32024 else
32025 {
32026 location_t loc
32027 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32028 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32029 "or %<exit%>");
32030 if_modifier = ERROR_MARK;
32031 }
32032 if (if_modifier == OMP_TARGET_ENTER_DATA
32033 || if_modifier == OMP_TARGET_EXIT_DATA)
32034 {
32035 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32036 {
32037 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32038 p = IDENTIFIER_POINTER (id);
32039 if (strcmp ("data", p) == 0)
32040 n = 4;
32041 }
32042 if (n != 4)
32043 {
32044 location_t loc
32045 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32046 error_at (loc, "expected %<data%>");
32047 if_modifier = ERROR_MARK;
32048 }
32049 }
32050 }
32051 }
32052 if (if_modifier != ERROR_MARK)
32053 {
32054 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32055 {
32056 while (n-- > 0)
32057 cp_lexer_consume_token (parser->lexer);
32058 }
32059 else
32060 {
32061 if (n > 2)
32062 {
32063 location_t loc
32064 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32065 error_at (loc, "expected %<:%>");
32066 }
32067 if_modifier = ERROR_MARK;
32068 }
32069 }
32070 }
32071
32072 t = cp_parser_condition (parser);
32073
32074 if (t == error_mark_node
32075 || !parens.require_close (parser))
32076 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32077 /*or_comma=*/false,
32078 /*consume_paren=*/true);
32079
32080 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32081 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32082 {
32083 if (if_modifier != ERROR_MARK
32084 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32085 {
32086 const char *p = NULL;
32087 switch (if_modifier)
32088 {
32089 case OMP_PARALLEL: p = "parallel"; break;
32090 case OMP_TASK: p = "task"; break;
32091 case OMP_TASKLOOP: p = "taskloop"; break;
32092 case OMP_TARGET_DATA: p = "target data"; break;
32093 case OMP_TARGET: p = "target"; break;
32094 case OMP_TARGET_UPDATE: p = "target update"; break;
32095 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32096 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32097 default: gcc_unreachable ();
32098 }
32099 error_at (location, "too many %<if%> clauses with %qs modifier",
32100 p);
32101 return list;
32102 }
32103 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32104 {
32105 if (!is_omp)
32106 error_at (location, "too many %<if%> clauses");
32107 else
32108 error_at (location, "too many %<if%> clauses without modifier");
32109 return list;
32110 }
32111 else if (if_modifier == ERROR_MARK
32112 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32113 {
32114 error_at (location, "if any %<if%> clause has modifier, then all "
32115 "%<if%> clauses have to use modifier");
32116 return list;
32117 }
32118 }
32119
32120 c = build_omp_clause (location, OMP_CLAUSE_IF);
32121 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32122 OMP_CLAUSE_IF_EXPR (c) = t;
32123 OMP_CLAUSE_CHAIN (c) = list;
32124
32125 return c;
32126 }
32127
32128 /* OpenMP 3.1:
32129 mergeable */
32130
32131 static tree
32132 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32133 tree list, location_t location)
32134 {
32135 tree c;
32136
32137 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32138 location);
32139
32140 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32141 OMP_CLAUSE_CHAIN (c) = list;
32142 return c;
32143 }
32144
32145 /* OpenMP 2.5:
32146 nowait */
32147
32148 static tree
32149 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32150 tree list, location_t location)
32151 {
32152 tree c;
32153
32154 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32155
32156 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32157 OMP_CLAUSE_CHAIN (c) = list;
32158 return c;
32159 }
32160
32161 /* OpenMP 2.5:
32162 num_threads ( expression ) */
32163
32164 static tree
32165 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32166 location_t location)
32167 {
32168 tree t, c;
32169
32170 matching_parens parens;
32171 if (!parens.require_open (parser))
32172 return list;
32173
32174 t = cp_parser_expression (parser);
32175
32176 if (t == error_mark_node
32177 || !parens.require_close (parser))
32178 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32179 /*or_comma=*/false,
32180 /*consume_paren=*/true);
32181
32182 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32183 "num_threads", location);
32184
32185 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32186 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32187 OMP_CLAUSE_CHAIN (c) = list;
32188
32189 return c;
32190 }
32191
32192 /* OpenMP 4.5:
32193 num_tasks ( expression ) */
32194
32195 static tree
32196 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32197 location_t location)
32198 {
32199 tree t, c;
32200
32201 matching_parens parens;
32202 if (!parens.require_open (parser))
32203 return list;
32204
32205 t = cp_parser_expression (parser);
32206
32207 if (t == error_mark_node
32208 || !parens.require_close (parser))
32209 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32210 /*or_comma=*/false,
32211 /*consume_paren=*/true);
32212
32213 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32214 "num_tasks", location);
32215
32216 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32217 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32218 OMP_CLAUSE_CHAIN (c) = list;
32219
32220 return c;
32221 }
32222
32223 /* OpenMP 4.5:
32224 grainsize ( expression ) */
32225
32226 static tree
32227 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32228 location_t location)
32229 {
32230 tree t, c;
32231
32232 matching_parens parens;
32233 if (!parens.require_open (parser))
32234 return list;
32235
32236 t = cp_parser_expression (parser);
32237
32238 if (t == error_mark_node
32239 || !parens.require_close (parser))
32240 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32241 /*or_comma=*/false,
32242 /*consume_paren=*/true);
32243
32244 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32245 "grainsize", location);
32246
32247 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32248 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32249 OMP_CLAUSE_CHAIN (c) = list;
32250
32251 return c;
32252 }
32253
32254 /* OpenMP 4.5:
32255 priority ( expression ) */
32256
32257 static tree
32258 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32259 location_t location)
32260 {
32261 tree t, c;
32262
32263 matching_parens parens;
32264 if (!parens.require_open (parser))
32265 return list;
32266
32267 t = cp_parser_expression (parser);
32268
32269 if (t == error_mark_node
32270 || !parens.require_close (parser))
32271 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32272 /*or_comma=*/false,
32273 /*consume_paren=*/true);
32274
32275 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32276 "priority", location);
32277
32278 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32279 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32280 OMP_CLAUSE_CHAIN (c) = list;
32281
32282 return c;
32283 }
32284
32285 /* OpenMP 4.5:
32286 hint ( expression ) */
32287
32288 static tree
32289 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32290 location_t location)
32291 {
32292 tree t, c;
32293
32294 matching_parens parens;
32295 if (!parens.require_open (parser))
32296 return list;
32297
32298 t = cp_parser_expression (parser);
32299
32300 if (t == error_mark_node
32301 || !parens.require_close (parser))
32302 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32303 /*or_comma=*/false,
32304 /*consume_paren=*/true);
32305
32306 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32307
32308 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32309 OMP_CLAUSE_HINT_EXPR (c) = t;
32310 OMP_CLAUSE_CHAIN (c) = list;
32311
32312 return c;
32313 }
32314
32315 /* OpenMP 4.5:
32316 defaultmap ( tofrom : scalar ) */
32317
32318 static tree
32319 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32320 location_t location)
32321 {
32322 tree c, id;
32323 const char *p;
32324
32325 matching_parens parens;
32326 if (!parens.require_open (parser))
32327 return list;
32328
32329 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32330 {
32331 cp_parser_error (parser, "expected %<tofrom%>");
32332 goto out_err;
32333 }
32334 id = cp_lexer_peek_token (parser->lexer)->u.value;
32335 p = IDENTIFIER_POINTER (id);
32336 if (strcmp (p, "tofrom") != 0)
32337 {
32338 cp_parser_error (parser, "expected %<tofrom%>");
32339 goto out_err;
32340 }
32341 cp_lexer_consume_token (parser->lexer);
32342 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32343 goto out_err;
32344
32345 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32346 {
32347 cp_parser_error (parser, "expected %<scalar%>");
32348 goto out_err;
32349 }
32350 id = cp_lexer_peek_token (parser->lexer)->u.value;
32351 p = IDENTIFIER_POINTER (id);
32352 if (strcmp (p, "scalar") != 0)
32353 {
32354 cp_parser_error (parser, "expected %<scalar%>");
32355 goto out_err;
32356 }
32357 cp_lexer_consume_token (parser->lexer);
32358 if (!parens.require_close (parser))
32359 goto out_err;
32360
32361 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32362 location);
32363
32364 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32365 OMP_CLAUSE_CHAIN (c) = list;
32366 return c;
32367
32368 out_err:
32369 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32370 /*or_comma=*/false,
32371 /*consume_paren=*/true);
32372 return list;
32373 }
32374
32375 /* OpenMP 2.5:
32376 ordered
32377
32378 OpenMP 4.5:
32379 ordered ( constant-expression ) */
32380
32381 static tree
32382 cp_parser_omp_clause_ordered (cp_parser *parser,
32383 tree list, location_t location)
32384 {
32385 tree c, num = NULL_TREE;
32386 HOST_WIDE_INT n;
32387
32388 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32389 "ordered", location);
32390
32391 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32392 {
32393 matching_parens parens;
32394 parens.consume_open (parser);
32395
32396 num = cp_parser_constant_expression (parser);
32397
32398 if (!parens.require_close (parser))
32399 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32400 /*or_comma=*/false,
32401 /*consume_paren=*/true);
32402
32403 if (num == error_mark_node)
32404 return list;
32405 num = fold_non_dependent_expr (num);
32406 if (!tree_fits_shwi_p (num)
32407 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32408 || (n = tree_to_shwi (num)) <= 0
32409 || (int) n != n)
32410 {
32411 error_at (location,
32412 "ordered argument needs positive constant integer "
32413 "expression");
32414 return list;
32415 }
32416 }
32417
32418 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32419 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32420 OMP_CLAUSE_CHAIN (c) = list;
32421 return c;
32422 }
32423
32424 /* OpenMP 2.5:
32425 reduction ( reduction-operator : variable-list )
32426
32427 reduction-operator:
32428 One of: + * - & ^ | && ||
32429
32430 OpenMP 3.1:
32431
32432 reduction-operator:
32433 One of: + * - & ^ | && || min max
32434
32435 OpenMP 4.0:
32436
32437 reduction-operator:
32438 One of: + * - & ^ | && ||
32439 id-expression */
32440
32441 static tree
32442 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32443 {
32444 enum tree_code code = ERROR_MARK;
32445 tree nlist, c, id = NULL_TREE;
32446
32447 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32448 return list;
32449
32450 switch (cp_lexer_peek_token (parser->lexer)->type)
32451 {
32452 case CPP_PLUS: code = PLUS_EXPR; break;
32453 case CPP_MULT: code = MULT_EXPR; break;
32454 case CPP_MINUS: code = MINUS_EXPR; break;
32455 case CPP_AND: code = BIT_AND_EXPR; break;
32456 case CPP_XOR: code = BIT_XOR_EXPR; break;
32457 case CPP_OR: code = BIT_IOR_EXPR; break;
32458 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32459 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32460 default: break;
32461 }
32462
32463 if (code != ERROR_MARK)
32464 cp_lexer_consume_token (parser->lexer);
32465 else
32466 {
32467 bool saved_colon_corrects_to_scope_p;
32468 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32469 parser->colon_corrects_to_scope_p = false;
32470 id = cp_parser_id_expression (parser, /*template_p=*/false,
32471 /*check_dependency_p=*/true,
32472 /*template_p=*/NULL,
32473 /*declarator_p=*/false,
32474 /*optional_p=*/false);
32475 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32476 if (identifier_p (id))
32477 {
32478 const char *p = IDENTIFIER_POINTER (id);
32479
32480 if (strcmp (p, "min") == 0)
32481 code = MIN_EXPR;
32482 else if (strcmp (p, "max") == 0)
32483 code = MAX_EXPR;
32484 else if (id == ovl_op_identifier (false, PLUS_EXPR))
32485 code = PLUS_EXPR;
32486 else if (id == ovl_op_identifier (false, MULT_EXPR))
32487 code = MULT_EXPR;
32488 else if (id == ovl_op_identifier (false, MINUS_EXPR))
32489 code = MINUS_EXPR;
32490 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
32491 code = BIT_AND_EXPR;
32492 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
32493 code = BIT_IOR_EXPR;
32494 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
32495 code = BIT_XOR_EXPR;
32496 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
32497 code = TRUTH_ANDIF_EXPR;
32498 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
32499 code = TRUTH_ORIF_EXPR;
32500 id = omp_reduction_id (code, id, NULL_TREE);
32501 tree scope = parser->scope;
32502 if (scope)
32503 id = build_qualified_name (NULL_TREE, scope, id, false);
32504 parser->scope = NULL_TREE;
32505 parser->qualifying_scope = NULL_TREE;
32506 parser->object_scope = NULL_TREE;
32507 }
32508 else
32509 {
32510 error ("invalid reduction-identifier");
32511 resync_fail:
32512 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32513 /*or_comma=*/false,
32514 /*consume_paren=*/true);
32515 return list;
32516 }
32517 }
32518
32519 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32520 goto resync_fail;
32521
32522 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32523 NULL);
32524 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32525 {
32526 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32527 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32528 }
32529
32530 return nlist;
32531 }
32532
32533 /* OpenMP 2.5:
32534 schedule ( schedule-kind )
32535 schedule ( schedule-kind , expression )
32536
32537 schedule-kind:
32538 static | dynamic | guided | runtime | auto
32539
32540 OpenMP 4.5:
32541 schedule ( schedule-modifier : schedule-kind )
32542 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32543
32544 schedule-modifier:
32545 simd
32546 monotonic
32547 nonmonotonic */
32548
32549 static tree
32550 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32551 {
32552 tree c, t;
32553 int modifiers = 0, nmodifiers = 0;
32554
32555 matching_parens parens;
32556 if (!parens.require_open (parser))
32557 return list;
32558
32559 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32560
32561 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32562 {
32563 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32564 const char *p = IDENTIFIER_POINTER (id);
32565 if (strcmp ("simd", p) == 0)
32566 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32567 else if (strcmp ("monotonic", p) == 0)
32568 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32569 else if (strcmp ("nonmonotonic", p) == 0)
32570 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32571 else
32572 break;
32573 cp_lexer_consume_token (parser->lexer);
32574 if (nmodifiers++ == 0
32575 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32576 cp_lexer_consume_token (parser->lexer);
32577 else
32578 {
32579 cp_parser_require (parser, CPP_COLON, RT_COLON);
32580 break;
32581 }
32582 }
32583
32584 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32585 {
32586 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32587 const char *p = IDENTIFIER_POINTER (id);
32588
32589 switch (p[0])
32590 {
32591 case 'd':
32592 if (strcmp ("dynamic", p) != 0)
32593 goto invalid_kind;
32594 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32595 break;
32596
32597 case 'g':
32598 if (strcmp ("guided", p) != 0)
32599 goto invalid_kind;
32600 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32601 break;
32602
32603 case 'r':
32604 if (strcmp ("runtime", p) != 0)
32605 goto invalid_kind;
32606 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32607 break;
32608
32609 default:
32610 goto invalid_kind;
32611 }
32612 }
32613 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32614 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32615 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32616 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32617 else
32618 goto invalid_kind;
32619 cp_lexer_consume_token (parser->lexer);
32620
32621 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32622 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32623 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32624 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32625 {
32626 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32627 "specified");
32628 modifiers = 0;
32629 }
32630
32631 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32632 {
32633 cp_token *token;
32634 cp_lexer_consume_token (parser->lexer);
32635
32636 token = cp_lexer_peek_token (parser->lexer);
32637 t = cp_parser_assignment_expression (parser);
32638
32639 if (t == error_mark_node)
32640 goto resync_fail;
32641 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32642 error_at (token->location, "schedule %<runtime%> does not take "
32643 "a %<chunk_size%> parameter");
32644 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32645 error_at (token->location, "schedule %<auto%> does not take "
32646 "a %<chunk_size%> parameter");
32647 else
32648 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32649
32650 if (!parens.require_close (parser))
32651 goto resync_fail;
32652 }
32653 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32654 goto resync_fail;
32655
32656 OMP_CLAUSE_SCHEDULE_KIND (c)
32657 = (enum omp_clause_schedule_kind)
32658 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32659
32660 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32661 OMP_CLAUSE_CHAIN (c) = list;
32662 return c;
32663
32664 invalid_kind:
32665 cp_parser_error (parser, "invalid schedule kind");
32666 resync_fail:
32667 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32668 /*or_comma=*/false,
32669 /*consume_paren=*/true);
32670 return list;
32671 }
32672
32673 /* OpenMP 3.0:
32674 untied */
32675
32676 static tree
32677 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32678 tree list, location_t location)
32679 {
32680 tree c;
32681
32682 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32683
32684 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32685 OMP_CLAUSE_CHAIN (c) = list;
32686 return c;
32687 }
32688
32689 /* OpenMP 4.0:
32690 inbranch
32691 notinbranch */
32692
32693 static tree
32694 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32695 tree list, location_t location)
32696 {
32697 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32698 tree c = build_omp_clause (location, code);
32699 OMP_CLAUSE_CHAIN (c) = list;
32700 return c;
32701 }
32702
32703 /* OpenMP 4.0:
32704 parallel
32705 for
32706 sections
32707 taskgroup */
32708
32709 static tree
32710 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32711 enum omp_clause_code code,
32712 tree list, location_t location)
32713 {
32714 tree c = build_omp_clause (location, code);
32715 OMP_CLAUSE_CHAIN (c) = list;
32716 return c;
32717 }
32718
32719 /* OpenMP 4.5:
32720 nogroup */
32721
32722 static tree
32723 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32724 tree list, location_t location)
32725 {
32726 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32727 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32728 OMP_CLAUSE_CHAIN (c) = list;
32729 return c;
32730 }
32731
32732 /* OpenMP 4.5:
32733 simd
32734 threads */
32735
32736 static tree
32737 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32738 enum omp_clause_code code,
32739 tree list, location_t location)
32740 {
32741 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32742 tree c = build_omp_clause (location, code);
32743 OMP_CLAUSE_CHAIN (c) = list;
32744 return c;
32745 }
32746
32747 /* OpenMP 4.0:
32748 num_teams ( expression ) */
32749
32750 static tree
32751 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32752 location_t location)
32753 {
32754 tree t, c;
32755
32756 matching_parens parens;
32757 if (!parens.require_open (parser))
32758 return list;
32759
32760 t = cp_parser_expression (parser);
32761
32762 if (t == error_mark_node
32763 || !parens.require_close (parser))
32764 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32765 /*or_comma=*/false,
32766 /*consume_paren=*/true);
32767
32768 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32769 "num_teams", location);
32770
32771 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32772 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32773 OMP_CLAUSE_CHAIN (c) = list;
32774
32775 return c;
32776 }
32777
32778 /* OpenMP 4.0:
32779 thread_limit ( expression ) */
32780
32781 static tree
32782 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32783 location_t location)
32784 {
32785 tree t, c;
32786
32787 matching_parens parens;
32788 if (!parens.require_open (parser))
32789 return list;
32790
32791 t = cp_parser_expression (parser);
32792
32793 if (t == error_mark_node
32794 || !parens.require_close (parser))
32795 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32796 /*or_comma=*/false,
32797 /*consume_paren=*/true);
32798
32799 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32800 "thread_limit", location);
32801
32802 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32803 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32804 OMP_CLAUSE_CHAIN (c) = list;
32805
32806 return c;
32807 }
32808
32809 /* OpenMP 4.0:
32810 aligned ( variable-list )
32811 aligned ( variable-list : constant-expression ) */
32812
32813 static tree
32814 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32815 {
32816 tree nlist, c, alignment = NULL_TREE;
32817 bool colon;
32818
32819 matching_parens parens;
32820 if (!parens.require_open (parser))
32821 return list;
32822
32823 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32824 &colon);
32825
32826 if (colon)
32827 {
32828 alignment = cp_parser_constant_expression (parser);
32829
32830 if (!parens.require_close (parser))
32831 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32832 /*or_comma=*/false,
32833 /*consume_paren=*/true);
32834
32835 if (alignment == error_mark_node)
32836 alignment = NULL_TREE;
32837 }
32838
32839 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32840 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32841
32842 return nlist;
32843 }
32844
32845 /* OpenMP 4.0:
32846 linear ( variable-list )
32847 linear ( variable-list : expression )
32848
32849 OpenMP 4.5:
32850 linear ( modifier ( variable-list ) )
32851 linear ( modifier ( variable-list ) : expression ) */
32852
32853 static tree
32854 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32855 bool declare_simd)
32856 {
32857 tree nlist, c, step = integer_one_node;
32858 bool colon;
32859 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32860
32861 matching_parens parens;
32862 if (!parens.require_open (parser))
32863 return list;
32864
32865 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32866 {
32867 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32868 const char *p = IDENTIFIER_POINTER (id);
32869
32870 if (strcmp ("ref", p) == 0)
32871 kind = OMP_CLAUSE_LINEAR_REF;
32872 else if (strcmp ("val", p) == 0)
32873 kind = OMP_CLAUSE_LINEAR_VAL;
32874 else if (strcmp ("uval", p) == 0)
32875 kind = OMP_CLAUSE_LINEAR_UVAL;
32876 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32877 cp_lexer_consume_token (parser->lexer);
32878 else
32879 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32880 }
32881
32882 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32883 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32884 &colon);
32885 else
32886 {
32887 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32888 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32889 if (colon)
32890 cp_parser_require (parser, CPP_COLON, RT_COLON);
32891 else if (!parens.require_close (parser))
32892 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32893 /*or_comma=*/false,
32894 /*consume_paren=*/true);
32895 }
32896
32897 if (colon)
32898 {
32899 step = NULL_TREE;
32900 if (declare_simd
32901 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32902 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32903 {
32904 cp_token *token = cp_lexer_peek_token (parser->lexer);
32905 cp_parser_parse_tentatively (parser);
32906 step = cp_parser_id_expression (parser, /*template_p=*/false,
32907 /*check_dependency_p=*/true,
32908 /*template_p=*/NULL,
32909 /*declarator_p=*/false,
32910 /*optional_p=*/false);
32911 if (step != error_mark_node)
32912 step = cp_parser_lookup_name_simple (parser, step, token->location);
32913 if (step == error_mark_node)
32914 {
32915 step = NULL_TREE;
32916 cp_parser_abort_tentative_parse (parser);
32917 }
32918 else if (!cp_parser_parse_definitely (parser))
32919 step = NULL_TREE;
32920 }
32921 if (!step)
32922 step = cp_parser_expression (parser);
32923
32924 if (!parens.require_close (parser))
32925 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32926 /*or_comma=*/false,
32927 /*consume_paren=*/true);
32928
32929 if (step == error_mark_node)
32930 return list;
32931 }
32932
32933 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32934 {
32935 OMP_CLAUSE_LINEAR_STEP (c) = step;
32936 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32937 }
32938
32939 return nlist;
32940 }
32941
32942 /* OpenMP 4.0:
32943 safelen ( constant-expression ) */
32944
32945 static tree
32946 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32947 location_t location)
32948 {
32949 tree t, c;
32950
32951 matching_parens parens;
32952 if (!parens.require_open (parser))
32953 return list;
32954
32955 t = cp_parser_constant_expression (parser);
32956
32957 if (t == error_mark_node
32958 || !parens.require_close (parser))
32959 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32960 /*or_comma=*/false,
32961 /*consume_paren=*/true);
32962
32963 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32964
32965 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32966 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32967 OMP_CLAUSE_CHAIN (c) = list;
32968
32969 return c;
32970 }
32971
32972 /* OpenMP 4.0:
32973 simdlen ( constant-expression ) */
32974
32975 static tree
32976 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32977 location_t location)
32978 {
32979 tree t, c;
32980
32981 matching_parens parens;
32982 if (!parens.require_open (parser))
32983 return list;
32984
32985 t = cp_parser_constant_expression (parser);
32986
32987 if (t == error_mark_node
32988 || !parens.require_close (parser))
32989 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32990 /*or_comma=*/false,
32991 /*consume_paren=*/true);
32992
32993 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32994
32995 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32996 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32997 OMP_CLAUSE_CHAIN (c) = list;
32998
32999 return c;
33000 }
33001
33002 /* OpenMP 4.5:
33003 vec:
33004 identifier [+/- integer]
33005 vec , identifier [+/- integer]
33006 */
33007
33008 static tree
33009 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33010 tree list)
33011 {
33012 tree vec = NULL;
33013
33014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33015 {
33016 cp_parser_error (parser, "expected identifier");
33017 return list;
33018 }
33019
33020 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33021 {
33022 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33023 tree t, identifier = cp_parser_identifier (parser);
33024 tree addend = NULL;
33025
33026 if (identifier == error_mark_node)
33027 t = error_mark_node;
33028 else
33029 {
33030 t = cp_parser_lookup_name_simple
33031 (parser, identifier,
33032 cp_lexer_peek_token (parser->lexer)->location);
33033 if (t == error_mark_node)
33034 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33035 id_loc);
33036 }
33037
33038 bool neg = false;
33039 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33040 neg = true;
33041 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33042 {
33043 addend = integer_zero_node;
33044 goto add_to_vector;
33045 }
33046 cp_lexer_consume_token (parser->lexer);
33047
33048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33049 {
33050 cp_parser_error (parser, "expected integer");
33051 return list;
33052 }
33053
33054 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33055 if (TREE_CODE (addend) != INTEGER_CST)
33056 {
33057 cp_parser_error (parser, "expected integer");
33058 return list;
33059 }
33060 cp_lexer_consume_token (parser->lexer);
33061
33062 add_to_vector:
33063 if (t != error_mark_node)
33064 {
33065 vec = tree_cons (addend, t, vec);
33066 if (neg)
33067 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33068 }
33069
33070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33071 break;
33072
33073 cp_lexer_consume_token (parser->lexer);
33074 }
33075
33076 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33077 {
33078 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33079 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33080 OMP_CLAUSE_DECL (u) = nreverse (vec);
33081 OMP_CLAUSE_CHAIN (u) = list;
33082 return u;
33083 }
33084 return list;
33085 }
33086
33087 /* OpenMP 4.0:
33088 depend ( depend-kind : variable-list )
33089
33090 depend-kind:
33091 in | out | inout
33092
33093 OpenMP 4.5:
33094 depend ( source )
33095
33096 depend ( sink : vec ) */
33097
33098 static tree
33099 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33100 {
33101 tree nlist, c;
33102 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33103
33104 matching_parens parens;
33105 if (!parens.require_open (parser))
33106 return list;
33107
33108 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33109 {
33110 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33111 const char *p = IDENTIFIER_POINTER (id);
33112
33113 if (strcmp ("in", p) == 0)
33114 kind = OMP_CLAUSE_DEPEND_IN;
33115 else if (strcmp ("inout", p) == 0)
33116 kind = OMP_CLAUSE_DEPEND_INOUT;
33117 else if (strcmp ("out", p) == 0)
33118 kind = OMP_CLAUSE_DEPEND_OUT;
33119 else if (strcmp ("source", p) == 0)
33120 kind = OMP_CLAUSE_DEPEND_SOURCE;
33121 else if (strcmp ("sink", p) == 0)
33122 kind = OMP_CLAUSE_DEPEND_SINK;
33123 else
33124 goto invalid_kind;
33125 }
33126 else
33127 goto invalid_kind;
33128
33129 cp_lexer_consume_token (parser->lexer);
33130
33131 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33132 {
33133 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33134 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33135 OMP_CLAUSE_DECL (c) = NULL_TREE;
33136 OMP_CLAUSE_CHAIN (c) = list;
33137 if (!parens.require_close (parser))
33138 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33139 /*or_comma=*/false,
33140 /*consume_paren=*/true);
33141 return c;
33142 }
33143
33144 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33145 goto resync_fail;
33146
33147 if (kind == OMP_CLAUSE_DEPEND_SINK)
33148 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33149 else
33150 {
33151 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33152 list, NULL);
33153
33154 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33155 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33156 }
33157 return nlist;
33158
33159 invalid_kind:
33160 cp_parser_error (parser, "invalid depend kind");
33161 resync_fail:
33162 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33163 /*or_comma=*/false,
33164 /*consume_paren=*/true);
33165 return list;
33166 }
33167
33168 /* OpenMP 4.0:
33169 map ( map-kind : variable-list )
33170 map ( variable-list )
33171
33172 map-kind:
33173 alloc | to | from | tofrom
33174
33175 OpenMP 4.5:
33176 map-kind:
33177 alloc | to | from | tofrom | release | delete
33178
33179 map ( always [,] map-kind: variable-list ) */
33180
33181 static tree
33182 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33183 {
33184 tree nlist, c;
33185 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33186 bool always = false;
33187
33188 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33189 return list;
33190
33191 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33192 {
33193 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33194 const char *p = IDENTIFIER_POINTER (id);
33195
33196 if (strcmp ("always", p) == 0)
33197 {
33198 int nth = 2;
33199 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33200 nth++;
33201 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33202 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33203 == RID_DELETE))
33204 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33205 == CPP_COLON))
33206 {
33207 always = true;
33208 cp_lexer_consume_token (parser->lexer);
33209 if (nth == 3)
33210 cp_lexer_consume_token (parser->lexer);
33211 }
33212 }
33213 }
33214
33215 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33216 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33217 {
33218 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33219 const char *p = IDENTIFIER_POINTER (id);
33220
33221 if (strcmp ("alloc", p) == 0)
33222 kind = GOMP_MAP_ALLOC;
33223 else if (strcmp ("to", p) == 0)
33224 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33225 else if (strcmp ("from", p) == 0)
33226 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33227 else if (strcmp ("tofrom", p) == 0)
33228 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33229 else if (strcmp ("release", p) == 0)
33230 kind = GOMP_MAP_RELEASE;
33231 else
33232 {
33233 cp_parser_error (parser, "invalid map kind");
33234 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33235 /*or_comma=*/false,
33236 /*consume_paren=*/true);
33237 return list;
33238 }
33239 cp_lexer_consume_token (parser->lexer);
33240 cp_lexer_consume_token (parser->lexer);
33241 }
33242 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33243 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33244 {
33245 kind = GOMP_MAP_DELETE;
33246 cp_lexer_consume_token (parser->lexer);
33247 cp_lexer_consume_token (parser->lexer);
33248 }
33249
33250 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33251 NULL);
33252
33253 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33254 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33255
33256 return nlist;
33257 }
33258
33259 /* OpenMP 4.0:
33260 device ( expression ) */
33261
33262 static tree
33263 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33264 location_t location)
33265 {
33266 tree t, c;
33267
33268 matching_parens parens;
33269 if (!parens.require_open (parser))
33270 return list;
33271
33272 t = cp_parser_expression (parser);
33273
33274 if (t == error_mark_node
33275 || !parens.require_close (parser))
33276 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33277 /*or_comma=*/false,
33278 /*consume_paren=*/true);
33279
33280 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33281 "device", location);
33282
33283 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33284 OMP_CLAUSE_DEVICE_ID (c) = t;
33285 OMP_CLAUSE_CHAIN (c) = list;
33286
33287 return c;
33288 }
33289
33290 /* OpenMP 4.0:
33291 dist_schedule ( static )
33292 dist_schedule ( static , expression ) */
33293
33294 static tree
33295 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33296 location_t location)
33297 {
33298 tree c, t;
33299
33300 matching_parens parens;
33301 if (!parens.require_open (parser))
33302 return list;
33303
33304 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33305
33306 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33307 goto invalid_kind;
33308 cp_lexer_consume_token (parser->lexer);
33309
33310 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33311 {
33312 cp_lexer_consume_token (parser->lexer);
33313
33314 t = cp_parser_assignment_expression (parser);
33315
33316 if (t == error_mark_node)
33317 goto resync_fail;
33318 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33319
33320 if (!parens.require_close (parser))
33321 goto resync_fail;
33322 }
33323 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33324 goto resync_fail;
33325
33326 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33327 location);
33328 OMP_CLAUSE_CHAIN (c) = list;
33329 return c;
33330
33331 invalid_kind:
33332 cp_parser_error (parser, "invalid dist_schedule kind");
33333 resync_fail:
33334 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33335 /*or_comma=*/false,
33336 /*consume_paren=*/true);
33337 return list;
33338 }
33339
33340 /* OpenMP 4.0:
33341 proc_bind ( proc-bind-kind )
33342
33343 proc-bind-kind:
33344 master | close | spread */
33345
33346 static tree
33347 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33348 location_t location)
33349 {
33350 tree c;
33351 enum omp_clause_proc_bind_kind kind;
33352
33353 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33354 return list;
33355
33356 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33357 {
33358 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33359 const char *p = IDENTIFIER_POINTER (id);
33360
33361 if (strcmp ("master", p) == 0)
33362 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33363 else if (strcmp ("close", p) == 0)
33364 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33365 else if (strcmp ("spread", p) == 0)
33366 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33367 else
33368 goto invalid_kind;
33369 }
33370 else
33371 goto invalid_kind;
33372
33373 cp_lexer_consume_token (parser->lexer);
33374 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33375 goto resync_fail;
33376
33377 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33378 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33379 location);
33380 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33381 OMP_CLAUSE_CHAIN (c) = list;
33382 return c;
33383
33384 invalid_kind:
33385 cp_parser_error (parser, "invalid depend kind");
33386 resync_fail:
33387 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33388 /*or_comma=*/false,
33389 /*consume_paren=*/true);
33390 return list;
33391 }
33392
33393 /* OpenACC:
33394 async [( int-expr )] */
33395
33396 static tree
33397 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33398 {
33399 tree c, t;
33400 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33401
33402 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33403
33404 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33405 {
33406 matching_parens parens;
33407 parens.consume_open (parser);
33408
33409 t = cp_parser_expression (parser);
33410 if (t == error_mark_node
33411 || !parens.require_close (parser))
33412 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33413 /*or_comma=*/false,
33414 /*consume_paren=*/true);
33415 }
33416
33417 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33418
33419 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33420 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33421 OMP_CLAUSE_CHAIN (c) = list;
33422 list = c;
33423
33424 return list;
33425 }
33426
33427 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33428 is a bitmask in MASK. Return the list of clauses found. */
33429
33430 static tree
33431 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33432 const char *where, cp_token *pragma_tok,
33433 bool finish_p = true)
33434 {
33435 tree clauses = NULL;
33436 bool first = true;
33437
33438 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33439 {
33440 location_t here;
33441 pragma_omp_clause c_kind;
33442 omp_clause_code code;
33443 const char *c_name;
33444 tree prev = clauses;
33445
33446 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33447 cp_lexer_consume_token (parser->lexer);
33448
33449 here = cp_lexer_peek_token (parser->lexer)->location;
33450 c_kind = cp_parser_omp_clause_name (parser);
33451
33452 switch (c_kind)
33453 {
33454 case PRAGMA_OACC_CLAUSE_ASYNC:
33455 clauses = cp_parser_oacc_clause_async (parser, clauses);
33456 c_name = "async";
33457 break;
33458 case PRAGMA_OACC_CLAUSE_AUTO:
33459 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33460 clauses, here);
33461 c_name = "auto";
33462 break;
33463 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33464 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33465 c_name = "collapse";
33466 break;
33467 case PRAGMA_OACC_CLAUSE_COPY:
33468 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33469 c_name = "copy";
33470 break;
33471 case PRAGMA_OACC_CLAUSE_COPYIN:
33472 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33473 c_name = "copyin";
33474 break;
33475 case PRAGMA_OACC_CLAUSE_COPYOUT:
33476 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33477 c_name = "copyout";
33478 break;
33479 case PRAGMA_OACC_CLAUSE_CREATE:
33480 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33481 c_name = "create";
33482 break;
33483 case PRAGMA_OACC_CLAUSE_DELETE:
33484 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33485 c_name = "delete";
33486 break;
33487 case PRAGMA_OMP_CLAUSE_DEFAULT:
33488 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33489 c_name = "default";
33490 break;
33491 case PRAGMA_OACC_CLAUSE_DEVICE:
33492 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33493 c_name = "device";
33494 break;
33495 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33496 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33497 c_name = "deviceptr";
33498 break;
33499 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33500 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33501 c_name = "device_resident";
33502 break;
33503 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33504 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33505 clauses);
33506 c_name = "firstprivate";
33507 break;
33508 case PRAGMA_OACC_CLAUSE_GANG:
33509 c_name = "gang";
33510 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33511 c_name, clauses);
33512 break;
33513 case PRAGMA_OACC_CLAUSE_HOST:
33514 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33515 c_name = "host";
33516 break;
33517 case PRAGMA_OACC_CLAUSE_IF:
33518 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33519 c_name = "if";
33520 break;
33521 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33522 clauses = cp_parser_oacc_simple_clause (parser,
33523 OMP_CLAUSE_INDEPENDENT,
33524 clauses, here);
33525 c_name = "independent";
33526 break;
33527 case PRAGMA_OACC_CLAUSE_LINK:
33528 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33529 c_name = "link";
33530 break;
33531 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33532 code = OMP_CLAUSE_NUM_GANGS;
33533 c_name = "num_gangs";
33534 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33535 clauses);
33536 break;
33537 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33538 c_name = "num_workers";
33539 code = OMP_CLAUSE_NUM_WORKERS;
33540 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33541 clauses);
33542 break;
33543 case PRAGMA_OACC_CLAUSE_PRESENT:
33544 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33545 c_name = "present";
33546 break;
33547 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33548 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33549 c_name = "present_or_copy";
33550 break;
33551 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33552 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33553 c_name = "present_or_copyin";
33554 break;
33555 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33556 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33557 c_name = "present_or_copyout";
33558 break;
33559 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33560 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33561 c_name = "present_or_create";
33562 break;
33563 case PRAGMA_OACC_CLAUSE_PRIVATE:
33564 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33565 clauses);
33566 c_name = "private";
33567 break;
33568 case PRAGMA_OACC_CLAUSE_REDUCTION:
33569 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33570 c_name = "reduction";
33571 break;
33572 case PRAGMA_OACC_CLAUSE_SELF:
33573 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33574 c_name = "self";
33575 break;
33576 case PRAGMA_OACC_CLAUSE_SEQ:
33577 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33578 clauses, here);
33579 c_name = "seq";
33580 break;
33581 case PRAGMA_OACC_CLAUSE_TILE:
33582 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33583 c_name = "tile";
33584 break;
33585 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33586 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33587 clauses);
33588 c_name = "use_device";
33589 break;
33590 case PRAGMA_OACC_CLAUSE_VECTOR:
33591 c_name = "vector";
33592 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33593 c_name, clauses);
33594 break;
33595 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33596 c_name = "vector_length";
33597 code = OMP_CLAUSE_VECTOR_LENGTH;
33598 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33599 clauses);
33600 break;
33601 case PRAGMA_OACC_CLAUSE_WAIT:
33602 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33603 c_name = "wait";
33604 break;
33605 case PRAGMA_OACC_CLAUSE_WORKER:
33606 c_name = "worker";
33607 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33608 c_name, clauses);
33609 break;
33610 default:
33611 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33612 goto saw_error;
33613 }
33614
33615 first = false;
33616
33617 if (((mask >> c_kind) & 1) == 0)
33618 {
33619 /* Remove the invalid clause(s) from the list to avoid
33620 confusing the rest of the compiler. */
33621 clauses = prev;
33622 error_at (here, "%qs is not valid for %qs", c_name, where);
33623 }
33624 }
33625
33626 saw_error:
33627 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33628
33629 if (finish_p)
33630 return finish_omp_clauses (clauses, C_ORT_ACC);
33631
33632 return clauses;
33633 }
33634
33635 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33636 is a bitmask in MASK. Return the list of clauses found; the result
33637 of clause default goes in *pdefault. */
33638
33639 static tree
33640 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33641 const char *where, cp_token *pragma_tok,
33642 bool finish_p = true)
33643 {
33644 tree clauses = NULL;
33645 bool first = true;
33646 cp_token *token = NULL;
33647
33648 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33649 {
33650 pragma_omp_clause c_kind;
33651 const char *c_name;
33652 tree prev = clauses;
33653
33654 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33655 cp_lexer_consume_token (parser->lexer);
33656
33657 token = cp_lexer_peek_token (parser->lexer);
33658 c_kind = cp_parser_omp_clause_name (parser);
33659
33660 switch (c_kind)
33661 {
33662 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33663 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33664 token->location);
33665 c_name = "collapse";
33666 break;
33667 case PRAGMA_OMP_CLAUSE_COPYIN:
33668 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33669 c_name = "copyin";
33670 break;
33671 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33672 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33673 clauses);
33674 c_name = "copyprivate";
33675 break;
33676 case PRAGMA_OMP_CLAUSE_DEFAULT:
33677 clauses = cp_parser_omp_clause_default (parser, clauses,
33678 token->location, false);
33679 c_name = "default";
33680 break;
33681 case PRAGMA_OMP_CLAUSE_FINAL:
33682 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33683 c_name = "final";
33684 break;
33685 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33686 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33687 clauses);
33688 c_name = "firstprivate";
33689 break;
33690 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33691 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33692 token->location);
33693 c_name = "grainsize";
33694 break;
33695 case PRAGMA_OMP_CLAUSE_HINT:
33696 clauses = cp_parser_omp_clause_hint (parser, clauses,
33697 token->location);
33698 c_name = "hint";
33699 break;
33700 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33701 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33702 token->location);
33703 c_name = "defaultmap";
33704 break;
33705 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33706 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33707 clauses);
33708 c_name = "use_device_ptr";
33709 break;
33710 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33711 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33712 clauses);
33713 c_name = "is_device_ptr";
33714 break;
33715 case PRAGMA_OMP_CLAUSE_IF:
33716 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33717 true);
33718 c_name = "if";
33719 break;
33720 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33721 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33722 clauses);
33723 c_name = "lastprivate";
33724 break;
33725 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33726 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33727 token->location);
33728 c_name = "mergeable";
33729 break;
33730 case PRAGMA_OMP_CLAUSE_NOWAIT:
33731 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33732 c_name = "nowait";
33733 break;
33734 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33735 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33736 token->location);
33737 c_name = "num_tasks";
33738 break;
33739 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33740 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33741 token->location);
33742 c_name = "num_threads";
33743 break;
33744 case PRAGMA_OMP_CLAUSE_ORDERED:
33745 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33746 token->location);
33747 c_name = "ordered";
33748 break;
33749 case PRAGMA_OMP_CLAUSE_PRIORITY:
33750 clauses = cp_parser_omp_clause_priority (parser, clauses,
33751 token->location);
33752 c_name = "priority";
33753 break;
33754 case PRAGMA_OMP_CLAUSE_PRIVATE:
33755 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33756 clauses);
33757 c_name = "private";
33758 break;
33759 case PRAGMA_OMP_CLAUSE_REDUCTION:
33760 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33761 c_name = "reduction";
33762 break;
33763 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33764 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33765 token->location);
33766 c_name = "schedule";
33767 break;
33768 case PRAGMA_OMP_CLAUSE_SHARED:
33769 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33770 clauses);
33771 c_name = "shared";
33772 break;
33773 case PRAGMA_OMP_CLAUSE_UNTIED:
33774 clauses = cp_parser_omp_clause_untied (parser, clauses,
33775 token->location);
33776 c_name = "untied";
33777 break;
33778 case PRAGMA_OMP_CLAUSE_INBRANCH:
33779 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33780 clauses, token->location);
33781 c_name = "inbranch";
33782 break;
33783 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33784 clauses = cp_parser_omp_clause_branch (parser,
33785 OMP_CLAUSE_NOTINBRANCH,
33786 clauses, token->location);
33787 c_name = "notinbranch";
33788 break;
33789 case PRAGMA_OMP_CLAUSE_PARALLEL:
33790 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33791 clauses, token->location);
33792 c_name = "parallel";
33793 if (!first)
33794 {
33795 clause_not_first:
33796 error_at (token->location, "%qs must be the first clause of %qs",
33797 c_name, where);
33798 clauses = prev;
33799 }
33800 break;
33801 case PRAGMA_OMP_CLAUSE_FOR:
33802 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33803 clauses, token->location);
33804 c_name = "for";
33805 if (!first)
33806 goto clause_not_first;
33807 break;
33808 case PRAGMA_OMP_CLAUSE_SECTIONS:
33809 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33810 clauses, token->location);
33811 c_name = "sections";
33812 if (!first)
33813 goto clause_not_first;
33814 break;
33815 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33816 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33817 clauses, token->location);
33818 c_name = "taskgroup";
33819 if (!first)
33820 goto clause_not_first;
33821 break;
33822 case PRAGMA_OMP_CLAUSE_LINK:
33823 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33824 c_name = "to";
33825 break;
33826 case PRAGMA_OMP_CLAUSE_TO:
33827 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33828 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33829 clauses);
33830 else
33831 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33832 c_name = "to";
33833 break;
33834 case PRAGMA_OMP_CLAUSE_FROM:
33835 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33836 c_name = "from";
33837 break;
33838 case PRAGMA_OMP_CLAUSE_UNIFORM:
33839 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33840 clauses);
33841 c_name = "uniform";
33842 break;
33843 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33844 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33845 token->location);
33846 c_name = "num_teams";
33847 break;
33848 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33849 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33850 token->location);
33851 c_name = "thread_limit";
33852 break;
33853 case PRAGMA_OMP_CLAUSE_ALIGNED:
33854 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33855 c_name = "aligned";
33856 break;
33857 case PRAGMA_OMP_CLAUSE_LINEAR:
33858 {
33859 bool declare_simd = false;
33860 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33861 declare_simd = true;
33862 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
33863 }
33864 c_name = "linear";
33865 break;
33866 case PRAGMA_OMP_CLAUSE_DEPEND:
33867 clauses = cp_parser_omp_clause_depend (parser, clauses,
33868 token->location);
33869 c_name = "depend";
33870 break;
33871 case PRAGMA_OMP_CLAUSE_MAP:
33872 clauses = cp_parser_omp_clause_map (parser, clauses);
33873 c_name = "map";
33874 break;
33875 case PRAGMA_OMP_CLAUSE_DEVICE:
33876 clauses = cp_parser_omp_clause_device (parser, clauses,
33877 token->location);
33878 c_name = "device";
33879 break;
33880 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33881 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33882 token->location);
33883 c_name = "dist_schedule";
33884 break;
33885 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33886 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33887 token->location);
33888 c_name = "proc_bind";
33889 break;
33890 case PRAGMA_OMP_CLAUSE_SAFELEN:
33891 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33892 token->location);
33893 c_name = "safelen";
33894 break;
33895 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33896 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33897 token->location);
33898 c_name = "simdlen";
33899 break;
33900 case PRAGMA_OMP_CLAUSE_NOGROUP:
33901 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33902 token->location);
33903 c_name = "nogroup";
33904 break;
33905 case PRAGMA_OMP_CLAUSE_THREADS:
33906 clauses
33907 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33908 clauses, token->location);
33909 c_name = "threads";
33910 break;
33911 case PRAGMA_OMP_CLAUSE_SIMD:
33912 clauses
33913 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33914 clauses, token->location);
33915 c_name = "simd";
33916 break;
33917 default:
33918 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33919 goto saw_error;
33920 }
33921
33922 first = false;
33923
33924 if (((mask >> c_kind) & 1) == 0)
33925 {
33926 /* Remove the invalid clause(s) from the list to avoid
33927 confusing the rest of the compiler. */
33928 clauses = prev;
33929 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33930 }
33931 }
33932 saw_error:
33933 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33934 if (finish_p)
33935 {
33936 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33937 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33938 else
33939 return finish_omp_clauses (clauses, C_ORT_OMP);
33940 }
33941 return clauses;
33942 }
33943
33944 /* OpenMP 2.5:
33945 structured-block:
33946 statement
33947
33948 In practice, we're also interested in adding the statement to an
33949 outer node. So it is convenient if we work around the fact that
33950 cp_parser_statement calls add_stmt. */
33951
33952 static unsigned
33953 cp_parser_begin_omp_structured_block (cp_parser *parser)
33954 {
33955 unsigned save = parser->in_statement;
33956
33957 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33958 This preserves the "not within loop or switch" style error messages
33959 for nonsense cases like
33960 void foo() {
33961 #pragma omp single
33962 break;
33963 }
33964 */
33965 if (parser->in_statement)
33966 parser->in_statement = IN_OMP_BLOCK;
33967
33968 return save;
33969 }
33970
33971 static void
33972 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33973 {
33974 parser->in_statement = save;
33975 }
33976
33977 static tree
33978 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33979 {
33980 tree stmt = begin_omp_structured_block ();
33981 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33982
33983 cp_parser_statement (parser, NULL_TREE, false, if_p);
33984
33985 cp_parser_end_omp_structured_block (parser, save);
33986 return finish_omp_structured_block (stmt);
33987 }
33988
33989 /* OpenMP 2.5:
33990 # pragma omp atomic new-line
33991 expression-stmt
33992
33993 expression-stmt:
33994 x binop= expr | x++ | ++x | x-- | --x
33995 binop:
33996 +, *, -, /, &, ^, |, <<, >>
33997
33998 where x is an lvalue expression with scalar type.
33999
34000 OpenMP 3.1:
34001 # pragma omp atomic new-line
34002 update-stmt
34003
34004 # pragma omp atomic read new-line
34005 read-stmt
34006
34007 # pragma omp atomic write new-line
34008 write-stmt
34009
34010 # pragma omp atomic update new-line
34011 update-stmt
34012
34013 # pragma omp atomic capture new-line
34014 capture-stmt
34015
34016 # pragma omp atomic capture new-line
34017 capture-block
34018
34019 read-stmt:
34020 v = x
34021 write-stmt:
34022 x = expr
34023 update-stmt:
34024 expression-stmt | x = x binop expr
34025 capture-stmt:
34026 v = expression-stmt
34027 capture-block:
34028 { v = x; update-stmt; } | { update-stmt; v = x; }
34029
34030 OpenMP 4.0:
34031 update-stmt:
34032 expression-stmt | x = x binop expr | x = expr binop x
34033 capture-stmt:
34034 v = update-stmt
34035 capture-block:
34036 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34037
34038 where x and v are lvalue expressions with scalar type. */
34039
34040 static void
34041 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34042 {
34043 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34044 tree rhs1 = NULL_TREE, orig_lhs;
34045 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34046 bool structured_block = false;
34047 bool seq_cst = false;
34048
34049 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34050 {
34051 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34052 const char *p = IDENTIFIER_POINTER (id);
34053
34054 if (!strcmp (p, "seq_cst"))
34055 {
34056 seq_cst = true;
34057 cp_lexer_consume_token (parser->lexer);
34058 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34059 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34060 cp_lexer_consume_token (parser->lexer);
34061 }
34062 }
34063 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34064 {
34065 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34066 const char *p = IDENTIFIER_POINTER (id);
34067
34068 if (!strcmp (p, "read"))
34069 code = OMP_ATOMIC_READ;
34070 else if (!strcmp (p, "write"))
34071 code = NOP_EXPR;
34072 else if (!strcmp (p, "update"))
34073 code = OMP_ATOMIC;
34074 else if (!strcmp (p, "capture"))
34075 code = OMP_ATOMIC_CAPTURE_NEW;
34076 else
34077 p = NULL;
34078 if (p)
34079 cp_lexer_consume_token (parser->lexer);
34080 }
34081 if (!seq_cst)
34082 {
34083 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34084 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34085 cp_lexer_consume_token (parser->lexer);
34086
34087 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34088 {
34089 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34090 const char *p = IDENTIFIER_POINTER (id);
34091
34092 if (!strcmp (p, "seq_cst"))
34093 {
34094 seq_cst = true;
34095 cp_lexer_consume_token (parser->lexer);
34096 }
34097 }
34098 }
34099 cp_parser_require_pragma_eol (parser, pragma_tok);
34100
34101 switch (code)
34102 {
34103 case OMP_ATOMIC_READ:
34104 case NOP_EXPR: /* atomic write */
34105 v = cp_parser_unary_expression (parser);
34106 if (v == error_mark_node)
34107 goto saw_error;
34108 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34109 goto saw_error;
34110 if (code == NOP_EXPR)
34111 lhs = cp_parser_expression (parser);
34112 else
34113 lhs = cp_parser_unary_expression (parser);
34114 if (lhs == error_mark_node)
34115 goto saw_error;
34116 if (code == NOP_EXPR)
34117 {
34118 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34119 opcode. */
34120 code = OMP_ATOMIC;
34121 rhs = lhs;
34122 lhs = v;
34123 v = NULL_TREE;
34124 }
34125 goto done;
34126 case OMP_ATOMIC_CAPTURE_NEW:
34127 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34128 {
34129 cp_lexer_consume_token (parser->lexer);
34130 structured_block = true;
34131 }
34132 else
34133 {
34134 v = cp_parser_unary_expression (parser);
34135 if (v == error_mark_node)
34136 goto saw_error;
34137 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34138 goto saw_error;
34139 }
34140 default:
34141 break;
34142 }
34143
34144 restart:
34145 lhs = cp_parser_unary_expression (parser);
34146 orig_lhs = lhs;
34147 switch (TREE_CODE (lhs))
34148 {
34149 case ERROR_MARK:
34150 goto saw_error;
34151
34152 case POSTINCREMENT_EXPR:
34153 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34154 code = OMP_ATOMIC_CAPTURE_OLD;
34155 /* FALLTHROUGH */
34156 case PREINCREMENT_EXPR:
34157 lhs = TREE_OPERAND (lhs, 0);
34158 opcode = PLUS_EXPR;
34159 rhs = integer_one_node;
34160 break;
34161
34162 case POSTDECREMENT_EXPR:
34163 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34164 code = OMP_ATOMIC_CAPTURE_OLD;
34165 /* FALLTHROUGH */
34166 case PREDECREMENT_EXPR:
34167 lhs = TREE_OPERAND (lhs, 0);
34168 opcode = MINUS_EXPR;
34169 rhs = integer_one_node;
34170 break;
34171
34172 case COMPOUND_EXPR:
34173 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34174 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34175 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34176 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34177 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34178 (TREE_OPERAND (lhs, 1), 0), 0)))
34179 == BOOLEAN_TYPE)
34180 /* Undo effects of boolean_increment for post {in,de}crement. */
34181 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34182 /* FALLTHRU */
34183 case MODIFY_EXPR:
34184 if (TREE_CODE (lhs) == MODIFY_EXPR
34185 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34186 {
34187 /* Undo effects of boolean_increment. */
34188 if (integer_onep (TREE_OPERAND (lhs, 1)))
34189 {
34190 /* This is pre or post increment. */
34191 rhs = TREE_OPERAND (lhs, 1);
34192 lhs = TREE_OPERAND (lhs, 0);
34193 opcode = NOP_EXPR;
34194 if (code == OMP_ATOMIC_CAPTURE_NEW
34195 && !structured_block
34196 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34197 code = OMP_ATOMIC_CAPTURE_OLD;
34198 break;
34199 }
34200 }
34201 /* FALLTHRU */
34202 default:
34203 switch (cp_lexer_peek_token (parser->lexer)->type)
34204 {
34205 case CPP_MULT_EQ:
34206 opcode = MULT_EXPR;
34207 break;
34208 case CPP_DIV_EQ:
34209 opcode = TRUNC_DIV_EXPR;
34210 break;
34211 case CPP_PLUS_EQ:
34212 opcode = PLUS_EXPR;
34213 break;
34214 case CPP_MINUS_EQ:
34215 opcode = MINUS_EXPR;
34216 break;
34217 case CPP_LSHIFT_EQ:
34218 opcode = LSHIFT_EXPR;
34219 break;
34220 case CPP_RSHIFT_EQ:
34221 opcode = RSHIFT_EXPR;
34222 break;
34223 case CPP_AND_EQ:
34224 opcode = BIT_AND_EXPR;
34225 break;
34226 case CPP_OR_EQ:
34227 opcode = BIT_IOR_EXPR;
34228 break;
34229 case CPP_XOR_EQ:
34230 opcode = BIT_XOR_EXPR;
34231 break;
34232 case CPP_EQ:
34233 enum cp_parser_prec oprec;
34234 cp_token *token;
34235 cp_lexer_consume_token (parser->lexer);
34236 cp_parser_parse_tentatively (parser);
34237 rhs1 = cp_parser_simple_cast_expression (parser);
34238 if (rhs1 == error_mark_node)
34239 {
34240 cp_parser_abort_tentative_parse (parser);
34241 cp_parser_simple_cast_expression (parser);
34242 goto saw_error;
34243 }
34244 token = cp_lexer_peek_token (parser->lexer);
34245 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34246 {
34247 cp_parser_abort_tentative_parse (parser);
34248 cp_parser_parse_tentatively (parser);
34249 rhs = cp_parser_binary_expression (parser, false, true,
34250 PREC_NOT_OPERATOR, NULL);
34251 if (rhs == error_mark_node)
34252 {
34253 cp_parser_abort_tentative_parse (parser);
34254 cp_parser_binary_expression (parser, false, true,
34255 PREC_NOT_OPERATOR, NULL);
34256 goto saw_error;
34257 }
34258 switch (TREE_CODE (rhs))
34259 {
34260 case MULT_EXPR:
34261 case TRUNC_DIV_EXPR:
34262 case RDIV_EXPR:
34263 case PLUS_EXPR:
34264 case MINUS_EXPR:
34265 case LSHIFT_EXPR:
34266 case RSHIFT_EXPR:
34267 case BIT_AND_EXPR:
34268 case BIT_IOR_EXPR:
34269 case BIT_XOR_EXPR:
34270 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34271 {
34272 if (cp_parser_parse_definitely (parser))
34273 {
34274 opcode = TREE_CODE (rhs);
34275 rhs1 = TREE_OPERAND (rhs, 0);
34276 rhs = TREE_OPERAND (rhs, 1);
34277 goto stmt_done;
34278 }
34279 else
34280 goto saw_error;
34281 }
34282 break;
34283 default:
34284 break;
34285 }
34286 cp_parser_abort_tentative_parse (parser);
34287 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34288 {
34289 rhs = cp_parser_expression (parser);
34290 if (rhs == error_mark_node)
34291 goto saw_error;
34292 opcode = NOP_EXPR;
34293 rhs1 = NULL_TREE;
34294 goto stmt_done;
34295 }
34296 cp_parser_error (parser,
34297 "invalid form of %<#pragma omp atomic%>");
34298 goto saw_error;
34299 }
34300 if (!cp_parser_parse_definitely (parser))
34301 goto saw_error;
34302 switch (token->type)
34303 {
34304 case CPP_SEMICOLON:
34305 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34306 {
34307 code = OMP_ATOMIC_CAPTURE_OLD;
34308 v = lhs;
34309 lhs = NULL_TREE;
34310 lhs1 = rhs1;
34311 rhs1 = NULL_TREE;
34312 cp_lexer_consume_token (parser->lexer);
34313 goto restart;
34314 }
34315 else if (structured_block)
34316 {
34317 opcode = NOP_EXPR;
34318 rhs = rhs1;
34319 rhs1 = NULL_TREE;
34320 goto stmt_done;
34321 }
34322 cp_parser_error (parser,
34323 "invalid form of %<#pragma omp atomic%>");
34324 goto saw_error;
34325 case CPP_MULT:
34326 opcode = MULT_EXPR;
34327 break;
34328 case CPP_DIV:
34329 opcode = TRUNC_DIV_EXPR;
34330 break;
34331 case CPP_PLUS:
34332 opcode = PLUS_EXPR;
34333 break;
34334 case CPP_MINUS:
34335 opcode = MINUS_EXPR;
34336 break;
34337 case CPP_LSHIFT:
34338 opcode = LSHIFT_EXPR;
34339 break;
34340 case CPP_RSHIFT:
34341 opcode = RSHIFT_EXPR;
34342 break;
34343 case CPP_AND:
34344 opcode = BIT_AND_EXPR;
34345 break;
34346 case CPP_OR:
34347 opcode = BIT_IOR_EXPR;
34348 break;
34349 case CPP_XOR:
34350 opcode = BIT_XOR_EXPR;
34351 break;
34352 default:
34353 cp_parser_error (parser,
34354 "invalid operator for %<#pragma omp atomic%>");
34355 goto saw_error;
34356 }
34357 oprec = TOKEN_PRECEDENCE (token);
34358 gcc_assert (oprec != PREC_NOT_OPERATOR);
34359 if (commutative_tree_code (opcode))
34360 oprec = (enum cp_parser_prec) (oprec - 1);
34361 cp_lexer_consume_token (parser->lexer);
34362 rhs = cp_parser_binary_expression (parser, false, false,
34363 oprec, NULL);
34364 if (rhs == error_mark_node)
34365 goto saw_error;
34366 goto stmt_done;
34367 /* FALLTHROUGH */
34368 default:
34369 cp_parser_error (parser,
34370 "invalid operator for %<#pragma omp atomic%>");
34371 goto saw_error;
34372 }
34373 cp_lexer_consume_token (parser->lexer);
34374
34375 rhs = cp_parser_expression (parser);
34376 if (rhs == error_mark_node)
34377 goto saw_error;
34378 break;
34379 }
34380 stmt_done:
34381 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34382 {
34383 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34384 goto saw_error;
34385 v = cp_parser_unary_expression (parser);
34386 if (v == error_mark_node)
34387 goto saw_error;
34388 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34389 goto saw_error;
34390 lhs1 = cp_parser_unary_expression (parser);
34391 if (lhs1 == error_mark_node)
34392 goto saw_error;
34393 }
34394 if (structured_block)
34395 {
34396 cp_parser_consume_semicolon_at_end_of_statement (parser);
34397 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34398 }
34399 done:
34400 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34401 if (!structured_block)
34402 cp_parser_consume_semicolon_at_end_of_statement (parser);
34403 return;
34404
34405 saw_error:
34406 cp_parser_skip_to_end_of_block_or_statement (parser);
34407 if (structured_block)
34408 {
34409 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34410 cp_lexer_consume_token (parser->lexer);
34411 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34412 {
34413 cp_parser_skip_to_end_of_block_or_statement (parser);
34414 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34415 cp_lexer_consume_token (parser->lexer);
34416 }
34417 }
34418 }
34419
34420
34421 /* OpenMP 2.5:
34422 # pragma omp barrier new-line */
34423
34424 static void
34425 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34426 {
34427 cp_parser_require_pragma_eol (parser, pragma_tok);
34428 finish_omp_barrier ();
34429 }
34430
34431 /* OpenMP 2.5:
34432 # pragma omp critical [(name)] new-line
34433 structured-block
34434
34435 OpenMP 4.5:
34436 # pragma omp critical [(name) [hint(expression)]] new-line
34437 structured-block */
34438
34439 #define OMP_CRITICAL_CLAUSE_MASK \
34440 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34441
34442 static tree
34443 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34444 {
34445 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34446
34447 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34448 {
34449 matching_parens parens;
34450 parens.consume_open (parser);
34451
34452 name = cp_parser_identifier (parser);
34453
34454 if (name == error_mark_node
34455 || !parens.require_close (parser))
34456 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34457 /*or_comma=*/false,
34458 /*consume_paren=*/true);
34459 if (name == error_mark_node)
34460 name = NULL;
34461
34462 clauses = cp_parser_omp_all_clauses (parser,
34463 OMP_CRITICAL_CLAUSE_MASK,
34464 "#pragma omp critical", pragma_tok);
34465 }
34466 else
34467 cp_parser_require_pragma_eol (parser, pragma_tok);
34468
34469 stmt = cp_parser_omp_structured_block (parser, if_p);
34470 return c_finish_omp_critical (input_location, stmt, name, clauses);
34471 }
34472
34473 /* OpenMP 2.5:
34474 # pragma omp flush flush-vars[opt] new-line
34475
34476 flush-vars:
34477 ( variable-list ) */
34478
34479 static void
34480 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34481 {
34482 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34483 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34484 cp_parser_require_pragma_eol (parser, pragma_tok);
34485
34486 finish_omp_flush ();
34487 }
34488
34489 /* Helper function, to parse omp for increment expression. */
34490
34491 static tree
34492 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
34493 {
34494 tree cond = cp_parser_binary_expression (parser, false, true,
34495 PREC_NOT_OPERATOR, NULL);
34496 if (cond == error_mark_node
34497 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34498 {
34499 cp_parser_skip_to_end_of_statement (parser);
34500 return error_mark_node;
34501 }
34502
34503 switch (TREE_CODE (cond))
34504 {
34505 case GT_EXPR:
34506 case GE_EXPR:
34507 case LT_EXPR:
34508 case LE_EXPR:
34509 break;
34510 case NE_EXPR:
34511 /* Fall through: OpenMP disallows NE_EXPR. */
34512 gcc_fallthrough ();
34513 default:
34514 return error_mark_node;
34515 }
34516
34517 /* If decl is an iterator, preserve LHS and RHS of the relational
34518 expr until finish_omp_for. */
34519 if (decl
34520 && (type_dependent_expression_p (decl)
34521 || CLASS_TYPE_P (TREE_TYPE (decl))))
34522 return cond;
34523
34524 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34525 TREE_CODE (cond),
34526 TREE_OPERAND (cond, 0), ERROR_MARK,
34527 TREE_OPERAND (cond, 1), ERROR_MARK,
34528 /*overload=*/NULL, tf_warning_or_error);
34529 }
34530
34531 /* Helper function, to parse omp for increment expression. */
34532
34533 static tree
34534 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34535 {
34536 cp_token *token = cp_lexer_peek_token (parser->lexer);
34537 enum tree_code op;
34538 tree lhs, rhs;
34539 cp_id_kind idk;
34540 bool decl_first;
34541
34542 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34543 {
34544 op = (token->type == CPP_PLUS_PLUS
34545 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34546 cp_lexer_consume_token (parser->lexer);
34547 lhs = cp_parser_simple_cast_expression (parser);
34548 if (lhs != decl
34549 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34550 return error_mark_node;
34551 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34552 }
34553
34554 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34555 if (lhs != decl
34556 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34557 return error_mark_node;
34558
34559 token = cp_lexer_peek_token (parser->lexer);
34560 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34561 {
34562 op = (token->type == CPP_PLUS_PLUS
34563 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34564 cp_lexer_consume_token (parser->lexer);
34565 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34566 }
34567
34568 op = cp_parser_assignment_operator_opt (parser);
34569 if (op == ERROR_MARK)
34570 return error_mark_node;
34571
34572 if (op != NOP_EXPR)
34573 {
34574 rhs = cp_parser_assignment_expression (parser);
34575 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34576 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34577 }
34578
34579 lhs = cp_parser_binary_expression (parser, false, false,
34580 PREC_ADDITIVE_EXPRESSION, NULL);
34581 token = cp_lexer_peek_token (parser->lexer);
34582 decl_first = (lhs == decl
34583 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34584 if (decl_first)
34585 lhs = NULL_TREE;
34586 if (token->type != CPP_PLUS
34587 && token->type != CPP_MINUS)
34588 return error_mark_node;
34589
34590 do
34591 {
34592 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34593 cp_lexer_consume_token (parser->lexer);
34594 rhs = cp_parser_binary_expression (parser, false, false,
34595 PREC_ADDITIVE_EXPRESSION, NULL);
34596 token = cp_lexer_peek_token (parser->lexer);
34597 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34598 {
34599 if (lhs == NULL_TREE)
34600 {
34601 if (op == PLUS_EXPR)
34602 lhs = rhs;
34603 else
34604 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34605 tf_warning_or_error);
34606 }
34607 else
34608 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34609 ERROR_MARK, NULL, tf_warning_or_error);
34610 }
34611 }
34612 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34613
34614 if (!decl_first)
34615 {
34616 if ((rhs != decl
34617 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34618 || op == MINUS_EXPR)
34619 return error_mark_node;
34620 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34621 }
34622 else
34623 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34624
34625 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34626 }
34627
34628 /* Parse the initialization statement of an OpenMP for loop.
34629
34630 Return true if the resulting construct should have an
34631 OMP_CLAUSE_PRIVATE added to it. */
34632
34633 static tree
34634 cp_parser_omp_for_loop_init (cp_parser *parser,
34635 tree &this_pre_body,
34636 vec<tree, va_gc> *for_block,
34637 tree &init,
34638 tree &orig_init,
34639 tree &decl,
34640 tree &real_decl)
34641 {
34642 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34643 return NULL_TREE;
34644
34645 tree add_private_clause = NULL_TREE;
34646
34647 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34648
34649 init-expr:
34650 var = lb
34651 integer-type var = lb
34652 random-access-iterator-type var = lb
34653 pointer-type var = lb
34654 */
34655 cp_decl_specifier_seq type_specifiers;
34656
34657 /* First, try to parse as an initialized declaration. See
34658 cp_parser_condition, from whence the bulk of this is copied. */
34659
34660 cp_parser_parse_tentatively (parser);
34661 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34662 /*is_trailing_return=*/false,
34663 &type_specifiers);
34664 if (cp_parser_parse_definitely (parser))
34665 {
34666 /* If parsing a type specifier seq succeeded, then this
34667 MUST be a initialized declaration. */
34668 tree asm_specification, attributes;
34669 cp_declarator *declarator;
34670
34671 declarator = cp_parser_declarator (parser,
34672 CP_PARSER_DECLARATOR_NAMED,
34673 /*ctor_dtor_or_conv_p=*/NULL,
34674 /*parenthesized_p=*/NULL,
34675 /*member_p=*/false,
34676 /*friend_p=*/false);
34677 attributes = cp_parser_attributes_opt (parser);
34678 asm_specification = cp_parser_asm_specification_opt (parser);
34679
34680 if (declarator == cp_error_declarator)
34681 cp_parser_skip_to_end_of_statement (parser);
34682
34683 else
34684 {
34685 tree pushed_scope, auto_node;
34686
34687 decl = start_decl (declarator, &type_specifiers,
34688 SD_INITIALIZED, attributes,
34689 /*prefix_attributes=*/NULL_TREE,
34690 &pushed_scope);
34691
34692 auto_node = type_uses_auto (TREE_TYPE (decl));
34693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34694 {
34695 if (cp_lexer_next_token_is (parser->lexer,
34696 CPP_OPEN_PAREN))
34697 error ("parenthesized initialization is not allowed in "
34698 "OpenMP %<for%> loop");
34699 else
34700 /* Trigger an error. */
34701 cp_parser_require (parser, CPP_EQ, RT_EQ);
34702
34703 init = error_mark_node;
34704 cp_parser_skip_to_end_of_statement (parser);
34705 }
34706 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34707 || type_dependent_expression_p (decl)
34708 || auto_node)
34709 {
34710 bool is_direct_init, is_non_constant_init;
34711
34712 init = cp_parser_initializer (parser,
34713 &is_direct_init,
34714 &is_non_constant_init);
34715
34716 if (auto_node)
34717 {
34718 TREE_TYPE (decl)
34719 = do_auto_deduction (TREE_TYPE (decl), init,
34720 auto_node);
34721
34722 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34723 && !type_dependent_expression_p (decl))
34724 goto non_class;
34725 }
34726
34727 cp_finish_decl (decl, init, !is_non_constant_init,
34728 asm_specification,
34729 LOOKUP_ONLYCONVERTING);
34730 orig_init = init;
34731 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34732 {
34733 vec_safe_push (for_block, this_pre_body);
34734 init = NULL_TREE;
34735 }
34736 else
34737 {
34738 init = pop_stmt_list (this_pre_body);
34739 if (init && TREE_CODE (init) == STATEMENT_LIST)
34740 {
34741 tree_stmt_iterator i = tsi_start (init);
34742 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34743 while (!tsi_end_p (i))
34744 {
34745 tree t = tsi_stmt (i);
34746 if (TREE_CODE (t) == DECL_EXPR
34747 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34748 {
34749 tsi_delink (&i);
34750 vec_safe_push (for_block, t);
34751 continue;
34752 }
34753 break;
34754 }
34755 if (tsi_one_before_end_p (i))
34756 {
34757 tree t = tsi_stmt (i);
34758 tsi_delink (&i);
34759 free_stmt_list (init);
34760 init = t;
34761 }
34762 }
34763 }
34764 this_pre_body = NULL_TREE;
34765 }
34766 else
34767 {
34768 /* Consume '='. */
34769 cp_lexer_consume_token (parser->lexer);
34770 init = cp_parser_assignment_expression (parser);
34771
34772 non_class:
34773 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34774 init = error_mark_node;
34775 else
34776 cp_finish_decl (decl, NULL_TREE,
34777 /*init_const_expr_p=*/false,
34778 asm_specification,
34779 LOOKUP_ONLYCONVERTING);
34780 }
34781
34782 if (pushed_scope)
34783 pop_scope (pushed_scope);
34784 }
34785 }
34786 else
34787 {
34788 cp_id_kind idk;
34789 /* If parsing a type specifier sequence failed, then
34790 this MUST be a simple expression. */
34791 cp_parser_parse_tentatively (parser);
34792 decl = cp_parser_primary_expression (parser, false, false,
34793 false, &idk);
34794 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34795 if (!cp_parser_error_occurred (parser)
34796 && decl
34797 && (TREE_CODE (decl) == COMPONENT_REF
34798 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34799 {
34800 cp_parser_abort_tentative_parse (parser);
34801 cp_parser_parse_tentatively (parser);
34802 cp_token *token = cp_lexer_peek_token (parser->lexer);
34803 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34804 /*check_dependency_p=*/true,
34805 /*template_p=*/NULL,
34806 /*declarator_p=*/false,
34807 /*optional_p=*/false);
34808 if (name != error_mark_node
34809 && last_tok == cp_lexer_peek_token (parser->lexer))
34810 {
34811 decl = cp_parser_lookup_name_simple (parser, name,
34812 token->location);
34813 if (TREE_CODE (decl) == FIELD_DECL)
34814 add_private_clause = omp_privatize_field (decl, false);
34815 }
34816 cp_parser_abort_tentative_parse (parser);
34817 cp_parser_parse_tentatively (parser);
34818 decl = cp_parser_primary_expression (parser, false, false,
34819 false, &idk);
34820 }
34821 if (!cp_parser_error_occurred (parser)
34822 && decl
34823 && DECL_P (decl)
34824 && CLASS_TYPE_P (TREE_TYPE (decl)))
34825 {
34826 tree rhs;
34827
34828 cp_parser_parse_definitely (parser);
34829 cp_parser_require (parser, CPP_EQ, RT_EQ);
34830 rhs = cp_parser_assignment_expression (parser);
34831 orig_init = rhs;
34832 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34833 decl, NOP_EXPR,
34834 rhs,
34835 tf_warning_or_error));
34836 if (!add_private_clause)
34837 add_private_clause = decl;
34838 }
34839 else
34840 {
34841 decl = NULL;
34842 cp_parser_abort_tentative_parse (parser);
34843 init = cp_parser_expression (parser);
34844 if (init)
34845 {
34846 if (TREE_CODE (init) == MODIFY_EXPR
34847 || TREE_CODE (init) == MODOP_EXPR)
34848 real_decl = TREE_OPERAND (init, 0);
34849 }
34850 }
34851 }
34852 return add_private_clause;
34853 }
34854
34855 /* Parse the restricted form of the for statement allowed by OpenMP. */
34856
34857 static tree
34858 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34859 tree *cclauses, bool *if_p)
34860 {
34861 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34862 tree real_decl, initv, condv, incrv, declv;
34863 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34864 location_t loc_first;
34865 bool collapse_err = false;
34866 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34867 vec<tree, va_gc> *for_block = make_tree_vector ();
34868 auto_vec<tree, 4> orig_inits;
34869 bool tiling = false;
34870
34871 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34872 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34873 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34874 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34875 {
34876 tiling = true;
34877 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34878 }
34879 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34880 && OMP_CLAUSE_ORDERED_EXPR (cl))
34881 {
34882 ordered_cl = cl;
34883 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34884 }
34885
34886 if (ordered && ordered < collapse)
34887 {
34888 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34889 "%<ordered%> clause parameter is less than %<collapse%>");
34890 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34891 = build_int_cst (NULL_TREE, collapse);
34892 ordered = collapse;
34893 }
34894 if (ordered)
34895 {
34896 for (tree *pc = &clauses; *pc; )
34897 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34898 {
34899 error_at (OMP_CLAUSE_LOCATION (*pc),
34900 "%<linear%> clause may not be specified together "
34901 "with %<ordered%> clause with a parameter");
34902 *pc = OMP_CLAUSE_CHAIN (*pc);
34903 }
34904 else
34905 pc = &OMP_CLAUSE_CHAIN (*pc);
34906 }
34907
34908 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34909 count = ordered ? ordered : collapse;
34910
34911 declv = make_tree_vec (count);
34912 initv = make_tree_vec (count);
34913 condv = make_tree_vec (count);
34914 incrv = make_tree_vec (count);
34915
34916 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34917
34918 for (i = 0; i < count; i++)
34919 {
34920 int bracecount = 0;
34921 tree add_private_clause = NULL_TREE;
34922 location_t loc;
34923
34924 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34925 {
34926 if (!collapse_err)
34927 cp_parser_error (parser, "for statement expected");
34928 return NULL;
34929 }
34930 loc = cp_lexer_consume_token (parser->lexer)->location;
34931
34932 matching_parens parens;
34933 if (!parens.require_open (parser))
34934 return NULL;
34935
34936 init = orig_init = decl = real_decl = NULL;
34937 this_pre_body = push_stmt_list ();
34938
34939 add_private_clause
34940 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
34941 init, orig_init, decl, real_decl);
34942
34943 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34944 if (this_pre_body)
34945 {
34946 this_pre_body = pop_stmt_list (this_pre_body);
34947 if (pre_body)
34948 {
34949 tree t = pre_body;
34950 pre_body = push_stmt_list ();
34951 add_stmt (t);
34952 add_stmt (this_pre_body);
34953 pre_body = pop_stmt_list (pre_body);
34954 }
34955 else
34956 pre_body = this_pre_body;
34957 }
34958
34959 if (decl)
34960 real_decl = decl;
34961 if (cclauses != NULL
34962 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34963 && real_decl != NULL_TREE)
34964 {
34965 tree *c;
34966 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34967 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34968 && OMP_CLAUSE_DECL (*c) == real_decl)
34969 {
34970 error_at (loc, "iteration variable %qD"
34971 " should not be firstprivate", real_decl);
34972 *c = OMP_CLAUSE_CHAIN (*c);
34973 }
34974 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34975 && OMP_CLAUSE_DECL (*c) == real_decl)
34976 {
34977 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34978 tree l = *c;
34979 *c = OMP_CLAUSE_CHAIN (*c);
34980 if (code == OMP_SIMD)
34981 {
34982 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34983 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34984 }
34985 else
34986 {
34987 OMP_CLAUSE_CHAIN (l) = clauses;
34988 clauses = l;
34989 }
34990 add_private_clause = NULL_TREE;
34991 }
34992 else
34993 {
34994 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34995 && OMP_CLAUSE_DECL (*c) == real_decl)
34996 add_private_clause = NULL_TREE;
34997 c = &OMP_CLAUSE_CHAIN (*c);
34998 }
34999 }
35000
35001 if (add_private_clause)
35002 {
35003 tree c;
35004 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35005 {
35006 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35007 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35008 && OMP_CLAUSE_DECL (c) == decl)
35009 break;
35010 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35011 && OMP_CLAUSE_DECL (c) == decl)
35012 error_at (loc, "iteration variable %qD "
35013 "should not be firstprivate",
35014 decl);
35015 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35016 && OMP_CLAUSE_DECL (c) == decl)
35017 error_at (loc, "iteration variable %qD should not be reduction",
35018 decl);
35019 }
35020 if (c == NULL)
35021 {
35022 if (code != OMP_SIMD)
35023 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35024 else if (collapse == 1)
35025 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35026 else
35027 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35028 OMP_CLAUSE_DECL (c) = add_private_clause;
35029 c = finish_omp_clauses (c, C_ORT_OMP);
35030 if (c)
35031 {
35032 OMP_CLAUSE_CHAIN (c) = clauses;
35033 clauses = c;
35034 /* For linear, signal that we need to fill up
35035 the so far unknown linear step. */
35036 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35037 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35038 }
35039 }
35040 }
35041
35042 cond = NULL;
35043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35044 cond = cp_parser_omp_for_cond (parser, decl);
35045 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35046
35047 incr = NULL;
35048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35049 {
35050 /* If decl is an iterator, preserve the operator on decl
35051 until finish_omp_for. */
35052 if (real_decl
35053 && ((processing_template_decl
35054 && (TREE_TYPE (real_decl) == NULL_TREE
35055 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35056 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35057 incr = cp_parser_omp_for_incr (parser, real_decl);
35058 else
35059 incr = cp_parser_expression (parser);
35060 if (!EXPR_HAS_LOCATION (incr))
35061 protected_set_expr_location (incr, input_location);
35062 }
35063
35064 if (!parens.require_close (parser))
35065 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35066 /*or_comma=*/false,
35067 /*consume_paren=*/true);
35068
35069 TREE_VEC_ELT (declv, i) = decl;
35070 TREE_VEC_ELT (initv, i) = init;
35071 TREE_VEC_ELT (condv, i) = cond;
35072 TREE_VEC_ELT (incrv, i) = incr;
35073 if (orig_init)
35074 {
35075 orig_inits.safe_grow_cleared (i + 1);
35076 orig_inits[i] = orig_init;
35077 }
35078
35079 if (i == count - 1)
35080 break;
35081
35082 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35083 in between the collapsed for loops to be still considered perfectly
35084 nested. Hopefully the final version clarifies this.
35085 For now handle (multiple) {'s and empty statements. */
35086 cp_parser_parse_tentatively (parser);
35087 for (;;)
35088 {
35089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35090 break;
35091 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35092 {
35093 cp_lexer_consume_token (parser->lexer);
35094 bracecount++;
35095 }
35096 else if (bracecount
35097 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35098 cp_lexer_consume_token (parser->lexer);
35099 else
35100 {
35101 loc = cp_lexer_peek_token (parser->lexer)->location;
35102 error_at (loc, "not enough for loops to collapse");
35103 collapse_err = true;
35104 cp_parser_abort_tentative_parse (parser);
35105 declv = NULL_TREE;
35106 break;
35107 }
35108 }
35109
35110 if (declv)
35111 {
35112 cp_parser_parse_definitely (parser);
35113 nbraces += bracecount;
35114 }
35115 }
35116
35117 if (nbraces)
35118 if_p = NULL;
35119
35120 /* Note that we saved the original contents of this flag when we entered
35121 the structured block, and so we don't need to re-save it here. */
35122 parser->in_statement = IN_OMP_FOR;
35123
35124 /* Note that the grammar doesn't call for a structured block here,
35125 though the loop as a whole is a structured block. */
35126 body = push_stmt_list ();
35127 cp_parser_statement (parser, NULL_TREE, false, if_p);
35128 body = pop_stmt_list (body);
35129
35130 if (declv == NULL_TREE)
35131 ret = NULL_TREE;
35132 else
35133 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35134 body, pre_body, &orig_inits, clauses);
35135
35136 while (nbraces)
35137 {
35138 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35139 {
35140 cp_lexer_consume_token (parser->lexer);
35141 nbraces--;
35142 }
35143 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35144 cp_lexer_consume_token (parser->lexer);
35145 else
35146 {
35147 if (!collapse_err)
35148 {
35149 error_at (cp_lexer_peek_token (parser->lexer)->location,
35150 "collapsed loops not perfectly nested");
35151 }
35152 collapse_err = true;
35153 cp_parser_statement_seq_opt (parser, NULL);
35154 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35155 break;
35156 }
35157 }
35158
35159 while (!for_block->is_empty ())
35160 {
35161 tree t = for_block->pop ();
35162 if (TREE_CODE (t) == STATEMENT_LIST)
35163 add_stmt (pop_stmt_list (t));
35164 else
35165 add_stmt (t);
35166 }
35167 release_tree_vector (for_block);
35168
35169 return ret;
35170 }
35171
35172 /* Helper function for OpenMP parsing, split clauses and call
35173 finish_omp_clauses on each of the set of clauses afterwards. */
35174
35175 static void
35176 cp_omp_split_clauses (location_t loc, enum tree_code code,
35177 omp_clause_mask mask, tree clauses, tree *cclauses)
35178 {
35179 int i;
35180 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35181 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35182 if (cclauses[i])
35183 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35184 }
35185
35186 /* OpenMP 4.0:
35187 #pragma omp simd simd-clause[optseq] new-line
35188 for-loop */
35189
35190 #define OMP_SIMD_CLAUSE_MASK \
35191 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35199
35200 static tree
35201 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35202 char *p_name, omp_clause_mask mask, tree *cclauses,
35203 bool *if_p)
35204 {
35205 tree clauses, sb, ret;
35206 unsigned int save;
35207 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35208
35209 strcat (p_name, " simd");
35210 mask |= OMP_SIMD_CLAUSE_MASK;
35211
35212 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35213 cclauses == NULL);
35214 if (cclauses)
35215 {
35216 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35217 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35218 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35219 OMP_CLAUSE_ORDERED);
35220 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35221 {
35222 error_at (OMP_CLAUSE_LOCATION (c),
35223 "%<ordered%> clause with parameter may not be specified "
35224 "on %qs construct", p_name);
35225 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35226 }
35227 }
35228
35229 sb = begin_omp_structured_block ();
35230 save = cp_parser_begin_omp_structured_block (parser);
35231
35232 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35233
35234 cp_parser_end_omp_structured_block (parser, save);
35235 add_stmt (finish_omp_structured_block (sb));
35236
35237 return ret;
35238 }
35239
35240 /* OpenMP 2.5:
35241 #pragma omp for for-clause[optseq] new-line
35242 for-loop
35243
35244 OpenMP 4.0:
35245 #pragma omp for simd for-simd-clause[optseq] new-line
35246 for-loop */
35247
35248 #define OMP_FOR_CLAUSE_MASK \
35249 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35258
35259 static tree
35260 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35261 char *p_name, omp_clause_mask mask, tree *cclauses,
35262 bool *if_p)
35263 {
35264 tree clauses, sb, ret;
35265 unsigned int save;
35266 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35267
35268 strcat (p_name, " for");
35269 mask |= OMP_FOR_CLAUSE_MASK;
35270 /* parallel for{, simd} disallows nowait clause, but for
35271 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35272 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35273 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35274 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35275 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35276 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35277
35278 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35279 {
35280 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35281 const char *p = IDENTIFIER_POINTER (id);
35282
35283 if (strcmp (p, "simd") == 0)
35284 {
35285 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35286 if (cclauses == NULL)
35287 cclauses = cclauses_buf;
35288
35289 cp_lexer_consume_token (parser->lexer);
35290 if (!flag_openmp) /* flag_openmp_simd */
35291 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35292 cclauses, if_p);
35293 sb = begin_omp_structured_block ();
35294 save = cp_parser_begin_omp_structured_block (parser);
35295 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35296 cclauses, if_p);
35297 cp_parser_end_omp_structured_block (parser, save);
35298 tree body = finish_omp_structured_block (sb);
35299 if (ret == NULL)
35300 return ret;
35301 ret = make_node (OMP_FOR);
35302 TREE_TYPE (ret) = void_type_node;
35303 OMP_FOR_BODY (ret) = body;
35304 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35305 SET_EXPR_LOCATION (ret, loc);
35306 add_stmt (ret);
35307 return ret;
35308 }
35309 }
35310 if (!flag_openmp) /* flag_openmp_simd */
35311 {
35312 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35313 return NULL_TREE;
35314 }
35315
35316 /* Composite distribute parallel for disallows linear clause. */
35317 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35318 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35319
35320 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35321 cclauses == NULL);
35322 if (cclauses)
35323 {
35324 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35325 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35326 }
35327
35328 sb = begin_omp_structured_block ();
35329 save = cp_parser_begin_omp_structured_block (parser);
35330
35331 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35332
35333 cp_parser_end_omp_structured_block (parser, save);
35334 add_stmt (finish_omp_structured_block (sb));
35335
35336 return ret;
35337 }
35338
35339 /* OpenMP 2.5:
35340 # pragma omp master new-line
35341 structured-block */
35342
35343 static tree
35344 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35345 {
35346 cp_parser_require_pragma_eol (parser, pragma_tok);
35347 return c_finish_omp_master (input_location,
35348 cp_parser_omp_structured_block (parser, if_p));
35349 }
35350
35351 /* OpenMP 2.5:
35352 # pragma omp ordered new-line
35353 structured-block
35354
35355 OpenMP 4.5:
35356 # pragma omp ordered ordered-clauses new-line
35357 structured-block */
35358
35359 #define OMP_ORDERED_CLAUSE_MASK \
35360 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35362
35363 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35364 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35365
35366 static bool
35367 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35368 enum pragma_context context, bool *if_p)
35369 {
35370 location_t loc = pragma_tok->location;
35371
35372 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35373 {
35374 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35375 const char *p = IDENTIFIER_POINTER (id);
35376
35377 if (strcmp (p, "depend") == 0)
35378 {
35379 if (!flag_openmp) /* flag_openmp_simd */
35380 {
35381 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35382 return false;
35383 }
35384 if (context == pragma_stmt)
35385 {
35386 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35387 "%<depend%> clause may only be used in compound "
35388 "statements");
35389 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35390 return false;
35391 }
35392 tree clauses
35393 = cp_parser_omp_all_clauses (parser,
35394 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35395 "#pragma omp ordered", pragma_tok);
35396 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35397 return false;
35398 }
35399 }
35400
35401 tree clauses
35402 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35403 "#pragma omp ordered", pragma_tok);
35404
35405 if (!flag_openmp /* flag_openmp_simd */
35406 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35407 return false;
35408
35409 c_finish_omp_ordered (loc, clauses,
35410 cp_parser_omp_structured_block (parser, if_p));
35411 return true;
35412 }
35413
35414 /* OpenMP 2.5:
35415
35416 section-scope:
35417 { section-sequence }
35418
35419 section-sequence:
35420 section-directive[opt] structured-block
35421 section-sequence section-directive structured-block */
35422
35423 static tree
35424 cp_parser_omp_sections_scope (cp_parser *parser)
35425 {
35426 tree stmt, substmt;
35427 bool error_suppress = false;
35428 cp_token *tok;
35429
35430 matching_braces braces;
35431 if (!braces.require_open (parser))
35432 return NULL_TREE;
35433
35434 stmt = push_stmt_list ();
35435
35436 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35437 != PRAGMA_OMP_SECTION)
35438 {
35439 substmt = cp_parser_omp_structured_block (parser, NULL);
35440 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35441 add_stmt (substmt);
35442 }
35443
35444 while (1)
35445 {
35446 tok = cp_lexer_peek_token (parser->lexer);
35447 if (tok->type == CPP_CLOSE_BRACE)
35448 break;
35449 if (tok->type == CPP_EOF)
35450 break;
35451
35452 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35453 {
35454 cp_lexer_consume_token (parser->lexer);
35455 cp_parser_require_pragma_eol (parser, tok);
35456 error_suppress = false;
35457 }
35458 else if (!error_suppress)
35459 {
35460 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35461 error_suppress = true;
35462 }
35463
35464 substmt = cp_parser_omp_structured_block (parser, NULL);
35465 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35466 add_stmt (substmt);
35467 }
35468 braces.require_close (parser);
35469
35470 substmt = pop_stmt_list (stmt);
35471
35472 stmt = make_node (OMP_SECTIONS);
35473 TREE_TYPE (stmt) = void_type_node;
35474 OMP_SECTIONS_BODY (stmt) = substmt;
35475
35476 add_stmt (stmt);
35477 return stmt;
35478 }
35479
35480 /* OpenMP 2.5:
35481 # pragma omp sections sections-clause[optseq] newline
35482 sections-scope */
35483
35484 #define OMP_SECTIONS_CLAUSE_MASK \
35485 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35490
35491 static tree
35492 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35493 char *p_name, omp_clause_mask mask, tree *cclauses)
35494 {
35495 tree clauses, ret;
35496 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35497
35498 strcat (p_name, " sections");
35499 mask |= OMP_SECTIONS_CLAUSE_MASK;
35500 if (cclauses)
35501 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35502
35503 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35504 cclauses == NULL);
35505 if (cclauses)
35506 {
35507 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35508 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35509 }
35510
35511 ret = cp_parser_omp_sections_scope (parser);
35512 if (ret)
35513 OMP_SECTIONS_CLAUSES (ret) = clauses;
35514
35515 return ret;
35516 }
35517
35518 /* OpenMP 2.5:
35519 # pragma omp parallel parallel-clause[optseq] new-line
35520 structured-block
35521 # pragma omp parallel for parallel-for-clause[optseq] new-line
35522 structured-block
35523 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35524 structured-block
35525
35526 OpenMP 4.0:
35527 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35528 structured-block */
35529
35530 #define OMP_PARALLEL_CLAUSE_MASK \
35531 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35540
35541 static tree
35542 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35543 char *p_name, omp_clause_mask mask, tree *cclauses,
35544 bool *if_p)
35545 {
35546 tree stmt, clauses, block;
35547 unsigned int save;
35548 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35549
35550 strcat (p_name, " parallel");
35551 mask |= OMP_PARALLEL_CLAUSE_MASK;
35552 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35553 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35554 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35555 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35556
35557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35558 {
35559 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35560 if (cclauses == NULL)
35561 cclauses = cclauses_buf;
35562
35563 cp_lexer_consume_token (parser->lexer);
35564 if (!flag_openmp) /* flag_openmp_simd */
35565 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35566 if_p);
35567 block = begin_omp_parallel ();
35568 save = cp_parser_begin_omp_structured_block (parser);
35569 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35570 if_p);
35571 cp_parser_end_omp_structured_block (parser, save);
35572 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35573 block);
35574 if (ret == NULL_TREE)
35575 return ret;
35576 OMP_PARALLEL_COMBINED (stmt) = 1;
35577 return stmt;
35578 }
35579 /* When combined with distribute, parallel has to be followed by for.
35580 #pragma omp target parallel is allowed though. */
35581 else if (cclauses
35582 && (mask & (OMP_CLAUSE_MASK_1
35583 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35584 {
35585 error_at (loc, "expected %<for%> after %qs", p_name);
35586 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35587 return NULL_TREE;
35588 }
35589 else if (!flag_openmp) /* flag_openmp_simd */
35590 {
35591 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35592 return NULL_TREE;
35593 }
35594 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35595 {
35596 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35597 const char *p = IDENTIFIER_POINTER (id);
35598 if (strcmp (p, "sections") == 0)
35599 {
35600 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35601 cclauses = cclauses_buf;
35602
35603 cp_lexer_consume_token (parser->lexer);
35604 block = begin_omp_parallel ();
35605 save = cp_parser_begin_omp_structured_block (parser);
35606 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35607 cp_parser_end_omp_structured_block (parser, save);
35608 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35609 block);
35610 OMP_PARALLEL_COMBINED (stmt) = 1;
35611 return stmt;
35612 }
35613 }
35614
35615 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35616 cclauses == NULL);
35617 if (cclauses)
35618 {
35619 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35620 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35621 }
35622
35623 block = begin_omp_parallel ();
35624 save = cp_parser_begin_omp_structured_block (parser);
35625 cp_parser_statement (parser, NULL_TREE, false, if_p);
35626 cp_parser_end_omp_structured_block (parser, save);
35627 stmt = finish_omp_parallel (clauses, block);
35628 return stmt;
35629 }
35630
35631 /* OpenMP 2.5:
35632 # pragma omp single single-clause[optseq] new-line
35633 structured-block */
35634
35635 #define OMP_SINGLE_CLAUSE_MASK \
35636 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35640
35641 static tree
35642 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35643 {
35644 tree stmt = make_node (OMP_SINGLE);
35645 TREE_TYPE (stmt) = void_type_node;
35646
35647 OMP_SINGLE_CLAUSES (stmt)
35648 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35649 "#pragma omp single", pragma_tok);
35650 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35651
35652 return add_stmt (stmt);
35653 }
35654
35655 /* OpenMP 3.0:
35656 # pragma omp task task-clause[optseq] new-line
35657 structured-block */
35658
35659 #define OMP_TASK_CLAUSE_MASK \
35660 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35670
35671 static tree
35672 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35673 {
35674 tree clauses, block;
35675 unsigned int save;
35676
35677 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35678 "#pragma omp task", pragma_tok);
35679 block = begin_omp_task ();
35680 save = cp_parser_begin_omp_structured_block (parser);
35681 cp_parser_statement (parser, NULL_TREE, false, if_p);
35682 cp_parser_end_omp_structured_block (parser, save);
35683 return finish_omp_task (clauses, block);
35684 }
35685
35686 /* OpenMP 3.0:
35687 # pragma omp taskwait new-line */
35688
35689 static void
35690 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35691 {
35692 cp_parser_require_pragma_eol (parser, pragma_tok);
35693 finish_omp_taskwait ();
35694 }
35695
35696 /* OpenMP 3.1:
35697 # pragma omp taskyield new-line */
35698
35699 static void
35700 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35701 {
35702 cp_parser_require_pragma_eol (parser, pragma_tok);
35703 finish_omp_taskyield ();
35704 }
35705
35706 /* OpenMP 4.0:
35707 # pragma omp taskgroup new-line
35708 structured-block */
35709
35710 static tree
35711 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35712 {
35713 cp_parser_require_pragma_eol (parser, pragma_tok);
35714 return c_finish_omp_taskgroup (input_location,
35715 cp_parser_omp_structured_block (parser,
35716 if_p));
35717 }
35718
35719
35720 /* OpenMP 2.5:
35721 # pragma omp threadprivate (variable-list) */
35722
35723 static void
35724 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35725 {
35726 tree vars;
35727
35728 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35729 cp_parser_require_pragma_eol (parser, pragma_tok);
35730
35731 finish_omp_threadprivate (vars);
35732 }
35733
35734 /* OpenMP 4.0:
35735 # pragma omp cancel cancel-clause[optseq] new-line */
35736
35737 #define OMP_CANCEL_CLAUSE_MASK \
35738 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35743
35744 static void
35745 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35746 {
35747 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35748 "#pragma omp cancel", pragma_tok);
35749 finish_omp_cancel (clauses);
35750 }
35751
35752 /* OpenMP 4.0:
35753 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35754
35755 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35756 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35760
35761 static void
35762 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35763 enum pragma_context context)
35764 {
35765 tree clauses;
35766 bool point_seen = false;
35767
35768 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35769 {
35770 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35771 const char *p = IDENTIFIER_POINTER (id);
35772
35773 if (strcmp (p, "point") == 0)
35774 {
35775 cp_lexer_consume_token (parser->lexer);
35776 point_seen = true;
35777 }
35778 }
35779 if (!point_seen)
35780 {
35781 cp_parser_error (parser, "expected %<point%>");
35782 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35783 return;
35784 }
35785
35786 if (context != pragma_compound)
35787 {
35788 if (context == pragma_stmt)
35789 error_at (pragma_tok->location,
35790 "%<#pragma %s%> may only be used in compound statements",
35791 "omp cancellation point");
35792 else
35793 cp_parser_error (parser, "expected declaration specifiers");
35794 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35795 return;
35796 }
35797
35798 clauses = cp_parser_omp_all_clauses (parser,
35799 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35800 "#pragma omp cancellation point",
35801 pragma_tok);
35802 finish_omp_cancellation_point (clauses);
35803 }
35804
35805 /* OpenMP 4.0:
35806 #pragma omp distribute distribute-clause[optseq] new-line
35807 for-loop */
35808
35809 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35810 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35815
35816 static tree
35817 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35818 char *p_name, omp_clause_mask mask, tree *cclauses,
35819 bool *if_p)
35820 {
35821 tree clauses, sb, ret;
35822 unsigned int save;
35823 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35824
35825 strcat (p_name, " distribute");
35826 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35827
35828 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35829 {
35830 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35831 const char *p = IDENTIFIER_POINTER (id);
35832 bool simd = false;
35833 bool parallel = false;
35834
35835 if (strcmp (p, "simd") == 0)
35836 simd = true;
35837 else
35838 parallel = strcmp (p, "parallel") == 0;
35839 if (parallel || simd)
35840 {
35841 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35842 if (cclauses == NULL)
35843 cclauses = cclauses_buf;
35844 cp_lexer_consume_token (parser->lexer);
35845 if (!flag_openmp) /* flag_openmp_simd */
35846 {
35847 if (simd)
35848 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35849 cclauses, if_p);
35850 else
35851 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35852 cclauses, if_p);
35853 }
35854 sb = begin_omp_structured_block ();
35855 save = cp_parser_begin_omp_structured_block (parser);
35856 if (simd)
35857 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35858 cclauses, if_p);
35859 else
35860 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35861 cclauses, if_p);
35862 cp_parser_end_omp_structured_block (parser, save);
35863 tree body = finish_omp_structured_block (sb);
35864 if (ret == NULL)
35865 return ret;
35866 ret = make_node (OMP_DISTRIBUTE);
35867 TREE_TYPE (ret) = void_type_node;
35868 OMP_FOR_BODY (ret) = body;
35869 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35870 SET_EXPR_LOCATION (ret, loc);
35871 add_stmt (ret);
35872 return ret;
35873 }
35874 }
35875 if (!flag_openmp) /* flag_openmp_simd */
35876 {
35877 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35878 return NULL_TREE;
35879 }
35880
35881 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35882 cclauses == NULL);
35883 if (cclauses)
35884 {
35885 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35886 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35887 }
35888
35889 sb = begin_omp_structured_block ();
35890 save = cp_parser_begin_omp_structured_block (parser);
35891
35892 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35893
35894 cp_parser_end_omp_structured_block (parser, save);
35895 add_stmt (finish_omp_structured_block (sb));
35896
35897 return ret;
35898 }
35899
35900 /* OpenMP 4.0:
35901 # pragma omp teams teams-clause[optseq] new-line
35902 structured-block */
35903
35904 #define OMP_TEAMS_CLAUSE_MASK \
35905 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35912
35913 static tree
35914 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35915 char *p_name, omp_clause_mask mask, tree *cclauses,
35916 bool *if_p)
35917 {
35918 tree clauses, sb, ret;
35919 unsigned int save;
35920 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35921
35922 strcat (p_name, " teams");
35923 mask |= OMP_TEAMS_CLAUSE_MASK;
35924
35925 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35926 {
35927 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35928 const char *p = IDENTIFIER_POINTER (id);
35929 if (strcmp (p, "distribute") == 0)
35930 {
35931 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35932 if (cclauses == NULL)
35933 cclauses = cclauses_buf;
35934
35935 cp_lexer_consume_token (parser->lexer);
35936 if (!flag_openmp) /* flag_openmp_simd */
35937 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35938 cclauses, if_p);
35939 sb = begin_omp_structured_block ();
35940 save = cp_parser_begin_omp_structured_block (parser);
35941 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35942 cclauses, if_p);
35943 cp_parser_end_omp_structured_block (parser, save);
35944 tree body = finish_omp_structured_block (sb);
35945 if (ret == NULL)
35946 return ret;
35947 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35948 ret = make_node (OMP_TEAMS);
35949 TREE_TYPE (ret) = void_type_node;
35950 OMP_TEAMS_CLAUSES (ret) = clauses;
35951 OMP_TEAMS_BODY (ret) = body;
35952 OMP_TEAMS_COMBINED (ret) = 1;
35953 SET_EXPR_LOCATION (ret, loc);
35954 return add_stmt (ret);
35955 }
35956 }
35957 if (!flag_openmp) /* flag_openmp_simd */
35958 {
35959 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35960 return NULL_TREE;
35961 }
35962
35963 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35964 cclauses == NULL);
35965 if (cclauses)
35966 {
35967 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35968 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35969 }
35970
35971 tree stmt = make_node (OMP_TEAMS);
35972 TREE_TYPE (stmt) = void_type_node;
35973 OMP_TEAMS_CLAUSES (stmt) = clauses;
35974 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35975 SET_EXPR_LOCATION (stmt, loc);
35976
35977 return add_stmt (stmt);
35978 }
35979
35980 /* OpenMP 4.0:
35981 # pragma omp target data target-data-clause[optseq] new-line
35982 structured-block */
35983
35984 #define OMP_TARGET_DATA_CLAUSE_MASK \
35985 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35989
35990 static tree
35991 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35992 {
35993 tree clauses
35994 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35995 "#pragma omp target data", pragma_tok);
35996 int map_seen = 0;
35997 for (tree *pc = &clauses; *pc;)
35998 {
35999 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36000 switch (OMP_CLAUSE_MAP_KIND (*pc))
36001 {
36002 case GOMP_MAP_TO:
36003 case GOMP_MAP_ALWAYS_TO:
36004 case GOMP_MAP_FROM:
36005 case GOMP_MAP_ALWAYS_FROM:
36006 case GOMP_MAP_TOFROM:
36007 case GOMP_MAP_ALWAYS_TOFROM:
36008 case GOMP_MAP_ALLOC:
36009 map_seen = 3;
36010 break;
36011 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36012 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36013 case GOMP_MAP_ALWAYS_POINTER:
36014 break;
36015 default:
36016 map_seen |= 1;
36017 error_at (OMP_CLAUSE_LOCATION (*pc),
36018 "%<#pragma omp target data%> with map-type other "
36019 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36020 "on %<map%> clause");
36021 *pc = OMP_CLAUSE_CHAIN (*pc);
36022 continue;
36023 }
36024 pc = &OMP_CLAUSE_CHAIN (*pc);
36025 }
36026
36027 if (map_seen != 3)
36028 {
36029 if (map_seen == 0)
36030 error_at (pragma_tok->location,
36031 "%<#pragma omp target data%> must contain at least "
36032 "one %<map%> clause");
36033 return NULL_TREE;
36034 }
36035
36036 tree stmt = make_node (OMP_TARGET_DATA);
36037 TREE_TYPE (stmt) = void_type_node;
36038 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36039
36040 keep_next_level (true);
36041 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36042
36043 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36044 return add_stmt (stmt);
36045 }
36046
36047 /* OpenMP 4.5:
36048 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36049 structured-block */
36050
36051 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36052 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36057
36058 static tree
36059 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36060 enum pragma_context context)
36061 {
36062 bool data_seen = false;
36063 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36064 {
36065 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36066 const char *p = IDENTIFIER_POINTER (id);
36067
36068 if (strcmp (p, "data") == 0)
36069 {
36070 cp_lexer_consume_token (parser->lexer);
36071 data_seen = true;
36072 }
36073 }
36074 if (!data_seen)
36075 {
36076 cp_parser_error (parser, "expected %<data%>");
36077 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36078 return NULL_TREE;
36079 }
36080
36081 if (context == pragma_stmt)
36082 {
36083 error_at (pragma_tok->location,
36084 "%<#pragma %s%> may only be used in compound statements",
36085 "omp target enter data");
36086 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36087 return NULL_TREE;
36088 }
36089
36090 tree clauses
36091 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36092 "#pragma omp target enter data", pragma_tok);
36093 int map_seen = 0;
36094 for (tree *pc = &clauses; *pc;)
36095 {
36096 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36097 switch (OMP_CLAUSE_MAP_KIND (*pc))
36098 {
36099 case GOMP_MAP_TO:
36100 case GOMP_MAP_ALWAYS_TO:
36101 case GOMP_MAP_ALLOC:
36102 map_seen = 3;
36103 break;
36104 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36105 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36106 case GOMP_MAP_ALWAYS_POINTER:
36107 break;
36108 default:
36109 map_seen |= 1;
36110 error_at (OMP_CLAUSE_LOCATION (*pc),
36111 "%<#pragma omp target enter data%> with map-type other "
36112 "than %<to%> or %<alloc%> on %<map%> clause");
36113 *pc = OMP_CLAUSE_CHAIN (*pc);
36114 continue;
36115 }
36116 pc = &OMP_CLAUSE_CHAIN (*pc);
36117 }
36118
36119 if (map_seen != 3)
36120 {
36121 if (map_seen == 0)
36122 error_at (pragma_tok->location,
36123 "%<#pragma omp target enter data%> must contain at least "
36124 "one %<map%> clause");
36125 return NULL_TREE;
36126 }
36127
36128 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36129 TREE_TYPE (stmt) = void_type_node;
36130 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36131 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36132 return add_stmt (stmt);
36133 }
36134
36135 /* OpenMP 4.5:
36136 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36137 structured-block */
36138
36139 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36140 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36145
36146 static tree
36147 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36148 enum pragma_context context)
36149 {
36150 bool data_seen = false;
36151 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36152 {
36153 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36154 const char *p = IDENTIFIER_POINTER (id);
36155
36156 if (strcmp (p, "data") == 0)
36157 {
36158 cp_lexer_consume_token (parser->lexer);
36159 data_seen = true;
36160 }
36161 }
36162 if (!data_seen)
36163 {
36164 cp_parser_error (parser, "expected %<data%>");
36165 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36166 return NULL_TREE;
36167 }
36168
36169 if (context == pragma_stmt)
36170 {
36171 error_at (pragma_tok->location,
36172 "%<#pragma %s%> may only be used in compound statements",
36173 "omp target exit data");
36174 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36175 return NULL_TREE;
36176 }
36177
36178 tree clauses
36179 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36180 "#pragma omp target exit data", pragma_tok);
36181 int map_seen = 0;
36182 for (tree *pc = &clauses; *pc;)
36183 {
36184 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36185 switch (OMP_CLAUSE_MAP_KIND (*pc))
36186 {
36187 case GOMP_MAP_FROM:
36188 case GOMP_MAP_ALWAYS_FROM:
36189 case GOMP_MAP_RELEASE:
36190 case GOMP_MAP_DELETE:
36191 map_seen = 3;
36192 break;
36193 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36194 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36195 case GOMP_MAP_ALWAYS_POINTER:
36196 break;
36197 default:
36198 map_seen |= 1;
36199 error_at (OMP_CLAUSE_LOCATION (*pc),
36200 "%<#pragma omp target exit data%> with map-type other "
36201 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36202 " clause");
36203 *pc = OMP_CLAUSE_CHAIN (*pc);
36204 continue;
36205 }
36206 pc = &OMP_CLAUSE_CHAIN (*pc);
36207 }
36208
36209 if (map_seen != 3)
36210 {
36211 if (map_seen == 0)
36212 error_at (pragma_tok->location,
36213 "%<#pragma omp target exit data%> must contain at least "
36214 "one %<map%> clause");
36215 return NULL_TREE;
36216 }
36217
36218 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36219 TREE_TYPE (stmt) = void_type_node;
36220 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36221 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36222 return add_stmt (stmt);
36223 }
36224
36225 /* OpenMP 4.0:
36226 # pragma omp target update target-update-clause[optseq] new-line */
36227
36228 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36229 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36235
36236 static bool
36237 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36238 enum pragma_context context)
36239 {
36240 if (context == pragma_stmt)
36241 {
36242 error_at (pragma_tok->location,
36243 "%<#pragma %s%> may only be used in compound statements",
36244 "omp target update");
36245 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36246 return false;
36247 }
36248
36249 tree clauses
36250 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36251 "#pragma omp target update", pragma_tok);
36252 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36253 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36254 {
36255 error_at (pragma_tok->location,
36256 "%<#pragma omp target update%> must contain at least one "
36257 "%<from%> or %<to%> clauses");
36258 return false;
36259 }
36260
36261 tree stmt = make_node (OMP_TARGET_UPDATE);
36262 TREE_TYPE (stmt) = void_type_node;
36263 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36264 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36265 add_stmt (stmt);
36266 return false;
36267 }
36268
36269 /* OpenMP 4.0:
36270 # pragma omp target target-clause[optseq] new-line
36271 structured-block */
36272
36273 #define OMP_TARGET_CLAUSE_MASK \
36274 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36283
36284 static bool
36285 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36286 enum pragma_context context, bool *if_p)
36287 {
36288 tree *pc = NULL, stmt;
36289
36290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36291 {
36292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36293 const char *p = IDENTIFIER_POINTER (id);
36294 enum tree_code ccode = ERROR_MARK;
36295
36296 if (strcmp (p, "teams") == 0)
36297 ccode = OMP_TEAMS;
36298 else if (strcmp (p, "parallel") == 0)
36299 ccode = OMP_PARALLEL;
36300 else if (strcmp (p, "simd") == 0)
36301 ccode = OMP_SIMD;
36302 if (ccode != ERROR_MARK)
36303 {
36304 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36305 char p_name[sizeof ("#pragma omp target teams distribute "
36306 "parallel for simd")];
36307
36308 cp_lexer_consume_token (parser->lexer);
36309 strcpy (p_name, "#pragma omp target");
36310 if (!flag_openmp) /* flag_openmp_simd */
36311 {
36312 tree stmt;
36313 switch (ccode)
36314 {
36315 case OMP_TEAMS:
36316 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36317 OMP_TARGET_CLAUSE_MASK,
36318 cclauses, if_p);
36319 break;
36320 case OMP_PARALLEL:
36321 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36322 OMP_TARGET_CLAUSE_MASK,
36323 cclauses, if_p);
36324 break;
36325 case OMP_SIMD:
36326 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36327 OMP_TARGET_CLAUSE_MASK,
36328 cclauses, if_p);
36329 break;
36330 default:
36331 gcc_unreachable ();
36332 }
36333 return stmt != NULL_TREE;
36334 }
36335 keep_next_level (true);
36336 tree sb = begin_omp_structured_block (), ret;
36337 unsigned save = cp_parser_begin_omp_structured_block (parser);
36338 switch (ccode)
36339 {
36340 case OMP_TEAMS:
36341 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36342 OMP_TARGET_CLAUSE_MASK, cclauses,
36343 if_p);
36344 break;
36345 case OMP_PARALLEL:
36346 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36347 OMP_TARGET_CLAUSE_MASK, cclauses,
36348 if_p);
36349 break;
36350 case OMP_SIMD:
36351 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36352 OMP_TARGET_CLAUSE_MASK, cclauses,
36353 if_p);
36354 break;
36355 default:
36356 gcc_unreachable ();
36357 }
36358 cp_parser_end_omp_structured_block (parser, save);
36359 tree body = finish_omp_structured_block (sb);
36360 if (ret == NULL_TREE)
36361 return false;
36362 if (ccode == OMP_TEAMS && !processing_template_decl)
36363 {
36364 /* For combined target teams, ensure the num_teams and
36365 thread_limit clause expressions are evaluated on the host,
36366 before entering the target construct. */
36367 tree c;
36368 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36369 c; c = OMP_CLAUSE_CHAIN (c))
36370 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36371 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36372 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36373 {
36374 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36375 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36376 if (expr == error_mark_node)
36377 continue;
36378 tree tmp = TARGET_EXPR_SLOT (expr);
36379 add_stmt (expr);
36380 OMP_CLAUSE_OPERAND (c, 0) = expr;
36381 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36382 OMP_CLAUSE_FIRSTPRIVATE);
36383 OMP_CLAUSE_DECL (tc) = tmp;
36384 OMP_CLAUSE_CHAIN (tc)
36385 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36386 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36387 }
36388 }
36389 tree stmt = make_node (OMP_TARGET);
36390 TREE_TYPE (stmt) = void_type_node;
36391 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36392 OMP_TARGET_BODY (stmt) = body;
36393 OMP_TARGET_COMBINED (stmt) = 1;
36394 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36395 add_stmt (stmt);
36396 pc = &OMP_TARGET_CLAUSES (stmt);
36397 goto check_clauses;
36398 }
36399 else if (!flag_openmp) /* flag_openmp_simd */
36400 {
36401 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36402 return false;
36403 }
36404 else if (strcmp (p, "data") == 0)
36405 {
36406 cp_lexer_consume_token (parser->lexer);
36407 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36408 return true;
36409 }
36410 else if (strcmp (p, "enter") == 0)
36411 {
36412 cp_lexer_consume_token (parser->lexer);
36413 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36414 return false;
36415 }
36416 else if (strcmp (p, "exit") == 0)
36417 {
36418 cp_lexer_consume_token (parser->lexer);
36419 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36420 return false;
36421 }
36422 else if (strcmp (p, "update") == 0)
36423 {
36424 cp_lexer_consume_token (parser->lexer);
36425 return cp_parser_omp_target_update (parser, pragma_tok, context);
36426 }
36427 }
36428 if (!flag_openmp) /* flag_openmp_simd */
36429 {
36430 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36431 return false;
36432 }
36433
36434 stmt = make_node (OMP_TARGET);
36435 TREE_TYPE (stmt) = void_type_node;
36436
36437 OMP_TARGET_CLAUSES (stmt)
36438 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36439 "#pragma omp target", pragma_tok);
36440 pc = &OMP_TARGET_CLAUSES (stmt);
36441 keep_next_level (true);
36442 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36443
36444 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36445 add_stmt (stmt);
36446
36447 check_clauses:
36448 while (*pc)
36449 {
36450 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36451 switch (OMP_CLAUSE_MAP_KIND (*pc))
36452 {
36453 case GOMP_MAP_TO:
36454 case GOMP_MAP_ALWAYS_TO:
36455 case GOMP_MAP_FROM:
36456 case GOMP_MAP_ALWAYS_FROM:
36457 case GOMP_MAP_TOFROM:
36458 case GOMP_MAP_ALWAYS_TOFROM:
36459 case GOMP_MAP_ALLOC:
36460 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36461 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36462 case GOMP_MAP_ALWAYS_POINTER:
36463 break;
36464 default:
36465 error_at (OMP_CLAUSE_LOCATION (*pc),
36466 "%<#pragma omp target%> with map-type other "
36467 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36468 "on %<map%> clause");
36469 *pc = OMP_CLAUSE_CHAIN (*pc);
36470 continue;
36471 }
36472 pc = &OMP_CLAUSE_CHAIN (*pc);
36473 }
36474 return true;
36475 }
36476
36477 /* OpenACC 2.0:
36478 # pragma acc cache (variable-list) new-line
36479 */
36480
36481 static tree
36482 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36483 {
36484 tree stmt, clauses;
36485
36486 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36487 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36488
36489 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36490
36491 stmt = make_node (OACC_CACHE);
36492 TREE_TYPE (stmt) = void_type_node;
36493 OACC_CACHE_CLAUSES (stmt) = clauses;
36494 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36495 add_stmt (stmt);
36496
36497 return stmt;
36498 }
36499
36500 /* OpenACC 2.0:
36501 # pragma acc data oacc-data-clause[optseq] new-line
36502 structured-block */
36503
36504 #define OACC_DATA_CLAUSE_MASK \
36505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36516
36517 static tree
36518 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36519 {
36520 tree stmt, clauses, block;
36521 unsigned int save;
36522
36523 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36524 "#pragma acc data", pragma_tok);
36525
36526 block = begin_omp_parallel ();
36527 save = cp_parser_begin_omp_structured_block (parser);
36528 cp_parser_statement (parser, NULL_TREE, false, if_p);
36529 cp_parser_end_omp_structured_block (parser, save);
36530 stmt = finish_oacc_data (clauses, block);
36531 return stmt;
36532 }
36533
36534 /* OpenACC 2.0:
36535 # pragma acc host_data <clauses> new-line
36536 structured-block */
36537
36538 #define OACC_HOST_DATA_CLAUSE_MASK \
36539 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36540
36541 static tree
36542 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36543 {
36544 tree stmt, clauses, block;
36545 unsigned int save;
36546
36547 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36548 "#pragma acc host_data", pragma_tok);
36549
36550 block = begin_omp_parallel ();
36551 save = cp_parser_begin_omp_structured_block (parser);
36552 cp_parser_statement (parser, NULL_TREE, false, if_p);
36553 cp_parser_end_omp_structured_block (parser, save);
36554 stmt = finish_oacc_host_data (clauses, block);
36555 return stmt;
36556 }
36557
36558 /* OpenACC 2.0:
36559 # pragma acc declare oacc-data-clause[optseq] new-line
36560 */
36561
36562 #define OACC_DECLARE_CLAUSE_MASK \
36563 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36575
36576 static tree
36577 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36578 {
36579 tree clauses, stmt;
36580 bool error = false;
36581
36582 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36583 "#pragma acc declare", pragma_tok, true);
36584
36585
36586 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36587 {
36588 error_at (pragma_tok->location,
36589 "no valid clauses specified in %<#pragma acc declare%>");
36590 return NULL_TREE;
36591 }
36592
36593 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36594 {
36595 location_t loc = OMP_CLAUSE_LOCATION (t);
36596 tree decl = OMP_CLAUSE_DECL (t);
36597 if (!DECL_P (decl))
36598 {
36599 error_at (loc, "array section in %<#pragma acc declare%>");
36600 error = true;
36601 continue;
36602 }
36603 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36604 switch (OMP_CLAUSE_MAP_KIND (t))
36605 {
36606 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36607 case GOMP_MAP_FORCE_ALLOC:
36608 case GOMP_MAP_FORCE_TO:
36609 case GOMP_MAP_FORCE_DEVICEPTR:
36610 case GOMP_MAP_DEVICE_RESIDENT:
36611 break;
36612
36613 case GOMP_MAP_LINK:
36614 if (!global_bindings_p ()
36615 && (TREE_STATIC (decl)
36616 || !DECL_EXTERNAL (decl)))
36617 {
36618 error_at (loc,
36619 "%qD must be a global variable in "
36620 "%<#pragma acc declare link%>",
36621 decl);
36622 error = true;
36623 continue;
36624 }
36625 break;
36626
36627 default:
36628 if (global_bindings_p ())
36629 {
36630 error_at (loc, "invalid OpenACC clause at file scope");
36631 error = true;
36632 continue;
36633 }
36634 if (DECL_EXTERNAL (decl))
36635 {
36636 error_at (loc,
36637 "invalid use of %<extern%> variable %qD "
36638 "in %<#pragma acc declare%>", decl);
36639 error = true;
36640 continue;
36641 }
36642 else if (TREE_PUBLIC (decl))
36643 {
36644 error_at (loc,
36645 "invalid use of %<global%> variable %qD "
36646 "in %<#pragma acc declare%>", decl);
36647 error = true;
36648 continue;
36649 }
36650 break;
36651 }
36652
36653 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36654 || lookup_attribute ("omp declare target link",
36655 DECL_ATTRIBUTES (decl)))
36656 {
36657 error_at (loc, "variable %qD used more than once with "
36658 "%<#pragma acc declare%>", decl);
36659 error = true;
36660 continue;
36661 }
36662
36663 if (!error)
36664 {
36665 tree id;
36666
36667 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36668 id = get_identifier ("omp declare target link");
36669 else
36670 id = get_identifier ("omp declare target");
36671
36672 DECL_ATTRIBUTES (decl)
36673 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36674 if (global_bindings_p ())
36675 {
36676 symtab_node *node = symtab_node::get (decl);
36677 if (node != NULL)
36678 {
36679 node->offloadable = 1;
36680 if (ENABLE_OFFLOADING)
36681 {
36682 g->have_offload = true;
36683 if (is_a <varpool_node *> (node))
36684 vec_safe_push (offload_vars, decl);
36685 }
36686 }
36687 }
36688 }
36689 }
36690
36691 if (error || global_bindings_p ())
36692 return NULL_TREE;
36693
36694 stmt = make_node (OACC_DECLARE);
36695 TREE_TYPE (stmt) = void_type_node;
36696 OACC_DECLARE_CLAUSES (stmt) = clauses;
36697 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36698
36699 add_stmt (stmt);
36700
36701 return NULL_TREE;
36702 }
36703
36704 /* OpenACC 2.0:
36705 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36706
36707 or
36708
36709 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36710
36711 LOC is the location of the #pragma token.
36712 */
36713
36714 #define OACC_ENTER_DATA_CLAUSE_MASK \
36715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36722
36723 #define OACC_EXIT_DATA_CLAUSE_MASK \
36724 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36729
36730 static tree
36731 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36732 bool enter)
36733 {
36734 location_t loc = pragma_tok->location;
36735 tree stmt, clauses;
36736 const char *p = "";
36737
36738 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36739 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36740
36741 if (strcmp (p, "data") != 0)
36742 {
36743 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36744 enter ? "enter" : "exit");
36745 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36746 return NULL_TREE;
36747 }
36748
36749 cp_lexer_consume_token (parser->lexer);
36750
36751 if (enter)
36752 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36753 "#pragma acc enter data", pragma_tok);
36754 else
36755 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36756 "#pragma acc exit data", pragma_tok);
36757
36758 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36759 {
36760 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36761 enter ? "enter" : "exit");
36762 return NULL_TREE;
36763 }
36764
36765 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36766 TREE_TYPE (stmt) = void_type_node;
36767 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36768 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36769 add_stmt (stmt);
36770 return stmt;
36771 }
36772
36773 /* OpenACC 2.0:
36774 # pragma acc loop oacc-loop-clause[optseq] new-line
36775 structured-block */
36776
36777 #define OACC_LOOP_CLAUSE_MASK \
36778 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36788
36789 static tree
36790 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36791 omp_clause_mask mask, tree *cclauses, bool *if_p)
36792 {
36793 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36794
36795 strcat (p_name, " loop");
36796 mask |= OACC_LOOP_CLAUSE_MASK;
36797
36798 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36799 cclauses == NULL);
36800 if (cclauses)
36801 {
36802 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36803 if (*cclauses)
36804 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36805 if (clauses)
36806 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36807 }
36808
36809 tree block = begin_omp_structured_block ();
36810 int save = cp_parser_begin_omp_structured_block (parser);
36811 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36812 cp_parser_end_omp_structured_block (parser, save);
36813 add_stmt (finish_omp_structured_block (block));
36814
36815 return stmt;
36816 }
36817
36818 /* OpenACC 2.0:
36819 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36820 structured-block
36821
36822 or
36823
36824 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36825 structured-block
36826 */
36827
36828 #define OACC_KERNELS_CLAUSE_MASK \
36829 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36846
36847 #define OACC_PARALLEL_CLAUSE_MASK \
36848 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36868
36869 static tree
36870 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36871 char *p_name, bool *if_p)
36872 {
36873 omp_clause_mask mask;
36874 enum tree_code code;
36875 switch (cp_parser_pragma_kind (pragma_tok))
36876 {
36877 case PRAGMA_OACC_KERNELS:
36878 strcat (p_name, " kernels");
36879 mask = OACC_KERNELS_CLAUSE_MASK;
36880 code = OACC_KERNELS;
36881 break;
36882 case PRAGMA_OACC_PARALLEL:
36883 strcat (p_name, " parallel");
36884 mask = OACC_PARALLEL_CLAUSE_MASK;
36885 code = OACC_PARALLEL;
36886 break;
36887 default:
36888 gcc_unreachable ();
36889 }
36890
36891 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36892 {
36893 const char *p
36894 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36895 if (strcmp (p, "loop") == 0)
36896 {
36897 cp_lexer_consume_token (parser->lexer);
36898 tree block = begin_omp_parallel ();
36899 tree clauses;
36900 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36901 if_p);
36902 return finish_omp_construct (code, block, clauses);
36903 }
36904 }
36905
36906 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36907
36908 tree block = begin_omp_parallel ();
36909 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36910 cp_parser_statement (parser, NULL_TREE, false, if_p);
36911 cp_parser_end_omp_structured_block (parser, save);
36912 return finish_omp_construct (code, block, clauses);
36913 }
36914
36915 /* OpenACC 2.0:
36916 # pragma acc update oacc-update-clause[optseq] new-line
36917 */
36918
36919 #define OACC_UPDATE_CLAUSE_MASK \
36920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36926
36927 static tree
36928 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36929 {
36930 tree stmt, clauses;
36931
36932 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36933 "#pragma acc update", pragma_tok);
36934
36935 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36936 {
36937 error_at (pragma_tok->location,
36938 "%<#pragma acc update%> must contain at least one "
36939 "%<device%> or %<host%> or %<self%> clause");
36940 return NULL_TREE;
36941 }
36942
36943 stmt = make_node (OACC_UPDATE);
36944 TREE_TYPE (stmt) = void_type_node;
36945 OACC_UPDATE_CLAUSES (stmt) = clauses;
36946 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36947 add_stmt (stmt);
36948 return stmt;
36949 }
36950
36951 /* OpenACC 2.0:
36952 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36953
36954 LOC is the location of the #pragma token.
36955 */
36956
36957 #define OACC_WAIT_CLAUSE_MASK \
36958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36959
36960 static tree
36961 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36962 {
36963 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36964 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36965
36966 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36967 list = cp_parser_oacc_wait_list (parser, loc, list);
36968
36969 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36970 "#pragma acc wait", pragma_tok);
36971
36972 stmt = c_finish_oacc_wait (loc, list, clauses);
36973 stmt = finish_expr_stmt (stmt);
36974
36975 return stmt;
36976 }
36977
36978 /* OpenMP 4.0:
36979 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36980
36981 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36982 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36988
36989 static void
36990 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36991 enum pragma_context context)
36992 {
36993 bool first_p = parser->omp_declare_simd == NULL;
36994 cp_omp_declare_simd_data data;
36995 if (first_p)
36996 {
36997 data.error_seen = false;
36998 data.fndecl_seen = false;
36999 data.tokens = vNULL;
37000 data.clauses = NULL_TREE;
37001 /* It is safe to take the address of a local variable; it will only be
37002 used while this scope is live. */
37003 parser->omp_declare_simd = &data;
37004 }
37005
37006 /* Store away all pragma tokens. */
37007 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37008 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37009 cp_lexer_consume_token (parser->lexer);
37010 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37011 parser->omp_declare_simd->error_seen = true;
37012 cp_parser_require_pragma_eol (parser, pragma_tok);
37013 struct cp_token_cache *cp
37014 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37015 parser->omp_declare_simd->tokens.safe_push (cp);
37016
37017 if (first_p)
37018 {
37019 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37020 cp_parser_pragma (parser, context, NULL);
37021 switch (context)
37022 {
37023 case pragma_external:
37024 cp_parser_declaration (parser);
37025 break;
37026 case pragma_member:
37027 cp_parser_member_declaration (parser);
37028 break;
37029 case pragma_objc_icode:
37030 cp_parser_block_declaration (parser, /*statement_p=*/false);
37031 break;
37032 default:
37033 cp_parser_declaration_statement (parser);
37034 break;
37035 }
37036 if (parser->omp_declare_simd
37037 && !parser->omp_declare_simd->error_seen
37038 && !parser->omp_declare_simd->fndecl_seen)
37039 error_at (pragma_tok->location,
37040 "%<#pragma omp declare simd%> not immediately followed by "
37041 "function declaration or definition");
37042 data.tokens.release ();
37043 parser->omp_declare_simd = NULL;
37044 }
37045 }
37046
37047 /* Finalize #pragma omp declare simd clauses after direct declarator has
37048 been parsed, and put that into "omp declare simd" attribute. */
37049
37050 static tree
37051 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37052 {
37053 struct cp_token_cache *ce;
37054 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37055 int i;
37056
37057 if (!data->error_seen && data->fndecl_seen)
37058 {
37059 error ("%<#pragma omp declare simd%> not immediately followed by "
37060 "a single function declaration or definition");
37061 data->error_seen = true;
37062 }
37063 if (data->error_seen)
37064 return attrs;
37065
37066 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37067 {
37068 tree c, cl;
37069
37070 cp_parser_push_lexer_for_tokens (parser, ce);
37071 parser->lexer->in_pragma = true;
37072 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37073 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37074 cp_lexer_consume_token (parser->lexer);
37075 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37076 "#pragma omp declare simd", pragma_tok);
37077 cp_parser_pop_lexer (parser);
37078 if (cl)
37079 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37080 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37081 TREE_CHAIN (c) = attrs;
37082 if (processing_template_decl)
37083 ATTR_IS_DEPENDENT (c) = 1;
37084 attrs = c;
37085 }
37086
37087 data->fndecl_seen = true;
37088 return attrs;
37089 }
37090
37091
37092 /* OpenMP 4.0:
37093 # pragma omp declare target new-line
37094 declarations and definitions
37095 # pragma omp end declare target new-line
37096
37097 OpenMP 4.5:
37098 # pragma omp declare target ( extended-list ) new-line
37099
37100 # pragma omp declare target declare-target-clauses[seq] new-line */
37101
37102 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37103 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37104 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37105
37106 static void
37107 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37108 {
37109 tree clauses = NULL_TREE;
37110 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37111 clauses
37112 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37113 "#pragma omp declare target", pragma_tok);
37114 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37115 {
37116 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37117 clauses);
37118 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37119 cp_parser_require_pragma_eol (parser, pragma_tok);
37120 }
37121 else
37122 {
37123 cp_parser_require_pragma_eol (parser, pragma_tok);
37124 scope_chain->omp_declare_target_attribute++;
37125 return;
37126 }
37127 if (scope_chain->omp_declare_target_attribute)
37128 error_at (pragma_tok->location,
37129 "%<#pragma omp declare target%> with clauses in between "
37130 "%<#pragma omp declare target%> without clauses and "
37131 "%<#pragma omp end declare target%>");
37132 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37133 {
37134 tree t = OMP_CLAUSE_DECL (c), id;
37135 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37136 tree at2 = lookup_attribute ("omp declare target link",
37137 DECL_ATTRIBUTES (t));
37138 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37139 {
37140 id = get_identifier ("omp declare target link");
37141 std::swap (at1, at2);
37142 }
37143 else
37144 id = get_identifier ("omp declare target");
37145 if (at2)
37146 {
37147 error_at (OMP_CLAUSE_LOCATION (c),
37148 "%qD specified both in declare target %<link%> and %<to%>"
37149 " clauses", t);
37150 continue;
37151 }
37152 if (!at1)
37153 {
37154 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37155 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37156 continue;
37157
37158 symtab_node *node = symtab_node::get (t);
37159 if (node != NULL)
37160 {
37161 node->offloadable = 1;
37162 if (ENABLE_OFFLOADING)
37163 {
37164 g->have_offload = true;
37165 if (is_a <varpool_node *> (node))
37166 vec_safe_push (offload_vars, t);
37167 }
37168 }
37169 }
37170 }
37171 }
37172
37173 static void
37174 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37175 {
37176 const char *p = "";
37177 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37178 {
37179 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37180 p = IDENTIFIER_POINTER (id);
37181 }
37182 if (strcmp (p, "declare") == 0)
37183 {
37184 cp_lexer_consume_token (parser->lexer);
37185 p = "";
37186 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37187 {
37188 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37189 p = IDENTIFIER_POINTER (id);
37190 }
37191 if (strcmp (p, "target") == 0)
37192 cp_lexer_consume_token (parser->lexer);
37193 else
37194 {
37195 cp_parser_error (parser, "expected %<target%>");
37196 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37197 return;
37198 }
37199 }
37200 else
37201 {
37202 cp_parser_error (parser, "expected %<declare%>");
37203 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37204 return;
37205 }
37206 cp_parser_require_pragma_eol (parser, pragma_tok);
37207 if (!scope_chain->omp_declare_target_attribute)
37208 error_at (pragma_tok->location,
37209 "%<#pragma omp end declare target%> without corresponding "
37210 "%<#pragma omp declare target%>");
37211 else
37212 scope_chain->omp_declare_target_attribute--;
37213 }
37214
37215 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37216 expression and optional initializer clause of
37217 #pragma omp declare reduction. We store the expression(s) as
37218 either 3, 6 or 7 special statements inside of the artificial function's
37219 body. The first two statements are DECL_EXPRs for the artificial
37220 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37221 expression that uses those variables.
37222 If there was any INITIALIZER clause, this is followed by further statements,
37223 the fourth and fifth statements are DECL_EXPRs for the artificial
37224 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37225 constructor variant (first token after open paren is not omp_priv),
37226 then the sixth statement is a statement with the function call expression
37227 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37228 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37229 to initialize the OMP_PRIV artificial variable and there is seventh
37230 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37231
37232 static bool
37233 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37234 {
37235 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37236 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37237 type = TREE_TYPE (type);
37238 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37239 DECL_ARTIFICIAL (omp_out) = 1;
37240 pushdecl (omp_out);
37241 add_decl_expr (omp_out);
37242 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37243 DECL_ARTIFICIAL (omp_in) = 1;
37244 pushdecl (omp_in);
37245 add_decl_expr (omp_in);
37246 tree combiner;
37247 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37248
37249 keep_next_level (true);
37250 tree block = begin_omp_structured_block ();
37251 combiner = cp_parser_expression (parser);
37252 finish_expr_stmt (combiner);
37253 block = finish_omp_structured_block (block);
37254 add_stmt (block);
37255
37256 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37257 return false;
37258
37259 const char *p = "";
37260 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37261 {
37262 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37263 p = IDENTIFIER_POINTER (id);
37264 }
37265
37266 if (strcmp (p, "initializer") == 0)
37267 {
37268 cp_lexer_consume_token (parser->lexer);
37269 matching_parens parens;
37270 if (!parens.require_open (parser))
37271 return false;
37272
37273 p = "";
37274 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37275 {
37276 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37277 p = IDENTIFIER_POINTER (id);
37278 }
37279
37280 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37281 DECL_ARTIFICIAL (omp_priv) = 1;
37282 pushdecl (omp_priv);
37283 add_decl_expr (omp_priv);
37284 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37285 DECL_ARTIFICIAL (omp_orig) = 1;
37286 pushdecl (omp_orig);
37287 add_decl_expr (omp_orig);
37288
37289 keep_next_level (true);
37290 block = begin_omp_structured_block ();
37291
37292 bool ctor = false;
37293 if (strcmp (p, "omp_priv") == 0)
37294 {
37295 bool is_direct_init, is_non_constant_init;
37296 ctor = true;
37297 cp_lexer_consume_token (parser->lexer);
37298 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37299 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37300 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37301 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37302 == CPP_CLOSE_PAREN
37303 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37304 == CPP_CLOSE_PAREN))
37305 {
37306 finish_omp_structured_block (block);
37307 error ("invalid initializer clause");
37308 return false;
37309 }
37310 initializer = cp_parser_initializer (parser, &is_direct_init,
37311 &is_non_constant_init);
37312 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37313 NULL_TREE, LOOKUP_ONLYCONVERTING);
37314 }
37315 else
37316 {
37317 cp_parser_parse_tentatively (parser);
37318 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37319 /*check_dependency_p=*/true,
37320 /*template_p=*/NULL,
37321 /*declarator_p=*/false,
37322 /*optional_p=*/false);
37323 vec<tree, va_gc> *args;
37324 if (fn_name == error_mark_node
37325 || cp_parser_error_occurred (parser)
37326 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37327 || ((args = cp_parser_parenthesized_expression_list
37328 (parser, non_attr, /*cast_p=*/false,
37329 /*allow_expansion_p=*/true,
37330 /*non_constant_p=*/NULL)),
37331 cp_parser_error_occurred (parser)))
37332 {
37333 finish_omp_structured_block (block);
37334 cp_parser_abort_tentative_parse (parser);
37335 cp_parser_error (parser, "expected id-expression (arguments)");
37336 return false;
37337 }
37338 unsigned int i;
37339 tree arg;
37340 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37341 if (arg == omp_priv
37342 || (TREE_CODE (arg) == ADDR_EXPR
37343 && TREE_OPERAND (arg, 0) == omp_priv))
37344 break;
37345 cp_parser_abort_tentative_parse (parser);
37346 if (arg == NULL_TREE)
37347 error ("one of the initializer call arguments should be %<omp_priv%>"
37348 " or %<&omp_priv%>");
37349 initializer = cp_parser_postfix_expression (parser, false, false, false,
37350 false, NULL);
37351 finish_expr_stmt (initializer);
37352 }
37353
37354 block = finish_omp_structured_block (block);
37355 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37356 add_stmt (block);
37357
37358 if (ctor)
37359 add_decl_expr (omp_orig);
37360
37361 if (!parens.require_close (parser))
37362 return false;
37363 }
37364
37365 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37366 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37367 UNKNOWN_LOCATION);
37368
37369 return true;
37370 }
37371
37372 /* OpenMP 4.0
37373 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37374 initializer-clause[opt] new-line
37375
37376 initializer-clause:
37377 initializer (omp_priv initializer)
37378 initializer (function-name (argument-list)) */
37379
37380 static void
37381 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37382 enum pragma_context)
37383 {
37384 auto_vec<tree> types;
37385 enum tree_code reduc_code = ERROR_MARK;
37386 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37387 unsigned int i;
37388 cp_token *first_token;
37389 cp_token_cache *cp;
37390 int errs;
37391 void *p;
37392
37393 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37394 p = obstack_alloc (&declarator_obstack, 0);
37395
37396 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37397 goto fail;
37398
37399 switch (cp_lexer_peek_token (parser->lexer)->type)
37400 {
37401 case CPP_PLUS:
37402 reduc_code = PLUS_EXPR;
37403 break;
37404 case CPP_MULT:
37405 reduc_code = MULT_EXPR;
37406 break;
37407 case CPP_MINUS:
37408 reduc_code = MINUS_EXPR;
37409 break;
37410 case CPP_AND:
37411 reduc_code = BIT_AND_EXPR;
37412 break;
37413 case CPP_XOR:
37414 reduc_code = BIT_XOR_EXPR;
37415 break;
37416 case CPP_OR:
37417 reduc_code = BIT_IOR_EXPR;
37418 break;
37419 case CPP_AND_AND:
37420 reduc_code = TRUTH_ANDIF_EXPR;
37421 break;
37422 case CPP_OR_OR:
37423 reduc_code = TRUTH_ORIF_EXPR;
37424 break;
37425 case CPP_NAME:
37426 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37427 break;
37428 default:
37429 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37430 "%<|%>, %<&&%>, %<||%> or identifier");
37431 goto fail;
37432 }
37433
37434 if (reduc_code != ERROR_MARK)
37435 cp_lexer_consume_token (parser->lexer);
37436
37437 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37438 if (reduc_id == error_mark_node)
37439 goto fail;
37440
37441 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37442 goto fail;
37443
37444 /* Types may not be defined in declare reduction type list. */
37445 const char *saved_message;
37446 saved_message = parser->type_definition_forbidden_message;
37447 parser->type_definition_forbidden_message
37448 = G_("types may not be defined in declare reduction type list");
37449 bool saved_colon_corrects_to_scope_p;
37450 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37451 parser->colon_corrects_to_scope_p = false;
37452 bool saved_colon_doesnt_start_class_def_p;
37453 saved_colon_doesnt_start_class_def_p
37454 = parser->colon_doesnt_start_class_def_p;
37455 parser->colon_doesnt_start_class_def_p = true;
37456
37457 while (true)
37458 {
37459 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37460 type = cp_parser_type_id (parser);
37461 if (type == error_mark_node)
37462 ;
37463 else if (ARITHMETIC_TYPE_P (type)
37464 && (orig_reduc_id == NULL_TREE
37465 || (TREE_CODE (type) != COMPLEX_TYPE
37466 && (id_equal (orig_reduc_id, "min")
37467 || id_equal (orig_reduc_id, "max")))))
37468 error_at (loc, "predeclared arithmetic type %qT in "
37469 "%<#pragma omp declare reduction%>", type);
37470 else if (TREE_CODE (type) == FUNCTION_TYPE
37471 || TREE_CODE (type) == METHOD_TYPE
37472 || TREE_CODE (type) == ARRAY_TYPE)
37473 error_at (loc, "function or array type %qT in "
37474 "%<#pragma omp declare reduction%>", type);
37475 else if (TREE_CODE (type) == REFERENCE_TYPE)
37476 error_at (loc, "reference type %qT in "
37477 "%<#pragma omp declare reduction%>", type);
37478 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37479 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37480 "%<#pragma omp declare reduction%>", type);
37481 else
37482 types.safe_push (type);
37483
37484 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37485 cp_lexer_consume_token (parser->lexer);
37486 else
37487 break;
37488 }
37489
37490 /* Restore the saved message. */
37491 parser->type_definition_forbidden_message = saved_message;
37492 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37493 parser->colon_doesnt_start_class_def_p
37494 = saved_colon_doesnt_start_class_def_p;
37495
37496 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37497 || types.is_empty ())
37498 {
37499 fail:
37500 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37501 goto done;
37502 }
37503
37504 first_token = cp_lexer_peek_token (parser->lexer);
37505 cp = NULL;
37506 errs = errorcount;
37507 FOR_EACH_VEC_ELT (types, i, type)
37508 {
37509 tree fntype
37510 = build_function_type_list (void_type_node,
37511 cp_build_reference_type (type, false),
37512 NULL_TREE);
37513 tree this_reduc_id = reduc_id;
37514 if (!dependent_type_p (type))
37515 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37516 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37517 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37518 DECL_ARTIFICIAL (fndecl) = 1;
37519 DECL_EXTERNAL (fndecl) = 1;
37520 DECL_DECLARED_INLINE_P (fndecl) = 1;
37521 DECL_IGNORED_P (fndecl) = 1;
37522 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37523 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37524 DECL_ATTRIBUTES (fndecl)
37525 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37526 DECL_ATTRIBUTES (fndecl));
37527 if (processing_template_decl)
37528 fndecl = push_template_decl (fndecl);
37529 bool block_scope = false;
37530 tree block = NULL_TREE;
37531 if (current_function_decl)
37532 {
37533 block_scope = true;
37534 DECL_CONTEXT (fndecl) = global_namespace;
37535 if (!processing_template_decl)
37536 pushdecl (fndecl);
37537 }
37538 else if (current_class_type)
37539 {
37540 if (cp == NULL)
37541 {
37542 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37543 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37544 cp_lexer_consume_token (parser->lexer);
37545 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37546 goto fail;
37547 cp = cp_token_cache_new (first_token,
37548 cp_lexer_peek_nth_token (parser->lexer,
37549 2));
37550 }
37551 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37552 finish_member_declaration (fndecl);
37553 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37554 DECL_PENDING_INLINE_P (fndecl) = 1;
37555 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37556 continue;
37557 }
37558 else
37559 {
37560 DECL_CONTEXT (fndecl) = current_namespace;
37561 pushdecl (fndecl);
37562 }
37563 if (!block_scope)
37564 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37565 else
37566 block = begin_omp_structured_block ();
37567 if (cp)
37568 {
37569 cp_parser_push_lexer_for_tokens (parser, cp);
37570 parser->lexer->in_pragma = true;
37571 }
37572 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37573 {
37574 if (!block_scope)
37575 finish_function (/*inline_p=*/false);
37576 else
37577 DECL_CONTEXT (fndecl) = current_function_decl;
37578 if (cp)
37579 cp_parser_pop_lexer (parser);
37580 goto fail;
37581 }
37582 if (cp)
37583 cp_parser_pop_lexer (parser);
37584 if (!block_scope)
37585 finish_function (/*inline_p=*/false);
37586 else
37587 {
37588 DECL_CONTEXT (fndecl) = current_function_decl;
37589 block = finish_omp_structured_block (block);
37590 if (TREE_CODE (block) == BIND_EXPR)
37591 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37592 else if (TREE_CODE (block) == STATEMENT_LIST)
37593 DECL_SAVED_TREE (fndecl) = block;
37594 if (processing_template_decl)
37595 add_decl_expr (fndecl);
37596 }
37597 cp_check_omp_declare_reduction (fndecl);
37598 if (cp == NULL && types.length () > 1)
37599 cp = cp_token_cache_new (first_token,
37600 cp_lexer_peek_nth_token (parser->lexer, 2));
37601 if (errs != errorcount)
37602 break;
37603 }
37604
37605 cp_parser_require_pragma_eol (parser, pragma_tok);
37606
37607 done:
37608 /* Free any declarators allocated. */
37609 obstack_free (&declarator_obstack, p);
37610 }
37611
37612 /* OpenMP 4.0
37613 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37614 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37615 initializer-clause[opt] new-line
37616 #pragma omp declare target new-line */
37617
37618 static bool
37619 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37620 enum pragma_context context)
37621 {
37622 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37623 {
37624 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37625 const char *p = IDENTIFIER_POINTER (id);
37626
37627 if (strcmp (p, "simd") == 0)
37628 {
37629 cp_lexer_consume_token (parser->lexer);
37630 cp_parser_omp_declare_simd (parser, pragma_tok,
37631 context);
37632 return true;
37633 }
37634 cp_ensure_no_omp_declare_simd (parser);
37635 if (strcmp (p, "reduction") == 0)
37636 {
37637 cp_lexer_consume_token (parser->lexer);
37638 cp_parser_omp_declare_reduction (parser, pragma_tok,
37639 context);
37640 return false;
37641 }
37642 if (!flag_openmp) /* flag_openmp_simd */
37643 {
37644 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37645 return false;
37646 }
37647 if (strcmp (p, "target") == 0)
37648 {
37649 cp_lexer_consume_token (parser->lexer);
37650 cp_parser_omp_declare_target (parser, pragma_tok);
37651 return false;
37652 }
37653 }
37654 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37655 "or %<target%>");
37656 cp_parser_require_pragma_eol (parser, pragma_tok);
37657 return false;
37658 }
37659
37660 /* OpenMP 4.5:
37661 #pragma omp taskloop taskloop-clause[optseq] new-line
37662 for-loop
37663
37664 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37665 for-loop */
37666
37667 #define OMP_TASKLOOP_CLAUSE_MASK \
37668 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37682
37683 static tree
37684 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37685 char *p_name, omp_clause_mask mask, tree *cclauses,
37686 bool *if_p)
37687 {
37688 tree clauses, sb, ret;
37689 unsigned int save;
37690 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37691
37692 strcat (p_name, " taskloop");
37693 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37694
37695 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37696 {
37697 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37698 const char *p = IDENTIFIER_POINTER (id);
37699
37700 if (strcmp (p, "simd") == 0)
37701 {
37702 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37703 if (cclauses == NULL)
37704 cclauses = cclauses_buf;
37705
37706 cp_lexer_consume_token (parser->lexer);
37707 if (!flag_openmp) /* flag_openmp_simd */
37708 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37709 cclauses, if_p);
37710 sb = begin_omp_structured_block ();
37711 save = cp_parser_begin_omp_structured_block (parser);
37712 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37713 cclauses, if_p);
37714 cp_parser_end_omp_structured_block (parser, save);
37715 tree body = finish_omp_structured_block (sb);
37716 if (ret == NULL)
37717 return ret;
37718 ret = make_node (OMP_TASKLOOP);
37719 TREE_TYPE (ret) = void_type_node;
37720 OMP_FOR_BODY (ret) = body;
37721 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37722 SET_EXPR_LOCATION (ret, loc);
37723 add_stmt (ret);
37724 return ret;
37725 }
37726 }
37727 if (!flag_openmp) /* flag_openmp_simd */
37728 {
37729 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37730 return NULL_TREE;
37731 }
37732
37733 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37734 cclauses == NULL);
37735 if (cclauses)
37736 {
37737 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37738 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37739 }
37740
37741 sb = begin_omp_structured_block ();
37742 save = cp_parser_begin_omp_structured_block (parser);
37743
37744 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37745 if_p);
37746
37747 cp_parser_end_omp_structured_block (parser, save);
37748 add_stmt (finish_omp_structured_block (sb));
37749
37750 return ret;
37751 }
37752
37753
37754 /* OpenACC 2.0:
37755 # pragma acc routine oacc-routine-clause[optseq] new-line
37756 function-definition
37757
37758 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37759 */
37760
37761 #define OACC_ROUTINE_CLAUSE_MASK \
37762 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37766
37767
37768 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37769 component, which must resolve to a declared namespace-scope
37770 function. The clauses are either processed directly (for a named
37771 function), or defered until the immediatley following declaration
37772 is parsed. */
37773
37774 static void
37775 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37776 enum pragma_context context)
37777 {
37778 gcc_checking_assert (context == pragma_external);
37779 /* The checking for "another pragma following this one" in the "no optional
37780 '( name )'" case makes sure that we dont re-enter. */
37781 gcc_checking_assert (parser->oacc_routine == NULL);
37782
37783 cp_oacc_routine_data data;
37784 data.error_seen = false;
37785 data.fndecl_seen = false;
37786 data.tokens = vNULL;
37787 data.clauses = NULL_TREE;
37788 data.loc = pragma_tok->location;
37789 /* It is safe to take the address of a local variable; it will only be
37790 used while this scope is live. */
37791 parser->oacc_routine = &data;
37792
37793 /* Look for optional '( name )'. */
37794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37795 {
37796 matching_parens parens;
37797 parens.consume_open (parser); /* '(' */
37798
37799 /* We parse the name as an id-expression. If it resolves to
37800 anything other than a non-overloaded function at namespace
37801 scope, it's an error. */
37802 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37803 tree name = cp_parser_id_expression (parser,
37804 /*template_keyword_p=*/false,
37805 /*check_dependency_p=*/false,
37806 /*template_p=*/NULL,
37807 /*declarator_p=*/false,
37808 /*optional_p=*/false);
37809 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37810 if (name != error_mark_node && decl == error_mark_node)
37811 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37812
37813 if (decl == error_mark_node
37814 || !parens.require_close (parser))
37815 {
37816 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37817 parser->oacc_routine = NULL;
37818 return;
37819 }
37820
37821 data.clauses
37822 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37823 "#pragma acc routine",
37824 cp_lexer_peek_token (parser->lexer));
37825
37826 if (decl && is_overloaded_fn (decl)
37827 && (TREE_CODE (decl) != FUNCTION_DECL
37828 || DECL_FUNCTION_TEMPLATE_P (decl)))
37829 {
37830 error_at (name_loc,
37831 "%<#pragma acc routine%> names a set of overloads");
37832 parser->oacc_routine = NULL;
37833 return;
37834 }
37835
37836 /* Perhaps we should use the same rule as declarations in different
37837 namespaces? */
37838 if (!DECL_NAMESPACE_SCOPE_P (decl))
37839 {
37840 error_at (name_loc,
37841 "%qD does not refer to a namespace scope function", decl);
37842 parser->oacc_routine = NULL;
37843 return;
37844 }
37845
37846 if (TREE_CODE (decl) != FUNCTION_DECL)
37847 {
37848 error_at (name_loc, "%qD does not refer to a function", decl);
37849 parser->oacc_routine = NULL;
37850 return;
37851 }
37852
37853 cp_finalize_oacc_routine (parser, decl, false);
37854 parser->oacc_routine = NULL;
37855 }
37856 else /* No optional '( name )'. */
37857 {
37858 /* Store away all pragma tokens. */
37859 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37860 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37861 cp_lexer_consume_token (parser->lexer);
37862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37863 parser->oacc_routine->error_seen = true;
37864 cp_parser_require_pragma_eol (parser, pragma_tok);
37865 struct cp_token_cache *cp
37866 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37867 parser->oacc_routine->tokens.safe_push (cp);
37868
37869 /* Emit a helpful diagnostic if there's another pragma following this
37870 one. */
37871 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37872 {
37873 cp_ensure_no_oacc_routine (parser);
37874 data.tokens.release ();
37875 /* ..., and then just keep going. */
37876 return;
37877 }
37878
37879 /* We only have to consider the pragma_external case here. */
37880 cp_parser_declaration (parser);
37881 if (parser->oacc_routine
37882 && !parser->oacc_routine->fndecl_seen)
37883 cp_ensure_no_oacc_routine (parser);
37884 else
37885 parser->oacc_routine = NULL;
37886 data.tokens.release ();
37887 }
37888 }
37889
37890 /* Finalize #pragma acc routine clauses after direct declarator has
37891 been parsed. */
37892
37893 static tree
37894 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37895 {
37896 struct cp_token_cache *ce;
37897 cp_oacc_routine_data *data = parser->oacc_routine;
37898
37899 if (!data->error_seen && data->fndecl_seen)
37900 {
37901 error_at (data->loc,
37902 "%<#pragma acc routine%> not immediately followed by "
37903 "a single function declaration or definition");
37904 data->error_seen = true;
37905 }
37906 if (data->error_seen)
37907 return attrs;
37908
37909 gcc_checking_assert (data->tokens.length () == 1);
37910 ce = data->tokens[0];
37911
37912 cp_parser_push_lexer_for_tokens (parser, ce);
37913 parser->lexer->in_pragma = true;
37914 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37915
37916 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37917 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37918 parser->oacc_routine->clauses
37919 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37920 "#pragma acc routine", pragma_tok);
37921 cp_parser_pop_lexer (parser);
37922 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37923 fndecl_seen. */
37924
37925 return attrs;
37926 }
37927
37928 /* Apply any saved OpenACC routine clauses to a just-parsed
37929 declaration. */
37930
37931 static void
37932 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37933 {
37934 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37935 {
37936 /* Keep going if we're in error reporting mode. */
37937 if (parser->oacc_routine->error_seen
37938 || fndecl == error_mark_node)
37939 return;
37940
37941 if (parser->oacc_routine->fndecl_seen)
37942 {
37943 error_at (parser->oacc_routine->loc,
37944 "%<#pragma acc routine%> not immediately followed by"
37945 " a single function declaration or definition");
37946 parser->oacc_routine = NULL;
37947 return;
37948 }
37949 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37950 {
37951 cp_ensure_no_oacc_routine (parser);
37952 return;
37953 }
37954
37955 if (oacc_get_fn_attrib (fndecl))
37956 {
37957 error_at (parser->oacc_routine->loc,
37958 "%<#pragma acc routine%> already applied to %qD", fndecl);
37959 parser->oacc_routine = NULL;
37960 return;
37961 }
37962
37963 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37964 {
37965 error_at (parser->oacc_routine->loc,
37966 TREE_USED (fndecl)
37967 ? G_("%<#pragma acc routine%> must be applied before use")
37968 : G_("%<#pragma acc routine%> must be applied before "
37969 "definition"));
37970 parser->oacc_routine = NULL;
37971 return;
37972 }
37973
37974 /* Process the routine's dimension clauses. */
37975 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37976 oacc_replace_fn_attrib (fndecl, dims);
37977
37978 /* Add an "omp declare target" attribute. */
37979 DECL_ATTRIBUTES (fndecl)
37980 = tree_cons (get_identifier ("omp declare target"),
37981 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37982
37983 /* Don't unset parser->oacc_routine here: we may still need it to
37984 diagnose wrong usage. But, remember that we've used this "#pragma acc
37985 routine". */
37986 parser->oacc_routine->fndecl_seen = true;
37987 }
37988 }
37989
37990 /* Main entry point to OpenMP statement pragmas. */
37991
37992 static void
37993 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37994 {
37995 tree stmt;
37996 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37997 omp_clause_mask mask (0);
37998
37999 switch (cp_parser_pragma_kind (pragma_tok))
38000 {
38001 case PRAGMA_OACC_ATOMIC:
38002 cp_parser_omp_atomic (parser, pragma_tok);
38003 return;
38004 case PRAGMA_OACC_CACHE:
38005 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38006 break;
38007 case PRAGMA_OACC_DATA:
38008 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38009 break;
38010 case PRAGMA_OACC_ENTER_DATA:
38011 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38012 break;
38013 case PRAGMA_OACC_EXIT_DATA:
38014 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38015 break;
38016 case PRAGMA_OACC_HOST_DATA:
38017 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38018 break;
38019 case PRAGMA_OACC_KERNELS:
38020 case PRAGMA_OACC_PARALLEL:
38021 strcpy (p_name, "#pragma acc");
38022 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38023 if_p);
38024 break;
38025 case PRAGMA_OACC_LOOP:
38026 strcpy (p_name, "#pragma acc");
38027 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38028 if_p);
38029 break;
38030 case PRAGMA_OACC_UPDATE:
38031 stmt = cp_parser_oacc_update (parser, pragma_tok);
38032 break;
38033 case PRAGMA_OACC_WAIT:
38034 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38035 break;
38036 case PRAGMA_OMP_ATOMIC:
38037 cp_parser_omp_atomic (parser, pragma_tok);
38038 return;
38039 case PRAGMA_OMP_CRITICAL:
38040 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38041 break;
38042 case PRAGMA_OMP_DISTRIBUTE:
38043 strcpy (p_name, "#pragma omp");
38044 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38045 if_p);
38046 break;
38047 case PRAGMA_OMP_FOR:
38048 strcpy (p_name, "#pragma omp");
38049 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38050 if_p);
38051 break;
38052 case PRAGMA_OMP_MASTER:
38053 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38054 break;
38055 case PRAGMA_OMP_PARALLEL:
38056 strcpy (p_name, "#pragma omp");
38057 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38058 if_p);
38059 break;
38060 case PRAGMA_OMP_SECTIONS:
38061 strcpy (p_name, "#pragma omp");
38062 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38063 break;
38064 case PRAGMA_OMP_SIMD:
38065 strcpy (p_name, "#pragma omp");
38066 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38067 if_p);
38068 break;
38069 case PRAGMA_OMP_SINGLE:
38070 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38071 break;
38072 case PRAGMA_OMP_TASK:
38073 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38074 break;
38075 case PRAGMA_OMP_TASKGROUP:
38076 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38077 break;
38078 case PRAGMA_OMP_TASKLOOP:
38079 strcpy (p_name, "#pragma omp");
38080 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38081 if_p);
38082 break;
38083 case PRAGMA_OMP_TEAMS:
38084 strcpy (p_name, "#pragma omp");
38085 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38086 if_p);
38087 break;
38088 default:
38089 gcc_unreachable ();
38090 }
38091
38092 protected_set_expr_location (stmt, pragma_tok->location);
38093 }
38094 \f
38095 /* Transactional Memory parsing routines. */
38096
38097 /* Parse a transaction attribute.
38098
38099 txn-attribute:
38100 attribute
38101 [ [ identifier ] ]
38102
38103 We use this instead of cp_parser_attributes_opt for transactions to avoid
38104 the pedwarn in C++98 mode. */
38105
38106 static tree
38107 cp_parser_txn_attribute_opt (cp_parser *parser)
38108 {
38109 cp_token *token;
38110 tree attr_name, attr = NULL;
38111
38112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38113 return cp_parser_attributes_opt (parser);
38114
38115 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38116 return NULL_TREE;
38117 cp_lexer_consume_token (parser->lexer);
38118 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38119 goto error1;
38120
38121 token = cp_lexer_peek_token (parser->lexer);
38122 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38123 {
38124 token = cp_lexer_consume_token (parser->lexer);
38125
38126 attr_name = (token->type == CPP_KEYWORD
38127 /* For keywords, use the canonical spelling,
38128 not the parsed identifier. */
38129 ? ridpointers[(int) token->keyword]
38130 : token->u.value);
38131 attr = build_tree_list (attr_name, NULL_TREE);
38132 }
38133 else
38134 cp_parser_error (parser, "expected identifier");
38135
38136 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38137 error1:
38138 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38139 return attr;
38140 }
38141
38142 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38143
38144 transaction-statement:
38145 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38146 compound-statement
38147 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38148 */
38149
38150 static tree
38151 cp_parser_transaction (cp_parser *parser, cp_token *token)
38152 {
38153 unsigned char old_in = parser->in_transaction;
38154 unsigned char this_in = 1, new_in;
38155 enum rid keyword = token->keyword;
38156 tree stmt, attrs, noex;
38157
38158 cp_lexer_consume_token (parser->lexer);
38159
38160 if (keyword == RID_TRANSACTION_RELAXED
38161 || keyword == RID_SYNCHRONIZED)
38162 this_in |= TM_STMT_ATTR_RELAXED;
38163 else
38164 {
38165 attrs = cp_parser_txn_attribute_opt (parser);
38166 if (attrs)
38167 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38168 }
38169
38170 /* Parse a noexcept specification. */
38171 if (keyword == RID_ATOMIC_NOEXCEPT)
38172 noex = boolean_true_node;
38173 else if (keyword == RID_ATOMIC_CANCEL)
38174 {
38175 /* cancel-and-throw is unimplemented. */
38176 sorry ("atomic_cancel");
38177 noex = NULL_TREE;
38178 }
38179 else
38180 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38181
38182 /* Keep track if we're in the lexical scope of an outer transaction. */
38183 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38184
38185 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38186
38187 parser->in_transaction = new_in;
38188 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38189 parser->in_transaction = old_in;
38190
38191 finish_transaction_stmt (stmt, NULL, this_in, noex);
38192
38193 return stmt;
38194 }
38195
38196 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38197
38198 transaction-expression:
38199 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38200 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38201 */
38202
38203 static tree
38204 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38205 {
38206 unsigned char old_in = parser->in_transaction;
38207 unsigned char this_in = 1;
38208 cp_token *token;
38209 tree expr, noex;
38210 bool noex_expr;
38211 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38212
38213 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38214 || keyword == RID_TRANSACTION_RELAXED);
38215
38216 if (!flag_tm)
38217 error_at (loc,
38218 keyword == RID_TRANSACTION_RELAXED
38219 ? G_("%<__transaction_relaxed%> without transactional memory "
38220 "support enabled")
38221 : G_("%<__transaction_atomic%> without transactional memory "
38222 "support enabled"));
38223
38224 token = cp_parser_require_keyword (parser, keyword,
38225 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38226 : RT_TRANSACTION_RELAXED));
38227 gcc_assert (token != NULL);
38228
38229 if (keyword == RID_TRANSACTION_RELAXED)
38230 this_in |= TM_STMT_ATTR_RELAXED;
38231
38232 /* Set this early. This might mean that we allow transaction_cancel in
38233 an expression that we find out later actually has to be a constexpr.
38234 However, we expect that cxx_constant_value will be able to deal with
38235 this; also, if the noexcept has no constexpr, then what we parse next
38236 really is a transaction's body. */
38237 parser->in_transaction = this_in;
38238
38239 /* Parse a noexcept specification. */
38240 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38241 true);
38242
38243 if (!noex || !noex_expr
38244 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38245 {
38246 matching_parens parens;
38247 parens.require_open (parser);
38248
38249 expr = cp_parser_expression (parser);
38250 expr = finish_parenthesized_expr (expr);
38251
38252 parens.require_close (parser);
38253 }
38254 else
38255 {
38256 /* The only expression that is available got parsed for the noexcept
38257 already. noexcept is true then. */
38258 expr = noex;
38259 noex = boolean_true_node;
38260 }
38261
38262 expr = build_transaction_expr (token->location, expr, this_in, noex);
38263 parser->in_transaction = old_in;
38264
38265 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38266 return error_mark_node;
38267
38268 return (flag_tm ? expr : error_mark_node);
38269 }
38270
38271 /* Parse a function-transaction-block.
38272
38273 function-transaction-block:
38274 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38275 function-body
38276 __transaction_atomic txn-attribute[opt] function-try-block
38277 __transaction_relaxed ctor-initializer[opt] function-body
38278 __transaction_relaxed function-try-block
38279 */
38280
38281 static void
38282 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38283 {
38284 unsigned char old_in = parser->in_transaction;
38285 unsigned char new_in = 1;
38286 tree compound_stmt, stmt, attrs;
38287 cp_token *token;
38288
38289 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38290 || keyword == RID_TRANSACTION_RELAXED);
38291 token = cp_parser_require_keyword (parser, keyword,
38292 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38293 : RT_TRANSACTION_RELAXED));
38294 gcc_assert (token != NULL);
38295
38296 if (keyword == RID_TRANSACTION_RELAXED)
38297 new_in |= TM_STMT_ATTR_RELAXED;
38298 else
38299 {
38300 attrs = cp_parser_txn_attribute_opt (parser);
38301 if (attrs)
38302 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38303 }
38304
38305 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38306
38307 parser->in_transaction = new_in;
38308
38309 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38310 cp_parser_function_try_block (parser);
38311 else
38312 cp_parser_ctor_initializer_opt_and_function_body
38313 (parser, /*in_function_try_block=*/false);
38314
38315 parser->in_transaction = old_in;
38316
38317 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38318 }
38319
38320 /* Parse a __transaction_cancel statement.
38321
38322 cancel-statement:
38323 __transaction_cancel txn-attribute[opt] ;
38324 __transaction_cancel txn-attribute[opt] throw-expression ;
38325
38326 ??? Cancel and throw is not yet implemented. */
38327
38328 static tree
38329 cp_parser_transaction_cancel (cp_parser *parser)
38330 {
38331 cp_token *token;
38332 bool is_outer = false;
38333 tree stmt, attrs;
38334
38335 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38336 RT_TRANSACTION_CANCEL);
38337 gcc_assert (token != NULL);
38338
38339 attrs = cp_parser_txn_attribute_opt (parser);
38340 if (attrs)
38341 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38342
38343 /* ??? Parse cancel-and-throw here. */
38344
38345 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38346
38347 if (!flag_tm)
38348 {
38349 error_at (token->location, "%<__transaction_cancel%> without "
38350 "transactional memory support enabled");
38351 return error_mark_node;
38352 }
38353 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38354 {
38355 error_at (token->location, "%<__transaction_cancel%> within a "
38356 "%<__transaction_relaxed%>");
38357 return error_mark_node;
38358 }
38359 else if (is_outer)
38360 {
38361 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38362 && !is_tm_may_cancel_outer (current_function_decl))
38363 {
38364 error_at (token->location, "outer %<__transaction_cancel%> not "
38365 "within outer %<__transaction_atomic%>");
38366 error_at (token->location,
38367 " or a %<transaction_may_cancel_outer%> function");
38368 return error_mark_node;
38369 }
38370 }
38371 else if (parser->in_transaction == 0)
38372 {
38373 error_at (token->location, "%<__transaction_cancel%> not within "
38374 "%<__transaction_atomic%>");
38375 return error_mark_node;
38376 }
38377
38378 stmt = build_tm_abort_call (token->location, is_outer);
38379 add_stmt (stmt);
38380
38381 return stmt;
38382 }
38383 \f
38384 /* The parser. */
38385
38386 static GTY (()) cp_parser *the_parser;
38387
38388 \f
38389 /* Special handling for the first token or line in the file. The first
38390 thing in the file might be #pragma GCC pch_preprocess, which loads a
38391 PCH file, which is a GC collection point. So we need to handle this
38392 first pragma without benefit of an existing lexer structure.
38393
38394 Always returns one token to the caller in *FIRST_TOKEN. This is
38395 either the true first token of the file, or the first token after
38396 the initial pragma. */
38397
38398 static void
38399 cp_parser_initial_pragma (cp_token *first_token)
38400 {
38401 tree name = NULL;
38402
38403 cp_lexer_get_preprocessor_token (NULL, first_token);
38404 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38405 return;
38406
38407 cp_lexer_get_preprocessor_token (NULL, first_token);
38408 if (first_token->type == CPP_STRING)
38409 {
38410 name = first_token->u.value;
38411
38412 cp_lexer_get_preprocessor_token (NULL, first_token);
38413 if (first_token->type != CPP_PRAGMA_EOL)
38414 error_at (first_token->location,
38415 "junk at end of %<#pragma GCC pch_preprocess%>");
38416 }
38417 else
38418 error_at (first_token->location, "expected string literal");
38419
38420 /* Skip to the end of the pragma. */
38421 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38422 cp_lexer_get_preprocessor_token (NULL, first_token);
38423
38424 /* Now actually load the PCH file. */
38425 if (name)
38426 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38427
38428 /* Read one more token to return to our caller. We have to do this
38429 after reading the PCH file in, since its pointers have to be
38430 live. */
38431 cp_lexer_get_preprocessor_token (NULL, first_token);
38432 }
38433
38434 /* Normal parsing of a pragma token. Here we can (and must) use the
38435 regular lexer. */
38436
38437 static bool
38438 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38439 {
38440 cp_token *pragma_tok;
38441 unsigned int id;
38442 tree stmt;
38443 bool ret;
38444
38445 pragma_tok = cp_lexer_consume_token (parser->lexer);
38446 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38447 parser->lexer->in_pragma = true;
38448
38449 id = cp_parser_pragma_kind (pragma_tok);
38450 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38451 cp_ensure_no_omp_declare_simd (parser);
38452 switch (id)
38453 {
38454 case PRAGMA_GCC_PCH_PREPROCESS:
38455 error_at (pragma_tok->location,
38456 "%<#pragma GCC pch_preprocess%> must be first");
38457 break;
38458
38459 case PRAGMA_OMP_BARRIER:
38460 switch (context)
38461 {
38462 case pragma_compound:
38463 cp_parser_omp_barrier (parser, pragma_tok);
38464 return false;
38465 case pragma_stmt:
38466 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38467 "used in compound statements", "omp barrier");
38468 break;
38469 default:
38470 goto bad_stmt;
38471 }
38472 break;
38473
38474 case PRAGMA_OMP_FLUSH:
38475 switch (context)
38476 {
38477 case pragma_compound:
38478 cp_parser_omp_flush (parser, pragma_tok);
38479 return false;
38480 case pragma_stmt:
38481 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38482 "used in compound statements", "omp flush");
38483 break;
38484 default:
38485 goto bad_stmt;
38486 }
38487 break;
38488
38489 case PRAGMA_OMP_TASKWAIT:
38490 switch (context)
38491 {
38492 case pragma_compound:
38493 cp_parser_omp_taskwait (parser, pragma_tok);
38494 return false;
38495 case pragma_stmt:
38496 error_at (pragma_tok->location,
38497 "%<#pragma %s%> may only be used in compound statements",
38498 "omp taskwait");
38499 break;
38500 default:
38501 goto bad_stmt;
38502 }
38503 break;
38504
38505 case PRAGMA_OMP_TASKYIELD:
38506 switch (context)
38507 {
38508 case pragma_compound:
38509 cp_parser_omp_taskyield (parser, pragma_tok);
38510 return false;
38511 case pragma_stmt:
38512 error_at (pragma_tok->location,
38513 "%<#pragma %s%> may only be used in compound statements",
38514 "omp taskyield");
38515 break;
38516 default:
38517 goto bad_stmt;
38518 }
38519 break;
38520
38521 case PRAGMA_OMP_CANCEL:
38522 switch (context)
38523 {
38524 case pragma_compound:
38525 cp_parser_omp_cancel (parser, pragma_tok);
38526 return false;
38527 case pragma_stmt:
38528 error_at (pragma_tok->location,
38529 "%<#pragma %s%> may only be used in compound statements",
38530 "omp cancel");
38531 break;
38532 default:
38533 goto bad_stmt;
38534 }
38535 break;
38536
38537 case PRAGMA_OMP_CANCELLATION_POINT:
38538 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38539 return false;
38540
38541 case PRAGMA_OMP_THREADPRIVATE:
38542 cp_parser_omp_threadprivate (parser, pragma_tok);
38543 return false;
38544
38545 case PRAGMA_OMP_DECLARE:
38546 return cp_parser_omp_declare (parser, pragma_tok, context);
38547
38548 case PRAGMA_OACC_DECLARE:
38549 cp_parser_oacc_declare (parser, pragma_tok);
38550 return false;
38551
38552 case PRAGMA_OACC_ENTER_DATA:
38553 if (context == pragma_stmt)
38554 {
38555 error_at (pragma_tok->location,
38556 "%<#pragma %s%> may only be used in compound statements",
38557 "acc enter data");
38558 break;
38559 }
38560 else if (context != pragma_compound)
38561 goto bad_stmt;
38562 cp_parser_omp_construct (parser, pragma_tok, if_p);
38563 return true;
38564
38565 case PRAGMA_OACC_EXIT_DATA:
38566 if (context == pragma_stmt)
38567 {
38568 error_at (pragma_tok->location,
38569 "%<#pragma %s%> may only be used in compound statements",
38570 "acc exit data");
38571 break;
38572 }
38573 else if (context != pragma_compound)
38574 goto bad_stmt;
38575 cp_parser_omp_construct (parser, pragma_tok, if_p);
38576 return true;
38577
38578 case PRAGMA_OACC_ROUTINE:
38579 if (context != pragma_external)
38580 {
38581 error_at (pragma_tok->location,
38582 "%<#pragma acc routine%> must be at file scope");
38583 break;
38584 }
38585 cp_parser_oacc_routine (parser, pragma_tok, context);
38586 return false;
38587
38588 case PRAGMA_OACC_UPDATE:
38589 if (context == pragma_stmt)
38590 {
38591 error_at (pragma_tok->location,
38592 "%<#pragma %s%> may only be used in compound statements",
38593 "acc update");
38594 break;
38595 }
38596 else if (context != pragma_compound)
38597 goto bad_stmt;
38598 cp_parser_omp_construct (parser, pragma_tok, if_p);
38599 return true;
38600
38601 case PRAGMA_OACC_WAIT:
38602 if (context == pragma_stmt)
38603 {
38604 error_at (pragma_tok->location,
38605 "%<#pragma %s%> may only be used in compound statements",
38606 "acc wait");
38607 break;
38608 }
38609 else if (context != pragma_compound)
38610 goto bad_stmt;
38611 cp_parser_omp_construct (parser, pragma_tok, if_p);
38612 return true;
38613
38614 case PRAGMA_OACC_ATOMIC:
38615 case PRAGMA_OACC_CACHE:
38616 case PRAGMA_OACC_DATA:
38617 case PRAGMA_OACC_HOST_DATA:
38618 case PRAGMA_OACC_KERNELS:
38619 case PRAGMA_OACC_PARALLEL:
38620 case PRAGMA_OACC_LOOP:
38621 case PRAGMA_OMP_ATOMIC:
38622 case PRAGMA_OMP_CRITICAL:
38623 case PRAGMA_OMP_DISTRIBUTE:
38624 case PRAGMA_OMP_FOR:
38625 case PRAGMA_OMP_MASTER:
38626 case PRAGMA_OMP_PARALLEL:
38627 case PRAGMA_OMP_SECTIONS:
38628 case PRAGMA_OMP_SIMD:
38629 case PRAGMA_OMP_SINGLE:
38630 case PRAGMA_OMP_TASK:
38631 case PRAGMA_OMP_TASKGROUP:
38632 case PRAGMA_OMP_TASKLOOP:
38633 case PRAGMA_OMP_TEAMS:
38634 if (context != pragma_stmt && context != pragma_compound)
38635 goto bad_stmt;
38636 stmt = push_omp_privatization_clauses (false);
38637 cp_parser_omp_construct (parser, pragma_tok, if_p);
38638 pop_omp_privatization_clauses (stmt);
38639 return true;
38640
38641 case PRAGMA_OMP_ORDERED:
38642 if (context != pragma_stmt && context != pragma_compound)
38643 goto bad_stmt;
38644 stmt = push_omp_privatization_clauses (false);
38645 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38646 pop_omp_privatization_clauses (stmt);
38647 return ret;
38648
38649 case PRAGMA_OMP_TARGET:
38650 if (context != pragma_stmt && context != pragma_compound)
38651 goto bad_stmt;
38652 stmt = push_omp_privatization_clauses (false);
38653 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38654 pop_omp_privatization_clauses (stmt);
38655 return ret;
38656
38657 case PRAGMA_OMP_END_DECLARE_TARGET:
38658 cp_parser_omp_end_declare_target (parser, pragma_tok);
38659 return false;
38660
38661 case PRAGMA_OMP_SECTION:
38662 error_at (pragma_tok->location,
38663 "%<#pragma omp section%> may only be used in "
38664 "%<#pragma omp sections%> construct");
38665 break;
38666
38667 case PRAGMA_IVDEP:
38668 {
38669 if (context == pragma_external)
38670 {
38671 error_at (pragma_tok->location,
38672 "%<#pragma GCC ivdep%> must be inside a function");
38673 break;
38674 }
38675 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38676 cp_token *tok;
38677 tok = cp_lexer_peek_token (the_parser->lexer);
38678 if (tok->type != CPP_KEYWORD
38679 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38680 && tok->keyword != RID_DO))
38681 {
38682 cp_parser_error (parser, "for, while or do statement expected");
38683 return false;
38684 }
38685 cp_parser_iteration_statement (parser, if_p, true);
38686 return true;
38687 }
38688
38689 default:
38690 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38691 c_invoke_pragma_handler (id);
38692 break;
38693
38694 bad_stmt:
38695 cp_parser_error (parser, "expected declaration specifiers");
38696 break;
38697 }
38698
38699 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38700 return false;
38701 }
38702
38703 /* The interface the pragma parsers have to the lexer. */
38704
38705 enum cpp_ttype
38706 pragma_lex (tree *value, location_t *loc)
38707 {
38708 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38709 enum cpp_ttype ret = tok->type;
38710
38711 *value = tok->u.value;
38712 if (loc)
38713 *loc = tok->location;
38714
38715 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38716 ret = CPP_EOF;
38717 else if (ret == CPP_STRING)
38718 *value = cp_parser_string_literal (the_parser, false, false);
38719 else
38720 {
38721 if (ret == CPP_KEYWORD)
38722 ret = CPP_NAME;
38723 cp_lexer_consume_token (the_parser->lexer);
38724 }
38725
38726 return ret;
38727 }
38728
38729 \f
38730 /* External interface. */
38731
38732 /* Parse one entire translation unit. */
38733
38734 void
38735 c_parse_file (void)
38736 {
38737 static bool already_called = false;
38738
38739 if (already_called)
38740 fatal_error (input_location,
38741 "inter-module optimizations not implemented for C++");
38742 already_called = true;
38743
38744 the_parser = cp_parser_new ();
38745 push_deferring_access_checks (flag_access_control
38746 ? dk_no_deferred : dk_no_check);
38747 cp_parser_translation_unit (the_parser);
38748 the_parser = NULL;
38749 }
38750
38751 /* Create an identifier for a generic parameter type (a synthesized
38752 template parameter implied by `auto' or a concept identifier). */
38753
38754 static GTY(()) int generic_parm_count;
38755 static tree
38756 make_generic_type_name ()
38757 {
38758 char buf[32];
38759 sprintf (buf, "auto:%d", ++generic_parm_count);
38760 return get_identifier (buf);
38761 }
38762
38763 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38764 (creating a new template parameter list if necessary). Returns the newly
38765 created template type parm. */
38766
38767 static tree
38768 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38769 {
38770 gcc_assert (current_binding_level->kind == sk_function_parms);
38771
38772 /* Before committing to modifying any scope, if we're in an
38773 implicit template scope, and we're trying to synthesize a
38774 constrained parameter, try to find a previous parameter with
38775 the same name. This is the same-type rule for abbreviated
38776 function templates.
38777
38778 NOTE: We can generate implicit parameters when tentatively
38779 parsing a nested name specifier, only to reject that parse
38780 later. However, matching the same template-id as part of a
38781 direct-declarator should generate an identical template
38782 parameter, so this rule will merge them. */
38783 if (parser->implicit_template_scope && constr)
38784 {
38785 tree t = parser->implicit_template_parms;
38786 while (t)
38787 {
38788 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38789 {
38790 tree d = TREE_VALUE (t);
38791 if (TREE_CODE (d) == PARM_DECL)
38792 /* Return the TEMPLATE_PARM_INDEX. */
38793 d = DECL_INITIAL (d);
38794 return d;
38795 }
38796 t = TREE_CHAIN (t);
38797 }
38798 }
38799
38800 /* We are either continuing a function template that already contains implicit
38801 template parameters, creating a new fully-implicit function template, or
38802 extending an existing explicit function template with implicit template
38803 parameters. */
38804
38805 cp_binding_level *const entry_scope = current_binding_level;
38806
38807 bool become_template = false;
38808 cp_binding_level *parent_scope = 0;
38809
38810 if (parser->implicit_template_scope)
38811 {
38812 gcc_assert (parser->implicit_template_parms);
38813
38814 current_binding_level = parser->implicit_template_scope;
38815 }
38816 else
38817 {
38818 /* Roll back to the existing template parameter scope (in the case of
38819 extending an explicit function template) or introduce a new template
38820 parameter scope ahead of the function parameter scope (or class scope
38821 in the case of out-of-line member definitions). The function scope is
38822 added back after template parameter synthesis below. */
38823
38824 cp_binding_level *scope = entry_scope;
38825
38826 while (scope->kind == sk_function_parms)
38827 {
38828 parent_scope = scope;
38829 scope = scope->level_chain;
38830 }
38831 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38832 {
38833 /* If not defining a class, then any class scope is a scope level in
38834 an out-of-line member definition. In this case simply wind back
38835 beyond the first such scope to inject the template parameter list.
38836 Otherwise wind back to the class being defined. The latter can
38837 occur in class member friend declarations such as:
38838
38839 class A {
38840 void foo (auto);
38841 };
38842 class B {
38843 friend void A::foo (auto);
38844 };
38845
38846 The template parameter list synthesized for the friend declaration
38847 must be injected in the scope of 'B'. This can also occur in
38848 erroneous cases such as:
38849
38850 struct A {
38851 struct B {
38852 void foo (auto);
38853 };
38854 void B::foo (auto) {}
38855 };
38856
38857 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38858 but, nevertheless, the template parameter list synthesized for the
38859 declarator should be injected into the scope of 'A' as if the
38860 ill-formed template was specified explicitly. */
38861
38862 while (scope->kind == sk_class && !scope->defining_class_p)
38863 {
38864 parent_scope = scope;
38865 scope = scope->level_chain;
38866 }
38867 }
38868
38869 current_binding_level = scope;
38870
38871 if (scope->kind != sk_template_parms
38872 || !function_being_declared_is_template_p (parser))
38873 {
38874 /* Introduce a new template parameter list for implicit template
38875 parameters. */
38876
38877 become_template = true;
38878
38879 parser->implicit_template_scope
38880 = begin_scope (sk_template_parms, NULL);
38881
38882 ++processing_template_decl;
38883
38884 parser->fully_implicit_function_template_p = true;
38885 ++parser->num_template_parameter_lists;
38886 }
38887 else
38888 {
38889 /* Synthesize implicit template parameters at the end of the explicit
38890 template parameter list. */
38891
38892 gcc_assert (current_template_parms);
38893
38894 parser->implicit_template_scope = scope;
38895
38896 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38897 parser->implicit_template_parms
38898 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38899 }
38900 }
38901
38902 /* Synthesize a new template parameter and track the current template
38903 parameter chain with implicit_template_parms. */
38904
38905 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38906 tree synth_id = make_generic_type_name ();
38907 tree synth_tmpl_parm;
38908 bool non_type = false;
38909
38910 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38911 synth_tmpl_parm
38912 = finish_template_type_parm (class_type_node, synth_id);
38913 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38914 synth_tmpl_parm
38915 = finish_constrained_template_template_parm (proto, synth_id);
38916 else
38917 {
38918 synth_tmpl_parm = copy_decl (proto);
38919 DECL_NAME (synth_tmpl_parm) = synth_id;
38920 non_type = true;
38921 }
38922
38923 // Attach the constraint to the parm before processing.
38924 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38925 TREE_TYPE (node) = constr;
38926 tree new_parm
38927 = process_template_parm (parser->implicit_template_parms,
38928 input_location,
38929 node,
38930 /*non_type=*/non_type,
38931 /*param_pack=*/false);
38932
38933 // Chain the new parameter to the list of implicit parameters.
38934 if (parser->implicit_template_parms)
38935 parser->implicit_template_parms
38936 = TREE_CHAIN (parser->implicit_template_parms);
38937 else
38938 parser->implicit_template_parms = new_parm;
38939
38940 tree new_decl = get_local_decls ();
38941 if (non_type)
38942 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38943 new_decl = DECL_INITIAL (new_decl);
38944
38945 /* If creating a fully implicit function template, start the new implicit
38946 template parameter list with this synthesized type, otherwise grow the
38947 current template parameter list. */
38948
38949 if (become_template)
38950 {
38951 parent_scope->level_chain = current_binding_level;
38952
38953 tree new_parms = make_tree_vec (1);
38954 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38955 current_template_parms = tree_cons (size_int (processing_template_decl),
38956 new_parms, current_template_parms);
38957 }
38958 else
38959 {
38960 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38961 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38962 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38963 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38964 }
38965
38966 // If the new parameter was constrained, we need to add that to the
38967 // constraints in the template parameter list.
38968 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38969 {
38970 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38971 reqs = conjoin_constraints (reqs, req);
38972 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38973 }
38974
38975 current_binding_level = entry_scope;
38976
38977 return new_decl;
38978 }
38979
38980 /* Finish the declaration of a fully implicit function template. Such a
38981 template has no explicit template parameter list so has not been through the
38982 normal template head and tail processing. synthesize_implicit_template_parm
38983 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38984 provided if the declaration is a class member such that its template
38985 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38986 form is returned. Otherwise NULL_TREE is returned. */
38987
38988 static tree
38989 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38990 {
38991 gcc_assert (parser->fully_implicit_function_template_p);
38992
38993 if (member_decl_opt && member_decl_opt != error_mark_node
38994 && DECL_VIRTUAL_P (member_decl_opt))
38995 {
38996 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38997 "implicit templates may not be %<virtual%>");
38998 DECL_VIRTUAL_P (member_decl_opt) = false;
38999 }
39000
39001 if (member_decl_opt)
39002 member_decl_opt = finish_member_template_decl (member_decl_opt);
39003 end_template_decl ();
39004
39005 parser->fully_implicit_function_template_p = false;
39006 --parser->num_template_parameter_lists;
39007
39008 return member_decl_opt;
39009 }
39010
39011 /* Helper function for diagnostics that have complained about things
39012 being used with 'extern "C"' linkage.
39013
39014 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39015
39016 void
39017 maybe_show_extern_c_location (void)
39018 {
39019 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39020 inform (the_parser->innermost_linkage_specification_location,
39021 "%<extern \"C\"%> linkage started here");
39022 }
39023
39024 #include "gt-cp-parser.h"