re PR c++/69095 (internal compiler error: in dependent_type_p, at cp/pt.c:19399)
[gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2016 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "omp-low.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
41 #include "context.h"
42 #include "cp-cilkplus.h"
43
44 \f
45 /* The lexer. */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
49
50 static cp_token eof_token =
51 {
52 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 enum non_integral_constant {
57 NIC_NONE,
58 /* floating-point literal */
59 NIC_FLOAT,
60 /* %<this%> */
61 NIC_THIS,
62 /* %<__FUNCTION__%> */
63 NIC_FUNC_NAME,
64 /* %<__PRETTY_FUNCTION__%> */
65 NIC_PRETTY_FUNC,
66 /* %<__func__%> */
67 NIC_C99_FUNC,
68 /* "%<va_arg%> */
69 NIC_VA_ARG,
70 /* a cast */
71 NIC_CAST,
72 /* %<typeid%> operator */
73 NIC_TYPEID,
74 /* non-constant compound literals */
75 NIC_NCC,
76 /* a function call */
77 NIC_FUNC_CALL,
78 /* an increment */
79 NIC_INC,
80 /* an decrement */
81 NIC_DEC,
82 /* an array reference */
83 NIC_ARRAY_REF,
84 /* %<->%> */
85 NIC_ARROW,
86 /* %<.%> */
87 NIC_POINT,
88 /* the address of a label */
89 NIC_ADDR_LABEL,
90 /* %<*%> */
91 NIC_STAR,
92 /* %<&%> */
93 NIC_ADDR,
94 /* %<++%> */
95 NIC_PREINCREMENT,
96 /* %<--%> */
97 NIC_PREDECREMENT,
98 /* %<new%> */
99 NIC_NEW,
100 /* %<delete%> */
101 NIC_DEL,
102 /* calls to overloaded operators */
103 NIC_OVERLOADED,
104 /* an assignment */
105 NIC_ASSIGNMENT,
106 /* a comma operator */
107 NIC_COMMA,
108 /* a call to a constructor */
109 NIC_CONSTRUCTOR,
110 /* a transaction expression */
111 NIC_TRANSACTION
112 };
113
114 /* The various kinds of errors about name-lookup failing. */
115 enum name_lookup_error {
116 /* NULL */
117 NLE_NULL,
118 /* is not a type */
119 NLE_TYPE,
120 /* is not a class or namespace */
121 NLE_CXX98,
122 /* is not a class, namespace, or enumeration */
123 NLE_NOT_CXX98
124 };
125
126 /* The various kinds of required token */
127 enum required_token {
128 RT_NONE,
129 RT_SEMICOLON, /* ';' */
130 RT_OPEN_PAREN, /* '(' */
131 RT_CLOSE_BRACE, /* '}' */
132 RT_OPEN_BRACE, /* '{' */
133 RT_CLOSE_SQUARE, /* ']' */
134 RT_OPEN_SQUARE, /* '[' */
135 RT_COMMA, /* ',' */
136 RT_SCOPE, /* '::' */
137 RT_LESS, /* '<' */
138 RT_GREATER, /* '>' */
139 RT_EQ, /* '=' */
140 RT_ELLIPSIS, /* '...' */
141 RT_MULT, /* '*' */
142 RT_COMPL, /* '~' */
143 RT_COLON, /* ':' */
144 RT_COLON_SCOPE, /* ':' or '::' */
145 RT_CLOSE_PAREN, /* ')' */
146 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147 RT_PRAGMA_EOL, /* end of line */
148 RT_NAME, /* identifier */
149
150 /* The type is CPP_KEYWORD */
151 RT_NEW, /* new */
152 RT_DELETE, /* delete */
153 RT_RETURN, /* return */
154 RT_WHILE, /* while */
155 RT_EXTERN, /* extern */
156 RT_STATIC_ASSERT, /* static_assert */
157 RT_DECLTYPE, /* decltype */
158 RT_OPERATOR, /* operator */
159 RT_CLASS, /* class */
160 RT_TEMPLATE, /* template */
161 RT_NAMESPACE, /* namespace */
162 RT_USING, /* using */
163 RT_ASM, /* asm */
164 RT_TRY, /* try */
165 RT_CATCH, /* catch */
166 RT_THROW, /* throw */
167 RT_LABEL, /* __label__ */
168 RT_AT_TRY, /* @try */
169 RT_AT_SYNCHRONIZED, /* @synchronized */
170 RT_AT_THROW, /* @throw */
171
172 RT_SELECT, /* selection-statement */
173 RT_INTERATION, /* iteration-statement */
174 RT_JUMP, /* jump-statement */
175 RT_CLASS_KEY, /* class-key */
176 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179 RT_TRANSACTION_CANCEL /* __transaction_cancel */
180 };
181
182 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
183 reverting it on destruction. */
184
185 class type_id_in_expr_sentinel
186 {
187 cp_parser *parser;
188 bool saved;
189 public:
190 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
191 : parser (parser),
192 saved (parser->in_type_id_in_expr_p)
193 { parser->in_type_id_in_expr_p = set; }
194 ~type_id_in_expr_sentinel ()
195 { parser->in_type_id_in_expr_p = saved; }
196 };
197
198 /* Prototypes. */
199
200 static cp_lexer *cp_lexer_new_main
201 (void);
202 static cp_lexer *cp_lexer_new_from_tokens
203 (cp_token_cache *tokens);
204 static void cp_lexer_destroy
205 (cp_lexer *);
206 static int cp_lexer_saving_tokens
207 (const cp_lexer *);
208 static cp_token *cp_lexer_token_at
209 (cp_lexer *, cp_token_position);
210 static void cp_lexer_get_preprocessor_token
211 (cp_lexer *, cp_token *);
212 static inline cp_token *cp_lexer_peek_token
213 (cp_lexer *);
214 static cp_token *cp_lexer_peek_nth_token
215 (cp_lexer *, size_t);
216 static inline bool cp_lexer_next_token_is
217 (cp_lexer *, enum cpp_ttype);
218 static bool cp_lexer_next_token_is_not
219 (cp_lexer *, enum cpp_ttype);
220 static bool cp_lexer_next_token_is_keyword
221 (cp_lexer *, enum rid);
222 static cp_token *cp_lexer_consume_token
223 (cp_lexer *);
224 static void cp_lexer_purge_token
225 (cp_lexer *);
226 static void cp_lexer_purge_tokens_after
227 (cp_lexer *, cp_token_position);
228 static void cp_lexer_save_tokens
229 (cp_lexer *);
230 static void cp_lexer_commit_tokens
231 (cp_lexer *);
232 static void cp_lexer_rollback_tokens
233 (cp_lexer *);
234 static void cp_lexer_print_token
235 (FILE *, cp_token *);
236 static inline bool cp_lexer_debugging_p
237 (cp_lexer *);
238 static void cp_lexer_start_debugging
239 (cp_lexer *) ATTRIBUTE_UNUSED;
240 static void cp_lexer_stop_debugging
241 (cp_lexer *) ATTRIBUTE_UNUSED;
242
243 static cp_token_cache *cp_token_cache_new
244 (cp_token *, cp_token *);
245
246 static void cp_parser_initial_pragma
247 (cp_token *);
248
249 static tree cp_literal_operator_id
250 (const char *);
251
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
262
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
266
267 /* Variables. */
268
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream;
271
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand;
275
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
281
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
286 {
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
290
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
292
293 if (buffer == NULL)
294 return;
295
296 if (num == 0)
297 num = buffer->length ();
298
299 if (start_token == NULL)
300 start_token = buffer->address ();
301
302 if (start_token > buffer->address ())
303 {
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
306 }
307
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
311 {
312 if (token == start_token)
313 do_print = true;
314
315 if (!do_print)
316 continue;
317
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
321
322 cp_lexer_print_token (file, token);
323
324 if (token == curr_token)
325 fprintf (file, "]]");
326
327 switch (token->type)
328 {
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
335
336 default:
337 fputc (' ', file);
338 }
339 }
340
341 if (i == num && i < buffer->length ())
342 {
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
345 }
346
347 fprintf (file, "\n");
348 }
349
350
351 /* Dump all tokens in BUFFER to stderr. */
352
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
355 {
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
357 }
358
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
361 {
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
363 }
364
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
367 {
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
372 }
373
374
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
377
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
380 {
381 if (t)
382 {
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
385 }
386 }
387
388
389 /* Dump parser context C to FILE. */
390
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
393 {
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
398 }
399
400
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
402
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
405 {
406 unsigned i;
407 cp_parser_context *c;
408
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
411 {
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
414 }
415 }
416
417
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
419
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
422 {
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
425 }
426
427
428 /* Print an unparsed function entry UF to FILE. */
429
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
432 {
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
436
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
441 {
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
447 }
448
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
452 {
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
455 }
456 fprintf (file, "\n");
457
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
461 {
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
464 }
465 fprintf (file, "\n");
466 }
467
468
469 /* Print the stack of unparsed member functions S to FILE. */
470
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
474 {
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
477
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
480 {
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
483 }
484 }
485
486
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
489
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
492 {
493 cp_token *next_token, *first_token, *start_token;
494
495 if (file == NULL)
496 file = stderr;
497
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
505 }
506
507
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
510
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
513 {
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
517
518 if (file == NULL)
519 file = stderr;
520
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
594 }
595
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
598 {
599 cp_debug_parser (stderr, &ref);
600 }
601
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
604 {
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
609 }
610
611 /* Allocate memory for a new lexer object and return it. */
612
613 static cp_lexer *
614 cp_lexer_alloc (void)
615 {
616 cp_lexer *lexer;
617
618 c_common_no_more_pch ();
619
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
622
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
625
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
627
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
630
631 return lexer;
632 }
633
634
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
637
638 static cp_lexer *
639 cp_lexer_new_main (void)
640 {
641 cp_lexer *lexer;
642 cp_token token;
643
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
648
649 lexer = cp_lexer_alloc ();
650
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
653
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
656 {
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
659 }
660
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
667
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
671
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
674 }
675
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
678
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
681 {
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
685
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
690
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
692
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
695
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
698 }
699
700 /* Frees all resources associated with LEXER. */
701
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
704 {
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
708 }
709
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
714
715 #define LEXER_DEBUGGING_ENABLED_P false
716
717 /* Returns nonzero if debugging information should be output. */
718
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
721 {
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
724
725 return lexer->debugging_p;
726 }
727
728
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
731 {
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
733
734 return lexer->next_token - previous_p;
735 }
736
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
739 {
740 return pos;
741 }
742
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
745 {
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
747 }
748
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
751 {
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
756 }
757
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
760 {
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
762
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
765 {
766 gcc_assert (tp != lexer->buffer->address ());
767 tp--;
768 }
769
770 return cp_lexer_token_at (lexer, tp);
771 }
772
773 /* nonzero if we are presently saving tokens. */
774
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
777 {
778 return lexer->saved_tokens.length () != 0;
779 }
780
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
785
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
788 {
789 static int is_extern_c = 0;
790
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
798
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
805
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
808 {
809 if (C_IS_RESERVED_WORD (token->u.value))
810 {
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
815 }
816 else
817 {
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
821 {
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
827
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
831 }
832
833 token->keyword = RID_MAX;
834 }
835 }
836 else if (token->type == CPP_AT_NAME)
837 {
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
841 {
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
849 */
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
859 }
860 }
861 }
862
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
866 {
867 if (token->type != CPP_EOF)
868 {
869 input_location = token->location;
870 }
871 }
872
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
876 {
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
879 }
880
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
883
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
886 {
887 if (cp_lexer_debugging_p (lexer))
888 {
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
892 }
893 return lexer->next_token;
894 }
895
896 /* Return true if the next token has the indicated TYPE. */
897
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
900 {
901 return cp_lexer_peek_token (lexer)->type == type;
902 }
903
904 /* Return true if the next token does not have the indicated TYPE. */
905
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
908 {
909 return !cp_lexer_next_token_is (lexer, type);
910 }
911
912 /* Return true if the next token is the indicated KEYWORD. */
913
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
916 {
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
918 }
919
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
922 {
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
924 }
925
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
928 {
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
930 }
931
932 /* Return true if the next token is not the indicated KEYWORD. */
933
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
936 {
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
938 }
939
940 /* Return true if the next token is a keyword for a decl-specifier. */
941
942 static bool
943 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
944 {
945 cp_token *token;
946
947 token = cp_lexer_peek_token (lexer);
948 switch (token->keyword)
949 {
950 /* auto specifier: storage-class-specifier in C++,
951 simple-type-specifier in C++0x. */
952 case RID_AUTO:
953 /* Storage classes. */
954 case RID_REGISTER:
955 case RID_STATIC:
956 case RID_EXTERN:
957 case RID_MUTABLE:
958 case RID_THREAD:
959 /* Elaborated type specifiers. */
960 case RID_ENUM:
961 case RID_CLASS:
962 case RID_STRUCT:
963 case RID_UNION:
964 case RID_TYPENAME:
965 /* Simple type specifiers. */
966 case RID_CHAR:
967 case RID_CHAR16:
968 case RID_CHAR32:
969 case RID_WCHAR:
970 case RID_BOOL:
971 case RID_SHORT:
972 case RID_INT:
973 case RID_LONG:
974 case RID_SIGNED:
975 case RID_UNSIGNED:
976 case RID_FLOAT:
977 case RID_DOUBLE:
978 case RID_VOID:
979 /* GNU extensions. */
980 case RID_ATTRIBUTE:
981 case RID_TYPEOF:
982 /* C++0x extensions. */
983 case RID_DECLTYPE:
984 case RID_UNDERLYING_TYPE:
985 return true;
986
987 default:
988 if (token->keyword >= RID_FIRST_INT_N
989 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
990 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
991 return true;
992 return false;
993 }
994 }
995
996 /* Returns TRUE iff the token T begins a decltype type. */
997
998 static bool
999 token_is_decltype (cp_token *t)
1000 {
1001 return (t->keyword == RID_DECLTYPE
1002 || t->type == CPP_DECLTYPE);
1003 }
1004
1005 /* Returns TRUE iff the next token begins a decltype type. */
1006
1007 static bool
1008 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1009 {
1010 cp_token *t = cp_lexer_peek_token (lexer);
1011 return token_is_decltype (t);
1012 }
1013
1014 /* Called when processing a token with tree_check_value; perform or defer the
1015 associated checks and return the value. */
1016
1017 static tree
1018 saved_checks_value (struct tree_check *check_value)
1019 {
1020 /* Perform any access checks that were deferred. */
1021 vec<deferred_access_check, va_gc> *checks;
1022 deferred_access_check *chk;
1023 checks = check_value->checks;
1024 if (checks)
1025 {
1026 int i;
1027 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1028 perform_or_defer_access_check (chk->binfo,
1029 chk->decl,
1030 chk->diag_decl, tf_warning_or_error);
1031 }
1032 /* Return the stored value. */
1033 return check_value->value;
1034 }
1035
1036 /* Return a pointer to the Nth token in the token stream. If N is 1,
1037 then this is precisely equivalent to cp_lexer_peek_token (except
1038 that it is not inline). One would like to disallow that case, but
1039 there is one case (cp_parser_nth_token_starts_template_id) where
1040 the caller passes a variable for N and it might be 1. */
1041
1042 static cp_token *
1043 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1044 {
1045 cp_token *token;
1046
1047 /* N is 1-based, not zero-based. */
1048 gcc_assert (n > 0);
1049
1050 if (cp_lexer_debugging_p (lexer))
1051 fprintf (cp_lexer_debug_stream,
1052 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1053
1054 --n;
1055 token = lexer->next_token;
1056 gcc_assert (!n || token != &eof_token);
1057 while (n != 0)
1058 {
1059 ++token;
1060 if (token == lexer->last_token)
1061 {
1062 token = &eof_token;
1063 break;
1064 }
1065
1066 if (!token->purged_p)
1067 --n;
1068 }
1069
1070 if (cp_lexer_debugging_p (lexer))
1071 {
1072 cp_lexer_print_token (cp_lexer_debug_stream, token);
1073 putc ('\n', cp_lexer_debug_stream);
1074 }
1075
1076 return token;
1077 }
1078
1079 /* Return the next token, and advance the lexer's next_token pointer
1080 to point to the next non-purged token. */
1081
1082 static cp_token *
1083 cp_lexer_consume_token (cp_lexer* lexer)
1084 {
1085 cp_token *token = lexer->next_token;
1086
1087 gcc_assert (token != &eof_token);
1088 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1089
1090 do
1091 {
1092 lexer->next_token++;
1093 if (lexer->next_token == lexer->last_token)
1094 {
1095 lexer->next_token = &eof_token;
1096 break;
1097 }
1098
1099 }
1100 while (lexer->next_token->purged_p);
1101
1102 cp_lexer_set_source_position_from_token (token);
1103
1104 /* Provide debugging output. */
1105 if (cp_lexer_debugging_p (lexer))
1106 {
1107 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1108 cp_lexer_print_token (cp_lexer_debug_stream, token);
1109 putc ('\n', cp_lexer_debug_stream);
1110 }
1111
1112 return token;
1113 }
1114
1115 /* Permanently remove the next token from the token stream, and
1116 advance the next_token pointer to refer to the next non-purged
1117 token. */
1118
1119 static void
1120 cp_lexer_purge_token (cp_lexer *lexer)
1121 {
1122 cp_token *tok = lexer->next_token;
1123
1124 gcc_assert (tok != &eof_token);
1125 tok->purged_p = true;
1126 tok->location = UNKNOWN_LOCATION;
1127 tok->u.value = NULL_TREE;
1128 tok->keyword = RID_MAX;
1129
1130 do
1131 {
1132 tok++;
1133 if (tok == lexer->last_token)
1134 {
1135 tok = &eof_token;
1136 break;
1137 }
1138 }
1139 while (tok->purged_p);
1140 lexer->next_token = tok;
1141 }
1142
1143 /* Permanently remove all tokens after TOK, up to, but not
1144 including, the token that will be returned next by
1145 cp_lexer_peek_token. */
1146
1147 static void
1148 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1149 {
1150 cp_token *peek = lexer->next_token;
1151
1152 if (peek == &eof_token)
1153 peek = lexer->last_token;
1154
1155 gcc_assert (tok < peek);
1156
1157 for ( tok += 1; tok != peek; tok += 1)
1158 {
1159 tok->purged_p = true;
1160 tok->location = UNKNOWN_LOCATION;
1161 tok->u.value = NULL_TREE;
1162 tok->keyword = RID_MAX;
1163 }
1164 }
1165
1166 /* Begin saving tokens. All tokens consumed after this point will be
1167 preserved. */
1168
1169 static void
1170 cp_lexer_save_tokens (cp_lexer* lexer)
1171 {
1172 /* Provide debugging output. */
1173 if (cp_lexer_debugging_p (lexer))
1174 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1175
1176 lexer->saved_tokens.safe_push (lexer->next_token);
1177 }
1178
1179 /* Commit to the portion of the token stream most recently saved. */
1180
1181 static void
1182 cp_lexer_commit_tokens (cp_lexer* lexer)
1183 {
1184 /* Provide debugging output. */
1185 if (cp_lexer_debugging_p (lexer))
1186 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1187
1188 lexer->saved_tokens.pop ();
1189 }
1190
1191 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1192 to the token stream. Stop saving tokens. */
1193
1194 static void
1195 cp_lexer_rollback_tokens (cp_lexer* lexer)
1196 {
1197 /* Provide debugging output. */
1198 if (cp_lexer_debugging_p (lexer))
1199 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1200
1201 lexer->next_token = lexer->saved_tokens.pop ();
1202 }
1203
1204 /* RAII wrapper around the above functions, with sanity checking. Creating
1205 a variable saves tokens, which are committed when the variable is
1206 destroyed unless they are explicitly rolled back by calling the rollback
1207 member function. */
1208
1209 struct saved_token_sentinel
1210 {
1211 cp_lexer *lexer;
1212 unsigned len;
1213 bool commit;
1214 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1215 {
1216 len = lexer->saved_tokens.length ();
1217 cp_lexer_save_tokens (lexer);
1218 }
1219 void rollback ()
1220 {
1221 cp_lexer_rollback_tokens (lexer);
1222 commit = false;
1223 }
1224 ~saved_token_sentinel()
1225 {
1226 if (commit)
1227 cp_lexer_commit_tokens (lexer);
1228 gcc_assert (lexer->saved_tokens.length () == len);
1229 }
1230 };
1231
1232 /* Print a representation of the TOKEN on the STREAM. */
1233
1234 static void
1235 cp_lexer_print_token (FILE * stream, cp_token *token)
1236 {
1237 /* We don't use cpp_type2name here because the parser defines
1238 a few tokens of its own. */
1239 static const char *const token_names[] = {
1240 /* cpplib-defined token types */
1241 #define OP(e, s) #e,
1242 #define TK(e, s) #e,
1243 TTYPE_TABLE
1244 #undef OP
1245 #undef TK
1246 /* C++ parser token types - see "Manifest constants", above. */
1247 "KEYWORD",
1248 "TEMPLATE_ID",
1249 "NESTED_NAME_SPECIFIER",
1250 };
1251
1252 /* For some tokens, print the associated data. */
1253 switch (token->type)
1254 {
1255 case CPP_KEYWORD:
1256 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1257 For example, `struct' is mapped to an INTEGER_CST. */
1258 if (!identifier_p (token->u.value))
1259 break;
1260 /* else fall through */
1261 case CPP_NAME:
1262 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1263 break;
1264
1265 case CPP_STRING:
1266 case CPP_STRING16:
1267 case CPP_STRING32:
1268 case CPP_WSTRING:
1269 case CPP_UTF8STRING:
1270 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1271 break;
1272
1273 case CPP_NUMBER:
1274 print_generic_expr (stream, token->u.value, 0);
1275 break;
1276
1277 default:
1278 /* If we have a name for the token, print it out. Otherwise, we
1279 simply give the numeric code. */
1280 if (token->type < ARRAY_SIZE(token_names))
1281 fputs (token_names[token->type], stream);
1282 else
1283 fprintf (stream, "[%d]", token->type);
1284 break;
1285 }
1286 }
1287
1288 DEBUG_FUNCTION void
1289 debug (cp_token &ref)
1290 {
1291 cp_lexer_print_token (stderr, &ref);
1292 fprintf (stderr, "\n");
1293 }
1294
1295 DEBUG_FUNCTION void
1296 debug (cp_token *ptr)
1297 {
1298 if (ptr)
1299 debug (*ptr);
1300 else
1301 fprintf (stderr, "<nil>\n");
1302 }
1303
1304
1305 /* Start emitting debugging information. */
1306
1307 static void
1308 cp_lexer_start_debugging (cp_lexer* lexer)
1309 {
1310 if (!LEXER_DEBUGGING_ENABLED_P)
1311 fatal_error (input_location,
1312 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1313
1314 lexer->debugging_p = true;
1315 cp_lexer_debug_stream = stderr;
1316 }
1317
1318 /* Stop emitting debugging information. */
1319
1320 static void
1321 cp_lexer_stop_debugging (cp_lexer* lexer)
1322 {
1323 if (!LEXER_DEBUGGING_ENABLED_P)
1324 fatal_error (input_location,
1325 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1326
1327 lexer->debugging_p = false;
1328 cp_lexer_debug_stream = NULL;
1329 }
1330
1331 /* Create a new cp_token_cache, representing a range of tokens. */
1332
1333 static cp_token_cache *
1334 cp_token_cache_new (cp_token *first, cp_token *last)
1335 {
1336 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1337 cache->first = first;
1338 cache->last = last;
1339 return cache;
1340 }
1341
1342 /* Diagnose if #pragma omp declare simd isn't followed immediately
1343 by function declaration or definition. */
1344
1345 static inline void
1346 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1347 {
1348 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1349 {
1350 error ("%<#pragma omp declare simd%> not immediately followed by "
1351 "function declaration or definition");
1352 parser->omp_declare_simd = NULL;
1353 }
1354 }
1355
1356 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1357 and put that into "omp declare simd" attribute. */
1358
1359 static inline void
1360 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1361 {
1362 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1363 {
1364 if (fndecl == error_mark_node)
1365 {
1366 parser->omp_declare_simd = NULL;
1367 return;
1368 }
1369 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1370 {
1371 cp_ensure_no_omp_declare_simd (parser);
1372 return;
1373 }
1374 }
1375 }
1376
1377 /* Diagnose if #pragma acc routine isn't followed immediately by function
1378 declaration or definition. */
1379
1380 static inline void
1381 cp_ensure_no_oacc_routine (cp_parser *parser)
1382 {
1383 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1384 {
1385 tree clauses = parser->oacc_routine->clauses;
1386 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
1387
1388 error_at (loc, "%<#pragma acc routine%> not followed by a function "
1389 "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->attributes = NULL_TREE;
1448 declarator->std_attributes = NULL_TREE;
1449 declarator->declarator = NULL;
1450 declarator->parameter_pack_p = false;
1451 declarator->id_loc = UNKNOWN_LOCATION;
1452
1453 return declarator;
1454 }
1455
1456 /* Make a declarator for a generalized identifier. If
1457 QUALIFYING_SCOPE is non-NULL, the identifier is
1458 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1459 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1460 is, if any. */
1461
1462 static cp_declarator *
1463 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1464 special_function_kind sfk)
1465 {
1466 cp_declarator *declarator;
1467
1468 /* It is valid to write:
1469
1470 class C { void f(); };
1471 typedef C D;
1472 void D::f();
1473
1474 The standard is not clear about whether `typedef const C D' is
1475 legal; as of 2002-09-15 the committee is considering that
1476 question. EDG 3.0 allows that syntax. Therefore, we do as
1477 well. */
1478 if (qualifying_scope && TYPE_P (qualifying_scope))
1479 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1480
1481 gcc_assert (identifier_p (unqualified_name)
1482 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1483 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1484
1485 declarator = make_declarator (cdk_id);
1486 declarator->u.id.qualifying_scope = qualifying_scope;
1487 declarator->u.id.unqualified_name = unqualified_name;
1488 declarator->u.id.sfk = sfk;
1489
1490 return declarator;
1491 }
1492
1493 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1494 of modifiers such as const or volatile to apply to the pointer
1495 type, represented as identifiers. ATTRIBUTES represent the attributes that
1496 appertain to the pointer or reference. */
1497
1498 cp_declarator *
1499 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1500 tree attributes)
1501 {
1502 cp_declarator *declarator;
1503
1504 declarator = make_declarator (cdk_pointer);
1505 declarator->declarator = target;
1506 declarator->u.pointer.qualifiers = cv_qualifiers;
1507 declarator->u.pointer.class_type = NULL_TREE;
1508 if (target)
1509 {
1510 declarator->id_loc = target->id_loc;
1511 declarator->parameter_pack_p = target->parameter_pack_p;
1512 target->parameter_pack_p = false;
1513 }
1514 else
1515 declarator->parameter_pack_p = false;
1516
1517 declarator->std_attributes = attributes;
1518
1519 return declarator;
1520 }
1521
1522 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1523 represent the attributes that appertain to the pointer or
1524 reference. */
1525
1526 cp_declarator *
1527 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1528 bool rvalue_ref, tree attributes)
1529 {
1530 cp_declarator *declarator;
1531
1532 declarator = make_declarator (cdk_reference);
1533 declarator->declarator = target;
1534 declarator->u.reference.qualifiers = cv_qualifiers;
1535 declarator->u.reference.rvalue_ref = rvalue_ref;
1536 if (target)
1537 {
1538 declarator->id_loc = target->id_loc;
1539 declarator->parameter_pack_p = target->parameter_pack_p;
1540 target->parameter_pack_p = false;
1541 }
1542 else
1543 declarator->parameter_pack_p = false;
1544
1545 declarator->std_attributes = attributes;
1546
1547 return declarator;
1548 }
1549
1550 /* Like make_pointer_declarator -- but for a pointer to a non-static
1551 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1552 appertain to the pointer or reference. */
1553
1554 cp_declarator *
1555 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1556 cp_declarator *pointee,
1557 tree attributes)
1558 {
1559 cp_declarator *declarator;
1560
1561 declarator = make_declarator (cdk_ptrmem);
1562 declarator->declarator = pointee;
1563 declarator->u.pointer.qualifiers = cv_qualifiers;
1564 declarator->u.pointer.class_type = class_type;
1565
1566 if (pointee)
1567 {
1568 declarator->parameter_pack_p = pointee->parameter_pack_p;
1569 pointee->parameter_pack_p = false;
1570 }
1571 else
1572 declarator->parameter_pack_p = false;
1573
1574 declarator->std_attributes = attributes;
1575
1576 return declarator;
1577 }
1578
1579 /* Make a declarator for the function given by TARGET, with the
1580 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1581 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1582 indicates what exceptions can be thrown. */
1583
1584 cp_declarator *
1585 make_call_declarator (cp_declarator *target,
1586 tree parms,
1587 cp_cv_quals cv_qualifiers,
1588 cp_virt_specifiers virt_specifiers,
1589 cp_ref_qualifier ref_qualifier,
1590 tree tx_qualifier,
1591 tree exception_specification,
1592 tree late_return_type,
1593 tree requires_clause)
1594 {
1595 cp_declarator *declarator;
1596
1597 declarator = make_declarator (cdk_function);
1598 declarator->declarator = target;
1599 declarator->u.function.parameters = parms;
1600 declarator->u.function.qualifiers = cv_qualifiers;
1601 declarator->u.function.virt_specifiers = virt_specifiers;
1602 declarator->u.function.ref_qualifier = ref_qualifier;
1603 declarator->u.function.tx_qualifier = tx_qualifier;
1604 declarator->u.function.exception_specification = exception_specification;
1605 declarator->u.function.late_return_type = late_return_type;
1606 declarator->u.function.requires_clause = requires_clause;
1607 if (target)
1608 {
1609 declarator->id_loc = target->id_loc;
1610 declarator->parameter_pack_p = target->parameter_pack_p;
1611 target->parameter_pack_p = false;
1612 }
1613 else
1614 declarator->parameter_pack_p = false;
1615
1616 return declarator;
1617 }
1618
1619 /* Make a declarator for an array of BOUNDS elements, each of which is
1620 defined by ELEMENT. */
1621
1622 cp_declarator *
1623 make_array_declarator (cp_declarator *element, tree bounds)
1624 {
1625 cp_declarator *declarator;
1626
1627 declarator = make_declarator (cdk_array);
1628 declarator->declarator = element;
1629 declarator->u.array.bounds = bounds;
1630 if (element)
1631 {
1632 declarator->id_loc = element->id_loc;
1633 declarator->parameter_pack_p = element->parameter_pack_p;
1634 element->parameter_pack_p = false;
1635 }
1636 else
1637 declarator->parameter_pack_p = false;
1638
1639 return declarator;
1640 }
1641
1642 /* Determine whether the declarator we've seen so far can be a
1643 parameter pack, when followed by an ellipsis. */
1644 static bool
1645 declarator_can_be_parameter_pack (cp_declarator *declarator)
1646 {
1647 if (declarator && declarator->parameter_pack_p)
1648 /* We already saw an ellipsis. */
1649 return false;
1650
1651 /* Search for a declarator name, or any other declarator that goes
1652 after the point where the ellipsis could appear in a parameter
1653 pack. If we find any of these, then this declarator can not be
1654 made into a parameter pack. */
1655 bool found = false;
1656 while (declarator && !found)
1657 {
1658 switch ((int)declarator->kind)
1659 {
1660 case cdk_id:
1661 case cdk_array:
1662 found = true;
1663 break;
1664
1665 case cdk_error:
1666 return true;
1667
1668 default:
1669 declarator = declarator->declarator;
1670 break;
1671 }
1672 }
1673
1674 return !found;
1675 }
1676
1677 cp_parameter_declarator *no_parameters;
1678
1679 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1680 DECLARATOR and DEFAULT_ARGUMENT. */
1681
1682 cp_parameter_declarator *
1683 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1684 cp_declarator *declarator,
1685 tree default_argument,
1686 bool template_parameter_pack_p = false)
1687 {
1688 cp_parameter_declarator *parameter;
1689
1690 parameter = ((cp_parameter_declarator *)
1691 alloc_declarator (sizeof (cp_parameter_declarator)));
1692 parameter->next = NULL;
1693 if (decl_specifiers)
1694 parameter->decl_specifiers = *decl_specifiers;
1695 else
1696 clear_decl_specs (&parameter->decl_specifiers);
1697 parameter->declarator = declarator;
1698 parameter->default_argument = default_argument;
1699 parameter->template_parameter_pack_p = template_parameter_pack_p;
1700
1701 return parameter;
1702 }
1703
1704 /* Returns true iff DECLARATOR is a declaration for a function. */
1705
1706 static bool
1707 function_declarator_p (const cp_declarator *declarator)
1708 {
1709 while (declarator)
1710 {
1711 if (declarator->kind == cdk_function
1712 && declarator->declarator->kind == cdk_id)
1713 return true;
1714 if (declarator->kind == cdk_id
1715 || declarator->kind == cdk_error)
1716 return false;
1717 declarator = declarator->declarator;
1718 }
1719 return false;
1720 }
1721
1722 /* The parser. */
1723
1724 /* Overview
1725 --------
1726
1727 A cp_parser parses the token stream as specified by the C++
1728 grammar. Its job is purely parsing, not semantic analysis. For
1729 example, the parser breaks the token stream into declarators,
1730 expressions, statements, and other similar syntactic constructs.
1731 It does not check that the types of the expressions on either side
1732 of an assignment-statement are compatible, or that a function is
1733 not declared with a parameter of type `void'.
1734
1735 The parser invokes routines elsewhere in the compiler to perform
1736 semantic analysis and to build up the abstract syntax tree for the
1737 code processed.
1738
1739 The parser (and the template instantiation code, which is, in a
1740 way, a close relative of parsing) are the only parts of the
1741 compiler that should be calling push_scope and pop_scope, or
1742 related functions. The parser (and template instantiation code)
1743 keeps track of what scope is presently active; everything else
1744 should simply honor that. (The code that generates static
1745 initializers may also need to set the scope, in order to check
1746 access control correctly when emitting the initializers.)
1747
1748 Methodology
1749 -----------
1750
1751 The parser is of the standard recursive-descent variety. Upcoming
1752 tokens in the token stream are examined in order to determine which
1753 production to use when parsing a non-terminal. Some C++ constructs
1754 require arbitrary look ahead to disambiguate. For example, it is
1755 impossible, in the general case, to tell whether a statement is an
1756 expression or declaration without scanning the entire statement.
1757 Therefore, the parser is capable of "parsing tentatively." When the
1758 parser is not sure what construct comes next, it enters this mode.
1759 Then, while we attempt to parse the construct, the parser queues up
1760 error messages, rather than issuing them immediately, and saves the
1761 tokens it consumes. If the construct is parsed successfully, the
1762 parser "commits", i.e., it issues any queued error messages and
1763 the tokens that were being preserved are permanently discarded.
1764 If, however, the construct is not parsed successfully, the parser
1765 rolls back its state completely so that it can resume parsing using
1766 a different alternative.
1767
1768 Future Improvements
1769 -------------------
1770
1771 The performance of the parser could probably be improved substantially.
1772 We could often eliminate the need to parse tentatively by looking ahead
1773 a little bit. In some places, this approach might not entirely eliminate
1774 the need to parse tentatively, but it might still speed up the average
1775 case. */
1776
1777 /* Flags that are passed to some parsing functions. These values can
1778 be bitwise-ored together. */
1779
1780 enum
1781 {
1782 /* No flags. */
1783 CP_PARSER_FLAGS_NONE = 0x0,
1784 /* The construct is optional. If it is not present, then no error
1785 should be issued. */
1786 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1787 /* When parsing a type-specifier, treat user-defined type-names
1788 as non-type identifiers. */
1789 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1790 /* When parsing a type-specifier, do not try to parse a class-specifier
1791 or enum-specifier. */
1792 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1793 /* When parsing a decl-specifier-seq, only allow type-specifier or
1794 constexpr. */
1795 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1796 };
1797
1798 /* This type is used for parameters and variables which hold
1799 combinations of the above flags. */
1800 typedef int cp_parser_flags;
1801
1802 /* The different kinds of declarators we want to parse. */
1803
1804 enum cp_parser_declarator_kind
1805 {
1806 /* We want an abstract declarator. */
1807 CP_PARSER_DECLARATOR_ABSTRACT,
1808 /* We want a named declarator. */
1809 CP_PARSER_DECLARATOR_NAMED,
1810 /* We don't mind, but the name must be an unqualified-id. */
1811 CP_PARSER_DECLARATOR_EITHER
1812 };
1813
1814 /* The precedence values used to parse binary expressions. The minimum value
1815 of PREC must be 1, because zero is reserved to quickly discriminate
1816 binary operators from other tokens. */
1817
1818 enum cp_parser_prec
1819 {
1820 PREC_NOT_OPERATOR,
1821 PREC_LOGICAL_OR_EXPRESSION,
1822 PREC_LOGICAL_AND_EXPRESSION,
1823 PREC_INCLUSIVE_OR_EXPRESSION,
1824 PREC_EXCLUSIVE_OR_EXPRESSION,
1825 PREC_AND_EXPRESSION,
1826 PREC_EQUALITY_EXPRESSION,
1827 PREC_RELATIONAL_EXPRESSION,
1828 PREC_SHIFT_EXPRESSION,
1829 PREC_ADDITIVE_EXPRESSION,
1830 PREC_MULTIPLICATIVE_EXPRESSION,
1831 PREC_PM_EXPRESSION,
1832 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1833 };
1834
1835 /* A mapping from a token type to a corresponding tree node type, with a
1836 precedence value. */
1837
1838 struct cp_parser_binary_operations_map_node
1839 {
1840 /* The token type. */
1841 enum cpp_ttype token_type;
1842 /* The corresponding tree code. */
1843 enum tree_code tree_type;
1844 /* The precedence of this operator. */
1845 enum cp_parser_prec prec;
1846 };
1847
1848 struct cp_parser_expression_stack_entry
1849 {
1850 /* Left hand side of the binary operation we are currently
1851 parsing. */
1852 cp_expr lhs;
1853 /* Original tree code for left hand side, if it was a binary
1854 expression itself (used for -Wparentheses). */
1855 enum tree_code lhs_type;
1856 /* Tree code for the binary operation we are parsing. */
1857 enum tree_code tree_type;
1858 /* Precedence of the binary operation we are parsing. */
1859 enum cp_parser_prec prec;
1860 /* Location of the binary operation we are parsing. */
1861 location_t loc;
1862 };
1863
1864 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1865 entries because precedence levels on the stack are monotonically
1866 increasing. */
1867 typedef struct cp_parser_expression_stack_entry
1868 cp_parser_expression_stack[NUM_PREC_VALUES];
1869
1870 /* Prototypes. */
1871
1872 /* Constructors and destructors. */
1873
1874 static cp_parser_context *cp_parser_context_new
1875 (cp_parser_context *);
1876
1877 /* Class variables. */
1878
1879 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1880
1881 /* The operator-precedence table used by cp_parser_binary_expression.
1882 Transformed into an associative array (binops_by_token) by
1883 cp_parser_new. */
1884
1885 static const cp_parser_binary_operations_map_node binops[] = {
1886 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1887 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1888
1889 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1891 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1892
1893 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1894 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1895
1896 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1897 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1898
1899 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1902 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1903
1904 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1905 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1906
1907 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1908
1909 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1910
1911 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1912
1913 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1914
1915 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1916 };
1917
1918 /* The same as binops, but initialized by cp_parser_new so that
1919 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1920 for speed. */
1921 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1922
1923 /* Constructors and destructors. */
1924
1925 /* Construct a new context. The context below this one on the stack
1926 is given by NEXT. */
1927
1928 static cp_parser_context *
1929 cp_parser_context_new (cp_parser_context* next)
1930 {
1931 cp_parser_context *context;
1932
1933 /* Allocate the storage. */
1934 if (cp_parser_context_free_list != NULL)
1935 {
1936 /* Pull the first entry from the free list. */
1937 context = cp_parser_context_free_list;
1938 cp_parser_context_free_list = context->next;
1939 memset (context, 0, sizeof (*context));
1940 }
1941 else
1942 context = ggc_cleared_alloc<cp_parser_context> ();
1943
1944 /* No errors have occurred yet in this context. */
1945 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1946 /* If this is not the bottommost context, copy information that we
1947 need from the previous context. */
1948 if (next)
1949 {
1950 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1951 expression, then we are parsing one in this context, too. */
1952 context->object_type = next->object_type;
1953 /* Thread the stack. */
1954 context->next = next;
1955 }
1956
1957 return context;
1958 }
1959
1960 /* Managing the unparsed function queues. */
1961
1962 #define unparsed_funs_with_default_args \
1963 parser->unparsed_queues->last ().funs_with_default_args
1964 #define unparsed_funs_with_definitions \
1965 parser->unparsed_queues->last ().funs_with_definitions
1966 #define unparsed_nsdmis \
1967 parser->unparsed_queues->last ().nsdmis
1968 #define unparsed_classes \
1969 parser->unparsed_queues->last ().classes
1970
1971 static void
1972 push_unparsed_function_queues (cp_parser *parser)
1973 {
1974 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1975 vec_safe_push (parser->unparsed_queues, e);
1976 }
1977
1978 static void
1979 pop_unparsed_function_queues (cp_parser *parser)
1980 {
1981 release_tree_vector (unparsed_funs_with_definitions);
1982 parser->unparsed_queues->pop ();
1983 }
1984
1985 /* Prototypes. */
1986
1987 /* Constructors and destructors. */
1988
1989 static cp_parser *cp_parser_new
1990 (void);
1991
1992 /* Routines to parse various constructs.
1993
1994 Those that return `tree' will return the error_mark_node (rather
1995 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1996 Sometimes, they will return an ordinary node if error-recovery was
1997 attempted, even though a parse error occurred. So, to check
1998 whether or not a parse error occurred, you should always use
1999 cp_parser_error_occurred. If the construct is optional (indicated
2000 either by an `_opt' in the name of the function that does the
2001 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2002 the construct is not present. */
2003
2004 /* Lexical conventions [gram.lex] */
2005
2006 static cp_expr cp_parser_identifier
2007 (cp_parser *);
2008 static cp_expr cp_parser_string_literal
2009 (cp_parser *, bool, bool, bool);
2010 static cp_expr cp_parser_userdef_char_literal
2011 (cp_parser *);
2012 static tree cp_parser_userdef_string_literal
2013 (tree);
2014 static cp_expr cp_parser_userdef_numeric_literal
2015 (cp_parser *);
2016
2017 /* Basic concepts [gram.basic] */
2018
2019 static bool cp_parser_translation_unit
2020 (cp_parser *);
2021
2022 /* Expressions [gram.expr] */
2023
2024 static cp_expr cp_parser_primary_expression
2025 (cp_parser *, bool, bool, bool, cp_id_kind *);
2026 static cp_expr cp_parser_id_expression
2027 (cp_parser *, bool, bool, bool *, bool, bool);
2028 static cp_expr cp_parser_unqualified_id
2029 (cp_parser *, bool, bool, bool, bool);
2030 static tree cp_parser_nested_name_specifier_opt
2031 (cp_parser *, bool, bool, bool, bool);
2032 static tree cp_parser_nested_name_specifier
2033 (cp_parser *, bool, bool, bool, bool);
2034 static tree cp_parser_qualifying_entity
2035 (cp_parser *, bool, bool, bool, bool, bool);
2036 static cp_expr cp_parser_postfix_expression
2037 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2038 static tree cp_parser_postfix_open_square_expression
2039 (cp_parser *, tree, bool, bool);
2040 static tree cp_parser_postfix_dot_deref_expression
2041 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2042 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2043 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2044 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2045 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2046 static void cp_parser_pseudo_destructor_name
2047 (cp_parser *, tree, tree *, tree *);
2048 static cp_expr cp_parser_unary_expression
2049 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2050 static enum tree_code cp_parser_unary_operator
2051 (cp_token *);
2052 static tree cp_parser_new_expression
2053 (cp_parser *);
2054 static vec<tree, va_gc> *cp_parser_new_placement
2055 (cp_parser *);
2056 static tree cp_parser_new_type_id
2057 (cp_parser *, tree *);
2058 static cp_declarator *cp_parser_new_declarator_opt
2059 (cp_parser *);
2060 static cp_declarator *cp_parser_direct_new_declarator
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_initializer
2063 (cp_parser *);
2064 static tree cp_parser_delete_expression
2065 (cp_parser *);
2066 static cp_expr cp_parser_cast_expression
2067 (cp_parser *, bool, bool, bool, cp_id_kind *);
2068 static cp_expr cp_parser_binary_expression
2069 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2070 static tree cp_parser_question_colon_clause
2071 (cp_parser *, cp_expr);
2072 static cp_expr cp_parser_assignment_expression
2073 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2074 static enum tree_code cp_parser_assignment_operator_opt
2075 (cp_parser *);
2076 static cp_expr cp_parser_expression
2077 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2078 static cp_expr cp_parser_constant_expression
2079 (cp_parser *, bool = false, bool * = NULL);
2080 static cp_expr cp_parser_builtin_offsetof
2081 (cp_parser *);
2082 static cp_expr cp_parser_lambda_expression
2083 (cp_parser *);
2084 static void cp_parser_lambda_introducer
2085 (cp_parser *, tree);
2086 static bool cp_parser_lambda_declarator_opt
2087 (cp_parser *, tree);
2088 static void cp_parser_lambda_body
2089 (cp_parser *, tree);
2090
2091 /* Statements [gram.stmt.stmt] */
2092
2093 static void cp_parser_statement
2094 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2095 static void cp_parser_label_for_labeled_statement
2096 (cp_parser *, tree);
2097 static tree cp_parser_expression_statement
2098 (cp_parser *, tree);
2099 static tree cp_parser_compound_statement
2100 (cp_parser *, tree, int, bool);
2101 static void cp_parser_statement_seq_opt
2102 (cp_parser *, tree);
2103 static tree cp_parser_selection_statement
2104 (cp_parser *, bool *, vec<tree> *);
2105 static tree cp_parser_condition
2106 (cp_parser *);
2107 static tree cp_parser_iteration_statement
2108 (cp_parser *, bool *, bool);
2109 static bool cp_parser_for_init_statement
2110 (cp_parser *, tree *decl);
2111 static tree cp_parser_for
2112 (cp_parser *, bool);
2113 static tree cp_parser_c_for
2114 (cp_parser *, tree, tree, bool);
2115 static tree cp_parser_range_for
2116 (cp_parser *, tree, tree, tree, bool);
2117 static void do_range_for_auto_deduction
2118 (tree, tree);
2119 static tree cp_parser_perform_range_for_lookup
2120 (tree, tree *, tree *);
2121 static tree cp_parser_range_for_member_function
2122 (tree, tree);
2123 static tree cp_parser_jump_statement
2124 (cp_parser *);
2125 static void cp_parser_declaration_statement
2126 (cp_parser *);
2127
2128 static tree cp_parser_implicitly_scoped_statement
2129 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2130 static void cp_parser_already_scoped_statement
2131 (cp_parser *, bool *, const token_indent_info &);
2132
2133 /* Declarations [gram.dcl.dcl] */
2134
2135 static void cp_parser_declaration_seq_opt
2136 (cp_parser *);
2137 static void cp_parser_declaration
2138 (cp_parser *);
2139 static void cp_parser_block_declaration
2140 (cp_parser *, bool);
2141 static void cp_parser_simple_declaration
2142 (cp_parser *, bool, tree *);
2143 static void cp_parser_decl_specifier_seq
2144 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2145 static tree cp_parser_storage_class_specifier_opt
2146 (cp_parser *);
2147 static tree cp_parser_function_specifier_opt
2148 (cp_parser *, cp_decl_specifier_seq *);
2149 static tree cp_parser_type_specifier
2150 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2151 int *, bool *);
2152 static tree cp_parser_simple_type_specifier
2153 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2154 static tree cp_parser_type_name
2155 (cp_parser *, bool);
2156 static tree cp_parser_type_name
2157 (cp_parser *);
2158 static tree cp_parser_nonclass_name
2159 (cp_parser* parser);
2160 static tree cp_parser_elaborated_type_specifier
2161 (cp_parser *, bool, bool);
2162 static tree cp_parser_enum_specifier
2163 (cp_parser *);
2164 static void cp_parser_enumerator_list
2165 (cp_parser *, tree);
2166 static void cp_parser_enumerator_definition
2167 (cp_parser *, tree);
2168 static tree cp_parser_namespace_name
2169 (cp_parser *);
2170 static void cp_parser_namespace_definition
2171 (cp_parser *);
2172 static void cp_parser_namespace_body
2173 (cp_parser *);
2174 static tree cp_parser_qualified_namespace_specifier
2175 (cp_parser *);
2176 static void cp_parser_namespace_alias_definition
2177 (cp_parser *);
2178 static bool cp_parser_using_declaration
2179 (cp_parser *, bool);
2180 static void cp_parser_using_directive
2181 (cp_parser *);
2182 static tree cp_parser_alias_declaration
2183 (cp_parser *);
2184 static void cp_parser_asm_definition
2185 (cp_parser *);
2186 static void cp_parser_linkage_specification
2187 (cp_parser *);
2188 static void cp_parser_static_assert
2189 (cp_parser *, bool);
2190 static tree cp_parser_decltype
2191 (cp_parser *);
2192
2193 /* Declarators [gram.dcl.decl] */
2194
2195 static tree cp_parser_init_declarator
2196 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2197 bool, bool, int, bool *, tree *, location_t *, tree *);
2198 static cp_declarator *cp_parser_declarator
2199 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2200 static cp_declarator *cp_parser_direct_declarator
2201 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2202 static enum tree_code cp_parser_ptr_operator
2203 (cp_parser *, tree *, cp_cv_quals *, tree *);
2204 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2205 (cp_parser *);
2206 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2207 (cp_parser *);
2208 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2209 (cp_parser *);
2210 static tree cp_parser_tx_qualifier_opt
2211 (cp_parser *);
2212 static tree cp_parser_late_return_type_opt
2213 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2214 static tree cp_parser_declarator_id
2215 (cp_parser *, bool);
2216 static tree cp_parser_type_id
2217 (cp_parser *);
2218 static tree cp_parser_template_type_arg
2219 (cp_parser *);
2220 static tree cp_parser_trailing_type_id (cp_parser *);
2221 static tree cp_parser_type_id_1
2222 (cp_parser *, bool, bool);
2223 static void cp_parser_type_specifier_seq
2224 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2225 static tree cp_parser_parameter_declaration_clause
2226 (cp_parser *);
2227 static tree cp_parser_parameter_declaration_list
2228 (cp_parser *, bool *);
2229 static cp_parameter_declarator *cp_parser_parameter_declaration
2230 (cp_parser *, bool, bool *);
2231 static tree cp_parser_default_argument
2232 (cp_parser *, bool);
2233 static void cp_parser_function_body
2234 (cp_parser *, bool);
2235 static tree cp_parser_initializer
2236 (cp_parser *, bool *, bool *);
2237 static cp_expr cp_parser_initializer_clause
2238 (cp_parser *, bool *);
2239 static cp_expr cp_parser_braced_list
2240 (cp_parser*, bool*);
2241 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2242 (cp_parser *, bool *);
2243
2244 static bool cp_parser_ctor_initializer_opt_and_function_body
2245 (cp_parser *, bool);
2246
2247 static tree cp_parser_late_parsing_omp_declare_simd
2248 (cp_parser *, tree);
2249
2250 static tree cp_parser_late_parsing_cilk_simd_fn_info
2251 (cp_parser *, tree);
2252
2253 static tree cp_parser_late_parsing_oacc_routine
2254 (cp_parser *, tree);
2255
2256 static tree synthesize_implicit_template_parm
2257 (cp_parser *, tree);
2258 static tree finish_fully_implicit_template
2259 (cp_parser *, tree);
2260
2261 /* Classes [gram.class] */
2262
2263 static tree cp_parser_class_name
2264 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2265 static tree cp_parser_class_specifier
2266 (cp_parser *);
2267 static tree cp_parser_class_head
2268 (cp_parser *, bool *);
2269 static enum tag_types cp_parser_class_key
2270 (cp_parser *);
2271 static void cp_parser_type_parameter_key
2272 (cp_parser* parser);
2273 static void cp_parser_member_specification_opt
2274 (cp_parser *);
2275 static void cp_parser_member_declaration
2276 (cp_parser *);
2277 static tree cp_parser_pure_specifier
2278 (cp_parser *);
2279 static tree cp_parser_constant_initializer
2280 (cp_parser *);
2281
2282 /* Derived classes [gram.class.derived] */
2283
2284 static tree cp_parser_base_clause
2285 (cp_parser *);
2286 static tree cp_parser_base_specifier
2287 (cp_parser *);
2288
2289 /* Special member functions [gram.special] */
2290
2291 static tree cp_parser_conversion_function_id
2292 (cp_parser *);
2293 static tree cp_parser_conversion_type_id
2294 (cp_parser *);
2295 static cp_declarator *cp_parser_conversion_declarator_opt
2296 (cp_parser *);
2297 static bool cp_parser_ctor_initializer_opt
2298 (cp_parser *);
2299 static void cp_parser_mem_initializer_list
2300 (cp_parser *);
2301 static tree cp_parser_mem_initializer
2302 (cp_parser *);
2303 static tree cp_parser_mem_initializer_id
2304 (cp_parser *);
2305
2306 /* Overloading [gram.over] */
2307
2308 static cp_expr cp_parser_operator_function_id
2309 (cp_parser *);
2310 static cp_expr cp_parser_operator
2311 (cp_parser *);
2312
2313 /* Templates [gram.temp] */
2314
2315 static void cp_parser_template_declaration
2316 (cp_parser *, bool);
2317 static tree cp_parser_template_parameter_list
2318 (cp_parser *);
2319 static tree cp_parser_template_parameter
2320 (cp_parser *, bool *, bool *);
2321 static tree cp_parser_type_parameter
2322 (cp_parser *, bool *);
2323 static tree cp_parser_template_id
2324 (cp_parser *, bool, bool, enum tag_types, bool);
2325 static tree cp_parser_template_name
2326 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2327 static tree cp_parser_template_argument_list
2328 (cp_parser *);
2329 static tree cp_parser_template_argument
2330 (cp_parser *);
2331 static void cp_parser_explicit_instantiation
2332 (cp_parser *);
2333 static void cp_parser_explicit_specialization
2334 (cp_parser *);
2335
2336 /* Exception handling [gram.exception] */
2337
2338 static tree cp_parser_try_block
2339 (cp_parser *);
2340 static bool cp_parser_function_try_block
2341 (cp_parser *);
2342 static void cp_parser_handler_seq
2343 (cp_parser *);
2344 static void cp_parser_handler
2345 (cp_parser *);
2346 static tree cp_parser_exception_declaration
2347 (cp_parser *);
2348 static tree cp_parser_throw_expression
2349 (cp_parser *);
2350 static tree cp_parser_exception_specification_opt
2351 (cp_parser *);
2352 static tree cp_parser_type_id_list
2353 (cp_parser *);
2354
2355 /* GNU Extensions */
2356
2357 static tree cp_parser_asm_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_asm_operand_list
2360 (cp_parser *);
2361 static tree cp_parser_asm_clobber_list
2362 (cp_parser *);
2363 static tree cp_parser_asm_label_list
2364 (cp_parser *);
2365 static bool cp_next_tokens_can_be_attribute_p
2366 (cp_parser *);
2367 static bool cp_next_tokens_can_be_gnu_attribute_p
2368 (cp_parser *);
2369 static bool cp_next_tokens_can_be_std_attribute_p
2370 (cp_parser *);
2371 static bool cp_nth_tokens_can_be_std_attribute_p
2372 (cp_parser *, size_t);
2373 static bool cp_nth_tokens_can_be_gnu_attribute_p
2374 (cp_parser *, size_t);
2375 static bool cp_nth_tokens_can_be_attribute_p
2376 (cp_parser *, size_t);
2377 static tree cp_parser_attributes_opt
2378 (cp_parser *);
2379 static tree cp_parser_gnu_attributes_opt
2380 (cp_parser *);
2381 static tree cp_parser_gnu_attribute_list
2382 (cp_parser *);
2383 static tree cp_parser_std_attribute
2384 (cp_parser *);
2385 static tree cp_parser_std_attribute_spec
2386 (cp_parser *);
2387 static tree cp_parser_std_attribute_spec_seq
2388 (cp_parser *);
2389 static bool cp_parser_extension_opt
2390 (cp_parser *, int *);
2391 static void cp_parser_label_declaration
2392 (cp_parser *);
2393
2394 /* Concept Extensions */
2395
2396 static tree cp_parser_requires_clause
2397 (cp_parser *);
2398 static tree cp_parser_requires_clause_opt
2399 (cp_parser *);
2400 static tree cp_parser_requires_expression
2401 (cp_parser *);
2402 static tree cp_parser_requirement_parameter_list
2403 (cp_parser *);
2404 static tree cp_parser_requirement_body
2405 (cp_parser *);
2406 static tree cp_parser_requirement_list
2407 (cp_parser *);
2408 static tree cp_parser_requirement
2409 (cp_parser *);
2410 static tree cp_parser_simple_requirement
2411 (cp_parser *);
2412 static tree cp_parser_compound_requirement
2413 (cp_parser *);
2414 static tree cp_parser_type_requirement
2415 (cp_parser *);
2416 static tree cp_parser_nested_requirement
2417 (cp_parser *);
2418
2419 /* Transactional Memory Extensions */
2420
2421 static tree cp_parser_transaction
2422 (cp_parser *, cp_token *);
2423 static tree cp_parser_transaction_expression
2424 (cp_parser *, enum rid);
2425 static bool cp_parser_function_transaction
2426 (cp_parser *, enum rid);
2427 static tree cp_parser_transaction_cancel
2428 (cp_parser *);
2429
2430 enum pragma_context {
2431 pragma_external,
2432 pragma_member,
2433 pragma_objc_icode,
2434 pragma_stmt,
2435 pragma_compound
2436 };
2437 static bool cp_parser_pragma
2438 (cp_parser *, enum pragma_context, bool *);
2439
2440 /* Objective-C++ Productions */
2441
2442 static tree cp_parser_objc_message_receiver
2443 (cp_parser *);
2444 static tree cp_parser_objc_message_args
2445 (cp_parser *);
2446 static tree cp_parser_objc_message_expression
2447 (cp_parser *);
2448 static cp_expr cp_parser_objc_encode_expression
2449 (cp_parser *);
2450 static tree cp_parser_objc_defs_expression
2451 (cp_parser *);
2452 static tree cp_parser_objc_protocol_expression
2453 (cp_parser *);
2454 static tree cp_parser_objc_selector_expression
2455 (cp_parser *);
2456 static cp_expr cp_parser_objc_expression
2457 (cp_parser *);
2458 static bool cp_parser_objc_selector_p
2459 (enum cpp_ttype);
2460 static tree cp_parser_objc_selector
2461 (cp_parser *);
2462 static tree cp_parser_objc_protocol_refs_opt
2463 (cp_parser *);
2464 static void cp_parser_objc_declaration
2465 (cp_parser *, tree);
2466 static tree cp_parser_objc_statement
2467 (cp_parser *);
2468 static bool cp_parser_objc_valid_prefix_attributes
2469 (cp_parser *, tree *);
2470 static void cp_parser_objc_at_property_declaration
2471 (cp_parser *) ;
2472 static void cp_parser_objc_at_synthesize_declaration
2473 (cp_parser *) ;
2474 static void cp_parser_objc_at_dynamic_declaration
2475 (cp_parser *) ;
2476 static tree cp_parser_objc_struct_declaration
2477 (cp_parser *) ;
2478
2479 /* Utility Routines */
2480
2481 static cp_expr cp_parser_lookup_name
2482 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2483 static tree cp_parser_lookup_name_simple
2484 (cp_parser *, tree, location_t);
2485 static tree cp_parser_maybe_treat_template_as_class
2486 (tree, bool);
2487 static bool cp_parser_check_declarator_template_parameters
2488 (cp_parser *, cp_declarator *, location_t);
2489 static bool cp_parser_check_template_parameters
2490 (cp_parser *, unsigned, location_t, cp_declarator *);
2491 static cp_expr cp_parser_simple_cast_expression
2492 (cp_parser *);
2493 static tree cp_parser_global_scope_opt
2494 (cp_parser *, bool);
2495 static bool cp_parser_constructor_declarator_p
2496 (cp_parser *, bool);
2497 static tree cp_parser_function_definition_from_specifiers_and_declarator
2498 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2499 static tree cp_parser_function_definition_after_declarator
2500 (cp_parser *, bool);
2501 static bool cp_parser_template_declaration_after_export
2502 (cp_parser *, bool);
2503 static void cp_parser_perform_template_parameter_access_checks
2504 (vec<deferred_access_check, va_gc> *);
2505 static tree cp_parser_single_declaration
2506 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2507 static cp_expr cp_parser_functional_cast
2508 (cp_parser *, tree);
2509 static tree cp_parser_save_member_function_body
2510 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2511 static tree cp_parser_save_nsdmi
2512 (cp_parser *);
2513 static tree cp_parser_enclosed_template_argument_list
2514 (cp_parser *);
2515 static void cp_parser_save_default_args
2516 (cp_parser *, tree);
2517 static void cp_parser_late_parsing_for_member
2518 (cp_parser *, tree);
2519 static tree cp_parser_late_parse_one_default_arg
2520 (cp_parser *, tree, tree, tree);
2521 static void cp_parser_late_parsing_nsdmi
2522 (cp_parser *, tree);
2523 static void cp_parser_late_parsing_default_args
2524 (cp_parser *, tree);
2525 static tree cp_parser_sizeof_operand
2526 (cp_parser *, enum rid);
2527 static tree cp_parser_trait_expr
2528 (cp_parser *, enum rid);
2529 static bool cp_parser_declares_only_class_p
2530 (cp_parser *);
2531 static void cp_parser_set_storage_class
2532 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2533 static void cp_parser_set_decl_spec_type
2534 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2535 static void set_and_check_decl_spec_loc
2536 (cp_decl_specifier_seq *decl_specs,
2537 cp_decl_spec ds, cp_token *);
2538 static bool cp_parser_friend_p
2539 (const cp_decl_specifier_seq *);
2540 static void cp_parser_required_error
2541 (cp_parser *, required_token, bool);
2542 static cp_token *cp_parser_require
2543 (cp_parser *, enum cpp_ttype, required_token);
2544 static cp_token *cp_parser_require_keyword
2545 (cp_parser *, enum rid, required_token);
2546 static bool cp_parser_token_starts_function_definition_p
2547 (cp_token *);
2548 static bool cp_parser_next_token_starts_class_definition_p
2549 (cp_parser *);
2550 static bool cp_parser_next_token_ends_template_argument_p
2551 (cp_parser *);
2552 static bool cp_parser_nth_token_starts_template_argument_list_p
2553 (cp_parser *, size_t);
2554 static enum tag_types cp_parser_token_is_class_key
2555 (cp_token *);
2556 static enum tag_types cp_parser_token_is_type_parameter_key
2557 (cp_token *);
2558 static void cp_parser_check_class_key
2559 (enum tag_types, tree type);
2560 static void cp_parser_check_access_in_redeclaration
2561 (tree type, location_t location);
2562 static bool cp_parser_optional_template_keyword
2563 (cp_parser *);
2564 static void cp_parser_pre_parsed_nested_name_specifier
2565 (cp_parser *);
2566 static bool cp_parser_cache_group
2567 (cp_parser *, enum cpp_ttype, unsigned);
2568 static tree cp_parser_cache_defarg
2569 (cp_parser *parser, bool nsdmi);
2570 static void cp_parser_parse_tentatively
2571 (cp_parser *);
2572 static void cp_parser_commit_to_tentative_parse
2573 (cp_parser *);
2574 static void cp_parser_commit_to_topmost_tentative_parse
2575 (cp_parser *);
2576 static void cp_parser_abort_tentative_parse
2577 (cp_parser *);
2578 static bool cp_parser_parse_definitely
2579 (cp_parser *);
2580 static inline bool cp_parser_parsing_tentatively
2581 (cp_parser *);
2582 static bool cp_parser_uncommitted_to_tentative_parse_p
2583 (cp_parser *);
2584 static void cp_parser_error
2585 (cp_parser *, const char *);
2586 static void cp_parser_name_lookup_error
2587 (cp_parser *, tree, tree, name_lookup_error, location_t);
2588 static bool cp_parser_simulate_error
2589 (cp_parser *);
2590 static bool cp_parser_check_type_definition
2591 (cp_parser *);
2592 static void cp_parser_check_for_definition_in_return_type
2593 (cp_declarator *, tree, location_t type_location);
2594 static void cp_parser_check_for_invalid_template_id
2595 (cp_parser *, tree, enum tag_types, location_t location);
2596 static bool cp_parser_non_integral_constant_expression
2597 (cp_parser *, non_integral_constant);
2598 static void cp_parser_diagnose_invalid_type_name
2599 (cp_parser *, tree, location_t);
2600 static bool cp_parser_parse_and_diagnose_invalid_type_name
2601 (cp_parser *);
2602 static int cp_parser_skip_to_closing_parenthesis
2603 (cp_parser *, bool, bool, bool);
2604 static void cp_parser_skip_to_end_of_statement
2605 (cp_parser *);
2606 static void cp_parser_consume_semicolon_at_end_of_statement
2607 (cp_parser *);
2608 static void cp_parser_skip_to_end_of_block_or_statement
2609 (cp_parser *);
2610 static bool cp_parser_skip_to_closing_brace
2611 (cp_parser *);
2612 static void cp_parser_skip_to_end_of_template_parameter_list
2613 (cp_parser *);
2614 static void cp_parser_skip_to_pragma_eol
2615 (cp_parser*, cp_token *);
2616 static bool cp_parser_error_occurred
2617 (cp_parser *);
2618 static bool cp_parser_allow_gnu_extensions_p
2619 (cp_parser *);
2620 static bool cp_parser_is_pure_string_literal
2621 (cp_token *);
2622 static bool cp_parser_is_string_literal
2623 (cp_token *);
2624 static bool cp_parser_is_keyword
2625 (cp_token *, enum rid);
2626 static tree cp_parser_make_typename_type
2627 (cp_parser *, tree, location_t location);
2628 static cp_declarator * cp_parser_make_indirect_declarator
2629 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2630 static bool cp_parser_compound_literal_p
2631 (cp_parser *);
2632 static bool cp_parser_array_designator_p
2633 (cp_parser *);
2634 static bool cp_parser_skip_to_closing_square_bracket
2635 (cp_parser *);
2636
2637 /* Concept-related syntactic transformations */
2638
2639 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2640 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2641
2642 // -------------------------------------------------------------------------- //
2643 // Unevaluated Operand Guard
2644 //
2645 // Implementation of an RAII helper for unevaluated operand parsing.
2646 cp_unevaluated::cp_unevaluated ()
2647 {
2648 ++cp_unevaluated_operand;
2649 ++c_inhibit_evaluation_warnings;
2650 }
2651
2652 cp_unevaluated::~cp_unevaluated ()
2653 {
2654 --c_inhibit_evaluation_warnings;
2655 --cp_unevaluated_operand;
2656 }
2657
2658 // -------------------------------------------------------------------------- //
2659 // Tentative Parsing
2660
2661 /* Returns nonzero if we are parsing tentatively. */
2662
2663 static inline bool
2664 cp_parser_parsing_tentatively (cp_parser* parser)
2665 {
2666 return parser->context->next != NULL;
2667 }
2668
2669 /* Returns nonzero if TOKEN is a string literal. */
2670
2671 static bool
2672 cp_parser_is_pure_string_literal (cp_token* token)
2673 {
2674 return (token->type == CPP_STRING ||
2675 token->type == CPP_STRING16 ||
2676 token->type == CPP_STRING32 ||
2677 token->type == CPP_WSTRING ||
2678 token->type == CPP_UTF8STRING);
2679 }
2680
2681 /* Returns nonzero if TOKEN is a string literal
2682 of a user-defined string literal. */
2683
2684 static bool
2685 cp_parser_is_string_literal (cp_token* token)
2686 {
2687 return (cp_parser_is_pure_string_literal (token) ||
2688 token->type == CPP_STRING_USERDEF ||
2689 token->type == CPP_STRING16_USERDEF ||
2690 token->type == CPP_STRING32_USERDEF ||
2691 token->type == CPP_WSTRING_USERDEF ||
2692 token->type == CPP_UTF8STRING_USERDEF);
2693 }
2694
2695 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2696
2697 static bool
2698 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2699 {
2700 return token->keyword == keyword;
2701 }
2702
2703 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2704 PRAGMA_NONE. */
2705
2706 static enum pragma_kind
2707 cp_parser_pragma_kind (cp_token *token)
2708 {
2709 if (token->type != CPP_PRAGMA)
2710 return PRAGMA_NONE;
2711 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2712 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2713 }
2714
2715 /* Helper function for cp_parser_error.
2716 Having peeked a token of kind TOK1_KIND that might signify
2717 a conflict marker, peek successor tokens to determine
2718 if we actually do have a conflict marker.
2719 Specifically, we consider a run of 7 '<', '=' or '>' characters
2720 at the start of a line as a conflict marker.
2721 These come through the lexer as three pairs and a single,
2722 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2723 If it returns true, *OUT_LOC is written to with the location/range
2724 of the marker. */
2725
2726 static bool
2727 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2728 location_t *out_loc)
2729 {
2730 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2731 if (token2->type != tok1_kind)
2732 return false;
2733 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2734 if (token3->type != tok1_kind)
2735 return false;
2736 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2737 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2738 return false;
2739
2740 /* It must be at the start of the line. */
2741 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2742 if (LOCATION_COLUMN (start_loc) != 1)
2743 return false;
2744
2745 /* We have a conflict marker. Construct a location of the form:
2746 <<<<<<<
2747 ^~~~~~~
2748 with start == caret, finishing at the end of the marker. */
2749 location_t finish_loc = get_finish (token4->location);
2750 *out_loc = make_location (start_loc, start_loc, finish_loc);
2751
2752 return true;
2753 }
2754
2755 /* If not parsing tentatively, issue a diagnostic of the form
2756 FILE:LINE: MESSAGE before TOKEN
2757 where TOKEN is the next token in the input stream. MESSAGE
2758 (specified by the caller) is usually of the form "expected
2759 OTHER-TOKEN". */
2760
2761 static void
2762 cp_parser_error (cp_parser* parser, const char* gmsgid)
2763 {
2764 if (!cp_parser_simulate_error (parser))
2765 {
2766 cp_token *token = cp_lexer_peek_token (parser->lexer);
2767 /* This diagnostic makes more sense if it is tagged to the line
2768 of the token we just peeked at. */
2769 cp_lexer_set_source_position_from_token (token);
2770
2771 if (token->type == CPP_PRAGMA)
2772 {
2773 error_at (token->location,
2774 "%<#pragma%> is not allowed here");
2775 cp_parser_skip_to_pragma_eol (parser, token);
2776 return;
2777 }
2778
2779 /* If this is actually a conflict marker, report it as such. */
2780 if (token->type == CPP_LSHIFT
2781 || token->type == CPP_RSHIFT
2782 || token->type == CPP_EQ_EQ)
2783 {
2784 location_t loc;
2785 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2786 {
2787 error_at (loc, "version control conflict marker in file");
2788 return;
2789 }
2790 }
2791
2792 c_parse_error (gmsgid,
2793 /* Because c_parser_error does not understand
2794 CPP_KEYWORD, keywords are treated like
2795 identifiers. */
2796 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2797 token->u.value, token->flags);
2798 }
2799 }
2800
2801 /* Issue an error about name-lookup failing. NAME is the
2802 IDENTIFIER_NODE DECL is the result of
2803 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2804 the thing that we hoped to find. */
2805
2806 static void
2807 cp_parser_name_lookup_error (cp_parser* parser,
2808 tree name,
2809 tree decl,
2810 name_lookup_error desired,
2811 location_t location)
2812 {
2813 /* If name lookup completely failed, tell the user that NAME was not
2814 declared. */
2815 if (decl == error_mark_node)
2816 {
2817 if (parser->scope && parser->scope != global_namespace)
2818 error_at (location, "%<%E::%E%> has not been declared",
2819 parser->scope, name);
2820 else if (parser->scope == global_namespace)
2821 error_at (location, "%<::%E%> has not been declared", name);
2822 else if (parser->object_scope
2823 && !CLASS_TYPE_P (parser->object_scope))
2824 error_at (location, "request for member %qE in non-class type %qT",
2825 name, parser->object_scope);
2826 else if (parser->object_scope)
2827 error_at (location, "%<%T::%E%> has not been declared",
2828 parser->object_scope, name);
2829 else
2830 error_at (location, "%qE has not been declared", name);
2831 }
2832 else if (parser->scope && parser->scope != global_namespace)
2833 {
2834 switch (desired)
2835 {
2836 case NLE_TYPE:
2837 error_at (location, "%<%E::%E%> is not a type",
2838 parser->scope, name);
2839 break;
2840 case NLE_CXX98:
2841 error_at (location, "%<%E::%E%> is not a class or namespace",
2842 parser->scope, name);
2843 break;
2844 case NLE_NOT_CXX98:
2845 error_at (location,
2846 "%<%E::%E%> is not a class, namespace, or enumeration",
2847 parser->scope, name);
2848 break;
2849 default:
2850 gcc_unreachable ();
2851
2852 }
2853 }
2854 else if (parser->scope == global_namespace)
2855 {
2856 switch (desired)
2857 {
2858 case NLE_TYPE:
2859 error_at (location, "%<::%E%> is not a type", name);
2860 break;
2861 case NLE_CXX98:
2862 error_at (location, "%<::%E%> is not a class or namespace", name);
2863 break;
2864 case NLE_NOT_CXX98:
2865 error_at (location,
2866 "%<::%E%> is not a class, namespace, or enumeration",
2867 name);
2868 break;
2869 default:
2870 gcc_unreachable ();
2871 }
2872 }
2873 else
2874 {
2875 switch (desired)
2876 {
2877 case NLE_TYPE:
2878 error_at (location, "%qE is not a type", name);
2879 break;
2880 case NLE_CXX98:
2881 error_at (location, "%qE is not a class or namespace", name);
2882 break;
2883 case NLE_NOT_CXX98:
2884 error_at (location,
2885 "%qE is not a class, namespace, or enumeration", name);
2886 break;
2887 default:
2888 gcc_unreachable ();
2889 }
2890 }
2891 }
2892
2893 /* If we are parsing tentatively, remember that an error has occurred
2894 during this tentative parse. Returns true if the error was
2895 simulated; false if a message should be issued by the caller. */
2896
2897 static bool
2898 cp_parser_simulate_error (cp_parser* parser)
2899 {
2900 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2901 {
2902 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2903 return true;
2904 }
2905 return false;
2906 }
2907
2908 /* This function is called when a type is defined. If type
2909 definitions are forbidden at this point, an error message is
2910 issued. */
2911
2912 static bool
2913 cp_parser_check_type_definition (cp_parser* parser)
2914 {
2915 /* If types are forbidden here, issue a message. */
2916 if (parser->type_definition_forbidden_message)
2917 {
2918 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2919 in the message need to be interpreted. */
2920 error (parser->type_definition_forbidden_message);
2921 return false;
2922 }
2923 return true;
2924 }
2925
2926 /* This function is called when the DECLARATOR is processed. The TYPE
2927 was a type defined in the decl-specifiers. If it is invalid to
2928 define a type in the decl-specifiers for DECLARATOR, an error is
2929 issued. TYPE_LOCATION is the location of TYPE and is used
2930 for error reporting. */
2931
2932 static void
2933 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2934 tree type, location_t type_location)
2935 {
2936 /* [dcl.fct] forbids type definitions in return types.
2937 Unfortunately, it's not easy to know whether or not we are
2938 processing a return type until after the fact. */
2939 while (declarator
2940 && (declarator->kind == cdk_pointer
2941 || declarator->kind == cdk_reference
2942 || declarator->kind == cdk_ptrmem))
2943 declarator = declarator->declarator;
2944 if (declarator
2945 && declarator->kind == cdk_function)
2946 {
2947 error_at (type_location,
2948 "new types may not be defined in a return type");
2949 inform (type_location,
2950 "(perhaps a semicolon is missing after the definition of %qT)",
2951 type);
2952 }
2953 }
2954
2955 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2956 "<" in any valid C++ program. If the next token is indeed "<",
2957 issue a message warning the user about what appears to be an
2958 invalid attempt to form a template-id. LOCATION is the location
2959 of the type-specifier (TYPE) */
2960
2961 static void
2962 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2963 tree type,
2964 enum tag_types tag_type,
2965 location_t location)
2966 {
2967 cp_token_position start = 0;
2968
2969 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2970 {
2971 if (TYPE_P (type))
2972 error_at (location, "%qT is not a template", type);
2973 else if (identifier_p (type))
2974 {
2975 if (tag_type != none_type)
2976 error_at (location, "%qE is not a class template", type);
2977 else
2978 error_at (location, "%qE is not a template", type);
2979 }
2980 else
2981 error_at (location, "invalid template-id");
2982 /* Remember the location of the invalid "<". */
2983 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2984 start = cp_lexer_token_position (parser->lexer, true);
2985 /* Consume the "<". */
2986 cp_lexer_consume_token (parser->lexer);
2987 /* Parse the template arguments. */
2988 cp_parser_enclosed_template_argument_list (parser);
2989 /* Permanently remove the invalid template arguments so that
2990 this error message is not issued again. */
2991 if (start)
2992 cp_lexer_purge_tokens_after (parser->lexer, start);
2993 }
2994 }
2995
2996 /* If parsing an integral constant-expression, issue an error message
2997 about the fact that THING appeared and return true. Otherwise,
2998 return false. In either case, set
2999 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3000
3001 static bool
3002 cp_parser_non_integral_constant_expression (cp_parser *parser,
3003 non_integral_constant thing)
3004 {
3005 parser->non_integral_constant_expression_p = true;
3006 if (parser->integral_constant_expression_p)
3007 {
3008 if (!parser->allow_non_integral_constant_expression_p)
3009 {
3010 const char *msg = NULL;
3011 switch (thing)
3012 {
3013 case NIC_FLOAT:
3014 error ("floating-point literal "
3015 "cannot appear in a constant-expression");
3016 return true;
3017 case NIC_CAST:
3018 error ("a cast to a type other than an integral or "
3019 "enumeration type cannot appear in a "
3020 "constant-expression");
3021 return true;
3022 case NIC_TYPEID:
3023 error ("%<typeid%> operator "
3024 "cannot appear in a constant-expression");
3025 return true;
3026 case NIC_NCC:
3027 error ("non-constant compound literals "
3028 "cannot appear in a constant-expression");
3029 return true;
3030 case NIC_FUNC_CALL:
3031 error ("a function call "
3032 "cannot appear in a constant-expression");
3033 return true;
3034 case NIC_INC:
3035 error ("an increment "
3036 "cannot appear in a constant-expression");
3037 return true;
3038 case NIC_DEC:
3039 error ("an decrement "
3040 "cannot appear in a constant-expression");
3041 return true;
3042 case NIC_ARRAY_REF:
3043 error ("an array reference "
3044 "cannot appear in a constant-expression");
3045 return true;
3046 case NIC_ADDR_LABEL:
3047 error ("the address of a label "
3048 "cannot appear in a constant-expression");
3049 return true;
3050 case NIC_OVERLOADED:
3051 error ("calls to overloaded operators "
3052 "cannot appear in a constant-expression");
3053 return true;
3054 case NIC_ASSIGNMENT:
3055 error ("an assignment cannot appear in a constant-expression");
3056 return true;
3057 case NIC_COMMA:
3058 error ("a comma operator "
3059 "cannot appear in a constant-expression");
3060 return true;
3061 case NIC_CONSTRUCTOR:
3062 error ("a call to a constructor "
3063 "cannot appear in a constant-expression");
3064 return true;
3065 case NIC_TRANSACTION:
3066 error ("a transaction expression "
3067 "cannot appear in a constant-expression");
3068 return true;
3069 case NIC_THIS:
3070 msg = "this";
3071 break;
3072 case NIC_FUNC_NAME:
3073 msg = "__FUNCTION__";
3074 break;
3075 case NIC_PRETTY_FUNC:
3076 msg = "__PRETTY_FUNCTION__";
3077 break;
3078 case NIC_C99_FUNC:
3079 msg = "__func__";
3080 break;
3081 case NIC_VA_ARG:
3082 msg = "va_arg";
3083 break;
3084 case NIC_ARROW:
3085 msg = "->";
3086 break;
3087 case NIC_POINT:
3088 msg = ".";
3089 break;
3090 case NIC_STAR:
3091 msg = "*";
3092 break;
3093 case NIC_ADDR:
3094 msg = "&";
3095 break;
3096 case NIC_PREINCREMENT:
3097 msg = "++";
3098 break;
3099 case NIC_PREDECREMENT:
3100 msg = "--";
3101 break;
3102 case NIC_NEW:
3103 msg = "new";
3104 break;
3105 case NIC_DEL:
3106 msg = "delete";
3107 break;
3108 default:
3109 gcc_unreachable ();
3110 }
3111 if (msg)
3112 error ("%qs cannot appear in a constant-expression", msg);
3113 return true;
3114 }
3115 }
3116 return false;
3117 }
3118
3119 /* Emit a diagnostic for an invalid type name. This function commits
3120 to the current active tentative parse, if any. (Otherwise, the
3121 problematic construct might be encountered again later, resulting
3122 in duplicate error messages.) LOCATION is the location of ID. */
3123
3124 static void
3125 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3126 location_t location)
3127 {
3128 tree decl, ambiguous_decls;
3129 cp_parser_commit_to_tentative_parse (parser);
3130 /* Try to lookup the identifier. */
3131 decl = cp_parser_lookup_name (parser, id, none_type,
3132 /*is_template=*/false,
3133 /*is_namespace=*/false,
3134 /*check_dependency=*/true,
3135 &ambiguous_decls, location);
3136 if (ambiguous_decls)
3137 /* If the lookup was ambiguous, an error will already have
3138 been issued. */
3139 return;
3140 /* If the lookup found a template-name, it means that the user forgot
3141 to specify an argument list. Emit a useful error message. */
3142 if (DECL_TYPE_TEMPLATE_P (decl))
3143 {
3144 error_at (location,
3145 "invalid use of template-name %qE without an argument list",
3146 decl);
3147 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3148 }
3149 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3150 error_at (location, "invalid use of destructor %qD as a type", id);
3151 else if (TREE_CODE (decl) == TYPE_DECL)
3152 /* Something like 'unsigned A a;' */
3153 error_at (location, "invalid combination of multiple type-specifiers");
3154 else if (!parser->scope)
3155 {
3156 /* Issue an error message. */
3157 error_at (location, "%qE does not name a type", id);
3158 /* If we're in a template class, it's possible that the user was
3159 referring to a type from a base class. For example:
3160
3161 template <typename T> struct A { typedef T X; };
3162 template <typename T> struct B : public A<T> { X x; };
3163
3164 The user should have said "typename A<T>::X". */
3165 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3166 inform (location, "C++11 %<constexpr%> only available with "
3167 "-std=c++11 or -std=gnu++11");
3168 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3169 inform (location, "C++11 %<noexcept%> only available with "
3170 "-std=c++11 or -std=gnu++11");
3171 else if (cxx_dialect < cxx11
3172 && TREE_CODE (id) == IDENTIFIER_NODE
3173 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3174 inform (location, "C++11 %<thread_local%> only available with "
3175 "-std=c++11 or -std=gnu++11");
3176 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3177 inform (location, "%<concept%> only available with -fconcepts");
3178 else if (processing_template_decl && current_class_type
3179 && TYPE_BINFO (current_class_type))
3180 {
3181 tree b;
3182
3183 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3184 b;
3185 b = TREE_CHAIN (b))
3186 {
3187 tree base_type = BINFO_TYPE (b);
3188 if (CLASS_TYPE_P (base_type)
3189 && dependent_type_p (base_type))
3190 {
3191 tree field;
3192 /* Go from a particular instantiation of the
3193 template (which will have an empty TYPE_FIELDs),
3194 to the main version. */
3195 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3196 for (field = TYPE_FIELDS (base_type);
3197 field;
3198 field = DECL_CHAIN (field))
3199 if (TREE_CODE (field) == TYPE_DECL
3200 && DECL_NAME (field) == id)
3201 {
3202 inform (location,
3203 "(perhaps %<typename %T::%E%> was intended)",
3204 BINFO_TYPE (b), id);
3205 break;
3206 }
3207 if (field)
3208 break;
3209 }
3210 }
3211 }
3212 }
3213 /* Here we diagnose qualified-ids where the scope is actually correct,
3214 but the identifier does not resolve to a valid type name. */
3215 else if (parser->scope != error_mark_node)
3216 {
3217 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3218 {
3219 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3220 error_at (location_of (id),
3221 "%qE in namespace %qE does not name a template type",
3222 id, parser->scope);
3223 else
3224 error_at (location_of (id),
3225 "%qE in namespace %qE does not name a type",
3226 id, parser->scope);
3227 if (DECL_P (decl))
3228 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3229 }
3230 else if (CLASS_TYPE_P (parser->scope)
3231 && constructor_name_p (id, parser->scope))
3232 {
3233 /* A<T>::A<T>() */
3234 error_at (location, "%<%T::%E%> names the constructor, not"
3235 " the type", parser->scope, id);
3236 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3237 error_at (location, "and %qT has no template constructors",
3238 parser->scope);
3239 }
3240 else if (TYPE_P (parser->scope)
3241 && dependent_scope_p (parser->scope))
3242 error_at (location, "need %<typename%> before %<%T::%E%> because "
3243 "%qT is a dependent scope",
3244 parser->scope, id, parser->scope);
3245 else if (TYPE_P (parser->scope))
3246 {
3247 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3248 error_at (location_of (id),
3249 "%qE in %q#T does not name a template type",
3250 id, parser->scope);
3251 else
3252 error_at (location_of (id),
3253 "%qE in %q#T does not name a type",
3254 id, parser->scope);
3255 if (DECL_P (decl))
3256 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3257 }
3258 else
3259 gcc_unreachable ();
3260 }
3261 }
3262
3263 /* Check for a common situation where a type-name should be present,
3264 but is not, and issue a sensible error message. Returns true if an
3265 invalid type-name was detected.
3266
3267 The situation handled by this function are variable declarations of the
3268 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3269 Usually, `ID' should name a type, but if we got here it means that it
3270 does not. We try to emit the best possible error message depending on
3271 how exactly the id-expression looks like. */
3272
3273 static bool
3274 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3275 {
3276 tree id;
3277 cp_token *token = cp_lexer_peek_token (parser->lexer);
3278
3279 /* Avoid duplicate error about ambiguous lookup. */
3280 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3281 {
3282 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3283 if (next->type == CPP_NAME && next->error_reported)
3284 goto out;
3285 }
3286
3287 cp_parser_parse_tentatively (parser);
3288 id = cp_parser_id_expression (parser,
3289 /*template_keyword_p=*/false,
3290 /*check_dependency_p=*/true,
3291 /*template_p=*/NULL,
3292 /*declarator_p=*/true,
3293 /*optional_p=*/false);
3294 /* If the next token is a (, this is a function with no explicit return
3295 type, i.e. constructor, destructor or conversion op. */
3296 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3297 || TREE_CODE (id) == TYPE_DECL)
3298 {
3299 cp_parser_abort_tentative_parse (parser);
3300 return false;
3301 }
3302 if (!cp_parser_parse_definitely (parser))
3303 return false;
3304
3305 /* Emit a diagnostic for the invalid type. */
3306 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3307 out:
3308 /* If we aren't in the middle of a declarator (i.e. in a
3309 parameter-declaration-clause), skip to the end of the declaration;
3310 there's no point in trying to process it. */
3311 if (!parser->in_declarator_p)
3312 cp_parser_skip_to_end_of_block_or_statement (parser);
3313 return true;
3314 }
3315
3316 /* Consume tokens up to, and including, the next non-nested closing `)'.
3317 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3318 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3319 found an unnested token of that type. */
3320
3321 static int
3322 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3323 bool recovering,
3324 cpp_ttype or_ttype,
3325 bool consume_paren)
3326 {
3327 unsigned paren_depth = 0;
3328 unsigned brace_depth = 0;
3329 unsigned square_depth = 0;
3330
3331 if (recovering && or_ttype == CPP_EOF
3332 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3333 return 0;
3334
3335 while (true)
3336 {
3337 cp_token * token = cp_lexer_peek_token (parser->lexer);
3338
3339 /* Have we found what we're looking for before the closing paren? */
3340 if (token->type == or_ttype && or_ttype != CPP_EOF
3341 && !brace_depth && !paren_depth && !square_depth)
3342 return -1;
3343
3344 switch (token->type)
3345 {
3346 case CPP_EOF:
3347 case CPP_PRAGMA_EOL:
3348 /* If we've run out of tokens, then there is no closing `)'. */
3349 return 0;
3350
3351 /* This is good for lambda expression capture-lists. */
3352 case CPP_OPEN_SQUARE:
3353 ++square_depth;
3354 break;
3355 case CPP_CLOSE_SQUARE:
3356 if (!square_depth--)
3357 return 0;
3358 break;
3359
3360 case CPP_SEMICOLON:
3361 /* This matches the processing in skip_to_end_of_statement. */
3362 if (!brace_depth)
3363 return 0;
3364 break;
3365
3366 case CPP_OPEN_BRACE:
3367 ++brace_depth;
3368 break;
3369 case CPP_CLOSE_BRACE:
3370 if (!brace_depth--)
3371 return 0;
3372 break;
3373
3374 case CPP_OPEN_PAREN:
3375 if (!brace_depth)
3376 ++paren_depth;
3377 break;
3378
3379 case CPP_CLOSE_PAREN:
3380 if (!brace_depth && !paren_depth--)
3381 {
3382 if (consume_paren)
3383 cp_lexer_consume_token (parser->lexer);
3384 return 1;
3385 }
3386 break;
3387
3388 default:
3389 break;
3390 }
3391
3392 /* Consume the token. */
3393 cp_lexer_consume_token (parser->lexer);
3394 }
3395 }
3396
3397 /* Consume tokens up to, and including, the next non-nested closing `)'.
3398 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3399 are doing error recovery. Returns -1 if OR_COMMA is true and we
3400 found an unnested token of that type. */
3401
3402 static int
3403 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3404 bool recovering,
3405 bool or_comma,
3406 bool consume_paren)
3407 {
3408 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3409 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3410 ttype, consume_paren);
3411 }
3412
3413 /* Consume tokens until we reach the end of the current statement.
3414 Normally, that will be just before consuming a `;'. However, if a
3415 non-nested `}' comes first, then we stop before consuming that. */
3416
3417 static void
3418 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3419 {
3420 unsigned nesting_depth = 0;
3421
3422 /* Unwind generic function template scope if necessary. */
3423 if (parser->fully_implicit_function_template_p)
3424 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3425
3426 while (true)
3427 {
3428 cp_token *token = cp_lexer_peek_token (parser->lexer);
3429
3430 switch (token->type)
3431 {
3432 case CPP_EOF:
3433 case CPP_PRAGMA_EOL:
3434 /* If we've run out of tokens, stop. */
3435 return;
3436
3437 case CPP_SEMICOLON:
3438 /* If the next token is a `;', we have reached the end of the
3439 statement. */
3440 if (!nesting_depth)
3441 return;
3442 break;
3443
3444 case CPP_CLOSE_BRACE:
3445 /* If this is a non-nested '}', stop before consuming it.
3446 That way, when confronted with something like:
3447
3448 { 3 + }
3449
3450 we stop before consuming the closing '}', even though we
3451 have not yet reached a `;'. */
3452 if (nesting_depth == 0)
3453 return;
3454
3455 /* If it is the closing '}' for a block that we have
3456 scanned, stop -- but only after consuming the token.
3457 That way given:
3458
3459 void f g () { ... }
3460 typedef int I;
3461
3462 we will stop after the body of the erroneously declared
3463 function, but before consuming the following `typedef'
3464 declaration. */
3465 if (--nesting_depth == 0)
3466 {
3467 cp_lexer_consume_token (parser->lexer);
3468 return;
3469 }
3470
3471 case CPP_OPEN_BRACE:
3472 ++nesting_depth;
3473 break;
3474
3475 default:
3476 break;
3477 }
3478
3479 /* Consume the token. */
3480 cp_lexer_consume_token (parser->lexer);
3481 }
3482 }
3483
3484 /* This function is called at the end of a statement or declaration.
3485 If the next token is a semicolon, it is consumed; otherwise, error
3486 recovery is attempted. */
3487
3488 static void
3489 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3490 {
3491 /* Look for the trailing `;'. */
3492 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3493 {
3494 /* If there is additional (erroneous) input, skip to the end of
3495 the statement. */
3496 cp_parser_skip_to_end_of_statement (parser);
3497 /* If the next token is now a `;', consume it. */
3498 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3499 cp_lexer_consume_token (parser->lexer);
3500 }
3501 }
3502
3503 /* Skip tokens until we have consumed an entire block, or until we
3504 have consumed a non-nested `;'. */
3505
3506 static void
3507 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3508 {
3509 int nesting_depth = 0;
3510
3511 /* Unwind generic function template scope if necessary. */
3512 if (parser->fully_implicit_function_template_p)
3513 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3514
3515 while (nesting_depth >= 0)
3516 {
3517 cp_token *token = cp_lexer_peek_token (parser->lexer);
3518
3519 switch (token->type)
3520 {
3521 case CPP_EOF:
3522 case CPP_PRAGMA_EOL:
3523 /* If we've run out of tokens, stop. */
3524 return;
3525
3526 case CPP_SEMICOLON:
3527 /* Stop if this is an unnested ';'. */
3528 if (!nesting_depth)
3529 nesting_depth = -1;
3530 break;
3531
3532 case CPP_CLOSE_BRACE:
3533 /* Stop if this is an unnested '}', or closes the outermost
3534 nesting level. */
3535 nesting_depth--;
3536 if (nesting_depth < 0)
3537 return;
3538 if (!nesting_depth)
3539 nesting_depth = -1;
3540 break;
3541
3542 case CPP_OPEN_BRACE:
3543 /* Nest. */
3544 nesting_depth++;
3545 break;
3546
3547 default:
3548 break;
3549 }
3550
3551 /* Consume the token. */
3552 cp_lexer_consume_token (parser->lexer);
3553 }
3554 }
3555
3556 /* Skip tokens until a non-nested closing curly brace is the next
3557 token, or there are no more tokens. Return true in the first case,
3558 false otherwise. */
3559
3560 static bool
3561 cp_parser_skip_to_closing_brace (cp_parser *parser)
3562 {
3563 unsigned nesting_depth = 0;
3564
3565 while (true)
3566 {
3567 cp_token *token = cp_lexer_peek_token (parser->lexer);
3568
3569 switch (token->type)
3570 {
3571 case CPP_EOF:
3572 case CPP_PRAGMA_EOL:
3573 /* If we've run out of tokens, stop. */
3574 return false;
3575
3576 case CPP_CLOSE_BRACE:
3577 /* If the next token is a non-nested `}', then we have reached
3578 the end of the current block. */
3579 if (nesting_depth-- == 0)
3580 return true;
3581 break;
3582
3583 case CPP_OPEN_BRACE:
3584 /* If it the next token is a `{', then we are entering a new
3585 block. Consume the entire block. */
3586 ++nesting_depth;
3587 break;
3588
3589 default:
3590 break;
3591 }
3592
3593 /* Consume the token. */
3594 cp_lexer_consume_token (parser->lexer);
3595 }
3596 }
3597
3598 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3599 parameter is the PRAGMA token, allowing us to purge the entire pragma
3600 sequence. */
3601
3602 static void
3603 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3604 {
3605 cp_token *token;
3606
3607 parser->lexer->in_pragma = false;
3608
3609 do
3610 token = cp_lexer_consume_token (parser->lexer);
3611 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3612
3613 /* Ensure that the pragma is not parsed again. */
3614 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3615 }
3616
3617 /* Require pragma end of line, resyncing with it as necessary. The
3618 arguments are as for cp_parser_skip_to_pragma_eol. */
3619
3620 static void
3621 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3622 {
3623 parser->lexer->in_pragma = false;
3624 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3625 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3626 }
3627
3628 /* This is a simple wrapper around make_typename_type. When the id is
3629 an unresolved identifier node, we can provide a superior diagnostic
3630 using cp_parser_diagnose_invalid_type_name. */
3631
3632 static tree
3633 cp_parser_make_typename_type (cp_parser *parser, tree id,
3634 location_t id_location)
3635 {
3636 tree result;
3637 if (identifier_p (id))
3638 {
3639 result = make_typename_type (parser->scope, id, typename_type,
3640 /*complain=*/tf_none);
3641 if (result == error_mark_node)
3642 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3643 return result;
3644 }
3645 return make_typename_type (parser->scope, id, typename_type, tf_error);
3646 }
3647
3648 /* This is a wrapper around the
3649 make_{pointer,ptrmem,reference}_declarator functions that decides
3650 which one to call based on the CODE and CLASS_TYPE arguments. The
3651 CODE argument should be one of the values returned by
3652 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3653 appertain to the pointer or reference. */
3654
3655 static cp_declarator *
3656 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3657 cp_cv_quals cv_qualifiers,
3658 cp_declarator *target,
3659 tree attributes)
3660 {
3661 if (code == ERROR_MARK)
3662 return cp_error_declarator;
3663
3664 if (code == INDIRECT_REF)
3665 if (class_type == NULL_TREE)
3666 return make_pointer_declarator (cv_qualifiers, target, attributes);
3667 else
3668 return make_ptrmem_declarator (cv_qualifiers, class_type,
3669 target, attributes);
3670 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3671 return make_reference_declarator (cv_qualifiers, target,
3672 false, attributes);
3673 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3674 return make_reference_declarator (cv_qualifiers, target,
3675 true, attributes);
3676 gcc_unreachable ();
3677 }
3678
3679 /* Create a new C++ parser. */
3680
3681 static cp_parser *
3682 cp_parser_new (void)
3683 {
3684 cp_parser *parser;
3685 cp_lexer *lexer;
3686 unsigned i;
3687
3688 /* cp_lexer_new_main is called before doing GC allocation because
3689 cp_lexer_new_main might load a PCH file. */
3690 lexer = cp_lexer_new_main ();
3691
3692 /* Initialize the binops_by_token so that we can get the tree
3693 directly from the token. */
3694 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3695 binops_by_token[binops[i].token_type] = binops[i];
3696
3697 parser = ggc_cleared_alloc<cp_parser> ();
3698 parser->lexer = lexer;
3699 parser->context = cp_parser_context_new (NULL);
3700
3701 /* For now, we always accept GNU extensions. */
3702 parser->allow_gnu_extensions_p = 1;
3703
3704 /* The `>' token is a greater-than operator, not the end of a
3705 template-id. */
3706 parser->greater_than_is_operator_p = true;
3707
3708 parser->default_arg_ok_p = true;
3709
3710 /* We are not parsing a constant-expression. */
3711 parser->integral_constant_expression_p = false;
3712 parser->allow_non_integral_constant_expression_p = false;
3713 parser->non_integral_constant_expression_p = false;
3714
3715 /* Local variable names are not forbidden. */
3716 parser->local_variables_forbidden_p = false;
3717
3718 /* We are not processing an `extern "C"' declaration. */
3719 parser->in_unbraced_linkage_specification_p = false;
3720
3721 /* We are not processing a declarator. */
3722 parser->in_declarator_p = false;
3723
3724 /* We are not processing a template-argument-list. */
3725 parser->in_template_argument_list_p = false;
3726
3727 /* We are not in an iteration statement. */
3728 parser->in_statement = 0;
3729
3730 /* We are not in a switch statement. */
3731 parser->in_switch_statement_p = false;
3732
3733 /* We are not parsing a type-id inside an expression. */
3734 parser->in_type_id_in_expr_p = false;
3735
3736 /* Declarations aren't implicitly extern "C". */
3737 parser->implicit_extern_c = false;
3738
3739 /* String literals should be translated to the execution character set. */
3740 parser->translate_strings_p = true;
3741
3742 /* We are not parsing a function body. */
3743 parser->in_function_body = false;
3744
3745 /* We can correct until told otherwise. */
3746 parser->colon_corrects_to_scope_p = true;
3747
3748 /* The unparsed function queue is empty. */
3749 push_unparsed_function_queues (parser);
3750
3751 /* There are no classes being defined. */
3752 parser->num_classes_being_defined = 0;
3753
3754 /* No template parameters apply. */
3755 parser->num_template_parameter_lists = 0;
3756
3757 /* Not declaring an implicit function template. */
3758 parser->auto_is_implicit_function_template_parm_p = false;
3759 parser->fully_implicit_function_template_p = false;
3760 parser->implicit_template_parms = 0;
3761 parser->implicit_template_scope = 0;
3762
3763 /* Active OpenACC routine clauses. */
3764 parser->oacc_routine = NULL;
3765
3766 /* Allow constrained-type-specifiers. */
3767 parser->prevent_constrained_type_specifiers = 0;
3768
3769 return parser;
3770 }
3771
3772 /* Create a cp_lexer structure which will emit the tokens in CACHE
3773 and push it onto the parser's lexer stack. This is used for delayed
3774 parsing of in-class method bodies and default arguments, and should
3775 not be confused with tentative parsing. */
3776 static void
3777 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3778 {
3779 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3780 lexer->next = parser->lexer;
3781 parser->lexer = lexer;
3782
3783 /* Move the current source position to that of the first token in the
3784 new lexer. */
3785 cp_lexer_set_source_position_from_token (lexer->next_token);
3786 }
3787
3788 /* Pop the top lexer off the parser stack. This is never used for the
3789 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3790 static void
3791 cp_parser_pop_lexer (cp_parser *parser)
3792 {
3793 cp_lexer *lexer = parser->lexer;
3794 parser->lexer = lexer->next;
3795 cp_lexer_destroy (lexer);
3796
3797 /* Put the current source position back where it was before this
3798 lexer was pushed. */
3799 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3800 }
3801
3802 /* Lexical conventions [gram.lex] */
3803
3804 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3805 identifier. */
3806
3807 static cp_expr
3808 cp_parser_identifier (cp_parser* parser)
3809 {
3810 cp_token *token;
3811
3812 /* Look for the identifier. */
3813 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3814 /* Return the value. */
3815 if (token)
3816 return cp_expr (token->u.value, token->location);
3817 else
3818 return error_mark_node;
3819 }
3820
3821 /* Parse a sequence of adjacent string constants. Returns a
3822 TREE_STRING representing the combined, nul-terminated string
3823 constant. If TRANSLATE is true, translate the string to the
3824 execution character set. If WIDE_OK is true, a wide string is
3825 invalid here.
3826
3827 C++98 [lex.string] says that if a narrow string literal token is
3828 adjacent to a wide string literal token, the behavior is undefined.
3829 However, C99 6.4.5p4 says that this results in a wide string literal.
3830 We follow C99 here, for consistency with the C front end.
3831
3832 This code is largely lifted from lex_string() in c-lex.c.
3833
3834 FUTURE: ObjC++ will need to handle @-strings here. */
3835 static cp_expr
3836 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3837 bool lookup_udlit = true)
3838 {
3839 tree value;
3840 size_t count;
3841 struct obstack str_ob;
3842 cpp_string str, istr, *strs;
3843 cp_token *tok;
3844 enum cpp_ttype type, curr_type;
3845 int have_suffix_p = 0;
3846 tree string_tree;
3847 tree suffix_id = NULL_TREE;
3848 bool curr_tok_is_userdef_p = false;
3849
3850 tok = cp_lexer_peek_token (parser->lexer);
3851 if (!cp_parser_is_string_literal (tok))
3852 {
3853 cp_parser_error (parser, "expected string-literal");
3854 return error_mark_node;
3855 }
3856
3857 location_t loc = tok->location;
3858
3859 if (cpp_userdef_string_p (tok->type))
3860 {
3861 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3862 curr_type = cpp_userdef_string_remove_type (tok->type);
3863 curr_tok_is_userdef_p = true;
3864 }
3865 else
3866 {
3867 string_tree = tok->u.value;
3868 curr_type = tok->type;
3869 }
3870 type = curr_type;
3871
3872 /* Try to avoid the overhead of creating and destroying an obstack
3873 for the common case of just one string. */
3874 if (!cp_parser_is_string_literal
3875 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3876 {
3877 cp_lexer_consume_token (parser->lexer);
3878
3879 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3880 str.len = TREE_STRING_LENGTH (string_tree);
3881 count = 1;
3882
3883 if (curr_tok_is_userdef_p)
3884 {
3885 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3886 have_suffix_p = 1;
3887 curr_type = cpp_userdef_string_remove_type (tok->type);
3888 }
3889 else
3890 curr_type = tok->type;
3891
3892 strs = &str;
3893 }
3894 else
3895 {
3896 location_t last_tok_loc;
3897 gcc_obstack_init (&str_ob);
3898 count = 0;
3899
3900 do
3901 {
3902 last_tok_loc = tok->location;
3903 cp_lexer_consume_token (parser->lexer);
3904 count++;
3905 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3906 str.len = TREE_STRING_LENGTH (string_tree);
3907
3908 if (curr_tok_is_userdef_p)
3909 {
3910 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3911 if (have_suffix_p == 0)
3912 {
3913 suffix_id = curr_suffix_id;
3914 have_suffix_p = 1;
3915 }
3916 else if (have_suffix_p == 1
3917 && curr_suffix_id != suffix_id)
3918 {
3919 error ("inconsistent user-defined literal suffixes"
3920 " %qD and %qD in string literal",
3921 suffix_id, curr_suffix_id);
3922 have_suffix_p = -1;
3923 }
3924 curr_type = cpp_userdef_string_remove_type (tok->type);
3925 }
3926 else
3927 curr_type = tok->type;
3928
3929 if (type != curr_type)
3930 {
3931 if (type == CPP_STRING)
3932 type = curr_type;
3933 else if (curr_type != CPP_STRING)
3934 error_at (tok->location,
3935 "unsupported non-standard concatenation "
3936 "of string literals");
3937 }
3938
3939 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3940
3941 tok = cp_lexer_peek_token (parser->lexer);
3942 if (cpp_userdef_string_p (tok->type))
3943 {
3944 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3945 curr_type = cpp_userdef_string_remove_type (tok->type);
3946 curr_tok_is_userdef_p = true;
3947 }
3948 else
3949 {
3950 string_tree = tok->u.value;
3951 curr_type = tok->type;
3952 curr_tok_is_userdef_p = false;
3953 }
3954 }
3955 while (cp_parser_is_string_literal (tok));
3956
3957 /* A string literal built by concatenation has its caret=start at
3958 the start of the initial string, and its finish at the finish of
3959 the final string literal. */
3960 loc = make_location (loc, loc, get_finish (last_tok_loc));
3961
3962 strs = (cpp_string *) obstack_finish (&str_ob);
3963 }
3964
3965 if (type != CPP_STRING && !wide_ok)
3966 {
3967 cp_parser_error (parser, "a wide string is invalid in this context");
3968 type = CPP_STRING;
3969 }
3970
3971 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3972 (parse_in, strs, count, &istr, type))
3973 {
3974 value = build_string (istr.len, (const char *)istr.text);
3975 free (CONST_CAST (unsigned char *, istr.text));
3976
3977 switch (type)
3978 {
3979 default:
3980 case CPP_STRING:
3981 case CPP_UTF8STRING:
3982 TREE_TYPE (value) = char_array_type_node;
3983 break;
3984 case CPP_STRING16:
3985 TREE_TYPE (value) = char16_array_type_node;
3986 break;
3987 case CPP_STRING32:
3988 TREE_TYPE (value) = char32_array_type_node;
3989 break;
3990 case CPP_WSTRING:
3991 TREE_TYPE (value) = wchar_array_type_node;
3992 break;
3993 }
3994
3995 value = fix_string_type (value);
3996
3997 if (have_suffix_p)
3998 {
3999 tree literal = build_userdef_literal (suffix_id, value,
4000 OT_NONE, NULL_TREE);
4001 if (lookup_udlit)
4002 value = cp_parser_userdef_string_literal (literal);
4003 else
4004 value = literal;
4005 }
4006 }
4007 else
4008 /* cpp_interpret_string has issued an error. */
4009 value = error_mark_node;
4010
4011 if (count > 1)
4012 obstack_free (&str_ob, 0);
4013
4014 return cp_expr (value, loc);
4015 }
4016
4017 /* Look up a literal operator with the name and the exact arguments. */
4018
4019 static tree
4020 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4021 {
4022 tree decl, fns;
4023 decl = lookup_name (name);
4024 if (!decl || !is_overloaded_fn (decl))
4025 return error_mark_node;
4026
4027 for (fns = decl; fns; fns = OVL_NEXT (fns))
4028 {
4029 unsigned int ix;
4030 bool found = true;
4031 tree fn = OVL_CURRENT (fns);
4032 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4033 if (parmtypes != NULL_TREE)
4034 {
4035 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4036 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4037 {
4038 tree tparm = TREE_VALUE (parmtypes);
4039 tree targ = TREE_TYPE ((*args)[ix]);
4040 bool ptr = TYPE_PTR_P (tparm);
4041 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4042 if ((ptr || arr || !same_type_p (tparm, targ))
4043 && (!ptr || !arr
4044 || !same_type_p (TREE_TYPE (tparm),
4045 TREE_TYPE (targ))))
4046 found = false;
4047 }
4048 if (found
4049 && ix == vec_safe_length (args)
4050 /* May be this should be sufficient_parms_p instead,
4051 depending on how exactly should user-defined literals
4052 work in presence of default arguments on the literal
4053 operator parameters. */
4054 && parmtypes == void_list_node)
4055 return decl;
4056 }
4057 }
4058
4059 return error_mark_node;
4060 }
4061
4062 /* Parse a user-defined char constant. Returns a call to a user-defined
4063 literal operator taking the character as an argument. */
4064
4065 static cp_expr
4066 cp_parser_userdef_char_literal (cp_parser *parser)
4067 {
4068 cp_token *token = cp_lexer_consume_token (parser->lexer);
4069 tree literal = token->u.value;
4070 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4071 tree value = USERDEF_LITERAL_VALUE (literal);
4072 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4073 tree decl, result;
4074
4075 /* Build up a call to the user-defined operator */
4076 /* Lookup the name we got back from the id-expression. */
4077 vec<tree, va_gc> *args = make_tree_vector ();
4078 vec_safe_push (args, value);
4079 decl = lookup_literal_operator (name, args);
4080 if (!decl || decl == error_mark_node)
4081 {
4082 error ("unable to find character literal operator %qD with %qT argument",
4083 name, TREE_TYPE (value));
4084 release_tree_vector (args);
4085 return error_mark_node;
4086 }
4087 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4088 release_tree_vector (args);
4089 return result;
4090 }
4091
4092 /* A subroutine of cp_parser_userdef_numeric_literal to
4093 create a char... template parameter pack from a string node. */
4094
4095 static tree
4096 make_char_string_pack (tree value)
4097 {
4098 tree charvec;
4099 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4100 const char *str = TREE_STRING_POINTER (value);
4101 int i, len = TREE_STRING_LENGTH (value) - 1;
4102 tree argvec = make_tree_vec (1);
4103
4104 /* Fill in CHARVEC with all of the parameters. */
4105 charvec = make_tree_vec (len);
4106 for (i = 0; i < len; ++i)
4107 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4108
4109 /* Build the argument packs. */
4110 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4111 TREE_TYPE (argpack) = char_type_node;
4112
4113 TREE_VEC_ELT (argvec, 0) = argpack;
4114
4115 return argvec;
4116 }
4117
4118 /* A subroutine of cp_parser_userdef_numeric_literal to
4119 create a char... template parameter pack from a string node. */
4120
4121 static tree
4122 make_string_pack (tree value)
4123 {
4124 tree charvec;
4125 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4126 const unsigned char *str
4127 = (const unsigned char *) TREE_STRING_POINTER (value);
4128 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4129 int len = TREE_STRING_LENGTH (value) / sz - 1;
4130 tree argvec = make_tree_vec (2);
4131
4132 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4133 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4134
4135 /* First template parm is character type. */
4136 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4137
4138 /* Fill in CHARVEC with all of the parameters. */
4139 charvec = make_tree_vec (len);
4140 for (int i = 0; i < len; ++i)
4141 TREE_VEC_ELT (charvec, i)
4142 = double_int_to_tree (str_char_type_node,
4143 double_int::from_buffer (str + i * sz, sz));
4144
4145 /* Build the argument packs. */
4146 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4147 TREE_TYPE (argpack) = str_char_type_node;
4148
4149 TREE_VEC_ELT (argvec, 1) = argpack;
4150
4151 return argvec;
4152 }
4153
4154 /* Parse a user-defined numeric constant. returns a call to a user-defined
4155 literal operator. */
4156
4157 static cp_expr
4158 cp_parser_userdef_numeric_literal (cp_parser *parser)
4159 {
4160 cp_token *token = cp_lexer_consume_token (parser->lexer);
4161 tree literal = token->u.value;
4162 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4163 tree value = USERDEF_LITERAL_VALUE (literal);
4164 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4165 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4166 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4167 tree decl, result;
4168 vec<tree, va_gc> *args;
4169
4170 /* Look for a literal operator taking the exact type of numeric argument
4171 as the literal value. */
4172 args = make_tree_vector ();
4173 vec_safe_push (args, value);
4174 decl = lookup_literal_operator (name, args);
4175 if (decl && decl != error_mark_node)
4176 {
4177 result = finish_call_expr (decl, &args, false, true,
4178 tf_warning_or_error);
4179
4180 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4181 {
4182 warning_at (token->location, OPT_Woverflow,
4183 "integer literal exceeds range of %qT type",
4184 long_long_unsigned_type_node);
4185 }
4186 else
4187 {
4188 if (overflow > 0)
4189 warning_at (token->location, OPT_Woverflow,
4190 "floating literal exceeds range of %qT type",
4191 long_double_type_node);
4192 else if (overflow < 0)
4193 warning_at (token->location, OPT_Woverflow,
4194 "floating literal truncated to zero");
4195 }
4196
4197 release_tree_vector (args);
4198 return result;
4199 }
4200 release_tree_vector (args);
4201
4202 /* If the numeric argument didn't work, look for a raw literal
4203 operator taking a const char* argument consisting of the number
4204 in string format. */
4205 args = make_tree_vector ();
4206 vec_safe_push (args, num_string);
4207 decl = lookup_literal_operator (name, args);
4208 if (decl && decl != error_mark_node)
4209 {
4210 result = finish_call_expr (decl, &args, false, true,
4211 tf_warning_or_error);
4212 release_tree_vector (args);
4213 return result;
4214 }
4215 release_tree_vector (args);
4216
4217 /* If the raw literal didn't work, look for a non-type template
4218 function with parameter pack char.... Call the function with
4219 template parameter characters representing the number. */
4220 args = make_tree_vector ();
4221 decl = lookup_literal_operator (name, args);
4222 if (decl && decl != error_mark_node)
4223 {
4224 tree tmpl_args = make_char_string_pack (num_string);
4225 decl = lookup_template_function (decl, tmpl_args);
4226 result = finish_call_expr (decl, &args, false, true,
4227 tf_warning_or_error);
4228 release_tree_vector (args);
4229 return result;
4230 }
4231
4232 release_tree_vector (args);
4233
4234 error ("unable to find numeric literal operator %qD", name);
4235 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4236 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4237 "to enable more built-in suffixes");
4238 return error_mark_node;
4239 }
4240
4241 /* Parse a user-defined string constant. Returns a call to a user-defined
4242 literal operator taking a character pointer and the length of the string
4243 as arguments. */
4244
4245 static tree
4246 cp_parser_userdef_string_literal (tree literal)
4247 {
4248 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4249 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4250 tree value = USERDEF_LITERAL_VALUE (literal);
4251 int len = TREE_STRING_LENGTH (value)
4252 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4253 tree decl, result;
4254 vec<tree, va_gc> *args;
4255
4256 /* Build up a call to the user-defined operator. */
4257 /* Lookup the name we got back from the id-expression. */
4258 args = make_tree_vector ();
4259 vec_safe_push (args, value);
4260 vec_safe_push (args, build_int_cst (size_type_node, len));
4261 decl = lookup_literal_operator (name, args);
4262
4263 if (decl && decl != error_mark_node)
4264 {
4265 result = finish_call_expr (decl, &args, false, true,
4266 tf_warning_or_error);
4267 release_tree_vector (args);
4268 return result;
4269 }
4270 release_tree_vector (args);
4271
4272 /* Look for a template function with typename parameter CharT
4273 and parameter pack CharT... Call the function with
4274 template parameter characters representing the string. */
4275 args = make_tree_vector ();
4276 decl = lookup_literal_operator (name, args);
4277 if (decl && decl != error_mark_node)
4278 {
4279 tree tmpl_args = make_string_pack (value);
4280 decl = lookup_template_function (decl, tmpl_args);
4281 result = finish_call_expr (decl, &args, false, true,
4282 tf_warning_or_error);
4283 release_tree_vector (args);
4284 return result;
4285 }
4286 release_tree_vector (args);
4287
4288 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4289 name, TREE_TYPE (value), size_type_node);
4290 return error_mark_node;
4291 }
4292
4293
4294 /* Basic concepts [gram.basic] */
4295
4296 /* Parse a translation-unit.
4297
4298 translation-unit:
4299 declaration-seq [opt]
4300
4301 Returns TRUE if all went well. */
4302
4303 static bool
4304 cp_parser_translation_unit (cp_parser* parser)
4305 {
4306 /* The address of the first non-permanent object on the declarator
4307 obstack. */
4308 static void *declarator_obstack_base;
4309
4310 bool success;
4311
4312 /* Create the declarator obstack, if necessary. */
4313 if (!cp_error_declarator)
4314 {
4315 gcc_obstack_init (&declarator_obstack);
4316 /* Create the error declarator. */
4317 cp_error_declarator = make_declarator (cdk_error);
4318 /* Create the empty parameter list. */
4319 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4320 /* Remember where the base of the declarator obstack lies. */
4321 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4322 }
4323
4324 cp_parser_declaration_seq_opt (parser);
4325
4326 /* If there are no tokens left then all went well. */
4327 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4328 {
4329 /* Get rid of the token array; we don't need it any more. */
4330 cp_lexer_destroy (parser->lexer);
4331 parser->lexer = NULL;
4332
4333 /* This file might have been a context that's implicitly extern
4334 "C". If so, pop the lang context. (Only relevant for PCH.) */
4335 if (parser->implicit_extern_c)
4336 {
4337 pop_lang_context ();
4338 parser->implicit_extern_c = false;
4339 }
4340
4341 /* Finish up. */
4342 finish_translation_unit ();
4343
4344 success = true;
4345 }
4346 else
4347 {
4348 cp_parser_error (parser, "expected declaration");
4349 success = false;
4350 }
4351
4352 /* Make sure the declarator obstack was fully cleaned up. */
4353 gcc_assert (obstack_next_free (&declarator_obstack)
4354 == declarator_obstack_base);
4355
4356 /* All went well. */
4357 return success;
4358 }
4359
4360 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4361 decltype context. */
4362
4363 static inline tsubst_flags_t
4364 complain_flags (bool decltype_p)
4365 {
4366 tsubst_flags_t complain = tf_warning_or_error;
4367 if (decltype_p)
4368 complain |= tf_decltype;
4369 return complain;
4370 }
4371
4372 /* We're about to parse a collection of statements. If we're currently
4373 parsing tentatively, set up a firewall so that any nested
4374 cp_parser_commit_to_tentative_parse won't affect the current context. */
4375
4376 static cp_token_position
4377 cp_parser_start_tentative_firewall (cp_parser *parser)
4378 {
4379 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4380 return 0;
4381
4382 cp_parser_parse_tentatively (parser);
4383 cp_parser_commit_to_topmost_tentative_parse (parser);
4384 return cp_lexer_token_position (parser->lexer, false);
4385 }
4386
4387 /* We've finished parsing the collection of statements. Wrap up the
4388 firewall and replace the relevant tokens with the parsed form. */
4389
4390 static void
4391 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4392 tree expr)
4393 {
4394 if (!start)
4395 return;
4396
4397 /* Finish the firewall level. */
4398 cp_parser_parse_definitely (parser);
4399 /* And remember the result of the parse for when we try again. */
4400 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4401 token->type = CPP_PREPARSED_EXPR;
4402 token->u.value = expr;
4403 token->keyword = RID_MAX;
4404 cp_lexer_purge_tokens_after (parser->lexer, start);
4405 }
4406
4407 /* Like the above functions, but let the user modify the tokens. Used by
4408 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4409 later parses, so it makes sense to localize the effects of
4410 cp_parser_commit_to_tentative_parse. */
4411
4412 struct tentative_firewall
4413 {
4414 cp_parser *parser;
4415 bool set;
4416
4417 tentative_firewall (cp_parser *p): parser(p)
4418 {
4419 /* If we're currently parsing tentatively, start a committed level as a
4420 firewall and then an inner tentative parse. */
4421 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4422 {
4423 cp_parser_parse_tentatively (parser);
4424 cp_parser_commit_to_topmost_tentative_parse (parser);
4425 cp_parser_parse_tentatively (parser);
4426 }
4427 }
4428
4429 ~tentative_firewall()
4430 {
4431 if (set)
4432 {
4433 /* Finish the inner tentative parse and the firewall, propagating any
4434 uncommitted error state to the outer tentative parse. */
4435 bool err = cp_parser_error_occurred (parser);
4436 cp_parser_parse_definitely (parser);
4437 cp_parser_parse_definitely (parser);
4438 if (err)
4439 cp_parser_simulate_error (parser);
4440 }
4441 }
4442 };
4443
4444 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4445 enclosing parentheses. */
4446
4447 static cp_expr
4448 cp_parser_statement_expr (cp_parser *parser)
4449 {
4450 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4451
4452 /* Consume the '('. */
4453 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4454 cp_lexer_consume_token (parser->lexer);
4455 /* Start the statement-expression. */
4456 tree expr = begin_stmt_expr ();
4457 /* Parse the compound-statement. */
4458 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4459 /* Finish up. */
4460 expr = finish_stmt_expr (expr, false);
4461 /* Consume the ')'. */
4462 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4463 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4464 cp_parser_skip_to_end_of_statement (parser);
4465
4466 cp_parser_end_tentative_firewall (parser, start, expr);
4467 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4468 return cp_expr (expr, combined_loc);
4469 }
4470
4471 /* Expressions [gram.expr] */
4472
4473 /* Parse a fold-operator.
4474
4475 fold-operator:
4476 - * / % ^ & | = < > << >>
4477 = -= *= /= %= ^= &= |= <<= >>=
4478 == != <= >= && || , .* ->*
4479
4480 This returns the tree code corresponding to the matched operator
4481 as an int. When the current token matches a compound assignment
4482 opertor, the resulting tree code is the negative value of the
4483 non-assignment operator. */
4484
4485 static int
4486 cp_parser_fold_operator (cp_token *token)
4487 {
4488 switch (token->type)
4489 {
4490 case CPP_PLUS: return PLUS_EXPR;
4491 case CPP_MINUS: return MINUS_EXPR;
4492 case CPP_MULT: return MULT_EXPR;
4493 case CPP_DIV: return TRUNC_DIV_EXPR;
4494 case CPP_MOD: return TRUNC_MOD_EXPR;
4495 case CPP_XOR: return BIT_XOR_EXPR;
4496 case CPP_AND: return BIT_AND_EXPR;
4497 case CPP_OR: return BIT_IOR_EXPR;
4498 case CPP_LSHIFT: return LSHIFT_EXPR;
4499 case CPP_RSHIFT: return RSHIFT_EXPR;
4500
4501 case CPP_EQ: return -NOP_EXPR;
4502 case CPP_PLUS_EQ: return -PLUS_EXPR;
4503 case CPP_MINUS_EQ: return -MINUS_EXPR;
4504 case CPP_MULT_EQ: return -MULT_EXPR;
4505 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4506 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4507 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4508 case CPP_AND_EQ: return -BIT_AND_EXPR;
4509 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4510 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4511 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4512
4513 case CPP_EQ_EQ: return EQ_EXPR;
4514 case CPP_NOT_EQ: return NE_EXPR;
4515 case CPP_LESS: return LT_EXPR;
4516 case CPP_GREATER: return GT_EXPR;
4517 case CPP_LESS_EQ: return LE_EXPR;
4518 case CPP_GREATER_EQ: return GE_EXPR;
4519
4520 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4521 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4522
4523 case CPP_COMMA: return COMPOUND_EXPR;
4524
4525 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4526 case CPP_DEREF_STAR: return MEMBER_REF;
4527
4528 default: return ERROR_MARK;
4529 }
4530 }
4531
4532 /* Returns true if CODE indicates a binary expression, which is not allowed in
4533 the LHS of a fold-expression. More codes will need to be added to use this
4534 function in other contexts. */
4535
4536 static bool
4537 is_binary_op (tree_code code)
4538 {
4539 switch (code)
4540 {
4541 case PLUS_EXPR:
4542 case POINTER_PLUS_EXPR:
4543 case MINUS_EXPR:
4544 case MULT_EXPR:
4545 case TRUNC_DIV_EXPR:
4546 case TRUNC_MOD_EXPR:
4547 case BIT_XOR_EXPR:
4548 case BIT_AND_EXPR:
4549 case BIT_IOR_EXPR:
4550 case LSHIFT_EXPR:
4551 case RSHIFT_EXPR:
4552
4553 case MODOP_EXPR:
4554
4555 case EQ_EXPR:
4556 case NE_EXPR:
4557 case LE_EXPR:
4558 case GE_EXPR:
4559 case LT_EXPR:
4560 case GT_EXPR:
4561
4562 case TRUTH_ANDIF_EXPR:
4563 case TRUTH_ORIF_EXPR:
4564
4565 case COMPOUND_EXPR:
4566
4567 case DOTSTAR_EXPR:
4568 case MEMBER_REF:
4569 return true;
4570
4571 default:
4572 return false;
4573 }
4574 }
4575
4576 /* If the next token is a suitable fold operator, consume it and return as
4577 the function above. */
4578
4579 static int
4580 cp_parser_fold_operator (cp_parser *parser)
4581 {
4582 cp_token* token = cp_lexer_peek_token (parser->lexer);
4583 int code = cp_parser_fold_operator (token);
4584 if (code != ERROR_MARK)
4585 cp_lexer_consume_token (parser->lexer);
4586 return code;
4587 }
4588
4589 /* Parse a fold-expression.
4590
4591 fold-expression:
4592 ( ... folding-operator cast-expression)
4593 ( cast-expression folding-operator ... )
4594 ( cast-expression folding operator ... folding-operator cast-expression)
4595
4596 Note that the '(' and ')' are matched in primary expression. */
4597
4598 static cp_expr
4599 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4600 {
4601 cp_id_kind pidk;
4602
4603 // Left fold.
4604 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4605 {
4606 cp_lexer_consume_token (parser->lexer);
4607 int op = cp_parser_fold_operator (parser);
4608 if (op == ERROR_MARK)
4609 {
4610 cp_parser_error (parser, "expected binary operator");
4611 return error_mark_node;
4612 }
4613
4614 tree expr = cp_parser_cast_expression (parser, false, false,
4615 false, &pidk);
4616 if (expr == error_mark_node)
4617 return error_mark_node;
4618 return finish_left_unary_fold_expr (expr, op);
4619 }
4620
4621 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4622 int op = cp_parser_fold_operator (parser);
4623 if (op == ERROR_MARK)
4624 {
4625 cp_parser_error (parser, "expected binary operator");
4626 return error_mark_node;
4627 }
4628
4629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4630 {
4631 cp_parser_error (parser, "expected ...");
4632 return error_mark_node;
4633 }
4634 cp_lexer_consume_token (parser->lexer);
4635
4636 /* The operands of a fold-expression are cast-expressions, so binary or
4637 conditional expressions are not allowed. We check this here to avoid
4638 tentative parsing. */
4639 if (is_binary_op (TREE_CODE (expr1)))
4640 error_at (location_of (expr1),
4641 "binary expression in operand of fold-expression");
4642 else if (TREE_CODE (expr1) == COND_EXPR)
4643 error_at (location_of (expr1),
4644 "conditional expression in operand of fold-expression");
4645
4646 // Right fold.
4647 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4648 return finish_right_unary_fold_expr (expr1, op);
4649
4650 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4651 {
4652 cp_parser_error (parser, "mismatched operator in fold-expression");
4653 return error_mark_node;
4654 }
4655 cp_lexer_consume_token (parser->lexer);
4656
4657 // Binary left or right fold.
4658 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4659 if (expr2 == error_mark_node)
4660 return error_mark_node;
4661 return finish_binary_fold_expr (expr1, expr2, op);
4662 }
4663
4664 /* Parse a primary-expression.
4665
4666 primary-expression:
4667 literal
4668 this
4669 ( expression )
4670 id-expression
4671 lambda-expression (C++11)
4672
4673 GNU Extensions:
4674
4675 primary-expression:
4676 ( compound-statement )
4677 __builtin_va_arg ( assignment-expression , type-id )
4678 __builtin_offsetof ( type-id , offsetof-expression )
4679
4680 C++ Extensions:
4681 __has_nothrow_assign ( type-id )
4682 __has_nothrow_constructor ( type-id )
4683 __has_nothrow_copy ( type-id )
4684 __has_trivial_assign ( type-id )
4685 __has_trivial_constructor ( type-id )
4686 __has_trivial_copy ( type-id )
4687 __has_trivial_destructor ( type-id )
4688 __has_virtual_destructor ( type-id )
4689 __is_abstract ( type-id )
4690 __is_base_of ( type-id , type-id )
4691 __is_class ( type-id )
4692 __is_empty ( type-id )
4693 __is_enum ( type-id )
4694 __is_final ( type-id )
4695 __is_literal_type ( type-id )
4696 __is_pod ( type-id )
4697 __is_polymorphic ( type-id )
4698 __is_std_layout ( type-id )
4699 __is_trivial ( type-id )
4700 __is_union ( type-id )
4701
4702 Objective-C++ Extension:
4703
4704 primary-expression:
4705 objc-expression
4706
4707 literal:
4708 __null
4709
4710 ADDRESS_P is true iff this expression was immediately preceded by
4711 "&" and therefore might denote a pointer-to-member. CAST_P is true
4712 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4713 true iff this expression is a template argument.
4714
4715 Returns a representation of the expression. Upon return, *IDK
4716 indicates what kind of id-expression (if any) was present. */
4717
4718 static cp_expr
4719 cp_parser_primary_expression (cp_parser *parser,
4720 bool address_p,
4721 bool cast_p,
4722 bool template_arg_p,
4723 bool decltype_p,
4724 cp_id_kind *idk)
4725 {
4726 cp_token *token = NULL;
4727
4728 /* Assume the primary expression is not an id-expression. */
4729 *idk = CP_ID_KIND_NONE;
4730
4731 /* Peek at the next token. */
4732 token = cp_lexer_peek_token (parser->lexer);
4733 switch ((int) token->type)
4734 {
4735 /* literal:
4736 integer-literal
4737 character-literal
4738 floating-literal
4739 string-literal
4740 boolean-literal
4741 pointer-literal
4742 user-defined-literal */
4743 case CPP_CHAR:
4744 case CPP_CHAR16:
4745 case CPP_CHAR32:
4746 case CPP_WCHAR:
4747 case CPP_UTF8CHAR:
4748 case CPP_NUMBER:
4749 case CPP_PREPARSED_EXPR:
4750 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4751 return cp_parser_userdef_numeric_literal (parser);
4752 token = cp_lexer_consume_token (parser->lexer);
4753 if (TREE_CODE (token->u.value) == FIXED_CST)
4754 {
4755 error_at (token->location,
4756 "fixed-point types not supported in C++");
4757 return error_mark_node;
4758 }
4759 /* Floating-point literals are only allowed in an integral
4760 constant expression if they are cast to an integral or
4761 enumeration type. */
4762 if (TREE_CODE (token->u.value) == REAL_CST
4763 && parser->integral_constant_expression_p
4764 && pedantic)
4765 {
4766 /* CAST_P will be set even in invalid code like "int(2.7 +
4767 ...)". Therefore, we have to check that the next token
4768 is sure to end the cast. */
4769 if (cast_p)
4770 {
4771 cp_token *next_token;
4772
4773 next_token = cp_lexer_peek_token (parser->lexer);
4774 if (/* The comma at the end of an
4775 enumerator-definition. */
4776 next_token->type != CPP_COMMA
4777 /* The curly brace at the end of an enum-specifier. */
4778 && next_token->type != CPP_CLOSE_BRACE
4779 /* The end of a statement. */
4780 && next_token->type != CPP_SEMICOLON
4781 /* The end of the cast-expression. */
4782 && next_token->type != CPP_CLOSE_PAREN
4783 /* The end of an array bound. */
4784 && next_token->type != CPP_CLOSE_SQUARE
4785 /* The closing ">" in a template-argument-list. */
4786 && (next_token->type != CPP_GREATER
4787 || parser->greater_than_is_operator_p)
4788 /* C++0x only: A ">>" treated like two ">" tokens,
4789 in a template-argument-list. */
4790 && (next_token->type != CPP_RSHIFT
4791 || (cxx_dialect == cxx98)
4792 || parser->greater_than_is_operator_p))
4793 cast_p = false;
4794 }
4795
4796 /* If we are within a cast, then the constraint that the
4797 cast is to an integral or enumeration type will be
4798 checked at that point. If we are not within a cast, then
4799 this code is invalid. */
4800 if (!cast_p)
4801 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4802 }
4803 return cp_expr (token->u.value, token->location);
4804
4805 case CPP_CHAR_USERDEF:
4806 case CPP_CHAR16_USERDEF:
4807 case CPP_CHAR32_USERDEF:
4808 case CPP_WCHAR_USERDEF:
4809 case CPP_UTF8CHAR_USERDEF:
4810 return cp_parser_userdef_char_literal (parser);
4811
4812 case CPP_STRING:
4813 case CPP_STRING16:
4814 case CPP_STRING32:
4815 case CPP_WSTRING:
4816 case CPP_UTF8STRING:
4817 case CPP_STRING_USERDEF:
4818 case CPP_STRING16_USERDEF:
4819 case CPP_STRING32_USERDEF:
4820 case CPP_WSTRING_USERDEF:
4821 case CPP_UTF8STRING_USERDEF:
4822 /* ??? Should wide strings be allowed when parser->translate_strings_p
4823 is false (i.e. in attributes)? If not, we can kill the third
4824 argument to cp_parser_string_literal. */
4825 return cp_parser_string_literal (parser,
4826 parser->translate_strings_p,
4827 true);
4828
4829 case CPP_OPEN_PAREN:
4830 /* If we see `( { ' then we are looking at the beginning of
4831 a GNU statement-expression. */
4832 if (cp_parser_allow_gnu_extensions_p (parser)
4833 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4834 {
4835 /* Statement-expressions are not allowed by the standard. */
4836 pedwarn (token->location, OPT_Wpedantic,
4837 "ISO C++ forbids braced-groups within expressions");
4838
4839 /* And they're not allowed outside of a function-body; you
4840 cannot, for example, write:
4841
4842 int i = ({ int j = 3; j + 1; });
4843
4844 at class or namespace scope. */
4845 if (!parser->in_function_body
4846 || parser->in_template_argument_list_p)
4847 {
4848 error_at (token->location,
4849 "statement-expressions are not allowed outside "
4850 "functions nor in template-argument lists");
4851 cp_parser_skip_to_end_of_block_or_statement (parser);
4852 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4853 cp_lexer_consume_token (parser->lexer);
4854 return error_mark_node;
4855 }
4856 else
4857 return cp_parser_statement_expr (parser);
4858 }
4859 /* Otherwise it's a normal parenthesized expression. */
4860 {
4861 cp_expr expr;
4862 bool saved_greater_than_is_operator_p;
4863
4864 location_t open_paren_loc = token->location;
4865
4866 /* Consume the `('. */
4867 cp_lexer_consume_token (parser->lexer);
4868 /* Within a parenthesized expression, a `>' token is always
4869 the greater-than operator. */
4870 saved_greater_than_is_operator_p
4871 = parser->greater_than_is_operator_p;
4872 parser->greater_than_is_operator_p = true;
4873
4874 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4875 /* Left fold expression. */
4876 expr = NULL_TREE;
4877 else
4878 /* Parse the parenthesized expression. */
4879 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4880
4881 token = cp_lexer_peek_token (parser->lexer);
4882 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4883 {
4884 expr = cp_parser_fold_expression (parser, expr);
4885 if (expr != error_mark_node
4886 && cxx_dialect < cxx1z
4887 && !in_system_header_at (input_location))
4888 pedwarn (input_location, 0, "fold-expressions only available "
4889 "with -std=c++1z or -std=gnu++1z");
4890 }
4891 else
4892 /* Let the front end know that this expression was
4893 enclosed in parentheses. This matters in case, for
4894 example, the expression is of the form `A::B', since
4895 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4896 not. */
4897 expr = finish_parenthesized_expr (expr);
4898
4899 /* DR 705: Wrapping an unqualified name in parentheses
4900 suppresses arg-dependent lookup. We want to pass back
4901 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4902 (c++/37862), but none of the others. */
4903 if (*idk != CP_ID_KIND_QUALIFIED)
4904 *idk = CP_ID_KIND_NONE;
4905
4906 /* The `>' token might be the end of a template-id or
4907 template-parameter-list now. */
4908 parser->greater_than_is_operator_p
4909 = saved_greater_than_is_operator_p;
4910
4911 /* Consume the `)'. */
4912 token = cp_lexer_peek_token (parser->lexer);
4913 location_t close_paren_loc = token->location;
4914 expr.set_range (open_paren_loc, close_paren_loc);
4915 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4916 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4917 cp_parser_skip_to_end_of_statement (parser);
4918
4919 return expr;
4920 }
4921
4922 case CPP_OPEN_SQUARE:
4923 {
4924 if (c_dialect_objc ())
4925 {
4926 /* We might have an Objective-C++ message. */
4927 cp_parser_parse_tentatively (parser);
4928 tree msg = cp_parser_objc_message_expression (parser);
4929 /* If that works out, we're done ... */
4930 if (cp_parser_parse_definitely (parser))
4931 return msg;
4932 /* ... else, fall though to see if it's a lambda. */
4933 }
4934 cp_expr lam = cp_parser_lambda_expression (parser);
4935 /* Don't warn about a failed tentative parse. */
4936 if (cp_parser_error_occurred (parser))
4937 return error_mark_node;
4938 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4939 return lam;
4940 }
4941
4942 case CPP_OBJC_STRING:
4943 if (c_dialect_objc ())
4944 /* We have an Objective-C++ string literal. */
4945 return cp_parser_objc_expression (parser);
4946 cp_parser_error (parser, "expected primary-expression");
4947 return error_mark_node;
4948
4949 case CPP_KEYWORD:
4950 switch (token->keyword)
4951 {
4952 /* These two are the boolean literals. */
4953 case RID_TRUE:
4954 cp_lexer_consume_token (parser->lexer);
4955 return cp_expr (boolean_true_node, token->location);
4956 case RID_FALSE:
4957 cp_lexer_consume_token (parser->lexer);
4958 return cp_expr (boolean_false_node, token->location);
4959
4960 /* The `__null' literal. */
4961 case RID_NULL:
4962 cp_lexer_consume_token (parser->lexer);
4963 return cp_expr (null_node, token->location);
4964
4965 /* The `nullptr' literal. */
4966 case RID_NULLPTR:
4967 cp_lexer_consume_token (parser->lexer);
4968 return cp_expr (nullptr_node, token->location);
4969
4970 /* Recognize the `this' keyword. */
4971 case RID_THIS:
4972 cp_lexer_consume_token (parser->lexer);
4973 if (parser->local_variables_forbidden_p)
4974 {
4975 error_at (token->location,
4976 "%<this%> may not be used in this context");
4977 return error_mark_node;
4978 }
4979 /* Pointers cannot appear in constant-expressions. */
4980 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4981 return error_mark_node;
4982 return cp_expr (finish_this_expr (), token->location);
4983
4984 /* The `operator' keyword can be the beginning of an
4985 id-expression. */
4986 case RID_OPERATOR:
4987 goto id_expression;
4988
4989 case RID_FUNCTION_NAME:
4990 case RID_PRETTY_FUNCTION_NAME:
4991 case RID_C99_FUNCTION_NAME:
4992 {
4993 non_integral_constant name;
4994
4995 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4996 __func__ are the names of variables -- but they are
4997 treated specially. Therefore, they are handled here,
4998 rather than relying on the generic id-expression logic
4999 below. Grammatically, these names are id-expressions.
5000
5001 Consume the token. */
5002 token = cp_lexer_consume_token (parser->lexer);
5003
5004 switch (token->keyword)
5005 {
5006 case RID_FUNCTION_NAME:
5007 name = NIC_FUNC_NAME;
5008 break;
5009 case RID_PRETTY_FUNCTION_NAME:
5010 name = NIC_PRETTY_FUNC;
5011 break;
5012 case RID_C99_FUNCTION_NAME:
5013 name = NIC_C99_FUNC;
5014 break;
5015 default:
5016 gcc_unreachable ();
5017 }
5018
5019 if (cp_parser_non_integral_constant_expression (parser, name))
5020 return error_mark_node;
5021
5022 /* Look up the name. */
5023 return finish_fname (token->u.value);
5024 }
5025
5026 case RID_VA_ARG:
5027 {
5028 tree expression;
5029 tree type;
5030 source_location type_location;
5031 location_t start_loc
5032 = cp_lexer_peek_token (parser->lexer)->location;
5033 /* The `__builtin_va_arg' construct is used to handle
5034 `va_arg'. Consume the `__builtin_va_arg' token. */
5035 cp_lexer_consume_token (parser->lexer);
5036 /* Look for the opening `('. */
5037 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5038 /* Now, parse the assignment-expression. */
5039 expression = cp_parser_assignment_expression (parser);
5040 /* Look for the `,'. */
5041 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5042 type_location = cp_lexer_peek_token (parser->lexer)->location;
5043 /* Parse the type-id. */
5044 {
5045 type_id_in_expr_sentinel s (parser);
5046 type = cp_parser_type_id (parser);
5047 }
5048 /* Look for the closing `)'. */
5049 location_t finish_loc
5050 = cp_lexer_peek_token (parser->lexer)->location;
5051 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5052 /* Using `va_arg' in a constant-expression is not
5053 allowed. */
5054 if (cp_parser_non_integral_constant_expression (parser,
5055 NIC_VA_ARG))
5056 return error_mark_node;
5057 /* Construct a location of the form:
5058 __builtin_va_arg (v, int)
5059 ~~~~~~~~~~~~~~~~~~~~~^~~~
5060 with the caret at the type, ranging from the start of the
5061 "__builtin_va_arg" token to the close paren. */
5062 location_t combined_loc
5063 = make_location (type_location, start_loc, finish_loc);
5064 return build_x_va_arg (combined_loc, expression, type);
5065 }
5066
5067 case RID_OFFSETOF:
5068 return cp_parser_builtin_offsetof (parser);
5069
5070 case RID_HAS_NOTHROW_ASSIGN:
5071 case RID_HAS_NOTHROW_CONSTRUCTOR:
5072 case RID_HAS_NOTHROW_COPY:
5073 case RID_HAS_TRIVIAL_ASSIGN:
5074 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5075 case RID_HAS_TRIVIAL_COPY:
5076 case RID_HAS_TRIVIAL_DESTRUCTOR:
5077 case RID_HAS_VIRTUAL_DESTRUCTOR:
5078 case RID_IS_ABSTRACT:
5079 case RID_IS_BASE_OF:
5080 case RID_IS_CLASS:
5081 case RID_IS_EMPTY:
5082 case RID_IS_ENUM:
5083 case RID_IS_FINAL:
5084 case RID_IS_LITERAL_TYPE:
5085 case RID_IS_POD:
5086 case RID_IS_POLYMORPHIC:
5087 case RID_IS_SAME_AS:
5088 case RID_IS_STD_LAYOUT:
5089 case RID_IS_TRIVIAL:
5090 case RID_IS_TRIVIALLY_ASSIGNABLE:
5091 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5092 case RID_IS_TRIVIALLY_COPYABLE:
5093 case RID_IS_UNION:
5094 return cp_parser_trait_expr (parser, token->keyword);
5095
5096 // C++ concepts
5097 case RID_REQUIRES:
5098 return cp_parser_requires_expression (parser);
5099
5100 /* Objective-C++ expressions. */
5101 case RID_AT_ENCODE:
5102 case RID_AT_PROTOCOL:
5103 case RID_AT_SELECTOR:
5104 return cp_parser_objc_expression (parser);
5105
5106 case RID_TEMPLATE:
5107 if (parser->in_function_body
5108 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5109 == CPP_LESS))
5110 {
5111 error_at (token->location,
5112 "a template declaration cannot appear at block scope");
5113 cp_parser_skip_to_end_of_block_or_statement (parser);
5114 return error_mark_node;
5115 }
5116 default:
5117 cp_parser_error (parser, "expected primary-expression");
5118 return error_mark_node;
5119 }
5120
5121 /* An id-expression can start with either an identifier, a
5122 `::' as the beginning of a qualified-id, or the "operator"
5123 keyword. */
5124 case CPP_NAME:
5125 case CPP_SCOPE:
5126 case CPP_TEMPLATE_ID:
5127 case CPP_NESTED_NAME_SPECIFIER:
5128 {
5129 id_expression:
5130 cp_expr id_expression;
5131 cp_expr decl;
5132 const char *error_msg;
5133 bool template_p;
5134 bool done;
5135 cp_token *id_expr_token;
5136
5137 /* Parse the id-expression. */
5138 id_expression
5139 = cp_parser_id_expression (parser,
5140 /*template_keyword_p=*/false,
5141 /*check_dependency_p=*/true,
5142 &template_p,
5143 /*declarator_p=*/false,
5144 /*optional_p=*/false);
5145 if (id_expression == error_mark_node)
5146 return error_mark_node;
5147 id_expr_token = token;
5148 token = cp_lexer_peek_token (parser->lexer);
5149 done = (token->type != CPP_OPEN_SQUARE
5150 && token->type != CPP_OPEN_PAREN
5151 && token->type != CPP_DOT
5152 && token->type != CPP_DEREF
5153 && token->type != CPP_PLUS_PLUS
5154 && token->type != CPP_MINUS_MINUS);
5155 /* If we have a template-id, then no further lookup is
5156 required. If the template-id was for a template-class, we
5157 will sometimes have a TYPE_DECL at this point. */
5158 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5159 || TREE_CODE (id_expression) == TYPE_DECL)
5160 decl = id_expression;
5161 /* Look up the name. */
5162 else
5163 {
5164 tree ambiguous_decls;
5165
5166 /* If we already know that this lookup is ambiguous, then
5167 we've already issued an error message; there's no reason
5168 to check again. */
5169 if (id_expr_token->type == CPP_NAME
5170 && id_expr_token->error_reported)
5171 {
5172 cp_parser_simulate_error (parser);
5173 return error_mark_node;
5174 }
5175
5176 decl = cp_parser_lookup_name (parser, id_expression,
5177 none_type,
5178 template_p,
5179 /*is_namespace=*/false,
5180 /*check_dependency=*/true,
5181 &ambiguous_decls,
5182 id_expr_token->location);
5183 /* If the lookup was ambiguous, an error will already have
5184 been issued. */
5185 if (ambiguous_decls)
5186 return error_mark_node;
5187
5188 /* In Objective-C++, we may have an Objective-C 2.0
5189 dot-syntax for classes here. */
5190 if (c_dialect_objc ()
5191 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5192 && TREE_CODE (decl) == TYPE_DECL
5193 && objc_is_class_name (decl))
5194 {
5195 tree component;
5196 cp_lexer_consume_token (parser->lexer);
5197 component = cp_parser_identifier (parser);
5198 if (component == error_mark_node)
5199 return error_mark_node;
5200
5201 tree result = objc_build_class_component_ref (id_expression,
5202 component);
5203 /* Build a location of the form:
5204 expr.component
5205 ~~~~~^~~~~~~~~
5206 with caret at the start of the component name (at
5207 input_location), ranging from the start of the id_expression
5208 to the end of the component name. */
5209 location_t combined_loc
5210 = make_location (input_location, id_expression.get_start (),
5211 get_finish (input_location));
5212 protected_set_expr_location (result, combined_loc);
5213 return result;
5214 }
5215
5216 /* In Objective-C++, an instance variable (ivar) may be preferred
5217 to whatever cp_parser_lookup_name() found.
5218 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5219 rest of c-family, we have to do a little extra work to preserve
5220 any location information in cp_expr "decl". Given that
5221 objc_lookup_ivar is implemented in "c-family" and "objc", we
5222 have a trip through the pure "tree" type, rather than cp_expr.
5223 Naively copying it back to "decl" would implicitly give the
5224 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5225 store an EXPR_LOCATION. Hence we only update "decl" (and
5226 hence its location_t) if we get back a different tree node. */
5227 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5228 id_expression);
5229 if (decl_tree != decl.get_value ())
5230 decl = cp_expr (decl_tree);
5231
5232 /* If name lookup gives us a SCOPE_REF, then the
5233 qualifying scope was dependent. */
5234 if (TREE_CODE (decl) == SCOPE_REF)
5235 {
5236 /* At this point, we do not know if DECL is a valid
5237 integral constant expression. We assume that it is
5238 in fact such an expression, so that code like:
5239
5240 template <int N> struct A {
5241 int a[B<N>::i];
5242 };
5243
5244 is accepted. At template-instantiation time, we
5245 will check that B<N>::i is actually a constant. */
5246 return decl;
5247 }
5248 /* Check to see if DECL is a local variable in a context
5249 where that is forbidden. */
5250 if (parser->local_variables_forbidden_p
5251 && local_variable_p (decl))
5252 {
5253 /* It might be that we only found DECL because we are
5254 trying to be generous with pre-ISO scoping rules.
5255 For example, consider:
5256
5257 int i;
5258 void g() {
5259 for (int i = 0; i < 10; ++i) {}
5260 extern void f(int j = i);
5261 }
5262
5263 Here, name look up will originally find the out
5264 of scope `i'. We need to issue a warning message,
5265 but then use the global `i'. */
5266 decl = check_for_out_of_scope_variable (decl);
5267 if (local_variable_p (decl))
5268 {
5269 error_at (id_expr_token->location,
5270 "local variable %qD may not appear in this context",
5271 decl.get_value ());
5272 return error_mark_node;
5273 }
5274 }
5275 }
5276
5277 decl = (finish_id_expression
5278 (id_expression, decl, parser->scope,
5279 idk,
5280 parser->integral_constant_expression_p,
5281 parser->allow_non_integral_constant_expression_p,
5282 &parser->non_integral_constant_expression_p,
5283 template_p, done, address_p,
5284 template_arg_p,
5285 &error_msg,
5286 id_expr_token->location));
5287 if (error_msg)
5288 cp_parser_error (parser, error_msg);
5289 decl.set_location (id_expr_token->location);
5290 return decl;
5291 }
5292
5293 /* Anything else is an error. */
5294 default:
5295 cp_parser_error (parser, "expected primary-expression");
5296 return error_mark_node;
5297 }
5298 }
5299
5300 static inline cp_expr
5301 cp_parser_primary_expression (cp_parser *parser,
5302 bool address_p,
5303 bool cast_p,
5304 bool template_arg_p,
5305 cp_id_kind *idk)
5306 {
5307 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5308 /*decltype*/false, idk);
5309 }
5310
5311 /* Parse an id-expression.
5312
5313 id-expression:
5314 unqualified-id
5315 qualified-id
5316
5317 qualified-id:
5318 :: [opt] nested-name-specifier template [opt] unqualified-id
5319 :: identifier
5320 :: operator-function-id
5321 :: template-id
5322
5323 Return a representation of the unqualified portion of the
5324 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5325 a `::' or nested-name-specifier.
5326
5327 Often, if the id-expression was a qualified-id, the caller will
5328 want to make a SCOPE_REF to represent the qualified-id. This
5329 function does not do this in order to avoid wastefully creating
5330 SCOPE_REFs when they are not required.
5331
5332 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5333 `template' keyword.
5334
5335 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5336 uninstantiated templates.
5337
5338 If *TEMPLATE_P is non-NULL, it is set to true iff the
5339 `template' keyword is used to explicitly indicate that the entity
5340 named is a template.
5341
5342 If DECLARATOR_P is true, the id-expression is appearing as part of
5343 a declarator, rather than as part of an expression. */
5344
5345 static cp_expr
5346 cp_parser_id_expression (cp_parser *parser,
5347 bool template_keyword_p,
5348 bool check_dependency_p,
5349 bool *template_p,
5350 bool declarator_p,
5351 bool optional_p)
5352 {
5353 bool global_scope_p;
5354 bool nested_name_specifier_p;
5355
5356 /* Assume the `template' keyword was not used. */
5357 if (template_p)
5358 *template_p = template_keyword_p;
5359
5360 /* Look for the optional `::' operator. */
5361 global_scope_p
5362 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5363 != NULL_TREE);
5364 /* Look for the optional nested-name-specifier. */
5365 nested_name_specifier_p
5366 = (cp_parser_nested_name_specifier_opt (parser,
5367 /*typename_keyword_p=*/false,
5368 check_dependency_p,
5369 /*type_p=*/false,
5370 declarator_p)
5371 != NULL_TREE);
5372 /* If there is a nested-name-specifier, then we are looking at
5373 the first qualified-id production. */
5374 if (nested_name_specifier_p)
5375 {
5376 tree saved_scope;
5377 tree saved_object_scope;
5378 tree saved_qualifying_scope;
5379 tree unqualified_id;
5380 bool is_template;
5381
5382 /* See if the next token is the `template' keyword. */
5383 if (!template_p)
5384 template_p = &is_template;
5385 *template_p = cp_parser_optional_template_keyword (parser);
5386 /* Name lookup we do during the processing of the
5387 unqualified-id might obliterate SCOPE. */
5388 saved_scope = parser->scope;
5389 saved_object_scope = parser->object_scope;
5390 saved_qualifying_scope = parser->qualifying_scope;
5391 /* Process the final unqualified-id. */
5392 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5393 check_dependency_p,
5394 declarator_p,
5395 /*optional_p=*/false);
5396 /* Restore the SAVED_SCOPE for our caller. */
5397 parser->scope = saved_scope;
5398 parser->object_scope = saved_object_scope;
5399 parser->qualifying_scope = saved_qualifying_scope;
5400
5401 return unqualified_id;
5402 }
5403 /* Otherwise, if we are in global scope, then we are looking at one
5404 of the other qualified-id productions. */
5405 else if (global_scope_p)
5406 {
5407 cp_token *token;
5408 tree id;
5409
5410 /* Peek at the next token. */
5411 token = cp_lexer_peek_token (parser->lexer);
5412
5413 /* If it's an identifier, and the next token is not a "<", then
5414 we can avoid the template-id case. This is an optimization
5415 for this common case. */
5416 if (token->type == CPP_NAME
5417 && !cp_parser_nth_token_starts_template_argument_list_p
5418 (parser, 2))
5419 return cp_parser_identifier (parser);
5420
5421 cp_parser_parse_tentatively (parser);
5422 /* Try a template-id. */
5423 id = cp_parser_template_id (parser,
5424 /*template_keyword_p=*/false,
5425 /*check_dependency_p=*/true,
5426 none_type,
5427 declarator_p);
5428 /* If that worked, we're done. */
5429 if (cp_parser_parse_definitely (parser))
5430 return id;
5431
5432 /* Peek at the next token. (Changes in the token buffer may
5433 have invalidated the pointer obtained above.) */
5434 token = cp_lexer_peek_token (parser->lexer);
5435
5436 switch (token->type)
5437 {
5438 case CPP_NAME:
5439 return cp_parser_identifier (parser);
5440
5441 case CPP_KEYWORD:
5442 if (token->keyword == RID_OPERATOR)
5443 return cp_parser_operator_function_id (parser);
5444 /* Fall through. */
5445
5446 default:
5447 cp_parser_error (parser, "expected id-expression");
5448 return error_mark_node;
5449 }
5450 }
5451 else
5452 return cp_parser_unqualified_id (parser, template_keyword_p,
5453 /*check_dependency_p=*/true,
5454 declarator_p,
5455 optional_p);
5456 }
5457
5458 /* Parse an unqualified-id.
5459
5460 unqualified-id:
5461 identifier
5462 operator-function-id
5463 conversion-function-id
5464 ~ class-name
5465 template-id
5466
5467 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5468 keyword, in a construct like `A::template ...'.
5469
5470 Returns a representation of unqualified-id. For the `identifier'
5471 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5472 production a BIT_NOT_EXPR is returned; the operand of the
5473 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5474 other productions, see the documentation accompanying the
5475 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5476 names are looked up in uninstantiated templates. If DECLARATOR_P
5477 is true, the unqualified-id is appearing as part of a declarator,
5478 rather than as part of an expression. */
5479
5480 static cp_expr
5481 cp_parser_unqualified_id (cp_parser* parser,
5482 bool template_keyword_p,
5483 bool check_dependency_p,
5484 bool declarator_p,
5485 bool optional_p)
5486 {
5487 cp_token *token;
5488
5489 /* Peek at the next token. */
5490 token = cp_lexer_peek_token (parser->lexer);
5491
5492 switch ((int) token->type)
5493 {
5494 case CPP_NAME:
5495 {
5496 tree id;
5497
5498 /* We don't know yet whether or not this will be a
5499 template-id. */
5500 cp_parser_parse_tentatively (parser);
5501 /* Try a template-id. */
5502 id = cp_parser_template_id (parser, template_keyword_p,
5503 check_dependency_p,
5504 none_type,
5505 declarator_p);
5506 /* If it worked, we're done. */
5507 if (cp_parser_parse_definitely (parser))
5508 return id;
5509 /* Otherwise, it's an ordinary identifier. */
5510 return cp_parser_identifier (parser);
5511 }
5512
5513 case CPP_TEMPLATE_ID:
5514 return cp_parser_template_id (parser, template_keyword_p,
5515 check_dependency_p,
5516 none_type,
5517 declarator_p);
5518
5519 case CPP_COMPL:
5520 {
5521 tree type_decl;
5522 tree qualifying_scope;
5523 tree object_scope;
5524 tree scope;
5525 bool done;
5526
5527 /* Consume the `~' token. */
5528 cp_lexer_consume_token (parser->lexer);
5529 /* Parse the class-name. The standard, as written, seems to
5530 say that:
5531
5532 template <typename T> struct S { ~S (); };
5533 template <typename T> S<T>::~S() {}
5534
5535 is invalid, since `~' must be followed by a class-name, but
5536 `S<T>' is dependent, and so not known to be a class.
5537 That's not right; we need to look in uninstantiated
5538 templates. A further complication arises from:
5539
5540 template <typename T> void f(T t) {
5541 t.T::~T();
5542 }
5543
5544 Here, it is not possible to look up `T' in the scope of `T'
5545 itself. We must look in both the current scope, and the
5546 scope of the containing complete expression.
5547
5548 Yet another issue is:
5549
5550 struct S {
5551 int S;
5552 ~S();
5553 };
5554
5555 S::~S() {}
5556
5557 The standard does not seem to say that the `S' in `~S'
5558 should refer to the type `S' and not the data member
5559 `S::S'. */
5560
5561 /* DR 244 says that we look up the name after the "~" in the
5562 same scope as we looked up the qualifying name. That idea
5563 isn't fully worked out; it's more complicated than that. */
5564 scope = parser->scope;
5565 object_scope = parser->object_scope;
5566 qualifying_scope = parser->qualifying_scope;
5567
5568 /* Check for invalid scopes. */
5569 if (scope == error_mark_node)
5570 {
5571 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5572 cp_lexer_consume_token (parser->lexer);
5573 return error_mark_node;
5574 }
5575 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5576 {
5577 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5578 error_at (token->location,
5579 "scope %qT before %<~%> is not a class-name",
5580 scope);
5581 cp_parser_simulate_error (parser);
5582 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5583 cp_lexer_consume_token (parser->lexer);
5584 return error_mark_node;
5585 }
5586 gcc_assert (!scope || TYPE_P (scope));
5587
5588 /* If the name is of the form "X::~X" it's OK even if X is a
5589 typedef. */
5590 token = cp_lexer_peek_token (parser->lexer);
5591 if (scope
5592 && token->type == CPP_NAME
5593 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5594 != CPP_LESS)
5595 && (token->u.value == TYPE_IDENTIFIER (scope)
5596 || (CLASS_TYPE_P (scope)
5597 && constructor_name_p (token->u.value, scope))))
5598 {
5599 cp_lexer_consume_token (parser->lexer);
5600 return build_nt (BIT_NOT_EXPR, scope);
5601 }
5602
5603 /* ~auto means the destructor of whatever the object is. */
5604 if (cp_parser_is_keyword (token, RID_AUTO))
5605 {
5606 if (cxx_dialect < cxx14)
5607 pedwarn (input_location, 0,
5608 "%<~auto%> only available with "
5609 "-std=c++14 or -std=gnu++14");
5610 cp_lexer_consume_token (parser->lexer);
5611 return build_nt (BIT_NOT_EXPR, make_auto ());
5612 }
5613
5614 /* If there was an explicit qualification (S::~T), first look
5615 in the scope given by the qualification (i.e., S).
5616
5617 Note: in the calls to cp_parser_class_name below we pass
5618 typename_type so that lookup finds the injected-class-name
5619 rather than the constructor. */
5620 done = false;
5621 type_decl = NULL_TREE;
5622 if (scope)
5623 {
5624 cp_parser_parse_tentatively (parser);
5625 type_decl = cp_parser_class_name (parser,
5626 /*typename_keyword_p=*/false,
5627 /*template_keyword_p=*/false,
5628 typename_type,
5629 /*check_dependency=*/false,
5630 /*class_head_p=*/false,
5631 declarator_p);
5632 if (cp_parser_parse_definitely (parser))
5633 done = true;
5634 }
5635 /* In "N::S::~S", look in "N" as well. */
5636 if (!done && scope && qualifying_scope)
5637 {
5638 cp_parser_parse_tentatively (parser);
5639 parser->scope = qualifying_scope;
5640 parser->object_scope = NULL_TREE;
5641 parser->qualifying_scope = NULL_TREE;
5642 type_decl
5643 = cp_parser_class_name (parser,
5644 /*typename_keyword_p=*/false,
5645 /*template_keyword_p=*/false,
5646 typename_type,
5647 /*check_dependency=*/false,
5648 /*class_head_p=*/false,
5649 declarator_p);
5650 if (cp_parser_parse_definitely (parser))
5651 done = true;
5652 }
5653 /* In "p->S::~T", look in the scope given by "*p" as well. */
5654 else if (!done && object_scope)
5655 {
5656 cp_parser_parse_tentatively (parser);
5657 parser->scope = object_scope;
5658 parser->object_scope = NULL_TREE;
5659 parser->qualifying_scope = NULL_TREE;
5660 type_decl
5661 = cp_parser_class_name (parser,
5662 /*typename_keyword_p=*/false,
5663 /*template_keyword_p=*/false,
5664 typename_type,
5665 /*check_dependency=*/false,
5666 /*class_head_p=*/false,
5667 declarator_p);
5668 if (cp_parser_parse_definitely (parser))
5669 done = true;
5670 }
5671 /* Look in the surrounding context. */
5672 if (!done)
5673 {
5674 parser->scope = NULL_TREE;
5675 parser->object_scope = NULL_TREE;
5676 parser->qualifying_scope = NULL_TREE;
5677 if (processing_template_decl)
5678 cp_parser_parse_tentatively (parser);
5679 type_decl
5680 = cp_parser_class_name (parser,
5681 /*typename_keyword_p=*/false,
5682 /*template_keyword_p=*/false,
5683 typename_type,
5684 /*check_dependency=*/false,
5685 /*class_head_p=*/false,
5686 declarator_p);
5687 if (processing_template_decl
5688 && ! cp_parser_parse_definitely (parser))
5689 {
5690 /* We couldn't find a type with this name. If we're parsing
5691 tentatively, fail and try something else. */
5692 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5693 {
5694 cp_parser_simulate_error (parser);
5695 return error_mark_node;
5696 }
5697 /* Otherwise, accept it and check for a match at instantiation
5698 time. */
5699 type_decl = cp_parser_identifier (parser);
5700 if (type_decl != error_mark_node)
5701 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5702 return type_decl;
5703 }
5704 }
5705 /* If an error occurred, assume that the name of the
5706 destructor is the same as the name of the qualifying
5707 class. That allows us to keep parsing after running
5708 into ill-formed destructor names. */
5709 if (type_decl == error_mark_node && scope)
5710 return build_nt (BIT_NOT_EXPR, scope);
5711 else if (type_decl == error_mark_node)
5712 return error_mark_node;
5713
5714 /* Check that destructor name and scope match. */
5715 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5716 {
5717 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5718 error_at (token->location,
5719 "declaration of %<~%T%> as member of %qT",
5720 type_decl, scope);
5721 cp_parser_simulate_error (parser);
5722 return error_mark_node;
5723 }
5724
5725 /* [class.dtor]
5726
5727 A typedef-name that names a class shall not be used as the
5728 identifier in the declarator for a destructor declaration. */
5729 if (declarator_p
5730 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5731 && !DECL_SELF_REFERENCE_P (type_decl)
5732 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5733 error_at (token->location,
5734 "typedef-name %qD used as destructor declarator",
5735 type_decl);
5736
5737 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5738 }
5739
5740 case CPP_KEYWORD:
5741 if (token->keyword == RID_OPERATOR)
5742 {
5743 cp_expr id;
5744
5745 /* This could be a template-id, so we try that first. */
5746 cp_parser_parse_tentatively (parser);
5747 /* Try a template-id. */
5748 id = cp_parser_template_id (parser, template_keyword_p,
5749 /*check_dependency_p=*/true,
5750 none_type,
5751 declarator_p);
5752 /* If that worked, we're done. */
5753 if (cp_parser_parse_definitely (parser))
5754 return id;
5755 /* We still don't know whether we're looking at an
5756 operator-function-id or a conversion-function-id. */
5757 cp_parser_parse_tentatively (parser);
5758 /* Try an operator-function-id. */
5759 id = cp_parser_operator_function_id (parser);
5760 /* If that didn't work, try a conversion-function-id. */
5761 if (!cp_parser_parse_definitely (parser))
5762 id = cp_parser_conversion_function_id (parser);
5763 else if (UDLIT_OPER_P (id))
5764 {
5765 /* 17.6.3.3.5 */
5766 const char *name = UDLIT_OP_SUFFIX (id);
5767 if (name[0] != '_' && !in_system_header_at (input_location)
5768 && declarator_p)
5769 warning (0, "literal operator suffixes not preceded by %<_%>"
5770 " are reserved for future standardization");
5771 }
5772
5773 return id;
5774 }
5775 /* Fall through. */
5776
5777 default:
5778 if (optional_p)
5779 return NULL_TREE;
5780 cp_parser_error (parser, "expected unqualified-id");
5781 return error_mark_node;
5782 }
5783 }
5784
5785 /* Parse an (optional) nested-name-specifier.
5786
5787 nested-name-specifier: [C++98]
5788 class-or-namespace-name :: nested-name-specifier [opt]
5789 class-or-namespace-name :: template nested-name-specifier [opt]
5790
5791 nested-name-specifier: [C++0x]
5792 type-name ::
5793 namespace-name ::
5794 nested-name-specifier identifier ::
5795 nested-name-specifier template [opt] simple-template-id ::
5796
5797 PARSER->SCOPE should be set appropriately before this function is
5798 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5799 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5800 in name lookups.
5801
5802 Sets PARSER->SCOPE to the class (TYPE) or namespace
5803 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5804 it unchanged if there is no nested-name-specifier. Returns the new
5805 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5806
5807 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5808 part of a declaration and/or decl-specifier. */
5809
5810 static tree
5811 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5812 bool typename_keyword_p,
5813 bool check_dependency_p,
5814 bool type_p,
5815 bool is_declaration)
5816 {
5817 bool success = false;
5818 cp_token_position start = 0;
5819 cp_token *token;
5820
5821 /* Remember where the nested-name-specifier starts. */
5822 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5823 {
5824 start = cp_lexer_token_position (parser->lexer, false);
5825 push_deferring_access_checks (dk_deferred);
5826 }
5827
5828 while (true)
5829 {
5830 tree new_scope;
5831 tree old_scope;
5832 tree saved_qualifying_scope;
5833 bool template_keyword_p;
5834
5835 /* Spot cases that cannot be the beginning of a
5836 nested-name-specifier. */
5837 token = cp_lexer_peek_token (parser->lexer);
5838
5839 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5840 the already parsed nested-name-specifier. */
5841 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5842 {
5843 /* Grab the nested-name-specifier and continue the loop. */
5844 cp_parser_pre_parsed_nested_name_specifier (parser);
5845 /* If we originally encountered this nested-name-specifier
5846 with IS_DECLARATION set to false, we will not have
5847 resolved TYPENAME_TYPEs, so we must do so here. */
5848 if (is_declaration
5849 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5850 {
5851 new_scope = resolve_typename_type (parser->scope,
5852 /*only_current_p=*/false);
5853 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5854 parser->scope = new_scope;
5855 }
5856 success = true;
5857 continue;
5858 }
5859
5860 /* Spot cases that cannot be the beginning of a
5861 nested-name-specifier. On the second and subsequent times
5862 through the loop, we look for the `template' keyword. */
5863 if (success && token->keyword == RID_TEMPLATE)
5864 ;
5865 /* A template-id can start a nested-name-specifier. */
5866 else if (token->type == CPP_TEMPLATE_ID)
5867 ;
5868 /* DR 743: decltype can be used in a nested-name-specifier. */
5869 else if (token_is_decltype (token))
5870 ;
5871 else
5872 {
5873 /* If the next token is not an identifier, then it is
5874 definitely not a type-name or namespace-name. */
5875 if (token->type != CPP_NAME)
5876 break;
5877 /* If the following token is neither a `<' (to begin a
5878 template-id), nor a `::', then we are not looking at a
5879 nested-name-specifier. */
5880 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5881
5882 if (token->type == CPP_COLON
5883 && parser->colon_corrects_to_scope_p
5884 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5885 {
5886 error_at (token->location,
5887 "found %<:%> in nested-name-specifier, expected %<::%>");
5888 token->type = CPP_SCOPE;
5889 }
5890
5891 if (token->type != CPP_SCOPE
5892 && !cp_parser_nth_token_starts_template_argument_list_p
5893 (parser, 2))
5894 break;
5895 }
5896
5897 /* The nested-name-specifier is optional, so we parse
5898 tentatively. */
5899 cp_parser_parse_tentatively (parser);
5900
5901 /* Look for the optional `template' keyword, if this isn't the
5902 first time through the loop. */
5903 if (success)
5904 template_keyword_p = cp_parser_optional_template_keyword (parser);
5905 else
5906 template_keyword_p = false;
5907
5908 /* Save the old scope since the name lookup we are about to do
5909 might destroy it. */
5910 old_scope = parser->scope;
5911 saved_qualifying_scope = parser->qualifying_scope;
5912 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5913 look up names in "X<T>::I" in order to determine that "Y" is
5914 a template. So, if we have a typename at this point, we make
5915 an effort to look through it. */
5916 if (is_declaration
5917 && !typename_keyword_p
5918 && parser->scope
5919 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5920 parser->scope = resolve_typename_type (parser->scope,
5921 /*only_current_p=*/false);
5922 /* Parse the qualifying entity. */
5923 new_scope
5924 = cp_parser_qualifying_entity (parser,
5925 typename_keyword_p,
5926 template_keyword_p,
5927 check_dependency_p,
5928 type_p,
5929 is_declaration);
5930 /* Look for the `::' token. */
5931 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5932
5933 /* If we found what we wanted, we keep going; otherwise, we're
5934 done. */
5935 if (!cp_parser_parse_definitely (parser))
5936 {
5937 bool error_p = false;
5938
5939 /* Restore the OLD_SCOPE since it was valid before the
5940 failed attempt at finding the last
5941 class-or-namespace-name. */
5942 parser->scope = old_scope;
5943 parser->qualifying_scope = saved_qualifying_scope;
5944
5945 /* If the next token is a decltype, and the one after that is a
5946 `::', then the decltype has failed to resolve to a class or
5947 enumeration type. Give this error even when parsing
5948 tentatively since it can't possibly be valid--and we're going
5949 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5950 won't get another chance.*/
5951 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5952 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5953 == CPP_SCOPE))
5954 {
5955 token = cp_lexer_consume_token (parser->lexer);
5956 error_at (token->location, "decltype evaluates to %qT, "
5957 "which is not a class or enumeration type",
5958 token->u.tree_check_value->value);
5959 parser->scope = error_mark_node;
5960 error_p = true;
5961 /* As below. */
5962 success = true;
5963 cp_lexer_consume_token (parser->lexer);
5964 }
5965
5966 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5967 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5968 {
5969 /* If we have a non-type template-id followed by ::, it can't
5970 possibly be valid. */
5971 token = cp_lexer_peek_token (parser->lexer);
5972 tree tid = token->u.tree_check_value->value;
5973 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5974 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5975 {
5976 tree tmpl = NULL_TREE;
5977 if (is_overloaded_fn (tid))
5978 {
5979 tree fns = get_fns (tid);
5980 if (!OVL_CHAIN (fns))
5981 tmpl = OVL_CURRENT (fns);
5982 error_at (token->location, "function template-id %qD "
5983 "in nested-name-specifier", tid);
5984 }
5985 else
5986 {
5987 /* Variable template. */
5988 tmpl = TREE_OPERAND (tid, 0);
5989 gcc_assert (variable_template_p (tmpl));
5990 error_at (token->location, "variable template-id %qD "
5991 "in nested-name-specifier", tid);
5992 }
5993 if (tmpl)
5994 inform (DECL_SOURCE_LOCATION (tmpl),
5995 "%qD declared here", tmpl);
5996
5997 parser->scope = error_mark_node;
5998 error_p = true;
5999 /* As below. */
6000 success = true;
6001 cp_lexer_consume_token (parser->lexer);
6002 cp_lexer_consume_token (parser->lexer);
6003 }
6004 }
6005
6006 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6007 break;
6008 /* If the next token is an identifier, and the one after
6009 that is a `::', then any valid interpretation would have
6010 found a class-or-namespace-name. */
6011 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6012 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6013 == CPP_SCOPE)
6014 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6015 != CPP_COMPL))
6016 {
6017 token = cp_lexer_consume_token (parser->lexer);
6018 if (!error_p)
6019 {
6020 if (!token->error_reported)
6021 {
6022 tree decl;
6023 tree ambiguous_decls;
6024
6025 decl = cp_parser_lookup_name (parser, token->u.value,
6026 none_type,
6027 /*is_template=*/false,
6028 /*is_namespace=*/false,
6029 /*check_dependency=*/true,
6030 &ambiguous_decls,
6031 token->location);
6032 if (TREE_CODE (decl) == TEMPLATE_DECL)
6033 error_at (token->location,
6034 "%qD used without template parameters",
6035 decl);
6036 else if (ambiguous_decls)
6037 {
6038 // cp_parser_lookup_name has the same diagnostic,
6039 // thus make sure to emit it at most once.
6040 if (cp_parser_uncommitted_to_tentative_parse_p
6041 (parser))
6042 {
6043 error_at (token->location,
6044 "reference to %qD is ambiguous",
6045 token->u.value);
6046 print_candidates (ambiguous_decls);
6047 }
6048 decl = error_mark_node;
6049 }
6050 else
6051 {
6052 if (cxx_dialect != cxx98)
6053 cp_parser_name_lookup_error
6054 (parser, token->u.value, decl, NLE_NOT_CXX98,
6055 token->location);
6056 else
6057 cp_parser_name_lookup_error
6058 (parser, token->u.value, decl, NLE_CXX98,
6059 token->location);
6060 }
6061 }
6062 parser->scope = error_mark_node;
6063 error_p = true;
6064 /* Treat this as a successful nested-name-specifier
6065 due to:
6066
6067 [basic.lookup.qual]
6068
6069 If the name found is not a class-name (clause
6070 _class_) or namespace-name (_namespace.def_), the
6071 program is ill-formed. */
6072 success = true;
6073 }
6074 cp_lexer_consume_token (parser->lexer);
6075 }
6076 break;
6077 }
6078 /* We've found one valid nested-name-specifier. */
6079 success = true;
6080 /* Name lookup always gives us a DECL. */
6081 if (TREE_CODE (new_scope) == TYPE_DECL)
6082 new_scope = TREE_TYPE (new_scope);
6083 /* Uses of "template" must be followed by actual templates. */
6084 if (template_keyword_p
6085 && !(CLASS_TYPE_P (new_scope)
6086 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6087 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6088 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6089 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6090 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6091 == TEMPLATE_ID_EXPR)))
6092 permerror (input_location, TYPE_P (new_scope)
6093 ? G_("%qT is not a template")
6094 : G_("%qD is not a template"),
6095 new_scope);
6096 /* If it is a class scope, try to complete it; we are about to
6097 be looking up names inside the class. */
6098 if (TYPE_P (new_scope)
6099 /* Since checking types for dependency can be expensive,
6100 avoid doing it if the type is already complete. */
6101 && !COMPLETE_TYPE_P (new_scope)
6102 /* Do not try to complete dependent types. */
6103 && !dependent_type_p (new_scope))
6104 {
6105 new_scope = complete_type (new_scope);
6106 /* If it is a typedef to current class, use the current
6107 class instead, as the typedef won't have any names inside
6108 it yet. */
6109 if (!COMPLETE_TYPE_P (new_scope)
6110 && currently_open_class (new_scope))
6111 new_scope = TYPE_MAIN_VARIANT (new_scope);
6112 }
6113 /* Make sure we look in the right scope the next time through
6114 the loop. */
6115 parser->scope = new_scope;
6116 }
6117
6118 /* If parsing tentatively, replace the sequence of tokens that makes
6119 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6120 token. That way, should we re-parse the token stream, we will
6121 not have to repeat the effort required to do the parse, nor will
6122 we issue duplicate error messages. */
6123 if (success && start)
6124 {
6125 cp_token *token;
6126
6127 token = cp_lexer_token_at (parser->lexer, start);
6128 /* Reset the contents of the START token. */
6129 token->type = CPP_NESTED_NAME_SPECIFIER;
6130 /* Retrieve any deferred checks. Do not pop this access checks yet
6131 so the memory will not be reclaimed during token replacing below. */
6132 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6133 token->u.tree_check_value->value = parser->scope;
6134 token->u.tree_check_value->checks = get_deferred_access_checks ();
6135 token->u.tree_check_value->qualifying_scope =
6136 parser->qualifying_scope;
6137 token->keyword = RID_MAX;
6138
6139 /* Purge all subsequent tokens. */
6140 cp_lexer_purge_tokens_after (parser->lexer, start);
6141 }
6142
6143 if (start)
6144 pop_to_parent_deferring_access_checks ();
6145
6146 return success ? parser->scope : NULL_TREE;
6147 }
6148
6149 /* Parse a nested-name-specifier. See
6150 cp_parser_nested_name_specifier_opt for details. This function
6151 behaves identically, except that it will an issue an error if no
6152 nested-name-specifier is present. */
6153
6154 static tree
6155 cp_parser_nested_name_specifier (cp_parser *parser,
6156 bool typename_keyword_p,
6157 bool check_dependency_p,
6158 bool type_p,
6159 bool is_declaration)
6160 {
6161 tree scope;
6162
6163 /* Look for the nested-name-specifier. */
6164 scope = cp_parser_nested_name_specifier_opt (parser,
6165 typename_keyword_p,
6166 check_dependency_p,
6167 type_p,
6168 is_declaration);
6169 /* If it was not present, issue an error message. */
6170 if (!scope)
6171 {
6172 cp_parser_error (parser, "expected nested-name-specifier");
6173 parser->scope = NULL_TREE;
6174 }
6175
6176 return scope;
6177 }
6178
6179 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6180 this is either a class-name or a namespace-name (which corresponds
6181 to the class-or-namespace-name production in the grammar). For
6182 C++0x, it can also be a type-name that refers to an enumeration
6183 type or a simple-template-id.
6184
6185 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6186 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6187 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6188 TYPE_P is TRUE iff the next name should be taken as a class-name,
6189 even the same name is declared to be another entity in the same
6190 scope.
6191
6192 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6193 specified by the class-or-namespace-name. If neither is found the
6194 ERROR_MARK_NODE is returned. */
6195
6196 static tree
6197 cp_parser_qualifying_entity (cp_parser *parser,
6198 bool typename_keyword_p,
6199 bool template_keyword_p,
6200 bool check_dependency_p,
6201 bool type_p,
6202 bool is_declaration)
6203 {
6204 tree saved_scope;
6205 tree saved_qualifying_scope;
6206 tree saved_object_scope;
6207 tree scope;
6208 bool only_class_p;
6209 bool successful_parse_p;
6210
6211 /* DR 743: decltype can appear in a nested-name-specifier. */
6212 if (cp_lexer_next_token_is_decltype (parser->lexer))
6213 {
6214 scope = cp_parser_decltype (parser);
6215 if (TREE_CODE (scope) != ENUMERAL_TYPE
6216 && !MAYBE_CLASS_TYPE_P (scope))
6217 {
6218 cp_parser_simulate_error (parser);
6219 return error_mark_node;
6220 }
6221 if (TYPE_NAME (scope))
6222 scope = TYPE_NAME (scope);
6223 return scope;
6224 }
6225
6226 /* Before we try to parse the class-name, we must save away the
6227 current PARSER->SCOPE since cp_parser_class_name will destroy
6228 it. */
6229 saved_scope = parser->scope;
6230 saved_qualifying_scope = parser->qualifying_scope;
6231 saved_object_scope = parser->object_scope;
6232 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6233 there is no need to look for a namespace-name. */
6234 only_class_p = template_keyword_p
6235 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6236 if (!only_class_p)
6237 cp_parser_parse_tentatively (parser);
6238 scope = cp_parser_class_name (parser,
6239 typename_keyword_p,
6240 template_keyword_p,
6241 type_p ? class_type : none_type,
6242 check_dependency_p,
6243 /*class_head_p=*/false,
6244 is_declaration,
6245 /*enum_ok=*/cxx_dialect > cxx98);
6246 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6247 /* If that didn't work, try for a namespace-name. */
6248 if (!only_class_p && !successful_parse_p)
6249 {
6250 /* Restore the saved scope. */
6251 parser->scope = saved_scope;
6252 parser->qualifying_scope = saved_qualifying_scope;
6253 parser->object_scope = saved_object_scope;
6254 /* If we are not looking at an identifier followed by the scope
6255 resolution operator, then this is not part of a
6256 nested-name-specifier. (Note that this function is only used
6257 to parse the components of a nested-name-specifier.) */
6258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6259 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6260 return error_mark_node;
6261 scope = cp_parser_namespace_name (parser);
6262 }
6263
6264 return scope;
6265 }
6266
6267 /* Return true if we are looking at a compound-literal, false otherwise. */
6268
6269 static bool
6270 cp_parser_compound_literal_p (cp_parser *parser)
6271 {
6272 /* Consume the `('. */
6273 cp_lexer_consume_token (parser->lexer);
6274
6275 cp_lexer_save_tokens (parser->lexer);
6276
6277 /* Skip tokens until the next token is a closing parenthesis.
6278 If we find the closing `)', and the next token is a `{', then
6279 we are looking at a compound-literal. */
6280 bool compound_literal_p
6281 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6282 /*consume_paren=*/true)
6283 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6284
6285 /* Roll back the tokens we skipped. */
6286 cp_lexer_rollback_tokens (parser->lexer);
6287
6288 return compound_literal_p;
6289 }
6290
6291 /* Parse a postfix-expression.
6292
6293 postfix-expression:
6294 primary-expression
6295 postfix-expression [ expression ]
6296 postfix-expression ( expression-list [opt] )
6297 simple-type-specifier ( expression-list [opt] )
6298 typename :: [opt] nested-name-specifier identifier
6299 ( expression-list [opt] )
6300 typename :: [opt] nested-name-specifier template [opt] template-id
6301 ( expression-list [opt] )
6302 postfix-expression . template [opt] id-expression
6303 postfix-expression -> template [opt] id-expression
6304 postfix-expression . pseudo-destructor-name
6305 postfix-expression -> pseudo-destructor-name
6306 postfix-expression ++
6307 postfix-expression --
6308 dynamic_cast < type-id > ( expression )
6309 static_cast < type-id > ( expression )
6310 reinterpret_cast < type-id > ( expression )
6311 const_cast < type-id > ( expression )
6312 typeid ( expression )
6313 typeid ( type-id )
6314
6315 GNU Extension:
6316
6317 postfix-expression:
6318 ( type-id ) { initializer-list , [opt] }
6319
6320 This extension is a GNU version of the C99 compound-literal
6321 construct. (The C99 grammar uses `type-name' instead of `type-id',
6322 but they are essentially the same concept.)
6323
6324 If ADDRESS_P is true, the postfix expression is the operand of the
6325 `&' operator. CAST_P is true if this expression is the target of a
6326 cast.
6327
6328 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6329 class member access expressions [expr.ref].
6330
6331 Returns a representation of the expression. */
6332
6333 static cp_expr
6334 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6335 bool member_access_only_p, bool decltype_p,
6336 cp_id_kind * pidk_return)
6337 {
6338 cp_token *token;
6339 location_t loc;
6340 enum rid keyword;
6341 cp_id_kind idk = CP_ID_KIND_NONE;
6342 cp_expr postfix_expression = NULL_TREE;
6343 bool is_member_access = false;
6344 int saved_in_statement = -1;
6345
6346 /* Peek at the next token. */
6347 token = cp_lexer_peek_token (parser->lexer);
6348 loc = token->location;
6349 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6350
6351 /* Some of the productions are determined by keywords. */
6352 keyword = token->keyword;
6353 switch (keyword)
6354 {
6355 case RID_DYNCAST:
6356 case RID_STATCAST:
6357 case RID_REINTCAST:
6358 case RID_CONSTCAST:
6359 {
6360 tree type;
6361 cp_expr expression;
6362 const char *saved_message;
6363 bool saved_in_type_id_in_expr_p;
6364
6365 /* All of these can be handled in the same way from the point
6366 of view of parsing. Begin by consuming the token
6367 identifying the cast. */
6368 cp_lexer_consume_token (parser->lexer);
6369
6370 /* New types cannot be defined in the cast. */
6371 saved_message = parser->type_definition_forbidden_message;
6372 parser->type_definition_forbidden_message
6373 = G_("types may not be defined in casts");
6374
6375 /* Look for the opening `<'. */
6376 cp_parser_require (parser, CPP_LESS, RT_LESS);
6377 /* Parse the type to which we are casting. */
6378 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6379 parser->in_type_id_in_expr_p = true;
6380 type = cp_parser_type_id (parser);
6381 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6382 /* Look for the closing `>'. */
6383 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6384 /* Restore the old message. */
6385 parser->type_definition_forbidden_message = saved_message;
6386
6387 bool saved_greater_than_is_operator_p
6388 = parser->greater_than_is_operator_p;
6389 parser->greater_than_is_operator_p = true;
6390
6391 /* And the expression which is being cast. */
6392 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6393 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6394 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6395 RT_CLOSE_PAREN);
6396 location_t end_loc = close_paren ?
6397 close_paren->location : UNKNOWN_LOCATION;
6398
6399 parser->greater_than_is_operator_p
6400 = saved_greater_than_is_operator_p;
6401
6402 /* Only type conversions to integral or enumeration types
6403 can be used in constant-expressions. */
6404 if (!cast_valid_in_integral_constant_expression_p (type)
6405 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6406 return error_mark_node;
6407
6408 switch (keyword)
6409 {
6410 case RID_DYNCAST:
6411 postfix_expression
6412 = build_dynamic_cast (type, expression, tf_warning_or_error);
6413 break;
6414 case RID_STATCAST:
6415 postfix_expression
6416 = build_static_cast (type, expression, tf_warning_or_error);
6417 break;
6418 case RID_REINTCAST:
6419 postfix_expression
6420 = build_reinterpret_cast (type, expression,
6421 tf_warning_or_error);
6422 break;
6423 case RID_CONSTCAST:
6424 postfix_expression
6425 = build_const_cast (type, expression, tf_warning_or_error);
6426 break;
6427 default:
6428 gcc_unreachable ();
6429 }
6430
6431 /* Construct a location e.g. :
6432 reinterpret_cast <int *> (expr)
6433 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6434 ranging from the start of the "*_cast" token to the final closing
6435 paren, with the caret at the start. */
6436 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6437 postfix_expression.set_location (cp_cast_loc);
6438 }
6439 break;
6440
6441 case RID_TYPEID:
6442 {
6443 tree type;
6444 const char *saved_message;
6445 bool saved_in_type_id_in_expr_p;
6446
6447 /* Consume the `typeid' token. */
6448 cp_lexer_consume_token (parser->lexer);
6449 /* Look for the `(' token. */
6450 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6451 /* Types cannot be defined in a `typeid' expression. */
6452 saved_message = parser->type_definition_forbidden_message;
6453 parser->type_definition_forbidden_message
6454 = G_("types may not be defined in a %<typeid%> expression");
6455 /* We can't be sure yet whether we're looking at a type-id or an
6456 expression. */
6457 cp_parser_parse_tentatively (parser);
6458 /* Try a type-id first. */
6459 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6460 parser->in_type_id_in_expr_p = true;
6461 type = cp_parser_type_id (parser);
6462 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6463 /* Look for the `)' token. Otherwise, we can't be sure that
6464 we're not looking at an expression: consider `typeid (int
6465 (3))', for example. */
6466 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6467 /* If all went well, simply lookup the type-id. */
6468 if (cp_parser_parse_definitely (parser))
6469 postfix_expression = get_typeid (type, tf_warning_or_error);
6470 /* Otherwise, fall back to the expression variant. */
6471 else
6472 {
6473 tree expression;
6474
6475 /* Look for an expression. */
6476 expression = cp_parser_expression (parser, & idk);
6477 /* Compute its typeid. */
6478 postfix_expression = build_typeid (expression, tf_warning_or_error);
6479 /* Look for the `)' token. */
6480 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6481 }
6482 /* Restore the saved message. */
6483 parser->type_definition_forbidden_message = saved_message;
6484 /* `typeid' may not appear in an integral constant expression. */
6485 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6486 return error_mark_node;
6487 }
6488 break;
6489
6490 case RID_TYPENAME:
6491 {
6492 tree type;
6493 /* The syntax permitted here is the same permitted for an
6494 elaborated-type-specifier. */
6495 ++parser->prevent_constrained_type_specifiers;
6496 type = cp_parser_elaborated_type_specifier (parser,
6497 /*is_friend=*/false,
6498 /*is_declaration=*/false);
6499 --parser->prevent_constrained_type_specifiers;
6500 postfix_expression = cp_parser_functional_cast (parser, type);
6501 }
6502 break;
6503
6504 case RID_CILK_SPAWN:
6505 {
6506 location_t cilk_spawn_loc
6507 = cp_lexer_peek_token (parser->lexer)->location;
6508 cp_lexer_consume_token (parser->lexer);
6509 token = cp_lexer_peek_token (parser->lexer);
6510 if (token->type == CPP_SEMICOLON)
6511 {
6512 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6513 "an expression");
6514 postfix_expression = error_mark_node;
6515 break;
6516 }
6517 else if (!current_function_decl)
6518 {
6519 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6520 "inside a function");
6521 postfix_expression = error_mark_node;
6522 break;
6523 }
6524 else
6525 {
6526 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6527 saved_in_statement = parser->in_statement;
6528 parser->in_statement |= IN_CILK_SPAWN;
6529 }
6530 cfun->calls_cilk_spawn = 1;
6531 postfix_expression =
6532 cp_parser_postfix_expression (parser, false, false,
6533 false, false, &idk);
6534 if (!flag_cilkplus)
6535 {
6536 error_at (token->location, "-fcilkplus must be enabled to use"
6537 " %<_Cilk_spawn%>");
6538 cfun->calls_cilk_spawn = 0;
6539 }
6540 else if (saved_in_statement & IN_CILK_SPAWN)
6541 {
6542 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6543 "are not permitted");
6544 postfix_expression = error_mark_node;
6545 cfun->calls_cilk_spawn = 0;
6546 }
6547 else
6548 {
6549 location_t loc = postfix_expression.get_location ();
6550 postfix_expression = build_cilk_spawn (token->location,
6551 postfix_expression);
6552 /* Build a location of the form:
6553 _Cilk_spawn expr
6554 ~~~~~~~~~~~~^~~~
6555 with caret at the expr, ranging from the start of the
6556 _Cilk_spawn token to the end of the expression. */
6557 location_t combined_loc =
6558 make_location (loc, cilk_spawn_loc, get_finish (loc));
6559 postfix_expression.set_location (combined_loc);
6560 if (postfix_expression != error_mark_node)
6561 SET_EXPR_LOCATION (postfix_expression, input_location);
6562 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6563 }
6564 break;
6565 }
6566
6567 case RID_BUILTIN_SHUFFLE:
6568 {
6569 vec<tree, va_gc> *vec;
6570 unsigned int i;
6571 tree p;
6572
6573 cp_lexer_consume_token (parser->lexer);
6574 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6575 /*cast_p=*/false, /*allow_expansion_p=*/true,
6576 /*non_constant_p=*/NULL);
6577 if (vec == NULL)
6578 return error_mark_node;
6579
6580 FOR_EACH_VEC_ELT (*vec, i, p)
6581 mark_exp_read (p);
6582
6583 if (vec->length () == 2)
6584 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6585 tf_warning_or_error);
6586 else if (vec->length () == 3)
6587 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6588 tf_warning_or_error);
6589 else
6590 {
6591 error_at (loc, "wrong number of arguments to "
6592 "%<__builtin_shuffle%>");
6593 return error_mark_node;
6594 }
6595 break;
6596 }
6597
6598 default:
6599 {
6600 tree type;
6601
6602 /* If the next thing is a simple-type-specifier, we may be
6603 looking at a functional cast. We could also be looking at
6604 an id-expression. So, we try the functional cast, and if
6605 that doesn't work we fall back to the primary-expression. */
6606 cp_parser_parse_tentatively (parser);
6607 /* Look for the simple-type-specifier. */
6608 ++parser->prevent_constrained_type_specifiers;
6609 type = cp_parser_simple_type_specifier (parser,
6610 /*decl_specs=*/NULL,
6611 CP_PARSER_FLAGS_NONE);
6612 --parser->prevent_constrained_type_specifiers;
6613 /* Parse the cast itself. */
6614 if (!cp_parser_error_occurred (parser))
6615 postfix_expression
6616 = cp_parser_functional_cast (parser, type);
6617 /* If that worked, we're done. */
6618 if (cp_parser_parse_definitely (parser))
6619 break;
6620
6621 /* If the functional-cast didn't work out, try a
6622 compound-literal. */
6623 if (cp_parser_allow_gnu_extensions_p (parser)
6624 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6625 {
6626 cp_expr initializer = NULL_TREE;
6627
6628 cp_parser_parse_tentatively (parser);
6629
6630 /* Avoid calling cp_parser_type_id pointlessly, see comment
6631 in cp_parser_cast_expression about c++/29234. */
6632 if (!cp_parser_compound_literal_p (parser))
6633 cp_parser_simulate_error (parser);
6634 else
6635 {
6636 /* Parse the type. */
6637 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6638 parser->in_type_id_in_expr_p = true;
6639 type = cp_parser_type_id (parser);
6640 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6641 /* Look for the `)'. */
6642 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6643 }
6644
6645 /* If things aren't going well, there's no need to
6646 keep going. */
6647 if (!cp_parser_error_occurred (parser))
6648 {
6649 bool non_constant_p;
6650 /* Parse the brace-enclosed initializer list. */
6651 initializer = cp_parser_braced_list (parser,
6652 &non_constant_p);
6653 }
6654 /* If that worked, we're definitely looking at a
6655 compound-literal expression. */
6656 if (cp_parser_parse_definitely (parser))
6657 {
6658 /* Warn the user that a compound literal is not
6659 allowed in standard C++. */
6660 pedwarn (input_location, OPT_Wpedantic,
6661 "ISO C++ forbids compound-literals");
6662 /* For simplicity, we disallow compound literals in
6663 constant-expressions. We could
6664 allow compound literals of integer type, whose
6665 initializer was a constant, in constant
6666 expressions. Permitting that usage, as a further
6667 extension, would not change the meaning of any
6668 currently accepted programs. (Of course, as
6669 compound literals are not part of ISO C++, the
6670 standard has nothing to say.) */
6671 if (cp_parser_non_integral_constant_expression (parser,
6672 NIC_NCC))
6673 {
6674 postfix_expression = error_mark_node;
6675 break;
6676 }
6677 /* Form the representation of the compound-literal. */
6678 postfix_expression
6679 = finish_compound_literal (type, initializer,
6680 tf_warning_or_error);
6681 postfix_expression.set_location (initializer.get_location ());
6682 break;
6683 }
6684 }
6685
6686 /* It must be a primary-expression. */
6687 postfix_expression
6688 = cp_parser_primary_expression (parser, address_p, cast_p,
6689 /*template_arg_p=*/false,
6690 decltype_p,
6691 &idk);
6692 }
6693 break;
6694 }
6695
6696 /* Note that we don't need to worry about calling build_cplus_new on a
6697 class-valued CALL_EXPR in decltype when it isn't the end of the
6698 postfix-expression; unary_complex_lvalue will take care of that for
6699 all these cases. */
6700
6701 /* Keep looping until the postfix-expression is complete. */
6702 while (true)
6703 {
6704 if (idk == CP_ID_KIND_UNQUALIFIED
6705 && identifier_p (postfix_expression)
6706 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6707 /* It is not a Koenig lookup function call. */
6708 postfix_expression
6709 = unqualified_name_lookup_error (postfix_expression);
6710
6711 /* Peek at the next token. */
6712 token = cp_lexer_peek_token (parser->lexer);
6713
6714 switch (token->type)
6715 {
6716 case CPP_OPEN_SQUARE:
6717 if (cp_next_tokens_can_be_std_attribute_p (parser))
6718 {
6719 cp_parser_error (parser,
6720 "two consecutive %<[%> shall "
6721 "only introduce an attribute");
6722 return error_mark_node;
6723 }
6724 postfix_expression
6725 = cp_parser_postfix_open_square_expression (parser,
6726 postfix_expression,
6727 false,
6728 decltype_p);
6729 postfix_expression.set_range (start_loc,
6730 postfix_expression.get_location ());
6731
6732 idk = CP_ID_KIND_NONE;
6733 is_member_access = false;
6734 break;
6735
6736 case CPP_OPEN_PAREN:
6737 /* postfix-expression ( expression-list [opt] ) */
6738 {
6739 bool koenig_p;
6740 bool is_builtin_constant_p;
6741 bool saved_integral_constant_expression_p = false;
6742 bool saved_non_integral_constant_expression_p = false;
6743 tsubst_flags_t complain = complain_flags (decltype_p);
6744 vec<tree, va_gc> *args;
6745 location_t close_paren_loc = UNKNOWN_LOCATION;
6746
6747 is_member_access = false;
6748
6749 is_builtin_constant_p
6750 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6751 if (is_builtin_constant_p)
6752 {
6753 /* The whole point of __builtin_constant_p is to allow
6754 non-constant expressions to appear as arguments. */
6755 saved_integral_constant_expression_p
6756 = parser->integral_constant_expression_p;
6757 saved_non_integral_constant_expression_p
6758 = parser->non_integral_constant_expression_p;
6759 parser->integral_constant_expression_p = false;
6760 }
6761 args = (cp_parser_parenthesized_expression_list
6762 (parser, non_attr,
6763 /*cast_p=*/false, /*allow_expansion_p=*/true,
6764 /*non_constant_p=*/NULL,
6765 /*close_paren_loc=*/&close_paren_loc));
6766 if (is_builtin_constant_p)
6767 {
6768 parser->integral_constant_expression_p
6769 = saved_integral_constant_expression_p;
6770 parser->non_integral_constant_expression_p
6771 = saved_non_integral_constant_expression_p;
6772 }
6773
6774 if (args == NULL)
6775 {
6776 postfix_expression = error_mark_node;
6777 break;
6778 }
6779
6780 /* Function calls are not permitted in
6781 constant-expressions. */
6782 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6783 && cp_parser_non_integral_constant_expression (parser,
6784 NIC_FUNC_CALL))
6785 {
6786 postfix_expression = error_mark_node;
6787 release_tree_vector (args);
6788 break;
6789 }
6790
6791 koenig_p = false;
6792 if (idk == CP_ID_KIND_UNQUALIFIED
6793 || idk == CP_ID_KIND_TEMPLATE_ID)
6794 {
6795 if (identifier_p (postfix_expression))
6796 {
6797 if (!args->is_empty ())
6798 {
6799 koenig_p = true;
6800 if (!any_type_dependent_arguments_p (args))
6801 postfix_expression
6802 = perform_koenig_lookup (postfix_expression, args,
6803 complain);
6804 }
6805 else
6806 postfix_expression
6807 = unqualified_fn_lookup_error (postfix_expression);
6808 }
6809 /* We do not perform argument-dependent lookup if
6810 normal lookup finds a non-function, in accordance
6811 with the expected resolution of DR 218. */
6812 else if (!args->is_empty ()
6813 && is_overloaded_fn (postfix_expression))
6814 {
6815 tree fn = get_first_fn (postfix_expression);
6816 fn = STRIP_TEMPLATE (fn);
6817
6818 /* Do not do argument dependent lookup if regular
6819 lookup finds a member function or a block-scope
6820 function declaration. [basic.lookup.argdep]/3 */
6821 if (!DECL_FUNCTION_MEMBER_P (fn)
6822 && !DECL_LOCAL_FUNCTION_P (fn))
6823 {
6824 koenig_p = true;
6825 if (!any_type_dependent_arguments_p (args))
6826 postfix_expression
6827 = perform_koenig_lookup (postfix_expression, args,
6828 complain);
6829 }
6830 }
6831 }
6832
6833 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6834 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6835 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6836 && vec_safe_length (args) == 3)
6837 {
6838 tree arg0 = (*args)[0];
6839 tree arg1 = (*args)[1];
6840 tree arg2 = (*args)[2];
6841 int literal_mask = ((!!integer_zerop (arg1) << 1)
6842 | (!!integer_zerop (arg2) << 2));
6843 if (TREE_CODE (arg2) == CONST_DECL)
6844 arg2 = DECL_INITIAL (arg2);
6845 warn_for_memset (input_location, arg0, arg2, literal_mask);
6846 }
6847
6848 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6849 {
6850 tree instance = TREE_OPERAND (postfix_expression, 0);
6851 tree fn = TREE_OPERAND (postfix_expression, 1);
6852
6853 if (processing_template_decl
6854 && (type_dependent_object_expression_p (instance)
6855 || (!BASELINK_P (fn)
6856 && TREE_CODE (fn) != FIELD_DECL)
6857 || type_dependent_expression_p (fn)
6858 || any_type_dependent_arguments_p (args)))
6859 {
6860 postfix_expression
6861 = build_nt_call_vec (postfix_expression, args);
6862 release_tree_vector (args);
6863 break;
6864 }
6865
6866 if (BASELINK_P (fn))
6867 {
6868 postfix_expression
6869 = (build_new_method_call
6870 (instance, fn, &args, NULL_TREE,
6871 (idk == CP_ID_KIND_QUALIFIED
6872 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6873 : LOOKUP_NORMAL),
6874 /*fn_p=*/NULL,
6875 complain));
6876 }
6877 else
6878 postfix_expression
6879 = finish_call_expr (postfix_expression, &args,
6880 /*disallow_virtual=*/false,
6881 /*koenig_p=*/false,
6882 complain);
6883 }
6884 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6885 || TREE_CODE (postfix_expression) == MEMBER_REF
6886 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6887 postfix_expression = (build_offset_ref_call_from_tree
6888 (postfix_expression, &args,
6889 complain));
6890 else if (idk == CP_ID_KIND_QUALIFIED)
6891 /* A call to a static class member, or a namespace-scope
6892 function. */
6893 postfix_expression
6894 = finish_call_expr (postfix_expression, &args,
6895 /*disallow_virtual=*/true,
6896 koenig_p,
6897 complain);
6898 else
6899 /* All other function calls. */
6900 postfix_expression
6901 = finish_call_expr (postfix_expression, &args,
6902 /*disallow_virtual=*/false,
6903 koenig_p,
6904 complain);
6905
6906 if (close_paren_loc != UNKNOWN_LOCATION)
6907 {
6908 location_t combined_loc = make_location (token->location,
6909 start_loc,
6910 close_paren_loc);
6911 postfix_expression.set_location (combined_loc);
6912 }
6913
6914 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6915 idk = CP_ID_KIND_NONE;
6916
6917 release_tree_vector (args);
6918 }
6919 break;
6920
6921 case CPP_DOT:
6922 case CPP_DEREF:
6923 /* postfix-expression . template [opt] id-expression
6924 postfix-expression . pseudo-destructor-name
6925 postfix-expression -> template [opt] id-expression
6926 postfix-expression -> pseudo-destructor-name */
6927
6928 /* Consume the `.' or `->' operator. */
6929 cp_lexer_consume_token (parser->lexer);
6930
6931 postfix_expression
6932 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6933 postfix_expression,
6934 false, &idk, loc);
6935
6936 is_member_access = true;
6937 break;
6938
6939 case CPP_PLUS_PLUS:
6940 /* postfix-expression ++ */
6941 /* Consume the `++' token. */
6942 cp_lexer_consume_token (parser->lexer);
6943 /* Generate a representation for the complete expression. */
6944 postfix_expression
6945 = finish_increment_expr (postfix_expression,
6946 POSTINCREMENT_EXPR);
6947 /* Increments may not appear in constant-expressions. */
6948 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6949 postfix_expression = error_mark_node;
6950 idk = CP_ID_KIND_NONE;
6951 is_member_access = false;
6952 break;
6953
6954 case CPP_MINUS_MINUS:
6955 /* postfix-expression -- */
6956 /* Consume the `--' token. */
6957 cp_lexer_consume_token (parser->lexer);
6958 /* Generate a representation for the complete expression. */
6959 postfix_expression
6960 = finish_increment_expr (postfix_expression,
6961 POSTDECREMENT_EXPR);
6962 /* Decrements may not appear in constant-expressions. */
6963 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6964 postfix_expression = error_mark_node;
6965 idk = CP_ID_KIND_NONE;
6966 is_member_access = false;
6967 break;
6968
6969 default:
6970 if (pidk_return != NULL)
6971 * pidk_return = idk;
6972 if (member_access_only_p)
6973 return is_member_access
6974 ? postfix_expression
6975 : cp_expr (error_mark_node);
6976 else
6977 return postfix_expression;
6978 }
6979 }
6980
6981 /* We should never get here. */
6982 gcc_unreachable ();
6983 return error_mark_node;
6984 }
6985
6986 /* This function parses Cilk Plus array notations. If a normal array expr. is
6987 parsed then the array index is passed back to the caller through *INIT_INDEX
6988 and the function returns a NULL_TREE. If array notation expr. is parsed,
6989 then *INIT_INDEX is ignored by the caller and the function returns
6990 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6991 error_mark_node. */
6992
6993 static tree
6994 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6995 tree array_value)
6996 {
6997 cp_token *token = NULL;
6998 tree length_index, stride = NULL_TREE, value_tree, array_type;
6999 if (!array_value || array_value == error_mark_node)
7000 {
7001 cp_parser_skip_to_end_of_statement (parser);
7002 return error_mark_node;
7003 }
7004
7005 array_type = TREE_TYPE (array_value);
7006
7007 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7008 parser->colon_corrects_to_scope_p = false;
7009 token = cp_lexer_peek_token (parser->lexer);
7010
7011 if (!token)
7012 {
7013 cp_parser_error (parser, "expected %<:%> or numeral");
7014 return error_mark_node;
7015 }
7016 else if (token->type == CPP_COLON)
7017 {
7018 /* Consume the ':'. */
7019 cp_lexer_consume_token (parser->lexer);
7020
7021 /* If we are here, then we have a case like this A[:]. */
7022 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7023 {
7024 cp_parser_error (parser, "expected %<]%>");
7025 cp_parser_skip_to_end_of_statement (parser);
7026 return error_mark_node;
7027 }
7028 *init_index = NULL_TREE;
7029 stride = NULL_TREE;
7030 length_index = NULL_TREE;
7031 }
7032 else
7033 {
7034 /* If we are here, then there are three valid possibilities:
7035 1. ARRAY [ EXP ]
7036 2. ARRAY [ EXP : EXP ]
7037 3. ARRAY [ EXP : EXP : EXP ] */
7038
7039 *init_index = cp_parser_expression (parser);
7040 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7041 {
7042 /* This indicates that we have a normal array expression. */
7043 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7044 return NULL_TREE;
7045 }
7046
7047 /* Consume the ':'. */
7048 cp_lexer_consume_token (parser->lexer);
7049 length_index = cp_parser_expression (parser);
7050 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7051 {
7052 cp_lexer_consume_token (parser->lexer);
7053 stride = cp_parser_expression (parser);
7054 }
7055 }
7056 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7057
7058 if (*init_index == error_mark_node || length_index == error_mark_node
7059 || stride == error_mark_node || array_type == error_mark_node)
7060 {
7061 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7062 cp_lexer_consume_token (parser->lexer);
7063 return error_mark_node;
7064 }
7065 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7066
7067 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7068 length_index, stride, array_type);
7069 return value_tree;
7070 }
7071
7072 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7073 by cp_parser_builtin_offsetof. We're looking for
7074
7075 postfix-expression [ expression ]
7076 postfix-expression [ braced-init-list ] (C++11)
7077
7078 FOR_OFFSETOF is set if we're being called in that context, which
7079 changes how we deal with integer constant expressions. */
7080
7081 static tree
7082 cp_parser_postfix_open_square_expression (cp_parser *parser,
7083 tree postfix_expression,
7084 bool for_offsetof,
7085 bool decltype_p)
7086 {
7087 tree index = NULL_TREE;
7088 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7089 bool saved_greater_than_is_operator_p;
7090
7091 /* Consume the `[' token. */
7092 cp_lexer_consume_token (parser->lexer);
7093
7094 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7095 parser->greater_than_is_operator_p = true;
7096
7097 /* Parse the index expression. */
7098 /* ??? For offsetof, there is a question of what to allow here. If
7099 offsetof is not being used in an integral constant expression context,
7100 then we *could* get the right answer by computing the value at runtime.
7101 If we are in an integral constant expression context, then we might
7102 could accept any constant expression; hard to say without analysis.
7103 Rather than open the barn door too wide right away, allow only integer
7104 constant expressions here. */
7105 if (for_offsetof)
7106 index = cp_parser_constant_expression (parser);
7107 else
7108 {
7109 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7110 {
7111 bool expr_nonconst_p;
7112 cp_lexer_set_source_position (parser->lexer);
7113 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7114 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7115 if (flag_cilkplus
7116 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7117 {
7118 error_at (cp_lexer_peek_token (parser->lexer)->location,
7119 "braced list index is not allowed with array "
7120 "notation");
7121 cp_parser_skip_to_end_of_statement (parser);
7122 return error_mark_node;
7123 }
7124 }
7125 else if (flag_cilkplus)
7126 {
7127 /* Here are have these two options:
7128 ARRAY[EXP : EXP] - Array notation expr with default
7129 stride of 1.
7130 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7131 stride. */
7132 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7133 postfix_expression);
7134 if (an_exp)
7135 return an_exp;
7136 }
7137 else
7138 index = cp_parser_expression (parser);
7139 }
7140
7141 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7142
7143 /* Look for the closing `]'. */
7144 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7145
7146 /* Build the ARRAY_REF. */
7147 postfix_expression = grok_array_decl (loc, postfix_expression,
7148 index, decltype_p);
7149
7150 /* When not doing offsetof, array references are not permitted in
7151 constant-expressions. */
7152 if (!for_offsetof
7153 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7154 postfix_expression = error_mark_node;
7155
7156 return postfix_expression;
7157 }
7158
7159 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7160 by cp_parser_builtin_offsetof. We're looking for
7161
7162 postfix-expression . template [opt] id-expression
7163 postfix-expression . pseudo-destructor-name
7164 postfix-expression -> template [opt] id-expression
7165 postfix-expression -> pseudo-destructor-name
7166
7167 FOR_OFFSETOF is set if we're being called in that context. That sorta
7168 limits what of the above we'll actually accept, but nevermind.
7169 TOKEN_TYPE is the "." or "->" token, which will already have been
7170 removed from the stream. */
7171
7172 static tree
7173 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7174 enum cpp_ttype token_type,
7175 cp_expr postfix_expression,
7176 bool for_offsetof, cp_id_kind *idk,
7177 location_t location)
7178 {
7179 tree name;
7180 bool dependent_p;
7181 bool pseudo_destructor_p;
7182 tree scope = NULL_TREE;
7183 location_t start_loc = postfix_expression.get_start ();
7184
7185 /* If this is a `->' operator, dereference the pointer. */
7186 if (token_type == CPP_DEREF)
7187 postfix_expression = build_x_arrow (location, postfix_expression,
7188 tf_warning_or_error);
7189 /* Check to see whether or not the expression is type-dependent and
7190 not the current instantiation. */
7191 dependent_p = type_dependent_object_expression_p (postfix_expression);
7192 /* The identifier following the `->' or `.' is not qualified. */
7193 parser->scope = NULL_TREE;
7194 parser->qualifying_scope = NULL_TREE;
7195 parser->object_scope = NULL_TREE;
7196 *idk = CP_ID_KIND_NONE;
7197
7198 /* Enter the scope corresponding to the type of the object
7199 given by the POSTFIX_EXPRESSION. */
7200 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7201 {
7202 scope = TREE_TYPE (postfix_expression);
7203 /* According to the standard, no expression should ever have
7204 reference type. Unfortunately, we do not currently match
7205 the standard in this respect in that our internal representation
7206 of an expression may have reference type even when the standard
7207 says it does not. Therefore, we have to manually obtain the
7208 underlying type here. */
7209 scope = non_reference (scope);
7210 /* The type of the POSTFIX_EXPRESSION must be complete. */
7211 /* Unlike the object expression in other contexts, *this is not
7212 required to be of complete type for purposes of class member
7213 access (5.2.5) outside the member function body. */
7214 if (postfix_expression != current_class_ref
7215 && !(processing_template_decl
7216 && current_class_type
7217 && (same_type_ignoring_top_level_qualifiers_p
7218 (scope, current_class_type))))
7219 scope = complete_type_or_else (scope, postfix_expression);
7220 /* Let the name lookup machinery know that we are processing a
7221 class member access expression. */
7222 parser->context->object_type = scope;
7223 /* If something went wrong, we want to be able to discern that case,
7224 as opposed to the case where there was no SCOPE due to the type
7225 of expression being dependent. */
7226 if (!scope)
7227 scope = error_mark_node;
7228 /* If the SCOPE was erroneous, make the various semantic analysis
7229 functions exit quickly -- and without issuing additional error
7230 messages. */
7231 if (scope == error_mark_node)
7232 postfix_expression = error_mark_node;
7233 }
7234 else
7235 /* Tell cp_parser_lookup_name that there was an object, even though it's
7236 type-dependent. */
7237 parser->context->object_type = unknown_type_node;
7238
7239 /* Assume this expression is not a pseudo-destructor access. */
7240 pseudo_destructor_p = false;
7241
7242 /* If the SCOPE is a scalar type, then, if this is a valid program,
7243 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7244 is type dependent, it can be pseudo-destructor-name or something else.
7245 Try to parse it as pseudo-destructor-name first. */
7246 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7247 {
7248 tree s;
7249 tree type;
7250
7251 cp_parser_parse_tentatively (parser);
7252 /* Parse the pseudo-destructor-name. */
7253 s = NULL_TREE;
7254 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7255 &s, &type);
7256 if (dependent_p
7257 && (cp_parser_error_occurred (parser)
7258 || !SCALAR_TYPE_P (type)))
7259 cp_parser_abort_tentative_parse (parser);
7260 else if (cp_parser_parse_definitely (parser))
7261 {
7262 pseudo_destructor_p = true;
7263 postfix_expression
7264 = finish_pseudo_destructor_expr (postfix_expression,
7265 s, type, location);
7266 }
7267 }
7268
7269 if (!pseudo_destructor_p)
7270 {
7271 /* If the SCOPE is not a scalar type, we are looking at an
7272 ordinary class member access expression, rather than a
7273 pseudo-destructor-name. */
7274 bool template_p;
7275 cp_token *token = cp_lexer_peek_token (parser->lexer);
7276 /* Parse the id-expression. */
7277 name = (cp_parser_id_expression
7278 (parser,
7279 cp_parser_optional_template_keyword (parser),
7280 /*check_dependency_p=*/true,
7281 &template_p,
7282 /*declarator_p=*/false,
7283 /*optional_p=*/false));
7284 /* In general, build a SCOPE_REF if the member name is qualified.
7285 However, if the name was not dependent and has already been
7286 resolved; there is no need to build the SCOPE_REF. For example;
7287
7288 struct X { void f(); };
7289 template <typename T> void f(T* t) { t->X::f(); }
7290
7291 Even though "t" is dependent, "X::f" is not and has been resolved
7292 to a BASELINK; there is no need to include scope information. */
7293
7294 /* But we do need to remember that there was an explicit scope for
7295 virtual function calls. */
7296 if (parser->scope)
7297 *idk = CP_ID_KIND_QUALIFIED;
7298
7299 /* If the name is a template-id that names a type, we will get a
7300 TYPE_DECL here. That is invalid code. */
7301 if (TREE_CODE (name) == TYPE_DECL)
7302 {
7303 error_at (token->location, "invalid use of %qD", name);
7304 postfix_expression = error_mark_node;
7305 }
7306 else
7307 {
7308 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7309 {
7310 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7311 {
7312 error_at (token->location, "%<%D::%D%> is not a class member",
7313 parser->scope, name);
7314 postfix_expression = error_mark_node;
7315 }
7316 else
7317 name = build_qualified_name (/*type=*/NULL_TREE,
7318 parser->scope,
7319 name,
7320 template_p);
7321 parser->scope = NULL_TREE;
7322 parser->qualifying_scope = NULL_TREE;
7323 parser->object_scope = NULL_TREE;
7324 }
7325 if (parser->scope && name && BASELINK_P (name))
7326 adjust_result_of_qualified_name_lookup
7327 (name, parser->scope, scope);
7328 postfix_expression
7329 = finish_class_member_access_expr (postfix_expression, name,
7330 template_p,
7331 tf_warning_or_error);
7332 /* Build a location e.g.:
7333 ptr->access_expr
7334 ~~~^~~~~~~~~~~~~
7335 where the caret is at the deref token, ranging from
7336 the start of postfix_expression to the end of the access expr. */
7337 location_t end_loc
7338 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7339 location_t combined_loc
7340 = make_location (input_location, start_loc, end_loc);
7341 protected_set_expr_location (postfix_expression, combined_loc);
7342 }
7343 }
7344
7345 /* We no longer need to look up names in the scope of the object on
7346 the left-hand side of the `.' or `->' operator. */
7347 parser->context->object_type = NULL_TREE;
7348
7349 /* Outside of offsetof, these operators may not appear in
7350 constant-expressions. */
7351 if (!for_offsetof
7352 && (cp_parser_non_integral_constant_expression
7353 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7354 postfix_expression = error_mark_node;
7355
7356 return postfix_expression;
7357 }
7358
7359 /* Parse a parenthesized expression-list.
7360
7361 expression-list:
7362 assignment-expression
7363 expression-list, assignment-expression
7364
7365 attribute-list:
7366 expression-list
7367 identifier
7368 identifier, expression-list
7369
7370 CAST_P is true if this expression is the target of a cast.
7371
7372 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7373 argument pack.
7374
7375 Returns a vector of trees. Each element is a representation of an
7376 assignment-expression. NULL is returned if the ( and or ) are
7377 missing. An empty, but allocated, vector is returned on no
7378 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7379 if we are parsing an attribute list for an attribute that wants a
7380 plain identifier argument, normal_attr for an attribute that wants
7381 an expression, or non_attr if we aren't parsing an attribute list. If
7382 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7383 not all of the expressions in the list were constant.
7384 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7385 will be written to with the location of the closing parenthesis. If
7386 an error occurs, it may or may not be written to. */
7387
7388 static vec<tree, va_gc> *
7389 cp_parser_parenthesized_expression_list (cp_parser* parser,
7390 int is_attribute_list,
7391 bool cast_p,
7392 bool allow_expansion_p,
7393 bool *non_constant_p,
7394 location_t *close_paren_loc)
7395 {
7396 vec<tree, va_gc> *expression_list;
7397 bool fold_expr_p = is_attribute_list != non_attr;
7398 tree identifier = NULL_TREE;
7399 bool saved_greater_than_is_operator_p;
7400
7401 /* Assume all the expressions will be constant. */
7402 if (non_constant_p)
7403 *non_constant_p = false;
7404
7405 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7406 return NULL;
7407
7408 expression_list = make_tree_vector ();
7409
7410 /* Within a parenthesized expression, a `>' token is always
7411 the greater-than operator. */
7412 saved_greater_than_is_operator_p
7413 = parser->greater_than_is_operator_p;
7414 parser->greater_than_is_operator_p = true;
7415
7416 /* Consume expressions until there are no more. */
7417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7418 while (true)
7419 {
7420 tree expr;
7421
7422 /* At the beginning of attribute lists, check to see if the
7423 next token is an identifier. */
7424 if (is_attribute_list == id_attr
7425 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7426 {
7427 cp_token *token;
7428
7429 /* Consume the identifier. */
7430 token = cp_lexer_consume_token (parser->lexer);
7431 /* Save the identifier. */
7432 identifier = token->u.value;
7433 }
7434 else
7435 {
7436 bool expr_non_constant_p;
7437
7438 /* Parse the next assignment-expression. */
7439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7440 {
7441 /* A braced-init-list. */
7442 cp_lexer_set_source_position (parser->lexer);
7443 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7444 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7445 if (non_constant_p && expr_non_constant_p)
7446 *non_constant_p = true;
7447 }
7448 else if (non_constant_p)
7449 {
7450 expr = (cp_parser_constant_expression
7451 (parser, /*allow_non_constant_p=*/true,
7452 &expr_non_constant_p));
7453 if (expr_non_constant_p)
7454 *non_constant_p = true;
7455 }
7456 else
7457 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7458 cast_p);
7459
7460 if (fold_expr_p)
7461 expr = instantiate_non_dependent_expr (expr);
7462
7463 /* If we have an ellipsis, then this is an expression
7464 expansion. */
7465 if (allow_expansion_p
7466 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7467 {
7468 /* Consume the `...'. */
7469 cp_lexer_consume_token (parser->lexer);
7470
7471 /* Build the argument pack. */
7472 expr = make_pack_expansion (expr);
7473 }
7474
7475 /* Add it to the list. We add error_mark_node
7476 expressions to the list, so that we can still tell if
7477 the correct form for a parenthesized expression-list
7478 is found. That gives better errors. */
7479 vec_safe_push (expression_list, expr);
7480
7481 if (expr == error_mark_node)
7482 goto skip_comma;
7483 }
7484
7485 /* After the first item, attribute lists look the same as
7486 expression lists. */
7487 is_attribute_list = non_attr;
7488
7489 get_comma:;
7490 /* If the next token isn't a `,', then we are done. */
7491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7492 break;
7493
7494 /* Otherwise, consume the `,' and keep going. */
7495 cp_lexer_consume_token (parser->lexer);
7496 }
7497
7498 if (close_paren_loc)
7499 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7500
7501 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7502 {
7503 int ending;
7504
7505 skip_comma:;
7506 /* We try and resync to an unnested comma, as that will give the
7507 user better diagnostics. */
7508 ending = cp_parser_skip_to_closing_parenthesis (parser,
7509 /*recovering=*/true,
7510 /*or_comma=*/true,
7511 /*consume_paren=*/true);
7512 if (ending < 0)
7513 goto get_comma;
7514 if (!ending)
7515 {
7516 parser->greater_than_is_operator_p
7517 = saved_greater_than_is_operator_p;
7518 return NULL;
7519 }
7520 }
7521
7522 parser->greater_than_is_operator_p
7523 = saved_greater_than_is_operator_p;
7524
7525 if (identifier)
7526 vec_safe_insert (expression_list, 0, identifier);
7527
7528 return expression_list;
7529 }
7530
7531 /* Parse a pseudo-destructor-name.
7532
7533 pseudo-destructor-name:
7534 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7535 :: [opt] nested-name-specifier template template-id :: ~ type-name
7536 :: [opt] nested-name-specifier [opt] ~ type-name
7537
7538 If either of the first two productions is used, sets *SCOPE to the
7539 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7540 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7541 or ERROR_MARK_NODE if the parse fails. */
7542
7543 static void
7544 cp_parser_pseudo_destructor_name (cp_parser* parser,
7545 tree object,
7546 tree* scope,
7547 tree* type)
7548 {
7549 bool nested_name_specifier_p;
7550
7551 /* Handle ~auto. */
7552 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7553 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7554 && !type_dependent_expression_p (object))
7555 {
7556 if (cxx_dialect < cxx14)
7557 pedwarn (input_location, 0,
7558 "%<~auto%> only available with "
7559 "-std=c++14 or -std=gnu++14");
7560 cp_lexer_consume_token (parser->lexer);
7561 cp_lexer_consume_token (parser->lexer);
7562 *scope = NULL_TREE;
7563 *type = TREE_TYPE (object);
7564 return;
7565 }
7566
7567 /* Assume that things will not work out. */
7568 *type = error_mark_node;
7569
7570 /* Look for the optional `::' operator. */
7571 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7572 /* Look for the optional nested-name-specifier. */
7573 nested_name_specifier_p
7574 = (cp_parser_nested_name_specifier_opt (parser,
7575 /*typename_keyword_p=*/false,
7576 /*check_dependency_p=*/true,
7577 /*type_p=*/false,
7578 /*is_declaration=*/false)
7579 != NULL_TREE);
7580 /* Now, if we saw a nested-name-specifier, we might be doing the
7581 second production. */
7582 if (nested_name_specifier_p
7583 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7584 {
7585 /* Consume the `template' keyword. */
7586 cp_lexer_consume_token (parser->lexer);
7587 /* Parse the template-id. */
7588 cp_parser_template_id (parser,
7589 /*template_keyword_p=*/true,
7590 /*check_dependency_p=*/false,
7591 class_type,
7592 /*is_declaration=*/true);
7593 /* Look for the `::' token. */
7594 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7595 }
7596 /* If the next token is not a `~', then there might be some
7597 additional qualification. */
7598 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7599 {
7600 /* At this point, we're looking for "type-name :: ~". The type-name
7601 must not be a class-name, since this is a pseudo-destructor. So,
7602 it must be either an enum-name, or a typedef-name -- both of which
7603 are just identifiers. So, we peek ahead to check that the "::"
7604 and "~" tokens are present; if they are not, then we can avoid
7605 calling type_name. */
7606 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7607 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7608 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7609 {
7610 cp_parser_error (parser, "non-scalar type");
7611 return;
7612 }
7613
7614 /* Look for the type-name. */
7615 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7616 if (*scope == error_mark_node)
7617 return;
7618
7619 /* Look for the `::' token. */
7620 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7621 }
7622 else
7623 *scope = NULL_TREE;
7624
7625 /* Look for the `~'. */
7626 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7627
7628 /* Once we see the ~, this has to be a pseudo-destructor. */
7629 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7630 cp_parser_commit_to_topmost_tentative_parse (parser);
7631
7632 /* Look for the type-name again. We are not responsible for
7633 checking that it matches the first type-name. */
7634 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7635 }
7636
7637 /* Parse a unary-expression.
7638
7639 unary-expression:
7640 postfix-expression
7641 ++ cast-expression
7642 -- cast-expression
7643 unary-operator cast-expression
7644 sizeof unary-expression
7645 sizeof ( type-id )
7646 alignof ( type-id ) [C++0x]
7647 new-expression
7648 delete-expression
7649
7650 GNU Extensions:
7651
7652 unary-expression:
7653 __extension__ cast-expression
7654 __alignof__ unary-expression
7655 __alignof__ ( type-id )
7656 alignof unary-expression [C++0x]
7657 __real__ cast-expression
7658 __imag__ cast-expression
7659 && identifier
7660 sizeof ( type-id ) { initializer-list , [opt] }
7661 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7662 __alignof__ ( type-id ) { initializer-list , [opt] }
7663
7664 ADDRESS_P is true iff the unary-expression is appearing as the
7665 operand of the `&' operator. CAST_P is true if this expression is
7666 the target of a cast.
7667
7668 Returns a representation of the expression. */
7669
7670 static cp_expr
7671 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7672 bool address_p, bool cast_p, bool decltype_p)
7673 {
7674 cp_token *token;
7675 enum tree_code unary_operator;
7676
7677 /* Peek at the next token. */
7678 token = cp_lexer_peek_token (parser->lexer);
7679 /* Some keywords give away the kind of expression. */
7680 if (token->type == CPP_KEYWORD)
7681 {
7682 enum rid keyword = token->keyword;
7683
7684 switch (keyword)
7685 {
7686 case RID_ALIGNOF:
7687 case RID_SIZEOF:
7688 {
7689 tree operand, ret;
7690 enum tree_code op;
7691 location_t first_loc;
7692
7693 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7694 /* Consume the token. */
7695 cp_lexer_consume_token (parser->lexer);
7696 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7697 /* Parse the operand. */
7698 operand = cp_parser_sizeof_operand (parser, keyword);
7699
7700 if (TYPE_P (operand))
7701 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7702 else
7703 {
7704 /* ISO C++ defines alignof only with types, not with
7705 expressions. So pedwarn if alignof is used with a non-
7706 type expression. However, __alignof__ is ok. */
7707 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7708 pedwarn (token->location, OPT_Wpedantic,
7709 "ISO C++ does not allow %<alignof%> "
7710 "with a non-type");
7711
7712 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7713 }
7714 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7715 SIZEOF_EXPR with the original operand. */
7716 if (op == SIZEOF_EXPR && ret != error_mark_node)
7717 {
7718 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7719 {
7720 if (!processing_template_decl && TYPE_P (operand))
7721 {
7722 ret = build_min (SIZEOF_EXPR, size_type_node,
7723 build1 (NOP_EXPR, operand,
7724 error_mark_node));
7725 SIZEOF_EXPR_TYPE_P (ret) = 1;
7726 }
7727 else
7728 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7729 TREE_SIDE_EFFECTS (ret) = 0;
7730 TREE_READONLY (ret) = 1;
7731 }
7732 SET_EXPR_LOCATION (ret, first_loc);
7733 }
7734 return ret;
7735 }
7736
7737 case RID_NEW:
7738 return cp_parser_new_expression (parser);
7739
7740 case RID_DELETE:
7741 return cp_parser_delete_expression (parser);
7742
7743 case RID_EXTENSION:
7744 {
7745 /* The saved value of the PEDANTIC flag. */
7746 int saved_pedantic;
7747 tree expr;
7748
7749 /* Save away the PEDANTIC flag. */
7750 cp_parser_extension_opt (parser, &saved_pedantic);
7751 /* Parse the cast-expression. */
7752 expr = cp_parser_simple_cast_expression (parser);
7753 /* Restore the PEDANTIC flag. */
7754 pedantic = saved_pedantic;
7755
7756 return expr;
7757 }
7758
7759 case RID_REALPART:
7760 case RID_IMAGPART:
7761 {
7762 tree expression;
7763
7764 /* Consume the `__real__' or `__imag__' token. */
7765 cp_lexer_consume_token (parser->lexer);
7766 /* Parse the cast-expression. */
7767 expression = cp_parser_simple_cast_expression (parser);
7768 /* Create the complete representation. */
7769 return build_x_unary_op (token->location,
7770 (keyword == RID_REALPART
7771 ? REALPART_EXPR : IMAGPART_EXPR),
7772 expression,
7773 tf_warning_or_error);
7774 }
7775 break;
7776
7777 case RID_TRANSACTION_ATOMIC:
7778 case RID_TRANSACTION_RELAXED:
7779 return cp_parser_transaction_expression (parser, keyword);
7780
7781 case RID_NOEXCEPT:
7782 {
7783 tree expr;
7784 const char *saved_message;
7785 bool saved_integral_constant_expression_p;
7786 bool saved_non_integral_constant_expression_p;
7787 bool saved_greater_than_is_operator_p;
7788
7789 cp_lexer_consume_token (parser->lexer);
7790 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7791
7792 saved_message = parser->type_definition_forbidden_message;
7793 parser->type_definition_forbidden_message
7794 = G_("types may not be defined in %<noexcept%> expressions");
7795
7796 saved_integral_constant_expression_p
7797 = parser->integral_constant_expression_p;
7798 saved_non_integral_constant_expression_p
7799 = parser->non_integral_constant_expression_p;
7800 parser->integral_constant_expression_p = false;
7801
7802 saved_greater_than_is_operator_p
7803 = parser->greater_than_is_operator_p;
7804 parser->greater_than_is_operator_p = true;
7805
7806 ++cp_unevaluated_operand;
7807 ++c_inhibit_evaluation_warnings;
7808 ++cp_noexcept_operand;
7809 expr = cp_parser_expression (parser);
7810 --cp_noexcept_operand;
7811 --c_inhibit_evaluation_warnings;
7812 --cp_unevaluated_operand;
7813
7814 parser->greater_than_is_operator_p
7815 = saved_greater_than_is_operator_p;
7816
7817 parser->integral_constant_expression_p
7818 = saved_integral_constant_expression_p;
7819 parser->non_integral_constant_expression_p
7820 = saved_non_integral_constant_expression_p;
7821
7822 parser->type_definition_forbidden_message = saved_message;
7823
7824 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7825 return finish_noexcept_expr (expr, tf_warning_or_error);
7826 }
7827
7828 default:
7829 break;
7830 }
7831 }
7832
7833 /* Look for the `:: new' and `:: delete', which also signal the
7834 beginning of a new-expression, or delete-expression,
7835 respectively. If the next token is `::', then it might be one of
7836 these. */
7837 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7838 {
7839 enum rid keyword;
7840
7841 /* See if the token after the `::' is one of the keywords in
7842 which we're interested. */
7843 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7844 /* If it's `new', we have a new-expression. */
7845 if (keyword == RID_NEW)
7846 return cp_parser_new_expression (parser);
7847 /* Similarly, for `delete'. */
7848 else if (keyword == RID_DELETE)
7849 return cp_parser_delete_expression (parser);
7850 }
7851
7852 /* Look for a unary operator. */
7853 unary_operator = cp_parser_unary_operator (token);
7854 /* The `++' and `--' operators can be handled similarly, even though
7855 they are not technically unary-operators in the grammar. */
7856 if (unary_operator == ERROR_MARK)
7857 {
7858 if (token->type == CPP_PLUS_PLUS)
7859 unary_operator = PREINCREMENT_EXPR;
7860 else if (token->type == CPP_MINUS_MINUS)
7861 unary_operator = PREDECREMENT_EXPR;
7862 /* Handle the GNU address-of-label extension. */
7863 else if (cp_parser_allow_gnu_extensions_p (parser)
7864 && token->type == CPP_AND_AND)
7865 {
7866 tree identifier;
7867 tree expression;
7868 location_t start_loc = token->location;
7869
7870 /* Consume the '&&' token. */
7871 cp_lexer_consume_token (parser->lexer);
7872 /* Look for the identifier. */
7873 location_t finish_loc
7874 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7875 identifier = cp_parser_identifier (parser);
7876 /* Construct a location of the form:
7877 &&label
7878 ^~~~~~~
7879 with caret==start at the "&&", finish at the end of the label. */
7880 location_t combined_loc
7881 = make_location (start_loc, start_loc, finish_loc);
7882 /* Create an expression representing the address. */
7883 expression = finish_label_address_expr (identifier, combined_loc);
7884 if (cp_parser_non_integral_constant_expression (parser,
7885 NIC_ADDR_LABEL))
7886 expression = error_mark_node;
7887 return expression;
7888 }
7889 }
7890 if (unary_operator != ERROR_MARK)
7891 {
7892 cp_expr cast_expression;
7893 cp_expr expression = error_mark_node;
7894 non_integral_constant non_constant_p = NIC_NONE;
7895 location_t loc = token->location;
7896 tsubst_flags_t complain = complain_flags (decltype_p);
7897
7898 /* Consume the operator token. */
7899 token = cp_lexer_consume_token (parser->lexer);
7900 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7901
7902 /* Parse the cast-expression. */
7903 cast_expression
7904 = cp_parser_cast_expression (parser,
7905 unary_operator == ADDR_EXPR,
7906 /*cast_p=*/false,
7907 /*decltype*/false,
7908 pidk);
7909
7910 /* Make a location:
7911 OP_TOKEN CAST_EXPRESSION
7912 ^~~~~~~~~~~~~~~~~~~~~~~~~
7913 with start==caret at the operator token, and
7914 extending to the end of the cast_expression. */
7915 loc = make_location (loc, loc, cast_expression.get_finish ());
7916
7917 /* Now, build an appropriate representation. */
7918 switch (unary_operator)
7919 {
7920 case INDIRECT_REF:
7921 non_constant_p = NIC_STAR;
7922 expression = build_x_indirect_ref (loc, cast_expression,
7923 RO_UNARY_STAR,
7924 complain);
7925 /* TODO: build_x_indirect_ref does not always honor the
7926 location, so ensure it is set. */
7927 expression.set_location (loc);
7928 break;
7929
7930 case ADDR_EXPR:
7931 non_constant_p = NIC_ADDR;
7932 /* Fall through. */
7933 case BIT_NOT_EXPR:
7934 expression = build_x_unary_op (loc, unary_operator,
7935 cast_expression,
7936 complain);
7937 /* TODO: build_x_unary_op does not always honor the location,
7938 so ensure it is set. */
7939 expression.set_location (loc);
7940 break;
7941
7942 case PREINCREMENT_EXPR:
7943 case PREDECREMENT_EXPR:
7944 non_constant_p = unary_operator == PREINCREMENT_EXPR
7945 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7946 /* Fall through. */
7947 case NEGATE_EXPR:
7948 /* Immediately fold negation of a constant, unless the constant is 0
7949 (since -0 == 0) or it would overflow. */
7950 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7951 && CONSTANT_CLASS_P (cast_expression)
7952 && !integer_zerop (cast_expression)
7953 && !TREE_OVERFLOW (cast_expression))
7954 {
7955 tree folded = fold_build1 (unary_operator,
7956 TREE_TYPE (cast_expression),
7957 cast_expression);
7958 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7959 {
7960 expression = cp_expr (folded, loc);
7961 break;
7962 }
7963 }
7964 /* Fall through. */
7965 case UNARY_PLUS_EXPR:
7966 case TRUTH_NOT_EXPR:
7967 expression = finish_unary_op_expr (loc, unary_operator,
7968 cast_expression, complain);
7969 break;
7970
7971 default:
7972 gcc_unreachable ();
7973 }
7974
7975 if (non_constant_p != NIC_NONE
7976 && cp_parser_non_integral_constant_expression (parser,
7977 non_constant_p))
7978 expression = error_mark_node;
7979
7980 return expression;
7981 }
7982
7983 return cp_parser_postfix_expression (parser, address_p, cast_p,
7984 /*member_access_only_p=*/false,
7985 decltype_p,
7986 pidk);
7987 }
7988
7989 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7990 unary-operator, the corresponding tree code is returned. */
7991
7992 static enum tree_code
7993 cp_parser_unary_operator (cp_token* token)
7994 {
7995 switch (token->type)
7996 {
7997 case CPP_MULT:
7998 return INDIRECT_REF;
7999
8000 case CPP_AND:
8001 return ADDR_EXPR;
8002
8003 case CPP_PLUS:
8004 return UNARY_PLUS_EXPR;
8005
8006 case CPP_MINUS:
8007 return NEGATE_EXPR;
8008
8009 case CPP_NOT:
8010 return TRUTH_NOT_EXPR;
8011
8012 case CPP_COMPL:
8013 return BIT_NOT_EXPR;
8014
8015 default:
8016 return ERROR_MARK;
8017 }
8018 }
8019
8020 /* Parse a new-expression.
8021
8022 new-expression:
8023 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8024 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8025
8026 Returns a representation of the expression. */
8027
8028 static tree
8029 cp_parser_new_expression (cp_parser* parser)
8030 {
8031 bool global_scope_p;
8032 vec<tree, va_gc> *placement;
8033 tree type;
8034 vec<tree, va_gc> *initializer;
8035 tree nelts = NULL_TREE;
8036 tree ret;
8037
8038 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8039
8040 /* Look for the optional `::' operator. */
8041 global_scope_p
8042 = (cp_parser_global_scope_opt (parser,
8043 /*current_scope_valid_p=*/false)
8044 != NULL_TREE);
8045 /* Look for the `new' operator. */
8046 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8047 /* There's no easy way to tell a new-placement from the
8048 `( type-id )' construct. */
8049 cp_parser_parse_tentatively (parser);
8050 /* Look for a new-placement. */
8051 placement = cp_parser_new_placement (parser);
8052 /* If that didn't work out, there's no new-placement. */
8053 if (!cp_parser_parse_definitely (parser))
8054 {
8055 if (placement != NULL)
8056 release_tree_vector (placement);
8057 placement = NULL;
8058 }
8059
8060 /* If the next token is a `(', then we have a parenthesized
8061 type-id. */
8062 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8063 {
8064 cp_token *token;
8065 const char *saved_message = parser->type_definition_forbidden_message;
8066
8067 /* Consume the `('. */
8068 cp_lexer_consume_token (parser->lexer);
8069
8070 /* Parse the type-id. */
8071 parser->type_definition_forbidden_message
8072 = G_("types may not be defined in a new-expression");
8073 {
8074 type_id_in_expr_sentinel s (parser);
8075 type = cp_parser_type_id (parser);
8076 }
8077 parser->type_definition_forbidden_message = saved_message;
8078
8079 /* Look for the closing `)'. */
8080 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8081 token = cp_lexer_peek_token (parser->lexer);
8082 /* There should not be a direct-new-declarator in this production,
8083 but GCC used to allowed this, so we check and emit a sensible error
8084 message for this case. */
8085 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8086 {
8087 error_at (token->location,
8088 "array bound forbidden after parenthesized type-id");
8089 inform (token->location,
8090 "try removing the parentheses around the type-id");
8091 cp_parser_direct_new_declarator (parser);
8092 }
8093 }
8094 /* Otherwise, there must be a new-type-id. */
8095 else
8096 type = cp_parser_new_type_id (parser, &nelts);
8097
8098 /* If the next token is a `(' or '{', then we have a new-initializer. */
8099 cp_token *token = cp_lexer_peek_token (parser->lexer);
8100 if (token->type == CPP_OPEN_PAREN
8101 || token->type == CPP_OPEN_BRACE)
8102 initializer = cp_parser_new_initializer (parser);
8103 else
8104 initializer = NULL;
8105
8106 /* A new-expression may not appear in an integral constant
8107 expression. */
8108 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8109 ret = error_mark_node;
8110 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8111 of a new-type-id or type-id of a new-expression, the new-expression shall
8112 contain a new-initializer of the form ( assignment-expression )".
8113 Additionally, consistently with the spirit of DR 1467, we want to accept
8114 'new auto { 2 }' too. */
8115 else if (type_uses_auto (type)
8116 && (vec_safe_length (initializer) != 1
8117 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8118 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8119 {
8120 error_at (token->location,
8121 "initialization of new-expression for type %<auto%> "
8122 "requires exactly one element");
8123 ret = error_mark_node;
8124 }
8125 else
8126 {
8127 /* Construct a location e.g.:
8128 ptr = new int[100]
8129 ^~~~~~~~~~~~
8130 with caret == start at the start of the "new" token, and the end
8131 at the end of the final token we consumed. */
8132 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8133 location_t end_loc = get_finish (end_tok->location);
8134 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8135
8136 /* Create a representation of the new-expression. */
8137 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8138 tf_warning_or_error);
8139 protected_set_expr_location (ret, combined_loc);
8140 }
8141
8142 if (placement != NULL)
8143 release_tree_vector (placement);
8144 if (initializer != NULL)
8145 release_tree_vector (initializer);
8146
8147 return ret;
8148 }
8149
8150 /* Parse a new-placement.
8151
8152 new-placement:
8153 ( expression-list )
8154
8155 Returns the same representation as for an expression-list. */
8156
8157 static vec<tree, va_gc> *
8158 cp_parser_new_placement (cp_parser* parser)
8159 {
8160 vec<tree, va_gc> *expression_list;
8161
8162 /* Parse the expression-list. */
8163 expression_list = (cp_parser_parenthesized_expression_list
8164 (parser, non_attr, /*cast_p=*/false,
8165 /*allow_expansion_p=*/true,
8166 /*non_constant_p=*/NULL));
8167
8168 if (expression_list && expression_list->is_empty ())
8169 error ("expected expression-list or type-id");
8170
8171 return expression_list;
8172 }
8173
8174 /* Parse a new-type-id.
8175
8176 new-type-id:
8177 type-specifier-seq new-declarator [opt]
8178
8179 Returns the TYPE allocated. If the new-type-id indicates an array
8180 type, *NELTS is set to the number of elements in the last array
8181 bound; the TYPE will not include the last array bound. */
8182
8183 static tree
8184 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8185 {
8186 cp_decl_specifier_seq type_specifier_seq;
8187 cp_declarator *new_declarator;
8188 cp_declarator *declarator;
8189 cp_declarator *outer_declarator;
8190 const char *saved_message;
8191
8192 /* The type-specifier sequence must not contain type definitions.
8193 (It cannot contain declarations of new types either, but if they
8194 are not definitions we will catch that because they are not
8195 complete.) */
8196 saved_message = parser->type_definition_forbidden_message;
8197 parser->type_definition_forbidden_message
8198 = G_("types may not be defined in a new-type-id");
8199 /* Parse the type-specifier-seq. */
8200 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8201 /*is_trailing_return=*/false,
8202 &type_specifier_seq);
8203 /* Restore the old message. */
8204 parser->type_definition_forbidden_message = saved_message;
8205
8206 if (type_specifier_seq.type == error_mark_node)
8207 return error_mark_node;
8208
8209 /* Parse the new-declarator. */
8210 new_declarator = cp_parser_new_declarator_opt (parser);
8211
8212 /* Determine the number of elements in the last array dimension, if
8213 any. */
8214 *nelts = NULL_TREE;
8215 /* Skip down to the last array dimension. */
8216 declarator = new_declarator;
8217 outer_declarator = NULL;
8218 while (declarator && (declarator->kind == cdk_pointer
8219 || declarator->kind == cdk_ptrmem))
8220 {
8221 outer_declarator = declarator;
8222 declarator = declarator->declarator;
8223 }
8224 while (declarator
8225 && declarator->kind == cdk_array
8226 && declarator->declarator
8227 && declarator->declarator->kind == cdk_array)
8228 {
8229 outer_declarator = declarator;
8230 declarator = declarator->declarator;
8231 }
8232
8233 if (declarator && declarator->kind == cdk_array)
8234 {
8235 *nelts = declarator->u.array.bounds;
8236 if (*nelts == error_mark_node)
8237 *nelts = integer_one_node;
8238
8239 if (outer_declarator)
8240 outer_declarator->declarator = declarator->declarator;
8241 else
8242 new_declarator = NULL;
8243 }
8244
8245 return groktypename (&type_specifier_seq, new_declarator, false);
8246 }
8247
8248 /* Parse an (optional) new-declarator.
8249
8250 new-declarator:
8251 ptr-operator new-declarator [opt]
8252 direct-new-declarator
8253
8254 Returns the declarator. */
8255
8256 static cp_declarator *
8257 cp_parser_new_declarator_opt (cp_parser* parser)
8258 {
8259 enum tree_code code;
8260 tree type, std_attributes = NULL_TREE;
8261 cp_cv_quals cv_quals;
8262
8263 /* We don't know if there's a ptr-operator next, or not. */
8264 cp_parser_parse_tentatively (parser);
8265 /* Look for a ptr-operator. */
8266 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8267 /* If that worked, look for more new-declarators. */
8268 if (cp_parser_parse_definitely (parser))
8269 {
8270 cp_declarator *declarator;
8271
8272 /* Parse another optional declarator. */
8273 declarator = cp_parser_new_declarator_opt (parser);
8274
8275 declarator = cp_parser_make_indirect_declarator
8276 (code, type, cv_quals, declarator, std_attributes);
8277
8278 return declarator;
8279 }
8280
8281 /* If the next token is a `[', there is a direct-new-declarator. */
8282 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8283 return cp_parser_direct_new_declarator (parser);
8284
8285 return NULL;
8286 }
8287
8288 /* Parse a direct-new-declarator.
8289
8290 direct-new-declarator:
8291 [ expression ]
8292 direct-new-declarator [constant-expression]
8293
8294 */
8295
8296 static cp_declarator *
8297 cp_parser_direct_new_declarator (cp_parser* parser)
8298 {
8299 cp_declarator *declarator = NULL;
8300
8301 while (true)
8302 {
8303 tree expression;
8304 cp_token *token;
8305
8306 /* Look for the opening `['. */
8307 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8308
8309 token = cp_lexer_peek_token (parser->lexer);
8310 expression = cp_parser_expression (parser);
8311 /* The standard requires that the expression have integral
8312 type. DR 74 adds enumeration types. We believe that the
8313 real intent is that these expressions be handled like the
8314 expression in a `switch' condition, which also allows
8315 classes with a single conversion to integral or
8316 enumeration type. */
8317 if (!processing_template_decl)
8318 {
8319 expression
8320 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8321 expression,
8322 /*complain=*/true);
8323 if (!expression)
8324 {
8325 error_at (token->location,
8326 "expression in new-declarator must have integral "
8327 "or enumeration type");
8328 expression = error_mark_node;
8329 }
8330 }
8331
8332 /* Look for the closing `]'. */
8333 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8334
8335 /* Add this bound to the declarator. */
8336 declarator = make_array_declarator (declarator, expression);
8337
8338 /* If the next token is not a `[', then there are no more
8339 bounds. */
8340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8341 break;
8342 }
8343
8344 return declarator;
8345 }
8346
8347 /* Parse a new-initializer.
8348
8349 new-initializer:
8350 ( expression-list [opt] )
8351 braced-init-list
8352
8353 Returns a representation of the expression-list. */
8354
8355 static vec<tree, va_gc> *
8356 cp_parser_new_initializer (cp_parser* parser)
8357 {
8358 vec<tree, va_gc> *expression_list;
8359
8360 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8361 {
8362 tree t;
8363 bool expr_non_constant_p;
8364 cp_lexer_set_source_position (parser->lexer);
8365 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8366 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8367 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8368 expression_list = make_tree_vector_single (t);
8369 }
8370 else
8371 expression_list = (cp_parser_parenthesized_expression_list
8372 (parser, non_attr, /*cast_p=*/false,
8373 /*allow_expansion_p=*/true,
8374 /*non_constant_p=*/NULL));
8375
8376 return expression_list;
8377 }
8378
8379 /* Parse a delete-expression.
8380
8381 delete-expression:
8382 :: [opt] delete cast-expression
8383 :: [opt] delete [ ] cast-expression
8384
8385 Returns a representation of the expression. */
8386
8387 static tree
8388 cp_parser_delete_expression (cp_parser* parser)
8389 {
8390 bool global_scope_p;
8391 bool array_p;
8392 tree expression;
8393
8394 /* Look for the optional `::' operator. */
8395 global_scope_p
8396 = (cp_parser_global_scope_opt (parser,
8397 /*current_scope_valid_p=*/false)
8398 != NULL_TREE);
8399 /* Look for the `delete' keyword. */
8400 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8401 /* See if the array syntax is in use. */
8402 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8403 {
8404 /* Consume the `[' token. */
8405 cp_lexer_consume_token (parser->lexer);
8406 /* Look for the `]' token. */
8407 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8408 /* Remember that this is the `[]' construct. */
8409 array_p = true;
8410 }
8411 else
8412 array_p = false;
8413
8414 /* Parse the cast-expression. */
8415 expression = cp_parser_simple_cast_expression (parser);
8416
8417 /* A delete-expression may not appear in an integral constant
8418 expression. */
8419 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8420 return error_mark_node;
8421
8422 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8423 tf_warning_or_error);
8424 }
8425
8426 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8427 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8428 0 otherwise. */
8429
8430 static int
8431 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8432 {
8433 cp_token *token = cp_lexer_peek_token (parser->lexer);
8434 switch (token->type)
8435 {
8436 case CPP_COMMA:
8437 case CPP_SEMICOLON:
8438 case CPP_QUERY:
8439 case CPP_COLON:
8440 case CPP_CLOSE_SQUARE:
8441 case CPP_CLOSE_PAREN:
8442 case CPP_CLOSE_BRACE:
8443 case CPP_OPEN_BRACE:
8444 case CPP_DOT:
8445 case CPP_DOT_STAR:
8446 case CPP_DEREF:
8447 case CPP_DEREF_STAR:
8448 case CPP_DIV:
8449 case CPP_MOD:
8450 case CPP_LSHIFT:
8451 case CPP_RSHIFT:
8452 case CPP_LESS:
8453 case CPP_GREATER:
8454 case CPP_LESS_EQ:
8455 case CPP_GREATER_EQ:
8456 case CPP_EQ_EQ:
8457 case CPP_NOT_EQ:
8458 case CPP_EQ:
8459 case CPP_MULT_EQ:
8460 case CPP_DIV_EQ:
8461 case CPP_MOD_EQ:
8462 case CPP_PLUS_EQ:
8463 case CPP_MINUS_EQ:
8464 case CPP_RSHIFT_EQ:
8465 case CPP_LSHIFT_EQ:
8466 case CPP_AND_EQ:
8467 case CPP_XOR_EQ:
8468 case CPP_OR_EQ:
8469 case CPP_XOR:
8470 case CPP_OR:
8471 case CPP_OR_OR:
8472 case CPP_EOF:
8473 case CPP_ELLIPSIS:
8474 return 0;
8475
8476 case CPP_OPEN_PAREN:
8477 /* In ((type ()) () the last () isn't a valid cast-expression,
8478 so the whole must be parsed as postfix-expression. */
8479 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8480 != CPP_CLOSE_PAREN;
8481
8482 case CPP_OPEN_SQUARE:
8483 /* '[' may start a primary-expression in obj-c++ and in C++11,
8484 as a lambda-expression, eg, '(void)[]{}'. */
8485 if (cxx_dialect >= cxx11)
8486 return -1;
8487 return c_dialect_objc ();
8488
8489 case CPP_PLUS_PLUS:
8490 case CPP_MINUS_MINUS:
8491 /* '++' and '--' may or may not start a cast-expression:
8492
8493 struct T { void operator++(int); };
8494 void f() { (T())++; }
8495
8496 vs
8497
8498 int a;
8499 (int)++a; */
8500 return -1;
8501
8502 default:
8503 return 1;
8504 }
8505 }
8506
8507 /* Parse a cast-expression.
8508
8509 cast-expression:
8510 unary-expression
8511 ( type-id ) cast-expression
8512
8513 ADDRESS_P is true iff the unary-expression is appearing as the
8514 operand of the `&' operator. CAST_P is true if this expression is
8515 the target of a cast.
8516
8517 Returns a representation of the expression. */
8518
8519 static cp_expr
8520 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8521 bool decltype_p, cp_id_kind * pidk)
8522 {
8523 /* If it's a `(', then we might be looking at a cast. */
8524 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8525 {
8526 tree type = NULL_TREE;
8527 cp_expr expr (NULL_TREE);
8528 int cast_expression = 0;
8529 const char *saved_message;
8530
8531 /* There's no way to know yet whether or not this is a cast.
8532 For example, `(int (3))' is a unary-expression, while `(int)
8533 3' is a cast. So, we resort to parsing tentatively. */
8534 cp_parser_parse_tentatively (parser);
8535 /* Types may not be defined in a cast. */
8536 saved_message = parser->type_definition_forbidden_message;
8537 parser->type_definition_forbidden_message
8538 = G_("types may not be defined in casts");
8539 /* Consume the `('. */
8540 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8541 location_t open_paren_loc = open_paren->location;
8542
8543 /* A very tricky bit is that `(struct S) { 3 }' is a
8544 compound-literal (which we permit in C++ as an extension).
8545 But, that construct is not a cast-expression -- it is a
8546 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8547 is legal; if the compound-literal were a cast-expression,
8548 you'd need an extra set of parentheses.) But, if we parse
8549 the type-id, and it happens to be a class-specifier, then we
8550 will commit to the parse at that point, because we cannot
8551 undo the action that is done when creating a new class. So,
8552 then we cannot back up and do a postfix-expression.
8553
8554 Another tricky case is the following (c++/29234):
8555
8556 struct S { void operator () (); };
8557
8558 void foo ()
8559 {
8560 ( S()() );
8561 }
8562
8563 As a type-id we parse the parenthesized S()() as a function
8564 returning a function, groktypename complains and we cannot
8565 back up in this case either.
8566
8567 Therefore, we scan ahead to the closing `)', and check to see
8568 if the tokens after the `)' can start a cast-expression. Otherwise
8569 we are dealing with an unary-expression, a postfix-expression
8570 or something else.
8571
8572 Yet another tricky case, in C++11, is the following (c++/54891):
8573
8574 (void)[]{};
8575
8576 The issue is that usually, besides the case of lambda-expressions,
8577 the parenthesized type-id cannot be followed by '[', and, eg, we
8578 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8579 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8580 we don't commit, we try a cast-expression, then an unary-expression.
8581
8582 Save tokens so that we can put them back. */
8583 cp_lexer_save_tokens (parser->lexer);
8584
8585 /* We may be looking at a cast-expression. */
8586 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8587 /*consume_paren=*/true))
8588 cast_expression
8589 = cp_parser_tokens_start_cast_expression (parser);
8590
8591 /* Roll back the tokens we skipped. */
8592 cp_lexer_rollback_tokens (parser->lexer);
8593 /* If we aren't looking at a cast-expression, simulate an error so
8594 that the call to cp_parser_error_occurred below returns true. */
8595 if (!cast_expression)
8596 cp_parser_simulate_error (parser);
8597 else
8598 {
8599 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8600 parser->in_type_id_in_expr_p = true;
8601 /* Look for the type-id. */
8602 type = cp_parser_type_id (parser);
8603 /* Look for the closing `)'. */
8604 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8605 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8606 }
8607
8608 /* Restore the saved message. */
8609 parser->type_definition_forbidden_message = saved_message;
8610
8611 /* At this point this can only be either a cast or a
8612 parenthesized ctor such as `(T ())' that looks like a cast to
8613 function returning T. */
8614 if (!cp_parser_error_occurred (parser))
8615 {
8616 /* Only commit if the cast-expression doesn't start with
8617 '++', '--', or '[' in C++11. */
8618 if (cast_expression > 0)
8619 cp_parser_commit_to_topmost_tentative_parse (parser);
8620
8621 expr = cp_parser_cast_expression (parser,
8622 /*address_p=*/false,
8623 /*cast_p=*/true,
8624 /*decltype_p=*/false,
8625 pidk);
8626
8627 if (cp_parser_parse_definitely (parser))
8628 {
8629 /* Warn about old-style casts, if so requested. */
8630 if (warn_old_style_cast
8631 && !in_system_header_at (input_location)
8632 && !VOID_TYPE_P (type)
8633 && current_lang_name != lang_name_c)
8634 warning (OPT_Wold_style_cast, "use of old-style cast");
8635
8636 /* Only type conversions to integral or enumeration types
8637 can be used in constant-expressions. */
8638 if (!cast_valid_in_integral_constant_expression_p (type)
8639 && cp_parser_non_integral_constant_expression (parser,
8640 NIC_CAST))
8641 return error_mark_node;
8642
8643 /* Perform the cast. */
8644 /* Make a location:
8645 (TYPE) EXPR
8646 ^~~~~~~~~~~
8647 with start==caret at the open paren, extending to the
8648 end of "expr". */
8649 location_t cast_loc = make_location (open_paren_loc,
8650 open_paren_loc,
8651 expr.get_finish ());
8652 expr = build_c_cast (cast_loc, type, expr);
8653 return expr;
8654 }
8655 }
8656 else
8657 cp_parser_abort_tentative_parse (parser);
8658 }
8659
8660 /* If we get here, then it's not a cast, so it must be a
8661 unary-expression. */
8662 return cp_parser_unary_expression (parser, pidk, address_p,
8663 cast_p, decltype_p);
8664 }
8665
8666 /* Parse a binary expression of the general form:
8667
8668 pm-expression:
8669 cast-expression
8670 pm-expression .* cast-expression
8671 pm-expression ->* cast-expression
8672
8673 multiplicative-expression:
8674 pm-expression
8675 multiplicative-expression * pm-expression
8676 multiplicative-expression / pm-expression
8677 multiplicative-expression % pm-expression
8678
8679 additive-expression:
8680 multiplicative-expression
8681 additive-expression + multiplicative-expression
8682 additive-expression - multiplicative-expression
8683
8684 shift-expression:
8685 additive-expression
8686 shift-expression << additive-expression
8687 shift-expression >> additive-expression
8688
8689 relational-expression:
8690 shift-expression
8691 relational-expression < shift-expression
8692 relational-expression > shift-expression
8693 relational-expression <= shift-expression
8694 relational-expression >= shift-expression
8695
8696 GNU Extension:
8697
8698 relational-expression:
8699 relational-expression <? shift-expression
8700 relational-expression >? shift-expression
8701
8702 equality-expression:
8703 relational-expression
8704 equality-expression == relational-expression
8705 equality-expression != relational-expression
8706
8707 and-expression:
8708 equality-expression
8709 and-expression & equality-expression
8710
8711 exclusive-or-expression:
8712 and-expression
8713 exclusive-or-expression ^ and-expression
8714
8715 inclusive-or-expression:
8716 exclusive-or-expression
8717 inclusive-or-expression | exclusive-or-expression
8718
8719 logical-and-expression:
8720 inclusive-or-expression
8721 logical-and-expression && inclusive-or-expression
8722
8723 logical-or-expression:
8724 logical-and-expression
8725 logical-or-expression || logical-and-expression
8726
8727 All these are implemented with a single function like:
8728
8729 binary-expression:
8730 simple-cast-expression
8731 binary-expression <token> binary-expression
8732
8733 CAST_P is true if this expression is the target of a cast.
8734
8735 The binops_by_token map is used to get the tree codes for each <token> type.
8736 binary-expressions are associated according to a precedence table. */
8737
8738 #define TOKEN_PRECEDENCE(token) \
8739 (((token->type == CPP_GREATER \
8740 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8741 && !parser->greater_than_is_operator_p) \
8742 ? PREC_NOT_OPERATOR \
8743 : binops_by_token[token->type].prec)
8744
8745 static cp_expr
8746 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8747 bool no_toplevel_fold_p,
8748 bool decltype_p,
8749 enum cp_parser_prec prec,
8750 cp_id_kind * pidk)
8751 {
8752 cp_parser_expression_stack stack;
8753 cp_parser_expression_stack_entry *sp = &stack[0];
8754 cp_parser_expression_stack_entry current;
8755 cp_expr rhs;
8756 cp_token *token;
8757 enum tree_code rhs_type;
8758 enum cp_parser_prec new_prec, lookahead_prec;
8759 tree overload;
8760
8761 /* Parse the first expression. */
8762 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8763 ? TRUTH_NOT_EXPR : ERROR_MARK);
8764 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8765 cast_p, decltype_p, pidk);
8766 current.prec = prec;
8767
8768 if (cp_parser_error_occurred (parser))
8769 return error_mark_node;
8770
8771 for (;;)
8772 {
8773 /* Get an operator token. */
8774 token = cp_lexer_peek_token (parser->lexer);
8775
8776 if (warn_cxx11_compat
8777 && token->type == CPP_RSHIFT
8778 && !parser->greater_than_is_operator_p)
8779 {
8780 if (warning_at (token->location, OPT_Wc__11_compat,
8781 "%<>>%> operator is treated"
8782 " as two right angle brackets in C++11"))
8783 inform (token->location,
8784 "suggest parentheses around %<>>%> expression");
8785 }
8786
8787 new_prec = TOKEN_PRECEDENCE (token);
8788 if (new_prec != PREC_NOT_OPERATOR
8789 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8790 /* This is a fold-expression; handle it later. */
8791 new_prec = PREC_NOT_OPERATOR;
8792
8793 /* Popping an entry off the stack means we completed a subexpression:
8794 - either we found a token which is not an operator (`>' where it is not
8795 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8796 will happen repeatedly;
8797 - or, we found an operator which has lower priority. This is the case
8798 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8799 parsing `3 * 4'. */
8800 if (new_prec <= current.prec)
8801 {
8802 if (sp == stack)
8803 break;
8804 else
8805 goto pop;
8806 }
8807
8808 get_rhs:
8809 current.tree_type = binops_by_token[token->type].tree_type;
8810 current.loc = token->location;
8811
8812 /* We used the operator token. */
8813 cp_lexer_consume_token (parser->lexer);
8814
8815 /* For "false && x" or "true || x", x will never be executed;
8816 disable warnings while evaluating it. */
8817 if (current.tree_type == TRUTH_ANDIF_EXPR)
8818 c_inhibit_evaluation_warnings +=
8819 cp_fully_fold (current.lhs) == truthvalue_false_node;
8820 else if (current.tree_type == TRUTH_ORIF_EXPR)
8821 c_inhibit_evaluation_warnings +=
8822 cp_fully_fold (current.lhs) == truthvalue_true_node;
8823
8824 /* Extract another operand. It may be the RHS of this expression
8825 or the LHS of a new, higher priority expression. */
8826 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8827 ? TRUTH_NOT_EXPR : ERROR_MARK);
8828 rhs = cp_parser_simple_cast_expression (parser);
8829
8830 /* Get another operator token. Look up its precedence to avoid
8831 building a useless (immediately popped) stack entry for common
8832 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8833 token = cp_lexer_peek_token (parser->lexer);
8834 lookahead_prec = TOKEN_PRECEDENCE (token);
8835 if (lookahead_prec != PREC_NOT_OPERATOR
8836 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8837 lookahead_prec = PREC_NOT_OPERATOR;
8838 if (lookahead_prec > new_prec)
8839 {
8840 /* ... and prepare to parse the RHS of the new, higher priority
8841 expression. Since precedence levels on the stack are
8842 monotonically increasing, we do not have to care about
8843 stack overflows. */
8844 *sp = current;
8845 ++sp;
8846 current.lhs = rhs;
8847 current.lhs_type = rhs_type;
8848 current.prec = new_prec;
8849 new_prec = lookahead_prec;
8850 goto get_rhs;
8851
8852 pop:
8853 lookahead_prec = new_prec;
8854 /* If the stack is not empty, we have parsed into LHS the right side
8855 (`4' in the example above) of an expression we had suspended.
8856 We can use the information on the stack to recover the LHS (`3')
8857 from the stack together with the tree code (`MULT_EXPR'), and
8858 the precedence of the higher level subexpression
8859 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8860 which will be used to actually build the additive expression. */
8861 rhs = current.lhs;
8862 rhs_type = current.lhs_type;
8863 --sp;
8864 current = *sp;
8865 }
8866
8867 /* Undo the disabling of warnings done above. */
8868 if (current.tree_type == TRUTH_ANDIF_EXPR)
8869 c_inhibit_evaluation_warnings -=
8870 cp_fully_fold (current.lhs) == truthvalue_false_node;
8871 else if (current.tree_type == TRUTH_ORIF_EXPR)
8872 c_inhibit_evaluation_warnings -=
8873 cp_fully_fold (current.lhs) == truthvalue_true_node;
8874
8875 if (warn_logical_not_paren
8876 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8877 && current.lhs_type == TRUTH_NOT_EXPR
8878 /* Avoid warning for !!x == y. */
8879 && (TREE_CODE (current.lhs) != NE_EXPR
8880 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8881 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8882 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8883 /* Avoid warning for !b == y where b is boolean. */
8884 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8885 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8886 != BOOLEAN_TYPE))))
8887 /* Avoid warning for !!b == y where b is boolean. */
8888 && (!DECL_P (current.lhs)
8889 || TREE_TYPE (current.lhs) == NULL_TREE
8890 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8891 warn_logical_not_parentheses (current.loc, current.tree_type,
8892 maybe_constant_value (rhs));
8893
8894 overload = NULL;
8895
8896 location_t combined_loc = make_location (current.loc,
8897 current.lhs.get_start (),
8898 rhs.get_finish ());
8899
8900 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8901 ERROR_MARK for everything that is not a binary expression.
8902 This makes warn_about_parentheses miss some warnings that
8903 involve unary operators. For unary expressions we should
8904 pass the correct tree_code unless the unary expression was
8905 surrounded by parentheses.
8906 */
8907 if (no_toplevel_fold_p
8908 && lookahead_prec <= current.prec
8909 && sp == stack)
8910 current.lhs = build2_loc (combined_loc,
8911 current.tree_type,
8912 TREE_CODE_CLASS (current.tree_type)
8913 == tcc_comparison
8914 ? boolean_type_node : TREE_TYPE (current.lhs),
8915 current.lhs, rhs);
8916 else
8917 {
8918 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8919 current.lhs, current.lhs_type,
8920 rhs, rhs_type, &overload,
8921 complain_flags (decltype_p));
8922 /* TODO: build_x_binary_op doesn't always honor the location. */
8923 current.lhs.set_location (combined_loc);
8924 }
8925 current.lhs_type = current.tree_type;
8926
8927 /* If the binary operator required the use of an overloaded operator,
8928 then this expression cannot be an integral constant-expression.
8929 An overloaded operator can be used even if both operands are
8930 otherwise permissible in an integral constant-expression if at
8931 least one of the operands is of enumeration type. */
8932
8933 if (overload
8934 && cp_parser_non_integral_constant_expression (parser,
8935 NIC_OVERLOADED))
8936 return error_mark_node;
8937 }
8938
8939 return current.lhs;
8940 }
8941
8942 static cp_expr
8943 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8944 bool no_toplevel_fold_p,
8945 enum cp_parser_prec prec,
8946 cp_id_kind * pidk)
8947 {
8948 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8949 /*decltype*/false, prec, pidk);
8950 }
8951
8952 /* Parse the `? expression : assignment-expression' part of a
8953 conditional-expression. The LOGICAL_OR_EXPR is the
8954 logical-or-expression that started the conditional-expression.
8955 Returns a representation of the entire conditional-expression.
8956
8957 This routine is used by cp_parser_assignment_expression.
8958
8959 ? expression : assignment-expression
8960
8961 GNU Extensions:
8962
8963 ? : assignment-expression */
8964
8965 static tree
8966 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8967 {
8968 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8969 cp_expr assignment_expr;
8970 struct cp_token *token;
8971 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8972
8973 /* Consume the `?' token. */
8974 cp_lexer_consume_token (parser->lexer);
8975 token = cp_lexer_peek_token (parser->lexer);
8976 if (cp_parser_allow_gnu_extensions_p (parser)
8977 && token->type == CPP_COLON)
8978 {
8979 pedwarn (token->location, OPT_Wpedantic,
8980 "ISO C++ does not allow ?: with omitted middle operand");
8981 /* Implicit true clause. */
8982 expr = NULL_TREE;
8983 c_inhibit_evaluation_warnings +=
8984 folded_logical_or_expr == truthvalue_true_node;
8985 warn_for_omitted_condop (token->location, logical_or_expr);
8986 }
8987 else
8988 {
8989 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8990 parser->colon_corrects_to_scope_p = false;
8991 /* Parse the expression. */
8992 c_inhibit_evaluation_warnings +=
8993 folded_logical_or_expr == truthvalue_false_node;
8994 expr = cp_parser_expression (parser);
8995 c_inhibit_evaluation_warnings +=
8996 ((folded_logical_or_expr == truthvalue_true_node)
8997 - (folded_logical_or_expr == truthvalue_false_node));
8998 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8999 }
9000
9001 /* The next token should be a `:'. */
9002 cp_parser_require (parser, CPP_COLON, RT_COLON);
9003 /* Parse the assignment-expression. */
9004 assignment_expr = cp_parser_assignment_expression (parser);
9005 c_inhibit_evaluation_warnings -=
9006 folded_logical_or_expr == truthvalue_true_node;
9007
9008 /* Make a location:
9009 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9010 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9011 with the caret at the "?", ranging from the start of
9012 the logical_or_expr to the end of the assignment_expr. */
9013 loc = make_location (loc,
9014 logical_or_expr.get_start (),
9015 assignment_expr.get_finish ());
9016
9017 /* Build the conditional-expression. */
9018 return build_x_conditional_expr (loc, logical_or_expr,
9019 expr,
9020 assignment_expr,
9021 tf_warning_or_error);
9022 }
9023
9024 /* Parse an assignment-expression.
9025
9026 assignment-expression:
9027 conditional-expression
9028 logical-or-expression assignment-operator assignment_expression
9029 throw-expression
9030
9031 CAST_P is true if this expression is the target of a cast.
9032 DECLTYPE_P is true if this expression is the operand of decltype.
9033
9034 Returns a representation for the expression. */
9035
9036 static cp_expr
9037 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9038 bool cast_p, bool decltype_p)
9039 {
9040 cp_expr expr;
9041
9042 /* If the next token is the `throw' keyword, then we're looking at
9043 a throw-expression. */
9044 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9045 expr = cp_parser_throw_expression (parser);
9046 /* Otherwise, it must be that we are looking at a
9047 logical-or-expression. */
9048 else
9049 {
9050 /* Parse the binary expressions (logical-or-expression). */
9051 expr = cp_parser_binary_expression (parser, cast_p, false,
9052 decltype_p,
9053 PREC_NOT_OPERATOR, pidk);
9054 /* If the next token is a `?' then we're actually looking at a
9055 conditional-expression. */
9056 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9057 return cp_parser_question_colon_clause (parser, expr);
9058 else
9059 {
9060 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9061
9062 /* If it's an assignment-operator, we're using the second
9063 production. */
9064 enum tree_code assignment_operator
9065 = cp_parser_assignment_operator_opt (parser);
9066 if (assignment_operator != ERROR_MARK)
9067 {
9068 bool non_constant_p;
9069
9070 /* Parse the right-hand side of the assignment. */
9071 cp_expr rhs = cp_parser_initializer_clause (parser,
9072 &non_constant_p);
9073
9074 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9075 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9076
9077 /* An assignment may not appear in a
9078 constant-expression. */
9079 if (cp_parser_non_integral_constant_expression (parser,
9080 NIC_ASSIGNMENT))
9081 return error_mark_node;
9082 /* Build the assignment expression. Its default
9083 location:
9084 LHS = RHS
9085 ~~~~^~~~~
9086 is the location of the '=' token as the
9087 caret, ranging from the start of the lhs to the
9088 end of the rhs. */
9089 loc = make_location (loc,
9090 expr.get_start (),
9091 rhs.get_finish ());
9092 expr = build_x_modify_expr (loc, expr,
9093 assignment_operator,
9094 rhs,
9095 complain_flags (decltype_p));
9096 /* TODO: build_x_modify_expr doesn't honor the location,
9097 so we must set it here. */
9098 expr.set_location (loc);
9099 }
9100 }
9101 }
9102
9103 return expr;
9104 }
9105
9106 /* Parse an (optional) assignment-operator.
9107
9108 assignment-operator: one of
9109 = *= /= %= += -= >>= <<= &= ^= |=
9110
9111 GNU Extension:
9112
9113 assignment-operator: one of
9114 <?= >?=
9115
9116 If the next token is an assignment operator, the corresponding tree
9117 code is returned, and the token is consumed. For example, for
9118 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9119 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9120 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9121 operator, ERROR_MARK is returned. */
9122
9123 static enum tree_code
9124 cp_parser_assignment_operator_opt (cp_parser* parser)
9125 {
9126 enum tree_code op;
9127 cp_token *token;
9128
9129 /* Peek at the next token. */
9130 token = cp_lexer_peek_token (parser->lexer);
9131
9132 switch (token->type)
9133 {
9134 case CPP_EQ:
9135 op = NOP_EXPR;
9136 break;
9137
9138 case CPP_MULT_EQ:
9139 op = MULT_EXPR;
9140 break;
9141
9142 case CPP_DIV_EQ:
9143 op = TRUNC_DIV_EXPR;
9144 break;
9145
9146 case CPP_MOD_EQ:
9147 op = TRUNC_MOD_EXPR;
9148 break;
9149
9150 case CPP_PLUS_EQ:
9151 op = PLUS_EXPR;
9152 break;
9153
9154 case CPP_MINUS_EQ:
9155 op = MINUS_EXPR;
9156 break;
9157
9158 case CPP_RSHIFT_EQ:
9159 op = RSHIFT_EXPR;
9160 break;
9161
9162 case CPP_LSHIFT_EQ:
9163 op = LSHIFT_EXPR;
9164 break;
9165
9166 case CPP_AND_EQ:
9167 op = BIT_AND_EXPR;
9168 break;
9169
9170 case CPP_XOR_EQ:
9171 op = BIT_XOR_EXPR;
9172 break;
9173
9174 case CPP_OR_EQ:
9175 op = BIT_IOR_EXPR;
9176 break;
9177
9178 default:
9179 /* Nothing else is an assignment operator. */
9180 op = ERROR_MARK;
9181 }
9182
9183 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9184 if (op != ERROR_MARK
9185 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9186 op = ERROR_MARK;
9187
9188 /* If it was an assignment operator, consume it. */
9189 if (op != ERROR_MARK)
9190 cp_lexer_consume_token (parser->lexer);
9191
9192 return op;
9193 }
9194
9195 /* Parse an expression.
9196
9197 expression:
9198 assignment-expression
9199 expression , assignment-expression
9200
9201 CAST_P is true if this expression is the target of a cast.
9202 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9203 except possibly parenthesized or on the RHS of a comma (N3276).
9204
9205 Returns a representation of the expression. */
9206
9207 static cp_expr
9208 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9209 bool cast_p, bool decltype_p)
9210 {
9211 cp_expr expression = NULL_TREE;
9212 location_t loc = UNKNOWN_LOCATION;
9213
9214 while (true)
9215 {
9216 cp_expr assignment_expression;
9217
9218 /* Parse the next assignment-expression. */
9219 assignment_expression
9220 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9221
9222 /* We don't create a temporary for a call that is the immediate operand
9223 of decltype or on the RHS of a comma. But when we see a comma, we
9224 need to create a temporary for a call on the LHS. */
9225 if (decltype_p && !processing_template_decl
9226 && TREE_CODE (assignment_expression) == CALL_EXPR
9227 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9228 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9229 assignment_expression
9230 = build_cplus_new (TREE_TYPE (assignment_expression),
9231 assignment_expression, tf_warning_or_error);
9232
9233 /* If this is the first assignment-expression, we can just
9234 save it away. */
9235 if (!expression)
9236 expression = assignment_expression;
9237 else
9238 {
9239 /* Create a location with caret at the comma, ranging
9240 from the start of the LHS to the end of the RHS. */
9241 loc = make_location (loc,
9242 expression.get_start (),
9243 assignment_expression.get_finish ());
9244 expression = build_x_compound_expr (loc, expression,
9245 assignment_expression,
9246 complain_flags (decltype_p));
9247 expression.set_location (loc);
9248 }
9249 /* If the next token is not a comma, or we're in a fold-expression, then
9250 we are done with the expression. */
9251 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9252 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9253 break;
9254 /* Consume the `,'. */
9255 loc = cp_lexer_peek_token (parser->lexer)->location;
9256 cp_lexer_consume_token (parser->lexer);
9257 /* A comma operator cannot appear in a constant-expression. */
9258 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9259 expression = error_mark_node;
9260 }
9261
9262 return expression;
9263 }
9264
9265 /* Parse a constant-expression.
9266
9267 constant-expression:
9268 conditional-expression
9269
9270 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9271 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9272 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9273 is false, NON_CONSTANT_P should be NULL. */
9274
9275 static cp_expr
9276 cp_parser_constant_expression (cp_parser* parser,
9277 bool allow_non_constant_p,
9278 bool *non_constant_p)
9279 {
9280 bool saved_integral_constant_expression_p;
9281 bool saved_allow_non_integral_constant_expression_p;
9282 bool saved_non_integral_constant_expression_p;
9283 cp_expr expression;
9284
9285 /* It might seem that we could simply parse the
9286 conditional-expression, and then check to see if it were
9287 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9288 one that the compiler can figure out is constant, possibly after
9289 doing some simplifications or optimizations. The standard has a
9290 precise definition of constant-expression, and we must honor
9291 that, even though it is somewhat more restrictive.
9292
9293 For example:
9294
9295 int i[(2, 3)];
9296
9297 is not a legal declaration, because `(2, 3)' is not a
9298 constant-expression. The `,' operator is forbidden in a
9299 constant-expression. However, GCC's constant-folding machinery
9300 will fold this operation to an INTEGER_CST for `3'. */
9301
9302 /* Save the old settings. */
9303 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9304 saved_allow_non_integral_constant_expression_p
9305 = parser->allow_non_integral_constant_expression_p;
9306 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9307 /* We are now parsing a constant-expression. */
9308 parser->integral_constant_expression_p = true;
9309 parser->allow_non_integral_constant_expression_p
9310 = (allow_non_constant_p || cxx_dialect >= cxx11);
9311 parser->non_integral_constant_expression_p = false;
9312 /* Although the grammar says "conditional-expression", we parse an
9313 "assignment-expression", which also permits "throw-expression"
9314 and the use of assignment operators. In the case that
9315 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9316 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9317 actually essential that we look for an assignment-expression.
9318 For example, cp_parser_initializer_clauses uses this function to
9319 determine whether a particular assignment-expression is in fact
9320 constant. */
9321 expression = cp_parser_assignment_expression (parser);
9322 /* Restore the old settings. */
9323 parser->integral_constant_expression_p
9324 = saved_integral_constant_expression_p;
9325 parser->allow_non_integral_constant_expression_p
9326 = saved_allow_non_integral_constant_expression_p;
9327 if (cxx_dialect >= cxx11)
9328 {
9329 /* Require an rvalue constant expression here; that's what our
9330 callers expect. Reference constant expressions are handled
9331 separately in e.g. cp_parser_template_argument. */
9332 bool is_const = potential_rvalue_constant_expression (expression);
9333 parser->non_integral_constant_expression_p = !is_const;
9334 if (!is_const && !allow_non_constant_p)
9335 require_potential_rvalue_constant_expression (expression);
9336 }
9337 if (allow_non_constant_p)
9338 *non_constant_p = parser->non_integral_constant_expression_p;
9339 parser->non_integral_constant_expression_p
9340 = saved_non_integral_constant_expression_p;
9341
9342 return expression;
9343 }
9344
9345 /* Parse __builtin_offsetof.
9346
9347 offsetof-expression:
9348 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9349
9350 offsetof-member-designator:
9351 id-expression
9352 | offsetof-member-designator "." id-expression
9353 | offsetof-member-designator "[" expression "]"
9354 | offsetof-member-designator "->" id-expression */
9355
9356 static cp_expr
9357 cp_parser_builtin_offsetof (cp_parser *parser)
9358 {
9359 int save_ice_p, save_non_ice_p;
9360 tree type;
9361 cp_expr expr;
9362 cp_id_kind dummy;
9363 cp_token *token;
9364 location_t finish_loc;
9365
9366 /* We're about to accept non-integral-constant things, but will
9367 definitely yield an integral constant expression. Save and
9368 restore these values around our local parsing. */
9369 save_ice_p = parser->integral_constant_expression_p;
9370 save_non_ice_p = parser->non_integral_constant_expression_p;
9371
9372 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9373
9374 /* Consume the "__builtin_offsetof" token. */
9375 cp_lexer_consume_token (parser->lexer);
9376 /* Consume the opening `('. */
9377 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9378 /* Parse the type-id. */
9379 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9380 type = cp_parser_type_id (parser);
9381 /* Look for the `,'. */
9382 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9383 token = cp_lexer_peek_token (parser->lexer);
9384
9385 /* Build the (type *)null that begins the traditional offsetof macro. */
9386 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9387 tf_warning_or_error);
9388
9389 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9390 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9391 true, &dummy, token->location);
9392 while (true)
9393 {
9394 token = cp_lexer_peek_token (parser->lexer);
9395 switch (token->type)
9396 {
9397 case CPP_OPEN_SQUARE:
9398 /* offsetof-member-designator "[" expression "]" */
9399 expr = cp_parser_postfix_open_square_expression (parser, expr,
9400 true, false);
9401 break;
9402
9403 case CPP_DEREF:
9404 /* offsetof-member-designator "->" identifier */
9405 expr = grok_array_decl (token->location, expr,
9406 integer_zero_node, false);
9407 /* FALLTHRU */
9408
9409 case CPP_DOT:
9410 /* offsetof-member-designator "." identifier */
9411 cp_lexer_consume_token (parser->lexer);
9412 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9413 expr, true, &dummy,
9414 token->location);
9415 break;
9416
9417 case CPP_CLOSE_PAREN:
9418 /* Consume the ")" token. */
9419 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9420 cp_lexer_consume_token (parser->lexer);
9421 goto success;
9422
9423 default:
9424 /* Error. We know the following require will fail, but
9425 that gives the proper error message. */
9426 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9427 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9428 expr = error_mark_node;
9429 goto failure;
9430 }
9431 }
9432
9433 success:
9434 /* Make a location of the form:
9435 __builtin_offsetof (struct s, f)
9436 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9437 with caret at the type-id, ranging from the start of the
9438 "_builtin_offsetof" token to the close paren. */
9439 loc = make_location (loc, start_loc, finish_loc);
9440 /* The result will be an INTEGER_CST, so we need to explicitly
9441 preserve the location. */
9442 expr = cp_expr (finish_offsetof (expr, loc), loc);
9443
9444 failure:
9445 parser->integral_constant_expression_p = save_ice_p;
9446 parser->non_integral_constant_expression_p = save_non_ice_p;
9447
9448 return expr;
9449 }
9450
9451 /* Parse a trait expression.
9452
9453 Returns a representation of the expression, the underlying type
9454 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9455
9456 static tree
9457 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9458 {
9459 cp_trait_kind kind;
9460 tree type1, type2 = NULL_TREE;
9461 bool binary = false;
9462 bool variadic = false;
9463
9464 switch (keyword)
9465 {
9466 case RID_HAS_NOTHROW_ASSIGN:
9467 kind = CPTK_HAS_NOTHROW_ASSIGN;
9468 break;
9469 case RID_HAS_NOTHROW_CONSTRUCTOR:
9470 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9471 break;
9472 case RID_HAS_NOTHROW_COPY:
9473 kind = CPTK_HAS_NOTHROW_COPY;
9474 break;
9475 case RID_HAS_TRIVIAL_ASSIGN:
9476 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9477 break;
9478 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9479 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9480 break;
9481 case RID_HAS_TRIVIAL_COPY:
9482 kind = CPTK_HAS_TRIVIAL_COPY;
9483 break;
9484 case RID_HAS_TRIVIAL_DESTRUCTOR:
9485 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9486 break;
9487 case RID_HAS_VIRTUAL_DESTRUCTOR:
9488 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9489 break;
9490 case RID_IS_ABSTRACT:
9491 kind = CPTK_IS_ABSTRACT;
9492 break;
9493 case RID_IS_BASE_OF:
9494 kind = CPTK_IS_BASE_OF;
9495 binary = true;
9496 break;
9497 case RID_IS_CLASS:
9498 kind = CPTK_IS_CLASS;
9499 break;
9500 case RID_IS_EMPTY:
9501 kind = CPTK_IS_EMPTY;
9502 break;
9503 case RID_IS_ENUM:
9504 kind = CPTK_IS_ENUM;
9505 break;
9506 case RID_IS_FINAL:
9507 kind = CPTK_IS_FINAL;
9508 break;
9509 case RID_IS_LITERAL_TYPE:
9510 kind = CPTK_IS_LITERAL_TYPE;
9511 break;
9512 case RID_IS_POD:
9513 kind = CPTK_IS_POD;
9514 break;
9515 case RID_IS_POLYMORPHIC:
9516 kind = CPTK_IS_POLYMORPHIC;
9517 break;
9518 case RID_IS_SAME_AS:
9519 kind = CPTK_IS_SAME_AS;
9520 binary = true;
9521 break;
9522 case RID_IS_STD_LAYOUT:
9523 kind = CPTK_IS_STD_LAYOUT;
9524 break;
9525 case RID_IS_TRIVIAL:
9526 kind = CPTK_IS_TRIVIAL;
9527 break;
9528 case RID_IS_TRIVIALLY_ASSIGNABLE:
9529 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9530 binary = true;
9531 break;
9532 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9533 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9534 variadic = true;
9535 break;
9536 case RID_IS_TRIVIALLY_COPYABLE:
9537 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9538 break;
9539 case RID_IS_UNION:
9540 kind = CPTK_IS_UNION;
9541 break;
9542 case RID_UNDERLYING_TYPE:
9543 kind = CPTK_UNDERLYING_TYPE;
9544 break;
9545 case RID_BASES:
9546 kind = CPTK_BASES;
9547 break;
9548 case RID_DIRECT_BASES:
9549 kind = CPTK_DIRECT_BASES;
9550 break;
9551 default:
9552 gcc_unreachable ();
9553 }
9554
9555 /* Consume the token. */
9556 cp_lexer_consume_token (parser->lexer);
9557
9558 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9559
9560 {
9561 type_id_in_expr_sentinel s (parser);
9562 type1 = cp_parser_type_id (parser);
9563 }
9564
9565 if (type1 == error_mark_node)
9566 return error_mark_node;
9567
9568 if (binary)
9569 {
9570 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9571
9572 {
9573 type_id_in_expr_sentinel s (parser);
9574 type2 = cp_parser_type_id (parser);
9575 }
9576
9577 if (type2 == error_mark_node)
9578 return error_mark_node;
9579 }
9580 else if (variadic)
9581 {
9582 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9583 {
9584 cp_lexer_consume_token (parser->lexer);
9585 tree elt = cp_parser_type_id (parser);
9586 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9587 {
9588 cp_lexer_consume_token (parser->lexer);
9589 elt = make_pack_expansion (elt);
9590 }
9591 if (elt == error_mark_node)
9592 return error_mark_node;
9593 type2 = tree_cons (NULL_TREE, elt, type2);
9594 }
9595 }
9596
9597 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9598
9599 /* Complete the trait expression, which may mean either processing
9600 the trait expr now or saving it for template instantiation. */
9601 switch(kind)
9602 {
9603 case CPTK_UNDERLYING_TYPE:
9604 return finish_underlying_type (type1);
9605 case CPTK_BASES:
9606 return finish_bases (type1, false);
9607 case CPTK_DIRECT_BASES:
9608 return finish_bases (type1, true);
9609 default:
9610 return finish_trait_expr (kind, type1, type2);
9611 }
9612 }
9613
9614 /* Lambdas that appear in variable initializer or default argument scope
9615 get that in their mangling, so we need to record it. We might as well
9616 use the count for function and namespace scopes as well. */
9617 static GTY(()) tree lambda_scope;
9618 static GTY(()) int lambda_count;
9619 struct GTY(()) tree_int
9620 {
9621 tree t;
9622 int i;
9623 };
9624 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9625
9626 static void
9627 start_lambda_scope (tree decl)
9628 {
9629 tree_int ti;
9630 gcc_assert (decl);
9631 /* Once we're inside a function, we ignore other scopes and just push
9632 the function again so that popping works properly. */
9633 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9634 decl = current_function_decl;
9635 ti.t = lambda_scope;
9636 ti.i = lambda_count;
9637 vec_safe_push (lambda_scope_stack, ti);
9638 if (lambda_scope != decl)
9639 {
9640 /* Don't reset the count if we're still in the same function. */
9641 lambda_scope = decl;
9642 lambda_count = 0;
9643 }
9644 }
9645
9646 static void
9647 record_lambda_scope (tree lambda)
9648 {
9649 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9650 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9651 }
9652
9653 static void
9654 finish_lambda_scope (void)
9655 {
9656 tree_int *p = &lambda_scope_stack->last ();
9657 if (lambda_scope != p->t)
9658 {
9659 lambda_scope = p->t;
9660 lambda_count = p->i;
9661 }
9662 lambda_scope_stack->pop ();
9663 }
9664
9665 /* Parse a lambda expression.
9666
9667 lambda-expression:
9668 lambda-introducer lambda-declarator [opt] compound-statement
9669
9670 Returns a representation of the expression. */
9671
9672 static cp_expr
9673 cp_parser_lambda_expression (cp_parser* parser)
9674 {
9675 tree lambda_expr = build_lambda_expr ();
9676 tree type;
9677 bool ok = true;
9678 cp_token *token = cp_lexer_peek_token (parser->lexer);
9679 cp_token_position start = 0;
9680
9681 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9682
9683 if (cp_unevaluated_operand)
9684 {
9685 if (!token->error_reported)
9686 {
9687 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9688 "lambda-expression in unevaluated context");
9689 token->error_reported = true;
9690 }
9691 ok = false;
9692 }
9693 else if (parser->in_template_argument_list_p)
9694 {
9695 if (!token->error_reported)
9696 {
9697 error_at (token->location, "lambda-expression in template-argument");
9698 token->error_reported = true;
9699 }
9700 ok = false;
9701 }
9702
9703 /* We may be in the middle of deferred access check. Disable
9704 it now. */
9705 push_deferring_access_checks (dk_no_deferred);
9706
9707 cp_parser_lambda_introducer (parser, lambda_expr);
9708
9709 type = begin_lambda_type (lambda_expr);
9710 if (type == error_mark_node)
9711 return error_mark_node;
9712
9713 record_lambda_scope (lambda_expr);
9714
9715 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9716 determine_visibility (TYPE_NAME (type));
9717
9718 /* Now that we've started the type, add the capture fields for any
9719 explicit captures. */
9720 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9721
9722 {
9723 /* Inside the class, surrounding template-parameter-lists do not apply. */
9724 unsigned int saved_num_template_parameter_lists
9725 = parser->num_template_parameter_lists;
9726 unsigned char in_statement = parser->in_statement;
9727 bool in_switch_statement_p = parser->in_switch_statement_p;
9728 bool fully_implicit_function_template_p
9729 = parser->fully_implicit_function_template_p;
9730 tree implicit_template_parms = parser->implicit_template_parms;
9731 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9732 bool auto_is_implicit_function_template_parm_p
9733 = parser->auto_is_implicit_function_template_parm_p;
9734
9735 parser->num_template_parameter_lists = 0;
9736 parser->in_statement = 0;
9737 parser->in_switch_statement_p = false;
9738 parser->fully_implicit_function_template_p = false;
9739 parser->implicit_template_parms = 0;
9740 parser->implicit_template_scope = 0;
9741 parser->auto_is_implicit_function_template_parm_p = false;
9742
9743 /* By virtue of defining a local class, a lambda expression has access to
9744 the private variables of enclosing classes. */
9745
9746 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9747
9748 if (ok)
9749 {
9750 if (!cp_parser_error_occurred (parser)
9751 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9752 && cp_parser_start_tentative_firewall (parser))
9753 start = token;
9754 cp_parser_lambda_body (parser, lambda_expr);
9755 }
9756 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9757 {
9758 if (cp_parser_skip_to_closing_brace (parser))
9759 cp_lexer_consume_token (parser->lexer);
9760 }
9761
9762 /* The capture list was built up in reverse order; fix that now. */
9763 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9764 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9765
9766 if (ok)
9767 maybe_add_lambda_conv_op (type);
9768
9769 type = finish_struct (type, /*attributes=*/NULL_TREE);
9770
9771 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9772 parser->in_statement = in_statement;
9773 parser->in_switch_statement_p = in_switch_statement_p;
9774 parser->fully_implicit_function_template_p
9775 = fully_implicit_function_template_p;
9776 parser->implicit_template_parms = implicit_template_parms;
9777 parser->implicit_template_scope = implicit_template_scope;
9778 parser->auto_is_implicit_function_template_parm_p
9779 = auto_is_implicit_function_template_parm_p;
9780 }
9781
9782 /* This field is only used during parsing of the lambda. */
9783 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9784
9785 /* This lambda shouldn't have any proxies left at this point. */
9786 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9787 /* And now that we're done, push proxies for an enclosing lambda. */
9788 insert_pending_capture_proxies ();
9789
9790 if (ok)
9791 lambda_expr = build_lambda_object (lambda_expr);
9792 else
9793 lambda_expr = error_mark_node;
9794
9795 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9796
9797 pop_deferring_access_checks ();
9798
9799 return lambda_expr;
9800 }
9801
9802 /* Parse the beginning of a lambda expression.
9803
9804 lambda-introducer:
9805 [ lambda-capture [opt] ]
9806
9807 LAMBDA_EXPR is the current representation of the lambda expression. */
9808
9809 static void
9810 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9811 {
9812 /* Need commas after the first capture. */
9813 bool first = true;
9814
9815 /* Eat the leading `['. */
9816 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9817
9818 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9819 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9820 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9821 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9822 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9823 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9824
9825 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9826 {
9827 cp_lexer_consume_token (parser->lexer);
9828 first = false;
9829 }
9830
9831 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9832 {
9833 cp_token* capture_token;
9834 tree capture_id;
9835 tree capture_init_expr;
9836 cp_id_kind idk = CP_ID_KIND_NONE;
9837 bool explicit_init_p = false;
9838
9839 enum capture_kind_type
9840 {
9841 BY_COPY,
9842 BY_REFERENCE
9843 };
9844 enum capture_kind_type capture_kind = BY_COPY;
9845
9846 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9847 {
9848 error ("expected end of capture-list");
9849 return;
9850 }
9851
9852 if (first)
9853 first = false;
9854 else
9855 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9856
9857 /* Possibly capture `this'. */
9858 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9859 {
9860 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9861 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9862 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9863 "with by-copy capture default");
9864 cp_lexer_consume_token (parser->lexer);
9865 add_capture (lambda_expr,
9866 /*id=*/this_identifier,
9867 /*initializer=*/finish_this_expr(),
9868 /*by_reference_p=*/false,
9869 explicit_init_p);
9870 continue;
9871 }
9872
9873 /* Remember whether we want to capture as a reference or not. */
9874 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9875 {
9876 capture_kind = BY_REFERENCE;
9877 cp_lexer_consume_token (parser->lexer);
9878 }
9879
9880 /* Get the identifier. */
9881 capture_token = cp_lexer_peek_token (parser->lexer);
9882 capture_id = cp_parser_identifier (parser);
9883
9884 if (capture_id == error_mark_node)
9885 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9886 delimiters, but I modified this to stop on unnested ']' as well. It
9887 was already changed to stop on unnested '}', so the
9888 "closing_parenthesis" name is no more misleading with my change. */
9889 {
9890 cp_parser_skip_to_closing_parenthesis (parser,
9891 /*recovering=*/true,
9892 /*or_comma=*/true,
9893 /*consume_paren=*/true);
9894 break;
9895 }
9896
9897 /* Find the initializer for this capture. */
9898 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9899 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9900 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9901 {
9902 bool direct, non_constant;
9903 /* An explicit initializer exists. */
9904 if (cxx_dialect < cxx14)
9905 pedwarn (input_location, 0,
9906 "lambda capture initializers "
9907 "only available with -std=c++14 or -std=gnu++14");
9908 capture_init_expr = cp_parser_initializer (parser, &direct,
9909 &non_constant);
9910 explicit_init_p = true;
9911 if (capture_init_expr == NULL_TREE)
9912 {
9913 error ("empty initializer for lambda init-capture");
9914 capture_init_expr = error_mark_node;
9915 }
9916 }
9917 else
9918 {
9919 const char* error_msg;
9920
9921 /* Turn the identifier into an id-expression. */
9922 capture_init_expr
9923 = cp_parser_lookup_name_simple (parser, capture_id,
9924 capture_token->location);
9925
9926 if (capture_init_expr == error_mark_node)
9927 {
9928 unqualified_name_lookup_error (capture_id);
9929 continue;
9930 }
9931 else if (DECL_P (capture_init_expr)
9932 && (!VAR_P (capture_init_expr)
9933 && TREE_CODE (capture_init_expr) != PARM_DECL))
9934 {
9935 error_at (capture_token->location,
9936 "capture of non-variable %qD ",
9937 capture_init_expr);
9938 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9939 "%q#D declared here", capture_init_expr);
9940 continue;
9941 }
9942 if (VAR_P (capture_init_expr)
9943 && decl_storage_duration (capture_init_expr) != dk_auto)
9944 {
9945 if (pedwarn (capture_token->location, 0, "capture of variable "
9946 "%qD with non-automatic storage duration",
9947 capture_init_expr))
9948 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9949 "%q#D declared here", capture_init_expr);
9950 continue;
9951 }
9952
9953 capture_init_expr
9954 = finish_id_expression
9955 (capture_id,
9956 capture_init_expr,
9957 parser->scope,
9958 &idk,
9959 /*integral_constant_expression_p=*/false,
9960 /*allow_non_integral_constant_expression_p=*/false,
9961 /*non_integral_constant_expression_p=*/NULL,
9962 /*template_p=*/false,
9963 /*done=*/true,
9964 /*address_p=*/false,
9965 /*template_arg_p=*/false,
9966 &error_msg,
9967 capture_token->location);
9968
9969 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9970 {
9971 cp_lexer_consume_token (parser->lexer);
9972 capture_init_expr = make_pack_expansion (capture_init_expr);
9973 }
9974 else
9975 check_for_bare_parameter_packs (capture_init_expr);
9976 }
9977
9978 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9979 && !explicit_init_p)
9980 {
9981 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9982 && capture_kind == BY_COPY)
9983 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9984 "of %qD redundant with by-copy capture default",
9985 capture_id);
9986 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9987 && capture_kind == BY_REFERENCE)
9988 pedwarn (capture_token->location, 0, "explicit by-reference "
9989 "capture of %qD redundant with by-reference capture "
9990 "default", capture_id);
9991 }
9992
9993 add_capture (lambda_expr,
9994 capture_id,
9995 capture_init_expr,
9996 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9997 explicit_init_p);
9998 }
9999
10000 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10001 }
10002
10003 /* Parse the (optional) middle of a lambda expression.
10004
10005 lambda-declarator:
10006 < template-parameter-list [opt] >
10007 ( parameter-declaration-clause [opt] )
10008 attribute-specifier [opt]
10009 mutable [opt]
10010 exception-specification [opt]
10011 lambda-return-type-clause [opt]
10012
10013 LAMBDA_EXPR is the current representation of the lambda expression. */
10014
10015 static bool
10016 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10017 {
10018 /* 5.1.1.4 of the standard says:
10019 If a lambda-expression does not include a lambda-declarator, it is as if
10020 the lambda-declarator were ().
10021 This means an empty parameter list, no attributes, and no exception
10022 specification. */
10023 tree param_list = void_list_node;
10024 tree attributes = NULL_TREE;
10025 tree exception_spec = NULL_TREE;
10026 tree template_param_list = NULL_TREE;
10027 tree tx_qual = NULL_TREE;
10028
10029 /* The template-parameter-list is optional, but must begin with
10030 an opening angle if present. */
10031 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10032 {
10033 if (cxx_dialect < cxx14)
10034 pedwarn (parser->lexer->next_token->location, 0,
10035 "lambda templates are only available with "
10036 "-std=c++14 or -std=gnu++14");
10037
10038 cp_lexer_consume_token (parser->lexer);
10039
10040 template_param_list = cp_parser_template_parameter_list (parser);
10041
10042 cp_parser_skip_to_end_of_template_parameter_list (parser);
10043
10044 /* We just processed one more parameter list. */
10045 ++parser->num_template_parameter_lists;
10046 }
10047
10048 /* The parameter-declaration-clause is optional (unless
10049 template-parameter-list was given), but must begin with an
10050 opening parenthesis if present. */
10051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10052 {
10053 cp_lexer_consume_token (parser->lexer);
10054
10055 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10056
10057 /* Parse parameters. */
10058 param_list = cp_parser_parameter_declaration_clause (parser);
10059
10060 /* Default arguments shall not be specified in the
10061 parameter-declaration-clause of a lambda-declarator. */
10062 for (tree t = param_list; t; t = TREE_CHAIN (t))
10063 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10064 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10065 "default argument specified for lambda parameter");
10066
10067 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10068
10069 attributes = cp_parser_attributes_opt (parser);
10070
10071 /* Parse optional `mutable' keyword. */
10072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10073 {
10074 cp_lexer_consume_token (parser->lexer);
10075 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10076 }
10077
10078 tx_qual = cp_parser_tx_qualifier_opt (parser);
10079
10080 /* Parse optional exception specification. */
10081 exception_spec = cp_parser_exception_specification_opt (parser);
10082
10083 /* Parse optional trailing return type. */
10084 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10085 {
10086 cp_lexer_consume_token (parser->lexer);
10087 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10088 = cp_parser_trailing_type_id (parser);
10089 }
10090
10091 /* The function parameters must be in scope all the way until after the
10092 trailing-return-type in case of decltype. */
10093 pop_bindings_and_leave_scope ();
10094 }
10095 else if (template_param_list != NULL_TREE) // generate diagnostic
10096 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10097
10098 /* Create the function call operator.
10099
10100 Messing with declarators like this is no uglier than building up the
10101 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10102 other code. */
10103 {
10104 cp_decl_specifier_seq return_type_specs;
10105 cp_declarator* declarator;
10106 tree fco;
10107 int quals;
10108 void *p;
10109
10110 clear_decl_specs (&return_type_specs);
10111 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10112 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10113 else
10114 /* Maybe we will deduce the return type later. */
10115 return_type_specs.type = make_auto ();
10116
10117 p = obstack_alloc (&declarator_obstack, 0);
10118
10119 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10120 sfk_none);
10121
10122 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10123 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10124 declarator = make_call_declarator (declarator, param_list, quals,
10125 VIRT_SPEC_UNSPECIFIED,
10126 REF_QUAL_NONE,
10127 tx_qual,
10128 exception_spec,
10129 /*late_return_type=*/NULL_TREE,
10130 /*requires_clause*/NULL_TREE);
10131 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10132
10133 fco = grokmethod (&return_type_specs,
10134 declarator,
10135 attributes);
10136 if (fco != error_mark_node)
10137 {
10138 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10139 DECL_ARTIFICIAL (fco) = 1;
10140 /* Give the object parameter a different name. */
10141 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10142 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10143 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10144 }
10145 if (template_param_list)
10146 {
10147 fco = finish_member_template_decl (fco);
10148 finish_template_decl (template_param_list);
10149 --parser->num_template_parameter_lists;
10150 }
10151 else if (parser->fully_implicit_function_template_p)
10152 fco = finish_fully_implicit_template (parser, fco);
10153
10154 finish_member_declaration (fco);
10155
10156 obstack_free (&declarator_obstack, p);
10157
10158 return (fco != error_mark_node);
10159 }
10160 }
10161
10162 /* Parse the body of a lambda expression, which is simply
10163
10164 compound-statement
10165
10166 but which requires special handling.
10167 LAMBDA_EXPR is the current representation of the lambda expression. */
10168
10169 static void
10170 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10171 {
10172 bool nested = (current_function_decl != NULL_TREE);
10173 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10174 if (nested)
10175 push_function_context ();
10176 else
10177 /* Still increment function_depth so that we don't GC in the
10178 middle of an expression. */
10179 ++function_depth;
10180 vec<tree> omp_privatization_save;
10181 save_omp_privatization_clauses (omp_privatization_save);
10182 /* Clear this in case we're in the middle of a default argument. */
10183 parser->local_variables_forbidden_p = false;
10184
10185 /* Finish the function call operator
10186 - class_specifier
10187 + late_parsing_for_member
10188 + function_definition_after_declarator
10189 + ctor_initializer_opt_and_function_body */
10190 {
10191 tree fco = lambda_function (lambda_expr);
10192 tree body;
10193 bool done = false;
10194 tree compound_stmt;
10195 tree cap;
10196
10197 /* Let the front end know that we are going to be defining this
10198 function. */
10199 start_preparsed_function (fco,
10200 NULL_TREE,
10201 SF_PRE_PARSED | SF_INCLASS_INLINE);
10202
10203 start_lambda_scope (fco);
10204 body = begin_function_body ();
10205
10206 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10207 goto out;
10208
10209 /* Push the proxies for any explicit captures. */
10210 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10211 cap = TREE_CHAIN (cap))
10212 build_capture_proxy (TREE_PURPOSE (cap));
10213
10214 compound_stmt = begin_compound_stmt (0);
10215
10216 /* 5.1.1.4 of the standard says:
10217 If a lambda-expression does not include a trailing-return-type, it
10218 is as if the trailing-return-type denotes the following type:
10219 * if the compound-statement is of the form
10220 { return attribute-specifier [opt] expression ; }
10221 the type of the returned expression after lvalue-to-rvalue
10222 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10223 (_conv.array_ 4.2), and function-to-pointer conversion
10224 (_conv.func_ 4.3);
10225 * otherwise, void. */
10226
10227 /* In a lambda that has neither a lambda-return-type-clause
10228 nor a deducible form, errors should be reported for return statements
10229 in the body. Since we used void as the placeholder return type, parsing
10230 the body as usual will give such desired behavior. */
10231 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10232 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10233 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10234 {
10235 tree expr = NULL_TREE;
10236 cp_id_kind idk = CP_ID_KIND_NONE;
10237
10238 /* Parse tentatively in case there's more after the initial return
10239 statement. */
10240 cp_parser_parse_tentatively (parser);
10241
10242 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10243
10244 expr = cp_parser_expression (parser, &idk);
10245
10246 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10247 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10248
10249 if (cp_parser_parse_definitely (parser))
10250 {
10251 if (!processing_template_decl)
10252 {
10253 tree type = lambda_return_type (expr);
10254 apply_deduced_return_type (fco, type);
10255 if (type == error_mark_node)
10256 expr = error_mark_node;
10257 }
10258
10259 /* Will get error here if type not deduced yet. */
10260 finish_return_stmt (expr);
10261
10262 done = true;
10263 }
10264 }
10265
10266 if (!done)
10267 {
10268 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10269 cp_parser_label_declaration (parser);
10270 cp_parser_statement_seq_opt (parser, NULL_TREE);
10271 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10272 }
10273
10274 finish_compound_stmt (compound_stmt);
10275
10276 out:
10277 finish_function_body (body);
10278 finish_lambda_scope ();
10279
10280 /* Finish the function and generate code for it if necessary. */
10281 tree fn = finish_function (/*inline*/2);
10282
10283 /* Only expand if the call op is not a template. */
10284 if (!DECL_TEMPLATE_INFO (fco))
10285 expand_or_defer_fn (fn);
10286 }
10287
10288 restore_omp_privatization_clauses (omp_privatization_save);
10289 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10290 if (nested)
10291 pop_function_context();
10292 else
10293 --function_depth;
10294 }
10295
10296 /* Statements [gram.stmt.stmt] */
10297
10298 /* Parse a statement.
10299
10300 statement:
10301 labeled-statement
10302 expression-statement
10303 compound-statement
10304 selection-statement
10305 iteration-statement
10306 jump-statement
10307 declaration-statement
10308 try-block
10309
10310 C++11:
10311
10312 statement:
10313 labeled-statement
10314 attribute-specifier-seq (opt) expression-statement
10315 attribute-specifier-seq (opt) compound-statement
10316 attribute-specifier-seq (opt) selection-statement
10317 attribute-specifier-seq (opt) iteration-statement
10318 attribute-specifier-seq (opt) jump-statement
10319 declaration-statement
10320 attribute-specifier-seq (opt) try-block
10321
10322 TM Extension:
10323
10324 statement:
10325 atomic-statement
10326
10327 IN_COMPOUND is true when the statement is nested inside a
10328 cp_parser_compound_statement; this matters for certain pragmas.
10329
10330 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10331 is a (possibly labeled) if statement which is not enclosed in braces
10332 and has an else clause. This is used to implement -Wparentheses.
10333
10334 CHAIN is a vector of if-else-if conditions. */
10335
10336 static void
10337 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10338 bool in_compound, bool *if_p, vec<tree> *chain)
10339 {
10340 tree statement, std_attrs = NULL_TREE;
10341 cp_token *token;
10342 location_t statement_location, attrs_location;
10343
10344 restart:
10345 if (if_p != NULL)
10346 *if_p = false;
10347 /* There is no statement yet. */
10348 statement = NULL_TREE;
10349
10350 saved_token_sentinel saved_tokens (parser->lexer);
10351 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10352 if (c_dialect_objc ())
10353 /* In obj-c++, seeing '[[' might be the either the beginning of
10354 c++11 attributes, or a nested objc-message-expression. So
10355 let's parse the c++11 attributes tentatively. */
10356 cp_parser_parse_tentatively (parser);
10357 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10358 if (c_dialect_objc ())
10359 {
10360 if (!cp_parser_parse_definitely (parser))
10361 std_attrs = NULL_TREE;
10362 }
10363
10364 /* Peek at the next token. */
10365 token = cp_lexer_peek_token (parser->lexer);
10366 /* Remember the location of the first token in the statement. */
10367 statement_location = token->location;
10368 /* If this is a keyword, then that will often determine what kind of
10369 statement we have. */
10370 if (token->type == CPP_KEYWORD)
10371 {
10372 enum rid keyword = token->keyword;
10373
10374 switch (keyword)
10375 {
10376 case RID_CASE:
10377 case RID_DEFAULT:
10378 /* Looks like a labeled-statement with a case label.
10379 Parse the label, and then use tail recursion to parse
10380 the statement. */
10381 cp_parser_label_for_labeled_statement (parser, std_attrs);
10382 in_compound = false;
10383 goto restart;
10384
10385 case RID_IF:
10386 case RID_SWITCH:
10387 statement = cp_parser_selection_statement (parser, if_p, chain);
10388 break;
10389
10390 case RID_WHILE:
10391 case RID_DO:
10392 case RID_FOR:
10393 statement = cp_parser_iteration_statement (parser, if_p, false);
10394 break;
10395
10396 case RID_CILK_FOR:
10397 if (!flag_cilkplus)
10398 {
10399 error_at (cp_lexer_peek_token (parser->lexer)->location,
10400 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10401 cp_lexer_consume_token (parser->lexer);
10402 statement = error_mark_node;
10403 }
10404 else
10405 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10406 break;
10407
10408 case RID_BREAK:
10409 case RID_CONTINUE:
10410 case RID_RETURN:
10411 case RID_GOTO:
10412 statement = cp_parser_jump_statement (parser);
10413 break;
10414
10415 case RID_CILK_SYNC:
10416 cp_lexer_consume_token (parser->lexer);
10417 if (flag_cilkplus)
10418 {
10419 tree sync_expr = build_cilk_sync ();
10420 SET_EXPR_LOCATION (sync_expr,
10421 token->location);
10422 statement = finish_expr_stmt (sync_expr);
10423 }
10424 else
10425 {
10426 error_at (token->location, "-fcilkplus must be enabled to use"
10427 " %<_Cilk_sync%>");
10428 statement = error_mark_node;
10429 }
10430 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10431 break;
10432
10433 /* Objective-C++ exception-handling constructs. */
10434 case RID_AT_TRY:
10435 case RID_AT_CATCH:
10436 case RID_AT_FINALLY:
10437 case RID_AT_SYNCHRONIZED:
10438 case RID_AT_THROW:
10439 statement = cp_parser_objc_statement (parser);
10440 break;
10441
10442 case RID_TRY:
10443 statement = cp_parser_try_block (parser);
10444 break;
10445
10446 case RID_NAMESPACE:
10447 /* This must be a namespace alias definition. */
10448 cp_parser_declaration_statement (parser);
10449 return;
10450
10451 case RID_TRANSACTION_ATOMIC:
10452 case RID_TRANSACTION_RELAXED:
10453 case RID_SYNCHRONIZED:
10454 case RID_ATOMIC_NOEXCEPT:
10455 case RID_ATOMIC_CANCEL:
10456 statement = cp_parser_transaction (parser, token);
10457 break;
10458 case RID_TRANSACTION_CANCEL:
10459 statement = cp_parser_transaction_cancel (parser);
10460 break;
10461
10462 default:
10463 /* It might be a keyword like `int' that can start a
10464 declaration-statement. */
10465 break;
10466 }
10467 }
10468 else if (token->type == CPP_NAME)
10469 {
10470 /* If the next token is a `:', then we are looking at a
10471 labeled-statement. */
10472 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10473 if (token->type == CPP_COLON)
10474 {
10475 /* Looks like a labeled-statement with an ordinary label.
10476 Parse the label, and then use tail recursion to parse
10477 the statement. */
10478
10479 cp_parser_label_for_labeled_statement (parser, std_attrs);
10480 in_compound = false;
10481 goto restart;
10482 }
10483 }
10484 /* Anything that starts with a `{' must be a compound-statement. */
10485 else if (token->type == CPP_OPEN_BRACE)
10486 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10487 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10488 a statement all its own. */
10489 else if (token->type == CPP_PRAGMA)
10490 {
10491 /* Only certain OpenMP pragmas are attached to statements, and thus
10492 are considered statements themselves. All others are not. In
10493 the context of a compound, accept the pragma as a "statement" and
10494 return so that we can check for a close brace. Otherwise we
10495 require a real statement and must go back and read one. */
10496 if (in_compound)
10497 cp_parser_pragma (parser, pragma_compound, if_p);
10498 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10499 goto restart;
10500 return;
10501 }
10502 else if (token->type == CPP_EOF)
10503 {
10504 cp_parser_error (parser, "expected statement");
10505 return;
10506 }
10507
10508 /* Everything else must be a declaration-statement or an
10509 expression-statement. Try for the declaration-statement
10510 first, unless we are looking at a `;', in which case we know that
10511 we have an expression-statement. */
10512 if (!statement)
10513 {
10514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10515 {
10516 if (std_attrs != NULL_TREE)
10517 {
10518 /* Attributes should be parsed as part of the the
10519 declaration, so let's un-parse them. */
10520 saved_tokens.rollback();
10521 std_attrs = NULL_TREE;
10522 }
10523
10524 cp_parser_parse_tentatively (parser);
10525 /* Try to parse the declaration-statement. */
10526 cp_parser_declaration_statement (parser);
10527 /* If that worked, we're done. */
10528 if (cp_parser_parse_definitely (parser))
10529 return;
10530 }
10531 /* Look for an expression-statement instead. */
10532 statement = cp_parser_expression_statement (parser, in_statement_expr);
10533 }
10534
10535 /* Set the line number for the statement. */
10536 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10537 SET_EXPR_LOCATION (statement, statement_location);
10538
10539 /* Note that for now, we don't do anything with c++11 statements
10540 parsed at this level. */
10541 if (std_attrs != NULL_TREE)
10542 warning_at (attrs_location,
10543 OPT_Wattributes,
10544 "attributes at the beginning of statement are ignored");
10545 }
10546
10547 /* Parse the label for a labeled-statement, i.e.
10548
10549 identifier :
10550 case constant-expression :
10551 default :
10552
10553 GNU Extension:
10554 case constant-expression ... constant-expression : statement
10555
10556 When a label is parsed without errors, the label is added to the
10557 parse tree by the finish_* functions, so this function doesn't
10558 have to return the label. */
10559
10560 static void
10561 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10562 {
10563 cp_token *token;
10564 tree label = NULL_TREE;
10565 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10566
10567 /* The next token should be an identifier. */
10568 token = cp_lexer_peek_token (parser->lexer);
10569 if (token->type != CPP_NAME
10570 && token->type != CPP_KEYWORD)
10571 {
10572 cp_parser_error (parser, "expected labeled-statement");
10573 return;
10574 }
10575
10576 parser->colon_corrects_to_scope_p = false;
10577 switch (token->keyword)
10578 {
10579 case RID_CASE:
10580 {
10581 tree expr, expr_hi;
10582 cp_token *ellipsis;
10583
10584 /* Consume the `case' token. */
10585 cp_lexer_consume_token (parser->lexer);
10586 /* Parse the constant-expression. */
10587 expr = cp_parser_constant_expression (parser);
10588 if (check_for_bare_parameter_packs (expr))
10589 expr = error_mark_node;
10590
10591 ellipsis = cp_lexer_peek_token (parser->lexer);
10592 if (ellipsis->type == CPP_ELLIPSIS)
10593 {
10594 /* Consume the `...' token. */
10595 cp_lexer_consume_token (parser->lexer);
10596 expr_hi = cp_parser_constant_expression (parser);
10597 if (check_for_bare_parameter_packs (expr_hi))
10598 expr_hi = error_mark_node;
10599
10600 /* We don't need to emit warnings here, as the common code
10601 will do this for us. */
10602 }
10603 else
10604 expr_hi = NULL_TREE;
10605
10606 if (parser->in_switch_statement_p)
10607 finish_case_label (token->location, expr, expr_hi);
10608 else
10609 error_at (token->location,
10610 "case label %qE not within a switch statement",
10611 expr);
10612 }
10613 break;
10614
10615 case RID_DEFAULT:
10616 /* Consume the `default' token. */
10617 cp_lexer_consume_token (parser->lexer);
10618
10619 if (parser->in_switch_statement_p)
10620 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10621 else
10622 error_at (token->location, "case label not within a switch statement");
10623 break;
10624
10625 default:
10626 /* Anything else must be an ordinary label. */
10627 label = finish_label_stmt (cp_parser_identifier (parser));
10628 break;
10629 }
10630
10631 /* Require the `:' token. */
10632 cp_parser_require (parser, CPP_COLON, RT_COLON);
10633
10634 /* An ordinary label may optionally be followed by attributes.
10635 However, this is only permitted if the attributes are then
10636 followed by a semicolon. This is because, for backward
10637 compatibility, when parsing
10638 lab: __attribute__ ((unused)) int i;
10639 we want the attribute to attach to "i", not "lab". */
10640 if (label != NULL_TREE
10641 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10642 {
10643 tree attrs;
10644 cp_parser_parse_tentatively (parser);
10645 attrs = cp_parser_gnu_attributes_opt (parser);
10646 if (attrs == NULL_TREE
10647 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10648 cp_parser_abort_tentative_parse (parser);
10649 else if (!cp_parser_parse_definitely (parser))
10650 ;
10651 else
10652 attributes = chainon (attributes, attrs);
10653 }
10654
10655 if (attributes != NULL_TREE)
10656 cplus_decl_attributes (&label, attributes, 0);
10657
10658 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10659 }
10660
10661 /* Parse an expression-statement.
10662
10663 expression-statement:
10664 expression [opt] ;
10665
10666 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10667 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10668 indicates whether this expression-statement is part of an
10669 expression statement. */
10670
10671 static tree
10672 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10673 {
10674 tree statement = NULL_TREE;
10675 cp_token *token = cp_lexer_peek_token (parser->lexer);
10676
10677 /* If the next token is a ';', then there is no expression
10678 statement. */
10679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10680 {
10681 statement = cp_parser_expression (parser);
10682 if (statement == error_mark_node
10683 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10684 {
10685 cp_parser_skip_to_end_of_block_or_statement (parser);
10686 return error_mark_node;
10687 }
10688 }
10689
10690 /* Give a helpful message for "A<T>::type t;" and the like. */
10691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10692 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10693 {
10694 if (TREE_CODE (statement) == SCOPE_REF)
10695 error_at (token->location, "need %<typename%> before %qE because "
10696 "%qT is a dependent scope",
10697 statement, TREE_OPERAND (statement, 0));
10698 else if (is_overloaded_fn (statement)
10699 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10700 {
10701 /* A::A a; */
10702 tree fn = get_first_fn (statement);
10703 error_at (token->location,
10704 "%<%T::%D%> names the constructor, not the type",
10705 DECL_CONTEXT (fn), DECL_NAME (fn));
10706 }
10707 }
10708
10709 /* Consume the final `;'. */
10710 cp_parser_consume_semicolon_at_end_of_statement (parser);
10711
10712 if (in_statement_expr
10713 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10714 /* This is the final expression statement of a statement
10715 expression. */
10716 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10717 else if (statement)
10718 statement = finish_expr_stmt (statement);
10719
10720 return statement;
10721 }
10722
10723 /* Parse a compound-statement.
10724
10725 compound-statement:
10726 { statement-seq [opt] }
10727
10728 GNU extension:
10729
10730 compound-statement:
10731 { label-declaration-seq [opt] statement-seq [opt] }
10732
10733 label-declaration-seq:
10734 label-declaration
10735 label-declaration-seq label-declaration
10736
10737 Returns a tree representing the statement. */
10738
10739 static tree
10740 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10741 int bcs_flags, bool function_body)
10742 {
10743 tree compound_stmt;
10744
10745 /* Consume the `{'. */
10746 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10747 return error_mark_node;
10748 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10749 && !function_body && cxx_dialect < cxx14)
10750 pedwarn (input_location, OPT_Wpedantic,
10751 "compound-statement in constexpr function");
10752 /* Begin the compound-statement. */
10753 compound_stmt = begin_compound_stmt (bcs_flags);
10754 /* If the next keyword is `__label__' we have a label declaration. */
10755 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10756 cp_parser_label_declaration (parser);
10757 /* Parse an (optional) statement-seq. */
10758 cp_parser_statement_seq_opt (parser, in_statement_expr);
10759 /* Finish the compound-statement. */
10760 finish_compound_stmt (compound_stmt);
10761 /* Consume the `}'. */
10762 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10763
10764 return compound_stmt;
10765 }
10766
10767 /* Parse an (optional) statement-seq.
10768
10769 statement-seq:
10770 statement
10771 statement-seq [opt] statement */
10772
10773 static void
10774 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10775 {
10776 /* Scan statements until there aren't any more. */
10777 while (true)
10778 {
10779 cp_token *token = cp_lexer_peek_token (parser->lexer);
10780
10781 /* If we are looking at a `}', then we have run out of
10782 statements; the same is true if we have reached the end
10783 of file, or have stumbled upon a stray '@end'. */
10784 if (token->type == CPP_CLOSE_BRACE
10785 || token->type == CPP_EOF
10786 || token->type == CPP_PRAGMA_EOL
10787 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10788 break;
10789
10790 /* If we are in a compound statement and find 'else' then
10791 something went wrong. */
10792 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10793 {
10794 if (parser->in_statement & IN_IF_STMT)
10795 break;
10796 else
10797 {
10798 token = cp_lexer_consume_token (parser->lexer);
10799 error_at (token->location, "%<else%> without a previous %<if%>");
10800 }
10801 }
10802
10803 /* Parse the statement. */
10804 cp_parser_statement (parser, in_statement_expr, true, NULL);
10805 }
10806 }
10807
10808 /* Parse a selection-statement.
10809
10810 selection-statement:
10811 if ( condition ) statement
10812 if ( condition ) statement else statement
10813 switch ( condition ) statement
10814
10815 Returns the new IF_STMT or SWITCH_STMT.
10816
10817 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10818 is a (possibly labeled) if statement which is not enclosed in
10819 braces and has an else clause. This is used to implement
10820 -Wparentheses.
10821
10822 CHAIN is a vector of if-else-if conditions. This is used to implement
10823 -Wduplicated-cond. */
10824
10825 static tree
10826 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10827 vec<tree> *chain)
10828 {
10829 cp_token *token;
10830 enum rid keyword;
10831 token_indent_info guard_tinfo;
10832
10833 if (if_p != NULL)
10834 *if_p = false;
10835
10836 /* Peek at the next token. */
10837 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10838 guard_tinfo = get_token_indent_info (token);
10839
10840 /* See what kind of keyword it is. */
10841 keyword = token->keyword;
10842 switch (keyword)
10843 {
10844 case RID_IF:
10845 case RID_SWITCH:
10846 {
10847 tree statement;
10848 tree condition;
10849
10850 /* Look for the `('. */
10851 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10852 {
10853 cp_parser_skip_to_end_of_statement (parser);
10854 return error_mark_node;
10855 }
10856
10857 /* Begin the selection-statement. */
10858 if (keyword == RID_IF)
10859 statement = begin_if_stmt ();
10860 else
10861 statement = begin_switch_stmt ();
10862
10863 /* Parse the condition. */
10864 condition = cp_parser_condition (parser);
10865 /* Look for the `)'. */
10866 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10867 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10868 /*consume_paren=*/true);
10869
10870 if (keyword == RID_IF)
10871 {
10872 bool nested_if;
10873 unsigned char in_statement;
10874
10875 /* Add the condition. */
10876 finish_if_stmt_cond (condition, statement);
10877
10878 if (warn_duplicated_cond)
10879 warn_duplicated_cond_add_or_warn (token->location, condition,
10880 &chain);
10881
10882 /* Parse the then-clause. */
10883 in_statement = parser->in_statement;
10884 parser->in_statement |= IN_IF_STMT;
10885 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10886 guard_tinfo);
10887 parser->in_statement = in_statement;
10888
10889 finish_then_clause (statement);
10890
10891 /* If the next token is `else', parse the else-clause. */
10892 if (cp_lexer_next_token_is_keyword (parser->lexer,
10893 RID_ELSE))
10894 {
10895 guard_tinfo
10896 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10897 /* Consume the `else' keyword. */
10898 cp_lexer_consume_token (parser->lexer);
10899 if (warn_duplicated_cond)
10900 {
10901 if (cp_lexer_next_token_is_keyword (parser->lexer,
10902 RID_IF)
10903 && chain == NULL)
10904 {
10905 /* We've got "if (COND) else if (COND2)". Start
10906 the condition chain and add COND as the first
10907 element. */
10908 chain = new vec<tree> ();
10909 if (!CONSTANT_CLASS_P (condition)
10910 && !TREE_SIDE_EFFECTS (condition))
10911 {
10912 /* Wrap it in a NOP_EXPR so that we can set the
10913 location of the condition. */
10914 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10915 condition);
10916 SET_EXPR_LOCATION (e, token->location);
10917 chain->safe_push (e);
10918 }
10919 }
10920 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10921 RID_IF))
10922 {
10923 /* This is if-else without subsequent if. Zap the
10924 condition chain; we would have already warned at
10925 this point. */
10926 delete chain;
10927 chain = NULL;
10928 }
10929 }
10930 begin_else_clause (statement);
10931 /* Parse the else-clause. */
10932 cp_parser_implicitly_scoped_statement (parser, NULL,
10933 guard_tinfo, chain);
10934
10935 finish_else_clause (statement);
10936
10937 /* If we are currently parsing a then-clause, then
10938 IF_P will not be NULL. We set it to true to
10939 indicate that this if statement has an else clause.
10940 This may trigger the Wparentheses warning below
10941 when we get back up to the parent if statement. */
10942 if (if_p != NULL)
10943 *if_p = true;
10944 }
10945 else
10946 {
10947 /* This if statement does not have an else clause. If
10948 NESTED_IF is true, then the then-clause has an if
10949 statement which does have an else clause. We warn
10950 about the potential ambiguity. */
10951 if (nested_if)
10952 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
10953 "suggest explicit braces to avoid ambiguous"
10954 " %<else%>");
10955 if (warn_duplicated_cond)
10956 {
10957 /* We don't need the condition chain anymore. */
10958 delete chain;
10959 chain = NULL;
10960 }
10961 }
10962
10963 /* Now we're all done with the if-statement. */
10964 finish_if_stmt (statement);
10965 }
10966 else
10967 {
10968 bool in_switch_statement_p;
10969 unsigned char in_statement;
10970
10971 /* Add the condition. */
10972 finish_switch_cond (condition, statement);
10973
10974 /* Parse the body of the switch-statement. */
10975 in_switch_statement_p = parser->in_switch_statement_p;
10976 in_statement = parser->in_statement;
10977 parser->in_switch_statement_p = true;
10978 parser->in_statement |= IN_SWITCH_STMT;
10979 cp_parser_implicitly_scoped_statement (parser, if_p,
10980 guard_tinfo);
10981 parser->in_switch_statement_p = in_switch_statement_p;
10982 parser->in_statement = in_statement;
10983
10984 /* Now we're all done with the switch-statement. */
10985 finish_switch_stmt (statement);
10986 }
10987
10988 return statement;
10989 }
10990 break;
10991
10992 default:
10993 cp_parser_error (parser, "expected selection-statement");
10994 return error_mark_node;
10995 }
10996 }
10997
10998 /* Parse a condition.
10999
11000 condition:
11001 expression
11002 type-specifier-seq declarator = initializer-clause
11003 type-specifier-seq declarator braced-init-list
11004
11005 GNU Extension:
11006
11007 condition:
11008 type-specifier-seq declarator asm-specification [opt]
11009 attributes [opt] = assignment-expression
11010
11011 Returns the expression that should be tested. */
11012
11013 static tree
11014 cp_parser_condition (cp_parser* parser)
11015 {
11016 cp_decl_specifier_seq type_specifiers;
11017 const char *saved_message;
11018 int declares_class_or_enum;
11019
11020 /* Try the declaration first. */
11021 cp_parser_parse_tentatively (parser);
11022 /* New types are not allowed in the type-specifier-seq for a
11023 condition. */
11024 saved_message = parser->type_definition_forbidden_message;
11025 parser->type_definition_forbidden_message
11026 = G_("types may not be defined in conditions");
11027 /* Parse the type-specifier-seq. */
11028 cp_parser_decl_specifier_seq (parser,
11029 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11030 &type_specifiers,
11031 &declares_class_or_enum);
11032 /* Restore the saved message. */
11033 parser->type_definition_forbidden_message = saved_message;
11034 /* If all is well, we might be looking at a declaration. */
11035 if (!cp_parser_error_occurred (parser))
11036 {
11037 tree decl;
11038 tree asm_specification;
11039 tree attributes;
11040 cp_declarator *declarator;
11041 tree initializer = NULL_TREE;
11042
11043 /* Parse the declarator. */
11044 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11045 /*ctor_dtor_or_conv_p=*/NULL,
11046 /*parenthesized_p=*/NULL,
11047 /*member_p=*/false,
11048 /*friend_p=*/false);
11049 /* Parse the attributes. */
11050 attributes = cp_parser_attributes_opt (parser);
11051 /* Parse the asm-specification. */
11052 asm_specification = cp_parser_asm_specification_opt (parser);
11053 /* If the next token is not an `=' or '{', then we might still be
11054 looking at an expression. For example:
11055
11056 if (A(a).x)
11057
11058 looks like a decl-specifier-seq and a declarator -- but then
11059 there is no `=', so this is an expression. */
11060 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11061 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11062 cp_parser_simulate_error (parser);
11063
11064 /* If we did see an `=' or '{', then we are looking at a declaration
11065 for sure. */
11066 if (cp_parser_parse_definitely (parser))
11067 {
11068 tree pushed_scope;
11069 bool non_constant_p;
11070 bool flags = LOOKUP_ONLYCONVERTING;
11071
11072 /* Create the declaration. */
11073 decl = start_decl (declarator, &type_specifiers,
11074 /*initialized_p=*/true,
11075 attributes, /*prefix_attributes=*/NULL_TREE,
11076 &pushed_scope);
11077
11078 /* Parse the initializer. */
11079 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11080 {
11081 initializer = cp_parser_braced_list (parser, &non_constant_p);
11082 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11083 flags = 0;
11084 }
11085 else
11086 {
11087 /* Consume the `='. */
11088 cp_parser_require (parser, CPP_EQ, RT_EQ);
11089 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11090 }
11091 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11092 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11093
11094 /* Process the initializer. */
11095 cp_finish_decl (decl,
11096 initializer, !non_constant_p,
11097 asm_specification,
11098 flags);
11099
11100 if (pushed_scope)
11101 pop_scope (pushed_scope);
11102
11103 return convert_from_reference (decl);
11104 }
11105 }
11106 /* If we didn't even get past the declarator successfully, we are
11107 definitely not looking at a declaration. */
11108 else
11109 cp_parser_abort_tentative_parse (parser);
11110
11111 /* Otherwise, we are looking at an expression. */
11112 return cp_parser_expression (parser);
11113 }
11114
11115 /* Parses a for-statement or range-for-statement until the closing ')',
11116 not included. */
11117
11118 static tree
11119 cp_parser_for (cp_parser *parser, bool ivdep)
11120 {
11121 tree init, scope, decl;
11122 bool is_range_for;
11123
11124 /* Begin the for-statement. */
11125 scope = begin_for_scope (&init);
11126
11127 /* Parse the initialization. */
11128 is_range_for = cp_parser_for_init_statement (parser, &decl);
11129
11130 if (is_range_for)
11131 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11132 else
11133 return cp_parser_c_for (parser, scope, init, ivdep);
11134 }
11135
11136 static tree
11137 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11138 {
11139 /* Normal for loop */
11140 tree condition = NULL_TREE;
11141 tree expression = NULL_TREE;
11142 tree stmt;
11143
11144 stmt = begin_for_stmt (scope, init);
11145 /* The for-init-statement has already been parsed in
11146 cp_parser_for_init_statement, so no work is needed here. */
11147 finish_for_init_stmt (stmt);
11148
11149 /* If there's a condition, process it. */
11150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11151 condition = cp_parser_condition (parser);
11152 else if (ivdep)
11153 {
11154 cp_parser_error (parser, "missing loop condition in loop with "
11155 "%<GCC ivdep%> pragma");
11156 condition = error_mark_node;
11157 }
11158 finish_for_cond (condition, stmt, ivdep);
11159 /* Look for the `;'. */
11160 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11161
11162 /* If there's an expression, process it. */
11163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11164 expression = cp_parser_expression (parser);
11165 finish_for_expr (expression, stmt);
11166
11167 return stmt;
11168 }
11169
11170 /* Tries to parse a range-based for-statement:
11171
11172 range-based-for:
11173 decl-specifier-seq declarator : expression
11174
11175 The decl-specifier-seq declarator and the `:' are already parsed by
11176 cp_parser_for_init_statement. If processing_template_decl it returns a
11177 newly created RANGE_FOR_STMT; if not, it is converted to a
11178 regular FOR_STMT. */
11179
11180 static tree
11181 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11182 bool ivdep)
11183 {
11184 tree stmt, range_expr;
11185
11186 /* Get the range declaration momentarily out of the way so that
11187 the range expression doesn't clash with it. */
11188 if (range_decl != error_mark_node)
11189 pop_binding (DECL_NAME (range_decl), range_decl);
11190
11191 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11192 {
11193 bool expr_non_constant_p;
11194 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11195 }
11196 else
11197 range_expr = cp_parser_expression (parser);
11198
11199 /* Put the range declaration back into scope. */
11200 if (range_decl != error_mark_node)
11201 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
11202
11203 /* If in template, STMT is converted to a normal for-statement
11204 at instantiation. If not, it is done just ahead. */
11205 if (processing_template_decl)
11206 {
11207 if (check_for_bare_parameter_packs (range_expr))
11208 range_expr = error_mark_node;
11209 stmt = begin_range_for_stmt (scope, init);
11210 if (ivdep)
11211 RANGE_FOR_IVDEP (stmt) = 1;
11212 finish_range_for_decl (stmt, range_decl, range_expr);
11213 if (!type_dependent_expression_p (range_expr)
11214 /* do_auto_deduction doesn't mess with template init-lists. */
11215 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11216 do_range_for_auto_deduction (range_decl, range_expr);
11217 }
11218 else
11219 {
11220 stmt = begin_for_stmt (scope, init);
11221 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11222 }
11223 return stmt;
11224 }
11225
11226 /* Subroutine of cp_convert_range_for: given the initializer expression,
11227 builds up the range temporary. */
11228
11229 static tree
11230 build_range_temp (tree range_expr)
11231 {
11232 tree range_type, range_temp;
11233
11234 /* Find out the type deduced by the declaration
11235 `auto &&__range = range_expr'. */
11236 range_type = cp_build_reference_type (make_auto (), true);
11237 range_type = do_auto_deduction (range_type, range_expr,
11238 type_uses_auto (range_type));
11239
11240 /* Create the __range variable. */
11241 range_temp = build_decl (input_location, VAR_DECL,
11242 get_identifier ("__for_range"), range_type);
11243 TREE_USED (range_temp) = 1;
11244 DECL_ARTIFICIAL (range_temp) = 1;
11245
11246 return range_temp;
11247 }
11248
11249 /* Used by cp_parser_range_for in template context: we aren't going to
11250 do a full conversion yet, but we still need to resolve auto in the
11251 type of the for-range-declaration if present. This is basically
11252 a shortcut version of cp_convert_range_for. */
11253
11254 static void
11255 do_range_for_auto_deduction (tree decl, tree range_expr)
11256 {
11257 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11258 if (auto_node)
11259 {
11260 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11261 range_temp = convert_from_reference (build_range_temp (range_expr));
11262 iter_type = (cp_parser_perform_range_for_lookup
11263 (range_temp, &begin_dummy, &end_dummy));
11264 if (iter_type)
11265 {
11266 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11267 iter_type);
11268 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11269 tf_warning_or_error);
11270 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11271 iter_decl, auto_node);
11272 }
11273 }
11274 }
11275
11276 /* Converts a range-based for-statement into a normal
11277 for-statement, as per the definition.
11278
11279 for (RANGE_DECL : RANGE_EXPR)
11280 BLOCK
11281
11282 should be equivalent to:
11283
11284 {
11285 auto &&__range = RANGE_EXPR;
11286 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11287 __begin != __end;
11288 ++__begin)
11289 {
11290 RANGE_DECL = *__begin;
11291 BLOCK
11292 }
11293 }
11294
11295 If RANGE_EXPR is an array:
11296 BEGIN_EXPR = __range
11297 END_EXPR = __range + ARRAY_SIZE(__range)
11298 Else if RANGE_EXPR has a member 'begin' or 'end':
11299 BEGIN_EXPR = __range.begin()
11300 END_EXPR = __range.end()
11301 Else:
11302 BEGIN_EXPR = begin(__range)
11303 END_EXPR = end(__range);
11304
11305 If __range has a member 'begin' but not 'end', or vice versa, we must
11306 still use the second alternative (it will surely fail, however).
11307 When calling begin()/end() in the third alternative we must use
11308 argument dependent lookup, but always considering 'std' as an associated
11309 namespace. */
11310
11311 tree
11312 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11313 bool ivdep)
11314 {
11315 tree begin, end;
11316 tree iter_type, begin_expr, end_expr;
11317 tree condition, expression;
11318
11319 if (range_decl == error_mark_node || range_expr == error_mark_node)
11320 /* If an error happened previously do nothing or else a lot of
11321 unhelpful errors would be issued. */
11322 begin_expr = end_expr = iter_type = error_mark_node;
11323 else
11324 {
11325 tree range_temp;
11326
11327 if (VAR_P (range_expr)
11328 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11329 /* Can't bind a reference to an array of runtime bound. */
11330 range_temp = range_expr;
11331 else
11332 {
11333 range_temp = build_range_temp (range_expr);
11334 pushdecl (range_temp);
11335 cp_finish_decl (range_temp, range_expr,
11336 /*is_constant_init*/false, NULL_TREE,
11337 LOOKUP_ONLYCONVERTING);
11338 range_temp = convert_from_reference (range_temp);
11339 }
11340 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11341 &begin_expr, &end_expr);
11342 }
11343
11344 /* The new for initialization statement. */
11345 begin = build_decl (input_location, VAR_DECL,
11346 get_identifier ("__for_begin"), iter_type);
11347 TREE_USED (begin) = 1;
11348 DECL_ARTIFICIAL (begin) = 1;
11349 pushdecl (begin);
11350 cp_finish_decl (begin, begin_expr,
11351 /*is_constant_init*/false, NULL_TREE,
11352 LOOKUP_ONLYCONVERTING);
11353
11354 if (cxx_dialect >= cxx1z)
11355 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11356 end = build_decl (input_location, VAR_DECL,
11357 get_identifier ("__for_end"), iter_type);
11358 TREE_USED (end) = 1;
11359 DECL_ARTIFICIAL (end) = 1;
11360 pushdecl (end);
11361 cp_finish_decl (end, end_expr,
11362 /*is_constant_init*/false, NULL_TREE,
11363 LOOKUP_ONLYCONVERTING);
11364
11365 finish_for_init_stmt (statement);
11366
11367 /* The new for condition. */
11368 condition = build_x_binary_op (input_location, NE_EXPR,
11369 begin, ERROR_MARK,
11370 end, ERROR_MARK,
11371 NULL, tf_warning_or_error);
11372 finish_for_cond (condition, statement, ivdep);
11373
11374 /* The new increment expression. */
11375 expression = finish_unary_op_expr (input_location,
11376 PREINCREMENT_EXPR, begin,
11377 tf_warning_or_error);
11378 finish_for_expr (expression, statement);
11379
11380 /* The declaration is initialized with *__begin inside the loop body. */
11381 cp_finish_decl (range_decl,
11382 build_x_indirect_ref (input_location, begin, RO_NULL,
11383 tf_warning_or_error),
11384 /*is_constant_init*/false, NULL_TREE,
11385 LOOKUP_ONLYCONVERTING);
11386
11387 return statement;
11388 }
11389
11390 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11391 We need to solve both at the same time because the method used
11392 depends on the existence of members begin or end.
11393 Returns the type deduced for the iterator expression. */
11394
11395 static tree
11396 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11397 {
11398 if (error_operand_p (range))
11399 {
11400 *begin = *end = error_mark_node;
11401 return error_mark_node;
11402 }
11403
11404 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11405 {
11406 error ("range-based %<for%> expression of type %qT "
11407 "has incomplete type", TREE_TYPE (range));
11408 *begin = *end = error_mark_node;
11409 return error_mark_node;
11410 }
11411 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11412 {
11413 /* If RANGE is an array, we will use pointer arithmetic. */
11414 *begin = decay_conversion (range, tf_warning_or_error);
11415 *end = build_binary_op (input_location, PLUS_EXPR,
11416 range,
11417 array_type_nelts_top (TREE_TYPE (range)),
11418 0);
11419 return TREE_TYPE (*begin);
11420 }
11421 else
11422 {
11423 /* If it is not an array, we must do a bit of magic. */
11424 tree id_begin, id_end;
11425 tree member_begin, member_end;
11426
11427 *begin = *end = error_mark_node;
11428
11429 id_begin = get_identifier ("begin");
11430 id_end = get_identifier ("end");
11431 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11432 /*protect=*/2, /*want_type=*/false,
11433 tf_warning_or_error);
11434 member_end = lookup_member (TREE_TYPE (range), id_end,
11435 /*protect=*/2, /*want_type=*/false,
11436 tf_warning_or_error);
11437
11438 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11439 {
11440 /* Use the member functions. */
11441 if (member_begin != NULL_TREE)
11442 *begin = cp_parser_range_for_member_function (range, id_begin);
11443 else
11444 error ("range-based %<for%> expression of type %qT has an "
11445 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11446
11447 if (member_end != NULL_TREE)
11448 *end = cp_parser_range_for_member_function (range, id_end);
11449 else
11450 error ("range-based %<for%> expression of type %qT has a "
11451 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11452 }
11453 else
11454 {
11455 /* Use global functions with ADL. */
11456 vec<tree, va_gc> *vec;
11457 vec = make_tree_vector ();
11458
11459 vec_safe_push (vec, range);
11460
11461 member_begin = perform_koenig_lookup (id_begin, vec,
11462 tf_warning_or_error);
11463 *begin = finish_call_expr (member_begin, &vec, false, true,
11464 tf_warning_or_error);
11465 member_end = perform_koenig_lookup (id_end, vec,
11466 tf_warning_or_error);
11467 *end = finish_call_expr (member_end, &vec, false, true,
11468 tf_warning_or_error);
11469
11470 release_tree_vector (vec);
11471 }
11472
11473 /* Last common checks. */
11474 if (*begin == error_mark_node || *end == error_mark_node)
11475 {
11476 /* If one of the expressions is an error do no more checks. */
11477 *begin = *end = error_mark_node;
11478 return error_mark_node;
11479 }
11480 else if (type_dependent_expression_p (*begin)
11481 || type_dependent_expression_p (*end))
11482 /* Can happen, when, eg, in a template context, Koenig lookup
11483 can't resolve begin/end (c++/58503). */
11484 return NULL_TREE;
11485 else
11486 {
11487 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11488 /* The unqualified type of the __begin and __end temporaries should
11489 be the same, as required by the multiple auto declaration. */
11490 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11491 {
11492 if (cxx_dialect >= cxx1z
11493 && (build_x_binary_op (input_location, NE_EXPR,
11494 *begin, ERROR_MARK,
11495 *end, ERROR_MARK,
11496 NULL, tf_none)
11497 != error_mark_node))
11498 /* P0184R0 allows __begin and __end to have different types,
11499 but make sure they are comparable so we can give a better
11500 diagnostic. */;
11501 else
11502 error ("inconsistent begin/end types in range-based %<for%> "
11503 "statement: %qT and %qT",
11504 TREE_TYPE (*begin), TREE_TYPE (*end));
11505 }
11506 return iter_type;
11507 }
11508 }
11509 }
11510
11511 /* Helper function for cp_parser_perform_range_for_lookup.
11512 Builds a tree for RANGE.IDENTIFIER(). */
11513
11514 static tree
11515 cp_parser_range_for_member_function (tree range, tree identifier)
11516 {
11517 tree member, res;
11518 vec<tree, va_gc> *vec;
11519
11520 member = finish_class_member_access_expr (range, identifier,
11521 false, tf_warning_or_error);
11522 if (member == error_mark_node)
11523 return error_mark_node;
11524
11525 vec = make_tree_vector ();
11526 res = finish_call_expr (member, &vec,
11527 /*disallow_virtual=*/false,
11528 /*koenig_p=*/false,
11529 tf_warning_or_error);
11530 release_tree_vector (vec);
11531 return res;
11532 }
11533
11534 /* Parse an iteration-statement.
11535
11536 iteration-statement:
11537 while ( condition ) statement
11538 do statement while ( expression ) ;
11539 for ( for-init-statement condition [opt] ; expression [opt] )
11540 statement
11541
11542 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11543
11544 static tree
11545 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11546 {
11547 cp_token *token;
11548 enum rid keyword;
11549 tree statement;
11550 unsigned char in_statement;
11551 token_indent_info guard_tinfo;
11552
11553 /* Peek at the next token. */
11554 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11555 if (!token)
11556 return error_mark_node;
11557
11558 guard_tinfo = get_token_indent_info (token);
11559
11560 /* Remember whether or not we are already within an iteration
11561 statement. */
11562 in_statement = parser->in_statement;
11563
11564 /* See what kind of keyword it is. */
11565 keyword = token->keyword;
11566 switch (keyword)
11567 {
11568 case RID_WHILE:
11569 {
11570 tree condition;
11571
11572 /* Begin the while-statement. */
11573 statement = begin_while_stmt ();
11574 /* Look for the `('. */
11575 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11576 /* Parse the condition. */
11577 condition = cp_parser_condition (parser);
11578 finish_while_stmt_cond (condition, statement, ivdep);
11579 /* Look for the `)'. */
11580 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11581 /* Parse the dependent statement. */
11582 parser->in_statement = IN_ITERATION_STMT;
11583 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11584 parser->in_statement = in_statement;
11585 /* We're done with the while-statement. */
11586 finish_while_stmt (statement);
11587 }
11588 break;
11589
11590 case RID_DO:
11591 {
11592 tree expression;
11593
11594 /* Begin the do-statement. */
11595 statement = begin_do_stmt ();
11596 /* Parse the body of the do-statement. */
11597 parser->in_statement = IN_ITERATION_STMT;
11598 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11599 parser->in_statement = in_statement;
11600 finish_do_body (statement);
11601 /* Look for the `while' keyword. */
11602 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11603 /* Look for the `('. */
11604 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11605 /* Parse the expression. */
11606 expression = cp_parser_expression (parser);
11607 /* We're done with the do-statement. */
11608 finish_do_stmt (expression, statement, ivdep);
11609 /* Look for the `)'. */
11610 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11611 /* Look for the `;'. */
11612 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11613 }
11614 break;
11615
11616 case RID_FOR:
11617 {
11618 /* Look for the `('. */
11619 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11620
11621 statement = cp_parser_for (parser, ivdep);
11622
11623 /* Look for the `)'. */
11624 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11625
11626 /* Parse the body of the for-statement. */
11627 parser->in_statement = IN_ITERATION_STMT;
11628 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11629 parser->in_statement = in_statement;
11630
11631 /* We're done with the for-statement. */
11632 finish_for_stmt (statement);
11633 }
11634 break;
11635
11636 default:
11637 cp_parser_error (parser, "expected iteration-statement");
11638 statement = error_mark_node;
11639 break;
11640 }
11641
11642 return statement;
11643 }
11644
11645 /* Parse a for-init-statement or the declarator of a range-based-for.
11646 Returns true if a range-based-for declaration is seen.
11647
11648 for-init-statement:
11649 expression-statement
11650 simple-declaration */
11651
11652 static bool
11653 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11654 {
11655 /* If the next token is a `;', then we have an empty
11656 expression-statement. Grammatically, this is also a
11657 simple-declaration, but an invalid one, because it does not
11658 declare anything. Therefore, if we did not handle this case
11659 specially, we would issue an error message about an invalid
11660 declaration. */
11661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11662 {
11663 bool is_range_for = false;
11664 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11665
11666 /* A colon is used in range-based for. */
11667 parser->colon_corrects_to_scope_p = false;
11668
11669 /* We're going to speculatively look for a declaration, falling back
11670 to an expression, if necessary. */
11671 cp_parser_parse_tentatively (parser);
11672 /* Parse the declaration. */
11673 cp_parser_simple_declaration (parser,
11674 /*function_definition_allowed_p=*/false,
11675 decl);
11676 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11677 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11678 {
11679 /* It is a range-for, consume the ':' */
11680 cp_lexer_consume_token (parser->lexer);
11681 is_range_for = true;
11682 if (cxx_dialect < cxx11)
11683 {
11684 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11685 "range-based %<for%> loops only available with "
11686 "-std=c++11 or -std=gnu++11");
11687 *decl = error_mark_node;
11688 }
11689 }
11690 else
11691 /* The ';' is not consumed yet because we told
11692 cp_parser_simple_declaration not to. */
11693 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11694
11695 if (cp_parser_parse_definitely (parser))
11696 return is_range_for;
11697 /* If the tentative parse failed, then we shall need to look for an
11698 expression-statement. */
11699 }
11700 /* If we are here, it is an expression-statement. */
11701 cp_parser_expression_statement (parser, NULL_TREE);
11702 return false;
11703 }
11704
11705 /* Parse a jump-statement.
11706
11707 jump-statement:
11708 break ;
11709 continue ;
11710 return expression [opt] ;
11711 return braced-init-list ;
11712 goto identifier ;
11713
11714 GNU extension:
11715
11716 jump-statement:
11717 goto * expression ;
11718
11719 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11720
11721 static tree
11722 cp_parser_jump_statement (cp_parser* parser)
11723 {
11724 tree statement = error_mark_node;
11725 cp_token *token;
11726 enum rid keyword;
11727 unsigned char in_statement;
11728
11729 /* Peek at the next token. */
11730 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11731 if (!token)
11732 return error_mark_node;
11733
11734 /* See what kind of keyword it is. */
11735 keyword = token->keyword;
11736 switch (keyword)
11737 {
11738 case RID_BREAK:
11739 in_statement = parser->in_statement & ~IN_IF_STMT;
11740 switch (in_statement)
11741 {
11742 case 0:
11743 error_at (token->location, "break statement not within loop or switch");
11744 break;
11745 default:
11746 gcc_assert ((in_statement & IN_SWITCH_STMT)
11747 || in_statement == IN_ITERATION_STMT);
11748 statement = finish_break_stmt ();
11749 if (in_statement == IN_ITERATION_STMT)
11750 break_maybe_infinite_loop ();
11751 break;
11752 case IN_OMP_BLOCK:
11753 error_at (token->location, "invalid exit from OpenMP structured block");
11754 break;
11755 case IN_OMP_FOR:
11756 error_at (token->location, "break statement used with OpenMP for loop");
11757 break;
11758 case IN_CILK_SIMD_FOR:
11759 error_at (token->location, "break statement used with Cilk Plus for loop");
11760 break;
11761 }
11762 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11763 break;
11764
11765 case RID_CONTINUE:
11766 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11767 {
11768 case 0:
11769 error_at (token->location, "continue statement not within a loop");
11770 break;
11771 case IN_CILK_SIMD_FOR:
11772 error_at (token->location,
11773 "continue statement within %<#pragma simd%> loop body");
11774 /* Fall through. */
11775 case IN_ITERATION_STMT:
11776 case IN_OMP_FOR:
11777 statement = finish_continue_stmt ();
11778 break;
11779 case IN_OMP_BLOCK:
11780 error_at (token->location, "invalid exit from OpenMP structured block");
11781 break;
11782 default:
11783 gcc_unreachable ();
11784 }
11785 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11786 break;
11787
11788 case RID_RETURN:
11789 {
11790 tree expr;
11791 bool expr_non_constant_p;
11792
11793 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11794 {
11795 cp_lexer_set_source_position (parser->lexer);
11796 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11797 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11798 }
11799 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11800 expr = cp_parser_expression (parser);
11801 else
11802 /* If the next token is a `;', then there is no
11803 expression. */
11804 expr = NULL_TREE;
11805 /* Build the return-statement. */
11806 statement = finish_return_stmt (expr);
11807 /* Look for the final `;'. */
11808 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11809 }
11810 break;
11811
11812 case RID_GOTO:
11813 if (parser->in_function_body
11814 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11815 {
11816 error ("%<goto%> in %<constexpr%> function");
11817 cp_function_chain->invalid_constexpr = true;
11818 }
11819
11820 /* Create the goto-statement. */
11821 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11822 {
11823 /* Issue a warning about this use of a GNU extension. */
11824 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11825 /* Consume the '*' token. */
11826 cp_lexer_consume_token (parser->lexer);
11827 /* Parse the dependent expression. */
11828 finish_goto_stmt (cp_parser_expression (parser));
11829 }
11830 else
11831 finish_goto_stmt (cp_parser_identifier (parser));
11832 /* Look for the final `;'. */
11833 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11834 break;
11835
11836 default:
11837 cp_parser_error (parser, "expected jump-statement");
11838 break;
11839 }
11840
11841 return statement;
11842 }
11843
11844 /* Parse a declaration-statement.
11845
11846 declaration-statement:
11847 block-declaration */
11848
11849 static void
11850 cp_parser_declaration_statement (cp_parser* parser)
11851 {
11852 void *p;
11853
11854 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11855 p = obstack_alloc (&declarator_obstack, 0);
11856
11857 /* Parse the block-declaration. */
11858 cp_parser_block_declaration (parser, /*statement_p=*/true);
11859
11860 /* Free any declarators allocated. */
11861 obstack_free (&declarator_obstack, p);
11862 }
11863
11864 /* Some dependent statements (like `if (cond) statement'), are
11865 implicitly in their own scope. In other words, if the statement is
11866 a single statement (as opposed to a compound-statement), it is
11867 none-the-less treated as if it were enclosed in braces. Any
11868 declarations appearing in the dependent statement are out of scope
11869 after control passes that point. This function parses a statement,
11870 but ensures that is in its own scope, even if it is not a
11871 compound-statement.
11872
11873 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11874 is a (possibly labeled) if statement which is not enclosed in
11875 braces and has an else clause. This is used to implement
11876 -Wparentheses.
11877
11878 CHAIN is a vector of if-else-if conditions. This is used to implement
11879 -Wduplicated-cond.
11880
11881 Returns the new statement. */
11882
11883 static tree
11884 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11885 const token_indent_info &guard_tinfo,
11886 vec<tree> *chain)
11887 {
11888 tree statement;
11889 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11890 token_indent_info body_tinfo
11891 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11892
11893 if (if_p != NULL)
11894 *if_p = false;
11895
11896 /* Mark if () ; with a special NOP_EXPR. */
11897 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11898 {
11899 cp_lexer_consume_token (parser->lexer);
11900 statement = add_stmt (build_empty_stmt (body_loc));
11901
11902 if (guard_tinfo.keyword == RID_IF
11903 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11904 warning_at (body_loc, OPT_Wempty_body,
11905 "suggest braces around empty body in an %<if%> statement");
11906 else if (guard_tinfo.keyword == RID_ELSE)
11907 warning_at (body_loc, OPT_Wempty_body,
11908 "suggest braces around empty body in an %<else%> statement");
11909 }
11910 /* if a compound is opened, we simply parse the statement directly. */
11911 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11912 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11913 /* If the token is not a `{', then we must take special action. */
11914 else
11915 {
11916 /* Create a compound-statement. */
11917 statement = begin_compound_stmt (0);
11918 /* Parse the dependent-statement. */
11919 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11920 /* Finish the dummy compound-statement. */
11921 finish_compound_stmt (statement);
11922 }
11923
11924 token_indent_info next_tinfo
11925 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11926 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11927
11928 /* Return the statement. */
11929 return statement;
11930 }
11931
11932 /* For some dependent statements (like `while (cond) statement'), we
11933 have already created a scope. Therefore, even if the dependent
11934 statement is a compound-statement, we do not want to create another
11935 scope. */
11936
11937 static void
11938 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
11939 const token_indent_info &guard_tinfo)
11940 {
11941 /* If the token is a `{', then we must take special action. */
11942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11943 {
11944 token_indent_info body_tinfo
11945 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11946
11947 cp_parser_statement (parser, NULL_TREE, false, if_p);
11948 token_indent_info next_tinfo
11949 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11950 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11951 }
11952 else
11953 {
11954 /* Avoid calling cp_parser_compound_statement, so that we
11955 don't create a new scope. Do everything else by hand. */
11956 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11957 /* If the next keyword is `__label__' we have a label declaration. */
11958 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11959 cp_parser_label_declaration (parser);
11960 /* Parse an (optional) statement-seq. */
11961 cp_parser_statement_seq_opt (parser, NULL_TREE);
11962 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11963 }
11964 }
11965
11966 /* Declarations [gram.dcl.dcl] */
11967
11968 /* Parse an optional declaration-sequence.
11969
11970 declaration-seq:
11971 declaration
11972 declaration-seq declaration */
11973
11974 static void
11975 cp_parser_declaration_seq_opt (cp_parser* parser)
11976 {
11977 while (true)
11978 {
11979 cp_token *token;
11980
11981 token = cp_lexer_peek_token (parser->lexer);
11982
11983 if (token->type == CPP_CLOSE_BRACE
11984 || token->type == CPP_EOF
11985 || token->type == CPP_PRAGMA_EOL)
11986 break;
11987
11988 if (token->type == CPP_SEMICOLON)
11989 {
11990 /* A declaration consisting of a single semicolon is
11991 invalid. Allow it unless we're being pedantic. */
11992 cp_lexer_consume_token (parser->lexer);
11993 if (!in_system_header_at (input_location))
11994 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11995 continue;
11996 }
11997
11998 /* If we're entering or exiting a region that's implicitly
11999 extern "C", modify the lang context appropriately. */
12000 if (!parser->implicit_extern_c && token->implicit_extern_c)
12001 {
12002 push_lang_context (lang_name_c);
12003 parser->implicit_extern_c = true;
12004 }
12005 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12006 {
12007 pop_lang_context ();
12008 parser->implicit_extern_c = false;
12009 }
12010
12011 if (token->type == CPP_PRAGMA)
12012 {
12013 /* A top-level declaration can consist solely of a #pragma.
12014 A nested declaration cannot, so this is done here and not
12015 in cp_parser_declaration. (A #pragma at block scope is
12016 handled in cp_parser_statement.) */
12017 cp_parser_pragma (parser, pragma_external, NULL);
12018 continue;
12019 }
12020
12021 /* Parse the declaration itself. */
12022 cp_parser_declaration (parser);
12023 }
12024 }
12025
12026 /* Parse a declaration.
12027
12028 declaration:
12029 block-declaration
12030 function-definition
12031 template-declaration
12032 explicit-instantiation
12033 explicit-specialization
12034 linkage-specification
12035 namespace-definition
12036
12037 GNU extension:
12038
12039 declaration:
12040 __extension__ declaration */
12041
12042 static void
12043 cp_parser_declaration (cp_parser* parser)
12044 {
12045 cp_token token1;
12046 cp_token token2;
12047 int saved_pedantic;
12048 void *p;
12049 tree attributes = NULL_TREE;
12050
12051 /* Check for the `__extension__' keyword. */
12052 if (cp_parser_extension_opt (parser, &saved_pedantic))
12053 {
12054 /* Parse the qualified declaration. */
12055 cp_parser_declaration (parser);
12056 /* Restore the PEDANTIC flag. */
12057 pedantic = saved_pedantic;
12058
12059 return;
12060 }
12061
12062 /* Try to figure out what kind of declaration is present. */
12063 token1 = *cp_lexer_peek_token (parser->lexer);
12064
12065 if (token1.type != CPP_EOF)
12066 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12067 else
12068 {
12069 token2.type = CPP_EOF;
12070 token2.keyword = RID_MAX;
12071 }
12072
12073 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12074 p = obstack_alloc (&declarator_obstack, 0);
12075
12076 /* If the next token is `extern' and the following token is a string
12077 literal, then we have a linkage specification. */
12078 if (token1.keyword == RID_EXTERN
12079 && cp_parser_is_pure_string_literal (&token2))
12080 cp_parser_linkage_specification (parser);
12081 /* If the next token is `template', then we have either a template
12082 declaration, an explicit instantiation, or an explicit
12083 specialization. */
12084 else if (token1.keyword == RID_TEMPLATE)
12085 {
12086 /* `template <>' indicates a template specialization. */
12087 if (token2.type == CPP_LESS
12088 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12089 cp_parser_explicit_specialization (parser);
12090 /* `template <' indicates a template declaration. */
12091 else if (token2.type == CPP_LESS)
12092 cp_parser_template_declaration (parser, /*member_p=*/false);
12093 /* Anything else must be an explicit instantiation. */
12094 else
12095 cp_parser_explicit_instantiation (parser);
12096 }
12097 /* If the next token is `export', then we have a template
12098 declaration. */
12099 else if (token1.keyword == RID_EXPORT)
12100 cp_parser_template_declaration (parser, /*member_p=*/false);
12101 /* If the next token is `extern', 'static' or 'inline' and the one
12102 after that is `template', we have a GNU extended explicit
12103 instantiation directive. */
12104 else if (cp_parser_allow_gnu_extensions_p (parser)
12105 && (token1.keyword == RID_EXTERN
12106 || token1.keyword == RID_STATIC
12107 || token1.keyword == RID_INLINE)
12108 && token2.keyword == RID_TEMPLATE)
12109 cp_parser_explicit_instantiation (parser);
12110 /* If the next token is `namespace', check for a named or unnamed
12111 namespace definition. */
12112 else if (token1.keyword == RID_NAMESPACE
12113 && (/* A named namespace definition. */
12114 (token2.type == CPP_NAME
12115 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12116 != CPP_EQ))
12117 || (token2.type == CPP_OPEN_SQUARE
12118 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12119 == CPP_OPEN_SQUARE)
12120 /* An unnamed namespace definition. */
12121 || token2.type == CPP_OPEN_BRACE
12122 || token2.keyword == RID_ATTRIBUTE))
12123 cp_parser_namespace_definition (parser);
12124 /* An inline (associated) namespace definition. */
12125 else if (token1.keyword == RID_INLINE
12126 && token2.keyword == RID_NAMESPACE)
12127 cp_parser_namespace_definition (parser);
12128 /* Objective-C++ declaration/definition. */
12129 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12130 cp_parser_objc_declaration (parser, NULL_TREE);
12131 else if (c_dialect_objc ()
12132 && token1.keyword == RID_ATTRIBUTE
12133 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12134 cp_parser_objc_declaration (parser, attributes);
12135 /* At this point we may have a template declared by a concept
12136 introduction. */
12137 else if (flag_concepts
12138 && cp_parser_template_declaration_after_export (parser,
12139 /*member_p=*/false))
12140 /* We did. */;
12141 else
12142 /* Try to parse a block-declaration, or a function-definition. */
12143 cp_parser_block_declaration (parser, /*statement_p=*/false);
12144
12145 /* Free any declarators allocated. */
12146 obstack_free (&declarator_obstack, p);
12147 }
12148
12149 /* Parse a block-declaration.
12150
12151 block-declaration:
12152 simple-declaration
12153 asm-definition
12154 namespace-alias-definition
12155 using-declaration
12156 using-directive
12157
12158 GNU Extension:
12159
12160 block-declaration:
12161 __extension__ block-declaration
12162
12163 C++0x Extension:
12164
12165 block-declaration:
12166 static_assert-declaration
12167
12168 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12169 part of a declaration-statement. */
12170
12171 static void
12172 cp_parser_block_declaration (cp_parser *parser,
12173 bool statement_p)
12174 {
12175 cp_token *token1;
12176 int saved_pedantic;
12177
12178 /* Check for the `__extension__' keyword. */
12179 if (cp_parser_extension_opt (parser, &saved_pedantic))
12180 {
12181 /* Parse the qualified declaration. */
12182 cp_parser_block_declaration (parser, statement_p);
12183 /* Restore the PEDANTIC flag. */
12184 pedantic = saved_pedantic;
12185
12186 return;
12187 }
12188
12189 /* Peek at the next token to figure out which kind of declaration is
12190 present. */
12191 token1 = cp_lexer_peek_token (parser->lexer);
12192
12193 /* If the next keyword is `asm', we have an asm-definition. */
12194 if (token1->keyword == RID_ASM)
12195 {
12196 if (statement_p)
12197 cp_parser_commit_to_tentative_parse (parser);
12198 cp_parser_asm_definition (parser);
12199 }
12200 /* If the next keyword is `namespace', we have a
12201 namespace-alias-definition. */
12202 else if (token1->keyword == RID_NAMESPACE)
12203 cp_parser_namespace_alias_definition (parser);
12204 /* If the next keyword is `using', we have a
12205 using-declaration, a using-directive, or an alias-declaration. */
12206 else if (token1->keyword == RID_USING)
12207 {
12208 cp_token *token2;
12209
12210 if (statement_p)
12211 cp_parser_commit_to_tentative_parse (parser);
12212 /* If the token after `using' is `namespace', then we have a
12213 using-directive. */
12214 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12215 if (token2->keyword == RID_NAMESPACE)
12216 cp_parser_using_directive (parser);
12217 /* If the second token after 'using' is '=', then we have an
12218 alias-declaration. */
12219 else if (cxx_dialect >= cxx11
12220 && token2->type == CPP_NAME
12221 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12222 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12223 cp_parser_alias_declaration (parser);
12224 /* Otherwise, it's a using-declaration. */
12225 else
12226 cp_parser_using_declaration (parser,
12227 /*access_declaration_p=*/false);
12228 }
12229 /* If the next keyword is `__label__' we have a misplaced label
12230 declaration. */
12231 else if (token1->keyword == RID_LABEL)
12232 {
12233 cp_lexer_consume_token (parser->lexer);
12234 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12235 cp_parser_skip_to_end_of_statement (parser);
12236 /* If the next token is now a `;', consume it. */
12237 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12238 cp_lexer_consume_token (parser->lexer);
12239 }
12240 /* If the next token is `static_assert' we have a static assertion. */
12241 else if (token1->keyword == RID_STATIC_ASSERT)
12242 cp_parser_static_assert (parser, /*member_p=*/false);
12243 /* Anything else must be a simple-declaration. */
12244 else
12245 cp_parser_simple_declaration (parser, !statement_p,
12246 /*maybe_range_for_decl*/NULL);
12247 }
12248
12249 /* Parse a simple-declaration.
12250
12251 simple-declaration:
12252 decl-specifier-seq [opt] init-declarator-list [opt] ;
12253
12254 init-declarator-list:
12255 init-declarator
12256 init-declarator-list , init-declarator
12257
12258 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12259 function-definition as a simple-declaration.
12260
12261 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12262 parsed declaration if it is an uninitialized single declarator not followed
12263 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12264 if present, will not be consumed. */
12265
12266 static void
12267 cp_parser_simple_declaration (cp_parser* parser,
12268 bool function_definition_allowed_p,
12269 tree *maybe_range_for_decl)
12270 {
12271 cp_decl_specifier_seq decl_specifiers;
12272 int declares_class_or_enum;
12273 bool saw_declarator;
12274 location_t comma_loc = UNKNOWN_LOCATION;
12275 location_t init_loc = UNKNOWN_LOCATION;
12276
12277 if (maybe_range_for_decl)
12278 *maybe_range_for_decl = NULL_TREE;
12279
12280 /* Defer access checks until we know what is being declared; the
12281 checks for names appearing in the decl-specifier-seq should be
12282 done as if we were in the scope of the thing being declared. */
12283 push_deferring_access_checks (dk_deferred);
12284
12285 /* Parse the decl-specifier-seq. We have to keep track of whether
12286 or not the decl-specifier-seq declares a named class or
12287 enumeration type, since that is the only case in which the
12288 init-declarator-list is allowed to be empty.
12289
12290 [dcl.dcl]
12291
12292 In a simple-declaration, the optional init-declarator-list can be
12293 omitted only when declaring a class or enumeration, that is when
12294 the decl-specifier-seq contains either a class-specifier, an
12295 elaborated-type-specifier, or an enum-specifier. */
12296 cp_parser_decl_specifier_seq (parser,
12297 CP_PARSER_FLAGS_OPTIONAL,
12298 &decl_specifiers,
12299 &declares_class_or_enum);
12300 /* We no longer need to defer access checks. */
12301 stop_deferring_access_checks ();
12302
12303 /* In a block scope, a valid declaration must always have a
12304 decl-specifier-seq. By not trying to parse declarators, we can
12305 resolve the declaration/expression ambiguity more quickly. */
12306 if (!function_definition_allowed_p
12307 && !decl_specifiers.any_specifiers_p)
12308 {
12309 cp_parser_error (parser, "expected declaration");
12310 goto done;
12311 }
12312
12313 /* If the next two tokens are both identifiers, the code is
12314 erroneous. The usual cause of this situation is code like:
12315
12316 T t;
12317
12318 where "T" should name a type -- but does not. */
12319 if (!decl_specifiers.any_type_specifiers_p
12320 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12321 {
12322 /* If parsing tentatively, we should commit; we really are
12323 looking at a declaration. */
12324 cp_parser_commit_to_tentative_parse (parser);
12325 /* Give up. */
12326 goto done;
12327 }
12328
12329 /* If we have seen at least one decl-specifier, and the next token
12330 is not a parenthesis, then we must be looking at a declaration.
12331 (After "int (" we might be looking at a functional cast.) */
12332 if (decl_specifiers.any_specifiers_p
12333 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12334 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12335 && !cp_parser_error_occurred (parser))
12336 cp_parser_commit_to_tentative_parse (parser);
12337
12338 tree last_type;
12339
12340 last_type = NULL_TREE;
12341
12342 /* Keep going until we hit the `;' at the end of the simple
12343 declaration. */
12344 saw_declarator = false;
12345 while (cp_lexer_next_token_is_not (parser->lexer,
12346 CPP_SEMICOLON))
12347 {
12348 cp_token *token;
12349 bool function_definition_p;
12350 tree decl;
12351 tree auto_result = NULL_TREE;
12352
12353 if (saw_declarator)
12354 {
12355 /* If we are processing next declarator, comma is expected */
12356 token = cp_lexer_peek_token (parser->lexer);
12357 gcc_assert (token->type == CPP_COMMA);
12358 cp_lexer_consume_token (parser->lexer);
12359 if (maybe_range_for_decl)
12360 {
12361 *maybe_range_for_decl = error_mark_node;
12362 if (comma_loc == UNKNOWN_LOCATION)
12363 comma_loc = token->location;
12364 }
12365 }
12366 else
12367 saw_declarator = true;
12368
12369 /* Parse the init-declarator. */
12370 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12371 /*checks=*/NULL,
12372 function_definition_allowed_p,
12373 /*member_p=*/false,
12374 declares_class_or_enum,
12375 &function_definition_p,
12376 maybe_range_for_decl,
12377 &init_loc,
12378 &auto_result);
12379 /* If an error occurred while parsing tentatively, exit quickly.
12380 (That usually happens when in the body of a function; each
12381 statement is treated as a declaration-statement until proven
12382 otherwise.) */
12383 if (cp_parser_error_occurred (parser))
12384 goto done;
12385
12386 if (auto_result)
12387 {
12388 if (last_type && last_type != error_mark_node
12389 && !same_type_p (auto_result, last_type))
12390 {
12391 /* If the list of declarators contains more than one declarator,
12392 the type of each declared variable is determined as described
12393 above. If the type deduced for the template parameter U is not
12394 the same in each deduction, the program is ill-formed. */
12395 error_at (decl_specifiers.locations[ds_type_spec],
12396 "inconsistent deduction for %qT: %qT and then %qT",
12397 decl_specifiers.type, last_type, auto_result);
12398 last_type = error_mark_node;
12399 }
12400 else
12401 last_type = auto_result;
12402 }
12403
12404 /* Handle function definitions specially. */
12405 if (function_definition_p)
12406 {
12407 /* If the next token is a `,', then we are probably
12408 processing something like:
12409
12410 void f() {}, *p;
12411
12412 which is erroneous. */
12413 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12414 {
12415 cp_token *token = cp_lexer_peek_token (parser->lexer);
12416 error_at (token->location,
12417 "mixing"
12418 " declarations and function-definitions is forbidden");
12419 }
12420 /* Otherwise, we're done with the list of declarators. */
12421 else
12422 {
12423 pop_deferring_access_checks ();
12424 return;
12425 }
12426 }
12427 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12428 *maybe_range_for_decl = decl;
12429 /* The next token should be either a `,' or a `;'. */
12430 token = cp_lexer_peek_token (parser->lexer);
12431 /* If it's a `,', there are more declarators to come. */
12432 if (token->type == CPP_COMMA)
12433 /* will be consumed next time around */;
12434 /* If it's a `;', we are done. */
12435 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12436 break;
12437 /* Anything else is an error. */
12438 else
12439 {
12440 /* If we have already issued an error message we don't need
12441 to issue another one. */
12442 if ((decl != error_mark_node
12443 && DECL_INITIAL (decl) != error_mark_node)
12444 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12445 cp_parser_error (parser, "expected %<,%> or %<;%>");
12446 /* Skip tokens until we reach the end of the statement. */
12447 cp_parser_skip_to_end_of_statement (parser);
12448 /* If the next token is now a `;', consume it. */
12449 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12450 cp_lexer_consume_token (parser->lexer);
12451 goto done;
12452 }
12453 /* After the first time around, a function-definition is not
12454 allowed -- even if it was OK at first. For example:
12455
12456 int i, f() {}
12457
12458 is not valid. */
12459 function_definition_allowed_p = false;
12460 }
12461
12462 /* Issue an error message if no declarators are present, and the
12463 decl-specifier-seq does not itself declare a class or
12464 enumeration: [dcl.dcl]/3. */
12465 if (!saw_declarator)
12466 {
12467 if (cp_parser_declares_only_class_p (parser))
12468 {
12469 if (!declares_class_or_enum
12470 && decl_specifiers.type
12471 && OVERLOAD_TYPE_P (decl_specifiers.type))
12472 /* Ensure an error is issued anyway when finish_decltype_type,
12473 called via cp_parser_decl_specifier_seq, returns a class or
12474 an enumeration (c++/51786). */
12475 decl_specifiers.type = NULL_TREE;
12476 shadow_tag (&decl_specifiers);
12477 }
12478 /* Perform any deferred access checks. */
12479 perform_deferred_access_checks (tf_warning_or_error);
12480 }
12481
12482 /* Consume the `;'. */
12483 if (!maybe_range_for_decl)
12484 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12485 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12486 {
12487 if (init_loc != UNKNOWN_LOCATION)
12488 error_at (init_loc, "initializer in range-based %<for%> loop");
12489 if (comma_loc != UNKNOWN_LOCATION)
12490 error_at (comma_loc,
12491 "multiple declarations in range-based %<for%> loop");
12492 }
12493
12494 done:
12495 pop_deferring_access_checks ();
12496 }
12497
12498 /* Parse a decl-specifier-seq.
12499
12500 decl-specifier-seq:
12501 decl-specifier-seq [opt] decl-specifier
12502 decl-specifier attribute-specifier-seq [opt] (C++11)
12503
12504 decl-specifier:
12505 storage-class-specifier
12506 type-specifier
12507 function-specifier
12508 friend
12509 typedef
12510
12511 GNU Extension:
12512
12513 decl-specifier:
12514 attributes
12515
12516 Concepts Extension:
12517
12518 decl-specifier:
12519 concept
12520
12521 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12522
12523 The parser flags FLAGS is used to control type-specifier parsing.
12524
12525 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12526 flags:
12527
12528 1: one of the decl-specifiers is an elaborated-type-specifier
12529 (i.e., a type declaration)
12530 2: one of the decl-specifiers is an enum-specifier or a
12531 class-specifier (i.e., a type definition)
12532
12533 */
12534
12535 static void
12536 cp_parser_decl_specifier_seq (cp_parser* parser,
12537 cp_parser_flags flags,
12538 cp_decl_specifier_seq *decl_specs,
12539 int* declares_class_or_enum)
12540 {
12541 bool constructor_possible_p = !parser->in_declarator_p;
12542 bool found_decl_spec = false;
12543 cp_token *start_token = NULL;
12544 cp_decl_spec ds;
12545
12546 /* Clear DECL_SPECS. */
12547 clear_decl_specs (decl_specs);
12548
12549 /* Assume no class or enumeration type is declared. */
12550 *declares_class_or_enum = 0;
12551
12552 /* Keep reading specifiers until there are no more to read. */
12553 while (true)
12554 {
12555 bool constructor_p;
12556 cp_token *token;
12557 ds = ds_last;
12558
12559 /* Peek at the next token. */
12560 token = cp_lexer_peek_token (parser->lexer);
12561
12562 /* Save the first token of the decl spec list for error
12563 reporting. */
12564 if (!start_token)
12565 start_token = token;
12566 /* Handle attributes. */
12567 if (cp_next_tokens_can_be_attribute_p (parser))
12568 {
12569 /* Parse the attributes. */
12570 tree attrs = cp_parser_attributes_opt (parser);
12571
12572 /* In a sequence of declaration specifiers, c++11 attributes
12573 appertain to the type that precede them. In that case
12574 [dcl.spec]/1 says:
12575
12576 The attribute-specifier-seq affects the type only for
12577 the declaration it appears in, not other declarations
12578 involving the same type.
12579
12580 But for now let's force the user to position the
12581 attribute either at the beginning of the declaration or
12582 after the declarator-id, which would clearly mean that it
12583 applies to the declarator. */
12584 if (cxx11_attribute_p (attrs))
12585 {
12586 if (!found_decl_spec)
12587 /* The c++11 attribute is at the beginning of the
12588 declaration. It appertains to the entity being
12589 declared. */;
12590 else
12591 {
12592 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12593 {
12594 /* This is an attribute following a
12595 class-specifier. */
12596 if (decl_specs->type_definition_p)
12597 warn_misplaced_attr_for_class_type (token->location,
12598 decl_specs->type);
12599 attrs = NULL_TREE;
12600 }
12601 else
12602 {
12603 decl_specs->std_attributes
12604 = chainon (decl_specs->std_attributes,
12605 attrs);
12606 if (decl_specs->locations[ds_std_attribute] == 0)
12607 decl_specs->locations[ds_std_attribute] = token->location;
12608 }
12609 continue;
12610 }
12611 }
12612
12613 decl_specs->attributes
12614 = chainon (decl_specs->attributes,
12615 attrs);
12616 if (decl_specs->locations[ds_attribute] == 0)
12617 decl_specs->locations[ds_attribute] = token->location;
12618 continue;
12619 }
12620 /* Assume we will find a decl-specifier keyword. */
12621 found_decl_spec = true;
12622 /* If the next token is an appropriate keyword, we can simply
12623 add it to the list. */
12624 switch (token->keyword)
12625 {
12626 /* decl-specifier:
12627 friend
12628 constexpr */
12629 case RID_FRIEND:
12630 if (!at_class_scope_p ())
12631 {
12632 error_at (token->location, "%<friend%> used outside of class");
12633 cp_lexer_purge_token (parser->lexer);
12634 }
12635 else
12636 {
12637 ds = ds_friend;
12638 /* Consume the token. */
12639 cp_lexer_consume_token (parser->lexer);
12640 }
12641 break;
12642
12643 case RID_CONSTEXPR:
12644 ds = ds_constexpr;
12645 cp_lexer_consume_token (parser->lexer);
12646 break;
12647
12648 case RID_CONCEPT:
12649 ds = ds_concept;
12650 cp_lexer_consume_token (parser->lexer);
12651 break;
12652
12653 /* function-specifier:
12654 inline
12655 virtual
12656 explicit */
12657 case RID_INLINE:
12658 case RID_VIRTUAL:
12659 case RID_EXPLICIT:
12660 cp_parser_function_specifier_opt (parser, decl_specs);
12661 break;
12662
12663 /* decl-specifier:
12664 typedef */
12665 case RID_TYPEDEF:
12666 ds = ds_typedef;
12667 /* Consume the token. */
12668 cp_lexer_consume_token (parser->lexer);
12669 /* A constructor declarator cannot appear in a typedef. */
12670 constructor_possible_p = false;
12671 /* The "typedef" keyword can only occur in a declaration; we
12672 may as well commit at this point. */
12673 cp_parser_commit_to_tentative_parse (parser);
12674
12675 if (decl_specs->storage_class != sc_none)
12676 decl_specs->conflicting_specifiers_p = true;
12677 break;
12678
12679 /* storage-class-specifier:
12680 auto
12681 register
12682 static
12683 extern
12684 mutable
12685
12686 GNU Extension:
12687 thread */
12688 case RID_AUTO:
12689 if (cxx_dialect == cxx98)
12690 {
12691 /* Consume the token. */
12692 cp_lexer_consume_token (parser->lexer);
12693
12694 /* Complain about `auto' as a storage specifier, if
12695 we're complaining about C++0x compatibility. */
12696 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12697 " changes meaning in C++11; please remove it");
12698
12699 /* Set the storage class anyway. */
12700 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12701 token);
12702 }
12703 else
12704 /* C++0x auto type-specifier. */
12705 found_decl_spec = false;
12706 break;
12707
12708 case RID_REGISTER:
12709 case RID_STATIC:
12710 case RID_EXTERN:
12711 case RID_MUTABLE:
12712 /* Consume the token. */
12713 cp_lexer_consume_token (parser->lexer);
12714 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12715 token);
12716 break;
12717 case RID_THREAD:
12718 /* Consume the token. */
12719 ds = ds_thread;
12720 cp_lexer_consume_token (parser->lexer);
12721 break;
12722
12723 default:
12724 /* We did not yet find a decl-specifier yet. */
12725 found_decl_spec = false;
12726 break;
12727 }
12728
12729 if (found_decl_spec
12730 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12731 && token->keyword != RID_CONSTEXPR)
12732 error ("decl-specifier invalid in condition");
12733
12734 if (ds != ds_last)
12735 set_and_check_decl_spec_loc (decl_specs, ds, token);
12736
12737 /* Constructors are a special case. The `S' in `S()' is not a
12738 decl-specifier; it is the beginning of the declarator. */
12739 constructor_p
12740 = (!found_decl_spec
12741 && constructor_possible_p
12742 && (cp_parser_constructor_declarator_p
12743 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12744
12745 /* If we don't have a DECL_SPEC yet, then we must be looking at
12746 a type-specifier. */
12747 if (!found_decl_spec && !constructor_p)
12748 {
12749 int decl_spec_declares_class_or_enum;
12750 bool is_cv_qualifier;
12751 tree type_spec;
12752
12753 type_spec
12754 = cp_parser_type_specifier (parser, flags,
12755 decl_specs,
12756 /*is_declaration=*/true,
12757 &decl_spec_declares_class_or_enum,
12758 &is_cv_qualifier);
12759 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12760
12761 /* If this type-specifier referenced a user-defined type
12762 (a typedef, class-name, etc.), then we can't allow any
12763 more such type-specifiers henceforth.
12764
12765 [dcl.spec]
12766
12767 The longest sequence of decl-specifiers that could
12768 possibly be a type name is taken as the
12769 decl-specifier-seq of a declaration. The sequence shall
12770 be self-consistent as described below.
12771
12772 [dcl.type]
12773
12774 As a general rule, at most one type-specifier is allowed
12775 in the complete decl-specifier-seq of a declaration. The
12776 only exceptions are the following:
12777
12778 -- const or volatile can be combined with any other
12779 type-specifier.
12780
12781 -- signed or unsigned can be combined with char, long,
12782 short, or int.
12783
12784 -- ..
12785
12786 Example:
12787
12788 typedef char* Pc;
12789 void g (const int Pc);
12790
12791 Here, Pc is *not* part of the decl-specifier seq; it's
12792 the declarator. Therefore, once we see a type-specifier
12793 (other than a cv-qualifier), we forbid any additional
12794 user-defined types. We *do* still allow things like `int
12795 int' to be considered a decl-specifier-seq, and issue the
12796 error message later. */
12797 if (type_spec && !is_cv_qualifier)
12798 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12799 /* A constructor declarator cannot follow a type-specifier. */
12800 if (type_spec)
12801 {
12802 constructor_possible_p = false;
12803 found_decl_spec = true;
12804 if (!is_cv_qualifier)
12805 decl_specs->any_type_specifiers_p = true;
12806 }
12807 }
12808
12809 /* If we still do not have a DECL_SPEC, then there are no more
12810 decl-specifiers. */
12811 if (!found_decl_spec)
12812 break;
12813
12814 decl_specs->any_specifiers_p = true;
12815 /* After we see one decl-specifier, further decl-specifiers are
12816 always optional. */
12817 flags |= CP_PARSER_FLAGS_OPTIONAL;
12818 }
12819
12820 /* Don't allow a friend specifier with a class definition. */
12821 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12822 && (*declares_class_or_enum & 2))
12823 error_at (decl_specs->locations[ds_friend],
12824 "class definition may not be declared a friend");
12825 }
12826
12827 /* Parse an (optional) storage-class-specifier.
12828
12829 storage-class-specifier:
12830 auto
12831 register
12832 static
12833 extern
12834 mutable
12835
12836 GNU Extension:
12837
12838 storage-class-specifier:
12839 thread
12840
12841 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12842
12843 static tree
12844 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12845 {
12846 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12847 {
12848 case RID_AUTO:
12849 if (cxx_dialect != cxx98)
12850 return NULL_TREE;
12851 /* Fall through for C++98. */
12852
12853 case RID_REGISTER:
12854 case RID_STATIC:
12855 case RID_EXTERN:
12856 case RID_MUTABLE:
12857 case RID_THREAD:
12858 /* Consume the token. */
12859 return cp_lexer_consume_token (parser->lexer)->u.value;
12860
12861 default:
12862 return NULL_TREE;
12863 }
12864 }
12865
12866 /* Parse an (optional) function-specifier.
12867
12868 function-specifier:
12869 inline
12870 virtual
12871 explicit
12872
12873 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12874 Updates DECL_SPECS, if it is non-NULL. */
12875
12876 static tree
12877 cp_parser_function_specifier_opt (cp_parser* parser,
12878 cp_decl_specifier_seq *decl_specs)
12879 {
12880 cp_token *token = cp_lexer_peek_token (parser->lexer);
12881 switch (token->keyword)
12882 {
12883 case RID_INLINE:
12884 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12885 break;
12886
12887 case RID_VIRTUAL:
12888 /* 14.5.2.3 [temp.mem]
12889
12890 A member function template shall not be virtual. */
12891 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12892 error_at (token->location, "templates may not be %<virtual%>");
12893 else
12894 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12895 break;
12896
12897 case RID_EXPLICIT:
12898 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12899 break;
12900
12901 default:
12902 return NULL_TREE;
12903 }
12904
12905 /* Consume the token. */
12906 return cp_lexer_consume_token (parser->lexer)->u.value;
12907 }
12908
12909 /* Parse a linkage-specification.
12910
12911 linkage-specification:
12912 extern string-literal { declaration-seq [opt] }
12913 extern string-literal declaration */
12914
12915 static void
12916 cp_parser_linkage_specification (cp_parser* parser)
12917 {
12918 tree linkage;
12919
12920 /* Look for the `extern' keyword. */
12921 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12922
12923 /* Look for the string-literal. */
12924 linkage = cp_parser_string_literal (parser, false, false);
12925
12926 /* Transform the literal into an identifier. If the literal is a
12927 wide-character string, or contains embedded NULs, then we can't
12928 handle it as the user wants. */
12929 if (strlen (TREE_STRING_POINTER (linkage))
12930 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12931 {
12932 cp_parser_error (parser, "invalid linkage-specification");
12933 /* Assume C++ linkage. */
12934 linkage = lang_name_cplusplus;
12935 }
12936 else
12937 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12938
12939 /* We're now using the new linkage. */
12940 push_lang_context (linkage);
12941
12942 /* If the next token is a `{', then we're using the first
12943 production. */
12944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12945 {
12946 cp_ensure_no_omp_declare_simd (parser);
12947 cp_ensure_no_oacc_routine (parser);
12948
12949 /* Consume the `{' token. */
12950 cp_lexer_consume_token (parser->lexer);
12951 /* Parse the declarations. */
12952 cp_parser_declaration_seq_opt (parser);
12953 /* Look for the closing `}'. */
12954 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12955 }
12956 /* Otherwise, there's just one declaration. */
12957 else
12958 {
12959 bool saved_in_unbraced_linkage_specification_p;
12960
12961 saved_in_unbraced_linkage_specification_p
12962 = parser->in_unbraced_linkage_specification_p;
12963 parser->in_unbraced_linkage_specification_p = true;
12964 cp_parser_declaration (parser);
12965 parser->in_unbraced_linkage_specification_p
12966 = saved_in_unbraced_linkage_specification_p;
12967 }
12968
12969 /* We're done with the linkage-specification. */
12970 pop_lang_context ();
12971 }
12972
12973 /* Parse a static_assert-declaration.
12974
12975 static_assert-declaration:
12976 static_assert ( constant-expression , string-literal ) ;
12977 static_assert ( constant-expression ) ; (C++1Z)
12978
12979 If MEMBER_P, this static_assert is a class member. */
12980
12981 static void
12982 cp_parser_static_assert(cp_parser *parser, bool member_p)
12983 {
12984 tree condition;
12985 tree message;
12986 cp_token *token;
12987 location_t saved_loc;
12988 bool dummy;
12989
12990 /* Peek at the `static_assert' token so we can keep track of exactly
12991 where the static assertion started. */
12992 token = cp_lexer_peek_token (parser->lexer);
12993 saved_loc = token->location;
12994
12995 /* Look for the `static_assert' keyword. */
12996 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12997 RT_STATIC_ASSERT))
12998 return;
12999
13000 /* We know we are in a static assertion; commit to any tentative
13001 parse. */
13002 if (cp_parser_parsing_tentatively (parser))
13003 cp_parser_commit_to_tentative_parse (parser);
13004
13005 /* Parse the `(' starting the static assertion condition. */
13006 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13007
13008 /* Parse the constant-expression. Allow a non-constant expression
13009 here in order to give better diagnostics in finish_static_assert. */
13010 condition =
13011 cp_parser_constant_expression (parser,
13012 /*allow_non_constant_p=*/true,
13013 /*non_constant_p=*/&dummy);
13014
13015 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13016 {
13017 if (cxx_dialect < cxx1z)
13018 pedwarn (input_location, OPT_Wpedantic,
13019 "static_assert without a message "
13020 "only available with -std=c++1z or -std=gnu++1z");
13021 /* Eat the ')' */
13022 cp_lexer_consume_token (parser->lexer);
13023 message = build_string (1, "");
13024 TREE_TYPE (message) = char_array_type_node;
13025 fix_string_type (message);
13026 }
13027 else
13028 {
13029 /* Parse the separating `,'. */
13030 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13031
13032 /* Parse the string-literal message. */
13033 message = cp_parser_string_literal (parser,
13034 /*translate=*/false,
13035 /*wide_ok=*/true);
13036
13037 /* A `)' completes the static assertion. */
13038 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13039 cp_parser_skip_to_closing_parenthesis (parser,
13040 /*recovering=*/true,
13041 /*or_comma=*/false,
13042 /*consume_paren=*/true);
13043 }
13044
13045 /* A semicolon terminates the declaration. */
13046 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13047
13048 /* Complete the static assertion, which may mean either processing
13049 the static assert now or saving it for template instantiation. */
13050 finish_static_assert (condition, message, saved_loc, member_p);
13051 }
13052
13053 /* Parse the expression in decltype ( expression ). */
13054
13055 static tree
13056 cp_parser_decltype_expr (cp_parser *parser,
13057 bool &id_expression_or_member_access_p)
13058 {
13059 cp_token *id_expr_start_token;
13060 tree expr;
13061
13062 /* Since we're going to preserve any side-effects from this parse, set up a
13063 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13064 in the expression. */
13065 tentative_firewall firewall (parser);
13066
13067 /* First, try parsing an id-expression. */
13068 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13069 cp_parser_parse_tentatively (parser);
13070 expr = cp_parser_id_expression (parser,
13071 /*template_keyword_p=*/false,
13072 /*check_dependency_p=*/true,
13073 /*template_p=*/NULL,
13074 /*declarator_p=*/false,
13075 /*optional_p=*/false);
13076
13077 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13078 {
13079 bool non_integral_constant_expression_p = false;
13080 tree id_expression = expr;
13081 cp_id_kind idk;
13082 const char *error_msg;
13083
13084 if (identifier_p (expr))
13085 /* Lookup the name we got back from the id-expression. */
13086 expr = cp_parser_lookup_name_simple (parser, expr,
13087 id_expr_start_token->location);
13088
13089 if (expr
13090 && expr != error_mark_node
13091 && TREE_CODE (expr) != TYPE_DECL
13092 && (TREE_CODE (expr) != BIT_NOT_EXPR
13093 || !TYPE_P (TREE_OPERAND (expr, 0)))
13094 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13095 {
13096 /* Complete lookup of the id-expression. */
13097 expr = (finish_id_expression
13098 (id_expression, expr, parser->scope, &idk,
13099 /*integral_constant_expression_p=*/false,
13100 /*allow_non_integral_constant_expression_p=*/true,
13101 &non_integral_constant_expression_p,
13102 /*template_p=*/false,
13103 /*done=*/true,
13104 /*address_p=*/false,
13105 /*template_arg_p=*/false,
13106 &error_msg,
13107 id_expr_start_token->location));
13108
13109 if (expr == error_mark_node)
13110 /* We found an id-expression, but it was something that we
13111 should not have found. This is an error, not something
13112 we can recover from, so note that we found an
13113 id-expression and we'll recover as gracefully as
13114 possible. */
13115 id_expression_or_member_access_p = true;
13116 }
13117
13118 if (expr
13119 && expr != error_mark_node
13120 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13121 /* We have an id-expression. */
13122 id_expression_or_member_access_p = true;
13123 }
13124
13125 if (!id_expression_or_member_access_p)
13126 {
13127 /* Abort the id-expression parse. */
13128 cp_parser_abort_tentative_parse (parser);
13129
13130 /* Parsing tentatively, again. */
13131 cp_parser_parse_tentatively (parser);
13132
13133 /* Parse a class member access. */
13134 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13135 /*cast_p=*/false, /*decltype*/true,
13136 /*member_access_only_p=*/true, NULL);
13137
13138 if (expr
13139 && expr != error_mark_node
13140 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13141 /* We have an id-expression. */
13142 id_expression_or_member_access_p = true;
13143 }
13144
13145 if (id_expression_or_member_access_p)
13146 /* We have parsed the complete id-expression or member access. */
13147 cp_parser_parse_definitely (parser);
13148 else
13149 {
13150 /* Abort our attempt to parse an id-expression or member access
13151 expression. */
13152 cp_parser_abort_tentative_parse (parser);
13153
13154 /* Parse a full expression. */
13155 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13156 /*decltype_p=*/true);
13157 }
13158
13159 return expr;
13160 }
13161
13162 /* Parse a `decltype' type. Returns the type.
13163
13164 simple-type-specifier:
13165 decltype ( expression )
13166 C++14 proposal:
13167 decltype ( auto ) */
13168
13169 static tree
13170 cp_parser_decltype (cp_parser *parser)
13171 {
13172 tree expr;
13173 bool id_expression_or_member_access_p = false;
13174 const char *saved_message;
13175 bool saved_integral_constant_expression_p;
13176 bool saved_non_integral_constant_expression_p;
13177 bool saved_greater_than_is_operator_p;
13178 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13179
13180 if (start_token->type == CPP_DECLTYPE)
13181 {
13182 /* Already parsed. */
13183 cp_lexer_consume_token (parser->lexer);
13184 return saved_checks_value (start_token->u.tree_check_value);
13185 }
13186
13187 /* Look for the `decltype' token. */
13188 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13189 return error_mark_node;
13190
13191 /* Parse the opening `('. */
13192 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13193 return error_mark_node;
13194
13195 /* decltype (auto) */
13196 if (cxx_dialect >= cxx14
13197 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13198 {
13199 cp_lexer_consume_token (parser->lexer);
13200 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13201 return error_mark_node;
13202 expr = make_decltype_auto ();
13203 AUTO_IS_DECLTYPE (expr) = true;
13204 goto rewrite;
13205 }
13206
13207 /* Types cannot be defined in a `decltype' expression. Save away the
13208 old message. */
13209 saved_message = parser->type_definition_forbidden_message;
13210
13211 /* And create the new one. */
13212 parser->type_definition_forbidden_message
13213 = G_("types may not be defined in %<decltype%> expressions");
13214
13215 /* The restrictions on constant-expressions do not apply inside
13216 decltype expressions. */
13217 saved_integral_constant_expression_p
13218 = parser->integral_constant_expression_p;
13219 saved_non_integral_constant_expression_p
13220 = parser->non_integral_constant_expression_p;
13221 parser->integral_constant_expression_p = false;
13222
13223 /* Within a parenthesized expression, a `>' token is always
13224 the greater-than operator. */
13225 saved_greater_than_is_operator_p
13226 = parser->greater_than_is_operator_p;
13227 parser->greater_than_is_operator_p = true;
13228
13229 /* Do not actually evaluate the expression. */
13230 ++cp_unevaluated_operand;
13231
13232 /* Do not warn about problems with the expression. */
13233 ++c_inhibit_evaluation_warnings;
13234
13235 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13236
13237 /* Go back to evaluating expressions. */
13238 --cp_unevaluated_operand;
13239 --c_inhibit_evaluation_warnings;
13240
13241 /* The `>' token might be the end of a template-id or
13242 template-parameter-list now. */
13243 parser->greater_than_is_operator_p
13244 = saved_greater_than_is_operator_p;
13245
13246 /* Restore the old message and the integral constant expression
13247 flags. */
13248 parser->type_definition_forbidden_message = saved_message;
13249 parser->integral_constant_expression_p
13250 = saved_integral_constant_expression_p;
13251 parser->non_integral_constant_expression_p
13252 = saved_non_integral_constant_expression_p;
13253
13254 /* Parse to the closing `)'. */
13255 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13256 {
13257 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13258 /*consume_paren=*/true);
13259 return error_mark_node;
13260 }
13261
13262 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13263 tf_warning_or_error);
13264
13265 rewrite:
13266 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13267 it again. */
13268 start_token->type = CPP_DECLTYPE;
13269 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13270 start_token->u.tree_check_value->value = expr;
13271 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13272 start_token->keyword = RID_MAX;
13273 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13274
13275 return expr;
13276 }
13277
13278 /* Special member functions [gram.special] */
13279
13280 /* Parse a conversion-function-id.
13281
13282 conversion-function-id:
13283 operator conversion-type-id
13284
13285 Returns an IDENTIFIER_NODE representing the operator. */
13286
13287 static tree
13288 cp_parser_conversion_function_id (cp_parser* parser)
13289 {
13290 tree type;
13291 tree saved_scope;
13292 tree saved_qualifying_scope;
13293 tree saved_object_scope;
13294 tree pushed_scope = NULL_TREE;
13295
13296 /* Look for the `operator' token. */
13297 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13298 return error_mark_node;
13299 /* When we parse the conversion-type-id, the current scope will be
13300 reset. However, we need that information in able to look up the
13301 conversion function later, so we save it here. */
13302 saved_scope = parser->scope;
13303 saved_qualifying_scope = parser->qualifying_scope;
13304 saved_object_scope = parser->object_scope;
13305 /* We must enter the scope of the class so that the names of
13306 entities declared within the class are available in the
13307 conversion-type-id. For example, consider:
13308
13309 struct S {
13310 typedef int I;
13311 operator I();
13312 };
13313
13314 S::operator I() { ... }
13315
13316 In order to see that `I' is a type-name in the definition, we
13317 must be in the scope of `S'. */
13318 if (saved_scope)
13319 pushed_scope = push_scope (saved_scope);
13320 /* Parse the conversion-type-id. */
13321 type = cp_parser_conversion_type_id (parser);
13322 /* Leave the scope of the class, if any. */
13323 if (pushed_scope)
13324 pop_scope (pushed_scope);
13325 /* Restore the saved scope. */
13326 parser->scope = saved_scope;
13327 parser->qualifying_scope = saved_qualifying_scope;
13328 parser->object_scope = saved_object_scope;
13329 /* If the TYPE is invalid, indicate failure. */
13330 if (type == error_mark_node)
13331 return error_mark_node;
13332 return mangle_conv_op_name_for_type (type);
13333 }
13334
13335 /* Parse a conversion-type-id:
13336
13337 conversion-type-id:
13338 type-specifier-seq conversion-declarator [opt]
13339
13340 Returns the TYPE specified. */
13341
13342 static tree
13343 cp_parser_conversion_type_id (cp_parser* parser)
13344 {
13345 tree attributes;
13346 cp_decl_specifier_seq type_specifiers;
13347 cp_declarator *declarator;
13348 tree type_specified;
13349 const char *saved_message;
13350
13351 /* Parse the attributes. */
13352 attributes = cp_parser_attributes_opt (parser);
13353
13354 saved_message = parser->type_definition_forbidden_message;
13355 parser->type_definition_forbidden_message
13356 = G_("types may not be defined in a conversion-type-id");
13357
13358 /* Parse the type-specifiers. */
13359 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13360 /*is_trailing_return=*/false,
13361 &type_specifiers);
13362
13363 parser->type_definition_forbidden_message = saved_message;
13364
13365 /* If that didn't work, stop. */
13366 if (type_specifiers.type == error_mark_node)
13367 return error_mark_node;
13368 /* Parse the conversion-declarator. */
13369 declarator = cp_parser_conversion_declarator_opt (parser);
13370
13371 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13372 /*initialized=*/0, &attributes);
13373 if (attributes)
13374 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13375
13376 /* Don't give this error when parsing tentatively. This happens to
13377 work because we always parse this definitively once. */
13378 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13379 && type_uses_auto (type_specified))
13380 {
13381 if (cxx_dialect < cxx14)
13382 {
13383 error ("invalid use of %<auto%> in conversion operator");
13384 return error_mark_node;
13385 }
13386 else if (template_parm_scope_p ())
13387 warning (0, "use of %<auto%> in member template "
13388 "conversion operator can never be deduced");
13389 }
13390
13391 return type_specified;
13392 }
13393
13394 /* Parse an (optional) conversion-declarator.
13395
13396 conversion-declarator:
13397 ptr-operator conversion-declarator [opt]
13398
13399 */
13400
13401 static cp_declarator *
13402 cp_parser_conversion_declarator_opt (cp_parser* parser)
13403 {
13404 enum tree_code code;
13405 tree class_type, std_attributes = NULL_TREE;
13406 cp_cv_quals cv_quals;
13407
13408 /* We don't know if there's a ptr-operator next, or not. */
13409 cp_parser_parse_tentatively (parser);
13410 /* Try the ptr-operator. */
13411 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13412 &std_attributes);
13413 /* If it worked, look for more conversion-declarators. */
13414 if (cp_parser_parse_definitely (parser))
13415 {
13416 cp_declarator *declarator;
13417
13418 /* Parse another optional declarator. */
13419 declarator = cp_parser_conversion_declarator_opt (parser);
13420
13421 declarator = cp_parser_make_indirect_declarator
13422 (code, class_type, cv_quals, declarator, std_attributes);
13423
13424 return declarator;
13425 }
13426
13427 return NULL;
13428 }
13429
13430 /* Parse an (optional) ctor-initializer.
13431
13432 ctor-initializer:
13433 : mem-initializer-list
13434
13435 Returns TRUE iff the ctor-initializer was actually present. */
13436
13437 static bool
13438 cp_parser_ctor_initializer_opt (cp_parser* parser)
13439 {
13440 /* If the next token is not a `:', then there is no
13441 ctor-initializer. */
13442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13443 {
13444 /* Do default initialization of any bases and members. */
13445 if (DECL_CONSTRUCTOR_P (current_function_decl))
13446 finish_mem_initializers (NULL_TREE);
13447
13448 return false;
13449 }
13450
13451 /* Consume the `:' token. */
13452 cp_lexer_consume_token (parser->lexer);
13453 /* And the mem-initializer-list. */
13454 cp_parser_mem_initializer_list (parser);
13455
13456 return true;
13457 }
13458
13459 /* Parse a mem-initializer-list.
13460
13461 mem-initializer-list:
13462 mem-initializer ... [opt]
13463 mem-initializer ... [opt] , mem-initializer-list */
13464
13465 static void
13466 cp_parser_mem_initializer_list (cp_parser* parser)
13467 {
13468 tree mem_initializer_list = NULL_TREE;
13469 tree target_ctor = error_mark_node;
13470 cp_token *token = cp_lexer_peek_token (parser->lexer);
13471
13472 /* Let the semantic analysis code know that we are starting the
13473 mem-initializer-list. */
13474 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13475 error_at (token->location,
13476 "only constructors take member initializers");
13477
13478 /* Loop through the list. */
13479 while (true)
13480 {
13481 tree mem_initializer;
13482
13483 token = cp_lexer_peek_token (parser->lexer);
13484 /* Parse the mem-initializer. */
13485 mem_initializer = cp_parser_mem_initializer (parser);
13486 /* If the next token is a `...', we're expanding member initializers. */
13487 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13488 {
13489 /* Consume the `...'. */
13490 cp_lexer_consume_token (parser->lexer);
13491
13492 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13493 can be expanded but members cannot. */
13494 if (mem_initializer != error_mark_node
13495 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13496 {
13497 error_at (token->location,
13498 "cannot expand initializer for member %<%D%>",
13499 TREE_PURPOSE (mem_initializer));
13500 mem_initializer = error_mark_node;
13501 }
13502
13503 /* Construct the pack expansion type. */
13504 if (mem_initializer != error_mark_node)
13505 mem_initializer = make_pack_expansion (mem_initializer);
13506 }
13507 if (target_ctor != error_mark_node
13508 && mem_initializer != error_mark_node)
13509 {
13510 error ("mem-initializer for %qD follows constructor delegation",
13511 TREE_PURPOSE (mem_initializer));
13512 mem_initializer = error_mark_node;
13513 }
13514 /* Look for a target constructor. */
13515 if (mem_initializer != error_mark_node
13516 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13517 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13518 {
13519 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13520 if (mem_initializer_list)
13521 {
13522 error ("constructor delegation follows mem-initializer for %qD",
13523 TREE_PURPOSE (mem_initializer_list));
13524 mem_initializer = error_mark_node;
13525 }
13526 target_ctor = mem_initializer;
13527 }
13528 /* Add it to the list, unless it was erroneous. */
13529 if (mem_initializer != error_mark_node)
13530 {
13531 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13532 mem_initializer_list = mem_initializer;
13533 }
13534 /* If the next token is not a `,', we're done. */
13535 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13536 break;
13537 /* Consume the `,' token. */
13538 cp_lexer_consume_token (parser->lexer);
13539 }
13540
13541 /* Perform semantic analysis. */
13542 if (DECL_CONSTRUCTOR_P (current_function_decl))
13543 finish_mem_initializers (mem_initializer_list);
13544 }
13545
13546 /* Parse a mem-initializer.
13547
13548 mem-initializer:
13549 mem-initializer-id ( expression-list [opt] )
13550 mem-initializer-id braced-init-list
13551
13552 GNU extension:
13553
13554 mem-initializer:
13555 ( expression-list [opt] )
13556
13557 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13558 class) or FIELD_DECL (for a non-static data member) to initialize;
13559 the TREE_VALUE is the expression-list. An empty initialization
13560 list is represented by void_list_node. */
13561
13562 static tree
13563 cp_parser_mem_initializer (cp_parser* parser)
13564 {
13565 tree mem_initializer_id;
13566 tree expression_list;
13567 tree member;
13568 cp_token *token = cp_lexer_peek_token (parser->lexer);
13569
13570 /* Find out what is being initialized. */
13571 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13572 {
13573 permerror (token->location,
13574 "anachronistic old-style base class initializer");
13575 mem_initializer_id = NULL_TREE;
13576 }
13577 else
13578 {
13579 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13580 if (mem_initializer_id == error_mark_node)
13581 return mem_initializer_id;
13582 }
13583 member = expand_member_init (mem_initializer_id);
13584 if (member && !DECL_P (member))
13585 in_base_initializer = 1;
13586
13587 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13588 {
13589 bool expr_non_constant_p;
13590 cp_lexer_set_source_position (parser->lexer);
13591 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13592 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13593 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13594 expression_list = build_tree_list (NULL_TREE, expression_list);
13595 }
13596 else
13597 {
13598 vec<tree, va_gc> *vec;
13599 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13600 /*cast_p=*/false,
13601 /*allow_expansion_p=*/true,
13602 /*non_constant_p=*/NULL);
13603 if (vec == NULL)
13604 return error_mark_node;
13605 expression_list = build_tree_list_vec (vec);
13606 release_tree_vector (vec);
13607 }
13608
13609 if (expression_list == error_mark_node)
13610 return error_mark_node;
13611 if (!expression_list)
13612 expression_list = void_type_node;
13613
13614 in_base_initializer = 0;
13615
13616 return member ? build_tree_list (member, expression_list) : error_mark_node;
13617 }
13618
13619 /* Parse a mem-initializer-id.
13620
13621 mem-initializer-id:
13622 :: [opt] nested-name-specifier [opt] class-name
13623 decltype-specifier (C++11)
13624 identifier
13625
13626 Returns a TYPE indicating the class to be initialized for the first
13627 production (and the second in C++11). Returns an IDENTIFIER_NODE
13628 indicating the data member to be initialized for the last production. */
13629
13630 static tree
13631 cp_parser_mem_initializer_id (cp_parser* parser)
13632 {
13633 bool global_scope_p;
13634 bool nested_name_specifier_p;
13635 bool template_p = false;
13636 tree id;
13637
13638 cp_token *token = cp_lexer_peek_token (parser->lexer);
13639
13640 /* `typename' is not allowed in this context ([temp.res]). */
13641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13642 {
13643 error_at (token->location,
13644 "keyword %<typename%> not allowed in this context (a qualified "
13645 "member initializer is implicitly a type)");
13646 cp_lexer_consume_token (parser->lexer);
13647 }
13648 /* Look for the optional `::' operator. */
13649 global_scope_p
13650 = (cp_parser_global_scope_opt (parser,
13651 /*current_scope_valid_p=*/false)
13652 != NULL_TREE);
13653 /* Look for the optional nested-name-specifier. The simplest way to
13654 implement:
13655
13656 [temp.res]
13657
13658 The keyword `typename' is not permitted in a base-specifier or
13659 mem-initializer; in these contexts a qualified name that
13660 depends on a template-parameter is implicitly assumed to be a
13661 type name.
13662
13663 is to assume that we have seen the `typename' keyword at this
13664 point. */
13665 nested_name_specifier_p
13666 = (cp_parser_nested_name_specifier_opt (parser,
13667 /*typename_keyword_p=*/true,
13668 /*check_dependency_p=*/true,
13669 /*type_p=*/true,
13670 /*is_declaration=*/true)
13671 != NULL_TREE);
13672 if (nested_name_specifier_p)
13673 template_p = cp_parser_optional_template_keyword (parser);
13674 /* If there is a `::' operator or a nested-name-specifier, then we
13675 are definitely looking for a class-name. */
13676 if (global_scope_p || nested_name_specifier_p)
13677 return cp_parser_class_name (parser,
13678 /*typename_keyword_p=*/true,
13679 /*template_keyword_p=*/template_p,
13680 typename_type,
13681 /*check_dependency_p=*/true,
13682 /*class_head_p=*/false,
13683 /*is_declaration=*/true);
13684 /* Otherwise, we could also be looking for an ordinary identifier. */
13685 cp_parser_parse_tentatively (parser);
13686 if (cp_lexer_next_token_is_decltype (parser->lexer))
13687 /* Try a decltype-specifier. */
13688 id = cp_parser_decltype (parser);
13689 else
13690 /* Otherwise, try a class-name. */
13691 id = cp_parser_class_name (parser,
13692 /*typename_keyword_p=*/true,
13693 /*template_keyword_p=*/false,
13694 none_type,
13695 /*check_dependency_p=*/true,
13696 /*class_head_p=*/false,
13697 /*is_declaration=*/true);
13698 /* If we found one, we're done. */
13699 if (cp_parser_parse_definitely (parser))
13700 return id;
13701 /* Otherwise, look for an ordinary identifier. */
13702 return cp_parser_identifier (parser);
13703 }
13704
13705 /* Overloading [gram.over] */
13706
13707 /* Parse an operator-function-id.
13708
13709 operator-function-id:
13710 operator operator
13711
13712 Returns an IDENTIFIER_NODE for the operator which is a
13713 human-readable spelling of the identifier, e.g., `operator +'. */
13714
13715 static cp_expr
13716 cp_parser_operator_function_id (cp_parser* parser)
13717 {
13718 /* Look for the `operator' keyword. */
13719 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13720 return error_mark_node;
13721 /* And then the name of the operator itself. */
13722 return cp_parser_operator (parser);
13723 }
13724
13725 /* Return an identifier node for a user-defined literal operator.
13726 The suffix identifier is chained to the operator name identifier. */
13727
13728 static tree
13729 cp_literal_operator_id (const char* name)
13730 {
13731 tree identifier;
13732 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13733 + strlen (name) + 10);
13734 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13735 identifier = get_identifier (buffer);
13736
13737 return identifier;
13738 }
13739
13740 /* Parse an operator.
13741
13742 operator:
13743 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13744 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13745 || ++ -- , ->* -> () []
13746
13747 GNU Extensions:
13748
13749 operator:
13750 <? >? <?= >?=
13751
13752 Returns an IDENTIFIER_NODE for the operator which is a
13753 human-readable spelling of the identifier, e.g., `operator +'. */
13754
13755 static cp_expr
13756 cp_parser_operator (cp_parser* parser)
13757 {
13758 tree id = NULL_TREE;
13759 cp_token *token;
13760 bool utf8 = false;
13761
13762 /* Peek at the next token. */
13763 token = cp_lexer_peek_token (parser->lexer);
13764
13765 location_t start_loc = token->location;
13766
13767 /* Figure out which operator we have. */
13768 switch (token->type)
13769 {
13770 case CPP_KEYWORD:
13771 {
13772 enum tree_code op;
13773
13774 /* The keyword should be either `new' or `delete'. */
13775 if (token->keyword == RID_NEW)
13776 op = NEW_EXPR;
13777 else if (token->keyword == RID_DELETE)
13778 op = DELETE_EXPR;
13779 else
13780 break;
13781
13782 /* Consume the `new' or `delete' token. */
13783 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13784
13785 /* Peek at the next token. */
13786 token = cp_lexer_peek_token (parser->lexer);
13787 /* If it's a `[' token then this is the array variant of the
13788 operator. */
13789 if (token->type == CPP_OPEN_SQUARE)
13790 {
13791 /* Consume the `[' token. */
13792 cp_lexer_consume_token (parser->lexer);
13793 /* Look for the `]' token. */
13794 if (cp_token *close_token
13795 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13796 end_loc = close_token->location;
13797 id = ansi_opname (op == NEW_EXPR
13798 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13799 }
13800 /* Otherwise, we have the non-array variant. */
13801 else
13802 id = ansi_opname (op);
13803
13804 location_t loc = make_location (start_loc, start_loc, end_loc);
13805
13806 return cp_expr (id, loc);
13807 }
13808
13809 case CPP_PLUS:
13810 id = ansi_opname (PLUS_EXPR);
13811 break;
13812
13813 case CPP_MINUS:
13814 id = ansi_opname (MINUS_EXPR);
13815 break;
13816
13817 case CPP_MULT:
13818 id = ansi_opname (MULT_EXPR);
13819 break;
13820
13821 case CPP_DIV:
13822 id = ansi_opname (TRUNC_DIV_EXPR);
13823 break;
13824
13825 case CPP_MOD:
13826 id = ansi_opname (TRUNC_MOD_EXPR);
13827 break;
13828
13829 case CPP_XOR:
13830 id = ansi_opname (BIT_XOR_EXPR);
13831 break;
13832
13833 case CPP_AND:
13834 id = ansi_opname (BIT_AND_EXPR);
13835 break;
13836
13837 case CPP_OR:
13838 id = ansi_opname (BIT_IOR_EXPR);
13839 break;
13840
13841 case CPP_COMPL:
13842 id = ansi_opname (BIT_NOT_EXPR);
13843 break;
13844
13845 case CPP_NOT:
13846 id = ansi_opname (TRUTH_NOT_EXPR);
13847 break;
13848
13849 case CPP_EQ:
13850 id = ansi_assopname (NOP_EXPR);
13851 break;
13852
13853 case CPP_LESS:
13854 id = ansi_opname (LT_EXPR);
13855 break;
13856
13857 case CPP_GREATER:
13858 id = ansi_opname (GT_EXPR);
13859 break;
13860
13861 case CPP_PLUS_EQ:
13862 id = ansi_assopname (PLUS_EXPR);
13863 break;
13864
13865 case CPP_MINUS_EQ:
13866 id = ansi_assopname (MINUS_EXPR);
13867 break;
13868
13869 case CPP_MULT_EQ:
13870 id = ansi_assopname (MULT_EXPR);
13871 break;
13872
13873 case CPP_DIV_EQ:
13874 id = ansi_assopname (TRUNC_DIV_EXPR);
13875 break;
13876
13877 case CPP_MOD_EQ:
13878 id = ansi_assopname (TRUNC_MOD_EXPR);
13879 break;
13880
13881 case CPP_XOR_EQ:
13882 id = ansi_assopname (BIT_XOR_EXPR);
13883 break;
13884
13885 case CPP_AND_EQ:
13886 id = ansi_assopname (BIT_AND_EXPR);
13887 break;
13888
13889 case CPP_OR_EQ:
13890 id = ansi_assopname (BIT_IOR_EXPR);
13891 break;
13892
13893 case CPP_LSHIFT:
13894 id = ansi_opname (LSHIFT_EXPR);
13895 break;
13896
13897 case CPP_RSHIFT:
13898 id = ansi_opname (RSHIFT_EXPR);
13899 break;
13900
13901 case CPP_LSHIFT_EQ:
13902 id = ansi_assopname (LSHIFT_EXPR);
13903 break;
13904
13905 case CPP_RSHIFT_EQ:
13906 id = ansi_assopname (RSHIFT_EXPR);
13907 break;
13908
13909 case CPP_EQ_EQ:
13910 id = ansi_opname (EQ_EXPR);
13911 break;
13912
13913 case CPP_NOT_EQ:
13914 id = ansi_opname (NE_EXPR);
13915 break;
13916
13917 case CPP_LESS_EQ:
13918 id = ansi_opname (LE_EXPR);
13919 break;
13920
13921 case CPP_GREATER_EQ:
13922 id = ansi_opname (GE_EXPR);
13923 break;
13924
13925 case CPP_AND_AND:
13926 id = ansi_opname (TRUTH_ANDIF_EXPR);
13927 break;
13928
13929 case CPP_OR_OR:
13930 id = ansi_opname (TRUTH_ORIF_EXPR);
13931 break;
13932
13933 case CPP_PLUS_PLUS:
13934 id = ansi_opname (POSTINCREMENT_EXPR);
13935 break;
13936
13937 case CPP_MINUS_MINUS:
13938 id = ansi_opname (PREDECREMENT_EXPR);
13939 break;
13940
13941 case CPP_COMMA:
13942 id = ansi_opname (COMPOUND_EXPR);
13943 break;
13944
13945 case CPP_DEREF_STAR:
13946 id = ansi_opname (MEMBER_REF);
13947 break;
13948
13949 case CPP_DEREF:
13950 id = ansi_opname (COMPONENT_REF);
13951 break;
13952
13953 case CPP_OPEN_PAREN:
13954 /* Consume the `('. */
13955 cp_lexer_consume_token (parser->lexer);
13956 /* Look for the matching `)'. */
13957 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13958 return ansi_opname (CALL_EXPR);
13959
13960 case CPP_OPEN_SQUARE:
13961 /* Consume the `['. */
13962 cp_lexer_consume_token (parser->lexer);
13963 /* Look for the matching `]'. */
13964 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13965 return ansi_opname (ARRAY_REF);
13966
13967 case CPP_UTF8STRING:
13968 case CPP_UTF8STRING_USERDEF:
13969 utf8 = true;
13970 case CPP_STRING:
13971 case CPP_WSTRING:
13972 case CPP_STRING16:
13973 case CPP_STRING32:
13974 case CPP_STRING_USERDEF:
13975 case CPP_WSTRING_USERDEF:
13976 case CPP_STRING16_USERDEF:
13977 case CPP_STRING32_USERDEF:
13978 {
13979 tree str, string_tree;
13980 int sz, len;
13981
13982 if (cxx_dialect == cxx98)
13983 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13984
13985 /* Consume the string. */
13986 str = cp_parser_string_literal (parser, /*translate=*/true,
13987 /*wide_ok=*/true, /*lookup_udlit=*/false);
13988 if (str == error_mark_node)
13989 return error_mark_node;
13990 else if (TREE_CODE (str) == USERDEF_LITERAL)
13991 {
13992 string_tree = USERDEF_LITERAL_VALUE (str);
13993 id = USERDEF_LITERAL_SUFFIX_ID (str);
13994 }
13995 else
13996 {
13997 string_tree = str;
13998 /* Look for the suffix identifier. */
13999 token = cp_lexer_peek_token (parser->lexer);
14000 if (token->type == CPP_NAME)
14001 id = cp_parser_identifier (parser);
14002 else if (token->type == CPP_KEYWORD)
14003 {
14004 error ("unexpected keyword;"
14005 " remove space between quotes and suffix identifier");
14006 return error_mark_node;
14007 }
14008 else
14009 {
14010 error ("expected suffix identifier");
14011 return error_mark_node;
14012 }
14013 }
14014 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14015 (TREE_TYPE (TREE_TYPE (string_tree))));
14016 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14017 if (len != 0)
14018 {
14019 error ("expected empty string after %<operator%> keyword");
14020 return error_mark_node;
14021 }
14022 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14023 != char_type_node)
14024 {
14025 error ("invalid encoding prefix in literal operator");
14026 return error_mark_node;
14027 }
14028 if (id != error_mark_node)
14029 {
14030 const char *name = IDENTIFIER_POINTER (id);
14031 id = cp_literal_operator_id (name);
14032 }
14033 return id;
14034 }
14035
14036 default:
14037 /* Anything else is an error. */
14038 break;
14039 }
14040
14041 /* If we have selected an identifier, we need to consume the
14042 operator token. */
14043 if (id)
14044 cp_lexer_consume_token (parser->lexer);
14045 /* Otherwise, no valid operator name was present. */
14046 else
14047 {
14048 cp_parser_error (parser, "expected operator");
14049 id = error_mark_node;
14050 }
14051
14052 return cp_expr (id, start_loc);
14053 }
14054
14055 /* Parse a template-declaration.
14056
14057 template-declaration:
14058 export [opt] template < template-parameter-list > declaration
14059
14060 If MEMBER_P is TRUE, this template-declaration occurs within a
14061 class-specifier.
14062
14063 The grammar rule given by the standard isn't correct. What
14064 is really meant is:
14065
14066 template-declaration:
14067 export [opt] template-parameter-list-seq
14068 decl-specifier-seq [opt] init-declarator [opt] ;
14069 export [opt] template-parameter-list-seq
14070 function-definition
14071
14072 template-parameter-list-seq:
14073 template-parameter-list-seq [opt]
14074 template < template-parameter-list >
14075
14076 Concept Extensions:
14077
14078 template-parameter-list-seq:
14079 template < template-parameter-list > requires-clause [opt]
14080
14081 requires-clause:
14082 requires logical-or-expression */
14083
14084 static void
14085 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14086 {
14087 /* Check for `export'. */
14088 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14089 {
14090 /* Consume the `export' token. */
14091 cp_lexer_consume_token (parser->lexer);
14092 /* Warn that we do not support `export'. */
14093 warning (0, "keyword %<export%> not implemented, and will be ignored");
14094 }
14095
14096 cp_parser_template_declaration_after_export (parser, member_p);
14097 }
14098
14099 /* Parse a template-parameter-list.
14100
14101 template-parameter-list:
14102 template-parameter
14103 template-parameter-list , template-parameter
14104
14105 Returns a TREE_LIST. Each node represents a template parameter.
14106 The nodes are connected via their TREE_CHAINs. */
14107
14108 static tree
14109 cp_parser_template_parameter_list (cp_parser* parser)
14110 {
14111 tree parameter_list = NULL_TREE;
14112
14113 begin_template_parm_list ();
14114
14115 /* The loop below parses the template parms. We first need to know
14116 the total number of template parms to be able to compute proper
14117 canonical types of each dependent type. So after the loop, when
14118 we know the total number of template parms,
14119 end_template_parm_list computes the proper canonical types and
14120 fixes up the dependent types accordingly. */
14121 while (true)
14122 {
14123 tree parameter;
14124 bool is_non_type;
14125 bool is_parameter_pack;
14126 location_t parm_loc;
14127
14128 /* Parse the template-parameter. */
14129 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14130 parameter = cp_parser_template_parameter (parser,
14131 &is_non_type,
14132 &is_parameter_pack);
14133 /* Add it to the list. */
14134 if (parameter != error_mark_node)
14135 parameter_list = process_template_parm (parameter_list,
14136 parm_loc,
14137 parameter,
14138 is_non_type,
14139 is_parameter_pack);
14140 else
14141 {
14142 tree err_parm = build_tree_list (parameter, parameter);
14143 parameter_list = chainon (parameter_list, err_parm);
14144 }
14145
14146 /* If the next token is not a `,', we're done. */
14147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14148 break;
14149 /* Otherwise, consume the `,' token. */
14150 cp_lexer_consume_token (parser->lexer);
14151 }
14152
14153 return end_template_parm_list (parameter_list);
14154 }
14155
14156 /* Parse a introduction-list.
14157
14158 introduction-list:
14159 introduced-parameter
14160 introduction-list , introduced-parameter
14161
14162 introduced-parameter:
14163 ...[opt] identifier
14164
14165 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14166 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14167 WILDCARD_DECL will also have DECL_NAME set and token location in
14168 DECL_SOURCE_LOCATION. */
14169
14170 static tree
14171 cp_parser_introduction_list (cp_parser *parser)
14172 {
14173 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14174
14175 while (true)
14176 {
14177 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14178 if (is_pack)
14179 cp_lexer_consume_token (parser->lexer);
14180
14181 /* Build placeholder. */
14182 tree parm = build_nt (WILDCARD_DECL);
14183 DECL_SOURCE_LOCATION (parm)
14184 = cp_lexer_peek_token (parser->lexer)->location;
14185 DECL_NAME (parm) = cp_parser_identifier (parser);
14186 WILDCARD_PACK_P (parm) = is_pack;
14187 vec_safe_push (introduction_vec, parm);
14188
14189 /* If the next token is not a `,', we're done. */
14190 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14191 break;
14192 /* Otherwise, consume the `,' token. */
14193 cp_lexer_consume_token (parser->lexer);
14194 }
14195
14196 /* Convert the vec into a TREE_VEC. */
14197 tree introduction_list = make_tree_vec (introduction_vec->length ());
14198 unsigned int n;
14199 tree parm;
14200 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14201 TREE_VEC_ELT (introduction_list, n) = parm;
14202
14203 release_tree_vector (introduction_vec);
14204 return introduction_list;
14205 }
14206
14207 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14208 is an abstract declarator. */
14209
14210 static inline cp_declarator*
14211 get_id_declarator (cp_declarator *declarator)
14212 {
14213 cp_declarator *d = declarator;
14214 while (d && d->kind != cdk_id)
14215 d = d->declarator;
14216 return d;
14217 }
14218
14219 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14220 is an abstract declarator. */
14221
14222 static inline tree
14223 get_unqualified_id (cp_declarator *declarator)
14224 {
14225 declarator = get_id_declarator (declarator);
14226 if (declarator)
14227 return declarator->u.id.unqualified_name;
14228 else
14229 return NULL_TREE;
14230 }
14231
14232 /* Returns true if DECL represents a constrained-parameter. */
14233
14234 static inline bool
14235 is_constrained_parameter (tree decl)
14236 {
14237 return (decl
14238 && TREE_CODE (decl) == TYPE_DECL
14239 && CONSTRAINED_PARM_CONCEPT (decl)
14240 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14241 }
14242
14243 /* Returns true if PARM declares a constrained-parameter. */
14244
14245 static inline bool
14246 is_constrained_parameter (cp_parameter_declarator *parm)
14247 {
14248 return is_constrained_parameter (parm->decl_specifiers.type);
14249 }
14250
14251 /* Check that the type parameter is only a declarator-id, and that its
14252 type is not cv-qualified. */
14253
14254 bool
14255 cp_parser_check_constrained_type_parm (cp_parser *parser,
14256 cp_parameter_declarator *parm)
14257 {
14258 if (!parm->declarator)
14259 return true;
14260
14261 if (parm->declarator->kind != cdk_id)
14262 {
14263 cp_parser_error (parser, "invalid constrained type parameter");
14264 return false;
14265 }
14266
14267 /* Don't allow cv-qualified type parameters. */
14268 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14269 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14270 {
14271 cp_parser_error (parser, "cv-qualified type parameter");
14272 return false;
14273 }
14274
14275 return true;
14276 }
14277
14278 /* Finish parsing/processing a template type parameter and checking
14279 various restrictions. */
14280
14281 static inline tree
14282 cp_parser_constrained_type_template_parm (cp_parser *parser,
14283 tree id,
14284 cp_parameter_declarator* parmdecl)
14285 {
14286 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14287 return finish_template_type_parm (class_type_node, id);
14288 else
14289 return error_mark_node;
14290 }
14291
14292 static tree
14293 finish_constrained_template_template_parm (tree proto, tree id)
14294 {
14295 /* FIXME: This should probably be copied, and we may need to adjust
14296 the template parameter depths. */
14297 tree saved_parms = current_template_parms;
14298 begin_template_parm_list ();
14299 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14300 end_template_parm_list ();
14301
14302 tree parm = finish_template_template_parm (class_type_node, id);
14303 current_template_parms = saved_parms;
14304
14305 return parm;
14306 }
14307
14308 /* Finish parsing/processing a template template parameter by borrowing
14309 the template parameter list from the prototype parameter. */
14310
14311 static tree
14312 cp_parser_constrained_template_template_parm (cp_parser *parser,
14313 tree proto,
14314 tree id,
14315 cp_parameter_declarator *parmdecl)
14316 {
14317 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14318 return error_mark_node;
14319 return finish_constrained_template_template_parm (proto, id);
14320 }
14321
14322 /* Create a new non-type template parameter from the given PARM
14323 declarator. */
14324
14325 static tree
14326 constrained_non_type_template_parm (bool *is_non_type,
14327 cp_parameter_declarator *parm)
14328 {
14329 *is_non_type = true;
14330 cp_declarator *decl = parm->declarator;
14331 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14332 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14333 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14334 }
14335
14336 /* Build a constrained template parameter based on the PARMDECL
14337 declarator. The type of PARMDECL is the constrained type, which
14338 refers to the prototype template parameter that ultimately
14339 specifies the type of the declared parameter. */
14340
14341 static tree
14342 finish_constrained_parameter (cp_parser *parser,
14343 cp_parameter_declarator *parmdecl,
14344 bool *is_non_type,
14345 bool *is_parameter_pack)
14346 {
14347 tree decl = parmdecl->decl_specifiers.type;
14348 tree id = get_unqualified_id (parmdecl->declarator);
14349 tree def = parmdecl->default_argument;
14350 tree proto = DECL_INITIAL (decl);
14351
14352 /* A template parameter constrained by a variadic concept shall also
14353 be declared as a template parameter pack. */
14354 bool is_variadic = template_parameter_pack_p (proto);
14355 if (is_variadic && !*is_parameter_pack)
14356 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14357
14358 /* Build the parameter. Return an error if the declarator was invalid. */
14359 tree parm;
14360 if (TREE_CODE (proto) == TYPE_DECL)
14361 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14362 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14363 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14364 parmdecl);
14365 else
14366 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14367 if (parm == error_mark_node)
14368 return error_mark_node;
14369
14370 /* Finish the parameter decl and create a node attaching the
14371 default argument and constraint. */
14372 parm = build_tree_list (def, parm);
14373 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14374
14375 return parm;
14376 }
14377
14378 /* Returns true if the parsed type actually represents the declaration
14379 of a type template-parameter. */
14380
14381 static inline bool
14382 declares_constrained_type_template_parameter (tree type)
14383 {
14384 return (is_constrained_parameter (type)
14385 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14386 }
14387
14388
14389 /* Returns true if the parsed type actually represents the declaration of
14390 a template template-parameter. */
14391
14392 static bool
14393 declares_constrained_template_template_parameter (tree type)
14394 {
14395 return (is_constrained_parameter (type)
14396 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14397 }
14398
14399 /* Parse a default argument for a type template-parameter.
14400 Note that diagnostics are handled in cp_parser_template_parameter. */
14401
14402 static tree
14403 cp_parser_default_type_template_argument (cp_parser *parser)
14404 {
14405 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14406
14407 /* Consume the `=' token. */
14408 cp_lexer_consume_token (parser->lexer);
14409
14410 cp_token *token = cp_lexer_peek_token (parser->lexer);
14411
14412 /* Parse the default-argument. */
14413 push_deferring_access_checks (dk_no_deferred);
14414 tree default_argument = cp_parser_type_id (parser);
14415 pop_deferring_access_checks ();
14416
14417 if (flag_concepts && type_uses_auto (default_argument))
14418 {
14419 error_at (token->location,
14420 "invalid use of %<auto%> in default template argument");
14421 return error_mark_node;
14422 }
14423
14424 return default_argument;
14425 }
14426
14427 /* Parse a default argument for a template template-parameter. */
14428
14429 static tree
14430 cp_parser_default_template_template_argument (cp_parser *parser)
14431 {
14432 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14433
14434 bool is_template;
14435
14436 /* Consume the `='. */
14437 cp_lexer_consume_token (parser->lexer);
14438 /* Parse the id-expression. */
14439 push_deferring_access_checks (dk_no_deferred);
14440 /* save token before parsing the id-expression, for error
14441 reporting */
14442 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14443 tree default_argument
14444 = cp_parser_id_expression (parser,
14445 /*template_keyword_p=*/false,
14446 /*check_dependency_p=*/true,
14447 /*template_p=*/&is_template,
14448 /*declarator_p=*/false,
14449 /*optional_p=*/false);
14450 if (TREE_CODE (default_argument) == TYPE_DECL)
14451 /* If the id-expression was a template-id that refers to
14452 a template-class, we already have the declaration here,
14453 so no further lookup is needed. */
14454 ;
14455 else
14456 /* Look up the name. */
14457 default_argument
14458 = cp_parser_lookup_name (parser, default_argument,
14459 none_type,
14460 /*is_template=*/is_template,
14461 /*is_namespace=*/false,
14462 /*check_dependency=*/true,
14463 /*ambiguous_decls=*/NULL,
14464 token->location);
14465 /* See if the default argument is valid. */
14466 default_argument = check_template_template_default_arg (default_argument);
14467 pop_deferring_access_checks ();
14468 return default_argument;
14469 }
14470
14471 /* Parse a template-parameter.
14472
14473 template-parameter:
14474 type-parameter
14475 parameter-declaration
14476
14477 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14478 the parameter. The TREE_PURPOSE is the default value, if any.
14479 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14480 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14481 set to true iff this parameter is a parameter pack. */
14482
14483 static tree
14484 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14485 bool *is_parameter_pack)
14486 {
14487 cp_token *token;
14488 cp_parameter_declarator *parameter_declarator;
14489 tree parm;
14490
14491 /* Assume it is a type parameter or a template parameter. */
14492 *is_non_type = false;
14493 /* Assume it not a parameter pack. */
14494 *is_parameter_pack = false;
14495 /* Peek at the next token. */
14496 token = cp_lexer_peek_token (parser->lexer);
14497 /* If it is `class' or `template', we have a type-parameter. */
14498 if (token->keyword == RID_TEMPLATE)
14499 return cp_parser_type_parameter (parser, is_parameter_pack);
14500 /* If it is `class' or `typename' we do not know yet whether it is a
14501 type parameter or a non-type parameter. Consider:
14502
14503 template <typename T, typename T::X X> ...
14504
14505 or:
14506
14507 template <class C, class D*> ...
14508
14509 Here, the first parameter is a type parameter, and the second is
14510 a non-type parameter. We can tell by looking at the token after
14511 the identifier -- if it is a `,', `=', or `>' then we have a type
14512 parameter. */
14513 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14514 {
14515 /* Peek at the token after `class' or `typename'. */
14516 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14517 /* If it's an ellipsis, we have a template type parameter
14518 pack. */
14519 if (token->type == CPP_ELLIPSIS)
14520 return cp_parser_type_parameter (parser, is_parameter_pack);
14521 /* If it's an identifier, skip it. */
14522 if (token->type == CPP_NAME)
14523 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14524 /* Now, see if the token looks like the end of a template
14525 parameter. */
14526 if (token->type == CPP_COMMA
14527 || token->type == CPP_EQ
14528 || token->type == CPP_GREATER)
14529 return cp_parser_type_parameter (parser, is_parameter_pack);
14530 }
14531
14532 /* Otherwise, it is a non-type parameter or a constrained parameter.
14533
14534 [temp.param]
14535
14536 When parsing a default template-argument for a non-type
14537 template-parameter, the first non-nested `>' is taken as the end
14538 of the template parameter-list rather than a greater-than
14539 operator. */
14540 parameter_declarator
14541 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14542 /*parenthesized_p=*/NULL);
14543
14544 if (!parameter_declarator)
14545 return error_mark_node;
14546
14547 /* If the parameter declaration is marked as a parameter pack, set
14548 *IS_PARAMETER_PACK to notify the caller. */
14549 if (parameter_declarator->template_parameter_pack_p)
14550 *is_parameter_pack = true;
14551
14552 if (parameter_declarator->default_argument)
14553 {
14554 /* Can happen in some cases of erroneous input (c++/34892). */
14555 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14556 /* Consume the `...' for better error recovery. */
14557 cp_lexer_consume_token (parser->lexer);
14558 }
14559
14560 // The parameter may have been constrained.
14561 if (is_constrained_parameter (parameter_declarator))
14562 return finish_constrained_parameter (parser,
14563 parameter_declarator,
14564 is_non_type,
14565 is_parameter_pack);
14566
14567 // Now we're sure that the parameter is a non-type parameter.
14568 *is_non_type = true;
14569
14570 parm = grokdeclarator (parameter_declarator->declarator,
14571 &parameter_declarator->decl_specifiers,
14572 TPARM, /*initialized=*/0,
14573 /*attrlist=*/NULL);
14574 if (parm == error_mark_node)
14575 return error_mark_node;
14576
14577 return build_tree_list (parameter_declarator->default_argument, parm);
14578 }
14579
14580 /* Parse a type-parameter.
14581
14582 type-parameter:
14583 class identifier [opt]
14584 class identifier [opt] = type-id
14585 typename identifier [opt]
14586 typename identifier [opt] = type-id
14587 template < template-parameter-list > class identifier [opt]
14588 template < template-parameter-list > class identifier [opt]
14589 = id-expression
14590
14591 GNU Extension (variadic templates):
14592
14593 type-parameter:
14594 class ... identifier [opt]
14595 typename ... identifier [opt]
14596
14597 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14598 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14599 the declaration of the parameter.
14600
14601 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14602
14603 static tree
14604 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14605 {
14606 cp_token *token;
14607 tree parameter;
14608
14609 /* Look for a keyword to tell us what kind of parameter this is. */
14610 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14611 if (!token)
14612 return error_mark_node;
14613
14614 switch (token->keyword)
14615 {
14616 case RID_CLASS:
14617 case RID_TYPENAME:
14618 {
14619 tree identifier;
14620 tree default_argument;
14621
14622 /* If the next token is an ellipsis, we have a template
14623 argument pack. */
14624 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14625 {
14626 /* Consume the `...' token. */
14627 cp_lexer_consume_token (parser->lexer);
14628 maybe_warn_variadic_templates ();
14629
14630 *is_parameter_pack = true;
14631 }
14632
14633 /* If the next token is an identifier, then it names the
14634 parameter. */
14635 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14636 identifier = cp_parser_identifier (parser);
14637 else
14638 identifier = NULL_TREE;
14639
14640 /* Create the parameter. */
14641 parameter = finish_template_type_parm (class_type_node, identifier);
14642
14643 /* If the next token is an `=', we have a default argument. */
14644 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14645 {
14646 default_argument
14647 = cp_parser_default_type_template_argument (parser);
14648
14649 /* Template parameter packs cannot have default
14650 arguments. */
14651 if (*is_parameter_pack)
14652 {
14653 if (identifier)
14654 error_at (token->location,
14655 "template parameter pack %qD cannot have a "
14656 "default argument", identifier);
14657 else
14658 error_at (token->location,
14659 "template parameter packs cannot have "
14660 "default arguments");
14661 default_argument = NULL_TREE;
14662 }
14663 else if (check_for_bare_parameter_packs (default_argument))
14664 default_argument = error_mark_node;
14665 }
14666 else
14667 default_argument = NULL_TREE;
14668
14669 /* Create the combined representation of the parameter and the
14670 default argument. */
14671 parameter = build_tree_list (default_argument, parameter);
14672 }
14673 break;
14674
14675 case RID_TEMPLATE:
14676 {
14677 tree identifier;
14678 tree default_argument;
14679
14680 /* Look for the `<'. */
14681 cp_parser_require (parser, CPP_LESS, RT_LESS);
14682 /* Parse the template-parameter-list. */
14683 cp_parser_template_parameter_list (parser);
14684 /* Look for the `>'. */
14685 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14686
14687 // If template requirements are present, parse them.
14688 tree reqs = get_shorthand_constraints (current_template_parms);
14689 if (tree r = cp_parser_requires_clause_opt (parser))
14690 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14691 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14692
14693 /* Look for the `class' or 'typename' keywords. */
14694 cp_parser_type_parameter_key (parser);
14695 /* If the next token is an ellipsis, we have a template
14696 argument pack. */
14697 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14698 {
14699 /* Consume the `...' token. */
14700 cp_lexer_consume_token (parser->lexer);
14701 maybe_warn_variadic_templates ();
14702
14703 *is_parameter_pack = true;
14704 }
14705 /* If the next token is an `=', then there is a
14706 default-argument. If the next token is a `>', we are at
14707 the end of the parameter-list. If the next token is a `,',
14708 then we are at the end of this parameter. */
14709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14710 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14711 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14712 {
14713 identifier = cp_parser_identifier (parser);
14714 /* Treat invalid names as if the parameter were nameless. */
14715 if (identifier == error_mark_node)
14716 identifier = NULL_TREE;
14717 }
14718 else
14719 identifier = NULL_TREE;
14720
14721 /* Create the template parameter. */
14722 parameter = finish_template_template_parm (class_type_node,
14723 identifier);
14724
14725 /* If the next token is an `=', then there is a
14726 default-argument. */
14727 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14728 {
14729 default_argument
14730 = cp_parser_default_template_template_argument (parser);
14731
14732 /* Template parameter packs cannot have default
14733 arguments. */
14734 if (*is_parameter_pack)
14735 {
14736 if (identifier)
14737 error_at (token->location,
14738 "template parameter pack %qD cannot "
14739 "have a default argument",
14740 identifier);
14741 else
14742 error_at (token->location, "template parameter packs cannot "
14743 "have default arguments");
14744 default_argument = NULL_TREE;
14745 }
14746 }
14747 else
14748 default_argument = NULL_TREE;
14749
14750 /* Create the combined representation of the parameter and the
14751 default argument. */
14752 parameter = build_tree_list (default_argument, parameter);
14753 }
14754 break;
14755
14756 default:
14757 gcc_unreachable ();
14758 break;
14759 }
14760
14761 return parameter;
14762 }
14763
14764 /* Parse a template-id.
14765
14766 template-id:
14767 template-name < template-argument-list [opt] >
14768
14769 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14770 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14771 returned. Otherwise, if the template-name names a function, or set
14772 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14773 names a class, returns a TYPE_DECL for the specialization.
14774
14775 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14776 uninstantiated templates. */
14777
14778 static tree
14779 cp_parser_template_id (cp_parser *parser,
14780 bool template_keyword_p,
14781 bool check_dependency_p,
14782 enum tag_types tag_type,
14783 bool is_declaration)
14784 {
14785 tree templ;
14786 tree arguments;
14787 tree template_id;
14788 cp_token_position start_of_id = 0;
14789 cp_token *next_token = NULL, *next_token_2 = NULL;
14790 bool is_identifier;
14791
14792 /* If the next token corresponds to a template-id, there is no need
14793 to reparse it. */
14794 next_token = cp_lexer_peek_token (parser->lexer);
14795 if (next_token->type == CPP_TEMPLATE_ID)
14796 {
14797 cp_lexer_consume_token (parser->lexer);
14798 return saved_checks_value (next_token->u.tree_check_value);
14799 }
14800
14801 /* Avoid performing name lookup if there is no possibility of
14802 finding a template-id. */
14803 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14804 || (next_token->type == CPP_NAME
14805 && !cp_parser_nth_token_starts_template_argument_list_p
14806 (parser, 2)))
14807 {
14808 cp_parser_error (parser, "expected template-id");
14809 return error_mark_node;
14810 }
14811
14812 /* Remember where the template-id starts. */
14813 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14814 start_of_id = cp_lexer_token_position (parser->lexer, false);
14815
14816 push_deferring_access_checks (dk_deferred);
14817
14818 /* Parse the template-name. */
14819 is_identifier = false;
14820 templ = cp_parser_template_name (parser, template_keyword_p,
14821 check_dependency_p,
14822 is_declaration,
14823 tag_type,
14824 &is_identifier);
14825 if (templ == error_mark_node || is_identifier)
14826 {
14827 pop_deferring_access_checks ();
14828 return templ;
14829 }
14830
14831 /* Since we're going to preserve any side-effects from this parse, set up a
14832 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14833 in the template arguments. */
14834 tentative_firewall firewall (parser);
14835
14836 /* If we find the sequence `[:' after a template-name, it's probably
14837 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14838 parse correctly the argument list. */
14839 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
14840 == CPP_OPEN_SQUARE)
14841 && next_token->flags & DIGRAPH
14842 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
14843 == CPP_COLON)
14844 && !(next_token_2->flags & PREV_WHITE))
14845 {
14846 cp_parser_parse_tentatively (parser);
14847 /* Change `:' into `::'. */
14848 next_token_2->type = CPP_SCOPE;
14849 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14850 CPP_LESS. */
14851 cp_lexer_consume_token (parser->lexer);
14852
14853 /* Parse the arguments. */
14854 arguments = cp_parser_enclosed_template_argument_list (parser);
14855 if (!cp_parser_parse_definitely (parser))
14856 {
14857 /* If we couldn't parse an argument list, then we revert our changes
14858 and return simply an error. Maybe this is not a template-id
14859 after all. */
14860 next_token_2->type = CPP_COLON;
14861 cp_parser_error (parser, "expected %<<%>");
14862 pop_deferring_access_checks ();
14863 return error_mark_node;
14864 }
14865 /* Otherwise, emit an error about the invalid digraph, but continue
14866 parsing because we got our argument list. */
14867 if (permerror (next_token->location,
14868 "%<<::%> cannot begin a template-argument list"))
14869 {
14870 static bool hint = false;
14871 inform (next_token->location,
14872 "%<<:%> is an alternate spelling for %<[%>."
14873 " Insert whitespace between %<<%> and %<::%>");
14874 if (!hint && !flag_permissive)
14875 {
14876 inform (next_token->location, "(if you use %<-fpermissive%> "
14877 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14878 "accept your code)");
14879 hint = true;
14880 }
14881 }
14882 }
14883 else
14884 {
14885 /* Look for the `<' that starts the template-argument-list. */
14886 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14887 {
14888 pop_deferring_access_checks ();
14889 return error_mark_node;
14890 }
14891 /* Parse the arguments. */
14892 arguments = cp_parser_enclosed_template_argument_list (parser);
14893 }
14894
14895 /* Build a representation of the specialization. */
14896 if (identifier_p (templ))
14897 template_id = build_min_nt_loc (next_token->location,
14898 TEMPLATE_ID_EXPR,
14899 templ, arguments);
14900 else if (DECL_TYPE_TEMPLATE_P (templ)
14901 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14902 {
14903 bool entering_scope;
14904 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14905 template (rather than some instantiation thereof) only if
14906 is not nested within some other construct. For example, in
14907 "template <typename T> void f(T) { A<T>::", A<T> is just an
14908 instantiation of A. */
14909 entering_scope = (template_parm_scope_p ()
14910 && cp_lexer_next_token_is (parser->lexer,
14911 CPP_SCOPE));
14912 template_id
14913 = finish_template_type (templ, arguments, entering_scope);
14914 }
14915 /* A template-like identifier may be a partial concept id. */
14916 else if (flag_concepts
14917 && (template_id = (cp_parser_maybe_partial_concept_id
14918 (parser, templ, arguments))))
14919 return template_id;
14920 else if (variable_template_p (templ))
14921 {
14922 template_id = lookup_template_variable (templ, arguments);
14923 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14924 SET_EXPR_LOCATION (template_id, next_token->location);
14925 }
14926 else
14927 {
14928 /* If it's not a class-template or a template-template, it should be
14929 a function-template. */
14930 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14931 || TREE_CODE (templ) == OVERLOAD
14932 || BASELINK_P (templ)));
14933
14934 template_id = lookup_template_function (templ, arguments);
14935 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14936 SET_EXPR_LOCATION (template_id, next_token->location);
14937 }
14938
14939 /* If parsing tentatively, replace the sequence of tokens that makes
14940 up the template-id with a CPP_TEMPLATE_ID token. That way,
14941 should we re-parse the token stream, we will not have to repeat
14942 the effort required to do the parse, nor will we issue duplicate
14943 error messages about problems during instantiation of the
14944 template. */
14945 if (start_of_id
14946 /* Don't do this if we had a parse error in a declarator; re-parsing
14947 might succeed if a name changes meaning (60361). */
14948 && !(cp_parser_error_occurred (parser)
14949 && cp_parser_parsing_tentatively (parser)
14950 && parser->in_declarator_p))
14951 {
14952 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14953
14954 /* Reset the contents of the START_OF_ID token. */
14955 token->type = CPP_TEMPLATE_ID;
14956
14957 /* Update the location to be of the form:
14958 template-name < template-argument-list [opt] >
14959 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14960 with caret == start at the start of the template-name,
14961 ranging until the closing '>'. */
14962 location_t finish_loc
14963 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
14964 location_t combined_loc
14965 = make_location (token->location, token->location, finish_loc);
14966 token->location = combined_loc;
14967
14968 /* Retrieve any deferred checks. Do not pop this access checks yet
14969 so the memory will not be reclaimed during token replacing below. */
14970 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14971 token->u.tree_check_value->value = template_id;
14972 token->u.tree_check_value->checks = get_deferred_access_checks ();
14973 token->keyword = RID_MAX;
14974
14975 /* Purge all subsequent tokens. */
14976 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14977
14978 /* ??? Can we actually assume that, if template_id ==
14979 error_mark_node, we will have issued a diagnostic to the
14980 user, as opposed to simply marking the tentative parse as
14981 failed? */
14982 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14983 error_at (token->location, "parse error in template argument list");
14984 }
14985
14986 pop_to_parent_deferring_access_checks ();
14987 return template_id;
14988 }
14989
14990 /* Parse a template-name.
14991
14992 template-name:
14993 identifier
14994
14995 The standard should actually say:
14996
14997 template-name:
14998 identifier
14999 operator-function-id
15000
15001 A defect report has been filed about this issue.
15002
15003 A conversion-function-id cannot be a template name because they cannot
15004 be part of a template-id. In fact, looking at this code:
15005
15006 a.operator K<int>()
15007
15008 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15009 It is impossible to call a templated conversion-function-id with an
15010 explicit argument list, since the only allowed template parameter is
15011 the type to which it is converting.
15012
15013 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15014 `template' keyword, in a construction like:
15015
15016 T::template f<3>()
15017
15018 In that case `f' is taken to be a template-name, even though there
15019 is no way of knowing for sure.
15020
15021 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15022 name refers to a set of overloaded functions, at least one of which
15023 is a template, or an IDENTIFIER_NODE with the name of the template,
15024 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15025 names are looked up inside uninstantiated templates. */
15026
15027 static tree
15028 cp_parser_template_name (cp_parser* parser,
15029 bool template_keyword_p,
15030 bool check_dependency_p,
15031 bool is_declaration,
15032 enum tag_types tag_type,
15033 bool *is_identifier)
15034 {
15035 tree identifier;
15036 tree decl;
15037 tree fns;
15038 cp_token *token = cp_lexer_peek_token (parser->lexer);
15039
15040 /* If the next token is `operator', then we have either an
15041 operator-function-id or a conversion-function-id. */
15042 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15043 {
15044 /* We don't know whether we're looking at an
15045 operator-function-id or a conversion-function-id. */
15046 cp_parser_parse_tentatively (parser);
15047 /* Try an operator-function-id. */
15048 identifier = cp_parser_operator_function_id (parser);
15049 /* If that didn't work, try a conversion-function-id. */
15050 if (!cp_parser_parse_definitely (parser))
15051 {
15052 cp_parser_error (parser, "expected template-name");
15053 return error_mark_node;
15054 }
15055 }
15056 /* Look for the identifier. */
15057 else
15058 identifier = cp_parser_identifier (parser);
15059
15060 /* If we didn't find an identifier, we don't have a template-id. */
15061 if (identifier == error_mark_node)
15062 return error_mark_node;
15063
15064 /* If the name immediately followed the `template' keyword, then it
15065 is a template-name. However, if the next token is not `<', then
15066 we do not treat it as a template-name, since it is not being used
15067 as part of a template-id. This enables us to handle constructs
15068 like:
15069
15070 template <typename T> struct S { S(); };
15071 template <typename T> S<T>::S();
15072
15073 correctly. We would treat `S' as a template -- if it were `S<T>'
15074 -- but we do not if there is no `<'. */
15075
15076 if (processing_template_decl
15077 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15078 {
15079 /* In a declaration, in a dependent context, we pretend that the
15080 "template" keyword was present in order to improve error
15081 recovery. For example, given:
15082
15083 template <typename T> void f(T::X<int>);
15084
15085 we want to treat "X<int>" as a template-id. */
15086 if (is_declaration
15087 && !template_keyword_p
15088 && parser->scope && TYPE_P (parser->scope)
15089 && check_dependency_p
15090 && dependent_scope_p (parser->scope)
15091 /* Do not do this for dtors (or ctors), since they never
15092 need the template keyword before their name. */
15093 && !constructor_name_p (identifier, parser->scope))
15094 {
15095 cp_token_position start = 0;
15096
15097 /* Explain what went wrong. */
15098 error_at (token->location, "non-template %qD used as template",
15099 identifier);
15100 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15101 parser->scope, identifier);
15102 /* If parsing tentatively, find the location of the "<" token. */
15103 if (cp_parser_simulate_error (parser))
15104 start = cp_lexer_token_position (parser->lexer, true);
15105 /* Parse the template arguments so that we can issue error
15106 messages about them. */
15107 cp_lexer_consume_token (parser->lexer);
15108 cp_parser_enclosed_template_argument_list (parser);
15109 /* Skip tokens until we find a good place from which to
15110 continue parsing. */
15111 cp_parser_skip_to_closing_parenthesis (parser,
15112 /*recovering=*/true,
15113 /*or_comma=*/true,
15114 /*consume_paren=*/false);
15115 /* If parsing tentatively, permanently remove the
15116 template argument list. That will prevent duplicate
15117 error messages from being issued about the missing
15118 "template" keyword. */
15119 if (start)
15120 cp_lexer_purge_tokens_after (parser->lexer, start);
15121 if (is_identifier)
15122 *is_identifier = true;
15123 return identifier;
15124 }
15125
15126 /* If the "template" keyword is present, then there is generally
15127 no point in doing name-lookup, so we just return IDENTIFIER.
15128 But, if the qualifying scope is non-dependent then we can
15129 (and must) do name-lookup normally. */
15130 if (template_keyword_p
15131 && (!parser->scope
15132 || (TYPE_P (parser->scope)
15133 && dependent_type_p (parser->scope))))
15134 return identifier;
15135 }
15136
15137 /* Look up the name. */
15138 decl = cp_parser_lookup_name (parser, identifier,
15139 tag_type,
15140 /*is_template=*/true,
15141 /*is_namespace=*/false,
15142 check_dependency_p,
15143 /*ambiguous_decls=*/NULL,
15144 token->location);
15145
15146 decl = strip_using_decl (decl);
15147
15148 /* If DECL is a template, then the name was a template-name. */
15149 if (TREE_CODE (decl) == TEMPLATE_DECL)
15150 {
15151 if (TREE_DEPRECATED (decl)
15152 && deprecated_state != DEPRECATED_SUPPRESS)
15153 warn_deprecated_use (decl, NULL_TREE);
15154 }
15155 else
15156 {
15157 tree fn = NULL_TREE;
15158
15159 /* The standard does not explicitly indicate whether a name that
15160 names a set of overloaded declarations, some of which are
15161 templates, is a template-name. However, such a name should
15162 be a template-name; otherwise, there is no way to form a
15163 template-id for the overloaded templates. */
15164 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15165 if (TREE_CODE (fns) == OVERLOAD)
15166 for (fn = fns; fn; fn = OVL_NEXT (fn))
15167 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15168 break;
15169
15170 if (!fn)
15171 {
15172 /* The name does not name a template. */
15173 cp_parser_error (parser, "expected template-name");
15174 return error_mark_node;
15175 }
15176 }
15177
15178 /* If DECL is dependent, and refers to a function, then just return
15179 its name; we will look it up again during template instantiation. */
15180 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15181 {
15182 tree scope = ovl_scope (decl);
15183 if (TYPE_P (scope) && dependent_type_p (scope))
15184 return identifier;
15185 }
15186
15187 return decl;
15188 }
15189
15190 /* Parse a template-argument-list.
15191
15192 template-argument-list:
15193 template-argument ... [opt]
15194 template-argument-list , template-argument ... [opt]
15195
15196 Returns a TREE_VEC containing the arguments. */
15197
15198 static tree
15199 cp_parser_template_argument_list (cp_parser* parser)
15200 {
15201 tree fixed_args[10];
15202 unsigned n_args = 0;
15203 unsigned alloced = 10;
15204 tree *arg_ary = fixed_args;
15205 tree vec;
15206 bool saved_in_template_argument_list_p;
15207 bool saved_ice_p;
15208 bool saved_non_ice_p;
15209
15210 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15211 parser->in_template_argument_list_p = true;
15212 /* Even if the template-id appears in an integral
15213 constant-expression, the contents of the argument list do
15214 not. */
15215 saved_ice_p = parser->integral_constant_expression_p;
15216 parser->integral_constant_expression_p = false;
15217 saved_non_ice_p = parser->non_integral_constant_expression_p;
15218 parser->non_integral_constant_expression_p = false;
15219
15220 /* Parse the arguments. */
15221 do
15222 {
15223 tree argument;
15224
15225 if (n_args)
15226 /* Consume the comma. */
15227 cp_lexer_consume_token (parser->lexer);
15228
15229 /* Parse the template-argument. */
15230 argument = cp_parser_template_argument (parser);
15231
15232 /* If the next token is an ellipsis, we're expanding a template
15233 argument pack. */
15234 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15235 {
15236 if (argument == error_mark_node)
15237 {
15238 cp_token *token = cp_lexer_peek_token (parser->lexer);
15239 error_at (token->location,
15240 "expected parameter pack before %<...%>");
15241 }
15242 /* Consume the `...' token. */
15243 cp_lexer_consume_token (parser->lexer);
15244
15245 /* Make the argument into a TYPE_PACK_EXPANSION or
15246 EXPR_PACK_EXPANSION. */
15247 argument = make_pack_expansion (argument);
15248 }
15249
15250 if (n_args == alloced)
15251 {
15252 alloced *= 2;
15253
15254 if (arg_ary == fixed_args)
15255 {
15256 arg_ary = XNEWVEC (tree, alloced);
15257 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15258 }
15259 else
15260 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15261 }
15262 arg_ary[n_args++] = argument;
15263 }
15264 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15265
15266 vec = make_tree_vec (n_args);
15267
15268 while (n_args--)
15269 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15270
15271 if (arg_ary != fixed_args)
15272 free (arg_ary);
15273 parser->non_integral_constant_expression_p = saved_non_ice_p;
15274 parser->integral_constant_expression_p = saved_ice_p;
15275 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15276 if (CHECKING_P)
15277 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15278 return vec;
15279 }
15280
15281 /* Parse a template-argument.
15282
15283 template-argument:
15284 assignment-expression
15285 type-id
15286 id-expression
15287
15288 The representation is that of an assignment-expression, type-id, or
15289 id-expression -- except that the qualified id-expression is
15290 evaluated, so that the value returned is either a DECL or an
15291 OVERLOAD.
15292
15293 Although the standard says "assignment-expression", it forbids
15294 throw-expressions or assignments in the template argument.
15295 Therefore, we use "conditional-expression" instead. */
15296
15297 static tree
15298 cp_parser_template_argument (cp_parser* parser)
15299 {
15300 tree argument;
15301 bool template_p;
15302 bool address_p;
15303 bool maybe_type_id = false;
15304 cp_token *token = NULL, *argument_start_token = NULL;
15305 location_t loc = 0;
15306 cp_id_kind idk;
15307
15308 /* There's really no way to know what we're looking at, so we just
15309 try each alternative in order.
15310
15311 [temp.arg]
15312
15313 In a template-argument, an ambiguity between a type-id and an
15314 expression is resolved to a type-id, regardless of the form of
15315 the corresponding template-parameter.
15316
15317 Therefore, we try a type-id first. */
15318 cp_parser_parse_tentatively (parser);
15319 argument = cp_parser_template_type_arg (parser);
15320 /* If there was no error parsing the type-id but the next token is a
15321 '>>', our behavior depends on which dialect of C++ we're
15322 parsing. In C++98, we probably found a typo for '> >'. But there
15323 are type-id which are also valid expressions. For instance:
15324
15325 struct X { int operator >> (int); };
15326 template <int V> struct Foo {};
15327 Foo<X () >> 5> r;
15328
15329 Here 'X()' is a valid type-id of a function type, but the user just
15330 wanted to write the expression "X() >> 5". Thus, we remember that we
15331 found a valid type-id, but we still try to parse the argument as an
15332 expression to see what happens.
15333
15334 In C++0x, the '>>' will be considered two separate '>'
15335 tokens. */
15336 if (!cp_parser_error_occurred (parser)
15337 && cxx_dialect == cxx98
15338 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15339 {
15340 maybe_type_id = true;
15341 cp_parser_abort_tentative_parse (parser);
15342 }
15343 else
15344 {
15345 /* If the next token isn't a `,' or a `>', then this argument wasn't
15346 really finished. This means that the argument is not a valid
15347 type-id. */
15348 if (!cp_parser_next_token_ends_template_argument_p (parser))
15349 cp_parser_error (parser, "expected template-argument");
15350 /* If that worked, we're done. */
15351 if (cp_parser_parse_definitely (parser))
15352 return argument;
15353 }
15354 /* We're still not sure what the argument will be. */
15355 cp_parser_parse_tentatively (parser);
15356 /* Try a template. */
15357 argument_start_token = cp_lexer_peek_token (parser->lexer);
15358 argument = cp_parser_id_expression (parser,
15359 /*template_keyword_p=*/false,
15360 /*check_dependency_p=*/true,
15361 &template_p,
15362 /*declarator_p=*/false,
15363 /*optional_p=*/false);
15364 /* If the next token isn't a `,' or a `>', then this argument wasn't
15365 really finished. */
15366 if (!cp_parser_next_token_ends_template_argument_p (parser))
15367 cp_parser_error (parser, "expected template-argument");
15368 if (!cp_parser_error_occurred (parser))
15369 {
15370 /* Figure out what is being referred to. If the id-expression
15371 was for a class template specialization, then we will have a
15372 TYPE_DECL at this point. There is no need to do name lookup
15373 at this point in that case. */
15374 if (TREE_CODE (argument) != TYPE_DECL)
15375 argument = cp_parser_lookup_name (parser, argument,
15376 none_type,
15377 /*is_template=*/template_p,
15378 /*is_namespace=*/false,
15379 /*check_dependency=*/true,
15380 /*ambiguous_decls=*/NULL,
15381 argument_start_token->location);
15382 /* Handle a constrained-type-specifier for a non-type template
15383 parameter. */
15384 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15385 argument = decl;
15386 else if (TREE_CODE (argument) != TEMPLATE_DECL
15387 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15388 cp_parser_error (parser, "expected template-name");
15389 }
15390 if (cp_parser_parse_definitely (parser))
15391 {
15392 if (TREE_DEPRECATED (argument))
15393 warn_deprecated_use (argument, NULL_TREE);
15394 return argument;
15395 }
15396 /* It must be a non-type argument. In C++17 any constant-expression is
15397 allowed. */
15398 if (cxx_dialect > cxx14)
15399 goto general_expr;
15400
15401 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15402
15403 -- an integral constant-expression of integral or enumeration
15404 type; or
15405
15406 -- the name of a non-type template-parameter; or
15407
15408 -- the name of an object or function with external linkage...
15409
15410 -- the address of an object or function with external linkage...
15411
15412 -- a pointer to member... */
15413 /* Look for a non-type template parameter. */
15414 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15415 {
15416 cp_parser_parse_tentatively (parser);
15417 argument = cp_parser_primary_expression (parser,
15418 /*address_p=*/false,
15419 /*cast_p=*/false,
15420 /*template_arg_p=*/true,
15421 &idk);
15422 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15423 || !cp_parser_next_token_ends_template_argument_p (parser))
15424 cp_parser_simulate_error (parser);
15425 if (cp_parser_parse_definitely (parser))
15426 return argument;
15427 }
15428
15429 /* If the next token is "&", the argument must be the address of an
15430 object or function with external linkage. */
15431 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15432 if (address_p)
15433 {
15434 loc = cp_lexer_peek_token (parser->lexer)->location;
15435 cp_lexer_consume_token (parser->lexer);
15436 }
15437 /* See if we might have an id-expression. */
15438 token = cp_lexer_peek_token (parser->lexer);
15439 if (token->type == CPP_NAME
15440 || token->keyword == RID_OPERATOR
15441 || token->type == CPP_SCOPE
15442 || token->type == CPP_TEMPLATE_ID
15443 || token->type == CPP_NESTED_NAME_SPECIFIER)
15444 {
15445 cp_parser_parse_tentatively (parser);
15446 argument = cp_parser_primary_expression (parser,
15447 address_p,
15448 /*cast_p=*/false,
15449 /*template_arg_p=*/true,
15450 &idk);
15451 if (cp_parser_error_occurred (parser)
15452 || !cp_parser_next_token_ends_template_argument_p (parser))
15453 cp_parser_abort_tentative_parse (parser);
15454 else
15455 {
15456 tree probe;
15457
15458 if (INDIRECT_REF_P (argument))
15459 {
15460 /* Strip the dereference temporarily. */
15461 gcc_assert (REFERENCE_REF_P (argument));
15462 argument = TREE_OPERAND (argument, 0);
15463 }
15464
15465 /* If we're in a template, we represent a qualified-id referring
15466 to a static data member as a SCOPE_REF even if the scope isn't
15467 dependent so that we can check access control later. */
15468 probe = argument;
15469 if (TREE_CODE (probe) == SCOPE_REF)
15470 probe = TREE_OPERAND (probe, 1);
15471 if (VAR_P (probe))
15472 {
15473 /* A variable without external linkage might still be a
15474 valid constant-expression, so no error is issued here
15475 if the external-linkage check fails. */
15476 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15477 cp_parser_simulate_error (parser);
15478 }
15479 else if (is_overloaded_fn (argument))
15480 /* All overloaded functions are allowed; if the external
15481 linkage test does not pass, an error will be issued
15482 later. */
15483 ;
15484 else if (address_p
15485 && (TREE_CODE (argument) == OFFSET_REF
15486 || TREE_CODE (argument) == SCOPE_REF))
15487 /* A pointer-to-member. */
15488 ;
15489 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15490 ;
15491 else
15492 cp_parser_simulate_error (parser);
15493
15494 if (cp_parser_parse_definitely (parser))
15495 {
15496 if (address_p)
15497 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15498 tf_warning_or_error);
15499 else
15500 argument = convert_from_reference (argument);
15501 return argument;
15502 }
15503 }
15504 }
15505 /* If the argument started with "&", there are no other valid
15506 alternatives at this point. */
15507 if (address_p)
15508 {
15509 cp_parser_error (parser, "invalid non-type template argument");
15510 return error_mark_node;
15511 }
15512
15513 general_expr:
15514 /* If the argument wasn't successfully parsed as a type-id followed
15515 by '>>', the argument can only be a constant expression now.
15516 Otherwise, we try parsing the constant-expression tentatively,
15517 because the argument could really be a type-id. */
15518 if (maybe_type_id)
15519 cp_parser_parse_tentatively (parser);
15520
15521 if (cxx_dialect <= cxx14)
15522 argument = cp_parser_constant_expression (parser);
15523 else
15524 {
15525 /* With C++17 generalized non-type template arguments we need to handle
15526 lvalue constant expressions, too. */
15527 argument = cp_parser_assignment_expression (parser);
15528 require_potential_constant_expression (argument);
15529 }
15530
15531 if (!maybe_type_id)
15532 return argument;
15533 if (!cp_parser_next_token_ends_template_argument_p (parser))
15534 cp_parser_error (parser, "expected template-argument");
15535 if (cp_parser_parse_definitely (parser))
15536 return argument;
15537 /* We did our best to parse the argument as a non type-id, but that
15538 was the only alternative that matched (albeit with a '>' after
15539 it). We can assume it's just a typo from the user, and a
15540 diagnostic will then be issued. */
15541 return cp_parser_template_type_arg (parser);
15542 }
15543
15544 /* Parse an explicit-instantiation.
15545
15546 explicit-instantiation:
15547 template declaration
15548
15549 Although the standard says `declaration', what it really means is:
15550
15551 explicit-instantiation:
15552 template decl-specifier-seq [opt] declarator [opt] ;
15553
15554 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15555 supposed to be allowed. A defect report has been filed about this
15556 issue.
15557
15558 GNU Extension:
15559
15560 explicit-instantiation:
15561 storage-class-specifier template
15562 decl-specifier-seq [opt] declarator [opt] ;
15563 function-specifier template
15564 decl-specifier-seq [opt] declarator [opt] ; */
15565
15566 static void
15567 cp_parser_explicit_instantiation (cp_parser* parser)
15568 {
15569 int declares_class_or_enum;
15570 cp_decl_specifier_seq decl_specifiers;
15571 tree extension_specifier = NULL_TREE;
15572
15573 timevar_push (TV_TEMPLATE_INST);
15574
15575 /* Look for an (optional) storage-class-specifier or
15576 function-specifier. */
15577 if (cp_parser_allow_gnu_extensions_p (parser))
15578 {
15579 extension_specifier
15580 = cp_parser_storage_class_specifier_opt (parser);
15581 if (!extension_specifier)
15582 extension_specifier
15583 = cp_parser_function_specifier_opt (parser,
15584 /*decl_specs=*/NULL);
15585 }
15586
15587 /* Look for the `template' keyword. */
15588 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15589 /* Let the front end know that we are processing an explicit
15590 instantiation. */
15591 begin_explicit_instantiation ();
15592 /* [temp.explicit] says that we are supposed to ignore access
15593 control while processing explicit instantiation directives. */
15594 push_deferring_access_checks (dk_no_check);
15595 /* Parse a decl-specifier-seq. */
15596 cp_parser_decl_specifier_seq (parser,
15597 CP_PARSER_FLAGS_OPTIONAL,
15598 &decl_specifiers,
15599 &declares_class_or_enum);
15600 /* If there was exactly one decl-specifier, and it declared a class,
15601 and there's no declarator, then we have an explicit type
15602 instantiation. */
15603 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15604 {
15605 tree type;
15606
15607 type = check_tag_decl (&decl_specifiers,
15608 /*explicit_type_instantiation_p=*/true);
15609 /* Turn access control back on for names used during
15610 template instantiation. */
15611 pop_deferring_access_checks ();
15612 if (type)
15613 do_type_instantiation (type, extension_specifier,
15614 /*complain=*/tf_error);
15615 }
15616 else
15617 {
15618 cp_declarator *declarator;
15619 tree decl;
15620
15621 /* Parse the declarator. */
15622 declarator
15623 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15624 /*ctor_dtor_or_conv_p=*/NULL,
15625 /*parenthesized_p=*/NULL,
15626 /*member_p=*/false,
15627 /*friend_p=*/false);
15628 if (declares_class_or_enum & 2)
15629 cp_parser_check_for_definition_in_return_type (declarator,
15630 decl_specifiers.type,
15631 decl_specifiers.locations[ds_type_spec]);
15632 if (declarator != cp_error_declarator)
15633 {
15634 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15635 permerror (decl_specifiers.locations[ds_inline],
15636 "explicit instantiation shall not use"
15637 " %<inline%> specifier");
15638 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15639 permerror (decl_specifiers.locations[ds_constexpr],
15640 "explicit instantiation shall not use"
15641 " %<constexpr%> specifier");
15642
15643 decl = grokdeclarator (declarator, &decl_specifiers,
15644 NORMAL, 0, &decl_specifiers.attributes);
15645 /* Turn access control back on for names used during
15646 template instantiation. */
15647 pop_deferring_access_checks ();
15648 /* Do the explicit instantiation. */
15649 do_decl_instantiation (decl, extension_specifier);
15650 }
15651 else
15652 {
15653 pop_deferring_access_checks ();
15654 /* Skip the body of the explicit instantiation. */
15655 cp_parser_skip_to_end_of_statement (parser);
15656 }
15657 }
15658 /* We're done with the instantiation. */
15659 end_explicit_instantiation ();
15660
15661 cp_parser_consume_semicolon_at_end_of_statement (parser);
15662
15663 timevar_pop (TV_TEMPLATE_INST);
15664 }
15665
15666 /* Parse an explicit-specialization.
15667
15668 explicit-specialization:
15669 template < > declaration
15670
15671 Although the standard says `declaration', what it really means is:
15672
15673 explicit-specialization:
15674 template <> decl-specifier [opt] init-declarator [opt] ;
15675 template <> function-definition
15676 template <> explicit-specialization
15677 template <> template-declaration */
15678
15679 static void
15680 cp_parser_explicit_specialization (cp_parser* parser)
15681 {
15682 bool need_lang_pop;
15683 cp_token *token = cp_lexer_peek_token (parser->lexer);
15684
15685 /* Look for the `template' keyword. */
15686 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15687 /* Look for the `<'. */
15688 cp_parser_require (parser, CPP_LESS, RT_LESS);
15689 /* Look for the `>'. */
15690 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15691 /* We have processed another parameter list. */
15692 ++parser->num_template_parameter_lists;
15693 /* [temp]
15694
15695 A template ... explicit specialization ... shall not have C
15696 linkage. */
15697 if (current_lang_name == lang_name_c)
15698 {
15699 error_at (token->location, "template specialization with C linkage");
15700 /* Give it C++ linkage to avoid confusing other parts of the
15701 front end. */
15702 push_lang_context (lang_name_cplusplus);
15703 need_lang_pop = true;
15704 }
15705 else
15706 need_lang_pop = false;
15707 /* Let the front end know that we are beginning a specialization. */
15708 if (!begin_specialization ())
15709 {
15710 end_specialization ();
15711 return;
15712 }
15713
15714 /* If the next keyword is `template', we need to figure out whether
15715 or not we're looking a template-declaration. */
15716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15717 {
15718 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15719 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15720 cp_parser_template_declaration_after_export (parser,
15721 /*member_p=*/false);
15722 else
15723 cp_parser_explicit_specialization (parser);
15724 }
15725 else
15726 /* Parse the dependent declaration. */
15727 cp_parser_single_declaration (parser,
15728 /*checks=*/NULL,
15729 /*member_p=*/false,
15730 /*explicit_specialization_p=*/true,
15731 /*friend_p=*/NULL);
15732 /* We're done with the specialization. */
15733 end_specialization ();
15734 /* For the erroneous case of a template with C linkage, we pushed an
15735 implicit C++ linkage scope; exit that scope now. */
15736 if (need_lang_pop)
15737 pop_lang_context ();
15738 /* We're done with this parameter list. */
15739 --parser->num_template_parameter_lists;
15740 }
15741
15742 /* Parse a type-specifier.
15743
15744 type-specifier:
15745 simple-type-specifier
15746 class-specifier
15747 enum-specifier
15748 elaborated-type-specifier
15749 cv-qualifier
15750
15751 GNU Extension:
15752
15753 type-specifier:
15754 __complex__
15755
15756 Returns a representation of the type-specifier. For a
15757 class-specifier, enum-specifier, or elaborated-type-specifier, a
15758 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15759
15760 The parser flags FLAGS is used to control type-specifier parsing.
15761
15762 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15763 in a decl-specifier-seq.
15764
15765 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15766 class-specifier, enum-specifier, or elaborated-type-specifier, then
15767 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15768 if a type is declared; 2 if it is defined. Otherwise, it is set to
15769 zero.
15770
15771 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15772 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15773 is set to FALSE. */
15774
15775 static tree
15776 cp_parser_type_specifier (cp_parser* parser,
15777 cp_parser_flags flags,
15778 cp_decl_specifier_seq *decl_specs,
15779 bool is_declaration,
15780 int* declares_class_or_enum,
15781 bool* is_cv_qualifier)
15782 {
15783 tree type_spec = NULL_TREE;
15784 cp_token *token;
15785 enum rid keyword;
15786 cp_decl_spec ds = ds_last;
15787
15788 /* Assume this type-specifier does not declare a new type. */
15789 if (declares_class_or_enum)
15790 *declares_class_or_enum = 0;
15791 /* And that it does not specify a cv-qualifier. */
15792 if (is_cv_qualifier)
15793 *is_cv_qualifier = false;
15794 /* Peek at the next token. */
15795 token = cp_lexer_peek_token (parser->lexer);
15796
15797 /* If we're looking at a keyword, we can use that to guide the
15798 production we choose. */
15799 keyword = token->keyword;
15800 switch (keyword)
15801 {
15802 case RID_ENUM:
15803 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15804 goto elaborated_type_specifier;
15805
15806 /* Look for the enum-specifier. */
15807 type_spec = cp_parser_enum_specifier (parser);
15808 /* If that worked, we're done. */
15809 if (type_spec)
15810 {
15811 if (declares_class_or_enum)
15812 *declares_class_or_enum = 2;
15813 if (decl_specs)
15814 cp_parser_set_decl_spec_type (decl_specs,
15815 type_spec,
15816 token,
15817 /*type_definition_p=*/true);
15818 return type_spec;
15819 }
15820 else
15821 goto elaborated_type_specifier;
15822
15823 /* Any of these indicate either a class-specifier, or an
15824 elaborated-type-specifier. */
15825 case RID_CLASS:
15826 case RID_STRUCT:
15827 case RID_UNION:
15828 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15829 goto elaborated_type_specifier;
15830
15831 /* Parse tentatively so that we can back up if we don't find a
15832 class-specifier. */
15833 cp_parser_parse_tentatively (parser);
15834 /* Look for the class-specifier. */
15835 type_spec = cp_parser_class_specifier (parser);
15836 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15837 /* If that worked, we're done. */
15838 if (cp_parser_parse_definitely (parser))
15839 {
15840 if (declares_class_or_enum)
15841 *declares_class_or_enum = 2;
15842 if (decl_specs)
15843 cp_parser_set_decl_spec_type (decl_specs,
15844 type_spec,
15845 token,
15846 /*type_definition_p=*/true);
15847 return type_spec;
15848 }
15849
15850 /* Fall through. */
15851 elaborated_type_specifier:
15852 /* We're declaring (not defining) a class or enum. */
15853 if (declares_class_or_enum)
15854 *declares_class_or_enum = 1;
15855
15856 /* Fall through. */
15857 case RID_TYPENAME:
15858 /* Look for an elaborated-type-specifier. */
15859 type_spec
15860 = (cp_parser_elaborated_type_specifier
15861 (parser,
15862 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15863 is_declaration));
15864 if (decl_specs)
15865 cp_parser_set_decl_spec_type (decl_specs,
15866 type_spec,
15867 token,
15868 /*type_definition_p=*/false);
15869 return type_spec;
15870
15871 case RID_CONST:
15872 ds = ds_const;
15873 if (is_cv_qualifier)
15874 *is_cv_qualifier = true;
15875 break;
15876
15877 case RID_VOLATILE:
15878 ds = ds_volatile;
15879 if (is_cv_qualifier)
15880 *is_cv_qualifier = true;
15881 break;
15882
15883 case RID_RESTRICT:
15884 ds = ds_restrict;
15885 if (is_cv_qualifier)
15886 *is_cv_qualifier = true;
15887 break;
15888
15889 case RID_COMPLEX:
15890 /* The `__complex__' keyword is a GNU extension. */
15891 ds = ds_complex;
15892 break;
15893
15894 default:
15895 break;
15896 }
15897
15898 /* Handle simple keywords. */
15899 if (ds != ds_last)
15900 {
15901 if (decl_specs)
15902 {
15903 set_and_check_decl_spec_loc (decl_specs, ds, token);
15904 decl_specs->any_specifiers_p = true;
15905 }
15906 return cp_lexer_consume_token (parser->lexer)->u.value;
15907 }
15908
15909 /* If we do not already have a type-specifier, assume we are looking
15910 at a simple-type-specifier. */
15911 type_spec = cp_parser_simple_type_specifier (parser,
15912 decl_specs,
15913 flags);
15914
15915 /* If we didn't find a type-specifier, and a type-specifier was not
15916 optional in this context, issue an error message. */
15917 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15918 {
15919 cp_parser_error (parser, "expected type specifier");
15920 return error_mark_node;
15921 }
15922
15923 return type_spec;
15924 }
15925
15926 /* Parse a simple-type-specifier.
15927
15928 simple-type-specifier:
15929 :: [opt] nested-name-specifier [opt] type-name
15930 :: [opt] nested-name-specifier template template-id
15931 char
15932 wchar_t
15933 bool
15934 short
15935 int
15936 long
15937 signed
15938 unsigned
15939 float
15940 double
15941 void
15942
15943 C++0x Extension:
15944
15945 simple-type-specifier:
15946 auto
15947 decltype ( expression )
15948 char16_t
15949 char32_t
15950 __underlying_type ( type-id )
15951
15952 GNU Extension:
15953
15954 simple-type-specifier:
15955 __int128
15956 __typeof__ unary-expression
15957 __typeof__ ( type-id )
15958 __typeof__ ( type-id ) { initializer-list , [opt] }
15959
15960 Concepts Extension:
15961
15962 simple-type-specifier:
15963 constrained-type-specifier
15964
15965 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15966 appropriately updated. */
15967
15968 static tree
15969 cp_parser_simple_type_specifier (cp_parser* parser,
15970 cp_decl_specifier_seq *decl_specs,
15971 cp_parser_flags flags)
15972 {
15973 tree type = NULL_TREE;
15974 cp_token *token;
15975 int idx;
15976
15977 /* Peek at the next token. */
15978 token = cp_lexer_peek_token (parser->lexer);
15979
15980 /* If we're looking at a keyword, things are easy. */
15981 switch (token->keyword)
15982 {
15983 case RID_CHAR:
15984 if (decl_specs)
15985 decl_specs->explicit_char_p = true;
15986 type = char_type_node;
15987 break;
15988 case RID_CHAR16:
15989 type = char16_type_node;
15990 break;
15991 case RID_CHAR32:
15992 type = char32_type_node;
15993 break;
15994 case RID_WCHAR:
15995 type = wchar_type_node;
15996 break;
15997 case RID_BOOL:
15998 type = boolean_type_node;
15999 break;
16000 case RID_SHORT:
16001 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16002 type = short_integer_type_node;
16003 break;
16004 case RID_INT:
16005 if (decl_specs)
16006 decl_specs->explicit_int_p = true;
16007 type = integer_type_node;
16008 break;
16009 case RID_INT_N_0:
16010 case RID_INT_N_1:
16011 case RID_INT_N_2:
16012 case RID_INT_N_3:
16013 idx = token->keyword - RID_INT_N_0;
16014 if (! int_n_enabled_p [idx])
16015 break;
16016 if (decl_specs)
16017 {
16018 decl_specs->explicit_intN_p = true;
16019 decl_specs->int_n_idx = idx;
16020 }
16021 type = int_n_trees [idx].signed_type;
16022 break;
16023 case RID_LONG:
16024 if (decl_specs)
16025 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16026 type = long_integer_type_node;
16027 break;
16028 case RID_SIGNED:
16029 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16030 type = integer_type_node;
16031 break;
16032 case RID_UNSIGNED:
16033 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16034 type = unsigned_type_node;
16035 break;
16036 case RID_FLOAT:
16037 type = float_type_node;
16038 break;
16039 case RID_DOUBLE:
16040 type = double_type_node;
16041 break;
16042 case RID_VOID:
16043 type = void_type_node;
16044 break;
16045
16046 case RID_AUTO:
16047 maybe_warn_cpp0x (CPP0X_AUTO);
16048 if (parser->auto_is_implicit_function_template_parm_p)
16049 {
16050 /* The 'auto' might be the placeholder return type for a function decl
16051 with trailing return type. */
16052 bool have_trailing_return_fn_decl = false;
16053
16054 cp_parser_parse_tentatively (parser);
16055 cp_lexer_consume_token (parser->lexer);
16056 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16057 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16058 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16059 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16060 {
16061 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16062 {
16063 cp_lexer_consume_token (parser->lexer);
16064 cp_parser_skip_to_closing_parenthesis (parser,
16065 /*recovering*/false,
16066 /*or_comma*/false,
16067 /*consume_paren*/true);
16068 continue;
16069 }
16070
16071 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16072 {
16073 have_trailing_return_fn_decl = true;
16074 break;
16075 }
16076
16077 cp_lexer_consume_token (parser->lexer);
16078 }
16079 cp_parser_abort_tentative_parse (parser);
16080
16081 if (have_trailing_return_fn_decl)
16082 {
16083 type = make_auto ();
16084 break;
16085 }
16086
16087 if (cxx_dialect >= cxx14)
16088 {
16089 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16090 type = TREE_TYPE (type);
16091 }
16092 else
16093 type = error_mark_node;
16094
16095 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16096 {
16097 if (cxx_dialect < cxx14)
16098 error_at (token->location,
16099 "use of %<auto%> in lambda parameter declaration "
16100 "only available with "
16101 "-std=c++14 or -std=gnu++14");
16102 }
16103 else if (cxx_dialect < cxx14)
16104 error_at (token->location,
16105 "use of %<auto%> in parameter declaration "
16106 "only available with "
16107 "-std=c++14 or -std=gnu++14");
16108 else if (!flag_concepts)
16109 pedwarn (token->location, OPT_Wpedantic,
16110 "ISO C++ forbids use of %<auto%> in parameter "
16111 "declaration");
16112 }
16113 else
16114 type = make_auto ();
16115 break;
16116
16117 case RID_DECLTYPE:
16118 /* Since DR 743, decltype can either be a simple-type-specifier by
16119 itself or begin a nested-name-specifier. Parsing it will replace
16120 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16121 handling below decide what to do. */
16122 cp_parser_decltype (parser);
16123 cp_lexer_set_token_position (parser->lexer, token);
16124 break;
16125
16126 case RID_TYPEOF:
16127 /* Consume the `typeof' token. */
16128 cp_lexer_consume_token (parser->lexer);
16129 /* Parse the operand to `typeof'. */
16130 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16131 /* If it is not already a TYPE, take its type. */
16132 if (!TYPE_P (type))
16133 type = finish_typeof (type);
16134
16135 if (decl_specs)
16136 cp_parser_set_decl_spec_type (decl_specs, type,
16137 token,
16138 /*type_definition_p=*/false);
16139
16140 return type;
16141
16142 case RID_UNDERLYING_TYPE:
16143 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16144 if (decl_specs)
16145 cp_parser_set_decl_spec_type (decl_specs, type,
16146 token,
16147 /*type_definition_p=*/false);
16148
16149 return type;
16150
16151 case RID_BASES:
16152 case RID_DIRECT_BASES:
16153 type = cp_parser_trait_expr (parser, token->keyword);
16154 if (decl_specs)
16155 cp_parser_set_decl_spec_type (decl_specs, type,
16156 token,
16157 /*type_definition_p=*/false);
16158 return type;
16159 default:
16160 break;
16161 }
16162
16163 /* If token is an already-parsed decltype not followed by ::,
16164 it's a simple-type-specifier. */
16165 if (token->type == CPP_DECLTYPE
16166 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16167 {
16168 type = saved_checks_value (token->u.tree_check_value);
16169 if (decl_specs)
16170 {
16171 cp_parser_set_decl_spec_type (decl_specs, type,
16172 token,
16173 /*type_definition_p=*/false);
16174 /* Remember that we are handling a decltype in order to
16175 implement the resolution of DR 1510 when the argument
16176 isn't instantiation dependent. */
16177 decl_specs->decltype_p = true;
16178 }
16179 cp_lexer_consume_token (parser->lexer);
16180 return type;
16181 }
16182
16183 /* If the type-specifier was for a built-in type, we're done. */
16184 if (type)
16185 {
16186 /* Record the type. */
16187 if (decl_specs
16188 && (token->keyword != RID_SIGNED
16189 && token->keyword != RID_UNSIGNED
16190 && token->keyword != RID_SHORT
16191 && token->keyword != RID_LONG))
16192 cp_parser_set_decl_spec_type (decl_specs,
16193 type,
16194 token,
16195 /*type_definition_p=*/false);
16196 if (decl_specs)
16197 decl_specs->any_specifiers_p = true;
16198
16199 /* Consume the token. */
16200 cp_lexer_consume_token (parser->lexer);
16201
16202 if (type == error_mark_node)
16203 return error_mark_node;
16204
16205 /* There is no valid C++ program where a non-template type is
16206 followed by a "<". That usually indicates that the user thought
16207 that the type was a template. */
16208 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16209 token->location);
16210
16211 return TYPE_NAME (type);
16212 }
16213
16214 /* The type-specifier must be a user-defined type. */
16215 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16216 {
16217 bool qualified_p;
16218 bool global_p;
16219
16220 /* Don't gobble tokens or issue error messages if this is an
16221 optional type-specifier. */
16222 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16223 cp_parser_parse_tentatively (parser);
16224
16225 /* Look for the optional `::' operator. */
16226 global_p
16227 = (cp_parser_global_scope_opt (parser,
16228 /*current_scope_valid_p=*/false)
16229 != NULL_TREE);
16230 /* Look for the nested-name specifier. */
16231 qualified_p
16232 = (cp_parser_nested_name_specifier_opt (parser,
16233 /*typename_keyword_p=*/false,
16234 /*check_dependency_p=*/true,
16235 /*type_p=*/false,
16236 /*is_declaration=*/false)
16237 != NULL_TREE);
16238 token = cp_lexer_peek_token (parser->lexer);
16239 /* If we have seen a nested-name-specifier, and the next token
16240 is `template', then we are using the template-id production. */
16241 if (parser->scope
16242 && cp_parser_optional_template_keyword (parser))
16243 {
16244 /* Look for the template-id. */
16245 type = cp_parser_template_id (parser,
16246 /*template_keyword_p=*/true,
16247 /*check_dependency_p=*/true,
16248 none_type,
16249 /*is_declaration=*/false);
16250 /* If the template-id did not name a type, we are out of
16251 luck. */
16252 if (TREE_CODE (type) != TYPE_DECL)
16253 {
16254 cp_parser_error (parser, "expected template-id for type");
16255 type = NULL_TREE;
16256 }
16257 }
16258 /* Otherwise, look for a type-name. */
16259 else
16260 type = cp_parser_type_name (parser);
16261 /* Keep track of all name-lookups performed in class scopes. */
16262 if (type
16263 && !global_p
16264 && !qualified_p
16265 && TREE_CODE (type) == TYPE_DECL
16266 && identifier_p (DECL_NAME (type)))
16267 maybe_note_name_used_in_class (DECL_NAME (type), type);
16268 /* If it didn't work out, we don't have a TYPE. */
16269 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16270 && !cp_parser_parse_definitely (parser))
16271 type = NULL_TREE;
16272 if (type && decl_specs)
16273 cp_parser_set_decl_spec_type (decl_specs, type,
16274 token,
16275 /*type_definition_p=*/false);
16276 }
16277
16278 /* If we didn't get a type-name, issue an error message. */
16279 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16280 {
16281 cp_parser_error (parser, "expected type-name");
16282 return error_mark_node;
16283 }
16284
16285 if (type && type != error_mark_node)
16286 {
16287 /* See if TYPE is an Objective-C type, and if so, parse and
16288 accept any protocol references following it. Do this before
16289 the cp_parser_check_for_invalid_template_id() call, because
16290 Objective-C types can be followed by '<...>' which would
16291 enclose protocol names rather than template arguments, and so
16292 everything is fine. */
16293 if (c_dialect_objc () && !parser->scope
16294 && (objc_is_id (type) || objc_is_class_name (type)))
16295 {
16296 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16297 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16298
16299 /* Clobber the "unqualified" type previously entered into
16300 DECL_SPECS with the new, improved protocol-qualified version. */
16301 if (decl_specs)
16302 decl_specs->type = qual_type;
16303
16304 return qual_type;
16305 }
16306
16307 /* There is no valid C++ program where a non-template type is
16308 followed by a "<". That usually indicates that the user
16309 thought that the type was a template. */
16310 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16311 none_type,
16312 token->location);
16313 }
16314
16315 return type;
16316 }
16317
16318 /* Parse a type-name.
16319
16320 type-name:
16321 class-name
16322 enum-name
16323 typedef-name
16324 simple-template-id [in c++0x]
16325
16326 enum-name:
16327 identifier
16328
16329 typedef-name:
16330 identifier
16331
16332 Concepts:
16333
16334 type-name:
16335 concept-name
16336 partial-concept-id
16337
16338 concept-name:
16339 identifier
16340
16341 Returns a TYPE_DECL for the type. */
16342
16343 static tree
16344 cp_parser_type_name (cp_parser* parser)
16345 {
16346 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16347 }
16348
16349 /* See above. */
16350 static tree
16351 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16352 {
16353 tree type_decl;
16354
16355 /* We can't know yet whether it is a class-name or not. */
16356 cp_parser_parse_tentatively (parser);
16357 /* Try a class-name. */
16358 type_decl = cp_parser_class_name (parser,
16359 typename_keyword_p,
16360 /*template_keyword_p=*/false,
16361 none_type,
16362 /*check_dependency_p=*/true,
16363 /*class_head_p=*/false,
16364 /*is_declaration=*/false);
16365 /* If it's not a class-name, keep looking. */
16366 if (!cp_parser_parse_definitely (parser))
16367 {
16368 if (cxx_dialect < cxx11)
16369 /* It must be a typedef-name or an enum-name. */
16370 return cp_parser_nonclass_name (parser);
16371
16372 cp_parser_parse_tentatively (parser);
16373 /* It is either a simple-template-id representing an
16374 instantiation of an alias template... */
16375 type_decl = cp_parser_template_id (parser,
16376 /*template_keyword_p=*/false,
16377 /*check_dependency_p=*/true,
16378 none_type,
16379 /*is_declaration=*/false);
16380 /* Note that this must be an instantiation of an alias template
16381 because [temp.names]/6 says:
16382
16383 A template-id that names an alias template specialization
16384 is a type-name.
16385
16386 Whereas [temp.names]/7 says:
16387
16388 A simple-template-id that names a class template
16389 specialization is a class-name.
16390
16391 With concepts, this could also be a partial-concept-id that
16392 declares a non-type template parameter. */
16393 if (type_decl != NULL_TREE
16394 && TREE_CODE (type_decl) == TYPE_DECL
16395 && TYPE_DECL_ALIAS_P (type_decl))
16396 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16397 else if (is_constrained_parameter (type_decl))
16398 /* Don't do anything. */ ;
16399 else
16400 cp_parser_simulate_error (parser);
16401
16402 if (!cp_parser_parse_definitely (parser))
16403 /* ... Or a typedef-name or an enum-name. */
16404 return cp_parser_nonclass_name (parser);
16405 }
16406
16407 return type_decl;
16408 }
16409
16410 /* Check if DECL and ARGS can form a constrained-type-specifier.
16411 If ARGS is non-null, we try to form a concept check of the
16412 form DECL<?, ARGS> where ? is a wildcard that matches any
16413 kind of template argument. If ARGS is NULL, then we try to
16414 form a concept check of the form DECL<?>. */
16415
16416 static tree
16417 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16418 tree decl, tree args)
16419 {
16420 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16421
16422 /* If we a constrained-type-specifier cannot be deduced. */
16423 if (parser->prevent_constrained_type_specifiers)
16424 return NULL_TREE;
16425
16426 /* A constrained type specifier can only be found in an
16427 overload set or as a reference to a template declaration.
16428
16429 FIXME: This might be masking a bug. It's possible that
16430 that the deduction below is causing template specializations
16431 to be formed with the wildcard as an argument. */
16432 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16433 return NULL_TREE;
16434
16435 /* Try to build a call expression that evaluates the
16436 concept. This can fail if the overload set refers
16437 only to non-templates. */
16438 tree placeholder = build_nt (WILDCARD_DECL);
16439 tree check = build_concept_check (decl, placeholder, args);
16440 if (check == error_mark_node)
16441 return NULL_TREE;
16442
16443 /* Deduce the checked constraint and the prototype parameter.
16444
16445 FIXME: In certain cases, failure to deduce should be a
16446 diagnosable error. */
16447 tree conc;
16448 tree proto;
16449 if (!deduce_constrained_parameter (check, conc, proto))
16450 return NULL_TREE;
16451
16452 /* In template parameter scope, this results in a constrained
16453 parameter. Return a descriptor of that parm. */
16454 if (processing_template_parmlist)
16455 return build_constrained_parameter (conc, proto, args);
16456
16457 /* In a parameter-declaration-clause, constrained-type
16458 specifiers result in invented template parameters. */
16459 if (parser->auto_is_implicit_function_template_parm_p)
16460 {
16461 tree x = build_constrained_parameter (conc, proto, args);
16462 return synthesize_implicit_template_parm (parser, x);
16463 }
16464 else
16465 {
16466 /* Otherwise, we're in a context where the constrained
16467 type name is deduced and the constraint applies
16468 after deduction. */
16469 return make_constrained_auto (conc, args);
16470 }
16471
16472 return NULL_TREE;
16473 }
16474
16475 /* If DECL refers to a concept, return a TYPE_DECL representing
16476 the result of using the constrained type specifier in the
16477 current context. DECL refers to a concept if
16478
16479 - it is an overload set containing a function concept taking a single
16480 type argument, or
16481
16482 - it is a variable concept taking a single type argument. */
16483
16484 static tree
16485 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16486 {
16487 if (flag_concepts
16488 && (TREE_CODE (decl) == OVERLOAD
16489 || BASELINK_P (decl)
16490 || variable_concept_p (decl)))
16491 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16492 else
16493 return NULL_TREE;
16494 }
16495
16496 /* Check if DECL and ARGS form a partial-concept-id. If so,
16497 assign ID to the resulting constrained placeholder.
16498
16499 Returns true if the partial-concept-id designates a placeholder
16500 and false otherwise. Note that *id is set to NULL_TREE in
16501 this case. */
16502
16503 static tree
16504 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16505 {
16506 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16507 }
16508
16509 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16510 or a concept-name.
16511
16512 enum-name:
16513 identifier
16514
16515 typedef-name:
16516 identifier
16517
16518 concept-name:
16519 identifier
16520
16521 Returns a TYPE_DECL for the type. */
16522
16523 static tree
16524 cp_parser_nonclass_name (cp_parser* parser)
16525 {
16526 tree type_decl;
16527 tree identifier;
16528
16529 cp_token *token = cp_lexer_peek_token (parser->lexer);
16530 identifier = cp_parser_identifier (parser);
16531 if (identifier == error_mark_node)
16532 return error_mark_node;
16533
16534 /* Look up the type-name. */
16535 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16536
16537 type_decl = strip_using_decl (type_decl);
16538
16539 /* If we found an overload set, then it may refer to a concept-name. */
16540 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16541 type_decl = decl;
16542
16543 if (TREE_CODE (type_decl) != TYPE_DECL
16544 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16545 {
16546 /* See if this is an Objective-C type. */
16547 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16548 tree type = objc_get_protocol_qualified_type (identifier, protos);
16549 if (type)
16550 type_decl = TYPE_NAME (type);
16551 }
16552
16553 /* Issue an error if we did not find a type-name. */
16554 if (TREE_CODE (type_decl) != TYPE_DECL
16555 /* In Objective-C, we have the complication that class names are
16556 normally type names and start declarations (eg, the
16557 "NSObject" in "NSObject *object;"), but can be used in an
16558 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16559 is an expression. So, a classname followed by a dot is not a
16560 valid type-name. */
16561 || (objc_is_class_name (TREE_TYPE (type_decl))
16562 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16563 {
16564 if (!cp_parser_simulate_error (parser))
16565 cp_parser_name_lookup_error (parser, identifier, type_decl,
16566 NLE_TYPE, token->location);
16567 return error_mark_node;
16568 }
16569 /* Remember that the name was used in the definition of the
16570 current class so that we can check later to see if the
16571 meaning would have been different after the class was
16572 entirely defined. */
16573 else if (type_decl != error_mark_node
16574 && !parser->scope)
16575 maybe_note_name_used_in_class (identifier, type_decl);
16576
16577 return type_decl;
16578 }
16579
16580 /* Parse an elaborated-type-specifier. Note that the grammar given
16581 here incorporates the resolution to DR68.
16582
16583 elaborated-type-specifier:
16584 class-key :: [opt] nested-name-specifier [opt] identifier
16585 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16586 enum-key :: [opt] nested-name-specifier [opt] identifier
16587 typename :: [opt] nested-name-specifier identifier
16588 typename :: [opt] nested-name-specifier template [opt]
16589 template-id
16590
16591 GNU extension:
16592
16593 elaborated-type-specifier:
16594 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16595 class-key attributes :: [opt] nested-name-specifier [opt]
16596 template [opt] template-id
16597 enum attributes :: [opt] nested-name-specifier [opt] identifier
16598
16599 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16600 declared `friend'. If IS_DECLARATION is TRUE, then this
16601 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16602 something is being declared.
16603
16604 Returns the TYPE specified. */
16605
16606 static tree
16607 cp_parser_elaborated_type_specifier (cp_parser* parser,
16608 bool is_friend,
16609 bool is_declaration)
16610 {
16611 enum tag_types tag_type;
16612 tree identifier;
16613 tree type = NULL_TREE;
16614 tree attributes = NULL_TREE;
16615 tree globalscope;
16616 cp_token *token = NULL;
16617
16618 /* See if we're looking at the `enum' keyword. */
16619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16620 {
16621 /* Consume the `enum' token. */
16622 cp_lexer_consume_token (parser->lexer);
16623 /* Remember that it's an enumeration type. */
16624 tag_type = enum_type;
16625 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16626 enums) is used here. */
16627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16628 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16629 {
16630 pedwarn (input_location, 0, "elaborated-type-specifier "
16631 "for a scoped enum must not use the %<%D%> keyword",
16632 cp_lexer_peek_token (parser->lexer)->u.value);
16633 /* Consume the `struct' or `class' and parse it anyway. */
16634 cp_lexer_consume_token (parser->lexer);
16635 }
16636 /* Parse the attributes. */
16637 attributes = cp_parser_attributes_opt (parser);
16638 }
16639 /* Or, it might be `typename'. */
16640 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16641 RID_TYPENAME))
16642 {
16643 /* Consume the `typename' token. */
16644 cp_lexer_consume_token (parser->lexer);
16645 /* Remember that it's a `typename' type. */
16646 tag_type = typename_type;
16647 }
16648 /* Otherwise it must be a class-key. */
16649 else
16650 {
16651 tag_type = cp_parser_class_key (parser);
16652 if (tag_type == none_type)
16653 return error_mark_node;
16654 /* Parse the attributes. */
16655 attributes = cp_parser_attributes_opt (parser);
16656 }
16657
16658 /* Look for the `::' operator. */
16659 globalscope = cp_parser_global_scope_opt (parser,
16660 /*current_scope_valid_p=*/false);
16661 /* Look for the nested-name-specifier. */
16662 if (tag_type == typename_type && !globalscope)
16663 {
16664 if (!cp_parser_nested_name_specifier (parser,
16665 /*typename_keyword_p=*/true,
16666 /*check_dependency_p=*/true,
16667 /*type_p=*/true,
16668 is_declaration))
16669 return error_mark_node;
16670 }
16671 else
16672 /* Even though `typename' is not present, the proposed resolution
16673 to Core Issue 180 says that in `class A<T>::B', `B' should be
16674 considered a type-name, even if `A<T>' is dependent. */
16675 cp_parser_nested_name_specifier_opt (parser,
16676 /*typename_keyword_p=*/true,
16677 /*check_dependency_p=*/true,
16678 /*type_p=*/true,
16679 is_declaration);
16680 /* For everything but enumeration types, consider a template-id.
16681 For an enumeration type, consider only a plain identifier. */
16682 if (tag_type != enum_type)
16683 {
16684 bool template_p = false;
16685 tree decl;
16686
16687 /* Allow the `template' keyword. */
16688 template_p = cp_parser_optional_template_keyword (parser);
16689 /* If we didn't see `template', we don't know if there's a
16690 template-id or not. */
16691 if (!template_p)
16692 cp_parser_parse_tentatively (parser);
16693 /* Parse the template-id. */
16694 token = cp_lexer_peek_token (parser->lexer);
16695 decl = cp_parser_template_id (parser, template_p,
16696 /*check_dependency_p=*/true,
16697 tag_type,
16698 is_declaration);
16699 /* If we didn't find a template-id, look for an ordinary
16700 identifier. */
16701 if (!template_p && !cp_parser_parse_definitely (parser))
16702 ;
16703 /* We can get here when cp_parser_template_id, called by
16704 cp_parser_class_name with tag_type == none_type, succeeds
16705 and caches a BASELINK. Then, when called again here,
16706 instead of failing and returning an error_mark_node
16707 returns it (see template/typename17.C in C++11).
16708 ??? Could we diagnose this earlier? */
16709 else if (tag_type == typename_type && BASELINK_P (decl))
16710 {
16711 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16712 type = error_mark_node;
16713 }
16714 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16715 in effect, then we must assume that, upon instantiation, the
16716 template will correspond to a class. */
16717 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16718 && tag_type == typename_type)
16719 type = make_typename_type (parser->scope, decl,
16720 typename_type,
16721 /*complain=*/tf_error);
16722 /* If the `typename' keyword is in effect and DECL is not a type
16723 decl, then type is non existent. */
16724 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16725 ;
16726 else if (TREE_CODE (decl) == TYPE_DECL)
16727 type = check_elaborated_type_specifier (tag_type, decl,
16728 /*allow_template_p=*/true);
16729 else if (decl == error_mark_node)
16730 type = error_mark_node;
16731 }
16732
16733 if (!type)
16734 {
16735 token = cp_lexer_peek_token (parser->lexer);
16736 identifier = cp_parser_identifier (parser);
16737
16738 if (identifier == error_mark_node)
16739 {
16740 parser->scope = NULL_TREE;
16741 return error_mark_node;
16742 }
16743
16744 /* For a `typename', we needn't call xref_tag. */
16745 if (tag_type == typename_type
16746 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16747 return cp_parser_make_typename_type (parser, identifier,
16748 token->location);
16749
16750 /* Template parameter lists apply only if we are not within a
16751 function parameter list. */
16752 bool template_parm_lists_apply
16753 = parser->num_template_parameter_lists;
16754 if (template_parm_lists_apply)
16755 for (cp_binding_level *s = current_binding_level;
16756 s && s->kind != sk_template_parms;
16757 s = s->level_chain)
16758 if (s->kind == sk_function_parms)
16759 template_parm_lists_apply = false;
16760
16761 /* Look up a qualified name in the usual way. */
16762 if (parser->scope)
16763 {
16764 tree decl;
16765 tree ambiguous_decls;
16766
16767 decl = cp_parser_lookup_name (parser, identifier,
16768 tag_type,
16769 /*is_template=*/false,
16770 /*is_namespace=*/false,
16771 /*check_dependency=*/true,
16772 &ambiguous_decls,
16773 token->location);
16774
16775 /* If the lookup was ambiguous, an error will already have been
16776 issued. */
16777 if (ambiguous_decls)
16778 return error_mark_node;
16779
16780 /* If we are parsing friend declaration, DECL may be a
16781 TEMPLATE_DECL tree node here. However, we need to check
16782 whether this TEMPLATE_DECL results in valid code. Consider
16783 the following example:
16784
16785 namespace N {
16786 template <class T> class C {};
16787 }
16788 class X {
16789 template <class T> friend class N::C; // #1, valid code
16790 };
16791 template <class T> class Y {
16792 friend class N::C; // #2, invalid code
16793 };
16794
16795 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16796 name lookup of `N::C'. We see that friend declaration must
16797 be template for the code to be valid. Note that
16798 processing_template_decl does not work here since it is
16799 always 1 for the above two cases. */
16800
16801 decl = (cp_parser_maybe_treat_template_as_class
16802 (decl, /*tag_name_p=*/is_friend
16803 && template_parm_lists_apply));
16804
16805 if (TREE_CODE (decl) != TYPE_DECL)
16806 {
16807 cp_parser_diagnose_invalid_type_name (parser,
16808 identifier,
16809 token->location);
16810 return error_mark_node;
16811 }
16812
16813 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16814 {
16815 bool allow_template = (template_parm_lists_apply
16816 || DECL_SELF_REFERENCE_P (decl));
16817 type = check_elaborated_type_specifier (tag_type, decl,
16818 allow_template);
16819
16820 if (type == error_mark_node)
16821 return error_mark_node;
16822 }
16823
16824 /* Forward declarations of nested types, such as
16825
16826 class C1::C2;
16827 class C1::C2::C3;
16828
16829 are invalid unless all components preceding the final '::'
16830 are complete. If all enclosing types are complete, these
16831 declarations become merely pointless.
16832
16833 Invalid forward declarations of nested types are errors
16834 caught elsewhere in parsing. Those that are pointless arrive
16835 here. */
16836
16837 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16838 && !is_friend && !processing_explicit_instantiation)
16839 warning (0, "declaration %qD does not declare anything", decl);
16840
16841 type = TREE_TYPE (decl);
16842 }
16843 else
16844 {
16845 /* An elaborated-type-specifier sometimes introduces a new type and
16846 sometimes names an existing type. Normally, the rule is that it
16847 introduces a new type only if there is not an existing type of
16848 the same name already in scope. For example, given:
16849
16850 struct S {};
16851 void f() { struct S s; }
16852
16853 the `struct S' in the body of `f' is the same `struct S' as in
16854 the global scope; the existing definition is used. However, if
16855 there were no global declaration, this would introduce a new
16856 local class named `S'.
16857
16858 An exception to this rule applies to the following code:
16859
16860 namespace N { struct S; }
16861
16862 Here, the elaborated-type-specifier names a new type
16863 unconditionally; even if there is already an `S' in the
16864 containing scope this declaration names a new type.
16865 This exception only applies if the elaborated-type-specifier
16866 forms the complete declaration:
16867
16868 [class.name]
16869
16870 A declaration consisting solely of `class-key identifier ;' is
16871 either a redeclaration of the name in the current scope or a
16872 forward declaration of the identifier as a class name. It
16873 introduces the name into the current scope.
16874
16875 We are in this situation precisely when the next token is a `;'.
16876
16877 An exception to the exception is that a `friend' declaration does
16878 *not* name a new type; i.e., given:
16879
16880 struct S { friend struct T; };
16881
16882 `T' is not a new type in the scope of `S'.
16883
16884 Also, `new struct S' or `sizeof (struct S)' never results in the
16885 definition of a new type; a new type can only be declared in a
16886 declaration context. */
16887
16888 tag_scope ts;
16889 bool template_p;
16890
16891 if (is_friend)
16892 /* Friends have special name lookup rules. */
16893 ts = ts_within_enclosing_non_class;
16894 else if (is_declaration
16895 && cp_lexer_next_token_is (parser->lexer,
16896 CPP_SEMICOLON))
16897 /* This is a `class-key identifier ;' */
16898 ts = ts_current;
16899 else
16900 ts = ts_global;
16901
16902 template_p =
16903 (template_parm_lists_apply
16904 && (cp_parser_next_token_starts_class_definition_p (parser)
16905 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16906 /* An unqualified name was used to reference this type, so
16907 there were no qualifying templates. */
16908 if (template_parm_lists_apply
16909 && !cp_parser_check_template_parameters (parser,
16910 /*num_templates=*/0,
16911 token->location,
16912 /*declarator=*/NULL))
16913 return error_mark_node;
16914 type = xref_tag (tag_type, identifier, ts, template_p);
16915 }
16916 }
16917
16918 if (type == error_mark_node)
16919 return error_mark_node;
16920
16921 /* Allow attributes on forward declarations of classes. */
16922 if (attributes)
16923 {
16924 if (TREE_CODE (type) == TYPENAME_TYPE)
16925 warning (OPT_Wattributes,
16926 "attributes ignored on uninstantiated type");
16927 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16928 && ! processing_explicit_instantiation)
16929 warning (OPT_Wattributes,
16930 "attributes ignored on template instantiation");
16931 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16932 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16933 else
16934 warning (OPT_Wattributes,
16935 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16936 }
16937
16938 if (tag_type != enum_type)
16939 {
16940 /* Indicate whether this class was declared as a `class' or as a
16941 `struct'. */
16942 if (CLASS_TYPE_P (type))
16943 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16944 cp_parser_check_class_key (tag_type, type);
16945 }
16946
16947 /* A "<" cannot follow an elaborated type specifier. If that
16948 happens, the user was probably trying to form a template-id. */
16949 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16950 token->location);
16951
16952 return type;
16953 }
16954
16955 /* Parse an enum-specifier.
16956
16957 enum-specifier:
16958 enum-head { enumerator-list [opt] }
16959 enum-head { enumerator-list , } [C++0x]
16960
16961 enum-head:
16962 enum-key identifier [opt] enum-base [opt]
16963 enum-key nested-name-specifier identifier enum-base [opt]
16964
16965 enum-key:
16966 enum
16967 enum class [C++0x]
16968 enum struct [C++0x]
16969
16970 enum-base: [C++0x]
16971 : type-specifier-seq
16972
16973 opaque-enum-specifier:
16974 enum-key identifier enum-base [opt] ;
16975
16976 GNU Extensions:
16977 enum-key attributes[opt] identifier [opt] enum-base [opt]
16978 { enumerator-list [opt] }attributes[opt]
16979 enum-key attributes[opt] identifier [opt] enum-base [opt]
16980 { enumerator-list, }attributes[opt] [C++0x]
16981
16982 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16983 if the token stream isn't an enum-specifier after all. */
16984
16985 static tree
16986 cp_parser_enum_specifier (cp_parser* parser)
16987 {
16988 tree identifier;
16989 tree type = NULL_TREE;
16990 tree prev_scope;
16991 tree nested_name_specifier = NULL_TREE;
16992 tree attributes;
16993 bool scoped_enum_p = false;
16994 bool has_underlying_type = false;
16995 bool nested_being_defined = false;
16996 bool new_value_list = false;
16997 bool is_new_type = false;
16998 bool is_anonymous = false;
16999 tree underlying_type = NULL_TREE;
17000 cp_token *type_start_token = NULL;
17001 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17002
17003 parser->colon_corrects_to_scope_p = false;
17004
17005 /* Parse tentatively so that we can back up if we don't find a
17006 enum-specifier. */
17007 cp_parser_parse_tentatively (parser);
17008
17009 /* Caller guarantees that the current token is 'enum', an identifier
17010 possibly follows, and the token after that is an opening brace.
17011 If we don't have an identifier, fabricate an anonymous name for
17012 the enumeration being defined. */
17013 cp_lexer_consume_token (parser->lexer);
17014
17015 /* Parse the "class" or "struct", which indicates a scoped
17016 enumeration type in C++0x. */
17017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17018 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17019 {
17020 if (cxx_dialect < cxx11)
17021 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17022
17023 /* Consume the `struct' or `class' token. */
17024 cp_lexer_consume_token (parser->lexer);
17025
17026 scoped_enum_p = true;
17027 }
17028
17029 attributes = cp_parser_attributes_opt (parser);
17030
17031 /* Clear the qualification. */
17032 parser->scope = NULL_TREE;
17033 parser->qualifying_scope = NULL_TREE;
17034 parser->object_scope = NULL_TREE;
17035
17036 /* Figure out in what scope the declaration is being placed. */
17037 prev_scope = current_scope ();
17038
17039 type_start_token = cp_lexer_peek_token (parser->lexer);
17040
17041 push_deferring_access_checks (dk_no_check);
17042 nested_name_specifier
17043 = cp_parser_nested_name_specifier_opt (parser,
17044 /*typename_keyword_p=*/true,
17045 /*check_dependency_p=*/false,
17046 /*type_p=*/false,
17047 /*is_declaration=*/false);
17048
17049 if (nested_name_specifier)
17050 {
17051 tree name;
17052
17053 identifier = cp_parser_identifier (parser);
17054 name = cp_parser_lookup_name (parser, identifier,
17055 enum_type,
17056 /*is_template=*/false,
17057 /*is_namespace=*/false,
17058 /*check_dependency=*/true,
17059 /*ambiguous_decls=*/NULL,
17060 input_location);
17061 if (name && name != error_mark_node)
17062 {
17063 type = TREE_TYPE (name);
17064 if (TREE_CODE (type) == TYPENAME_TYPE)
17065 {
17066 /* Are template enums allowed in ISO? */
17067 if (template_parm_scope_p ())
17068 pedwarn (type_start_token->location, OPT_Wpedantic,
17069 "%qD is an enumeration template", name);
17070 /* ignore a typename reference, for it will be solved by name
17071 in start_enum. */
17072 type = NULL_TREE;
17073 }
17074 }
17075 else if (nested_name_specifier == error_mark_node)
17076 /* We already issued an error. */;
17077 else
17078 {
17079 error_at (type_start_token->location,
17080 "%qD does not name an enumeration in %qT",
17081 identifier, nested_name_specifier);
17082 nested_name_specifier = error_mark_node;
17083 }
17084 }
17085 else
17086 {
17087 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17088 identifier = cp_parser_identifier (parser);
17089 else
17090 {
17091 identifier = make_anon_name ();
17092 is_anonymous = true;
17093 if (scoped_enum_p)
17094 error_at (type_start_token->location,
17095 "anonymous scoped enum is not allowed");
17096 }
17097 }
17098 pop_deferring_access_checks ();
17099
17100 /* Check for the `:' that denotes a specified underlying type in C++0x.
17101 Note that a ':' could also indicate a bitfield width, however. */
17102 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17103 {
17104 cp_decl_specifier_seq type_specifiers;
17105
17106 /* Consume the `:'. */
17107 cp_lexer_consume_token (parser->lexer);
17108
17109 /* Parse the type-specifier-seq. */
17110 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17111 /*is_trailing_return=*/false,
17112 &type_specifiers);
17113
17114 /* At this point this is surely not elaborated type specifier. */
17115 if (!cp_parser_parse_definitely (parser))
17116 return NULL_TREE;
17117
17118 if (cxx_dialect < cxx11)
17119 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17120
17121 has_underlying_type = true;
17122
17123 /* If that didn't work, stop. */
17124 if (type_specifiers.type != error_mark_node)
17125 {
17126 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17127 /*initialized=*/0, NULL);
17128 if (underlying_type == error_mark_node
17129 || check_for_bare_parameter_packs (underlying_type))
17130 underlying_type = NULL_TREE;
17131 }
17132 }
17133
17134 /* Look for the `{' but don't consume it yet. */
17135 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17136 {
17137 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17138 {
17139 cp_parser_error (parser, "expected %<{%>");
17140 if (has_underlying_type)
17141 {
17142 type = NULL_TREE;
17143 goto out;
17144 }
17145 }
17146 /* An opaque-enum-specifier must have a ';' here. */
17147 if ((scoped_enum_p || underlying_type)
17148 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17149 {
17150 cp_parser_error (parser, "expected %<;%> or %<{%>");
17151 if (has_underlying_type)
17152 {
17153 type = NULL_TREE;
17154 goto out;
17155 }
17156 }
17157 }
17158
17159 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17160 return NULL_TREE;
17161
17162 if (nested_name_specifier)
17163 {
17164 if (CLASS_TYPE_P (nested_name_specifier))
17165 {
17166 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17167 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17168 push_scope (nested_name_specifier);
17169 }
17170 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17171 {
17172 push_nested_namespace (nested_name_specifier);
17173 }
17174 }
17175
17176 /* Issue an error message if type-definitions are forbidden here. */
17177 if (!cp_parser_check_type_definition (parser))
17178 type = error_mark_node;
17179 else
17180 /* Create the new type. We do this before consuming the opening
17181 brace so the enum will be recorded as being on the line of its
17182 tag (or the 'enum' keyword, if there is no tag). */
17183 type = start_enum (identifier, type, underlying_type,
17184 attributes, scoped_enum_p, &is_new_type);
17185
17186 /* If the next token is not '{' it is an opaque-enum-specifier or an
17187 elaborated-type-specifier. */
17188 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17189 {
17190 timevar_push (TV_PARSE_ENUM);
17191 if (nested_name_specifier
17192 && nested_name_specifier != error_mark_node)
17193 {
17194 /* The following catches invalid code such as:
17195 enum class S<int>::E { A, B, C }; */
17196 if (!processing_specialization
17197 && CLASS_TYPE_P (nested_name_specifier)
17198 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17199 error_at (type_start_token->location, "cannot add an enumerator "
17200 "list to a template instantiation");
17201
17202 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17203 {
17204 error_at (type_start_token->location,
17205 "%<%T::%E%> has not been declared",
17206 TYPE_CONTEXT (nested_name_specifier),
17207 nested_name_specifier);
17208 type = error_mark_node;
17209 }
17210 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17211 && !CLASS_TYPE_P (nested_name_specifier))
17212 {
17213 error_at (type_start_token->location, "nested name specifier "
17214 "%qT for enum declaration does not name a class "
17215 "or namespace", nested_name_specifier);
17216 type = error_mark_node;
17217 }
17218 /* If that scope does not contain the scope in which the
17219 class was originally declared, the program is invalid. */
17220 else if (prev_scope && !is_ancestor (prev_scope,
17221 nested_name_specifier))
17222 {
17223 if (at_namespace_scope_p ())
17224 error_at (type_start_token->location,
17225 "declaration of %qD in namespace %qD which does not "
17226 "enclose %qD",
17227 type, prev_scope, nested_name_specifier);
17228 else
17229 error_at (type_start_token->location,
17230 "declaration of %qD in %qD which does not "
17231 "enclose %qD",
17232 type, prev_scope, nested_name_specifier);
17233 type = error_mark_node;
17234 }
17235 /* If that scope is the scope where the declaration is being placed
17236 the program is invalid. */
17237 else if (CLASS_TYPE_P (nested_name_specifier)
17238 && CLASS_TYPE_P (prev_scope)
17239 && same_type_p (nested_name_specifier, prev_scope))
17240 {
17241 permerror (type_start_token->location,
17242 "extra qualification not allowed");
17243 nested_name_specifier = NULL_TREE;
17244 }
17245 }
17246
17247 if (scoped_enum_p)
17248 begin_scope (sk_scoped_enum, type);
17249
17250 /* Consume the opening brace. */
17251 cp_lexer_consume_token (parser->lexer);
17252
17253 if (type == error_mark_node)
17254 ; /* Nothing to add */
17255 else if (OPAQUE_ENUM_P (type)
17256 || (cxx_dialect > cxx98 && processing_specialization))
17257 {
17258 new_value_list = true;
17259 SET_OPAQUE_ENUM_P (type, false);
17260 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17261 }
17262 else
17263 {
17264 error_at (type_start_token->location,
17265 "multiple definition of %q#T", type);
17266 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17267 "previous definition here");
17268 type = error_mark_node;
17269 }
17270
17271 if (type == error_mark_node)
17272 cp_parser_skip_to_end_of_block_or_statement (parser);
17273 /* If the next token is not '}', then there are some enumerators. */
17274 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17275 {
17276 if (is_anonymous && !scoped_enum_p)
17277 pedwarn (type_start_token->location, OPT_Wpedantic,
17278 "ISO C++ forbids empty anonymous enum");
17279 }
17280 else
17281 cp_parser_enumerator_list (parser, type);
17282
17283 /* Consume the final '}'. */
17284 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17285
17286 if (scoped_enum_p)
17287 finish_scope ();
17288 timevar_pop (TV_PARSE_ENUM);
17289 }
17290 else
17291 {
17292 /* If a ';' follows, then it is an opaque-enum-specifier
17293 and additional restrictions apply. */
17294 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17295 {
17296 if (is_anonymous)
17297 error_at (type_start_token->location,
17298 "opaque-enum-specifier without name");
17299 else if (nested_name_specifier)
17300 error_at (type_start_token->location,
17301 "opaque-enum-specifier must use a simple identifier");
17302 }
17303 }
17304
17305 /* Look for trailing attributes to apply to this enumeration, and
17306 apply them if appropriate. */
17307 if (cp_parser_allow_gnu_extensions_p (parser))
17308 {
17309 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17310 cplus_decl_attributes (&type,
17311 trailing_attr,
17312 (int) ATTR_FLAG_TYPE_IN_PLACE);
17313 }
17314
17315 /* Finish up the enumeration. */
17316 if (type != error_mark_node)
17317 {
17318 if (new_value_list)
17319 finish_enum_value_list (type);
17320 if (is_new_type)
17321 finish_enum (type);
17322 }
17323
17324 if (nested_name_specifier)
17325 {
17326 if (CLASS_TYPE_P (nested_name_specifier))
17327 {
17328 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17329 pop_scope (nested_name_specifier);
17330 }
17331 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17332 {
17333 pop_nested_namespace (nested_name_specifier);
17334 }
17335 }
17336 out:
17337 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17338 return type;
17339 }
17340
17341 /* Parse an enumerator-list. The enumerators all have the indicated
17342 TYPE.
17343
17344 enumerator-list:
17345 enumerator-definition
17346 enumerator-list , enumerator-definition */
17347
17348 static void
17349 cp_parser_enumerator_list (cp_parser* parser, tree type)
17350 {
17351 while (true)
17352 {
17353 /* Parse an enumerator-definition. */
17354 cp_parser_enumerator_definition (parser, type);
17355
17356 /* If the next token is not a ',', we've reached the end of
17357 the list. */
17358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17359 break;
17360 /* Otherwise, consume the `,' and keep going. */
17361 cp_lexer_consume_token (parser->lexer);
17362 /* If the next token is a `}', there is a trailing comma. */
17363 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17364 {
17365 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17366 pedwarn (input_location, OPT_Wpedantic,
17367 "comma at end of enumerator list");
17368 break;
17369 }
17370 }
17371 }
17372
17373 /* Parse an enumerator-definition. The enumerator has the indicated
17374 TYPE.
17375
17376 enumerator-definition:
17377 enumerator
17378 enumerator = constant-expression
17379
17380 enumerator:
17381 identifier
17382
17383 GNU Extensions:
17384
17385 enumerator-definition:
17386 enumerator attributes [opt]
17387 enumerator attributes [opt] = constant-expression */
17388
17389 static void
17390 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17391 {
17392 tree identifier;
17393 tree value;
17394 location_t loc;
17395
17396 /* Save the input location because we are interested in the location
17397 of the identifier and not the location of the explicit value. */
17398 loc = cp_lexer_peek_token (parser->lexer)->location;
17399
17400 /* Look for the identifier. */
17401 identifier = cp_parser_identifier (parser);
17402 if (identifier == error_mark_node)
17403 return;
17404
17405 /* Parse any specified attributes. */
17406 tree attrs = cp_parser_attributes_opt (parser);
17407
17408 /* If the next token is an '=', then there is an explicit value. */
17409 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17410 {
17411 /* Consume the `=' token. */
17412 cp_lexer_consume_token (parser->lexer);
17413 /* Parse the value. */
17414 value = cp_parser_constant_expression (parser);
17415 }
17416 else
17417 value = NULL_TREE;
17418
17419 /* If we are processing a template, make sure the initializer of the
17420 enumerator doesn't contain any bare template parameter pack. */
17421 if (check_for_bare_parameter_packs (value))
17422 value = error_mark_node;
17423
17424 /* Create the enumerator. */
17425 build_enumerator (identifier, value, type, attrs, loc);
17426 }
17427
17428 /* Parse a namespace-name.
17429
17430 namespace-name:
17431 original-namespace-name
17432 namespace-alias
17433
17434 Returns the NAMESPACE_DECL for the namespace. */
17435
17436 static tree
17437 cp_parser_namespace_name (cp_parser* parser)
17438 {
17439 tree identifier;
17440 tree namespace_decl;
17441
17442 cp_token *token = cp_lexer_peek_token (parser->lexer);
17443
17444 /* Get the name of the namespace. */
17445 identifier = cp_parser_identifier (parser);
17446 if (identifier == error_mark_node)
17447 return error_mark_node;
17448
17449 /* Look up the identifier in the currently active scope. Look only
17450 for namespaces, due to:
17451
17452 [basic.lookup.udir]
17453
17454 When looking up a namespace-name in a using-directive or alias
17455 definition, only namespace names are considered.
17456
17457 And:
17458
17459 [basic.lookup.qual]
17460
17461 During the lookup of a name preceding the :: scope resolution
17462 operator, object, function, and enumerator names are ignored.
17463
17464 (Note that cp_parser_qualifying_entity only calls this
17465 function if the token after the name is the scope resolution
17466 operator.) */
17467 namespace_decl = cp_parser_lookup_name (parser, identifier,
17468 none_type,
17469 /*is_template=*/false,
17470 /*is_namespace=*/true,
17471 /*check_dependency=*/true,
17472 /*ambiguous_decls=*/NULL,
17473 token->location);
17474 /* If it's not a namespace, issue an error. */
17475 if (namespace_decl == error_mark_node
17476 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17477 {
17478 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17479 error_at (token->location, "%qD is not a namespace-name", identifier);
17480 cp_parser_error (parser, "expected namespace-name");
17481 namespace_decl = error_mark_node;
17482 }
17483
17484 return namespace_decl;
17485 }
17486
17487 /* Parse a namespace-definition.
17488
17489 namespace-definition:
17490 named-namespace-definition
17491 unnamed-namespace-definition
17492
17493 named-namespace-definition:
17494 original-namespace-definition
17495 extension-namespace-definition
17496
17497 original-namespace-definition:
17498 namespace identifier { namespace-body }
17499
17500 extension-namespace-definition:
17501 namespace original-namespace-name { namespace-body }
17502
17503 unnamed-namespace-definition:
17504 namespace { namespace-body } */
17505
17506 static void
17507 cp_parser_namespace_definition (cp_parser* parser)
17508 {
17509 tree identifier, attribs;
17510 bool has_visibility;
17511 bool is_inline;
17512 cp_token* token;
17513 int nested_definition_count = 0;
17514
17515 cp_ensure_no_omp_declare_simd (parser);
17516 cp_ensure_no_oacc_routine (parser);
17517 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17518 {
17519 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17520 is_inline = true;
17521 cp_lexer_consume_token (parser->lexer);
17522 }
17523 else
17524 is_inline = false;
17525
17526 /* Look for the `namespace' keyword. */
17527 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17528
17529 /* Parse any specified attributes before the identifier. */
17530 attribs = cp_parser_attributes_opt (parser);
17531
17532 /* Get the name of the namespace. We do not attempt to distinguish
17533 between an original-namespace-definition and an
17534 extension-namespace-definition at this point. The semantic
17535 analysis routines are responsible for that. */
17536 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17537 identifier = cp_parser_identifier (parser);
17538 else
17539 identifier = NULL_TREE;
17540
17541 /* Parse any specified attributes after the identifier. */
17542 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17543 if (post_ident_attribs)
17544 {
17545 if (attribs)
17546 attribs = chainon (attribs, post_ident_attribs);
17547 else
17548 attribs = post_ident_attribs;
17549 }
17550
17551 /* Start the namespace. */
17552 push_namespace (identifier);
17553
17554 /* Parse any nested namespace definition. */
17555 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17556 {
17557 if (attribs)
17558 error_at (token->location, "a nested namespace definition cannot have attributes");
17559 if (cxx_dialect < cxx1z)
17560 pedwarn (input_location, OPT_Wpedantic,
17561 "nested namespace definitions only available with "
17562 "-std=c++1z or -std=gnu++1z");
17563 if (is_inline)
17564 error_at (token->location, "a nested namespace definition cannot be inline");
17565 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17566 {
17567 cp_lexer_consume_token (parser->lexer);
17568 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17569 identifier = cp_parser_identifier (parser);
17570 else
17571 {
17572 cp_parser_error (parser, "nested identifier required");
17573 break;
17574 }
17575 ++nested_definition_count;
17576 push_namespace (identifier);
17577 }
17578 }
17579
17580 /* Look for the `{' to validate starting the namespace. */
17581 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17582
17583 /* "inline namespace" is equivalent to a stub namespace definition
17584 followed by a strong using directive. */
17585 if (is_inline)
17586 {
17587 tree name_space = current_namespace;
17588 /* Set up namespace association. */
17589 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17590 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17591 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17592 /* Import the contents of the inline namespace. */
17593 pop_namespace ();
17594 do_using_directive (name_space);
17595 push_namespace (identifier);
17596 }
17597
17598 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17599
17600 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17601
17602 /* Parse the body of the namespace. */
17603 cp_parser_namespace_body (parser);
17604
17605 if (has_visibility)
17606 pop_visibility (1);
17607
17608 /* Finish the nested namespace definitions. */
17609 while (nested_definition_count--)
17610 pop_namespace ();
17611
17612 /* Finish the namespace. */
17613 pop_namespace ();
17614 /* Look for the final `}'. */
17615 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17616 }
17617
17618 /* Parse a namespace-body.
17619
17620 namespace-body:
17621 declaration-seq [opt] */
17622
17623 static void
17624 cp_parser_namespace_body (cp_parser* parser)
17625 {
17626 cp_parser_declaration_seq_opt (parser);
17627 }
17628
17629 /* Parse a namespace-alias-definition.
17630
17631 namespace-alias-definition:
17632 namespace identifier = qualified-namespace-specifier ; */
17633
17634 static void
17635 cp_parser_namespace_alias_definition (cp_parser* parser)
17636 {
17637 tree identifier;
17638 tree namespace_specifier;
17639
17640 cp_token *token = cp_lexer_peek_token (parser->lexer);
17641
17642 /* Look for the `namespace' keyword. */
17643 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17644 /* Look for the identifier. */
17645 identifier = cp_parser_identifier (parser);
17646 if (identifier == error_mark_node)
17647 return;
17648 /* Look for the `=' token. */
17649 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17650 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17651 {
17652 error_at (token->location, "%<namespace%> definition is not allowed here");
17653 /* Skip the definition. */
17654 cp_lexer_consume_token (parser->lexer);
17655 if (cp_parser_skip_to_closing_brace (parser))
17656 cp_lexer_consume_token (parser->lexer);
17657 return;
17658 }
17659 cp_parser_require (parser, CPP_EQ, RT_EQ);
17660 /* Look for the qualified-namespace-specifier. */
17661 namespace_specifier
17662 = cp_parser_qualified_namespace_specifier (parser);
17663 /* Look for the `;' token. */
17664 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17665
17666 /* Register the alias in the symbol table. */
17667 do_namespace_alias (identifier, namespace_specifier);
17668 }
17669
17670 /* Parse a qualified-namespace-specifier.
17671
17672 qualified-namespace-specifier:
17673 :: [opt] nested-name-specifier [opt] namespace-name
17674
17675 Returns a NAMESPACE_DECL corresponding to the specified
17676 namespace. */
17677
17678 static tree
17679 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17680 {
17681 /* Look for the optional `::'. */
17682 cp_parser_global_scope_opt (parser,
17683 /*current_scope_valid_p=*/false);
17684
17685 /* Look for the optional nested-name-specifier. */
17686 cp_parser_nested_name_specifier_opt (parser,
17687 /*typename_keyword_p=*/false,
17688 /*check_dependency_p=*/true,
17689 /*type_p=*/false,
17690 /*is_declaration=*/true);
17691
17692 return cp_parser_namespace_name (parser);
17693 }
17694
17695 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17696 access declaration.
17697
17698 using-declaration:
17699 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17700 using :: unqualified-id ;
17701
17702 access-declaration:
17703 qualified-id ;
17704
17705 */
17706
17707 static bool
17708 cp_parser_using_declaration (cp_parser* parser,
17709 bool access_declaration_p)
17710 {
17711 cp_token *token;
17712 bool typename_p = false;
17713 bool global_scope_p;
17714 tree decl;
17715 tree identifier;
17716 tree qscope;
17717 int oldcount = errorcount;
17718 cp_token *diag_token = NULL;
17719
17720 if (access_declaration_p)
17721 {
17722 diag_token = cp_lexer_peek_token (parser->lexer);
17723 cp_parser_parse_tentatively (parser);
17724 }
17725 else
17726 {
17727 /* Look for the `using' keyword. */
17728 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17729
17730 /* Peek at the next token. */
17731 token = cp_lexer_peek_token (parser->lexer);
17732 /* See if it's `typename'. */
17733 if (token->keyword == RID_TYPENAME)
17734 {
17735 /* Remember that we've seen it. */
17736 typename_p = true;
17737 /* Consume the `typename' token. */
17738 cp_lexer_consume_token (parser->lexer);
17739 }
17740 }
17741
17742 /* Look for the optional global scope qualification. */
17743 global_scope_p
17744 = (cp_parser_global_scope_opt (parser,
17745 /*current_scope_valid_p=*/false)
17746 != NULL_TREE);
17747
17748 /* If we saw `typename', or didn't see `::', then there must be a
17749 nested-name-specifier present. */
17750 if (typename_p || !global_scope_p)
17751 {
17752 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17753 /*check_dependency_p=*/true,
17754 /*type_p=*/false,
17755 /*is_declaration=*/true);
17756 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17757 {
17758 cp_parser_skip_to_end_of_block_or_statement (parser);
17759 return false;
17760 }
17761 }
17762 /* Otherwise, we could be in either of the two productions. In that
17763 case, treat the nested-name-specifier as optional. */
17764 else
17765 qscope = cp_parser_nested_name_specifier_opt (parser,
17766 /*typename_keyword_p=*/false,
17767 /*check_dependency_p=*/true,
17768 /*type_p=*/false,
17769 /*is_declaration=*/true);
17770 if (!qscope)
17771 qscope = global_namespace;
17772 else if (UNSCOPED_ENUM_P (qscope))
17773 qscope = CP_TYPE_CONTEXT (qscope);
17774
17775 if (access_declaration_p && cp_parser_error_occurred (parser))
17776 /* Something has already gone wrong; there's no need to parse
17777 further. Since an error has occurred, the return value of
17778 cp_parser_parse_definitely will be false, as required. */
17779 return cp_parser_parse_definitely (parser);
17780
17781 token = cp_lexer_peek_token (parser->lexer);
17782 /* Parse the unqualified-id. */
17783 identifier = cp_parser_unqualified_id (parser,
17784 /*template_keyword_p=*/false,
17785 /*check_dependency_p=*/true,
17786 /*declarator_p=*/true,
17787 /*optional_p=*/false);
17788
17789 if (access_declaration_p)
17790 {
17791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17792 cp_parser_simulate_error (parser);
17793 if (!cp_parser_parse_definitely (parser))
17794 return false;
17795 }
17796
17797 /* The function we call to handle a using-declaration is different
17798 depending on what scope we are in. */
17799 if (qscope == error_mark_node || identifier == error_mark_node)
17800 ;
17801 else if (!identifier_p (identifier)
17802 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17803 /* [namespace.udecl]
17804
17805 A using declaration shall not name a template-id. */
17806 error_at (token->location,
17807 "a template-id may not appear in a using-declaration");
17808 else
17809 {
17810 if (at_class_scope_p ())
17811 {
17812 /* Create the USING_DECL. */
17813 decl = do_class_using_decl (parser->scope, identifier);
17814
17815 if (decl && typename_p)
17816 USING_DECL_TYPENAME_P (decl) = 1;
17817
17818 if (check_for_bare_parameter_packs (decl))
17819 {
17820 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17821 return false;
17822 }
17823 else
17824 /* Add it to the list of members in this class. */
17825 finish_member_declaration (decl);
17826 }
17827 else
17828 {
17829 decl = cp_parser_lookup_name_simple (parser,
17830 identifier,
17831 token->location);
17832 if (decl == error_mark_node)
17833 cp_parser_name_lookup_error (parser, identifier,
17834 decl, NLE_NULL,
17835 token->location);
17836 else if (check_for_bare_parameter_packs (decl))
17837 {
17838 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17839 return false;
17840 }
17841 else if (!at_namespace_scope_p ())
17842 do_local_using_decl (decl, qscope, identifier);
17843 else
17844 do_toplevel_using_decl (decl, qscope, identifier);
17845 }
17846 }
17847
17848 /* Look for the final `;'. */
17849 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17850
17851 if (access_declaration_p && errorcount == oldcount)
17852 warning_at (diag_token->location, OPT_Wdeprecated,
17853 "access declarations are deprecated "
17854 "in favour of using-declarations; "
17855 "suggestion: add the %<using%> keyword");
17856
17857 return true;
17858 }
17859
17860 /* Parse an alias-declaration.
17861
17862 alias-declaration:
17863 using identifier attribute-specifier-seq [opt] = type-id */
17864
17865 static tree
17866 cp_parser_alias_declaration (cp_parser* parser)
17867 {
17868 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17869 location_t id_location;
17870 cp_declarator *declarator;
17871 cp_decl_specifier_seq decl_specs;
17872 bool member_p;
17873 const char *saved_message = NULL;
17874
17875 /* Look for the `using' keyword. */
17876 cp_token *using_token
17877 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17878 if (using_token == NULL)
17879 return error_mark_node;
17880
17881 id_location = cp_lexer_peek_token (parser->lexer)->location;
17882 id = cp_parser_identifier (parser);
17883 if (id == error_mark_node)
17884 return error_mark_node;
17885
17886 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17887 attributes = cp_parser_attributes_opt (parser);
17888 if (attributes == error_mark_node)
17889 return error_mark_node;
17890
17891 cp_parser_require (parser, CPP_EQ, RT_EQ);
17892
17893 if (cp_parser_error_occurred (parser))
17894 return error_mark_node;
17895
17896 cp_parser_commit_to_tentative_parse (parser);
17897
17898 /* Now we are going to parse the type-id of the declaration. */
17899
17900 /*
17901 [dcl.type]/3 says:
17902
17903 "A type-specifier-seq shall not define a class or enumeration
17904 unless it appears in the type-id of an alias-declaration (7.1.3) that
17905 is not the declaration of a template-declaration."
17906
17907 In other words, if we currently are in an alias template, the
17908 type-id should not define a type.
17909
17910 So let's set parser->type_definition_forbidden_message in that
17911 case; cp_parser_check_type_definition (called by
17912 cp_parser_class_specifier) will then emit an error if a type is
17913 defined in the type-id. */
17914 if (parser->num_template_parameter_lists)
17915 {
17916 saved_message = parser->type_definition_forbidden_message;
17917 parser->type_definition_forbidden_message =
17918 G_("types may not be defined in alias template declarations");
17919 }
17920
17921 type = cp_parser_type_id (parser);
17922
17923 /* Restore the error message if need be. */
17924 if (parser->num_template_parameter_lists)
17925 parser->type_definition_forbidden_message = saved_message;
17926
17927 if (type == error_mark_node
17928 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17929 {
17930 cp_parser_skip_to_end_of_block_or_statement (parser);
17931 return error_mark_node;
17932 }
17933
17934 /* A typedef-name can also be introduced by an alias-declaration. The
17935 identifier following the using keyword becomes a typedef-name. It has
17936 the same semantics as if it were introduced by the typedef
17937 specifier. In particular, it does not define a new type and it shall
17938 not appear in the type-id. */
17939
17940 clear_decl_specs (&decl_specs);
17941 decl_specs.type = type;
17942 if (attributes != NULL_TREE)
17943 {
17944 decl_specs.attributes = attributes;
17945 set_and_check_decl_spec_loc (&decl_specs,
17946 ds_attribute,
17947 attrs_token);
17948 }
17949 set_and_check_decl_spec_loc (&decl_specs,
17950 ds_typedef,
17951 using_token);
17952 set_and_check_decl_spec_loc (&decl_specs,
17953 ds_alias,
17954 using_token);
17955
17956 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17957 declarator->id_loc = id_location;
17958
17959 member_p = at_class_scope_p ();
17960 if (member_p)
17961 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17962 NULL_TREE, attributes);
17963 else
17964 decl = start_decl (declarator, &decl_specs, 0,
17965 attributes, NULL_TREE, &pushed_scope);
17966 if (decl == error_mark_node)
17967 return decl;
17968
17969 // Attach constraints to the alias declaration.
17970 if (flag_concepts && current_template_parms)
17971 {
17972 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17973 tree constr = build_constraints (reqs, NULL_TREE);
17974 set_constraints (decl, constr);
17975 }
17976
17977 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17978
17979 if (pushed_scope)
17980 pop_scope (pushed_scope);
17981
17982 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17983 added into the symbol table; otherwise, return the TYPE_DECL. */
17984 if (DECL_LANG_SPECIFIC (decl)
17985 && DECL_TEMPLATE_INFO (decl)
17986 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17987 {
17988 decl = DECL_TI_TEMPLATE (decl);
17989 if (member_p)
17990 check_member_template (decl);
17991 }
17992
17993 return decl;
17994 }
17995
17996 /* Parse a using-directive.
17997
17998 using-directive:
17999 using namespace :: [opt] nested-name-specifier [opt]
18000 namespace-name ; */
18001
18002 static void
18003 cp_parser_using_directive (cp_parser* parser)
18004 {
18005 tree namespace_decl;
18006 tree attribs;
18007
18008 /* Look for the `using' keyword. */
18009 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18010 /* And the `namespace' keyword. */
18011 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18012 /* Look for the optional `::' operator. */
18013 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18014 /* And the optional nested-name-specifier. */
18015 cp_parser_nested_name_specifier_opt (parser,
18016 /*typename_keyword_p=*/false,
18017 /*check_dependency_p=*/true,
18018 /*type_p=*/false,
18019 /*is_declaration=*/true);
18020 /* Get the namespace being used. */
18021 namespace_decl = cp_parser_namespace_name (parser);
18022 /* And any specified attributes. */
18023 attribs = cp_parser_attributes_opt (parser);
18024 /* Update the symbol table. */
18025 parse_using_directive (namespace_decl, attribs);
18026 /* Look for the final `;'. */
18027 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18028 }
18029
18030 /* Parse an asm-definition.
18031
18032 asm-definition:
18033 asm ( string-literal ) ;
18034
18035 GNU Extension:
18036
18037 asm-definition:
18038 asm volatile [opt] ( string-literal ) ;
18039 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18040 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18041 : asm-operand-list [opt] ) ;
18042 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18043 : asm-operand-list [opt]
18044 : asm-clobber-list [opt] ) ;
18045 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18046 : asm-clobber-list [opt]
18047 : asm-goto-list ) ; */
18048
18049 static void
18050 cp_parser_asm_definition (cp_parser* parser)
18051 {
18052 tree string;
18053 tree outputs = NULL_TREE;
18054 tree inputs = NULL_TREE;
18055 tree clobbers = NULL_TREE;
18056 tree labels = NULL_TREE;
18057 tree asm_stmt;
18058 bool volatile_p = false;
18059 bool extended_p = false;
18060 bool invalid_inputs_p = false;
18061 bool invalid_outputs_p = false;
18062 bool goto_p = false;
18063 required_token missing = RT_NONE;
18064
18065 /* Look for the `asm' keyword. */
18066 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18067
18068 if (parser->in_function_body
18069 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18070 {
18071 error ("%<asm%> in %<constexpr%> function");
18072 cp_function_chain->invalid_constexpr = true;
18073 }
18074
18075 /* See if the next token is `volatile'. */
18076 if (cp_parser_allow_gnu_extensions_p (parser)
18077 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18078 {
18079 /* Remember that we saw the `volatile' keyword. */
18080 volatile_p = true;
18081 /* Consume the token. */
18082 cp_lexer_consume_token (parser->lexer);
18083 }
18084 if (cp_parser_allow_gnu_extensions_p (parser)
18085 && parser->in_function_body
18086 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18087 {
18088 /* Remember that we saw the `goto' keyword. */
18089 goto_p = true;
18090 /* Consume the token. */
18091 cp_lexer_consume_token (parser->lexer);
18092 }
18093 /* Look for the opening `('. */
18094 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18095 return;
18096 /* Look for the string. */
18097 string = cp_parser_string_literal (parser, false, false);
18098 if (string == error_mark_node)
18099 {
18100 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18101 /*consume_paren=*/true);
18102 return;
18103 }
18104
18105 /* If we're allowing GNU extensions, check for the extended assembly
18106 syntax. Unfortunately, the `:' tokens need not be separated by
18107 a space in C, and so, for compatibility, we tolerate that here
18108 too. Doing that means that we have to treat the `::' operator as
18109 two `:' tokens. */
18110 if (cp_parser_allow_gnu_extensions_p (parser)
18111 && parser->in_function_body
18112 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18113 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18114 {
18115 bool inputs_p = false;
18116 bool clobbers_p = false;
18117 bool labels_p = false;
18118
18119 /* The extended syntax was used. */
18120 extended_p = true;
18121
18122 /* Look for outputs. */
18123 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18124 {
18125 /* Consume the `:'. */
18126 cp_lexer_consume_token (parser->lexer);
18127 /* Parse the output-operands. */
18128 if (cp_lexer_next_token_is_not (parser->lexer,
18129 CPP_COLON)
18130 && cp_lexer_next_token_is_not (parser->lexer,
18131 CPP_SCOPE)
18132 && cp_lexer_next_token_is_not (parser->lexer,
18133 CPP_CLOSE_PAREN)
18134 && !goto_p)
18135 {
18136 outputs = cp_parser_asm_operand_list (parser);
18137 if (outputs == error_mark_node)
18138 invalid_outputs_p = true;
18139 }
18140 }
18141 /* If the next token is `::', there are no outputs, and the
18142 next token is the beginning of the inputs. */
18143 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18144 /* The inputs are coming next. */
18145 inputs_p = true;
18146
18147 /* Look for inputs. */
18148 if (inputs_p
18149 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18150 {
18151 /* Consume the `:' or `::'. */
18152 cp_lexer_consume_token (parser->lexer);
18153 /* Parse the output-operands. */
18154 if (cp_lexer_next_token_is_not (parser->lexer,
18155 CPP_COLON)
18156 && cp_lexer_next_token_is_not (parser->lexer,
18157 CPP_SCOPE)
18158 && cp_lexer_next_token_is_not (parser->lexer,
18159 CPP_CLOSE_PAREN))
18160 {
18161 inputs = cp_parser_asm_operand_list (parser);
18162 if (inputs == error_mark_node)
18163 invalid_inputs_p = true;
18164 }
18165 }
18166 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18167 /* The clobbers are coming next. */
18168 clobbers_p = true;
18169
18170 /* Look for clobbers. */
18171 if (clobbers_p
18172 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18173 {
18174 clobbers_p = true;
18175 /* Consume the `:' or `::'. */
18176 cp_lexer_consume_token (parser->lexer);
18177 /* Parse the clobbers. */
18178 if (cp_lexer_next_token_is_not (parser->lexer,
18179 CPP_COLON)
18180 && cp_lexer_next_token_is_not (parser->lexer,
18181 CPP_CLOSE_PAREN))
18182 clobbers = cp_parser_asm_clobber_list (parser);
18183 }
18184 else if (goto_p
18185 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18186 /* The labels are coming next. */
18187 labels_p = true;
18188
18189 /* Look for labels. */
18190 if (labels_p
18191 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18192 {
18193 labels_p = true;
18194 /* Consume the `:' or `::'. */
18195 cp_lexer_consume_token (parser->lexer);
18196 /* Parse the labels. */
18197 labels = cp_parser_asm_label_list (parser);
18198 }
18199
18200 if (goto_p && !labels_p)
18201 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18202 }
18203 else if (goto_p)
18204 missing = RT_COLON_SCOPE;
18205
18206 /* Look for the closing `)'. */
18207 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18208 missing ? missing : RT_CLOSE_PAREN))
18209 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18210 /*consume_paren=*/true);
18211 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18212
18213 if (!invalid_inputs_p && !invalid_outputs_p)
18214 {
18215 /* Create the ASM_EXPR. */
18216 if (parser->in_function_body)
18217 {
18218 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18219 inputs, clobbers, labels);
18220 /* If the extended syntax was not used, mark the ASM_EXPR. */
18221 if (!extended_p)
18222 {
18223 tree temp = asm_stmt;
18224 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18225 temp = TREE_OPERAND (temp, 0);
18226
18227 ASM_INPUT_P (temp) = 1;
18228 }
18229 }
18230 else
18231 symtab->finalize_toplevel_asm (string);
18232 }
18233 }
18234
18235 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18236 type that comes from the decl-specifier-seq. */
18237
18238 static tree
18239 strip_declarator_types (tree type, cp_declarator *declarator)
18240 {
18241 for (cp_declarator *d = declarator; d;)
18242 switch (d->kind)
18243 {
18244 case cdk_id:
18245 case cdk_error:
18246 d = NULL;
18247 break;
18248
18249 default:
18250 if (TYPE_PTRMEMFUNC_P (type))
18251 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18252 type = TREE_TYPE (type);
18253 d = d->declarator;
18254 break;
18255 }
18256
18257 return type;
18258 }
18259
18260 /* Declarators [gram.dcl.decl] */
18261
18262 /* Parse an init-declarator.
18263
18264 init-declarator:
18265 declarator initializer [opt]
18266
18267 GNU Extension:
18268
18269 init-declarator:
18270 declarator asm-specification [opt] attributes [opt] initializer [opt]
18271
18272 function-definition:
18273 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18274 function-body
18275 decl-specifier-seq [opt] declarator function-try-block
18276
18277 GNU Extension:
18278
18279 function-definition:
18280 __extension__ function-definition
18281
18282 TM Extension:
18283
18284 function-definition:
18285 decl-specifier-seq [opt] declarator function-transaction-block
18286
18287 The DECL_SPECIFIERS apply to this declarator. Returns a
18288 representation of the entity declared. If MEMBER_P is TRUE, then
18289 this declarator appears in a class scope. The new DECL created by
18290 this declarator is returned.
18291
18292 The CHECKS are access checks that should be performed once we know
18293 what entity is being declared (and, therefore, what classes have
18294 befriended it).
18295
18296 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18297 for a function-definition here as well. If the declarator is a
18298 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18299 be TRUE upon return. By that point, the function-definition will
18300 have been completely parsed.
18301
18302 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18303 is FALSE.
18304
18305 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18306 parsed declaration if it is an uninitialized single declarator not followed
18307 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18308 if present, will not be consumed. If returned, this declarator will be
18309 created with SD_INITIALIZED but will not call cp_finish_decl.
18310
18311 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18312 and there is an initializer, the pointed location_t is set to the
18313 location of the '=' or `(', or '{' in C++11 token introducing the
18314 initializer. */
18315
18316 static tree
18317 cp_parser_init_declarator (cp_parser* parser,
18318 cp_decl_specifier_seq *decl_specifiers,
18319 vec<deferred_access_check, va_gc> *checks,
18320 bool function_definition_allowed_p,
18321 bool member_p,
18322 int declares_class_or_enum,
18323 bool* function_definition_p,
18324 tree* maybe_range_for_decl,
18325 location_t* init_loc,
18326 tree* auto_result)
18327 {
18328 cp_token *token = NULL, *asm_spec_start_token = NULL,
18329 *attributes_start_token = NULL;
18330 cp_declarator *declarator;
18331 tree prefix_attributes;
18332 tree attributes = NULL;
18333 tree asm_specification;
18334 tree initializer;
18335 tree decl = NULL_TREE;
18336 tree scope;
18337 int is_initialized;
18338 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18339 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18340 "(...)". */
18341 enum cpp_ttype initialization_kind;
18342 bool is_direct_init = false;
18343 bool is_non_constant_init;
18344 int ctor_dtor_or_conv_p;
18345 bool friend_p = cp_parser_friend_p (decl_specifiers);
18346 tree pushed_scope = NULL_TREE;
18347 bool range_for_decl_p = false;
18348 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18349 location_t tmp_init_loc = UNKNOWN_LOCATION;
18350
18351 /* Gather the attributes that were provided with the
18352 decl-specifiers. */
18353 prefix_attributes = decl_specifiers->attributes;
18354
18355 /* Assume that this is not the declarator for a function
18356 definition. */
18357 if (function_definition_p)
18358 *function_definition_p = false;
18359
18360 /* Default arguments are only permitted for function parameters. */
18361 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18362 parser->default_arg_ok_p = false;
18363
18364 /* Defer access checks while parsing the declarator; we cannot know
18365 what names are accessible until we know what is being
18366 declared. */
18367 resume_deferring_access_checks ();
18368
18369 /* Parse the declarator. */
18370 token = cp_lexer_peek_token (parser->lexer);
18371 declarator
18372 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18373 &ctor_dtor_or_conv_p,
18374 /*parenthesized_p=*/NULL,
18375 member_p, friend_p);
18376 /* Gather up the deferred checks. */
18377 stop_deferring_access_checks ();
18378
18379 parser->default_arg_ok_p = saved_default_arg_ok_p;
18380
18381 /* If the DECLARATOR was erroneous, there's no need to go
18382 further. */
18383 if (declarator == cp_error_declarator)
18384 return error_mark_node;
18385
18386 /* Check that the number of template-parameter-lists is OK. */
18387 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18388 token->location))
18389 return error_mark_node;
18390
18391 if (declares_class_or_enum & 2)
18392 cp_parser_check_for_definition_in_return_type (declarator,
18393 decl_specifiers->type,
18394 decl_specifiers->locations[ds_type_spec]);
18395
18396 /* Figure out what scope the entity declared by the DECLARATOR is
18397 located in. `grokdeclarator' sometimes changes the scope, so
18398 we compute it now. */
18399 scope = get_scope_of_declarator (declarator);
18400
18401 /* Perform any lookups in the declared type which were thought to be
18402 dependent, but are not in the scope of the declarator. */
18403 decl_specifiers->type
18404 = maybe_update_decl_type (decl_specifiers->type, scope);
18405
18406 /* If we're allowing GNU extensions, look for an
18407 asm-specification. */
18408 if (cp_parser_allow_gnu_extensions_p (parser))
18409 {
18410 /* Look for an asm-specification. */
18411 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18412 asm_specification = cp_parser_asm_specification_opt (parser);
18413 }
18414 else
18415 asm_specification = NULL_TREE;
18416
18417 /* Look for attributes. */
18418 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18419 attributes = cp_parser_attributes_opt (parser);
18420
18421 /* Peek at the next token. */
18422 token = cp_lexer_peek_token (parser->lexer);
18423
18424 bool bogus_implicit_tmpl = false;
18425
18426 if (function_declarator_p (declarator))
18427 {
18428 /* Check to see if the token indicates the start of a
18429 function-definition. */
18430 if (cp_parser_token_starts_function_definition_p (token))
18431 {
18432 if (!function_definition_allowed_p)
18433 {
18434 /* If a function-definition should not appear here, issue an
18435 error message. */
18436 cp_parser_error (parser,
18437 "a function-definition is not allowed here");
18438 return error_mark_node;
18439 }
18440
18441 location_t func_brace_location
18442 = cp_lexer_peek_token (parser->lexer)->location;
18443
18444 /* Neither attributes nor an asm-specification are allowed
18445 on a function-definition. */
18446 if (asm_specification)
18447 error_at (asm_spec_start_token->location,
18448 "an asm-specification is not allowed "
18449 "on a function-definition");
18450 if (attributes)
18451 error_at (attributes_start_token->location,
18452 "attributes are not allowed "
18453 "on a function-definition");
18454 /* This is a function-definition. */
18455 *function_definition_p = true;
18456
18457 /* Parse the function definition. */
18458 if (member_p)
18459 decl = cp_parser_save_member_function_body (parser,
18460 decl_specifiers,
18461 declarator,
18462 prefix_attributes);
18463 else
18464 decl =
18465 (cp_parser_function_definition_from_specifiers_and_declarator
18466 (parser, decl_specifiers, prefix_attributes, declarator));
18467
18468 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18469 {
18470 /* This is where the prologue starts... */
18471 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18472 = func_brace_location;
18473 }
18474
18475 return decl;
18476 }
18477 }
18478 else if (parser->fully_implicit_function_template_p)
18479 {
18480 /* A non-template declaration involving a function parameter list
18481 containing an implicit template parameter will be made into a
18482 template. If the resulting declaration is not going to be an
18483 actual function then finish the template scope here to prevent it.
18484 An error message will be issued once we have a decl to talk about.
18485
18486 FIXME probably we should do type deduction rather than create an
18487 implicit template, but the standard currently doesn't allow it. */
18488 bogus_implicit_tmpl = true;
18489 finish_fully_implicit_template (parser, NULL_TREE);
18490 }
18491
18492 /* [dcl.dcl]
18493
18494 Only in function declarations for constructors, destructors, and
18495 type conversions can the decl-specifier-seq be omitted.
18496
18497 We explicitly postpone this check past the point where we handle
18498 function-definitions because we tolerate function-definitions
18499 that are missing their return types in some modes. */
18500 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18501 {
18502 cp_parser_error (parser,
18503 "expected constructor, destructor, or type conversion");
18504 return error_mark_node;
18505 }
18506
18507 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18508 if (token->type == CPP_EQ
18509 || token->type == CPP_OPEN_PAREN
18510 || token->type == CPP_OPEN_BRACE)
18511 {
18512 is_initialized = SD_INITIALIZED;
18513 initialization_kind = token->type;
18514 if (maybe_range_for_decl)
18515 *maybe_range_for_decl = error_mark_node;
18516 tmp_init_loc = token->location;
18517 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18518 *init_loc = tmp_init_loc;
18519
18520 if (token->type == CPP_EQ
18521 && function_declarator_p (declarator))
18522 {
18523 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18524 if (t2->keyword == RID_DEFAULT)
18525 is_initialized = SD_DEFAULTED;
18526 else if (t2->keyword == RID_DELETE)
18527 is_initialized = SD_DELETED;
18528 }
18529 }
18530 else
18531 {
18532 /* If the init-declarator isn't initialized and isn't followed by a
18533 `,' or `;', it's not a valid init-declarator. */
18534 if (token->type != CPP_COMMA
18535 && token->type != CPP_SEMICOLON)
18536 {
18537 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18538 range_for_decl_p = true;
18539 else
18540 {
18541 if (!maybe_range_for_decl)
18542 cp_parser_error (parser, "expected initializer");
18543 return error_mark_node;
18544 }
18545 }
18546 is_initialized = SD_UNINITIALIZED;
18547 initialization_kind = CPP_EOF;
18548 }
18549
18550 /* Because start_decl has side-effects, we should only call it if we
18551 know we're going ahead. By this point, we know that we cannot
18552 possibly be looking at any other construct. */
18553 cp_parser_commit_to_tentative_parse (parser);
18554
18555 /* Enter the newly declared entry in the symbol table. If we're
18556 processing a declaration in a class-specifier, we wait until
18557 after processing the initializer. */
18558 if (!member_p)
18559 {
18560 if (parser->in_unbraced_linkage_specification_p)
18561 decl_specifiers->storage_class = sc_extern;
18562 decl = start_decl (declarator, decl_specifiers,
18563 range_for_decl_p? SD_INITIALIZED : is_initialized,
18564 attributes, prefix_attributes, &pushed_scope);
18565 cp_finalize_omp_declare_simd (parser, decl);
18566 cp_finalize_oacc_routine (parser, decl, false);
18567 /* Adjust location of decl if declarator->id_loc is more appropriate:
18568 set, and decl wasn't merged with another decl, in which case its
18569 location would be different from input_location, and more accurate. */
18570 if (DECL_P (decl)
18571 && declarator->id_loc != UNKNOWN_LOCATION
18572 && DECL_SOURCE_LOCATION (decl) == input_location)
18573 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18574 }
18575 else if (scope)
18576 /* Enter the SCOPE. That way unqualified names appearing in the
18577 initializer will be looked up in SCOPE. */
18578 pushed_scope = push_scope (scope);
18579
18580 /* Perform deferred access control checks, now that we know in which
18581 SCOPE the declared entity resides. */
18582 if (!member_p && decl)
18583 {
18584 tree saved_current_function_decl = NULL_TREE;
18585
18586 /* If the entity being declared is a function, pretend that we
18587 are in its scope. If it is a `friend', it may have access to
18588 things that would not otherwise be accessible. */
18589 if (TREE_CODE (decl) == FUNCTION_DECL)
18590 {
18591 saved_current_function_decl = current_function_decl;
18592 current_function_decl = decl;
18593 }
18594
18595 /* Perform access checks for template parameters. */
18596 cp_parser_perform_template_parameter_access_checks (checks);
18597
18598 /* Perform the access control checks for the declarator and the
18599 decl-specifiers. */
18600 perform_deferred_access_checks (tf_warning_or_error);
18601
18602 /* Restore the saved value. */
18603 if (TREE_CODE (decl) == FUNCTION_DECL)
18604 current_function_decl = saved_current_function_decl;
18605 }
18606
18607 /* Parse the initializer. */
18608 initializer = NULL_TREE;
18609 is_direct_init = false;
18610 is_non_constant_init = true;
18611 if (is_initialized)
18612 {
18613 if (function_declarator_p (declarator))
18614 {
18615 if (initialization_kind == CPP_EQ)
18616 initializer = cp_parser_pure_specifier (parser);
18617 else
18618 {
18619 /* If the declaration was erroneous, we don't really
18620 know what the user intended, so just silently
18621 consume the initializer. */
18622 if (decl != error_mark_node)
18623 error_at (tmp_init_loc, "initializer provided for function");
18624 cp_parser_skip_to_closing_parenthesis (parser,
18625 /*recovering=*/true,
18626 /*or_comma=*/false,
18627 /*consume_paren=*/true);
18628 }
18629 }
18630 else
18631 {
18632 /* We want to record the extra mangling scope for in-class
18633 initializers of class members and initializers of static data
18634 member templates. The former involves deferring
18635 parsing of the initializer until end of class as with default
18636 arguments. So right here we only handle the latter. */
18637 if (!member_p && processing_template_decl)
18638 start_lambda_scope (decl);
18639 initializer = cp_parser_initializer (parser,
18640 &is_direct_init,
18641 &is_non_constant_init);
18642 if (!member_p && processing_template_decl)
18643 finish_lambda_scope ();
18644 if (initializer == error_mark_node)
18645 cp_parser_skip_to_end_of_statement (parser);
18646 }
18647 }
18648
18649 /* The old parser allows attributes to appear after a parenthesized
18650 initializer. Mark Mitchell proposed removing this functionality
18651 on the GCC mailing lists on 2002-08-13. This parser accepts the
18652 attributes -- but ignores them. */
18653 if (cp_parser_allow_gnu_extensions_p (parser)
18654 && initialization_kind == CPP_OPEN_PAREN)
18655 if (cp_parser_attributes_opt (parser))
18656 warning (OPT_Wattributes,
18657 "attributes after parenthesized initializer ignored");
18658
18659 /* And now complain about a non-function implicit template. */
18660 if (bogus_implicit_tmpl && decl != error_mark_node)
18661 error_at (DECL_SOURCE_LOCATION (decl),
18662 "non-function %qD declared as implicit template", decl);
18663
18664 /* For an in-class declaration, use `grokfield' to create the
18665 declaration. */
18666 if (member_p)
18667 {
18668 if (pushed_scope)
18669 {
18670 pop_scope (pushed_scope);
18671 pushed_scope = NULL_TREE;
18672 }
18673 decl = grokfield (declarator, decl_specifiers,
18674 initializer, !is_non_constant_init,
18675 /*asmspec=*/NULL_TREE,
18676 chainon (attributes, prefix_attributes));
18677 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18678 cp_parser_save_default_args (parser, decl);
18679 cp_finalize_omp_declare_simd (parser, decl);
18680 cp_finalize_oacc_routine (parser, decl, false);
18681 }
18682
18683 /* Finish processing the declaration. But, skip member
18684 declarations. */
18685 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18686 {
18687 cp_finish_decl (decl,
18688 initializer, !is_non_constant_init,
18689 asm_specification,
18690 /* If the initializer is in parentheses, then this is
18691 a direct-initialization, which means that an
18692 `explicit' constructor is OK. Otherwise, an
18693 `explicit' constructor cannot be used. */
18694 ((is_direct_init || !is_initialized)
18695 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18696 }
18697 else if ((cxx_dialect != cxx98) && friend_p
18698 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18699 /* Core issue #226 (C++0x only): A default template-argument
18700 shall not be specified in a friend class template
18701 declaration. */
18702 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18703 /*is_partial=*/false, /*is_friend_decl=*/1);
18704
18705 if (!friend_p && pushed_scope)
18706 pop_scope (pushed_scope);
18707
18708 if (function_declarator_p (declarator)
18709 && parser->fully_implicit_function_template_p)
18710 {
18711 if (member_p)
18712 decl = finish_fully_implicit_template (parser, decl);
18713 else
18714 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18715 }
18716
18717 if (auto_result && is_initialized && decl_specifiers->type
18718 && type_uses_auto (decl_specifiers->type))
18719 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18720
18721 return decl;
18722 }
18723
18724 /* Parse a declarator.
18725
18726 declarator:
18727 direct-declarator
18728 ptr-operator declarator
18729
18730 abstract-declarator:
18731 ptr-operator abstract-declarator [opt]
18732 direct-abstract-declarator
18733
18734 GNU Extensions:
18735
18736 declarator:
18737 attributes [opt] direct-declarator
18738 attributes [opt] ptr-operator declarator
18739
18740 abstract-declarator:
18741 attributes [opt] ptr-operator abstract-declarator [opt]
18742 attributes [opt] direct-abstract-declarator
18743
18744 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18745 detect constructor, destructor or conversion operators. It is set
18746 to -1 if the declarator is a name, and +1 if it is a
18747 function. Otherwise it is set to zero. Usually you just want to
18748 test for >0, but internally the negative value is used.
18749
18750 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18751 a decl-specifier-seq unless it declares a constructor, destructor,
18752 or conversion. It might seem that we could check this condition in
18753 semantic analysis, rather than parsing, but that makes it difficult
18754 to handle something like `f()'. We want to notice that there are
18755 no decl-specifiers, and therefore realize that this is an
18756 expression, not a declaration.)
18757
18758 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18759 the declarator is a direct-declarator of the form "(...)".
18760
18761 MEMBER_P is true iff this declarator is a member-declarator.
18762
18763 FRIEND_P is true iff this declarator is a friend. */
18764
18765 static cp_declarator *
18766 cp_parser_declarator (cp_parser* parser,
18767 cp_parser_declarator_kind dcl_kind,
18768 int* ctor_dtor_or_conv_p,
18769 bool* parenthesized_p,
18770 bool member_p, bool friend_p)
18771 {
18772 cp_declarator *declarator;
18773 enum tree_code code;
18774 cp_cv_quals cv_quals;
18775 tree class_type;
18776 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18777
18778 /* Assume this is not a constructor, destructor, or type-conversion
18779 operator. */
18780 if (ctor_dtor_or_conv_p)
18781 *ctor_dtor_or_conv_p = 0;
18782
18783 if (cp_parser_allow_gnu_extensions_p (parser))
18784 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18785
18786 /* Check for the ptr-operator production. */
18787 cp_parser_parse_tentatively (parser);
18788 /* Parse the ptr-operator. */
18789 code = cp_parser_ptr_operator (parser,
18790 &class_type,
18791 &cv_quals,
18792 &std_attributes);
18793
18794 /* If that worked, then we have a ptr-operator. */
18795 if (cp_parser_parse_definitely (parser))
18796 {
18797 /* If a ptr-operator was found, then this declarator was not
18798 parenthesized. */
18799 if (parenthesized_p)
18800 *parenthesized_p = true;
18801 /* The dependent declarator is optional if we are parsing an
18802 abstract-declarator. */
18803 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18804 cp_parser_parse_tentatively (parser);
18805
18806 /* Parse the dependent declarator. */
18807 declarator = cp_parser_declarator (parser, dcl_kind,
18808 /*ctor_dtor_or_conv_p=*/NULL,
18809 /*parenthesized_p=*/NULL,
18810 /*member_p=*/false,
18811 friend_p);
18812
18813 /* If we are parsing an abstract-declarator, we must handle the
18814 case where the dependent declarator is absent. */
18815 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18816 && !cp_parser_parse_definitely (parser))
18817 declarator = NULL;
18818
18819 declarator = cp_parser_make_indirect_declarator
18820 (code, class_type, cv_quals, declarator, std_attributes);
18821 }
18822 /* Everything else is a direct-declarator. */
18823 else
18824 {
18825 if (parenthesized_p)
18826 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18827 CPP_OPEN_PAREN);
18828 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18829 ctor_dtor_or_conv_p,
18830 member_p, friend_p);
18831 }
18832
18833 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18834 declarator->attributes = gnu_attributes;
18835 return declarator;
18836 }
18837
18838 /* Parse a direct-declarator or direct-abstract-declarator.
18839
18840 direct-declarator:
18841 declarator-id
18842 direct-declarator ( parameter-declaration-clause )
18843 cv-qualifier-seq [opt]
18844 ref-qualifier [opt]
18845 exception-specification [opt]
18846 direct-declarator [ constant-expression [opt] ]
18847 ( declarator )
18848
18849 direct-abstract-declarator:
18850 direct-abstract-declarator [opt]
18851 ( parameter-declaration-clause )
18852 cv-qualifier-seq [opt]
18853 ref-qualifier [opt]
18854 exception-specification [opt]
18855 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18856 ( abstract-declarator )
18857
18858 Returns a representation of the declarator. DCL_KIND is
18859 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18860 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18861 we are parsing a direct-declarator. It is
18862 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18863 of ambiguity we prefer an abstract declarator, as per
18864 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18865 as for cp_parser_declarator. */
18866
18867 static cp_declarator *
18868 cp_parser_direct_declarator (cp_parser* parser,
18869 cp_parser_declarator_kind dcl_kind,
18870 int* ctor_dtor_or_conv_p,
18871 bool member_p, bool friend_p)
18872 {
18873 cp_token *token;
18874 cp_declarator *declarator = NULL;
18875 tree scope = NULL_TREE;
18876 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18877 bool saved_in_declarator_p = parser->in_declarator_p;
18878 bool first = true;
18879 tree pushed_scope = NULL_TREE;
18880
18881 while (true)
18882 {
18883 /* Peek at the next token. */
18884 token = cp_lexer_peek_token (parser->lexer);
18885 if (token->type == CPP_OPEN_PAREN)
18886 {
18887 /* This is either a parameter-declaration-clause, or a
18888 parenthesized declarator. When we know we are parsing a
18889 named declarator, it must be a parenthesized declarator
18890 if FIRST is true. For instance, `(int)' is a
18891 parameter-declaration-clause, with an omitted
18892 direct-abstract-declarator. But `((*))', is a
18893 parenthesized abstract declarator. Finally, when T is a
18894 template parameter `(T)' is a
18895 parameter-declaration-clause, and not a parenthesized
18896 named declarator.
18897
18898 We first try and parse a parameter-declaration-clause,
18899 and then try a nested declarator (if FIRST is true).
18900
18901 It is not an error for it not to be a
18902 parameter-declaration-clause, even when FIRST is
18903 false. Consider,
18904
18905 int i (int);
18906 int i (3);
18907
18908 The first is the declaration of a function while the
18909 second is the definition of a variable, including its
18910 initializer.
18911
18912 Having seen only the parenthesis, we cannot know which of
18913 these two alternatives should be selected. Even more
18914 complex are examples like:
18915
18916 int i (int (a));
18917 int i (int (3));
18918
18919 The former is a function-declaration; the latter is a
18920 variable initialization.
18921
18922 Thus again, we try a parameter-declaration-clause, and if
18923 that fails, we back out and return. */
18924
18925 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18926 {
18927 tree params;
18928 bool is_declarator = false;
18929
18930 /* In a member-declarator, the only valid interpretation
18931 of a parenthesis is the start of a
18932 parameter-declaration-clause. (It is invalid to
18933 initialize a static data member with a parenthesized
18934 initializer; only the "=" form of initialization is
18935 permitted.) */
18936 if (!member_p)
18937 cp_parser_parse_tentatively (parser);
18938
18939 /* Consume the `('. */
18940 cp_lexer_consume_token (parser->lexer);
18941 if (first)
18942 {
18943 /* If this is going to be an abstract declarator, we're
18944 in a declarator and we can't have default args. */
18945 parser->default_arg_ok_p = false;
18946 parser->in_declarator_p = true;
18947 }
18948
18949 begin_scope (sk_function_parms, NULL_TREE);
18950
18951 /* Parse the parameter-declaration-clause. */
18952 params = cp_parser_parameter_declaration_clause (parser);
18953
18954 /* Consume the `)'. */
18955 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18956
18957 /* If all went well, parse the cv-qualifier-seq,
18958 ref-qualifier and the exception-specification. */
18959 if (member_p || cp_parser_parse_definitely (parser))
18960 {
18961 cp_cv_quals cv_quals;
18962 cp_virt_specifiers virt_specifiers;
18963 cp_ref_qualifier ref_qual;
18964 tree exception_specification;
18965 tree late_return;
18966 tree attrs;
18967 bool memfn = (member_p || (pushed_scope
18968 && CLASS_TYPE_P (pushed_scope)));
18969
18970 is_declarator = true;
18971
18972 if (ctor_dtor_or_conv_p)
18973 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18974 first = false;
18975
18976 /* Parse the cv-qualifier-seq. */
18977 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18978 /* Parse the ref-qualifier. */
18979 ref_qual = cp_parser_ref_qualifier_opt (parser);
18980 /* Parse the tx-qualifier. */
18981 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18982 /* And the exception-specification. */
18983 exception_specification
18984 = cp_parser_exception_specification_opt (parser);
18985
18986 attrs = cp_parser_std_attribute_spec_seq (parser);
18987
18988 /* In here, we handle cases where attribute is used after
18989 the function declaration. For example:
18990 void func (int x) __attribute__((vector(..))); */
18991 tree gnu_attrs = NULL_TREE;
18992 if (flag_cilkplus
18993 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18994 {
18995 cp_parser_parse_tentatively (parser);
18996 tree attr = cp_parser_gnu_attributes_opt (parser);
18997 if (cp_lexer_next_token_is_not (parser->lexer,
18998 CPP_SEMICOLON)
18999 && cp_lexer_next_token_is_not (parser->lexer,
19000 CPP_OPEN_BRACE))
19001 cp_parser_abort_tentative_parse (parser);
19002 else if (!cp_parser_parse_definitely (parser))
19003 ;
19004 else
19005 gnu_attrs = attr;
19006 }
19007 tree requires_clause = NULL_TREE;
19008 late_return = (cp_parser_late_return_type_opt
19009 (parser, declarator, requires_clause,
19010 memfn ? cv_quals : -1));
19011
19012 /* Parse the virt-specifier-seq. */
19013 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19014
19015 /* Create the function-declarator. */
19016 declarator = make_call_declarator (declarator,
19017 params,
19018 cv_quals,
19019 virt_specifiers,
19020 ref_qual,
19021 tx_qual,
19022 exception_specification,
19023 late_return,
19024 requires_clause);
19025 declarator->std_attributes = attrs;
19026 declarator->attributes = gnu_attrs;
19027 /* Any subsequent parameter lists are to do with
19028 return type, so are not those of the declared
19029 function. */
19030 parser->default_arg_ok_p = false;
19031 }
19032
19033 /* Remove the function parms from scope. */
19034 pop_bindings_and_leave_scope ();
19035
19036 if (is_declarator)
19037 /* Repeat the main loop. */
19038 continue;
19039 }
19040
19041 /* If this is the first, we can try a parenthesized
19042 declarator. */
19043 if (first)
19044 {
19045 bool saved_in_type_id_in_expr_p;
19046
19047 parser->default_arg_ok_p = saved_default_arg_ok_p;
19048 parser->in_declarator_p = saved_in_declarator_p;
19049
19050 /* Consume the `('. */
19051 cp_lexer_consume_token (parser->lexer);
19052 /* Parse the nested declarator. */
19053 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19054 parser->in_type_id_in_expr_p = true;
19055 declarator
19056 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19057 /*parenthesized_p=*/NULL,
19058 member_p, friend_p);
19059 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19060 first = false;
19061 /* Expect a `)'. */
19062 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19063 declarator = cp_error_declarator;
19064 if (declarator == cp_error_declarator)
19065 break;
19066
19067 goto handle_declarator;
19068 }
19069 /* Otherwise, we must be done. */
19070 else
19071 break;
19072 }
19073 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19074 && token->type == CPP_OPEN_SQUARE
19075 && !cp_next_tokens_can_be_attribute_p (parser))
19076 {
19077 /* Parse an array-declarator. */
19078 tree bounds, attrs;
19079
19080 if (ctor_dtor_or_conv_p)
19081 *ctor_dtor_or_conv_p = 0;
19082
19083 first = false;
19084 parser->default_arg_ok_p = false;
19085 parser->in_declarator_p = true;
19086 /* Consume the `['. */
19087 cp_lexer_consume_token (parser->lexer);
19088 /* Peek at the next token. */
19089 token = cp_lexer_peek_token (parser->lexer);
19090 /* If the next token is `]', then there is no
19091 constant-expression. */
19092 if (token->type != CPP_CLOSE_SQUARE)
19093 {
19094 bool non_constant_p;
19095 bounds
19096 = cp_parser_constant_expression (parser,
19097 /*allow_non_constant=*/true,
19098 &non_constant_p);
19099 if (!non_constant_p)
19100 /* OK */;
19101 else if (error_operand_p (bounds))
19102 /* Already gave an error. */;
19103 else if (!parser->in_function_body
19104 || current_binding_level->kind == sk_function_parms)
19105 {
19106 /* Normally, the array bound must be an integral constant
19107 expression. However, as an extension, we allow VLAs
19108 in function scopes as long as they aren't part of a
19109 parameter declaration. */
19110 cp_parser_error (parser,
19111 "array bound is not an integer constant");
19112 bounds = error_mark_node;
19113 }
19114 else if (processing_template_decl
19115 && !type_dependent_expression_p (bounds))
19116 {
19117 /* Remember this wasn't a constant-expression. */
19118 bounds = build_nop (TREE_TYPE (bounds), bounds);
19119 TREE_SIDE_EFFECTS (bounds) = 1;
19120 }
19121 }
19122 else
19123 bounds = NULL_TREE;
19124 /* Look for the closing `]'. */
19125 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19126 {
19127 declarator = cp_error_declarator;
19128 break;
19129 }
19130
19131 attrs = cp_parser_std_attribute_spec_seq (parser);
19132 declarator = make_array_declarator (declarator, bounds);
19133 declarator->std_attributes = attrs;
19134 }
19135 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19136 {
19137 {
19138 tree qualifying_scope;
19139 tree unqualified_name;
19140 tree attrs;
19141 special_function_kind sfk;
19142 bool abstract_ok;
19143 bool pack_expansion_p = false;
19144 cp_token *declarator_id_start_token;
19145
19146 /* Parse a declarator-id */
19147 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19148 if (abstract_ok)
19149 {
19150 cp_parser_parse_tentatively (parser);
19151
19152 /* If we see an ellipsis, we should be looking at a
19153 parameter pack. */
19154 if (token->type == CPP_ELLIPSIS)
19155 {
19156 /* Consume the `...' */
19157 cp_lexer_consume_token (parser->lexer);
19158
19159 pack_expansion_p = true;
19160 }
19161 }
19162
19163 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19164 unqualified_name
19165 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19166 qualifying_scope = parser->scope;
19167 if (abstract_ok)
19168 {
19169 bool okay = false;
19170
19171 if (!unqualified_name && pack_expansion_p)
19172 {
19173 /* Check whether an error occurred. */
19174 okay = !cp_parser_error_occurred (parser);
19175
19176 /* We already consumed the ellipsis to mark a
19177 parameter pack, but we have no way to report it,
19178 so abort the tentative parse. We will be exiting
19179 immediately anyway. */
19180 cp_parser_abort_tentative_parse (parser);
19181 }
19182 else
19183 okay = cp_parser_parse_definitely (parser);
19184
19185 if (!okay)
19186 unqualified_name = error_mark_node;
19187 else if (unqualified_name
19188 && (qualifying_scope
19189 || (!identifier_p (unqualified_name))))
19190 {
19191 cp_parser_error (parser, "expected unqualified-id");
19192 unqualified_name = error_mark_node;
19193 }
19194 }
19195
19196 if (!unqualified_name)
19197 return NULL;
19198 if (unqualified_name == error_mark_node)
19199 {
19200 declarator = cp_error_declarator;
19201 pack_expansion_p = false;
19202 declarator->parameter_pack_p = false;
19203 break;
19204 }
19205
19206 attrs = cp_parser_std_attribute_spec_seq (parser);
19207
19208 if (qualifying_scope && at_namespace_scope_p ()
19209 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19210 {
19211 /* In the declaration of a member of a template class
19212 outside of the class itself, the SCOPE will sometimes
19213 be a TYPENAME_TYPE. For example, given:
19214
19215 template <typename T>
19216 int S<T>::R::i = 3;
19217
19218 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19219 this context, we must resolve S<T>::R to an ordinary
19220 type, rather than a typename type.
19221
19222 The reason we normally avoid resolving TYPENAME_TYPEs
19223 is that a specialization of `S' might render
19224 `S<T>::R' not a type. However, if `S' is
19225 specialized, then this `i' will not be used, so there
19226 is no harm in resolving the types here. */
19227 tree type;
19228
19229 /* Resolve the TYPENAME_TYPE. */
19230 type = resolve_typename_type (qualifying_scope,
19231 /*only_current_p=*/false);
19232 /* If that failed, the declarator is invalid. */
19233 if (TREE_CODE (type) == TYPENAME_TYPE)
19234 {
19235 if (typedef_variant_p (type))
19236 error_at (declarator_id_start_token->location,
19237 "cannot define member of dependent typedef "
19238 "%qT", type);
19239 else
19240 error_at (declarator_id_start_token->location,
19241 "%<%T::%E%> is not a type",
19242 TYPE_CONTEXT (qualifying_scope),
19243 TYPE_IDENTIFIER (qualifying_scope));
19244 }
19245 qualifying_scope = type;
19246 }
19247
19248 sfk = sfk_none;
19249
19250 if (unqualified_name)
19251 {
19252 tree class_type;
19253
19254 if (qualifying_scope
19255 && CLASS_TYPE_P (qualifying_scope))
19256 class_type = qualifying_scope;
19257 else
19258 class_type = current_class_type;
19259
19260 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19261 {
19262 tree name_type = TREE_TYPE (unqualified_name);
19263 if (class_type && same_type_p (name_type, class_type))
19264 {
19265 if (qualifying_scope
19266 && CLASSTYPE_USE_TEMPLATE (name_type))
19267 {
19268 error_at (declarator_id_start_token->location,
19269 "invalid use of constructor as a template");
19270 inform (declarator_id_start_token->location,
19271 "use %<%T::%D%> instead of %<%T::%D%> to "
19272 "name the constructor in a qualified name",
19273 class_type,
19274 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19275 class_type, name_type);
19276 declarator = cp_error_declarator;
19277 break;
19278 }
19279 else
19280 unqualified_name = constructor_name (class_type);
19281 }
19282 else
19283 {
19284 /* We do not attempt to print the declarator
19285 here because we do not have enough
19286 information about its original syntactic
19287 form. */
19288 cp_parser_error (parser, "invalid declarator");
19289 declarator = cp_error_declarator;
19290 break;
19291 }
19292 }
19293
19294 if (class_type)
19295 {
19296 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19297 sfk = sfk_destructor;
19298 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19299 sfk = sfk_conversion;
19300 else if (/* There's no way to declare a constructor
19301 for an anonymous type, even if the type
19302 got a name for linkage purposes. */
19303 !TYPE_WAS_ANONYMOUS (class_type)
19304 /* Handle correctly (c++/19200):
19305
19306 struct S {
19307 struct T{};
19308 friend void S(T);
19309 };
19310
19311 and also:
19312
19313 namespace N {
19314 void S();
19315 }
19316
19317 struct S {
19318 friend void N::S();
19319 }; */
19320 && !(friend_p
19321 && class_type != qualifying_scope)
19322 && constructor_name_p (unqualified_name,
19323 class_type))
19324 {
19325 unqualified_name = constructor_name (class_type);
19326 sfk = sfk_constructor;
19327 }
19328 else if (is_overloaded_fn (unqualified_name)
19329 && DECL_CONSTRUCTOR_P (get_first_fn
19330 (unqualified_name)))
19331 sfk = sfk_constructor;
19332
19333 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19334 *ctor_dtor_or_conv_p = -1;
19335 }
19336 }
19337 declarator = make_id_declarator (qualifying_scope,
19338 unqualified_name,
19339 sfk);
19340 declarator->std_attributes = attrs;
19341 declarator->id_loc = token->location;
19342 declarator->parameter_pack_p = pack_expansion_p;
19343
19344 if (pack_expansion_p)
19345 maybe_warn_variadic_templates ();
19346 }
19347
19348 handle_declarator:;
19349 scope = get_scope_of_declarator (declarator);
19350 if (scope)
19351 {
19352 /* Any names that appear after the declarator-id for a
19353 member are looked up in the containing scope. */
19354 if (at_function_scope_p ())
19355 {
19356 /* But declarations with qualified-ids can't appear in a
19357 function. */
19358 cp_parser_error (parser, "qualified-id in declaration");
19359 declarator = cp_error_declarator;
19360 break;
19361 }
19362 pushed_scope = push_scope (scope);
19363 }
19364 parser->in_declarator_p = true;
19365 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19366 || (declarator && declarator->kind == cdk_id))
19367 /* Default args are only allowed on function
19368 declarations. */
19369 parser->default_arg_ok_p = saved_default_arg_ok_p;
19370 else
19371 parser->default_arg_ok_p = false;
19372
19373 first = false;
19374 }
19375 /* We're done. */
19376 else
19377 break;
19378 }
19379
19380 /* For an abstract declarator, we might wind up with nothing at this
19381 point. That's an error; the declarator is not optional. */
19382 if (!declarator)
19383 cp_parser_error (parser, "expected declarator");
19384
19385 /* If we entered a scope, we must exit it now. */
19386 if (pushed_scope)
19387 pop_scope (pushed_scope);
19388
19389 parser->default_arg_ok_p = saved_default_arg_ok_p;
19390 parser->in_declarator_p = saved_in_declarator_p;
19391
19392 return declarator;
19393 }
19394
19395 /* Parse a ptr-operator.
19396
19397 ptr-operator:
19398 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19399 * cv-qualifier-seq [opt]
19400 &
19401 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19402 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19403
19404 GNU Extension:
19405
19406 ptr-operator:
19407 & cv-qualifier-seq [opt]
19408
19409 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19410 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19411 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19412 filled in with the TYPE containing the member. *CV_QUALS is
19413 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19414 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19415 Note that the tree codes returned by this function have nothing
19416 to do with the types of trees that will be eventually be created
19417 to represent the pointer or reference type being parsed. They are
19418 just constants with suggestive names. */
19419 static enum tree_code
19420 cp_parser_ptr_operator (cp_parser* parser,
19421 tree* type,
19422 cp_cv_quals *cv_quals,
19423 tree *attributes)
19424 {
19425 enum tree_code code = ERROR_MARK;
19426 cp_token *token;
19427 tree attrs = NULL_TREE;
19428
19429 /* Assume that it's not a pointer-to-member. */
19430 *type = NULL_TREE;
19431 /* And that there are no cv-qualifiers. */
19432 *cv_quals = TYPE_UNQUALIFIED;
19433
19434 /* Peek at the next token. */
19435 token = cp_lexer_peek_token (parser->lexer);
19436
19437 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19438 if (token->type == CPP_MULT)
19439 code = INDIRECT_REF;
19440 else if (token->type == CPP_AND)
19441 code = ADDR_EXPR;
19442 else if ((cxx_dialect != cxx98) &&
19443 token->type == CPP_AND_AND) /* C++0x only */
19444 code = NON_LVALUE_EXPR;
19445
19446 if (code != ERROR_MARK)
19447 {
19448 /* Consume the `*', `&' or `&&'. */
19449 cp_lexer_consume_token (parser->lexer);
19450
19451 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19452 `&', if we are allowing GNU extensions. (The only qualifier
19453 that can legally appear after `&' is `restrict', but that is
19454 enforced during semantic analysis. */
19455 if (code == INDIRECT_REF
19456 || cp_parser_allow_gnu_extensions_p (parser))
19457 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19458
19459 attrs = cp_parser_std_attribute_spec_seq (parser);
19460 if (attributes != NULL)
19461 *attributes = attrs;
19462 }
19463 else
19464 {
19465 /* Try the pointer-to-member case. */
19466 cp_parser_parse_tentatively (parser);
19467 /* Look for the optional `::' operator. */
19468 cp_parser_global_scope_opt (parser,
19469 /*current_scope_valid_p=*/false);
19470 /* Look for the nested-name specifier. */
19471 token = cp_lexer_peek_token (parser->lexer);
19472 cp_parser_nested_name_specifier (parser,
19473 /*typename_keyword_p=*/false,
19474 /*check_dependency_p=*/true,
19475 /*type_p=*/false,
19476 /*is_declaration=*/false);
19477 /* If we found it, and the next token is a `*', then we are
19478 indeed looking at a pointer-to-member operator. */
19479 if (!cp_parser_error_occurred (parser)
19480 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19481 {
19482 /* Indicate that the `*' operator was used. */
19483 code = INDIRECT_REF;
19484
19485 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19486 error_at (token->location, "%qD is a namespace", parser->scope);
19487 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19488 error_at (token->location, "cannot form pointer to member of "
19489 "non-class %q#T", parser->scope);
19490 else
19491 {
19492 /* The type of which the member is a member is given by the
19493 current SCOPE. */
19494 *type = parser->scope;
19495 /* The next name will not be qualified. */
19496 parser->scope = NULL_TREE;
19497 parser->qualifying_scope = NULL_TREE;
19498 parser->object_scope = NULL_TREE;
19499 /* Look for optional c++11 attributes. */
19500 attrs = cp_parser_std_attribute_spec_seq (parser);
19501 if (attributes != NULL)
19502 *attributes = attrs;
19503 /* Look for the optional cv-qualifier-seq. */
19504 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19505 }
19506 }
19507 /* If that didn't work we don't have a ptr-operator. */
19508 if (!cp_parser_parse_definitely (parser))
19509 cp_parser_error (parser, "expected ptr-operator");
19510 }
19511
19512 return code;
19513 }
19514
19515 /* Parse an (optional) cv-qualifier-seq.
19516
19517 cv-qualifier-seq:
19518 cv-qualifier cv-qualifier-seq [opt]
19519
19520 cv-qualifier:
19521 const
19522 volatile
19523
19524 GNU Extension:
19525
19526 cv-qualifier:
19527 __restrict__
19528
19529 Returns a bitmask representing the cv-qualifiers. */
19530
19531 static cp_cv_quals
19532 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19533 {
19534 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19535
19536 while (true)
19537 {
19538 cp_token *token;
19539 cp_cv_quals cv_qualifier;
19540
19541 /* Peek at the next token. */
19542 token = cp_lexer_peek_token (parser->lexer);
19543 /* See if it's a cv-qualifier. */
19544 switch (token->keyword)
19545 {
19546 case RID_CONST:
19547 cv_qualifier = TYPE_QUAL_CONST;
19548 break;
19549
19550 case RID_VOLATILE:
19551 cv_qualifier = TYPE_QUAL_VOLATILE;
19552 break;
19553
19554 case RID_RESTRICT:
19555 cv_qualifier = TYPE_QUAL_RESTRICT;
19556 break;
19557
19558 default:
19559 cv_qualifier = TYPE_UNQUALIFIED;
19560 break;
19561 }
19562
19563 if (!cv_qualifier)
19564 break;
19565
19566 if (cv_quals & cv_qualifier)
19567 {
19568 error_at (token->location, "duplicate cv-qualifier");
19569 cp_lexer_purge_token (parser->lexer);
19570 }
19571 else
19572 {
19573 cp_lexer_consume_token (parser->lexer);
19574 cv_quals |= cv_qualifier;
19575 }
19576 }
19577
19578 return cv_quals;
19579 }
19580
19581 /* Parse an (optional) ref-qualifier
19582
19583 ref-qualifier:
19584 &
19585 &&
19586
19587 Returns cp_ref_qualifier representing ref-qualifier. */
19588
19589 static cp_ref_qualifier
19590 cp_parser_ref_qualifier_opt (cp_parser* parser)
19591 {
19592 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19593
19594 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19595 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19596 return ref_qual;
19597
19598 while (true)
19599 {
19600 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19601 cp_token *token = cp_lexer_peek_token (parser->lexer);
19602
19603 switch (token->type)
19604 {
19605 case CPP_AND:
19606 curr_ref_qual = REF_QUAL_LVALUE;
19607 break;
19608
19609 case CPP_AND_AND:
19610 curr_ref_qual = REF_QUAL_RVALUE;
19611 break;
19612
19613 default:
19614 curr_ref_qual = REF_QUAL_NONE;
19615 break;
19616 }
19617
19618 if (!curr_ref_qual)
19619 break;
19620 else if (ref_qual)
19621 {
19622 error_at (token->location, "multiple ref-qualifiers");
19623 cp_lexer_purge_token (parser->lexer);
19624 }
19625 else
19626 {
19627 ref_qual = curr_ref_qual;
19628 cp_lexer_consume_token (parser->lexer);
19629 }
19630 }
19631
19632 return ref_qual;
19633 }
19634
19635 /* Parse an optional tx-qualifier.
19636
19637 tx-qualifier:
19638 transaction_safe
19639 transaction_safe_dynamic */
19640
19641 static tree
19642 cp_parser_tx_qualifier_opt (cp_parser *parser)
19643 {
19644 cp_token *token = cp_lexer_peek_token (parser->lexer);
19645 if (token->type == CPP_NAME)
19646 {
19647 tree name = token->u.value;
19648 const char *p = IDENTIFIER_POINTER (name);
19649 const int len = strlen ("transaction_safe");
19650 if (!strncmp (p, "transaction_safe", len))
19651 {
19652 p += len;
19653 if (*p == '\0'
19654 || !strcmp (p, "_dynamic"))
19655 {
19656 cp_lexer_consume_token (parser->lexer);
19657 if (!flag_tm)
19658 {
19659 error ("%E requires %<-fgnu-tm%>", name);
19660 return NULL_TREE;
19661 }
19662 else
19663 return name;
19664 }
19665 }
19666 }
19667 return NULL_TREE;
19668 }
19669
19670 /* Parse an (optional) virt-specifier-seq.
19671
19672 virt-specifier-seq:
19673 virt-specifier virt-specifier-seq [opt]
19674
19675 virt-specifier:
19676 override
19677 final
19678
19679 Returns a bitmask representing the virt-specifiers. */
19680
19681 static cp_virt_specifiers
19682 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19683 {
19684 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19685
19686 while (true)
19687 {
19688 cp_token *token;
19689 cp_virt_specifiers virt_specifier;
19690
19691 /* Peek at the next token. */
19692 token = cp_lexer_peek_token (parser->lexer);
19693 /* See if it's a virt-specifier-qualifier. */
19694 if (token->type != CPP_NAME)
19695 break;
19696 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19697 {
19698 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19699 virt_specifier = VIRT_SPEC_OVERRIDE;
19700 }
19701 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19702 {
19703 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19704 virt_specifier = VIRT_SPEC_FINAL;
19705 }
19706 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19707 {
19708 virt_specifier = VIRT_SPEC_FINAL;
19709 }
19710 else
19711 break;
19712
19713 if (virt_specifiers & virt_specifier)
19714 {
19715 error_at (token->location, "duplicate virt-specifier");
19716 cp_lexer_purge_token (parser->lexer);
19717 }
19718 else
19719 {
19720 cp_lexer_consume_token (parser->lexer);
19721 virt_specifiers |= virt_specifier;
19722 }
19723 }
19724 return virt_specifiers;
19725 }
19726
19727 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19728 is in scope even though it isn't real. */
19729
19730 void
19731 inject_this_parameter (tree ctype, cp_cv_quals quals)
19732 {
19733 tree this_parm;
19734
19735 if (current_class_ptr)
19736 {
19737 /* We don't clear this between NSDMIs. Is it already what we want? */
19738 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19739 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19740 && cp_type_quals (type) == quals)
19741 return;
19742 }
19743
19744 this_parm = build_this_parm (ctype, quals);
19745 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19746 current_class_ptr = NULL_TREE;
19747 current_class_ref
19748 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19749 current_class_ptr = this_parm;
19750 }
19751
19752 /* Return true iff our current scope is a non-static data member
19753 initializer. */
19754
19755 bool
19756 parsing_nsdmi (void)
19757 {
19758 /* We recognize NSDMI context by the context-less 'this' pointer set up
19759 by the function above. */
19760 if (current_class_ptr
19761 && TREE_CODE (current_class_ptr) == PARM_DECL
19762 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19763 return true;
19764 return false;
19765 }
19766
19767 /* Parse a late-specified return type, if any. This is not a separate
19768 non-terminal, but part of a function declarator, which looks like
19769
19770 -> trailing-type-specifier-seq abstract-declarator(opt)
19771
19772 Returns the type indicated by the type-id.
19773
19774 In addition to this, parse any queued up omp declare simd
19775 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19776
19777 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19778 function. */
19779
19780 static tree
19781 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19782 tree& requires_clause, cp_cv_quals quals)
19783 {
19784 cp_token *token;
19785 tree type = NULL_TREE;
19786 bool declare_simd_p = (parser->omp_declare_simd
19787 && declarator
19788 && declarator->kind == cdk_id);
19789
19790 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19791 && declarator && declarator->kind == cdk_id);
19792
19793 bool oacc_routine_p = (parser->oacc_routine
19794 && declarator
19795 && declarator->kind == cdk_id);
19796
19797 /* Peek at the next token. */
19798 token = cp_lexer_peek_token (parser->lexer);
19799 /* A late-specified return type is indicated by an initial '->'. */
19800 if (token->type != CPP_DEREF
19801 && token->keyword != RID_REQUIRES
19802 && !(token->type == CPP_NAME
19803 && token->u.value == ridpointers[RID_REQUIRES])
19804 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19805 return NULL_TREE;
19806
19807 tree save_ccp = current_class_ptr;
19808 tree save_ccr = current_class_ref;
19809 if (quals >= 0)
19810 {
19811 /* DR 1207: 'this' is in scope in the trailing return type. */
19812 inject_this_parameter (current_class_type, quals);
19813 }
19814
19815 if (token->type == CPP_DEREF)
19816 {
19817 /* Consume the ->. */
19818 cp_lexer_consume_token (parser->lexer);
19819
19820 type = cp_parser_trailing_type_id (parser);
19821 }
19822
19823 /* Function declarations may be followed by a trailing
19824 requires-clause. */
19825 requires_clause = cp_parser_requires_clause_opt (parser);
19826
19827 if (cilk_simd_fn_vector_p)
19828 declarator->attributes
19829 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19830 declarator->attributes);
19831 if (declare_simd_p)
19832 declarator->attributes
19833 = cp_parser_late_parsing_omp_declare_simd (parser,
19834 declarator->attributes);
19835 if (oacc_routine_p)
19836 declarator->attributes
19837 = cp_parser_late_parsing_oacc_routine (parser,
19838 declarator->attributes);
19839
19840 if (quals >= 0)
19841 {
19842 current_class_ptr = save_ccp;
19843 current_class_ref = save_ccr;
19844 }
19845
19846 return type;
19847 }
19848
19849 /* Parse a declarator-id.
19850
19851 declarator-id:
19852 id-expression
19853 :: [opt] nested-name-specifier [opt] type-name
19854
19855 In the `id-expression' case, the value returned is as for
19856 cp_parser_id_expression if the id-expression was an unqualified-id.
19857 If the id-expression was a qualified-id, then a SCOPE_REF is
19858 returned. The first operand is the scope (either a NAMESPACE_DECL
19859 or TREE_TYPE), but the second is still just a representation of an
19860 unqualified-id. */
19861
19862 static tree
19863 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19864 {
19865 tree id;
19866 /* The expression must be an id-expression. Assume that qualified
19867 names are the names of types so that:
19868
19869 template <class T>
19870 int S<T>::R::i = 3;
19871
19872 will work; we must treat `S<T>::R' as the name of a type.
19873 Similarly, assume that qualified names are templates, where
19874 required, so that:
19875
19876 template <class T>
19877 int S<T>::R<T>::i = 3;
19878
19879 will work, too. */
19880 id = cp_parser_id_expression (parser,
19881 /*template_keyword_p=*/false,
19882 /*check_dependency_p=*/false,
19883 /*template_p=*/NULL,
19884 /*declarator_p=*/true,
19885 optional_p);
19886 if (id && BASELINK_P (id))
19887 id = BASELINK_FUNCTIONS (id);
19888 return id;
19889 }
19890
19891 /* Parse a type-id.
19892
19893 type-id:
19894 type-specifier-seq abstract-declarator [opt]
19895
19896 Returns the TYPE specified. */
19897
19898 static tree
19899 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19900 bool is_trailing_return)
19901 {
19902 cp_decl_specifier_seq type_specifier_seq;
19903 cp_declarator *abstract_declarator;
19904
19905 /* Parse the type-specifier-seq. */
19906 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19907 is_trailing_return,
19908 &type_specifier_seq);
19909 if (type_specifier_seq.type == error_mark_node)
19910 return error_mark_node;
19911
19912 /* There might or might not be an abstract declarator. */
19913 cp_parser_parse_tentatively (parser);
19914 /* Look for the declarator. */
19915 abstract_declarator
19916 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19917 /*parenthesized_p=*/NULL,
19918 /*member_p=*/false,
19919 /*friend_p=*/false);
19920 /* Check to see if there really was a declarator. */
19921 if (!cp_parser_parse_definitely (parser))
19922 abstract_declarator = NULL;
19923
19924 if (type_specifier_seq.type
19925 /* The concepts TS allows 'auto' as a type-id. */
19926 && (!flag_concepts || parser->in_type_id_in_expr_p)
19927 /* None of the valid uses of 'auto' in C++14 involve the type-id
19928 nonterminal, but it is valid in a trailing-return-type. */
19929 && !(cxx_dialect >= cxx14 && is_trailing_return)
19930 && type_uses_auto (type_specifier_seq.type))
19931 {
19932 /* A type-id with type 'auto' is only ok if the abstract declarator
19933 is a function declarator with a late-specified return type.
19934
19935 A type-id with 'auto' is also valid in a trailing-return-type
19936 in a compound-requirement. */
19937 if (abstract_declarator
19938 && abstract_declarator->kind == cdk_function
19939 && abstract_declarator->u.function.late_return_type)
19940 /* OK */;
19941 else if (parser->in_result_type_constraint_p)
19942 /* OK */;
19943 else
19944 {
19945 error ("invalid use of %<auto%>");
19946 return error_mark_node;
19947 }
19948 }
19949
19950 return groktypename (&type_specifier_seq, abstract_declarator,
19951 is_template_arg);
19952 }
19953
19954 static tree
19955 cp_parser_type_id (cp_parser *parser)
19956 {
19957 return cp_parser_type_id_1 (parser, false, false);
19958 }
19959
19960 static tree
19961 cp_parser_template_type_arg (cp_parser *parser)
19962 {
19963 tree r;
19964 const char *saved_message = parser->type_definition_forbidden_message;
19965 parser->type_definition_forbidden_message
19966 = G_("types may not be defined in template arguments");
19967 r = cp_parser_type_id_1 (parser, true, false);
19968 parser->type_definition_forbidden_message = saved_message;
19969 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19970 {
19971 error ("invalid use of %<auto%> in template argument");
19972 r = error_mark_node;
19973 }
19974 return r;
19975 }
19976
19977 static tree
19978 cp_parser_trailing_type_id (cp_parser *parser)
19979 {
19980 return cp_parser_type_id_1 (parser, false, true);
19981 }
19982
19983 /* Parse a type-specifier-seq.
19984
19985 type-specifier-seq:
19986 type-specifier type-specifier-seq [opt]
19987
19988 GNU extension:
19989
19990 type-specifier-seq:
19991 attributes type-specifier-seq [opt]
19992
19993 If IS_DECLARATION is true, we are at the start of a "condition" or
19994 exception-declaration, so we might be followed by a declarator-id.
19995
19996 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19997 i.e. we've just seen "->".
19998
19999 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20000
20001 static void
20002 cp_parser_type_specifier_seq (cp_parser* parser,
20003 bool is_declaration,
20004 bool is_trailing_return,
20005 cp_decl_specifier_seq *type_specifier_seq)
20006 {
20007 bool seen_type_specifier = false;
20008 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20009 cp_token *start_token = NULL;
20010
20011 /* Clear the TYPE_SPECIFIER_SEQ. */
20012 clear_decl_specs (type_specifier_seq);
20013
20014 /* In the context of a trailing return type, enum E { } is an
20015 elaborated-type-specifier followed by a function-body, not an
20016 enum-specifier. */
20017 if (is_trailing_return)
20018 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20019
20020 /* Parse the type-specifiers and attributes. */
20021 while (true)
20022 {
20023 tree type_specifier;
20024 bool is_cv_qualifier;
20025
20026 /* Check for attributes first. */
20027 if (cp_next_tokens_can_be_attribute_p (parser))
20028 {
20029 type_specifier_seq->attributes =
20030 chainon (type_specifier_seq->attributes,
20031 cp_parser_attributes_opt (parser));
20032 continue;
20033 }
20034
20035 /* record the token of the beginning of the type specifier seq,
20036 for error reporting purposes*/
20037 if (!start_token)
20038 start_token = cp_lexer_peek_token (parser->lexer);
20039
20040 /* Look for the type-specifier. */
20041 type_specifier = cp_parser_type_specifier (parser,
20042 flags,
20043 type_specifier_seq,
20044 /*is_declaration=*/false,
20045 NULL,
20046 &is_cv_qualifier);
20047 if (!type_specifier)
20048 {
20049 /* If the first type-specifier could not be found, this is not a
20050 type-specifier-seq at all. */
20051 if (!seen_type_specifier)
20052 {
20053 /* Set in_declarator_p to avoid skipping to the semicolon. */
20054 int in_decl = parser->in_declarator_p;
20055 parser->in_declarator_p = true;
20056
20057 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20058 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20059 cp_parser_error (parser, "expected type-specifier");
20060
20061 parser->in_declarator_p = in_decl;
20062
20063 type_specifier_seq->type = error_mark_node;
20064 return;
20065 }
20066 /* If subsequent type-specifiers could not be found, the
20067 type-specifier-seq is complete. */
20068 break;
20069 }
20070
20071 seen_type_specifier = true;
20072 /* The standard says that a condition can be:
20073
20074 type-specifier-seq declarator = assignment-expression
20075
20076 However, given:
20077
20078 struct S {};
20079 if (int S = ...)
20080
20081 we should treat the "S" as a declarator, not as a
20082 type-specifier. The standard doesn't say that explicitly for
20083 type-specifier-seq, but it does say that for
20084 decl-specifier-seq in an ordinary declaration. Perhaps it
20085 would be clearer just to allow a decl-specifier-seq here, and
20086 then add a semantic restriction that if any decl-specifiers
20087 that are not type-specifiers appear, the program is invalid. */
20088 if (is_declaration && !is_cv_qualifier)
20089 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20090 }
20091 }
20092
20093 /* Return whether the function currently being declared has an associated
20094 template parameter list. */
20095
20096 static bool
20097 function_being_declared_is_template_p (cp_parser* parser)
20098 {
20099 if (!current_template_parms || processing_template_parmlist)
20100 return false;
20101
20102 if (parser->implicit_template_scope)
20103 return true;
20104
20105 if (at_class_scope_p ()
20106 && TYPE_BEING_DEFINED (current_class_type))
20107 return parser->num_template_parameter_lists != 0;
20108
20109 return ((int) parser->num_template_parameter_lists > template_class_depth
20110 (current_class_type));
20111 }
20112
20113 /* Parse a parameter-declaration-clause.
20114
20115 parameter-declaration-clause:
20116 parameter-declaration-list [opt] ... [opt]
20117 parameter-declaration-list , ...
20118
20119 Returns a representation for the parameter declarations. A return
20120 value of NULL indicates a parameter-declaration-clause consisting
20121 only of an ellipsis. */
20122
20123 static tree
20124 cp_parser_parameter_declaration_clause (cp_parser* parser)
20125 {
20126 tree parameters;
20127 cp_token *token;
20128 bool ellipsis_p;
20129 bool is_error;
20130
20131 struct cleanup {
20132 cp_parser* parser;
20133 int auto_is_implicit_function_template_parm_p;
20134 ~cleanup() {
20135 parser->auto_is_implicit_function_template_parm_p
20136 = auto_is_implicit_function_template_parm_p;
20137 }
20138 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20139
20140 (void) cleanup;
20141
20142 if (!processing_specialization
20143 && !processing_template_parmlist
20144 && !processing_explicit_instantiation)
20145 if (!current_function_decl
20146 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20147 parser->auto_is_implicit_function_template_parm_p = true;
20148
20149 /* Peek at the next token. */
20150 token = cp_lexer_peek_token (parser->lexer);
20151 /* Check for trivial parameter-declaration-clauses. */
20152 if (token->type == CPP_ELLIPSIS)
20153 {
20154 /* Consume the `...' token. */
20155 cp_lexer_consume_token (parser->lexer);
20156 return NULL_TREE;
20157 }
20158 else if (token->type == CPP_CLOSE_PAREN)
20159 /* There are no parameters. */
20160 {
20161 #ifndef NO_IMPLICIT_EXTERN_C
20162 if (in_system_header_at (input_location)
20163 && current_class_type == NULL
20164 && current_lang_name == lang_name_c)
20165 return NULL_TREE;
20166 else
20167 #endif
20168 return void_list_node;
20169 }
20170 /* Check for `(void)', too, which is a special case. */
20171 else if (token->keyword == RID_VOID
20172 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20173 == CPP_CLOSE_PAREN))
20174 {
20175 /* Consume the `void' token. */
20176 cp_lexer_consume_token (parser->lexer);
20177 /* There are no parameters. */
20178 return void_list_node;
20179 }
20180
20181 /* Parse the parameter-declaration-list. */
20182 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20183 /* If a parse error occurred while parsing the
20184 parameter-declaration-list, then the entire
20185 parameter-declaration-clause is erroneous. */
20186 if (is_error)
20187 return NULL;
20188
20189 /* Peek at the next token. */
20190 token = cp_lexer_peek_token (parser->lexer);
20191 /* If it's a `,', the clause should terminate with an ellipsis. */
20192 if (token->type == CPP_COMMA)
20193 {
20194 /* Consume the `,'. */
20195 cp_lexer_consume_token (parser->lexer);
20196 /* Expect an ellipsis. */
20197 ellipsis_p
20198 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20199 }
20200 /* It might also be `...' if the optional trailing `,' was
20201 omitted. */
20202 else if (token->type == CPP_ELLIPSIS)
20203 {
20204 /* Consume the `...' token. */
20205 cp_lexer_consume_token (parser->lexer);
20206 /* And remember that we saw it. */
20207 ellipsis_p = true;
20208 }
20209 else
20210 ellipsis_p = false;
20211
20212 /* Finish the parameter list. */
20213 if (!ellipsis_p)
20214 parameters = chainon (parameters, void_list_node);
20215
20216 return parameters;
20217 }
20218
20219 /* Parse a parameter-declaration-list.
20220
20221 parameter-declaration-list:
20222 parameter-declaration
20223 parameter-declaration-list , parameter-declaration
20224
20225 Returns a representation of the parameter-declaration-list, as for
20226 cp_parser_parameter_declaration_clause. However, the
20227 `void_list_node' is never appended to the list. Upon return,
20228 *IS_ERROR will be true iff an error occurred. */
20229
20230 static tree
20231 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20232 {
20233 tree parameters = NULL_TREE;
20234 tree *tail = &parameters;
20235 bool saved_in_unbraced_linkage_specification_p;
20236 int index = 0;
20237
20238 /* Assume all will go well. */
20239 *is_error = false;
20240 /* The special considerations that apply to a function within an
20241 unbraced linkage specifications do not apply to the parameters
20242 to the function. */
20243 saved_in_unbraced_linkage_specification_p
20244 = parser->in_unbraced_linkage_specification_p;
20245 parser->in_unbraced_linkage_specification_p = false;
20246
20247 /* Look for more parameters. */
20248 while (true)
20249 {
20250 cp_parameter_declarator *parameter;
20251 tree decl = error_mark_node;
20252 bool parenthesized_p = false;
20253 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20254 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20255 (current_template_parms)) : 0);
20256
20257 /* Parse the parameter. */
20258 parameter
20259 = cp_parser_parameter_declaration (parser,
20260 /*template_parm_p=*/false,
20261 &parenthesized_p);
20262
20263 /* We don't know yet if the enclosing context is deprecated, so wait
20264 and warn in grokparms if appropriate. */
20265 deprecated_state = DEPRECATED_SUPPRESS;
20266
20267 if (parameter)
20268 {
20269 /* If a function parameter pack was specified and an implicit template
20270 parameter was introduced during cp_parser_parameter_declaration,
20271 change any implicit parameters introduced into packs. */
20272 if (parser->implicit_template_parms
20273 && parameter->declarator
20274 && parameter->declarator->parameter_pack_p)
20275 {
20276 int latest_template_parm_idx = TREE_VEC_LENGTH
20277 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20278
20279 if (latest_template_parm_idx != template_parm_idx)
20280 parameter->decl_specifiers.type = convert_generic_types_to_packs
20281 (parameter->decl_specifiers.type,
20282 template_parm_idx, latest_template_parm_idx);
20283 }
20284
20285 decl = grokdeclarator (parameter->declarator,
20286 &parameter->decl_specifiers,
20287 PARM,
20288 parameter->default_argument != NULL_TREE,
20289 &parameter->decl_specifiers.attributes);
20290 }
20291
20292 deprecated_state = DEPRECATED_NORMAL;
20293
20294 /* If a parse error occurred parsing the parameter declaration,
20295 then the entire parameter-declaration-list is erroneous. */
20296 if (decl == error_mark_node)
20297 {
20298 *is_error = true;
20299 parameters = error_mark_node;
20300 break;
20301 }
20302
20303 if (parameter->decl_specifiers.attributes)
20304 cplus_decl_attributes (&decl,
20305 parameter->decl_specifiers.attributes,
20306 0);
20307 if (DECL_NAME (decl))
20308 decl = pushdecl (decl);
20309
20310 if (decl != error_mark_node)
20311 {
20312 retrofit_lang_decl (decl);
20313 DECL_PARM_INDEX (decl) = ++index;
20314 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20315 }
20316
20317 /* Add the new parameter to the list. */
20318 *tail = build_tree_list (parameter->default_argument, decl);
20319 tail = &TREE_CHAIN (*tail);
20320
20321 /* Peek at the next token. */
20322 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20323 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20324 /* These are for Objective-C++ */
20325 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20326 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20327 /* The parameter-declaration-list is complete. */
20328 break;
20329 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20330 {
20331 cp_token *token;
20332
20333 /* Peek at the next token. */
20334 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20335 /* If it's an ellipsis, then the list is complete. */
20336 if (token->type == CPP_ELLIPSIS)
20337 break;
20338 /* Otherwise, there must be more parameters. Consume the
20339 `,'. */
20340 cp_lexer_consume_token (parser->lexer);
20341 /* When parsing something like:
20342
20343 int i(float f, double d)
20344
20345 we can tell after seeing the declaration for "f" that we
20346 are not looking at an initialization of a variable "i",
20347 but rather at the declaration of a function "i".
20348
20349 Due to the fact that the parsing of template arguments
20350 (as specified to a template-id) requires backtracking we
20351 cannot use this technique when inside a template argument
20352 list. */
20353 if (!parser->in_template_argument_list_p
20354 && !parser->in_type_id_in_expr_p
20355 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20356 /* However, a parameter-declaration of the form
20357 "float(f)" (which is a valid declaration of a
20358 parameter "f") can also be interpreted as an
20359 expression (the conversion of "f" to "float"). */
20360 && !parenthesized_p)
20361 cp_parser_commit_to_tentative_parse (parser);
20362 }
20363 else
20364 {
20365 cp_parser_error (parser, "expected %<,%> or %<...%>");
20366 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20367 cp_parser_skip_to_closing_parenthesis (parser,
20368 /*recovering=*/true,
20369 /*or_comma=*/false,
20370 /*consume_paren=*/false);
20371 break;
20372 }
20373 }
20374
20375 parser->in_unbraced_linkage_specification_p
20376 = saved_in_unbraced_linkage_specification_p;
20377
20378 /* Reset implicit_template_scope if we are about to leave the function
20379 parameter list that introduced it. Note that for out-of-line member
20380 definitions, there will be one or more class scopes before we get to
20381 the template parameter scope. */
20382
20383 if (cp_binding_level *its = parser->implicit_template_scope)
20384 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20385 {
20386 while (maybe_its->kind == sk_class)
20387 maybe_its = maybe_its->level_chain;
20388 if (maybe_its == its)
20389 {
20390 parser->implicit_template_parms = 0;
20391 parser->implicit_template_scope = 0;
20392 }
20393 }
20394
20395 return parameters;
20396 }
20397
20398 /* Parse a parameter declaration.
20399
20400 parameter-declaration:
20401 decl-specifier-seq ... [opt] declarator
20402 decl-specifier-seq declarator = assignment-expression
20403 decl-specifier-seq ... [opt] abstract-declarator [opt]
20404 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20405
20406 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20407 declares a template parameter. (In that case, a non-nested `>'
20408 token encountered during the parsing of the assignment-expression
20409 is not interpreted as a greater-than operator.)
20410
20411 Returns a representation of the parameter, or NULL if an error
20412 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20413 true iff the declarator is of the form "(p)". */
20414
20415 static cp_parameter_declarator *
20416 cp_parser_parameter_declaration (cp_parser *parser,
20417 bool template_parm_p,
20418 bool *parenthesized_p)
20419 {
20420 int declares_class_or_enum;
20421 cp_decl_specifier_seq decl_specifiers;
20422 cp_declarator *declarator;
20423 tree default_argument;
20424 cp_token *token = NULL, *declarator_token_start = NULL;
20425 const char *saved_message;
20426 bool template_parameter_pack_p = false;
20427
20428 /* In a template parameter, `>' is not an operator.
20429
20430 [temp.param]
20431
20432 When parsing a default template-argument for a non-type
20433 template-parameter, the first non-nested `>' is taken as the end
20434 of the template parameter-list rather than a greater-than
20435 operator. */
20436
20437 /* Type definitions may not appear in parameter types. */
20438 saved_message = parser->type_definition_forbidden_message;
20439 parser->type_definition_forbidden_message
20440 = G_("types may not be defined in parameter types");
20441
20442 /* Parse the declaration-specifiers. */
20443 cp_parser_decl_specifier_seq (parser,
20444 CP_PARSER_FLAGS_NONE,
20445 &decl_specifiers,
20446 &declares_class_or_enum);
20447
20448 /* Complain about missing 'typename' or other invalid type names. */
20449 if (!decl_specifiers.any_type_specifiers_p
20450 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20451 decl_specifiers.type = error_mark_node;
20452
20453 /* If an error occurred, there's no reason to attempt to parse the
20454 rest of the declaration. */
20455 if (cp_parser_error_occurred (parser))
20456 {
20457 parser->type_definition_forbidden_message = saved_message;
20458 return NULL;
20459 }
20460
20461 /* Peek at the next token. */
20462 token = cp_lexer_peek_token (parser->lexer);
20463
20464 /* If the next token is a `)', `,', `=', `>', or `...', then there
20465 is no declarator. However, when variadic templates are enabled,
20466 there may be a declarator following `...'. */
20467 if (token->type == CPP_CLOSE_PAREN
20468 || token->type == CPP_COMMA
20469 || token->type == CPP_EQ
20470 || token->type == CPP_GREATER)
20471 {
20472 declarator = NULL;
20473 if (parenthesized_p)
20474 *parenthesized_p = false;
20475 }
20476 /* Otherwise, there should be a declarator. */
20477 else
20478 {
20479 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20480 parser->default_arg_ok_p = false;
20481
20482 /* After seeing a decl-specifier-seq, if the next token is not a
20483 "(", there is no possibility that the code is a valid
20484 expression. Therefore, if parsing tentatively, we commit at
20485 this point. */
20486 if (!parser->in_template_argument_list_p
20487 /* In an expression context, having seen:
20488
20489 (int((char ...
20490
20491 we cannot be sure whether we are looking at a
20492 function-type (taking a "char" as a parameter) or a cast
20493 of some object of type "char" to "int". */
20494 && !parser->in_type_id_in_expr_p
20495 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20496 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20497 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20498 cp_parser_commit_to_tentative_parse (parser);
20499 /* Parse the declarator. */
20500 declarator_token_start = token;
20501 declarator = cp_parser_declarator (parser,
20502 CP_PARSER_DECLARATOR_EITHER,
20503 /*ctor_dtor_or_conv_p=*/NULL,
20504 parenthesized_p,
20505 /*member_p=*/false,
20506 /*friend_p=*/false);
20507 parser->default_arg_ok_p = saved_default_arg_ok_p;
20508 /* After the declarator, allow more attributes. */
20509 decl_specifiers.attributes
20510 = chainon (decl_specifiers.attributes,
20511 cp_parser_attributes_opt (parser));
20512
20513 /* If the declarator is a template parameter pack, remember that and
20514 clear the flag in the declarator itself so we don't get errors
20515 from grokdeclarator. */
20516 if (template_parm_p && declarator && declarator->parameter_pack_p)
20517 {
20518 declarator->parameter_pack_p = false;
20519 template_parameter_pack_p = true;
20520 }
20521 }
20522
20523 /* If the next token is an ellipsis, and we have not seen a declarator
20524 name, and if either the type of the declarator contains parameter
20525 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20526 for, eg, abbreviated integral type names), then we actually have a
20527 parameter pack expansion expression. Otherwise, leave the ellipsis
20528 for a C-style variadic function. */
20529 token = cp_lexer_peek_token (parser->lexer);
20530 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20531 {
20532 tree type = decl_specifiers.type;
20533
20534 if (type && DECL_P (type))
20535 type = TREE_TYPE (type);
20536
20537 if (((type
20538 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20539 && (template_parm_p || uses_parameter_packs (type)))
20540 || (!type && template_parm_p))
20541 && declarator_can_be_parameter_pack (declarator))
20542 {
20543 /* Consume the `...'. */
20544 cp_lexer_consume_token (parser->lexer);
20545 maybe_warn_variadic_templates ();
20546
20547 /* Build a pack expansion type */
20548 if (template_parm_p)
20549 template_parameter_pack_p = true;
20550 else if (declarator)
20551 declarator->parameter_pack_p = true;
20552 else
20553 decl_specifiers.type = make_pack_expansion (type);
20554 }
20555 }
20556
20557 /* The restriction on defining new types applies only to the type
20558 of the parameter, not to the default argument. */
20559 parser->type_definition_forbidden_message = saved_message;
20560
20561 /* If the next token is `=', then process a default argument. */
20562 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20563 {
20564 tree type = decl_specifiers.type;
20565 token = cp_lexer_peek_token (parser->lexer);
20566 /* If we are defining a class, then the tokens that make up the
20567 default argument must be saved and processed later. */
20568 if (!template_parm_p && at_class_scope_p ()
20569 && TYPE_BEING_DEFINED (current_class_type)
20570 && !LAMBDA_TYPE_P (current_class_type))
20571 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20572
20573 // A constrained-type-specifier may declare a type template-parameter.
20574 else if (declares_constrained_type_template_parameter (type))
20575 default_argument
20576 = cp_parser_default_type_template_argument (parser);
20577
20578 // A constrained-type-specifier may declare a template-template-parameter.
20579 else if (declares_constrained_template_template_parameter (type))
20580 default_argument
20581 = cp_parser_default_template_template_argument (parser);
20582
20583 /* Outside of a class definition, we can just parse the
20584 assignment-expression. */
20585 else
20586 default_argument
20587 = cp_parser_default_argument (parser, template_parm_p);
20588
20589 if (!parser->default_arg_ok_p)
20590 {
20591 permerror (token->location,
20592 "default arguments are only "
20593 "permitted for function parameters");
20594 }
20595 else if ((declarator && declarator->parameter_pack_p)
20596 || template_parameter_pack_p
20597 || (decl_specifiers.type
20598 && PACK_EXPANSION_P (decl_specifiers.type)))
20599 {
20600 /* Find the name of the parameter pack. */
20601 cp_declarator *id_declarator = declarator;
20602 while (id_declarator && id_declarator->kind != cdk_id)
20603 id_declarator = id_declarator->declarator;
20604
20605 if (id_declarator && id_declarator->kind == cdk_id)
20606 error_at (declarator_token_start->location,
20607 template_parm_p
20608 ? G_("template parameter pack %qD "
20609 "cannot have a default argument")
20610 : G_("parameter pack %qD cannot have "
20611 "a default argument"),
20612 id_declarator->u.id.unqualified_name);
20613 else
20614 error_at (declarator_token_start->location,
20615 template_parm_p
20616 ? G_("template parameter pack cannot have "
20617 "a default argument")
20618 : G_("parameter pack cannot have a "
20619 "default argument"));
20620
20621 default_argument = NULL_TREE;
20622 }
20623 }
20624 else
20625 default_argument = NULL_TREE;
20626
20627 return make_parameter_declarator (&decl_specifiers,
20628 declarator,
20629 default_argument,
20630 template_parameter_pack_p);
20631 }
20632
20633 /* Parse a default argument and return it.
20634
20635 TEMPLATE_PARM_P is true if this is a default argument for a
20636 non-type template parameter. */
20637 static tree
20638 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20639 {
20640 tree default_argument = NULL_TREE;
20641 bool saved_greater_than_is_operator_p;
20642 bool saved_local_variables_forbidden_p;
20643 bool non_constant_p, is_direct_init;
20644
20645 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20646 set correctly. */
20647 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20648 parser->greater_than_is_operator_p = !template_parm_p;
20649 /* Local variable names (and the `this' keyword) may not
20650 appear in a default argument. */
20651 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20652 parser->local_variables_forbidden_p = true;
20653 /* Parse the assignment-expression. */
20654 if (template_parm_p)
20655 push_deferring_access_checks (dk_no_deferred);
20656 tree saved_class_ptr = NULL_TREE;
20657 tree saved_class_ref = NULL_TREE;
20658 /* The "this" pointer is not valid in a default argument. */
20659 if (cfun)
20660 {
20661 saved_class_ptr = current_class_ptr;
20662 cp_function_chain->x_current_class_ptr = NULL_TREE;
20663 saved_class_ref = current_class_ref;
20664 cp_function_chain->x_current_class_ref = NULL_TREE;
20665 }
20666 default_argument
20667 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20668 /* Restore the "this" pointer. */
20669 if (cfun)
20670 {
20671 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20672 cp_function_chain->x_current_class_ref = saved_class_ref;
20673 }
20674 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20675 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20676 if (template_parm_p)
20677 pop_deferring_access_checks ();
20678 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20679 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20680
20681 return default_argument;
20682 }
20683
20684 /* Parse a function-body.
20685
20686 function-body:
20687 compound_statement */
20688
20689 static void
20690 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20691 {
20692 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20693 ? BCS_TRY_BLOCK : BCS_NORMAL),
20694 true);
20695 }
20696
20697 /* Parse a ctor-initializer-opt followed by a function-body. Return
20698 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20699 is true we are parsing a function-try-block. */
20700
20701 static bool
20702 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20703 bool in_function_try_block)
20704 {
20705 tree body, list;
20706 bool ctor_initializer_p;
20707 const bool check_body_p =
20708 DECL_CONSTRUCTOR_P (current_function_decl)
20709 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20710 tree last = NULL;
20711
20712 /* Begin the function body. */
20713 body = begin_function_body ();
20714 /* Parse the optional ctor-initializer. */
20715 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20716
20717 /* If we're parsing a constexpr constructor definition, we need
20718 to check that the constructor body is indeed empty. However,
20719 before we get to cp_parser_function_body lot of junk has been
20720 generated, so we can't just check that we have an empty block.
20721 Rather we take a snapshot of the outermost block, and check whether
20722 cp_parser_function_body changed its state. */
20723 if (check_body_p)
20724 {
20725 list = cur_stmt_list;
20726 if (STATEMENT_LIST_TAIL (list))
20727 last = STATEMENT_LIST_TAIL (list)->stmt;
20728 }
20729 /* Parse the function-body. */
20730 cp_parser_function_body (parser, in_function_try_block);
20731 if (check_body_p)
20732 check_constexpr_ctor_body (last, list, /*complain=*/true);
20733 /* Finish the function body. */
20734 finish_function_body (body);
20735
20736 return ctor_initializer_p;
20737 }
20738
20739 /* Parse an initializer.
20740
20741 initializer:
20742 = initializer-clause
20743 ( expression-list )
20744
20745 Returns an expression representing the initializer. If no
20746 initializer is present, NULL_TREE is returned.
20747
20748 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20749 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20750 set to TRUE if there is no initializer present. If there is an
20751 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20752 is set to true; otherwise it is set to false. */
20753
20754 static tree
20755 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20756 bool* non_constant_p)
20757 {
20758 cp_token *token;
20759 tree init;
20760
20761 /* Peek at the next token. */
20762 token = cp_lexer_peek_token (parser->lexer);
20763
20764 /* Let our caller know whether or not this initializer was
20765 parenthesized. */
20766 *is_direct_init = (token->type != CPP_EQ);
20767 /* Assume that the initializer is constant. */
20768 *non_constant_p = false;
20769
20770 if (token->type == CPP_EQ)
20771 {
20772 /* Consume the `='. */
20773 cp_lexer_consume_token (parser->lexer);
20774 /* Parse the initializer-clause. */
20775 init = cp_parser_initializer_clause (parser, non_constant_p);
20776 }
20777 else if (token->type == CPP_OPEN_PAREN)
20778 {
20779 vec<tree, va_gc> *vec;
20780 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20781 /*cast_p=*/false,
20782 /*allow_expansion_p=*/true,
20783 non_constant_p);
20784 if (vec == NULL)
20785 return error_mark_node;
20786 init = build_tree_list_vec (vec);
20787 release_tree_vector (vec);
20788 }
20789 else if (token->type == CPP_OPEN_BRACE)
20790 {
20791 cp_lexer_set_source_position (parser->lexer);
20792 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20793 init = cp_parser_braced_list (parser, non_constant_p);
20794 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20795 }
20796 else
20797 {
20798 /* Anything else is an error. */
20799 cp_parser_error (parser, "expected initializer");
20800 init = error_mark_node;
20801 }
20802
20803 if (check_for_bare_parameter_packs (init))
20804 init = error_mark_node;
20805
20806 return init;
20807 }
20808
20809 /* Parse an initializer-clause.
20810
20811 initializer-clause:
20812 assignment-expression
20813 braced-init-list
20814
20815 Returns an expression representing the initializer.
20816
20817 If the `assignment-expression' production is used the value
20818 returned is simply a representation for the expression.
20819
20820 Otherwise, calls cp_parser_braced_list. */
20821
20822 static cp_expr
20823 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20824 {
20825 cp_expr initializer;
20826
20827 /* Assume the expression is constant. */
20828 *non_constant_p = false;
20829
20830 /* If it is not a `{', then we are looking at an
20831 assignment-expression. */
20832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20833 {
20834 initializer
20835 = cp_parser_constant_expression (parser,
20836 /*allow_non_constant_p=*/true,
20837 non_constant_p);
20838 }
20839 else
20840 initializer = cp_parser_braced_list (parser, non_constant_p);
20841
20842 return initializer;
20843 }
20844
20845 /* Parse a brace-enclosed initializer list.
20846
20847 braced-init-list:
20848 { initializer-list , [opt] }
20849 { }
20850
20851 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20852 the elements of the initializer-list (or NULL, if the last
20853 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20854 NULL_TREE. There is no way to detect whether or not the optional
20855 trailing `,' was provided. NON_CONSTANT_P is as for
20856 cp_parser_initializer. */
20857
20858 static cp_expr
20859 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20860 {
20861 tree initializer;
20862 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20863
20864 /* Consume the `{' token. */
20865 cp_lexer_consume_token (parser->lexer);
20866 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20867 initializer = make_node (CONSTRUCTOR);
20868 /* If it's not a `}', then there is a non-trivial initializer. */
20869 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20870 {
20871 /* Parse the initializer list. */
20872 CONSTRUCTOR_ELTS (initializer)
20873 = cp_parser_initializer_list (parser, non_constant_p);
20874 /* A trailing `,' token is allowed. */
20875 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20876 cp_lexer_consume_token (parser->lexer);
20877 }
20878 else
20879 *non_constant_p = false;
20880 /* Now, there should be a trailing `}'. */
20881 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20882 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20883 TREE_TYPE (initializer) = init_list_type_node;
20884
20885 cp_expr result (initializer);
20886 /* Build a location of the form:
20887 { ... }
20888 ^~~~~~~
20889 with caret==start at the open brace, finish at the close brace. */
20890 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20891 result.set_location (combined_loc);
20892 return result;
20893 }
20894
20895 /* Consume tokens up to, and including, the next non-nested closing `]'.
20896 Returns true iff we found a closing `]'. */
20897
20898 static bool
20899 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20900 {
20901 unsigned square_depth = 0;
20902
20903 while (true)
20904 {
20905 cp_token * token = cp_lexer_peek_token (parser->lexer);
20906
20907 switch (token->type)
20908 {
20909 case CPP_EOF:
20910 case CPP_PRAGMA_EOL:
20911 /* If we've run out of tokens, then there is no closing `]'. */
20912 return false;
20913
20914 case CPP_OPEN_SQUARE:
20915 ++square_depth;
20916 break;
20917
20918 case CPP_CLOSE_SQUARE:
20919 if (!square_depth--)
20920 {
20921 cp_lexer_consume_token (parser->lexer);
20922 return true;
20923 }
20924 break;
20925
20926 default:
20927 break;
20928 }
20929
20930 /* Consume the token. */
20931 cp_lexer_consume_token (parser->lexer);
20932 }
20933 }
20934
20935 /* Return true if we are looking at an array-designator, false otherwise. */
20936
20937 static bool
20938 cp_parser_array_designator_p (cp_parser *parser)
20939 {
20940 /* Consume the `['. */
20941 cp_lexer_consume_token (parser->lexer);
20942
20943 cp_lexer_save_tokens (parser->lexer);
20944
20945 /* Skip tokens until the next token is a closing square bracket.
20946 If we find the closing `]', and the next token is a `=', then
20947 we are looking at an array designator. */
20948 bool array_designator_p
20949 = (cp_parser_skip_to_closing_square_bracket (parser)
20950 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20951
20952 /* Roll back the tokens we skipped. */
20953 cp_lexer_rollback_tokens (parser->lexer);
20954
20955 return array_designator_p;
20956 }
20957
20958 /* Parse an initializer-list.
20959
20960 initializer-list:
20961 initializer-clause ... [opt]
20962 initializer-list , initializer-clause ... [opt]
20963
20964 GNU Extension:
20965
20966 initializer-list:
20967 designation initializer-clause ...[opt]
20968 initializer-list , designation initializer-clause ...[opt]
20969
20970 designation:
20971 . identifier =
20972 identifier :
20973 [ constant-expression ] =
20974
20975 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20976 for the initializer. If the INDEX of the elt is non-NULL, it is the
20977 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20978 as for cp_parser_initializer. */
20979
20980 static vec<constructor_elt, va_gc> *
20981 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20982 {
20983 vec<constructor_elt, va_gc> *v = NULL;
20984
20985 /* Assume all of the expressions are constant. */
20986 *non_constant_p = false;
20987
20988 /* Parse the rest of the list. */
20989 while (true)
20990 {
20991 cp_token *token;
20992 tree designator;
20993 tree initializer;
20994 bool clause_non_constant_p;
20995
20996 /* If the next token is an identifier and the following one is a
20997 colon, we are looking at the GNU designated-initializer
20998 syntax. */
20999 if (cp_parser_allow_gnu_extensions_p (parser)
21000 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21001 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21002 {
21003 /* Warn the user that they are using an extension. */
21004 pedwarn (input_location, OPT_Wpedantic,
21005 "ISO C++ does not allow designated initializers");
21006 /* Consume the identifier. */
21007 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21008 /* Consume the `:'. */
21009 cp_lexer_consume_token (parser->lexer);
21010 }
21011 /* Also handle the C99 syntax, '. id ='. */
21012 else if (cp_parser_allow_gnu_extensions_p (parser)
21013 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21014 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21015 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21016 {
21017 /* Warn the user that they are using an extension. */
21018 pedwarn (input_location, OPT_Wpedantic,
21019 "ISO C++ does not allow C99 designated initializers");
21020 /* Consume the `.'. */
21021 cp_lexer_consume_token (parser->lexer);
21022 /* Consume the identifier. */
21023 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21024 /* Consume the `='. */
21025 cp_lexer_consume_token (parser->lexer);
21026 }
21027 /* Also handle C99 array designators, '[ const ] ='. */
21028 else if (cp_parser_allow_gnu_extensions_p (parser)
21029 && !c_dialect_objc ()
21030 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21031 {
21032 /* In C++11, [ could start a lambda-introducer. */
21033 bool non_const = false;
21034
21035 cp_parser_parse_tentatively (parser);
21036
21037 if (!cp_parser_array_designator_p (parser))
21038 {
21039 cp_parser_simulate_error (parser);
21040 designator = NULL_TREE;
21041 }
21042 else
21043 {
21044 designator = cp_parser_constant_expression (parser, true,
21045 &non_const);
21046 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21047 cp_parser_require (parser, CPP_EQ, RT_EQ);
21048 }
21049
21050 if (!cp_parser_parse_definitely (parser))
21051 designator = NULL_TREE;
21052 else if (non_const)
21053 require_potential_rvalue_constant_expression (designator);
21054 }
21055 else
21056 designator = NULL_TREE;
21057
21058 /* Parse the initializer. */
21059 initializer = cp_parser_initializer_clause (parser,
21060 &clause_non_constant_p);
21061 /* If any clause is non-constant, so is the entire initializer. */
21062 if (clause_non_constant_p)
21063 *non_constant_p = true;
21064
21065 /* If we have an ellipsis, this is an initializer pack
21066 expansion. */
21067 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21068 {
21069 /* Consume the `...'. */
21070 cp_lexer_consume_token (parser->lexer);
21071
21072 /* Turn the initializer into an initializer expansion. */
21073 initializer = make_pack_expansion (initializer);
21074 }
21075
21076 /* Add it to the vector. */
21077 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21078
21079 /* If the next token is not a comma, we have reached the end of
21080 the list. */
21081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21082 break;
21083
21084 /* Peek at the next token. */
21085 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21086 /* If the next token is a `}', then we're still done. An
21087 initializer-clause can have a trailing `,' after the
21088 initializer-list and before the closing `}'. */
21089 if (token->type == CPP_CLOSE_BRACE)
21090 break;
21091
21092 /* Consume the `,' token. */
21093 cp_lexer_consume_token (parser->lexer);
21094 }
21095
21096 return v;
21097 }
21098
21099 /* Classes [gram.class] */
21100
21101 /* Parse a class-name.
21102
21103 class-name:
21104 identifier
21105 template-id
21106
21107 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21108 to indicate that names looked up in dependent types should be
21109 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21110 keyword has been used to indicate that the name that appears next
21111 is a template. TAG_TYPE indicates the explicit tag given before
21112 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21113 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21114 is the class being defined in a class-head. If ENUM_OK is TRUE,
21115 enum-names are also accepted.
21116
21117 Returns the TYPE_DECL representing the class. */
21118
21119 static tree
21120 cp_parser_class_name (cp_parser *parser,
21121 bool typename_keyword_p,
21122 bool template_keyword_p,
21123 enum tag_types tag_type,
21124 bool check_dependency_p,
21125 bool class_head_p,
21126 bool is_declaration,
21127 bool enum_ok)
21128 {
21129 tree decl;
21130 tree scope;
21131 bool typename_p;
21132 cp_token *token;
21133 tree identifier = NULL_TREE;
21134
21135 /* All class-names start with an identifier. */
21136 token = cp_lexer_peek_token (parser->lexer);
21137 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21138 {
21139 cp_parser_error (parser, "expected class-name");
21140 return error_mark_node;
21141 }
21142
21143 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21144 to a template-id, so we save it here. */
21145 scope = parser->scope;
21146 if (scope == error_mark_node)
21147 return error_mark_node;
21148
21149 /* Any name names a type if we're following the `typename' keyword
21150 in a qualified name where the enclosing scope is type-dependent. */
21151 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21152 && dependent_type_p (scope));
21153 /* Handle the common case (an identifier, but not a template-id)
21154 efficiently. */
21155 if (token->type == CPP_NAME
21156 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21157 {
21158 cp_token *identifier_token;
21159 bool ambiguous_p;
21160
21161 /* Look for the identifier. */
21162 identifier_token = cp_lexer_peek_token (parser->lexer);
21163 ambiguous_p = identifier_token->error_reported;
21164 identifier = cp_parser_identifier (parser);
21165 /* If the next token isn't an identifier, we are certainly not
21166 looking at a class-name. */
21167 if (identifier == error_mark_node)
21168 decl = error_mark_node;
21169 /* If we know this is a type-name, there's no need to look it
21170 up. */
21171 else if (typename_p)
21172 decl = identifier;
21173 else
21174 {
21175 tree ambiguous_decls;
21176 /* If we already know that this lookup is ambiguous, then
21177 we've already issued an error message; there's no reason
21178 to check again. */
21179 if (ambiguous_p)
21180 {
21181 cp_parser_simulate_error (parser);
21182 return error_mark_node;
21183 }
21184 /* If the next token is a `::', then the name must be a type
21185 name.
21186
21187 [basic.lookup.qual]
21188
21189 During the lookup for a name preceding the :: scope
21190 resolution operator, object, function, and enumerator
21191 names are ignored. */
21192 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21193 tag_type = typename_type;
21194 /* Look up the name. */
21195 decl = cp_parser_lookup_name (parser, identifier,
21196 tag_type,
21197 /*is_template=*/false,
21198 /*is_namespace=*/false,
21199 check_dependency_p,
21200 &ambiguous_decls,
21201 identifier_token->location);
21202 if (ambiguous_decls)
21203 {
21204 if (cp_parser_parsing_tentatively (parser))
21205 cp_parser_simulate_error (parser);
21206 return error_mark_node;
21207 }
21208 }
21209 }
21210 else
21211 {
21212 /* Try a template-id. */
21213 decl = cp_parser_template_id (parser, template_keyword_p,
21214 check_dependency_p,
21215 tag_type,
21216 is_declaration);
21217 if (decl == error_mark_node)
21218 return error_mark_node;
21219 }
21220
21221 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21222
21223 /* If this is a typename, create a TYPENAME_TYPE. */
21224 if (typename_p && decl != error_mark_node)
21225 {
21226 decl = make_typename_type (scope, decl, typename_type,
21227 /*complain=*/tf_error);
21228 if (decl != error_mark_node)
21229 decl = TYPE_NAME (decl);
21230 }
21231
21232 decl = strip_using_decl (decl);
21233
21234 /* Check to see that it is really the name of a class. */
21235 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21236 && identifier_p (TREE_OPERAND (decl, 0))
21237 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21238 /* Situations like this:
21239
21240 template <typename T> struct A {
21241 typename T::template X<int>::I i;
21242 };
21243
21244 are problematic. Is `T::template X<int>' a class-name? The
21245 standard does not seem to be definitive, but there is no other
21246 valid interpretation of the following `::'. Therefore, those
21247 names are considered class-names. */
21248 {
21249 decl = make_typename_type (scope, decl, tag_type, tf_error);
21250 if (decl != error_mark_node)
21251 decl = TYPE_NAME (decl);
21252 }
21253 else if (TREE_CODE (decl) != TYPE_DECL
21254 || TREE_TYPE (decl) == error_mark_node
21255 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21256 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21257 /* In Objective-C 2.0, a classname followed by '.' starts a
21258 dot-syntax expression, and it's not a type-name. */
21259 || (c_dialect_objc ()
21260 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21261 && objc_is_class_name (decl)))
21262 decl = error_mark_node;
21263
21264 if (decl == error_mark_node)
21265 cp_parser_error (parser, "expected class-name");
21266 else if (identifier && !parser->scope)
21267 maybe_note_name_used_in_class (identifier, decl);
21268
21269 return decl;
21270 }
21271
21272 /* Parse a class-specifier.
21273
21274 class-specifier:
21275 class-head { member-specification [opt] }
21276
21277 Returns the TREE_TYPE representing the class. */
21278
21279 static tree
21280 cp_parser_class_specifier_1 (cp_parser* parser)
21281 {
21282 tree type;
21283 tree attributes = NULL_TREE;
21284 bool nested_name_specifier_p;
21285 unsigned saved_num_template_parameter_lists;
21286 bool saved_in_function_body;
21287 unsigned char in_statement;
21288 bool in_switch_statement_p;
21289 bool saved_in_unbraced_linkage_specification_p;
21290 tree old_scope = NULL_TREE;
21291 tree scope = NULL_TREE;
21292 cp_token *closing_brace;
21293
21294 push_deferring_access_checks (dk_no_deferred);
21295
21296 /* Parse the class-head. */
21297 type = cp_parser_class_head (parser,
21298 &nested_name_specifier_p);
21299 /* If the class-head was a semantic disaster, skip the entire body
21300 of the class. */
21301 if (!type)
21302 {
21303 cp_parser_skip_to_end_of_block_or_statement (parser);
21304 pop_deferring_access_checks ();
21305 return error_mark_node;
21306 }
21307
21308 /* Look for the `{'. */
21309 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21310 {
21311 pop_deferring_access_checks ();
21312 return error_mark_node;
21313 }
21314
21315 cp_ensure_no_omp_declare_simd (parser);
21316 cp_ensure_no_oacc_routine (parser);
21317
21318 /* Issue an error message if type-definitions are forbidden here. */
21319 cp_parser_check_type_definition (parser);
21320 /* Remember that we are defining one more class. */
21321 ++parser->num_classes_being_defined;
21322 /* Inside the class, surrounding template-parameter-lists do not
21323 apply. */
21324 saved_num_template_parameter_lists
21325 = parser->num_template_parameter_lists;
21326 parser->num_template_parameter_lists = 0;
21327 /* We are not in a function body. */
21328 saved_in_function_body = parser->in_function_body;
21329 parser->in_function_body = false;
21330 /* Or in a loop. */
21331 in_statement = parser->in_statement;
21332 parser->in_statement = 0;
21333 /* Or in a switch. */
21334 in_switch_statement_p = parser->in_switch_statement_p;
21335 parser->in_switch_statement_p = false;
21336 /* We are not immediately inside an extern "lang" block. */
21337 saved_in_unbraced_linkage_specification_p
21338 = parser->in_unbraced_linkage_specification_p;
21339 parser->in_unbraced_linkage_specification_p = false;
21340
21341 // Associate constraints with the type.
21342 if (flag_concepts)
21343 type = associate_classtype_constraints (type);
21344
21345 /* Start the class. */
21346 if (nested_name_specifier_p)
21347 {
21348 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21349 old_scope = push_inner_scope (scope);
21350 }
21351 type = begin_class_definition (type);
21352
21353 if (type == error_mark_node)
21354 /* If the type is erroneous, skip the entire body of the class. */
21355 cp_parser_skip_to_closing_brace (parser);
21356 else
21357 /* Parse the member-specification. */
21358 cp_parser_member_specification_opt (parser);
21359
21360 /* Look for the trailing `}'. */
21361 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21362 /* Look for trailing attributes to apply to this class. */
21363 if (cp_parser_allow_gnu_extensions_p (parser))
21364 attributes = cp_parser_gnu_attributes_opt (parser);
21365 if (type != error_mark_node)
21366 type = finish_struct (type, attributes);
21367 if (nested_name_specifier_p)
21368 pop_inner_scope (old_scope, scope);
21369
21370 /* We've finished a type definition. Check for the common syntax
21371 error of forgetting a semicolon after the definition. We need to
21372 be careful, as we can't just check for not-a-semicolon and be done
21373 with it; the user might have typed:
21374
21375 class X { } c = ...;
21376 class X { } *p = ...;
21377
21378 and so forth. Instead, enumerate all the possible tokens that
21379 might follow this production; if we don't see one of them, then
21380 complain and silently insert the semicolon. */
21381 {
21382 cp_token *token = cp_lexer_peek_token (parser->lexer);
21383 bool want_semicolon = true;
21384
21385 if (cp_next_tokens_can_be_std_attribute_p (parser))
21386 /* Don't try to parse c++11 attributes here. As per the
21387 grammar, that should be a task for
21388 cp_parser_decl_specifier_seq. */
21389 want_semicolon = false;
21390
21391 switch (token->type)
21392 {
21393 case CPP_NAME:
21394 case CPP_SEMICOLON:
21395 case CPP_MULT:
21396 case CPP_AND:
21397 case CPP_OPEN_PAREN:
21398 case CPP_CLOSE_PAREN:
21399 case CPP_COMMA:
21400 want_semicolon = false;
21401 break;
21402
21403 /* While it's legal for type qualifiers and storage class
21404 specifiers to follow type definitions in the grammar, only
21405 compiler testsuites contain code like that. Assume that if
21406 we see such code, then what we're really seeing is a case
21407 like:
21408
21409 class X { }
21410 const <type> var = ...;
21411
21412 or
21413
21414 class Y { }
21415 static <type> func (...) ...
21416
21417 i.e. the qualifier or specifier applies to the next
21418 declaration. To do so, however, we need to look ahead one
21419 more token to see if *that* token is a type specifier.
21420
21421 This code could be improved to handle:
21422
21423 class Z { }
21424 static const <type> var = ...; */
21425 case CPP_KEYWORD:
21426 if (keyword_is_decl_specifier (token->keyword))
21427 {
21428 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21429
21430 /* Handling user-defined types here would be nice, but very
21431 tricky. */
21432 want_semicolon
21433 = (lookahead->type == CPP_KEYWORD
21434 && keyword_begins_type_specifier (lookahead->keyword));
21435 }
21436 break;
21437 default:
21438 break;
21439 }
21440
21441 /* If we don't have a type, then something is very wrong and we
21442 shouldn't try to do anything clever. Likewise for not seeing the
21443 closing brace. */
21444 if (closing_brace && TYPE_P (type) && want_semicolon)
21445 {
21446 cp_token_position prev
21447 = cp_lexer_previous_token_position (parser->lexer);
21448 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21449 location_t loc = prev_token->location;
21450
21451 if (CLASSTYPE_DECLARED_CLASS (type))
21452 error_at (loc, "expected %<;%> after class definition");
21453 else if (TREE_CODE (type) == RECORD_TYPE)
21454 error_at (loc, "expected %<;%> after struct definition");
21455 else if (TREE_CODE (type) == UNION_TYPE)
21456 error_at (loc, "expected %<;%> after union definition");
21457 else
21458 gcc_unreachable ();
21459
21460 /* Unget one token and smash it to look as though we encountered
21461 a semicolon in the input stream. */
21462 cp_lexer_set_token_position (parser->lexer, prev);
21463 token = cp_lexer_peek_token (parser->lexer);
21464 token->type = CPP_SEMICOLON;
21465 token->keyword = RID_MAX;
21466 }
21467 }
21468
21469 /* If this class is not itself within the scope of another class,
21470 then we need to parse the bodies of all of the queued function
21471 definitions. Note that the queued functions defined in a class
21472 are not always processed immediately following the
21473 class-specifier for that class. Consider:
21474
21475 struct A {
21476 struct B { void f() { sizeof (A); } };
21477 };
21478
21479 If `f' were processed before the processing of `A' were
21480 completed, there would be no way to compute the size of `A'.
21481 Note that the nesting we are interested in here is lexical --
21482 not the semantic nesting given by TYPE_CONTEXT. In particular,
21483 for:
21484
21485 struct A { struct B; };
21486 struct A::B { void f() { } };
21487
21488 there is no need to delay the parsing of `A::B::f'. */
21489 if (--parser->num_classes_being_defined == 0)
21490 {
21491 tree decl;
21492 tree class_type = NULL_TREE;
21493 tree pushed_scope = NULL_TREE;
21494 unsigned ix;
21495 cp_default_arg_entry *e;
21496 tree save_ccp, save_ccr;
21497
21498 /* In a first pass, parse default arguments to the functions.
21499 Then, in a second pass, parse the bodies of the functions.
21500 This two-phased approach handles cases like:
21501
21502 struct S {
21503 void f() { g(); }
21504 void g(int i = 3);
21505 };
21506
21507 */
21508 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21509 {
21510 decl = e->decl;
21511 /* If there are default arguments that have not yet been processed,
21512 take care of them now. */
21513 if (class_type != e->class_type)
21514 {
21515 if (pushed_scope)
21516 pop_scope (pushed_scope);
21517 class_type = e->class_type;
21518 pushed_scope = push_scope (class_type);
21519 }
21520 /* Make sure that any template parameters are in scope. */
21521 maybe_begin_member_template_processing (decl);
21522 /* Parse the default argument expressions. */
21523 cp_parser_late_parsing_default_args (parser, decl);
21524 /* Remove any template parameters from the symbol table. */
21525 maybe_end_member_template_processing ();
21526 }
21527 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21528 /* Now parse any NSDMIs. */
21529 save_ccp = current_class_ptr;
21530 save_ccr = current_class_ref;
21531 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21532 {
21533 if (class_type != DECL_CONTEXT (decl))
21534 {
21535 if (pushed_scope)
21536 pop_scope (pushed_scope);
21537 class_type = DECL_CONTEXT (decl);
21538 pushed_scope = push_scope (class_type);
21539 }
21540 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21541 cp_parser_late_parsing_nsdmi (parser, decl);
21542 }
21543 vec_safe_truncate (unparsed_nsdmis, 0);
21544 current_class_ptr = save_ccp;
21545 current_class_ref = save_ccr;
21546 if (pushed_scope)
21547 pop_scope (pushed_scope);
21548
21549 /* Now do some post-NSDMI bookkeeping. */
21550 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21551 after_nsdmi_defaulted_late_checks (class_type);
21552 vec_safe_truncate (unparsed_classes, 0);
21553 after_nsdmi_defaulted_late_checks (type);
21554
21555 /* Now parse the body of the functions. */
21556 if (flag_openmp)
21557 {
21558 /* OpenMP UDRs need to be parsed before all other functions. */
21559 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21560 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21561 cp_parser_late_parsing_for_member (parser, decl);
21562 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21563 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21564 cp_parser_late_parsing_for_member (parser, decl);
21565 }
21566 else
21567 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21568 cp_parser_late_parsing_for_member (parser, decl);
21569 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21570 }
21571 else
21572 vec_safe_push (unparsed_classes, type);
21573
21574 /* Put back any saved access checks. */
21575 pop_deferring_access_checks ();
21576
21577 /* Restore saved state. */
21578 parser->in_switch_statement_p = in_switch_statement_p;
21579 parser->in_statement = in_statement;
21580 parser->in_function_body = saved_in_function_body;
21581 parser->num_template_parameter_lists
21582 = saved_num_template_parameter_lists;
21583 parser->in_unbraced_linkage_specification_p
21584 = saved_in_unbraced_linkage_specification_p;
21585
21586 return type;
21587 }
21588
21589 static tree
21590 cp_parser_class_specifier (cp_parser* parser)
21591 {
21592 tree ret;
21593 timevar_push (TV_PARSE_STRUCT);
21594 ret = cp_parser_class_specifier_1 (parser);
21595 timevar_pop (TV_PARSE_STRUCT);
21596 return ret;
21597 }
21598
21599 /* Parse a class-head.
21600
21601 class-head:
21602 class-key identifier [opt] base-clause [opt]
21603 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21604 class-key nested-name-specifier [opt] template-id
21605 base-clause [opt]
21606
21607 class-virt-specifier:
21608 final
21609
21610 GNU Extensions:
21611 class-key attributes identifier [opt] base-clause [opt]
21612 class-key attributes nested-name-specifier identifier base-clause [opt]
21613 class-key attributes nested-name-specifier [opt] template-id
21614 base-clause [opt]
21615
21616 Upon return BASES is initialized to the list of base classes (or
21617 NULL, if there are none) in the same form returned by
21618 cp_parser_base_clause.
21619
21620 Returns the TYPE of the indicated class. Sets
21621 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21622 involving a nested-name-specifier was used, and FALSE otherwise.
21623
21624 Returns error_mark_node if this is not a class-head.
21625
21626 Returns NULL_TREE if the class-head is syntactically valid, but
21627 semantically invalid in a way that means we should skip the entire
21628 body of the class. */
21629
21630 static tree
21631 cp_parser_class_head (cp_parser* parser,
21632 bool* nested_name_specifier_p)
21633 {
21634 tree nested_name_specifier;
21635 enum tag_types class_key;
21636 tree id = NULL_TREE;
21637 tree type = NULL_TREE;
21638 tree attributes;
21639 tree bases;
21640 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21641 bool template_id_p = false;
21642 bool qualified_p = false;
21643 bool invalid_nested_name_p = false;
21644 bool invalid_explicit_specialization_p = false;
21645 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21646 tree pushed_scope = NULL_TREE;
21647 unsigned num_templates;
21648 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21649 /* Assume no nested-name-specifier will be present. */
21650 *nested_name_specifier_p = false;
21651 /* Assume no template parameter lists will be used in defining the
21652 type. */
21653 num_templates = 0;
21654 parser->colon_corrects_to_scope_p = false;
21655
21656 /* Look for the class-key. */
21657 class_key = cp_parser_class_key (parser);
21658 if (class_key == none_type)
21659 return error_mark_node;
21660
21661 location_t class_head_start_location = input_location;
21662
21663 /* Parse the attributes. */
21664 attributes = cp_parser_attributes_opt (parser);
21665
21666 /* If the next token is `::', that is invalid -- but sometimes
21667 people do try to write:
21668
21669 struct ::S {};
21670
21671 Handle this gracefully by accepting the extra qualifier, and then
21672 issuing an error about it later if this really is a
21673 class-head. If it turns out just to be an elaborated type
21674 specifier, remain silent. */
21675 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21676 qualified_p = true;
21677
21678 push_deferring_access_checks (dk_no_check);
21679
21680 /* Determine the name of the class. Begin by looking for an
21681 optional nested-name-specifier. */
21682 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21683 nested_name_specifier
21684 = cp_parser_nested_name_specifier_opt (parser,
21685 /*typename_keyword_p=*/false,
21686 /*check_dependency_p=*/false,
21687 /*type_p=*/true,
21688 /*is_declaration=*/false);
21689 /* If there was a nested-name-specifier, then there *must* be an
21690 identifier. */
21691 if (nested_name_specifier)
21692 {
21693 type_start_token = cp_lexer_peek_token (parser->lexer);
21694 /* Although the grammar says `identifier', it really means
21695 `class-name' or `template-name'. You are only allowed to
21696 define a class that has already been declared with this
21697 syntax.
21698
21699 The proposed resolution for Core Issue 180 says that wherever
21700 you see `class T::X' you should treat `X' as a type-name.
21701
21702 It is OK to define an inaccessible class; for example:
21703
21704 class A { class B; };
21705 class A::B {};
21706
21707 We do not know if we will see a class-name, or a
21708 template-name. We look for a class-name first, in case the
21709 class-name is a template-id; if we looked for the
21710 template-name first we would stop after the template-name. */
21711 cp_parser_parse_tentatively (parser);
21712 type = cp_parser_class_name (parser,
21713 /*typename_keyword_p=*/false,
21714 /*template_keyword_p=*/false,
21715 class_type,
21716 /*check_dependency_p=*/false,
21717 /*class_head_p=*/true,
21718 /*is_declaration=*/false);
21719 /* If that didn't work, ignore the nested-name-specifier. */
21720 if (!cp_parser_parse_definitely (parser))
21721 {
21722 invalid_nested_name_p = true;
21723 type_start_token = cp_lexer_peek_token (parser->lexer);
21724 id = cp_parser_identifier (parser);
21725 if (id == error_mark_node)
21726 id = NULL_TREE;
21727 }
21728 /* If we could not find a corresponding TYPE, treat this
21729 declaration like an unqualified declaration. */
21730 if (type == error_mark_node)
21731 nested_name_specifier = NULL_TREE;
21732 /* Otherwise, count the number of templates used in TYPE and its
21733 containing scopes. */
21734 else
21735 {
21736 tree scope;
21737
21738 for (scope = TREE_TYPE (type);
21739 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21740 scope = get_containing_scope (scope))
21741 if (TYPE_P (scope)
21742 && CLASS_TYPE_P (scope)
21743 && CLASSTYPE_TEMPLATE_INFO (scope)
21744 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21745 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21746 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21747 ++num_templates;
21748 }
21749 }
21750 /* Otherwise, the identifier is optional. */
21751 else
21752 {
21753 /* We don't know whether what comes next is a template-id,
21754 an identifier, or nothing at all. */
21755 cp_parser_parse_tentatively (parser);
21756 /* Check for a template-id. */
21757 type_start_token = cp_lexer_peek_token (parser->lexer);
21758 id = cp_parser_template_id (parser,
21759 /*template_keyword_p=*/false,
21760 /*check_dependency_p=*/true,
21761 class_key,
21762 /*is_declaration=*/true);
21763 /* If that didn't work, it could still be an identifier. */
21764 if (!cp_parser_parse_definitely (parser))
21765 {
21766 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21767 {
21768 type_start_token = cp_lexer_peek_token (parser->lexer);
21769 id = cp_parser_identifier (parser);
21770 }
21771 else
21772 id = NULL_TREE;
21773 }
21774 else
21775 {
21776 template_id_p = true;
21777 ++num_templates;
21778 }
21779 }
21780
21781 pop_deferring_access_checks ();
21782
21783 if (id)
21784 {
21785 cp_parser_check_for_invalid_template_id (parser, id,
21786 class_key,
21787 type_start_token->location);
21788 }
21789 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21790
21791 /* If it's not a `:' or a `{' then we can't really be looking at a
21792 class-head, since a class-head only appears as part of a
21793 class-specifier. We have to detect this situation before calling
21794 xref_tag, since that has irreversible side-effects. */
21795 if (!cp_parser_next_token_starts_class_definition_p (parser))
21796 {
21797 cp_parser_error (parser, "expected %<{%> or %<:%>");
21798 type = error_mark_node;
21799 goto out;
21800 }
21801
21802 /* At this point, we're going ahead with the class-specifier, even
21803 if some other problem occurs. */
21804 cp_parser_commit_to_tentative_parse (parser);
21805 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21806 {
21807 cp_parser_error (parser,
21808 "cannot specify %<override%> for a class");
21809 type = error_mark_node;
21810 goto out;
21811 }
21812 /* Issue the error about the overly-qualified name now. */
21813 if (qualified_p)
21814 {
21815 cp_parser_error (parser,
21816 "global qualification of class name is invalid");
21817 type = error_mark_node;
21818 goto out;
21819 }
21820 else if (invalid_nested_name_p)
21821 {
21822 cp_parser_error (parser,
21823 "qualified name does not name a class");
21824 type = error_mark_node;
21825 goto out;
21826 }
21827 else if (nested_name_specifier)
21828 {
21829 tree scope;
21830
21831 /* Reject typedef-names in class heads. */
21832 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21833 {
21834 error_at (type_start_token->location,
21835 "invalid class name in declaration of %qD",
21836 type);
21837 type = NULL_TREE;
21838 goto done;
21839 }
21840
21841 /* Figure out in what scope the declaration is being placed. */
21842 scope = current_scope ();
21843 /* If that scope does not contain the scope in which the
21844 class was originally declared, the program is invalid. */
21845 if (scope && !is_ancestor (scope, nested_name_specifier))
21846 {
21847 if (at_namespace_scope_p ())
21848 error_at (type_start_token->location,
21849 "declaration of %qD in namespace %qD which does not "
21850 "enclose %qD",
21851 type, scope, nested_name_specifier);
21852 else
21853 error_at (type_start_token->location,
21854 "declaration of %qD in %qD which does not enclose %qD",
21855 type, scope, nested_name_specifier);
21856 type = NULL_TREE;
21857 goto done;
21858 }
21859 /* [dcl.meaning]
21860
21861 A declarator-id shall not be qualified except for the
21862 definition of a ... nested class outside of its class
21863 ... [or] the definition or explicit instantiation of a
21864 class member of a namespace outside of its namespace. */
21865 if (scope == nested_name_specifier)
21866 {
21867 permerror (nested_name_specifier_token_start->location,
21868 "extra qualification not allowed");
21869 nested_name_specifier = NULL_TREE;
21870 num_templates = 0;
21871 }
21872 }
21873 /* An explicit-specialization must be preceded by "template <>". If
21874 it is not, try to recover gracefully. */
21875 if (at_namespace_scope_p ()
21876 && parser->num_template_parameter_lists == 0
21877 && template_id_p)
21878 {
21879 /* Build a location of this form:
21880 struct typename <ARGS>
21881 ^~~~~~~~~~~~~~~~~~~~~~
21882 with caret==start at the start token, and
21883 finishing at the end of the type. */
21884 location_t reported_loc
21885 = make_location (class_head_start_location,
21886 class_head_start_location,
21887 get_finish (type_start_token->location));
21888 rich_location richloc (line_table, reported_loc);
21889 richloc.add_fixit_insert (class_head_start_location, "template <> ");
21890 error_at_rich_loc
21891 (&richloc,
21892 "an explicit specialization must be preceded by %<template <>%>");
21893 invalid_explicit_specialization_p = true;
21894 /* Take the same action that would have been taken by
21895 cp_parser_explicit_specialization. */
21896 ++parser->num_template_parameter_lists;
21897 begin_specialization ();
21898 }
21899 /* There must be no "return" statements between this point and the
21900 end of this function; set "type "to the correct return value and
21901 use "goto done;" to return. */
21902 /* Make sure that the right number of template parameters were
21903 present. */
21904 if (!cp_parser_check_template_parameters (parser, num_templates,
21905 type_start_token->location,
21906 /*declarator=*/NULL))
21907 {
21908 /* If something went wrong, there is no point in even trying to
21909 process the class-definition. */
21910 type = NULL_TREE;
21911 goto done;
21912 }
21913
21914 /* Look up the type. */
21915 if (template_id_p)
21916 {
21917 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21918 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21919 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21920 {
21921 error_at (type_start_token->location,
21922 "function template %qD redeclared as a class template", id);
21923 type = error_mark_node;
21924 }
21925 else
21926 {
21927 type = TREE_TYPE (id);
21928 type = maybe_process_partial_specialization (type);
21929 }
21930 if (nested_name_specifier)
21931 pushed_scope = push_scope (nested_name_specifier);
21932 }
21933 else if (nested_name_specifier)
21934 {
21935 tree class_type;
21936
21937 /* Given:
21938
21939 template <typename T> struct S { struct T };
21940 template <typename T> struct S<T>::T { };
21941
21942 we will get a TYPENAME_TYPE when processing the definition of
21943 `S::T'. We need to resolve it to the actual type before we
21944 try to define it. */
21945 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21946 {
21947 class_type = resolve_typename_type (TREE_TYPE (type),
21948 /*only_current_p=*/false);
21949 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21950 type = TYPE_NAME (class_type);
21951 else
21952 {
21953 cp_parser_error (parser, "could not resolve typename type");
21954 type = error_mark_node;
21955 }
21956 }
21957
21958 if (maybe_process_partial_specialization (TREE_TYPE (type))
21959 == error_mark_node)
21960 {
21961 type = NULL_TREE;
21962 goto done;
21963 }
21964
21965 class_type = current_class_type;
21966 /* Enter the scope indicated by the nested-name-specifier. */
21967 pushed_scope = push_scope (nested_name_specifier);
21968 /* Get the canonical version of this type. */
21969 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21970 /* Call push_template_decl if it seems like we should be defining a
21971 template either from the template headers or the type we're
21972 defining, so that we diagnose both extra and missing headers. */
21973 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21974 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21975 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21976 {
21977 type = push_template_decl (type);
21978 if (type == error_mark_node)
21979 {
21980 type = NULL_TREE;
21981 goto done;
21982 }
21983 }
21984
21985 type = TREE_TYPE (type);
21986 *nested_name_specifier_p = true;
21987 }
21988 else /* The name is not a nested name. */
21989 {
21990 /* If the class was unnamed, create a dummy name. */
21991 if (!id)
21992 id = make_anon_name ();
21993 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21994 parser->num_template_parameter_lists);
21995 }
21996
21997 /* Indicate whether this class was declared as a `class' or as a
21998 `struct'. */
21999 if (TREE_CODE (type) == RECORD_TYPE)
22000 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22001 cp_parser_check_class_key (class_key, type);
22002
22003 /* If this type was already complete, and we see another definition,
22004 that's an error. */
22005 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22006 {
22007 error_at (type_start_token->location, "redefinition of %q#T",
22008 type);
22009 error_at (type_start_token->location, "previous definition of %q+#T",
22010 type);
22011 type = NULL_TREE;
22012 goto done;
22013 }
22014 else if (type == error_mark_node)
22015 type = NULL_TREE;
22016
22017 if (type)
22018 {
22019 /* Apply attributes now, before any use of the class as a template
22020 argument in its base list. */
22021 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22022 fixup_attribute_variants (type);
22023 }
22024
22025 /* We will have entered the scope containing the class; the names of
22026 base classes should be looked up in that context. For example:
22027
22028 struct A { struct B {}; struct C; };
22029 struct A::C : B {};
22030
22031 is valid. */
22032
22033 /* Get the list of base-classes, if there is one. */
22034 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22035 {
22036 /* PR59482: enter the class scope so that base-specifiers are looked
22037 up correctly. */
22038 if (type)
22039 pushclass (type);
22040 bases = cp_parser_base_clause (parser);
22041 /* PR59482: get out of the previously pushed class scope so that the
22042 subsequent pops pop the right thing. */
22043 if (type)
22044 popclass ();
22045 }
22046 else
22047 bases = NULL_TREE;
22048
22049 /* If we're really defining a class, process the base classes.
22050 If they're invalid, fail. */
22051 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22052 && !xref_basetypes (type, bases))
22053 type = NULL_TREE;
22054
22055 done:
22056 /* Leave the scope given by the nested-name-specifier. We will
22057 enter the class scope itself while processing the members. */
22058 if (pushed_scope)
22059 pop_scope (pushed_scope);
22060
22061 if (invalid_explicit_specialization_p)
22062 {
22063 end_specialization ();
22064 --parser->num_template_parameter_lists;
22065 }
22066
22067 if (type)
22068 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22069 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22070 CLASSTYPE_FINAL (type) = 1;
22071 out:
22072 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22073 return type;
22074 }
22075
22076 /* Parse a class-key.
22077
22078 class-key:
22079 class
22080 struct
22081 union
22082
22083 Returns the kind of class-key specified, or none_type to indicate
22084 error. */
22085
22086 static enum tag_types
22087 cp_parser_class_key (cp_parser* parser)
22088 {
22089 cp_token *token;
22090 enum tag_types tag_type;
22091
22092 /* Look for the class-key. */
22093 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22094 if (!token)
22095 return none_type;
22096
22097 /* Check to see if the TOKEN is a class-key. */
22098 tag_type = cp_parser_token_is_class_key (token);
22099 if (!tag_type)
22100 cp_parser_error (parser, "expected class-key");
22101 return tag_type;
22102 }
22103
22104 /* Parse a type-parameter-key.
22105
22106 type-parameter-key:
22107 class
22108 typename
22109 */
22110
22111 static void
22112 cp_parser_type_parameter_key (cp_parser* parser)
22113 {
22114 /* Look for the type-parameter-key. */
22115 enum tag_types tag_type = none_type;
22116 cp_token *token = cp_lexer_peek_token (parser->lexer);
22117 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22118 {
22119 cp_lexer_consume_token (parser->lexer);
22120 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22121 /* typename is not allowed in a template template parameter
22122 by the standard until C++1Z. */
22123 pedwarn (token->location, OPT_Wpedantic,
22124 "ISO C++ forbids typename key in template template parameter;"
22125 " use -std=c++1z or -std=gnu++1z");
22126 }
22127 else
22128 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22129
22130 return;
22131 }
22132
22133 /* Parse an (optional) member-specification.
22134
22135 member-specification:
22136 member-declaration member-specification [opt]
22137 access-specifier : member-specification [opt] */
22138
22139 static void
22140 cp_parser_member_specification_opt (cp_parser* parser)
22141 {
22142 while (true)
22143 {
22144 cp_token *token;
22145 enum rid keyword;
22146
22147 /* Peek at the next token. */
22148 token = cp_lexer_peek_token (parser->lexer);
22149 /* If it's a `}', or EOF then we've seen all the members. */
22150 if (token->type == CPP_CLOSE_BRACE
22151 || token->type == CPP_EOF
22152 || token->type == CPP_PRAGMA_EOL)
22153 break;
22154
22155 /* See if this token is a keyword. */
22156 keyword = token->keyword;
22157 switch (keyword)
22158 {
22159 case RID_PUBLIC:
22160 case RID_PROTECTED:
22161 case RID_PRIVATE:
22162 /* Consume the access-specifier. */
22163 cp_lexer_consume_token (parser->lexer);
22164 /* Remember which access-specifier is active. */
22165 current_access_specifier = token->u.value;
22166 /* Look for the `:'. */
22167 cp_parser_require (parser, CPP_COLON, RT_COLON);
22168 break;
22169
22170 default:
22171 /* Accept #pragmas at class scope. */
22172 if (token->type == CPP_PRAGMA)
22173 {
22174 cp_parser_pragma (parser, pragma_member, NULL);
22175 break;
22176 }
22177
22178 /* Otherwise, the next construction must be a
22179 member-declaration. */
22180 cp_parser_member_declaration (parser);
22181 }
22182 }
22183 }
22184
22185 /* Parse a member-declaration.
22186
22187 member-declaration:
22188 decl-specifier-seq [opt] member-declarator-list [opt] ;
22189 function-definition ; [opt]
22190 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22191 using-declaration
22192 template-declaration
22193 alias-declaration
22194
22195 member-declarator-list:
22196 member-declarator
22197 member-declarator-list , member-declarator
22198
22199 member-declarator:
22200 declarator pure-specifier [opt]
22201 declarator constant-initializer [opt]
22202 identifier [opt] : constant-expression
22203
22204 GNU Extensions:
22205
22206 member-declaration:
22207 __extension__ member-declaration
22208
22209 member-declarator:
22210 declarator attributes [opt] pure-specifier [opt]
22211 declarator attributes [opt] constant-initializer [opt]
22212 identifier [opt] attributes [opt] : constant-expression
22213
22214 C++0x Extensions:
22215
22216 member-declaration:
22217 static_assert-declaration */
22218
22219 static void
22220 cp_parser_member_declaration (cp_parser* parser)
22221 {
22222 cp_decl_specifier_seq decl_specifiers;
22223 tree prefix_attributes;
22224 tree decl;
22225 int declares_class_or_enum;
22226 bool friend_p;
22227 cp_token *token = NULL;
22228 cp_token *decl_spec_token_start = NULL;
22229 cp_token *initializer_token_start = NULL;
22230 int saved_pedantic;
22231 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22232
22233 /* Check for the `__extension__' keyword. */
22234 if (cp_parser_extension_opt (parser, &saved_pedantic))
22235 {
22236 /* Recurse. */
22237 cp_parser_member_declaration (parser);
22238 /* Restore the old value of the PEDANTIC flag. */
22239 pedantic = saved_pedantic;
22240
22241 return;
22242 }
22243
22244 /* Check for a template-declaration. */
22245 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22246 {
22247 /* An explicit specialization here is an error condition, and we
22248 expect the specialization handler to detect and report this. */
22249 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22250 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22251 cp_parser_explicit_specialization (parser);
22252 else
22253 cp_parser_template_declaration (parser, /*member_p=*/true);
22254
22255 return;
22256 }
22257 /* Check for a template introduction. */
22258 else if (cp_parser_template_declaration_after_export (parser, true))
22259 return;
22260
22261 /* Check for a using-declaration. */
22262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22263 {
22264 if (cxx_dialect < cxx11)
22265 {
22266 /* Parse the using-declaration. */
22267 cp_parser_using_declaration (parser,
22268 /*access_declaration_p=*/false);
22269 return;
22270 }
22271 else
22272 {
22273 tree decl;
22274 bool alias_decl_expected;
22275 cp_parser_parse_tentatively (parser);
22276 decl = cp_parser_alias_declaration (parser);
22277 /* Note that if we actually see the '=' token after the
22278 identifier, cp_parser_alias_declaration commits the
22279 tentative parse. In that case, we really expect an
22280 alias-declaration. Otherwise, we expect a using
22281 declaration. */
22282 alias_decl_expected =
22283 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22284 cp_parser_parse_definitely (parser);
22285
22286 if (alias_decl_expected)
22287 finish_member_declaration (decl);
22288 else
22289 cp_parser_using_declaration (parser,
22290 /*access_declaration_p=*/false);
22291 return;
22292 }
22293 }
22294
22295 /* Check for @defs. */
22296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22297 {
22298 tree ivar, member;
22299 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22300 ivar = ivar_chains;
22301 while (ivar)
22302 {
22303 member = ivar;
22304 ivar = TREE_CHAIN (member);
22305 TREE_CHAIN (member) = NULL_TREE;
22306 finish_member_declaration (member);
22307 }
22308 return;
22309 }
22310
22311 /* If the next token is `static_assert' we have a static assertion. */
22312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22313 {
22314 cp_parser_static_assert (parser, /*member_p=*/true);
22315 return;
22316 }
22317
22318 parser->colon_corrects_to_scope_p = false;
22319
22320 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22321 goto out;
22322
22323 /* Parse the decl-specifier-seq. */
22324 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22325 cp_parser_decl_specifier_seq (parser,
22326 CP_PARSER_FLAGS_OPTIONAL,
22327 &decl_specifiers,
22328 &declares_class_or_enum);
22329 /* Check for an invalid type-name. */
22330 if (!decl_specifiers.any_type_specifiers_p
22331 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22332 goto out;
22333 /* If there is no declarator, then the decl-specifier-seq should
22334 specify a type. */
22335 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22336 {
22337 /* If there was no decl-specifier-seq, and the next token is a
22338 `;', then we have something like:
22339
22340 struct S { ; };
22341
22342 [class.mem]
22343
22344 Each member-declaration shall declare at least one member
22345 name of the class. */
22346 if (!decl_specifiers.any_specifiers_p)
22347 {
22348 cp_token *token = cp_lexer_peek_token (parser->lexer);
22349 if (!in_system_header_at (token->location))
22350 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22351 }
22352 else
22353 {
22354 tree type;
22355
22356 /* See if this declaration is a friend. */
22357 friend_p = cp_parser_friend_p (&decl_specifiers);
22358 /* If there were decl-specifiers, check to see if there was
22359 a class-declaration. */
22360 type = check_tag_decl (&decl_specifiers,
22361 /*explicit_type_instantiation_p=*/false);
22362 /* Nested classes have already been added to the class, but
22363 a `friend' needs to be explicitly registered. */
22364 if (friend_p)
22365 {
22366 /* If the `friend' keyword was present, the friend must
22367 be introduced with a class-key. */
22368 if (!declares_class_or_enum && cxx_dialect < cxx11)
22369 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22370 "in C++03 a class-key must be used "
22371 "when declaring a friend");
22372 /* In this case:
22373
22374 template <typename T> struct A {
22375 friend struct A<T>::B;
22376 };
22377
22378 A<T>::B will be represented by a TYPENAME_TYPE, and
22379 therefore not recognized by check_tag_decl. */
22380 if (!type)
22381 {
22382 type = decl_specifiers.type;
22383 if (type && TREE_CODE (type) == TYPE_DECL)
22384 type = TREE_TYPE (type);
22385 }
22386 if (!type || !TYPE_P (type))
22387 error_at (decl_spec_token_start->location,
22388 "friend declaration does not name a class or "
22389 "function");
22390 else
22391 make_friend_class (current_class_type, type,
22392 /*complain=*/true);
22393 }
22394 /* If there is no TYPE, an error message will already have
22395 been issued. */
22396 else if (!type || type == error_mark_node)
22397 ;
22398 /* An anonymous aggregate has to be handled specially; such
22399 a declaration really declares a data member (with a
22400 particular type), as opposed to a nested class. */
22401 else if (ANON_AGGR_TYPE_P (type))
22402 {
22403 /* C++11 9.5/6. */
22404 if (decl_specifiers.storage_class != sc_none)
22405 error_at (decl_spec_token_start->location,
22406 "a storage class on an anonymous aggregate "
22407 "in class scope is not allowed");
22408
22409 /* Remove constructors and such from TYPE, now that we
22410 know it is an anonymous aggregate. */
22411 fixup_anonymous_aggr (type);
22412 /* And make the corresponding data member. */
22413 decl = build_decl (decl_spec_token_start->location,
22414 FIELD_DECL, NULL_TREE, type);
22415 /* Add it to the class. */
22416 finish_member_declaration (decl);
22417 }
22418 else
22419 cp_parser_check_access_in_redeclaration
22420 (TYPE_NAME (type),
22421 decl_spec_token_start->location);
22422 }
22423 }
22424 else
22425 {
22426 bool assume_semicolon = false;
22427
22428 /* Clear attributes from the decl_specifiers but keep them
22429 around as prefix attributes that apply them to the entity
22430 being declared. */
22431 prefix_attributes = decl_specifiers.attributes;
22432 decl_specifiers.attributes = NULL_TREE;
22433
22434 /* See if these declarations will be friends. */
22435 friend_p = cp_parser_friend_p (&decl_specifiers);
22436
22437 /* Keep going until we hit the `;' at the end of the
22438 declaration. */
22439 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22440 {
22441 tree attributes = NULL_TREE;
22442 tree first_attribute;
22443
22444 /* Peek at the next token. */
22445 token = cp_lexer_peek_token (parser->lexer);
22446
22447 /* Check for a bitfield declaration. */
22448 if (token->type == CPP_COLON
22449 || (token->type == CPP_NAME
22450 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22451 == CPP_COLON))
22452 {
22453 tree identifier;
22454 tree width;
22455
22456 /* Get the name of the bitfield. Note that we cannot just
22457 check TOKEN here because it may have been invalidated by
22458 the call to cp_lexer_peek_nth_token above. */
22459 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22460 identifier = cp_parser_identifier (parser);
22461 else
22462 identifier = NULL_TREE;
22463
22464 /* Consume the `:' token. */
22465 cp_lexer_consume_token (parser->lexer);
22466 /* Get the width of the bitfield. */
22467 width
22468 = cp_parser_constant_expression (parser);
22469
22470 /* Look for attributes that apply to the bitfield. */
22471 attributes = cp_parser_attributes_opt (parser);
22472 /* Remember which attributes are prefix attributes and
22473 which are not. */
22474 first_attribute = attributes;
22475 /* Combine the attributes. */
22476 attributes = chainon (prefix_attributes, attributes);
22477
22478 /* Create the bitfield declaration. */
22479 decl = grokbitfield (identifier
22480 ? make_id_declarator (NULL_TREE,
22481 identifier,
22482 sfk_none)
22483 : NULL,
22484 &decl_specifiers,
22485 width,
22486 attributes);
22487 }
22488 else
22489 {
22490 cp_declarator *declarator;
22491 tree initializer;
22492 tree asm_specification;
22493 int ctor_dtor_or_conv_p;
22494
22495 /* Parse the declarator. */
22496 declarator
22497 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22498 &ctor_dtor_or_conv_p,
22499 /*parenthesized_p=*/NULL,
22500 /*member_p=*/true,
22501 friend_p);
22502
22503 /* If something went wrong parsing the declarator, make sure
22504 that we at least consume some tokens. */
22505 if (declarator == cp_error_declarator)
22506 {
22507 /* Skip to the end of the statement. */
22508 cp_parser_skip_to_end_of_statement (parser);
22509 /* If the next token is not a semicolon, that is
22510 probably because we just skipped over the body of
22511 a function. So, we consume a semicolon if
22512 present, but do not issue an error message if it
22513 is not present. */
22514 if (cp_lexer_next_token_is (parser->lexer,
22515 CPP_SEMICOLON))
22516 cp_lexer_consume_token (parser->lexer);
22517 goto out;
22518 }
22519
22520 if (declares_class_or_enum & 2)
22521 cp_parser_check_for_definition_in_return_type
22522 (declarator, decl_specifiers.type,
22523 decl_specifiers.locations[ds_type_spec]);
22524
22525 /* Look for an asm-specification. */
22526 asm_specification = cp_parser_asm_specification_opt (parser);
22527 /* Look for attributes that apply to the declaration. */
22528 attributes = cp_parser_attributes_opt (parser);
22529 /* Remember which attributes are prefix attributes and
22530 which are not. */
22531 first_attribute = attributes;
22532 /* Combine the attributes. */
22533 attributes = chainon (prefix_attributes, attributes);
22534
22535 /* If it's an `=', then we have a constant-initializer or a
22536 pure-specifier. It is not correct to parse the
22537 initializer before registering the member declaration
22538 since the member declaration should be in scope while
22539 its initializer is processed. However, the rest of the
22540 front end does not yet provide an interface that allows
22541 us to handle this correctly. */
22542 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22543 {
22544 /* In [class.mem]:
22545
22546 A pure-specifier shall be used only in the declaration of
22547 a virtual function.
22548
22549 A member-declarator can contain a constant-initializer
22550 only if it declares a static member of integral or
22551 enumeration type.
22552
22553 Therefore, if the DECLARATOR is for a function, we look
22554 for a pure-specifier; otherwise, we look for a
22555 constant-initializer. When we call `grokfield', it will
22556 perform more stringent semantics checks. */
22557 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22558 if (function_declarator_p (declarator)
22559 || (decl_specifiers.type
22560 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22561 && declarator->kind == cdk_id
22562 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22563 == FUNCTION_TYPE)))
22564 initializer = cp_parser_pure_specifier (parser);
22565 else if (decl_specifiers.storage_class != sc_static)
22566 initializer = cp_parser_save_nsdmi (parser);
22567 else if (cxx_dialect >= cxx11)
22568 {
22569 bool nonconst;
22570 /* Don't require a constant rvalue in C++11, since we
22571 might want a reference constant. We'll enforce
22572 constancy later. */
22573 cp_lexer_consume_token (parser->lexer);
22574 /* Parse the initializer. */
22575 initializer = cp_parser_initializer_clause (parser,
22576 &nonconst);
22577 }
22578 else
22579 /* Parse the initializer. */
22580 initializer = cp_parser_constant_initializer (parser);
22581 }
22582 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22583 && !function_declarator_p (declarator))
22584 {
22585 bool x;
22586 if (decl_specifiers.storage_class != sc_static)
22587 initializer = cp_parser_save_nsdmi (parser);
22588 else
22589 initializer = cp_parser_initializer (parser, &x, &x);
22590 }
22591 /* Otherwise, there is no initializer. */
22592 else
22593 initializer = NULL_TREE;
22594
22595 /* See if we are probably looking at a function
22596 definition. We are certainly not looking at a
22597 member-declarator. Calling `grokfield' has
22598 side-effects, so we must not do it unless we are sure
22599 that we are looking at a member-declarator. */
22600 if (cp_parser_token_starts_function_definition_p
22601 (cp_lexer_peek_token (parser->lexer)))
22602 {
22603 /* The grammar does not allow a pure-specifier to be
22604 used when a member function is defined. (It is
22605 possible that this fact is an oversight in the
22606 standard, since a pure function may be defined
22607 outside of the class-specifier. */
22608 if (initializer && initializer_token_start)
22609 error_at (initializer_token_start->location,
22610 "pure-specifier on function-definition");
22611 decl = cp_parser_save_member_function_body (parser,
22612 &decl_specifiers,
22613 declarator,
22614 attributes);
22615 if (parser->fully_implicit_function_template_p)
22616 decl = finish_fully_implicit_template (parser, decl);
22617 /* If the member was not a friend, declare it here. */
22618 if (!friend_p)
22619 finish_member_declaration (decl);
22620 /* Peek at the next token. */
22621 token = cp_lexer_peek_token (parser->lexer);
22622 /* If the next token is a semicolon, consume it. */
22623 if (token->type == CPP_SEMICOLON)
22624 cp_lexer_consume_token (parser->lexer);
22625 goto out;
22626 }
22627 else
22628 if (declarator->kind == cdk_function)
22629 declarator->id_loc = token->location;
22630 /* Create the declaration. */
22631 decl = grokfield (declarator, &decl_specifiers,
22632 initializer, /*init_const_expr_p=*/true,
22633 asm_specification, attributes);
22634 if (parser->fully_implicit_function_template_p)
22635 {
22636 if (friend_p)
22637 finish_fully_implicit_template (parser, 0);
22638 else
22639 decl = finish_fully_implicit_template (parser, decl);
22640 }
22641 }
22642
22643 cp_finalize_omp_declare_simd (parser, decl);
22644 cp_finalize_oacc_routine (parser, decl, false);
22645
22646 /* Reset PREFIX_ATTRIBUTES. */
22647 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22648 attributes = TREE_CHAIN (attributes);
22649 if (attributes)
22650 TREE_CHAIN (attributes) = NULL_TREE;
22651
22652 /* If there is any qualification still in effect, clear it
22653 now; we will be starting fresh with the next declarator. */
22654 parser->scope = NULL_TREE;
22655 parser->qualifying_scope = NULL_TREE;
22656 parser->object_scope = NULL_TREE;
22657 /* If it's a `,', then there are more declarators. */
22658 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22659 {
22660 cp_lexer_consume_token (parser->lexer);
22661 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22662 {
22663 cp_token *token = cp_lexer_previous_token (parser->lexer);
22664 error_at (token->location,
22665 "stray %<,%> at end of member declaration");
22666 }
22667 }
22668 /* If the next token isn't a `;', then we have a parse error. */
22669 else if (cp_lexer_next_token_is_not (parser->lexer,
22670 CPP_SEMICOLON))
22671 {
22672 /* The next token might be a ways away from where the
22673 actual semicolon is missing. Find the previous token
22674 and use that for our error position. */
22675 cp_token *token = cp_lexer_previous_token (parser->lexer);
22676 error_at (token->location,
22677 "expected %<;%> at end of member declaration");
22678
22679 /* Assume that the user meant to provide a semicolon. If
22680 we were to cp_parser_skip_to_end_of_statement, we might
22681 skip to a semicolon inside a member function definition
22682 and issue nonsensical error messages. */
22683 assume_semicolon = true;
22684 }
22685
22686 if (decl)
22687 {
22688 /* Add DECL to the list of members. */
22689 if (!friend_p
22690 /* Explicitly include, eg, NSDMIs, for better error
22691 recovery (c++/58650). */
22692 || !DECL_DECLARES_FUNCTION_P (decl))
22693 finish_member_declaration (decl);
22694
22695 if (TREE_CODE (decl) == FUNCTION_DECL)
22696 cp_parser_save_default_args (parser, decl);
22697 else if (TREE_CODE (decl) == FIELD_DECL
22698 && !DECL_C_BIT_FIELD (decl)
22699 && DECL_INITIAL (decl))
22700 /* Add DECL to the queue of NSDMI to be parsed later. */
22701 vec_safe_push (unparsed_nsdmis, decl);
22702 }
22703
22704 if (assume_semicolon)
22705 goto out;
22706 }
22707 }
22708
22709 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22710 out:
22711 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22712 }
22713
22714 /* Parse a pure-specifier.
22715
22716 pure-specifier:
22717 = 0
22718
22719 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22720 Otherwise, ERROR_MARK_NODE is returned. */
22721
22722 static tree
22723 cp_parser_pure_specifier (cp_parser* parser)
22724 {
22725 cp_token *token;
22726
22727 /* Look for the `=' token. */
22728 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22729 return error_mark_node;
22730 /* Look for the `0' token. */
22731 token = cp_lexer_peek_token (parser->lexer);
22732
22733 if (token->type == CPP_EOF
22734 || token->type == CPP_PRAGMA_EOL)
22735 return error_mark_node;
22736
22737 cp_lexer_consume_token (parser->lexer);
22738
22739 /* Accept = default or = delete in c++0x mode. */
22740 if (token->keyword == RID_DEFAULT
22741 || token->keyword == RID_DELETE)
22742 {
22743 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22744 return token->u.value;
22745 }
22746
22747 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22748 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22749 {
22750 cp_parser_error (parser,
22751 "invalid pure specifier (only %<= 0%> is allowed)");
22752 cp_parser_skip_to_end_of_statement (parser);
22753 return error_mark_node;
22754 }
22755 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22756 {
22757 error_at (token->location, "templates may not be %<virtual%>");
22758 return error_mark_node;
22759 }
22760
22761 return integer_zero_node;
22762 }
22763
22764 /* Parse a constant-initializer.
22765
22766 constant-initializer:
22767 = constant-expression
22768
22769 Returns a representation of the constant-expression. */
22770
22771 static tree
22772 cp_parser_constant_initializer (cp_parser* parser)
22773 {
22774 /* Look for the `=' token. */
22775 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22776 return error_mark_node;
22777
22778 /* It is invalid to write:
22779
22780 struct S { static const int i = { 7 }; };
22781
22782 */
22783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22784 {
22785 cp_parser_error (parser,
22786 "a brace-enclosed initializer is not allowed here");
22787 /* Consume the opening brace. */
22788 cp_lexer_consume_token (parser->lexer);
22789 /* Skip the initializer. */
22790 cp_parser_skip_to_closing_brace (parser);
22791 /* Look for the trailing `}'. */
22792 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22793
22794 return error_mark_node;
22795 }
22796
22797 return cp_parser_constant_expression (parser);
22798 }
22799
22800 /* Derived classes [gram.class.derived] */
22801
22802 /* Parse a base-clause.
22803
22804 base-clause:
22805 : base-specifier-list
22806
22807 base-specifier-list:
22808 base-specifier ... [opt]
22809 base-specifier-list , base-specifier ... [opt]
22810
22811 Returns a TREE_LIST representing the base-classes, in the order in
22812 which they were declared. The representation of each node is as
22813 described by cp_parser_base_specifier.
22814
22815 In the case that no bases are specified, this function will return
22816 NULL_TREE, not ERROR_MARK_NODE. */
22817
22818 static tree
22819 cp_parser_base_clause (cp_parser* parser)
22820 {
22821 tree bases = NULL_TREE;
22822
22823 /* Look for the `:' that begins the list. */
22824 cp_parser_require (parser, CPP_COLON, RT_COLON);
22825
22826 /* Scan the base-specifier-list. */
22827 while (true)
22828 {
22829 cp_token *token;
22830 tree base;
22831 bool pack_expansion_p = false;
22832
22833 /* Look for the base-specifier. */
22834 base = cp_parser_base_specifier (parser);
22835 /* Look for the (optional) ellipsis. */
22836 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22837 {
22838 /* Consume the `...'. */
22839 cp_lexer_consume_token (parser->lexer);
22840
22841 pack_expansion_p = true;
22842 }
22843
22844 /* Add BASE to the front of the list. */
22845 if (base && base != error_mark_node)
22846 {
22847 if (pack_expansion_p)
22848 /* Make this a pack expansion type. */
22849 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22850
22851 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22852 {
22853 TREE_CHAIN (base) = bases;
22854 bases = base;
22855 }
22856 }
22857 /* Peek at the next token. */
22858 token = cp_lexer_peek_token (parser->lexer);
22859 /* If it's not a comma, then the list is complete. */
22860 if (token->type != CPP_COMMA)
22861 break;
22862 /* Consume the `,'. */
22863 cp_lexer_consume_token (parser->lexer);
22864 }
22865
22866 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22867 base class had a qualified name. However, the next name that
22868 appears is certainly not qualified. */
22869 parser->scope = NULL_TREE;
22870 parser->qualifying_scope = NULL_TREE;
22871 parser->object_scope = NULL_TREE;
22872
22873 return nreverse (bases);
22874 }
22875
22876 /* Parse a base-specifier.
22877
22878 base-specifier:
22879 :: [opt] nested-name-specifier [opt] class-name
22880 virtual access-specifier [opt] :: [opt] nested-name-specifier
22881 [opt] class-name
22882 access-specifier virtual [opt] :: [opt] nested-name-specifier
22883 [opt] class-name
22884
22885 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22886 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22887 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22888 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22889
22890 static tree
22891 cp_parser_base_specifier (cp_parser* parser)
22892 {
22893 cp_token *token;
22894 bool done = false;
22895 bool virtual_p = false;
22896 bool duplicate_virtual_error_issued_p = false;
22897 bool duplicate_access_error_issued_p = false;
22898 bool class_scope_p, template_p;
22899 tree access = access_default_node;
22900 tree type;
22901
22902 /* Process the optional `virtual' and `access-specifier'. */
22903 while (!done)
22904 {
22905 /* Peek at the next token. */
22906 token = cp_lexer_peek_token (parser->lexer);
22907 /* Process `virtual'. */
22908 switch (token->keyword)
22909 {
22910 case RID_VIRTUAL:
22911 /* If `virtual' appears more than once, issue an error. */
22912 if (virtual_p && !duplicate_virtual_error_issued_p)
22913 {
22914 cp_parser_error (parser,
22915 "%<virtual%> specified more than once in base-specified");
22916 duplicate_virtual_error_issued_p = true;
22917 }
22918
22919 virtual_p = true;
22920
22921 /* Consume the `virtual' token. */
22922 cp_lexer_consume_token (parser->lexer);
22923
22924 break;
22925
22926 case RID_PUBLIC:
22927 case RID_PROTECTED:
22928 case RID_PRIVATE:
22929 /* If more than one access specifier appears, issue an
22930 error. */
22931 if (access != access_default_node
22932 && !duplicate_access_error_issued_p)
22933 {
22934 cp_parser_error (parser,
22935 "more than one access specifier in base-specified");
22936 duplicate_access_error_issued_p = true;
22937 }
22938
22939 access = ridpointers[(int) token->keyword];
22940
22941 /* Consume the access-specifier. */
22942 cp_lexer_consume_token (parser->lexer);
22943
22944 break;
22945
22946 default:
22947 done = true;
22948 break;
22949 }
22950 }
22951 /* It is not uncommon to see programs mechanically, erroneously, use
22952 the 'typename' keyword to denote (dependent) qualified types
22953 as base classes. */
22954 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22955 {
22956 token = cp_lexer_peek_token (parser->lexer);
22957 if (!processing_template_decl)
22958 error_at (token->location,
22959 "keyword %<typename%> not allowed outside of templates");
22960 else
22961 error_at (token->location,
22962 "keyword %<typename%> not allowed in this context "
22963 "(the base class is implicitly a type)");
22964 cp_lexer_consume_token (parser->lexer);
22965 }
22966
22967 /* Look for the optional `::' operator. */
22968 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22969 /* Look for the nested-name-specifier. The simplest way to
22970 implement:
22971
22972 [temp.res]
22973
22974 The keyword `typename' is not permitted in a base-specifier or
22975 mem-initializer; in these contexts a qualified name that
22976 depends on a template-parameter is implicitly assumed to be a
22977 type name.
22978
22979 is to pretend that we have seen the `typename' keyword at this
22980 point. */
22981 cp_parser_nested_name_specifier_opt (parser,
22982 /*typename_keyword_p=*/true,
22983 /*check_dependency_p=*/true,
22984 typename_type,
22985 /*is_declaration=*/true);
22986 /* If the base class is given by a qualified name, assume that names
22987 we see are type names or templates, as appropriate. */
22988 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22989 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22990
22991 if (!parser->scope
22992 && cp_lexer_next_token_is_decltype (parser->lexer))
22993 /* DR 950 allows decltype as a base-specifier. */
22994 type = cp_parser_decltype (parser);
22995 else
22996 {
22997 /* Otherwise, look for the class-name. */
22998 type = cp_parser_class_name (parser,
22999 class_scope_p,
23000 template_p,
23001 typename_type,
23002 /*check_dependency_p=*/true,
23003 /*class_head_p=*/false,
23004 /*is_declaration=*/true);
23005 type = TREE_TYPE (type);
23006 }
23007
23008 if (type == error_mark_node)
23009 return error_mark_node;
23010
23011 return finish_base_specifier (type, access, virtual_p);
23012 }
23013
23014 /* Exception handling [gram.exception] */
23015
23016 /* Parse an (optional) noexcept-specification.
23017
23018 noexcept-specification:
23019 noexcept ( constant-expression ) [opt]
23020
23021 If no noexcept-specification is present, returns NULL_TREE.
23022 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23023 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23024 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23025 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23026 in which case a boolean condition is returned instead. */
23027
23028 static tree
23029 cp_parser_noexcept_specification_opt (cp_parser* parser,
23030 bool require_constexpr,
23031 bool* consumed_expr,
23032 bool return_cond)
23033 {
23034 cp_token *token;
23035 const char *saved_message;
23036
23037 /* Peek at the next token. */
23038 token = cp_lexer_peek_token (parser->lexer);
23039
23040 /* Is it a noexcept-specification? */
23041 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23042 {
23043 tree expr;
23044 cp_lexer_consume_token (parser->lexer);
23045
23046 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23047 {
23048 cp_lexer_consume_token (parser->lexer);
23049
23050 if (require_constexpr)
23051 {
23052 /* Types may not be defined in an exception-specification. */
23053 saved_message = parser->type_definition_forbidden_message;
23054 parser->type_definition_forbidden_message
23055 = G_("types may not be defined in an exception-specification");
23056
23057 expr = cp_parser_constant_expression (parser);
23058
23059 /* Restore the saved message. */
23060 parser->type_definition_forbidden_message = saved_message;
23061 }
23062 else
23063 {
23064 expr = cp_parser_expression (parser);
23065 *consumed_expr = true;
23066 }
23067
23068 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23069 }
23070 else
23071 {
23072 expr = boolean_true_node;
23073 if (!require_constexpr)
23074 *consumed_expr = false;
23075 }
23076
23077 /* We cannot build a noexcept-spec right away because this will check
23078 that expr is a constexpr. */
23079 if (!return_cond)
23080 return build_noexcept_spec (expr, tf_warning_or_error);
23081 else
23082 return expr;
23083 }
23084 else
23085 return NULL_TREE;
23086 }
23087
23088 /* Parse an (optional) exception-specification.
23089
23090 exception-specification:
23091 throw ( type-id-list [opt] )
23092
23093 Returns a TREE_LIST representing the exception-specification. The
23094 TREE_VALUE of each node is a type. */
23095
23096 static tree
23097 cp_parser_exception_specification_opt (cp_parser* parser)
23098 {
23099 cp_token *token;
23100 tree type_id_list;
23101 const char *saved_message;
23102
23103 /* Peek at the next token. */
23104 token = cp_lexer_peek_token (parser->lexer);
23105
23106 /* Is it a noexcept-specification? */
23107 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23108 false);
23109 if (type_id_list != NULL_TREE)
23110 return type_id_list;
23111
23112 /* If it's not `throw', then there's no exception-specification. */
23113 if (!cp_parser_is_keyword (token, RID_THROW))
23114 return NULL_TREE;
23115
23116 #if 0
23117 /* Enable this once a lot of code has transitioned to noexcept? */
23118 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23119 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23120 "deprecated in C++0x; use %<noexcept%> instead");
23121 #endif
23122
23123 /* Consume the `throw'. */
23124 cp_lexer_consume_token (parser->lexer);
23125
23126 /* Look for the `('. */
23127 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23128
23129 /* Peek at the next token. */
23130 token = cp_lexer_peek_token (parser->lexer);
23131 /* If it's not a `)', then there is a type-id-list. */
23132 if (token->type != CPP_CLOSE_PAREN)
23133 {
23134 /* Types may not be defined in an exception-specification. */
23135 saved_message = parser->type_definition_forbidden_message;
23136 parser->type_definition_forbidden_message
23137 = G_("types may not be defined in an exception-specification");
23138 /* Parse the type-id-list. */
23139 type_id_list = cp_parser_type_id_list (parser);
23140 /* Restore the saved message. */
23141 parser->type_definition_forbidden_message = saved_message;
23142 }
23143 else
23144 type_id_list = empty_except_spec;
23145
23146 /* Look for the `)'. */
23147 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23148
23149 return type_id_list;
23150 }
23151
23152 /* Parse an (optional) type-id-list.
23153
23154 type-id-list:
23155 type-id ... [opt]
23156 type-id-list , type-id ... [opt]
23157
23158 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23159 in the order that the types were presented. */
23160
23161 static tree
23162 cp_parser_type_id_list (cp_parser* parser)
23163 {
23164 tree types = NULL_TREE;
23165
23166 while (true)
23167 {
23168 cp_token *token;
23169 tree type;
23170
23171 token = cp_lexer_peek_token (parser->lexer);
23172
23173 /* Get the next type-id. */
23174 type = cp_parser_type_id (parser);
23175 /* Check for invalid 'auto'. */
23176 if (flag_concepts && type_uses_auto (type))
23177 {
23178 error_at (token->location,
23179 "invalid use of %<auto%> in exception-specification");
23180 type = error_mark_node;
23181 }
23182 /* Parse the optional ellipsis. */
23183 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23184 {
23185 /* Consume the `...'. */
23186 cp_lexer_consume_token (parser->lexer);
23187
23188 /* Turn the type into a pack expansion expression. */
23189 type = make_pack_expansion (type);
23190 }
23191 /* Add it to the list. */
23192 types = add_exception_specifier (types, type, /*complain=*/1);
23193 /* Peek at the next token. */
23194 token = cp_lexer_peek_token (parser->lexer);
23195 /* If it is not a `,', we are done. */
23196 if (token->type != CPP_COMMA)
23197 break;
23198 /* Consume the `,'. */
23199 cp_lexer_consume_token (parser->lexer);
23200 }
23201
23202 return nreverse (types);
23203 }
23204
23205 /* Parse a try-block.
23206
23207 try-block:
23208 try compound-statement handler-seq */
23209
23210 static tree
23211 cp_parser_try_block (cp_parser* parser)
23212 {
23213 tree try_block;
23214
23215 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23216 if (parser->in_function_body
23217 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23218 error ("%<try%> in %<constexpr%> function");
23219
23220 try_block = begin_try_block ();
23221 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23222 finish_try_block (try_block);
23223 cp_parser_handler_seq (parser);
23224 finish_handler_sequence (try_block);
23225
23226 return try_block;
23227 }
23228
23229 /* Parse a function-try-block.
23230
23231 function-try-block:
23232 try ctor-initializer [opt] function-body handler-seq */
23233
23234 static bool
23235 cp_parser_function_try_block (cp_parser* parser)
23236 {
23237 tree compound_stmt;
23238 tree try_block;
23239 bool ctor_initializer_p;
23240
23241 /* Look for the `try' keyword. */
23242 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23243 return false;
23244 /* Let the rest of the front end know where we are. */
23245 try_block = begin_function_try_block (&compound_stmt);
23246 /* Parse the function-body. */
23247 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23248 (parser, /*in_function_try_block=*/true);
23249 /* We're done with the `try' part. */
23250 finish_function_try_block (try_block);
23251 /* Parse the handlers. */
23252 cp_parser_handler_seq (parser);
23253 /* We're done with the handlers. */
23254 finish_function_handler_sequence (try_block, compound_stmt);
23255
23256 return ctor_initializer_p;
23257 }
23258
23259 /* Parse a handler-seq.
23260
23261 handler-seq:
23262 handler handler-seq [opt] */
23263
23264 static void
23265 cp_parser_handler_seq (cp_parser* parser)
23266 {
23267 while (true)
23268 {
23269 cp_token *token;
23270
23271 /* Parse the handler. */
23272 cp_parser_handler (parser);
23273 /* Peek at the next token. */
23274 token = cp_lexer_peek_token (parser->lexer);
23275 /* If it's not `catch' then there are no more handlers. */
23276 if (!cp_parser_is_keyword (token, RID_CATCH))
23277 break;
23278 }
23279 }
23280
23281 /* Parse a handler.
23282
23283 handler:
23284 catch ( exception-declaration ) compound-statement */
23285
23286 static void
23287 cp_parser_handler (cp_parser* parser)
23288 {
23289 tree handler;
23290 tree declaration;
23291
23292 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23293 handler = begin_handler ();
23294 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23295 declaration = cp_parser_exception_declaration (parser);
23296 finish_handler_parms (declaration, handler);
23297 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23298 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23299 finish_handler (handler);
23300 }
23301
23302 /* Parse an exception-declaration.
23303
23304 exception-declaration:
23305 type-specifier-seq declarator
23306 type-specifier-seq abstract-declarator
23307 type-specifier-seq
23308 ...
23309
23310 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23311 ellipsis variant is used. */
23312
23313 static tree
23314 cp_parser_exception_declaration (cp_parser* parser)
23315 {
23316 cp_decl_specifier_seq type_specifiers;
23317 cp_declarator *declarator;
23318 const char *saved_message;
23319
23320 /* If it's an ellipsis, it's easy to handle. */
23321 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23322 {
23323 /* Consume the `...' token. */
23324 cp_lexer_consume_token (parser->lexer);
23325 return NULL_TREE;
23326 }
23327
23328 /* Types may not be defined in exception-declarations. */
23329 saved_message = parser->type_definition_forbidden_message;
23330 parser->type_definition_forbidden_message
23331 = G_("types may not be defined in exception-declarations");
23332
23333 /* Parse the type-specifier-seq. */
23334 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23335 /*is_trailing_return=*/false,
23336 &type_specifiers);
23337 /* If it's a `)', then there is no declarator. */
23338 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23339 declarator = NULL;
23340 else
23341 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23342 /*ctor_dtor_or_conv_p=*/NULL,
23343 /*parenthesized_p=*/NULL,
23344 /*member_p=*/false,
23345 /*friend_p=*/false);
23346
23347 /* Restore the saved message. */
23348 parser->type_definition_forbidden_message = saved_message;
23349
23350 if (!type_specifiers.any_specifiers_p)
23351 return error_mark_node;
23352
23353 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23354 }
23355
23356 /* Parse a throw-expression.
23357
23358 throw-expression:
23359 throw assignment-expression [opt]
23360
23361 Returns a THROW_EXPR representing the throw-expression. */
23362
23363 static tree
23364 cp_parser_throw_expression (cp_parser* parser)
23365 {
23366 tree expression;
23367 cp_token* token;
23368
23369 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23370 token = cp_lexer_peek_token (parser->lexer);
23371 /* Figure out whether or not there is an assignment-expression
23372 following the "throw" keyword. */
23373 if (token->type == CPP_COMMA
23374 || token->type == CPP_SEMICOLON
23375 || token->type == CPP_CLOSE_PAREN
23376 || token->type == CPP_CLOSE_SQUARE
23377 || token->type == CPP_CLOSE_BRACE
23378 || token->type == CPP_COLON)
23379 expression = NULL_TREE;
23380 else
23381 expression = cp_parser_assignment_expression (parser);
23382
23383 return build_throw (expression);
23384 }
23385
23386 /* GNU Extensions */
23387
23388 /* Parse an (optional) asm-specification.
23389
23390 asm-specification:
23391 asm ( string-literal )
23392
23393 If the asm-specification is present, returns a STRING_CST
23394 corresponding to the string-literal. Otherwise, returns
23395 NULL_TREE. */
23396
23397 static tree
23398 cp_parser_asm_specification_opt (cp_parser* parser)
23399 {
23400 cp_token *token;
23401 tree asm_specification;
23402
23403 /* Peek at the next token. */
23404 token = cp_lexer_peek_token (parser->lexer);
23405 /* If the next token isn't the `asm' keyword, then there's no
23406 asm-specification. */
23407 if (!cp_parser_is_keyword (token, RID_ASM))
23408 return NULL_TREE;
23409
23410 /* Consume the `asm' token. */
23411 cp_lexer_consume_token (parser->lexer);
23412 /* Look for the `('. */
23413 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23414
23415 /* Look for the string-literal. */
23416 asm_specification = cp_parser_string_literal (parser, false, false);
23417
23418 /* Look for the `)'. */
23419 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23420
23421 return asm_specification;
23422 }
23423
23424 /* Parse an asm-operand-list.
23425
23426 asm-operand-list:
23427 asm-operand
23428 asm-operand-list , asm-operand
23429
23430 asm-operand:
23431 string-literal ( expression )
23432 [ string-literal ] string-literal ( expression )
23433
23434 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23435 each node is the expression. The TREE_PURPOSE is itself a
23436 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23437 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23438 is a STRING_CST for the string literal before the parenthesis. Returns
23439 ERROR_MARK_NODE if any of the operands are invalid. */
23440
23441 static tree
23442 cp_parser_asm_operand_list (cp_parser* parser)
23443 {
23444 tree asm_operands = NULL_TREE;
23445 bool invalid_operands = false;
23446
23447 while (true)
23448 {
23449 tree string_literal;
23450 tree expression;
23451 tree name;
23452
23453 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23454 {
23455 /* Consume the `[' token. */
23456 cp_lexer_consume_token (parser->lexer);
23457 /* Read the operand name. */
23458 name = cp_parser_identifier (parser);
23459 if (name != error_mark_node)
23460 name = build_string (IDENTIFIER_LENGTH (name),
23461 IDENTIFIER_POINTER (name));
23462 /* Look for the closing `]'. */
23463 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23464 }
23465 else
23466 name = NULL_TREE;
23467 /* Look for the string-literal. */
23468 string_literal = cp_parser_string_literal (parser, false, false);
23469
23470 /* Look for the `('. */
23471 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23472 /* Parse the expression. */
23473 expression = cp_parser_expression (parser);
23474 /* Look for the `)'. */
23475 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23476
23477 if (name == error_mark_node
23478 || string_literal == error_mark_node
23479 || expression == error_mark_node)
23480 invalid_operands = true;
23481
23482 /* Add this operand to the list. */
23483 asm_operands = tree_cons (build_tree_list (name, string_literal),
23484 expression,
23485 asm_operands);
23486 /* If the next token is not a `,', there are no more
23487 operands. */
23488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23489 break;
23490 /* Consume the `,'. */
23491 cp_lexer_consume_token (parser->lexer);
23492 }
23493
23494 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23495 }
23496
23497 /* Parse an asm-clobber-list.
23498
23499 asm-clobber-list:
23500 string-literal
23501 asm-clobber-list , string-literal
23502
23503 Returns a TREE_LIST, indicating the clobbers in the order that they
23504 appeared. The TREE_VALUE of each node is a STRING_CST. */
23505
23506 static tree
23507 cp_parser_asm_clobber_list (cp_parser* parser)
23508 {
23509 tree clobbers = NULL_TREE;
23510
23511 while (true)
23512 {
23513 tree string_literal;
23514
23515 /* Look for the string literal. */
23516 string_literal = cp_parser_string_literal (parser, false, false);
23517 /* Add it to the list. */
23518 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23519 /* If the next token is not a `,', then the list is
23520 complete. */
23521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23522 break;
23523 /* Consume the `,' token. */
23524 cp_lexer_consume_token (parser->lexer);
23525 }
23526
23527 return clobbers;
23528 }
23529
23530 /* Parse an asm-label-list.
23531
23532 asm-label-list:
23533 identifier
23534 asm-label-list , identifier
23535
23536 Returns a TREE_LIST, indicating the labels in the order that they
23537 appeared. The TREE_VALUE of each node is a label. */
23538
23539 static tree
23540 cp_parser_asm_label_list (cp_parser* parser)
23541 {
23542 tree labels = NULL_TREE;
23543
23544 while (true)
23545 {
23546 tree identifier, label, name;
23547
23548 /* Look for the identifier. */
23549 identifier = cp_parser_identifier (parser);
23550 if (!error_operand_p (identifier))
23551 {
23552 label = lookup_label (identifier);
23553 if (TREE_CODE (label) == LABEL_DECL)
23554 {
23555 TREE_USED (label) = 1;
23556 check_goto (label);
23557 name = build_string (IDENTIFIER_LENGTH (identifier),
23558 IDENTIFIER_POINTER (identifier));
23559 labels = tree_cons (name, label, labels);
23560 }
23561 }
23562 /* If the next token is not a `,', then the list is
23563 complete. */
23564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23565 break;
23566 /* Consume the `,' token. */
23567 cp_lexer_consume_token (parser->lexer);
23568 }
23569
23570 return nreverse (labels);
23571 }
23572
23573 /* Return TRUE iff the next tokens in the stream are possibly the
23574 beginning of a GNU extension attribute. */
23575
23576 static bool
23577 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23578 {
23579 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23580 }
23581
23582 /* Return TRUE iff the next tokens in the stream are possibly the
23583 beginning of a standard C++-11 attribute specifier. */
23584
23585 static bool
23586 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23587 {
23588 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23589 }
23590
23591 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23592 beginning of a standard C++-11 attribute specifier. */
23593
23594 static bool
23595 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23596 {
23597 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23598
23599 return (cxx_dialect >= cxx11
23600 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23601 || (token->type == CPP_OPEN_SQUARE
23602 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23603 && token->type == CPP_OPEN_SQUARE)));
23604 }
23605
23606 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23607 beginning of a GNU extension attribute. */
23608
23609 static bool
23610 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23611 {
23612 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23613
23614 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23615 }
23616
23617 /* Return true iff the next tokens can be the beginning of either a
23618 GNU attribute list, or a standard C++11 attribute sequence. */
23619
23620 static bool
23621 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23622 {
23623 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23624 || cp_next_tokens_can_be_std_attribute_p (parser));
23625 }
23626
23627 /* Return true iff the next Nth tokens can be the beginning of either
23628 a GNU attribute list, or a standard C++11 attribute sequence. */
23629
23630 static bool
23631 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23632 {
23633 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23634 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23635 }
23636
23637 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23638 of GNU attributes, or return NULL. */
23639
23640 static tree
23641 cp_parser_attributes_opt (cp_parser *parser)
23642 {
23643 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23644 return cp_parser_gnu_attributes_opt (parser);
23645 return cp_parser_std_attribute_spec_seq (parser);
23646 }
23647
23648 #define CILK_SIMD_FN_CLAUSE_MASK \
23649 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23650 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23651 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23652 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23653 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23654
23655 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23656 vector [(<clauses>)] */
23657
23658 static void
23659 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23660 {
23661 bool first_p = parser->cilk_simd_fn_info == NULL;
23662 cp_token *token = v_token;
23663 if (first_p)
23664 {
23665 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23666 parser->cilk_simd_fn_info->error_seen = false;
23667 parser->cilk_simd_fn_info->fndecl_seen = false;
23668 parser->cilk_simd_fn_info->tokens = vNULL;
23669 }
23670 int paren_scope = 0;
23671 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23672 {
23673 cp_lexer_consume_token (parser->lexer);
23674 v_token = cp_lexer_peek_token (parser->lexer);
23675 paren_scope++;
23676 }
23677 while (paren_scope > 0)
23678 {
23679 token = cp_lexer_peek_token (parser->lexer);
23680 if (token->type == CPP_OPEN_PAREN)
23681 paren_scope++;
23682 else if (token->type == CPP_CLOSE_PAREN)
23683 paren_scope--;
23684 /* Do not push the last ')' */
23685 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23686 cp_lexer_consume_token (parser->lexer);
23687 }
23688
23689 token->type = CPP_PRAGMA_EOL;
23690 parser->lexer->next_token = token;
23691 cp_lexer_consume_token (parser->lexer);
23692
23693 struct cp_token_cache *cp
23694 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23695 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23696 }
23697
23698 /* Parse an (optional) series of attributes.
23699
23700 attributes:
23701 attributes attribute
23702
23703 attribute:
23704 __attribute__ (( attribute-list [opt] ))
23705
23706 The return value is as for cp_parser_gnu_attribute_list. */
23707
23708 static tree
23709 cp_parser_gnu_attributes_opt (cp_parser* parser)
23710 {
23711 tree attributes = NULL_TREE;
23712
23713 while (true)
23714 {
23715 cp_token *token;
23716 tree attribute_list;
23717 bool ok = true;
23718
23719 /* Peek at the next token. */
23720 token = cp_lexer_peek_token (parser->lexer);
23721 /* If it's not `__attribute__', then we're done. */
23722 if (token->keyword != RID_ATTRIBUTE)
23723 break;
23724
23725 /* Consume the `__attribute__' keyword. */
23726 cp_lexer_consume_token (parser->lexer);
23727 /* Look for the two `(' tokens. */
23728 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23729 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23730
23731 /* Peek at the next token. */
23732 token = cp_lexer_peek_token (parser->lexer);
23733 if (token->type != CPP_CLOSE_PAREN)
23734 /* Parse the attribute-list. */
23735 attribute_list = cp_parser_gnu_attribute_list (parser);
23736 else
23737 /* If the next token is a `)', then there is no attribute
23738 list. */
23739 attribute_list = NULL;
23740
23741 /* Look for the two `)' tokens. */
23742 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23743 ok = false;
23744 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23745 ok = false;
23746 if (!ok)
23747 cp_parser_skip_to_end_of_statement (parser);
23748
23749 /* Add these new attributes to the list. */
23750 attributes = chainon (attributes, attribute_list);
23751 }
23752
23753 return attributes;
23754 }
23755
23756 /* Parse a GNU attribute-list.
23757
23758 attribute-list:
23759 attribute
23760 attribute-list , attribute
23761
23762 attribute:
23763 identifier
23764 identifier ( identifier )
23765 identifier ( identifier , expression-list )
23766 identifier ( expression-list )
23767
23768 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23769 to an attribute. The TREE_PURPOSE of each node is the identifier
23770 indicating which attribute is in use. The TREE_VALUE represents
23771 the arguments, if any. */
23772
23773 static tree
23774 cp_parser_gnu_attribute_list (cp_parser* parser)
23775 {
23776 tree attribute_list = NULL_TREE;
23777 bool save_translate_strings_p = parser->translate_strings_p;
23778
23779 parser->translate_strings_p = false;
23780 while (true)
23781 {
23782 cp_token *token;
23783 tree identifier;
23784 tree attribute;
23785
23786 /* Look for the identifier. We also allow keywords here; for
23787 example `__attribute__ ((const))' is legal. */
23788 token = cp_lexer_peek_token (parser->lexer);
23789 if (token->type == CPP_NAME
23790 || token->type == CPP_KEYWORD)
23791 {
23792 tree arguments = NULL_TREE;
23793
23794 /* Consume the token, but save it since we need it for the
23795 SIMD enabled function parsing. */
23796 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23797
23798 /* Save away the identifier that indicates which attribute
23799 this is. */
23800 identifier = (token->type == CPP_KEYWORD)
23801 /* For keywords, use the canonical spelling, not the
23802 parsed identifier. */
23803 ? ridpointers[(int) token->keyword]
23804 : id_token->u.value;
23805
23806 attribute = build_tree_list (identifier, NULL_TREE);
23807
23808 /* Peek at the next token. */
23809 token = cp_lexer_peek_token (parser->lexer);
23810 /* If it's an `(', then parse the attribute arguments. */
23811 if (token->type == CPP_OPEN_PAREN)
23812 {
23813 vec<tree, va_gc> *vec;
23814 int attr_flag = (attribute_takes_identifier_p (identifier)
23815 ? id_attr : normal_attr);
23816 if (is_cilkplus_vector_p (identifier))
23817 {
23818 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23819 continue;
23820 }
23821 else
23822 vec = cp_parser_parenthesized_expression_list
23823 (parser, attr_flag, /*cast_p=*/false,
23824 /*allow_expansion_p=*/false,
23825 /*non_constant_p=*/NULL);
23826 if (vec == NULL)
23827 arguments = error_mark_node;
23828 else
23829 {
23830 arguments = build_tree_list_vec (vec);
23831 release_tree_vector (vec);
23832 }
23833 /* Save the arguments away. */
23834 TREE_VALUE (attribute) = arguments;
23835 }
23836 else if (is_cilkplus_vector_p (identifier))
23837 {
23838 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23839 continue;
23840 }
23841
23842 if (arguments != error_mark_node)
23843 {
23844 /* Add this attribute to the list. */
23845 TREE_CHAIN (attribute) = attribute_list;
23846 attribute_list = attribute;
23847 }
23848
23849 token = cp_lexer_peek_token (parser->lexer);
23850 }
23851 /* Now, look for more attributes. If the next token isn't a
23852 `,', we're done. */
23853 if (token->type != CPP_COMMA)
23854 break;
23855
23856 /* Consume the comma and keep going. */
23857 cp_lexer_consume_token (parser->lexer);
23858 }
23859 parser->translate_strings_p = save_translate_strings_p;
23860
23861 /* We built up the list in reverse order. */
23862 return nreverse (attribute_list);
23863 }
23864
23865 /* Parse a standard C++11 attribute.
23866
23867 The returned representation is a TREE_LIST which TREE_PURPOSE is
23868 the scoped name of the attribute, and the TREE_VALUE is its
23869 arguments list.
23870
23871 Note that the scoped name of the attribute is itself a TREE_LIST
23872 which TREE_PURPOSE is the namespace of the attribute, and
23873 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23874 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23875 and which TREE_PURPOSE is directly the attribute name.
23876
23877 Clients of the attribute code should use get_attribute_namespace
23878 and get_attribute_name to get the actual namespace and name of
23879 attributes, regardless of their being GNU or C++11 attributes.
23880
23881 attribute:
23882 attribute-token attribute-argument-clause [opt]
23883
23884 attribute-token:
23885 identifier
23886 attribute-scoped-token
23887
23888 attribute-scoped-token:
23889 attribute-namespace :: identifier
23890
23891 attribute-namespace:
23892 identifier
23893
23894 attribute-argument-clause:
23895 ( balanced-token-seq )
23896
23897 balanced-token-seq:
23898 balanced-token [opt]
23899 balanced-token-seq balanced-token
23900
23901 balanced-token:
23902 ( balanced-token-seq )
23903 [ balanced-token-seq ]
23904 { balanced-token-seq }. */
23905
23906 static tree
23907 cp_parser_std_attribute (cp_parser *parser)
23908 {
23909 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23910 cp_token *token;
23911
23912 /* First, parse name of the attribute, a.k.a attribute-token. */
23913
23914 token = cp_lexer_peek_token (parser->lexer);
23915 if (token->type == CPP_NAME)
23916 attr_id = token->u.value;
23917 else if (token->type == CPP_KEYWORD)
23918 attr_id = ridpointers[(int) token->keyword];
23919 else if (token->flags & NAMED_OP)
23920 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23921
23922 if (attr_id == NULL_TREE)
23923 return NULL_TREE;
23924
23925 cp_lexer_consume_token (parser->lexer);
23926
23927 token = cp_lexer_peek_token (parser->lexer);
23928 if (token->type == CPP_SCOPE)
23929 {
23930 /* We are seeing a scoped attribute token. */
23931
23932 cp_lexer_consume_token (parser->lexer);
23933 attr_ns = attr_id;
23934
23935 token = cp_lexer_consume_token (parser->lexer);
23936 if (token->type == CPP_NAME)
23937 attr_id = token->u.value;
23938 else if (token->type == CPP_KEYWORD)
23939 attr_id = ridpointers[(int) token->keyword];
23940 else
23941 {
23942 error_at (token->location,
23943 "expected an identifier for the attribute name");
23944 return error_mark_node;
23945 }
23946 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23947 NULL_TREE);
23948 token = cp_lexer_peek_token (parser->lexer);
23949 }
23950 else
23951 {
23952 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23953 NULL_TREE);
23954 /* C++11 noreturn attribute is equivalent to GNU's. */
23955 if (is_attribute_p ("noreturn", attr_id))
23956 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23957 /* C++14 deprecated attribute is equivalent to GNU's. */
23958 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23959 {
23960 if (cxx_dialect == cxx11)
23961 pedwarn (token->location, OPT_Wpedantic,
23962 "%<deprecated%> is a C++14 feature;"
23963 " use %<gnu::deprecated%>");
23964 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23965 }
23966 /* Transactional Memory TS optimize_for_synchronized attribute is
23967 equivalent to GNU transaction_callable. */
23968 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23969 TREE_PURPOSE (attribute)
23970 = get_identifier ("transaction_callable");
23971 /* Transactional Memory attributes are GNU attributes. */
23972 else if (tm_attr_to_mask (attr_id))
23973 TREE_PURPOSE (attribute) = attr_id;
23974 }
23975
23976 /* Now parse the optional argument clause of the attribute. */
23977
23978 if (token->type != CPP_OPEN_PAREN)
23979 return attribute;
23980
23981 {
23982 vec<tree, va_gc> *vec;
23983 int attr_flag = normal_attr;
23984
23985 if (attr_ns == get_identifier ("gnu")
23986 && attribute_takes_identifier_p (attr_id))
23987 /* A GNU attribute that takes an identifier in parameter. */
23988 attr_flag = id_attr;
23989
23990 vec = cp_parser_parenthesized_expression_list
23991 (parser, attr_flag, /*cast_p=*/false,
23992 /*allow_expansion_p=*/true,
23993 /*non_constant_p=*/NULL);
23994 if (vec == NULL)
23995 arguments = error_mark_node;
23996 else
23997 {
23998 arguments = build_tree_list_vec (vec);
23999 release_tree_vector (vec);
24000 }
24001
24002 if (arguments == error_mark_node)
24003 attribute = error_mark_node;
24004 else
24005 TREE_VALUE (attribute) = arguments;
24006 }
24007
24008 return attribute;
24009 }
24010
24011 /* Check that the attribute ATTRIBUTE appears at most once in the
24012 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24013 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24014 isn't implemented yet in GCC. */
24015
24016 static void
24017 cp_parser_check_std_attribute (tree attributes, tree attribute)
24018 {
24019 if (attributes)
24020 {
24021 tree name = get_attribute_name (attribute);
24022 if (is_attribute_p ("noreturn", name)
24023 && lookup_attribute ("noreturn", attributes))
24024 error ("attribute noreturn can appear at most once "
24025 "in an attribute-list");
24026 else if (is_attribute_p ("deprecated", name)
24027 && lookup_attribute ("deprecated", attributes))
24028 error ("attribute deprecated can appear at most once "
24029 "in an attribute-list");
24030 }
24031 }
24032
24033 /* Parse a list of standard C++-11 attributes.
24034
24035 attribute-list:
24036 attribute [opt]
24037 attribute-list , attribute[opt]
24038 attribute ...
24039 attribute-list , attribute ...
24040 */
24041
24042 static tree
24043 cp_parser_std_attribute_list (cp_parser *parser)
24044 {
24045 tree attributes = NULL_TREE, attribute = NULL_TREE;
24046 cp_token *token = NULL;
24047
24048 while (true)
24049 {
24050 attribute = cp_parser_std_attribute (parser);
24051 if (attribute == error_mark_node)
24052 break;
24053 if (attribute != NULL_TREE)
24054 {
24055 cp_parser_check_std_attribute (attributes, attribute);
24056 TREE_CHAIN (attribute) = attributes;
24057 attributes = attribute;
24058 }
24059 token = cp_lexer_peek_token (parser->lexer);
24060 if (token->type == CPP_ELLIPSIS)
24061 {
24062 cp_lexer_consume_token (parser->lexer);
24063 TREE_VALUE (attribute)
24064 = make_pack_expansion (TREE_VALUE (attribute));
24065 token = cp_lexer_peek_token (parser->lexer);
24066 }
24067 if (token->type != CPP_COMMA)
24068 break;
24069 cp_lexer_consume_token (parser->lexer);
24070 }
24071 attributes = nreverse (attributes);
24072 return attributes;
24073 }
24074
24075 /* Parse a standard C++-11 attribute specifier.
24076
24077 attribute-specifier:
24078 [ [ attribute-list ] ]
24079 alignment-specifier
24080
24081 alignment-specifier:
24082 alignas ( type-id ... [opt] )
24083 alignas ( alignment-expression ... [opt] ). */
24084
24085 static tree
24086 cp_parser_std_attribute_spec (cp_parser *parser)
24087 {
24088 tree attributes = NULL_TREE;
24089 cp_token *token = cp_lexer_peek_token (parser->lexer);
24090
24091 if (token->type == CPP_OPEN_SQUARE
24092 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24093 {
24094 cp_lexer_consume_token (parser->lexer);
24095 cp_lexer_consume_token (parser->lexer);
24096
24097 attributes = cp_parser_std_attribute_list (parser);
24098
24099 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24100 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24101 cp_parser_skip_to_end_of_statement (parser);
24102 else
24103 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24104 when we are sure that we have actually parsed them. */
24105 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24106 }
24107 else
24108 {
24109 tree alignas_expr;
24110
24111 /* Look for an alignment-specifier. */
24112
24113 token = cp_lexer_peek_token (parser->lexer);
24114
24115 if (token->type != CPP_KEYWORD
24116 || token->keyword != RID_ALIGNAS)
24117 return NULL_TREE;
24118
24119 cp_lexer_consume_token (parser->lexer);
24120 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24121
24122 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24123 {
24124 cp_parser_error (parser, "expected %<(%>");
24125 return error_mark_node;
24126 }
24127
24128 cp_parser_parse_tentatively (parser);
24129 alignas_expr = cp_parser_type_id (parser);
24130
24131 if (!cp_parser_parse_definitely (parser))
24132 {
24133 gcc_assert (alignas_expr == error_mark_node
24134 || alignas_expr == NULL_TREE);
24135
24136 alignas_expr =
24137 cp_parser_assignment_expression (parser);
24138 if (alignas_expr == error_mark_node)
24139 cp_parser_skip_to_end_of_statement (parser);
24140 if (alignas_expr == NULL_TREE
24141 || alignas_expr == error_mark_node)
24142 return alignas_expr;
24143 }
24144
24145 alignas_expr = cxx_alignas_expr (alignas_expr);
24146 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24147
24148 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24149 {
24150 cp_lexer_consume_token (parser->lexer);
24151 alignas_expr = make_pack_expansion (alignas_expr);
24152 }
24153
24154 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24155 {
24156 cp_parser_error (parser, "expected %<)%>");
24157 return error_mark_node;
24158 }
24159
24160 /* Build the C++-11 representation of an 'aligned'
24161 attribute. */
24162 attributes =
24163 build_tree_list (build_tree_list (get_identifier ("gnu"),
24164 get_identifier ("aligned")),
24165 alignas_expr);
24166 }
24167
24168 return attributes;
24169 }
24170
24171 /* Parse a standard C++-11 attribute-specifier-seq.
24172
24173 attribute-specifier-seq:
24174 attribute-specifier-seq [opt] attribute-specifier
24175 */
24176
24177 static tree
24178 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24179 {
24180 tree attr_specs = NULL_TREE;
24181 tree attr_last = NULL_TREE;
24182
24183 while (true)
24184 {
24185 tree attr_spec = cp_parser_std_attribute_spec (parser);
24186 if (attr_spec == NULL_TREE)
24187 break;
24188 if (attr_spec == error_mark_node)
24189 return error_mark_node;
24190
24191 if (attr_last)
24192 TREE_CHAIN (attr_last) = attr_spec;
24193 else
24194 attr_specs = attr_last = attr_spec;
24195 attr_last = tree_last (attr_last);
24196 }
24197
24198 return attr_specs;
24199 }
24200
24201 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24202 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24203 current value of the PEDANTIC flag, regardless of whether or not
24204 the `__extension__' keyword is present. The caller is responsible
24205 for restoring the value of the PEDANTIC flag. */
24206
24207 static bool
24208 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24209 {
24210 /* Save the old value of the PEDANTIC flag. */
24211 *saved_pedantic = pedantic;
24212
24213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24214 {
24215 /* Consume the `__extension__' token. */
24216 cp_lexer_consume_token (parser->lexer);
24217 /* We're not being pedantic while the `__extension__' keyword is
24218 in effect. */
24219 pedantic = 0;
24220
24221 return true;
24222 }
24223
24224 return false;
24225 }
24226
24227 /* Parse a label declaration.
24228
24229 label-declaration:
24230 __label__ label-declarator-seq ;
24231
24232 label-declarator-seq:
24233 identifier , label-declarator-seq
24234 identifier */
24235
24236 static void
24237 cp_parser_label_declaration (cp_parser* parser)
24238 {
24239 /* Look for the `__label__' keyword. */
24240 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24241
24242 while (true)
24243 {
24244 tree identifier;
24245
24246 /* Look for an identifier. */
24247 identifier = cp_parser_identifier (parser);
24248 /* If we failed, stop. */
24249 if (identifier == error_mark_node)
24250 break;
24251 /* Declare it as a label. */
24252 finish_label_decl (identifier);
24253 /* If the next token is a `;', stop. */
24254 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24255 break;
24256 /* Look for the `,' separating the label declarations. */
24257 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24258 }
24259
24260 /* Look for the final `;'. */
24261 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24262 }
24263
24264 // -------------------------------------------------------------------------- //
24265 // Requires Clause
24266
24267 // Parse a requires clause.
24268 //
24269 // requires-clause:
24270 // 'requires' logical-or-expression
24271 //
24272 // The required logical-or-expression must be a constant expression. Note
24273 // that we don't check that the expression is constepxr here. We defer until
24274 // we analyze constraints and then, we only check atomic constraints.
24275 static tree
24276 cp_parser_requires_clause (cp_parser *parser)
24277 {
24278 // Parse the requires clause so that it is not automatically folded.
24279 ++processing_template_decl;
24280 tree expr = cp_parser_binary_expression (parser, false, false,
24281 PREC_NOT_OPERATOR, NULL);
24282 if (check_for_bare_parameter_packs (expr))
24283 expr = error_mark_node;
24284 --processing_template_decl;
24285 return expr;
24286 }
24287
24288 // Optionally parse a requires clause:
24289 static tree
24290 cp_parser_requires_clause_opt (cp_parser *parser)
24291 {
24292 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24293 if (tok->keyword != RID_REQUIRES)
24294 {
24295 if (!flag_concepts && tok->type == CPP_NAME
24296 && tok->u.value == ridpointers[RID_REQUIRES])
24297 {
24298 error_at (cp_lexer_peek_token (parser->lexer)->location,
24299 "%<requires%> only available with -fconcepts");
24300 /* Parse and discard the requires-clause. */
24301 cp_lexer_consume_token (parser->lexer);
24302 cp_parser_requires_clause (parser);
24303 }
24304 return NULL_TREE;
24305 }
24306 cp_lexer_consume_token (parser->lexer);
24307 return cp_parser_requires_clause (parser);
24308 }
24309
24310
24311 /*---------------------------------------------------------------------------
24312 Requires expressions
24313 ---------------------------------------------------------------------------*/
24314
24315 /* Parse a requires expression
24316
24317 requirement-expression:
24318 'requires' requirement-parameter-list [opt] requirement-body */
24319 static tree
24320 cp_parser_requires_expression (cp_parser *parser)
24321 {
24322 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24323 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24324
24325 /* A requires-expression shall appear only within a concept
24326 definition or a requires-clause.
24327
24328 TODO: Implement this diagnostic correctly. */
24329 if (!processing_template_decl)
24330 {
24331 error_at (loc, "a requires expression cannot appear outside a template");
24332 cp_parser_skip_to_end_of_statement (parser);
24333 return error_mark_node;
24334 }
24335
24336 tree parms, reqs;
24337 {
24338 /* Local parameters are delared as variables within the scope
24339 of the expression. They are not visible past the end of
24340 the expression. Expressions within the requires-expression
24341 are unevaluated. */
24342 struct scope_sentinel
24343 {
24344 scope_sentinel ()
24345 {
24346 ++cp_unevaluated_operand;
24347 begin_scope (sk_block, NULL_TREE);
24348 }
24349
24350 ~scope_sentinel ()
24351 {
24352 pop_bindings_and_leave_scope ();
24353 --cp_unevaluated_operand;
24354 }
24355 } s;
24356
24357 /* Parse the optional parameter list. */
24358 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24359 {
24360 parms = cp_parser_requirement_parameter_list (parser);
24361 if (parms == error_mark_node)
24362 return error_mark_node;
24363 }
24364 else
24365 parms = NULL_TREE;
24366
24367 /* Parse the requirement body. */
24368 reqs = cp_parser_requirement_body (parser);
24369 if (reqs == error_mark_node)
24370 return error_mark_node;
24371 }
24372
24373 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24374 the parm chain. */
24375 grokparms (parms, &parms);
24376 return finish_requires_expr (parms, reqs);
24377 }
24378
24379 /* Parse a parameterized requirement.
24380
24381 requirement-parameter-list:
24382 '(' parameter-declaration-clause ')' */
24383 static tree
24384 cp_parser_requirement_parameter_list (cp_parser *parser)
24385 {
24386 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24387 return error_mark_node;
24388
24389 tree parms = cp_parser_parameter_declaration_clause (parser);
24390
24391 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24392 return error_mark_node;
24393
24394 return parms;
24395 }
24396
24397 /* Parse the body of a requirement.
24398
24399 requirement-body:
24400 '{' requirement-list '}' */
24401 static tree
24402 cp_parser_requirement_body (cp_parser *parser)
24403 {
24404 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24405 return error_mark_node;
24406
24407 tree reqs = cp_parser_requirement_list (parser);
24408
24409 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24410 return error_mark_node;
24411
24412 return reqs;
24413 }
24414
24415 /* Parse a list of requirements.
24416
24417 requirement-list:
24418 requirement
24419 requirement-list ';' requirement[opt] */
24420 static tree
24421 cp_parser_requirement_list (cp_parser *parser)
24422 {
24423 tree result = NULL_TREE;
24424 while (true)
24425 {
24426 tree req = cp_parser_requirement (parser);
24427 if (req == error_mark_node)
24428 return error_mark_node;
24429
24430 result = tree_cons (NULL_TREE, req, result);
24431
24432 /* If we see a semi-colon, consume it. */
24433 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24434 cp_lexer_consume_token (parser->lexer);
24435
24436 /* Stop processing at the end of the list. */
24437 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24438 break;
24439 }
24440
24441 /* Reverse the order of requirements so they are analyzed in
24442 declaration order. */
24443 return nreverse (result);
24444 }
24445
24446 /* Parse a syntactic requirement or type requirement.
24447
24448 requirement:
24449 simple-requirement
24450 compound-requirement
24451 type-requirement
24452 nested-requirement */
24453 static tree
24454 cp_parser_requirement (cp_parser *parser)
24455 {
24456 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24457 return cp_parser_compound_requirement (parser);
24458 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24459 return cp_parser_type_requirement (parser);
24460 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24461 return cp_parser_nested_requirement (parser);
24462 else
24463 return cp_parser_simple_requirement (parser);
24464 }
24465
24466 /* Parse a simple requirement.
24467
24468 simple-requirement:
24469 expression ';' */
24470 static tree
24471 cp_parser_simple_requirement (cp_parser *parser)
24472 {
24473 tree expr = cp_parser_expression (parser, NULL, false, false);
24474 if (!expr || expr == error_mark_node)
24475 return error_mark_node;
24476
24477 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24478 return error_mark_node;
24479
24480 return finish_simple_requirement (expr);
24481 }
24482
24483 /* Parse a type requirement
24484
24485 type-requirement
24486 nested-name-specifier [opt] required-type-name ';'
24487
24488 required-type-name:
24489 type-name
24490 'template' [opt] simple-template-id */
24491 static tree
24492 cp_parser_type_requirement (cp_parser *parser)
24493 {
24494 cp_lexer_consume_token (parser->lexer);
24495
24496 // Save the scope before parsing name specifiers.
24497 tree saved_scope = parser->scope;
24498 tree saved_object_scope = parser->object_scope;
24499 tree saved_qualifying_scope = parser->qualifying_scope;
24500 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24501 cp_parser_nested_name_specifier_opt (parser,
24502 /*typename_keyword_p=*/true,
24503 /*check_dependency_p=*/false,
24504 /*type_p=*/true,
24505 /*is_declaration=*/false);
24506
24507 tree type;
24508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24509 {
24510 cp_lexer_consume_token (parser->lexer);
24511 type = cp_parser_template_id (parser,
24512 /*template_keyword_p=*/true,
24513 /*check_dependency=*/false,
24514 /*tag_type=*/none_type,
24515 /*is_declaration=*/false);
24516 type = make_typename_type (parser->scope, type, typename_type,
24517 /*complain=*/tf_error);
24518 }
24519 else
24520 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24521
24522 if (TREE_CODE (type) == TYPE_DECL)
24523 type = TREE_TYPE (type);
24524
24525 parser->scope = saved_scope;
24526 parser->object_scope = saved_object_scope;
24527 parser->qualifying_scope = saved_qualifying_scope;
24528
24529 if (type == error_mark_node)
24530 cp_parser_skip_to_end_of_statement (parser);
24531
24532 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24533 return error_mark_node;
24534 if (type == error_mark_node)
24535 return error_mark_node;
24536
24537 return finish_type_requirement (type);
24538 }
24539
24540 /* Parse a compound requirement
24541
24542 compound-requirement:
24543 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24544 static tree
24545 cp_parser_compound_requirement (cp_parser *parser)
24546 {
24547 /* Parse an expression enclosed in '{ }'s. */
24548 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24549 return error_mark_node;
24550
24551 tree expr = cp_parser_expression (parser, NULL, false, false);
24552 if (!expr || expr == error_mark_node)
24553 return error_mark_node;
24554
24555 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24556 return error_mark_node;
24557
24558 /* Parse the optional noexcept. */
24559 bool noexcept_p = false;
24560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24561 {
24562 cp_lexer_consume_token (parser->lexer);
24563 noexcept_p = true;
24564 }
24565
24566 /* Parse the optional trailing return type. */
24567 tree type = NULL_TREE;
24568 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24569 {
24570 cp_lexer_consume_token (parser->lexer);
24571 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24572 parser->in_result_type_constraint_p = true;
24573 type = cp_parser_trailing_type_id (parser);
24574 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24575 if (type == error_mark_node)
24576 return error_mark_node;
24577 }
24578
24579 return finish_compound_requirement (expr, type, noexcept_p);
24580 }
24581
24582 /* Parse a nested requirement. This is the same as a requires clause.
24583
24584 nested-requirement:
24585 requires-clause */
24586 static tree
24587 cp_parser_nested_requirement (cp_parser *parser)
24588 {
24589 cp_lexer_consume_token (parser->lexer);
24590 tree req = cp_parser_requires_clause (parser);
24591 if (req == error_mark_node)
24592 return error_mark_node;
24593 return finish_nested_requirement (req);
24594 }
24595
24596 /* Support Functions */
24597
24598 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24599 NAME should have one of the representations used for an
24600 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24601 is returned. If PARSER->SCOPE is a dependent type, then a
24602 SCOPE_REF is returned.
24603
24604 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24605 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24606 was formed. Abstractly, such entities should not be passed to this
24607 function, because they do not need to be looked up, but it is
24608 simpler to check for this special case here, rather than at the
24609 call-sites.
24610
24611 In cases not explicitly covered above, this function returns a
24612 DECL, OVERLOAD, or baselink representing the result of the lookup.
24613 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24614 is returned.
24615
24616 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24617 (e.g., "struct") that was used. In that case bindings that do not
24618 refer to types are ignored.
24619
24620 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24621 ignored.
24622
24623 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24624 are ignored.
24625
24626 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24627 types.
24628
24629 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24630 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24631 NULL_TREE otherwise. */
24632
24633 static cp_expr
24634 cp_parser_lookup_name (cp_parser *parser, tree name,
24635 enum tag_types tag_type,
24636 bool is_template,
24637 bool is_namespace,
24638 bool check_dependency,
24639 tree *ambiguous_decls,
24640 location_t name_location)
24641 {
24642 tree decl;
24643 tree object_type = parser->context->object_type;
24644
24645 /* Assume that the lookup will be unambiguous. */
24646 if (ambiguous_decls)
24647 *ambiguous_decls = NULL_TREE;
24648
24649 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24650 no longer valid. Note that if we are parsing tentatively, and
24651 the parse fails, OBJECT_TYPE will be automatically restored. */
24652 parser->context->object_type = NULL_TREE;
24653
24654 if (name == error_mark_node)
24655 return error_mark_node;
24656
24657 /* A template-id has already been resolved; there is no lookup to
24658 do. */
24659 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24660 return name;
24661 if (BASELINK_P (name))
24662 {
24663 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24664 == TEMPLATE_ID_EXPR);
24665 return name;
24666 }
24667
24668 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24669 it should already have been checked to make sure that the name
24670 used matches the type being destroyed. */
24671 if (TREE_CODE (name) == BIT_NOT_EXPR)
24672 {
24673 tree type;
24674
24675 /* Figure out to which type this destructor applies. */
24676 if (parser->scope)
24677 type = parser->scope;
24678 else if (object_type)
24679 type = object_type;
24680 else
24681 type = current_class_type;
24682 /* If that's not a class type, there is no destructor. */
24683 if (!type || !CLASS_TYPE_P (type))
24684 return error_mark_node;
24685 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24686 lazily_declare_fn (sfk_destructor, type);
24687 if (!CLASSTYPE_DESTRUCTORS (type))
24688 return error_mark_node;
24689 /* If it was a class type, return the destructor. */
24690 return CLASSTYPE_DESTRUCTORS (type);
24691 }
24692
24693 /* By this point, the NAME should be an ordinary identifier. If
24694 the id-expression was a qualified name, the qualifying scope is
24695 stored in PARSER->SCOPE at this point. */
24696 gcc_assert (identifier_p (name));
24697
24698 /* Perform the lookup. */
24699 if (parser->scope)
24700 {
24701 bool dependent_p;
24702
24703 if (parser->scope == error_mark_node)
24704 return error_mark_node;
24705
24706 /* If the SCOPE is dependent, the lookup must be deferred until
24707 the template is instantiated -- unless we are explicitly
24708 looking up names in uninstantiated templates. Even then, we
24709 cannot look up the name if the scope is not a class type; it
24710 might, for example, be a template type parameter. */
24711 dependent_p = (TYPE_P (parser->scope)
24712 && dependent_scope_p (parser->scope));
24713 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24714 && dependent_p)
24715 /* Defer lookup. */
24716 decl = error_mark_node;
24717 else
24718 {
24719 tree pushed_scope = NULL_TREE;
24720
24721 /* If PARSER->SCOPE is a dependent type, then it must be a
24722 class type, and we must not be checking dependencies;
24723 otherwise, we would have processed this lookup above. So
24724 that PARSER->SCOPE is not considered a dependent base by
24725 lookup_member, we must enter the scope here. */
24726 if (dependent_p)
24727 pushed_scope = push_scope (parser->scope);
24728
24729 /* If the PARSER->SCOPE is a template specialization, it
24730 may be instantiated during name lookup. In that case,
24731 errors may be issued. Even if we rollback the current
24732 tentative parse, those errors are valid. */
24733 decl = lookup_qualified_name (parser->scope, name,
24734 tag_type != none_type,
24735 /*complain=*/true);
24736
24737 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24738 lookup result and the nested-name-specifier nominates a class C:
24739 * if the name specified after the nested-name-specifier, when
24740 looked up in C, is the injected-class-name of C (Clause 9), or
24741 * if the name specified after the nested-name-specifier is the
24742 same as the identifier or the simple-template-id's template-
24743 name in the last component of the nested-name-specifier,
24744 the name is instead considered to name the constructor of
24745 class C. [ Note: for example, the constructor is not an
24746 acceptable lookup result in an elaborated-type-specifier so
24747 the constructor would not be used in place of the
24748 injected-class-name. --end note ] Such a constructor name
24749 shall be used only in the declarator-id of a declaration that
24750 names a constructor or in a using-declaration. */
24751 if (tag_type == none_type
24752 && DECL_SELF_REFERENCE_P (decl)
24753 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24754 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24755 tag_type != none_type,
24756 /*complain=*/true);
24757
24758 /* If we have a single function from a using decl, pull it out. */
24759 if (TREE_CODE (decl) == OVERLOAD
24760 && !really_overloaded_fn (decl))
24761 decl = OVL_FUNCTION (decl);
24762
24763 if (pushed_scope)
24764 pop_scope (pushed_scope);
24765 }
24766
24767 /* If the scope is a dependent type and either we deferred lookup or
24768 we did lookup but didn't find the name, rememeber the name. */
24769 if (decl == error_mark_node && TYPE_P (parser->scope)
24770 && dependent_type_p (parser->scope))
24771 {
24772 if (tag_type)
24773 {
24774 tree type;
24775
24776 /* The resolution to Core Issue 180 says that `struct
24777 A::B' should be considered a type-name, even if `A'
24778 is dependent. */
24779 type = make_typename_type (parser->scope, name, tag_type,
24780 /*complain=*/tf_error);
24781 if (type != error_mark_node)
24782 decl = TYPE_NAME (type);
24783 }
24784 else if (is_template
24785 && (cp_parser_next_token_ends_template_argument_p (parser)
24786 || cp_lexer_next_token_is (parser->lexer,
24787 CPP_CLOSE_PAREN)))
24788 decl = make_unbound_class_template (parser->scope,
24789 name, NULL_TREE,
24790 /*complain=*/tf_error);
24791 else
24792 decl = build_qualified_name (/*type=*/NULL_TREE,
24793 parser->scope, name,
24794 is_template);
24795 }
24796 parser->qualifying_scope = parser->scope;
24797 parser->object_scope = NULL_TREE;
24798 }
24799 else if (object_type)
24800 {
24801 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24802 OBJECT_TYPE is not a class. */
24803 if (CLASS_TYPE_P (object_type))
24804 /* If the OBJECT_TYPE is a template specialization, it may
24805 be instantiated during name lookup. In that case, errors
24806 may be issued. Even if we rollback the current tentative
24807 parse, those errors are valid. */
24808 decl = lookup_member (object_type,
24809 name,
24810 /*protect=*/0,
24811 tag_type != none_type,
24812 tf_warning_or_error);
24813 else
24814 decl = NULL_TREE;
24815
24816 if (!decl)
24817 /* Look it up in the enclosing context. DR 141: When looking for a
24818 template-name after -> or ., only consider class templates. */
24819 decl = lookup_name_real (name, tag_type != none_type || is_template,
24820 /*nonclass=*/0,
24821 /*block_p=*/true, is_namespace, 0);
24822 if (object_type == unknown_type_node)
24823 /* The object is type-dependent, so we can't look anything up; we used
24824 this to get the DR 141 behavior. */
24825 object_type = NULL_TREE;
24826 parser->object_scope = object_type;
24827 parser->qualifying_scope = NULL_TREE;
24828 }
24829 else
24830 {
24831 decl = lookup_name_real (name, tag_type != none_type,
24832 /*nonclass=*/0,
24833 /*block_p=*/true, is_namespace, 0);
24834 parser->qualifying_scope = NULL_TREE;
24835 parser->object_scope = NULL_TREE;
24836 }
24837
24838 /* If the lookup failed, let our caller know. */
24839 if (!decl || decl == error_mark_node)
24840 return error_mark_node;
24841
24842 /* Pull out the template from an injected-class-name (or multiple). */
24843 if (is_template)
24844 decl = maybe_get_template_decl_from_type_decl (decl);
24845
24846 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24847 if (TREE_CODE (decl) == TREE_LIST)
24848 {
24849 if (ambiguous_decls)
24850 *ambiguous_decls = decl;
24851 /* The error message we have to print is too complicated for
24852 cp_parser_error, so we incorporate its actions directly. */
24853 if (!cp_parser_simulate_error (parser))
24854 {
24855 error_at (name_location, "reference to %qD is ambiguous",
24856 name);
24857 print_candidates (decl);
24858 }
24859 return error_mark_node;
24860 }
24861
24862 gcc_assert (DECL_P (decl)
24863 || TREE_CODE (decl) == OVERLOAD
24864 || TREE_CODE (decl) == SCOPE_REF
24865 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24866 || BASELINK_P (decl));
24867
24868 /* If we have resolved the name of a member declaration, check to
24869 see if the declaration is accessible. When the name resolves to
24870 set of overloaded functions, accessibility is checked when
24871 overload resolution is done.
24872
24873 During an explicit instantiation, access is not checked at all,
24874 as per [temp.explicit]. */
24875 if (DECL_P (decl))
24876 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24877
24878 maybe_record_typedef_use (decl);
24879
24880 return cp_expr (decl, name_location);
24881 }
24882
24883 /* Like cp_parser_lookup_name, but for use in the typical case where
24884 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24885 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24886
24887 static tree
24888 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24889 {
24890 return cp_parser_lookup_name (parser, name,
24891 none_type,
24892 /*is_template=*/false,
24893 /*is_namespace=*/false,
24894 /*check_dependency=*/true,
24895 /*ambiguous_decls=*/NULL,
24896 location);
24897 }
24898
24899 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24900 the current context, return the TYPE_DECL. If TAG_NAME_P is
24901 true, the DECL indicates the class being defined in a class-head,
24902 or declared in an elaborated-type-specifier.
24903
24904 Otherwise, return DECL. */
24905
24906 static tree
24907 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24908 {
24909 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24910 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24911
24912 struct A {
24913 template <typename T> struct B;
24914 };
24915
24916 template <typename T> struct A::B {};
24917
24918 Similarly, in an elaborated-type-specifier:
24919
24920 namespace N { struct X{}; }
24921
24922 struct A {
24923 template <typename T> friend struct N::X;
24924 };
24925
24926 However, if the DECL refers to a class type, and we are in
24927 the scope of the class, then the name lookup automatically
24928 finds the TYPE_DECL created by build_self_reference rather
24929 than a TEMPLATE_DECL. For example, in:
24930
24931 template <class T> struct S {
24932 S s;
24933 };
24934
24935 there is no need to handle such case. */
24936
24937 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24938 return DECL_TEMPLATE_RESULT (decl);
24939
24940 return decl;
24941 }
24942
24943 /* If too many, or too few, template-parameter lists apply to the
24944 declarator, issue an error message. Returns TRUE if all went well,
24945 and FALSE otherwise. */
24946
24947 static bool
24948 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24949 cp_declarator *declarator,
24950 location_t declarator_location)
24951 {
24952 switch (declarator->kind)
24953 {
24954 case cdk_id:
24955 {
24956 unsigned num_templates = 0;
24957 tree scope = declarator->u.id.qualifying_scope;
24958
24959 if (scope)
24960 num_templates = num_template_headers_for_class (scope);
24961 else if (TREE_CODE (declarator->u.id.unqualified_name)
24962 == TEMPLATE_ID_EXPR)
24963 /* If the DECLARATOR has the form `X<y>' then it uses one
24964 additional level of template parameters. */
24965 ++num_templates;
24966
24967 return cp_parser_check_template_parameters
24968 (parser, num_templates, declarator_location, declarator);
24969 }
24970
24971 case cdk_function:
24972 case cdk_array:
24973 case cdk_pointer:
24974 case cdk_reference:
24975 case cdk_ptrmem:
24976 return (cp_parser_check_declarator_template_parameters
24977 (parser, declarator->declarator, declarator_location));
24978
24979 case cdk_error:
24980 return true;
24981
24982 default:
24983 gcc_unreachable ();
24984 }
24985 return false;
24986 }
24987
24988 /* NUM_TEMPLATES were used in the current declaration. If that is
24989 invalid, return FALSE and issue an error messages. Otherwise,
24990 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24991 declarator and we can print more accurate diagnostics. */
24992
24993 static bool
24994 cp_parser_check_template_parameters (cp_parser* parser,
24995 unsigned num_templates,
24996 location_t location,
24997 cp_declarator *declarator)
24998 {
24999 /* If there are the same number of template classes and parameter
25000 lists, that's OK. */
25001 if (parser->num_template_parameter_lists == num_templates)
25002 return true;
25003 /* If there are more, but only one more, then we are referring to a
25004 member template. That's OK too. */
25005 if (parser->num_template_parameter_lists == num_templates + 1)
25006 return true;
25007 /* If there are more template classes than parameter lists, we have
25008 something like:
25009
25010 template <class T> void S<T>::R<T>::f (); */
25011 if (parser->num_template_parameter_lists < num_templates)
25012 {
25013 if (declarator && !current_function_decl)
25014 error_at (location, "specializing member %<%T::%E%> "
25015 "requires %<template<>%> syntax",
25016 declarator->u.id.qualifying_scope,
25017 declarator->u.id.unqualified_name);
25018 else if (declarator)
25019 error_at (location, "invalid declaration of %<%T::%E%>",
25020 declarator->u.id.qualifying_scope,
25021 declarator->u.id.unqualified_name);
25022 else
25023 error_at (location, "too few template-parameter-lists");
25024 return false;
25025 }
25026 /* Otherwise, there are too many template parameter lists. We have
25027 something like:
25028
25029 template <class T> template <class U> void S::f(); */
25030 error_at (location, "too many template-parameter-lists");
25031 return false;
25032 }
25033
25034 /* Parse an optional `::' token indicating that the following name is
25035 from the global namespace. If so, PARSER->SCOPE is set to the
25036 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25037 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25038 Returns the new value of PARSER->SCOPE, if the `::' token is
25039 present, and NULL_TREE otherwise. */
25040
25041 static tree
25042 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25043 {
25044 cp_token *token;
25045
25046 /* Peek at the next token. */
25047 token = cp_lexer_peek_token (parser->lexer);
25048 /* If we're looking at a `::' token then we're starting from the
25049 global namespace, not our current location. */
25050 if (token->type == CPP_SCOPE)
25051 {
25052 /* Consume the `::' token. */
25053 cp_lexer_consume_token (parser->lexer);
25054 /* Set the SCOPE so that we know where to start the lookup. */
25055 parser->scope = global_namespace;
25056 parser->qualifying_scope = global_namespace;
25057 parser->object_scope = NULL_TREE;
25058
25059 return parser->scope;
25060 }
25061 else if (!current_scope_valid_p)
25062 {
25063 parser->scope = NULL_TREE;
25064 parser->qualifying_scope = NULL_TREE;
25065 parser->object_scope = NULL_TREE;
25066 }
25067
25068 return NULL_TREE;
25069 }
25070
25071 /* Returns TRUE if the upcoming token sequence is the start of a
25072 constructor declarator. If FRIEND_P is true, the declarator is
25073 preceded by the `friend' specifier. */
25074
25075 static bool
25076 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25077 {
25078 bool constructor_p;
25079 bool outside_class_specifier_p;
25080 tree nested_name_specifier;
25081 cp_token *next_token;
25082
25083 /* The common case is that this is not a constructor declarator, so
25084 try to avoid doing lots of work if at all possible. It's not
25085 valid declare a constructor at function scope. */
25086 if (parser->in_function_body)
25087 return false;
25088 /* And only certain tokens can begin a constructor declarator. */
25089 next_token = cp_lexer_peek_token (parser->lexer);
25090 if (next_token->type != CPP_NAME
25091 && next_token->type != CPP_SCOPE
25092 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25093 && next_token->type != CPP_TEMPLATE_ID)
25094 return false;
25095
25096 /* Parse tentatively; we are going to roll back all of the tokens
25097 consumed here. */
25098 cp_parser_parse_tentatively (parser);
25099 /* Assume that we are looking at a constructor declarator. */
25100 constructor_p = true;
25101
25102 /* Look for the optional `::' operator. */
25103 cp_parser_global_scope_opt (parser,
25104 /*current_scope_valid_p=*/false);
25105 /* Look for the nested-name-specifier. */
25106 nested_name_specifier
25107 = (cp_parser_nested_name_specifier_opt (parser,
25108 /*typename_keyword_p=*/false,
25109 /*check_dependency_p=*/false,
25110 /*type_p=*/false,
25111 /*is_declaration=*/false));
25112
25113 outside_class_specifier_p = (!at_class_scope_p ()
25114 || !TYPE_BEING_DEFINED (current_class_type)
25115 || friend_p);
25116
25117 /* Outside of a class-specifier, there must be a
25118 nested-name-specifier. */
25119 if (!nested_name_specifier && outside_class_specifier_p)
25120 constructor_p = false;
25121 else if (nested_name_specifier == error_mark_node)
25122 constructor_p = false;
25123
25124 /* If we have a class scope, this is easy; DR 147 says that S::S always
25125 names the constructor, and no other qualified name could. */
25126 if (constructor_p && nested_name_specifier
25127 && CLASS_TYPE_P (nested_name_specifier))
25128 {
25129 tree id = cp_parser_unqualified_id (parser,
25130 /*template_keyword_p=*/false,
25131 /*check_dependency_p=*/false,
25132 /*declarator_p=*/true,
25133 /*optional_p=*/false);
25134 if (is_overloaded_fn (id))
25135 id = DECL_NAME (get_first_fn (id));
25136 if (!constructor_name_p (id, nested_name_specifier))
25137 constructor_p = false;
25138 }
25139 /* If we still think that this might be a constructor-declarator,
25140 look for a class-name. */
25141 else if (constructor_p)
25142 {
25143 /* If we have:
25144
25145 template <typename T> struct S {
25146 S();
25147 };
25148
25149 we must recognize that the nested `S' names a class. */
25150 tree type_decl;
25151 type_decl = cp_parser_class_name (parser,
25152 /*typename_keyword_p=*/false,
25153 /*template_keyword_p=*/false,
25154 none_type,
25155 /*check_dependency_p=*/false,
25156 /*class_head_p=*/false,
25157 /*is_declaration=*/false);
25158 /* If there was no class-name, then this is not a constructor.
25159 Otherwise, if we are in a class-specifier and we aren't
25160 handling a friend declaration, check that its type matches
25161 current_class_type (c++/38313). Note: error_mark_node
25162 is left alone for error recovery purposes. */
25163 constructor_p = (!cp_parser_error_occurred (parser)
25164 && (outside_class_specifier_p
25165 || type_decl == error_mark_node
25166 || same_type_p (current_class_type,
25167 TREE_TYPE (type_decl))));
25168
25169 /* If we're still considering a constructor, we have to see a `(',
25170 to begin the parameter-declaration-clause, followed by either a
25171 `)', an `...', or a decl-specifier. We need to check for a
25172 type-specifier to avoid being fooled into thinking that:
25173
25174 S (f) (int);
25175
25176 is a constructor. (It is actually a function named `f' that
25177 takes one parameter (of type `int') and returns a value of type
25178 `S'. */
25179 if (constructor_p
25180 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25181 constructor_p = false;
25182
25183 if (constructor_p
25184 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25185 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25186 /* A parameter declaration begins with a decl-specifier,
25187 which is either the "attribute" keyword, a storage class
25188 specifier, or (usually) a type-specifier. */
25189 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25190 {
25191 tree type;
25192 tree pushed_scope = NULL_TREE;
25193 unsigned saved_num_template_parameter_lists;
25194
25195 /* Names appearing in the type-specifier should be looked up
25196 in the scope of the class. */
25197 if (current_class_type)
25198 type = NULL_TREE;
25199 else
25200 {
25201 type = TREE_TYPE (type_decl);
25202 if (TREE_CODE (type) == TYPENAME_TYPE)
25203 {
25204 type = resolve_typename_type (type,
25205 /*only_current_p=*/false);
25206 if (TREE_CODE (type) == TYPENAME_TYPE)
25207 {
25208 cp_parser_abort_tentative_parse (parser);
25209 return false;
25210 }
25211 }
25212 pushed_scope = push_scope (type);
25213 }
25214
25215 /* Inside the constructor parameter list, surrounding
25216 template-parameter-lists do not apply. */
25217 saved_num_template_parameter_lists
25218 = parser->num_template_parameter_lists;
25219 parser->num_template_parameter_lists = 0;
25220
25221 /* Look for the type-specifier. */
25222 cp_parser_type_specifier (parser,
25223 CP_PARSER_FLAGS_NONE,
25224 /*decl_specs=*/NULL,
25225 /*is_declarator=*/true,
25226 /*declares_class_or_enum=*/NULL,
25227 /*is_cv_qualifier=*/NULL);
25228
25229 parser->num_template_parameter_lists
25230 = saved_num_template_parameter_lists;
25231
25232 /* Leave the scope of the class. */
25233 if (pushed_scope)
25234 pop_scope (pushed_scope);
25235
25236 constructor_p = !cp_parser_error_occurred (parser);
25237 }
25238 }
25239
25240 /* We did not really want to consume any tokens. */
25241 cp_parser_abort_tentative_parse (parser);
25242
25243 return constructor_p;
25244 }
25245
25246 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25247 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25248 they must be performed once we are in the scope of the function.
25249
25250 Returns the function defined. */
25251
25252 static tree
25253 cp_parser_function_definition_from_specifiers_and_declarator
25254 (cp_parser* parser,
25255 cp_decl_specifier_seq *decl_specifiers,
25256 tree attributes,
25257 const cp_declarator *declarator)
25258 {
25259 tree fn;
25260 bool success_p;
25261
25262 /* Begin the function-definition. */
25263 success_p = start_function (decl_specifiers, declarator, attributes);
25264
25265 /* The things we're about to see are not directly qualified by any
25266 template headers we've seen thus far. */
25267 reset_specialization ();
25268
25269 /* If there were names looked up in the decl-specifier-seq that we
25270 did not check, check them now. We must wait until we are in the
25271 scope of the function to perform the checks, since the function
25272 might be a friend. */
25273 perform_deferred_access_checks (tf_warning_or_error);
25274
25275 if (success_p)
25276 {
25277 cp_finalize_omp_declare_simd (parser, current_function_decl);
25278 parser->omp_declare_simd = NULL;
25279 cp_finalize_oacc_routine (parser, current_function_decl, true);
25280 parser->oacc_routine = NULL;
25281 }
25282
25283 if (!success_p)
25284 {
25285 /* Skip the entire function. */
25286 cp_parser_skip_to_end_of_block_or_statement (parser);
25287 fn = error_mark_node;
25288 }
25289 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25290 {
25291 /* Seen already, skip it. An error message has already been output. */
25292 cp_parser_skip_to_end_of_block_or_statement (parser);
25293 fn = current_function_decl;
25294 current_function_decl = NULL_TREE;
25295 /* If this is a function from a class, pop the nested class. */
25296 if (current_class_name)
25297 pop_nested_class ();
25298 }
25299 else
25300 {
25301 timevar_id_t tv;
25302 if (DECL_DECLARED_INLINE_P (current_function_decl))
25303 tv = TV_PARSE_INLINE;
25304 else
25305 tv = TV_PARSE_FUNC;
25306 timevar_push (tv);
25307 fn = cp_parser_function_definition_after_declarator (parser,
25308 /*inline_p=*/false);
25309 timevar_pop (tv);
25310 }
25311
25312 return fn;
25313 }
25314
25315 /* Parse the part of a function-definition that follows the
25316 declarator. INLINE_P is TRUE iff this function is an inline
25317 function defined within a class-specifier.
25318
25319 Returns the function defined. */
25320
25321 static tree
25322 cp_parser_function_definition_after_declarator (cp_parser* parser,
25323 bool inline_p)
25324 {
25325 tree fn;
25326 bool ctor_initializer_p = false;
25327 bool saved_in_unbraced_linkage_specification_p;
25328 bool saved_in_function_body;
25329 unsigned saved_num_template_parameter_lists;
25330 cp_token *token;
25331 bool fully_implicit_function_template_p
25332 = parser->fully_implicit_function_template_p;
25333 parser->fully_implicit_function_template_p = false;
25334 tree implicit_template_parms
25335 = parser->implicit_template_parms;
25336 parser->implicit_template_parms = 0;
25337 cp_binding_level* implicit_template_scope
25338 = parser->implicit_template_scope;
25339 parser->implicit_template_scope = 0;
25340
25341 saved_in_function_body = parser->in_function_body;
25342 parser->in_function_body = true;
25343 /* If the next token is `return', then the code may be trying to
25344 make use of the "named return value" extension that G++ used to
25345 support. */
25346 token = cp_lexer_peek_token (parser->lexer);
25347 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25348 {
25349 /* Consume the `return' keyword. */
25350 cp_lexer_consume_token (parser->lexer);
25351 /* Look for the identifier that indicates what value is to be
25352 returned. */
25353 cp_parser_identifier (parser);
25354 /* Issue an error message. */
25355 error_at (token->location,
25356 "named return values are no longer supported");
25357 /* Skip tokens until we reach the start of the function body. */
25358 while (true)
25359 {
25360 cp_token *token = cp_lexer_peek_token (parser->lexer);
25361 if (token->type == CPP_OPEN_BRACE
25362 || token->type == CPP_EOF
25363 || token->type == CPP_PRAGMA_EOL)
25364 break;
25365 cp_lexer_consume_token (parser->lexer);
25366 }
25367 }
25368 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25369 anything declared inside `f'. */
25370 saved_in_unbraced_linkage_specification_p
25371 = parser->in_unbraced_linkage_specification_p;
25372 parser->in_unbraced_linkage_specification_p = false;
25373 /* Inside the function, surrounding template-parameter-lists do not
25374 apply. */
25375 saved_num_template_parameter_lists
25376 = parser->num_template_parameter_lists;
25377 parser->num_template_parameter_lists = 0;
25378
25379 start_lambda_scope (current_function_decl);
25380
25381 /* If the next token is `try', `__transaction_atomic', or
25382 `__transaction_relaxed`, then we are looking at either function-try-block
25383 or function-transaction-block. Note that all of these include the
25384 function-body. */
25385 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25386 ctor_initializer_p = cp_parser_function_transaction (parser,
25387 RID_TRANSACTION_ATOMIC);
25388 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25389 RID_TRANSACTION_RELAXED))
25390 ctor_initializer_p = cp_parser_function_transaction (parser,
25391 RID_TRANSACTION_RELAXED);
25392 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25393 ctor_initializer_p = cp_parser_function_try_block (parser);
25394 else
25395 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25396 (parser, /*in_function_try_block=*/false);
25397
25398 finish_lambda_scope ();
25399
25400 /* Finish the function. */
25401 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25402 (inline_p ? 2 : 0));
25403 /* Generate code for it, if necessary. */
25404 expand_or_defer_fn (fn);
25405 /* Restore the saved values. */
25406 parser->in_unbraced_linkage_specification_p
25407 = saved_in_unbraced_linkage_specification_p;
25408 parser->num_template_parameter_lists
25409 = saved_num_template_parameter_lists;
25410 parser->in_function_body = saved_in_function_body;
25411
25412 parser->fully_implicit_function_template_p
25413 = fully_implicit_function_template_p;
25414 parser->implicit_template_parms
25415 = implicit_template_parms;
25416 parser->implicit_template_scope
25417 = implicit_template_scope;
25418
25419 if (parser->fully_implicit_function_template_p)
25420 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25421
25422 return fn;
25423 }
25424
25425 /* Parse a template-declaration body (following argument list). */
25426
25427 static void
25428 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25429 tree parameter_list,
25430 bool member_p)
25431 {
25432 tree decl = NULL_TREE;
25433 bool friend_p = false;
25434
25435 /* We just processed one more parameter list. */
25436 ++parser->num_template_parameter_lists;
25437
25438 /* Get the deferred access checks from the parameter list. These
25439 will be checked once we know what is being declared, as for a
25440 member template the checks must be performed in the scope of the
25441 class containing the member. */
25442 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25443
25444 /* Tentatively parse for a new template parameter list, which can either be
25445 the template keyword or a template introduction. */
25446 if (cp_parser_template_declaration_after_export (parser, member_p))
25447 /* OK */;
25448 else if (cxx_dialect >= cxx11
25449 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25450 decl = cp_parser_alias_declaration (parser);
25451 else
25452 {
25453 /* There are no access checks when parsing a template, as we do not
25454 know if a specialization will be a friend. */
25455 push_deferring_access_checks (dk_no_check);
25456 cp_token *token = cp_lexer_peek_token (parser->lexer);
25457 decl = cp_parser_single_declaration (parser,
25458 checks,
25459 member_p,
25460 /*explicit_specialization_p=*/false,
25461 &friend_p);
25462 pop_deferring_access_checks ();
25463
25464 /* If this is a member template declaration, let the front
25465 end know. */
25466 if (member_p && !friend_p && decl)
25467 {
25468 if (TREE_CODE (decl) == TYPE_DECL)
25469 cp_parser_check_access_in_redeclaration (decl, token->location);
25470
25471 decl = finish_member_template_decl (decl);
25472 }
25473 else if (friend_p && decl
25474 && DECL_DECLARES_TYPE_P (decl))
25475 make_friend_class (current_class_type, TREE_TYPE (decl),
25476 /*complain=*/true);
25477 }
25478 /* We are done with the current parameter list. */
25479 --parser->num_template_parameter_lists;
25480
25481 pop_deferring_access_checks ();
25482
25483 /* Finish up. */
25484 finish_template_decl (parameter_list);
25485
25486 /* Check the template arguments for a literal operator template. */
25487 if (decl
25488 && DECL_DECLARES_FUNCTION_P (decl)
25489 && UDLIT_OPER_P (DECL_NAME (decl)))
25490 {
25491 bool ok = true;
25492 if (parameter_list == NULL_TREE)
25493 ok = false;
25494 else
25495 {
25496 int num_parms = TREE_VEC_LENGTH (parameter_list);
25497 if (num_parms == 1)
25498 {
25499 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25500 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25501 if (TREE_TYPE (parm) != char_type_node
25502 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25503 ok = false;
25504 }
25505 else if (num_parms == 2 && cxx_dialect >= cxx14)
25506 {
25507 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25508 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25509 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25510 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25511 if (TREE_TYPE (parm) != TREE_TYPE (type)
25512 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25513 ok = false;
25514 }
25515 else
25516 ok = false;
25517 }
25518 if (!ok)
25519 {
25520 if (cxx_dialect >= cxx14)
25521 error ("literal operator template %qD has invalid parameter list."
25522 " Expected non-type template argument pack <char...>"
25523 " or <typename CharT, CharT...>",
25524 decl);
25525 else
25526 error ("literal operator template %qD has invalid parameter list."
25527 " Expected non-type template argument pack <char...>",
25528 decl);
25529 }
25530 }
25531
25532 /* Register member declarations. */
25533 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25534 finish_member_declaration (decl);
25535 /* If DECL is a function template, we must return to parse it later.
25536 (Even though there is no definition, there might be default
25537 arguments that need handling.) */
25538 if (member_p && decl
25539 && DECL_DECLARES_FUNCTION_P (decl))
25540 vec_safe_push (unparsed_funs_with_definitions, decl);
25541 }
25542
25543 /* Parse a template introduction header for a template-declaration. Returns
25544 false if tentative parse fails. */
25545
25546 static bool
25547 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25548 {
25549 cp_parser_parse_tentatively (parser);
25550
25551 tree saved_scope = parser->scope;
25552 tree saved_object_scope = parser->object_scope;
25553 tree saved_qualifying_scope = parser->qualifying_scope;
25554
25555 /* Look for the optional `::' operator. */
25556 cp_parser_global_scope_opt (parser,
25557 /*current_scope_valid_p=*/false);
25558 /* Look for the nested-name-specifier. */
25559 cp_parser_nested_name_specifier_opt (parser,
25560 /*typename_keyword_p=*/false,
25561 /*check_dependency_p=*/true,
25562 /*type_p=*/false,
25563 /*is_declaration=*/false);
25564
25565 cp_token *token = cp_lexer_peek_token (parser->lexer);
25566 tree concept_name = cp_parser_identifier (parser);
25567
25568 /* Look up the concept for which we will be matching
25569 template parameters. */
25570 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25571 token->location);
25572 parser->scope = saved_scope;
25573 parser->object_scope = saved_object_scope;
25574 parser->qualifying_scope = saved_qualifying_scope;
25575
25576 if (concept_name == error_mark_node)
25577 cp_parser_simulate_error (parser);
25578
25579 /* Look for opening brace for introduction. */
25580 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25581
25582 if (!cp_parser_parse_definitely (parser))
25583 return false;
25584
25585 push_deferring_access_checks (dk_deferred);
25586
25587 /* Build vector of placeholder parameters and grab
25588 matching identifiers. */
25589 tree introduction_list = cp_parser_introduction_list (parser);
25590
25591 /* The introduction-list shall not be empty. */
25592 int nargs = TREE_VEC_LENGTH (introduction_list);
25593 if (nargs == 0)
25594 {
25595 error ("empty introduction-list");
25596 return true;
25597 }
25598
25599 /* Look for closing brace for introduction. */
25600 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25601 return true;
25602
25603 if (tmpl_decl == error_mark_node)
25604 {
25605 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25606 token->location);
25607 return true;
25608 }
25609
25610 /* Build and associate the constraint. */
25611 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25612 if (parms && parms != error_mark_node)
25613 {
25614 cp_parser_template_declaration_after_parameters (parser, parms,
25615 member_p);
25616 return true;
25617 }
25618
25619 error_at (token->location, "no matching concept for template-introduction");
25620 return true;
25621 }
25622
25623 /* Parse a normal template-declaration following the template keyword. */
25624
25625 static void
25626 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25627 {
25628 tree parameter_list;
25629 bool need_lang_pop;
25630 location_t location = input_location;
25631
25632 /* Look for the `<' token. */
25633 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25634 return;
25635 if (at_class_scope_p () && current_function_decl)
25636 {
25637 /* 14.5.2.2 [temp.mem]
25638
25639 A local class shall not have member templates. */
25640 error_at (location,
25641 "invalid declaration of member template in local class");
25642 cp_parser_skip_to_end_of_block_or_statement (parser);
25643 return;
25644 }
25645 /* [temp]
25646
25647 A template ... shall not have C linkage. */
25648 if (current_lang_name == lang_name_c)
25649 {
25650 error_at (location, "template with C linkage");
25651 /* Give it C++ linkage to avoid confusing other parts of the
25652 front end. */
25653 push_lang_context (lang_name_cplusplus);
25654 need_lang_pop = true;
25655 }
25656 else
25657 need_lang_pop = false;
25658
25659 /* We cannot perform access checks on the template parameter
25660 declarations until we know what is being declared, just as we
25661 cannot check the decl-specifier list. */
25662 push_deferring_access_checks (dk_deferred);
25663
25664 /* If the next token is `>', then we have an invalid
25665 specialization. Rather than complain about an invalid template
25666 parameter, issue an error message here. */
25667 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25668 {
25669 cp_parser_error (parser, "invalid explicit specialization");
25670 begin_specialization ();
25671 parameter_list = NULL_TREE;
25672 }
25673 else
25674 {
25675 /* Parse the template parameters. */
25676 parameter_list = cp_parser_template_parameter_list (parser);
25677 }
25678
25679 /* Look for the `>'. */
25680 cp_parser_skip_to_end_of_template_parameter_list (parser);
25681
25682 /* Manage template requirements */
25683 tree reqs = get_shorthand_constraints (current_template_parms);
25684 if (tree r = cp_parser_requires_clause_opt (parser))
25685 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25686 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25687
25688 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25689 member_p);
25690
25691 /* For the erroneous case of a template with C linkage, we pushed an
25692 implicit C++ linkage scope; exit that scope now. */
25693 if (need_lang_pop)
25694 pop_lang_context ();
25695 }
25696
25697 /* Parse a template-declaration, assuming that the `export' (and
25698 `extern') keywords, if present, has already been scanned. MEMBER_P
25699 is as for cp_parser_template_declaration. */
25700
25701 static bool
25702 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25703 {
25704 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25705 {
25706 cp_lexer_consume_token (parser->lexer);
25707 cp_parser_explicit_template_declaration (parser, member_p);
25708 return true;
25709 }
25710 else if (flag_concepts)
25711 return cp_parser_template_introduction (parser, member_p);
25712
25713 return false;
25714 }
25715
25716 /* Perform the deferred access checks from a template-parameter-list.
25717 CHECKS is a TREE_LIST of access checks, as returned by
25718 get_deferred_access_checks. */
25719
25720 static void
25721 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25722 {
25723 ++processing_template_parmlist;
25724 perform_access_checks (checks, tf_warning_or_error);
25725 --processing_template_parmlist;
25726 }
25727
25728 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25729 `function-definition' sequence that follows a template header.
25730 If MEMBER_P is true, this declaration appears in a class scope.
25731
25732 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25733 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25734
25735 static tree
25736 cp_parser_single_declaration (cp_parser* parser,
25737 vec<deferred_access_check, va_gc> *checks,
25738 bool member_p,
25739 bool explicit_specialization_p,
25740 bool* friend_p)
25741 {
25742 int declares_class_or_enum;
25743 tree decl = NULL_TREE;
25744 cp_decl_specifier_seq decl_specifiers;
25745 bool function_definition_p = false;
25746 cp_token *decl_spec_token_start;
25747
25748 /* This function is only used when processing a template
25749 declaration. */
25750 gcc_assert (innermost_scope_kind () == sk_template_parms
25751 || innermost_scope_kind () == sk_template_spec);
25752
25753 /* Defer access checks until we know what is being declared. */
25754 push_deferring_access_checks (dk_deferred);
25755
25756 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25757 alternative. */
25758 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25759 cp_parser_decl_specifier_seq (parser,
25760 CP_PARSER_FLAGS_OPTIONAL,
25761 &decl_specifiers,
25762 &declares_class_or_enum);
25763 if (friend_p)
25764 *friend_p = cp_parser_friend_p (&decl_specifiers);
25765
25766 /* There are no template typedefs. */
25767 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25768 {
25769 error_at (decl_spec_token_start->location,
25770 "template declaration of %<typedef%>");
25771 decl = error_mark_node;
25772 }
25773
25774 /* Gather up the access checks that occurred the
25775 decl-specifier-seq. */
25776 stop_deferring_access_checks ();
25777
25778 /* Check for the declaration of a template class. */
25779 if (declares_class_or_enum)
25780 {
25781 if (cp_parser_declares_only_class_p (parser)
25782 || (declares_class_or_enum & 2))
25783 {
25784 // If this is a declaration, but not a definition, associate
25785 // any constraints with the type declaration. Constraints
25786 // are associated with definitions in cp_parser_class_specifier.
25787 if (declares_class_or_enum == 1)
25788 associate_classtype_constraints (decl_specifiers.type);
25789
25790 decl = shadow_tag (&decl_specifiers);
25791
25792 /* In this case:
25793
25794 struct C {
25795 friend template <typename T> struct A<T>::B;
25796 };
25797
25798 A<T>::B will be represented by a TYPENAME_TYPE, and
25799 therefore not recognized by shadow_tag. */
25800 if (friend_p && *friend_p
25801 && !decl
25802 && decl_specifiers.type
25803 && TYPE_P (decl_specifiers.type))
25804 decl = decl_specifiers.type;
25805
25806 if (decl && decl != error_mark_node)
25807 decl = TYPE_NAME (decl);
25808 else
25809 decl = error_mark_node;
25810
25811 /* Perform access checks for template parameters. */
25812 cp_parser_perform_template_parameter_access_checks (checks);
25813
25814 /* Give a helpful diagnostic for
25815 template <class T> struct A { } a;
25816 if we aren't already recovering from an error. */
25817 if (!cp_parser_declares_only_class_p (parser)
25818 && !seen_error ())
25819 {
25820 error_at (cp_lexer_peek_token (parser->lexer)->location,
25821 "a class template declaration must not declare "
25822 "anything else");
25823 cp_parser_skip_to_end_of_block_or_statement (parser);
25824 goto out;
25825 }
25826 }
25827 }
25828
25829 /* Complain about missing 'typename' or other invalid type names. */
25830 if (!decl_specifiers.any_type_specifiers_p
25831 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25832 {
25833 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25834 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25835 the rest of this declaration. */
25836 decl = error_mark_node;
25837 goto out;
25838 }
25839
25840 /* If it's not a template class, try for a template function. If
25841 the next token is a `;', then this declaration does not declare
25842 anything. But, if there were errors in the decl-specifiers, then
25843 the error might well have come from an attempted class-specifier.
25844 In that case, there's no need to warn about a missing declarator. */
25845 if (!decl
25846 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25847 || decl_specifiers.type != error_mark_node))
25848 {
25849 decl = cp_parser_init_declarator (parser,
25850 &decl_specifiers,
25851 checks,
25852 /*function_definition_allowed_p=*/true,
25853 member_p,
25854 declares_class_or_enum,
25855 &function_definition_p,
25856 NULL, NULL, NULL);
25857
25858 /* 7.1.1-1 [dcl.stc]
25859
25860 A storage-class-specifier shall not be specified in an explicit
25861 specialization... */
25862 if (decl
25863 && explicit_specialization_p
25864 && decl_specifiers.storage_class != sc_none)
25865 {
25866 error_at (decl_spec_token_start->location,
25867 "explicit template specialization cannot have a storage class");
25868 decl = error_mark_node;
25869 }
25870
25871 if (decl && VAR_P (decl))
25872 check_template_variable (decl);
25873 }
25874
25875 /* Look for a trailing `;' after the declaration. */
25876 if (!function_definition_p
25877 && (decl == error_mark_node
25878 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25879 cp_parser_skip_to_end_of_block_or_statement (parser);
25880
25881 out:
25882 pop_deferring_access_checks ();
25883
25884 /* Clear any current qualification; whatever comes next is the start
25885 of something new. */
25886 parser->scope = NULL_TREE;
25887 parser->qualifying_scope = NULL_TREE;
25888 parser->object_scope = NULL_TREE;
25889
25890 return decl;
25891 }
25892
25893 /* Parse a cast-expression that is not the operand of a unary "&". */
25894
25895 static cp_expr
25896 cp_parser_simple_cast_expression (cp_parser *parser)
25897 {
25898 return cp_parser_cast_expression (parser, /*address_p=*/false,
25899 /*cast_p=*/false, /*decltype*/false, NULL);
25900 }
25901
25902 /* Parse a functional cast to TYPE. Returns an expression
25903 representing the cast. */
25904
25905 static cp_expr
25906 cp_parser_functional_cast (cp_parser* parser, tree type)
25907 {
25908 vec<tree, va_gc> *vec;
25909 tree expression_list;
25910 cp_expr cast;
25911 bool nonconst_p;
25912
25913 location_t start_loc = input_location;
25914
25915 if (!type)
25916 type = error_mark_node;
25917
25918 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25919 {
25920 cp_lexer_set_source_position (parser->lexer);
25921 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25922 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25923 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25924 if (TREE_CODE (type) == TYPE_DECL)
25925 type = TREE_TYPE (type);
25926
25927 cast = finish_compound_literal (type, expression_list,
25928 tf_warning_or_error);
25929 /* Create a location of the form:
25930 type_name{i, f}
25931 ^~~~~~~~~~~~~~~
25932 with caret == start at the start of the type name,
25933 finishing at the closing brace. */
25934 location_t finish_loc
25935 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25936 location_t combined_loc = make_location (start_loc, start_loc,
25937 finish_loc);
25938 cast.set_location (combined_loc);
25939 return cast;
25940 }
25941
25942
25943 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25944 /*cast_p=*/true,
25945 /*allow_expansion_p=*/true,
25946 /*non_constant_p=*/NULL);
25947 if (vec == NULL)
25948 expression_list = error_mark_node;
25949 else
25950 {
25951 expression_list = build_tree_list_vec (vec);
25952 release_tree_vector (vec);
25953 }
25954
25955 cast = build_functional_cast (type, expression_list,
25956 tf_warning_or_error);
25957 /* [expr.const]/1: In an integral constant expression "only type
25958 conversions to integral or enumeration type can be used". */
25959 if (TREE_CODE (type) == TYPE_DECL)
25960 type = TREE_TYPE (type);
25961 if (cast != error_mark_node
25962 && !cast_valid_in_integral_constant_expression_p (type)
25963 && cp_parser_non_integral_constant_expression (parser,
25964 NIC_CONSTRUCTOR))
25965 return error_mark_node;
25966
25967 /* Create a location of the form:
25968 float(i)
25969 ^~~~~~~~
25970 with caret == start at the start of the type name,
25971 finishing at the closing paren. */
25972 location_t finish_loc
25973 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25974 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25975 cast.set_location (combined_loc);
25976 return cast;
25977 }
25978
25979 /* Save the tokens that make up the body of a member function defined
25980 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25981 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25982 specifiers applied to the declaration. Returns the FUNCTION_DECL
25983 for the member function. */
25984
25985 static tree
25986 cp_parser_save_member_function_body (cp_parser* parser,
25987 cp_decl_specifier_seq *decl_specifiers,
25988 cp_declarator *declarator,
25989 tree attributes)
25990 {
25991 cp_token *first;
25992 cp_token *last;
25993 tree fn;
25994
25995 /* Create the FUNCTION_DECL. */
25996 fn = grokmethod (decl_specifiers, declarator, attributes);
25997 cp_finalize_omp_declare_simd (parser, fn);
25998 cp_finalize_oacc_routine (parser, fn, true);
25999 /* If something went badly wrong, bail out now. */
26000 if (fn == error_mark_node)
26001 {
26002 /* If there's a function-body, skip it. */
26003 if (cp_parser_token_starts_function_definition_p
26004 (cp_lexer_peek_token (parser->lexer)))
26005 cp_parser_skip_to_end_of_block_or_statement (parser);
26006 return error_mark_node;
26007 }
26008
26009 /* Remember it, if there default args to post process. */
26010 cp_parser_save_default_args (parser, fn);
26011
26012 /* Save away the tokens that make up the body of the
26013 function. */
26014 first = parser->lexer->next_token;
26015 /* Handle function try blocks. */
26016 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26017 cp_lexer_consume_token (parser->lexer);
26018 /* We can have braced-init-list mem-initializers before the fn body. */
26019 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26020 {
26021 cp_lexer_consume_token (parser->lexer);
26022 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26023 {
26024 /* cache_group will stop after an un-nested { } pair, too. */
26025 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26026 break;
26027
26028 /* variadic mem-inits have ... after the ')'. */
26029 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26030 cp_lexer_consume_token (parser->lexer);
26031 }
26032 }
26033 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26034 /* Handle function try blocks. */
26035 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26036 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26037 last = parser->lexer->next_token;
26038
26039 /* Save away the inline definition; we will process it when the
26040 class is complete. */
26041 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26042 DECL_PENDING_INLINE_P (fn) = 1;
26043
26044 /* We need to know that this was defined in the class, so that
26045 friend templates are handled correctly. */
26046 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26047
26048 /* Add FN to the queue of functions to be parsed later. */
26049 vec_safe_push (unparsed_funs_with_definitions, fn);
26050
26051 return fn;
26052 }
26053
26054 /* Save the tokens that make up the in-class initializer for a non-static
26055 data member. Returns a DEFAULT_ARG. */
26056
26057 static tree
26058 cp_parser_save_nsdmi (cp_parser* parser)
26059 {
26060 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26061 }
26062
26063 /* Parse a template-argument-list, as well as the trailing ">" (but
26064 not the opening "<"). See cp_parser_template_argument_list for the
26065 return value. */
26066
26067 static tree
26068 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26069 {
26070 tree arguments;
26071 tree saved_scope;
26072 tree saved_qualifying_scope;
26073 tree saved_object_scope;
26074 bool saved_greater_than_is_operator_p;
26075 int saved_unevaluated_operand;
26076 int saved_inhibit_evaluation_warnings;
26077
26078 /* [temp.names]
26079
26080 When parsing a template-id, the first non-nested `>' is taken as
26081 the end of the template-argument-list rather than a greater-than
26082 operator. */
26083 saved_greater_than_is_operator_p
26084 = parser->greater_than_is_operator_p;
26085 parser->greater_than_is_operator_p = false;
26086 /* Parsing the argument list may modify SCOPE, so we save it
26087 here. */
26088 saved_scope = parser->scope;
26089 saved_qualifying_scope = parser->qualifying_scope;
26090 saved_object_scope = parser->object_scope;
26091 /* We need to evaluate the template arguments, even though this
26092 template-id may be nested within a "sizeof". */
26093 saved_unevaluated_operand = cp_unevaluated_operand;
26094 cp_unevaluated_operand = 0;
26095 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26096 c_inhibit_evaluation_warnings = 0;
26097 /* Parse the template-argument-list itself. */
26098 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26099 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26100 arguments = NULL_TREE;
26101 else
26102 arguments = cp_parser_template_argument_list (parser);
26103 /* Look for the `>' that ends the template-argument-list. If we find
26104 a '>>' instead, it's probably just a typo. */
26105 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26106 {
26107 if (cxx_dialect != cxx98)
26108 {
26109 /* In C++0x, a `>>' in a template argument list or cast
26110 expression is considered to be two separate `>'
26111 tokens. So, change the current token to a `>', but don't
26112 consume it: it will be consumed later when the outer
26113 template argument list (or cast expression) is parsed.
26114 Note that this replacement of `>' for `>>' is necessary
26115 even if we are parsing tentatively: in the tentative
26116 case, after calling
26117 cp_parser_enclosed_template_argument_list we will always
26118 throw away all of the template arguments and the first
26119 closing `>', either because the template argument list
26120 was erroneous or because we are replacing those tokens
26121 with a CPP_TEMPLATE_ID token. The second `>' (which will
26122 not have been thrown away) is needed either to close an
26123 outer template argument list or to complete a new-style
26124 cast. */
26125 cp_token *token = cp_lexer_peek_token (parser->lexer);
26126 token->type = CPP_GREATER;
26127 }
26128 else if (!saved_greater_than_is_operator_p)
26129 {
26130 /* If we're in a nested template argument list, the '>>' has
26131 to be a typo for '> >'. We emit the error message, but we
26132 continue parsing and we push a '>' as next token, so that
26133 the argument list will be parsed correctly. Note that the
26134 global source location is still on the token before the
26135 '>>', so we need to say explicitly where we want it. */
26136 cp_token *token = cp_lexer_peek_token (parser->lexer);
26137 error_at (token->location, "%<>>%> should be %<> >%> "
26138 "within a nested template argument list");
26139
26140 token->type = CPP_GREATER;
26141 }
26142 else
26143 {
26144 /* If this is not a nested template argument list, the '>>'
26145 is a typo for '>'. Emit an error message and continue.
26146 Same deal about the token location, but here we can get it
26147 right by consuming the '>>' before issuing the diagnostic. */
26148 cp_token *token = cp_lexer_consume_token (parser->lexer);
26149 error_at (token->location,
26150 "spurious %<>>%>, use %<>%> to terminate "
26151 "a template argument list");
26152 }
26153 }
26154 else
26155 cp_parser_skip_to_end_of_template_parameter_list (parser);
26156 /* The `>' token might be a greater-than operator again now. */
26157 parser->greater_than_is_operator_p
26158 = saved_greater_than_is_operator_p;
26159 /* Restore the SAVED_SCOPE. */
26160 parser->scope = saved_scope;
26161 parser->qualifying_scope = saved_qualifying_scope;
26162 parser->object_scope = saved_object_scope;
26163 cp_unevaluated_operand = saved_unevaluated_operand;
26164 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26165
26166 return arguments;
26167 }
26168
26169 /* MEMBER_FUNCTION is a member function, or a friend. If default
26170 arguments, or the body of the function have not yet been parsed,
26171 parse them now. */
26172
26173 static void
26174 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26175 {
26176 timevar_push (TV_PARSE_INMETH);
26177 /* If this member is a template, get the underlying
26178 FUNCTION_DECL. */
26179 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26180 member_function = DECL_TEMPLATE_RESULT (member_function);
26181
26182 /* There should not be any class definitions in progress at this
26183 point; the bodies of members are only parsed outside of all class
26184 definitions. */
26185 gcc_assert (parser->num_classes_being_defined == 0);
26186 /* While we're parsing the member functions we might encounter more
26187 classes. We want to handle them right away, but we don't want
26188 them getting mixed up with functions that are currently in the
26189 queue. */
26190 push_unparsed_function_queues (parser);
26191
26192 /* Make sure that any template parameters are in scope. */
26193 maybe_begin_member_template_processing (member_function);
26194
26195 /* If the body of the function has not yet been parsed, parse it
26196 now. */
26197 if (DECL_PENDING_INLINE_P (member_function))
26198 {
26199 tree function_scope;
26200 cp_token_cache *tokens;
26201
26202 /* The function is no longer pending; we are processing it. */
26203 tokens = DECL_PENDING_INLINE_INFO (member_function);
26204 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26205 DECL_PENDING_INLINE_P (member_function) = 0;
26206
26207 /* If this is a local class, enter the scope of the containing
26208 function. */
26209 function_scope = current_function_decl;
26210 if (function_scope)
26211 push_function_context ();
26212
26213 /* Push the body of the function onto the lexer stack. */
26214 cp_parser_push_lexer_for_tokens (parser, tokens);
26215
26216 /* Let the front end know that we going to be defining this
26217 function. */
26218 start_preparsed_function (member_function, NULL_TREE,
26219 SF_PRE_PARSED | SF_INCLASS_INLINE);
26220
26221 /* Don't do access checking if it is a templated function. */
26222 if (processing_template_decl)
26223 push_deferring_access_checks (dk_no_check);
26224
26225 /* #pragma omp declare reduction needs special parsing. */
26226 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26227 {
26228 parser->lexer->in_pragma = true;
26229 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26230 finish_function (/*inline*/2);
26231 cp_check_omp_declare_reduction (member_function);
26232 }
26233 else
26234 /* Now, parse the body of the function. */
26235 cp_parser_function_definition_after_declarator (parser,
26236 /*inline_p=*/true);
26237
26238 if (processing_template_decl)
26239 pop_deferring_access_checks ();
26240
26241 /* Leave the scope of the containing function. */
26242 if (function_scope)
26243 pop_function_context ();
26244 cp_parser_pop_lexer (parser);
26245 }
26246
26247 /* Remove any template parameters from the symbol table. */
26248 maybe_end_member_template_processing ();
26249
26250 /* Restore the queue. */
26251 pop_unparsed_function_queues (parser);
26252 timevar_pop (TV_PARSE_INMETH);
26253 }
26254
26255 /* If DECL contains any default args, remember it on the unparsed
26256 functions queue. */
26257
26258 static void
26259 cp_parser_save_default_args (cp_parser* parser, tree decl)
26260 {
26261 tree probe;
26262
26263 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26264 probe;
26265 probe = TREE_CHAIN (probe))
26266 if (TREE_PURPOSE (probe))
26267 {
26268 cp_default_arg_entry entry = {current_class_type, decl};
26269 vec_safe_push (unparsed_funs_with_default_args, entry);
26270 break;
26271 }
26272 }
26273
26274 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26275 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26276 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26277 from the parameter-type-list. */
26278
26279 static tree
26280 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26281 tree default_arg, tree parmtype)
26282 {
26283 cp_token_cache *tokens;
26284 tree parsed_arg;
26285 bool dummy;
26286
26287 if (default_arg == error_mark_node)
26288 return error_mark_node;
26289
26290 /* Push the saved tokens for the default argument onto the parser's
26291 lexer stack. */
26292 tokens = DEFARG_TOKENS (default_arg);
26293 cp_parser_push_lexer_for_tokens (parser, tokens);
26294
26295 start_lambda_scope (decl);
26296
26297 /* Parse the default argument. */
26298 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26299 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26300 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26301
26302 finish_lambda_scope ();
26303
26304 if (parsed_arg == error_mark_node)
26305 cp_parser_skip_to_end_of_statement (parser);
26306
26307 if (!processing_template_decl)
26308 {
26309 /* In a non-template class, check conversions now. In a template,
26310 we'll wait and instantiate these as needed. */
26311 if (TREE_CODE (decl) == PARM_DECL)
26312 parsed_arg = check_default_argument (parmtype, parsed_arg,
26313 tf_warning_or_error);
26314 else
26315 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26316 }
26317
26318 /* If the token stream has not been completely used up, then
26319 there was extra junk after the end of the default
26320 argument. */
26321 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26322 {
26323 if (TREE_CODE (decl) == PARM_DECL)
26324 cp_parser_error (parser, "expected %<,%>");
26325 else
26326 cp_parser_error (parser, "expected %<;%>");
26327 }
26328
26329 /* Revert to the main lexer. */
26330 cp_parser_pop_lexer (parser);
26331
26332 return parsed_arg;
26333 }
26334
26335 /* FIELD is a non-static data member with an initializer which we saved for
26336 later; parse it now. */
26337
26338 static void
26339 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26340 {
26341 tree def;
26342
26343 maybe_begin_member_template_processing (field);
26344
26345 push_unparsed_function_queues (parser);
26346 def = cp_parser_late_parse_one_default_arg (parser, field,
26347 DECL_INITIAL (field),
26348 NULL_TREE);
26349 pop_unparsed_function_queues (parser);
26350
26351 maybe_end_member_template_processing ();
26352
26353 DECL_INITIAL (field) = def;
26354 }
26355
26356 /* FN is a FUNCTION_DECL which may contains a parameter with an
26357 unparsed DEFAULT_ARG. Parse the default args now. This function
26358 assumes that the current scope is the scope in which the default
26359 argument should be processed. */
26360
26361 static void
26362 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26363 {
26364 bool saved_local_variables_forbidden_p;
26365 tree parm, parmdecl;
26366
26367 /* While we're parsing the default args, we might (due to the
26368 statement expression extension) encounter more classes. We want
26369 to handle them right away, but we don't want them getting mixed
26370 up with default args that are currently in the queue. */
26371 push_unparsed_function_queues (parser);
26372
26373 /* Local variable names (and the `this' keyword) may not appear
26374 in a default argument. */
26375 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26376 parser->local_variables_forbidden_p = true;
26377
26378 push_defarg_context (fn);
26379
26380 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26381 parmdecl = DECL_ARGUMENTS (fn);
26382 parm && parm != void_list_node;
26383 parm = TREE_CHAIN (parm),
26384 parmdecl = DECL_CHAIN (parmdecl))
26385 {
26386 tree default_arg = TREE_PURPOSE (parm);
26387 tree parsed_arg;
26388 vec<tree, va_gc> *insts;
26389 tree copy;
26390 unsigned ix;
26391
26392 if (!default_arg)
26393 continue;
26394
26395 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26396 /* This can happen for a friend declaration for a function
26397 already declared with default arguments. */
26398 continue;
26399
26400 parsed_arg
26401 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26402 default_arg,
26403 TREE_VALUE (parm));
26404 if (parsed_arg == error_mark_node)
26405 {
26406 continue;
26407 }
26408
26409 TREE_PURPOSE (parm) = parsed_arg;
26410
26411 /* Update any instantiations we've already created. */
26412 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26413 vec_safe_iterate (insts, ix, &copy); ix++)
26414 TREE_PURPOSE (copy) = parsed_arg;
26415 }
26416
26417 pop_defarg_context ();
26418
26419 /* Make sure no default arg is missing. */
26420 check_default_args (fn);
26421
26422 /* Restore the state of local_variables_forbidden_p. */
26423 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26424
26425 /* Restore the queue. */
26426 pop_unparsed_function_queues (parser);
26427 }
26428
26429 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26430
26431 sizeof ... ( identifier )
26432
26433 where the 'sizeof' token has already been consumed. */
26434
26435 static tree
26436 cp_parser_sizeof_pack (cp_parser *parser)
26437 {
26438 /* Consume the `...'. */
26439 cp_lexer_consume_token (parser->lexer);
26440 maybe_warn_variadic_templates ();
26441
26442 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26443 if (paren)
26444 cp_lexer_consume_token (parser->lexer);
26445 else
26446 permerror (cp_lexer_peek_token (parser->lexer)->location,
26447 "%<sizeof...%> argument must be surrounded by parentheses");
26448
26449 cp_token *token = cp_lexer_peek_token (parser->lexer);
26450 tree name = cp_parser_identifier (parser);
26451 if (name == error_mark_node)
26452 return error_mark_node;
26453 /* The name is not qualified. */
26454 parser->scope = NULL_TREE;
26455 parser->qualifying_scope = NULL_TREE;
26456 parser->object_scope = NULL_TREE;
26457 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26458 if (expr == error_mark_node)
26459 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26460 token->location);
26461 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26462 expr = TREE_TYPE (expr);
26463 else if (TREE_CODE (expr) == CONST_DECL)
26464 expr = DECL_INITIAL (expr);
26465 expr = make_pack_expansion (expr);
26466 PACK_EXPANSION_SIZEOF_P (expr) = true;
26467
26468 if (paren)
26469 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26470
26471 return expr;
26472 }
26473
26474 /* Parse the operand of `sizeof' (or a similar operator). Returns
26475 either a TYPE or an expression, depending on the form of the
26476 input. The KEYWORD indicates which kind of expression we have
26477 encountered. */
26478
26479 static tree
26480 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26481 {
26482 tree expr = NULL_TREE;
26483 const char *saved_message;
26484 char *tmp;
26485 bool saved_integral_constant_expression_p;
26486 bool saved_non_integral_constant_expression_p;
26487
26488 /* If it's a `...', then we are computing the length of a parameter
26489 pack. */
26490 if (keyword == RID_SIZEOF
26491 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26492 return cp_parser_sizeof_pack (parser);
26493
26494 /* Types cannot be defined in a `sizeof' expression. Save away the
26495 old message. */
26496 saved_message = parser->type_definition_forbidden_message;
26497 /* And create the new one. */
26498 tmp = concat ("types may not be defined in %<",
26499 IDENTIFIER_POINTER (ridpointers[keyword]),
26500 "%> expressions", NULL);
26501 parser->type_definition_forbidden_message = tmp;
26502
26503 /* The restrictions on constant-expressions do not apply inside
26504 sizeof expressions. */
26505 saved_integral_constant_expression_p
26506 = parser->integral_constant_expression_p;
26507 saved_non_integral_constant_expression_p
26508 = parser->non_integral_constant_expression_p;
26509 parser->integral_constant_expression_p = false;
26510
26511 /* Do not actually evaluate the expression. */
26512 ++cp_unevaluated_operand;
26513 ++c_inhibit_evaluation_warnings;
26514 /* If it's a `(', then we might be looking at the type-id
26515 construction. */
26516 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26517 {
26518 tree type = NULL_TREE;
26519
26520 /* We can't be sure yet whether we're looking at a type-id or an
26521 expression. */
26522 cp_parser_parse_tentatively (parser);
26523 /* Note: as a GNU Extension, compound literals are considered
26524 postfix-expressions as they are in C99, so they are valid
26525 arguments to sizeof. See comment in cp_parser_cast_expression
26526 for details. */
26527 if (cp_parser_compound_literal_p (parser))
26528 cp_parser_simulate_error (parser);
26529 else
26530 {
26531 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26532 parser->in_type_id_in_expr_p = true;
26533 /* Look for the type-id. */
26534 type = cp_parser_type_id (parser);
26535 /* Look for the closing `)'. */
26536 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26537 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26538 }
26539
26540 /* If all went well, then we're done. */
26541 if (cp_parser_parse_definitely (parser))
26542 {
26543 cp_decl_specifier_seq decl_specs;
26544
26545 /* Build a trivial decl-specifier-seq. */
26546 clear_decl_specs (&decl_specs);
26547 decl_specs.type = type;
26548
26549 /* Call grokdeclarator to figure out what type this is. */
26550 expr = grokdeclarator (NULL,
26551 &decl_specs,
26552 TYPENAME,
26553 /*initialized=*/0,
26554 /*attrlist=*/NULL);
26555 }
26556 }
26557
26558 /* If the type-id production did not work out, then we must be
26559 looking at the unary-expression production. */
26560 if (!expr)
26561 expr = cp_parser_unary_expression (parser);
26562
26563 /* Go back to evaluating expressions. */
26564 --cp_unevaluated_operand;
26565 --c_inhibit_evaluation_warnings;
26566
26567 /* Free the message we created. */
26568 free (tmp);
26569 /* And restore the old one. */
26570 parser->type_definition_forbidden_message = saved_message;
26571 parser->integral_constant_expression_p
26572 = saved_integral_constant_expression_p;
26573 parser->non_integral_constant_expression_p
26574 = saved_non_integral_constant_expression_p;
26575
26576 return expr;
26577 }
26578
26579 /* If the current declaration has no declarator, return true. */
26580
26581 static bool
26582 cp_parser_declares_only_class_p (cp_parser *parser)
26583 {
26584 /* If the next token is a `;' or a `,' then there is no
26585 declarator. */
26586 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26587 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26588 }
26589
26590 /* Update the DECL_SPECS to reflect the storage class indicated by
26591 KEYWORD. */
26592
26593 static void
26594 cp_parser_set_storage_class (cp_parser *parser,
26595 cp_decl_specifier_seq *decl_specs,
26596 enum rid keyword,
26597 cp_token *token)
26598 {
26599 cp_storage_class storage_class;
26600
26601 if (parser->in_unbraced_linkage_specification_p)
26602 {
26603 error_at (token->location, "invalid use of %qD in linkage specification",
26604 ridpointers[keyword]);
26605 return;
26606 }
26607 else if (decl_specs->storage_class != sc_none)
26608 {
26609 decl_specs->conflicting_specifiers_p = true;
26610 return;
26611 }
26612
26613 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26614 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26615 && decl_specs->gnu_thread_keyword_p)
26616 {
26617 pedwarn (decl_specs->locations[ds_thread], 0,
26618 "%<__thread%> before %qD", ridpointers[keyword]);
26619 }
26620
26621 switch (keyword)
26622 {
26623 case RID_AUTO:
26624 storage_class = sc_auto;
26625 break;
26626 case RID_REGISTER:
26627 storage_class = sc_register;
26628 break;
26629 case RID_STATIC:
26630 storage_class = sc_static;
26631 break;
26632 case RID_EXTERN:
26633 storage_class = sc_extern;
26634 break;
26635 case RID_MUTABLE:
26636 storage_class = sc_mutable;
26637 break;
26638 default:
26639 gcc_unreachable ();
26640 }
26641 decl_specs->storage_class = storage_class;
26642 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26643
26644 /* A storage class specifier cannot be applied alongside a typedef
26645 specifier. If there is a typedef specifier present then set
26646 conflicting_specifiers_p which will trigger an error later
26647 on in grokdeclarator. */
26648 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26649 decl_specs->conflicting_specifiers_p = true;
26650 }
26651
26652 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26653 is true, the type is a class or enum definition. */
26654
26655 static void
26656 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26657 tree type_spec,
26658 cp_token *token,
26659 bool type_definition_p)
26660 {
26661 decl_specs->any_specifiers_p = true;
26662
26663 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26664 (with, for example, in "typedef int wchar_t;") we remember that
26665 this is what happened. In system headers, we ignore these
26666 declarations so that G++ can work with system headers that are not
26667 C++-safe. */
26668 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26669 && !type_definition_p
26670 && (type_spec == boolean_type_node
26671 || type_spec == char16_type_node
26672 || type_spec == char32_type_node
26673 || type_spec == wchar_type_node)
26674 && (decl_specs->type
26675 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26676 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26677 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26678 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26679 {
26680 decl_specs->redefined_builtin_type = type_spec;
26681 set_and_check_decl_spec_loc (decl_specs,
26682 ds_redefined_builtin_type_spec,
26683 token);
26684 if (!decl_specs->type)
26685 {
26686 decl_specs->type = type_spec;
26687 decl_specs->type_definition_p = false;
26688 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26689 }
26690 }
26691 else if (decl_specs->type)
26692 decl_specs->multiple_types_p = true;
26693 else
26694 {
26695 decl_specs->type = type_spec;
26696 decl_specs->type_definition_p = type_definition_p;
26697 decl_specs->redefined_builtin_type = NULL_TREE;
26698 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26699 }
26700 }
26701
26702 /* True iff TOKEN is the GNU keyword __thread. */
26703
26704 static bool
26705 token_is__thread (cp_token *token)
26706 {
26707 gcc_assert (token->keyword == RID_THREAD);
26708 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26709 }
26710
26711 /* Set the location for a declarator specifier and check if it is
26712 duplicated.
26713
26714 DECL_SPECS is the sequence of declarator specifiers onto which to
26715 set the location.
26716
26717 DS is the single declarator specifier to set which location is to
26718 be set onto the existing sequence of declarators.
26719
26720 LOCATION is the location for the declarator specifier to
26721 consider. */
26722
26723 static void
26724 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26725 cp_decl_spec ds, cp_token *token)
26726 {
26727 gcc_assert (ds < ds_last);
26728
26729 if (decl_specs == NULL)
26730 return;
26731
26732 source_location location = token->location;
26733
26734 if (decl_specs->locations[ds] == 0)
26735 {
26736 decl_specs->locations[ds] = location;
26737 if (ds == ds_thread)
26738 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26739 }
26740 else
26741 {
26742 if (ds == ds_long)
26743 {
26744 if (decl_specs->locations[ds_long_long] != 0)
26745 error_at (location,
26746 "%<long long long%> is too long for GCC");
26747 else
26748 {
26749 decl_specs->locations[ds_long_long] = location;
26750 pedwarn_cxx98 (location,
26751 OPT_Wlong_long,
26752 "ISO C++ 1998 does not support %<long long%>");
26753 }
26754 }
26755 else if (ds == ds_thread)
26756 {
26757 bool gnu = token_is__thread (token);
26758 if (gnu != decl_specs->gnu_thread_keyword_p)
26759 error_at (location,
26760 "both %<__thread%> and %<thread_local%> specified");
26761 else
26762 error_at (location, "duplicate %qD", token->u.value);
26763 }
26764 else
26765 {
26766 static const char *const decl_spec_names[] = {
26767 "signed",
26768 "unsigned",
26769 "short",
26770 "long",
26771 "const",
26772 "volatile",
26773 "restrict",
26774 "inline",
26775 "virtual",
26776 "explicit",
26777 "friend",
26778 "typedef",
26779 "using",
26780 "constexpr",
26781 "__complex"
26782 };
26783 error_at (location,
26784 "duplicate %qs", decl_spec_names[ds]);
26785 }
26786 }
26787 }
26788
26789 /* Return true iff the declarator specifier DS is present in the
26790 sequence of declarator specifiers DECL_SPECS. */
26791
26792 bool
26793 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26794 cp_decl_spec ds)
26795 {
26796 gcc_assert (ds < ds_last);
26797
26798 if (decl_specs == NULL)
26799 return false;
26800
26801 return decl_specs->locations[ds] != 0;
26802 }
26803
26804 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26805 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26806
26807 static bool
26808 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26809 {
26810 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26811 }
26812
26813 /* Issue an error message indicating that TOKEN_DESC was expected.
26814 If KEYWORD is true, it indicated this function is called by
26815 cp_parser_require_keword and the required token can only be
26816 a indicated keyword. */
26817
26818 static void
26819 cp_parser_required_error (cp_parser *parser,
26820 required_token token_desc,
26821 bool keyword)
26822 {
26823 switch (token_desc)
26824 {
26825 case RT_NEW:
26826 cp_parser_error (parser, "expected %<new%>");
26827 return;
26828 case RT_DELETE:
26829 cp_parser_error (parser, "expected %<delete%>");
26830 return;
26831 case RT_RETURN:
26832 cp_parser_error (parser, "expected %<return%>");
26833 return;
26834 case RT_WHILE:
26835 cp_parser_error (parser, "expected %<while%>");
26836 return;
26837 case RT_EXTERN:
26838 cp_parser_error (parser, "expected %<extern%>");
26839 return;
26840 case RT_STATIC_ASSERT:
26841 cp_parser_error (parser, "expected %<static_assert%>");
26842 return;
26843 case RT_DECLTYPE:
26844 cp_parser_error (parser, "expected %<decltype%>");
26845 return;
26846 case RT_OPERATOR:
26847 cp_parser_error (parser, "expected %<operator%>");
26848 return;
26849 case RT_CLASS:
26850 cp_parser_error (parser, "expected %<class%>");
26851 return;
26852 case RT_TEMPLATE:
26853 cp_parser_error (parser, "expected %<template%>");
26854 return;
26855 case RT_NAMESPACE:
26856 cp_parser_error (parser, "expected %<namespace%>");
26857 return;
26858 case RT_USING:
26859 cp_parser_error (parser, "expected %<using%>");
26860 return;
26861 case RT_ASM:
26862 cp_parser_error (parser, "expected %<asm%>");
26863 return;
26864 case RT_TRY:
26865 cp_parser_error (parser, "expected %<try%>");
26866 return;
26867 case RT_CATCH:
26868 cp_parser_error (parser, "expected %<catch%>");
26869 return;
26870 case RT_THROW:
26871 cp_parser_error (parser, "expected %<throw%>");
26872 return;
26873 case RT_LABEL:
26874 cp_parser_error (parser, "expected %<__label__%>");
26875 return;
26876 case RT_AT_TRY:
26877 cp_parser_error (parser, "expected %<@try%>");
26878 return;
26879 case RT_AT_SYNCHRONIZED:
26880 cp_parser_error (parser, "expected %<@synchronized%>");
26881 return;
26882 case RT_AT_THROW:
26883 cp_parser_error (parser, "expected %<@throw%>");
26884 return;
26885 case RT_TRANSACTION_ATOMIC:
26886 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26887 return;
26888 case RT_TRANSACTION_RELAXED:
26889 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26890 return;
26891 default:
26892 break;
26893 }
26894 if (!keyword)
26895 {
26896 switch (token_desc)
26897 {
26898 case RT_SEMICOLON:
26899 cp_parser_error (parser, "expected %<;%>");
26900 return;
26901 case RT_OPEN_PAREN:
26902 cp_parser_error (parser, "expected %<(%>");
26903 return;
26904 case RT_CLOSE_BRACE:
26905 cp_parser_error (parser, "expected %<}%>");
26906 return;
26907 case RT_OPEN_BRACE:
26908 cp_parser_error (parser, "expected %<{%>");
26909 return;
26910 case RT_CLOSE_SQUARE:
26911 cp_parser_error (parser, "expected %<]%>");
26912 return;
26913 case RT_OPEN_SQUARE:
26914 cp_parser_error (parser, "expected %<[%>");
26915 return;
26916 case RT_COMMA:
26917 cp_parser_error (parser, "expected %<,%>");
26918 return;
26919 case RT_SCOPE:
26920 cp_parser_error (parser, "expected %<::%>");
26921 return;
26922 case RT_LESS:
26923 cp_parser_error (parser, "expected %<<%>");
26924 return;
26925 case RT_GREATER:
26926 cp_parser_error (parser, "expected %<>%>");
26927 return;
26928 case RT_EQ:
26929 cp_parser_error (parser, "expected %<=%>");
26930 return;
26931 case RT_ELLIPSIS:
26932 cp_parser_error (parser, "expected %<...%>");
26933 return;
26934 case RT_MULT:
26935 cp_parser_error (parser, "expected %<*%>");
26936 return;
26937 case RT_COMPL:
26938 cp_parser_error (parser, "expected %<~%>");
26939 return;
26940 case RT_COLON:
26941 cp_parser_error (parser, "expected %<:%>");
26942 return;
26943 case RT_COLON_SCOPE:
26944 cp_parser_error (parser, "expected %<:%> or %<::%>");
26945 return;
26946 case RT_CLOSE_PAREN:
26947 cp_parser_error (parser, "expected %<)%>");
26948 return;
26949 case RT_COMMA_CLOSE_PAREN:
26950 cp_parser_error (parser, "expected %<,%> or %<)%>");
26951 return;
26952 case RT_PRAGMA_EOL:
26953 cp_parser_error (parser, "expected end of line");
26954 return;
26955 case RT_NAME:
26956 cp_parser_error (parser, "expected identifier");
26957 return;
26958 case RT_SELECT:
26959 cp_parser_error (parser, "expected selection-statement");
26960 return;
26961 case RT_INTERATION:
26962 cp_parser_error (parser, "expected iteration-statement");
26963 return;
26964 case RT_JUMP:
26965 cp_parser_error (parser, "expected jump-statement");
26966 return;
26967 case RT_CLASS_KEY:
26968 cp_parser_error (parser, "expected class-key");
26969 return;
26970 case RT_CLASS_TYPENAME_TEMPLATE:
26971 cp_parser_error (parser,
26972 "expected %<class%>, %<typename%>, or %<template%>");
26973 return;
26974 default:
26975 gcc_unreachable ();
26976 }
26977 }
26978 else
26979 gcc_unreachable ();
26980 }
26981
26982
26983
26984 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26985 issue an error message indicating that TOKEN_DESC was expected.
26986
26987 Returns the token consumed, if the token had the appropriate type.
26988 Otherwise, returns NULL. */
26989
26990 static cp_token *
26991 cp_parser_require (cp_parser* parser,
26992 enum cpp_ttype type,
26993 required_token token_desc)
26994 {
26995 if (cp_lexer_next_token_is (parser->lexer, type))
26996 return cp_lexer_consume_token (parser->lexer);
26997 else
26998 {
26999 /* Output the MESSAGE -- unless we're parsing tentatively. */
27000 if (!cp_parser_simulate_error (parser))
27001 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27002 return NULL;
27003 }
27004 }
27005
27006 /* An error message is produced if the next token is not '>'.
27007 All further tokens are skipped until the desired token is
27008 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27009
27010 static void
27011 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27012 {
27013 /* Current level of '< ... >'. */
27014 unsigned level = 0;
27015 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27016 unsigned nesting_depth = 0;
27017
27018 /* Are we ready, yet? If not, issue error message. */
27019 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27020 return;
27021
27022 /* Skip tokens until the desired token is found. */
27023 while (true)
27024 {
27025 /* Peek at the next token. */
27026 switch (cp_lexer_peek_token (parser->lexer)->type)
27027 {
27028 case CPP_LESS:
27029 if (!nesting_depth)
27030 ++level;
27031 break;
27032
27033 case CPP_RSHIFT:
27034 if (cxx_dialect == cxx98)
27035 /* C++0x views the `>>' operator as two `>' tokens, but
27036 C++98 does not. */
27037 break;
27038 else if (!nesting_depth && level-- == 0)
27039 {
27040 /* We've hit a `>>' where the first `>' closes the
27041 template argument list, and the second `>' is
27042 spurious. Just consume the `>>' and stop; we've
27043 already produced at least one error. */
27044 cp_lexer_consume_token (parser->lexer);
27045 return;
27046 }
27047 /* Fall through for C++0x, so we handle the second `>' in
27048 the `>>'. */
27049
27050 case CPP_GREATER:
27051 if (!nesting_depth && level-- == 0)
27052 {
27053 /* We've reached the token we want, consume it and stop. */
27054 cp_lexer_consume_token (parser->lexer);
27055 return;
27056 }
27057 break;
27058
27059 case CPP_OPEN_PAREN:
27060 case CPP_OPEN_SQUARE:
27061 ++nesting_depth;
27062 break;
27063
27064 case CPP_CLOSE_PAREN:
27065 case CPP_CLOSE_SQUARE:
27066 if (nesting_depth-- == 0)
27067 return;
27068 break;
27069
27070 case CPP_EOF:
27071 case CPP_PRAGMA_EOL:
27072 case CPP_SEMICOLON:
27073 case CPP_OPEN_BRACE:
27074 case CPP_CLOSE_BRACE:
27075 /* The '>' was probably forgotten, don't look further. */
27076 return;
27077
27078 default:
27079 break;
27080 }
27081
27082 /* Consume this token. */
27083 cp_lexer_consume_token (parser->lexer);
27084 }
27085 }
27086
27087 /* If the next token is the indicated keyword, consume it. Otherwise,
27088 issue an error message indicating that TOKEN_DESC was expected.
27089
27090 Returns the token consumed, if the token had the appropriate type.
27091 Otherwise, returns NULL. */
27092
27093 static cp_token *
27094 cp_parser_require_keyword (cp_parser* parser,
27095 enum rid keyword,
27096 required_token token_desc)
27097 {
27098 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27099
27100 if (token && token->keyword != keyword)
27101 {
27102 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27103 return NULL;
27104 }
27105
27106 return token;
27107 }
27108
27109 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27110 function-definition. */
27111
27112 static bool
27113 cp_parser_token_starts_function_definition_p (cp_token* token)
27114 {
27115 return (/* An ordinary function-body begins with an `{'. */
27116 token->type == CPP_OPEN_BRACE
27117 /* A ctor-initializer begins with a `:'. */
27118 || token->type == CPP_COLON
27119 /* A function-try-block begins with `try'. */
27120 || token->keyword == RID_TRY
27121 /* A function-transaction-block begins with `__transaction_atomic'
27122 or `__transaction_relaxed'. */
27123 || token->keyword == RID_TRANSACTION_ATOMIC
27124 || token->keyword == RID_TRANSACTION_RELAXED
27125 /* The named return value extension begins with `return'. */
27126 || token->keyword == RID_RETURN);
27127 }
27128
27129 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27130 definition. */
27131
27132 static bool
27133 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27134 {
27135 cp_token *token;
27136
27137 token = cp_lexer_peek_token (parser->lexer);
27138 return (token->type == CPP_OPEN_BRACE
27139 || (token->type == CPP_COLON
27140 && !parser->colon_doesnt_start_class_def_p));
27141 }
27142
27143 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27144 C++0x) ending a template-argument. */
27145
27146 static bool
27147 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27148 {
27149 cp_token *token;
27150
27151 token = cp_lexer_peek_token (parser->lexer);
27152 return (token->type == CPP_COMMA
27153 || token->type == CPP_GREATER
27154 || token->type == CPP_ELLIPSIS
27155 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27156 }
27157
27158 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27159 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27160
27161 static bool
27162 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27163 size_t n)
27164 {
27165 cp_token *token;
27166
27167 token = cp_lexer_peek_nth_token (parser->lexer, n);
27168 if (token->type == CPP_LESS)
27169 return true;
27170 /* Check for the sequence `<::' in the original code. It would be lexed as
27171 `[:', where `[' is a digraph, and there is no whitespace before
27172 `:'. */
27173 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27174 {
27175 cp_token *token2;
27176 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27177 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27178 return true;
27179 }
27180 return false;
27181 }
27182
27183 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27184 or none_type otherwise. */
27185
27186 static enum tag_types
27187 cp_parser_token_is_class_key (cp_token* token)
27188 {
27189 switch (token->keyword)
27190 {
27191 case RID_CLASS:
27192 return class_type;
27193 case RID_STRUCT:
27194 return record_type;
27195 case RID_UNION:
27196 return union_type;
27197
27198 default:
27199 return none_type;
27200 }
27201 }
27202
27203 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27204 or none_type otherwise or if the token is null. */
27205
27206 static enum tag_types
27207 cp_parser_token_is_type_parameter_key (cp_token* token)
27208 {
27209 if (!token)
27210 return none_type;
27211
27212 switch (token->keyword)
27213 {
27214 case RID_CLASS:
27215 return class_type;
27216 case RID_TYPENAME:
27217 return typename_type;
27218
27219 default:
27220 return none_type;
27221 }
27222 }
27223
27224 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27225
27226 static void
27227 cp_parser_check_class_key (enum tag_types class_key, tree type)
27228 {
27229 if (type == error_mark_node)
27230 return;
27231 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27232 {
27233 if (permerror (input_location, "%qs tag used in naming %q#T",
27234 class_key == union_type ? "union"
27235 : class_key == record_type ? "struct" : "class",
27236 type))
27237 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27238 "%q#T was previously declared here", type);
27239 }
27240 }
27241
27242 /* Issue an error message if DECL is redeclared with different
27243 access than its original declaration [class.access.spec/3].
27244 This applies to nested classes, nested class templates and
27245 enumerations [class.mem/1]. */
27246
27247 static void
27248 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27249 {
27250 if (!decl
27251 || (!CLASS_TYPE_P (TREE_TYPE (decl))
27252 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
27253 return;
27254
27255 if ((TREE_PRIVATE (decl)
27256 != (current_access_specifier == access_private_node))
27257 || (TREE_PROTECTED (decl)
27258 != (current_access_specifier == access_protected_node)))
27259 error_at (location, "%qD redeclared with different access", decl);
27260 }
27261
27262 /* Look for the `template' keyword, as a syntactic disambiguator.
27263 Return TRUE iff it is present, in which case it will be
27264 consumed. */
27265
27266 static bool
27267 cp_parser_optional_template_keyword (cp_parser *parser)
27268 {
27269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27270 {
27271 /* In C++98 the `template' keyword can only be used within templates;
27272 outside templates the parser can always figure out what is a
27273 template and what is not. In C++11, per the resolution of DR 468,
27274 `template' is allowed in cases where it is not strictly necessary. */
27275 if (!processing_template_decl
27276 && pedantic && cxx_dialect == cxx98)
27277 {
27278 cp_token *token = cp_lexer_peek_token (parser->lexer);
27279 pedwarn (token->location, OPT_Wpedantic,
27280 "in C++98 %<template%> (as a disambiguator) is only "
27281 "allowed within templates");
27282 /* If this part of the token stream is rescanned, the same
27283 error message would be generated. So, we purge the token
27284 from the stream. */
27285 cp_lexer_purge_token (parser->lexer);
27286 return false;
27287 }
27288 else
27289 {
27290 /* Consume the `template' keyword. */
27291 cp_lexer_consume_token (parser->lexer);
27292 return true;
27293 }
27294 }
27295 return false;
27296 }
27297
27298 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27299 set PARSER->SCOPE, and perform other related actions. */
27300
27301 static void
27302 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27303 {
27304 struct tree_check *check_value;
27305
27306 /* Get the stored value. */
27307 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27308 /* Set the scope from the stored value. */
27309 parser->scope = saved_checks_value (check_value);
27310 parser->qualifying_scope = check_value->qualifying_scope;
27311 parser->object_scope = NULL_TREE;
27312 }
27313
27314 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27315 encounter the end of a block before what we were looking for. */
27316
27317 static bool
27318 cp_parser_cache_group (cp_parser *parser,
27319 enum cpp_ttype end,
27320 unsigned depth)
27321 {
27322 while (true)
27323 {
27324 cp_token *token = cp_lexer_peek_token (parser->lexer);
27325
27326 /* Abort a parenthesized expression if we encounter a semicolon. */
27327 if ((end == CPP_CLOSE_PAREN || depth == 0)
27328 && token->type == CPP_SEMICOLON)
27329 return true;
27330 /* If we've reached the end of the file, stop. */
27331 if (token->type == CPP_EOF
27332 || (end != CPP_PRAGMA_EOL
27333 && token->type == CPP_PRAGMA_EOL))
27334 return true;
27335 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27336 /* We've hit the end of an enclosing block, so there's been some
27337 kind of syntax error. */
27338 return true;
27339
27340 /* Consume the token. */
27341 cp_lexer_consume_token (parser->lexer);
27342 /* See if it starts a new group. */
27343 if (token->type == CPP_OPEN_BRACE)
27344 {
27345 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27346 /* In theory this should probably check end == '}', but
27347 cp_parser_save_member_function_body needs it to exit
27348 after either '}' or ')' when called with ')'. */
27349 if (depth == 0)
27350 return false;
27351 }
27352 else if (token->type == CPP_OPEN_PAREN)
27353 {
27354 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27355 if (depth == 0 && end == CPP_CLOSE_PAREN)
27356 return false;
27357 }
27358 else if (token->type == CPP_PRAGMA)
27359 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27360 else if (token->type == end)
27361 return false;
27362 }
27363 }
27364
27365 /* Like above, for caching a default argument or NSDMI. Both of these are
27366 terminated by a non-nested comma, but it can be unclear whether or not a
27367 comma is nested in a template argument list unless we do more parsing.
27368 In order to handle this ambiguity, when we encounter a ',' after a '<'
27369 we try to parse what follows as a parameter-declaration-list (in the
27370 case of a default argument) or a member-declarator (in the case of an
27371 NSDMI). If that succeeds, then we stop caching. */
27372
27373 static tree
27374 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27375 {
27376 unsigned depth = 0;
27377 int maybe_template_id = 0;
27378 cp_token *first_token;
27379 cp_token *token;
27380 tree default_argument;
27381
27382 /* Add tokens until we have processed the entire default
27383 argument. We add the range [first_token, token). */
27384 first_token = cp_lexer_peek_token (parser->lexer);
27385 if (first_token->type == CPP_OPEN_BRACE)
27386 {
27387 /* For list-initialization, this is straightforward. */
27388 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27389 token = cp_lexer_peek_token (parser->lexer);
27390 }
27391 else while (true)
27392 {
27393 bool done = false;
27394
27395 /* Peek at the next token. */
27396 token = cp_lexer_peek_token (parser->lexer);
27397 /* What we do depends on what token we have. */
27398 switch (token->type)
27399 {
27400 /* In valid code, a default argument must be
27401 immediately followed by a `,' `)', or `...'. */
27402 case CPP_COMMA:
27403 if (depth == 0 && maybe_template_id)
27404 {
27405 /* If we've seen a '<', we might be in a
27406 template-argument-list. Until Core issue 325 is
27407 resolved, we don't know how this situation ought
27408 to be handled, so try to DTRT. We check whether
27409 what comes after the comma is a valid parameter
27410 declaration list. If it is, then the comma ends
27411 the default argument; otherwise the default
27412 argument continues. */
27413 bool error = false;
27414 cp_token *peek;
27415
27416 /* Set ITALP so cp_parser_parameter_declaration_list
27417 doesn't decide to commit to this parse. */
27418 bool saved_italp = parser->in_template_argument_list_p;
27419 parser->in_template_argument_list_p = true;
27420
27421 cp_parser_parse_tentatively (parser);
27422
27423 if (nsdmi)
27424 {
27425 /* Parse declarators until we reach a non-comma or
27426 somthing that cannot be an initializer.
27427 Just checking whether we're looking at a single
27428 declarator is insufficient. Consider:
27429 int var = tuple<T,U>::x;
27430 The template parameter 'U' looks exactly like a
27431 declarator. */
27432 do
27433 {
27434 int ctor_dtor_or_conv_p;
27435 cp_lexer_consume_token (parser->lexer);
27436 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27437 &ctor_dtor_or_conv_p,
27438 /*parenthesized_p=*/NULL,
27439 /*member_p=*/true,
27440 /*friend_p=*/false);
27441 peek = cp_lexer_peek_token (parser->lexer);
27442 if (cp_parser_error_occurred (parser))
27443 break;
27444 }
27445 while (peek->type == CPP_COMMA);
27446 /* If we met an '=' or ';' then the original comma
27447 was the end of the NSDMI. Otherwise assume
27448 we're still in the NSDMI. */
27449 error = (peek->type != CPP_EQ
27450 && peek->type != CPP_SEMICOLON);
27451 }
27452 else
27453 {
27454 cp_lexer_consume_token (parser->lexer);
27455 begin_scope (sk_function_parms, NULL_TREE);
27456 cp_parser_parameter_declaration_list (parser, &error);
27457 pop_bindings_and_leave_scope ();
27458 }
27459 if (!cp_parser_error_occurred (parser) && !error)
27460 done = true;
27461 cp_parser_abort_tentative_parse (parser);
27462
27463 parser->in_template_argument_list_p = saved_italp;
27464 break;
27465 }
27466 case CPP_CLOSE_PAREN:
27467 case CPP_ELLIPSIS:
27468 /* If we run into a non-nested `;', `}', or `]',
27469 then the code is invalid -- but the default
27470 argument is certainly over. */
27471 case CPP_SEMICOLON:
27472 case CPP_CLOSE_BRACE:
27473 case CPP_CLOSE_SQUARE:
27474 if (depth == 0
27475 /* Handle correctly int n = sizeof ... ( p ); */
27476 && token->type != CPP_ELLIPSIS)
27477 done = true;
27478 /* Update DEPTH, if necessary. */
27479 else if (token->type == CPP_CLOSE_PAREN
27480 || token->type == CPP_CLOSE_BRACE
27481 || token->type == CPP_CLOSE_SQUARE)
27482 --depth;
27483 break;
27484
27485 case CPP_OPEN_PAREN:
27486 case CPP_OPEN_SQUARE:
27487 case CPP_OPEN_BRACE:
27488 ++depth;
27489 break;
27490
27491 case CPP_LESS:
27492 if (depth == 0)
27493 /* This might be the comparison operator, or it might
27494 start a template argument list. */
27495 ++maybe_template_id;
27496 break;
27497
27498 case CPP_RSHIFT:
27499 if (cxx_dialect == cxx98)
27500 break;
27501 /* Fall through for C++0x, which treats the `>>'
27502 operator like two `>' tokens in certain
27503 cases. */
27504
27505 case CPP_GREATER:
27506 if (depth == 0)
27507 {
27508 /* This might be an operator, or it might close a
27509 template argument list. But if a previous '<'
27510 started a template argument list, this will have
27511 closed it, so we can't be in one anymore. */
27512 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27513 if (maybe_template_id < 0)
27514 maybe_template_id = 0;
27515 }
27516 break;
27517
27518 /* If we run out of tokens, issue an error message. */
27519 case CPP_EOF:
27520 case CPP_PRAGMA_EOL:
27521 error_at (token->location, "file ends in default argument");
27522 return error_mark_node;
27523
27524 case CPP_NAME:
27525 case CPP_SCOPE:
27526 /* In these cases, we should look for template-ids.
27527 For example, if the default argument is
27528 `X<int, double>()', we need to do name lookup to
27529 figure out whether or not `X' is a template; if
27530 so, the `,' does not end the default argument.
27531
27532 That is not yet done. */
27533 break;
27534
27535 default:
27536 break;
27537 }
27538
27539 /* If we've reached the end, stop. */
27540 if (done)
27541 break;
27542
27543 /* Add the token to the token block. */
27544 token = cp_lexer_consume_token (parser->lexer);
27545 }
27546
27547 /* Create a DEFAULT_ARG to represent the unparsed default
27548 argument. */
27549 default_argument = make_node (DEFAULT_ARG);
27550 DEFARG_TOKENS (default_argument)
27551 = cp_token_cache_new (first_token, token);
27552 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27553
27554 return default_argument;
27555 }
27556
27557 /* Begin parsing tentatively. We always save tokens while parsing
27558 tentatively so that if the tentative parsing fails we can restore the
27559 tokens. */
27560
27561 static void
27562 cp_parser_parse_tentatively (cp_parser* parser)
27563 {
27564 /* Enter a new parsing context. */
27565 parser->context = cp_parser_context_new (parser->context);
27566 /* Begin saving tokens. */
27567 cp_lexer_save_tokens (parser->lexer);
27568 /* In order to avoid repetitive access control error messages,
27569 access checks are queued up until we are no longer parsing
27570 tentatively. */
27571 push_deferring_access_checks (dk_deferred);
27572 }
27573
27574 /* Commit to the currently active tentative parse. */
27575
27576 static void
27577 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27578 {
27579 cp_parser_context *context;
27580 cp_lexer *lexer;
27581
27582 /* Mark all of the levels as committed. */
27583 lexer = parser->lexer;
27584 for (context = parser->context; context->next; context = context->next)
27585 {
27586 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27587 break;
27588 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27589 while (!cp_lexer_saving_tokens (lexer))
27590 lexer = lexer->next;
27591 cp_lexer_commit_tokens (lexer);
27592 }
27593 }
27594
27595 /* Commit to the topmost currently active tentative parse.
27596
27597 Note that this function shouldn't be called when there are
27598 irreversible side-effects while in a tentative state. For
27599 example, we shouldn't create a permanent entry in the symbol
27600 table, or issue an error message that might not apply if the
27601 tentative parse is aborted. */
27602
27603 static void
27604 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27605 {
27606 cp_parser_context *context = parser->context;
27607 cp_lexer *lexer = parser->lexer;
27608
27609 if (context)
27610 {
27611 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27612 return;
27613 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27614
27615 while (!cp_lexer_saving_tokens (lexer))
27616 lexer = lexer->next;
27617 cp_lexer_commit_tokens (lexer);
27618 }
27619 }
27620
27621 /* Abort the currently active tentative parse. All consumed tokens
27622 will be rolled back, and no diagnostics will be issued. */
27623
27624 static void
27625 cp_parser_abort_tentative_parse (cp_parser* parser)
27626 {
27627 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27628 || errorcount > 0);
27629 cp_parser_simulate_error (parser);
27630 /* Now, pretend that we want to see if the construct was
27631 successfully parsed. */
27632 cp_parser_parse_definitely (parser);
27633 }
27634
27635 /* Stop parsing tentatively. If a parse error has occurred, restore the
27636 token stream. Otherwise, commit to the tokens we have consumed.
27637 Returns true if no error occurred; false otherwise. */
27638
27639 static bool
27640 cp_parser_parse_definitely (cp_parser* parser)
27641 {
27642 bool error_occurred;
27643 cp_parser_context *context;
27644
27645 /* Remember whether or not an error occurred, since we are about to
27646 destroy that information. */
27647 error_occurred = cp_parser_error_occurred (parser);
27648 /* Remove the topmost context from the stack. */
27649 context = parser->context;
27650 parser->context = context->next;
27651 /* If no parse errors occurred, commit to the tentative parse. */
27652 if (!error_occurred)
27653 {
27654 /* Commit to the tokens read tentatively, unless that was
27655 already done. */
27656 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27657 cp_lexer_commit_tokens (parser->lexer);
27658
27659 pop_to_parent_deferring_access_checks ();
27660 }
27661 /* Otherwise, if errors occurred, roll back our state so that things
27662 are just as they were before we began the tentative parse. */
27663 else
27664 {
27665 cp_lexer_rollback_tokens (parser->lexer);
27666 pop_deferring_access_checks ();
27667 }
27668 /* Add the context to the front of the free list. */
27669 context->next = cp_parser_context_free_list;
27670 cp_parser_context_free_list = context;
27671
27672 return !error_occurred;
27673 }
27674
27675 /* Returns true if we are parsing tentatively and are not committed to
27676 this tentative parse. */
27677
27678 static bool
27679 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27680 {
27681 return (cp_parser_parsing_tentatively (parser)
27682 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27683 }
27684
27685 /* Returns nonzero iff an error has occurred during the most recent
27686 tentative parse. */
27687
27688 static bool
27689 cp_parser_error_occurred (cp_parser* parser)
27690 {
27691 return (cp_parser_parsing_tentatively (parser)
27692 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27693 }
27694
27695 /* Returns nonzero if GNU extensions are allowed. */
27696
27697 static bool
27698 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27699 {
27700 return parser->allow_gnu_extensions_p;
27701 }
27702 \f
27703 /* Objective-C++ Productions */
27704
27705
27706 /* Parse an Objective-C expression, which feeds into a primary-expression
27707 above.
27708
27709 objc-expression:
27710 objc-message-expression
27711 objc-string-literal
27712 objc-encode-expression
27713 objc-protocol-expression
27714 objc-selector-expression
27715
27716 Returns a tree representation of the expression. */
27717
27718 static cp_expr
27719 cp_parser_objc_expression (cp_parser* parser)
27720 {
27721 /* Try to figure out what kind of declaration is present. */
27722 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27723
27724 switch (kwd->type)
27725 {
27726 case CPP_OPEN_SQUARE:
27727 return cp_parser_objc_message_expression (parser);
27728
27729 case CPP_OBJC_STRING:
27730 kwd = cp_lexer_consume_token (parser->lexer);
27731 return objc_build_string_object (kwd->u.value);
27732
27733 case CPP_KEYWORD:
27734 switch (kwd->keyword)
27735 {
27736 case RID_AT_ENCODE:
27737 return cp_parser_objc_encode_expression (parser);
27738
27739 case RID_AT_PROTOCOL:
27740 return cp_parser_objc_protocol_expression (parser);
27741
27742 case RID_AT_SELECTOR:
27743 return cp_parser_objc_selector_expression (parser);
27744
27745 default:
27746 break;
27747 }
27748 default:
27749 error_at (kwd->location,
27750 "misplaced %<@%D%> Objective-C++ construct",
27751 kwd->u.value);
27752 cp_parser_skip_to_end_of_block_or_statement (parser);
27753 }
27754
27755 return error_mark_node;
27756 }
27757
27758 /* Parse an Objective-C message expression.
27759
27760 objc-message-expression:
27761 [ objc-message-receiver objc-message-args ]
27762
27763 Returns a representation of an Objective-C message. */
27764
27765 static tree
27766 cp_parser_objc_message_expression (cp_parser* parser)
27767 {
27768 tree receiver, messageargs;
27769
27770 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27771 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27772 receiver = cp_parser_objc_message_receiver (parser);
27773 messageargs = cp_parser_objc_message_args (parser);
27774 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27775 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27776
27777 tree result = objc_build_message_expr (receiver, messageargs);
27778
27779 /* Construct a location e.g.
27780 [self func1:5]
27781 ^~~~~~~~~~~~~~
27782 ranging from the '[' to the ']', with the caret at the start. */
27783 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27784 protected_set_expr_location (result, combined_loc);
27785
27786 return result;
27787 }
27788
27789 /* Parse an objc-message-receiver.
27790
27791 objc-message-receiver:
27792 expression
27793 simple-type-specifier
27794
27795 Returns a representation of the type or expression. */
27796
27797 static tree
27798 cp_parser_objc_message_receiver (cp_parser* parser)
27799 {
27800 tree rcv;
27801
27802 /* An Objective-C message receiver may be either (1) a type
27803 or (2) an expression. */
27804 cp_parser_parse_tentatively (parser);
27805 rcv = cp_parser_expression (parser);
27806
27807 /* If that worked out, fine. */
27808 if (cp_parser_parse_definitely (parser))
27809 return rcv;
27810
27811 cp_parser_parse_tentatively (parser);
27812 rcv = cp_parser_simple_type_specifier (parser,
27813 /*decl_specs=*/NULL,
27814 CP_PARSER_FLAGS_NONE);
27815
27816 if (cp_parser_parse_definitely (parser))
27817 return objc_get_class_reference (rcv);
27818
27819 cp_parser_error (parser, "objective-c++ message receiver expected");
27820 return error_mark_node;
27821 }
27822
27823 /* Parse the arguments and selectors comprising an Objective-C message.
27824
27825 objc-message-args:
27826 objc-selector
27827 objc-selector-args
27828 objc-selector-args , objc-comma-args
27829
27830 objc-selector-args:
27831 objc-selector [opt] : assignment-expression
27832 objc-selector-args objc-selector [opt] : assignment-expression
27833
27834 objc-comma-args:
27835 assignment-expression
27836 objc-comma-args , assignment-expression
27837
27838 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27839 selector arguments and TREE_VALUE containing a list of comma
27840 arguments. */
27841
27842 static tree
27843 cp_parser_objc_message_args (cp_parser* parser)
27844 {
27845 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27846 bool maybe_unary_selector_p = true;
27847 cp_token *token = cp_lexer_peek_token (parser->lexer);
27848
27849 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27850 {
27851 tree selector = NULL_TREE, arg;
27852
27853 if (token->type != CPP_COLON)
27854 selector = cp_parser_objc_selector (parser);
27855
27856 /* Detect if we have a unary selector. */
27857 if (maybe_unary_selector_p
27858 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27859 return build_tree_list (selector, NULL_TREE);
27860
27861 maybe_unary_selector_p = false;
27862 cp_parser_require (parser, CPP_COLON, RT_COLON);
27863 arg = cp_parser_assignment_expression (parser);
27864
27865 sel_args
27866 = chainon (sel_args,
27867 build_tree_list (selector, arg));
27868
27869 token = cp_lexer_peek_token (parser->lexer);
27870 }
27871
27872 /* Handle non-selector arguments, if any. */
27873 while (token->type == CPP_COMMA)
27874 {
27875 tree arg;
27876
27877 cp_lexer_consume_token (parser->lexer);
27878 arg = cp_parser_assignment_expression (parser);
27879
27880 addl_args
27881 = chainon (addl_args,
27882 build_tree_list (NULL_TREE, arg));
27883
27884 token = cp_lexer_peek_token (parser->lexer);
27885 }
27886
27887 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27888 {
27889 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27890 return build_tree_list (error_mark_node, error_mark_node);
27891 }
27892
27893 return build_tree_list (sel_args, addl_args);
27894 }
27895
27896 /* Parse an Objective-C encode expression.
27897
27898 objc-encode-expression:
27899 @encode objc-typename
27900
27901 Returns an encoded representation of the type argument. */
27902
27903 static cp_expr
27904 cp_parser_objc_encode_expression (cp_parser* parser)
27905 {
27906 tree type;
27907 cp_token *token;
27908 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27909
27910 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27911 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27912 token = cp_lexer_peek_token (parser->lexer);
27913 type = complete_type (cp_parser_type_id (parser));
27914 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27915
27916 if (!type)
27917 {
27918 error_at (token->location,
27919 "%<@encode%> must specify a type as an argument");
27920 return error_mark_node;
27921 }
27922
27923 /* This happens if we find @encode(T) (where T is a template
27924 typename or something dependent on a template typename) when
27925 parsing a template. In that case, we can't compile it
27926 immediately, but we rather create an AT_ENCODE_EXPR which will
27927 need to be instantiated when the template is used.
27928 */
27929 if (dependent_type_p (type))
27930 {
27931 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27932 TREE_READONLY (value) = 1;
27933 return value;
27934 }
27935
27936
27937 /* Build a location of the form:
27938 @encode(int)
27939 ^~~~~~~~~~~~
27940 with caret==start at the @ token, finishing at the close paren. */
27941 location_t combined_loc
27942 = make_location (start_loc, start_loc,
27943 cp_lexer_previous_token (parser->lexer)->location);
27944
27945 return cp_expr (objc_build_encode_expr (type), combined_loc);
27946 }
27947
27948 /* Parse an Objective-C @defs expression. */
27949
27950 static tree
27951 cp_parser_objc_defs_expression (cp_parser *parser)
27952 {
27953 tree name;
27954
27955 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27956 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27957 name = cp_parser_identifier (parser);
27958 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27959
27960 return objc_get_class_ivars (name);
27961 }
27962
27963 /* Parse an Objective-C protocol expression.
27964
27965 objc-protocol-expression:
27966 @protocol ( identifier )
27967
27968 Returns a representation of the protocol expression. */
27969
27970 static tree
27971 cp_parser_objc_protocol_expression (cp_parser* parser)
27972 {
27973 tree proto;
27974 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27975
27976 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27977 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27978 proto = cp_parser_identifier (parser);
27979 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27980
27981 /* Build a location of the form:
27982 @protocol(prot)
27983 ^~~~~~~~~~~~~~~
27984 with caret==start at the @ token, finishing at the close paren. */
27985 location_t combined_loc
27986 = make_location (start_loc, start_loc,
27987 cp_lexer_previous_token (parser->lexer)->location);
27988 tree result = objc_build_protocol_expr (proto);
27989 protected_set_expr_location (result, combined_loc);
27990 return result;
27991 }
27992
27993 /* Parse an Objective-C selector expression.
27994
27995 objc-selector-expression:
27996 @selector ( objc-method-signature )
27997
27998 objc-method-signature:
27999 objc-selector
28000 objc-selector-seq
28001
28002 objc-selector-seq:
28003 objc-selector :
28004 objc-selector-seq objc-selector :
28005
28006 Returns a representation of the method selector. */
28007
28008 static tree
28009 cp_parser_objc_selector_expression (cp_parser* parser)
28010 {
28011 tree sel_seq = NULL_TREE;
28012 bool maybe_unary_selector_p = true;
28013 cp_token *token;
28014 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28015
28016 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28017 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28018 token = cp_lexer_peek_token (parser->lexer);
28019
28020 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28021 || token->type == CPP_SCOPE)
28022 {
28023 tree selector = NULL_TREE;
28024
28025 if (token->type != CPP_COLON
28026 || token->type == CPP_SCOPE)
28027 selector = cp_parser_objc_selector (parser);
28028
28029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28030 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28031 {
28032 /* Detect if we have a unary selector. */
28033 if (maybe_unary_selector_p)
28034 {
28035 sel_seq = selector;
28036 goto finish_selector;
28037 }
28038 else
28039 {
28040 cp_parser_error (parser, "expected %<:%>");
28041 }
28042 }
28043 maybe_unary_selector_p = false;
28044 token = cp_lexer_consume_token (parser->lexer);
28045
28046 if (token->type == CPP_SCOPE)
28047 {
28048 sel_seq
28049 = chainon (sel_seq,
28050 build_tree_list (selector, NULL_TREE));
28051 sel_seq
28052 = chainon (sel_seq,
28053 build_tree_list (NULL_TREE, NULL_TREE));
28054 }
28055 else
28056 sel_seq
28057 = chainon (sel_seq,
28058 build_tree_list (selector, NULL_TREE));
28059
28060 token = cp_lexer_peek_token (parser->lexer);
28061 }
28062
28063 finish_selector:
28064 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28065
28066
28067 /* Build a location of the form:
28068 @selector(func)
28069 ^~~~~~~~~~~~~~~
28070 with caret==start at the @ token, finishing at the close paren. */
28071 location_t combined_loc
28072 = make_location (loc, loc,
28073 cp_lexer_previous_token (parser->lexer)->location);
28074 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28075 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28076 protected_set_expr_location (result, combined_loc);
28077 return result;
28078 }
28079
28080 /* Parse a list of identifiers.
28081
28082 objc-identifier-list:
28083 identifier
28084 objc-identifier-list , identifier
28085
28086 Returns a TREE_LIST of identifier nodes. */
28087
28088 static tree
28089 cp_parser_objc_identifier_list (cp_parser* parser)
28090 {
28091 tree identifier;
28092 tree list;
28093 cp_token *sep;
28094
28095 identifier = cp_parser_identifier (parser);
28096 if (identifier == error_mark_node)
28097 return error_mark_node;
28098
28099 list = build_tree_list (NULL_TREE, identifier);
28100 sep = cp_lexer_peek_token (parser->lexer);
28101
28102 while (sep->type == CPP_COMMA)
28103 {
28104 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28105 identifier = cp_parser_identifier (parser);
28106 if (identifier == error_mark_node)
28107 return list;
28108
28109 list = chainon (list, build_tree_list (NULL_TREE,
28110 identifier));
28111 sep = cp_lexer_peek_token (parser->lexer);
28112 }
28113
28114 return list;
28115 }
28116
28117 /* Parse an Objective-C alias declaration.
28118
28119 objc-alias-declaration:
28120 @compatibility_alias identifier identifier ;
28121
28122 This function registers the alias mapping with the Objective-C front end.
28123 It returns nothing. */
28124
28125 static void
28126 cp_parser_objc_alias_declaration (cp_parser* parser)
28127 {
28128 tree alias, orig;
28129
28130 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28131 alias = cp_parser_identifier (parser);
28132 orig = cp_parser_identifier (parser);
28133 objc_declare_alias (alias, orig);
28134 cp_parser_consume_semicolon_at_end_of_statement (parser);
28135 }
28136
28137 /* Parse an Objective-C class forward-declaration.
28138
28139 objc-class-declaration:
28140 @class objc-identifier-list ;
28141
28142 The function registers the forward declarations with the Objective-C
28143 front end. It returns nothing. */
28144
28145 static void
28146 cp_parser_objc_class_declaration (cp_parser* parser)
28147 {
28148 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28149 while (true)
28150 {
28151 tree id;
28152
28153 id = cp_parser_identifier (parser);
28154 if (id == error_mark_node)
28155 break;
28156
28157 objc_declare_class (id);
28158
28159 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28160 cp_lexer_consume_token (parser->lexer);
28161 else
28162 break;
28163 }
28164 cp_parser_consume_semicolon_at_end_of_statement (parser);
28165 }
28166
28167 /* Parse a list of Objective-C protocol references.
28168
28169 objc-protocol-refs-opt:
28170 objc-protocol-refs [opt]
28171
28172 objc-protocol-refs:
28173 < objc-identifier-list >
28174
28175 Returns a TREE_LIST of identifiers, if any. */
28176
28177 static tree
28178 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28179 {
28180 tree protorefs = NULL_TREE;
28181
28182 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28183 {
28184 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28185 protorefs = cp_parser_objc_identifier_list (parser);
28186 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28187 }
28188
28189 return protorefs;
28190 }
28191
28192 /* Parse a Objective-C visibility specification. */
28193
28194 static void
28195 cp_parser_objc_visibility_spec (cp_parser* parser)
28196 {
28197 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28198
28199 switch (vis->keyword)
28200 {
28201 case RID_AT_PRIVATE:
28202 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28203 break;
28204 case RID_AT_PROTECTED:
28205 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28206 break;
28207 case RID_AT_PUBLIC:
28208 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28209 break;
28210 case RID_AT_PACKAGE:
28211 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28212 break;
28213 default:
28214 return;
28215 }
28216
28217 /* Eat '@private'/'@protected'/'@public'. */
28218 cp_lexer_consume_token (parser->lexer);
28219 }
28220
28221 /* Parse an Objective-C method type. Return 'true' if it is a class
28222 (+) method, and 'false' if it is an instance (-) method. */
28223
28224 static inline bool
28225 cp_parser_objc_method_type (cp_parser* parser)
28226 {
28227 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28228 return true;
28229 else
28230 return false;
28231 }
28232
28233 /* Parse an Objective-C protocol qualifier. */
28234
28235 static tree
28236 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28237 {
28238 tree quals = NULL_TREE, node;
28239 cp_token *token = cp_lexer_peek_token (parser->lexer);
28240
28241 node = token->u.value;
28242
28243 while (node && identifier_p (node)
28244 && (node == ridpointers [(int) RID_IN]
28245 || node == ridpointers [(int) RID_OUT]
28246 || node == ridpointers [(int) RID_INOUT]
28247 || node == ridpointers [(int) RID_BYCOPY]
28248 || node == ridpointers [(int) RID_BYREF]
28249 || node == ridpointers [(int) RID_ONEWAY]))
28250 {
28251 quals = tree_cons (NULL_TREE, node, quals);
28252 cp_lexer_consume_token (parser->lexer);
28253 token = cp_lexer_peek_token (parser->lexer);
28254 node = token->u.value;
28255 }
28256
28257 return quals;
28258 }
28259
28260 /* Parse an Objective-C typename. */
28261
28262 static tree
28263 cp_parser_objc_typename (cp_parser* parser)
28264 {
28265 tree type_name = NULL_TREE;
28266
28267 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28268 {
28269 tree proto_quals, cp_type = NULL_TREE;
28270
28271 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28272 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28273
28274 /* An ObjC type name may consist of just protocol qualifiers, in which
28275 case the type shall default to 'id'. */
28276 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28277 {
28278 cp_type = cp_parser_type_id (parser);
28279
28280 /* If the type could not be parsed, an error has already
28281 been produced. For error recovery, behave as if it had
28282 not been specified, which will use the default type
28283 'id'. */
28284 if (cp_type == error_mark_node)
28285 {
28286 cp_type = NULL_TREE;
28287 /* We need to skip to the closing parenthesis as
28288 cp_parser_type_id() does not seem to do it for
28289 us. */
28290 cp_parser_skip_to_closing_parenthesis (parser,
28291 /*recovering=*/true,
28292 /*or_comma=*/false,
28293 /*consume_paren=*/false);
28294 }
28295 }
28296
28297 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28298 type_name = build_tree_list (proto_quals, cp_type);
28299 }
28300
28301 return type_name;
28302 }
28303
28304 /* Check to see if TYPE refers to an Objective-C selector name. */
28305
28306 static bool
28307 cp_parser_objc_selector_p (enum cpp_ttype type)
28308 {
28309 return (type == CPP_NAME || type == CPP_KEYWORD
28310 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28311 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28312 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28313 || type == CPP_XOR || type == CPP_XOR_EQ);
28314 }
28315
28316 /* Parse an Objective-C selector. */
28317
28318 static tree
28319 cp_parser_objc_selector (cp_parser* parser)
28320 {
28321 cp_token *token = cp_lexer_consume_token (parser->lexer);
28322
28323 if (!cp_parser_objc_selector_p (token->type))
28324 {
28325 error_at (token->location, "invalid Objective-C++ selector name");
28326 return error_mark_node;
28327 }
28328
28329 /* C++ operator names are allowed to appear in ObjC selectors. */
28330 switch (token->type)
28331 {
28332 case CPP_AND_AND: return get_identifier ("and");
28333 case CPP_AND_EQ: return get_identifier ("and_eq");
28334 case CPP_AND: return get_identifier ("bitand");
28335 case CPP_OR: return get_identifier ("bitor");
28336 case CPP_COMPL: return get_identifier ("compl");
28337 case CPP_NOT: return get_identifier ("not");
28338 case CPP_NOT_EQ: return get_identifier ("not_eq");
28339 case CPP_OR_OR: return get_identifier ("or");
28340 case CPP_OR_EQ: return get_identifier ("or_eq");
28341 case CPP_XOR: return get_identifier ("xor");
28342 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28343 default: return token->u.value;
28344 }
28345 }
28346
28347 /* Parse an Objective-C params list. */
28348
28349 static tree
28350 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28351 {
28352 tree params = NULL_TREE;
28353 bool maybe_unary_selector_p = true;
28354 cp_token *token = cp_lexer_peek_token (parser->lexer);
28355
28356 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28357 {
28358 tree selector = NULL_TREE, type_name, identifier;
28359 tree parm_attr = NULL_TREE;
28360
28361 if (token->keyword == RID_ATTRIBUTE)
28362 break;
28363
28364 if (token->type != CPP_COLON)
28365 selector = cp_parser_objc_selector (parser);
28366
28367 /* Detect if we have a unary selector. */
28368 if (maybe_unary_selector_p
28369 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28370 {
28371 params = selector; /* Might be followed by attributes. */
28372 break;
28373 }
28374
28375 maybe_unary_selector_p = false;
28376 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28377 {
28378 /* Something went quite wrong. There should be a colon
28379 here, but there is not. Stop parsing parameters. */
28380 break;
28381 }
28382 type_name = cp_parser_objc_typename (parser);
28383 /* New ObjC allows attributes on parameters too. */
28384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28385 parm_attr = cp_parser_attributes_opt (parser);
28386 identifier = cp_parser_identifier (parser);
28387
28388 params
28389 = chainon (params,
28390 objc_build_keyword_decl (selector,
28391 type_name,
28392 identifier,
28393 parm_attr));
28394
28395 token = cp_lexer_peek_token (parser->lexer);
28396 }
28397
28398 if (params == NULL_TREE)
28399 {
28400 cp_parser_error (parser, "objective-c++ method declaration is expected");
28401 return error_mark_node;
28402 }
28403
28404 /* We allow tail attributes for the method. */
28405 if (token->keyword == RID_ATTRIBUTE)
28406 {
28407 *attributes = cp_parser_attributes_opt (parser);
28408 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28409 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28410 return params;
28411 cp_parser_error (parser,
28412 "method attributes must be specified at the end");
28413 return error_mark_node;
28414 }
28415
28416 if (params == NULL_TREE)
28417 {
28418 cp_parser_error (parser, "objective-c++ method declaration is expected");
28419 return error_mark_node;
28420 }
28421 return params;
28422 }
28423
28424 /* Parse the non-keyword Objective-C params. */
28425
28426 static tree
28427 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28428 tree* attributes)
28429 {
28430 tree params = make_node (TREE_LIST);
28431 cp_token *token = cp_lexer_peek_token (parser->lexer);
28432 *ellipsisp = false; /* Initially, assume no ellipsis. */
28433
28434 while (token->type == CPP_COMMA)
28435 {
28436 cp_parameter_declarator *parmdecl;
28437 tree parm;
28438
28439 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28440 token = cp_lexer_peek_token (parser->lexer);
28441
28442 if (token->type == CPP_ELLIPSIS)
28443 {
28444 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28445 *ellipsisp = true;
28446 token = cp_lexer_peek_token (parser->lexer);
28447 break;
28448 }
28449
28450 /* TODO: parse attributes for tail parameters. */
28451 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28452 parm = grokdeclarator (parmdecl->declarator,
28453 &parmdecl->decl_specifiers,
28454 PARM, /*initialized=*/0,
28455 /*attrlist=*/NULL);
28456
28457 chainon (params, build_tree_list (NULL_TREE, parm));
28458 token = cp_lexer_peek_token (parser->lexer);
28459 }
28460
28461 /* We allow tail attributes for the method. */
28462 if (token->keyword == RID_ATTRIBUTE)
28463 {
28464 if (*attributes == NULL_TREE)
28465 {
28466 *attributes = cp_parser_attributes_opt (parser);
28467 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28468 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28469 return params;
28470 }
28471 else
28472 /* We have an error, but parse the attributes, so that we can
28473 carry on. */
28474 *attributes = cp_parser_attributes_opt (parser);
28475
28476 cp_parser_error (parser,
28477 "method attributes must be specified at the end");
28478 return error_mark_node;
28479 }
28480
28481 return params;
28482 }
28483
28484 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28485
28486 static void
28487 cp_parser_objc_interstitial_code (cp_parser* parser)
28488 {
28489 cp_token *token = cp_lexer_peek_token (parser->lexer);
28490
28491 /* If the next token is `extern' and the following token is a string
28492 literal, then we have a linkage specification. */
28493 if (token->keyword == RID_EXTERN
28494 && cp_parser_is_pure_string_literal
28495 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28496 cp_parser_linkage_specification (parser);
28497 /* Handle #pragma, if any. */
28498 else if (token->type == CPP_PRAGMA)
28499 cp_parser_pragma (parser, pragma_objc_icode, NULL);
28500 /* Allow stray semicolons. */
28501 else if (token->type == CPP_SEMICOLON)
28502 cp_lexer_consume_token (parser->lexer);
28503 /* Mark methods as optional or required, when building protocols. */
28504 else if (token->keyword == RID_AT_OPTIONAL)
28505 {
28506 cp_lexer_consume_token (parser->lexer);
28507 objc_set_method_opt (true);
28508 }
28509 else if (token->keyword == RID_AT_REQUIRED)
28510 {
28511 cp_lexer_consume_token (parser->lexer);
28512 objc_set_method_opt (false);
28513 }
28514 else if (token->keyword == RID_NAMESPACE)
28515 cp_parser_namespace_definition (parser);
28516 /* Other stray characters must generate errors. */
28517 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28518 {
28519 cp_lexer_consume_token (parser->lexer);
28520 error ("stray %qs between Objective-C++ methods",
28521 token->type == CPP_OPEN_BRACE ? "{" : "}");
28522 }
28523 /* Finally, try to parse a block-declaration, or a function-definition. */
28524 else
28525 cp_parser_block_declaration (parser, /*statement_p=*/false);
28526 }
28527
28528 /* Parse a method signature. */
28529
28530 static tree
28531 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28532 {
28533 tree rettype, kwdparms, optparms;
28534 bool ellipsis = false;
28535 bool is_class_method;
28536
28537 is_class_method = cp_parser_objc_method_type (parser);
28538 rettype = cp_parser_objc_typename (parser);
28539 *attributes = NULL_TREE;
28540 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28541 if (kwdparms == error_mark_node)
28542 return error_mark_node;
28543 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28544 if (optparms == error_mark_node)
28545 return error_mark_node;
28546
28547 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28548 }
28549
28550 static bool
28551 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28552 {
28553 tree tattr;
28554 cp_lexer_save_tokens (parser->lexer);
28555 tattr = cp_parser_attributes_opt (parser);
28556 gcc_assert (tattr) ;
28557
28558 /* If the attributes are followed by a method introducer, this is not allowed.
28559 Dump the attributes and flag the situation. */
28560 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28561 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28562 return true;
28563
28564 /* Otherwise, the attributes introduce some interstitial code, possibly so
28565 rewind to allow that check. */
28566 cp_lexer_rollback_tokens (parser->lexer);
28567 return false;
28568 }
28569
28570 /* Parse an Objective-C method prototype list. */
28571
28572 static void
28573 cp_parser_objc_method_prototype_list (cp_parser* parser)
28574 {
28575 cp_token *token = cp_lexer_peek_token (parser->lexer);
28576
28577 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28578 {
28579 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28580 {
28581 tree attributes, sig;
28582 bool is_class_method;
28583 if (token->type == CPP_PLUS)
28584 is_class_method = true;
28585 else
28586 is_class_method = false;
28587 sig = cp_parser_objc_method_signature (parser, &attributes);
28588 if (sig == error_mark_node)
28589 {
28590 cp_parser_skip_to_end_of_block_or_statement (parser);
28591 token = cp_lexer_peek_token (parser->lexer);
28592 continue;
28593 }
28594 objc_add_method_declaration (is_class_method, sig, attributes);
28595 cp_parser_consume_semicolon_at_end_of_statement (parser);
28596 }
28597 else if (token->keyword == RID_AT_PROPERTY)
28598 cp_parser_objc_at_property_declaration (parser);
28599 else if (token->keyword == RID_ATTRIBUTE
28600 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28601 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28602 OPT_Wattributes,
28603 "prefix attributes are ignored for methods");
28604 else
28605 /* Allow for interspersed non-ObjC++ code. */
28606 cp_parser_objc_interstitial_code (parser);
28607
28608 token = cp_lexer_peek_token (parser->lexer);
28609 }
28610
28611 if (token->type != CPP_EOF)
28612 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28613 else
28614 cp_parser_error (parser, "expected %<@end%>");
28615
28616 objc_finish_interface ();
28617 }
28618
28619 /* Parse an Objective-C method definition list. */
28620
28621 static void
28622 cp_parser_objc_method_definition_list (cp_parser* parser)
28623 {
28624 cp_token *token = cp_lexer_peek_token (parser->lexer);
28625
28626 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28627 {
28628 tree meth;
28629
28630 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28631 {
28632 cp_token *ptk;
28633 tree sig, attribute;
28634 bool is_class_method;
28635 if (token->type == CPP_PLUS)
28636 is_class_method = true;
28637 else
28638 is_class_method = false;
28639 push_deferring_access_checks (dk_deferred);
28640 sig = cp_parser_objc_method_signature (parser, &attribute);
28641 if (sig == error_mark_node)
28642 {
28643 cp_parser_skip_to_end_of_block_or_statement (parser);
28644 token = cp_lexer_peek_token (parser->lexer);
28645 continue;
28646 }
28647 objc_start_method_definition (is_class_method, sig, attribute,
28648 NULL_TREE);
28649
28650 /* For historical reasons, we accept an optional semicolon. */
28651 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28652 cp_lexer_consume_token (parser->lexer);
28653
28654 ptk = cp_lexer_peek_token (parser->lexer);
28655 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28656 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28657 {
28658 perform_deferred_access_checks (tf_warning_or_error);
28659 stop_deferring_access_checks ();
28660 meth = cp_parser_function_definition_after_declarator (parser,
28661 false);
28662 pop_deferring_access_checks ();
28663 objc_finish_method_definition (meth);
28664 }
28665 }
28666 /* The following case will be removed once @synthesize is
28667 completely implemented. */
28668 else if (token->keyword == RID_AT_PROPERTY)
28669 cp_parser_objc_at_property_declaration (parser);
28670 else if (token->keyword == RID_AT_SYNTHESIZE)
28671 cp_parser_objc_at_synthesize_declaration (parser);
28672 else if (token->keyword == RID_AT_DYNAMIC)
28673 cp_parser_objc_at_dynamic_declaration (parser);
28674 else if (token->keyword == RID_ATTRIBUTE
28675 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28676 warning_at (token->location, OPT_Wattributes,
28677 "prefix attributes are ignored for methods");
28678 else
28679 /* Allow for interspersed non-ObjC++ code. */
28680 cp_parser_objc_interstitial_code (parser);
28681
28682 token = cp_lexer_peek_token (parser->lexer);
28683 }
28684
28685 if (token->type != CPP_EOF)
28686 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28687 else
28688 cp_parser_error (parser, "expected %<@end%>");
28689
28690 objc_finish_implementation ();
28691 }
28692
28693 /* Parse Objective-C ivars. */
28694
28695 static void
28696 cp_parser_objc_class_ivars (cp_parser* parser)
28697 {
28698 cp_token *token = cp_lexer_peek_token (parser->lexer);
28699
28700 if (token->type != CPP_OPEN_BRACE)
28701 return; /* No ivars specified. */
28702
28703 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28704 token = cp_lexer_peek_token (parser->lexer);
28705
28706 while (token->type != CPP_CLOSE_BRACE
28707 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28708 {
28709 cp_decl_specifier_seq declspecs;
28710 int decl_class_or_enum_p;
28711 tree prefix_attributes;
28712
28713 cp_parser_objc_visibility_spec (parser);
28714
28715 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28716 break;
28717
28718 cp_parser_decl_specifier_seq (parser,
28719 CP_PARSER_FLAGS_OPTIONAL,
28720 &declspecs,
28721 &decl_class_or_enum_p);
28722
28723 /* auto, register, static, extern, mutable. */
28724 if (declspecs.storage_class != sc_none)
28725 {
28726 cp_parser_error (parser, "invalid type for instance variable");
28727 declspecs.storage_class = sc_none;
28728 }
28729
28730 /* thread_local. */
28731 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28732 {
28733 cp_parser_error (parser, "invalid type for instance variable");
28734 declspecs.locations[ds_thread] = 0;
28735 }
28736
28737 /* typedef. */
28738 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28739 {
28740 cp_parser_error (parser, "invalid type for instance variable");
28741 declspecs.locations[ds_typedef] = 0;
28742 }
28743
28744 prefix_attributes = declspecs.attributes;
28745 declspecs.attributes = NULL_TREE;
28746
28747 /* Keep going until we hit the `;' at the end of the
28748 declaration. */
28749 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28750 {
28751 tree width = NULL_TREE, attributes, first_attribute, decl;
28752 cp_declarator *declarator = NULL;
28753 int ctor_dtor_or_conv_p;
28754
28755 /* Check for a (possibly unnamed) bitfield declaration. */
28756 token = cp_lexer_peek_token (parser->lexer);
28757 if (token->type == CPP_COLON)
28758 goto eat_colon;
28759
28760 if (token->type == CPP_NAME
28761 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28762 == CPP_COLON))
28763 {
28764 /* Get the name of the bitfield. */
28765 declarator = make_id_declarator (NULL_TREE,
28766 cp_parser_identifier (parser),
28767 sfk_none);
28768
28769 eat_colon:
28770 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28771 /* Get the width of the bitfield. */
28772 width
28773 = cp_parser_constant_expression (parser);
28774 }
28775 else
28776 {
28777 /* Parse the declarator. */
28778 declarator
28779 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28780 &ctor_dtor_or_conv_p,
28781 /*parenthesized_p=*/NULL,
28782 /*member_p=*/false,
28783 /*friend_p=*/false);
28784 }
28785
28786 /* Look for attributes that apply to the ivar. */
28787 attributes = cp_parser_attributes_opt (parser);
28788 /* Remember which attributes are prefix attributes and
28789 which are not. */
28790 first_attribute = attributes;
28791 /* Combine the attributes. */
28792 attributes = chainon (prefix_attributes, attributes);
28793
28794 if (width)
28795 /* Create the bitfield declaration. */
28796 decl = grokbitfield (declarator, &declspecs,
28797 width,
28798 attributes);
28799 else
28800 decl = grokfield (declarator, &declspecs,
28801 NULL_TREE, /*init_const_expr_p=*/false,
28802 NULL_TREE, attributes);
28803
28804 /* Add the instance variable. */
28805 if (decl != error_mark_node && decl != NULL_TREE)
28806 objc_add_instance_variable (decl);
28807
28808 /* Reset PREFIX_ATTRIBUTES. */
28809 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28810 attributes = TREE_CHAIN (attributes);
28811 if (attributes)
28812 TREE_CHAIN (attributes) = NULL_TREE;
28813
28814 token = cp_lexer_peek_token (parser->lexer);
28815
28816 if (token->type == CPP_COMMA)
28817 {
28818 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28819 continue;
28820 }
28821 break;
28822 }
28823
28824 cp_parser_consume_semicolon_at_end_of_statement (parser);
28825 token = cp_lexer_peek_token (parser->lexer);
28826 }
28827
28828 if (token->keyword == RID_AT_END)
28829 cp_parser_error (parser, "expected %<}%>");
28830
28831 /* Do not consume the RID_AT_END, so it will be read again as terminating
28832 the @interface of @implementation. */
28833 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28834 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28835
28836 /* For historical reasons, we accept an optional semicolon. */
28837 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28838 cp_lexer_consume_token (parser->lexer);
28839 }
28840
28841 /* Parse an Objective-C protocol declaration. */
28842
28843 static void
28844 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28845 {
28846 tree proto, protorefs;
28847 cp_token *tok;
28848
28849 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28850 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28851 {
28852 tok = cp_lexer_peek_token (parser->lexer);
28853 error_at (tok->location, "identifier expected after %<@protocol%>");
28854 cp_parser_consume_semicolon_at_end_of_statement (parser);
28855 return;
28856 }
28857
28858 /* See if we have a forward declaration or a definition. */
28859 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28860
28861 /* Try a forward declaration first. */
28862 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28863 {
28864 while (true)
28865 {
28866 tree id;
28867
28868 id = cp_parser_identifier (parser);
28869 if (id == error_mark_node)
28870 break;
28871
28872 objc_declare_protocol (id, attributes);
28873
28874 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28875 cp_lexer_consume_token (parser->lexer);
28876 else
28877 break;
28878 }
28879 cp_parser_consume_semicolon_at_end_of_statement (parser);
28880 }
28881
28882 /* Ok, we got a full-fledged definition (or at least should). */
28883 else
28884 {
28885 proto = cp_parser_identifier (parser);
28886 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28887 objc_start_protocol (proto, protorefs, attributes);
28888 cp_parser_objc_method_prototype_list (parser);
28889 }
28890 }
28891
28892 /* Parse an Objective-C superclass or category. */
28893
28894 static void
28895 cp_parser_objc_superclass_or_category (cp_parser *parser,
28896 bool iface_p,
28897 tree *super,
28898 tree *categ, bool *is_class_extension)
28899 {
28900 cp_token *next = cp_lexer_peek_token (parser->lexer);
28901
28902 *super = *categ = NULL_TREE;
28903 *is_class_extension = false;
28904 if (next->type == CPP_COLON)
28905 {
28906 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28907 *super = cp_parser_identifier (parser);
28908 }
28909 else if (next->type == CPP_OPEN_PAREN)
28910 {
28911 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28912
28913 /* If there is no category name, and this is an @interface, we
28914 have a class extension. */
28915 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28916 {
28917 *categ = NULL_TREE;
28918 *is_class_extension = true;
28919 }
28920 else
28921 *categ = cp_parser_identifier (parser);
28922
28923 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28924 }
28925 }
28926
28927 /* Parse an Objective-C class interface. */
28928
28929 static void
28930 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28931 {
28932 tree name, super, categ, protos;
28933 bool is_class_extension;
28934
28935 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28936 name = cp_parser_identifier (parser);
28937 if (name == error_mark_node)
28938 {
28939 /* It's hard to recover because even if valid @interface stuff
28940 is to follow, we can't compile it (or validate it) if we
28941 don't even know which class it refers to. Let's assume this
28942 was a stray '@interface' token in the stream and skip it.
28943 */
28944 return;
28945 }
28946 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28947 &is_class_extension);
28948 protos = cp_parser_objc_protocol_refs_opt (parser);
28949
28950 /* We have either a class or a category on our hands. */
28951 if (categ || is_class_extension)
28952 objc_start_category_interface (name, categ, protos, attributes);
28953 else
28954 {
28955 objc_start_class_interface (name, super, protos, attributes);
28956 /* Handle instance variable declarations, if any. */
28957 cp_parser_objc_class_ivars (parser);
28958 objc_continue_interface ();
28959 }
28960
28961 cp_parser_objc_method_prototype_list (parser);
28962 }
28963
28964 /* Parse an Objective-C class implementation. */
28965
28966 static void
28967 cp_parser_objc_class_implementation (cp_parser* parser)
28968 {
28969 tree name, super, categ;
28970 bool is_class_extension;
28971
28972 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28973 name = cp_parser_identifier (parser);
28974 if (name == error_mark_node)
28975 {
28976 /* It's hard to recover because even if valid @implementation
28977 stuff is to follow, we can't compile it (or validate it) if
28978 we don't even know which class it refers to. Let's assume
28979 this was a stray '@implementation' token in the stream and
28980 skip it.
28981 */
28982 return;
28983 }
28984 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
28985 &is_class_extension);
28986
28987 /* We have either a class or a category on our hands. */
28988 if (categ)
28989 objc_start_category_implementation (name, categ);
28990 else
28991 {
28992 objc_start_class_implementation (name, super);
28993 /* Handle instance variable declarations, if any. */
28994 cp_parser_objc_class_ivars (parser);
28995 objc_continue_implementation ();
28996 }
28997
28998 cp_parser_objc_method_definition_list (parser);
28999 }
29000
29001 /* Consume the @end token and finish off the implementation. */
29002
29003 static void
29004 cp_parser_objc_end_implementation (cp_parser* parser)
29005 {
29006 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29007 objc_finish_implementation ();
29008 }
29009
29010 /* Parse an Objective-C declaration. */
29011
29012 static void
29013 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29014 {
29015 /* Try to figure out what kind of declaration is present. */
29016 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29017
29018 if (attributes)
29019 switch (kwd->keyword)
29020 {
29021 case RID_AT_ALIAS:
29022 case RID_AT_CLASS:
29023 case RID_AT_END:
29024 error_at (kwd->location, "attributes may not be specified before"
29025 " the %<@%D%> Objective-C++ keyword",
29026 kwd->u.value);
29027 attributes = NULL;
29028 break;
29029 case RID_AT_IMPLEMENTATION:
29030 warning_at (kwd->location, OPT_Wattributes,
29031 "prefix attributes are ignored before %<@%D%>",
29032 kwd->u.value);
29033 attributes = NULL;
29034 default:
29035 break;
29036 }
29037
29038 switch (kwd->keyword)
29039 {
29040 case RID_AT_ALIAS:
29041 cp_parser_objc_alias_declaration (parser);
29042 break;
29043 case RID_AT_CLASS:
29044 cp_parser_objc_class_declaration (parser);
29045 break;
29046 case RID_AT_PROTOCOL:
29047 cp_parser_objc_protocol_declaration (parser, attributes);
29048 break;
29049 case RID_AT_INTERFACE:
29050 cp_parser_objc_class_interface (parser, attributes);
29051 break;
29052 case RID_AT_IMPLEMENTATION:
29053 cp_parser_objc_class_implementation (parser);
29054 break;
29055 case RID_AT_END:
29056 cp_parser_objc_end_implementation (parser);
29057 break;
29058 default:
29059 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29060 kwd->u.value);
29061 cp_parser_skip_to_end_of_block_or_statement (parser);
29062 }
29063 }
29064
29065 /* Parse an Objective-C try-catch-finally statement.
29066
29067 objc-try-catch-finally-stmt:
29068 @try compound-statement objc-catch-clause-seq [opt]
29069 objc-finally-clause [opt]
29070
29071 objc-catch-clause-seq:
29072 objc-catch-clause objc-catch-clause-seq [opt]
29073
29074 objc-catch-clause:
29075 @catch ( objc-exception-declaration ) compound-statement
29076
29077 objc-finally-clause:
29078 @finally compound-statement
29079
29080 objc-exception-declaration:
29081 parameter-declaration
29082 '...'
29083
29084 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29085
29086 Returns NULL_TREE.
29087
29088 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29089 for C. Keep them in sync. */
29090
29091 static tree
29092 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29093 {
29094 location_t location;
29095 tree stmt;
29096
29097 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29098 location = cp_lexer_peek_token (parser->lexer)->location;
29099 objc_maybe_warn_exceptions (location);
29100 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29101 node, lest it get absorbed into the surrounding block. */
29102 stmt = push_stmt_list ();
29103 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29104 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29105
29106 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29107 {
29108 cp_parameter_declarator *parm;
29109 tree parameter_declaration = error_mark_node;
29110 bool seen_open_paren = false;
29111
29112 cp_lexer_consume_token (parser->lexer);
29113 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29114 seen_open_paren = true;
29115 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29116 {
29117 /* We have "@catch (...)" (where the '...' are literally
29118 what is in the code). Skip the '...'.
29119 parameter_declaration is set to NULL_TREE, and
29120 objc_being_catch_clauses() knows that that means
29121 '...'. */
29122 cp_lexer_consume_token (parser->lexer);
29123 parameter_declaration = NULL_TREE;
29124 }
29125 else
29126 {
29127 /* We have "@catch (NSException *exception)" or something
29128 like that. Parse the parameter declaration. */
29129 parm = cp_parser_parameter_declaration (parser, false, NULL);
29130 if (parm == NULL)
29131 parameter_declaration = error_mark_node;
29132 else
29133 parameter_declaration = grokdeclarator (parm->declarator,
29134 &parm->decl_specifiers,
29135 PARM, /*initialized=*/0,
29136 /*attrlist=*/NULL);
29137 }
29138 if (seen_open_paren)
29139 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29140 else
29141 {
29142 /* If there was no open parenthesis, we are recovering from
29143 an error, and we are trying to figure out what mistake
29144 the user has made. */
29145
29146 /* If there is an immediate closing parenthesis, the user
29147 probably forgot the opening one (ie, they typed "@catch
29148 NSException *e)". Parse the closing parenthesis and keep
29149 going. */
29150 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29151 cp_lexer_consume_token (parser->lexer);
29152
29153 /* If these is no immediate closing parenthesis, the user
29154 probably doesn't know that parenthesis are required at
29155 all (ie, they typed "@catch NSException *e"). So, just
29156 forget about the closing parenthesis and keep going. */
29157 }
29158 objc_begin_catch_clause (parameter_declaration);
29159 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29160 objc_finish_catch_clause ();
29161 }
29162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29163 {
29164 cp_lexer_consume_token (parser->lexer);
29165 location = cp_lexer_peek_token (parser->lexer)->location;
29166 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29167 node, lest it get absorbed into the surrounding block. */
29168 stmt = push_stmt_list ();
29169 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29170 objc_build_finally_clause (location, pop_stmt_list (stmt));
29171 }
29172
29173 return objc_finish_try_stmt ();
29174 }
29175
29176 /* Parse an Objective-C synchronized statement.
29177
29178 objc-synchronized-stmt:
29179 @synchronized ( expression ) compound-statement
29180
29181 Returns NULL_TREE. */
29182
29183 static tree
29184 cp_parser_objc_synchronized_statement (cp_parser *parser)
29185 {
29186 location_t location;
29187 tree lock, stmt;
29188
29189 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29190
29191 location = cp_lexer_peek_token (parser->lexer)->location;
29192 objc_maybe_warn_exceptions (location);
29193 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29194 lock = cp_parser_expression (parser);
29195 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29196
29197 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29198 node, lest it get absorbed into the surrounding block. */
29199 stmt = push_stmt_list ();
29200 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29201
29202 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29203 }
29204
29205 /* Parse an Objective-C throw statement.
29206
29207 objc-throw-stmt:
29208 @throw assignment-expression [opt] ;
29209
29210 Returns a constructed '@throw' statement. */
29211
29212 static tree
29213 cp_parser_objc_throw_statement (cp_parser *parser)
29214 {
29215 tree expr = NULL_TREE;
29216 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29217
29218 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29219
29220 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29221 expr = cp_parser_expression (parser);
29222
29223 cp_parser_consume_semicolon_at_end_of_statement (parser);
29224
29225 return objc_build_throw_stmt (loc, expr);
29226 }
29227
29228 /* Parse an Objective-C statement. */
29229
29230 static tree
29231 cp_parser_objc_statement (cp_parser * parser)
29232 {
29233 /* Try to figure out what kind of declaration is present. */
29234 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29235
29236 switch (kwd->keyword)
29237 {
29238 case RID_AT_TRY:
29239 return cp_parser_objc_try_catch_finally_statement (parser);
29240 case RID_AT_SYNCHRONIZED:
29241 return cp_parser_objc_synchronized_statement (parser);
29242 case RID_AT_THROW:
29243 return cp_parser_objc_throw_statement (parser);
29244 default:
29245 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29246 kwd->u.value);
29247 cp_parser_skip_to_end_of_block_or_statement (parser);
29248 }
29249
29250 return error_mark_node;
29251 }
29252
29253 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29254 look ahead to see if an objc keyword follows the attributes. This
29255 is to detect the use of prefix attributes on ObjC @interface and
29256 @protocol. */
29257
29258 static bool
29259 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29260 {
29261 cp_lexer_save_tokens (parser->lexer);
29262 *attrib = cp_parser_attributes_opt (parser);
29263 gcc_assert (*attrib);
29264 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29265 {
29266 cp_lexer_commit_tokens (parser->lexer);
29267 return true;
29268 }
29269 cp_lexer_rollback_tokens (parser->lexer);
29270 return false;
29271 }
29272
29273 /* This routine is a minimal replacement for
29274 c_parser_struct_declaration () used when parsing the list of
29275 types/names or ObjC++ properties. For example, when parsing the
29276 code
29277
29278 @property (readonly) int a, b, c;
29279
29280 this function is responsible for parsing "int a, int b, int c" and
29281 returning the declarations as CHAIN of DECLs.
29282
29283 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29284 similar parsing. */
29285 static tree
29286 cp_parser_objc_struct_declaration (cp_parser *parser)
29287 {
29288 tree decls = NULL_TREE;
29289 cp_decl_specifier_seq declspecs;
29290 int decl_class_or_enum_p;
29291 tree prefix_attributes;
29292
29293 cp_parser_decl_specifier_seq (parser,
29294 CP_PARSER_FLAGS_NONE,
29295 &declspecs,
29296 &decl_class_or_enum_p);
29297
29298 if (declspecs.type == error_mark_node)
29299 return error_mark_node;
29300
29301 /* auto, register, static, extern, mutable. */
29302 if (declspecs.storage_class != sc_none)
29303 {
29304 cp_parser_error (parser, "invalid type for property");
29305 declspecs.storage_class = sc_none;
29306 }
29307
29308 /* thread_local. */
29309 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29310 {
29311 cp_parser_error (parser, "invalid type for property");
29312 declspecs.locations[ds_thread] = 0;
29313 }
29314
29315 /* typedef. */
29316 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29317 {
29318 cp_parser_error (parser, "invalid type for property");
29319 declspecs.locations[ds_typedef] = 0;
29320 }
29321
29322 prefix_attributes = declspecs.attributes;
29323 declspecs.attributes = NULL_TREE;
29324
29325 /* Keep going until we hit the `;' at the end of the declaration. */
29326 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29327 {
29328 tree attributes, first_attribute, decl;
29329 cp_declarator *declarator;
29330 cp_token *token;
29331
29332 /* Parse the declarator. */
29333 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29334 NULL, NULL, false, false);
29335
29336 /* Look for attributes that apply to the ivar. */
29337 attributes = cp_parser_attributes_opt (parser);
29338 /* Remember which attributes are prefix attributes and
29339 which are not. */
29340 first_attribute = attributes;
29341 /* Combine the attributes. */
29342 attributes = chainon (prefix_attributes, attributes);
29343
29344 decl = grokfield (declarator, &declspecs,
29345 NULL_TREE, /*init_const_expr_p=*/false,
29346 NULL_TREE, attributes);
29347
29348 if (decl == error_mark_node || decl == NULL_TREE)
29349 return error_mark_node;
29350
29351 /* Reset PREFIX_ATTRIBUTES. */
29352 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29353 attributes = TREE_CHAIN (attributes);
29354 if (attributes)
29355 TREE_CHAIN (attributes) = NULL_TREE;
29356
29357 DECL_CHAIN (decl) = decls;
29358 decls = decl;
29359
29360 token = cp_lexer_peek_token (parser->lexer);
29361 if (token->type == CPP_COMMA)
29362 {
29363 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29364 continue;
29365 }
29366 else
29367 break;
29368 }
29369 return decls;
29370 }
29371
29372 /* Parse an Objective-C @property declaration. The syntax is:
29373
29374 objc-property-declaration:
29375 '@property' objc-property-attributes[opt] struct-declaration ;
29376
29377 objc-property-attributes:
29378 '(' objc-property-attribute-list ')'
29379
29380 objc-property-attribute-list:
29381 objc-property-attribute
29382 objc-property-attribute-list, objc-property-attribute
29383
29384 objc-property-attribute
29385 'getter' = identifier
29386 'setter' = identifier
29387 'readonly'
29388 'readwrite'
29389 'assign'
29390 'retain'
29391 'copy'
29392 'nonatomic'
29393
29394 For example:
29395 @property NSString *name;
29396 @property (readonly) id object;
29397 @property (retain, nonatomic, getter=getTheName) id name;
29398 @property int a, b, c;
29399
29400 PS: This function is identical to
29401 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29402 static void
29403 cp_parser_objc_at_property_declaration (cp_parser *parser)
29404 {
29405 /* The following variables hold the attributes of the properties as
29406 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29407 seen. When we see an attribute, we set them to 'true' (if they
29408 are boolean properties) or to the identifier (if they have an
29409 argument, ie, for getter and setter). Note that here we only
29410 parse the list of attributes, check the syntax and accumulate the
29411 attributes that we find. objc_add_property_declaration() will
29412 then process the information. */
29413 bool property_assign = false;
29414 bool property_copy = false;
29415 tree property_getter_ident = NULL_TREE;
29416 bool property_nonatomic = false;
29417 bool property_readonly = false;
29418 bool property_readwrite = false;
29419 bool property_retain = false;
29420 tree property_setter_ident = NULL_TREE;
29421
29422 /* 'properties' is the list of properties that we read. Usually a
29423 single one, but maybe more (eg, in "@property int a, b, c;" there
29424 are three). */
29425 tree properties;
29426 location_t loc;
29427
29428 loc = cp_lexer_peek_token (parser->lexer)->location;
29429
29430 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29431
29432 /* Parse the optional attribute list... */
29433 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29434 {
29435 /* Eat the '('. */
29436 cp_lexer_consume_token (parser->lexer);
29437
29438 while (true)
29439 {
29440 bool syntax_error = false;
29441 cp_token *token = cp_lexer_peek_token (parser->lexer);
29442 enum rid keyword;
29443
29444 if (token->type != CPP_NAME)
29445 {
29446 cp_parser_error (parser, "expected identifier");
29447 break;
29448 }
29449 keyword = C_RID_CODE (token->u.value);
29450 cp_lexer_consume_token (parser->lexer);
29451 switch (keyword)
29452 {
29453 case RID_ASSIGN: property_assign = true; break;
29454 case RID_COPY: property_copy = true; break;
29455 case RID_NONATOMIC: property_nonatomic = true; break;
29456 case RID_READONLY: property_readonly = true; break;
29457 case RID_READWRITE: property_readwrite = true; break;
29458 case RID_RETAIN: property_retain = true; break;
29459
29460 case RID_GETTER:
29461 case RID_SETTER:
29462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29463 {
29464 if (keyword == RID_GETTER)
29465 cp_parser_error (parser,
29466 "missing %<=%> (after %<getter%> attribute)");
29467 else
29468 cp_parser_error (parser,
29469 "missing %<=%> (after %<setter%> attribute)");
29470 syntax_error = true;
29471 break;
29472 }
29473 cp_lexer_consume_token (parser->lexer); /* eat the = */
29474 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29475 {
29476 cp_parser_error (parser, "expected identifier");
29477 syntax_error = true;
29478 break;
29479 }
29480 if (keyword == RID_SETTER)
29481 {
29482 if (property_setter_ident != NULL_TREE)
29483 {
29484 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29485 cp_lexer_consume_token (parser->lexer);
29486 }
29487 else
29488 property_setter_ident = cp_parser_objc_selector (parser);
29489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29490 cp_parser_error (parser, "setter name must terminate with %<:%>");
29491 else
29492 cp_lexer_consume_token (parser->lexer);
29493 }
29494 else
29495 {
29496 if (property_getter_ident != NULL_TREE)
29497 {
29498 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29499 cp_lexer_consume_token (parser->lexer);
29500 }
29501 else
29502 property_getter_ident = cp_parser_objc_selector (parser);
29503 }
29504 break;
29505 default:
29506 cp_parser_error (parser, "unknown property attribute");
29507 syntax_error = true;
29508 break;
29509 }
29510
29511 if (syntax_error)
29512 break;
29513
29514 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29515 cp_lexer_consume_token (parser->lexer);
29516 else
29517 break;
29518 }
29519
29520 /* FIXME: "@property (setter, assign);" will generate a spurious
29521 "error: expected ‘)’ before ‘,’ token". This is because
29522 cp_parser_require, unlike the C counterpart, will produce an
29523 error even if we are in error recovery. */
29524 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29525 {
29526 cp_parser_skip_to_closing_parenthesis (parser,
29527 /*recovering=*/true,
29528 /*or_comma=*/false,
29529 /*consume_paren=*/true);
29530 }
29531 }
29532
29533 /* ... and the property declaration(s). */
29534 properties = cp_parser_objc_struct_declaration (parser);
29535
29536 if (properties == error_mark_node)
29537 {
29538 cp_parser_skip_to_end_of_statement (parser);
29539 /* If the next token is now a `;', consume it. */
29540 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29541 cp_lexer_consume_token (parser->lexer);
29542 return;
29543 }
29544
29545 if (properties == NULL_TREE)
29546 cp_parser_error (parser, "expected identifier");
29547 else
29548 {
29549 /* Comma-separated properties are chained together in
29550 reverse order; add them one by one. */
29551 properties = nreverse (properties);
29552
29553 for (; properties; properties = TREE_CHAIN (properties))
29554 objc_add_property_declaration (loc, copy_node (properties),
29555 property_readonly, property_readwrite,
29556 property_assign, property_retain,
29557 property_copy, property_nonatomic,
29558 property_getter_ident, property_setter_ident);
29559 }
29560
29561 cp_parser_consume_semicolon_at_end_of_statement (parser);
29562 }
29563
29564 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29565
29566 objc-synthesize-declaration:
29567 @synthesize objc-synthesize-identifier-list ;
29568
29569 objc-synthesize-identifier-list:
29570 objc-synthesize-identifier
29571 objc-synthesize-identifier-list, objc-synthesize-identifier
29572
29573 objc-synthesize-identifier
29574 identifier
29575 identifier = identifier
29576
29577 For example:
29578 @synthesize MyProperty;
29579 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29580
29581 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29582 for C. Keep them in sync.
29583 */
29584 static void
29585 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29586 {
29587 tree list = NULL_TREE;
29588 location_t loc;
29589 loc = cp_lexer_peek_token (parser->lexer)->location;
29590
29591 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29592 while (true)
29593 {
29594 tree property, ivar;
29595 property = cp_parser_identifier (parser);
29596 if (property == error_mark_node)
29597 {
29598 cp_parser_consume_semicolon_at_end_of_statement (parser);
29599 return;
29600 }
29601 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29602 {
29603 cp_lexer_consume_token (parser->lexer);
29604 ivar = cp_parser_identifier (parser);
29605 if (ivar == error_mark_node)
29606 {
29607 cp_parser_consume_semicolon_at_end_of_statement (parser);
29608 return;
29609 }
29610 }
29611 else
29612 ivar = NULL_TREE;
29613 list = chainon (list, build_tree_list (ivar, property));
29614 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29615 cp_lexer_consume_token (parser->lexer);
29616 else
29617 break;
29618 }
29619 cp_parser_consume_semicolon_at_end_of_statement (parser);
29620 objc_add_synthesize_declaration (loc, list);
29621 }
29622
29623 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29624
29625 objc-dynamic-declaration:
29626 @dynamic identifier-list ;
29627
29628 For example:
29629 @dynamic MyProperty;
29630 @dynamic MyProperty, AnotherProperty;
29631
29632 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29633 for C. Keep them in sync.
29634 */
29635 static void
29636 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29637 {
29638 tree list = NULL_TREE;
29639 location_t loc;
29640 loc = cp_lexer_peek_token (parser->lexer)->location;
29641
29642 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29643 while (true)
29644 {
29645 tree property;
29646 property = cp_parser_identifier (parser);
29647 if (property == error_mark_node)
29648 {
29649 cp_parser_consume_semicolon_at_end_of_statement (parser);
29650 return;
29651 }
29652 list = chainon (list, build_tree_list (NULL, property));
29653 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29654 cp_lexer_consume_token (parser->lexer);
29655 else
29656 break;
29657 }
29658 cp_parser_consume_semicolon_at_end_of_statement (parser);
29659 objc_add_dynamic_declaration (loc, list);
29660 }
29661
29662 \f
29663 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29664
29665 /* Returns name of the next clause.
29666 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29667 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29668 returned and the token is consumed. */
29669
29670 static pragma_omp_clause
29671 cp_parser_omp_clause_name (cp_parser *parser)
29672 {
29673 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29674
29675 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29676 result = PRAGMA_OACC_CLAUSE_AUTO;
29677 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29678 result = PRAGMA_OMP_CLAUSE_IF;
29679 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29680 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29681 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29682 result = PRAGMA_OACC_CLAUSE_DELETE;
29683 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29684 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29685 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29686 result = PRAGMA_OMP_CLAUSE_FOR;
29687 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29688 {
29689 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29690 const char *p = IDENTIFIER_POINTER (id);
29691
29692 switch (p[0])
29693 {
29694 case 'a':
29695 if (!strcmp ("aligned", p))
29696 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29697 else if (!strcmp ("async", p))
29698 result = PRAGMA_OACC_CLAUSE_ASYNC;
29699 break;
29700 case 'c':
29701 if (!strcmp ("collapse", p))
29702 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29703 else if (!strcmp ("copy", p))
29704 result = PRAGMA_OACC_CLAUSE_COPY;
29705 else if (!strcmp ("copyin", p))
29706 result = PRAGMA_OMP_CLAUSE_COPYIN;
29707 else if (!strcmp ("copyout", p))
29708 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29709 else if (!strcmp ("copyprivate", p))
29710 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29711 else if (!strcmp ("create", p))
29712 result = PRAGMA_OACC_CLAUSE_CREATE;
29713 break;
29714 case 'd':
29715 if (!strcmp ("defaultmap", p))
29716 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29717 else if (!strcmp ("depend", p))
29718 result = PRAGMA_OMP_CLAUSE_DEPEND;
29719 else if (!strcmp ("device", p))
29720 result = PRAGMA_OMP_CLAUSE_DEVICE;
29721 else if (!strcmp ("deviceptr", p))
29722 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29723 else if (!strcmp ("device_resident", p))
29724 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29725 else if (!strcmp ("dist_schedule", p))
29726 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29727 break;
29728 case 'f':
29729 if (!strcmp ("final", p))
29730 result = PRAGMA_OMP_CLAUSE_FINAL;
29731 else if (!strcmp ("firstprivate", p))
29732 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29733 else if (!strcmp ("from", p))
29734 result = PRAGMA_OMP_CLAUSE_FROM;
29735 break;
29736 case 'g':
29737 if (!strcmp ("gang", p))
29738 result = PRAGMA_OACC_CLAUSE_GANG;
29739 else if (!strcmp ("grainsize", p))
29740 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29741 break;
29742 case 'h':
29743 if (!strcmp ("hint", p))
29744 result = PRAGMA_OMP_CLAUSE_HINT;
29745 else if (!strcmp ("host", p))
29746 result = PRAGMA_OACC_CLAUSE_HOST;
29747 break;
29748 case 'i':
29749 if (!strcmp ("inbranch", p))
29750 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29751 else if (!strcmp ("independent", p))
29752 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29753 else if (!strcmp ("is_device_ptr", p))
29754 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29755 break;
29756 case 'l':
29757 if (!strcmp ("lastprivate", p))
29758 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29759 else if (!strcmp ("linear", p))
29760 result = PRAGMA_OMP_CLAUSE_LINEAR;
29761 else if (!strcmp ("link", p))
29762 result = PRAGMA_OMP_CLAUSE_LINK;
29763 break;
29764 case 'm':
29765 if (!strcmp ("map", p))
29766 result = PRAGMA_OMP_CLAUSE_MAP;
29767 else if (!strcmp ("mergeable", p))
29768 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29769 else if (flag_cilkplus && !strcmp ("mask", p))
29770 result = PRAGMA_CILK_CLAUSE_MASK;
29771 break;
29772 case 'n':
29773 if (!strcmp ("nogroup", p))
29774 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29775 else if (!strcmp ("notinbranch", p))
29776 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29777 else if (!strcmp ("nowait", p))
29778 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29779 else if (flag_cilkplus && !strcmp ("nomask", p))
29780 result = PRAGMA_CILK_CLAUSE_NOMASK;
29781 else if (!strcmp ("num_gangs", p))
29782 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29783 else if (!strcmp ("num_tasks", p))
29784 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29785 else if (!strcmp ("num_teams", p))
29786 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29787 else if (!strcmp ("num_threads", p))
29788 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29789 else if (!strcmp ("num_workers", p))
29790 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29791 break;
29792 case 'o':
29793 if (!strcmp ("ordered", p))
29794 result = PRAGMA_OMP_CLAUSE_ORDERED;
29795 break;
29796 case 'p':
29797 if (!strcmp ("parallel", p))
29798 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29799 else if (!strcmp ("present", p))
29800 result = PRAGMA_OACC_CLAUSE_PRESENT;
29801 else if (!strcmp ("present_or_copy", p)
29802 || !strcmp ("pcopy", p))
29803 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29804 else if (!strcmp ("present_or_copyin", p)
29805 || !strcmp ("pcopyin", p))
29806 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29807 else if (!strcmp ("present_or_copyout", p)
29808 || !strcmp ("pcopyout", p))
29809 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29810 else if (!strcmp ("present_or_create", p)
29811 || !strcmp ("pcreate", p))
29812 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29813 else if (!strcmp ("priority", p))
29814 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29815 else if (!strcmp ("proc_bind", p))
29816 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29817 break;
29818 case 'r':
29819 if (!strcmp ("reduction", p))
29820 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29821 break;
29822 case 's':
29823 if (!strcmp ("safelen", p))
29824 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29825 else if (!strcmp ("schedule", p))
29826 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29827 else if (!strcmp ("sections", p))
29828 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29829 else if (!strcmp ("self", p))
29830 result = PRAGMA_OACC_CLAUSE_SELF;
29831 else if (!strcmp ("seq", p))
29832 result = PRAGMA_OACC_CLAUSE_SEQ;
29833 else if (!strcmp ("shared", p))
29834 result = PRAGMA_OMP_CLAUSE_SHARED;
29835 else if (!strcmp ("simd", p))
29836 result = PRAGMA_OMP_CLAUSE_SIMD;
29837 else if (!strcmp ("simdlen", p))
29838 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29839 break;
29840 case 't':
29841 if (!strcmp ("taskgroup", p))
29842 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29843 else if (!strcmp ("thread_limit", p))
29844 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29845 else if (!strcmp ("threads", p))
29846 result = PRAGMA_OMP_CLAUSE_THREADS;
29847 else if (!strcmp ("tile", p))
29848 result = PRAGMA_OACC_CLAUSE_TILE;
29849 else if (!strcmp ("to", p))
29850 result = PRAGMA_OMP_CLAUSE_TO;
29851 break;
29852 case 'u':
29853 if (!strcmp ("uniform", p))
29854 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29855 else if (!strcmp ("untied", p))
29856 result = PRAGMA_OMP_CLAUSE_UNTIED;
29857 else if (!strcmp ("use_device", p))
29858 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29859 else if (!strcmp ("use_device_ptr", p))
29860 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29861 break;
29862 case 'v':
29863 if (!strcmp ("vector", p))
29864 result = PRAGMA_OACC_CLAUSE_VECTOR;
29865 else if (!strcmp ("vector_length", p))
29866 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29867 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29868 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29869 break;
29870 case 'w':
29871 if (!strcmp ("wait", p))
29872 result = PRAGMA_OACC_CLAUSE_WAIT;
29873 else if (!strcmp ("worker", p))
29874 result = PRAGMA_OACC_CLAUSE_WORKER;
29875 break;
29876 }
29877 }
29878
29879 if (result != PRAGMA_OMP_CLAUSE_NONE)
29880 cp_lexer_consume_token (parser->lexer);
29881
29882 return result;
29883 }
29884
29885 /* Validate that a clause of the given type does not already exist. */
29886
29887 static void
29888 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29889 const char *name, location_t location)
29890 {
29891 tree c;
29892
29893 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29894 if (OMP_CLAUSE_CODE (c) == code)
29895 {
29896 error_at (location, "too many %qs clauses", name);
29897 break;
29898 }
29899 }
29900
29901 /* OpenMP 2.5:
29902 variable-list:
29903 identifier
29904 variable-list , identifier
29905
29906 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29907 colon). An opening parenthesis will have been consumed by the caller.
29908
29909 If KIND is nonzero, create the appropriate node and install the decl
29910 in OMP_CLAUSE_DECL and add the node to the head of the list.
29911
29912 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29913 return the list created.
29914
29915 COLON can be NULL if only closing parenthesis should end the list,
29916 or pointer to bool which will receive false if the list is terminated
29917 by closing parenthesis or true if the list is terminated by colon. */
29918
29919 static tree
29920 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29921 tree list, bool *colon)
29922 {
29923 cp_token *token;
29924 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29925 if (colon)
29926 {
29927 parser->colon_corrects_to_scope_p = false;
29928 *colon = false;
29929 }
29930 while (1)
29931 {
29932 tree name, decl;
29933
29934 token = cp_lexer_peek_token (parser->lexer);
29935 if (kind != 0
29936 && current_class_ptr
29937 && cp_parser_is_keyword (token, RID_THIS))
29938 {
29939 decl = finish_this_expr ();
29940 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29941 || CONVERT_EXPR_P (decl))
29942 decl = TREE_OPERAND (decl, 0);
29943 cp_lexer_consume_token (parser->lexer);
29944 }
29945 else
29946 {
29947 name = cp_parser_id_expression (parser, /*template_p=*/false,
29948 /*check_dependency_p=*/true,
29949 /*template_p=*/NULL,
29950 /*declarator_p=*/false,
29951 /*optional_p=*/false);
29952 if (name == error_mark_node)
29953 goto skip_comma;
29954
29955 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29956 if (decl == error_mark_node)
29957 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29958 token->location);
29959 }
29960 if (decl == error_mark_node)
29961 ;
29962 else if (kind != 0)
29963 {
29964 switch (kind)
29965 {
29966 case OMP_CLAUSE__CACHE_:
29967 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29968 {
29969 error_at (token->location, "expected %<[%>");
29970 decl = error_mark_node;
29971 break;
29972 }
29973 /* FALLTHROUGH. */
29974 case OMP_CLAUSE_MAP:
29975 case OMP_CLAUSE_FROM:
29976 case OMP_CLAUSE_TO:
29977 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
29978 {
29979 location_t loc
29980 = cp_lexer_peek_token (parser->lexer)->location;
29981 cp_id_kind idk = CP_ID_KIND_NONE;
29982 cp_lexer_consume_token (parser->lexer);
29983 decl
29984 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
29985 decl, false,
29986 &idk, loc);
29987 }
29988 /* FALLTHROUGH. */
29989 case OMP_CLAUSE_DEPEND:
29990 case OMP_CLAUSE_REDUCTION:
29991 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29992 {
29993 tree low_bound = NULL_TREE, length = NULL_TREE;
29994
29995 parser->colon_corrects_to_scope_p = false;
29996 cp_lexer_consume_token (parser->lexer);
29997 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29998 low_bound = cp_parser_expression (parser);
29999 if (!colon)
30000 parser->colon_corrects_to_scope_p
30001 = saved_colon_corrects_to_scope_p;
30002 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30003 length = integer_one_node;
30004 else
30005 {
30006 /* Look for `:'. */
30007 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30008 goto skip_comma;
30009 if (!cp_lexer_next_token_is (parser->lexer,
30010 CPP_CLOSE_SQUARE))
30011 length = cp_parser_expression (parser);
30012 }
30013 /* Look for the closing `]'. */
30014 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30015 RT_CLOSE_SQUARE))
30016 goto skip_comma;
30017
30018 if (kind == OMP_CLAUSE__CACHE_)
30019 {
30020 if (TREE_CODE (low_bound) != INTEGER_CST
30021 && !TREE_READONLY (low_bound))
30022 {
30023 error_at (token->location,
30024 "%qD is not a constant", low_bound);
30025 decl = error_mark_node;
30026 }
30027
30028 if (TREE_CODE (length) != INTEGER_CST
30029 && !TREE_READONLY (length))
30030 {
30031 error_at (token->location,
30032 "%qD is not a constant", length);
30033 decl = error_mark_node;
30034 }
30035 }
30036
30037 decl = tree_cons (low_bound, length, decl);
30038 }
30039 break;
30040 default:
30041 break;
30042 }
30043
30044 tree u = build_omp_clause (token->location, kind);
30045 OMP_CLAUSE_DECL (u) = decl;
30046 OMP_CLAUSE_CHAIN (u) = list;
30047 list = u;
30048 }
30049 else
30050 list = tree_cons (decl, NULL_TREE, list);
30051
30052 get_comma:
30053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30054 break;
30055 cp_lexer_consume_token (parser->lexer);
30056 }
30057
30058 if (colon)
30059 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30060
30061 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30062 {
30063 *colon = true;
30064 cp_parser_require (parser, CPP_COLON, RT_COLON);
30065 return list;
30066 }
30067
30068 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30069 {
30070 int ending;
30071
30072 /* Try to resync to an unnested comma. Copied from
30073 cp_parser_parenthesized_expression_list. */
30074 skip_comma:
30075 if (colon)
30076 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30077 ending = cp_parser_skip_to_closing_parenthesis (parser,
30078 /*recovering=*/true,
30079 /*or_comma=*/true,
30080 /*consume_paren=*/true);
30081 if (ending < 0)
30082 goto get_comma;
30083 }
30084
30085 return list;
30086 }
30087
30088 /* Similarly, but expect leading and trailing parenthesis. This is a very
30089 common case for omp clauses. */
30090
30091 static tree
30092 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30093 {
30094 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30095 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30096 return list;
30097 }
30098
30099 /* OpenACC 2.0:
30100 copy ( variable-list )
30101 copyin ( variable-list )
30102 copyout ( variable-list )
30103 create ( variable-list )
30104 delete ( variable-list )
30105 present ( variable-list )
30106 present_or_copy ( variable-list )
30107 pcopy ( variable-list )
30108 present_or_copyin ( variable-list )
30109 pcopyin ( variable-list )
30110 present_or_copyout ( variable-list )
30111 pcopyout ( variable-list )
30112 present_or_create ( variable-list )
30113 pcreate ( variable-list ) */
30114
30115 static tree
30116 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30117 tree list)
30118 {
30119 enum gomp_map_kind kind;
30120 switch (c_kind)
30121 {
30122 case PRAGMA_OACC_CLAUSE_COPY:
30123 kind = GOMP_MAP_FORCE_TOFROM;
30124 break;
30125 case PRAGMA_OACC_CLAUSE_COPYIN:
30126 kind = GOMP_MAP_FORCE_TO;
30127 break;
30128 case PRAGMA_OACC_CLAUSE_COPYOUT:
30129 kind = GOMP_MAP_FORCE_FROM;
30130 break;
30131 case PRAGMA_OACC_CLAUSE_CREATE:
30132 kind = GOMP_MAP_FORCE_ALLOC;
30133 break;
30134 case PRAGMA_OACC_CLAUSE_DELETE:
30135 kind = GOMP_MAP_DELETE;
30136 break;
30137 case PRAGMA_OACC_CLAUSE_DEVICE:
30138 kind = GOMP_MAP_FORCE_TO;
30139 break;
30140 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30141 kind = GOMP_MAP_DEVICE_RESIDENT;
30142 break;
30143 case PRAGMA_OACC_CLAUSE_HOST:
30144 case PRAGMA_OACC_CLAUSE_SELF:
30145 kind = GOMP_MAP_FORCE_FROM;
30146 break;
30147 case PRAGMA_OACC_CLAUSE_LINK:
30148 kind = GOMP_MAP_LINK;
30149 break;
30150 case PRAGMA_OACC_CLAUSE_PRESENT:
30151 kind = GOMP_MAP_FORCE_PRESENT;
30152 break;
30153 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30154 kind = GOMP_MAP_TOFROM;
30155 break;
30156 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30157 kind = GOMP_MAP_TO;
30158 break;
30159 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30160 kind = GOMP_MAP_FROM;
30161 break;
30162 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30163 kind = GOMP_MAP_ALLOC;
30164 break;
30165 default:
30166 gcc_unreachable ();
30167 }
30168 tree nl, c;
30169 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30170
30171 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30172 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30173
30174 return nl;
30175 }
30176
30177 /* OpenACC 2.0:
30178 deviceptr ( variable-list ) */
30179
30180 static tree
30181 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30182 {
30183 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30184 tree vars, t;
30185
30186 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30187 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30188 variable-list must only allow for pointer variables. */
30189 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30190 for (t = vars; t; t = TREE_CHAIN (t))
30191 {
30192 tree v = TREE_PURPOSE (t);
30193 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30194 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30195 OMP_CLAUSE_DECL (u) = v;
30196 OMP_CLAUSE_CHAIN (u) = list;
30197 list = u;
30198 }
30199
30200 return list;
30201 }
30202
30203 /* OpenACC 2.0:
30204 auto
30205 independent
30206 nohost
30207 seq */
30208
30209 static tree
30210 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30211 enum omp_clause_code code,
30212 tree list, location_t location)
30213 {
30214 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30215 tree c = build_omp_clause (location, code);
30216 OMP_CLAUSE_CHAIN (c) = list;
30217 return c;
30218 }
30219
30220 /* OpenACC:
30221 num_gangs ( expression )
30222 num_workers ( expression )
30223 vector_length ( expression ) */
30224
30225 static tree
30226 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30227 const char *str, tree list)
30228 {
30229 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30230
30231 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30232 return list;
30233
30234 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30235
30236 if (t == error_mark_node
30237 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30238 {
30239 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30240 /*or_comma=*/false,
30241 /*consume_paren=*/true);
30242 return list;
30243 }
30244
30245 check_no_duplicate_clause (list, code, str, loc);
30246
30247 tree c = build_omp_clause (loc, code);
30248 OMP_CLAUSE_OPERAND (c, 0) = t;
30249 OMP_CLAUSE_CHAIN (c) = list;
30250 return c;
30251 }
30252
30253 /* OpenACC:
30254
30255 gang [( gang-arg-list )]
30256 worker [( [num:] int-expr )]
30257 vector [( [length:] int-expr )]
30258
30259 where gang-arg is one of:
30260
30261 [num:] int-expr
30262 static: size-expr
30263
30264 and size-expr may be:
30265
30266 *
30267 int-expr
30268 */
30269
30270 static tree
30271 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30272 const char *str, tree list)
30273 {
30274 const char *id = "num";
30275 cp_lexer *lexer = parser->lexer;
30276 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30277 location_t loc = cp_lexer_peek_token (lexer)->location;
30278
30279 if (kind == OMP_CLAUSE_VECTOR)
30280 id = "length";
30281
30282 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30283 {
30284 cp_lexer_consume_token (lexer);
30285
30286 do
30287 {
30288 cp_token *next = cp_lexer_peek_token (lexer);
30289 int idx = 0;
30290
30291 /* Gang static argument. */
30292 if (kind == OMP_CLAUSE_GANG
30293 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30294 {
30295 cp_lexer_consume_token (lexer);
30296
30297 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30298 goto cleanup_error;
30299
30300 idx = 1;
30301 if (ops[idx] != NULL)
30302 {
30303 cp_parser_error (parser, "too many %<static%> arguments");
30304 goto cleanup_error;
30305 }
30306
30307 /* Check for the '*' argument. */
30308 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30309 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30310 || cp_lexer_nth_token_is (parser->lexer, 2,
30311 CPP_CLOSE_PAREN)))
30312 {
30313 cp_lexer_consume_token (lexer);
30314 ops[idx] = integer_minus_one_node;
30315
30316 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30317 {
30318 cp_lexer_consume_token (lexer);
30319 continue;
30320 }
30321 else break;
30322 }
30323 }
30324 /* Worker num: argument and vector length: arguments. */
30325 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30326 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30327 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30328 {
30329 cp_lexer_consume_token (lexer); /* id */
30330 cp_lexer_consume_token (lexer); /* ':' */
30331 }
30332
30333 /* Now collect the actual argument. */
30334 if (ops[idx] != NULL_TREE)
30335 {
30336 cp_parser_error (parser, "unexpected argument");
30337 goto cleanup_error;
30338 }
30339
30340 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30341 false);
30342 if (expr == error_mark_node)
30343 goto cleanup_error;
30344
30345 mark_exp_read (expr);
30346 ops[idx] = expr;
30347
30348 if (kind == OMP_CLAUSE_GANG
30349 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30350 {
30351 cp_lexer_consume_token (lexer);
30352 continue;
30353 }
30354 break;
30355 }
30356 while (1);
30357
30358 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30359 goto cleanup_error;
30360 }
30361
30362 check_no_duplicate_clause (list, kind, str, loc);
30363
30364 c = build_omp_clause (loc, kind);
30365
30366 if (ops[1])
30367 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30368
30369 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30370 OMP_CLAUSE_CHAIN (c) = list;
30371
30372 return c;
30373
30374 cleanup_error:
30375 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30376 return list;
30377 }
30378
30379 /* OpenACC 2.0:
30380 tile ( size-expr-list ) */
30381
30382 static tree
30383 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30384 {
30385 tree c, expr = error_mark_node;
30386 tree tile = NULL_TREE;
30387
30388 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30389
30390 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30391 return list;
30392
30393 do
30394 {
30395 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30396 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30397 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30398 {
30399 cp_lexer_consume_token (parser->lexer);
30400 expr = integer_minus_one_node;
30401 }
30402 else
30403 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30404
30405 if (expr == error_mark_node)
30406 return list;
30407
30408 tile = tree_cons (NULL_TREE, expr, tile);
30409
30410 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30411 cp_lexer_consume_token (parser->lexer);
30412 }
30413 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30414
30415 /* Consume the trailing ')'. */
30416 cp_lexer_consume_token (parser->lexer);
30417
30418 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30419 tile = nreverse (tile);
30420 OMP_CLAUSE_TILE_LIST (c) = tile;
30421 OMP_CLAUSE_CHAIN (c) = list;
30422 return c;
30423 }
30424
30425 /* OpenACC 2.0
30426 Parse wait clause or directive parameters. */
30427
30428 static tree
30429 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30430 {
30431 vec<tree, va_gc> *args;
30432 tree t, args_tree;
30433
30434 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30435 /*cast_p=*/false,
30436 /*allow_expansion_p=*/true,
30437 /*non_constant_p=*/NULL);
30438
30439 if (args == NULL || args->length () == 0)
30440 {
30441 cp_parser_error (parser, "expected integer expression before ')'");
30442 if (args != NULL)
30443 release_tree_vector (args);
30444 return list;
30445 }
30446
30447 args_tree = build_tree_list_vec (args);
30448
30449 release_tree_vector (args);
30450
30451 for (t = args_tree; t; t = TREE_CHAIN (t))
30452 {
30453 tree targ = TREE_VALUE (t);
30454
30455 if (targ != error_mark_node)
30456 {
30457 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30458 error ("%<wait%> expression must be integral");
30459 else
30460 {
30461 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30462
30463 mark_rvalue_use (targ);
30464 OMP_CLAUSE_DECL (c) = targ;
30465 OMP_CLAUSE_CHAIN (c) = list;
30466 list = c;
30467 }
30468 }
30469 }
30470
30471 return list;
30472 }
30473
30474 /* OpenACC:
30475 wait ( int-expr-list ) */
30476
30477 static tree
30478 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30479 {
30480 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30481
30482 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30483 return list;
30484
30485 list = cp_parser_oacc_wait_list (parser, location, list);
30486
30487 return list;
30488 }
30489
30490 /* OpenMP 3.0:
30491 collapse ( constant-expression ) */
30492
30493 static tree
30494 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30495 {
30496 tree c, num;
30497 location_t loc;
30498 HOST_WIDE_INT n;
30499
30500 loc = cp_lexer_peek_token (parser->lexer)->location;
30501 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30502 return list;
30503
30504 num = cp_parser_constant_expression (parser);
30505
30506 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30507 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30508 /*or_comma=*/false,
30509 /*consume_paren=*/true);
30510
30511 if (num == error_mark_node)
30512 return list;
30513 num = fold_non_dependent_expr (num);
30514 if (!tree_fits_shwi_p (num)
30515 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30516 || (n = tree_to_shwi (num)) <= 0
30517 || (int) n != n)
30518 {
30519 error_at (loc, "collapse argument needs positive constant integer expression");
30520 return list;
30521 }
30522
30523 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30524 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30525 OMP_CLAUSE_CHAIN (c) = list;
30526 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30527
30528 return c;
30529 }
30530
30531 /* OpenMP 2.5:
30532 default ( shared | none )
30533
30534 OpenACC 2.0
30535 default (none) */
30536
30537 static tree
30538 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30539 location_t location, bool is_oacc)
30540 {
30541 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30542 tree c;
30543
30544 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30545 return list;
30546 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30547 {
30548 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30549 const char *p = IDENTIFIER_POINTER (id);
30550
30551 switch (p[0])
30552 {
30553 case 'n':
30554 if (strcmp ("none", p) != 0)
30555 goto invalid_kind;
30556 kind = OMP_CLAUSE_DEFAULT_NONE;
30557 break;
30558
30559 case 's':
30560 if (strcmp ("shared", p) != 0 || is_oacc)
30561 goto invalid_kind;
30562 kind = OMP_CLAUSE_DEFAULT_SHARED;
30563 break;
30564
30565 default:
30566 goto invalid_kind;
30567 }
30568
30569 cp_lexer_consume_token (parser->lexer);
30570 }
30571 else
30572 {
30573 invalid_kind:
30574 if (is_oacc)
30575 cp_parser_error (parser, "expected %<none%>");
30576 else
30577 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30578 }
30579
30580 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30581 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30582 /*or_comma=*/false,
30583 /*consume_paren=*/true);
30584
30585 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30586 return list;
30587
30588 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30589 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30590 OMP_CLAUSE_CHAIN (c) = list;
30591 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30592
30593 return c;
30594 }
30595
30596 /* OpenMP 3.1:
30597 final ( expression ) */
30598
30599 static tree
30600 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30601 {
30602 tree t, c;
30603
30604 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30605 return list;
30606
30607 t = cp_parser_condition (parser);
30608
30609 if (t == error_mark_node
30610 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30611 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30612 /*or_comma=*/false,
30613 /*consume_paren=*/true);
30614
30615 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30616
30617 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30618 OMP_CLAUSE_FINAL_EXPR (c) = t;
30619 OMP_CLAUSE_CHAIN (c) = list;
30620
30621 return c;
30622 }
30623
30624 /* OpenMP 2.5:
30625 if ( expression )
30626
30627 OpenMP 4.5:
30628 if ( directive-name-modifier : expression )
30629
30630 directive-name-modifier:
30631 parallel | task | taskloop | target data | target | target update
30632 | target enter data | target exit data */
30633
30634 static tree
30635 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30636 bool is_omp)
30637 {
30638 tree t, c;
30639 enum tree_code if_modifier = ERROR_MARK;
30640
30641 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30642 return list;
30643
30644 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30645 {
30646 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30647 const char *p = IDENTIFIER_POINTER (id);
30648 int n = 2;
30649
30650 if (strcmp ("parallel", p) == 0)
30651 if_modifier = OMP_PARALLEL;
30652 else if (strcmp ("task", p) == 0)
30653 if_modifier = OMP_TASK;
30654 else if (strcmp ("taskloop", p) == 0)
30655 if_modifier = OMP_TASKLOOP;
30656 else if (strcmp ("target", p) == 0)
30657 {
30658 if_modifier = OMP_TARGET;
30659 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30660 {
30661 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30662 p = IDENTIFIER_POINTER (id);
30663 if (strcmp ("data", p) == 0)
30664 if_modifier = OMP_TARGET_DATA;
30665 else if (strcmp ("update", p) == 0)
30666 if_modifier = OMP_TARGET_UPDATE;
30667 else if (strcmp ("enter", p) == 0)
30668 if_modifier = OMP_TARGET_ENTER_DATA;
30669 else if (strcmp ("exit", p) == 0)
30670 if_modifier = OMP_TARGET_EXIT_DATA;
30671 if (if_modifier != OMP_TARGET)
30672 n = 3;
30673 else
30674 {
30675 location_t loc
30676 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30677 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30678 "or %<exit%>");
30679 if_modifier = ERROR_MARK;
30680 }
30681 if (if_modifier == OMP_TARGET_ENTER_DATA
30682 || if_modifier == OMP_TARGET_EXIT_DATA)
30683 {
30684 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30685 {
30686 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30687 p = IDENTIFIER_POINTER (id);
30688 if (strcmp ("data", p) == 0)
30689 n = 4;
30690 }
30691 if (n != 4)
30692 {
30693 location_t loc
30694 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30695 error_at (loc, "expected %<data%>");
30696 if_modifier = ERROR_MARK;
30697 }
30698 }
30699 }
30700 }
30701 if (if_modifier != ERROR_MARK)
30702 {
30703 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30704 {
30705 while (n-- > 0)
30706 cp_lexer_consume_token (parser->lexer);
30707 }
30708 else
30709 {
30710 if (n > 2)
30711 {
30712 location_t loc
30713 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30714 error_at (loc, "expected %<:%>");
30715 }
30716 if_modifier = ERROR_MARK;
30717 }
30718 }
30719 }
30720
30721 t = cp_parser_condition (parser);
30722
30723 if (t == error_mark_node
30724 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30725 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30726 /*or_comma=*/false,
30727 /*consume_paren=*/true);
30728
30729 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30730 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30731 {
30732 if (if_modifier != ERROR_MARK
30733 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30734 {
30735 const char *p = NULL;
30736 switch (if_modifier)
30737 {
30738 case OMP_PARALLEL: p = "parallel"; break;
30739 case OMP_TASK: p = "task"; break;
30740 case OMP_TASKLOOP: p = "taskloop"; break;
30741 case OMP_TARGET_DATA: p = "target data"; break;
30742 case OMP_TARGET: p = "target"; break;
30743 case OMP_TARGET_UPDATE: p = "target update"; break;
30744 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30745 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30746 default: gcc_unreachable ();
30747 }
30748 error_at (location, "too many %<if%> clauses with %qs modifier",
30749 p);
30750 return list;
30751 }
30752 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30753 {
30754 if (!is_omp)
30755 error_at (location, "too many %<if%> clauses");
30756 else
30757 error_at (location, "too many %<if%> clauses without modifier");
30758 return list;
30759 }
30760 else if (if_modifier == ERROR_MARK
30761 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30762 {
30763 error_at (location, "if any %<if%> clause has modifier, then all "
30764 "%<if%> clauses have to use modifier");
30765 return list;
30766 }
30767 }
30768
30769 c = build_omp_clause (location, OMP_CLAUSE_IF);
30770 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30771 OMP_CLAUSE_IF_EXPR (c) = t;
30772 OMP_CLAUSE_CHAIN (c) = list;
30773
30774 return c;
30775 }
30776
30777 /* OpenMP 3.1:
30778 mergeable */
30779
30780 static tree
30781 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30782 tree list, location_t location)
30783 {
30784 tree c;
30785
30786 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30787 location);
30788
30789 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30790 OMP_CLAUSE_CHAIN (c) = list;
30791 return c;
30792 }
30793
30794 /* OpenMP 2.5:
30795 nowait */
30796
30797 static tree
30798 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30799 tree list, location_t location)
30800 {
30801 tree c;
30802
30803 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30804
30805 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30806 OMP_CLAUSE_CHAIN (c) = list;
30807 return c;
30808 }
30809
30810 /* OpenMP 2.5:
30811 num_threads ( expression ) */
30812
30813 static tree
30814 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30815 location_t location)
30816 {
30817 tree t, c;
30818
30819 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30820 return list;
30821
30822 t = cp_parser_expression (parser);
30823
30824 if (t == error_mark_node
30825 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30826 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30827 /*or_comma=*/false,
30828 /*consume_paren=*/true);
30829
30830 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30831 "num_threads", location);
30832
30833 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30834 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30835 OMP_CLAUSE_CHAIN (c) = list;
30836
30837 return c;
30838 }
30839
30840 /* OpenMP 4.5:
30841 num_tasks ( expression ) */
30842
30843 static tree
30844 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30845 location_t location)
30846 {
30847 tree t, c;
30848
30849 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30850 return list;
30851
30852 t = cp_parser_expression (parser);
30853
30854 if (t == error_mark_node
30855 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30856 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30857 /*or_comma=*/false,
30858 /*consume_paren=*/true);
30859
30860 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30861 "num_tasks", location);
30862
30863 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30864 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30865 OMP_CLAUSE_CHAIN (c) = list;
30866
30867 return c;
30868 }
30869
30870 /* OpenMP 4.5:
30871 grainsize ( expression ) */
30872
30873 static tree
30874 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30875 location_t location)
30876 {
30877 tree t, c;
30878
30879 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30880 return list;
30881
30882 t = cp_parser_expression (parser);
30883
30884 if (t == error_mark_node
30885 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30886 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30887 /*or_comma=*/false,
30888 /*consume_paren=*/true);
30889
30890 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30891 "grainsize", location);
30892
30893 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30894 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30895 OMP_CLAUSE_CHAIN (c) = list;
30896
30897 return c;
30898 }
30899
30900 /* OpenMP 4.5:
30901 priority ( expression ) */
30902
30903 static tree
30904 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30905 location_t location)
30906 {
30907 tree t, c;
30908
30909 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30910 return list;
30911
30912 t = cp_parser_expression (parser);
30913
30914 if (t == error_mark_node
30915 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30916 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30917 /*or_comma=*/false,
30918 /*consume_paren=*/true);
30919
30920 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30921 "priority", location);
30922
30923 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30924 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30925 OMP_CLAUSE_CHAIN (c) = list;
30926
30927 return c;
30928 }
30929
30930 /* OpenMP 4.5:
30931 hint ( expression ) */
30932
30933 static tree
30934 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30935 location_t location)
30936 {
30937 tree t, c;
30938
30939 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30940 return list;
30941
30942 t = cp_parser_expression (parser);
30943
30944 if (t == error_mark_node
30945 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30946 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30947 /*or_comma=*/false,
30948 /*consume_paren=*/true);
30949
30950 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30951
30952 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30953 OMP_CLAUSE_HINT_EXPR (c) = t;
30954 OMP_CLAUSE_CHAIN (c) = list;
30955
30956 return c;
30957 }
30958
30959 /* OpenMP 4.5:
30960 defaultmap ( tofrom : scalar ) */
30961
30962 static tree
30963 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30964 location_t location)
30965 {
30966 tree c, id;
30967 const char *p;
30968
30969 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30970 return list;
30971
30972 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30973 {
30974 cp_parser_error (parser, "expected %<tofrom%>");
30975 goto out_err;
30976 }
30977 id = cp_lexer_peek_token (parser->lexer)->u.value;
30978 p = IDENTIFIER_POINTER (id);
30979 if (strcmp (p, "tofrom") != 0)
30980 {
30981 cp_parser_error (parser, "expected %<tofrom%>");
30982 goto out_err;
30983 }
30984 cp_lexer_consume_token (parser->lexer);
30985 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30986 goto out_err;
30987
30988 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30989 {
30990 cp_parser_error (parser, "expected %<scalar%>");
30991 goto out_err;
30992 }
30993 id = cp_lexer_peek_token (parser->lexer)->u.value;
30994 p = IDENTIFIER_POINTER (id);
30995 if (strcmp (p, "scalar") != 0)
30996 {
30997 cp_parser_error (parser, "expected %<scalar%>");
30998 goto out_err;
30999 }
31000 cp_lexer_consume_token (parser->lexer);
31001 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31002 goto out_err;
31003
31004 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31005 location);
31006
31007 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31008 OMP_CLAUSE_CHAIN (c) = list;
31009 return c;
31010
31011 out_err:
31012 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31013 /*or_comma=*/false,
31014 /*consume_paren=*/true);
31015 return list;
31016 }
31017
31018 /* OpenMP 2.5:
31019 ordered
31020
31021 OpenMP 4.5:
31022 ordered ( constant-expression ) */
31023
31024 static tree
31025 cp_parser_omp_clause_ordered (cp_parser *parser,
31026 tree list, location_t location)
31027 {
31028 tree c, num = NULL_TREE;
31029 HOST_WIDE_INT n;
31030
31031 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31032 "ordered", location);
31033
31034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31035 {
31036 cp_lexer_consume_token (parser->lexer);
31037
31038 num = cp_parser_constant_expression (parser);
31039
31040 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31041 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31042 /*or_comma=*/false,
31043 /*consume_paren=*/true);
31044
31045 if (num == error_mark_node)
31046 return list;
31047 num = fold_non_dependent_expr (num);
31048 if (!tree_fits_shwi_p (num)
31049 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31050 || (n = tree_to_shwi (num)) <= 0
31051 || (int) n != n)
31052 {
31053 error_at (location,
31054 "ordered argument needs positive constant integer "
31055 "expression");
31056 return list;
31057 }
31058 }
31059
31060 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31061 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31062 OMP_CLAUSE_CHAIN (c) = list;
31063 return c;
31064 }
31065
31066 /* OpenMP 2.5:
31067 reduction ( reduction-operator : variable-list )
31068
31069 reduction-operator:
31070 One of: + * - & ^ | && ||
31071
31072 OpenMP 3.1:
31073
31074 reduction-operator:
31075 One of: + * - & ^ | && || min max
31076
31077 OpenMP 4.0:
31078
31079 reduction-operator:
31080 One of: + * - & ^ | && ||
31081 id-expression */
31082
31083 static tree
31084 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31085 {
31086 enum tree_code code = ERROR_MARK;
31087 tree nlist, c, id = NULL_TREE;
31088
31089 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31090 return list;
31091
31092 switch (cp_lexer_peek_token (parser->lexer)->type)
31093 {
31094 case CPP_PLUS: code = PLUS_EXPR; break;
31095 case CPP_MULT: code = MULT_EXPR; break;
31096 case CPP_MINUS: code = MINUS_EXPR; break;
31097 case CPP_AND: code = BIT_AND_EXPR; break;
31098 case CPP_XOR: code = BIT_XOR_EXPR; break;
31099 case CPP_OR: code = BIT_IOR_EXPR; break;
31100 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31101 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31102 default: break;
31103 }
31104
31105 if (code != ERROR_MARK)
31106 cp_lexer_consume_token (parser->lexer);
31107 else
31108 {
31109 bool saved_colon_corrects_to_scope_p;
31110 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31111 parser->colon_corrects_to_scope_p = false;
31112 id = cp_parser_id_expression (parser, /*template_p=*/false,
31113 /*check_dependency_p=*/true,
31114 /*template_p=*/NULL,
31115 /*declarator_p=*/false,
31116 /*optional_p=*/false);
31117 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31118 if (identifier_p (id))
31119 {
31120 const char *p = IDENTIFIER_POINTER (id);
31121
31122 if (strcmp (p, "min") == 0)
31123 code = MIN_EXPR;
31124 else if (strcmp (p, "max") == 0)
31125 code = MAX_EXPR;
31126 else if (id == ansi_opname (PLUS_EXPR))
31127 code = PLUS_EXPR;
31128 else if (id == ansi_opname (MULT_EXPR))
31129 code = MULT_EXPR;
31130 else if (id == ansi_opname (MINUS_EXPR))
31131 code = MINUS_EXPR;
31132 else if (id == ansi_opname (BIT_AND_EXPR))
31133 code = BIT_AND_EXPR;
31134 else if (id == ansi_opname (BIT_IOR_EXPR))
31135 code = BIT_IOR_EXPR;
31136 else if (id == ansi_opname (BIT_XOR_EXPR))
31137 code = BIT_XOR_EXPR;
31138 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31139 code = TRUTH_ANDIF_EXPR;
31140 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31141 code = TRUTH_ORIF_EXPR;
31142 id = omp_reduction_id (code, id, NULL_TREE);
31143 tree scope = parser->scope;
31144 if (scope)
31145 id = build_qualified_name (NULL_TREE, scope, id, false);
31146 parser->scope = NULL_TREE;
31147 parser->qualifying_scope = NULL_TREE;
31148 parser->object_scope = NULL_TREE;
31149 }
31150 else
31151 {
31152 error ("invalid reduction-identifier");
31153 resync_fail:
31154 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31155 /*or_comma=*/false,
31156 /*consume_paren=*/true);
31157 return list;
31158 }
31159 }
31160
31161 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31162 goto resync_fail;
31163
31164 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31165 NULL);
31166 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31167 {
31168 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31169 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31170 }
31171
31172 return nlist;
31173 }
31174
31175 /* OpenMP 2.5:
31176 schedule ( schedule-kind )
31177 schedule ( schedule-kind , expression )
31178
31179 schedule-kind:
31180 static | dynamic | guided | runtime | auto
31181
31182 OpenMP 4.5:
31183 schedule ( schedule-modifier : schedule-kind )
31184 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31185
31186 schedule-modifier:
31187 simd
31188 monotonic
31189 nonmonotonic */
31190
31191 static tree
31192 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31193 {
31194 tree c, t;
31195 int modifiers = 0, nmodifiers = 0;
31196
31197 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31198 return list;
31199
31200 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31201
31202 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31203 {
31204 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31205 const char *p = IDENTIFIER_POINTER (id);
31206 if (strcmp ("simd", p) == 0)
31207 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31208 else if (strcmp ("monotonic", p) == 0)
31209 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31210 else if (strcmp ("nonmonotonic", p) == 0)
31211 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31212 else
31213 break;
31214 cp_lexer_consume_token (parser->lexer);
31215 if (nmodifiers++ == 0
31216 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31217 cp_lexer_consume_token (parser->lexer);
31218 else
31219 {
31220 cp_parser_require (parser, CPP_COLON, RT_COLON);
31221 break;
31222 }
31223 }
31224
31225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31226 {
31227 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31228 const char *p = IDENTIFIER_POINTER (id);
31229
31230 switch (p[0])
31231 {
31232 case 'd':
31233 if (strcmp ("dynamic", p) != 0)
31234 goto invalid_kind;
31235 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31236 break;
31237
31238 case 'g':
31239 if (strcmp ("guided", p) != 0)
31240 goto invalid_kind;
31241 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31242 break;
31243
31244 case 'r':
31245 if (strcmp ("runtime", p) != 0)
31246 goto invalid_kind;
31247 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31248 break;
31249
31250 default:
31251 goto invalid_kind;
31252 }
31253 }
31254 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31255 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31256 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31257 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31258 else
31259 goto invalid_kind;
31260 cp_lexer_consume_token (parser->lexer);
31261
31262 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31263 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31264 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31265 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31266 {
31267 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31268 "specified");
31269 modifiers = 0;
31270 }
31271
31272 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31273 {
31274 cp_token *token;
31275 cp_lexer_consume_token (parser->lexer);
31276
31277 token = cp_lexer_peek_token (parser->lexer);
31278 t = cp_parser_assignment_expression (parser);
31279
31280 if (t == error_mark_node)
31281 goto resync_fail;
31282 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31283 error_at (token->location, "schedule %<runtime%> does not take "
31284 "a %<chunk_size%> parameter");
31285 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31286 error_at (token->location, "schedule %<auto%> does not take "
31287 "a %<chunk_size%> parameter");
31288 else
31289 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31290
31291 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31292 goto resync_fail;
31293 }
31294 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31295 goto resync_fail;
31296
31297 OMP_CLAUSE_SCHEDULE_KIND (c)
31298 = (enum omp_clause_schedule_kind)
31299 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31300
31301 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31302 OMP_CLAUSE_CHAIN (c) = list;
31303 return c;
31304
31305 invalid_kind:
31306 cp_parser_error (parser, "invalid schedule kind");
31307 resync_fail:
31308 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31309 /*or_comma=*/false,
31310 /*consume_paren=*/true);
31311 return list;
31312 }
31313
31314 /* OpenMP 3.0:
31315 untied */
31316
31317 static tree
31318 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31319 tree list, location_t location)
31320 {
31321 tree c;
31322
31323 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31324
31325 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31326 OMP_CLAUSE_CHAIN (c) = list;
31327 return c;
31328 }
31329
31330 /* OpenMP 4.0:
31331 inbranch
31332 notinbranch */
31333
31334 static tree
31335 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31336 tree list, location_t location)
31337 {
31338 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31339 tree c = build_omp_clause (location, code);
31340 OMP_CLAUSE_CHAIN (c) = list;
31341 return c;
31342 }
31343
31344 /* OpenMP 4.0:
31345 parallel
31346 for
31347 sections
31348 taskgroup */
31349
31350 static tree
31351 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31352 enum omp_clause_code code,
31353 tree list, location_t location)
31354 {
31355 tree c = build_omp_clause (location, code);
31356 OMP_CLAUSE_CHAIN (c) = list;
31357 return c;
31358 }
31359
31360 /* OpenMP 4.5:
31361 nogroup */
31362
31363 static tree
31364 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31365 tree list, location_t location)
31366 {
31367 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31368 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31369 OMP_CLAUSE_CHAIN (c) = list;
31370 return c;
31371 }
31372
31373 /* OpenMP 4.5:
31374 simd
31375 threads */
31376
31377 static tree
31378 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31379 enum omp_clause_code code,
31380 tree list, location_t location)
31381 {
31382 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31383 tree c = build_omp_clause (location, code);
31384 OMP_CLAUSE_CHAIN (c) = list;
31385 return c;
31386 }
31387
31388 /* OpenMP 4.0:
31389 num_teams ( expression ) */
31390
31391 static tree
31392 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31393 location_t location)
31394 {
31395 tree t, c;
31396
31397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31398 return list;
31399
31400 t = cp_parser_expression (parser);
31401
31402 if (t == error_mark_node
31403 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31404 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31405 /*or_comma=*/false,
31406 /*consume_paren=*/true);
31407
31408 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31409 "num_teams", location);
31410
31411 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31412 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31413 OMP_CLAUSE_CHAIN (c) = list;
31414
31415 return c;
31416 }
31417
31418 /* OpenMP 4.0:
31419 thread_limit ( expression ) */
31420
31421 static tree
31422 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31423 location_t location)
31424 {
31425 tree t, c;
31426
31427 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31428 return list;
31429
31430 t = cp_parser_expression (parser);
31431
31432 if (t == error_mark_node
31433 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31434 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31435 /*or_comma=*/false,
31436 /*consume_paren=*/true);
31437
31438 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31439 "thread_limit", location);
31440
31441 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31442 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31443 OMP_CLAUSE_CHAIN (c) = list;
31444
31445 return c;
31446 }
31447
31448 /* OpenMP 4.0:
31449 aligned ( variable-list )
31450 aligned ( variable-list : constant-expression ) */
31451
31452 static tree
31453 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31454 {
31455 tree nlist, c, alignment = NULL_TREE;
31456 bool colon;
31457
31458 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31459 return list;
31460
31461 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31462 &colon);
31463
31464 if (colon)
31465 {
31466 alignment = cp_parser_constant_expression (parser);
31467
31468 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31469 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31470 /*or_comma=*/false,
31471 /*consume_paren=*/true);
31472
31473 if (alignment == error_mark_node)
31474 alignment = NULL_TREE;
31475 }
31476
31477 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31478 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31479
31480 return nlist;
31481 }
31482
31483 /* OpenMP 4.0:
31484 linear ( variable-list )
31485 linear ( variable-list : expression )
31486
31487 OpenMP 4.5:
31488 linear ( modifier ( variable-list ) )
31489 linear ( modifier ( variable-list ) : expression ) */
31490
31491 static tree
31492 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31493 bool is_cilk_simd_fn, bool declare_simd)
31494 {
31495 tree nlist, c, step = integer_one_node;
31496 bool colon;
31497 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31498
31499 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31500 return list;
31501
31502 if (!is_cilk_simd_fn
31503 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31504 {
31505 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31506 const char *p = IDENTIFIER_POINTER (id);
31507
31508 if (strcmp ("ref", p) == 0)
31509 kind = OMP_CLAUSE_LINEAR_REF;
31510 else if (strcmp ("val", p) == 0)
31511 kind = OMP_CLAUSE_LINEAR_VAL;
31512 else if (strcmp ("uval", p) == 0)
31513 kind = OMP_CLAUSE_LINEAR_UVAL;
31514 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31515 cp_lexer_consume_token (parser->lexer);
31516 else
31517 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31518 }
31519
31520 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31521 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31522 &colon);
31523 else
31524 {
31525 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31526 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31527 if (colon)
31528 cp_parser_require (parser, CPP_COLON, RT_COLON);
31529 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31530 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31531 /*or_comma=*/false,
31532 /*consume_paren=*/true);
31533 }
31534
31535 if (colon)
31536 {
31537 step = NULL_TREE;
31538 if (declare_simd
31539 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31540 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31541 {
31542 cp_token *token = cp_lexer_peek_token (parser->lexer);
31543 cp_parser_parse_tentatively (parser);
31544 step = cp_parser_id_expression (parser, /*template_p=*/false,
31545 /*check_dependency_p=*/true,
31546 /*template_p=*/NULL,
31547 /*declarator_p=*/false,
31548 /*optional_p=*/false);
31549 if (step != error_mark_node)
31550 step = cp_parser_lookup_name_simple (parser, step, token->location);
31551 if (step == error_mark_node)
31552 {
31553 step = NULL_TREE;
31554 cp_parser_abort_tentative_parse (parser);
31555 }
31556 else if (!cp_parser_parse_definitely (parser))
31557 step = NULL_TREE;
31558 }
31559 if (!step)
31560 step = cp_parser_expression (parser);
31561
31562 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31563 {
31564 sorry ("using parameters for %<linear%> step is not supported yet");
31565 step = integer_one_node;
31566 }
31567 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31568 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31569 /*or_comma=*/false,
31570 /*consume_paren=*/true);
31571
31572 if (step == error_mark_node)
31573 return list;
31574 }
31575
31576 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31577 {
31578 OMP_CLAUSE_LINEAR_STEP (c) = step;
31579 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31580 }
31581
31582 return nlist;
31583 }
31584
31585 /* OpenMP 4.0:
31586 safelen ( constant-expression ) */
31587
31588 static tree
31589 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31590 location_t location)
31591 {
31592 tree t, c;
31593
31594 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31595 return list;
31596
31597 t = cp_parser_constant_expression (parser);
31598
31599 if (t == error_mark_node
31600 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31601 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31602 /*or_comma=*/false,
31603 /*consume_paren=*/true);
31604
31605 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31606
31607 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31608 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31609 OMP_CLAUSE_CHAIN (c) = list;
31610
31611 return c;
31612 }
31613
31614 /* OpenMP 4.0:
31615 simdlen ( constant-expression ) */
31616
31617 static tree
31618 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31619 location_t location)
31620 {
31621 tree t, c;
31622
31623 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31624 return list;
31625
31626 t = cp_parser_constant_expression (parser);
31627
31628 if (t == error_mark_node
31629 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31630 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31631 /*or_comma=*/false,
31632 /*consume_paren=*/true);
31633
31634 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31635
31636 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31637 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31638 OMP_CLAUSE_CHAIN (c) = list;
31639
31640 return c;
31641 }
31642
31643 /* OpenMP 4.5:
31644 vec:
31645 identifier [+/- integer]
31646 vec , identifier [+/- integer]
31647 */
31648
31649 static tree
31650 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31651 tree list)
31652 {
31653 tree vec = NULL;
31654
31655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31656 {
31657 cp_parser_error (parser, "expected identifier");
31658 return list;
31659 }
31660
31661 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31662 {
31663 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31664 tree t, identifier = cp_parser_identifier (parser);
31665 tree addend = NULL;
31666
31667 if (identifier == error_mark_node)
31668 t = error_mark_node;
31669 else
31670 {
31671 t = cp_parser_lookup_name_simple
31672 (parser, identifier,
31673 cp_lexer_peek_token (parser->lexer)->location);
31674 if (t == error_mark_node)
31675 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31676 id_loc);
31677 }
31678
31679 bool neg = false;
31680 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31681 neg = true;
31682 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31683 {
31684 addend = integer_zero_node;
31685 goto add_to_vector;
31686 }
31687 cp_lexer_consume_token (parser->lexer);
31688
31689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31690 {
31691 cp_parser_error (parser, "expected integer");
31692 return list;
31693 }
31694
31695 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31696 if (TREE_CODE (addend) != INTEGER_CST)
31697 {
31698 cp_parser_error (parser, "expected integer");
31699 return list;
31700 }
31701 cp_lexer_consume_token (parser->lexer);
31702
31703 add_to_vector:
31704 if (t != error_mark_node)
31705 {
31706 vec = tree_cons (addend, t, vec);
31707 if (neg)
31708 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31709 }
31710
31711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31712 break;
31713
31714 cp_lexer_consume_token (parser->lexer);
31715 }
31716
31717 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31718 {
31719 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31720 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31721 OMP_CLAUSE_DECL (u) = nreverse (vec);
31722 OMP_CLAUSE_CHAIN (u) = list;
31723 return u;
31724 }
31725 return list;
31726 }
31727
31728 /* OpenMP 4.0:
31729 depend ( depend-kind : variable-list )
31730
31731 depend-kind:
31732 in | out | inout
31733
31734 OpenMP 4.5:
31735 depend ( source )
31736
31737 depend ( sink : vec ) */
31738
31739 static tree
31740 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31741 {
31742 tree nlist, c;
31743 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31744
31745 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31746 return list;
31747
31748 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31749 {
31750 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31751 const char *p = IDENTIFIER_POINTER (id);
31752
31753 if (strcmp ("in", p) == 0)
31754 kind = OMP_CLAUSE_DEPEND_IN;
31755 else if (strcmp ("inout", p) == 0)
31756 kind = OMP_CLAUSE_DEPEND_INOUT;
31757 else if (strcmp ("out", p) == 0)
31758 kind = OMP_CLAUSE_DEPEND_OUT;
31759 else if (strcmp ("source", p) == 0)
31760 kind = OMP_CLAUSE_DEPEND_SOURCE;
31761 else if (strcmp ("sink", p) == 0)
31762 kind = OMP_CLAUSE_DEPEND_SINK;
31763 else
31764 goto invalid_kind;
31765 }
31766 else
31767 goto invalid_kind;
31768
31769 cp_lexer_consume_token (parser->lexer);
31770
31771 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31772 {
31773 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31774 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31775 OMP_CLAUSE_DECL (c) = NULL_TREE;
31776 OMP_CLAUSE_CHAIN (c) = list;
31777 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31778 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31779 /*or_comma=*/false,
31780 /*consume_paren=*/true);
31781 return c;
31782 }
31783
31784 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31785 goto resync_fail;
31786
31787 if (kind == OMP_CLAUSE_DEPEND_SINK)
31788 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31789 else
31790 {
31791 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31792 list, NULL);
31793
31794 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31795 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31796 }
31797 return nlist;
31798
31799 invalid_kind:
31800 cp_parser_error (parser, "invalid depend kind");
31801 resync_fail:
31802 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31803 /*or_comma=*/false,
31804 /*consume_paren=*/true);
31805 return list;
31806 }
31807
31808 /* OpenMP 4.0:
31809 map ( map-kind : variable-list )
31810 map ( variable-list )
31811
31812 map-kind:
31813 alloc | to | from | tofrom
31814
31815 OpenMP 4.5:
31816 map-kind:
31817 alloc | to | from | tofrom | release | delete
31818
31819 map ( always [,] map-kind: variable-list ) */
31820
31821 static tree
31822 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31823 {
31824 tree nlist, c;
31825 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31826 bool always = false;
31827
31828 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31829 return list;
31830
31831 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31832 {
31833 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31834 const char *p = IDENTIFIER_POINTER (id);
31835
31836 if (strcmp ("always", p) == 0)
31837 {
31838 int nth = 2;
31839 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31840 nth++;
31841 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31842 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31843 == RID_DELETE))
31844 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31845 == CPP_COLON))
31846 {
31847 always = true;
31848 cp_lexer_consume_token (parser->lexer);
31849 if (nth == 3)
31850 cp_lexer_consume_token (parser->lexer);
31851 }
31852 }
31853 }
31854
31855 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31856 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31857 {
31858 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31859 const char *p = IDENTIFIER_POINTER (id);
31860
31861 if (strcmp ("alloc", p) == 0)
31862 kind = GOMP_MAP_ALLOC;
31863 else if (strcmp ("to", p) == 0)
31864 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31865 else if (strcmp ("from", p) == 0)
31866 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31867 else if (strcmp ("tofrom", p) == 0)
31868 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31869 else if (strcmp ("release", p) == 0)
31870 kind = GOMP_MAP_RELEASE;
31871 else
31872 {
31873 cp_parser_error (parser, "invalid map kind");
31874 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31875 /*or_comma=*/false,
31876 /*consume_paren=*/true);
31877 return list;
31878 }
31879 cp_lexer_consume_token (parser->lexer);
31880 cp_lexer_consume_token (parser->lexer);
31881 }
31882 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31883 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31884 {
31885 kind = GOMP_MAP_DELETE;
31886 cp_lexer_consume_token (parser->lexer);
31887 cp_lexer_consume_token (parser->lexer);
31888 }
31889
31890 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31891 NULL);
31892
31893 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31894 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31895
31896 return nlist;
31897 }
31898
31899 /* OpenMP 4.0:
31900 device ( expression ) */
31901
31902 static tree
31903 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31904 location_t location)
31905 {
31906 tree t, c;
31907
31908 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31909 return list;
31910
31911 t = cp_parser_expression (parser);
31912
31913 if (t == error_mark_node
31914 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31915 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31916 /*or_comma=*/false,
31917 /*consume_paren=*/true);
31918
31919 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31920 "device", location);
31921
31922 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31923 OMP_CLAUSE_DEVICE_ID (c) = t;
31924 OMP_CLAUSE_CHAIN (c) = list;
31925
31926 return c;
31927 }
31928
31929 /* OpenMP 4.0:
31930 dist_schedule ( static )
31931 dist_schedule ( static , expression ) */
31932
31933 static tree
31934 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31935 location_t location)
31936 {
31937 tree c, t;
31938
31939 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31940 return list;
31941
31942 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31943
31944 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31945 goto invalid_kind;
31946 cp_lexer_consume_token (parser->lexer);
31947
31948 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31949 {
31950 cp_lexer_consume_token (parser->lexer);
31951
31952 t = cp_parser_assignment_expression (parser);
31953
31954 if (t == error_mark_node)
31955 goto resync_fail;
31956 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31957
31958 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31959 goto resync_fail;
31960 }
31961 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31962 goto resync_fail;
31963
31964 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31965 location);
31966 OMP_CLAUSE_CHAIN (c) = list;
31967 return c;
31968
31969 invalid_kind:
31970 cp_parser_error (parser, "invalid dist_schedule kind");
31971 resync_fail:
31972 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31973 /*or_comma=*/false,
31974 /*consume_paren=*/true);
31975 return list;
31976 }
31977
31978 /* OpenMP 4.0:
31979 proc_bind ( proc-bind-kind )
31980
31981 proc-bind-kind:
31982 master | close | spread */
31983
31984 static tree
31985 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
31986 location_t location)
31987 {
31988 tree c;
31989 enum omp_clause_proc_bind_kind kind;
31990
31991 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31992 return list;
31993
31994 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31995 {
31996 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31997 const char *p = IDENTIFIER_POINTER (id);
31998
31999 if (strcmp ("master", p) == 0)
32000 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32001 else if (strcmp ("close", p) == 0)
32002 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32003 else if (strcmp ("spread", p) == 0)
32004 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32005 else
32006 goto invalid_kind;
32007 }
32008 else
32009 goto invalid_kind;
32010
32011 cp_lexer_consume_token (parser->lexer);
32012 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32013 goto resync_fail;
32014
32015 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32016 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32017 location);
32018 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32019 OMP_CLAUSE_CHAIN (c) = list;
32020 return c;
32021
32022 invalid_kind:
32023 cp_parser_error (parser, "invalid depend kind");
32024 resync_fail:
32025 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32026 /*or_comma=*/false,
32027 /*consume_paren=*/true);
32028 return list;
32029 }
32030
32031 /* OpenACC:
32032 async [( int-expr )] */
32033
32034 static tree
32035 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32036 {
32037 tree c, t;
32038 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32039
32040 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32041
32042 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32043 {
32044 cp_lexer_consume_token (parser->lexer);
32045
32046 t = cp_parser_expression (parser);
32047 if (t == error_mark_node
32048 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32049 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32050 /*or_comma=*/false,
32051 /*consume_paren=*/true);
32052 }
32053
32054 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32055
32056 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32057 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32058 OMP_CLAUSE_CHAIN (c) = list;
32059 list = c;
32060
32061 return list;
32062 }
32063
32064 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32065 is a bitmask in MASK. Return the list of clauses found. */
32066
32067 static tree
32068 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32069 const char *where, cp_token *pragma_tok,
32070 bool finish_p = true)
32071 {
32072 tree clauses = NULL;
32073 bool first = true;
32074
32075 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32076 {
32077 location_t here;
32078 pragma_omp_clause c_kind;
32079 omp_clause_code code;
32080 const char *c_name;
32081 tree prev = clauses;
32082
32083 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32084 cp_lexer_consume_token (parser->lexer);
32085
32086 here = cp_lexer_peek_token (parser->lexer)->location;
32087 c_kind = cp_parser_omp_clause_name (parser);
32088
32089 switch (c_kind)
32090 {
32091 case PRAGMA_OACC_CLAUSE_ASYNC:
32092 clauses = cp_parser_oacc_clause_async (parser, clauses);
32093 c_name = "async";
32094 break;
32095 case PRAGMA_OACC_CLAUSE_AUTO:
32096 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32097 clauses, here);
32098 c_name = "auto";
32099 break;
32100 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32101 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32102 c_name = "collapse";
32103 break;
32104 case PRAGMA_OACC_CLAUSE_COPY:
32105 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32106 c_name = "copy";
32107 break;
32108 case PRAGMA_OACC_CLAUSE_COPYIN:
32109 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32110 c_name = "copyin";
32111 break;
32112 case PRAGMA_OACC_CLAUSE_COPYOUT:
32113 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32114 c_name = "copyout";
32115 break;
32116 case PRAGMA_OACC_CLAUSE_CREATE:
32117 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32118 c_name = "create";
32119 break;
32120 case PRAGMA_OACC_CLAUSE_DELETE:
32121 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32122 c_name = "delete";
32123 break;
32124 case PRAGMA_OMP_CLAUSE_DEFAULT:
32125 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32126 c_name = "default";
32127 break;
32128 case PRAGMA_OACC_CLAUSE_DEVICE:
32129 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32130 c_name = "device";
32131 break;
32132 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32133 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32134 c_name = "deviceptr";
32135 break;
32136 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32137 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32138 c_name = "device_resident";
32139 break;
32140 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32141 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32142 clauses);
32143 c_name = "firstprivate";
32144 break;
32145 case PRAGMA_OACC_CLAUSE_GANG:
32146 c_name = "gang";
32147 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32148 c_name, clauses);
32149 break;
32150 case PRAGMA_OACC_CLAUSE_HOST:
32151 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32152 c_name = "host";
32153 break;
32154 case PRAGMA_OACC_CLAUSE_IF:
32155 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32156 c_name = "if";
32157 break;
32158 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32159 clauses = cp_parser_oacc_simple_clause (parser,
32160 OMP_CLAUSE_INDEPENDENT,
32161 clauses, here);
32162 c_name = "independent";
32163 break;
32164 case PRAGMA_OACC_CLAUSE_LINK:
32165 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32166 c_name = "link";
32167 break;
32168 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32169 code = OMP_CLAUSE_NUM_GANGS;
32170 c_name = "num_gangs";
32171 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32172 clauses);
32173 break;
32174 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32175 c_name = "num_workers";
32176 code = OMP_CLAUSE_NUM_WORKERS;
32177 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32178 clauses);
32179 break;
32180 case PRAGMA_OACC_CLAUSE_PRESENT:
32181 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32182 c_name = "present";
32183 break;
32184 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32185 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32186 c_name = "present_or_copy";
32187 break;
32188 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32189 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32190 c_name = "present_or_copyin";
32191 break;
32192 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32193 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32194 c_name = "present_or_copyout";
32195 break;
32196 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32197 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32198 c_name = "present_or_create";
32199 break;
32200 case PRAGMA_OACC_CLAUSE_PRIVATE:
32201 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32202 clauses);
32203 c_name = "private";
32204 break;
32205 case PRAGMA_OACC_CLAUSE_REDUCTION:
32206 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32207 c_name = "reduction";
32208 break;
32209 case PRAGMA_OACC_CLAUSE_SELF:
32210 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32211 c_name = "self";
32212 break;
32213 case PRAGMA_OACC_CLAUSE_SEQ:
32214 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32215 clauses, here);
32216 c_name = "seq";
32217 break;
32218 case PRAGMA_OACC_CLAUSE_TILE:
32219 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32220 c_name = "tile";
32221 break;
32222 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32223 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32224 clauses);
32225 c_name = "use_device";
32226 break;
32227 case PRAGMA_OACC_CLAUSE_VECTOR:
32228 c_name = "vector";
32229 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32230 c_name, clauses);
32231 break;
32232 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32233 c_name = "vector_length";
32234 code = OMP_CLAUSE_VECTOR_LENGTH;
32235 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32236 clauses);
32237 break;
32238 case PRAGMA_OACC_CLAUSE_WAIT:
32239 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32240 c_name = "wait";
32241 break;
32242 case PRAGMA_OACC_CLAUSE_WORKER:
32243 c_name = "worker";
32244 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32245 c_name, clauses);
32246 break;
32247 default:
32248 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32249 goto saw_error;
32250 }
32251
32252 first = false;
32253
32254 if (((mask >> c_kind) & 1) == 0)
32255 {
32256 /* Remove the invalid clause(s) from the list to avoid
32257 confusing the rest of the compiler. */
32258 clauses = prev;
32259 error_at (here, "%qs is not valid for %qs", c_name, where);
32260 }
32261 }
32262
32263 saw_error:
32264 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32265
32266 if (finish_p)
32267 return finish_omp_clauses (clauses, C_ORT_ACC);
32268
32269 return clauses;
32270 }
32271
32272 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32273 is a bitmask in MASK. Return the list of clauses found; the result
32274 of clause default goes in *pdefault. */
32275
32276 static tree
32277 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32278 const char *where, cp_token *pragma_tok,
32279 bool finish_p = true)
32280 {
32281 tree clauses = NULL;
32282 bool first = true;
32283 cp_token *token = NULL;
32284
32285 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32286 {
32287 pragma_omp_clause c_kind;
32288 const char *c_name;
32289 tree prev = clauses;
32290
32291 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32292 cp_lexer_consume_token (parser->lexer);
32293
32294 token = cp_lexer_peek_token (parser->lexer);
32295 c_kind = cp_parser_omp_clause_name (parser);
32296
32297 switch (c_kind)
32298 {
32299 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32300 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32301 token->location);
32302 c_name = "collapse";
32303 break;
32304 case PRAGMA_OMP_CLAUSE_COPYIN:
32305 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32306 c_name = "copyin";
32307 break;
32308 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32309 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32310 clauses);
32311 c_name = "copyprivate";
32312 break;
32313 case PRAGMA_OMP_CLAUSE_DEFAULT:
32314 clauses = cp_parser_omp_clause_default (parser, clauses,
32315 token->location, false);
32316 c_name = "default";
32317 break;
32318 case PRAGMA_OMP_CLAUSE_FINAL:
32319 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32320 c_name = "final";
32321 break;
32322 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32323 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32324 clauses);
32325 c_name = "firstprivate";
32326 break;
32327 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32328 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32329 token->location);
32330 c_name = "grainsize";
32331 break;
32332 case PRAGMA_OMP_CLAUSE_HINT:
32333 clauses = cp_parser_omp_clause_hint (parser, clauses,
32334 token->location);
32335 c_name = "hint";
32336 break;
32337 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32338 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32339 token->location);
32340 c_name = "defaultmap";
32341 break;
32342 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32343 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32344 clauses);
32345 c_name = "use_device_ptr";
32346 break;
32347 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32348 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32349 clauses);
32350 c_name = "is_device_ptr";
32351 break;
32352 case PRAGMA_OMP_CLAUSE_IF:
32353 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32354 true);
32355 c_name = "if";
32356 break;
32357 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32358 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32359 clauses);
32360 c_name = "lastprivate";
32361 break;
32362 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32363 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32364 token->location);
32365 c_name = "mergeable";
32366 break;
32367 case PRAGMA_OMP_CLAUSE_NOWAIT:
32368 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32369 c_name = "nowait";
32370 break;
32371 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32372 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32373 token->location);
32374 c_name = "num_tasks";
32375 break;
32376 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32377 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32378 token->location);
32379 c_name = "num_threads";
32380 break;
32381 case PRAGMA_OMP_CLAUSE_ORDERED:
32382 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32383 token->location);
32384 c_name = "ordered";
32385 break;
32386 case PRAGMA_OMP_CLAUSE_PRIORITY:
32387 clauses = cp_parser_omp_clause_priority (parser, clauses,
32388 token->location);
32389 c_name = "priority";
32390 break;
32391 case PRAGMA_OMP_CLAUSE_PRIVATE:
32392 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32393 clauses);
32394 c_name = "private";
32395 break;
32396 case PRAGMA_OMP_CLAUSE_REDUCTION:
32397 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32398 c_name = "reduction";
32399 break;
32400 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32401 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32402 token->location);
32403 c_name = "schedule";
32404 break;
32405 case PRAGMA_OMP_CLAUSE_SHARED:
32406 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32407 clauses);
32408 c_name = "shared";
32409 break;
32410 case PRAGMA_OMP_CLAUSE_UNTIED:
32411 clauses = cp_parser_omp_clause_untied (parser, clauses,
32412 token->location);
32413 c_name = "untied";
32414 break;
32415 case PRAGMA_OMP_CLAUSE_INBRANCH:
32416 case PRAGMA_CILK_CLAUSE_MASK:
32417 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32418 clauses, token->location);
32419 c_name = "inbranch";
32420 break;
32421 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32422 case PRAGMA_CILK_CLAUSE_NOMASK:
32423 clauses = cp_parser_omp_clause_branch (parser,
32424 OMP_CLAUSE_NOTINBRANCH,
32425 clauses, token->location);
32426 c_name = "notinbranch";
32427 break;
32428 case PRAGMA_OMP_CLAUSE_PARALLEL:
32429 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32430 clauses, token->location);
32431 c_name = "parallel";
32432 if (!first)
32433 {
32434 clause_not_first:
32435 error_at (token->location, "%qs must be the first clause of %qs",
32436 c_name, where);
32437 clauses = prev;
32438 }
32439 break;
32440 case PRAGMA_OMP_CLAUSE_FOR:
32441 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32442 clauses, token->location);
32443 c_name = "for";
32444 if (!first)
32445 goto clause_not_first;
32446 break;
32447 case PRAGMA_OMP_CLAUSE_SECTIONS:
32448 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32449 clauses, token->location);
32450 c_name = "sections";
32451 if (!first)
32452 goto clause_not_first;
32453 break;
32454 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32455 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32456 clauses, token->location);
32457 c_name = "taskgroup";
32458 if (!first)
32459 goto clause_not_first;
32460 break;
32461 case PRAGMA_OMP_CLAUSE_LINK:
32462 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32463 c_name = "to";
32464 break;
32465 case PRAGMA_OMP_CLAUSE_TO:
32466 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32467 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32468 clauses);
32469 else
32470 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32471 c_name = "to";
32472 break;
32473 case PRAGMA_OMP_CLAUSE_FROM:
32474 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32475 c_name = "from";
32476 break;
32477 case PRAGMA_OMP_CLAUSE_UNIFORM:
32478 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32479 clauses);
32480 c_name = "uniform";
32481 break;
32482 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32483 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32484 token->location);
32485 c_name = "num_teams";
32486 break;
32487 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32488 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32489 token->location);
32490 c_name = "thread_limit";
32491 break;
32492 case PRAGMA_OMP_CLAUSE_ALIGNED:
32493 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32494 c_name = "aligned";
32495 break;
32496 case PRAGMA_OMP_CLAUSE_LINEAR:
32497 {
32498 bool cilk_simd_fn = false, declare_simd = false;
32499 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32500 cilk_simd_fn = true;
32501 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32502 declare_simd = true;
32503 clauses = cp_parser_omp_clause_linear (parser, clauses,
32504 cilk_simd_fn, declare_simd);
32505 }
32506 c_name = "linear";
32507 break;
32508 case PRAGMA_OMP_CLAUSE_DEPEND:
32509 clauses = cp_parser_omp_clause_depend (parser, clauses,
32510 token->location);
32511 c_name = "depend";
32512 break;
32513 case PRAGMA_OMP_CLAUSE_MAP:
32514 clauses = cp_parser_omp_clause_map (parser, clauses);
32515 c_name = "map";
32516 break;
32517 case PRAGMA_OMP_CLAUSE_DEVICE:
32518 clauses = cp_parser_omp_clause_device (parser, clauses,
32519 token->location);
32520 c_name = "device";
32521 break;
32522 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32523 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32524 token->location);
32525 c_name = "dist_schedule";
32526 break;
32527 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32528 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32529 token->location);
32530 c_name = "proc_bind";
32531 break;
32532 case PRAGMA_OMP_CLAUSE_SAFELEN:
32533 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32534 token->location);
32535 c_name = "safelen";
32536 break;
32537 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32538 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32539 token->location);
32540 c_name = "simdlen";
32541 break;
32542 case PRAGMA_OMP_CLAUSE_NOGROUP:
32543 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32544 token->location);
32545 c_name = "nogroup";
32546 break;
32547 case PRAGMA_OMP_CLAUSE_THREADS:
32548 clauses
32549 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32550 clauses, token->location);
32551 c_name = "threads";
32552 break;
32553 case PRAGMA_OMP_CLAUSE_SIMD:
32554 clauses
32555 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32556 clauses, token->location);
32557 c_name = "simd";
32558 break;
32559 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32560 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32561 c_name = "simdlen";
32562 break;
32563 default:
32564 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32565 goto saw_error;
32566 }
32567
32568 first = false;
32569
32570 if (((mask >> c_kind) & 1) == 0)
32571 {
32572 /* Remove the invalid clause(s) from the list to avoid
32573 confusing the rest of the compiler. */
32574 clauses = prev;
32575 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32576 }
32577 }
32578 saw_error:
32579 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32580 no reason to skip to the end. */
32581 if (!(flag_cilkplus && pragma_tok == NULL))
32582 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32583 if (finish_p)
32584 {
32585 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32586 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
32587 else
32588 return finish_omp_clauses (clauses, C_ORT_OMP);
32589 }
32590 return clauses;
32591 }
32592
32593 /* OpenMP 2.5:
32594 structured-block:
32595 statement
32596
32597 In practice, we're also interested in adding the statement to an
32598 outer node. So it is convenient if we work around the fact that
32599 cp_parser_statement calls add_stmt. */
32600
32601 static unsigned
32602 cp_parser_begin_omp_structured_block (cp_parser *parser)
32603 {
32604 unsigned save = parser->in_statement;
32605
32606 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32607 This preserves the "not within loop or switch" style error messages
32608 for nonsense cases like
32609 void foo() {
32610 #pragma omp single
32611 break;
32612 }
32613 */
32614 if (parser->in_statement)
32615 parser->in_statement = IN_OMP_BLOCK;
32616
32617 return save;
32618 }
32619
32620 static void
32621 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32622 {
32623 parser->in_statement = save;
32624 }
32625
32626 static tree
32627 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32628 {
32629 tree stmt = begin_omp_structured_block ();
32630 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32631
32632 cp_parser_statement (parser, NULL_TREE, false, if_p);
32633
32634 cp_parser_end_omp_structured_block (parser, save);
32635 return finish_omp_structured_block (stmt);
32636 }
32637
32638 /* OpenMP 2.5:
32639 # pragma omp atomic new-line
32640 expression-stmt
32641
32642 expression-stmt:
32643 x binop= expr | x++ | ++x | x-- | --x
32644 binop:
32645 +, *, -, /, &, ^, |, <<, >>
32646
32647 where x is an lvalue expression with scalar type.
32648
32649 OpenMP 3.1:
32650 # pragma omp atomic new-line
32651 update-stmt
32652
32653 # pragma omp atomic read new-line
32654 read-stmt
32655
32656 # pragma omp atomic write new-line
32657 write-stmt
32658
32659 # pragma omp atomic update new-line
32660 update-stmt
32661
32662 # pragma omp atomic capture new-line
32663 capture-stmt
32664
32665 # pragma omp atomic capture new-line
32666 capture-block
32667
32668 read-stmt:
32669 v = x
32670 write-stmt:
32671 x = expr
32672 update-stmt:
32673 expression-stmt | x = x binop expr
32674 capture-stmt:
32675 v = expression-stmt
32676 capture-block:
32677 { v = x; update-stmt; } | { update-stmt; v = x; }
32678
32679 OpenMP 4.0:
32680 update-stmt:
32681 expression-stmt | x = x binop expr | x = expr binop x
32682 capture-stmt:
32683 v = update-stmt
32684 capture-block:
32685 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32686
32687 where x and v are lvalue expressions with scalar type. */
32688
32689 static void
32690 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32691 {
32692 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32693 tree rhs1 = NULL_TREE, orig_lhs;
32694 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32695 bool structured_block = false;
32696 bool seq_cst = false;
32697
32698 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32699 {
32700 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32701 const char *p = IDENTIFIER_POINTER (id);
32702
32703 if (!strcmp (p, "seq_cst"))
32704 {
32705 seq_cst = true;
32706 cp_lexer_consume_token (parser->lexer);
32707 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32708 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32709 cp_lexer_consume_token (parser->lexer);
32710 }
32711 }
32712 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32713 {
32714 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32715 const char *p = IDENTIFIER_POINTER (id);
32716
32717 if (!strcmp (p, "read"))
32718 code = OMP_ATOMIC_READ;
32719 else if (!strcmp (p, "write"))
32720 code = NOP_EXPR;
32721 else if (!strcmp (p, "update"))
32722 code = OMP_ATOMIC;
32723 else if (!strcmp (p, "capture"))
32724 code = OMP_ATOMIC_CAPTURE_NEW;
32725 else
32726 p = NULL;
32727 if (p)
32728 cp_lexer_consume_token (parser->lexer);
32729 }
32730 if (!seq_cst)
32731 {
32732 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32733 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32734 cp_lexer_consume_token (parser->lexer);
32735
32736 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32737 {
32738 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32739 const char *p = IDENTIFIER_POINTER (id);
32740
32741 if (!strcmp (p, "seq_cst"))
32742 {
32743 seq_cst = true;
32744 cp_lexer_consume_token (parser->lexer);
32745 }
32746 }
32747 }
32748 cp_parser_require_pragma_eol (parser, pragma_tok);
32749
32750 switch (code)
32751 {
32752 case OMP_ATOMIC_READ:
32753 case NOP_EXPR: /* atomic write */
32754 v = cp_parser_unary_expression (parser);
32755 if (v == error_mark_node)
32756 goto saw_error;
32757 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32758 goto saw_error;
32759 if (code == NOP_EXPR)
32760 lhs = cp_parser_expression (parser);
32761 else
32762 lhs = cp_parser_unary_expression (parser);
32763 if (lhs == error_mark_node)
32764 goto saw_error;
32765 if (code == NOP_EXPR)
32766 {
32767 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32768 opcode. */
32769 code = OMP_ATOMIC;
32770 rhs = lhs;
32771 lhs = v;
32772 v = NULL_TREE;
32773 }
32774 goto done;
32775 case OMP_ATOMIC_CAPTURE_NEW:
32776 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32777 {
32778 cp_lexer_consume_token (parser->lexer);
32779 structured_block = true;
32780 }
32781 else
32782 {
32783 v = cp_parser_unary_expression (parser);
32784 if (v == error_mark_node)
32785 goto saw_error;
32786 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32787 goto saw_error;
32788 }
32789 default:
32790 break;
32791 }
32792
32793 restart:
32794 lhs = cp_parser_unary_expression (parser);
32795 orig_lhs = lhs;
32796 switch (TREE_CODE (lhs))
32797 {
32798 case ERROR_MARK:
32799 goto saw_error;
32800
32801 case POSTINCREMENT_EXPR:
32802 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32803 code = OMP_ATOMIC_CAPTURE_OLD;
32804 /* FALLTHROUGH */
32805 case PREINCREMENT_EXPR:
32806 lhs = TREE_OPERAND (lhs, 0);
32807 opcode = PLUS_EXPR;
32808 rhs = integer_one_node;
32809 break;
32810
32811 case POSTDECREMENT_EXPR:
32812 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32813 code = OMP_ATOMIC_CAPTURE_OLD;
32814 /* FALLTHROUGH */
32815 case PREDECREMENT_EXPR:
32816 lhs = TREE_OPERAND (lhs, 0);
32817 opcode = MINUS_EXPR;
32818 rhs = integer_one_node;
32819 break;
32820
32821 case COMPOUND_EXPR:
32822 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32823 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32824 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32825 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32826 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32827 (TREE_OPERAND (lhs, 1), 0), 0)))
32828 == BOOLEAN_TYPE)
32829 /* Undo effects of boolean_increment for post {in,de}crement. */
32830 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32831 /* FALLTHRU */
32832 case MODIFY_EXPR:
32833 if (TREE_CODE (lhs) == MODIFY_EXPR
32834 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32835 {
32836 /* Undo effects of boolean_increment. */
32837 if (integer_onep (TREE_OPERAND (lhs, 1)))
32838 {
32839 /* This is pre or post increment. */
32840 rhs = TREE_OPERAND (lhs, 1);
32841 lhs = TREE_OPERAND (lhs, 0);
32842 opcode = NOP_EXPR;
32843 if (code == OMP_ATOMIC_CAPTURE_NEW
32844 && !structured_block
32845 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32846 code = OMP_ATOMIC_CAPTURE_OLD;
32847 break;
32848 }
32849 }
32850 /* FALLTHRU */
32851 default:
32852 switch (cp_lexer_peek_token (parser->lexer)->type)
32853 {
32854 case CPP_MULT_EQ:
32855 opcode = MULT_EXPR;
32856 break;
32857 case CPP_DIV_EQ:
32858 opcode = TRUNC_DIV_EXPR;
32859 break;
32860 case CPP_PLUS_EQ:
32861 opcode = PLUS_EXPR;
32862 break;
32863 case CPP_MINUS_EQ:
32864 opcode = MINUS_EXPR;
32865 break;
32866 case CPP_LSHIFT_EQ:
32867 opcode = LSHIFT_EXPR;
32868 break;
32869 case CPP_RSHIFT_EQ:
32870 opcode = RSHIFT_EXPR;
32871 break;
32872 case CPP_AND_EQ:
32873 opcode = BIT_AND_EXPR;
32874 break;
32875 case CPP_OR_EQ:
32876 opcode = BIT_IOR_EXPR;
32877 break;
32878 case CPP_XOR_EQ:
32879 opcode = BIT_XOR_EXPR;
32880 break;
32881 case CPP_EQ:
32882 enum cp_parser_prec oprec;
32883 cp_token *token;
32884 cp_lexer_consume_token (parser->lexer);
32885 cp_parser_parse_tentatively (parser);
32886 rhs1 = cp_parser_simple_cast_expression (parser);
32887 if (rhs1 == error_mark_node)
32888 {
32889 cp_parser_abort_tentative_parse (parser);
32890 cp_parser_simple_cast_expression (parser);
32891 goto saw_error;
32892 }
32893 token = cp_lexer_peek_token (parser->lexer);
32894 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32895 {
32896 cp_parser_abort_tentative_parse (parser);
32897 cp_parser_parse_tentatively (parser);
32898 rhs = cp_parser_binary_expression (parser, false, true,
32899 PREC_NOT_OPERATOR, NULL);
32900 if (rhs == error_mark_node)
32901 {
32902 cp_parser_abort_tentative_parse (parser);
32903 cp_parser_binary_expression (parser, false, true,
32904 PREC_NOT_OPERATOR, NULL);
32905 goto saw_error;
32906 }
32907 switch (TREE_CODE (rhs))
32908 {
32909 case MULT_EXPR:
32910 case TRUNC_DIV_EXPR:
32911 case RDIV_EXPR:
32912 case PLUS_EXPR:
32913 case MINUS_EXPR:
32914 case LSHIFT_EXPR:
32915 case RSHIFT_EXPR:
32916 case BIT_AND_EXPR:
32917 case BIT_IOR_EXPR:
32918 case BIT_XOR_EXPR:
32919 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32920 {
32921 if (cp_parser_parse_definitely (parser))
32922 {
32923 opcode = TREE_CODE (rhs);
32924 rhs1 = TREE_OPERAND (rhs, 0);
32925 rhs = TREE_OPERAND (rhs, 1);
32926 goto stmt_done;
32927 }
32928 else
32929 goto saw_error;
32930 }
32931 break;
32932 default:
32933 break;
32934 }
32935 cp_parser_abort_tentative_parse (parser);
32936 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32937 {
32938 rhs = cp_parser_expression (parser);
32939 if (rhs == error_mark_node)
32940 goto saw_error;
32941 opcode = NOP_EXPR;
32942 rhs1 = NULL_TREE;
32943 goto stmt_done;
32944 }
32945 cp_parser_error (parser,
32946 "invalid form of %<#pragma omp atomic%>");
32947 goto saw_error;
32948 }
32949 if (!cp_parser_parse_definitely (parser))
32950 goto saw_error;
32951 switch (token->type)
32952 {
32953 case CPP_SEMICOLON:
32954 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32955 {
32956 code = OMP_ATOMIC_CAPTURE_OLD;
32957 v = lhs;
32958 lhs = NULL_TREE;
32959 lhs1 = rhs1;
32960 rhs1 = NULL_TREE;
32961 cp_lexer_consume_token (parser->lexer);
32962 goto restart;
32963 }
32964 else if (structured_block)
32965 {
32966 opcode = NOP_EXPR;
32967 rhs = rhs1;
32968 rhs1 = NULL_TREE;
32969 goto stmt_done;
32970 }
32971 cp_parser_error (parser,
32972 "invalid form of %<#pragma omp atomic%>");
32973 goto saw_error;
32974 case CPP_MULT:
32975 opcode = MULT_EXPR;
32976 break;
32977 case CPP_DIV:
32978 opcode = TRUNC_DIV_EXPR;
32979 break;
32980 case CPP_PLUS:
32981 opcode = PLUS_EXPR;
32982 break;
32983 case CPP_MINUS:
32984 opcode = MINUS_EXPR;
32985 break;
32986 case CPP_LSHIFT:
32987 opcode = LSHIFT_EXPR;
32988 break;
32989 case CPP_RSHIFT:
32990 opcode = RSHIFT_EXPR;
32991 break;
32992 case CPP_AND:
32993 opcode = BIT_AND_EXPR;
32994 break;
32995 case CPP_OR:
32996 opcode = BIT_IOR_EXPR;
32997 break;
32998 case CPP_XOR:
32999 opcode = BIT_XOR_EXPR;
33000 break;
33001 default:
33002 cp_parser_error (parser,
33003 "invalid operator for %<#pragma omp atomic%>");
33004 goto saw_error;
33005 }
33006 oprec = TOKEN_PRECEDENCE (token);
33007 gcc_assert (oprec != PREC_NOT_OPERATOR);
33008 if (commutative_tree_code (opcode))
33009 oprec = (enum cp_parser_prec) (oprec - 1);
33010 cp_lexer_consume_token (parser->lexer);
33011 rhs = cp_parser_binary_expression (parser, false, false,
33012 oprec, NULL);
33013 if (rhs == error_mark_node)
33014 goto saw_error;
33015 goto stmt_done;
33016 /* FALLTHROUGH */
33017 default:
33018 cp_parser_error (parser,
33019 "invalid operator for %<#pragma omp atomic%>");
33020 goto saw_error;
33021 }
33022 cp_lexer_consume_token (parser->lexer);
33023
33024 rhs = cp_parser_expression (parser);
33025 if (rhs == error_mark_node)
33026 goto saw_error;
33027 break;
33028 }
33029 stmt_done:
33030 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33031 {
33032 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33033 goto saw_error;
33034 v = cp_parser_unary_expression (parser);
33035 if (v == error_mark_node)
33036 goto saw_error;
33037 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33038 goto saw_error;
33039 lhs1 = cp_parser_unary_expression (parser);
33040 if (lhs1 == error_mark_node)
33041 goto saw_error;
33042 }
33043 if (structured_block)
33044 {
33045 cp_parser_consume_semicolon_at_end_of_statement (parser);
33046 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33047 }
33048 done:
33049 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33050 if (!structured_block)
33051 cp_parser_consume_semicolon_at_end_of_statement (parser);
33052 return;
33053
33054 saw_error:
33055 cp_parser_skip_to_end_of_block_or_statement (parser);
33056 if (structured_block)
33057 {
33058 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33059 cp_lexer_consume_token (parser->lexer);
33060 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33061 {
33062 cp_parser_skip_to_end_of_block_or_statement (parser);
33063 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33064 cp_lexer_consume_token (parser->lexer);
33065 }
33066 }
33067 }
33068
33069
33070 /* OpenMP 2.5:
33071 # pragma omp barrier new-line */
33072
33073 static void
33074 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33075 {
33076 cp_parser_require_pragma_eol (parser, pragma_tok);
33077 finish_omp_barrier ();
33078 }
33079
33080 /* OpenMP 2.5:
33081 # pragma omp critical [(name)] new-line
33082 structured-block
33083
33084 OpenMP 4.5:
33085 # pragma omp critical [(name) [hint(expression)]] new-line
33086 structured-block */
33087
33088 #define OMP_CRITICAL_CLAUSE_MASK \
33089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33090
33091 static tree
33092 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33093 {
33094 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33095
33096 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33097 {
33098 cp_lexer_consume_token (parser->lexer);
33099
33100 name = cp_parser_identifier (parser);
33101
33102 if (name == error_mark_node
33103 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33104 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33105 /*or_comma=*/false,
33106 /*consume_paren=*/true);
33107 if (name == error_mark_node)
33108 name = NULL;
33109
33110 clauses = cp_parser_omp_all_clauses (parser,
33111 OMP_CRITICAL_CLAUSE_MASK,
33112 "#pragma omp critical", pragma_tok);
33113 }
33114 else
33115 cp_parser_require_pragma_eol (parser, pragma_tok);
33116
33117 stmt = cp_parser_omp_structured_block (parser, if_p);
33118 return c_finish_omp_critical (input_location, stmt, name, clauses);
33119 }
33120
33121 /* OpenMP 2.5:
33122 # pragma omp flush flush-vars[opt] new-line
33123
33124 flush-vars:
33125 ( variable-list ) */
33126
33127 static void
33128 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33129 {
33130 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33131 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33132 cp_parser_require_pragma_eol (parser, pragma_tok);
33133
33134 finish_omp_flush ();
33135 }
33136
33137 /* Helper function, to parse omp for increment expression. */
33138
33139 static tree
33140 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33141 {
33142 tree cond = cp_parser_binary_expression (parser, false, true,
33143 PREC_NOT_OPERATOR, NULL);
33144 if (cond == error_mark_node
33145 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33146 {
33147 cp_parser_skip_to_end_of_statement (parser);
33148 return error_mark_node;
33149 }
33150
33151 switch (TREE_CODE (cond))
33152 {
33153 case GT_EXPR:
33154 case GE_EXPR:
33155 case LT_EXPR:
33156 case LE_EXPR:
33157 break;
33158 case NE_EXPR:
33159 if (code == CILK_SIMD || code == CILK_FOR)
33160 break;
33161 /* Fall through: OpenMP disallows NE_EXPR. */
33162 default:
33163 return error_mark_node;
33164 }
33165
33166 /* If decl is an iterator, preserve LHS and RHS of the relational
33167 expr until finish_omp_for. */
33168 if (decl
33169 && (type_dependent_expression_p (decl)
33170 || CLASS_TYPE_P (TREE_TYPE (decl))))
33171 return cond;
33172
33173 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33174 TREE_CODE (cond),
33175 TREE_OPERAND (cond, 0), ERROR_MARK,
33176 TREE_OPERAND (cond, 1), ERROR_MARK,
33177 /*overload=*/NULL, tf_warning_or_error);
33178 }
33179
33180 /* Helper function, to parse omp for increment expression. */
33181
33182 static tree
33183 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33184 {
33185 cp_token *token = cp_lexer_peek_token (parser->lexer);
33186 enum tree_code op;
33187 tree lhs, rhs;
33188 cp_id_kind idk;
33189 bool decl_first;
33190
33191 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33192 {
33193 op = (token->type == CPP_PLUS_PLUS
33194 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33195 cp_lexer_consume_token (parser->lexer);
33196 lhs = cp_parser_simple_cast_expression (parser);
33197 if (lhs != decl
33198 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33199 return error_mark_node;
33200 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33201 }
33202
33203 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33204 if (lhs != decl
33205 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33206 return error_mark_node;
33207
33208 token = cp_lexer_peek_token (parser->lexer);
33209 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33210 {
33211 op = (token->type == CPP_PLUS_PLUS
33212 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33213 cp_lexer_consume_token (parser->lexer);
33214 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33215 }
33216
33217 op = cp_parser_assignment_operator_opt (parser);
33218 if (op == ERROR_MARK)
33219 return error_mark_node;
33220
33221 if (op != NOP_EXPR)
33222 {
33223 rhs = cp_parser_assignment_expression (parser);
33224 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33225 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33226 }
33227
33228 lhs = cp_parser_binary_expression (parser, false, false,
33229 PREC_ADDITIVE_EXPRESSION, NULL);
33230 token = cp_lexer_peek_token (parser->lexer);
33231 decl_first = (lhs == decl
33232 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33233 if (decl_first)
33234 lhs = NULL_TREE;
33235 if (token->type != CPP_PLUS
33236 && token->type != CPP_MINUS)
33237 return error_mark_node;
33238
33239 do
33240 {
33241 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33242 cp_lexer_consume_token (parser->lexer);
33243 rhs = cp_parser_binary_expression (parser, false, false,
33244 PREC_ADDITIVE_EXPRESSION, NULL);
33245 token = cp_lexer_peek_token (parser->lexer);
33246 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33247 {
33248 if (lhs == NULL_TREE)
33249 {
33250 if (op == PLUS_EXPR)
33251 lhs = rhs;
33252 else
33253 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33254 tf_warning_or_error);
33255 }
33256 else
33257 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33258 ERROR_MARK, NULL, tf_warning_or_error);
33259 }
33260 }
33261 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33262
33263 if (!decl_first)
33264 {
33265 if ((rhs != decl
33266 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33267 || op == MINUS_EXPR)
33268 return error_mark_node;
33269 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33270 }
33271 else
33272 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33273
33274 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33275 }
33276
33277 /* Parse the initialization statement of either an OpenMP for loop or
33278 a Cilk Plus for loop.
33279
33280 Return true if the resulting construct should have an
33281 OMP_CLAUSE_PRIVATE added to it. */
33282
33283 static tree
33284 cp_parser_omp_for_loop_init (cp_parser *parser,
33285 enum tree_code code,
33286 tree &this_pre_body,
33287 vec<tree, va_gc> *for_block,
33288 tree &init,
33289 tree &orig_init,
33290 tree &decl,
33291 tree &real_decl)
33292 {
33293 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33294 return NULL_TREE;
33295
33296 tree add_private_clause = NULL_TREE;
33297
33298 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33299
33300 init-expr:
33301 var = lb
33302 integer-type var = lb
33303 random-access-iterator-type var = lb
33304 pointer-type var = lb
33305 */
33306 cp_decl_specifier_seq type_specifiers;
33307
33308 /* First, try to parse as an initialized declaration. See
33309 cp_parser_condition, from whence the bulk of this is copied. */
33310
33311 cp_parser_parse_tentatively (parser);
33312 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33313 /*is_trailing_return=*/false,
33314 &type_specifiers);
33315 if (cp_parser_parse_definitely (parser))
33316 {
33317 /* If parsing a type specifier seq succeeded, then this
33318 MUST be a initialized declaration. */
33319 tree asm_specification, attributes;
33320 cp_declarator *declarator;
33321
33322 declarator = cp_parser_declarator (parser,
33323 CP_PARSER_DECLARATOR_NAMED,
33324 /*ctor_dtor_or_conv_p=*/NULL,
33325 /*parenthesized_p=*/NULL,
33326 /*member_p=*/false,
33327 /*friend_p=*/false);
33328 attributes = cp_parser_attributes_opt (parser);
33329 asm_specification = cp_parser_asm_specification_opt (parser);
33330
33331 if (declarator == cp_error_declarator)
33332 cp_parser_skip_to_end_of_statement (parser);
33333
33334 else
33335 {
33336 tree pushed_scope, auto_node;
33337
33338 decl = start_decl (declarator, &type_specifiers,
33339 SD_INITIALIZED, attributes,
33340 /*prefix_attributes=*/NULL_TREE,
33341 &pushed_scope);
33342
33343 auto_node = type_uses_auto (TREE_TYPE (decl));
33344 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33345 {
33346 if (cp_lexer_next_token_is (parser->lexer,
33347 CPP_OPEN_PAREN))
33348 {
33349 if (code != CILK_SIMD && code != CILK_FOR)
33350 error ("parenthesized initialization is not allowed in "
33351 "OpenMP %<for%> loop");
33352 else
33353 error ("parenthesized initialization is "
33354 "not allowed in for-loop");
33355 }
33356 else
33357 /* Trigger an error. */
33358 cp_parser_require (parser, CPP_EQ, RT_EQ);
33359
33360 init = error_mark_node;
33361 cp_parser_skip_to_end_of_statement (parser);
33362 }
33363 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33364 || type_dependent_expression_p (decl)
33365 || auto_node)
33366 {
33367 bool is_direct_init, is_non_constant_init;
33368
33369 init = cp_parser_initializer (parser,
33370 &is_direct_init,
33371 &is_non_constant_init);
33372
33373 if (auto_node)
33374 {
33375 TREE_TYPE (decl)
33376 = do_auto_deduction (TREE_TYPE (decl), init,
33377 auto_node);
33378
33379 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33380 && !type_dependent_expression_p (decl))
33381 goto non_class;
33382 }
33383
33384 cp_finish_decl (decl, init, !is_non_constant_init,
33385 asm_specification,
33386 LOOKUP_ONLYCONVERTING);
33387 orig_init = init;
33388 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33389 {
33390 vec_safe_push (for_block, this_pre_body);
33391 init = NULL_TREE;
33392 }
33393 else
33394 init = pop_stmt_list (this_pre_body);
33395 this_pre_body = NULL_TREE;
33396 }
33397 else
33398 {
33399 /* Consume '='. */
33400 cp_lexer_consume_token (parser->lexer);
33401 init = cp_parser_assignment_expression (parser);
33402
33403 non_class:
33404 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33405 init = error_mark_node;
33406 else
33407 cp_finish_decl (decl, NULL_TREE,
33408 /*init_const_expr_p=*/false,
33409 asm_specification,
33410 LOOKUP_ONLYCONVERTING);
33411 }
33412
33413 if (pushed_scope)
33414 pop_scope (pushed_scope);
33415 }
33416 }
33417 else
33418 {
33419 cp_id_kind idk;
33420 /* If parsing a type specifier sequence failed, then
33421 this MUST be a simple expression. */
33422 if (code == CILK_FOR)
33423 error ("%<_Cilk_for%> allows expression instead of declaration only "
33424 "in C, not in C++");
33425 cp_parser_parse_tentatively (parser);
33426 decl = cp_parser_primary_expression (parser, false, false,
33427 false, &idk);
33428 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33429 if (!cp_parser_error_occurred (parser)
33430 && decl
33431 && (TREE_CODE (decl) == COMPONENT_REF
33432 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33433 {
33434 cp_parser_abort_tentative_parse (parser);
33435 cp_parser_parse_tentatively (parser);
33436 cp_token *token = cp_lexer_peek_token (parser->lexer);
33437 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33438 /*check_dependency_p=*/true,
33439 /*template_p=*/NULL,
33440 /*declarator_p=*/false,
33441 /*optional_p=*/false);
33442 if (name != error_mark_node
33443 && last_tok == cp_lexer_peek_token (parser->lexer))
33444 {
33445 decl = cp_parser_lookup_name_simple (parser, name,
33446 token->location);
33447 if (TREE_CODE (decl) == FIELD_DECL)
33448 add_private_clause = omp_privatize_field (decl, false);
33449 }
33450 cp_parser_abort_tentative_parse (parser);
33451 cp_parser_parse_tentatively (parser);
33452 decl = cp_parser_primary_expression (parser, false, false,
33453 false, &idk);
33454 }
33455 if (!cp_parser_error_occurred (parser)
33456 && decl
33457 && DECL_P (decl)
33458 && CLASS_TYPE_P (TREE_TYPE (decl)))
33459 {
33460 tree rhs;
33461
33462 cp_parser_parse_definitely (parser);
33463 cp_parser_require (parser, CPP_EQ, RT_EQ);
33464 rhs = cp_parser_assignment_expression (parser);
33465 orig_init = rhs;
33466 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33467 decl, NOP_EXPR,
33468 rhs,
33469 tf_warning_or_error));
33470 if (!add_private_clause)
33471 add_private_clause = decl;
33472 }
33473 else
33474 {
33475 decl = NULL;
33476 cp_parser_abort_tentative_parse (parser);
33477 init = cp_parser_expression (parser);
33478 if (init)
33479 {
33480 if (TREE_CODE (init) == MODIFY_EXPR
33481 || TREE_CODE (init) == MODOP_EXPR)
33482 real_decl = TREE_OPERAND (init, 0);
33483 }
33484 }
33485 }
33486 return add_private_clause;
33487 }
33488
33489 /* Parse the restricted form of the for statement allowed by OpenMP. */
33490
33491 static tree
33492 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33493 tree *cclauses, bool *if_p)
33494 {
33495 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33496 tree real_decl, initv, condv, incrv, declv;
33497 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33498 location_t loc_first;
33499 bool collapse_err = false;
33500 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33501 vec<tree, va_gc> *for_block = make_tree_vector ();
33502 auto_vec<tree, 4> orig_inits;
33503
33504 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33505 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33506 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33507 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33508 && OMP_CLAUSE_ORDERED_EXPR (cl))
33509 {
33510 ordered_cl = cl;
33511 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33512 }
33513
33514 if (ordered && ordered < collapse)
33515 {
33516 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33517 "%<ordered%> clause parameter is less than %<collapse%>");
33518 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33519 = build_int_cst (NULL_TREE, collapse);
33520 ordered = collapse;
33521 }
33522 if (ordered)
33523 {
33524 for (tree *pc = &clauses; *pc; )
33525 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33526 {
33527 error_at (OMP_CLAUSE_LOCATION (*pc),
33528 "%<linear%> clause may not be specified together "
33529 "with %<ordered%> clause with a parameter");
33530 *pc = OMP_CLAUSE_CHAIN (*pc);
33531 }
33532 else
33533 pc = &OMP_CLAUSE_CHAIN (*pc);
33534 }
33535
33536 gcc_assert (collapse >= 1 && ordered >= 0);
33537 count = ordered ? ordered : collapse;
33538
33539 declv = make_tree_vec (count);
33540 initv = make_tree_vec (count);
33541 condv = make_tree_vec (count);
33542 incrv = make_tree_vec (count);
33543
33544 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33545
33546 for (i = 0; i < count; i++)
33547 {
33548 int bracecount = 0;
33549 tree add_private_clause = NULL_TREE;
33550 location_t loc;
33551
33552 if (code != CILK_FOR
33553 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33554 {
33555 cp_parser_error (parser, "for statement expected");
33556 return NULL;
33557 }
33558 if (code == CILK_FOR
33559 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33560 {
33561 cp_parser_error (parser, "_Cilk_for statement expected");
33562 return NULL;
33563 }
33564 loc = cp_lexer_consume_token (parser->lexer)->location;
33565
33566 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33567 return NULL;
33568
33569 init = orig_init = decl = real_decl = NULL;
33570 this_pre_body = push_stmt_list ();
33571
33572 add_private_clause
33573 = cp_parser_omp_for_loop_init (parser, code,
33574 this_pre_body, for_block,
33575 init, orig_init, decl, real_decl);
33576
33577 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33578 if (this_pre_body)
33579 {
33580 this_pre_body = pop_stmt_list (this_pre_body);
33581 if (pre_body)
33582 {
33583 tree t = pre_body;
33584 pre_body = push_stmt_list ();
33585 add_stmt (t);
33586 add_stmt (this_pre_body);
33587 pre_body = pop_stmt_list (pre_body);
33588 }
33589 else
33590 pre_body = this_pre_body;
33591 }
33592
33593 if (decl)
33594 real_decl = decl;
33595 if (cclauses != NULL
33596 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33597 && real_decl != NULL_TREE)
33598 {
33599 tree *c;
33600 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33601 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33602 && OMP_CLAUSE_DECL (*c) == real_decl)
33603 {
33604 error_at (loc, "iteration variable %qD"
33605 " should not be firstprivate", real_decl);
33606 *c = OMP_CLAUSE_CHAIN (*c);
33607 }
33608 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33609 && OMP_CLAUSE_DECL (*c) == real_decl)
33610 {
33611 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33612 tree l = *c;
33613 *c = OMP_CLAUSE_CHAIN (*c);
33614 if (code == OMP_SIMD)
33615 {
33616 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33617 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33618 }
33619 else
33620 {
33621 OMP_CLAUSE_CHAIN (l) = clauses;
33622 clauses = l;
33623 }
33624 add_private_clause = NULL_TREE;
33625 }
33626 else
33627 {
33628 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33629 && OMP_CLAUSE_DECL (*c) == real_decl)
33630 add_private_clause = NULL_TREE;
33631 c = &OMP_CLAUSE_CHAIN (*c);
33632 }
33633 }
33634
33635 if (add_private_clause)
33636 {
33637 tree c;
33638 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33639 {
33640 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33641 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33642 && OMP_CLAUSE_DECL (c) == decl)
33643 break;
33644 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33645 && OMP_CLAUSE_DECL (c) == decl)
33646 error_at (loc, "iteration variable %qD "
33647 "should not be firstprivate",
33648 decl);
33649 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33650 && OMP_CLAUSE_DECL (c) == decl)
33651 error_at (loc, "iteration variable %qD should not be reduction",
33652 decl);
33653 }
33654 if (c == NULL)
33655 {
33656 if (code != OMP_SIMD)
33657 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33658 else if (collapse == 1)
33659 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33660 else
33661 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33662 OMP_CLAUSE_DECL (c) = add_private_clause;
33663 c = finish_omp_clauses (c, C_ORT_OMP);
33664 if (c)
33665 {
33666 OMP_CLAUSE_CHAIN (c) = clauses;
33667 clauses = c;
33668 /* For linear, signal that we need to fill up
33669 the so far unknown linear step. */
33670 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33671 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33672 }
33673 }
33674 }
33675
33676 cond = NULL;
33677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33678 cond = cp_parser_omp_for_cond (parser, decl, code);
33679 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33680
33681 incr = NULL;
33682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33683 {
33684 /* If decl is an iterator, preserve the operator on decl
33685 until finish_omp_for. */
33686 if (real_decl
33687 && ((processing_template_decl
33688 && (TREE_TYPE (real_decl) == NULL_TREE
33689 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33690 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33691 incr = cp_parser_omp_for_incr (parser, real_decl);
33692 else
33693 incr = cp_parser_expression (parser);
33694 if (!EXPR_HAS_LOCATION (incr))
33695 protected_set_expr_location (incr, input_location);
33696 }
33697
33698 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33699 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33700 /*or_comma=*/false,
33701 /*consume_paren=*/true);
33702
33703 TREE_VEC_ELT (declv, i) = decl;
33704 TREE_VEC_ELT (initv, i) = init;
33705 TREE_VEC_ELT (condv, i) = cond;
33706 TREE_VEC_ELT (incrv, i) = incr;
33707 if (orig_init)
33708 {
33709 orig_inits.safe_grow_cleared (i + 1);
33710 orig_inits[i] = orig_init;
33711 }
33712
33713 if (i == count - 1)
33714 break;
33715
33716 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33717 in between the collapsed for loops to be still considered perfectly
33718 nested. Hopefully the final version clarifies this.
33719 For now handle (multiple) {'s and empty statements. */
33720 cp_parser_parse_tentatively (parser);
33721 do
33722 {
33723 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33724 break;
33725 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33726 {
33727 cp_lexer_consume_token (parser->lexer);
33728 bracecount++;
33729 }
33730 else if (bracecount
33731 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33732 cp_lexer_consume_token (parser->lexer);
33733 else
33734 {
33735 loc = cp_lexer_peek_token (parser->lexer)->location;
33736 error_at (loc, "not enough collapsed for loops");
33737 collapse_err = true;
33738 cp_parser_abort_tentative_parse (parser);
33739 declv = NULL_TREE;
33740 break;
33741 }
33742 }
33743 while (1);
33744
33745 if (declv)
33746 {
33747 cp_parser_parse_definitely (parser);
33748 nbraces += bracecount;
33749 }
33750 }
33751
33752 if (nbraces)
33753 if_p = NULL;
33754
33755 /* Note that we saved the original contents of this flag when we entered
33756 the structured block, and so we don't need to re-save it here. */
33757 if (code == CILK_SIMD || code == CILK_FOR)
33758 parser->in_statement = IN_CILK_SIMD_FOR;
33759 else
33760 parser->in_statement = IN_OMP_FOR;
33761
33762 /* Note that the grammar doesn't call for a structured block here,
33763 though the loop as a whole is a structured block. */
33764 body = push_stmt_list ();
33765 cp_parser_statement (parser, NULL_TREE, false, if_p);
33766 body = pop_stmt_list (body);
33767
33768 if (declv == NULL_TREE)
33769 ret = NULL_TREE;
33770 else
33771 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33772 body, pre_body, &orig_inits, clauses);
33773
33774 while (nbraces)
33775 {
33776 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33777 {
33778 cp_lexer_consume_token (parser->lexer);
33779 nbraces--;
33780 }
33781 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33782 cp_lexer_consume_token (parser->lexer);
33783 else
33784 {
33785 if (!collapse_err)
33786 {
33787 error_at (cp_lexer_peek_token (parser->lexer)->location,
33788 "collapsed loops not perfectly nested");
33789 }
33790 collapse_err = true;
33791 cp_parser_statement_seq_opt (parser, NULL);
33792 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33793 break;
33794 }
33795 }
33796
33797 while (!for_block->is_empty ())
33798 add_stmt (pop_stmt_list (for_block->pop ()));
33799 release_tree_vector (for_block);
33800
33801 return ret;
33802 }
33803
33804 /* Helper function for OpenMP parsing, split clauses and call
33805 finish_omp_clauses on each of the set of clauses afterwards. */
33806
33807 static void
33808 cp_omp_split_clauses (location_t loc, enum tree_code code,
33809 omp_clause_mask mask, tree clauses, tree *cclauses)
33810 {
33811 int i;
33812 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33813 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33814 if (cclauses[i])
33815 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
33816 }
33817
33818 /* OpenMP 4.0:
33819 #pragma omp simd simd-clause[optseq] new-line
33820 for-loop */
33821
33822 #define OMP_SIMD_CLAUSE_MASK \
33823 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33831
33832 static tree
33833 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33834 char *p_name, omp_clause_mask mask, tree *cclauses,
33835 bool *if_p)
33836 {
33837 tree clauses, sb, ret;
33838 unsigned int save;
33839 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33840
33841 strcat (p_name, " simd");
33842 mask |= OMP_SIMD_CLAUSE_MASK;
33843
33844 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33845 cclauses == NULL);
33846 if (cclauses)
33847 {
33848 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33849 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33850 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33851 OMP_CLAUSE_ORDERED);
33852 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33853 {
33854 error_at (OMP_CLAUSE_LOCATION (c),
33855 "%<ordered%> clause with parameter may not be specified "
33856 "on %qs construct", p_name);
33857 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33858 }
33859 }
33860
33861 sb = begin_omp_structured_block ();
33862 save = cp_parser_begin_omp_structured_block (parser);
33863
33864 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
33865
33866 cp_parser_end_omp_structured_block (parser, save);
33867 add_stmt (finish_omp_structured_block (sb));
33868
33869 return ret;
33870 }
33871
33872 /* OpenMP 2.5:
33873 #pragma omp for for-clause[optseq] new-line
33874 for-loop
33875
33876 OpenMP 4.0:
33877 #pragma omp for simd for-simd-clause[optseq] new-line
33878 for-loop */
33879
33880 #define OMP_FOR_CLAUSE_MASK \
33881 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33890
33891 static tree
33892 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33893 char *p_name, omp_clause_mask mask, tree *cclauses,
33894 bool *if_p)
33895 {
33896 tree clauses, sb, ret;
33897 unsigned int save;
33898 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33899
33900 strcat (p_name, " for");
33901 mask |= OMP_FOR_CLAUSE_MASK;
33902 if (cclauses)
33903 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33904 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33905 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33906 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33907
33908 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33909 {
33910 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33911 const char *p = IDENTIFIER_POINTER (id);
33912
33913 if (strcmp (p, "simd") == 0)
33914 {
33915 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33916 if (cclauses == NULL)
33917 cclauses = cclauses_buf;
33918
33919 cp_lexer_consume_token (parser->lexer);
33920 if (!flag_openmp) /* flag_openmp_simd */
33921 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33922 cclauses, if_p);
33923 sb = begin_omp_structured_block ();
33924 save = cp_parser_begin_omp_structured_block (parser);
33925 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33926 cclauses, if_p);
33927 cp_parser_end_omp_structured_block (parser, save);
33928 tree body = finish_omp_structured_block (sb);
33929 if (ret == NULL)
33930 return ret;
33931 ret = make_node (OMP_FOR);
33932 TREE_TYPE (ret) = void_type_node;
33933 OMP_FOR_BODY (ret) = body;
33934 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33935 SET_EXPR_LOCATION (ret, loc);
33936 add_stmt (ret);
33937 return ret;
33938 }
33939 }
33940 if (!flag_openmp) /* flag_openmp_simd */
33941 {
33942 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33943 return NULL_TREE;
33944 }
33945
33946 /* Composite distribute parallel for disallows linear clause. */
33947 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33948 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33949
33950 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33951 cclauses == NULL);
33952 if (cclauses)
33953 {
33954 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33955 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33956 }
33957
33958 sb = begin_omp_structured_block ();
33959 save = cp_parser_begin_omp_structured_block (parser);
33960
33961 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
33962
33963 cp_parser_end_omp_structured_block (parser, save);
33964 add_stmt (finish_omp_structured_block (sb));
33965
33966 return ret;
33967 }
33968
33969 /* OpenMP 2.5:
33970 # pragma omp master new-line
33971 structured-block */
33972
33973 static tree
33974 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33975 {
33976 cp_parser_require_pragma_eol (parser, pragma_tok);
33977 return c_finish_omp_master (input_location,
33978 cp_parser_omp_structured_block (parser, if_p));
33979 }
33980
33981 /* OpenMP 2.5:
33982 # pragma omp ordered new-line
33983 structured-block
33984
33985 OpenMP 4.5:
33986 # pragma omp ordered ordered-clauses new-line
33987 structured-block */
33988
33989 #define OMP_ORDERED_CLAUSE_MASK \
33990 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33992
33993 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33994 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
33995
33996 static bool
33997 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
33998 enum pragma_context context, bool *if_p)
33999 {
34000 location_t loc = pragma_tok->location;
34001
34002 if (context != pragma_stmt && context != pragma_compound)
34003 {
34004 cp_parser_error (parser, "expected declaration specifiers");
34005 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34006 return false;
34007 }
34008
34009 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34010 {
34011 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34012 const char *p = IDENTIFIER_POINTER (id);
34013
34014 if (strcmp (p, "depend") == 0)
34015 {
34016 if (context == pragma_stmt)
34017 {
34018 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34019 "%<depend%> clause may only be used in compound "
34020 "statements");
34021 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34022 return false;
34023 }
34024 tree clauses
34025 = cp_parser_omp_all_clauses (parser,
34026 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34027 "#pragma omp ordered", pragma_tok);
34028 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34029 return false;
34030 }
34031 }
34032
34033 tree clauses
34034 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34035 "#pragma omp ordered", pragma_tok);
34036 c_finish_omp_ordered (loc, clauses,
34037 cp_parser_omp_structured_block (parser, if_p));
34038 return true;
34039 }
34040
34041 /* OpenMP 2.5:
34042
34043 section-scope:
34044 { section-sequence }
34045
34046 section-sequence:
34047 section-directive[opt] structured-block
34048 section-sequence section-directive structured-block */
34049
34050 static tree
34051 cp_parser_omp_sections_scope (cp_parser *parser)
34052 {
34053 tree stmt, substmt;
34054 bool error_suppress = false;
34055 cp_token *tok;
34056
34057 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34058 return NULL_TREE;
34059
34060 stmt = push_stmt_list ();
34061
34062 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34063 != PRAGMA_OMP_SECTION)
34064 {
34065 substmt = cp_parser_omp_structured_block (parser, NULL);
34066 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34067 add_stmt (substmt);
34068 }
34069
34070 while (1)
34071 {
34072 tok = cp_lexer_peek_token (parser->lexer);
34073 if (tok->type == CPP_CLOSE_BRACE)
34074 break;
34075 if (tok->type == CPP_EOF)
34076 break;
34077
34078 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34079 {
34080 cp_lexer_consume_token (parser->lexer);
34081 cp_parser_require_pragma_eol (parser, tok);
34082 error_suppress = false;
34083 }
34084 else if (!error_suppress)
34085 {
34086 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34087 error_suppress = true;
34088 }
34089
34090 substmt = cp_parser_omp_structured_block (parser, NULL);
34091 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34092 add_stmt (substmt);
34093 }
34094 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34095
34096 substmt = pop_stmt_list (stmt);
34097
34098 stmt = make_node (OMP_SECTIONS);
34099 TREE_TYPE (stmt) = void_type_node;
34100 OMP_SECTIONS_BODY (stmt) = substmt;
34101
34102 add_stmt (stmt);
34103 return stmt;
34104 }
34105
34106 /* OpenMP 2.5:
34107 # pragma omp sections sections-clause[optseq] newline
34108 sections-scope */
34109
34110 #define OMP_SECTIONS_CLAUSE_MASK \
34111 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34116
34117 static tree
34118 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34119 char *p_name, omp_clause_mask mask, tree *cclauses)
34120 {
34121 tree clauses, ret;
34122 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34123
34124 strcat (p_name, " sections");
34125 mask |= OMP_SECTIONS_CLAUSE_MASK;
34126 if (cclauses)
34127 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34128
34129 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34130 cclauses == NULL);
34131 if (cclauses)
34132 {
34133 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34134 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34135 }
34136
34137 ret = cp_parser_omp_sections_scope (parser);
34138 if (ret)
34139 OMP_SECTIONS_CLAUSES (ret) = clauses;
34140
34141 return ret;
34142 }
34143
34144 /* OpenMP 2.5:
34145 # pragma omp parallel parallel-clause[optseq] new-line
34146 structured-block
34147 # pragma omp parallel for parallel-for-clause[optseq] new-line
34148 structured-block
34149 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34150 structured-block
34151
34152 OpenMP 4.0:
34153 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34154 structured-block */
34155
34156 #define OMP_PARALLEL_CLAUSE_MASK \
34157 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34166
34167 static tree
34168 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34169 char *p_name, omp_clause_mask mask, tree *cclauses,
34170 bool *if_p)
34171 {
34172 tree stmt, clauses, block;
34173 unsigned int save;
34174 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34175
34176 strcat (p_name, " parallel");
34177 mask |= OMP_PARALLEL_CLAUSE_MASK;
34178 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34179 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34180 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34181 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34182
34183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34184 {
34185 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34186 if (cclauses == NULL)
34187 cclauses = cclauses_buf;
34188
34189 cp_lexer_consume_token (parser->lexer);
34190 if (!flag_openmp) /* flag_openmp_simd */
34191 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34192 if_p);
34193 block = begin_omp_parallel ();
34194 save = cp_parser_begin_omp_structured_block (parser);
34195 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34196 if_p);
34197 cp_parser_end_omp_structured_block (parser, save);
34198 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34199 block);
34200 if (ret == NULL_TREE)
34201 return ret;
34202 OMP_PARALLEL_COMBINED (stmt) = 1;
34203 return stmt;
34204 }
34205 /* When combined with distribute, parallel has to be followed by for.
34206 #pragma omp target parallel is allowed though. */
34207 else if (cclauses
34208 && (mask & (OMP_CLAUSE_MASK_1
34209 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34210 {
34211 error_at (loc, "expected %<for%> after %qs", p_name);
34212 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34213 return NULL_TREE;
34214 }
34215 else if (!flag_openmp) /* flag_openmp_simd */
34216 {
34217 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34218 return NULL_TREE;
34219 }
34220 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34221 {
34222 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34223 const char *p = IDENTIFIER_POINTER (id);
34224 if (strcmp (p, "sections") == 0)
34225 {
34226 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34227 cclauses = cclauses_buf;
34228
34229 cp_lexer_consume_token (parser->lexer);
34230 block = begin_omp_parallel ();
34231 save = cp_parser_begin_omp_structured_block (parser);
34232 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34233 cp_parser_end_omp_structured_block (parser, save);
34234 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34235 block);
34236 OMP_PARALLEL_COMBINED (stmt) = 1;
34237 return stmt;
34238 }
34239 }
34240
34241 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
34242 if (cclauses)
34243 {
34244 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34245 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34246 }
34247
34248 block = begin_omp_parallel ();
34249 save = cp_parser_begin_omp_structured_block (parser);
34250 cp_parser_statement (parser, NULL_TREE, false, if_p);
34251 cp_parser_end_omp_structured_block (parser, save);
34252 stmt = finish_omp_parallel (clauses, block);
34253 return stmt;
34254 }
34255
34256 /* OpenMP 2.5:
34257 # pragma omp single single-clause[optseq] new-line
34258 structured-block */
34259
34260 #define OMP_SINGLE_CLAUSE_MASK \
34261 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34265
34266 static tree
34267 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34268 {
34269 tree stmt = make_node (OMP_SINGLE);
34270 TREE_TYPE (stmt) = void_type_node;
34271
34272 OMP_SINGLE_CLAUSES (stmt)
34273 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34274 "#pragma omp single", pragma_tok);
34275 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34276
34277 return add_stmt (stmt);
34278 }
34279
34280 /* OpenMP 3.0:
34281 # pragma omp task task-clause[optseq] new-line
34282 structured-block */
34283
34284 #define OMP_TASK_CLAUSE_MASK \
34285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34295
34296 static tree
34297 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34298 {
34299 tree clauses, block;
34300 unsigned int save;
34301
34302 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34303 "#pragma omp task", pragma_tok);
34304 block = begin_omp_task ();
34305 save = cp_parser_begin_omp_structured_block (parser);
34306 cp_parser_statement (parser, NULL_TREE, false, if_p);
34307 cp_parser_end_omp_structured_block (parser, save);
34308 return finish_omp_task (clauses, block);
34309 }
34310
34311 /* OpenMP 3.0:
34312 # pragma omp taskwait new-line */
34313
34314 static void
34315 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34316 {
34317 cp_parser_require_pragma_eol (parser, pragma_tok);
34318 finish_omp_taskwait ();
34319 }
34320
34321 /* OpenMP 3.1:
34322 # pragma omp taskyield new-line */
34323
34324 static void
34325 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34326 {
34327 cp_parser_require_pragma_eol (parser, pragma_tok);
34328 finish_omp_taskyield ();
34329 }
34330
34331 /* OpenMP 4.0:
34332 # pragma omp taskgroup new-line
34333 structured-block */
34334
34335 static tree
34336 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34337 {
34338 cp_parser_require_pragma_eol (parser, pragma_tok);
34339 return c_finish_omp_taskgroup (input_location,
34340 cp_parser_omp_structured_block (parser,
34341 if_p));
34342 }
34343
34344
34345 /* OpenMP 2.5:
34346 # pragma omp threadprivate (variable-list) */
34347
34348 static void
34349 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34350 {
34351 tree vars;
34352
34353 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34354 cp_parser_require_pragma_eol (parser, pragma_tok);
34355
34356 finish_omp_threadprivate (vars);
34357 }
34358
34359 /* OpenMP 4.0:
34360 # pragma omp cancel cancel-clause[optseq] new-line */
34361
34362 #define OMP_CANCEL_CLAUSE_MASK \
34363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34368
34369 static void
34370 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34371 {
34372 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34373 "#pragma omp cancel", pragma_tok);
34374 finish_omp_cancel (clauses);
34375 }
34376
34377 /* OpenMP 4.0:
34378 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34379
34380 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34381 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34385
34386 static void
34387 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34388 {
34389 tree clauses;
34390 bool point_seen = false;
34391
34392 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34393 {
34394 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34395 const char *p = IDENTIFIER_POINTER (id);
34396
34397 if (strcmp (p, "point") == 0)
34398 {
34399 cp_lexer_consume_token (parser->lexer);
34400 point_seen = true;
34401 }
34402 }
34403 if (!point_seen)
34404 {
34405 cp_parser_error (parser, "expected %<point%>");
34406 cp_parser_require_pragma_eol (parser, pragma_tok);
34407 return;
34408 }
34409
34410 clauses = cp_parser_omp_all_clauses (parser,
34411 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34412 "#pragma omp cancellation point",
34413 pragma_tok);
34414 finish_omp_cancellation_point (clauses);
34415 }
34416
34417 /* OpenMP 4.0:
34418 #pragma omp distribute distribute-clause[optseq] new-line
34419 for-loop */
34420
34421 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34422 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34427
34428 static tree
34429 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34430 char *p_name, omp_clause_mask mask, tree *cclauses,
34431 bool *if_p)
34432 {
34433 tree clauses, sb, ret;
34434 unsigned int save;
34435 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34436
34437 strcat (p_name, " distribute");
34438 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34439
34440 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34441 {
34442 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34443 const char *p = IDENTIFIER_POINTER (id);
34444 bool simd = false;
34445 bool parallel = false;
34446
34447 if (strcmp (p, "simd") == 0)
34448 simd = true;
34449 else
34450 parallel = strcmp (p, "parallel") == 0;
34451 if (parallel || simd)
34452 {
34453 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34454 if (cclauses == NULL)
34455 cclauses = cclauses_buf;
34456 cp_lexer_consume_token (parser->lexer);
34457 if (!flag_openmp) /* flag_openmp_simd */
34458 {
34459 if (simd)
34460 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34461 cclauses, if_p);
34462 else
34463 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34464 cclauses, if_p);
34465 }
34466 sb = begin_omp_structured_block ();
34467 save = cp_parser_begin_omp_structured_block (parser);
34468 if (simd)
34469 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34470 cclauses, if_p);
34471 else
34472 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34473 cclauses, if_p);
34474 cp_parser_end_omp_structured_block (parser, save);
34475 tree body = finish_omp_structured_block (sb);
34476 if (ret == NULL)
34477 return ret;
34478 ret = make_node (OMP_DISTRIBUTE);
34479 TREE_TYPE (ret) = void_type_node;
34480 OMP_FOR_BODY (ret) = body;
34481 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34482 SET_EXPR_LOCATION (ret, loc);
34483 add_stmt (ret);
34484 return ret;
34485 }
34486 }
34487 if (!flag_openmp) /* flag_openmp_simd */
34488 {
34489 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34490 return NULL_TREE;
34491 }
34492
34493 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34494 cclauses == NULL);
34495 if (cclauses)
34496 {
34497 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34498 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34499 }
34500
34501 sb = begin_omp_structured_block ();
34502 save = cp_parser_begin_omp_structured_block (parser);
34503
34504 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34505
34506 cp_parser_end_omp_structured_block (parser, save);
34507 add_stmt (finish_omp_structured_block (sb));
34508
34509 return ret;
34510 }
34511
34512 /* OpenMP 4.0:
34513 # pragma omp teams teams-clause[optseq] new-line
34514 structured-block */
34515
34516 #define OMP_TEAMS_CLAUSE_MASK \
34517 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34524
34525 static tree
34526 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34527 char *p_name, omp_clause_mask mask, tree *cclauses,
34528 bool *if_p)
34529 {
34530 tree clauses, sb, ret;
34531 unsigned int save;
34532 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34533
34534 strcat (p_name, " teams");
34535 mask |= OMP_TEAMS_CLAUSE_MASK;
34536
34537 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34538 {
34539 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34540 const char *p = IDENTIFIER_POINTER (id);
34541 if (strcmp (p, "distribute") == 0)
34542 {
34543 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34544 if (cclauses == NULL)
34545 cclauses = cclauses_buf;
34546
34547 cp_lexer_consume_token (parser->lexer);
34548 if (!flag_openmp) /* flag_openmp_simd */
34549 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34550 cclauses, if_p);
34551 sb = begin_omp_structured_block ();
34552 save = cp_parser_begin_omp_structured_block (parser);
34553 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34554 cclauses, if_p);
34555 cp_parser_end_omp_structured_block (parser, save);
34556 tree body = finish_omp_structured_block (sb);
34557 if (ret == NULL)
34558 return ret;
34559 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34560 ret = make_node (OMP_TEAMS);
34561 TREE_TYPE (ret) = void_type_node;
34562 OMP_TEAMS_CLAUSES (ret) = clauses;
34563 OMP_TEAMS_BODY (ret) = body;
34564 OMP_TEAMS_COMBINED (ret) = 1;
34565 return add_stmt (ret);
34566 }
34567 }
34568 if (!flag_openmp) /* flag_openmp_simd */
34569 {
34570 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34571 return NULL_TREE;
34572 }
34573
34574 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34575 cclauses == NULL);
34576 if (cclauses)
34577 {
34578 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34579 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34580 }
34581
34582 tree stmt = make_node (OMP_TEAMS);
34583 TREE_TYPE (stmt) = void_type_node;
34584 OMP_TEAMS_CLAUSES (stmt) = clauses;
34585 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34586
34587 return add_stmt (stmt);
34588 }
34589
34590 /* OpenMP 4.0:
34591 # pragma omp target data target-data-clause[optseq] new-line
34592 structured-block */
34593
34594 #define OMP_TARGET_DATA_CLAUSE_MASK \
34595 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34599
34600 static tree
34601 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34602 {
34603 tree clauses
34604 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34605 "#pragma omp target data", pragma_tok);
34606 int map_seen = 0;
34607 for (tree *pc = &clauses; *pc;)
34608 {
34609 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34610 switch (OMP_CLAUSE_MAP_KIND (*pc))
34611 {
34612 case GOMP_MAP_TO:
34613 case GOMP_MAP_ALWAYS_TO:
34614 case GOMP_MAP_FROM:
34615 case GOMP_MAP_ALWAYS_FROM:
34616 case GOMP_MAP_TOFROM:
34617 case GOMP_MAP_ALWAYS_TOFROM:
34618 case GOMP_MAP_ALLOC:
34619 map_seen = 3;
34620 break;
34621 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34622 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34623 case GOMP_MAP_ALWAYS_POINTER:
34624 break;
34625 default:
34626 map_seen |= 1;
34627 error_at (OMP_CLAUSE_LOCATION (*pc),
34628 "%<#pragma omp target data%> with map-type other "
34629 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34630 "on %<map%> clause");
34631 *pc = OMP_CLAUSE_CHAIN (*pc);
34632 continue;
34633 }
34634 pc = &OMP_CLAUSE_CHAIN (*pc);
34635 }
34636
34637 if (map_seen != 3)
34638 {
34639 if (map_seen == 0)
34640 error_at (pragma_tok->location,
34641 "%<#pragma omp target data%> must contain at least "
34642 "one %<map%> clause");
34643 return NULL_TREE;
34644 }
34645
34646 tree stmt = make_node (OMP_TARGET_DATA);
34647 TREE_TYPE (stmt) = void_type_node;
34648 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34649
34650 keep_next_level (true);
34651 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34652
34653 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34654 return add_stmt (stmt);
34655 }
34656
34657 /* OpenMP 4.5:
34658 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34659 structured-block */
34660
34661 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34662 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34667
34668 static tree
34669 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34670 enum pragma_context context)
34671 {
34672 bool data_seen = false;
34673 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34674 {
34675 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34676 const char *p = IDENTIFIER_POINTER (id);
34677
34678 if (strcmp (p, "data") == 0)
34679 {
34680 cp_lexer_consume_token (parser->lexer);
34681 data_seen = true;
34682 }
34683 }
34684 if (!data_seen)
34685 {
34686 cp_parser_error (parser, "expected %<data%>");
34687 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34688 return NULL_TREE;
34689 }
34690
34691 if (context == pragma_stmt)
34692 {
34693 error_at (pragma_tok->location,
34694 "%<#pragma omp target enter data%> may only be "
34695 "used in compound statements");
34696 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34697 return NULL_TREE;
34698 }
34699
34700 tree clauses
34701 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34702 "#pragma omp target enter data", pragma_tok);
34703 int map_seen = 0;
34704 for (tree *pc = &clauses; *pc;)
34705 {
34706 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34707 switch (OMP_CLAUSE_MAP_KIND (*pc))
34708 {
34709 case GOMP_MAP_TO:
34710 case GOMP_MAP_ALWAYS_TO:
34711 case GOMP_MAP_ALLOC:
34712 map_seen = 3;
34713 break;
34714 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34715 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34716 case GOMP_MAP_ALWAYS_POINTER:
34717 break;
34718 default:
34719 map_seen |= 1;
34720 error_at (OMP_CLAUSE_LOCATION (*pc),
34721 "%<#pragma omp target enter data%> with map-type other "
34722 "than %<to%> or %<alloc%> on %<map%> clause");
34723 *pc = OMP_CLAUSE_CHAIN (*pc);
34724 continue;
34725 }
34726 pc = &OMP_CLAUSE_CHAIN (*pc);
34727 }
34728
34729 if (map_seen != 3)
34730 {
34731 if (map_seen == 0)
34732 error_at (pragma_tok->location,
34733 "%<#pragma omp target enter data%> must contain at least "
34734 "one %<map%> clause");
34735 return NULL_TREE;
34736 }
34737
34738 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34739 TREE_TYPE (stmt) = void_type_node;
34740 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34741 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34742 return add_stmt (stmt);
34743 }
34744
34745 /* OpenMP 4.5:
34746 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34747 structured-block */
34748
34749 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34750 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34755
34756 static tree
34757 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34758 enum pragma_context context)
34759 {
34760 bool data_seen = false;
34761 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34762 {
34763 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34764 const char *p = IDENTIFIER_POINTER (id);
34765
34766 if (strcmp (p, "data") == 0)
34767 {
34768 cp_lexer_consume_token (parser->lexer);
34769 data_seen = true;
34770 }
34771 }
34772 if (!data_seen)
34773 {
34774 cp_parser_error (parser, "expected %<data%>");
34775 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34776 return NULL_TREE;
34777 }
34778
34779 if (context == pragma_stmt)
34780 {
34781 error_at (pragma_tok->location,
34782 "%<#pragma omp target exit data%> may only be "
34783 "used in compound statements");
34784 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34785 return NULL_TREE;
34786 }
34787
34788 tree clauses
34789 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34790 "#pragma omp target exit data", pragma_tok);
34791 int map_seen = 0;
34792 for (tree *pc = &clauses; *pc;)
34793 {
34794 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34795 switch (OMP_CLAUSE_MAP_KIND (*pc))
34796 {
34797 case GOMP_MAP_FROM:
34798 case GOMP_MAP_ALWAYS_FROM:
34799 case GOMP_MAP_RELEASE:
34800 case GOMP_MAP_DELETE:
34801 map_seen = 3;
34802 break;
34803 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34804 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34805 case GOMP_MAP_ALWAYS_POINTER:
34806 break;
34807 default:
34808 map_seen |= 1;
34809 error_at (OMP_CLAUSE_LOCATION (*pc),
34810 "%<#pragma omp target exit data%> with map-type other "
34811 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34812 " clause");
34813 *pc = OMP_CLAUSE_CHAIN (*pc);
34814 continue;
34815 }
34816 pc = &OMP_CLAUSE_CHAIN (*pc);
34817 }
34818
34819 if (map_seen != 3)
34820 {
34821 if (map_seen == 0)
34822 error_at (pragma_tok->location,
34823 "%<#pragma omp target exit data%> must contain at least "
34824 "one %<map%> clause");
34825 return NULL_TREE;
34826 }
34827
34828 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34829 TREE_TYPE (stmt) = void_type_node;
34830 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34831 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34832 return add_stmt (stmt);
34833 }
34834
34835 /* OpenMP 4.0:
34836 # pragma omp target update target-update-clause[optseq] new-line */
34837
34838 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34839 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34845
34846 static bool
34847 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34848 enum pragma_context context)
34849 {
34850 if (context == pragma_stmt)
34851 {
34852 error_at (pragma_tok->location,
34853 "%<#pragma omp target update%> may only be "
34854 "used in compound statements");
34855 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34856 return false;
34857 }
34858
34859 tree clauses
34860 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34861 "#pragma omp target update", pragma_tok);
34862 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34863 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34864 {
34865 error_at (pragma_tok->location,
34866 "%<#pragma omp target update%> must contain at least one "
34867 "%<from%> or %<to%> clauses");
34868 return false;
34869 }
34870
34871 tree stmt = make_node (OMP_TARGET_UPDATE);
34872 TREE_TYPE (stmt) = void_type_node;
34873 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34874 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34875 add_stmt (stmt);
34876 return false;
34877 }
34878
34879 /* OpenMP 4.0:
34880 # pragma omp target target-clause[optseq] new-line
34881 structured-block */
34882
34883 #define OMP_TARGET_CLAUSE_MASK \
34884 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34893
34894 static bool
34895 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34896 enum pragma_context context, bool *if_p)
34897 {
34898 tree *pc = NULL, stmt;
34899
34900 if (context != pragma_stmt && context != pragma_compound)
34901 {
34902 cp_parser_error (parser, "expected declaration specifiers");
34903 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34904 return false;
34905 }
34906
34907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34908 {
34909 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34910 const char *p = IDENTIFIER_POINTER (id);
34911 enum tree_code ccode = ERROR_MARK;
34912
34913 if (strcmp (p, "teams") == 0)
34914 ccode = OMP_TEAMS;
34915 else if (strcmp (p, "parallel") == 0)
34916 ccode = OMP_PARALLEL;
34917 else if (strcmp (p, "simd") == 0)
34918 ccode = OMP_SIMD;
34919 if (ccode != ERROR_MARK)
34920 {
34921 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34922 char p_name[sizeof ("#pragma omp target teams distribute "
34923 "parallel for simd")];
34924
34925 cp_lexer_consume_token (parser->lexer);
34926 strcpy (p_name, "#pragma omp target");
34927 if (!flag_openmp) /* flag_openmp_simd */
34928 {
34929 tree stmt;
34930 switch (ccode)
34931 {
34932 case OMP_TEAMS:
34933 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34934 OMP_TARGET_CLAUSE_MASK,
34935 cclauses, if_p);
34936 break;
34937 case OMP_PARALLEL:
34938 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34939 OMP_TARGET_CLAUSE_MASK,
34940 cclauses, if_p);
34941 break;
34942 case OMP_SIMD:
34943 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34944 OMP_TARGET_CLAUSE_MASK,
34945 cclauses, if_p);
34946 break;
34947 default:
34948 gcc_unreachable ();
34949 }
34950 return stmt != NULL_TREE;
34951 }
34952 keep_next_level (true);
34953 tree sb = begin_omp_structured_block (), ret;
34954 unsigned save = cp_parser_begin_omp_structured_block (parser);
34955 switch (ccode)
34956 {
34957 case OMP_TEAMS:
34958 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34959 OMP_TARGET_CLAUSE_MASK, cclauses,
34960 if_p);
34961 break;
34962 case OMP_PARALLEL:
34963 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34964 OMP_TARGET_CLAUSE_MASK, cclauses,
34965 if_p);
34966 break;
34967 case OMP_SIMD:
34968 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
34969 OMP_TARGET_CLAUSE_MASK, cclauses,
34970 if_p);
34971 break;
34972 default:
34973 gcc_unreachable ();
34974 }
34975 cp_parser_end_omp_structured_block (parser, save);
34976 tree body = finish_omp_structured_block (sb);
34977 if (ret == NULL_TREE)
34978 return false;
34979 if (ccode == OMP_TEAMS && !processing_template_decl)
34980 {
34981 /* For combined target teams, ensure the num_teams and
34982 thread_limit clause expressions are evaluated on the host,
34983 before entering the target construct. */
34984 tree c;
34985 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34986 c; c = OMP_CLAUSE_CHAIN (c))
34987 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
34988 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
34989 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
34990 {
34991 tree expr = OMP_CLAUSE_OPERAND (c, 0);
34992 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
34993 if (expr == error_mark_node)
34994 continue;
34995 tree tmp = TARGET_EXPR_SLOT (expr);
34996 add_stmt (expr);
34997 OMP_CLAUSE_OPERAND (c, 0) = expr;
34998 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
34999 OMP_CLAUSE_FIRSTPRIVATE);
35000 OMP_CLAUSE_DECL (tc) = tmp;
35001 OMP_CLAUSE_CHAIN (tc)
35002 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35003 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35004 }
35005 }
35006 tree stmt = make_node (OMP_TARGET);
35007 TREE_TYPE (stmt) = void_type_node;
35008 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35009 OMP_TARGET_BODY (stmt) = body;
35010 OMP_TARGET_COMBINED (stmt) = 1;
35011 add_stmt (stmt);
35012 pc = &OMP_TARGET_CLAUSES (stmt);
35013 goto check_clauses;
35014 }
35015 else if (!flag_openmp) /* flag_openmp_simd */
35016 {
35017 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35018 return false;
35019 }
35020 else if (strcmp (p, "data") == 0)
35021 {
35022 cp_lexer_consume_token (parser->lexer);
35023 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35024 return true;
35025 }
35026 else if (strcmp (p, "enter") == 0)
35027 {
35028 cp_lexer_consume_token (parser->lexer);
35029 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35030 return false;
35031 }
35032 else if (strcmp (p, "exit") == 0)
35033 {
35034 cp_lexer_consume_token (parser->lexer);
35035 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35036 return false;
35037 }
35038 else if (strcmp (p, "update") == 0)
35039 {
35040 cp_lexer_consume_token (parser->lexer);
35041 return cp_parser_omp_target_update (parser, pragma_tok, context);
35042 }
35043 }
35044
35045 stmt = make_node (OMP_TARGET);
35046 TREE_TYPE (stmt) = void_type_node;
35047
35048 OMP_TARGET_CLAUSES (stmt)
35049 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35050 "#pragma omp target", pragma_tok);
35051 pc = &OMP_TARGET_CLAUSES (stmt);
35052 keep_next_level (true);
35053 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35054
35055 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35056 add_stmt (stmt);
35057
35058 check_clauses:
35059 while (*pc)
35060 {
35061 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35062 switch (OMP_CLAUSE_MAP_KIND (*pc))
35063 {
35064 case GOMP_MAP_TO:
35065 case GOMP_MAP_ALWAYS_TO:
35066 case GOMP_MAP_FROM:
35067 case GOMP_MAP_ALWAYS_FROM:
35068 case GOMP_MAP_TOFROM:
35069 case GOMP_MAP_ALWAYS_TOFROM:
35070 case GOMP_MAP_ALLOC:
35071 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35072 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35073 case GOMP_MAP_ALWAYS_POINTER:
35074 break;
35075 default:
35076 error_at (OMP_CLAUSE_LOCATION (*pc),
35077 "%<#pragma omp target%> with map-type other "
35078 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35079 "on %<map%> clause");
35080 *pc = OMP_CLAUSE_CHAIN (*pc);
35081 continue;
35082 }
35083 pc = &OMP_CLAUSE_CHAIN (*pc);
35084 }
35085 return true;
35086 }
35087
35088 /* OpenACC 2.0:
35089 # pragma acc cache (variable-list) new-line
35090 */
35091
35092 static tree
35093 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35094 {
35095 tree stmt, clauses;
35096
35097 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35098 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35099
35100 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35101
35102 stmt = make_node (OACC_CACHE);
35103 TREE_TYPE (stmt) = void_type_node;
35104 OACC_CACHE_CLAUSES (stmt) = clauses;
35105 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35106 add_stmt (stmt);
35107
35108 return stmt;
35109 }
35110
35111 /* OpenACC 2.0:
35112 # pragma acc data oacc-data-clause[optseq] new-line
35113 structured-block */
35114
35115 #define OACC_DATA_CLAUSE_MASK \
35116 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35127
35128 static tree
35129 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35130 {
35131 tree stmt, clauses, block;
35132 unsigned int save;
35133
35134 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35135 "#pragma acc data", pragma_tok);
35136
35137 block = begin_omp_parallel ();
35138 save = cp_parser_begin_omp_structured_block (parser);
35139 cp_parser_statement (parser, NULL_TREE, false, if_p);
35140 cp_parser_end_omp_structured_block (parser, save);
35141 stmt = finish_oacc_data (clauses, block);
35142 return stmt;
35143 }
35144
35145 /* OpenACC 2.0:
35146 # pragma acc host_data <clauses> new-line
35147 structured-block */
35148
35149 #define OACC_HOST_DATA_CLAUSE_MASK \
35150 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35151
35152 static tree
35153 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35154 {
35155 tree stmt, clauses, block;
35156 unsigned int save;
35157
35158 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35159 "#pragma acc host_data", pragma_tok);
35160
35161 block = begin_omp_parallel ();
35162 save = cp_parser_begin_omp_structured_block (parser);
35163 cp_parser_statement (parser, NULL_TREE, false, if_p);
35164 cp_parser_end_omp_structured_block (parser, save);
35165 stmt = finish_oacc_host_data (clauses, block);
35166 return stmt;
35167 }
35168
35169 /* OpenACC 2.0:
35170 # pragma acc declare oacc-data-clause[optseq] new-line
35171 */
35172
35173 #define OACC_DECLARE_CLAUSE_MASK \
35174 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35186
35187 static tree
35188 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35189 {
35190 tree clauses, stmt;
35191 bool error = false;
35192
35193 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35194 "#pragma acc declare", pragma_tok, true);
35195
35196
35197 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35198 {
35199 error_at (pragma_tok->location,
35200 "no valid clauses specified in %<#pragma acc declare%>");
35201 return NULL_TREE;
35202 }
35203
35204 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35205 {
35206 location_t loc = OMP_CLAUSE_LOCATION (t);
35207 tree decl = OMP_CLAUSE_DECL (t);
35208 if (!DECL_P (decl))
35209 {
35210 error_at (loc, "array section in %<#pragma acc declare%>");
35211 error = true;
35212 continue;
35213 }
35214 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35215 switch (OMP_CLAUSE_MAP_KIND (t))
35216 {
35217 case GOMP_MAP_FORCE_ALLOC:
35218 case GOMP_MAP_FORCE_TO:
35219 case GOMP_MAP_FORCE_DEVICEPTR:
35220 case GOMP_MAP_DEVICE_RESIDENT:
35221 break;
35222
35223 case GOMP_MAP_POINTER:
35224 /* Generated by c_finish_omp_clauses from array sections;
35225 avoid spurious diagnostics. */
35226 break;
35227
35228 case GOMP_MAP_LINK:
35229 if (!global_bindings_p ()
35230 && (TREE_STATIC (decl)
35231 || !DECL_EXTERNAL (decl)))
35232 {
35233 error_at (loc,
35234 "%qD must be a global variable in"
35235 "%<#pragma acc declare link%>",
35236 decl);
35237 error = true;
35238 continue;
35239 }
35240 break;
35241
35242 default:
35243 if (global_bindings_p ())
35244 {
35245 error_at (loc, "invalid OpenACC clause at file scope");
35246 error = true;
35247 continue;
35248 }
35249 if (DECL_EXTERNAL (decl))
35250 {
35251 error_at (loc,
35252 "invalid use of %<extern%> variable %qD "
35253 "in %<#pragma acc declare%>", decl);
35254 error = true;
35255 continue;
35256 }
35257 else if (TREE_PUBLIC (decl))
35258 {
35259 error_at (loc,
35260 "invalid use of %<global%> variable %qD "
35261 "in %<#pragma acc declare%>", decl);
35262 error = true;
35263 continue;
35264 }
35265 break;
35266 }
35267
35268 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35269 || lookup_attribute ("omp declare target link",
35270 DECL_ATTRIBUTES (decl)))
35271 {
35272 error_at (loc, "variable %qD used more than once with "
35273 "%<#pragma acc declare%>", decl);
35274 error = true;
35275 continue;
35276 }
35277
35278 if (!error)
35279 {
35280 tree id;
35281
35282 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35283 id = get_identifier ("omp declare target link");
35284 else
35285 id = get_identifier ("omp declare target");
35286
35287 DECL_ATTRIBUTES (decl)
35288 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35289 if (global_bindings_p ())
35290 {
35291 symtab_node *node = symtab_node::get (decl);
35292 if (node != NULL)
35293 {
35294 node->offloadable = 1;
35295 if (ENABLE_OFFLOADING)
35296 {
35297 g->have_offload = true;
35298 if (is_a <varpool_node *> (node))
35299 vec_safe_push (offload_vars, decl);
35300 }
35301 }
35302 }
35303 }
35304 }
35305
35306 if (error || global_bindings_p ())
35307 return NULL_TREE;
35308
35309 stmt = make_node (OACC_DECLARE);
35310 TREE_TYPE (stmt) = void_type_node;
35311 OACC_DECLARE_CLAUSES (stmt) = clauses;
35312 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35313
35314 add_stmt (stmt);
35315
35316 return NULL_TREE;
35317 }
35318
35319 /* OpenACC 2.0:
35320 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35321
35322 or
35323
35324 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35325
35326 LOC is the location of the #pragma token.
35327 */
35328
35329 #define OACC_ENTER_DATA_CLAUSE_MASK \
35330 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35337
35338 #define OACC_EXIT_DATA_CLAUSE_MASK \
35339 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35344
35345 static tree
35346 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35347 bool enter)
35348 {
35349 tree stmt, clauses;
35350
35351 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35352 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35353 {
35354 cp_parser_error (parser, enter
35355 ? "expected %<data%> in %<#pragma acc enter data%>"
35356 : "expected %<data%> in %<#pragma acc exit data%>");
35357 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35358 return NULL_TREE;
35359 }
35360
35361 const char *p =
35362 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35363 if (strcmp (p, "data") != 0)
35364 {
35365 cp_parser_error (parser, "invalid pragma");
35366 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35367 return NULL_TREE;
35368 }
35369
35370 cp_lexer_consume_token (parser->lexer);
35371
35372 if (enter)
35373 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35374 "#pragma acc enter data", pragma_tok);
35375 else
35376 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35377 "#pragma acc exit data", pragma_tok);
35378
35379 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35380 {
35381 error_at (pragma_tok->location,
35382 "%<#pragma acc enter data%> has no data movement clause");
35383 return NULL_TREE;
35384 }
35385
35386 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35387 TREE_TYPE (stmt) = void_type_node;
35388 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35389 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35390 add_stmt (stmt);
35391 return stmt;
35392 }
35393
35394 /* OpenACC 2.0:
35395 # pragma acc loop oacc-loop-clause[optseq] new-line
35396 structured-block */
35397
35398 #define OACC_LOOP_CLAUSE_MASK \
35399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35409
35410 static tree
35411 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35412 omp_clause_mask mask, tree *cclauses, bool *if_p)
35413 {
35414 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35415
35416 strcat (p_name, " loop");
35417 mask |= OACC_LOOP_CLAUSE_MASK;
35418
35419 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35420 cclauses == NULL);
35421 if (cclauses)
35422 {
35423 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35424 if (*cclauses)
35425 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
35426 if (clauses)
35427 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35428 }
35429
35430 tree block = begin_omp_structured_block ();
35431 int save = cp_parser_begin_omp_structured_block (parser);
35432 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35433 cp_parser_end_omp_structured_block (parser, save);
35434 add_stmt (finish_omp_structured_block (block));
35435
35436 return stmt;
35437 }
35438
35439 /* OpenACC 2.0:
35440 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35441 structured-block
35442
35443 or
35444
35445 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35446 structured-block
35447 */
35448
35449 #define OACC_KERNELS_CLAUSE_MASK \
35450 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35464
35465 #define OACC_PARALLEL_CLAUSE_MASK \
35466 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35486
35487 static tree
35488 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35489 char *p_name, bool *if_p)
35490 {
35491 omp_clause_mask mask;
35492 enum tree_code code;
35493 switch (cp_parser_pragma_kind (pragma_tok))
35494 {
35495 case PRAGMA_OACC_KERNELS:
35496 strcat (p_name, " kernels");
35497 mask = OACC_KERNELS_CLAUSE_MASK;
35498 code = OACC_KERNELS;
35499 break;
35500 case PRAGMA_OACC_PARALLEL:
35501 strcat (p_name, " parallel");
35502 mask = OACC_PARALLEL_CLAUSE_MASK;
35503 code = OACC_PARALLEL;
35504 break;
35505 default:
35506 gcc_unreachable ();
35507 }
35508
35509 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35510 {
35511 const char *p
35512 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35513 if (strcmp (p, "loop") == 0)
35514 {
35515 cp_lexer_consume_token (parser->lexer);
35516 tree block = begin_omp_parallel ();
35517 tree clauses;
35518 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35519 if_p);
35520 return finish_omp_construct (code, block, clauses);
35521 }
35522 }
35523
35524 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35525
35526 tree block = begin_omp_parallel ();
35527 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35528 cp_parser_statement (parser, NULL_TREE, false, if_p);
35529 cp_parser_end_omp_structured_block (parser, save);
35530 return finish_omp_construct (code, block, clauses);
35531 }
35532
35533 /* OpenACC 2.0:
35534 # pragma acc update oacc-update-clause[optseq] new-line
35535 */
35536
35537 #define OACC_UPDATE_CLAUSE_MASK \
35538 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35544
35545 static tree
35546 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35547 {
35548 tree stmt, clauses;
35549
35550 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35551 "#pragma acc update", pragma_tok);
35552
35553 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35554 {
35555 error_at (pragma_tok->location,
35556 "%<#pragma acc update%> must contain at least one "
35557 "%<device%> or %<host%> or %<self%> clause");
35558 return NULL_TREE;
35559 }
35560
35561 stmt = make_node (OACC_UPDATE);
35562 TREE_TYPE (stmt) = void_type_node;
35563 OACC_UPDATE_CLAUSES (stmt) = clauses;
35564 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35565 add_stmt (stmt);
35566 return stmt;
35567 }
35568
35569 /* OpenACC 2.0:
35570 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35571
35572 LOC is the location of the #pragma token.
35573 */
35574
35575 #define OACC_WAIT_CLAUSE_MASK \
35576 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35577
35578 static tree
35579 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35580 {
35581 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35582 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35583
35584 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35585 list = cp_parser_oacc_wait_list (parser, loc, list);
35586
35587 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35588 "#pragma acc wait", pragma_tok);
35589
35590 stmt = c_finish_oacc_wait (loc, list, clauses);
35591 stmt = finish_expr_stmt (stmt);
35592
35593 return stmt;
35594 }
35595
35596 /* OpenMP 4.0:
35597 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35598
35599 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35600 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35606
35607 static void
35608 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35609 enum pragma_context context)
35610 {
35611 bool first_p = parser->omp_declare_simd == NULL;
35612 cp_omp_declare_simd_data data;
35613 if (first_p)
35614 {
35615 data.error_seen = false;
35616 data.fndecl_seen = false;
35617 data.tokens = vNULL;
35618 parser->omp_declare_simd = &data;
35619 }
35620 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35621 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35622 cp_lexer_consume_token (parser->lexer);
35623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35624 parser->omp_declare_simd->error_seen = true;
35625 cp_parser_require_pragma_eol (parser, pragma_tok);
35626 struct cp_token_cache *cp
35627 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35628 parser->omp_declare_simd->tokens.safe_push (cp);
35629 if (first_p)
35630 {
35631 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35632 cp_parser_pragma (parser, context, NULL);
35633 switch (context)
35634 {
35635 case pragma_external:
35636 cp_parser_declaration (parser);
35637 break;
35638 case pragma_member:
35639 cp_parser_member_declaration (parser);
35640 break;
35641 case pragma_objc_icode:
35642 cp_parser_block_declaration (parser, /*statement_p=*/false);
35643 break;
35644 default:
35645 cp_parser_declaration_statement (parser);
35646 break;
35647 }
35648 if (parser->omp_declare_simd
35649 && !parser->omp_declare_simd->error_seen
35650 && !parser->omp_declare_simd->fndecl_seen)
35651 error_at (pragma_tok->location,
35652 "%<#pragma omp declare simd%> not immediately followed by "
35653 "function declaration or definition");
35654 data.tokens.release ();
35655 parser->omp_declare_simd = NULL;
35656 }
35657 }
35658
35659 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35660 This function is modelled similar to the late parsing of omp declare
35661 simd. */
35662
35663 static tree
35664 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35665 {
35666 struct cp_token_cache *ce;
35667 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35668 int ii = 0;
35669
35670 if (parser->omp_declare_simd != NULL
35671 || lookup_attribute ("simd", attrs))
35672 {
35673 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35674 "used in the same function marked as a Cilk Plus SIMD-enabled "
35675 " function");
35676 parser->cilk_simd_fn_info->tokens.release ();
35677 XDELETE (parser->cilk_simd_fn_info);
35678 parser->cilk_simd_fn_info = NULL;
35679 return attrs;
35680 }
35681 if (!info->error_seen && info->fndecl_seen)
35682 {
35683 error ("vector attribute not immediately followed by a single function"
35684 " declaration or definition");
35685 info->error_seen = true;
35686 }
35687 if (info->error_seen)
35688 return attrs;
35689
35690 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35691 {
35692 tree c, cl;
35693
35694 cp_parser_push_lexer_for_tokens (parser, ce);
35695 parser->lexer->in_pragma = true;
35696 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35697 "SIMD-enabled functions attribute",
35698 NULL);
35699 cp_parser_pop_lexer (parser);
35700 if (cl)
35701 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35702
35703 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35704 TREE_CHAIN (c) = attrs;
35705 attrs = c;
35706
35707 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35708 TREE_CHAIN (c) = attrs;
35709 if (processing_template_decl)
35710 ATTR_IS_DEPENDENT (c) = 1;
35711 attrs = c;
35712 }
35713 info->fndecl_seen = true;
35714 parser->cilk_simd_fn_info->tokens.release ();
35715 XDELETE (parser->cilk_simd_fn_info);
35716 parser->cilk_simd_fn_info = NULL;
35717 return attrs;
35718 }
35719
35720 /* Finalize #pragma omp declare simd clauses after direct declarator has
35721 been parsed, and put that into "omp declare simd" attribute. */
35722
35723 static tree
35724 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35725 {
35726 struct cp_token_cache *ce;
35727 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35728 int i;
35729
35730 if (!data->error_seen && data->fndecl_seen)
35731 {
35732 error ("%<#pragma omp declare simd%> not immediately followed by "
35733 "a single function declaration or definition");
35734 data->error_seen = true;
35735 return attrs;
35736 }
35737 if (data->error_seen)
35738 return attrs;
35739
35740 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35741 {
35742 tree c, cl;
35743
35744 cp_parser_push_lexer_for_tokens (parser, ce);
35745 parser->lexer->in_pragma = true;
35746 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35747 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35748 cp_lexer_consume_token (parser->lexer);
35749 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35750 "#pragma omp declare simd", pragma_tok);
35751 cp_parser_pop_lexer (parser);
35752 if (cl)
35753 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35754 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35755 TREE_CHAIN (c) = attrs;
35756 if (processing_template_decl)
35757 ATTR_IS_DEPENDENT (c) = 1;
35758 attrs = c;
35759 }
35760
35761 data->fndecl_seen = true;
35762 return attrs;
35763 }
35764
35765
35766 /* OpenMP 4.0:
35767 # pragma omp declare target new-line
35768 declarations and definitions
35769 # pragma omp end declare target new-line
35770
35771 OpenMP 4.5:
35772 # pragma omp declare target ( extended-list ) new-line
35773
35774 # pragma omp declare target declare-target-clauses[seq] new-line */
35775
35776 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35777 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35779
35780 static void
35781 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35782 {
35783 tree clauses = NULL_TREE;
35784 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35785 clauses
35786 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35787 "#pragma omp declare target", pragma_tok);
35788 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35789 {
35790 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35791 clauses);
35792 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35793 cp_parser_require_pragma_eol (parser, pragma_tok);
35794 }
35795 else
35796 {
35797 cp_parser_require_pragma_eol (parser, pragma_tok);
35798 scope_chain->omp_declare_target_attribute++;
35799 return;
35800 }
35801 if (scope_chain->omp_declare_target_attribute)
35802 error_at (pragma_tok->location,
35803 "%<#pragma omp declare target%> with clauses in between "
35804 "%<#pragma omp declare target%> without clauses and "
35805 "%<#pragma omp end declare target%>");
35806 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35807 {
35808 tree t = OMP_CLAUSE_DECL (c), id;
35809 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35810 tree at2 = lookup_attribute ("omp declare target link",
35811 DECL_ATTRIBUTES (t));
35812 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35813 {
35814 id = get_identifier ("omp declare target link");
35815 std::swap (at1, at2);
35816 }
35817 else
35818 id = get_identifier ("omp declare target");
35819 if (at2)
35820 {
35821 error_at (OMP_CLAUSE_LOCATION (c),
35822 "%qD specified both in declare target %<link%> and %<to%>"
35823 " clauses", t);
35824 continue;
35825 }
35826 if (!at1)
35827 {
35828 symtab_node *node = symtab_node::get (t);
35829 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35830 if (node != NULL)
35831 {
35832 node->offloadable = 1;
35833 if (ENABLE_OFFLOADING)
35834 {
35835 g->have_offload = true;
35836 if (is_a <varpool_node *> (node))
35837 vec_safe_push (offload_vars, t);
35838 }
35839 }
35840 }
35841 }
35842 }
35843
35844 static void
35845 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35846 {
35847 const char *p = "";
35848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35849 {
35850 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35851 p = IDENTIFIER_POINTER (id);
35852 }
35853 if (strcmp (p, "declare") == 0)
35854 {
35855 cp_lexer_consume_token (parser->lexer);
35856 p = "";
35857 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35858 {
35859 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35860 p = IDENTIFIER_POINTER (id);
35861 }
35862 if (strcmp (p, "target") == 0)
35863 cp_lexer_consume_token (parser->lexer);
35864 else
35865 {
35866 cp_parser_error (parser, "expected %<target%>");
35867 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35868 return;
35869 }
35870 }
35871 else
35872 {
35873 cp_parser_error (parser, "expected %<declare%>");
35874 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35875 return;
35876 }
35877 cp_parser_require_pragma_eol (parser, pragma_tok);
35878 if (!scope_chain->omp_declare_target_attribute)
35879 error_at (pragma_tok->location,
35880 "%<#pragma omp end declare target%> without corresponding "
35881 "%<#pragma omp declare target%>");
35882 else
35883 scope_chain->omp_declare_target_attribute--;
35884 }
35885
35886 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35887 expression and optional initializer clause of
35888 #pragma omp declare reduction. We store the expression(s) as
35889 either 3, 6 or 7 special statements inside of the artificial function's
35890 body. The first two statements are DECL_EXPRs for the artificial
35891 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35892 expression that uses those variables.
35893 If there was any INITIALIZER clause, this is followed by further statements,
35894 the fourth and fifth statements are DECL_EXPRs for the artificial
35895 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35896 constructor variant (first token after open paren is not omp_priv),
35897 then the sixth statement is a statement with the function call expression
35898 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35899 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35900 to initialize the OMP_PRIV artificial variable and there is seventh
35901 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35902
35903 static bool
35904 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35905 {
35906 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35907 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35908 type = TREE_TYPE (type);
35909 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35910 DECL_ARTIFICIAL (omp_out) = 1;
35911 pushdecl (omp_out);
35912 add_decl_expr (omp_out);
35913 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35914 DECL_ARTIFICIAL (omp_in) = 1;
35915 pushdecl (omp_in);
35916 add_decl_expr (omp_in);
35917 tree combiner;
35918 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35919
35920 keep_next_level (true);
35921 tree block = begin_omp_structured_block ();
35922 combiner = cp_parser_expression (parser);
35923 finish_expr_stmt (combiner);
35924 block = finish_omp_structured_block (block);
35925 add_stmt (block);
35926
35927 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35928 return false;
35929
35930 const char *p = "";
35931 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35932 {
35933 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35934 p = IDENTIFIER_POINTER (id);
35935 }
35936
35937 if (strcmp (p, "initializer") == 0)
35938 {
35939 cp_lexer_consume_token (parser->lexer);
35940 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35941 return false;
35942
35943 p = "";
35944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35945 {
35946 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35947 p = IDENTIFIER_POINTER (id);
35948 }
35949
35950 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35951 DECL_ARTIFICIAL (omp_priv) = 1;
35952 pushdecl (omp_priv);
35953 add_decl_expr (omp_priv);
35954 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35955 DECL_ARTIFICIAL (omp_orig) = 1;
35956 pushdecl (omp_orig);
35957 add_decl_expr (omp_orig);
35958
35959 keep_next_level (true);
35960 block = begin_omp_structured_block ();
35961
35962 bool ctor = false;
35963 if (strcmp (p, "omp_priv") == 0)
35964 {
35965 bool is_direct_init, is_non_constant_init;
35966 ctor = true;
35967 cp_lexer_consume_token (parser->lexer);
35968 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35969 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
35970 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35971 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
35972 == CPP_CLOSE_PAREN
35973 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
35974 == CPP_CLOSE_PAREN))
35975 {
35976 finish_omp_structured_block (block);
35977 error ("invalid initializer clause");
35978 return false;
35979 }
35980 initializer = cp_parser_initializer (parser, &is_direct_init,
35981 &is_non_constant_init);
35982 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
35983 NULL_TREE, LOOKUP_ONLYCONVERTING);
35984 }
35985 else
35986 {
35987 cp_parser_parse_tentatively (parser);
35988 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
35989 /*check_dependency_p=*/true,
35990 /*template_p=*/NULL,
35991 /*declarator_p=*/false,
35992 /*optional_p=*/false);
35993 vec<tree, va_gc> *args;
35994 if (fn_name == error_mark_node
35995 || cp_parser_error_occurred (parser)
35996 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35997 || ((args = cp_parser_parenthesized_expression_list
35998 (parser, non_attr, /*cast_p=*/false,
35999 /*allow_expansion_p=*/true,
36000 /*non_constant_p=*/NULL)),
36001 cp_parser_error_occurred (parser)))
36002 {
36003 finish_omp_structured_block (block);
36004 cp_parser_abort_tentative_parse (parser);
36005 cp_parser_error (parser, "expected id-expression (arguments)");
36006 return false;
36007 }
36008 unsigned int i;
36009 tree arg;
36010 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36011 if (arg == omp_priv
36012 || (TREE_CODE (arg) == ADDR_EXPR
36013 && TREE_OPERAND (arg, 0) == omp_priv))
36014 break;
36015 cp_parser_abort_tentative_parse (parser);
36016 if (arg == NULL_TREE)
36017 error ("one of the initializer call arguments should be %<omp_priv%>"
36018 " or %<&omp_priv%>");
36019 initializer = cp_parser_postfix_expression (parser, false, false, false,
36020 false, NULL);
36021 finish_expr_stmt (initializer);
36022 }
36023
36024 block = finish_omp_structured_block (block);
36025 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36026 add_stmt (block);
36027
36028 if (ctor)
36029 add_decl_expr (omp_orig);
36030
36031 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36032 return false;
36033 }
36034
36035 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36036 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36037
36038 return true;
36039 }
36040
36041 /* OpenMP 4.0
36042 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36043 initializer-clause[opt] new-line
36044
36045 initializer-clause:
36046 initializer (omp_priv initializer)
36047 initializer (function-name (argument-list)) */
36048
36049 static void
36050 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36051 enum pragma_context)
36052 {
36053 auto_vec<tree> types;
36054 enum tree_code reduc_code = ERROR_MARK;
36055 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36056 unsigned int i;
36057 cp_token *first_token;
36058 cp_token_cache *cp;
36059 int errs;
36060 void *p;
36061
36062 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36063 p = obstack_alloc (&declarator_obstack, 0);
36064
36065 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36066 goto fail;
36067
36068 switch (cp_lexer_peek_token (parser->lexer)->type)
36069 {
36070 case CPP_PLUS:
36071 reduc_code = PLUS_EXPR;
36072 break;
36073 case CPP_MULT:
36074 reduc_code = MULT_EXPR;
36075 break;
36076 case CPP_MINUS:
36077 reduc_code = MINUS_EXPR;
36078 break;
36079 case CPP_AND:
36080 reduc_code = BIT_AND_EXPR;
36081 break;
36082 case CPP_XOR:
36083 reduc_code = BIT_XOR_EXPR;
36084 break;
36085 case CPP_OR:
36086 reduc_code = BIT_IOR_EXPR;
36087 break;
36088 case CPP_AND_AND:
36089 reduc_code = TRUTH_ANDIF_EXPR;
36090 break;
36091 case CPP_OR_OR:
36092 reduc_code = TRUTH_ORIF_EXPR;
36093 break;
36094 case CPP_NAME:
36095 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36096 break;
36097 default:
36098 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36099 "%<|%>, %<&&%>, %<||%> or identifier");
36100 goto fail;
36101 }
36102
36103 if (reduc_code != ERROR_MARK)
36104 cp_lexer_consume_token (parser->lexer);
36105
36106 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36107 if (reduc_id == error_mark_node)
36108 goto fail;
36109
36110 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36111 goto fail;
36112
36113 /* Types may not be defined in declare reduction type list. */
36114 const char *saved_message;
36115 saved_message = parser->type_definition_forbidden_message;
36116 parser->type_definition_forbidden_message
36117 = G_("types may not be defined in declare reduction type list");
36118 bool saved_colon_corrects_to_scope_p;
36119 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36120 parser->colon_corrects_to_scope_p = false;
36121 bool saved_colon_doesnt_start_class_def_p;
36122 saved_colon_doesnt_start_class_def_p
36123 = parser->colon_doesnt_start_class_def_p;
36124 parser->colon_doesnt_start_class_def_p = true;
36125
36126 while (true)
36127 {
36128 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36129 type = cp_parser_type_id (parser);
36130 if (type == error_mark_node)
36131 ;
36132 else if (ARITHMETIC_TYPE_P (type)
36133 && (orig_reduc_id == NULL_TREE
36134 || (TREE_CODE (type) != COMPLEX_TYPE
36135 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36136 "min") == 0
36137 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36138 "max") == 0))))
36139 error_at (loc, "predeclared arithmetic type %qT in "
36140 "%<#pragma omp declare reduction%>", type);
36141 else if (TREE_CODE (type) == FUNCTION_TYPE
36142 || TREE_CODE (type) == METHOD_TYPE
36143 || TREE_CODE (type) == ARRAY_TYPE)
36144 error_at (loc, "function or array type %qT in "
36145 "%<#pragma omp declare reduction%>", type);
36146 else if (TREE_CODE (type) == REFERENCE_TYPE)
36147 error_at (loc, "reference type %qT in "
36148 "%<#pragma omp declare reduction%>", type);
36149 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36150 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36151 "%<#pragma omp declare reduction%>", type);
36152 else
36153 types.safe_push (type);
36154
36155 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36156 cp_lexer_consume_token (parser->lexer);
36157 else
36158 break;
36159 }
36160
36161 /* Restore the saved message. */
36162 parser->type_definition_forbidden_message = saved_message;
36163 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36164 parser->colon_doesnt_start_class_def_p
36165 = saved_colon_doesnt_start_class_def_p;
36166
36167 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36168 || types.is_empty ())
36169 {
36170 fail:
36171 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36172 goto done;
36173 }
36174
36175 first_token = cp_lexer_peek_token (parser->lexer);
36176 cp = NULL;
36177 errs = errorcount;
36178 FOR_EACH_VEC_ELT (types, i, type)
36179 {
36180 tree fntype
36181 = build_function_type_list (void_type_node,
36182 cp_build_reference_type (type, false),
36183 NULL_TREE);
36184 tree this_reduc_id = reduc_id;
36185 if (!dependent_type_p (type))
36186 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36187 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36188 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36189 DECL_ARTIFICIAL (fndecl) = 1;
36190 DECL_EXTERNAL (fndecl) = 1;
36191 DECL_DECLARED_INLINE_P (fndecl) = 1;
36192 DECL_IGNORED_P (fndecl) = 1;
36193 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36194 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36195 DECL_ATTRIBUTES (fndecl)
36196 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36197 DECL_ATTRIBUTES (fndecl));
36198 if (processing_template_decl)
36199 fndecl = push_template_decl (fndecl);
36200 bool block_scope = false;
36201 tree block = NULL_TREE;
36202 if (current_function_decl)
36203 {
36204 block_scope = true;
36205 DECL_CONTEXT (fndecl) = global_namespace;
36206 if (!processing_template_decl)
36207 pushdecl (fndecl);
36208 }
36209 else if (current_class_type)
36210 {
36211 if (cp == NULL)
36212 {
36213 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36214 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36215 cp_lexer_consume_token (parser->lexer);
36216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36217 goto fail;
36218 cp = cp_token_cache_new (first_token,
36219 cp_lexer_peek_nth_token (parser->lexer,
36220 2));
36221 }
36222 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36223 finish_member_declaration (fndecl);
36224 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36225 DECL_PENDING_INLINE_P (fndecl) = 1;
36226 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36227 continue;
36228 }
36229 else
36230 {
36231 DECL_CONTEXT (fndecl) = current_namespace;
36232 pushdecl (fndecl);
36233 }
36234 if (!block_scope)
36235 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36236 else
36237 block = begin_omp_structured_block ();
36238 if (cp)
36239 {
36240 cp_parser_push_lexer_for_tokens (parser, cp);
36241 parser->lexer->in_pragma = true;
36242 }
36243 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36244 {
36245 if (!block_scope)
36246 finish_function (0);
36247 else
36248 DECL_CONTEXT (fndecl) = current_function_decl;
36249 if (cp)
36250 cp_parser_pop_lexer (parser);
36251 goto fail;
36252 }
36253 if (cp)
36254 cp_parser_pop_lexer (parser);
36255 if (!block_scope)
36256 finish_function (0);
36257 else
36258 {
36259 DECL_CONTEXT (fndecl) = current_function_decl;
36260 block = finish_omp_structured_block (block);
36261 if (TREE_CODE (block) == BIND_EXPR)
36262 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36263 else if (TREE_CODE (block) == STATEMENT_LIST)
36264 DECL_SAVED_TREE (fndecl) = block;
36265 if (processing_template_decl)
36266 add_decl_expr (fndecl);
36267 }
36268 cp_check_omp_declare_reduction (fndecl);
36269 if (cp == NULL && types.length () > 1)
36270 cp = cp_token_cache_new (first_token,
36271 cp_lexer_peek_nth_token (parser->lexer, 2));
36272 if (errs != errorcount)
36273 break;
36274 }
36275
36276 cp_parser_require_pragma_eol (parser, pragma_tok);
36277
36278 done:
36279 /* Free any declarators allocated. */
36280 obstack_free (&declarator_obstack, p);
36281 }
36282
36283 /* OpenMP 4.0
36284 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36285 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36286 initializer-clause[opt] new-line
36287 #pragma omp declare target new-line */
36288
36289 static void
36290 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36291 enum pragma_context context)
36292 {
36293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36294 {
36295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36296 const char *p = IDENTIFIER_POINTER (id);
36297
36298 if (strcmp (p, "simd") == 0)
36299 {
36300 cp_lexer_consume_token (parser->lexer);
36301 cp_parser_omp_declare_simd (parser, pragma_tok,
36302 context);
36303 return;
36304 }
36305 cp_ensure_no_omp_declare_simd (parser);
36306 if (strcmp (p, "reduction") == 0)
36307 {
36308 cp_lexer_consume_token (parser->lexer);
36309 cp_parser_omp_declare_reduction (parser, pragma_tok,
36310 context);
36311 return;
36312 }
36313 if (!flag_openmp) /* flag_openmp_simd */
36314 {
36315 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36316 return;
36317 }
36318 if (strcmp (p, "target") == 0)
36319 {
36320 cp_lexer_consume_token (parser->lexer);
36321 cp_parser_omp_declare_target (parser, pragma_tok);
36322 return;
36323 }
36324 }
36325 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36326 "or %<target%>");
36327 cp_parser_require_pragma_eol (parser, pragma_tok);
36328 }
36329
36330 /* OpenMP 4.5:
36331 #pragma omp taskloop taskloop-clause[optseq] new-line
36332 for-loop
36333
36334 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36335 for-loop */
36336
36337 #define OMP_TASKLOOP_CLAUSE_MASK \
36338 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36352
36353 static tree
36354 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36355 char *p_name, omp_clause_mask mask, tree *cclauses,
36356 bool *if_p)
36357 {
36358 tree clauses, sb, ret;
36359 unsigned int save;
36360 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36361
36362 strcat (p_name, " taskloop");
36363 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36364
36365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36366 {
36367 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36368 const char *p = IDENTIFIER_POINTER (id);
36369
36370 if (strcmp (p, "simd") == 0)
36371 {
36372 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36373 if (cclauses == NULL)
36374 cclauses = cclauses_buf;
36375
36376 cp_lexer_consume_token (parser->lexer);
36377 if (!flag_openmp) /* flag_openmp_simd */
36378 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36379 cclauses, if_p);
36380 sb = begin_omp_structured_block ();
36381 save = cp_parser_begin_omp_structured_block (parser);
36382 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36383 cclauses, if_p);
36384 cp_parser_end_omp_structured_block (parser, save);
36385 tree body = finish_omp_structured_block (sb);
36386 if (ret == NULL)
36387 return ret;
36388 ret = make_node (OMP_TASKLOOP);
36389 TREE_TYPE (ret) = void_type_node;
36390 OMP_FOR_BODY (ret) = body;
36391 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36392 SET_EXPR_LOCATION (ret, loc);
36393 add_stmt (ret);
36394 return ret;
36395 }
36396 }
36397 if (!flag_openmp) /* flag_openmp_simd */
36398 {
36399 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36400 return NULL_TREE;
36401 }
36402
36403 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36404 cclauses == NULL);
36405 if (cclauses)
36406 {
36407 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36408 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36409 }
36410
36411 sb = begin_omp_structured_block ();
36412 save = cp_parser_begin_omp_structured_block (parser);
36413
36414 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36415 if_p);
36416
36417 cp_parser_end_omp_structured_block (parser, save);
36418 add_stmt (finish_omp_structured_block (sb));
36419
36420 return ret;
36421 }
36422
36423
36424 /* OpenACC 2.0:
36425 # pragma acc routine oacc-routine-clause[optseq] new-line
36426 function-definition
36427
36428 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36429 */
36430
36431 #define OACC_ROUTINE_CLAUSE_MASK \
36432 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36436
36437
36438 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36439 component, which must resolve to a declared namespace-scope
36440 function. The clauses are either processed directly (for a named
36441 function), or defered until the immediatley following declaration
36442 is parsed. */
36443
36444 static void
36445 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36446 enum pragma_context context)
36447 {
36448 bool first_p = parser->oacc_routine == NULL;
36449 location_t loc = pragma_tok->location;
36450 cp_omp_declare_simd_data data;
36451 if (first_p)
36452 {
36453 data.error_seen = false;
36454 data.fndecl_seen = false;
36455 data.tokens = vNULL;
36456 data.clauses = NULL_TREE;
36457 parser->oacc_routine = &data;
36458 }
36459
36460 tree decl = NULL_TREE;
36461 /* Create a dummy claue, to record location. */
36462 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36463
36464 if (context != pragma_external)
36465 {
36466 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36467 parser->oacc_routine->error_seen = true;
36468 parser->oacc_routine = NULL;
36469 return;
36470 }
36471
36472 /* Look for optional '( name )'. */
36473 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36474 {
36475 if (!first_p)
36476 {
36477 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36478 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36479 cp_lexer_consume_token (parser->lexer);
36480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36481 parser->oacc_routine->error_seen = true;
36482 cp_parser_require_pragma_eol (parser, pragma_tok);
36483
36484 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36485 "%<#pragma acc routine%> not followed by a "
36486 "function declaration or definition");
36487
36488 parser->oacc_routine->error_seen = true;
36489 return;
36490 }
36491
36492 cp_lexer_consume_token (parser->lexer);
36493 cp_token *token = cp_lexer_peek_token (parser->lexer);
36494
36495 /* We parse the name as an id-expression. If it resolves to
36496 anything other than a non-overloaded function at namespace
36497 scope, it's an error. */
36498 tree id = cp_parser_id_expression (parser,
36499 /*template_keyword_p=*/false,
36500 /*check_dependency_p=*/false,
36501 /*template_p=*/NULL,
36502 /*declarator_p=*/false,
36503 /*optional_p=*/false);
36504 decl = cp_parser_lookup_name_simple (parser, id, token->location);
36505 if (id != error_mark_node && decl == error_mark_node)
36506 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36507 token->location);
36508
36509 if (decl == error_mark_node
36510 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36511 {
36512 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36513 parser->oacc_routine = NULL;
36514 return;
36515 }
36516
36517 /* Build a chain of clauses. */
36518 parser->lexer->in_pragma = true;
36519 tree clauses = NULL_TREE;
36520 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36521 "#pragma acc routine",
36522 cp_lexer_peek_token
36523 (parser->lexer));
36524
36525 /* Force clauses to be non-null, by attaching context to it. */
36526 clauses = tree_cons (c_head, clauses, NULL_TREE);
36527
36528 if (decl && is_overloaded_fn (decl)
36529 && (TREE_CODE (decl) != FUNCTION_DECL
36530 || DECL_FUNCTION_TEMPLATE_P (decl)))
36531 {
36532 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36533 parser->oacc_routine = NULL;
36534 return;
36535 }
36536
36537 /* Perhaps we should use the same rule as declarations in different
36538 namespaces? */
36539 if (!DECL_NAMESPACE_SCOPE_P (decl))
36540 {
36541 error_at (loc, "%<#pragma acc routine%> does not refer to a "
36542 "namespace scope function");
36543 parser->oacc_routine = NULL;
36544 return;
36545 }
36546
36547 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36548 {
36549 error_at (loc,
36550 "%<#pragma acc routine%> does not refer to a function");
36551 parser->oacc_routine = NULL;
36552 return;
36553 }
36554
36555 data.clauses = clauses;
36556
36557 cp_finalize_oacc_routine (parser, decl, false);
36558 data.tokens.release ();
36559 parser->oacc_routine = NULL;
36560 }
36561 else
36562 {
36563 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36564 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36565 cp_lexer_consume_token (parser->lexer);
36566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36567 parser->oacc_routine->error_seen = true;
36568 cp_parser_require_pragma_eol (parser, pragma_tok);
36569
36570 struct cp_token_cache *cp
36571 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36572 parser->oacc_routine->tokens.safe_push (cp);
36573
36574 if (first_p)
36575 parser->oacc_routine->clauses = c_head;
36576
36577 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36578 cp_parser_pragma (parser, context, NULL);
36579
36580 if (first_p)
36581 {
36582 /* Create an empty list of clauses. */
36583 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36584 NULL_TREE);
36585 cp_parser_declaration (parser);
36586
36587 if (parser->oacc_routine
36588 && !parser->oacc_routine->error_seen
36589 && !parser->oacc_routine->fndecl_seen)
36590 error_at (loc, "%<#pragma acc routine%> not followed by a "
36591 "function declaration or definition");
36592
36593 data.tokens.release ();
36594 parser->oacc_routine = NULL;
36595 }
36596 }
36597 }
36598
36599 /* Finalize #pragma acc routine clauses after direct declarator has
36600 been parsed, and put that into "oacc function" attribute. */
36601
36602 static tree
36603 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36604 {
36605 struct cp_token_cache *ce;
36606 cp_omp_declare_simd_data *data = parser->oacc_routine;
36607 tree cl, clauses = parser->oacc_routine->clauses;
36608 location_t loc;
36609
36610 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36611
36612 if ((!data->error_seen && data->fndecl_seen)
36613 || data->tokens.length () != 1)
36614 {
36615 error_at (loc, "%<#pragma acc routine%> not followed by a "
36616 "function declaration or definition");
36617 data->error_seen = true;
36618 return attrs;
36619 }
36620 if (data->error_seen)
36621 return attrs;
36622
36623 ce = data->tokens[0];
36624
36625 cp_parser_push_lexer_for_tokens (parser, ce);
36626 parser->lexer->in_pragma = true;
36627 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36628
36629 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36630 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36631 "#pragma acc routine", pragma_tok);
36632 cp_parser_pop_lexer (parser);
36633
36634 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36635
36636 /* Force clauses to be non-null, by attaching context to it. */
36637 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36638
36639 data->fndecl_seen = true;
36640 return attrs;
36641 }
36642
36643 /* Apply any saved OpenACC routine clauses to a just-parsed
36644 declaration. */
36645
36646 static void
36647 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36648 {
36649 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36650 {
36651 tree clauses = parser->oacc_routine->clauses;
36652 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36653
36654 if (parser->oacc_routine->error_seen)
36655 return;
36656
36657 if (fndecl == error_mark_node)
36658 {
36659 parser->oacc_routine = NULL;
36660 return;
36661 }
36662
36663 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36664 {
36665 cp_ensure_no_oacc_routine (parser);
36666 return;
36667 }
36668
36669 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36670 {
36671 error_at (loc,
36672 "%<#pragma acc routine%> not followed by a function "
36673 "declaration or definition");
36674 parser->oacc_routine = NULL;
36675 }
36676
36677 if (get_oacc_fn_attrib (fndecl))
36678 {
36679 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36680 fndecl);
36681 parser->oacc_routine = NULL;
36682 }
36683
36684 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36685 {
36686 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36687 TREE_USED (fndecl) ? "use" : "definition");
36688 parser->oacc_routine = NULL;
36689 }
36690
36691 /* Process for function attrib */
36692 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36693 replace_oacc_fn_attrib (fndecl, dims);
36694
36695 /* Add an "omp target" attribute. */
36696 DECL_ATTRIBUTES (fndecl)
36697 = tree_cons (get_identifier ("omp declare target"),
36698 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36699 }
36700 }
36701
36702 /* Main entry point to OpenMP statement pragmas. */
36703
36704 static void
36705 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36706 {
36707 tree stmt;
36708 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36709 omp_clause_mask mask (0);
36710
36711 switch (cp_parser_pragma_kind (pragma_tok))
36712 {
36713 case PRAGMA_OACC_ATOMIC:
36714 cp_parser_omp_atomic (parser, pragma_tok);
36715 return;
36716 case PRAGMA_OACC_CACHE:
36717 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36718 break;
36719 case PRAGMA_OACC_DATA:
36720 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36721 break;
36722 case PRAGMA_OACC_ENTER_DATA:
36723 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36724 break;
36725 case PRAGMA_OACC_EXIT_DATA:
36726 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36727 break;
36728 case PRAGMA_OACC_HOST_DATA:
36729 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36730 break;
36731 case PRAGMA_OACC_KERNELS:
36732 case PRAGMA_OACC_PARALLEL:
36733 strcpy (p_name, "#pragma acc");
36734 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36735 if_p);
36736 break;
36737 case PRAGMA_OACC_LOOP:
36738 strcpy (p_name, "#pragma acc");
36739 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36740 if_p);
36741 break;
36742 case PRAGMA_OACC_UPDATE:
36743 stmt = cp_parser_oacc_update (parser, pragma_tok);
36744 break;
36745 case PRAGMA_OACC_WAIT:
36746 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36747 break;
36748 case PRAGMA_OMP_ATOMIC:
36749 cp_parser_omp_atomic (parser, pragma_tok);
36750 return;
36751 case PRAGMA_OMP_CRITICAL:
36752 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36753 break;
36754 case PRAGMA_OMP_DISTRIBUTE:
36755 strcpy (p_name, "#pragma omp");
36756 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36757 if_p);
36758 break;
36759 case PRAGMA_OMP_FOR:
36760 strcpy (p_name, "#pragma omp");
36761 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36762 if_p);
36763 break;
36764 case PRAGMA_OMP_MASTER:
36765 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36766 break;
36767 case PRAGMA_OMP_PARALLEL:
36768 strcpy (p_name, "#pragma omp");
36769 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36770 if_p);
36771 break;
36772 case PRAGMA_OMP_SECTIONS:
36773 strcpy (p_name, "#pragma omp");
36774 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36775 break;
36776 case PRAGMA_OMP_SIMD:
36777 strcpy (p_name, "#pragma omp");
36778 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36779 if_p);
36780 break;
36781 case PRAGMA_OMP_SINGLE:
36782 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36783 break;
36784 case PRAGMA_OMP_TASK:
36785 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36786 break;
36787 case PRAGMA_OMP_TASKGROUP:
36788 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36789 break;
36790 case PRAGMA_OMP_TASKLOOP:
36791 strcpy (p_name, "#pragma omp");
36792 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36793 if_p);
36794 break;
36795 case PRAGMA_OMP_TEAMS:
36796 strcpy (p_name, "#pragma omp");
36797 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36798 if_p);
36799 break;
36800 default:
36801 gcc_unreachable ();
36802 }
36803
36804 protected_set_expr_location (stmt, pragma_tok->location);
36805 }
36806 \f
36807 /* Transactional Memory parsing routines. */
36808
36809 /* Parse a transaction attribute.
36810
36811 txn-attribute:
36812 attribute
36813 [ [ identifier ] ]
36814
36815 We use this instead of cp_parser_attributes_opt for transactions to avoid
36816 the pedwarn in C++98 mode. */
36817
36818 static tree
36819 cp_parser_txn_attribute_opt (cp_parser *parser)
36820 {
36821 cp_token *token;
36822 tree attr_name, attr = NULL;
36823
36824 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36825 return cp_parser_attributes_opt (parser);
36826
36827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36828 return NULL_TREE;
36829 cp_lexer_consume_token (parser->lexer);
36830 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36831 goto error1;
36832
36833 token = cp_lexer_peek_token (parser->lexer);
36834 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36835 {
36836 token = cp_lexer_consume_token (parser->lexer);
36837
36838 attr_name = (token->type == CPP_KEYWORD
36839 /* For keywords, use the canonical spelling,
36840 not the parsed identifier. */
36841 ? ridpointers[(int) token->keyword]
36842 : token->u.value);
36843 attr = build_tree_list (attr_name, NULL_TREE);
36844 }
36845 else
36846 cp_parser_error (parser, "expected identifier");
36847
36848 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36849 error1:
36850 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36851 return attr;
36852 }
36853
36854 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36855
36856 transaction-statement:
36857 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36858 compound-statement
36859 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36860 */
36861
36862 static tree
36863 cp_parser_transaction (cp_parser *parser, cp_token *token)
36864 {
36865 unsigned char old_in = parser->in_transaction;
36866 unsigned char this_in = 1, new_in;
36867 enum rid keyword = token->keyword;
36868 tree stmt, attrs, noex;
36869
36870 cp_lexer_consume_token (parser->lexer);
36871
36872 if (keyword == RID_TRANSACTION_RELAXED
36873 || keyword == RID_SYNCHRONIZED)
36874 this_in |= TM_STMT_ATTR_RELAXED;
36875 else
36876 {
36877 attrs = cp_parser_txn_attribute_opt (parser);
36878 if (attrs)
36879 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36880 }
36881
36882 /* Parse a noexcept specification. */
36883 if (keyword == RID_ATOMIC_NOEXCEPT)
36884 noex = boolean_true_node;
36885 else if (keyword == RID_ATOMIC_CANCEL)
36886 {
36887 /* cancel-and-throw is unimplemented. */
36888 sorry ("atomic_cancel");
36889 noex = NULL_TREE;
36890 }
36891 else
36892 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36893
36894 /* Keep track if we're in the lexical scope of an outer transaction. */
36895 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36896
36897 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36898
36899 parser->in_transaction = new_in;
36900 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36901 parser->in_transaction = old_in;
36902
36903 finish_transaction_stmt (stmt, NULL, this_in, noex);
36904
36905 return stmt;
36906 }
36907
36908 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36909
36910 transaction-expression:
36911 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36912 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36913 */
36914
36915 static tree
36916 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36917 {
36918 unsigned char old_in = parser->in_transaction;
36919 unsigned char this_in = 1;
36920 cp_token *token;
36921 tree expr, noex;
36922 bool noex_expr;
36923 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36924
36925 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36926 || keyword == RID_TRANSACTION_RELAXED);
36927
36928 if (!flag_tm)
36929 error_at (loc,
36930 keyword == RID_TRANSACTION_RELAXED
36931 ? G_("%<__transaction_relaxed%> without transactional memory "
36932 "support enabled")
36933 : G_("%<__transaction_atomic%> without transactional memory "
36934 "support enabled"));
36935
36936 token = cp_parser_require_keyword (parser, keyword,
36937 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36938 : RT_TRANSACTION_RELAXED));
36939 gcc_assert (token != NULL);
36940
36941 if (keyword == RID_TRANSACTION_RELAXED)
36942 this_in |= TM_STMT_ATTR_RELAXED;
36943
36944 /* Set this early. This might mean that we allow transaction_cancel in
36945 an expression that we find out later actually has to be a constexpr.
36946 However, we expect that cxx_constant_value will be able to deal with
36947 this; also, if the noexcept has no constexpr, then what we parse next
36948 really is a transaction's body. */
36949 parser->in_transaction = this_in;
36950
36951 /* Parse a noexcept specification. */
36952 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36953 true);
36954
36955 if (!noex || !noex_expr
36956 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36957 {
36958 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36959
36960 expr = cp_parser_expression (parser);
36961 expr = finish_parenthesized_expr (expr);
36962
36963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
36964 }
36965 else
36966 {
36967 /* The only expression that is available got parsed for the noexcept
36968 already. noexcept is true then. */
36969 expr = noex;
36970 noex = boolean_true_node;
36971 }
36972
36973 expr = build_transaction_expr (token->location, expr, this_in, noex);
36974 parser->in_transaction = old_in;
36975
36976 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
36977 return error_mark_node;
36978
36979 return (flag_tm ? expr : error_mark_node);
36980 }
36981
36982 /* Parse a function-transaction-block.
36983
36984 function-transaction-block:
36985 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36986 function-body
36987 __transaction_atomic txn-attribute[opt] function-try-block
36988 __transaction_relaxed ctor-initializer[opt] function-body
36989 __transaction_relaxed function-try-block
36990 */
36991
36992 static bool
36993 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
36994 {
36995 unsigned char old_in = parser->in_transaction;
36996 unsigned char new_in = 1;
36997 tree compound_stmt, stmt, attrs;
36998 bool ctor_initializer_p;
36999 cp_token *token;
37000
37001 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37002 || keyword == RID_TRANSACTION_RELAXED);
37003 token = cp_parser_require_keyword (parser, keyword,
37004 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37005 : RT_TRANSACTION_RELAXED));
37006 gcc_assert (token != NULL);
37007
37008 if (keyword == RID_TRANSACTION_RELAXED)
37009 new_in |= TM_STMT_ATTR_RELAXED;
37010 else
37011 {
37012 attrs = cp_parser_txn_attribute_opt (parser);
37013 if (attrs)
37014 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37015 }
37016
37017 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37018
37019 parser->in_transaction = new_in;
37020
37021 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37022 ctor_initializer_p = cp_parser_function_try_block (parser);
37023 else
37024 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37025 (parser, /*in_function_try_block=*/false);
37026
37027 parser->in_transaction = old_in;
37028
37029 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37030
37031 return ctor_initializer_p;
37032 }
37033
37034 /* Parse a __transaction_cancel statement.
37035
37036 cancel-statement:
37037 __transaction_cancel txn-attribute[opt] ;
37038 __transaction_cancel txn-attribute[opt] throw-expression ;
37039
37040 ??? Cancel and throw is not yet implemented. */
37041
37042 static tree
37043 cp_parser_transaction_cancel (cp_parser *parser)
37044 {
37045 cp_token *token;
37046 bool is_outer = false;
37047 tree stmt, attrs;
37048
37049 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37050 RT_TRANSACTION_CANCEL);
37051 gcc_assert (token != NULL);
37052
37053 attrs = cp_parser_txn_attribute_opt (parser);
37054 if (attrs)
37055 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37056
37057 /* ??? Parse cancel-and-throw here. */
37058
37059 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37060
37061 if (!flag_tm)
37062 {
37063 error_at (token->location, "%<__transaction_cancel%> without "
37064 "transactional memory support enabled");
37065 return error_mark_node;
37066 }
37067 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37068 {
37069 error_at (token->location, "%<__transaction_cancel%> within a "
37070 "%<__transaction_relaxed%>");
37071 return error_mark_node;
37072 }
37073 else if (is_outer)
37074 {
37075 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37076 && !is_tm_may_cancel_outer (current_function_decl))
37077 {
37078 error_at (token->location, "outer %<__transaction_cancel%> not "
37079 "within outer %<__transaction_atomic%>");
37080 error_at (token->location,
37081 " or a %<transaction_may_cancel_outer%> function");
37082 return error_mark_node;
37083 }
37084 }
37085 else if (parser->in_transaction == 0)
37086 {
37087 error_at (token->location, "%<__transaction_cancel%> not within "
37088 "%<__transaction_atomic%>");
37089 return error_mark_node;
37090 }
37091
37092 stmt = build_tm_abort_call (token->location, is_outer);
37093 add_stmt (stmt);
37094
37095 return stmt;
37096 }
37097 \f
37098 /* The parser. */
37099
37100 static GTY (()) cp_parser *the_parser;
37101
37102 \f
37103 /* Special handling for the first token or line in the file. The first
37104 thing in the file might be #pragma GCC pch_preprocess, which loads a
37105 PCH file, which is a GC collection point. So we need to handle this
37106 first pragma without benefit of an existing lexer structure.
37107
37108 Always returns one token to the caller in *FIRST_TOKEN. This is
37109 either the true first token of the file, or the first token after
37110 the initial pragma. */
37111
37112 static void
37113 cp_parser_initial_pragma (cp_token *first_token)
37114 {
37115 tree name = NULL;
37116
37117 cp_lexer_get_preprocessor_token (NULL, first_token);
37118 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37119 return;
37120
37121 cp_lexer_get_preprocessor_token (NULL, first_token);
37122 if (first_token->type == CPP_STRING)
37123 {
37124 name = first_token->u.value;
37125
37126 cp_lexer_get_preprocessor_token (NULL, first_token);
37127 if (first_token->type != CPP_PRAGMA_EOL)
37128 error_at (first_token->location,
37129 "junk at end of %<#pragma GCC pch_preprocess%>");
37130 }
37131 else
37132 error_at (first_token->location, "expected string literal");
37133
37134 /* Skip to the end of the pragma. */
37135 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37136 cp_lexer_get_preprocessor_token (NULL, first_token);
37137
37138 /* Now actually load the PCH file. */
37139 if (name)
37140 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37141
37142 /* Read one more token to return to our caller. We have to do this
37143 after reading the PCH file in, since its pointers have to be
37144 live. */
37145 cp_lexer_get_preprocessor_token (NULL, first_token);
37146 }
37147
37148 /* Parses the grainsize pragma for the _Cilk_for statement.
37149 Syntax:
37150 #pragma cilk grainsize = <VALUE>. */
37151
37152 static void
37153 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37154 {
37155 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37156 {
37157 tree exp = cp_parser_binary_expression (parser, false, false,
37158 PREC_NOT_OPERATOR, NULL);
37159 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37160 if (!exp || exp == error_mark_node)
37161 {
37162 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37163 return;
37164 }
37165
37166 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37167 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37168 cp_parser_cilk_for (parser, exp, if_p);
37169 else
37170 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37171 "%<#pragma cilk grainsize%> is not followed by "
37172 "%<_Cilk_for%>");
37173 return;
37174 }
37175 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37176 }
37177
37178 /* Normal parsing of a pragma token. Here we can (and must) use the
37179 regular lexer. */
37180
37181 static bool
37182 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37183 {
37184 cp_token *pragma_tok;
37185 unsigned int id;
37186 tree stmt;
37187 bool ret;
37188
37189 pragma_tok = cp_lexer_consume_token (parser->lexer);
37190 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37191 parser->lexer->in_pragma = true;
37192
37193 id = cp_parser_pragma_kind (pragma_tok);
37194 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37195 cp_ensure_no_omp_declare_simd (parser);
37196 switch (id)
37197 {
37198 case PRAGMA_GCC_PCH_PREPROCESS:
37199 error_at (pragma_tok->location,
37200 "%<#pragma GCC pch_preprocess%> must be first");
37201 break;
37202
37203 case PRAGMA_OMP_BARRIER:
37204 switch (context)
37205 {
37206 case pragma_compound:
37207 cp_parser_omp_barrier (parser, pragma_tok);
37208 return false;
37209 case pragma_stmt:
37210 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37211 "used in compound statements");
37212 break;
37213 default:
37214 goto bad_stmt;
37215 }
37216 break;
37217
37218 case PRAGMA_OMP_FLUSH:
37219 switch (context)
37220 {
37221 case pragma_compound:
37222 cp_parser_omp_flush (parser, pragma_tok);
37223 return false;
37224 case pragma_stmt:
37225 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37226 "used in compound statements");
37227 break;
37228 default:
37229 goto bad_stmt;
37230 }
37231 break;
37232
37233 case PRAGMA_OMP_TASKWAIT:
37234 switch (context)
37235 {
37236 case pragma_compound:
37237 cp_parser_omp_taskwait (parser, pragma_tok);
37238 return false;
37239 case pragma_stmt:
37240 error_at (pragma_tok->location,
37241 "%<#pragma omp taskwait%> may only be "
37242 "used in compound statements");
37243 break;
37244 default:
37245 goto bad_stmt;
37246 }
37247 break;
37248
37249 case PRAGMA_OMP_TASKYIELD:
37250 switch (context)
37251 {
37252 case pragma_compound:
37253 cp_parser_omp_taskyield (parser, pragma_tok);
37254 return false;
37255 case pragma_stmt:
37256 error_at (pragma_tok->location,
37257 "%<#pragma omp taskyield%> may only be "
37258 "used in compound statements");
37259 break;
37260 default:
37261 goto bad_stmt;
37262 }
37263 break;
37264
37265 case PRAGMA_OMP_CANCEL:
37266 switch (context)
37267 {
37268 case pragma_compound:
37269 cp_parser_omp_cancel (parser, pragma_tok);
37270 return false;
37271 case pragma_stmt:
37272 error_at (pragma_tok->location,
37273 "%<#pragma omp cancel%> may only be "
37274 "used in compound statements");
37275 break;
37276 default:
37277 goto bad_stmt;
37278 }
37279 break;
37280
37281 case PRAGMA_OMP_CANCELLATION_POINT:
37282 switch (context)
37283 {
37284 case pragma_compound:
37285 cp_parser_omp_cancellation_point (parser, pragma_tok);
37286 return false;
37287 case pragma_stmt:
37288 error_at (pragma_tok->location,
37289 "%<#pragma omp cancellation point%> may only be "
37290 "used in compound statements");
37291 break;
37292 default:
37293 goto bad_stmt;
37294 }
37295 break;
37296
37297 case PRAGMA_OMP_THREADPRIVATE:
37298 cp_parser_omp_threadprivate (parser, pragma_tok);
37299 return false;
37300
37301 case PRAGMA_OMP_DECLARE_REDUCTION:
37302 cp_parser_omp_declare (parser, pragma_tok, context);
37303 return false;
37304
37305 case PRAGMA_OACC_DECLARE:
37306 cp_parser_oacc_declare (parser, pragma_tok);
37307 return false;
37308
37309 case PRAGMA_OACC_ROUTINE:
37310 cp_parser_oacc_routine (parser, pragma_tok, context);
37311 return false;
37312
37313 case PRAGMA_OACC_ATOMIC:
37314 case PRAGMA_OACC_CACHE:
37315 case PRAGMA_OACC_DATA:
37316 case PRAGMA_OACC_ENTER_DATA:
37317 case PRAGMA_OACC_EXIT_DATA:
37318 case PRAGMA_OACC_HOST_DATA:
37319 case PRAGMA_OACC_KERNELS:
37320 case PRAGMA_OACC_PARALLEL:
37321 case PRAGMA_OACC_LOOP:
37322 case PRAGMA_OACC_UPDATE:
37323 case PRAGMA_OACC_WAIT:
37324 case PRAGMA_OMP_ATOMIC:
37325 case PRAGMA_OMP_CRITICAL:
37326 case PRAGMA_OMP_DISTRIBUTE:
37327 case PRAGMA_OMP_FOR:
37328 case PRAGMA_OMP_MASTER:
37329 case PRAGMA_OMP_PARALLEL:
37330 case PRAGMA_OMP_SECTIONS:
37331 case PRAGMA_OMP_SIMD:
37332 case PRAGMA_OMP_SINGLE:
37333 case PRAGMA_OMP_TASK:
37334 case PRAGMA_OMP_TASKGROUP:
37335 case PRAGMA_OMP_TASKLOOP:
37336 case PRAGMA_OMP_TEAMS:
37337 if (context != pragma_stmt && context != pragma_compound)
37338 goto bad_stmt;
37339 stmt = push_omp_privatization_clauses (false);
37340 cp_parser_omp_construct (parser, pragma_tok, if_p);
37341 pop_omp_privatization_clauses (stmt);
37342 return true;
37343
37344 case PRAGMA_OMP_ORDERED:
37345 stmt = push_omp_privatization_clauses (false);
37346 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37347 pop_omp_privatization_clauses (stmt);
37348 return ret;
37349
37350 case PRAGMA_OMP_TARGET:
37351 stmt = push_omp_privatization_clauses (false);
37352 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37353 pop_omp_privatization_clauses (stmt);
37354 return ret;
37355
37356 case PRAGMA_OMP_END_DECLARE_TARGET:
37357 cp_parser_omp_end_declare_target (parser, pragma_tok);
37358 return false;
37359
37360 case PRAGMA_OMP_SECTION:
37361 error_at (pragma_tok->location,
37362 "%<#pragma omp section%> may only be used in "
37363 "%<#pragma omp sections%> construct");
37364 break;
37365
37366 case PRAGMA_IVDEP:
37367 {
37368 if (context == pragma_external)
37369 {
37370 error_at (pragma_tok->location,
37371 "%<#pragma GCC ivdep%> must be inside a function");
37372 break;
37373 }
37374 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37375 cp_token *tok;
37376 tok = cp_lexer_peek_token (the_parser->lexer);
37377 if (tok->type != CPP_KEYWORD
37378 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37379 && tok->keyword != RID_DO))
37380 {
37381 cp_parser_error (parser, "for, while or do statement expected");
37382 return false;
37383 }
37384 cp_parser_iteration_statement (parser, if_p, true);
37385 return true;
37386 }
37387
37388 case PRAGMA_CILK_SIMD:
37389 if (context == pragma_external)
37390 {
37391 error_at (pragma_tok->location,
37392 "%<#pragma simd%> must be inside a function");
37393 break;
37394 }
37395 stmt = push_omp_privatization_clauses (false);
37396 cp_parser_cilk_simd (parser, pragma_tok, if_p);
37397 pop_omp_privatization_clauses (stmt);
37398 return true;
37399
37400 case PRAGMA_CILK_GRAINSIZE:
37401 if (context == pragma_external)
37402 {
37403 error_at (pragma_tok->location,
37404 "%<#pragma cilk grainsize%> must be inside a function");
37405 break;
37406 }
37407
37408 /* Ignore the pragma if Cilk Plus is not enabled. */
37409 if (flag_cilkplus)
37410 {
37411 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37412 return true;
37413 }
37414 else
37415 {
37416 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37417 "%<#pragma cilk grainsize%>");
37418 break;
37419 }
37420
37421 default:
37422 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37423 c_invoke_pragma_handler (id);
37424 break;
37425
37426 bad_stmt:
37427 cp_parser_error (parser, "expected declaration specifiers");
37428 break;
37429 }
37430
37431 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37432 return false;
37433 }
37434
37435 /* The interface the pragma parsers have to the lexer. */
37436
37437 enum cpp_ttype
37438 pragma_lex (tree *value, location_t *loc)
37439 {
37440 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37441 enum cpp_ttype ret = tok->type;
37442
37443 *value = tok->u.value;
37444 if (loc)
37445 *loc = tok->location;
37446
37447 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37448 ret = CPP_EOF;
37449 else if (ret == CPP_STRING)
37450 *value = cp_parser_string_literal (the_parser, false, false);
37451 else
37452 {
37453 if (ret == CPP_KEYWORD)
37454 ret = CPP_NAME;
37455 cp_lexer_consume_token (the_parser->lexer);
37456 }
37457
37458 return ret;
37459 }
37460
37461 \f
37462 /* External interface. */
37463
37464 /* Parse one entire translation unit. */
37465
37466 void
37467 c_parse_file (void)
37468 {
37469 static bool already_called = false;
37470
37471 if (already_called)
37472 fatal_error (input_location,
37473 "inter-module optimizations not implemented for C++");
37474 already_called = true;
37475
37476 the_parser = cp_parser_new ();
37477 push_deferring_access_checks (flag_access_control
37478 ? dk_no_deferred : dk_no_check);
37479 cp_parser_translation_unit (the_parser);
37480 the_parser = NULL;
37481 }
37482
37483 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37484 vectorlength clause:
37485 Syntax:
37486 vectorlength ( constant-expression ) */
37487
37488 static tree
37489 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37490 bool is_simd_fn)
37491 {
37492 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37493 tree expr;
37494 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37495 safelen clause. Thus, vectorlength is represented as OMP 4.0
37496 safelen. For SIMD-enabled function it is represented by OMP 4.0
37497 simdlen. */
37498 if (!is_simd_fn)
37499 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37500 loc);
37501 else
37502 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37503 loc);
37504
37505 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37506 return error_mark_node;
37507
37508 expr = cp_parser_constant_expression (parser);
37509 expr = maybe_constant_value (expr);
37510
37511 /* If expr == error_mark_node, then don't emit any errors nor
37512 create a clause. if any of the above functions returns
37513 error mark node then they would have emitted an error message. */
37514 if (expr == error_mark_node)
37515 ;
37516 else if (!TREE_TYPE (expr)
37517 || !TREE_CONSTANT (expr)
37518 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37519 error_at (loc, "vectorlength must be an integer constant");
37520 else if (TREE_CONSTANT (expr)
37521 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37522 error_at (loc, "vectorlength must be a power of 2");
37523 else
37524 {
37525 tree c;
37526 if (!is_simd_fn)
37527 {
37528 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37529 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37530 OMP_CLAUSE_CHAIN (c) = clauses;
37531 clauses = c;
37532 }
37533 else
37534 {
37535 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37536 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37537 OMP_CLAUSE_CHAIN (c) = clauses;
37538 clauses = c;
37539 }
37540 }
37541
37542 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37543 return error_mark_node;
37544 return clauses;
37545 }
37546
37547 /* Handles the Cilk Plus #pragma simd linear clause.
37548 Syntax:
37549 linear ( simd-linear-variable-list )
37550
37551 simd-linear-variable-list:
37552 simd-linear-variable
37553 simd-linear-variable-list , simd-linear-variable
37554
37555 simd-linear-variable:
37556 id-expression
37557 id-expression : simd-linear-step
37558
37559 simd-linear-step:
37560 conditional-expression */
37561
37562 static tree
37563 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37564 {
37565 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37566
37567 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37568 return clauses;
37569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37570 {
37571 cp_parser_error (parser, "expected identifier");
37572 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37573 return error_mark_node;
37574 }
37575
37576 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37577 parser->colon_corrects_to_scope_p = false;
37578 while (1)
37579 {
37580 cp_token *token = cp_lexer_peek_token (parser->lexer);
37581 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37582 {
37583 cp_parser_error (parser, "expected variable-name");
37584 clauses = error_mark_node;
37585 break;
37586 }
37587
37588 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37589 false, false);
37590 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37591 token->location);
37592 if (decl == error_mark_node)
37593 {
37594 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37595 token->location);
37596 clauses = error_mark_node;
37597 }
37598 else
37599 {
37600 tree e = NULL_TREE;
37601 tree step_size = integer_one_node;
37602
37603 /* If present, parse the linear step. Otherwise, assume the default
37604 value of 1. */
37605 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37606 {
37607 cp_lexer_consume_token (parser->lexer);
37608
37609 e = cp_parser_assignment_expression (parser);
37610 e = maybe_constant_value (e);
37611
37612 if (e == error_mark_node)
37613 {
37614 /* If an error has occurred, then the whole pragma is
37615 considered ill-formed. Thus, no reason to keep
37616 parsing. */
37617 clauses = error_mark_node;
37618 break;
37619 }
37620 else if (type_dependent_expression_p (e)
37621 || value_dependent_expression_p (e)
37622 || (TREE_TYPE (e)
37623 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37624 && (TREE_CONSTANT (e)
37625 || DECL_P (e))))
37626 step_size = e;
37627 else
37628 cp_parser_error (parser,
37629 "step size must be an integer constant "
37630 "expression or an integer variable");
37631 }
37632
37633 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37634 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37635 OMP_CLAUSE_DECL (l) = decl;
37636 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37637 OMP_CLAUSE_CHAIN (l) = clauses;
37638 clauses = l;
37639 }
37640 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37641 cp_lexer_consume_token (parser->lexer);
37642 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37643 break;
37644 else
37645 {
37646 error_at (cp_lexer_peek_token (parser->lexer)->location,
37647 "expected %<,%> or %<)%> after %qE", decl);
37648 clauses = error_mark_node;
37649 break;
37650 }
37651 }
37652 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37653 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37654 return clauses;
37655 }
37656
37657 /* Returns the name of the next clause. If the clause is not
37658 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37659 token is not consumed. Otherwise, the appropriate enum from the
37660 pragma_simd_clause is returned and the token is consumed. */
37661
37662 static pragma_omp_clause
37663 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37664 {
37665 pragma_omp_clause clause_type;
37666 cp_token *token = cp_lexer_peek_token (parser->lexer);
37667
37668 if (token->keyword == RID_PRIVATE)
37669 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37670 else if (!token->u.value || token->type != CPP_NAME)
37671 return PRAGMA_CILK_CLAUSE_NONE;
37672 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37673 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37674 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37675 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37676 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37677 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37678 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37679 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37680 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37681 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37682 else
37683 return PRAGMA_CILK_CLAUSE_NONE;
37684
37685 cp_lexer_consume_token (parser->lexer);
37686 return clause_type;
37687 }
37688
37689 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37690
37691 static tree
37692 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37693 {
37694 tree clauses = NULL_TREE;
37695
37696 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37697 && clauses != error_mark_node)
37698 {
37699 pragma_omp_clause c_kind;
37700 c_kind = cp_parser_cilk_simd_clause_name (parser);
37701 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37702 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37703 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37704 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37705 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37706 /* Use the OpenMP 4.0 equivalent function. */
37707 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37708 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37709 /* Use the OpenMP 4.0 equivalent function. */
37710 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37711 clauses);
37712 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37713 /* Use the OMP 4.0 equivalent function. */
37714 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37715 clauses);
37716 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37717 /* Use the OMP 4.0 equivalent function. */
37718 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37719 else
37720 {
37721 clauses = error_mark_node;
37722 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37723 break;
37724 }
37725 }
37726
37727 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37728
37729 if (clauses == error_mark_node)
37730 return error_mark_node;
37731 else
37732 return finish_omp_clauses (clauses, C_ORT_CILK);
37733 }
37734
37735 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37736
37737 static void
37738 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37739 {
37740 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37741
37742 if (clauses == error_mark_node)
37743 return;
37744
37745 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37746 {
37747 error_at (cp_lexer_peek_token (parser->lexer)->location,
37748 "for statement expected");
37749 return;
37750 }
37751
37752 tree sb = begin_omp_structured_block ();
37753 int save = cp_parser_begin_omp_structured_block (parser);
37754 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37755 if (ret)
37756 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37757 cp_parser_end_omp_structured_block (parser, save);
37758 add_stmt (finish_omp_structured_block (sb));
37759 }
37760
37761 /* Main entry-point for parsing Cilk Plus _Cilk_for
37762 loops. The return value is error_mark_node
37763 when errors happen and CILK_FOR tree on success. */
37764
37765 static tree
37766 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37767 {
37768 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37769 gcc_unreachable ();
37770
37771 tree sb = begin_omp_structured_block ();
37772 int save = cp_parser_begin_omp_structured_block (parser);
37773
37774 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37775 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37776 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37777 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
37778
37779 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37780 if (ret)
37781 cpp_validate_cilk_plus_loop (ret);
37782 else
37783 ret = error_mark_node;
37784
37785 cp_parser_end_omp_structured_block (parser, save);
37786 add_stmt (finish_omp_structured_block (sb));
37787 return ret;
37788 }
37789
37790 /* Create an identifier for a generic parameter type (a synthesized
37791 template parameter implied by `auto' or a concept identifier). */
37792
37793 static GTY(()) int generic_parm_count;
37794 static tree
37795 make_generic_type_name ()
37796 {
37797 char buf[32];
37798 sprintf (buf, "auto:%d", ++generic_parm_count);
37799 return get_identifier (buf);
37800 }
37801
37802 /* Predicate that behaves as is_auto_or_concept but matches the parent
37803 node of the generic type rather than the generic type itself. This
37804 allows for type transformation in add_implicit_template_parms. */
37805
37806 static inline bool
37807 tree_type_is_auto_or_concept (const_tree t)
37808 {
37809 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37810 }
37811
37812 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37813 (creating a new template parameter list if necessary). Returns the newly
37814 created template type parm. */
37815
37816 static tree
37817 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37818 {
37819 gcc_assert (current_binding_level->kind == sk_function_parms);
37820
37821 /* Before committing to modifying any scope, if we're in an
37822 implicit template scope, and we're trying to synthesize a
37823 constrained parameter, try to find a previous parameter with
37824 the same name. This is the same-type rule for abbreviated
37825 function templates. */
37826 if (parser->implicit_template_scope && constr)
37827 {
37828 tree t = parser->implicit_template_parms;
37829 while (t)
37830 {
37831 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37832 {
37833 tree d = TREE_VALUE (t);
37834 if (TREE_CODE (d) == PARM_DECL)
37835 /* Return the TEMPLATE_PARM_INDEX. */
37836 d = DECL_INITIAL (d);
37837 return d;
37838 }
37839 t = TREE_CHAIN (t);
37840 }
37841 }
37842
37843 /* We are either continuing a function template that already contains implicit
37844 template parameters, creating a new fully-implicit function template, or
37845 extending an existing explicit function template with implicit template
37846 parameters. */
37847
37848 cp_binding_level *const entry_scope = current_binding_level;
37849
37850 bool become_template = false;
37851 cp_binding_level *parent_scope = 0;
37852
37853 if (parser->implicit_template_scope)
37854 {
37855 gcc_assert (parser->implicit_template_parms);
37856
37857 current_binding_level = parser->implicit_template_scope;
37858 }
37859 else
37860 {
37861 /* Roll back to the existing template parameter scope (in the case of
37862 extending an explicit function template) or introduce a new template
37863 parameter scope ahead of the function parameter scope (or class scope
37864 in the case of out-of-line member definitions). The function scope is
37865 added back after template parameter synthesis below. */
37866
37867 cp_binding_level *scope = entry_scope;
37868
37869 while (scope->kind == sk_function_parms)
37870 {
37871 parent_scope = scope;
37872 scope = scope->level_chain;
37873 }
37874 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37875 {
37876 /* If not defining a class, then any class scope is a scope level in
37877 an out-of-line member definition. In this case simply wind back
37878 beyond the first such scope to inject the template parameter list.
37879 Otherwise wind back to the class being defined. The latter can
37880 occur in class member friend declarations such as:
37881
37882 class A {
37883 void foo (auto);
37884 };
37885 class B {
37886 friend void A::foo (auto);
37887 };
37888
37889 The template parameter list synthesized for the friend declaration
37890 must be injected in the scope of 'B'. This can also occur in
37891 erroneous cases such as:
37892
37893 struct A {
37894 struct B {
37895 void foo (auto);
37896 };
37897 void B::foo (auto) {}
37898 };
37899
37900 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37901 but, nevertheless, the template parameter list synthesized for the
37902 declarator should be injected into the scope of 'A' as if the
37903 ill-formed template was specified explicitly. */
37904
37905 while (scope->kind == sk_class && !scope->defining_class_p)
37906 {
37907 parent_scope = scope;
37908 scope = scope->level_chain;
37909 }
37910 }
37911
37912 current_binding_level = scope;
37913
37914 if (scope->kind != sk_template_parms
37915 || !function_being_declared_is_template_p (parser))
37916 {
37917 /* Introduce a new template parameter list for implicit template
37918 parameters. */
37919
37920 become_template = true;
37921
37922 parser->implicit_template_scope
37923 = begin_scope (sk_template_parms, NULL);
37924
37925 ++processing_template_decl;
37926
37927 parser->fully_implicit_function_template_p = true;
37928 ++parser->num_template_parameter_lists;
37929 }
37930 else
37931 {
37932 /* Synthesize implicit template parameters at the end of the explicit
37933 template parameter list. */
37934
37935 gcc_assert (current_template_parms);
37936
37937 parser->implicit_template_scope = scope;
37938
37939 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37940 parser->implicit_template_parms
37941 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
37942 }
37943 }
37944
37945 /* Synthesize a new template parameter and track the current template
37946 parameter chain with implicit_template_parms. */
37947
37948 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
37949 tree synth_id = make_generic_type_name ();
37950 tree synth_tmpl_parm;
37951 bool non_type = false;
37952
37953 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37954 synth_tmpl_parm
37955 = finish_template_type_parm (class_type_node, synth_id);
37956 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37957 synth_tmpl_parm
37958 = finish_constrained_template_template_parm (proto, synth_id);
37959 else
37960 {
37961 synth_tmpl_parm = copy_decl (proto);
37962 DECL_NAME (synth_tmpl_parm) = synth_id;
37963 non_type = true;
37964 }
37965
37966 // Attach the constraint to the parm before processing.
37967 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37968 TREE_TYPE (node) = constr;
37969 tree new_parm
37970 = process_template_parm (parser->implicit_template_parms,
37971 input_location,
37972 node,
37973 /*non_type=*/non_type,
37974 /*param_pack=*/false);
37975
37976 // Chain the new parameter to the list of implicit parameters.
37977 if (parser->implicit_template_parms)
37978 parser->implicit_template_parms
37979 = TREE_CHAIN (parser->implicit_template_parms);
37980 else
37981 parser->implicit_template_parms = new_parm;
37982
37983 tree new_decl = getdecls ();
37984 if (non_type)
37985 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37986 new_decl = DECL_INITIAL (new_decl);
37987
37988 /* If creating a fully implicit function template, start the new implicit
37989 template parameter list with this synthesized type, otherwise grow the
37990 current template parameter list. */
37991
37992 if (become_template)
37993 {
37994 parent_scope->level_chain = current_binding_level;
37995
37996 tree new_parms = make_tree_vec (1);
37997 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
37998 current_template_parms = tree_cons (size_int (processing_template_decl),
37999 new_parms, current_template_parms);
38000 }
38001 else
38002 {
38003 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38004 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38005 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38006 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38007 }
38008
38009 // If the new parameter was constrained, we need to add that to the
38010 // constraints in the template parameter list.
38011 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38012 {
38013 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38014 reqs = conjoin_constraints (reqs, req);
38015 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38016 }
38017
38018 current_binding_level = entry_scope;
38019
38020 return new_decl;
38021 }
38022
38023 /* Finish the declaration of a fully implicit function template. Such a
38024 template has no explicit template parameter list so has not been through the
38025 normal template head and tail processing. synthesize_implicit_template_parm
38026 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38027 provided if the declaration is a class member such that its template
38028 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38029 form is returned. Otherwise NULL_TREE is returned. */
38030
38031 static tree
38032 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38033 {
38034 gcc_assert (parser->fully_implicit_function_template_p);
38035
38036 if (member_decl_opt && member_decl_opt != error_mark_node
38037 && DECL_VIRTUAL_P (member_decl_opt))
38038 {
38039 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38040 "implicit templates may not be %<virtual%>");
38041 DECL_VIRTUAL_P (member_decl_opt) = false;
38042 }
38043
38044 if (member_decl_opt)
38045 member_decl_opt = finish_member_template_decl (member_decl_opt);
38046 end_template_decl ();
38047
38048 parser->fully_implicit_function_template_p = false;
38049 --parser->num_template_parameter_lists;
38050
38051 return member_decl_opt;
38052 }
38053
38054 #include "gt-cp-parser.h"