7030740c239a0f62b19c2801fdd476a8ed15ad85
[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_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. */
7190 dependent_p = type_dependent_expression_p (postfix_expression);
7191 /* The identifier following the `->' or `.' is not qualified. */
7192 parser->scope = NULL_TREE;
7193 parser->qualifying_scope = NULL_TREE;
7194 parser->object_scope = NULL_TREE;
7195 *idk = CP_ID_KIND_NONE;
7196
7197 /* Enter the scope corresponding to the type of the object
7198 given by the POSTFIX_EXPRESSION. */
7199 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7200 {
7201 scope = TREE_TYPE (postfix_expression);
7202 /* According to the standard, no expression should ever have
7203 reference type. Unfortunately, we do not currently match
7204 the standard in this respect in that our internal representation
7205 of an expression may have reference type even when the standard
7206 says it does not. Therefore, we have to manually obtain the
7207 underlying type here. */
7208 scope = non_reference (scope);
7209 /* The type of the POSTFIX_EXPRESSION must be complete. */
7210 /* Unlike the object expression in other contexts, *this is not
7211 required to be of complete type for purposes of class member
7212 access (5.2.5) outside the member function body. */
7213 if (postfix_expression != current_class_ref
7214 && !(processing_template_decl && scope == current_class_type))
7215 scope = complete_type_or_else (scope, postfix_expression);
7216 /* Let the name lookup machinery know that we are processing a
7217 class member access expression. */
7218 parser->context->object_type = scope;
7219 /* If something went wrong, we want to be able to discern that case,
7220 as opposed to the case where there was no SCOPE due to the type
7221 of expression being dependent. */
7222 if (!scope)
7223 scope = error_mark_node;
7224 /* If the SCOPE was erroneous, make the various semantic analysis
7225 functions exit quickly -- and without issuing additional error
7226 messages. */
7227 if (scope == error_mark_node)
7228 postfix_expression = error_mark_node;
7229 }
7230 else
7231 /* Tell cp_parser_lookup_name that there was an object, even though it's
7232 type-dependent. */
7233 parser->context->object_type = unknown_type_node;
7234
7235 /* Assume this expression is not a pseudo-destructor access. */
7236 pseudo_destructor_p = false;
7237
7238 /* If the SCOPE is a scalar type, then, if this is a valid program,
7239 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7240 is type dependent, it can be pseudo-destructor-name or something else.
7241 Try to parse it as pseudo-destructor-name first. */
7242 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7243 {
7244 tree s;
7245 tree type;
7246
7247 cp_parser_parse_tentatively (parser);
7248 /* Parse the pseudo-destructor-name. */
7249 s = NULL_TREE;
7250 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7251 &s, &type);
7252 if (dependent_p
7253 && (cp_parser_error_occurred (parser)
7254 || !SCALAR_TYPE_P (type)))
7255 cp_parser_abort_tentative_parse (parser);
7256 else if (cp_parser_parse_definitely (parser))
7257 {
7258 pseudo_destructor_p = true;
7259 postfix_expression
7260 = finish_pseudo_destructor_expr (postfix_expression,
7261 s, type, location);
7262 }
7263 }
7264
7265 if (!pseudo_destructor_p)
7266 {
7267 /* If the SCOPE is not a scalar type, we are looking at an
7268 ordinary class member access expression, rather than a
7269 pseudo-destructor-name. */
7270 bool template_p;
7271 cp_token *token = cp_lexer_peek_token (parser->lexer);
7272 /* Parse the id-expression. */
7273 name = (cp_parser_id_expression
7274 (parser,
7275 cp_parser_optional_template_keyword (parser),
7276 /*check_dependency_p=*/true,
7277 &template_p,
7278 /*declarator_p=*/false,
7279 /*optional_p=*/false));
7280 /* In general, build a SCOPE_REF if the member name is qualified.
7281 However, if the name was not dependent and has already been
7282 resolved; there is no need to build the SCOPE_REF. For example;
7283
7284 struct X { void f(); };
7285 template <typename T> void f(T* t) { t->X::f(); }
7286
7287 Even though "t" is dependent, "X::f" is not and has been resolved
7288 to a BASELINK; there is no need to include scope information. */
7289
7290 /* But we do need to remember that there was an explicit scope for
7291 virtual function calls. */
7292 if (parser->scope)
7293 *idk = CP_ID_KIND_QUALIFIED;
7294
7295 /* If the name is a template-id that names a type, we will get a
7296 TYPE_DECL here. That is invalid code. */
7297 if (TREE_CODE (name) == TYPE_DECL)
7298 {
7299 error_at (token->location, "invalid use of %qD", name);
7300 postfix_expression = error_mark_node;
7301 }
7302 else
7303 {
7304 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7305 {
7306 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7307 {
7308 error_at (token->location, "%<%D::%D%> is not a class member",
7309 parser->scope, name);
7310 postfix_expression = error_mark_node;
7311 }
7312 else
7313 name = build_qualified_name (/*type=*/NULL_TREE,
7314 parser->scope,
7315 name,
7316 template_p);
7317 parser->scope = NULL_TREE;
7318 parser->qualifying_scope = NULL_TREE;
7319 parser->object_scope = NULL_TREE;
7320 }
7321 if (parser->scope && name && BASELINK_P (name))
7322 adjust_result_of_qualified_name_lookup
7323 (name, parser->scope, scope);
7324 postfix_expression
7325 = finish_class_member_access_expr (postfix_expression, name,
7326 template_p,
7327 tf_warning_or_error);
7328 /* Build a location e.g.:
7329 ptr->access_expr
7330 ~~~^~~~~~~~~~~~~
7331 where the caret is at the deref token, ranging from
7332 the start of postfix_expression to the end of the access expr. */
7333 location_t end_loc
7334 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7335 location_t combined_loc
7336 = make_location (input_location, start_loc, end_loc);
7337 protected_set_expr_location (postfix_expression, combined_loc);
7338 }
7339 }
7340
7341 /* We no longer need to look up names in the scope of the object on
7342 the left-hand side of the `.' or `->' operator. */
7343 parser->context->object_type = NULL_TREE;
7344
7345 /* Outside of offsetof, these operators may not appear in
7346 constant-expressions. */
7347 if (!for_offsetof
7348 && (cp_parser_non_integral_constant_expression
7349 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7350 postfix_expression = error_mark_node;
7351
7352 return postfix_expression;
7353 }
7354
7355 /* Parse a parenthesized expression-list.
7356
7357 expression-list:
7358 assignment-expression
7359 expression-list, assignment-expression
7360
7361 attribute-list:
7362 expression-list
7363 identifier
7364 identifier, expression-list
7365
7366 CAST_P is true if this expression is the target of a cast.
7367
7368 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7369 argument pack.
7370
7371 Returns a vector of trees. Each element is a representation of an
7372 assignment-expression. NULL is returned if the ( and or ) are
7373 missing. An empty, but allocated, vector is returned on no
7374 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7375 if we are parsing an attribute list for an attribute that wants a
7376 plain identifier argument, normal_attr for an attribute that wants
7377 an expression, or non_attr if we aren't parsing an attribute list. If
7378 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7379 not all of the expressions in the list were constant.
7380 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7381 will be written to with the location of the closing parenthesis. If
7382 an error occurs, it may or may not be written to. */
7383
7384 static vec<tree, va_gc> *
7385 cp_parser_parenthesized_expression_list (cp_parser* parser,
7386 int is_attribute_list,
7387 bool cast_p,
7388 bool allow_expansion_p,
7389 bool *non_constant_p,
7390 location_t *close_paren_loc)
7391 {
7392 vec<tree, va_gc> *expression_list;
7393 bool fold_expr_p = is_attribute_list != non_attr;
7394 tree identifier = NULL_TREE;
7395 bool saved_greater_than_is_operator_p;
7396
7397 /* Assume all the expressions will be constant. */
7398 if (non_constant_p)
7399 *non_constant_p = false;
7400
7401 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7402 return NULL;
7403
7404 expression_list = make_tree_vector ();
7405
7406 /* Within a parenthesized expression, a `>' token is always
7407 the greater-than operator. */
7408 saved_greater_than_is_operator_p
7409 = parser->greater_than_is_operator_p;
7410 parser->greater_than_is_operator_p = true;
7411
7412 /* Consume expressions until there are no more. */
7413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7414 while (true)
7415 {
7416 tree expr;
7417
7418 /* At the beginning of attribute lists, check to see if the
7419 next token is an identifier. */
7420 if (is_attribute_list == id_attr
7421 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7422 {
7423 cp_token *token;
7424
7425 /* Consume the identifier. */
7426 token = cp_lexer_consume_token (parser->lexer);
7427 /* Save the identifier. */
7428 identifier = token->u.value;
7429 }
7430 else
7431 {
7432 bool expr_non_constant_p;
7433
7434 /* Parse the next assignment-expression. */
7435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7436 {
7437 /* A braced-init-list. */
7438 cp_lexer_set_source_position (parser->lexer);
7439 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7440 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7441 if (non_constant_p && expr_non_constant_p)
7442 *non_constant_p = true;
7443 }
7444 else if (non_constant_p)
7445 {
7446 expr = (cp_parser_constant_expression
7447 (parser, /*allow_non_constant_p=*/true,
7448 &expr_non_constant_p));
7449 if (expr_non_constant_p)
7450 *non_constant_p = true;
7451 }
7452 else
7453 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7454 cast_p);
7455
7456 if (fold_expr_p)
7457 expr = instantiate_non_dependent_expr (expr);
7458
7459 /* If we have an ellipsis, then this is an expression
7460 expansion. */
7461 if (allow_expansion_p
7462 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7463 {
7464 /* Consume the `...'. */
7465 cp_lexer_consume_token (parser->lexer);
7466
7467 /* Build the argument pack. */
7468 expr = make_pack_expansion (expr);
7469 }
7470
7471 /* Add it to the list. We add error_mark_node
7472 expressions to the list, so that we can still tell if
7473 the correct form for a parenthesized expression-list
7474 is found. That gives better errors. */
7475 vec_safe_push (expression_list, expr);
7476
7477 if (expr == error_mark_node)
7478 goto skip_comma;
7479 }
7480
7481 /* After the first item, attribute lists look the same as
7482 expression lists. */
7483 is_attribute_list = non_attr;
7484
7485 get_comma:;
7486 /* If the next token isn't a `,', then we are done. */
7487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7488 break;
7489
7490 /* Otherwise, consume the `,' and keep going. */
7491 cp_lexer_consume_token (parser->lexer);
7492 }
7493
7494 if (close_paren_loc)
7495 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7496
7497 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7498 {
7499 int ending;
7500
7501 skip_comma:;
7502 /* We try and resync to an unnested comma, as that will give the
7503 user better diagnostics. */
7504 ending = cp_parser_skip_to_closing_parenthesis (parser,
7505 /*recovering=*/true,
7506 /*or_comma=*/true,
7507 /*consume_paren=*/true);
7508 if (ending < 0)
7509 goto get_comma;
7510 if (!ending)
7511 {
7512 parser->greater_than_is_operator_p
7513 = saved_greater_than_is_operator_p;
7514 return NULL;
7515 }
7516 }
7517
7518 parser->greater_than_is_operator_p
7519 = saved_greater_than_is_operator_p;
7520
7521 if (identifier)
7522 vec_safe_insert (expression_list, 0, identifier);
7523
7524 return expression_list;
7525 }
7526
7527 /* Parse a pseudo-destructor-name.
7528
7529 pseudo-destructor-name:
7530 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7531 :: [opt] nested-name-specifier template template-id :: ~ type-name
7532 :: [opt] nested-name-specifier [opt] ~ type-name
7533
7534 If either of the first two productions is used, sets *SCOPE to the
7535 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7536 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7537 or ERROR_MARK_NODE if the parse fails. */
7538
7539 static void
7540 cp_parser_pseudo_destructor_name (cp_parser* parser,
7541 tree object,
7542 tree* scope,
7543 tree* type)
7544 {
7545 bool nested_name_specifier_p;
7546
7547 /* Handle ~auto. */
7548 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7549 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7550 && !type_dependent_expression_p (object))
7551 {
7552 if (cxx_dialect < cxx14)
7553 pedwarn (input_location, 0,
7554 "%<~auto%> only available with "
7555 "-std=c++14 or -std=gnu++14");
7556 cp_lexer_consume_token (parser->lexer);
7557 cp_lexer_consume_token (parser->lexer);
7558 *scope = NULL_TREE;
7559 *type = TREE_TYPE (object);
7560 return;
7561 }
7562
7563 /* Assume that things will not work out. */
7564 *type = error_mark_node;
7565
7566 /* Look for the optional `::' operator. */
7567 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7568 /* Look for the optional nested-name-specifier. */
7569 nested_name_specifier_p
7570 = (cp_parser_nested_name_specifier_opt (parser,
7571 /*typename_keyword_p=*/false,
7572 /*check_dependency_p=*/true,
7573 /*type_p=*/false,
7574 /*is_declaration=*/false)
7575 != NULL_TREE);
7576 /* Now, if we saw a nested-name-specifier, we might be doing the
7577 second production. */
7578 if (nested_name_specifier_p
7579 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7580 {
7581 /* Consume the `template' keyword. */
7582 cp_lexer_consume_token (parser->lexer);
7583 /* Parse the template-id. */
7584 cp_parser_template_id (parser,
7585 /*template_keyword_p=*/true,
7586 /*check_dependency_p=*/false,
7587 class_type,
7588 /*is_declaration=*/true);
7589 /* Look for the `::' token. */
7590 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7591 }
7592 /* If the next token is not a `~', then there might be some
7593 additional qualification. */
7594 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7595 {
7596 /* At this point, we're looking for "type-name :: ~". The type-name
7597 must not be a class-name, since this is a pseudo-destructor. So,
7598 it must be either an enum-name, or a typedef-name -- both of which
7599 are just identifiers. So, we peek ahead to check that the "::"
7600 and "~" tokens are present; if they are not, then we can avoid
7601 calling type_name. */
7602 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7603 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7604 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7605 {
7606 cp_parser_error (parser, "non-scalar type");
7607 return;
7608 }
7609
7610 /* Look for the type-name. */
7611 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7612 if (*scope == error_mark_node)
7613 return;
7614
7615 /* Look for the `::' token. */
7616 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7617 }
7618 else
7619 *scope = NULL_TREE;
7620
7621 /* Look for the `~'. */
7622 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7623
7624 /* Once we see the ~, this has to be a pseudo-destructor. */
7625 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7626 cp_parser_commit_to_topmost_tentative_parse (parser);
7627
7628 /* Look for the type-name again. We are not responsible for
7629 checking that it matches the first type-name. */
7630 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7631 }
7632
7633 /* Parse a unary-expression.
7634
7635 unary-expression:
7636 postfix-expression
7637 ++ cast-expression
7638 -- cast-expression
7639 unary-operator cast-expression
7640 sizeof unary-expression
7641 sizeof ( type-id )
7642 alignof ( type-id ) [C++0x]
7643 new-expression
7644 delete-expression
7645
7646 GNU Extensions:
7647
7648 unary-expression:
7649 __extension__ cast-expression
7650 __alignof__ unary-expression
7651 __alignof__ ( type-id )
7652 alignof unary-expression [C++0x]
7653 __real__ cast-expression
7654 __imag__ cast-expression
7655 && identifier
7656 sizeof ( type-id ) { initializer-list , [opt] }
7657 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7658 __alignof__ ( type-id ) { initializer-list , [opt] }
7659
7660 ADDRESS_P is true iff the unary-expression is appearing as the
7661 operand of the `&' operator. CAST_P is true if this expression is
7662 the target of a cast.
7663
7664 Returns a representation of the expression. */
7665
7666 static cp_expr
7667 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7668 bool address_p, bool cast_p, bool decltype_p)
7669 {
7670 cp_token *token;
7671 enum tree_code unary_operator;
7672
7673 /* Peek at the next token. */
7674 token = cp_lexer_peek_token (parser->lexer);
7675 /* Some keywords give away the kind of expression. */
7676 if (token->type == CPP_KEYWORD)
7677 {
7678 enum rid keyword = token->keyword;
7679
7680 switch (keyword)
7681 {
7682 case RID_ALIGNOF:
7683 case RID_SIZEOF:
7684 {
7685 tree operand, ret;
7686 enum tree_code op;
7687 location_t first_loc;
7688
7689 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7690 /* Consume the token. */
7691 cp_lexer_consume_token (parser->lexer);
7692 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7693 /* Parse the operand. */
7694 operand = cp_parser_sizeof_operand (parser, keyword);
7695
7696 if (TYPE_P (operand))
7697 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7698 else
7699 {
7700 /* ISO C++ defines alignof only with types, not with
7701 expressions. So pedwarn if alignof is used with a non-
7702 type expression. However, __alignof__ is ok. */
7703 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7704 pedwarn (token->location, OPT_Wpedantic,
7705 "ISO C++ does not allow %<alignof%> "
7706 "with a non-type");
7707
7708 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7709 }
7710 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7711 SIZEOF_EXPR with the original operand. */
7712 if (op == SIZEOF_EXPR && ret != error_mark_node)
7713 {
7714 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7715 {
7716 if (!processing_template_decl && TYPE_P (operand))
7717 {
7718 ret = build_min (SIZEOF_EXPR, size_type_node,
7719 build1 (NOP_EXPR, operand,
7720 error_mark_node));
7721 SIZEOF_EXPR_TYPE_P (ret) = 1;
7722 }
7723 else
7724 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7725 TREE_SIDE_EFFECTS (ret) = 0;
7726 TREE_READONLY (ret) = 1;
7727 }
7728 SET_EXPR_LOCATION (ret, first_loc);
7729 }
7730 return ret;
7731 }
7732
7733 case RID_NEW:
7734 return cp_parser_new_expression (parser);
7735
7736 case RID_DELETE:
7737 return cp_parser_delete_expression (parser);
7738
7739 case RID_EXTENSION:
7740 {
7741 /* The saved value of the PEDANTIC flag. */
7742 int saved_pedantic;
7743 tree expr;
7744
7745 /* Save away the PEDANTIC flag. */
7746 cp_parser_extension_opt (parser, &saved_pedantic);
7747 /* Parse the cast-expression. */
7748 expr = cp_parser_simple_cast_expression (parser);
7749 /* Restore the PEDANTIC flag. */
7750 pedantic = saved_pedantic;
7751
7752 return expr;
7753 }
7754
7755 case RID_REALPART:
7756 case RID_IMAGPART:
7757 {
7758 tree expression;
7759
7760 /* Consume the `__real__' or `__imag__' token. */
7761 cp_lexer_consume_token (parser->lexer);
7762 /* Parse the cast-expression. */
7763 expression = cp_parser_simple_cast_expression (parser);
7764 /* Create the complete representation. */
7765 return build_x_unary_op (token->location,
7766 (keyword == RID_REALPART
7767 ? REALPART_EXPR : IMAGPART_EXPR),
7768 expression,
7769 tf_warning_or_error);
7770 }
7771 break;
7772
7773 case RID_TRANSACTION_ATOMIC:
7774 case RID_TRANSACTION_RELAXED:
7775 return cp_parser_transaction_expression (parser, keyword);
7776
7777 case RID_NOEXCEPT:
7778 {
7779 tree expr;
7780 const char *saved_message;
7781 bool saved_integral_constant_expression_p;
7782 bool saved_non_integral_constant_expression_p;
7783 bool saved_greater_than_is_operator_p;
7784
7785 cp_lexer_consume_token (parser->lexer);
7786 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7787
7788 saved_message = parser->type_definition_forbidden_message;
7789 parser->type_definition_forbidden_message
7790 = G_("types may not be defined in %<noexcept%> expressions");
7791
7792 saved_integral_constant_expression_p
7793 = parser->integral_constant_expression_p;
7794 saved_non_integral_constant_expression_p
7795 = parser->non_integral_constant_expression_p;
7796 parser->integral_constant_expression_p = false;
7797
7798 saved_greater_than_is_operator_p
7799 = parser->greater_than_is_operator_p;
7800 parser->greater_than_is_operator_p = true;
7801
7802 ++cp_unevaluated_operand;
7803 ++c_inhibit_evaluation_warnings;
7804 ++cp_noexcept_operand;
7805 expr = cp_parser_expression (parser);
7806 --cp_noexcept_operand;
7807 --c_inhibit_evaluation_warnings;
7808 --cp_unevaluated_operand;
7809
7810 parser->greater_than_is_operator_p
7811 = saved_greater_than_is_operator_p;
7812
7813 parser->integral_constant_expression_p
7814 = saved_integral_constant_expression_p;
7815 parser->non_integral_constant_expression_p
7816 = saved_non_integral_constant_expression_p;
7817
7818 parser->type_definition_forbidden_message = saved_message;
7819
7820 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7821 return finish_noexcept_expr (expr, tf_warning_or_error);
7822 }
7823
7824 default:
7825 break;
7826 }
7827 }
7828
7829 /* Look for the `:: new' and `:: delete', which also signal the
7830 beginning of a new-expression, or delete-expression,
7831 respectively. If the next token is `::', then it might be one of
7832 these. */
7833 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7834 {
7835 enum rid keyword;
7836
7837 /* See if the token after the `::' is one of the keywords in
7838 which we're interested. */
7839 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7840 /* If it's `new', we have a new-expression. */
7841 if (keyword == RID_NEW)
7842 return cp_parser_new_expression (parser);
7843 /* Similarly, for `delete'. */
7844 else if (keyword == RID_DELETE)
7845 return cp_parser_delete_expression (parser);
7846 }
7847
7848 /* Look for a unary operator. */
7849 unary_operator = cp_parser_unary_operator (token);
7850 /* The `++' and `--' operators can be handled similarly, even though
7851 they are not technically unary-operators in the grammar. */
7852 if (unary_operator == ERROR_MARK)
7853 {
7854 if (token->type == CPP_PLUS_PLUS)
7855 unary_operator = PREINCREMENT_EXPR;
7856 else if (token->type == CPP_MINUS_MINUS)
7857 unary_operator = PREDECREMENT_EXPR;
7858 /* Handle the GNU address-of-label extension. */
7859 else if (cp_parser_allow_gnu_extensions_p (parser)
7860 && token->type == CPP_AND_AND)
7861 {
7862 tree identifier;
7863 tree expression;
7864 location_t start_loc = token->location;
7865
7866 /* Consume the '&&' token. */
7867 cp_lexer_consume_token (parser->lexer);
7868 /* Look for the identifier. */
7869 location_t finish_loc
7870 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7871 identifier = cp_parser_identifier (parser);
7872 /* Construct a location of the form:
7873 &&label
7874 ^~~~~~~
7875 with caret==start at the "&&", finish at the end of the label. */
7876 location_t combined_loc
7877 = make_location (start_loc, start_loc, finish_loc);
7878 /* Create an expression representing the address. */
7879 expression = finish_label_address_expr (identifier, combined_loc);
7880 if (cp_parser_non_integral_constant_expression (parser,
7881 NIC_ADDR_LABEL))
7882 expression = error_mark_node;
7883 return expression;
7884 }
7885 }
7886 if (unary_operator != ERROR_MARK)
7887 {
7888 cp_expr cast_expression;
7889 cp_expr expression = error_mark_node;
7890 non_integral_constant non_constant_p = NIC_NONE;
7891 location_t loc = token->location;
7892 tsubst_flags_t complain = complain_flags (decltype_p);
7893
7894 /* Consume the operator token. */
7895 token = cp_lexer_consume_token (parser->lexer);
7896 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7897
7898 /* Parse the cast-expression. */
7899 cast_expression
7900 = cp_parser_cast_expression (parser,
7901 unary_operator == ADDR_EXPR,
7902 /*cast_p=*/false,
7903 /*decltype*/false,
7904 pidk);
7905
7906 /* Make a location:
7907 OP_TOKEN CAST_EXPRESSION
7908 ^~~~~~~~~~~~~~~~~~~~~~~~~
7909 with start==caret at the operator token, and
7910 extending to the end of the cast_expression. */
7911 loc = make_location (loc, loc, cast_expression.get_finish ());
7912
7913 /* Now, build an appropriate representation. */
7914 switch (unary_operator)
7915 {
7916 case INDIRECT_REF:
7917 non_constant_p = NIC_STAR;
7918 expression = build_x_indirect_ref (loc, cast_expression,
7919 RO_UNARY_STAR,
7920 complain);
7921 /* TODO: build_x_indirect_ref does not always honor the
7922 location, so ensure it is set. */
7923 expression.set_location (loc);
7924 break;
7925
7926 case ADDR_EXPR:
7927 non_constant_p = NIC_ADDR;
7928 /* Fall through. */
7929 case BIT_NOT_EXPR:
7930 expression = build_x_unary_op (loc, unary_operator,
7931 cast_expression,
7932 complain);
7933 /* TODO: build_x_unary_op does not always honor the location,
7934 so ensure it is set. */
7935 expression.set_location (loc);
7936 break;
7937
7938 case PREINCREMENT_EXPR:
7939 case PREDECREMENT_EXPR:
7940 non_constant_p = unary_operator == PREINCREMENT_EXPR
7941 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7942 /* Fall through. */
7943 case NEGATE_EXPR:
7944 /* Immediately fold negation of a constant, unless the constant is 0
7945 (since -0 == 0) or it would overflow. */
7946 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7947 && CONSTANT_CLASS_P (cast_expression)
7948 && !integer_zerop (cast_expression)
7949 && !TREE_OVERFLOW (cast_expression))
7950 {
7951 tree folded = fold_build1 (unary_operator,
7952 TREE_TYPE (cast_expression),
7953 cast_expression);
7954 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7955 {
7956 expression = cp_expr (folded, loc);
7957 break;
7958 }
7959 }
7960 /* Fall through. */
7961 case UNARY_PLUS_EXPR:
7962 case TRUTH_NOT_EXPR:
7963 expression = finish_unary_op_expr (loc, unary_operator,
7964 cast_expression, complain);
7965 break;
7966
7967 default:
7968 gcc_unreachable ();
7969 }
7970
7971 if (non_constant_p != NIC_NONE
7972 && cp_parser_non_integral_constant_expression (parser,
7973 non_constant_p))
7974 expression = error_mark_node;
7975
7976 return expression;
7977 }
7978
7979 return cp_parser_postfix_expression (parser, address_p, cast_p,
7980 /*member_access_only_p=*/false,
7981 decltype_p,
7982 pidk);
7983 }
7984
7985 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7986 unary-operator, the corresponding tree code is returned. */
7987
7988 static enum tree_code
7989 cp_parser_unary_operator (cp_token* token)
7990 {
7991 switch (token->type)
7992 {
7993 case CPP_MULT:
7994 return INDIRECT_REF;
7995
7996 case CPP_AND:
7997 return ADDR_EXPR;
7998
7999 case CPP_PLUS:
8000 return UNARY_PLUS_EXPR;
8001
8002 case CPP_MINUS:
8003 return NEGATE_EXPR;
8004
8005 case CPP_NOT:
8006 return TRUTH_NOT_EXPR;
8007
8008 case CPP_COMPL:
8009 return BIT_NOT_EXPR;
8010
8011 default:
8012 return ERROR_MARK;
8013 }
8014 }
8015
8016 /* Parse a new-expression.
8017
8018 new-expression:
8019 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8020 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8021
8022 Returns a representation of the expression. */
8023
8024 static tree
8025 cp_parser_new_expression (cp_parser* parser)
8026 {
8027 bool global_scope_p;
8028 vec<tree, va_gc> *placement;
8029 tree type;
8030 vec<tree, va_gc> *initializer;
8031 tree nelts = NULL_TREE;
8032 tree ret;
8033
8034 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8035
8036 /* Look for the optional `::' operator. */
8037 global_scope_p
8038 = (cp_parser_global_scope_opt (parser,
8039 /*current_scope_valid_p=*/false)
8040 != NULL_TREE);
8041 /* Look for the `new' operator. */
8042 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8043 /* There's no easy way to tell a new-placement from the
8044 `( type-id )' construct. */
8045 cp_parser_parse_tentatively (parser);
8046 /* Look for a new-placement. */
8047 placement = cp_parser_new_placement (parser);
8048 /* If that didn't work out, there's no new-placement. */
8049 if (!cp_parser_parse_definitely (parser))
8050 {
8051 if (placement != NULL)
8052 release_tree_vector (placement);
8053 placement = NULL;
8054 }
8055
8056 /* If the next token is a `(', then we have a parenthesized
8057 type-id. */
8058 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8059 {
8060 cp_token *token;
8061 const char *saved_message = parser->type_definition_forbidden_message;
8062
8063 /* Consume the `('. */
8064 cp_lexer_consume_token (parser->lexer);
8065
8066 /* Parse the type-id. */
8067 parser->type_definition_forbidden_message
8068 = G_("types may not be defined in a new-expression");
8069 {
8070 type_id_in_expr_sentinel s (parser);
8071 type = cp_parser_type_id (parser);
8072 }
8073 parser->type_definition_forbidden_message = saved_message;
8074
8075 /* Look for the closing `)'. */
8076 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8077 token = cp_lexer_peek_token (parser->lexer);
8078 /* There should not be a direct-new-declarator in this production,
8079 but GCC used to allowed this, so we check and emit a sensible error
8080 message for this case. */
8081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8082 {
8083 error_at (token->location,
8084 "array bound forbidden after parenthesized type-id");
8085 inform (token->location,
8086 "try removing the parentheses around the type-id");
8087 cp_parser_direct_new_declarator (parser);
8088 }
8089 }
8090 /* Otherwise, there must be a new-type-id. */
8091 else
8092 type = cp_parser_new_type_id (parser, &nelts);
8093
8094 /* If the next token is a `(' or '{', then we have a new-initializer. */
8095 cp_token *token = cp_lexer_peek_token (parser->lexer);
8096 if (token->type == CPP_OPEN_PAREN
8097 || token->type == CPP_OPEN_BRACE)
8098 initializer = cp_parser_new_initializer (parser);
8099 else
8100 initializer = NULL;
8101
8102 /* A new-expression may not appear in an integral constant
8103 expression. */
8104 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8105 ret = error_mark_node;
8106 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8107 of a new-type-id or type-id of a new-expression, the new-expression shall
8108 contain a new-initializer of the form ( assignment-expression )".
8109 Additionally, consistently with the spirit of DR 1467, we want to accept
8110 'new auto { 2 }' too. */
8111 else if (type_uses_auto (type)
8112 && (vec_safe_length (initializer) != 1
8113 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8114 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8115 {
8116 error_at (token->location,
8117 "initialization of new-expression for type %<auto%> "
8118 "requires exactly one element");
8119 ret = error_mark_node;
8120 }
8121 else
8122 {
8123 /* Construct a location e.g.:
8124 ptr = new int[100]
8125 ^~~~~~~~~~~~
8126 with caret == start at the start of the "new" token, and the end
8127 at the end of the final token we consumed. */
8128 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8129 location_t end_loc = get_finish (end_tok->location);
8130 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8131
8132 /* Create a representation of the new-expression. */
8133 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8134 tf_warning_or_error);
8135 protected_set_expr_location (ret, combined_loc);
8136 }
8137
8138 if (placement != NULL)
8139 release_tree_vector (placement);
8140 if (initializer != NULL)
8141 release_tree_vector (initializer);
8142
8143 return ret;
8144 }
8145
8146 /* Parse a new-placement.
8147
8148 new-placement:
8149 ( expression-list )
8150
8151 Returns the same representation as for an expression-list. */
8152
8153 static vec<tree, va_gc> *
8154 cp_parser_new_placement (cp_parser* parser)
8155 {
8156 vec<tree, va_gc> *expression_list;
8157
8158 /* Parse the expression-list. */
8159 expression_list = (cp_parser_parenthesized_expression_list
8160 (parser, non_attr, /*cast_p=*/false,
8161 /*allow_expansion_p=*/true,
8162 /*non_constant_p=*/NULL));
8163
8164 if (expression_list && expression_list->is_empty ())
8165 error ("expected expression-list or type-id");
8166
8167 return expression_list;
8168 }
8169
8170 /* Parse a new-type-id.
8171
8172 new-type-id:
8173 type-specifier-seq new-declarator [opt]
8174
8175 Returns the TYPE allocated. If the new-type-id indicates an array
8176 type, *NELTS is set to the number of elements in the last array
8177 bound; the TYPE will not include the last array bound. */
8178
8179 static tree
8180 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8181 {
8182 cp_decl_specifier_seq type_specifier_seq;
8183 cp_declarator *new_declarator;
8184 cp_declarator *declarator;
8185 cp_declarator *outer_declarator;
8186 const char *saved_message;
8187
8188 /* The type-specifier sequence must not contain type definitions.
8189 (It cannot contain declarations of new types either, but if they
8190 are not definitions we will catch that because they are not
8191 complete.) */
8192 saved_message = parser->type_definition_forbidden_message;
8193 parser->type_definition_forbidden_message
8194 = G_("types may not be defined in a new-type-id");
8195 /* Parse the type-specifier-seq. */
8196 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8197 /*is_trailing_return=*/false,
8198 &type_specifier_seq);
8199 /* Restore the old message. */
8200 parser->type_definition_forbidden_message = saved_message;
8201
8202 if (type_specifier_seq.type == error_mark_node)
8203 return error_mark_node;
8204
8205 /* Parse the new-declarator. */
8206 new_declarator = cp_parser_new_declarator_opt (parser);
8207
8208 /* Determine the number of elements in the last array dimension, if
8209 any. */
8210 *nelts = NULL_TREE;
8211 /* Skip down to the last array dimension. */
8212 declarator = new_declarator;
8213 outer_declarator = NULL;
8214 while (declarator && (declarator->kind == cdk_pointer
8215 || declarator->kind == cdk_ptrmem))
8216 {
8217 outer_declarator = declarator;
8218 declarator = declarator->declarator;
8219 }
8220 while (declarator
8221 && declarator->kind == cdk_array
8222 && declarator->declarator
8223 && declarator->declarator->kind == cdk_array)
8224 {
8225 outer_declarator = declarator;
8226 declarator = declarator->declarator;
8227 }
8228
8229 if (declarator && declarator->kind == cdk_array)
8230 {
8231 *nelts = declarator->u.array.bounds;
8232 if (*nelts == error_mark_node)
8233 *nelts = integer_one_node;
8234
8235 if (outer_declarator)
8236 outer_declarator->declarator = declarator->declarator;
8237 else
8238 new_declarator = NULL;
8239 }
8240
8241 return groktypename (&type_specifier_seq, new_declarator, false);
8242 }
8243
8244 /* Parse an (optional) new-declarator.
8245
8246 new-declarator:
8247 ptr-operator new-declarator [opt]
8248 direct-new-declarator
8249
8250 Returns the declarator. */
8251
8252 static cp_declarator *
8253 cp_parser_new_declarator_opt (cp_parser* parser)
8254 {
8255 enum tree_code code;
8256 tree type, std_attributes = NULL_TREE;
8257 cp_cv_quals cv_quals;
8258
8259 /* We don't know if there's a ptr-operator next, or not. */
8260 cp_parser_parse_tentatively (parser);
8261 /* Look for a ptr-operator. */
8262 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8263 /* If that worked, look for more new-declarators. */
8264 if (cp_parser_parse_definitely (parser))
8265 {
8266 cp_declarator *declarator;
8267
8268 /* Parse another optional declarator. */
8269 declarator = cp_parser_new_declarator_opt (parser);
8270
8271 declarator = cp_parser_make_indirect_declarator
8272 (code, type, cv_quals, declarator, std_attributes);
8273
8274 return declarator;
8275 }
8276
8277 /* If the next token is a `[', there is a direct-new-declarator. */
8278 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8279 return cp_parser_direct_new_declarator (parser);
8280
8281 return NULL;
8282 }
8283
8284 /* Parse a direct-new-declarator.
8285
8286 direct-new-declarator:
8287 [ expression ]
8288 direct-new-declarator [constant-expression]
8289
8290 */
8291
8292 static cp_declarator *
8293 cp_parser_direct_new_declarator (cp_parser* parser)
8294 {
8295 cp_declarator *declarator = NULL;
8296
8297 while (true)
8298 {
8299 tree expression;
8300 cp_token *token;
8301
8302 /* Look for the opening `['. */
8303 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8304
8305 token = cp_lexer_peek_token (parser->lexer);
8306 expression = cp_parser_expression (parser);
8307 /* The standard requires that the expression have integral
8308 type. DR 74 adds enumeration types. We believe that the
8309 real intent is that these expressions be handled like the
8310 expression in a `switch' condition, which also allows
8311 classes with a single conversion to integral or
8312 enumeration type. */
8313 if (!processing_template_decl)
8314 {
8315 expression
8316 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8317 expression,
8318 /*complain=*/true);
8319 if (!expression)
8320 {
8321 error_at (token->location,
8322 "expression in new-declarator must have integral "
8323 "or enumeration type");
8324 expression = error_mark_node;
8325 }
8326 }
8327
8328 /* Look for the closing `]'. */
8329 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8330
8331 /* Add this bound to the declarator. */
8332 declarator = make_array_declarator (declarator, expression);
8333
8334 /* If the next token is not a `[', then there are no more
8335 bounds. */
8336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8337 break;
8338 }
8339
8340 return declarator;
8341 }
8342
8343 /* Parse a new-initializer.
8344
8345 new-initializer:
8346 ( expression-list [opt] )
8347 braced-init-list
8348
8349 Returns a representation of the expression-list. */
8350
8351 static vec<tree, va_gc> *
8352 cp_parser_new_initializer (cp_parser* parser)
8353 {
8354 vec<tree, va_gc> *expression_list;
8355
8356 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8357 {
8358 tree t;
8359 bool expr_non_constant_p;
8360 cp_lexer_set_source_position (parser->lexer);
8361 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8362 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8363 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8364 expression_list = make_tree_vector_single (t);
8365 }
8366 else
8367 expression_list = (cp_parser_parenthesized_expression_list
8368 (parser, non_attr, /*cast_p=*/false,
8369 /*allow_expansion_p=*/true,
8370 /*non_constant_p=*/NULL));
8371
8372 return expression_list;
8373 }
8374
8375 /* Parse a delete-expression.
8376
8377 delete-expression:
8378 :: [opt] delete cast-expression
8379 :: [opt] delete [ ] cast-expression
8380
8381 Returns a representation of the expression. */
8382
8383 static tree
8384 cp_parser_delete_expression (cp_parser* parser)
8385 {
8386 bool global_scope_p;
8387 bool array_p;
8388 tree expression;
8389
8390 /* Look for the optional `::' operator. */
8391 global_scope_p
8392 = (cp_parser_global_scope_opt (parser,
8393 /*current_scope_valid_p=*/false)
8394 != NULL_TREE);
8395 /* Look for the `delete' keyword. */
8396 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8397 /* See if the array syntax is in use. */
8398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8399 {
8400 /* Consume the `[' token. */
8401 cp_lexer_consume_token (parser->lexer);
8402 /* Look for the `]' token. */
8403 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8404 /* Remember that this is the `[]' construct. */
8405 array_p = true;
8406 }
8407 else
8408 array_p = false;
8409
8410 /* Parse the cast-expression. */
8411 expression = cp_parser_simple_cast_expression (parser);
8412
8413 /* A delete-expression may not appear in an integral constant
8414 expression. */
8415 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8416 return error_mark_node;
8417
8418 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8419 tf_warning_or_error);
8420 }
8421
8422 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8423 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8424 0 otherwise. */
8425
8426 static int
8427 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8428 {
8429 cp_token *token = cp_lexer_peek_token (parser->lexer);
8430 switch (token->type)
8431 {
8432 case CPP_COMMA:
8433 case CPP_SEMICOLON:
8434 case CPP_QUERY:
8435 case CPP_COLON:
8436 case CPP_CLOSE_SQUARE:
8437 case CPP_CLOSE_PAREN:
8438 case CPP_CLOSE_BRACE:
8439 case CPP_OPEN_BRACE:
8440 case CPP_DOT:
8441 case CPP_DOT_STAR:
8442 case CPP_DEREF:
8443 case CPP_DEREF_STAR:
8444 case CPP_DIV:
8445 case CPP_MOD:
8446 case CPP_LSHIFT:
8447 case CPP_RSHIFT:
8448 case CPP_LESS:
8449 case CPP_GREATER:
8450 case CPP_LESS_EQ:
8451 case CPP_GREATER_EQ:
8452 case CPP_EQ_EQ:
8453 case CPP_NOT_EQ:
8454 case CPP_EQ:
8455 case CPP_MULT_EQ:
8456 case CPP_DIV_EQ:
8457 case CPP_MOD_EQ:
8458 case CPP_PLUS_EQ:
8459 case CPP_MINUS_EQ:
8460 case CPP_RSHIFT_EQ:
8461 case CPP_LSHIFT_EQ:
8462 case CPP_AND_EQ:
8463 case CPP_XOR_EQ:
8464 case CPP_OR_EQ:
8465 case CPP_XOR:
8466 case CPP_OR:
8467 case CPP_OR_OR:
8468 case CPP_EOF:
8469 case CPP_ELLIPSIS:
8470 return 0;
8471
8472 case CPP_OPEN_PAREN:
8473 /* In ((type ()) () the last () isn't a valid cast-expression,
8474 so the whole must be parsed as postfix-expression. */
8475 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8476 != CPP_CLOSE_PAREN;
8477
8478 case CPP_OPEN_SQUARE:
8479 /* '[' may start a primary-expression in obj-c++ and in C++11,
8480 as a lambda-expression, eg, '(void)[]{}'. */
8481 if (cxx_dialect >= cxx11)
8482 return -1;
8483 return c_dialect_objc ();
8484
8485 case CPP_PLUS_PLUS:
8486 case CPP_MINUS_MINUS:
8487 /* '++' and '--' may or may not start a cast-expression:
8488
8489 struct T { void operator++(int); };
8490 void f() { (T())++; }
8491
8492 vs
8493
8494 int a;
8495 (int)++a; */
8496 return -1;
8497
8498 default:
8499 return 1;
8500 }
8501 }
8502
8503 /* Parse a cast-expression.
8504
8505 cast-expression:
8506 unary-expression
8507 ( type-id ) cast-expression
8508
8509 ADDRESS_P is true iff the unary-expression is appearing as the
8510 operand of the `&' operator. CAST_P is true if this expression is
8511 the target of a cast.
8512
8513 Returns a representation of the expression. */
8514
8515 static cp_expr
8516 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8517 bool decltype_p, cp_id_kind * pidk)
8518 {
8519 /* If it's a `(', then we might be looking at a cast. */
8520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8521 {
8522 tree type = NULL_TREE;
8523 cp_expr expr (NULL_TREE);
8524 int cast_expression = 0;
8525 const char *saved_message;
8526
8527 /* There's no way to know yet whether or not this is a cast.
8528 For example, `(int (3))' is a unary-expression, while `(int)
8529 3' is a cast. So, we resort to parsing tentatively. */
8530 cp_parser_parse_tentatively (parser);
8531 /* Types may not be defined in a cast. */
8532 saved_message = parser->type_definition_forbidden_message;
8533 parser->type_definition_forbidden_message
8534 = G_("types may not be defined in casts");
8535 /* Consume the `('. */
8536 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8537 location_t open_paren_loc = open_paren->location;
8538
8539 /* A very tricky bit is that `(struct S) { 3 }' is a
8540 compound-literal (which we permit in C++ as an extension).
8541 But, that construct is not a cast-expression -- it is a
8542 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8543 is legal; if the compound-literal were a cast-expression,
8544 you'd need an extra set of parentheses.) But, if we parse
8545 the type-id, and it happens to be a class-specifier, then we
8546 will commit to the parse at that point, because we cannot
8547 undo the action that is done when creating a new class. So,
8548 then we cannot back up and do a postfix-expression.
8549
8550 Another tricky case is the following (c++/29234):
8551
8552 struct S { void operator () (); };
8553
8554 void foo ()
8555 {
8556 ( S()() );
8557 }
8558
8559 As a type-id we parse the parenthesized S()() as a function
8560 returning a function, groktypename complains and we cannot
8561 back up in this case either.
8562
8563 Therefore, we scan ahead to the closing `)', and check to see
8564 if the tokens after the `)' can start a cast-expression. Otherwise
8565 we are dealing with an unary-expression, a postfix-expression
8566 or something else.
8567
8568 Yet another tricky case, in C++11, is the following (c++/54891):
8569
8570 (void)[]{};
8571
8572 The issue is that usually, besides the case of lambda-expressions,
8573 the parenthesized type-id cannot be followed by '[', and, eg, we
8574 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8575 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8576 we don't commit, we try a cast-expression, then an unary-expression.
8577
8578 Save tokens so that we can put them back. */
8579 cp_lexer_save_tokens (parser->lexer);
8580
8581 /* We may be looking at a cast-expression. */
8582 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8583 /*consume_paren=*/true))
8584 cast_expression
8585 = cp_parser_tokens_start_cast_expression (parser);
8586
8587 /* Roll back the tokens we skipped. */
8588 cp_lexer_rollback_tokens (parser->lexer);
8589 /* If we aren't looking at a cast-expression, simulate an error so
8590 that the call to cp_parser_error_occurred below returns true. */
8591 if (!cast_expression)
8592 cp_parser_simulate_error (parser);
8593 else
8594 {
8595 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8596 parser->in_type_id_in_expr_p = true;
8597 /* Look for the type-id. */
8598 type = cp_parser_type_id (parser);
8599 /* Look for the closing `)'. */
8600 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8601 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8602 }
8603
8604 /* Restore the saved message. */
8605 parser->type_definition_forbidden_message = saved_message;
8606
8607 /* At this point this can only be either a cast or a
8608 parenthesized ctor such as `(T ())' that looks like a cast to
8609 function returning T. */
8610 if (!cp_parser_error_occurred (parser))
8611 {
8612 /* Only commit if the cast-expression doesn't start with
8613 '++', '--', or '[' in C++11. */
8614 if (cast_expression > 0)
8615 cp_parser_commit_to_topmost_tentative_parse (parser);
8616
8617 expr = cp_parser_cast_expression (parser,
8618 /*address_p=*/false,
8619 /*cast_p=*/true,
8620 /*decltype_p=*/false,
8621 pidk);
8622
8623 if (cp_parser_parse_definitely (parser))
8624 {
8625 /* Warn about old-style casts, if so requested. */
8626 if (warn_old_style_cast
8627 && !in_system_header_at (input_location)
8628 && !VOID_TYPE_P (type)
8629 && current_lang_name != lang_name_c)
8630 warning (OPT_Wold_style_cast, "use of old-style cast");
8631
8632 /* Only type conversions to integral or enumeration types
8633 can be used in constant-expressions. */
8634 if (!cast_valid_in_integral_constant_expression_p (type)
8635 && cp_parser_non_integral_constant_expression (parser,
8636 NIC_CAST))
8637 return error_mark_node;
8638
8639 /* Perform the cast. */
8640 /* Make a location:
8641 (TYPE) EXPR
8642 ^~~~~~~~~~~
8643 with start==caret at the open paren, extending to the
8644 end of "expr". */
8645 location_t cast_loc = make_location (open_paren_loc,
8646 open_paren_loc,
8647 expr.get_finish ());
8648 expr = build_c_cast (cast_loc, type, expr);
8649 return expr;
8650 }
8651 }
8652 else
8653 cp_parser_abort_tentative_parse (parser);
8654 }
8655
8656 /* If we get here, then it's not a cast, so it must be a
8657 unary-expression. */
8658 return cp_parser_unary_expression (parser, pidk, address_p,
8659 cast_p, decltype_p);
8660 }
8661
8662 /* Parse a binary expression of the general form:
8663
8664 pm-expression:
8665 cast-expression
8666 pm-expression .* cast-expression
8667 pm-expression ->* cast-expression
8668
8669 multiplicative-expression:
8670 pm-expression
8671 multiplicative-expression * pm-expression
8672 multiplicative-expression / pm-expression
8673 multiplicative-expression % pm-expression
8674
8675 additive-expression:
8676 multiplicative-expression
8677 additive-expression + multiplicative-expression
8678 additive-expression - multiplicative-expression
8679
8680 shift-expression:
8681 additive-expression
8682 shift-expression << additive-expression
8683 shift-expression >> additive-expression
8684
8685 relational-expression:
8686 shift-expression
8687 relational-expression < shift-expression
8688 relational-expression > shift-expression
8689 relational-expression <= shift-expression
8690 relational-expression >= shift-expression
8691
8692 GNU Extension:
8693
8694 relational-expression:
8695 relational-expression <? shift-expression
8696 relational-expression >? shift-expression
8697
8698 equality-expression:
8699 relational-expression
8700 equality-expression == relational-expression
8701 equality-expression != relational-expression
8702
8703 and-expression:
8704 equality-expression
8705 and-expression & equality-expression
8706
8707 exclusive-or-expression:
8708 and-expression
8709 exclusive-or-expression ^ and-expression
8710
8711 inclusive-or-expression:
8712 exclusive-or-expression
8713 inclusive-or-expression | exclusive-or-expression
8714
8715 logical-and-expression:
8716 inclusive-or-expression
8717 logical-and-expression && inclusive-or-expression
8718
8719 logical-or-expression:
8720 logical-and-expression
8721 logical-or-expression || logical-and-expression
8722
8723 All these are implemented with a single function like:
8724
8725 binary-expression:
8726 simple-cast-expression
8727 binary-expression <token> binary-expression
8728
8729 CAST_P is true if this expression is the target of a cast.
8730
8731 The binops_by_token map is used to get the tree codes for each <token> type.
8732 binary-expressions are associated according to a precedence table. */
8733
8734 #define TOKEN_PRECEDENCE(token) \
8735 (((token->type == CPP_GREATER \
8736 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8737 && !parser->greater_than_is_operator_p) \
8738 ? PREC_NOT_OPERATOR \
8739 : binops_by_token[token->type].prec)
8740
8741 static cp_expr
8742 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8743 bool no_toplevel_fold_p,
8744 bool decltype_p,
8745 enum cp_parser_prec prec,
8746 cp_id_kind * pidk)
8747 {
8748 cp_parser_expression_stack stack;
8749 cp_parser_expression_stack_entry *sp = &stack[0];
8750 cp_parser_expression_stack_entry current;
8751 cp_expr rhs;
8752 cp_token *token;
8753 enum tree_code rhs_type;
8754 enum cp_parser_prec new_prec, lookahead_prec;
8755 tree overload;
8756
8757 /* Parse the first expression. */
8758 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8759 ? TRUTH_NOT_EXPR : ERROR_MARK);
8760 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8761 cast_p, decltype_p, pidk);
8762 current.prec = prec;
8763
8764 if (cp_parser_error_occurred (parser))
8765 return error_mark_node;
8766
8767 for (;;)
8768 {
8769 /* Get an operator token. */
8770 token = cp_lexer_peek_token (parser->lexer);
8771
8772 if (warn_cxx11_compat
8773 && token->type == CPP_RSHIFT
8774 && !parser->greater_than_is_operator_p)
8775 {
8776 if (warning_at (token->location, OPT_Wc__11_compat,
8777 "%<>>%> operator is treated"
8778 " as two right angle brackets in C++11"))
8779 inform (token->location,
8780 "suggest parentheses around %<>>%> expression");
8781 }
8782
8783 new_prec = TOKEN_PRECEDENCE (token);
8784 if (new_prec != PREC_NOT_OPERATOR
8785 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8786 /* This is a fold-expression; handle it later. */
8787 new_prec = PREC_NOT_OPERATOR;
8788
8789 /* Popping an entry off the stack means we completed a subexpression:
8790 - either we found a token which is not an operator (`>' where it is not
8791 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8792 will happen repeatedly;
8793 - or, we found an operator which has lower priority. This is the case
8794 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8795 parsing `3 * 4'. */
8796 if (new_prec <= current.prec)
8797 {
8798 if (sp == stack)
8799 break;
8800 else
8801 goto pop;
8802 }
8803
8804 get_rhs:
8805 current.tree_type = binops_by_token[token->type].tree_type;
8806 current.loc = token->location;
8807
8808 /* We used the operator token. */
8809 cp_lexer_consume_token (parser->lexer);
8810
8811 /* For "false && x" or "true || x", x will never be executed;
8812 disable warnings while evaluating it. */
8813 if (current.tree_type == TRUTH_ANDIF_EXPR)
8814 c_inhibit_evaluation_warnings +=
8815 cp_fully_fold (current.lhs) == truthvalue_false_node;
8816 else if (current.tree_type == TRUTH_ORIF_EXPR)
8817 c_inhibit_evaluation_warnings +=
8818 cp_fully_fold (current.lhs) == truthvalue_true_node;
8819
8820 /* Extract another operand. It may be the RHS of this expression
8821 or the LHS of a new, higher priority expression. */
8822 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8823 ? TRUTH_NOT_EXPR : ERROR_MARK);
8824 rhs = cp_parser_simple_cast_expression (parser);
8825
8826 /* Get another operator token. Look up its precedence to avoid
8827 building a useless (immediately popped) stack entry for common
8828 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8829 token = cp_lexer_peek_token (parser->lexer);
8830 lookahead_prec = TOKEN_PRECEDENCE (token);
8831 if (lookahead_prec != PREC_NOT_OPERATOR
8832 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8833 lookahead_prec = PREC_NOT_OPERATOR;
8834 if (lookahead_prec > new_prec)
8835 {
8836 /* ... and prepare to parse the RHS of the new, higher priority
8837 expression. Since precedence levels on the stack are
8838 monotonically increasing, we do not have to care about
8839 stack overflows. */
8840 *sp = current;
8841 ++sp;
8842 current.lhs = rhs;
8843 current.lhs_type = rhs_type;
8844 current.prec = new_prec;
8845 new_prec = lookahead_prec;
8846 goto get_rhs;
8847
8848 pop:
8849 lookahead_prec = new_prec;
8850 /* If the stack is not empty, we have parsed into LHS the right side
8851 (`4' in the example above) of an expression we had suspended.
8852 We can use the information on the stack to recover the LHS (`3')
8853 from the stack together with the tree code (`MULT_EXPR'), and
8854 the precedence of the higher level subexpression
8855 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8856 which will be used to actually build the additive expression. */
8857 rhs = current.lhs;
8858 rhs_type = current.lhs_type;
8859 --sp;
8860 current = *sp;
8861 }
8862
8863 /* Undo the disabling of warnings done above. */
8864 if (current.tree_type == TRUTH_ANDIF_EXPR)
8865 c_inhibit_evaluation_warnings -=
8866 cp_fully_fold (current.lhs) == truthvalue_false_node;
8867 else if (current.tree_type == TRUTH_ORIF_EXPR)
8868 c_inhibit_evaluation_warnings -=
8869 cp_fully_fold (current.lhs) == truthvalue_true_node;
8870
8871 if (warn_logical_not_paren
8872 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8873 && current.lhs_type == TRUTH_NOT_EXPR
8874 /* Avoid warning for !!x == y. */
8875 && (TREE_CODE (current.lhs) != NE_EXPR
8876 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8877 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8878 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8879 /* Avoid warning for !b == y where b is boolean. */
8880 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8881 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8882 != BOOLEAN_TYPE))))
8883 /* Avoid warning for !!b == y where b is boolean. */
8884 && (!DECL_P (current.lhs)
8885 || TREE_TYPE (current.lhs) == NULL_TREE
8886 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8887 warn_logical_not_parentheses (current.loc, current.tree_type,
8888 maybe_constant_value (rhs));
8889
8890 overload = NULL;
8891
8892 location_t combined_loc = make_location (current.loc,
8893 current.lhs.get_start (),
8894 rhs.get_finish ());
8895
8896 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8897 ERROR_MARK for everything that is not a binary expression.
8898 This makes warn_about_parentheses miss some warnings that
8899 involve unary operators. For unary expressions we should
8900 pass the correct tree_code unless the unary expression was
8901 surrounded by parentheses.
8902 */
8903 if (no_toplevel_fold_p
8904 && lookahead_prec <= current.prec
8905 && sp == stack)
8906 current.lhs = build2_loc (combined_loc,
8907 current.tree_type,
8908 TREE_CODE_CLASS (current.tree_type)
8909 == tcc_comparison
8910 ? boolean_type_node : TREE_TYPE (current.lhs),
8911 current.lhs, rhs);
8912 else
8913 {
8914 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8915 current.lhs, current.lhs_type,
8916 rhs, rhs_type, &overload,
8917 complain_flags (decltype_p));
8918 /* TODO: build_x_binary_op doesn't always honor the location. */
8919 current.lhs.set_location (combined_loc);
8920 }
8921 current.lhs_type = current.tree_type;
8922
8923 /* If the binary operator required the use of an overloaded operator,
8924 then this expression cannot be an integral constant-expression.
8925 An overloaded operator can be used even if both operands are
8926 otherwise permissible in an integral constant-expression if at
8927 least one of the operands is of enumeration type. */
8928
8929 if (overload
8930 && cp_parser_non_integral_constant_expression (parser,
8931 NIC_OVERLOADED))
8932 return error_mark_node;
8933 }
8934
8935 return current.lhs;
8936 }
8937
8938 static cp_expr
8939 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8940 bool no_toplevel_fold_p,
8941 enum cp_parser_prec prec,
8942 cp_id_kind * pidk)
8943 {
8944 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8945 /*decltype*/false, prec, pidk);
8946 }
8947
8948 /* Parse the `? expression : assignment-expression' part of a
8949 conditional-expression. The LOGICAL_OR_EXPR is the
8950 logical-or-expression that started the conditional-expression.
8951 Returns a representation of the entire conditional-expression.
8952
8953 This routine is used by cp_parser_assignment_expression.
8954
8955 ? expression : assignment-expression
8956
8957 GNU Extensions:
8958
8959 ? : assignment-expression */
8960
8961 static tree
8962 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8963 {
8964 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8965 cp_expr assignment_expr;
8966 struct cp_token *token;
8967 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8968
8969 /* Consume the `?' token. */
8970 cp_lexer_consume_token (parser->lexer);
8971 token = cp_lexer_peek_token (parser->lexer);
8972 if (cp_parser_allow_gnu_extensions_p (parser)
8973 && token->type == CPP_COLON)
8974 {
8975 pedwarn (token->location, OPT_Wpedantic,
8976 "ISO C++ does not allow ?: with omitted middle operand");
8977 /* Implicit true clause. */
8978 expr = NULL_TREE;
8979 c_inhibit_evaluation_warnings +=
8980 folded_logical_or_expr == truthvalue_true_node;
8981 warn_for_omitted_condop (token->location, logical_or_expr);
8982 }
8983 else
8984 {
8985 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8986 parser->colon_corrects_to_scope_p = false;
8987 /* Parse the expression. */
8988 c_inhibit_evaluation_warnings +=
8989 folded_logical_or_expr == truthvalue_false_node;
8990 expr = cp_parser_expression (parser);
8991 c_inhibit_evaluation_warnings +=
8992 ((folded_logical_or_expr == truthvalue_true_node)
8993 - (folded_logical_or_expr == truthvalue_false_node));
8994 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8995 }
8996
8997 /* The next token should be a `:'. */
8998 cp_parser_require (parser, CPP_COLON, RT_COLON);
8999 /* Parse the assignment-expression. */
9000 assignment_expr = cp_parser_assignment_expression (parser);
9001 c_inhibit_evaluation_warnings -=
9002 folded_logical_or_expr == truthvalue_true_node;
9003
9004 /* Make a location:
9005 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9006 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9007 with the caret at the "?", ranging from the start of
9008 the logical_or_expr to the end of the assignment_expr. */
9009 loc = make_location (loc,
9010 logical_or_expr.get_start (),
9011 assignment_expr.get_finish ());
9012
9013 /* Build the conditional-expression. */
9014 return build_x_conditional_expr (loc, logical_or_expr,
9015 expr,
9016 assignment_expr,
9017 tf_warning_or_error);
9018 }
9019
9020 /* Parse an assignment-expression.
9021
9022 assignment-expression:
9023 conditional-expression
9024 logical-or-expression assignment-operator assignment_expression
9025 throw-expression
9026
9027 CAST_P is true if this expression is the target of a cast.
9028 DECLTYPE_P is true if this expression is the operand of decltype.
9029
9030 Returns a representation for the expression. */
9031
9032 static cp_expr
9033 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9034 bool cast_p, bool decltype_p)
9035 {
9036 cp_expr expr;
9037
9038 /* If the next token is the `throw' keyword, then we're looking at
9039 a throw-expression. */
9040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9041 expr = cp_parser_throw_expression (parser);
9042 /* Otherwise, it must be that we are looking at a
9043 logical-or-expression. */
9044 else
9045 {
9046 /* Parse the binary expressions (logical-or-expression). */
9047 expr = cp_parser_binary_expression (parser, cast_p, false,
9048 decltype_p,
9049 PREC_NOT_OPERATOR, pidk);
9050 /* If the next token is a `?' then we're actually looking at a
9051 conditional-expression. */
9052 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9053 return cp_parser_question_colon_clause (parser, expr);
9054 else
9055 {
9056 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9057
9058 /* If it's an assignment-operator, we're using the second
9059 production. */
9060 enum tree_code assignment_operator
9061 = cp_parser_assignment_operator_opt (parser);
9062 if (assignment_operator != ERROR_MARK)
9063 {
9064 bool non_constant_p;
9065
9066 /* Parse the right-hand side of the assignment. */
9067 cp_expr rhs = cp_parser_initializer_clause (parser,
9068 &non_constant_p);
9069
9070 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9071 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9072
9073 /* An assignment may not appear in a
9074 constant-expression. */
9075 if (cp_parser_non_integral_constant_expression (parser,
9076 NIC_ASSIGNMENT))
9077 return error_mark_node;
9078 /* Build the assignment expression. Its default
9079 location:
9080 LHS = RHS
9081 ~~~~^~~~~
9082 is the location of the '=' token as the
9083 caret, ranging from the start of the lhs to the
9084 end of the rhs. */
9085 loc = make_location (loc,
9086 expr.get_start (),
9087 rhs.get_finish ());
9088 expr = build_x_modify_expr (loc, expr,
9089 assignment_operator,
9090 rhs,
9091 complain_flags (decltype_p));
9092 /* TODO: build_x_modify_expr doesn't honor the location,
9093 so we must set it here. */
9094 expr.set_location (loc);
9095 }
9096 }
9097 }
9098
9099 return expr;
9100 }
9101
9102 /* Parse an (optional) assignment-operator.
9103
9104 assignment-operator: one of
9105 = *= /= %= += -= >>= <<= &= ^= |=
9106
9107 GNU Extension:
9108
9109 assignment-operator: one of
9110 <?= >?=
9111
9112 If the next token is an assignment operator, the corresponding tree
9113 code is returned, and the token is consumed. For example, for
9114 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9115 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9116 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9117 operator, ERROR_MARK is returned. */
9118
9119 static enum tree_code
9120 cp_parser_assignment_operator_opt (cp_parser* parser)
9121 {
9122 enum tree_code op;
9123 cp_token *token;
9124
9125 /* Peek at the next token. */
9126 token = cp_lexer_peek_token (parser->lexer);
9127
9128 switch (token->type)
9129 {
9130 case CPP_EQ:
9131 op = NOP_EXPR;
9132 break;
9133
9134 case CPP_MULT_EQ:
9135 op = MULT_EXPR;
9136 break;
9137
9138 case CPP_DIV_EQ:
9139 op = TRUNC_DIV_EXPR;
9140 break;
9141
9142 case CPP_MOD_EQ:
9143 op = TRUNC_MOD_EXPR;
9144 break;
9145
9146 case CPP_PLUS_EQ:
9147 op = PLUS_EXPR;
9148 break;
9149
9150 case CPP_MINUS_EQ:
9151 op = MINUS_EXPR;
9152 break;
9153
9154 case CPP_RSHIFT_EQ:
9155 op = RSHIFT_EXPR;
9156 break;
9157
9158 case CPP_LSHIFT_EQ:
9159 op = LSHIFT_EXPR;
9160 break;
9161
9162 case CPP_AND_EQ:
9163 op = BIT_AND_EXPR;
9164 break;
9165
9166 case CPP_XOR_EQ:
9167 op = BIT_XOR_EXPR;
9168 break;
9169
9170 case CPP_OR_EQ:
9171 op = BIT_IOR_EXPR;
9172 break;
9173
9174 default:
9175 /* Nothing else is an assignment operator. */
9176 op = ERROR_MARK;
9177 }
9178
9179 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9180 if (op != ERROR_MARK
9181 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9182 op = ERROR_MARK;
9183
9184 /* If it was an assignment operator, consume it. */
9185 if (op != ERROR_MARK)
9186 cp_lexer_consume_token (parser->lexer);
9187
9188 return op;
9189 }
9190
9191 /* Parse an expression.
9192
9193 expression:
9194 assignment-expression
9195 expression , assignment-expression
9196
9197 CAST_P is true if this expression is the target of a cast.
9198 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9199 except possibly parenthesized or on the RHS of a comma (N3276).
9200
9201 Returns a representation of the expression. */
9202
9203 static cp_expr
9204 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9205 bool cast_p, bool decltype_p)
9206 {
9207 cp_expr expression = NULL_TREE;
9208 location_t loc = UNKNOWN_LOCATION;
9209
9210 while (true)
9211 {
9212 cp_expr assignment_expression;
9213
9214 /* Parse the next assignment-expression. */
9215 assignment_expression
9216 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9217
9218 /* We don't create a temporary for a call that is the immediate operand
9219 of decltype or on the RHS of a comma. But when we see a comma, we
9220 need to create a temporary for a call on the LHS. */
9221 if (decltype_p && !processing_template_decl
9222 && TREE_CODE (assignment_expression) == CALL_EXPR
9223 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9224 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9225 assignment_expression
9226 = build_cplus_new (TREE_TYPE (assignment_expression),
9227 assignment_expression, tf_warning_or_error);
9228
9229 /* If this is the first assignment-expression, we can just
9230 save it away. */
9231 if (!expression)
9232 expression = assignment_expression;
9233 else
9234 {
9235 /* Create a location with caret at the comma, ranging
9236 from the start of the LHS to the end of the RHS. */
9237 loc = make_location (loc,
9238 expression.get_start (),
9239 assignment_expression.get_finish ());
9240 expression = build_x_compound_expr (loc, expression,
9241 assignment_expression,
9242 complain_flags (decltype_p));
9243 expression.set_location (loc);
9244 }
9245 /* If the next token is not a comma, or we're in a fold-expression, then
9246 we are done with the expression. */
9247 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9248 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9249 break;
9250 /* Consume the `,'. */
9251 loc = cp_lexer_peek_token (parser->lexer)->location;
9252 cp_lexer_consume_token (parser->lexer);
9253 /* A comma operator cannot appear in a constant-expression. */
9254 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9255 expression = error_mark_node;
9256 }
9257
9258 return expression;
9259 }
9260
9261 /* Parse a constant-expression.
9262
9263 constant-expression:
9264 conditional-expression
9265
9266 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9267 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9268 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9269 is false, NON_CONSTANT_P should be NULL. */
9270
9271 static cp_expr
9272 cp_parser_constant_expression (cp_parser* parser,
9273 bool allow_non_constant_p,
9274 bool *non_constant_p)
9275 {
9276 bool saved_integral_constant_expression_p;
9277 bool saved_allow_non_integral_constant_expression_p;
9278 bool saved_non_integral_constant_expression_p;
9279 cp_expr expression;
9280
9281 /* It might seem that we could simply parse the
9282 conditional-expression, and then check to see if it were
9283 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9284 one that the compiler can figure out is constant, possibly after
9285 doing some simplifications or optimizations. The standard has a
9286 precise definition of constant-expression, and we must honor
9287 that, even though it is somewhat more restrictive.
9288
9289 For example:
9290
9291 int i[(2, 3)];
9292
9293 is not a legal declaration, because `(2, 3)' is not a
9294 constant-expression. The `,' operator is forbidden in a
9295 constant-expression. However, GCC's constant-folding machinery
9296 will fold this operation to an INTEGER_CST for `3'. */
9297
9298 /* Save the old settings. */
9299 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9300 saved_allow_non_integral_constant_expression_p
9301 = parser->allow_non_integral_constant_expression_p;
9302 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9303 /* We are now parsing a constant-expression. */
9304 parser->integral_constant_expression_p = true;
9305 parser->allow_non_integral_constant_expression_p
9306 = (allow_non_constant_p || cxx_dialect >= cxx11);
9307 parser->non_integral_constant_expression_p = false;
9308 /* Although the grammar says "conditional-expression", we parse an
9309 "assignment-expression", which also permits "throw-expression"
9310 and the use of assignment operators. In the case that
9311 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9312 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9313 actually essential that we look for an assignment-expression.
9314 For example, cp_parser_initializer_clauses uses this function to
9315 determine whether a particular assignment-expression is in fact
9316 constant. */
9317 expression = cp_parser_assignment_expression (parser);
9318 /* Restore the old settings. */
9319 parser->integral_constant_expression_p
9320 = saved_integral_constant_expression_p;
9321 parser->allow_non_integral_constant_expression_p
9322 = saved_allow_non_integral_constant_expression_p;
9323 if (cxx_dialect >= cxx11)
9324 {
9325 /* Require an rvalue constant expression here; that's what our
9326 callers expect. Reference constant expressions are handled
9327 separately in e.g. cp_parser_template_argument. */
9328 bool is_const = potential_rvalue_constant_expression (expression);
9329 parser->non_integral_constant_expression_p = !is_const;
9330 if (!is_const && !allow_non_constant_p)
9331 require_potential_rvalue_constant_expression (expression);
9332 }
9333 if (allow_non_constant_p)
9334 *non_constant_p = parser->non_integral_constant_expression_p;
9335 parser->non_integral_constant_expression_p
9336 = saved_non_integral_constant_expression_p;
9337
9338 return expression;
9339 }
9340
9341 /* Parse __builtin_offsetof.
9342
9343 offsetof-expression:
9344 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9345
9346 offsetof-member-designator:
9347 id-expression
9348 | offsetof-member-designator "." id-expression
9349 | offsetof-member-designator "[" expression "]"
9350 | offsetof-member-designator "->" id-expression */
9351
9352 static cp_expr
9353 cp_parser_builtin_offsetof (cp_parser *parser)
9354 {
9355 int save_ice_p, save_non_ice_p;
9356 tree type;
9357 cp_expr expr;
9358 cp_id_kind dummy;
9359 cp_token *token;
9360 location_t finish_loc;
9361
9362 /* We're about to accept non-integral-constant things, but will
9363 definitely yield an integral constant expression. Save and
9364 restore these values around our local parsing. */
9365 save_ice_p = parser->integral_constant_expression_p;
9366 save_non_ice_p = parser->non_integral_constant_expression_p;
9367
9368 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9369
9370 /* Consume the "__builtin_offsetof" token. */
9371 cp_lexer_consume_token (parser->lexer);
9372 /* Consume the opening `('. */
9373 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9374 /* Parse the type-id. */
9375 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9376 type = cp_parser_type_id (parser);
9377 /* Look for the `,'. */
9378 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9379 token = cp_lexer_peek_token (parser->lexer);
9380
9381 /* Build the (type *)null that begins the traditional offsetof macro. */
9382 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9383 tf_warning_or_error);
9384
9385 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9386 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9387 true, &dummy, token->location);
9388 while (true)
9389 {
9390 token = cp_lexer_peek_token (parser->lexer);
9391 switch (token->type)
9392 {
9393 case CPP_OPEN_SQUARE:
9394 /* offsetof-member-designator "[" expression "]" */
9395 expr = cp_parser_postfix_open_square_expression (parser, expr,
9396 true, false);
9397 break;
9398
9399 case CPP_DEREF:
9400 /* offsetof-member-designator "->" identifier */
9401 expr = grok_array_decl (token->location, expr,
9402 integer_zero_node, false);
9403 /* FALLTHRU */
9404
9405 case CPP_DOT:
9406 /* offsetof-member-designator "." identifier */
9407 cp_lexer_consume_token (parser->lexer);
9408 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9409 expr, true, &dummy,
9410 token->location);
9411 break;
9412
9413 case CPP_CLOSE_PAREN:
9414 /* Consume the ")" token. */
9415 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9416 cp_lexer_consume_token (parser->lexer);
9417 goto success;
9418
9419 default:
9420 /* Error. We know the following require will fail, but
9421 that gives the proper error message. */
9422 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9423 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9424 expr = error_mark_node;
9425 goto failure;
9426 }
9427 }
9428
9429 success:
9430 /* Make a location of the form:
9431 __builtin_offsetof (struct s, f)
9432 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9433 with caret at the type-id, ranging from the start of the
9434 "_builtin_offsetof" token to the close paren. */
9435 loc = make_location (loc, start_loc, finish_loc);
9436 /* The result will be an INTEGER_CST, so we need to explicitly
9437 preserve the location. */
9438 expr = cp_expr (finish_offsetof (expr, loc), loc);
9439
9440 failure:
9441 parser->integral_constant_expression_p = save_ice_p;
9442 parser->non_integral_constant_expression_p = save_non_ice_p;
9443
9444 return expr;
9445 }
9446
9447 /* Parse a trait expression.
9448
9449 Returns a representation of the expression, the underlying type
9450 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9451
9452 static tree
9453 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9454 {
9455 cp_trait_kind kind;
9456 tree type1, type2 = NULL_TREE;
9457 bool binary = false;
9458 bool variadic = false;
9459
9460 switch (keyword)
9461 {
9462 case RID_HAS_NOTHROW_ASSIGN:
9463 kind = CPTK_HAS_NOTHROW_ASSIGN;
9464 break;
9465 case RID_HAS_NOTHROW_CONSTRUCTOR:
9466 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9467 break;
9468 case RID_HAS_NOTHROW_COPY:
9469 kind = CPTK_HAS_NOTHROW_COPY;
9470 break;
9471 case RID_HAS_TRIVIAL_ASSIGN:
9472 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9473 break;
9474 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9475 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9476 break;
9477 case RID_HAS_TRIVIAL_COPY:
9478 kind = CPTK_HAS_TRIVIAL_COPY;
9479 break;
9480 case RID_HAS_TRIVIAL_DESTRUCTOR:
9481 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9482 break;
9483 case RID_HAS_VIRTUAL_DESTRUCTOR:
9484 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9485 break;
9486 case RID_IS_ABSTRACT:
9487 kind = CPTK_IS_ABSTRACT;
9488 break;
9489 case RID_IS_BASE_OF:
9490 kind = CPTK_IS_BASE_OF;
9491 binary = true;
9492 break;
9493 case RID_IS_CLASS:
9494 kind = CPTK_IS_CLASS;
9495 break;
9496 case RID_IS_EMPTY:
9497 kind = CPTK_IS_EMPTY;
9498 break;
9499 case RID_IS_ENUM:
9500 kind = CPTK_IS_ENUM;
9501 break;
9502 case RID_IS_FINAL:
9503 kind = CPTK_IS_FINAL;
9504 break;
9505 case RID_IS_LITERAL_TYPE:
9506 kind = CPTK_IS_LITERAL_TYPE;
9507 break;
9508 case RID_IS_POD:
9509 kind = CPTK_IS_POD;
9510 break;
9511 case RID_IS_POLYMORPHIC:
9512 kind = CPTK_IS_POLYMORPHIC;
9513 break;
9514 case RID_IS_SAME_AS:
9515 kind = CPTK_IS_SAME_AS;
9516 binary = true;
9517 break;
9518 case RID_IS_STD_LAYOUT:
9519 kind = CPTK_IS_STD_LAYOUT;
9520 break;
9521 case RID_IS_TRIVIAL:
9522 kind = CPTK_IS_TRIVIAL;
9523 break;
9524 case RID_IS_TRIVIALLY_ASSIGNABLE:
9525 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9526 binary = true;
9527 break;
9528 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9529 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9530 variadic = true;
9531 break;
9532 case RID_IS_TRIVIALLY_COPYABLE:
9533 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9534 break;
9535 case RID_IS_UNION:
9536 kind = CPTK_IS_UNION;
9537 break;
9538 case RID_UNDERLYING_TYPE:
9539 kind = CPTK_UNDERLYING_TYPE;
9540 break;
9541 case RID_BASES:
9542 kind = CPTK_BASES;
9543 break;
9544 case RID_DIRECT_BASES:
9545 kind = CPTK_DIRECT_BASES;
9546 break;
9547 default:
9548 gcc_unreachable ();
9549 }
9550
9551 /* Consume the token. */
9552 cp_lexer_consume_token (parser->lexer);
9553
9554 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9555
9556 {
9557 type_id_in_expr_sentinel s (parser);
9558 type1 = cp_parser_type_id (parser);
9559 }
9560
9561 if (type1 == error_mark_node)
9562 return error_mark_node;
9563
9564 if (binary)
9565 {
9566 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9567
9568 {
9569 type_id_in_expr_sentinel s (parser);
9570 type2 = cp_parser_type_id (parser);
9571 }
9572
9573 if (type2 == error_mark_node)
9574 return error_mark_node;
9575 }
9576 else if (variadic)
9577 {
9578 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9579 {
9580 cp_lexer_consume_token (parser->lexer);
9581 tree elt = cp_parser_type_id (parser);
9582 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9583 {
9584 cp_lexer_consume_token (parser->lexer);
9585 elt = make_pack_expansion (elt);
9586 }
9587 if (elt == error_mark_node)
9588 return error_mark_node;
9589 type2 = tree_cons (NULL_TREE, elt, type2);
9590 }
9591 }
9592
9593 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9594
9595 /* Complete the trait expression, which may mean either processing
9596 the trait expr now or saving it for template instantiation. */
9597 switch(kind)
9598 {
9599 case CPTK_UNDERLYING_TYPE:
9600 return finish_underlying_type (type1);
9601 case CPTK_BASES:
9602 return finish_bases (type1, false);
9603 case CPTK_DIRECT_BASES:
9604 return finish_bases (type1, true);
9605 default:
9606 return finish_trait_expr (kind, type1, type2);
9607 }
9608 }
9609
9610 /* Lambdas that appear in variable initializer or default argument scope
9611 get that in their mangling, so we need to record it. We might as well
9612 use the count for function and namespace scopes as well. */
9613 static GTY(()) tree lambda_scope;
9614 static GTY(()) int lambda_count;
9615 struct GTY(()) tree_int
9616 {
9617 tree t;
9618 int i;
9619 };
9620 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9621
9622 static void
9623 start_lambda_scope (tree decl)
9624 {
9625 tree_int ti;
9626 gcc_assert (decl);
9627 /* Once we're inside a function, we ignore other scopes and just push
9628 the function again so that popping works properly. */
9629 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9630 decl = current_function_decl;
9631 ti.t = lambda_scope;
9632 ti.i = lambda_count;
9633 vec_safe_push (lambda_scope_stack, ti);
9634 if (lambda_scope != decl)
9635 {
9636 /* Don't reset the count if we're still in the same function. */
9637 lambda_scope = decl;
9638 lambda_count = 0;
9639 }
9640 }
9641
9642 static void
9643 record_lambda_scope (tree lambda)
9644 {
9645 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9646 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9647 }
9648
9649 static void
9650 finish_lambda_scope (void)
9651 {
9652 tree_int *p = &lambda_scope_stack->last ();
9653 if (lambda_scope != p->t)
9654 {
9655 lambda_scope = p->t;
9656 lambda_count = p->i;
9657 }
9658 lambda_scope_stack->pop ();
9659 }
9660
9661 /* Parse a lambda expression.
9662
9663 lambda-expression:
9664 lambda-introducer lambda-declarator [opt] compound-statement
9665
9666 Returns a representation of the expression. */
9667
9668 static cp_expr
9669 cp_parser_lambda_expression (cp_parser* parser)
9670 {
9671 tree lambda_expr = build_lambda_expr ();
9672 tree type;
9673 bool ok = true;
9674 cp_token *token = cp_lexer_peek_token (parser->lexer);
9675 cp_token_position start = 0;
9676
9677 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9678
9679 if (cp_unevaluated_operand)
9680 {
9681 if (!token->error_reported)
9682 {
9683 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9684 "lambda-expression in unevaluated context");
9685 token->error_reported = true;
9686 }
9687 ok = false;
9688 }
9689 else if (parser->in_template_argument_list_p)
9690 {
9691 if (!token->error_reported)
9692 {
9693 error_at (token->location, "lambda-expression in template-argument");
9694 token->error_reported = true;
9695 }
9696 ok = false;
9697 }
9698
9699 /* We may be in the middle of deferred access check. Disable
9700 it now. */
9701 push_deferring_access_checks (dk_no_deferred);
9702
9703 cp_parser_lambda_introducer (parser, lambda_expr);
9704
9705 type = begin_lambda_type (lambda_expr);
9706 if (type == error_mark_node)
9707 return error_mark_node;
9708
9709 record_lambda_scope (lambda_expr);
9710
9711 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9712 determine_visibility (TYPE_NAME (type));
9713
9714 /* Now that we've started the type, add the capture fields for any
9715 explicit captures. */
9716 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9717
9718 {
9719 /* Inside the class, surrounding template-parameter-lists do not apply. */
9720 unsigned int saved_num_template_parameter_lists
9721 = parser->num_template_parameter_lists;
9722 unsigned char in_statement = parser->in_statement;
9723 bool in_switch_statement_p = parser->in_switch_statement_p;
9724 bool fully_implicit_function_template_p
9725 = parser->fully_implicit_function_template_p;
9726 tree implicit_template_parms = parser->implicit_template_parms;
9727 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9728 bool auto_is_implicit_function_template_parm_p
9729 = parser->auto_is_implicit_function_template_parm_p;
9730
9731 parser->num_template_parameter_lists = 0;
9732 parser->in_statement = 0;
9733 parser->in_switch_statement_p = false;
9734 parser->fully_implicit_function_template_p = false;
9735 parser->implicit_template_parms = 0;
9736 parser->implicit_template_scope = 0;
9737 parser->auto_is_implicit_function_template_parm_p = false;
9738
9739 /* By virtue of defining a local class, a lambda expression has access to
9740 the private variables of enclosing classes. */
9741
9742 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9743
9744 if (ok)
9745 {
9746 if (!cp_parser_error_occurred (parser)
9747 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9748 && cp_parser_start_tentative_firewall (parser))
9749 start = token;
9750 cp_parser_lambda_body (parser, lambda_expr);
9751 }
9752 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9753 {
9754 if (cp_parser_skip_to_closing_brace (parser))
9755 cp_lexer_consume_token (parser->lexer);
9756 }
9757
9758 /* The capture list was built up in reverse order; fix that now. */
9759 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9760 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9761
9762 if (ok)
9763 maybe_add_lambda_conv_op (type);
9764
9765 type = finish_struct (type, /*attributes=*/NULL_TREE);
9766
9767 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9768 parser->in_statement = in_statement;
9769 parser->in_switch_statement_p = in_switch_statement_p;
9770 parser->fully_implicit_function_template_p
9771 = fully_implicit_function_template_p;
9772 parser->implicit_template_parms = implicit_template_parms;
9773 parser->implicit_template_scope = implicit_template_scope;
9774 parser->auto_is_implicit_function_template_parm_p
9775 = auto_is_implicit_function_template_parm_p;
9776 }
9777
9778 /* This field is only used during parsing of the lambda. */
9779 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9780
9781 /* This lambda shouldn't have any proxies left at this point. */
9782 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9783 /* And now that we're done, push proxies for an enclosing lambda. */
9784 insert_pending_capture_proxies ();
9785
9786 if (ok)
9787 lambda_expr = build_lambda_object (lambda_expr);
9788 else
9789 lambda_expr = error_mark_node;
9790
9791 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9792
9793 pop_deferring_access_checks ();
9794
9795 return lambda_expr;
9796 }
9797
9798 /* Parse the beginning of a lambda expression.
9799
9800 lambda-introducer:
9801 [ lambda-capture [opt] ]
9802
9803 LAMBDA_EXPR is the current representation of the lambda expression. */
9804
9805 static void
9806 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9807 {
9808 /* Need commas after the first capture. */
9809 bool first = true;
9810
9811 /* Eat the leading `['. */
9812 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9813
9814 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9815 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9816 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9817 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9818 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9819 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9820
9821 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9822 {
9823 cp_lexer_consume_token (parser->lexer);
9824 first = false;
9825 }
9826
9827 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9828 {
9829 cp_token* capture_token;
9830 tree capture_id;
9831 tree capture_init_expr;
9832 cp_id_kind idk = CP_ID_KIND_NONE;
9833 bool explicit_init_p = false;
9834
9835 enum capture_kind_type
9836 {
9837 BY_COPY,
9838 BY_REFERENCE
9839 };
9840 enum capture_kind_type capture_kind = BY_COPY;
9841
9842 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9843 {
9844 error ("expected end of capture-list");
9845 return;
9846 }
9847
9848 if (first)
9849 first = false;
9850 else
9851 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9852
9853 /* Possibly capture `this'. */
9854 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9855 {
9856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9857 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9858 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9859 "with by-copy capture default");
9860 cp_lexer_consume_token (parser->lexer);
9861 add_capture (lambda_expr,
9862 /*id=*/this_identifier,
9863 /*initializer=*/finish_this_expr(),
9864 /*by_reference_p=*/false,
9865 explicit_init_p);
9866 continue;
9867 }
9868
9869 /* Remember whether we want to capture as a reference or not. */
9870 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9871 {
9872 capture_kind = BY_REFERENCE;
9873 cp_lexer_consume_token (parser->lexer);
9874 }
9875
9876 /* Get the identifier. */
9877 capture_token = cp_lexer_peek_token (parser->lexer);
9878 capture_id = cp_parser_identifier (parser);
9879
9880 if (capture_id == error_mark_node)
9881 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9882 delimiters, but I modified this to stop on unnested ']' as well. It
9883 was already changed to stop on unnested '}', so the
9884 "closing_parenthesis" name is no more misleading with my change. */
9885 {
9886 cp_parser_skip_to_closing_parenthesis (parser,
9887 /*recovering=*/true,
9888 /*or_comma=*/true,
9889 /*consume_paren=*/true);
9890 break;
9891 }
9892
9893 /* Find the initializer for this capture. */
9894 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9895 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9896 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9897 {
9898 bool direct, non_constant;
9899 /* An explicit initializer exists. */
9900 if (cxx_dialect < cxx14)
9901 pedwarn (input_location, 0,
9902 "lambda capture initializers "
9903 "only available with -std=c++14 or -std=gnu++14");
9904 capture_init_expr = cp_parser_initializer (parser, &direct,
9905 &non_constant);
9906 explicit_init_p = true;
9907 if (capture_init_expr == NULL_TREE)
9908 {
9909 error ("empty initializer for lambda init-capture");
9910 capture_init_expr = error_mark_node;
9911 }
9912 }
9913 else
9914 {
9915 const char* error_msg;
9916
9917 /* Turn the identifier into an id-expression. */
9918 capture_init_expr
9919 = cp_parser_lookup_name_simple (parser, capture_id,
9920 capture_token->location);
9921
9922 if (capture_init_expr == error_mark_node)
9923 {
9924 unqualified_name_lookup_error (capture_id);
9925 continue;
9926 }
9927 else if (DECL_P (capture_init_expr)
9928 && (!VAR_P (capture_init_expr)
9929 && TREE_CODE (capture_init_expr) != PARM_DECL))
9930 {
9931 error_at (capture_token->location,
9932 "capture of non-variable %qD ",
9933 capture_init_expr);
9934 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9935 "%q#D declared here", capture_init_expr);
9936 continue;
9937 }
9938 if (VAR_P (capture_init_expr)
9939 && decl_storage_duration (capture_init_expr) != dk_auto)
9940 {
9941 if (pedwarn (capture_token->location, 0, "capture of variable "
9942 "%qD with non-automatic storage duration",
9943 capture_init_expr))
9944 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9945 "%q#D declared here", capture_init_expr);
9946 continue;
9947 }
9948
9949 capture_init_expr
9950 = finish_id_expression
9951 (capture_id,
9952 capture_init_expr,
9953 parser->scope,
9954 &idk,
9955 /*integral_constant_expression_p=*/false,
9956 /*allow_non_integral_constant_expression_p=*/false,
9957 /*non_integral_constant_expression_p=*/NULL,
9958 /*template_p=*/false,
9959 /*done=*/true,
9960 /*address_p=*/false,
9961 /*template_arg_p=*/false,
9962 &error_msg,
9963 capture_token->location);
9964
9965 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9966 {
9967 cp_lexer_consume_token (parser->lexer);
9968 capture_init_expr = make_pack_expansion (capture_init_expr);
9969 }
9970 else
9971 check_for_bare_parameter_packs (capture_init_expr);
9972 }
9973
9974 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9975 && !explicit_init_p)
9976 {
9977 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9978 && capture_kind == BY_COPY)
9979 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9980 "of %qD redundant with by-copy capture default",
9981 capture_id);
9982 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9983 && capture_kind == BY_REFERENCE)
9984 pedwarn (capture_token->location, 0, "explicit by-reference "
9985 "capture of %qD redundant with by-reference capture "
9986 "default", capture_id);
9987 }
9988
9989 add_capture (lambda_expr,
9990 capture_id,
9991 capture_init_expr,
9992 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9993 explicit_init_p);
9994 }
9995
9996 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9997 }
9998
9999 /* Parse the (optional) middle of a lambda expression.
10000
10001 lambda-declarator:
10002 < template-parameter-list [opt] >
10003 ( parameter-declaration-clause [opt] )
10004 attribute-specifier [opt]
10005 mutable [opt]
10006 exception-specification [opt]
10007 lambda-return-type-clause [opt]
10008
10009 LAMBDA_EXPR is the current representation of the lambda expression. */
10010
10011 static bool
10012 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10013 {
10014 /* 5.1.1.4 of the standard says:
10015 If a lambda-expression does not include a lambda-declarator, it is as if
10016 the lambda-declarator were ().
10017 This means an empty parameter list, no attributes, and no exception
10018 specification. */
10019 tree param_list = void_list_node;
10020 tree attributes = NULL_TREE;
10021 tree exception_spec = NULL_TREE;
10022 tree template_param_list = NULL_TREE;
10023 tree tx_qual = NULL_TREE;
10024
10025 /* The template-parameter-list is optional, but must begin with
10026 an opening angle if present. */
10027 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10028 {
10029 if (cxx_dialect < cxx14)
10030 pedwarn (parser->lexer->next_token->location, 0,
10031 "lambda templates are only available with "
10032 "-std=c++14 or -std=gnu++14");
10033
10034 cp_lexer_consume_token (parser->lexer);
10035
10036 template_param_list = cp_parser_template_parameter_list (parser);
10037
10038 cp_parser_skip_to_end_of_template_parameter_list (parser);
10039
10040 /* We just processed one more parameter list. */
10041 ++parser->num_template_parameter_lists;
10042 }
10043
10044 /* The parameter-declaration-clause is optional (unless
10045 template-parameter-list was given), but must begin with an
10046 opening parenthesis if present. */
10047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10048 {
10049 cp_lexer_consume_token (parser->lexer);
10050
10051 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10052
10053 /* Parse parameters. */
10054 param_list = cp_parser_parameter_declaration_clause (parser);
10055
10056 /* Default arguments shall not be specified in the
10057 parameter-declaration-clause of a lambda-declarator. */
10058 for (tree t = param_list; t; t = TREE_CHAIN (t))
10059 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10060 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10061 "default argument specified for lambda parameter");
10062
10063 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10064
10065 attributes = cp_parser_attributes_opt (parser);
10066
10067 /* Parse optional `mutable' keyword. */
10068 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10069 {
10070 cp_lexer_consume_token (parser->lexer);
10071 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10072 }
10073
10074 tx_qual = cp_parser_tx_qualifier_opt (parser);
10075
10076 /* Parse optional exception specification. */
10077 exception_spec = cp_parser_exception_specification_opt (parser);
10078
10079 /* Parse optional trailing return type. */
10080 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10081 {
10082 cp_lexer_consume_token (parser->lexer);
10083 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10084 = cp_parser_trailing_type_id (parser);
10085 }
10086
10087 /* The function parameters must be in scope all the way until after the
10088 trailing-return-type in case of decltype. */
10089 pop_bindings_and_leave_scope ();
10090 }
10091 else if (template_param_list != NULL_TREE) // generate diagnostic
10092 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10093
10094 /* Create the function call operator.
10095
10096 Messing with declarators like this is no uglier than building up the
10097 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10098 other code. */
10099 {
10100 cp_decl_specifier_seq return_type_specs;
10101 cp_declarator* declarator;
10102 tree fco;
10103 int quals;
10104 void *p;
10105
10106 clear_decl_specs (&return_type_specs);
10107 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10108 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10109 else
10110 /* Maybe we will deduce the return type later. */
10111 return_type_specs.type = make_auto ();
10112
10113 p = obstack_alloc (&declarator_obstack, 0);
10114
10115 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10116 sfk_none);
10117
10118 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10119 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10120 declarator = make_call_declarator (declarator, param_list, quals,
10121 VIRT_SPEC_UNSPECIFIED,
10122 REF_QUAL_NONE,
10123 tx_qual,
10124 exception_spec,
10125 /*late_return_type=*/NULL_TREE,
10126 /*requires_clause*/NULL_TREE);
10127 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10128
10129 fco = grokmethod (&return_type_specs,
10130 declarator,
10131 attributes);
10132 if (fco != error_mark_node)
10133 {
10134 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10135 DECL_ARTIFICIAL (fco) = 1;
10136 /* Give the object parameter a different name. */
10137 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10138 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10139 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10140 }
10141 if (template_param_list)
10142 {
10143 fco = finish_member_template_decl (fco);
10144 finish_template_decl (template_param_list);
10145 --parser->num_template_parameter_lists;
10146 }
10147 else if (parser->fully_implicit_function_template_p)
10148 fco = finish_fully_implicit_template (parser, fco);
10149
10150 finish_member_declaration (fco);
10151
10152 obstack_free (&declarator_obstack, p);
10153
10154 return (fco != error_mark_node);
10155 }
10156 }
10157
10158 /* Parse the body of a lambda expression, which is simply
10159
10160 compound-statement
10161
10162 but which requires special handling.
10163 LAMBDA_EXPR is the current representation of the lambda expression. */
10164
10165 static void
10166 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10167 {
10168 bool nested = (current_function_decl != NULL_TREE);
10169 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10170 if (nested)
10171 push_function_context ();
10172 else
10173 /* Still increment function_depth so that we don't GC in the
10174 middle of an expression. */
10175 ++function_depth;
10176 vec<tree> omp_privatization_save;
10177 save_omp_privatization_clauses (omp_privatization_save);
10178 /* Clear this in case we're in the middle of a default argument. */
10179 parser->local_variables_forbidden_p = false;
10180
10181 /* Finish the function call operator
10182 - class_specifier
10183 + late_parsing_for_member
10184 + function_definition_after_declarator
10185 + ctor_initializer_opt_and_function_body */
10186 {
10187 tree fco = lambda_function (lambda_expr);
10188 tree body;
10189 bool done = false;
10190 tree compound_stmt;
10191 tree cap;
10192
10193 /* Let the front end know that we are going to be defining this
10194 function. */
10195 start_preparsed_function (fco,
10196 NULL_TREE,
10197 SF_PRE_PARSED | SF_INCLASS_INLINE);
10198
10199 start_lambda_scope (fco);
10200 body = begin_function_body ();
10201
10202 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10203 goto out;
10204
10205 /* Push the proxies for any explicit captures. */
10206 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10207 cap = TREE_CHAIN (cap))
10208 build_capture_proxy (TREE_PURPOSE (cap));
10209
10210 compound_stmt = begin_compound_stmt (0);
10211
10212 /* 5.1.1.4 of the standard says:
10213 If a lambda-expression does not include a trailing-return-type, it
10214 is as if the trailing-return-type denotes the following type:
10215 * if the compound-statement is of the form
10216 { return attribute-specifier [opt] expression ; }
10217 the type of the returned expression after lvalue-to-rvalue
10218 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10219 (_conv.array_ 4.2), and function-to-pointer conversion
10220 (_conv.func_ 4.3);
10221 * otherwise, void. */
10222
10223 /* In a lambda that has neither a lambda-return-type-clause
10224 nor a deducible form, errors should be reported for return statements
10225 in the body. Since we used void as the placeholder return type, parsing
10226 the body as usual will give such desired behavior. */
10227 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10228 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10229 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10230 {
10231 tree expr = NULL_TREE;
10232 cp_id_kind idk = CP_ID_KIND_NONE;
10233
10234 /* Parse tentatively in case there's more after the initial return
10235 statement. */
10236 cp_parser_parse_tentatively (parser);
10237
10238 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10239
10240 expr = cp_parser_expression (parser, &idk);
10241
10242 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10243 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10244
10245 if (cp_parser_parse_definitely (parser))
10246 {
10247 if (!processing_template_decl)
10248 {
10249 tree type = lambda_return_type (expr);
10250 apply_deduced_return_type (fco, type);
10251 if (type == error_mark_node)
10252 expr = error_mark_node;
10253 }
10254
10255 /* Will get error here if type not deduced yet. */
10256 finish_return_stmt (expr);
10257
10258 done = true;
10259 }
10260 }
10261
10262 if (!done)
10263 {
10264 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10265 cp_parser_label_declaration (parser);
10266 cp_parser_statement_seq_opt (parser, NULL_TREE);
10267 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10268 }
10269
10270 finish_compound_stmt (compound_stmt);
10271
10272 out:
10273 finish_function_body (body);
10274 finish_lambda_scope ();
10275
10276 /* Finish the function and generate code for it if necessary. */
10277 tree fn = finish_function (/*inline*/2);
10278
10279 /* Only expand if the call op is not a template. */
10280 if (!DECL_TEMPLATE_INFO (fco))
10281 expand_or_defer_fn (fn);
10282 }
10283
10284 restore_omp_privatization_clauses (omp_privatization_save);
10285 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10286 if (nested)
10287 pop_function_context();
10288 else
10289 --function_depth;
10290 }
10291
10292 /* Statements [gram.stmt.stmt] */
10293
10294 /* Parse a statement.
10295
10296 statement:
10297 labeled-statement
10298 expression-statement
10299 compound-statement
10300 selection-statement
10301 iteration-statement
10302 jump-statement
10303 declaration-statement
10304 try-block
10305
10306 C++11:
10307
10308 statement:
10309 labeled-statement
10310 attribute-specifier-seq (opt) expression-statement
10311 attribute-specifier-seq (opt) compound-statement
10312 attribute-specifier-seq (opt) selection-statement
10313 attribute-specifier-seq (opt) iteration-statement
10314 attribute-specifier-seq (opt) jump-statement
10315 declaration-statement
10316 attribute-specifier-seq (opt) try-block
10317
10318 TM Extension:
10319
10320 statement:
10321 atomic-statement
10322
10323 IN_COMPOUND is true when the statement is nested inside a
10324 cp_parser_compound_statement; this matters for certain pragmas.
10325
10326 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10327 is a (possibly labeled) if statement which is not enclosed in braces
10328 and has an else clause. This is used to implement -Wparentheses.
10329
10330 CHAIN is a vector of if-else-if conditions. */
10331
10332 static void
10333 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10334 bool in_compound, bool *if_p, vec<tree> *chain)
10335 {
10336 tree statement, std_attrs = NULL_TREE;
10337 cp_token *token;
10338 location_t statement_location, attrs_location;
10339
10340 restart:
10341 if (if_p != NULL)
10342 *if_p = false;
10343 /* There is no statement yet. */
10344 statement = NULL_TREE;
10345
10346 saved_token_sentinel saved_tokens (parser->lexer);
10347 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10348 if (c_dialect_objc ())
10349 /* In obj-c++, seeing '[[' might be the either the beginning of
10350 c++11 attributes, or a nested objc-message-expression. So
10351 let's parse the c++11 attributes tentatively. */
10352 cp_parser_parse_tentatively (parser);
10353 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10354 if (c_dialect_objc ())
10355 {
10356 if (!cp_parser_parse_definitely (parser))
10357 std_attrs = NULL_TREE;
10358 }
10359
10360 /* Peek at the next token. */
10361 token = cp_lexer_peek_token (parser->lexer);
10362 /* Remember the location of the first token in the statement. */
10363 statement_location = token->location;
10364 /* If this is a keyword, then that will often determine what kind of
10365 statement we have. */
10366 if (token->type == CPP_KEYWORD)
10367 {
10368 enum rid keyword = token->keyword;
10369
10370 switch (keyword)
10371 {
10372 case RID_CASE:
10373 case RID_DEFAULT:
10374 /* Looks like a labeled-statement with a case label.
10375 Parse the label, and then use tail recursion to parse
10376 the statement. */
10377 cp_parser_label_for_labeled_statement (parser, std_attrs);
10378 in_compound = false;
10379 goto restart;
10380
10381 case RID_IF:
10382 case RID_SWITCH:
10383 statement = cp_parser_selection_statement (parser, if_p, chain);
10384 break;
10385
10386 case RID_WHILE:
10387 case RID_DO:
10388 case RID_FOR:
10389 statement = cp_parser_iteration_statement (parser, if_p, false);
10390 break;
10391
10392 case RID_CILK_FOR:
10393 if (!flag_cilkplus)
10394 {
10395 error_at (cp_lexer_peek_token (parser->lexer)->location,
10396 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10397 cp_lexer_consume_token (parser->lexer);
10398 statement = error_mark_node;
10399 }
10400 else
10401 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10402 break;
10403
10404 case RID_BREAK:
10405 case RID_CONTINUE:
10406 case RID_RETURN:
10407 case RID_GOTO:
10408 statement = cp_parser_jump_statement (parser);
10409 break;
10410
10411 case RID_CILK_SYNC:
10412 cp_lexer_consume_token (parser->lexer);
10413 if (flag_cilkplus)
10414 {
10415 tree sync_expr = build_cilk_sync ();
10416 SET_EXPR_LOCATION (sync_expr,
10417 token->location);
10418 statement = finish_expr_stmt (sync_expr);
10419 }
10420 else
10421 {
10422 error_at (token->location, "-fcilkplus must be enabled to use"
10423 " %<_Cilk_sync%>");
10424 statement = error_mark_node;
10425 }
10426 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10427 break;
10428
10429 /* Objective-C++ exception-handling constructs. */
10430 case RID_AT_TRY:
10431 case RID_AT_CATCH:
10432 case RID_AT_FINALLY:
10433 case RID_AT_SYNCHRONIZED:
10434 case RID_AT_THROW:
10435 statement = cp_parser_objc_statement (parser);
10436 break;
10437
10438 case RID_TRY:
10439 statement = cp_parser_try_block (parser);
10440 break;
10441
10442 case RID_NAMESPACE:
10443 /* This must be a namespace alias definition. */
10444 cp_parser_declaration_statement (parser);
10445 return;
10446
10447 case RID_TRANSACTION_ATOMIC:
10448 case RID_TRANSACTION_RELAXED:
10449 case RID_SYNCHRONIZED:
10450 case RID_ATOMIC_NOEXCEPT:
10451 case RID_ATOMIC_CANCEL:
10452 statement = cp_parser_transaction (parser, token);
10453 break;
10454 case RID_TRANSACTION_CANCEL:
10455 statement = cp_parser_transaction_cancel (parser);
10456 break;
10457
10458 default:
10459 /* It might be a keyword like `int' that can start a
10460 declaration-statement. */
10461 break;
10462 }
10463 }
10464 else if (token->type == CPP_NAME)
10465 {
10466 /* If the next token is a `:', then we are looking at a
10467 labeled-statement. */
10468 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10469 if (token->type == CPP_COLON)
10470 {
10471 /* Looks like a labeled-statement with an ordinary label.
10472 Parse the label, and then use tail recursion to parse
10473 the statement. */
10474
10475 cp_parser_label_for_labeled_statement (parser, std_attrs);
10476 in_compound = false;
10477 goto restart;
10478 }
10479 }
10480 /* Anything that starts with a `{' must be a compound-statement. */
10481 else if (token->type == CPP_OPEN_BRACE)
10482 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10483 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10484 a statement all its own. */
10485 else if (token->type == CPP_PRAGMA)
10486 {
10487 /* Only certain OpenMP pragmas are attached to statements, and thus
10488 are considered statements themselves. All others are not. In
10489 the context of a compound, accept the pragma as a "statement" and
10490 return so that we can check for a close brace. Otherwise we
10491 require a real statement and must go back and read one. */
10492 if (in_compound)
10493 cp_parser_pragma (parser, pragma_compound, if_p);
10494 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10495 goto restart;
10496 return;
10497 }
10498 else if (token->type == CPP_EOF)
10499 {
10500 cp_parser_error (parser, "expected statement");
10501 return;
10502 }
10503
10504 /* Everything else must be a declaration-statement or an
10505 expression-statement. Try for the declaration-statement
10506 first, unless we are looking at a `;', in which case we know that
10507 we have an expression-statement. */
10508 if (!statement)
10509 {
10510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10511 {
10512 if (std_attrs != NULL_TREE)
10513 {
10514 /* Attributes should be parsed as part of the the
10515 declaration, so let's un-parse them. */
10516 saved_tokens.rollback();
10517 std_attrs = NULL_TREE;
10518 }
10519
10520 cp_parser_parse_tentatively (parser);
10521 /* Try to parse the declaration-statement. */
10522 cp_parser_declaration_statement (parser);
10523 /* If that worked, we're done. */
10524 if (cp_parser_parse_definitely (parser))
10525 return;
10526 }
10527 /* Look for an expression-statement instead. */
10528 statement = cp_parser_expression_statement (parser, in_statement_expr);
10529 }
10530
10531 /* Set the line number for the statement. */
10532 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10533 SET_EXPR_LOCATION (statement, statement_location);
10534
10535 /* Note that for now, we don't do anything with c++11 statements
10536 parsed at this level. */
10537 if (std_attrs != NULL_TREE)
10538 warning_at (attrs_location,
10539 OPT_Wattributes,
10540 "attributes at the beginning of statement are ignored");
10541 }
10542
10543 /* Parse the label for a labeled-statement, i.e.
10544
10545 identifier :
10546 case constant-expression :
10547 default :
10548
10549 GNU Extension:
10550 case constant-expression ... constant-expression : statement
10551
10552 When a label is parsed without errors, the label is added to the
10553 parse tree by the finish_* functions, so this function doesn't
10554 have to return the label. */
10555
10556 static void
10557 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10558 {
10559 cp_token *token;
10560 tree label = NULL_TREE;
10561 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10562
10563 /* The next token should be an identifier. */
10564 token = cp_lexer_peek_token (parser->lexer);
10565 if (token->type != CPP_NAME
10566 && token->type != CPP_KEYWORD)
10567 {
10568 cp_parser_error (parser, "expected labeled-statement");
10569 return;
10570 }
10571
10572 parser->colon_corrects_to_scope_p = false;
10573 switch (token->keyword)
10574 {
10575 case RID_CASE:
10576 {
10577 tree expr, expr_hi;
10578 cp_token *ellipsis;
10579
10580 /* Consume the `case' token. */
10581 cp_lexer_consume_token (parser->lexer);
10582 /* Parse the constant-expression. */
10583 expr = cp_parser_constant_expression (parser);
10584 if (check_for_bare_parameter_packs (expr))
10585 expr = error_mark_node;
10586
10587 ellipsis = cp_lexer_peek_token (parser->lexer);
10588 if (ellipsis->type == CPP_ELLIPSIS)
10589 {
10590 /* Consume the `...' token. */
10591 cp_lexer_consume_token (parser->lexer);
10592 expr_hi = cp_parser_constant_expression (parser);
10593 if (check_for_bare_parameter_packs (expr_hi))
10594 expr_hi = error_mark_node;
10595
10596 /* We don't need to emit warnings here, as the common code
10597 will do this for us. */
10598 }
10599 else
10600 expr_hi = NULL_TREE;
10601
10602 if (parser->in_switch_statement_p)
10603 finish_case_label (token->location, expr, expr_hi);
10604 else
10605 error_at (token->location,
10606 "case label %qE not within a switch statement",
10607 expr);
10608 }
10609 break;
10610
10611 case RID_DEFAULT:
10612 /* Consume the `default' token. */
10613 cp_lexer_consume_token (parser->lexer);
10614
10615 if (parser->in_switch_statement_p)
10616 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10617 else
10618 error_at (token->location, "case label not within a switch statement");
10619 break;
10620
10621 default:
10622 /* Anything else must be an ordinary label. */
10623 label = finish_label_stmt (cp_parser_identifier (parser));
10624 break;
10625 }
10626
10627 /* Require the `:' token. */
10628 cp_parser_require (parser, CPP_COLON, RT_COLON);
10629
10630 /* An ordinary label may optionally be followed by attributes.
10631 However, this is only permitted if the attributes are then
10632 followed by a semicolon. This is because, for backward
10633 compatibility, when parsing
10634 lab: __attribute__ ((unused)) int i;
10635 we want the attribute to attach to "i", not "lab". */
10636 if (label != NULL_TREE
10637 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10638 {
10639 tree attrs;
10640 cp_parser_parse_tentatively (parser);
10641 attrs = cp_parser_gnu_attributes_opt (parser);
10642 if (attrs == NULL_TREE
10643 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10644 cp_parser_abort_tentative_parse (parser);
10645 else if (!cp_parser_parse_definitely (parser))
10646 ;
10647 else
10648 attributes = chainon (attributes, attrs);
10649 }
10650
10651 if (attributes != NULL_TREE)
10652 cplus_decl_attributes (&label, attributes, 0);
10653
10654 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10655 }
10656
10657 /* Parse an expression-statement.
10658
10659 expression-statement:
10660 expression [opt] ;
10661
10662 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10663 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10664 indicates whether this expression-statement is part of an
10665 expression statement. */
10666
10667 static tree
10668 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10669 {
10670 tree statement = NULL_TREE;
10671 cp_token *token = cp_lexer_peek_token (parser->lexer);
10672
10673 /* If the next token is a ';', then there is no expression
10674 statement. */
10675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10676 {
10677 statement = cp_parser_expression (parser);
10678 if (statement == error_mark_node
10679 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10680 {
10681 cp_parser_skip_to_end_of_block_or_statement (parser);
10682 return error_mark_node;
10683 }
10684 }
10685
10686 /* Give a helpful message for "A<T>::type t;" and the like. */
10687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10688 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10689 {
10690 if (TREE_CODE (statement) == SCOPE_REF)
10691 error_at (token->location, "need %<typename%> before %qE because "
10692 "%qT is a dependent scope",
10693 statement, TREE_OPERAND (statement, 0));
10694 else if (is_overloaded_fn (statement)
10695 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10696 {
10697 /* A::A a; */
10698 tree fn = get_first_fn (statement);
10699 error_at (token->location,
10700 "%<%T::%D%> names the constructor, not the type",
10701 DECL_CONTEXT (fn), DECL_NAME (fn));
10702 }
10703 }
10704
10705 /* Consume the final `;'. */
10706 cp_parser_consume_semicolon_at_end_of_statement (parser);
10707
10708 if (in_statement_expr
10709 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10710 /* This is the final expression statement of a statement
10711 expression. */
10712 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10713 else if (statement)
10714 statement = finish_expr_stmt (statement);
10715
10716 return statement;
10717 }
10718
10719 /* Parse a compound-statement.
10720
10721 compound-statement:
10722 { statement-seq [opt] }
10723
10724 GNU extension:
10725
10726 compound-statement:
10727 { label-declaration-seq [opt] statement-seq [opt] }
10728
10729 label-declaration-seq:
10730 label-declaration
10731 label-declaration-seq label-declaration
10732
10733 Returns a tree representing the statement. */
10734
10735 static tree
10736 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10737 int bcs_flags, bool function_body)
10738 {
10739 tree compound_stmt;
10740
10741 /* Consume the `{'. */
10742 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10743 return error_mark_node;
10744 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10745 && !function_body && cxx_dialect < cxx14)
10746 pedwarn (input_location, OPT_Wpedantic,
10747 "compound-statement in constexpr function");
10748 /* Begin the compound-statement. */
10749 compound_stmt = begin_compound_stmt (bcs_flags);
10750 /* If the next keyword is `__label__' we have a label declaration. */
10751 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10752 cp_parser_label_declaration (parser);
10753 /* Parse an (optional) statement-seq. */
10754 cp_parser_statement_seq_opt (parser, in_statement_expr);
10755 /* Finish the compound-statement. */
10756 finish_compound_stmt (compound_stmt);
10757 /* Consume the `}'. */
10758 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10759
10760 return compound_stmt;
10761 }
10762
10763 /* Parse an (optional) statement-seq.
10764
10765 statement-seq:
10766 statement
10767 statement-seq [opt] statement */
10768
10769 static void
10770 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10771 {
10772 /* Scan statements until there aren't any more. */
10773 while (true)
10774 {
10775 cp_token *token = cp_lexer_peek_token (parser->lexer);
10776
10777 /* If we are looking at a `}', then we have run out of
10778 statements; the same is true if we have reached the end
10779 of file, or have stumbled upon a stray '@end'. */
10780 if (token->type == CPP_CLOSE_BRACE
10781 || token->type == CPP_EOF
10782 || token->type == CPP_PRAGMA_EOL
10783 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10784 break;
10785
10786 /* If we are in a compound statement and find 'else' then
10787 something went wrong. */
10788 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10789 {
10790 if (parser->in_statement & IN_IF_STMT)
10791 break;
10792 else
10793 {
10794 token = cp_lexer_consume_token (parser->lexer);
10795 error_at (token->location, "%<else%> without a previous %<if%>");
10796 }
10797 }
10798
10799 /* Parse the statement. */
10800 cp_parser_statement (parser, in_statement_expr, true, NULL);
10801 }
10802 }
10803
10804 /* Parse a selection-statement.
10805
10806 selection-statement:
10807 if ( condition ) statement
10808 if ( condition ) statement else statement
10809 switch ( condition ) statement
10810
10811 Returns the new IF_STMT or SWITCH_STMT.
10812
10813 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10814 is a (possibly labeled) if statement which is not enclosed in
10815 braces and has an else clause. This is used to implement
10816 -Wparentheses.
10817
10818 CHAIN is a vector of if-else-if conditions. This is used to implement
10819 -Wduplicated-cond. */
10820
10821 static tree
10822 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10823 vec<tree> *chain)
10824 {
10825 cp_token *token;
10826 enum rid keyword;
10827 token_indent_info guard_tinfo;
10828
10829 if (if_p != NULL)
10830 *if_p = false;
10831
10832 /* Peek at the next token. */
10833 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10834 guard_tinfo = get_token_indent_info (token);
10835
10836 /* See what kind of keyword it is. */
10837 keyword = token->keyword;
10838 switch (keyword)
10839 {
10840 case RID_IF:
10841 case RID_SWITCH:
10842 {
10843 tree statement;
10844 tree condition;
10845
10846 /* Look for the `('. */
10847 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10848 {
10849 cp_parser_skip_to_end_of_statement (parser);
10850 return error_mark_node;
10851 }
10852
10853 /* Begin the selection-statement. */
10854 if (keyword == RID_IF)
10855 statement = begin_if_stmt ();
10856 else
10857 statement = begin_switch_stmt ();
10858
10859 /* Parse the condition. */
10860 condition = cp_parser_condition (parser);
10861 /* Look for the `)'. */
10862 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10863 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10864 /*consume_paren=*/true);
10865
10866 if (keyword == RID_IF)
10867 {
10868 bool nested_if;
10869 unsigned char in_statement;
10870
10871 /* Add the condition. */
10872 finish_if_stmt_cond (condition, statement);
10873
10874 if (warn_duplicated_cond)
10875 warn_duplicated_cond_add_or_warn (token->location, condition,
10876 &chain);
10877
10878 /* Parse the then-clause. */
10879 in_statement = parser->in_statement;
10880 parser->in_statement |= IN_IF_STMT;
10881 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10882 guard_tinfo);
10883 parser->in_statement = in_statement;
10884
10885 finish_then_clause (statement);
10886
10887 /* If the next token is `else', parse the else-clause. */
10888 if (cp_lexer_next_token_is_keyword (parser->lexer,
10889 RID_ELSE))
10890 {
10891 guard_tinfo
10892 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10893 /* Consume the `else' keyword. */
10894 cp_lexer_consume_token (parser->lexer);
10895 if (warn_duplicated_cond)
10896 {
10897 if (cp_lexer_next_token_is_keyword (parser->lexer,
10898 RID_IF)
10899 && chain == NULL)
10900 {
10901 /* We've got "if (COND) else if (COND2)". Start
10902 the condition chain and add COND as the first
10903 element. */
10904 chain = new vec<tree> ();
10905 if (!CONSTANT_CLASS_P (condition)
10906 && !TREE_SIDE_EFFECTS (condition))
10907 {
10908 /* Wrap it in a NOP_EXPR so that we can set the
10909 location of the condition. */
10910 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10911 condition);
10912 SET_EXPR_LOCATION (e, token->location);
10913 chain->safe_push (e);
10914 }
10915 }
10916 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10917 RID_IF))
10918 {
10919 /* This is if-else without subsequent if. Zap the
10920 condition chain; we would have already warned at
10921 this point. */
10922 delete chain;
10923 chain = NULL;
10924 }
10925 }
10926 begin_else_clause (statement);
10927 /* Parse the else-clause. */
10928 cp_parser_implicitly_scoped_statement (parser, NULL,
10929 guard_tinfo, chain);
10930
10931 finish_else_clause (statement);
10932
10933 /* If we are currently parsing a then-clause, then
10934 IF_P will not be NULL. We set it to true to
10935 indicate that this if statement has an else clause.
10936 This may trigger the Wparentheses warning below
10937 when we get back up to the parent if statement. */
10938 if (if_p != NULL)
10939 *if_p = true;
10940 }
10941 else
10942 {
10943 /* This if statement does not have an else clause. If
10944 NESTED_IF is true, then the then-clause has an if
10945 statement which does have an else clause. We warn
10946 about the potential ambiguity. */
10947 if (nested_if)
10948 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
10949 "suggest explicit braces to avoid ambiguous"
10950 " %<else%>");
10951 if (warn_duplicated_cond)
10952 {
10953 /* We don't need the condition chain anymore. */
10954 delete chain;
10955 chain = NULL;
10956 }
10957 }
10958
10959 /* Now we're all done with the if-statement. */
10960 finish_if_stmt (statement);
10961 }
10962 else
10963 {
10964 bool in_switch_statement_p;
10965 unsigned char in_statement;
10966
10967 /* Add the condition. */
10968 finish_switch_cond (condition, statement);
10969
10970 /* Parse the body of the switch-statement. */
10971 in_switch_statement_p = parser->in_switch_statement_p;
10972 in_statement = parser->in_statement;
10973 parser->in_switch_statement_p = true;
10974 parser->in_statement |= IN_SWITCH_STMT;
10975 cp_parser_implicitly_scoped_statement (parser, if_p,
10976 guard_tinfo);
10977 parser->in_switch_statement_p = in_switch_statement_p;
10978 parser->in_statement = in_statement;
10979
10980 /* Now we're all done with the switch-statement. */
10981 finish_switch_stmt (statement);
10982 }
10983
10984 return statement;
10985 }
10986 break;
10987
10988 default:
10989 cp_parser_error (parser, "expected selection-statement");
10990 return error_mark_node;
10991 }
10992 }
10993
10994 /* Parse a condition.
10995
10996 condition:
10997 expression
10998 type-specifier-seq declarator = initializer-clause
10999 type-specifier-seq declarator braced-init-list
11000
11001 GNU Extension:
11002
11003 condition:
11004 type-specifier-seq declarator asm-specification [opt]
11005 attributes [opt] = assignment-expression
11006
11007 Returns the expression that should be tested. */
11008
11009 static tree
11010 cp_parser_condition (cp_parser* parser)
11011 {
11012 cp_decl_specifier_seq type_specifiers;
11013 const char *saved_message;
11014 int declares_class_or_enum;
11015
11016 /* Try the declaration first. */
11017 cp_parser_parse_tentatively (parser);
11018 /* New types are not allowed in the type-specifier-seq for a
11019 condition. */
11020 saved_message = parser->type_definition_forbidden_message;
11021 parser->type_definition_forbidden_message
11022 = G_("types may not be defined in conditions");
11023 /* Parse the type-specifier-seq. */
11024 cp_parser_decl_specifier_seq (parser,
11025 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11026 &type_specifiers,
11027 &declares_class_or_enum);
11028 /* Restore the saved message. */
11029 parser->type_definition_forbidden_message = saved_message;
11030 /* If all is well, we might be looking at a declaration. */
11031 if (!cp_parser_error_occurred (parser))
11032 {
11033 tree decl;
11034 tree asm_specification;
11035 tree attributes;
11036 cp_declarator *declarator;
11037 tree initializer = NULL_TREE;
11038
11039 /* Parse the declarator. */
11040 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11041 /*ctor_dtor_or_conv_p=*/NULL,
11042 /*parenthesized_p=*/NULL,
11043 /*member_p=*/false,
11044 /*friend_p=*/false);
11045 /* Parse the attributes. */
11046 attributes = cp_parser_attributes_opt (parser);
11047 /* Parse the asm-specification. */
11048 asm_specification = cp_parser_asm_specification_opt (parser);
11049 /* If the next token is not an `=' or '{', then we might still be
11050 looking at an expression. For example:
11051
11052 if (A(a).x)
11053
11054 looks like a decl-specifier-seq and a declarator -- but then
11055 there is no `=', so this is an expression. */
11056 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11057 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11058 cp_parser_simulate_error (parser);
11059
11060 /* If we did see an `=' or '{', then we are looking at a declaration
11061 for sure. */
11062 if (cp_parser_parse_definitely (parser))
11063 {
11064 tree pushed_scope;
11065 bool non_constant_p;
11066 bool flags = LOOKUP_ONLYCONVERTING;
11067
11068 /* Create the declaration. */
11069 decl = start_decl (declarator, &type_specifiers,
11070 /*initialized_p=*/true,
11071 attributes, /*prefix_attributes=*/NULL_TREE,
11072 &pushed_scope);
11073
11074 /* Parse the initializer. */
11075 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11076 {
11077 initializer = cp_parser_braced_list (parser, &non_constant_p);
11078 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11079 flags = 0;
11080 }
11081 else
11082 {
11083 /* Consume the `='. */
11084 cp_parser_require (parser, CPP_EQ, RT_EQ);
11085 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11086 }
11087 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11088 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11089
11090 /* Process the initializer. */
11091 cp_finish_decl (decl,
11092 initializer, !non_constant_p,
11093 asm_specification,
11094 flags);
11095
11096 if (pushed_scope)
11097 pop_scope (pushed_scope);
11098
11099 return convert_from_reference (decl);
11100 }
11101 }
11102 /* If we didn't even get past the declarator successfully, we are
11103 definitely not looking at a declaration. */
11104 else
11105 cp_parser_abort_tentative_parse (parser);
11106
11107 /* Otherwise, we are looking at an expression. */
11108 return cp_parser_expression (parser);
11109 }
11110
11111 /* Parses a for-statement or range-for-statement until the closing ')',
11112 not included. */
11113
11114 static tree
11115 cp_parser_for (cp_parser *parser, bool ivdep)
11116 {
11117 tree init, scope, decl;
11118 bool is_range_for;
11119
11120 /* Begin the for-statement. */
11121 scope = begin_for_scope (&init);
11122
11123 /* Parse the initialization. */
11124 is_range_for = cp_parser_for_init_statement (parser, &decl);
11125
11126 if (is_range_for)
11127 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11128 else
11129 return cp_parser_c_for (parser, scope, init, ivdep);
11130 }
11131
11132 static tree
11133 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11134 {
11135 /* Normal for loop */
11136 tree condition = NULL_TREE;
11137 tree expression = NULL_TREE;
11138 tree stmt;
11139
11140 stmt = begin_for_stmt (scope, init);
11141 /* The for-init-statement has already been parsed in
11142 cp_parser_for_init_statement, so no work is needed here. */
11143 finish_for_init_stmt (stmt);
11144
11145 /* If there's a condition, process it. */
11146 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11147 condition = cp_parser_condition (parser);
11148 else if (ivdep)
11149 {
11150 cp_parser_error (parser, "missing loop condition in loop with "
11151 "%<GCC ivdep%> pragma");
11152 condition = error_mark_node;
11153 }
11154 finish_for_cond (condition, stmt, ivdep);
11155 /* Look for the `;'. */
11156 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11157
11158 /* If there's an expression, process it. */
11159 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11160 expression = cp_parser_expression (parser);
11161 finish_for_expr (expression, stmt);
11162
11163 return stmt;
11164 }
11165
11166 /* Tries to parse a range-based for-statement:
11167
11168 range-based-for:
11169 decl-specifier-seq declarator : expression
11170
11171 The decl-specifier-seq declarator and the `:' are already parsed by
11172 cp_parser_for_init_statement. If processing_template_decl it returns a
11173 newly created RANGE_FOR_STMT; if not, it is converted to a
11174 regular FOR_STMT. */
11175
11176 static tree
11177 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11178 bool ivdep)
11179 {
11180 tree stmt, range_expr;
11181
11182 /* Get the range declaration momentarily out of the way so that
11183 the range expression doesn't clash with it. */
11184 if (range_decl != error_mark_node)
11185 pop_binding (DECL_NAME (range_decl), range_decl);
11186
11187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11188 {
11189 bool expr_non_constant_p;
11190 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11191 }
11192 else
11193 range_expr = cp_parser_expression (parser);
11194
11195 /* Put the range declaration back into scope. */
11196 if (range_decl != error_mark_node)
11197 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
11198
11199 /* If in template, STMT is converted to a normal for-statement
11200 at instantiation. If not, it is done just ahead. */
11201 if (processing_template_decl)
11202 {
11203 if (check_for_bare_parameter_packs (range_expr))
11204 range_expr = error_mark_node;
11205 stmt = begin_range_for_stmt (scope, init);
11206 if (ivdep)
11207 RANGE_FOR_IVDEP (stmt) = 1;
11208 finish_range_for_decl (stmt, range_decl, range_expr);
11209 if (!type_dependent_expression_p (range_expr)
11210 /* do_auto_deduction doesn't mess with template init-lists. */
11211 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11212 do_range_for_auto_deduction (range_decl, range_expr);
11213 }
11214 else
11215 {
11216 stmt = begin_for_stmt (scope, init);
11217 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11218 }
11219 return stmt;
11220 }
11221
11222 /* Subroutine of cp_convert_range_for: given the initializer expression,
11223 builds up the range temporary. */
11224
11225 static tree
11226 build_range_temp (tree range_expr)
11227 {
11228 tree range_type, range_temp;
11229
11230 /* Find out the type deduced by the declaration
11231 `auto &&__range = range_expr'. */
11232 range_type = cp_build_reference_type (make_auto (), true);
11233 range_type = do_auto_deduction (range_type, range_expr,
11234 type_uses_auto (range_type));
11235
11236 /* Create the __range variable. */
11237 range_temp = build_decl (input_location, VAR_DECL,
11238 get_identifier ("__for_range"), range_type);
11239 TREE_USED (range_temp) = 1;
11240 DECL_ARTIFICIAL (range_temp) = 1;
11241
11242 return range_temp;
11243 }
11244
11245 /* Used by cp_parser_range_for in template context: we aren't going to
11246 do a full conversion yet, but we still need to resolve auto in the
11247 type of the for-range-declaration if present. This is basically
11248 a shortcut version of cp_convert_range_for. */
11249
11250 static void
11251 do_range_for_auto_deduction (tree decl, tree range_expr)
11252 {
11253 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11254 if (auto_node)
11255 {
11256 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11257 range_temp = convert_from_reference (build_range_temp (range_expr));
11258 iter_type = (cp_parser_perform_range_for_lookup
11259 (range_temp, &begin_dummy, &end_dummy));
11260 if (iter_type)
11261 {
11262 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11263 iter_type);
11264 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11265 tf_warning_or_error);
11266 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11267 iter_decl, auto_node);
11268 }
11269 }
11270 }
11271
11272 /* Converts a range-based for-statement into a normal
11273 for-statement, as per the definition.
11274
11275 for (RANGE_DECL : RANGE_EXPR)
11276 BLOCK
11277
11278 should be equivalent to:
11279
11280 {
11281 auto &&__range = RANGE_EXPR;
11282 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11283 __begin != __end;
11284 ++__begin)
11285 {
11286 RANGE_DECL = *__begin;
11287 BLOCK
11288 }
11289 }
11290
11291 If RANGE_EXPR is an array:
11292 BEGIN_EXPR = __range
11293 END_EXPR = __range + ARRAY_SIZE(__range)
11294 Else if RANGE_EXPR has a member 'begin' or 'end':
11295 BEGIN_EXPR = __range.begin()
11296 END_EXPR = __range.end()
11297 Else:
11298 BEGIN_EXPR = begin(__range)
11299 END_EXPR = end(__range);
11300
11301 If __range has a member 'begin' but not 'end', or vice versa, we must
11302 still use the second alternative (it will surely fail, however).
11303 When calling begin()/end() in the third alternative we must use
11304 argument dependent lookup, but always considering 'std' as an associated
11305 namespace. */
11306
11307 tree
11308 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11309 bool ivdep)
11310 {
11311 tree begin, end;
11312 tree iter_type, begin_expr, end_expr;
11313 tree condition, expression;
11314
11315 if (range_decl == error_mark_node || range_expr == error_mark_node)
11316 /* If an error happened previously do nothing or else a lot of
11317 unhelpful errors would be issued. */
11318 begin_expr = end_expr = iter_type = error_mark_node;
11319 else
11320 {
11321 tree range_temp;
11322
11323 if (VAR_P (range_expr)
11324 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11325 /* Can't bind a reference to an array of runtime bound. */
11326 range_temp = range_expr;
11327 else
11328 {
11329 range_temp = build_range_temp (range_expr);
11330 pushdecl (range_temp);
11331 cp_finish_decl (range_temp, range_expr,
11332 /*is_constant_init*/false, NULL_TREE,
11333 LOOKUP_ONLYCONVERTING);
11334 range_temp = convert_from_reference (range_temp);
11335 }
11336 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11337 &begin_expr, &end_expr);
11338 }
11339
11340 /* The new for initialization statement. */
11341 begin = build_decl (input_location, VAR_DECL,
11342 get_identifier ("__for_begin"), iter_type);
11343 TREE_USED (begin) = 1;
11344 DECL_ARTIFICIAL (begin) = 1;
11345 pushdecl (begin);
11346 cp_finish_decl (begin, begin_expr,
11347 /*is_constant_init*/false, NULL_TREE,
11348 LOOKUP_ONLYCONVERTING);
11349
11350 if (cxx_dialect >= cxx1z)
11351 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11352 end = build_decl (input_location, VAR_DECL,
11353 get_identifier ("__for_end"), iter_type);
11354 TREE_USED (end) = 1;
11355 DECL_ARTIFICIAL (end) = 1;
11356 pushdecl (end);
11357 cp_finish_decl (end, end_expr,
11358 /*is_constant_init*/false, NULL_TREE,
11359 LOOKUP_ONLYCONVERTING);
11360
11361 finish_for_init_stmt (statement);
11362
11363 /* The new for condition. */
11364 condition = build_x_binary_op (input_location, NE_EXPR,
11365 begin, ERROR_MARK,
11366 end, ERROR_MARK,
11367 NULL, tf_warning_or_error);
11368 finish_for_cond (condition, statement, ivdep);
11369
11370 /* The new increment expression. */
11371 expression = finish_unary_op_expr (input_location,
11372 PREINCREMENT_EXPR, begin,
11373 tf_warning_or_error);
11374 finish_for_expr (expression, statement);
11375
11376 /* The declaration is initialized with *__begin inside the loop body. */
11377 cp_finish_decl (range_decl,
11378 build_x_indirect_ref (input_location, begin, RO_NULL,
11379 tf_warning_or_error),
11380 /*is_constant_init*/false, NULL_TREE,
11381 LOOKUP_ONLYCONVERTING);
11382
11383 return statement;
11384 }
11385
11386 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11387 We need to solve both at the same time because the method used
11388 depends on the existence of members begin or end.
11389 Returns the type deduced for the iterator expression. */
11390
11391 static tree
11392 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11393 {
11394 if (error_operand_p (range))
11395 {
11396 *begin = *end = error_mark_node;
11397 return error_mark_node;
11398 }
11399
11400 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11401 {
11402 error ("range-based %<for%> expression of type %qT "
11403 "has incomplete type", TREE_TYPE (range));
11404 *begin = *end = error_mark_node;
11405 return error_mark_node;
11406 }
11407 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11408 {
11409 /* If RANGE is an array, we will use pointer arithmetic. */
11410 *begin = decay_conversion (range, tf_warning_or_error);
11411 *end = build_binary_op (input_location, PLUS_EXPR,
11412 range,
11413 array_type_nelts_top (TREE_TYPE (range)),
11414 0);
11415 return TREE_TYPE (*begin);
11416 }
11417 else
11418 {
11419 /* If it is not an array, we must do a bit of magic. */
11420 tree id_begin, id_end;
11421 tree member_begin, member_end;
11422
11423 *begin = *end = error_mark_node;
11424
11425 id_begin = get_identifier ("begin");
11426 id_end = get_identifier ("end");
11427 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11428 /*protect=*/2, /*want_type=*/false,
11429 tf_warning_or_error);
11430 member_end = lookup_member (TREE_TYPE (range), id_end,
11431 /*protect=*/2, /*want_type=*/false,
11432 tf_warning_or_error);
11433
11434 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11435 {
11436 /* Use the member functions. */
11437 if (member_begin != NULL_TREE)
11438 *begin = cp_parser_range_for_member_function (range, id_begin);
11439 else
11440 error ("range-based %<for%> expression of type %qT has an "
11441 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11442
11443 if (member_end != NULL_TREE)
11444 *end = cp_parser_range_for_member_function (range, id_end);
11445 else
11446 error ("range-based %<for%> expression of type %qT has a "
11447 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11448 }
11449 else
11450 {
11451 /* Use global functions with ADL. */
11452 vec<tree, va_gc> *vec;
11453 vec = make_tree_vector ();
11454
11455 vec_safe_push (vec, range);
11456
11457 member_begin = perform_koenig_lookup (id_begin, vec,
11458 tf_warning_or_error);
11459 *begin = finish_call_expr (member_begin, &vec, false, true,
11460 tf_warning_or_error);
11461 member_end = perform_koenig_lookup (id_end, vec,
11462 tf_warning_or_error);
11463 *end = finish_call_expr (member_end, &vec, false, true,
11464 tf_warning_or_error);
11465
11466 release_tree_vector (vec);
11467 }
11468
11469 /* Last common checks. */
11470 if (*begin == error_mark_node || *end == error_mark_node)
11471 {
11472 /* If one of the expressions is an error do no more checks. */
11473 *begin = *end = error_mark_node;
11474 return error_mark_node;
11475 }
11476 else if (type_dependent_expression_p (*begin)
11477 || type_dependent_expression_p (*end))
11478 /* Can happen, when, eg, in a template context, Koenig lookup
11479 can't resolve begin/end (c++/58503). */
11480 return NULL_TREE;
11481 else
11482 {
11483 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11484 /* The unqualified type of the __begin and __end temporaries should
11485 be the same, as required by the multiple auto declaration. */
11486 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11487 {
11488 if (cxx_dialect >= cxx1z
11489 && (build_x_binary_op (input_location, NE_EXPR,
11490 *begin, ERROR_MARK,
11491 *end, ERROR_MARK,
11492 NULL, tf_none)
11493 != error_mark_node))
11494 /* P0184R0 allows __begin and __end to have different types,
11495 but make sure they are comparable so we can give a better
11496 diagnostic. */;
11497 else
11498 error ("inconsistent begin/end types in range-based %<for%> "
11499 "statement: %qT and %qT",
11500 TREE_TYPE (*begin), TREE_TYPE (*end));
11501 }
11502 return iter_type;
11503 }
11504 }
11505 }
11506
11507 /* Helper function for cp_parser_perform_range_for_lookup.
11508 Builds a tree for RANGE.IDENTIFIER(). */
11509
11510 static tree
11511 cp_parser_range_for_member_function (tree range, tree identifier)
11512 {
11513 tree member, res;
11514 vec<tree, va_gc> *vec;
11515
11516 member = finish_class_member_access_expr (range, identifier,
11517 false, tf_warning_or_error);
11518 if (member == error_mark_node)
11519 return error_mark_node;
11520
11521 vec = make_tree_vector ();
11522 res = finish_call_expr (member, &vec,
11523 /*disallow_virtual=*/false,
11524 /*koenig_p=*/false,
11525 tf_warning_or_error);
11526 release_tree_vector (vec);
11527 return res;
11528 }
11529
11530 /* Parse an iteration-statement.
11531
11532 iteration-statement:
11533 while ( condition ) statement
11534 do statement while ( expression ) ;
11535 for ( for-init-statement condition [opt] ; expression [opt] )
11536 statement
11537
11538 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11539
11540 static tree
11541 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11542 {
11543 cp_token *token;
11544 enum rid keyword;
11545 tree statement;
11546 unsigned char in_statement;
11547 token_indent_info guard_tinfo;
11548
11549 /* Peek at the next token. */
11550 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11551 if (!token)
11552 return error_mark_node;
11553
11554 guard_tinfo = get_token_indent_info (token);
11555
11556 /* Remember whether or not we are already within an iteration
11557 statement. */
11558 in_statement = parser->in_statement;
11559
11560 /* See what kind of keyword it is. */
11561 keyword = token->keyword;
11562 switch (keyword)
11563 {
11564 case RID_WHILE:
11565 {
11566 tree condition;
11567
11568 /* Begin the while-statement. */
11569 statement = begin_while_stmt ();
11570 /* Look for the `('. */
11571 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11572 /* Parse the condition. */
11573 condition = cp_parser_condition (parser);
11574 finish_while_stmt_cond (condition, statement, ivdep);
11575 /* Look for the `)'. */
11576 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11577 /* Parse the dependent statement. */
11578 parser->in_statement = IN_ITERATION_STMT;
11579 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11580 parser->in_statement = in_statement;
11581 /* We're done with the while-statement. */
11582 finish_while_stmt (statement);
11583 }
11584 break;
11585
11586 case RID_DO:
11587 {
11588 tree expression;
11589
11590 /* Begin the do-statement. */
11591 statement = begin_do_stmt ();
11592 /* Parse the body of the do-statement. */
11593 parser->in_statement = IN_ITERATION_STMT;
11594 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11595 parser->in_statement = in_statement;
11596 finish_do_body (statement);
11597 /* Look for the `while' keyword. */
11598 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11599 /* Look for the `('. */
11600 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11601 /* Parse the expression. */
11602 expression = cp_parser_expression (parser);
11603 /* We're done with the do-statement. */
11604 finish_do_stmt (expression, statement, ivdep);
11605 /* Look for the `)'. */
11606 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11607 /* Look for the `;'. */
11608 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11609 }
11610 break;
11611
11612 case RID_FOR:
11613 {
11614 /* Look for the `('. */
11615 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11616
11617 statement = cp_parser_for (parser, ivdep);
11618
11619 /* Look for the `)'. */
11620 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11621
11622 /* Parse the body of the for-statement. */
11623 parser->in_statement = IN_ITERATION_STMT;
11624 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11625 parser->in_statement = in_statement;
11626
11627 /* We're done with the for-statement. */
11628 finish_for_stmt (statement);
11629 }
11630 break;
11631
11632 default:
11633 cp_parser_error (parser, "expected iteration-statement");
11634 statement = error_mark_node;
11635 break;
11636 }
11637
11638 return statement;
11639 }
11640
11641 /* Parse a for-init-statement or the declarator of a range-based-for.
11642 Returns true if a range-based-for declaration is seen.
11643
11644 for-init-statement:
11645 expression-statement
11646 simple-declaration */
11647
11648 static bool
11649 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11650 {
11651 /* If the next token is a `;', then we have an empty
11652 expression-statement. Grammatically, this is also a
11653 simple-declaration, but an invalid one, because it does not
11654 declare anything. Therefore, if we did not handle this case
11655 specially, we would issue an error message about an invalid
11656 declaration. */
11657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11658 {
11659 bool is_range_for = false;
11660 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11661
11662 /* A colon is used in range-based for. */
11663 parser->colon_corrects_to_scope_p = false;
11664
11665 /* We're going to speculatively look for a declaration, falling back
11666 to an expression, if necessary. */
11667 cp_parser_parse_tentatively (parser);
11668 /* Parse the declaration. */
11669 cp_parser_simple_declaration (parser,
11670 /*function_definition_allowed_p=*/false,
11671 decl);
11672 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11673 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11674 {
11675 /* It is a range-for, consume the ':' */
11676 cp_lexer_consume_token (parser->lexer);
11677 is_range_for = true;
11678 if (cxx_dialect < cxx11)
11679 {
11680 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11681 "range-based %<for%> loops only available with "
11682 "-std=c++11 or -std=gnu++11");
11683 *decl = error_mark_node;
11684 }
11685 }
11686 else
11687 /* The ';' is not consumed yet because we told
11688 cp_parser_simple_declaration not to. */
11689 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11690
11691 if (cp_parser_parse_definitely (parser))
11692 return is_range_for;
11693 /* If the tentative parse failed, then we shall need to look for an
11694 expression-statement. */
11695 }
11696 /* If we are here, it is an expression-statement. */
11697 cp_parser_expression_statement (parser, NULL_TREE);
11698 return false;
11699 }
11700
11701 /* Parse a jump-statement.
11702
11703 jump-statement:
11704 break ;
11705 continue ;
11706 return expression [opt] ;
11707 return braced-init-list ;
11708 goto identifier ;
11709
11710 GNU extension:
11711
11712 jump-statement:
11713 goto * expression ;
11714
11715 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11716
11717 static tree
11718 cp_parser_jump_statement (cp_parser* parser)
11719 {
11720 tree statement = error_mark_node;
11721 cp_token *token;
11722 enum rid keyword;
11723 unsigned char in_statement;
11724
11725 /* Peek at the next token. */
11726 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11727 if (!token)
11728 return error_mark_node;
11729
11730 /* See what kind of keyword it is. */
11731 keyword = token->keyword;
11732 switch (keyword)
11733 {
11734 case RID_BREAK:
11735 in_statement = parser->in_statement & ~IN_IF_STMT;
11736 switch (in_statement)
11737 {
11738 case 0:
11739 error_at (token->location, "break statement not within loop or switch");
11740 break;
11741 default:
11742 gcc_assert ((in_statement & IN_SWITCH_STMT)
11743 || in_statement == IN_ITERATION_STMT);
11744 statement = finish_break_stmt ();
11745 if (in_statement == IN_ITERATION_STMT)
11746 break_maybe_infinite_loop ();
11747 break;
11748 case IN_OMP_BLOCK:
11749 error_at (token->location, "invalid exit from OpenMP structured block");
11750 break;
11751 case IN_OMP_FOR:
11752 error_at (token->location, "break statement used with OpenMP for loop");
11753 break;
11754 case IN_CILK_SIMD_FOR:
11755 error_at (token->location, "break statement used with Cilk Plus for loop");
11756 break;
11757 }
11758 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11759 break;
11760
11761 case RID_CONTINUE:
11762 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11763 {
11764 case 0:
11765 error_at (token->location, "continue statement not within a loop");
11766 break;
11767 case IN_CILK_SIMD_FOR:
11768 error_at (token->location,
11769 "continue statement within %<#pragma simd%> loop body");
11770 /* Fall through. */
11771 case IN_ITERATION_STMT:
11772 case IN_OMP_FOR:
11773 statement = finish_continue_stmt ();
11774 break;
11775 case IN_OMP_BLOCK:
11776 error_at (token->location, "invalid exit from OpenMP structured block");
11777 break;
11778 default:
11779 gcc_unreachable ();
11780 }
11781 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11782 break;
11783
11784 case RID_RETURN:
11785 {
11786 tree expr;
11787 bool expr_non_constant_p;
11788
11789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11790 {
11791 cp_lexer_set_source_position (parser->lexer);
11792 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11793 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11794 }
11795 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11796 expr = cp_parser_expression (parser);
11797 else
11798 /* If the next token is a `;', then there is no
11799 expression. */
11800 expr = NULL_TREE;
11801 /* Build the return-statement. */
11802 statement = finish_return_stmt (expr);
11803 /* Look for the final `;'. */
11804 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11805 }
11806 break;
11807
11808 case RID_GOTO:
11809 if (parser->in_function_body
11810 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11811 {
11812 error ("%<goto%> in %<constexpr%> function");
11813 cp_function_chain->invalid_constexpr = true;
11814 }
11815
11816 /* Create the goto-statement. */
11817 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11818 {
11819 /* Issue a warning about this use of a GNU extension. */
11820 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11821 /* Consume the '*' token. */
11822 cp_lexer_consume_token (parser->lexer);
11823 /* Parse the dependent expression. */
11824 finish_goto_stmt (cp_parser_expression (parser));
11825 }
11826 else
11827 finish_goto_stmt (cp_parser_identifier (parser));
11828 /* Look for the final `;'. */
11829 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11830 break;
11831
11832 default:
11833 cp_parser_error (parser, "expected jump-statement");
11834 break;
11835 }
11836
11837 return statement;
11838 }
11839
11840 /* Parse a declaration-statement.
11841
11842 declaration-statement:
11843 block-declaration */
11844
11845 static void
11846 cp_parser_declaration_statement (cp_parser* parser)
11847 {
11848 void *p;
11849
11850 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11851 p = obstack_alloc (&declarator_obstack, 0);
11852
11853 /* Parse the block-declaration. */
11854 cp_parser_block_declaration (parser, /*statement_p=*/true);
11855
11856 /* Free any declarators allocated. */
11857 obstack_free (&declarator_obstack, p);
11858 }
11859
11860 /* Some dependent statements (like `if (cond) statement'), are
11861 implicitly in their own scope. In other words, if the statement is
11862 a single statement (as opposed to a compound-statement), it is
11863 none-the-less treated as if it were enclosed in braces. Any
11864 declarations appearing in the dependent statement are out of scope
11865 after control passes that point. This function parses a statement,
11866 but ensures that is in its own scope, even if it is not a
11867 compound-statement.
11868
11869 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11870 is a (possibly labeled) if statement which is not enclosed in
11871 braces and has an else clause. This is used to implement
11872 -Wparentheses.
11873
11874 CHAIN is a vector of if-else-if conditions. This is used to implement
11875 -Wduplicated-cond.
11876
11877 Returns the new statement. */
11878
11879 static tree
11880 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11881 const token_indent_info &guard_tinfo,
11882 vec<tree> *chain)
11883 {
11884 tree statement;
11885 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11886 token_indent_info body_tinfo
11887 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11888
11889 if (if_p != NULL)
11890 *if_p = false;
11891
11892 /* Mark if () ; with a special NOP_EXPR. */
11893 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11894 {
11895 cp_lexer_consume_token (parser->lexer);
11896 statement = add_stmt (build_empty_stmt (body_loc));
11897
11898 if (guard_tinfo.keyword == RID_IF
11899 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11900 warning_at (body_loc, OPT_Wempty_body,
11901 "suggest braces around empty body in an %<if%> statement");
11902 else if (guard_tinfo.keyword == RID_ELSE)
11903 warning_at (body_loc, OPT_Wempty_body,
11904 "suggest braces around empty body in an %<else%> statement");
11905 }
11906 /* if a compound is opened, we simply parse the statement directly. */
11907 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11908 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11909 /* If the token is not a `{', then we must take special action. */
11910 else
11911 {
11912 /* Create a compound-statement. */
11913 statement = begin_compound_stmt (0);
11914 /* Parse the dependent-statement. */
11915 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11916 /* Finish the dummy compound-statement. */
11917 finish_compound_stmt (statement);
11918 }
11919
11920 token_indent_info next_tinfo
11921 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11922 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11923
11924 /* Return the statement. */
11925 return statement;
11926 }
11927
11928 /* For some dependent statements (like `while (cond) statement'), we
11929 have already created a scope. Therefore, even if the dependent
11930 statement is a compound-statement, we do not want to create another
11931 scope. */
11932
11933 static void
11934 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
11935 const token_indent_info &guard_tinfo)
11936 {
11937 /* If the token is a `{', then we must take special action. */
11938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11939 {
11940 token_indent_info body_tinfo
11941 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11942
11943 cp_parser_statement (parser, NULL_TREE, false, if_p);
11944 token_indent_info next_tinfo
11945 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11946 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11947 }
11948 else
11949 {
11950 /* Avoid calling cp_parser_compound_statement, so that we
11951 don't create a new scope. Do everything else by hand. */
11952 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11953 /* If the next keyword is `__label__' we have a label declaration. */
11954 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11955 cp_parser_label_declaration (parser);
11956 /* Parse an (optional) statement-seq. */
11957 cp_parser_statement_seq_opt (parser, NULL_TREE);
11958 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11959 }
11960 }
11961
11962 /* Declarations [gram.dcl.dcl] */
11963
11964 /* Parse an optional declaration-sequence.
11965
11966 declaration-seq:
11967 declaration
11968 declaration-seq declaration */
11969
11970 static void
11971 cp_parser_declaration_seq_opt (cp_parser* parser)
11972 {
11973 while (true)
11974 {
11975 cp_token *token;
11976
11977 token = cp_lexer_peek_token (parser->lexer);
11978
11979 if (token->type == CPP_CLOSE_BRACE
11980 || token->type == CPP_EOF
11981 || token->type == CPP_PRAGMA_EOL)
11982 break;
11983
11984 if (token->type == CPP_SEMICOLON)
11985 {
11986 /* A declaration consisting of a single semicolon is
11987 invalid. Allow it unless we're being pedantic. */
11988 cp_lexer_consume_token (parser->lexer);
11989 if (!in_system_header_at (input_location))
11990 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11991 continue;
11992 }
11993
11994 /* If we're entering or exiting a region that's implicitly
11995 extern "C", modify the lang context appropriately. */
11996 if (!parser->implicit_extern_c && token->implicit_extern_c)
11997 {
11998 push_lang_context (lang_name_c);
11999 parser->implicit_extern_c = true;
12000 }
12001 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12002 {
12003 pop_lang_context ();
12004 parser->implicit_extern_c = false;
12005 }
12006
12007 if (token->type == CPP_PRAGMA)
12008 {
12009 /* A top-level declaration can consist solely of a #pragma.
12010 A nested declaration cannot, so this is done here and not
12011 in cp_parser_declaration. (A #pragma at block scope is
12012 handled in cp_parser_statement.) */
12013 cp_parser_pragma (parser, pragma_external, NULL);
12014 continue;
12015 }
12016
12017 /* Parse the declaration itself. */
12018 cp_parser_declaration (parser);
12019 }
12020 }
12021
12022 /* Parse a declaration.
12023
12024 declaration:
12025 block-declaration
12026 function-definition
12027 template-declaration
12028 explicit-instantiation
12029 explicit-specialization
12030 linkage-specification
12031 namespace-definition
12032
12033 GNU extension:
12034
12035 declaration:
12036 __extension__ declaration */
12037
12038 static void
12039 cp_parser_declaration (cp_parser* parser)
12040 {
12041 cp_token token1;
12042 cp_token token2;
12043 int saved_pedantic;
12044 void *p;
12045 tree attributes = NULL_TREE;
12046
12047 /* Check for the `__extension__' keyword. */
12048 if (cp_parser_extension_opt (parser, &saved_pedantic))
12049 {
12050 /* Parse the qualified declaration. */
12051 cp_parser_declaration (parser);
12052 /* Restore the PEDANTIC flag. */
12053 pedantic = saved_pedantic;
12054
12055 return;
12056 }
12057
12058 /* Try to figure out what kind of declaration is present. */
12059 token1 = *cp_lexer_peek_token (parser->lexer);
12060
12061 if (token1.type != CPP_EOF)
12062 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12063 else
12064 {
12065 token2.type = CPP_EOF;
12066 token2.keyword = RID_MAX;
12067 }
12068
12069 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12070 p = obstack_alloc (&declarator_obstack, 0);
12071
12072 /* If the next token is `extern' and the following token is a string
12073 literal, then we have a linkage specification. */
12074 if (token1.keyword == RID_EXTERN
12075 && cp_parser_is_pure_string_literal (&token2))
12076 cp_parser_linkage_specification (parser);
12077 /* If the next token is `template', then we have either a template
12078 declaration, an explicit instantiation, or an explicit
12079 specialization. */
12080 else if (token1.keyword == RID_TEMPLATE)
12081 {
12082 /* `template <>' indicates a template specialization. */
12083 if (token2.type == CPP_LESS
12084 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12085 cp_parser_explicit_specialization (parser);
12086 /* `template <' indicates a template declaration. */
12087 else if (token2.type == CPP_LESS)
12088 cp_parser_template_declaration (parser, /*member_p=*/false);
12089 /* Anything else must be an explicit instantiation. */
12090 else
12091 cp_parser_explicit_instantiation (parser);
12092 }
12093 /* If the next token is `export', then we have a template
12094 declaration. */
12095 else if (token1.keyword == RID_EXPORT)
12096 cp_parser_template_declaration (parser, /*member_p=*/false);
12097 /* If the next token is `extern', 'static' or 'inline' and the one
12098 after that is `template', we have a GNU extended explicit
12099 instantiation directive. */
12100 else if (cp_parser_allow_gnu_extensions_p (parser)
12101 && (token1.keyword == RID_EXTERN
12102 || token1.keyword == RID_STATIC
12103 || token1.keyword == RID_INLINE)
12104 && token2.keyword == RID_TEMPLATE)
12105 cp_parser_explicit_instantiation (parser);
12106 /* If the next token is `namespace', check for a named or unnamed
12107 namespace definition. */
12108 else if (token1.keyword == RID_NAMESPACE
12109 && (/* A named namespace definition. */
12110 (token2.type == CPP_NAME
12111 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12112 != CPP_EQ))
12113 || (token2.type == CPP_OPEN_SQUARE
12114 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12115 == CPP_OPEN_SQUARE)
12116 /* An unnamed namespace definition. */
12117 || token2.type == CPP_OPEN_BRACE
12118 || token2.keyword == RID_ATTRIBUTE))
12119 cp_parser_namespace_definition (parser);
12120 /* An inline (associated) namespace definition. */
12121 else if (token1.keyword == RID_INLINE
12122 && token2.keyword == RID_NAMESPACE)
12123 cp_parser_namespace_definition (parser);
12124 /* Objective-C++ declaration/definition. */
12125 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12126 cp_parser_objc_declaration (parser, NULL_TREE);
12127 else if (c_dialect_objc ()
12128 && token1.keyword == RID_ATTRIBUTE
12129 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12130 cp_parser_objc_declaration (parser, attributes);
12131 /* At this point we may have a template declared by a concept
12132 introduction. */
12133 else if (flag_concepts
12134 && cp_parser_template_declaration_after_export (parser,
12135 /*member_p=*/false))
12136 /* We did. */;
12137 else
12138 /* Try to parse a block-declaration, or a function-definition. */
12139 cp_parser_block_declaration (parser, /*statement_p=*/false);
12140
12141 /* Free any declarators allocated. */
12142 obstack_free (&declarator_obstack, p);
12143 }
12144
12145 /* Parse a block-declaration.
12146
12147 block-declaration:
12148 simple-declaration
12149 asm-definition
12150 namespace-alias-definition
12151 using-declaration
12152 using-directive
12153
12154 GNU Extension:
12155
12156 block-declaration:
12157 __extension__ block-declaration
12158
12159 C++0x Extension:
12160
12161 block-declaration:
12162 static_assert-declaration
12163
12164 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12165 part of a declaration-statement. */
12166
12167 static void
12168 cp_parser_block_declaration (cp_parser *parser,
12169 bool statement_p)
12170 {
12171 cp_token *token1;
12172 int saved_pedantic;
12173
12174 /* Check for the `__extension__' keyword. */
12175 if (cp_parser_extension_opt (parser, &saved_pedantic))
12176 {
12177 /* Parse the qualified declaration. */
12178 cp_parser_block_declaration (parser, statement_p);
12179 /* Restore the PEDANTIC flag. */
12180 pedantic = saved_pedantic;
12181
12182 return;
12183 }
12184
12185 /* Peek at the next token to figure out which kind of declaration is
12186 present. */
12187 token1 = cp_lexer_peek_token (parser->lexer);
12188
12189 /* If the next keyword is `asm', we have an asm-definition. */
12190 if (token1->keyword == RID_ASM)
12191 {
12192 if (statement_p)
12193 cp_parser_commit_to_tentative_parse (parser);
12194 cp_parser_asm_definition (parser);
12195 }
12196 /* If the next keyword is `namespace', we have a
12197 namespace-alias-definition. */
12198 else if (token1->keyword == RID_NAMESPACE)
12199 cp_parser_namespace_alias_definition (parser);
12200 /* If the next keyword is `using', we have a
12201 using-declaration, a using-directive, or an alias-declaration. */
12202 else if (token1->keyword == RID_USING)
12203 {
12204 cp_token *token2;
12205
12206 if (statement_p)
12207 cp_parser_commit_to_tentative_parse (parser);
12208 /* If the token after `using' is `namespace', then we have a
12209 using-directive. */
12210 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12211 if (token2->keyword == RID_NAMESPACE)
12212 cp_parser_using_directive (parser);
12213 /* If the second token after 'using' is '=', then we have an
12214 alias-declaration. */
12215 else if (cxx_dialect >= cxx11
12216 && token2->type == CPP_NAME
12217 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12218 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12219 cp_parser_alias_declaration (parser);
12220 /* Otherwise, it's a using-declaration. */
12221 else
12222 cp_parser_using_declaration (parser,
12223 /*access_declaration_p=*/false);
12224 }
12225 /* If the next keyword is `__label__' we have a misplaced label
12226 declaration. */
12227 else if (token1->keyword == RID_LABEL)
12228 {
12229 cp_lexer_consume_token (parser->lexer);
12230 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12231 cp_parser_skip_to_end_of_statement (parser);
12232 /* If the next token is now a `;', consume it. */
12233 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12234 cp_lexer_consume_token (parser->lexer);
12235 }
12236 /* If the next token is `static_assert' we have a static assertion. */
12237 else if (token1->keyword == RID_STATIC_ASSERT)
12238 cp_parser_static_assert (parser, /*member_p=*/false);
12239 /* Anything else must be a simple-declaration. */
12240 else
12241 cp_parser_simple_declaration (parser, !statement_p,
12242 /*maybe_range_for_decl*/NULL);
12243 }
12244
12245 /* Parse a simple-declaration.
12246
12247 simple-declaration:
12248 decl-specifier-seq [opt] init-declarator-list [opt] ;
12249
12250 init-declarator-list:
12251 init-declarator
12252 init-declarator-list , init-declarator
12253
12254 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12255 function-definition as a simple-declaration.
12256
12257 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12258 parsed declaration if it is an uninitialized single declarator not followed
12259 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12260 if present, will not be consumed. */
12261
12262 static void
12263 cp_parser_simple_declaration (cp_parser* parser,
12264 bool function_definition_allowed_p,
12265 tree *maybe_range_for_decl)
12266 {
12267 cp_decl_specifier_seq decl_specifiers;
12268 int declares_class_or_enum;
12269 bool saw_declarator;
12270 location_t comma_loc = UNKNOWN_LOCATION;
12271 location_t init_loc = UNKNOWN_LOCATION;
12272
12273 if (maybe_range_for_decl)
12274 *maybe_range_for_decl = NULL_TREE;
12275
12276 /* Defer access checks until we know what is being declared; the
12277 checks for names appearing in the decl-specifier-seq should be
12278 done as if we were in the scope of the thing being declared. */
12279 push_deferring_access_checks (dk_deferred);
12280
12281 /* Parse the decl-specifier-seq. We have to keep track of whether
12282 or not the decl-specifier-seq declares a named class or
12283 enumeration type, since that is the only case in which the
12284 init-declarator-list is allowed to be empty.
12285
12286 [dcl.dcl]
12287
12288 In a simple-declaration, the optional init-declarator-list can be
12289 omitted only when declaring a class or enumeration, that is when
12290 the decl-specifier-seq contains either a class-specifier, an
12291 elaborated-type-specifier, or an enum-specifier. */
12292 cp_parser_decl_specifier_seq (parser,
12293 CP_PARSER_FLAGS_OPTIONAL,
12294 &decl_specifiers,
12295 &declares_class_or_enum);
12296 /* We no longer need to defer access checks. */
12297 stop_deferring_access_checks ();
12298
12299 /* In a block scope, a valid declaration must always have a
12300 decl-specifier-seq. By not trying to parse declarators, we can
12301 resolve the declaration/expression ambiguity more quickly. */
12302 if (!function_definition_allowed_p
12303 && !decl_specifiers.any_specifiers_p)
12304 {
12305 cp_parser_error (parser, "expected declaration");
12306 goto done;
12307 }
12308
12309 /* If the next two tokens are both identifiers, the code is
12310 erroneous. The usual cause of this situation is code like:
12311
12312 T t;
12313
12314 where "T" should name a type -- but does not. */
12315 if (!decl_specifiers.any_type_specifiers_p
12316 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12317 {
12318 /* If parsing tentatively, we should commit; we really are
12319 looking at a declaration. */
12320 cp_parser_commit_to_tentative_parse (parser);
12321 /* Give up. */
12322 goto done;
12323 }
12324
12325 /* If we have seen at least one decl-specifier, and the next token
12326 is not a parenthesis, then we must be looking at a declaration.
12327 (After "int (" we might be looking at a functional cast.) */
12328 if (decl_specifiers.any_specifiers_p
12329 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12330 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12331 && !cp_parser_error_occurred (parser))
12332 cp_parser_commit_to_tentative_parse (parser);
12333
12334 tree last_type;
12335
12336 last_type = NULL_TREE;
12337
12338 /* Keep going until we hit the `;' at the end of the simple
12339 declaration. */
12340 saw_declarator = false;
12341 while (cp_lexer_next_token_is_not (parser->lexer,
12342 CPP_SEMICOLON))
12343 {
12344 cp_token *token;
12345 bool function_definition_p;
12346 tree decl;
12347 tree auto_result = NULL_TREE;
12348
12349 if (saw_declarator)
12350 {
12351 /* If we are processing next declarator, comma is expected */
12352 token = cp_lexer_peek_token (parser->lexer);
12353 gcc_assert (token->type == CPP_COMMA);
12354 cp_lexer_consume_token (parser->lexer);
12355 if (maybe_range_for_decl)
12356 {
12357 *maybe_range_for_decl = error_mark_node;
12358 if (comma_loc == UNKNOWN_LOCATION)
12359 comma_loc = token->location;
12360 }
12361 }
12362 else
12363 saw_declarator = true;
12364
12365 /* Parse the init-declarator. */
12366 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12367 /*checks=*/NULL,
12368 function_definition_allowed_p,
12369 /*member_p=*/false,
12370 declares_class_or_enum,
12371 &function_definition_p,
12372 maybe_range_for_decl,
12373 &init_loc,
12374 &auto_result);
12375 /* If an error occurred while parsing tentatively, exit quickly.
12376 (That usually happens when in the body of a function; each
12377 statement is treated as a declaration-statement until proven
12378 otherwise.) */
12379 if (cp_parser_error_occurred (parser))
12380 goto done;
12381
12382 if (auto_result)
12383 {
12384 if (last_type && last_type != error_mark_node
12385 && !same_type_p (auto_result, last_type))
12386 {
12387 /* If the list of declarators contains more than one declarator,
12388 the type of each declared variable is determined as described
12389 above. If the type deduced for the template parameter U is not
12390 the same in each deduction, the program is ill-formed. */
12391 error_at (decl_specifiers.locations[ds_type_spec],
12392 "inconsistent deduction for %qT: %qT and then %qT",
12393 decl_specifiers.type, last_type, auto_result);
12394 last_type = error_mark_node;
12395 }
12396 else
12397 last_type = auto_result;
12398 }
12399
12400 /* Handle function definitions specially. */
12401 if (function_definition_p)
12402 {
12403 /* If the next token is a `,', then we are probably
12404 processing something like:
12405
12406 void f() {}, *p;
12407
12408 which is erroneous. */
12409 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12410 {
12411 cp_token *token = cp_lexer_peek_token (parser->lexer);
12412 error_at (token->location,
12413 "mixing"
12414 " declarations and function-definitions is forbidden");
12415 }
12416 /* Otherwise, we're done with the list of declarators. */
12417 else
12418 {
12419 pop_deferring_access_checks ();
12420 return;
12421 }
12422 }
12423 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12424 *maybe_range_for_decl = decl;
12425 /* The next token should be either a `,' or a `;'. */
12426 token = cp_lexer_peek_token (parser->lexer);
12427 /* If it's a `,', there are more declarators to come. */
12428 if (token->type == CPP_COMMA)
12429 /* will be consumed next time around */;
12430 /* If it's a `;', we are done. */
12431 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12432 break;
12433 /* Anything else is an error. */
12434 else
12435 {
12436 /* If we have already issued an error message we don't need
12437 to issue another one. */
12438 if ((decl != error_mark_node
12439 && DECL_INITIAL (decl) != error_mark_node)
12440 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12441 cp_parser_error (parser, "expected %<,%> or %<;%>");
12442 /* Skip tokens until we reach the end of the statement. */
12443 cp_parser_skip_to_end_of_statement (parser);
12444 /* If the next token is now a `;', consume it. */
12445 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12446 cp_lexer_consume_token (parser->lexer);
12447 goto done;
12448 }
12449 /* After the first time around, a function-definition is not
12450 allowed -- even if it was OK at first. For example:
12451
12452 int i, f() {}
12453
12454 is not valid. */
12455 function_definition_allowed_p = false;
12456 }
12457
12458 /* Issue an error message if no declarators are present, and the
12459 decl-specifier-seq does not itself declare a class or
12460 enumeration: [dcl.dcl]/3. */
12461 if (!saw_declarator)
12462 {
12463 if (cp_parser_declares_only_class_p (parser))
12464 {
12465 if (!declares_class_or_enum
12466 && decl_specifiers.type
12467 && OVERLOAD_TYPE_P (decl_specifiers.type))
12468 /* Ensure an error is issued anyway when finish_decltype_type,
12469 called via cp_parser_decl_specifier_seq, returns a class or
12470 an enumeration (c++/51786). */
12471 decl_specifiers.type = NULL_TREE;
12472 shadow_tag (&decl_specifiers);
12473 }
12474 /* Perform any deferred access checks. */
12475 perform_deferred_access_checks (tf_warning_or_error);
12476 }
12477
12478 /* Consume the `;'. */
12479 if (!maybe_range_for_decl)
12480 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12481 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12482 {
12483 if (init_loc != UNKNOWN_LOCATION)
12484 error_at (init_loc, "initializer in range-based %<for%> loop");
12485 if (comma_loc != UNKNOWN_LOCATION)
12486 error_at (comma_loc,
12487 "multiple declarations in range-based %<for%> loop");
12488 }
12489
12490 done:
12491 pop_deferring_access_checks ();
12492 }
12493
12494 /* Parse a decl-specifier-seq.
12495
12496 decl-specifier-seq:
12497 decl-specifier-seq [opt] decl-specifier
12498 decl-specifier attribute-specifier-seq [opt] (C++11)
12499
12500 decl-specifier:
12501 storage-class-specifier
12502 type-specifier
12503 function-specifier
12504 friend
12505 typedef
12506
12507 GNU Extension:
12508
12509 decl-specifier:
12510 attributes
12511
12512 Concepts Extension:
12513
12514 decl-specifier:
12515 concept
12516
12517 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12518
12519 The parser flags FLAGS is used to control type-specifier parsing.
12520
12521 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12522 flags:
12523
12524 1: one of the decl-specifiers is an elaborated-type-specifier
12525 (i.e., a type declaration)
12526 2: one of the decl-specifiers is an enum-specifier or a
12527 class-specifier (i.e., a type definition)
12528
12529 */
12530
12531 static void
12532 cp_parser_decl_specifier_seq (cp_parser* parser,
12533 cp_parser_flags flags,
12534 cp_decl_specifier_seq *decl_specs,
12535 int* declares_class_or_enum)
12536 {
12537 bool constructor_possible_p = !parser->in_declarator_p;
12538 bool found_decl_spec = false;
12539 cp_token *start_token = NULL;
12540 cp_decl_spec ds;
12541
12542 /* Clear DECL_SPECS. */
12543 clear_decl_specs (decl_specs);
12544
12545 /* Assume no class or enumeration type is declared. */
12546 *declares_class_or_enum = 0;
12547
12548 /* Keep reading specifiers until there are no more to read. */
12549 while (true)
12550 {
12551 bool constructor_p;
12552 cp_token *token;
12553 ds = ds_last;
12554
12555 /* Peek at the next token. */
12556 token = cp_lexer_peek_token (parser->lexer);
12557
12558 /* Save the first token of the decl spec list for error
12559 reporting. */
12560 if (!start_token)
12561 start_token = token;
12562 /* Handle attributes. */
12563 if (cp_next_tokens_can_be_attribute_p (parser))
12564 {
12565 /* Parse the attributes. */
12566 tree attrs = cp_parser_attributes_opt (parser);
12567
12568 /* In a sequence of declaration specifiers, c++11 attributes
12569 appertain to the type that precede them. In that case
12570 [dcl.spec]/1 says:
12571
12572 The attribute-specifier-seq affects the type only for
12573 the declaration it appears in, not other declarations
12574 involving the same type.
12575
12576 But for now let's force the user to position the
12577 attribute either at the beginning of the declaration or
12578 after the declarator-id, which would clearly mean that it
12579 applies to the declarator. */
12580 if (cxx11_attribute_p (attrs))
12581 {
12582 if (!found_decl_spec)
12583 /* The c++11 attribute is at the beginning of the
12584 declaration. It appertains to the entity being
12585 declared. */;
12586 else
12587 {
12588 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12589 {
12590 /* This is an attribute following a
12591 class-specifier. */
12592 if (decl_specs->type_definition_p)
12593 warn_misplaced_attr_for_class_type (token->location,
12594 decl_specs->type);
12595 attrs = NULL_TREE;
12596 }
12597 else
12598 {
12599 decl_specs->std_attributes
12600 = chainon (decl_specs->std_attributes,
12601 attrs);
12602 if (decl_specs->locations[ds_std_attribute] == 0)
12603 decl_specs->locations[ds_std_attribute] = token->location;
12604 }
12605 continue;
12606 }
12607 }
12608
12609 decl_specs->attributes
12610 = chainon (decl_specs->attributes,
12611 attrs);
12612 if (decl_specs->locations[ds_attribute] == 0)
12613 decl_specs->locations[ds_attribute] = token->location;
12614 continue;
12615 }
12616 /* Assume we will find a decl-specifier keyword. */
12617 found_decl_spec = true;
12618 /* If the next token is an appropriate keyword, we can simply
12619 add it to the list. */
12620 switch (token->keyword)
12621 {
12622 /* decl-specifier:
12623 friend
12624 constexpr */
12625 case RID_FRIEND:
12626 if (!at_class_scope_p ())
12627 {
12628 error_at (token->location, "%<friend%> used outside of class");
12629 cp_lexer_purge_token (parser->lexer);
12630 }
12631 else
12632 {
12633 ds = ds_friend;
12634 /* Consume the token. */
12635 cp_lexer_consume_token (parser->lexer);
12636 }
12637 break;
12638
12639 case RID_CONSTEXPR:
12640 ds = ds_constexpr;
12641 cp_lexer_consume_token (parser->lexer);
12642 break;
12643
12644 case RID_CONCEPT:
12645 ds = ds_concept;
12646 cp_lexer_consume_token (parser->lexer);
12647 break;
12648
12649 /* function-specifier:
12650 inline
12651 virtual
12652 explicit */
12653 case RID_INLINE:
12654 case RID_VIRTUAL:
12655 case RID_EXPLICIT:
12656 cp_parser_function_specifier_opt (parser, decl_specs);
12657 break;
12658
12659 /* decl-specifier:
12660 typedef */
12661 case RID_TYPEDEF:
12662 ds = ds_typedef;
12663 /* Consume the token. */
12664 cp_lexer_consume_token (parser->lexer);
12665 /* A constructor declarator cannot appear in a typedef. */
12666 constructor_possible_p = false;
12667 /* The "typedef" keyword can only occur in a declaration; we
12668 may as well commit at this point. */
12669 cp_parser_commit_to_tentative_parse (parser);
12670
12671 if (decl_specs->storage_class != sc_none)
12672 decl_specs->conflicting_specifiers_p = true;
12673 break;
12674
12675 /* storage-class-specifier:
12676 auto
12677 register
12678 static
12679 extern
12680 mutable
12681
12682 GNU Extension:
12683 thread */
12684 case RID_AUTO:
12685 if (cxx_dialect == cxx98)
12686 {
12687 /* Consume the token. */
12688 cp_lexer_consume_token (parser->lexer);
12689
12690 /* Complain about `auto' as a storage specifier, if
12691 we're complaining about C++0x compatibility. */
12692 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12693 " changes meaning in C++11; please remove it");
12694
12695 /* Set the storage class anyway. */
12696 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12697 token);
12698 }
12699 else
12700 /* C++0x auto type-specifier. */
12701 found_decl_spec = false;
12702 break;
12703
12704 case RID_REGISTER:
12705 case RID_STATIC:
12706 case RID_EXTERN:
12707 case RID_MUTABLE:
12708 /* Consume the token. */
12709 cp_lexer_consume_token (parser->lexer);
12710 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12711 token);
12712 break;
12713 case RID_THREAD:
12714 /* Consume the token. */
12715 ds = ds_thread;
12716 cp_lexer_consume_token (parser->lexer);
12717 break;
12718
12719 default:
12720 /* We did not yet find a decl-specifier yet. */
12721 found_decl_spec = false;
12722 break;
12723 }
12724
12725 if (found_decl_spec
12726 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12727 && token->keyword != RID_CONSTEXPR)
12728 error ("decl-specifier invalid in condition");
12729
12730 if (ds != ds_last)
12731 set_and_check_decl_spec_loc (decl_specs, ds, token);
12732
12733 /* Constructors are a special case. The `S' in `S()' is not a
12734 decl-specifier; it is the beginning of the declarator. */
12735 constructor_p
12736 = (!found_decl_spec
12737 && constructor_possible_p
12738 && (cp_parser_constructor_declarator_p
12739 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12740
12741 /* If we don't have a DECL_SPEC yet, then we must be looking at
12742 a type-specifier. */
12743 if (!found_decl_spec && !constructor_p)
12744 {
12745 int decl_spec_declares_class_or_enum;
12746 bool is_cv_qualifier;
12747 tree type_spec;
12748
12749 type_spec
12750 = cp_parser_type_specifier (parser, flags,
12751 decl_specs,
12752 /*is_declaration=*/true,
12753 &decl_spec_declares_class_or_enum,
12754 &is_cv_qualifier);
12755 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12756
12757 /* If this type-specifier referenced a user-defined type
12758 (a typedef, class-name, etc.), then we can't allow any
12759 more such type-specifiers henceforth.
12760
12761 [dcl.spec]
12762
12763 The longest sequence of decl-specifiers that could
12764 possibly be a type name is taken as the
12765 decl-specifier-seq of a declaration. The sequence shall
12766 be self-consistent as described below.
12767
12768 [dcl.type]
12769
12770 As a general rule, at most one type-specifier is allowed
12771 in the complete decl-specifier-seq of a declaration. The
12772 only exceptions are the following:
12773
12774 -- const or volatile can be combined with any other
12775 type-specifier.
12776
12777 -- signed or unsigned can be combined with char, long,
12778 short, or int.
12779
12780 -- ..
12781
12782 Example:
12783
12784 typedef char* Pc;
12785 void g (const int Pc);
12786
12787 Here, Pc is *not* part of the decl-specifier seq; it's
12788 the declarator. Therefore, once we see a type-specifier
12789 (other than a cv-qualifier), we forbid any additional
12790 user-defined types. We *do* still allow things like `int
12791 int' to be considered a decl-specifier-seq, and issue the
12792 error message later. */
12793 if (type_spec && !is_cv_qualifier)
12794 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12795 /* A constructor declarator cannot follow a type-specifier. */
12796 if (type_spec)
12797 {
12798 constructor_possible_p = false;
12799 found_decl_spec = true;
12800 if (!is_cv_qualifier)
12801 decl_specs->any_type_specifiers_p = true;
12802 }
12803 }
12804
12805 /* If we still do not have a DECL_SPEC, then there are no more
12806 decl-specifiers. */
12807 if (!found_decl_spec)
12808 break;
12809
12810 decl_specs->any_specifiers_p = true;
12811 /* After we see one decl-specifier, further decl-specifiers are
12812 always optional. */
12813 flags |= CP_PARSER_FLAGS_OPTIONAL;
12814 }
12815
12816 /* Don't allow a friend specifier with a class definition. */
12817 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12818 && (*declares_class_or_enum & 2))
12819 error_at (decl_specs->locations[ds_friend],
12820 "class definition may not be declared a friend");
12821 }
12822
12823 /* Parse an (optional) storage-class-specifier.
12824
12825 storage-class-specifier:
12826 auto
12827 register
12828 static
12829 extern
12830 mutable
12831
12832 GNU Extension:
12833
12834 storage-class-specifier:
12835 thread
12836
12837 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12838
12839 static tree
12840 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12841 {
12842 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12843 {
12844 case RID_AUTO:
12845 if (cxx_dialect != cxx98)
12846 return NULL_TREE;
12847 /* Fall through for C++98. */
12848
12849 case RID_REGISTER:
12850 case RID_STATIC:
12851 case RID_EXTERN:
12852 case RID_MUTABLE:
12853 case RID_THREAD:
12854 /* Consume the token. */
12855 return cp_lexer_consume_token (parser->lexer)->u.value;
12856
12857 default:
12858 return NULL_TREE;
12859 }
12860 }
12861
12862 /* Parse an (optional) function-specifier.
12863
12864 function-specifier:
12865 inline
12866 virtual
12867 explicit
12868
12869 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12870 Updates DECL_SPECS, if it is non-NULL. */
12871
12872 static tree
12873 cp_parser_function_specifier_opt (cp_parser* parser,
12874 cp_decl_specifier_seq *decl_specs)
12875 {
12876 cp_token *token = cp_lexer_peek_token (parser->lexer);
12877 switch (token->keyword)
12878 {
12879 case RID_INLINE:
12880 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12881 break;
12882
12883 case RID_VIRTUAL:
12884 /* 14.5.2.3 [temp.mem]
12885
12886 A member function template shall not be virtual. */
12887 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12888 error_at (token->location, "templates may not be %<virtual%>");
12889 else
12890 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12891 break;
12892
12893 case RID_EXPLICIT:
12894 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12895 break;
12896
12897 default:
12898 return NULL_TREE;
12899 }
12900
12901 /* Consume the token. */
12902 return cp_lexer_consume_token (parser->lexer)->u.value;
12903 }
12904
12905 /* Parse a linkage-specification.
12906
12907 linkage-specification:
12908 extern string-literal { declaration-seq [opt] }
12909 extern string-literal declaration */
12910
12911 static void
12912 cp_parser_linkage_specification (cp_parser* parser)
12913 {
12914 tree linkage;
12915
12916 /* Look for the `extern' keyword. */
12917 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12918
12919 /* Look for the string-literal. */
12920 linkage = cp_parser_string_literal (parser, false, false);
12921
12922 /* Transform the literal into an identifier. If the literal is a
12923 wide-character string, or contains embedded NULs, then we can't
12924 handle it as the user wants. */
12925 if (strlen (TREE_STRING_POINTER (linkage))
12926 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12927 {
12928 cp_parser_error (parser, "invalid linkage-specification");
12929 /* Assume C++ linkage. */
12930 linkage = lang_name_cplusplus;
12931 }
12932 else
12933 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12934
12935 /* We're now using the new linkage. */
12936 push_lang_context (linkage);
12937
12938 /* If the next token is a `{', then we're using the first
12939 production. */
12940 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12941 {
12942 cp_ensure_no_omp_declare_simd (parser);
12943 cp_ensure_no_oacc_routine (parser);
12944
12945 /* Consume the `{' token. */
12946 cp_lexer_consume_token (parser->lexer);
12947 /* Parse the declarations. */
12948 cp_parser_declaration_seq_opt (parser);
12949 /* Look for the closing `}'. */
12950 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12951 }
12952 /* Otherwise, there's just one declaration. */
12953 else
12954 {
12955 bool saved_in_unbraced_linkage_specification_p;
12956
12957 saved_in_unbraced_linkage_specification_p
12958 = parser->in_unbraced_linkage_specification_p;
12959 parser->in_unbraced_linkage_specification_p = true;
12960 cp_parser_declaration (parser);
12961 parser->in_unbraced_linkage_specification_p
12962 = saved_in_unbraced_linkage_specification_p;
12963 }
12964
12965 /* We're done with the linkage-specification. */
12966 pop_lang_context ();
12967 }
12968
12969 /* Parse a static_assert-declaration.
12970
12971 static_assert-declaration:
12972 static_assert ( constant-expression , string-literal ) ;
12973 static_assert ( constant-expression ) ; (C++1Z)
12974
12975 If MEMBER_P, this static_assert is a class member. */
12976
12977 static void
12978 cp_parser_static_assert(cp_parser *parser, bool member_p)
12979 {
12980 tree condition;
12981 tree message;
12982 cp_token *token;
12983 location_t saved_loc;
12984 bool dummy;
12985
12986 /* Peek at the `static_assert' token so we can keep track of exactly
12987 where the static assertion started. */
12988 token = cp_lexer_peek_token (parser->lexer);
12989 saved_loc = token->location;
12990
12991 /* Look for the `static_assert' keyword. */
12992 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12993 RT_STATIC_ASSERT))
12994 return;
12995
12996 /* We know we are in a static assertion; commit to any tentative
12997 parse. */
12998 if (cp_parser_parsing_tentatively (parser))
12999 cp_parser_commit_to_tentative_parse (parser);
13000
13001 /* Parse the `(' starting the static assertion condition. */
13002 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13003
13004 /* Parse the constant-expression. Allow a non-constant expression
13005 here in order to give better diagnostics in finish_static_assert. */
13006 condition =
13007 cp_parser_constant_expression (parser,
13008 /*allow_non_constant_p=*/true,
13009 /*non_constant_p=*/&dummy);
13010
13011 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13012 {
13013 if (cxx_dialect < cxx1z)
13014 pedwarn (input_location, OPT_Wpedantic,
13015 "static_assert without a message "
13016 "only available with -std=c++1z or -std=gnu++1z");
13017 /* Eat the ')' */
13018 cp_lexer_consume_token (parser->lexer);
13019 message = build_string (1, "");
13020 TREE_TYPE (message) = char_array_type_node;
13021 fix_string_type (message);
13022 }
13023 else
13024 {
13025 /* Parse the separating `,'. */
13026 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13027
13028 /* Parse the string-literal message. */
13029 message = cp_parser_string_literal (parser,
13030 /*translate=*/false,
13031 /*wide_ok=*/true);
13032
13033 /* A `)' completes the static assertion. */
13034 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13035 cp_parser_skip_to_closing_parenthesis (parser,
13036 /*recovering=*/true,
13037 /*or_comma=*/false,
13038 /*consume_paren=*/true);
13039 }
13040
13041 /* A semicolon terminates the declaration. */
13042 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13043
13044 /* Complete the static assertion, which may mean either processing
13045 the static assert now or saving it for template instantiation. */
13046 finish_static_assert (condition, message, saved_loc, member_p);
13047 }
13048
13049 /* Parse the expression in decltype ( expression ). */
13050
13051 static tree
13052 cp_parser_decltype_expr (cp_parser *parser,
13053 bool &id_expression_or_member_access_p)
13054 {
13055 cp_token *id_expr_start_token;
13056 tree expr;
13057
13058 /* Since we're going to preserve any side-effects from this parse, set up a
13059 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13060 in the expression. */
13061 tentative_firewall firewall (parser);
13062
13063 /* First, try parsing an id-expression. */
13064 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13065 cp_parser_parse_tentatively (parser);
13066 expr = cp_parser_id_expression (parser,
13067 /*template_keyword_p=*/false,
13068 /*check_dependency_p=*/true,
13069 /*template_p=*/NULL,
13070 /*declarator_p=*/false,
13071 /*optional_p=*/false);
13072
13073 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13074 {
13075 bool non_integral_constant_expression_p = false;
13076 tree id_expression = expr;
13077 cp_id_kind idk;
13078 const char *error_msg;
13079
13080 if (identifier_p (expr))
13081 /* Lookup the name we got back from the id-expression. */
13082 expr = cp_parser_lookup_name_simple (parser, expr,
13083 id_expr_start_token->location);
13084
13085 if (expr
13086 && expr != error_mark_node
13087 && TREE_CODE (expr) != TYPE_DECL
13088 && (TREE_CODE (expr) != BIT_NOT_EXPR
13089 || !TYPE_P (TREE_OPERAND (expr, 0)))
13090 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13091 {
13092 /* Complete lookup of the id-expression. */
13093 expr = (finish_id_expression
13094 (id_expression, expr, parser->scope, &idk,
13095 /*integral_constant_expression_p=*/false,
13096 /*allow_non_integral_constant_expression_p=*/true,
13097 &non_integral_constant_expression_p,
13098 /*template_p=*/false,
13099 /*done=*/true,
13100 /*address_p=*/false,
13101 /*template_arg_p=*/false,
13102 &error_msg,
13103 id_expr_start_token->location));
13104
13105 if (expr == error_mark_node)
13106 /* We found an id-expression, but it was something that we
13107 should not have found. This is an error, not something
13108 we can recover from, so note that we found an
13109 id-expression and we'll recover as gracefully as
13110 possible. */
13111 id_expression_or_member_access_p = true;
13112 }
13113
13114 if (expr
13115 && expr != error_mark_node
13116 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13117 /* We have an id-expression. */
13118 id_expression_or_member_access_p = true;
13119 }
13120
13121 if (!id_expression_or_member_access_p)
13122 {
13123 /* Abort the id-expression parse. */
13124 cp_parser_abort_tentative_parse (parser);
13125
13126 /* Parsing tentatively, again. */
13127 cp_parser_parse_tentatively (parser);
13128
13129 /* Parse a class member access. */
13130 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13131 /*cast_p=*/false, /*decltype*/true,
13132 /*member_access_only_p=*/true, NULL);
13133
13134 if (expr
13135 && expr != error_mark_node
13136 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13137 /* We have an id-expression. */
13138 id_expression_or_member_access_p = true;
13139 }
13140
13141 if (id_expression_or_member_access_p)
13142 /* We have parsed the complete id-expression or member access. */
13143 cp_parser_parse_definitely (parser);
13144 else
13145 {
13146 /* Abort our attempt to parse an id-expression or member access
13147 expression. */
13148 cp_parser_abort_tentative_parse (parser);
13149
13150 /* Parse a full expression. */
13151 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13152 /*decltype_p=*/true);
13153 }
13154
13155 return expr;
13156 }
13157
13158 /* Parse a `decltype' type. Returns the type.
13159
13160 simple-type-specifier:
13161 decltype ( expression )
13162 C++14 proposal:
13163 decltype ( auto ) */
13164
13165 static tree
13166 cp_parser_decltype (cp_parser *parser)
13167 {
13168 tree expr;
13169 bool id_expression_or_member_access_p = false;
13170 const char *saved_message;
13171 bool saved_integral_constant_expression_p;
13172 bool saved_non_integral_constant_expression_p;
13173 bool saved_greater_than_is_operator_p;
13174 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13175
13176 if (start_token->type == CPP_DECLTYPE)
13177 {
13178 /* Already parsed. */
13179 cp_lexer_consume_token (parser->lexer);
13180 return saved_checks_value (start_token->u.tree_check_value);
13181 }
13182
13183 /* Look for the `decltype' token. */
13184 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13185 return error_mark_node;
13186
13187 /* Parse the opening `('. */
13188 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13189 return error_mark_node;
13190
13191 /* decltype (auto) */
13192 if (cxx_dialect >= cxx14
13193 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13194 {
13195 cp_lexer_consume_token (parser->lexer);
13196 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13197 return error_mark_node;
13198 expr = make_decltype_auto ();
13199 AUTO_IS_DECLTYPE (expr) = true;
13200 goto rewrite;
13201 }
13202
13203 /* Types cannot be defined in a `decltype' expression. Save away the
13204 old message. */
13205 saved_message = parser->type_definition_forbidden_message;
13206
13207 /* And create the new one. */
13208 parser->type_definition_forbidden_message
13209 = G_("types may not be defined in %<decltype%> expressions");
13210
13211 /* The restrictions on constant-expressions do not apply inside
13212 decltype expressions. */
13213 saved_integral_constant_expression_p
13214 = parser->integral_constant_expression_p;
13215 saved_non_integral_constant_expression_p
13216 = parser->non_integral_constant_expression_p;
13217 parser->integral_constant_expression_p = false;
13218
13219 /* Within a parenthesized expression, a `>' token is always
13220 the greater-than operator. */
13221 saved_greater_than_is_operator_p
13222 = parser->greater_than_is_operator_p;
13223 parser->greater_than_is_operator_p = true;
13224
13225 /* Do not actually evaluate the expression. */
13226 ++cp_unevaluated_operand;
13227
13228 /* Do not warn about problems with the expression. */
13229 ++c_inhibit_evaluation_warnings;
13230
13231 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13232
13233 /* Go back to evaluating expressions. */
13234 --cp_unevaluated_operand;
13235 --c_inhibit_evaluation_warnings;
13236
13237 /* The `>' token might be the end of a template-id or
13238 template-parameter-list now. */
13239 parser->greater_than_is_operator_p
13240 = saved_greater_than_is_operator_p;
13241
13242 /* Restore the old message and the integral constant expression
13243 flags. */
13244 parser->type_definition_forbidden_message = saved_message;
13245 parser->integral_constant_expression_p
13246 = saved_integral_constant_expression_p;
13247 parser->non_integral_constant_expression_p
13248 = saved_non_integral_constant_expression_p;
13249
13250 /* Parse to the closing `)'. */
13251 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13252 {
13253 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13254 /*consume_paren=*/true);
13255 return error_mark_node;
13256 }
13257
13258 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13259 tf_warning_or_error);
13260
13261 rewrite:
13262 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13263 it again. */
13264 start_token->type = CPP_DECLTYPE;
13265 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13266 start_token->u.tree_check_value->value = expr;
13267 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13268 start_token->keyword = RID_MAX;
13269 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13270
13271 return expr;
13272 }
13273
13274 /* Special member functions [gram.special] */
13275
13276 /* Parse a conversion-function-id.
13277
13278 conversion-function-id:
13279 operator conversion-type-id
13280
13281 Returns an IDENTIFIER_NODE representing the operator. */
13282
13283 static tree
13284 cp_parser_conversion_function_id (cp_parser* parser)
13285 {
13286 tree type;
13287 tree saved_scope;
13288 tree saved_qualifying_scope;
13289 tree saved_object_scope;
13290 tree pushed_scope = NULL_TREE;
13291
13292 /* Look for the `operator' token. */
13293 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13294 return error_mark_node;
13295 /* When we parse the conversion-type-id, the current scope will be
13296 reset. However, we need that information in able to look up the
13297 conversion function later, so we save it here. */
13298 saved_scope = parser->scope;
13299 saved_qualifying_scope = parser->qualifying_scope;
13300 saved_object_scope = parser->object_scope;
13301 /* We must enter the scope of the class so that the names of
13302 entities declared within the class are available in the
13303 conversion-type-id. For example, consider:
13304
13305 struct S {
13306 typedef int I;
13307 operator I();
13308 };
13309
13310 S::operator I() { ... }
13311
13312 In order to see that `I' is a type-name in the definition, we
13313 must be in the scope of `S'. */
13314 if (saved_scope)
13315 pushed_scope = push_scope (saved_scope);
13316 /* Parse the conversion-type-id. */
13317 type = cp_parser_conversion_type_id (parser);
13318 /* Leave the scope of the class, if any. */
13319 if (pushed_scope)
13320 pop_scope (pushed_scope);
13321 /* Restore the saved scope. */
13322 parser->scope = saved_scope;
13323 parser->qualifying_scope = saved_qualifying_scope;
13324 parser->object_scope = saved_object_scope;
13325 /* If the TYPE is invalid, indicate failure. */
13326 if (type == error_mark_node)
13327 return error_mark_node;
13328 return mangle_conv_op_name_for_type (type);
13329 }
13330
13331 /* Parse a conversion-type-id:
13332
13333 conversion-type-id:
13334 type-specifier-seq conversion-declarator [opt]
13335
13336 Returns the TYPE specified. */
13337
13338 static tree
13339 cp_parser_conversion_type_id (cp_parser* parser)
13340 {
13341 tree attributes;
13342 cp_decl_specifier_seq type_specifiers;
13343 cp_declarator *declarator;
13344 tree type_specified;
13345 const char *saved_message;
13346
13347 /* Parse the attributes. */
13348 attributes = cp_parser_attributes_opt (parser);
13349
13350 saved_message = parser->type_definition_forbidden_message;
13351 parser->type_definition_forbidden_message
13352 = G_("types may not be defined in a conversion-type-id");
13353
13354 /* Parse the type-specifiers. */
13355 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13356 /*is_trailing_return=*/false,
13357 &type_specifiers);
13358
13359 parser->type_definition_forbidden_message = saved_message;
13360
13361 /* If that didn't work, stop. */
13362 if (type_specifiers.type == error_mark_node)
13363 return error_mark_node;
13364 /* Parse the conversion-declarator. */
13365 declarator = cp_parser_conversion_declarator_opt (parser);
13366
13367 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13368 /*initialized=*/0, &attributes);
13369 if (attributes)
13370 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13371
13372 /* Don't give this error when parsing tentatively. This happens to
13373 work because we always parse this definitively once. */
13374 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13375 && type_uses_auto (type_specified))
13376 {
13377 if (cxx_dialect < cxx14)
13378 {
13379 error ("invalid use of %<auto%> in conversion operator");
13380 return error_mark_node;
13381 }
13382 else if (template_parm_scope_p ())
13383 warning (0, "use of %<auto%> in member template "
13384 "conversion operator can never be deduced");
13385 }
13386
13387 return type_specified;
13388 }
13389
13390 /* Parse an (optional) conversion-declarator.
13391
13392 conversion-declarator:
13393 ptr-operator conversion-declarator [opt]
13394
13395 */
13396
13397 static cp_declarator *
13398 cp_parser_conversion_declarator_opt (cp_parser* parser)
13399 {
13400 enum tree_code code;
13401 tree class_type, std_attributes = NULL_TREE;
13402 cp_cv_quals cv_quals;
13403
13404 /* We don't know if there's a ptr-operator next, or not. */
13405 cp_parser_parse_tentatively (parser);
13406 /* Try the ptr-operator. */
13407 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13408 &std_attributes);
13409 /* If it worked, look for more conversion-declarators. */
13410 if (cp_parser_parse_definitely (parser))
13411 {
13412 cp_declarator *declarator;
13413
13414 /* Parse another optional declarator. */
13415 declarator = cp_parser_conversion_declarator_opt (parser);
13416
13417 declarator = cp_parser_make_indirect_declarator
13418 (code, class_type, cv_quals, declarator, std_attributes);
13419
13420 return declarator;
13421 }
13422
13423 return NULL;
13424 }
13425
13426 /* Parse an (optional) ctor-initializer.
13427
13428 ctor-initializer:
13429 : mem-initializer-list
13430
13431 Returns TRUE iff the ctor-initializer was actually present. */
13432
13433 static bool
13434 cp_parser_ctor_initializer_opt (cp_parser* parser)
13435 {
13436 /* If the next token is not a `:', then there is no
13437 ctor-initializer. */
13438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13439 {
13440 /* Do default initialization of any bases and members. */
13441 if (DECL_CONSTRUCTOR_P (current_function_decl))
13442 finish_mem_initializers (NULL_TREE);
13443
13444 return false;
13445 }
13446
13447 /* Consume the `:' token. */
13448 cp_lexer_consume_token (parser->lexer);
13449 /* And the mem-initializer-list. */
13450 cp_parser_mem_initializer_list (parser);
13451
13452 return true;
13453 }
13454
13455 /* Parse a mem-initializer-list.
13456
13457 mem-initializer-list:
13458 mem-initializer ... [opt]
13459 mem-initializer ... [opt] , mem-initializer-list */
13460
13461 static void
13462 cp_parser_mem_initializer_list (cp_parser* parser)
13463 {
13464 tree mem_initializer_list = NULL_TREE;
13465 tree target_ctor = error_mark_node;
13466 cp_token *token = cp_lexer_peek_token (parser->lexer);
13467
13468 /* Let the semantic analysis code know that we are starting the
13469 mem-initializer-list. */
13470 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13471 error_at (token->location,
13472 "only constructors take member initializers");
13473
13474 /* Loop through the list. */
13475 while (true)
13476 {
13477 tree mem_initializer;
13478
13479 token = cp_lexer_peek_token (parser->lexer);
13480 /* Parse the mem-initializer. */
13481 mem_initializer = cp_parser_mem_initializer (parser);
13482 /* If the next token is a `...', we're expanding member initializers. */
13483 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13484 {
13485 /* Consume the `...'. */
13486 cp_lexer_consume_token (parser->lexer);
13487
13488 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13489 can be expanded but members cannot. */
13490 if (mem_initializer != error_mark_node
13491 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13492 {
13493 error_at (token->location,
13494 "cannot expand initializer for member %<%D%>",
13495 TREE_PURPOSE (mem_initializer));
13496 mem_initializer = error_mark_node;
13497 }
13498
13499 /* Construct the pack expansion type. */
13500 if (mem_initializer != error_mark_node)
13501 mem_initializer = make_pack_expansion (mem_initializer);
13502 }
13503 if (target_ctor != error_mark_node
13504 && mem_initializer != error_mark_node)
13505 {
13506 error ("mem-initializer for %qD follows constructor delegation",
13507 TREE_PURPOSE (mem_initializer));
13508 mem_initializer = error_mark_node;
13509 }
13510 /* Look for a target constructor. */
13511 if (mem_initializer != error_mark_node
13512 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13513 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13514 {
13515 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13516 if (mem_initializer_list)
13517 {
13518 error ("constructor delegation follows mem-initializer for %qD",
13519 TREE_PURPOSE (mem_initializer_list));
13520 mem_initializer = error_mark_node;
13521 }
13522 target_ctor = mem_initializer;
13523 }
13524 /* Add it to the list, unless it was erroneous. */
13525 if (mem_initializer != error_mark_node)
13526 {
13527 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13528 mem_initializer_list = mem_initializer;
13529 }
13530 /* If the next token is not a `,', we're done. */
13531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13532 break;
13533 /* Consume the `,' token. */
13534 cp_lexer_consume_token (parser->lexer);
13535 }
13536
13537 /* Perform semantic analysis. */
13538 if (DECL_CONSTRUCTOR_P (current_function_decl))
13539 finish_mem_initializers (mem_initializer_list);
13540 }
13541
13542 /* Parse a mem-initializer.
13543
13544 mem-initializer:
13545 mem-initializer-id ( expression-list [opt] )
13546 mem-initializer-id braced-init-list
13547
13548 GNU extension:
13549
13550 mem-initializer:
13551 ( expression-list [opt] )
13552
13553 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13554 class) or FIELD_DECL (for a non-static data member) to initialize;
13555 the TREE_VALUE is the expression-list. An empty initialization
13556 list is represented by void_list_node. */
13557
13558 static tree
13559 cp_parser_mem_initializer (cp_parser* parser)
13560 {
13561 tree mem_initializer_id;
13562 tree expression_list;
13563 tree member;
13564 cp_token *token = cp_lexer_peek_token (parser->lexer);
13565
13566 /* Find out what is being initialized. */
13567 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13568 {
13569 permerror (token->location,
13570 "anachronistic old-style base class initializer");
13571 mem_initializer_id = NULL_TREE;
13572 }
13573 else
13574 {
13575 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13576 if (mem_initializer_id == error_mark_node)
13577 return mem_initializer_id;
13578 }
13579 member = expand_member_init (mem_initializer_id);
13580 if (member && !DECL_P (member))
13581 in_base_initializer = 1;
13582
13583 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13584 {
13585 bool expr_non_constant_p;
13586 cp_lexer_set_source_position (parser->lexer);
13587 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13588 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13589 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13590 expression_list = build_tree_list (NULL_TREE, expression_list);
13591 }
13592 else
13593 {
13594 vec<tree, va_gc> *vec;
13595 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13596 /*cast_p=*/false,
13597 /*allow_expansion_p=*/true,
13598 /*non_constant_p=*/NULL);
13599 if (vec == NULL)
13600 return error_mark_node;
13601 expression_list = build_tree_list_vec (vec);
13602 release_tree_vector (vec);
13603 }
13604
13605 if (expression_list == error_mark_node)
13606 return error_mark_node;
13607 if (!expression_list)
13608 expression_list = void_type_node;
13609
13610 in_base_initializer = 0;
13611
13612 return member ? build_tree_list (member, expression_list) : error_mark_node;
13613 }
13614
13615 /* Parse a mem-initializer-id.
13616
13617 mem-initializer-id:
13618 :: [opt] nested-name-specifier [opt] class-name
13619 decltype-specifier (C++11)
13620 identifier
13621
13622 Returns a TYPE indicating the class to be initialized for the first
13623 production (and the second in C++11). Returns an IDENTIFIER_NODE
13624 indicating the data member to be initialized for the last production. */
13625
13626 static tree
13627 cp_parser_mem_initializer_id (cp_parser* parser)
13628 {
13629 bool global_scope_p;
13630 bool nested_name_specifier_p;
13631 bool template_p = false;
13632 tree id;
13633
13634 cp_token *token = cp_lexer_peek_token (parser->lexer);
13635
13636 /* `typename' is not allowed in this context ([temp.res]). */
13637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13638 {
13639 error_at (token->location,
13640 "keyword %<typename%> not allowed in this context (a qualified "
13641 "member initializer is implicitly a type)");
13642 cp_lexer_consume_token (parser->lexer);
13643 }
13644 /* Look for the optional `::' operator. */
13645 global_scope_p
13646 = (cp_parser_global_scope_opt (parser,
13647 /*current_scope_valid_p=*/false)
13648 != NULL_TREE);
13649 /* Look for the optional nested-name-specifier. The simplest way to
13650 implement:
13651
13652 [temp.res]
13653
13654 The keyword `typename' is not permitted in a base-specifier or
13655 mem-initializer; in these contexts a qualified name that
13656 depends on a template-parameter is implicitly assumed to be a
13657 type name.
13658
13659 is to assume that we have seen the `typename' keyword at this
13660 point. */
13661 nested_name_specifier_p
13662 = (cp_parser_nested_name_specifier_opt (parser,
13663 /*typename_keyword_p=*/true,
13664 /*check_dependency_p=*/true,
13665 /*type_p=*/true,
13666 /*is_declaration=*/true)
13667 != NULL_TREE);
13668 if (nested_name_specifier_p)
13669 template_p = cp_parser_optional_template_keyword (parser);
13670 /* If there is a `::' operator or a nested-name-specifier, then we
13671 are definitely looking for a class-name. */
13672 if (global_scope_p || nested_name_specifier_p)
13673 return cp_parser_class_name (parser,
13674 /*typename_keyword_p=*/true,
13675 /*template_keyword_p=*/template_p,
13676 typename_type,
13677 /*check_dependency_p=*/true,
13678 /*class_head_p=*/false,
13679 /*is_declaration=*/true);
13680 /* Otherwise, we could also be looking for an ordinary identifier. */
13681 cp_parser_parse_tentatively (parser);
13682 if (cp_lexer_next_token_is_decltype (parser->lexer))
13683 /* Try a decltype-specifier. */
13684 id = cp_parser_decltype (parser);
13685 else
13686 /* Otherwise, try a class-name. */
13687 id = cp_parser_class_name (parser,
13688 /*typename_keyword_p=*/true,
13689 /*template_keyword_p=*/false,
13690 none_type,
13691 /*check_dependency_p=*/true,
13692 /*class_head_p=*/false,
13693 /*is_declaration=*/true);
13694 /* If we found one, we're done. */
13695 if (cp_parser_parse_definitely (parser))
13696 return id;
13697 /* Otherwise, look for an ordinary identifier. */
13698 return cp_parser_identifier (parser);
13699 }
13700
13701 /* Overloading [gram.over] */
13702
13703 /* Parse an operator-function-id.
13704
13705 operator-function-id:
13706 operator operator
13707
13708 Returns an IDENTIFIER_NODE for the operator which is a
13709 human-readable spelling of the identifier, e.g., `operator +'. */
13710
13711 static cp_expr
13712 cp_parser_operator_function_id (cp_parser* parser)
13713 {
13714 /* Look for the `operator' keyword. */
13715 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13716 return error_mark_node;
13717 /* And then the name of the operator itself. */
13718 return cp_parser_operator (parser);
13719 }
13720
13721 /* Return an identifier node for a user-defined literal operator.
13722 The suffix identifier is chained to the operator name identifier. */
13723
13724 static tree
13725 cp_literal_operator_id (const char* name)
13726 {
13727 tree identifier;
13728 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13729 + strlen (name) + 10);
13730 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13731 identifier = get_identifier (buffer);
13732
13733 return identifier;
13734 }
13735
13736 /* Parse an operator.
13737
13738 operator:
13739 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13740 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13741 || ++ -- , ->* -> () []
13742
13743 GNU Extensions:
13744
13745 operator:
13746 <? >? <?= >?=
13747
13748 Returns an IDENTIFIER_NODE for the operator which is a
13749 human-readable spelling of the identifier, e.g., `operator +'. */
13750
13751 static cp_expr
13752 cp_parser_operator (cp_parser* parser)
13753 {
13754 tree id = NULL_TREE;
13755 cp_token *token;
13756 bool utf8 = false;
13757
13758 /* Peek at the next token. */
13759 token = cp_lexer_peek_token (parser->lexer);
13760
13761 location_t start_loc = token->location;
13762
13763 /* Figure out which operator we have. */
13764 switch (token->type)
13765 {
13766 case CPP_KEYWORD:
13767 {
13768 enum tree_code op;
13769
13770 /* The keyword should be either `new' or `delete'. */
13771 if (token->keyword == RID_NEW)
13772 op = NEW_EXPR;
13773 else if (token->keyword == RID_DELETE)
13774 op = DELETE_EXPR;
13775 else
13776 break;
13777
13778 /* Consume the `new' or `delete' token. */
13779 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13780
13781 /* Peek at the next token. */
13782 token = cp_lexer_peek_token (parser->lexer);
13783 /* If it's a `[' token then this is the array variant of the
13784 operator. */
13785 if (token->type == CPP_OPEN_SQUARE)
13786 {
13787 /* Consume the `[' token. */
13788 cp_lexer_consume_token (parser->lexer);
13789 /* Look for the `]' token. */
13790 end_loc = cp_parser_require (parser, CPP_CLOSE_SQUARE,
13791 RT_CLOSE_SQUARE)->location;
13792 id = ansi_opname (op == NEW_EXPR
13793 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13794 }
13795 /* Otherwise, we have the non-array variant. */
13796 else
13797 id = ansi_opname (op);
13798
13799 location_t loc = make_location (start_loc, start_loc, end_loc);
13800
13801 return cp_expr (id, loc);
13802 }
13803
13804 case CPP_PLUS:
13805 id = ansi_opname (PLUS_EXPR);
13806 break;
13807
13808 case CPP_MINUS:
13809 id = ansi_opname (MINUS_EXPR);
13810 break;
13811
13812 case CPP_MULT:
13813 id = ansi_opname (MULT_EXPR);
13814 break;
13815
13816 case CPP_DIV:
13817 id = ansi_opname (TRUNC_DIV_EXPR);
13818 break;
13819
13820 case CPP_MOD:
13821 id = ansi_opname (TRUNC_MOD_EXPR);
13822 break;
13823
13824 case CPP_XOR:
13825 id = ansi_opname (BIT_XOR_EXPR);
13826 break;
13827
13828 case CPP_AND:
13829 id = ansi_opname (BIT_AND_EXPR);
13830 break;
13831
13832 case CPP_OR:
13833 id = ansi_opname (BIT_IOR_EXPR);
13834 break;
13835
13836 case CPP_COMPL:
13837 id = ansi_opname (BIT_NOT_EXPR);
13838 break;
13839
13840 case CPP_NOT:
13841 id = ansi_opname (TRUTH_NOT_EXPR);
13842 break;
13843
13844 case CPP_EQ:
13845 id = ansi_assopname (NOP_EXPR);
13846 break;
13847
13848 case CPP_LESS:
13849 id = ansi_opname (LT_EXPR);
13850 break;
13851
13852 case CPP_GREATER:
13853 id = ansi_opname (GT_EXPR);
13854 break;
13855
13856 case CPP_PLUS_EQ:
13857 id = ansi_assopname (PLUS_EXPR);
13858 break;
13859
13860 case CPP_MINUS_EQ:
13861 id = ansi_assopname (MINUS_EXPR);
13862 break;
13863
13864 case CPP_MULT_EQ:
13865 id = ansi_assopname (MULT_EXPR);
13866 break;
13867
13868 case CPP_DIV_EQ:
13869 id = ansi_assopname (TRUNC_DIV_EXPR);
13870 break;
13871
13872 case CPP_MOD_EQ:
13873 id = ansi_assopname (TRUNC_MOD_EXPR);
13874 break;
13875
13876 case CPP_XOR_EQ:
13877 id = ansi_assopname (BIT_XOR_EXPR);
13878 break;
13879
13880 case CPP_AND_EQ:
13881 id = ansi_assopname (BIT_AND_EXPR);
13882 break;
13883
13884 case CPP_OR_EQ:
13885 id = ansi_assopname (BIT_IOR_EXPR);
13886 break;
13887
13888 case CPP_LSHIFT:
13889 id = ansi_opname (LSHIFT_EXPR);
13890 break;
13891
13892 case CPP_RSHIFT:
13893 id = ansi_opname (RSHIFT_EXPR);
13894 break;
13895
13896 case CPP_LSHIFT_EQ:
13897 id = ansi_assopname (LSHIFT_EXPR);
13898 break;
13899
13900 case CPP_RSHIFT_EQ:
13901 id = ansi_assopname (RSHIFT_EXPR);
13902 break;
13903
13904 case CPP_EQ_EQ:
13905 id = ansi_opname (EQ_EXPR);
13906 break;
13907
13908 case CPP_NOT_EQ:
13909 id = ansi_opname (NE_EXPR);
13910 break;
13911
13912 case CPP_LESS_EQ:
13913 id = ansi_opname (LE_EXPR);
13914 break;
13915
13916 case CPP_GREATER_EQ:
13917 id = ansi_opname (GE_EXPR);
13918 break;
13919
13920 case CPP_AND_AND:
13921 id = ansi_opname (TRUTH_ANDIF_EXPR);
13922 break;
13923
13924 case CPP_OR_OR:
13925 id = ansi_opname (TRUTH_ORIF_EXPR);
13926 break;
13927
13928 case CPP_PLUS_PLUS:
13929 id = ansi_opname (POSTINCREMENT_EXPR);
13930 break;
13931
13932 case CPP_MINUS_MINUS:
13933 id = ansi_opname (PREDECREMENT_EXPR);
13934 break;
13935
13936 case CPP_COMMA:
13937 id = ansi_opname (COMPOUND_EXPR);
13938 break;
13939
13940 case CPP_DEREF_STAR:
13941 id = ansi_opname (MEMBER_REF);
13942 break;
13943
13944 case CPP_DEREF:
13945 id = ansi_opname (COMPONENT_REF);
13946 break;
13947
13948 case CPP_OPEN_PAREN:
13949 /* Consume the `('. */
13950 cp_lexer_consume_token (parser->lexer);
13951 /* Look for the matching `)'. */
13952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13953 return ansi_opname (CALL_EXPR);
13954
13955 case CPP_OPEN_SQUARE:
13956 /* Consume the `['. */
13957 cp_lexer_consume_token (parser->lexer);
13958 /* Look for the matching `]'. */
13959 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13960 return ansi_opname (ARRAY_REF);
13961
13962 case CPP_UTF8STRING:
13963 case CPP_UTF8STRING_USERDEF:
13964 utf8 = true;
13965 case CPP_STRING:
13966 case CPP_WSTRING:
13967 case CPP_STRING16:
13968 case CPP_STRING32:
13969 case CPP_STRING_USERDEF:
13970 case CPP_WSTRING_USERDEF:
13971 case CPP_STRING16_USERDEF:
13972 case CPP_STRING32_USERDEF:
13973 {
13974 tree str, string_tree;
13975 int sz, len;
13976
13977 if (cxx_dialect == cxx98)
13978 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13979
13980 /* Consume the string. */
13981 str = cp_parser_string_literal (parser, /*translate=*/true,
13982 /*wide_ok=*/true, /*lookup_udlit=*/false);
13983 if (str == error_mark_node)
13984 return error_mark_node;
13985 else if (TREE_CODE (str) == USERDEF_LITERAL)
13986 {
13987 string_tree = USERDEF_LITERAL_VALUE (str);
13988 id = USERDEF_LITERAL_SUFFIX_ID (str);
13989 }
13990 else
13991 {
13992 string_tree = str;
13993 /* Look for the suffix identifier. */
13994 token = cp_lexer_peek_token (parser->lexer);
13995 if (token->type == CPP_NAME)
13996 id = cp_parser_identifier (parser);
13997 else if (token->type == CPP_KEYWORD)
13998 {
13999 error ("unexpected keyword;"
14000 " remove space between quotes and suffix identifier");
14001 return error_mark_node;
14002 }
14003 else
14004 {
14005 error ("expected suffix identifier");
14006 return error_mark_node;
14007 }
14008 }
14009 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14010 (TREE_TYPE (TREE_TYPE (string_tree))));
14011 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14012 if (len != 0)
14013 {
14014 error ("expected empty string after %<operator%> keyword");
14015 return error_mark_node;
14016 }
14017 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14018 != char_type_node)
14019 {
14020 error ("invalid encoding prefix in literal operator");
14021 return error_mark_node;
14022 }
14023 if (id != error_mark_node)
14024 {
14025 const char *name = IDENTIFIER_POINTER (id);
14026 id = cp_literal_operator_id (name);
14027 }
14028 return id;
14029 }
14030
14031 default:
14032 /* Anything else is an error. */
14033 break;
14034 }
14035
14036 /* If we have selected an identifier, we need to consume the
14037 operator token. */
14038 if (id)
14039 cp_lexer_consume_token (parser->lexer);
14040 /* Otherwise, no valid operator name was present. */
14041 else
14042 {
14043 cp_parser_error (parser, "expected operator");
14044 id = error_mark_node;
14045 }
14046
14047 return cp_expr (id, start_loc);
14048 }
14049
14050 /* Parse a template-declaration.
14051
14052 template-declaration:
14053 export [opt] template < template-parameter-list > declaration
14054
14055 If MEMBER_P is TRUE, this template-declaration occurs within a
14056 class-specifier.
14057
14058 The grammar rule given by the standard isn't correct. What
14059 is really meant is:
14060
14061 template-declaration:
14062 export [opt] template-parameter-list-seq
14063 decl-specifier-seq [opt] init-declarator [opt] ;
14064 export [opt] template-parameter-list-seq
14065 function-definition
14066
14067 template-parameter-list-seq:
14068 template-parameter-list-seq [opt]
14069 template < template-parameter-list >
14070
14071 Concept Extensions:
14072
14073 template-parameter-list-seq:
14074 template < template-parameter-list > requires-clause [opt]
14075
14076 requires-clause:
14077 requires logical-or-expression */
14078
14079 static void
14080 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14081 {
14082 /* Check for `export'. */
14083 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14084 {
14085 /* Consume the `export' token. */
14086 cp_lexer_consume_token (parser->lexer);
14087 /* Warn that we do not support `export'. */
14088 warning (0, "keyword %<export%> not implemented, and will be ignored");
14089 }
14090
14091 cp_parser_template_declaration_after_export (parser, member_p);
14092 }
14093
14094 /* Parse a template-parameter-list.
14095
14096 template-parameter-list:
14097 template-parameter
14098 template-parameter-list , template-parameter
14099
14100 Returns a TREE_LIST. Each node represents a template parameter.
14101 The nodes are connected via their TREE_CHAINs. */
14102
14103 static tree
14104 cp_parser_template_parameter_list (cp_parser* parser)
14105 {
14106 tree parameter_list = NULL_TREE;
14107
14108 begin_template_parm_list ();
14109
14110 /* The loop below parses the template parms. We first need to know
14111 the total number of template parms to be able to compute proper
14112 canonical types of each dependent type. So after the loop, when
14113 we know the total number of template parms,
14114 end_template_parm_list computes the proper canonical types and
14115 fixes up the dependent types accordingly. */
14116 while (true)
14117 {
14118 tree parameter;
14119 bool is_non_type;
14120 bool is_parameter_pack;
14121 location_t parm_loc;
14122
14123 /* Parse the template-parameter. */
14124 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14125 parameter = cp_parser_template_parameter (parser,
14126 &is_non_type,
14127 &is_parameter_pack);
14128 /* Add it to the list. */
14129 if (parameter != error_mark_node)
14130 parameter_list = process_template_parm (parameter_list,
14131 parm_loc,
14132 parameter,
14133 is_non_type,
14134 is_parameter_pack);
14135 else
14136 {
14137 tree err_parm = build_tree_list (parameter, parameter);
14138 parameter_list = chainon (parameter_list, err_parm);
14139 }
14140
14141 /* If the next token is not a `,', we're done. */
14142 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14143 break;
14144 /* Otherwise, consume the `,' token. */
14145 cp_lexer_consume_token (parser->lexer);
14146 }
14147
14148 return end_template_parm_list (parameter_list);
14149 }
14150
14151 /* Parse a introduction-list.
14152
14153 introduction-list:
14154 introduced-parameter
14155 introduction-list , introduced-parameter
14156
14157 introduced-parameter:
14158 ...[opt] identifier
14159
14160 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14161 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14162 WILDCARD_DECL will also have DECL_NAME set and token location in
14163 DECL_SOURCE_LOCATION. */
14164
14165 static tree
14166 cp_parser_introduction_list (cp_parser *parser)
14167 {
14168 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14169
14170 while (true)
14171 {
14172 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14173 if (is_pack)
14174 cp_lexer_consume_token (parser->lexer);
14175
14176 /* Build placeholder. */
14177 tree parm = build_nt (WILDCARD_DECL);
14178 DECL_SOURCE_LOCATION (parm)
14179 = cp_lexer_peek_token (parser->lexer)->location;
14180 DECL_NAME (parm) = cp_parser_identifier (parser);
14181 WILDCARD_PACK_P (parm) = is_pack;
14182 vec_safe_push (introduction_vec, parm);
14183
14184 /* If the next token is not a `,', we're done. */
14185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14186 break;
14187 /* Otherwise, consume the `,' token. */
14188 cp_lexer_consume_token (parser->lexer);
14189 }
14190
14191 /* Convert the vec into a TREE_VEC. */
14192 tree introduction_list = make_tree_vec (introduction_vec->length ());
14193 unsigned int n;
14194 tree parm;
14195 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14196 TREE_VEC_ELT (introduction_list, n) = parm;
14197
14198 release_tree_vector (introduction_vec);
14199 return introduction_list;
14200 }
14201
14202 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14203 is an abstract declarator. */
14204
14205 static inline cp_declarator*
14206 get_id_declarator (cp_declarator *declarator)
14207 {
14208 cp_declarator *d = declarator;
14209 while (d && d->kind != cdk_id)
14210 d = d->declarator;
14211 return d;
14212 }
14213
14214 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14215 is an abstract declarator. */
14216
14217 static inline tree
14218 get_unqualified_id (cp_declarator *declarator)
14219 {
14220 declarator = get_id_declarator (declarator);
14221 if (declarator)
14222 return declarator->u.id.unqualified_name;
14223 else
14224 return NULL_TREE;
14225 }
14226
14227 /* Returns true if DECL represents a constrained-parameter. */
14228
14229 static inline bool
14230 is_constrained_parameter (tree decl)
14231 {
14232 return (decl
14233 && TREE_CODE (decl) == TYPE_DECL
14234 && CONSTRAINED_PARM_CONCEPT (decl)
14235 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14236 }
14237
14238 /* Returns true if PARM declares a constrained-parameter. */
14239
14240 static inline bool
14241 is_constrained_parameter (cp_parameter_declarator *parm)
14242 {
14243 return is_constrained_parameter (parm->decl_specifiers.type);
14244 }
14245
14246 /* Check that the type parameter is only a declarator-id, and that its
14247 type is not cv-qualified. */
14248
14249 bool
14250 cp_parser_check_constrained_type_parm (cp_parser *parser,
14251 cp_parameter_declarator *parm)
14252 {
14253 if (!parm->declarator)
14254 return true;
14255
14256 if (parm->declarator->kind != cdk_id)
14257 {
14258 cp_parser_error (parser, "invalid constrained type parameter");
14259 return false;
14260 }
14261
14262 /* Don't allow cv-qualified type parameters. */
14263 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14264 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14265 {
14266 cp_parser_error (parser, "cv-qualified type parameter");
14267 return false;
14268 }
14269
14270 return true;
14271 }
14272
14273 /* Finish parsing/processing a template type parameter and checking
14274 various restrictions. */
14275
14276 static inline tree
14277 cp_parser_constrained_type_template_parm (cp_parser *parser,
14278 tree id,
14279 cp_parameter_declarator* parmdecl)
14280 {
14281 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14282 return finish_template_type_parm (class_type_node, id);
14283 else
14284 return error_mark_node;
14285 }
14286
14287 static tree
14288 finish_constrained_template_template_parm (tree proto, tree id)
14289 {
14290 /* FIXME: This should probably be copied, and we may need to adjust
14291 the template parameter depths. */
14292 tree saved_parms = current_template_parms;
14293 begin_template_parm_list ();
14294 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14295 end_template_parm_list ();
14296
14297 tree parm = finish_template_template_parm (class_type_node, id);
14298 current_template_parms = saved_parms;
14299
14300 return parm;
14301 }
14302
14303 /* Finish parsing/processing a template template parameter by borrowing
14304 the template parameter list from the prototype parameter. */
14305
14306 static tree
14307 cp_parser_constrained_template_template_parm (cp_parser *parser,
14308 tree proto,
14309 tree id,
14310 cp_parameter_declarator *parmdecl)
14311 {
14312 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14313 return error_mark_node;
14314 return finish_constrained_template_template_parm (proto, id);
14315 }
14316
14317 /* Create a new non-type template parameter from the given PARM
14318 declarator. */
14319
14320 static tree
14321 constrained_non_type_template_parm (bool *is_non_type,
14322 cp_parameter_declarator *parm)
14323 {
14324 *is_non_type = true;
14325 cp_declarator *decl = parm->declarator;
14326 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14327 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14328 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14329 }
14330
14331 /* Build a constrained template parameter based on the PARMDECL
14332 declarator. The type of PARMDECL is the constrained type, which
14333 refers to the prototype template parameter that ultimately
14334 specifies the type of the declared parameter. */
14335
14336 static tree
14337 finish_constrained_parameter (cp_parser *parser,
14338 cp_parameter_declarator *parmdecl,
14339 bool *is_non_type,
14340 bool *is_parameter_pack)
14341 {
14342 tree decl = parmdecl->decl_specifiers.type;
14343 tree id = get_unqualified_id (parmdecl->declarator);
14344 tree def = parmdecl->default_argument;
14345 tree proto = DECL_INITIAL (decl);
14346
14347 /* A template parameter constrained by a variadic concept shall also
14348 be declared as a template parameter pack. */
14349 bool is_variadic = template_parameter_pack_p (proto);
14350 if (is_variadic && !*is_parameter_pack)
14351 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14352
14353 /* Build the parameter. Return an error if the declarator was invalid. */
14354 tree parm;
14355 if (TREE_CODE (proto) == TYPE_DECL)
14356 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14357 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14358 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14359 parmdecl);
14360 else
14361 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14362 if (parm == error_mark_node)
14363 return error_mark_node;
14364
14365 /* Finish the parameter decl and create a node attaching the
14366 default argument and constraint. */
14367 parm = build_tree_list (def, parm);
14368 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14369
14370 return parm;
14371 }
14372
14373 /* Returns true if the parsed type actually represents the declaration
14374 of a type template-parameter. */
14375
14376 static inline bool
14377 declares_constrained_type_template_parameter (tree type)
14378 {
14379 return (is_constrained_parameter (type)
14380 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14381 }
14382
14383
14384 /* Returns true if the parsed type actually represents the declaration of
14385 a template template-parameter. */
14386
14387 static bool
14388 declares_constrained_template_template_parameter (tree type)
14389 {
14390 return (is_constrained_parameter (type)
14391 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14392 }
14393
14394 /* Parse a default argument for a type template-parameter.
14395 Note that diagnostics are handled in cp_parser_template_parameter. */
14396
14397 static tree
14398 cp_parser_default_type_template_argument (cp_parser *parser)
14399 {
14400 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14401
14402 /* Consume the `=' token. */
14403 cp_lexer_consume_token (parser->lexer);
14404
14405 cp_token *token = cp_lexer_peek_token (parser->lexer);
14406
14407 /* Parse the default-argument. */
14408 push_deferring_access_checks (dk_no_deferred);
14409 tree default_argument = cp_parser_type_id (parser);
14410 pop_deferring_access_checks ();
14411
14412 if (flag_concepts && type_uses_auto (default_argument))
14413 {
14414 error_at (token->location,
14415 "invalid use of %<auto%> in default template argument");
14416 return error_mark_node;
14417 }
14418
14419 return default_argument;
14420 }
14421
14422 /* Parse a default argument for a template template-parameter. */
14423
14424 static tree
14425 cp_parser_default_template_template_argument (cp_parser *parser)
14426 {
14427 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14428
14429 bool is_template;
14430
14431 /* Consume the `='. */
14432 cp_lexer_consume_token (parser->lexer);
14433 /* Parse the id-expression. */
14434 push_deferring_access_checks (dk_no_deferred);
14435 /* save token before parsing the id-expression, for error
14436 reporting */
14437 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14438 tree default_argument
14439 = cp_parser_id_expression (parser,
14440 /*template_keyword_p=*/false,
14441 /*check_dependency_p=*/true,
14442 /*template_p=*/&is_template,
14443 /*declarator_p=*/false,
14444 /*optional_p=*/false);
14445 if (TREE_CODE (default_argument) == TYPE_DECL)
14446 /* If the id-expression was a template-id that refers to
14447 a template-class, we already have the declaration here,
14448 so no further lookup is needed. */
14449 ;
14450 else
14451 /* Look up the name. */
14452 default_argument
14453 = cp_parser_lookup_name (parser, default_argument,
14454 none_type,
14455 /*is_template=*/is_template,
14456 /*is_namespace=*/false,
14457 /*check_dependency=*/true,
14458 /*ambiguous_decls=*/NULL,
14459 token->location);
14460 /* See if the default argument is valid. */
14461 default_argument = check_template_template_default_arg (default_argument);
14462 pop_deferring_access_checks ();
14463 return default_argument;
14464 }
14465
14466 /* Parse a template-parameter.
14467
14468 template-parameter:
14469 type-parameter
14470 parameter-declaration
14471
14472 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14473 the parameter. The TREE_PURPOSE is the default value, if any.
14474 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14475 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14476 set to true iff this parameter is a parameter pack. */
14477
14478 static tree
14479 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14480 bool *is_parameter_pack)
14481 {
14482 cp_token *token;
14483 cp_parameter_declarator *parameter_declarator;
14484 tree parm;
14485
14486 /* Assume it is a type parameter or a template parameter. */
14487 *is_non_type = false;
14488 /* Assume it not a parameter pack. */
14489 *is_parameter_pack = false;
14490 /* Peek at the next token. */
14491 token = cp_lexer_peek_token (parser->lexer);
14492 /* If it is `class' or `template', we have a type-parameter. */
14493 if (token->keyword == RID_TEMPLATE)
14494 return cp_parser_type_parameter (parser, is_parameter_pack);
14495 /* If it is `class' or `typename' we do not know yet whether it is a
14496 type parameter or a non-type parameter. Consider:
14497
14498 template <typename T, typename T::X X> ...
14499
14500 or:
14501
14502 template <class C, class D*> ...
14503
14504 Here, the first parameter is a type parameter, and the second is
14505 a non-type parameter. We can tell by looking at the token after
14506 the identifier -- if it is a `,', `=', or `>' then we have a type
14507 parameter. */
14508 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14509 {
14510 /* Peek at the token after `class' or `typename'. */
14511 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14512 /* If it's an ellipsis, we have a template type parameter
14513 pack. */
14514 if (token->type == CPP_ELLIPSIS)
14515 return cp_parser_type_parameter (parser, is_parameter_pack);
14516 /* If it's an identifier, skip it. */
14517 if (token->type == CPP_NAME)
14518 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14519 /* Now, see if the token looks like the end of a template
14520 parameter. */
14521 if (token->type == CPP_COMMA
14522 || token->type == CPP_EQ
14523 || token->type == CPP_GREATER)
14524 return cp_parser_type_parameter (parser, is_parameter_pack);
14525 }
14526
14527 /* Otherwise, it is a non-type parameter or a constrained parameter.
14528
14529 [temp.param]
14530
14531 When parsing a default template-argument for a non-type
14532 template-parameter, the first non-nested `>' is taken as the end
14533 of the template parameter-list rather than a greater-than
14534 operator. */
14535 parameter_declarator
14536 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14537 /*parenthesized_p=*/NULL);
14538
14539 if (!parameter_declarator)
14540 return error_mark_node;
14541
14542 /* If the parameter declaration is marked as a parameter pack, set
14543 *IS_PARAMETER_PACK to notify the caller. */
14544 if (parameter_declarator->template_parameter_pack_p)
14545 *is_parameter_pack = true;
14546
14547 if (parameter_declarator->default_argument)
14548 {
14549 /* Can happen in some cases of erroneous input (c++/34892). */
14550 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14551 /* Consume the `...' for better error recovery. */
14552 cp_lexer_consume_token (parser->lexer);
14553 }
14554
14555 // The parameter may have been constrained.
14556 if (is_constrained_parameter (parameter_declarator))
14557 return finish_constrained_parameter (parser,
14558 parameter_declarator,
14559 is_non_type,
14560 is_parameter_pack);
14561
14562 // Now we're sure that the parameter is a non-type parameter.
14563 *is_non_type = true;
14564
14565 parm = grokdeclarator (parameter_declarator->declarator,
14566 &parameter_declarator->decl_specifiers,
14567 TPARM, /*initialized=*/0,
14568 /*attrlist=*/NULL);
14569 if (parm == error_mark_node)
14570 return error_mark_node;
14571
14572 return build_tree_list (parameter_declarator->default_argument, parm);
14573 }
14574
14575 /* Parse a type-parameter.
14576
14577 type-parameter:
14578 class identifier [opt]
14579 class identifier [opt] = type-id
14580 typename identifier [opt]
14581 typename identifier [opt] = type-id
14582 template < template-parameter-list > class identifier [opt]
14583 template < template-parameter-list > class identifier [opt]
14584 = id-expression
14585
14586 GNU Extension (variadic templates):
14587
14588 type-parameter:
14589 class ... identifier [opt]
14590 typename ... identifier [opt]
14591
14592 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14593 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14594 the declaration of the parameter.
14595
14596 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14597
14598 static tree
14599 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14600 {
14601 cp_token *token;
14602 tree parameter;
14603
14604 /* Look for a keyword to tell us what kind of parameter this is. */
14605 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14606 if (!token)
14607 return error_mark_node;
14608
14609 switch (token->keyword)
14610 {
14611 case RID_CLASS:
14612 case RID_TYPENAME:
14613 {
14614 tree identifier;
14615 tree default_argument;
14616
14617 /* If the next token is an ellipsis, we have a template
14618 argument pack. */
14619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14620 {
14621 /* Consume the `...' token. */
14622 cp_lexer_consume_token (parser->lexer);
14623 maybe_warn_variadic_templates ();
14624
14625 *is_parameter_pack = true;
14626 }
14627
14628 /* If the next token is an identifier, then it names the
14629 parameter. */
14630 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14631 identifier = cp_parser_identifier (parser);
14632 else
14633 identifier = NULL_TREE;
14634
14635 /* Create the parameter. */
14636 parameter = finish_template_type_parm (class_type_node, identifier);
14637
14638 /* If the next token is an `=', we have a default argument. */
14639 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14640 {
14641 default_argument
14642 = cp_parser_default_type_template_argument (parser);
14643
14644 /* Template parameter packs cannot have default
14645 arguments. */
14646 if (*is_parameter_pack)
14647 {
14648 if (identifier)
14649 error_at (token->location,
14650 "template parameter pack %qD cannot have a "
14651 "default argument", identifier);
14652 else
14653 error_at (token->location,
14654 "template parameter packs cannot have "
14655 "default arguments");
14656 default_argument = NULL_TREE;
14657 }
14658 else if (check_for_bare_parameter_packs (default_argument))
14659 default_argument = error_mark_node;
14660 }
14661 else
14662 default_argument = NULL_TREE;
14663
14664 /* Create the combined representation of the parameter and the
14665 default argument. */
14666 parameter = build_tree_list (default_argument, parameter);
14667 }
14668 break;
14669
14670 case RID_TEMPLATE:
14671 {
14672 tree identifier;
14673 tree default_argument;
14674
14675 /* Look for the `<'. */
14676 cp_parser_require (parser, CPP_LESS, RT_LESS);
14677 /* Parse the template-parameter-list. */
14678 cp_parser_template_parameter_list (parser);
14679 /* Look for the `>'. */
14680 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14681
14682 // If template requirements are present, parse them.
14683 tree reqs = get_shorthand_constraints (current_template_parms);
14684 if (tree r = cp_parser_requires_clause_opt (parser))
14685 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14686 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14687
14688 /* Look for the `class' or 'typename' keywords. */
14689 cp_parser_type_parameter_key (parser);
14690 /* If the next token is an ellipsis, we have a template
14691 argument pack. */
14692 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14693 {
14694 /* Consume the `...' token. */
14695 cp_lexer_consume_token (parser->lexer);
14696 maybe_warn_variadic_templates ();
14697
14698 *is_parameter_pack = true;
14699 }
14700 /* If the next token is an `=', then there is a
14701 default-argument. If the next token is a `>', we are at
14702 the end of the parameter-list. If the next token is a `,',
14703 then we are at the end of this parameter. */
14704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14705 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14706 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14707 {
14708 identifier = cp_parser_identifier (parser);
14709 /* Treat invalid names as if the parameter were nameless. */
14710 if (identifier == error_mark_node)
14711 identifier = NULL_TREE;
14712 }
14713 else
14714 identifier = NULL_TREE;
14715
14716 /* Create the template parameter. */
14717 parameter = finish_template_template_parm (class_type_node,
14718 identifier);
14719
14720 /* If the next token is an `=', then there is a
14721 default-argument. */
14722 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14723 {
14724 default_argument
14725 = cp_parser_default_template_template_argument (parser);
14726
14727 /* Template parameter packs cannot have default
14728 arguments. */
14729 if (*is_parameter_pack)
14730 {
14731 if (identifier)
14732 error_at (token->location,
14733 "template parameter pack %qD cannot "
14734 "have a default argument",
14735 identifier);
14736 else
14737 error_at (token->location, "template parameter packs cannot "
14738 "have default arguments");
14739 default_argument = NULL_TREE;
14740 }
14741 }
14742 else
14743 default_argument = NULL_TREE;
14744
14745 /* Create the combined representation of the parameter and the
14746 default argument. */
14747 parameter = build_tree_list (default_argument, parameter);
14748 }
14749 break;
14750
14751 default:
14752 gcc_unreachable ();
14753 break;
14754 }
14755
14756 return parameter;
14757 }
14758
14759 /* Parse a template-id.
14760
14761 template-id:
14762 template-name < template-argument-list [opt] >
14763
14764 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14765 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14766 returned. Otherwise, if the template-name names a function, or set
14767 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14768 names a class, returns a TYPE_DECL for the specialization.
14769
14770 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14771 uninstantiated templates. */
14772
14773 static tree
14774 cp_parser_template_id (cp_parser *parser,
14775 bool template_keyword_p,
14776 bool check_dependency_p,
14777 enum tag_types tag_type,
14778 bool is_declaration)
14779 {
14780 tree templ;
14781 tree arguments;
14782 tree template_id;
14783 cp_token_position start_of_id = 0;
14784 cp_token *next_token = NULL, *next_token_2 = NULL;
14785 bool is_identifier;
14786
14787 /* If the next token corresponds to a template-id, there is no need
14788 to reparse it. */
14789 next_token = cp_lexer_peek_token (parser->lexer);
14790 if (next_token->type == CPP_TEMPLATE_ID)
14791 {
14792 cp_lexer_consume_token (parser->lexer);
14793 return saved_checks_value (next_token->u.tree_check_value);
14794 }
14795
14796 /* Avoid performing name lookup if there is no possibility of
14797 finding a template-id. */
14798 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14799 || (next_token->type == CPP_NAME
14800 && !cp_parser_nth_token_starts_template_argument_list_p
14801 (parser, 2)))
14802 {
14803 cp_parser_error (parser, "expected template-id");
14804 return error_mark_node;
14805 }
14806
14807 /* Remember where the template-id starts. */
14808 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14809 start_of_id = cp_lexer_token_position (parser->lexer, false);
14810
14811 push_deferring_access_checks (dk_deferred);
14812
14813 /* Parse the template-name. */
14814 is_identifier = false;
14815 templ = cp_parser_template_name (parser, template_keyword_p,
14816 check_dependency_p,
14817 is_declaration,
14818 tag_type,
14819 &is_identifier);
14820 if (templ == error_mark_node || is_identifier)
14821 {
14822 pop_deferring_access_checks ();
14823 return templ;
14824 }
14825
14826 /* Since we're going to preserve any side-effects from this parse, set up a
14827 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14828 in the template arguments. */
14829 tentative_firewall firewall (parser);
14830
14831 /* If we find the sequence `[:' after a template-name, it's probably
14832 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14833 parse correctly the argument list. */
14834 next_token = cp_lexer_peek_token (parser->lexer);
14835 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14836 if (next_token->type == CPP_OPEN_SQUARE
14837 && next_token->flags & DIGRAPH
14838 && next_token_2->type == CPP_COLON
14839 && !(next_token_2->flags & PREV_WHITE))
14840 {
14841 cp_parser_parse_tentatively (parser);
14842 /* Change `:' into `::'. */
14843 next_token_2->type = CPP_SCOPE;
14844 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14845 CPP_LESS. */
14846 cp_lexer_consume_token (parser->lexer);
14847
14848 /* Parse the arguments. */
14849 arguments = cp_parser_enclosed_template_argument_list (parser);
14850 if (!cp_parser_parse_definitely (parser))
14851 {
14852 /* If we couldn't parse an argument list, then we revert our changes
14853 and return simply an error. Maybe this is not a template-id
14854 after all. */
14855 next_token_2->type = CPP_COLON;
14856 cp_parser_error (parser, "expected %<<%>");
14857 pop_deferring_access_checks ();
14858 return error_mark_node;
14859 }
14860 /* Otherwise, emit an error about the invalid digraph, but continue
14861 parsing because we got our argument list. */
14862 if (permerror (next_token->location,
14863 "%<<::%> cannot begin a template-argument list"))
14864 {
14865 static bool hint = false;
14866 inform (next_token->location,
14867 "%<<:%> is an alternate spelling for %<[%>."
14868 " Insert whitespace between %<<%> and %<::%>");
14869 if (!hint && !flag_permissive)
14870 {
14871 inform (next_token->location, "(if you use %<-fpermissive%> "
14872 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14873 "accept your code)");
14874 hint = true;
14875 }
14876 }
14877 }
14878 else
14879 {
14880 /* Look for the `<' that starts the template-argument-list. */
14881 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14882 {
14883 pop_deferring_access_checks ();
14884 return error_mark_node;
14885 }
14886 /* Parse the arguments. */
14887 arguments = cp_parser_enclosed_template_argument_list (parser);
14888 }
14889
14890 /* Build a representation of the specialization. */
14891 if (identifier_p (templ))
14892 template_id = build_min_nt_loc (next_token->location,
14893 TEMPLATE_ID_EXPR,
14894 templ, arguments);
14895 else if (DECL_TYPE_TEMPLATE_P (templ)
14896 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14897 {
14898 bool entering_scope;
14899 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14900 template (rather than some instantiation thereof) only if
14901 is not nested within some other construct. For example, in
14902 "template <typename T> void f(T) { A<T>::", A<T> is just an
14903 instantiation of A. */
14904 entering_scope = (template_parm_scope_p ()
14905 && cp_lexer_next_token_is (parser->lexer,
14906 CPP_SCOPE));
14907 template_id
14908 = finish_template_type (templ, arguments, entering_scope);
14909 }
14910 /* A template-like identifier may be a partial concept id. */
14911 else if (flag_concepts
14912 && (template_id = (cp_parser_maybe_partial_concept_id
14913 (parser, templ, arguments))))
14914 return template_id;
14915 else if (variable_template_p (templ))
14916 {
14917 template_id = lookup_template_variable (templ, arguments);
14918 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14919 SET_EXPR_LOCATION (template_id, next_token->location);
14920 }
14921 else
14922 {
14923 /* If it's not a class-template or a template-template, it should be
14924 a function-template. */
14925 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14926 || TREE_CODE (templ) == OVERLOAD
14927 || BASELINK_P (templ)));
14928
14929 template_id = lookup_template_function (templ, arguments);
14930 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14931 SET_EXPR_LOCATION (template_id, next_token->location);
14932 }
14933
14934 /* If parsing tentatively, replace the sequence of tokens that makes
14935 up the template-id with a CPP_TEMPLATE_ID token. That way,
14936 should we re-parse the token stream, we will not have to repeat
14937 the effort required to do the parse, nor will we issue duplicate
14938 error messages about problems during instantiation of the
14939 template. */
14940 if (start_of_id
14941 /* Don't do this if we had a parse error in a declarator; re-parsing
14942 might succeed if a name changes meaning (60361). */
14943 && !(cp_parser_error_occurred (parser)
14944 && cp_parser_parsing_tentatively (parser)
14945 && parser->in_declarator_p))
14946 {
14947 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14948
14949 /* Reset the contents of the START_OF_ID token. */
14950 token->type = CPP_TEMPLATE_ID;
14951
14952 /* Update the location to be of the form:
14953 template-name < template-argument-list [opt] >
14954 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14955 with caret == start at the start of the template-name,
14956 ranging until the closing '>'. */
14957 location_t finish_loc
14958 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
14959 location_t combined_loc
14960 = make_location (token->location, token->location, finish_loc);
14961 token->location = combined_loc;
14962
14963 /* Retrieve any deferred checks. Do not pop this access checks yet
14964 so the memory will not be reclaimed during token replacing below. */
14965 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14966 token->u.tree_check_value->value = template_id;
14967 token->u.tree_check_value->checks = get_deferred_access_checks ();
14968 token->keyword = RID_MAX;
14969
14970 /* Purge all subsequent tokens. */
14971 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14972
14973 /* ??? Can we actually assume that, if template_id ==
14974 error_mark_node, we will have issued a diagnostic to the
14975 user, as opposed to simply marking the tentative parse as
14976 failed? */
14977 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14978 error_at (token->location, "parse error in template argument list");
14979 }
14980
14981 pop_to_parent_deferring_access_checks ();
14982 return template_id;
14983 }
14984
14985 /* Parse a template-name.
14986
14987 template-name:
14988 identifier
14989
14990 The standard should actually say:
14991
14992 template-name:
14993 identifier
14994 operator-function-id
14995
14996 A defect report has been filed about this issue.
14997
14998 A conversion-function-id cannot be a template name because they cannot
14999 be part of a template-id. In fact, looking at this code:
15000
15001 a.operator K<int>()
15002
15003 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15004 It is impossible to call a templated conversion-function-id with an
15005 explicit argument list, since the only allowed template parameter is
15006 the type to which it is converting.
15007
15008 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15009 `template' keyword, in a construction like:
15010
15011 T::template f<3>()
15012
15013 In that case `f' is taken to be a template-name, even though there
15014 is no way of knowing for sure.
15015
15016 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15017 name refers to a set of overloaded functions, at least one of which
15018 is a template, or an IDENTIFIER_NODE with the name of the template,
15019 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15020 names are looked up inside uninstantiated templates. */
15021
15022 static tree
15023 cp_parser_template_name (cp_parser* parser,
15024 bool template_keyword_p,
15025 bool check_dependency_p,
15026 bool is_declaration,
15027 enum tag_types tag_type,
15028 bool *is_identifier)
15029 {
15030 tree identifier;
15031 tree decl;
15032 tree fns;
15033 cp_token *token = cp_lexer_peek_token (parser->lexer);
15034
15035 /* If the next token is `operator', then we have either an
15036 operator-function-id or a conversion-function-id. */
15037 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15038 {
15039 /* We don't know whether we're looking at an
15040 operator-function-id or a conversion-function-id. */
15041 cp_parser_parse_tentatively (parser);
15042 /* Try an operator-function-id. */
15043 identifier = cp_parser_operator_function_id (parser);
15044 /* If that didn't work, try a conversion-function-id. */
15045 if (!cp_parser_parse_definitely (parser))
15046 {
15047 cp_parser_error (parser, "expected template-name");
15048 return error_mark_node;
15049 }
15050 }
15051 /* Look for the identifier. */
15052 else
15053 identifier = cp_parser_identifier (parser);
15054
15055 /* If we didn't find an identifier, we don't have a template-id. */
15056 if (identifier == error_mark_node)
15057 return error_mark_node;
15058
15059 /* If the name immediately followed the `template' keyword, then it
15060 is a template-name. However, if the next token is not `<', then
15061 we do not treat it as a template-name, since it is not being used
15062 as part of a template-id. This enables us to handle constructs
15063 like:
15064
15065 template <typename T> struct S { S(); };
15066 template <typename T> S<T>::S();
15067
15068 correctly. We would treat `S' as a template -- if it were `S<T>'
15069 -- but we do not if there is no `<'. */
15070
15071 if (processing_template_decl
15072 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15073 {
15074 /* In a declaration, in a dependent context, we pretend that the
15075 "template" keyword was present in order to improve error
15076 recovery. For example, given:
15077
15078 template <typename T> void f(T::X<int>);
15079
15080 we want to treat "X<int>" as a template-id. */
15081 if (is_declaration
15082 && !template_keyword_p
15083 && parser->scope && TYPE_P (parser->scope)
15084 && check_dependency_p
15085 && dependent_scope_p (parser->scope)
15086 /* Do not do this for dtors (or ctors), since they never
15087 need the template keyword before their name. */
15088 && !constructor_name_p (identifier, parser->scope))
15089 {
15090 cp_token_position start = 0;
15091
15092 /* Explain what went wrong. */
15093 error_at (token->location, "non-template %qD used as template",
15094 identifier);
15095 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15096 parser->scope, identifier);
15097 /* If parsing tentatively, find the location of the "<" token. */
15098 if (cp_parser_simulate_error (parser))
15099 start = cp_lexer_token_position (parser->lexer, true);
15100 /* Parse the template arguments so that we can issue error
15101 messages about them. */
15102 cp_lexer_consume_token (parser->lexer);
15103 cp_parser_enclosed_template_argument_list (parser);
15104 /* Skip tokens until we find a good place from which to
15105 continue parsing. */
15106 cp_parser_skip_to_closing_parenthesis (parser,
15107 /*recovering=*/true,
15108 /*or_comma=*/true,
15109 /*consume_paren=*/false);
15110 /* If parsing tentatively, permanently remove the
15111 template argument list. That will prevent duplicate
15112 error messages from being issued about the missing
15113 "template" keyword. */
15114 if (start)
15115 cp_lexer_purge_tokens_after (parser->lexer, start);
15116 if (is_identifier)
15117 *is_identifier = true;
15118 return identifier;
15119 }
15120
15121 /* If the "template" keyword is present, then there is generally
15122 no point in doing name-lookup, so we just return IDENTIFIER.
15123 But, if the qualifying scope is non-dependent then we can
15124 (and must) do name-lookup normally. */
15125 if (template_keyword_p
15126 && (!parser->scope
15127 || (TYPE_P (parser->scope)
15128 && dependent_type_p (parser->scope))))
15129 return identifier;
15130 }
15131
15132 /* Look up the name. */
15133 decl = cp_parser_lookup_name (parser, identifier,
15134 tag_type,
15135 /*is_template=*/true,
15136 /*is_namespace=*/false,
15137 check_dependency_p,
15138 /*ambiguous_decls=*/NULL,
15139 token->location);
15140
15141 decl = strip_using_decl (decl);
15142
15143 /* If DECL is a template, then the name was a template-name. */
15144 if (TREE_CODE (decl) == TEMPLATE_DECL)
15145 {
15146 if (TREE_DEPRECATED (decl)
15147 && deprecated_state != DEPRECATED_SUPPRESS)
15148 warn_deprecated_use (decl, NULL_TREE);
15149 }
15150 else
15151 {
15152 tree fn = NULL_TREE;
15153
15154 /* The standard does not explicitly indicate whether a name that
15155 names a set of overloaded declarations, some of which are
15156 templates, is a template-name. However, such a name should
15157 be a template-name; otherwise, there is no way to form a
15158 template-id for the overloaded templates. */
15159 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15160 if (TREE_CODE (fns) == OVERLOAD)
15161 for (fn = fns; fn; fn = OVL_NEXT (fn))
15162 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15163 break;
15164
15165 if (!fn)
15166 {
15167 /* The name does not name a template. */
15168 cp_parser_error (parser, "expected template-name");
15169 return error_mark_node;
15170 }
15171 }
15172
15173 /* If DECL is dependent, and refers to a function, then just return
15174 its name; we will look it up again during template instantiation. */
15175 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15176 {
15177 tree scope = ovl_scope (decl);
15178 if (TYPE_P (scope) && dependent_type_p (scope))
15179 return identifier;
15180 }
15181
15182 return decl;
15183 }
15184
15185 /* Parse a template-argument-list.
15186
15187 template-argument-list:
15188 template-argument ... [opt]
15189 template-argument-list , template-argument ... [opt]
15190
15191 Returns a TREE_VEC containing the arguments. */
15192
15193 static tree
15194 cp_parser_template_argument_list (cp_parser* parser)
15195 {
15196 tree fixed_args[10];
15197 unsigned n_args = 0;
15198 unsigned alloced = 10;
15199 tree *arg_ary = fixed_args;
15200 tree vec;
15201 bool saved_in_template_argument_list_p;
15202 bool saved_ice_p;
15203 bool saved_non_ice_p;
15204
15205 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15206 parser->in_template_argument_list_p = true;
15207 /* Even if the template-id appears in an integral
15208 constant-expression, the contents of the argument list do
15209 not. */
15210 saved_ice_p = parser->integral_constant_expression_p;
15211 parser->integral_constant_expression_p = false;
15212 saved_non_ice_p = parser->non_integral_constant_expression_p;
15213 parser->non_integral_constant_expression_p = false;
15214
15215 /* Parse the arguments. */
15216 do
15217 {
15218 tree argument;
15219
15220 if (n_args)
15221 /* Consume the comma. */
15222 cp_lexer_consume_token (parser->lexer);
15223
15224 /* Parse the template-argument. */
15225 argument = cp_parser_template_argument (parser);
15226
15227 /* If the next token is an ellipsis, we're expanding a template
15228 argument pack. */
15229 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15230 {
15231 if (argument == error_mark_node)
15232 {
15233 cp_token *token = cp_lexer_peek_token (parser->lexer);
15234 error_at (token->location,
15235 "expected parameter pack before %<...%>");
15236 }
15237 /* Consume the `...' token. */
15238 cp_lexer_consume_token (parser->lexer);
15239
15240 /* Make the argument into a TYPE_PACK_EXPANSION or
15241 EXPR_PACK_EXPANSION. */
15242 argument = make_pack_expansion (argument);
15243 }
15244
15245 if (n_args == alloced)
15246 {
15247 alloced *= 2;
15248
15249 if (arg_ary == fixed_args)
15250 {
15251 arg_ary = XNEWVEC (tree, alloced);
15252 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15253 }
15254 else
15255 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15256 }
15257 arg_ary[n_args++] = argument;
15258 }
15259 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15260
15261 vec = make_tree_vec (n_args);
15262
15263 while (n_args--)
15264 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15265
15266 if (arg_ary != fixed_args)
15267 free (arg_ary);
15268 parser->non_integral_constant_expression_p = saved_non_ice_p;
15269 parser->integral_constant_expression_p = saved_ice_p;
15270 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15271 if (CHECKING_P)
15272 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15273 return vec;
15274 }
15275
15276 /* Parse a template-argument.
15277
15278 template-argument:
15279 assignment-expression
15280 type-id
15281 id-expression
15282
15283 The representation is that of an assignment-expression, type-id, or
15284 id-expression -- except that the qualified id-expression is
15285 evaluated, so that the value returned is either a DECL or an
15286 OVERLOAD.
15287
15288 Although the standard says "assignment-expression", it forbids
15289 throw-expressions or assignments in the template argument.
15290 Therefore, we use "conditional-expression" instead. */
15291
15292 static tree
15293 cp_parser_template_argument (cp_parser* parser)
15294 {
15295 tree argument;
15296 bool template_p;
15297 bool address_p;
15298 bool maybe_type_id = false;
15299 cp_token *token = NULL, *argument_start_token = NULL;
15300 location_t loc = 0;
15301 cp_id_kind idk;
15302
15303 /* There's really no way to know what we're looking at, so we just
15304 try each alternative in order.
15305
15306 [temp.arg]
15307
15308 In a template-argument, an ambiguity between a type-id and an
15309 expression is resolved to a type-id, regardless of the form of
15310 the corresponding template-parameter.
15311
15312 Therefore, we try a type-id first. */
15313 cp_parser_parse_tentatively (parser);
15314 argument = cp_parser_template_type_arg (parser);
15315 /* If there was no error parsing the type-id but the next token is a
15316 '>>', our behavior depends on which dialect of C++ we're
15317 parsing. In C++98, we probably found a typo for '> >'. But there
15318 are type-id which are also valid expressions. For instance:
15319
15320 struct X { int operator >> (int); };
15321 template <int V> struct Foo {};
15322 Foo<X () >> 5> r;
15323
15324 Here 'X()' is a valid type-id of a function type, but the user just
15325 wanted to write the expression "X() >> 5". Thus, we remember that we
15326 found a valid type-id, but we still try to parse the argument as an
15327 expression to see what happens.
15328
15329 In C++0x, the '>>' will be considered two separate '>'
15330 tokens. */
15331 if (!cp_parser_error_occurred (parser)
15332 && cxx_dialect == cxx98
15333 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15334 {
15335 maybe_type_id = true;
15336 cp_parser_abort_tentative_parse (parser);
15337 }
15338 else
15339 {
15340 /* If the next token isn't a `,' or a `>', then this argument wasn't
15341 really finished. This means that the argument is not a valid
15342 type-id. */
15343 if (!cp_parser_next_token_ends_template_argument_p (parser))
15344 cp_parser_error (parser, "expected template-argument");
15345 /* If that worked, we're done. */
15346 if (cp_parser_parse_definitely (parser))
15347 return argument;
15348 }
15349 /* We're still not sure what the argument will be. */
15350 cp_parser_parse_tentatively (parser);
15351 /* Try a template. */
15352 argument_start_token = cp_lexer_peek_token (parser->lexer);
15353 argument = cp_parser_id_expression (parser,
15354 /*template_keyword_p=*/false,
15355 /*check_dependency_p=*/true,
15356 &template_p,
15357 /*declarator_p=*/false,
15358 /*optional_p=*/false);
15359 /* If the next token isn't a `,' or a `>', then this argument wasn't
15360 really finished. */
15361 if (!cp_parser_next_token_ends_template_argument_p (parser))
15362 cp_parser_error (parser, "expected template-argument");
15363 if (!cp_parser_error_occurred (parser))
15364 {
15365 /* Figure out what is being referred to. If the id-expression
15366 was for a class template specialization, then we will have a
15367 TYPE_DECL at this point. There is no need to do name lookup
15368 at this point in that case. */
15369 if (TREE_CODE (argument) != TYPE_DECL)
15370 argument = cp_parser_lookup_name (parser, argument,
15371 none_type,
15372 /*is_template=*/template_p,
15373 /*is_namespace=*/false,
15374 /*check_dependency=*/true,
15375 /*ambiguous_decls=*/NULL,
15376 argument_start_token->location);
15377 /* Handle a constrained-type-specifier for a non-type template
15378 parameter. */
15379 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15380 argument = decl;
15381 else if (TREE_CODE (argument) != TEMPLATE_DECL
15382 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15383 cp_parser_error (parser, "expected template-name");
15384 }
15385 if (cp_parser_parse_definitely (parser))
15386 {
15387 if (TREE_DEPRECATED (argument))
15388 warn_deprecated_use (argument, NULL_TREE);
15389 return argument;
15390 }
15391 /* It must be a non-type argument. In C++17 any constant-expression is
15392 allowed. */
15393 if (cxx_dialect > cxx14)
15394 goto general_expr;
15395
15396 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15397
15398 -- an integral constant-expression of integral or enumeration
15399 type; or
15400
15401 -- the name of a non-type template-parameter; or
15402
15403 -- the name of an object or function with external linkage...
15404
15405 -- the address of an object or function with external linkage...
15406
15407 -- a pointer to member... */
15408 /* Look for a non-type template parameter. */
15409 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15410 {
15411 cp_parser_parse_tentatively (parser);
15412 argument = cp_parser_primary_expression (parser,
15413 /*address_p=*/false,
15414 /*cast_p=*/false,
15415 /*template_arg_p=*/true,
15416 &idk);
15417 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15418 || !cp_parser_next_token_ends_template_argument_p (parser))
15419 cp_parser_simulate_error (parser);
15420 if (cp_parser_parse_definitely (parser))
15421 return argument;
15422 }
15423
15424 /* If the next token is "&", the argument must be the address of an
15425 object or function with external linkage. */
15426 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15427 if (address_p)
15428 {
15429 loc = cp_lexer_peek_token (parser->lexer)->location;
15430 cp_lexer_consume_token (parser->lexer);
15431 }
15432 /* See if we might have an id-expression. */
15433 token = cp_lexer_peek_token (parser->lexer);
15434 if (token->type == CPP_NAME
15435 || token->keyword == RID_OPERATOR
15436 || token->type == CPP_SCOPE
15437 || token->type == CPP_TEMPLATE_ID
15438 || token->type == CPP_NESTED_NAME_SPECIFIER)
15439 {
15440 cp_parser_parse_tentatively (parser);
15441 argument = cp_parser_primary_expression (parser,
15442 address_p,
15443 /*cast_p=*/false,
15444 /*template_arg_p=*/true,
15445 &idk);
15446 if (cp_parser_error_occurred (parser)
15447 || !cp_parser_next_token_ends_template_argument_p (parser))
15448 cp_parser_abort_tentative_parse (parser);
15449 else
15450 {
15451 tree probe;
15452
15453 if (INDIRECT_REF_P (argument))
15454 {
15455 /* Strip the dereference temporarily. */
15456 gcc_assert (REFERENCE_REF_P (argument));
15457 argument = TREE_OPERAND (argument, 0);
15458 }
15459
15460 /* If we're in a template, we represent a qualified-id referring
15461 to a static data member as a SCOPE_REF even if the scope isn't
15462 dependent so that we can check access control later. */
15463 probe = argument;
15464 if (TREE_CODE (probe) == SCOPE_REF)
15465 probe = TREE_OPERAND (probe, 1);
15466 if (VAR_P (probe))
15467 {
15468 /* A variable without external linkage might still be a
15469 valid constant-expression, so no error is issued here
15470 if the external-linkage check fails. */
15471 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15472 cp_parser_simulate_error (parser);
15473 }
15474 else if (is_overloaded_fn (argument))
15475 /* All overloaded functions are allowed; if the external
15476 linkage test does not pass, an error will be issued
15477 later. */
15478 ;
15479 else if (address_p
15480 && (TREE_CODE (argument) == OFFSET_REF
15481 || TREE_CODE (argument) == SCOPE_REF))
15482 /* A pointer-to-member. */
15483 ;
15484 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15485 ;
15486 else
15487 cp_parser_simulate_error (parser);
15488
15489 if (cp_parser_parse_definitely (parser))
15490 {
15491 if (address_p)
15492 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15493 tf_warning_or_error);
15494 else
15495 argument = convert_from_reference (argument);
15496 return argument;
15497 }
15498 }
15499 }
15500 /* If the argument started with "&", there are no other valid
15501 alternatives at this point. */
15502 if (address_p)
15503 {
15504 cp_parser_error (parser, "invalid non-type template argument");
15505 return error_mark_node;
15506 }
15507
15508 general_expr:
15509 /* If the argument wasn't successfully parsed as a type-id followed
15510 by '>>', the argument can only be a constant expression now.
15511 Otherwise, we try parsing the constant-expression tentatively,
15512 because the argument could really be a type-id. */
15513 if (maybe_type_id)
15514 cp_parser_parse_tentatively (parser);
15515
15516 if (cxx_dialect <= cxx14)
15517 argument = cp_parser_constant_expression (parser);
15518 else
15519 {
15520 /* With C++17 generalized non-type template arguments we need to handle
15521 lvalue constant expressions, too. */
15522 argument = cp_parser_assignment_expression (parser);
15523 require_potential_constant_expression (argument);
15524 }
15525
15526 if (!maybe_type_id)
15527 return argument;
15528 if (!cp_parser_next_token_ends_template_argument_p (parser))
15529 cp_parser_error (parser, "expected template-argument");
15530 if (cp_parser_parse_definitely (parser))
15531 return argument;
15532 /* We did our best to parse the argument as a non type-id, but that
15533 was the only alternative that matched (albeit with a '>' after
15534 it). We can assume it's just a typo from the user, and a
15535 diagnostic will then be issued. */
15536 return cp_parser_template_type_arg (parser);
15537 }
15538
15539 /* Parse an explicit-instantiation.
15540
15541 explicit-instantiation:
15542 template declaration
15543
15544 Although the standard says `declaration', what it really means is:
15545
15546 explicit-instantiation:
15547 template decl-specifier-seq [opt] declarator [opt] ;
15548
15549 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15550 supposed to be allowed. A defect report has been filed about this
15551 issue.
15552
15553 GNU Extension:
15554
15555 explicit-instantiation:
15556 storage-class-specifier template
15557 decl-specifier-seq [opt] declarator [opt] ;
15558 function-specifier template
15559 decl-specifier-seq [opt] declarator [opt] ; */
15560
15561 static void
15562 cp_parser_explicit_instantiation (cp_parser* parser)
15563 {
15564 int declares_class_or_enum;
15565 cp_decl_specifier_seq decl_specifiers;
15566 tree extension_specifier = NULL_TREE;
15567
15568 timevar_push (TV_TEMPLATE_INST);
15569
15570 /* Look for an (optional) storage-class-specifier or
15571 function-specifier. */
15572 if (cp_parser_allow_gnu_extensions_p (parser))
15573 {
15574 extension_specifier
15575 = cp_parser_storage_class_specifier_opt (parser);
15576 if (!extension_specifier)
15577 extension_specifier
15578 = cp_parser_function_specifier_opt (parser,
15579 /*decl_specs=*/NULL);
15580 }
15581
15582 /* Look for the `template' keyword. */
15583 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15584 /* Let the front end know that we are processing an explicit
15585 instantiation. */
15586 begin_explicit_instantiation ();
15587 /* [temp.explicit] says that we are supposed to ignore access
15588 control while processing explicit instantiation directives. */
15589 push_deferring_access_checks (dk_no_check);
15590 /* Parse a decl-specifier-seq. */
15591 cp_parser_decl_specifier_seq (parser,
15592 CP_PARSER_FLAGS_OPTIONAL,
15593 &decl_specifiers,
15594 &declares_class_or_enum);
15595 /* If there was exactly one decl-specifier, and it declared a class,
15596 and there's no declarator, then we have an explicit type
15597 instantiation. */
15598 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15599 {
15600 tree type;
15601
15602 type = check_tag_decl (&decl_specifiers,
15603 /*explicit_type_instantiation_p=*/true);
15604 /* Turn access control back on for names used during
15605 template instantiation. */
15606 pop_deferring_access_checks ();
15607 if (type)
15608 do_type_instantiation (type, extension_specifier,
15609 /*complain=*/tf_error);
15610 }
15611 else
15612 {
15613 cp_declarator *declarator;
15614 tree decl;
15615
15616 /* Parse the declarator. */
15617 declarator
15618 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15619 /*ctor_dtor_or_conv_p=*/NULL,
15620 /*parenthesized_p=*/NULL,
15621 /*member_p=*/false,
15622 /*friend_p=*/false);
15623 if (declares_class_or_enum & 2)
15624 cp_parser_check_for_definition_in_return_type (declarator,
15625 decl_specifiers.type,
15626 decl_specifiers.locations[ds_type_spec]);
15627 if (declarator != cp_error_declarator)
15628 {
15629 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15630 permerror (decl_specifiers.locations[ds_inline],
15631 "explicit instantiation shall not use"
15632 " %<inline%> specifier");
15633 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15634 permerror (decl_specifiers.locations[ds_constexpr],
15635 "explicit instantiation shall not use"
15636 " %<constexpr%> specifier");
15637
15638 decl = grokdeclarator (declarator, &decl_specifiers,
15639 NORMAL, 0, &decl_specifiers.attributes);
15640 /* Turn access control back on for names used during
15641 template instantiation. */
15642 pop_deferring_access_checks ();
15643 /* Do the explicit instantiation. */
15644 do_decl_instantiation (decl, extension_specifier);
15645 }
15646 else
15647 {
15648 pop_deferring_access_checks ();
15649 /* Skip the body of the explicit instantiation. */
15650 cp_parser_skip_to_end_of_statement (parser);
15651 }
15652 }
15653 /* We're done with the instantiation. */
15654 end_explicit_instantiation ();
15655
15656 cp_parser_consume_semicolon_at_end_of_statement (parser);
15657
15658 timevar_pop (TV_TEMPLATE_INST);
15659 }
15660
15661 /* Parse an explicit-specialization.
15662
15663 explicit-specialization:
15664 template < > declaration
15665
15666 Although the standard says `declaration', what it really means is:
15667
15668 explicit-specialization:
15669 template <> decl-specifier [opt] init-declarator [opt] ;
15670 template <> function-definition
15671 template <> explicit-specialization
15672 template <> template-declaration */
15673
15674 static void
15675 cp_parser_explicit_specialization (cp_parser* parser)
15676 {
15677 bool need_lang_pop;
15678 cp_token *token = cp_lexer_peek_token (parser->lexer);
15679
15680 /* Look for the `template' keyword. */
15681 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15682 /* Look for the `<'. */
15683 cp_parser_require (parser, CPP_LESS, RT_LESS);
15684 /* Look for the `>'. */
15685 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15686 /* We have processed another parameter list. */
15687 ++parser->num_template_parameter_lists;
15688 /* [temp]
15689
15690 A template ... explicit specialization ... shall not have C
15691 linkage. */
15692 if (current_lang_name == lang_name_c)
15693 {
15694 error_at (token->location, "template specialization with C linkage");
15695 /* Give it C++ linkage to avoid confusing other parts of the
15696 front end. */
15697 push_lang_context (lang_name_cplusplus);
15698 need_lang_pop = true;
15699 }
15700 else
15701 need_lang_pop = false;
15702 /* Let the front end know that we are beginning a specialization. */
15703 if (!begin_specialization ())
15704 {
15705 end_specialization ();
15706 return;
15707 }
15708
15709 /* If the next keyword is `template', we need to figure out whether
15710 or not we're looking a template-declaration. */
15711 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15712 {
15713 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15714 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15715 cp_parser_template_declaration_after_export (parser,
15716 /*member_p=*/false);
15717 else
15718 cp_parser_explicit_specialization (parser);
15719 }
15720 else
15721 /* Parse the dependent declaration. */
15722 cp_parser_single_declaration (parser,
15723 /*checks=*/NULL,
15724 /*member_p=*/false,
15725 /*explicit_specialization_p=*/true,
15726 /*friend_p=*/NULL);
15727 /* We're done with the specialization. */
15728 end_specialization ();
15729 /* For the erroneous case of a template with C linkage, we pushed an
15730 implicit C++ linkage scope; exit that scope now. */
15731 if (need_lang_pop)
15732 pop_lang_context ();
15733 /* We're done with this parameter list. */
15734 --parser->num_template_parameter_lists;
15735 }
15736
15737 /* Parse a type-specifier.
15738
15739 type-specifier:
15740 simple-type-specifier
15741 class-specifier
15742 enum-specifier
15743 elaborated-type-specifier
15744 cv-qualifier
15745
15746 GNU Extension:
15747
15748 type-specifier:
15749 __complex__
15750
15751 Returns a representation of the type-specifier. For a
15752 class-specifier, enum-specifier, or elaborated-type-specifier, a
15753 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15754
15755 The parser flags FLAGS is used to control type-specifier parsing.
15756
15757 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15758 in a decl-specifier-seq.
15759
15760 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15761 class-specifier, enum-specifier, or elaborated-type-specifier, then
15762 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15763 if a type is declared; 2 if it is defined. Otherwise, it is set to
15764 zero.
15765
15766 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15767 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15768 is set to FALSE. */
15769
15770 static tree
15771 cp_parser_type_specifier (cp_parser* parser,
15772 cp_parser_flags flags,
15773 cp_decl_specifier_seq *decl_specs,
15774 bool is_declaration,
15775 int* declares_class_or_enum,
15776 bool* is_cv_qualifier)
15777 {
15778 tree type_spec = NULL_TREE;
15779 cp_token *token;
15780 enum rid keyword;
15781 cp_decl_spec ds = ds_last;
15782
15783 /* Assume this type-specifier does not declare a new type. */
15784 if (declares_class_or_enum)
15785 *declares_class_or_enum = 0;
15786 /* And that it does not specify a cv-qualifier. */
15787 if (is_cv_qualifier)
15788 *is_cv_qualifier = false;
15789 /* Peek at the next token. */
15790 token = cp_lexer_peek_token (parser->lexer);
15791
15792 /* If we're looking at a keyword, we can use that to guide the
15793 production we choose. */
15794 keyword = token->keyword;
15795 switch (keyword)
15796 {
15797 case RID_ENUM:
15798 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15799 goto elaborated_type_specifier;
15800
15801 /* Look for the enum-specifier. */
15802 type_spec = cp_parser_enum_specifier (parser);
15803 /* If that worked, we're done. */
15804 if (type_spec)
15805 {
15806 if (declares_class_or_enum)
15807 *declares_class_or_enum = 2;
15808 if (decl_specs)
15809 cp_parser_set_decl_spec_type (decl_specs,
15810 type_spec,
15811 token,
15812 /*type_definition_p=*/true);
15813 return type_spec;
15814 }
15815 else
15816 goto elaborated_type_specifier;
15817
15818 /* Any of these indicate either a class-specifier, or an
15819 elaborated-type-specifier. */
15820 case RID_CLASS:
15821 case RID_STRUCT:
15822 case RID_UNION:
15823 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15824 goto elaborated_type_specifier;
15825
15826 /* Parse tentatively so that we can back up if we don't find a
15827 class-specifier. */
15828 cp_parser_parse_tentatively (parser);
15829 /* Look for the class-specifier. */
15830 type_spec = cp_parser_class_specifier (parser);
15831 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15832 /* If that worked, we're done. */
15833 if (cp_parser_parse_definitely (parser))
15834 {
15835 if (declares_class_or_enum)
15836 *declares_class_or_enum = 2;
15837 if (decl_specs)
15838 cp_parser_set_decl_spec_type (decl_specs,
15839 type_spec,
15840 token,
15841 /*type_definition_p=*/true);
15842 return type_spec;
15843 }
15844
15845 /* Fall through. */
15846 elaborated_type_specifier:
15847 /* We're declaring (not defining) a class or enum. */
15848 if (declares_class_or_enum)
15849 *declares_class_or_enum = 1;
15850
15851 /* Fall through. */
15852 case RID_TYPENAME:
15853 /* Look for an elaborated-type-specifier. */
15854 type_spec
15855 = (cp_parser_elaborated_type_specifier
15856 (parser,
15857 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15858 is_declaration));
15859 if (decl_specs)
15860 cp_parser_set_decl_spec_type (decl_specs,
15861 type_spec,
15862 token,
15863 /*type_definition_p=*/false);
15864 return type_spec;
15865
15866 case RID_CONST:
15867 ds = ds_const;
15868 if (is_cv_qualifier)
15869 *is_cv_qualifier = true;
15870 break;
15871
15872 case RID_VOLATILE:
15873 ds = ds_volatile;
15874 if (is_cv_qualifier)
15875 *is_cv_qualifier = true;
15876 break;
15877
15878 case RID_RESTRICT:
15879 ds = ds_restrict;
15880 if (is_cv_qualifier)
15881 *is_cv_qualifier = true;
15882 break;
15883
15884 case RID_COMPLEX:
15885 /* The `__complex__' keyword is a GNU extension. */
15886 ds = ds_complex;
15887 break;
15888
15889 default:
15890 break;
15891 }
15892
15893 /* Handle simple keywords. */
15894 if (ds != ds_last)
15895 {
15896 if (decl_specs)
15897 {
15898 set_and_check_decl_spec_loc (decl_specs, ds, token);
15899 decl_specs->any_specifiers_p = true;
15900 }
15901 return cp_lexer_consume_token (parser->lexer)->u.value;
15902 }
15903
15904 /* If we do not already have a type-specifier, assume we are looking
15905 at a simple-type-specifier. */
15906 type_spec = cp_parser_simple_type_specifier (parser,
15907 decl_specs,
15908 flags);
15909
15910 /* If we didn't find a type-specifier, and a type-specifier was not
15911 optional in this context, issue an error message. */
15912 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15913 {
15914 cp_parser_error (parser, "expected type specifier");
15915 return error_mark_node;
15916 }
15917
15918 return type_spec;
15919 }
15920
15921 /* Parse a simple-type-specifier.
15922
15923 simple-type-specifier:
15924 :: [opt] nested-name-specifier [opt] type-name
15925 :: [opt] nested-name-specifier template template-id
15926 char
15927 wchar_t
15928 bool
15929 short
15930 int
15931 long
15932 signed
15933 unsigned
15934 float
15935 double
15936 void
15937
15938 C++0x Extension:
15939
15940 simple-type-specifier:
15941 auto
15942 decltype ( expression )
15943 char16_t
15944 char32_t
15945 __underlying_type ( type-id )
15946
15947 GNU Extension:
15948
15949 simple-type-specifier:
15950 __int128
15951 __typeof__ unary-expression
15952 __typeof__ ( type-id )
15953 __typeof__ ( type-id ) { initializer-list , [opt] }
15954
15955 Concepts Extension:
15956
15957 simple-type-specifier:
15958 constrained-type-specifier
15959
15960 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15961 appropriately updated. */
15962
15963 static tree
15964 cp_parser_simple_type_specifier (cp_parser* parser,
15965 cp_decl_specifier_seq *decl_specs,
15966 cp_parser_flags flags)
15967 {
15968 tree type = NULL_TREE;
15969 cp_token *token;
15970 int idx;
15971
15972 /* Peek at the next token. */
15973 token = cp_lexer_peek_token (parser->lexer);
15974
15975 /* If we're looking at a keyword, things are easy. */
15976 switch (token->keyword)
15977 {
15978 case RID_CHAR:
15979 if (decl_specs)
15980 decl_specs->explicit_char_p = true;
15981 type = char_type_node;
15982 break;
15983 case RID_CHAR16:
15984 type = char16_type_node;
15985 break;
15986 case RID_CHAR32:
15987 type = char32_type_node;
15988 break;
15989 case RID_WCHAR:
15990 type = wchar_type_node;
15991 break;
15992 case RID_BOOL:
15993 type = boolean_type_node;
15994 break;
15995 case RID_SHORT:
15996 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
15997 type = short_integer_type_node;
15998 break;
15999 case RID_INT:
16000 if (decl_specs)
16001 decl_specs->explicit_int_p = true;
16002 type = integer_type_node;
16003 break;
16004 case RID_INT_N_0:
16005 case RID_INT_N_1:
16006 case RID_INT_N_2:
16007 case RID_INT_N_3:
16008 idx = token->keyword - RID_INT_N_0;
16009 if (! int_n_enabled_p [idx])
16010 break;
16011 if (decl_specs)
16012 {
16013 decl_specs->explicit_intN_p = true;
16014 decl_specs->int_n_idx = idx;
16015 }
16016 type = int_n_trees [idx].signed_type;
16017 break;
16018 case RID_LONG:
16019 if (decl_specs)
16020 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16021 type = long_integer_type_node;
16022 break;
16023 case RID_SIGNED:
16024 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16025 type = integer_type_node;
16026 break;
16027 case RID_UNSIGNED:
16028 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16029 type = unsigned_type_node;
16030 break;
16031 case RID_FLOAT:
16032 type = float_type_node;
16033 break;
16034 case RID_DOUBLE:
16035 type = double_type_node;
16036 break;
16037 case RID_VOID:
16038 type = void_type_node;
16039 break;
16040
16041 case RID_AUTO:
16042 maybe_warn_cpp0x (CPP0X_AUTO);
16043 if (parser->auto_is_implicit_function_template_parm_p)
16044 {
16045 /* The 'auto' might be the placeholder return type for a function decl
16046 with trailing return type. */
16047 bool have_trailing_return_fn_decl = false;
16048
16049 cp_parser_parse_tentatively (parser);
16050 cp_lexer_consume_token (parser->lexer);
16051 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16052 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16053 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16054 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16055 {
16056 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16057 {
16058 cp_lexer_consume_token (parser->lexer);
16059 cp_parser_skip_to_closing_parenthesis (parser,
16060 /*recovering*/false,
16061 /*or_comma*/false,
16062 /*consume_paren*/true);
16063 continue;
16064 }
16065
16066 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16067 {
16068 have_trailing_return_fn_decl = true;
16069 break;
16070 }
16071
16072 cp_lexer_consume_token (parser->lexer);
16073 }
16074 cp_parser_abort_tentative_parse (parser);
16075
16076 if (have_trailing_return_fn_decl)
16077 {
16078 type = make_auto ();
16079 break;
16080 }
16081
16082 if (cxx_dialect >= cxx14)
16083 {
16084 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16085 type = TREE_TYPE (type);
16086 }
16087 else
16088 type = error_mark_node;
16089
16090 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16091 {
16092 if (cxx_dialect < cxx14)
16093 error_at (token->location,
16094 "use of %<auto%> in lambda parameter declaration "
16095 "only available with "
16096 "-std=c++14 or -std=gnu++14");
16097 }
16098 else if (cxx_dialect < cxx14)
16099 error_at (token->location,
16100 "use of %<auto%> in parameter declaration "
16101 "only available with "
16102 "-std=c++14 or -std=gnu++14");
16103 else if (!flag_concepts)
16104 pedwarn (token->location, OPT_Wpedantic,
16105 "ISO C++ forbids use of %<auto%> in parameter "
16106 "declaration");
16107 }
16108 else
16109 type = make_auto ();
16110 break;
16111
16112 case RID_DECLTYPE:
16113 /* Since DR 743, decltype can either be a simple-type-specifier by
16114 itself or begin a nested-name-specifier. Parsing it will replace
16115 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16116 handling below decide what to do. */
16117 cp_parser_decltype (parser);
16118 cp_lexer_set_token_position (parser->lexer, token);
16119 break;
16120
16121 case RID_TYPEOF:
16122 /* Consume the `typeof' token. */
16123 cp_lexer_consume_token (parser->lexer);
16124 /* Parse the operand to `typeof'. */
16125 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16126 /* If it is not already a TYPE, take its type. */
16127 if (!TYPE_P (type))
16128 type = finish_typeof (type);
16129
16130 if (decl_specs)
16131 cp_parser_set_decl_spec_type (decl_specs, type,
16132 token,
16133 /*type_definition_p=*/false);
16134
16135 return type;
16136
16137 case RID_UNDERLYING_TYPE:
16138 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16139 if (decl_specs)
16140 cp_parser_set_decl_spec_type (decl_specs, type,
16141 token,
16142 /*type_definition_p=*/false);
16143
16144 return type;
16145
16146 case RID_BASES:
16147 case RID_DIRECT_BASES:
16148 type = cp_parser_trait_expr (parser, token->keyword);
16149 if (decl_specs)
16150 cp_parser_set_decl_spec_type (decl_specs, type,
16151 token,
16152 /*type_definition_p=*/false);
16153 return type;
16154 default:
16155 break;
16156 }
16157
16158 /* If token is an already-parsed decltype not followed by ::,
16159 it's a simple-type-specifier. */
16160 if (token->type == CPP_DECLTYPE
16161 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16162 {
16163 type = saved_checks_value (token->u.tree_check_value);
16164 if (decl_specs)
16165 {
16166 cp_parser_set_decl_spec_type (decl_specs, type,
16167 token,
16168 /*type_definition_p=*/false);
16169 /* Remember that we are handling a decltype in order to
16170 implement the resolution of DR 1510 when the argument
16171 isn't instantiation dependent. */
16172 decl_specs->decltype_p = true;
16173 }
16174 cp_lexer_consume_token (parser->lexer);
16175 return type;
16176 }
16177
16178 /* If the type-specifier was for a built-in type, we're done. */
16179 if (type)
16180 {
16181 /* Record the type. */
16182 if (decl_specs
16183 && (token->keyword != RID_SIGNED
16184 && token->keyword != RID_UNSIGNED
16185 && token->keyword != RID_SHORT
16186 && token->keyword != RID_LONG))
16187 cp_parser_set_decl_spec_type (decl_specs,
16188 type,
16189 token,
16190 /*type_definition_p=*/false);
16191 if (decl_specs)
16192 decl_specs->any_specifiers_p = true;
16193
16194 /* Consume the token. */
16195 cp_lexer_consume_token (parser->lexer);
16196
16197 if (type == error_mark_node)
16198 return error_mark_node;
16199
16200 /* There is no valid C++ program where a non-template type is
16201 followed by a "<". That usually indicates that the user thought
16202 that the type was a template. */
16203 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16204 token->location);
16205
16206 return TYPE_NAME (type);
16207 }
16208
16209 /* The type-specifier must be a user-defined type. */
16210 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16211 {
16212 bool qualified_p;
16213 bool global_p;
16214
16215 /* Don't gobble tokens or issue error messages if this is an
16216 optional type-specifier. */
16217 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16218 cp_parser_parse_tentatively (parser);
16219
16220 /* Look for the optional `::' operator. */
16221 global_p
16222 = (cp_parser_global_scope_opt (parser,
16223 /*current_scope_valid_p=*/false)
16224 != NULL_TREE);
16225 /* Look for the nested-name specifier. */
16226 qualified_p
16227 = (cp_parser_nested_name_specifier_opt (parser,
16228 /*typename_keyword_p=*/false,
16229 /*check_dependency_p=*/true,
16230 /*type_p=*/false,
16231 /*is_declaration=*/false)
16232 != NULL_TREE);
16233 token = cp_lexer_peek_token (parser->lexer);
16234 /* If we have seen a nested-name-specifier, and the next token
16235 is `template', then we are using the template-id production. */
16236 if (parser->scope
16237 && cp_parser_optional_template_keyword (parser))
16238 {
16239 /* Look for the template-id. */
16240 type = cp_parser_template_id (parser,
16241 /*template_keyword_p=*/true,
16242 /*check_dependency_p=*/true,
16243 none_type,
16244 /*is_declaration=*/false);
16245 /* If the template-id did not name a type, we are out of
16246 luck. */
16247 if (TREE_CODE (type) != TYPE_DECL)
16248 {
16249 cp_parser_error (parser, "expected template-id for type");
16250 type = NULL_TREE;
16251 }
16252 }
16253 /* Otherwise, look for a type-name. */
16254 else
16255 type = cp_parser_type_name (parser);
16256 /* Keep track of all name-lookups performed in class scopes. */
16257 if (type
16258 && !global_p
16259 && !qualified_p
16260 && TREE_CODE (type) == TYPE_DECL
16261 && identifier_p (DECL_NAME (type)))
16262 maybe_note_name_used_in_class (DECL_NAME (type), type);
16263 /* If it didn't work out, we don't have a TYPE. */
16264 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16265 && !cp_parser_parse_definitely (parser))
16266 type = NULL_TREE;
16267 if (type && decl_specs)
16268 cp_parser_set_decl_spec_type (decl_specs, type,
16269 token,
16270 /*type_definition_p=*/false);
16271 }
16272
16273 /* If we didn't get a type-name, issue an error message. */
16274 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16275 {
16276 cp_parser_error (parser, "expected type-name");
16277 return error_mark_node;
16278 }
16279
16280 if (type && type != error_mark_node)
16281 {
16282 /* See if TYPE is an Objective-C type, and if so, parse and
16283 accept any protocol references following it. Do this before
16284 the cp_parser_check_for_invalid_template_id() call, because
16285 Objective-C types can be followed by '<...>' which would
16286 enclose protocol names rather than template arguments, and so
16287 everything is fine. */
16288 if (c_dialect_objc () && !parser->scope
16289 && (objc_is_id (type) || objc_is_class_name (type)))
16290 {
16291 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16292 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16293
16294 /* Clobber the "unqualified" type previously entered into
16295 DECL_SPECS with the new, improved protocol-qualified version. */
16296 if (decl_specs)
16297 decl_specs->type = qual_type;
16298
16299 return qual_type;
16300 }
16301
16302 /* There is no valid C++ program where a non-template type is
16303 followed by a "<". That usually indicates that the user
16304 thought that the type was a template. */
16305 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16306 none_type,
16307 token->location);
16308 }
16309
16310 return type;
16311 }
16312
16313 /* Parse a type-name.
16314
16315 type-name:
16316 class-name
16317 enum-name
16318 typedef-name
16319 simple-template-id [in c++0x]
16320
16321 enum-name:
16322 identifier
16323
16324 typedef-name:
16325 identifier
16326
16327 Concepts:
16328
16329 type-name:
16330 concept-name
16331 partial-concept-id
16332
16333 concept-name:
16334 identifier
16335
16336 Returns a TYPE_DECL for the type. */
16337
16338 static tree
16339 cp_parser_type_name (cp_parser* parser)
16340 {
16341 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16342 }
16343
16344 /* See above. */
16345 static tree
16346 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16347 {
16348 tree type_decl;
16349
16350 /* We can't know yet whether it is a class-name or not. */
16351 cp_parser_parse_tentatively (parser);
16352 /* Try a class-name. */
16353 type_decl = cp_parser_class_name (parser,
16354 typename_keyword_p,
16355 /*template_keyword_p=*/false,
16356 none_type,
16357 /*check_dependency_p=*/true,
16358 /*class_head_p=*/false,
16359 /*is_declaration=*/false);
16360 /* If it's not a class-name, keep looking. */
16361 if (!cp_parser_parse_definitely (parser))
16362 {
16363 if (cxx_dialect < cxx11)
16364 /* It must be a typedef-name or an enum-name. */
16365 return cp_parser_nonclass_name (parser);
16366
16367 cp_parser_parse_tentatively (parser);
16368 /* It is either a simple-template-id representing an
16369 instantiation of an alias template... */
16370 type_decl = cp_parser_template_id (parser,
16371 /*template_keyword_p=*/false,
16372 /*check_dependency_p=*/true,
16373 none_type,
16374 /*is_declaration=*/false);
16375 /* Note that this must be an instantiation of an alias template
16376 because [temp.names]/6 says:
16377
16378 A template-id that names an alias template specialization
16379 is a type-name.
16380
16381 Whereas [temp.names]/7 says:
16382
16383 A simple-template-id that names a class template
16384 specialization is a class-name.
16385
16386 With concepts, this could also be a partial-concept-id that
16387 declares a non-type template parameter. */
16388 if (type_decl != NULL_TREE
16389 && TREE_CODE (type_decl) == TYPE_DECL
16390 && TYPE_DECL_ALIAS_P (type_decl))
16391 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16392 else if (is_constrained_parameter (type_decl))
16393 /* Don't do anything. */ ;
16394 else
16395 cp_parser_simulate_error (parser);
16396
16397 if (!cp_parser_parse_definitely (parser))
16398 /* ... Or a typedef-name or an enum-name. */
16399 return cp_parser_nonclass_name (parser);
16400 }
16401
16402 return type_decl;
16403 }
16404
16405 /* Check if DECL and ARGS can form a constrained-type-specifier.
16406 If ARGS is non-null, we try to form a concept check of the
16407 form DECL<?, ARGS> where ? is a wildcard that matches any
16408 kind of template argument. If ARGS is NULL, then we try to
16409 form a concept check of the form DECL<?>. */
16410
16411 static tree
16412 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16413 tree decl, tree args)
16414 {
16415 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16416
16417 /* If we a constrained-type-specifier cannot be deduced. */
16418 if (parser->prevent_constrained_type_specifiers)
16419 return NULL_TREE;
16420
16421 /* A constrained type specifier can only be found in an
16422 overload set or as a reference to a template declaration.
16423
16424 FIXME: This might be masking a bug. It's possible that
16425 that the deduction below is causing template specializations
16426 to be formed with the wildcard as an argument. */
16427 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16428 return NULL_TREE;
16429
16430 /* Try to build a call expression that evaluates the
16431 concept. This can fail if the overload set refers
16432 only to non-templates. */
16433 tree placeholder = build_nt (WILDCARD_DECL);
16434 tree check = build_concept_check (decl, placeholder, args);
16435 if (check == error_mark_node)
16436 return NULL_TREE;
16437
16438 /* Deduce the checked constraint and the prototype parameter.
16439
16440 FIXME: In certain cases, failure to deduce should be a
16441 diagnosable error. */
16442 tree conc;
16443 tree proto;
16444 if (!deduce_constrained_parameter (check, conc, proto))
16445 return NULL_TREE;
16446
16447 /* In template parameter scope, this results in a constrained
16448 parameter. Return a descriptor of that parm. */
16449 if (processing_template_parmlist)
16450 return build_constrained_parameter (conc, proto, args);
16451
16452 /* In a parameter-declaration-clause, constrained-type
16453 specifiers result in invented template parameters. */
16454 if (parser->auto_is_implicit_function_template_parm_p)
16455 {
16456 tree x = build_constrained_parameter (conc, proto, args);
16457 return synthesize_implicit_template_parm (parser, x);
16458 }
16459 else
16460 {
16461 /* Otherwise, we're in a context where the constrained
16462 type name is deduced and the constraint applies
16463 after deduction. */
16464 return make_constrained_auto (conc, args);
16465 }
16466
16467 return NULL_TREE;
16468 }
16469
16470 /* If DECL refers to a concept, return a TYPE_DECL representing
16471 the result of using the constrained type specifier in the
16472 current context. DECL refers to a concept if
16473
16474 - it is an overload set containing a function concept taking a single
16475 type argument, or
16476
16477 - it is a variable concept taking a single type argument. */
16478
16479 static tree
16480 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16481 {
16482 if (flag_concepts
16483 && (TREE_CODE (decl) == OVERLOAD
16484 || BASELINK_P (decl)
16485 || variable_concept_p (decl)))
16486 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16487 else
16488 return NULL_TREE;
16489 }
16490
16491 /* Check if DECL and ARGS form a partial-concept-id. If so,
16492 assign ID to the resulting constrained placeholder.
16493
16494 Returns true if the partial-concept-id designates a placeholder
16495 and false otherwise. Note that *id is set to NULL_TREE in
16496 this case. */
16497
16498 static tree
16499 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16500 {
16501 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16502 }
16503
16504 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16505 or a concept-name.
16506
16507 enum-name:
16508 identifier
16509
16510 typedef-name:
16511 identifier
16512
16513 concept-name:
16514 identifier
16515
16516 Returns a TYPE_DECL for the type. */
16517
16518 static tree
16519 cp_parser_nonclass_name (cp_parser* parser)
16520 {
16521 tree type_decl;
16522 tree identifier;
16523
16524 cp_token *token = cp_lexer_peek_token (parser->lexer);
16525 identifier = cp_parser_identifier (parser);
16526 if (identifier == error_mark_node)
16527 return error_mark_node;
16528
16529 /* Look up the type-name. */
16530 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16531
16532 type_decl = strip_using_decl (type_decl);
16533
16534 /* If we found an overload set, then it may refer to a concept-name. */
16535 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16536 type_decl = decl;
16537
16538 if (TREE_CODE (type_decl) != TYPE_DECL
16539 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16540 {
16541 /* See if this is an Objective-C type. */
16542 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16543 tree type = objc_get_protocol_qualified_type (identifier, protos);
16544 if (type)
16545 type_decl = TYPE_NAME (type);
16546 }
16547
16548 /* Issue an error if we did not find a type-name. */
16549 if (TREE_CODE (type_decl) != TYPE_DECL
16550 /* In Objective-C, we have the complication that class names are
16551 normally type names and start declarations (eg, the
16552 "NSObject" in "NSObject *object;"), but can be used in an
16553 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16554 is an expression. So, a classname followed by a dot is not a
16555 valid type-name. */
16556 || (objc_is_class_name (TREE_TYPE (type_decl))
16557 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16558 {
16559 if (!cp_parser_simulate_error (parser))
16560 cp_parser_name_lookup_error (parser, identifier, type_decl,
16561 NLE_TYPE, token->location);
16562 return error_mark_node;
16563 }
16564 /* Remember that the name was used in the definition of the
16565 current class so that we can check later to see if the
16566 meaning would have been different after the class was
16567 entirely defined. */
16568 else if (type_decl != error_mark_node
16569 && !parser->scope)
16570 maybe_note_name_used_in_class (identifier, type_decl);
16571
16572 return type_decl;
16573 }
16574
16575 /* Parse an elaborated-type-specifier. Note that the grammar given
16576 here incorporates the resolution to DR68.
16577
16578 elaborated-type-specifier:
16579 class-key :: [opt] nested-name-specifier [opt] identifier
16580 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16581 enum-key :: [opt] nested-name-specifier [opt] identifier
16582 typename :: [opt] nested-name-specifier identifier
16583 typename :: [opt] nested-name-specifier template [opt]
16584 template-id
16585
16586 GNU extension:
16587
16588 elaborated-type-specifier:
16589 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16590 class-key attributes :: [opt] nested-name-specifier [opt]
16591 template [opt] template-id
16592 enum attributes :: [opt] nested-name-specifier [opt] identifier
16593
16594 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16595 declared `friend'. If IS_DECLARATION is TRUE, then this
16596 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16597 something is being declared.
16598
16599 Returns the TYPE specified. */
16600
16601 static tree
16602 cp_parser_elaborated_type_specifier (cp_parser* parser,
16603 bool is_friend,
16604 bool is_declaration)
16605 {
16606 enum tag_types tag_type;
16607 tree identifier;
16608 tree type = NULL_TREE;
16609 tree attributes = NULL_TREE;
16610 tree globalscope;
16611 cp_token *token = NULL;
16612
16613 /* See if we're looking at the `enum' keyword. */
16614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16615 {
16616 /* Consume the `enum' token. */
16617 cp_lexer_consume_token (parser->lexer);
16618 /* Remember that it's an enumeration type. */
16619 tag_type = enum_type;
16620 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16621 enums) is used here. */
16622 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16623 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16624 {
16625 pedwarn (input_location, 0, "elaborated-type-specifier "
16626 "for a scoped enum must not use the %<%D%> keyword",
16627 cp_lexer_peek_token (parser->lexer)->u.value);
16628 /* Consume the `struct' or `class' and parse it anyway. */
16629 cp_lexer_consume_token (parser->lexer);
16630 }
16631 /* Parse the attributes. */
16632 attributes = cp_parser_attributes_opt (parser);
16633 }
16634 /* Or, it might be `typename'. */
16635 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16636 RID_TYPENAME))
16637 {
16638 /* Consume the `typename' token. */
16639 cp_lexer_consume_token (parser->lexer);
16640 /* Remember that it's a `typename' type. */
16641 tag_type = typename_type;
16642 }
16643 /* Otherwise it must be a class-key. */
16644 else
16645 {
16646 tag_type = cp_parser_class_key (parser);
16647 if (tag_type == none_type)
16648 return error_mark_node;
16649 /* Parse the attributes. */
16650 attributes = cp_parser_attributes_opt (parser);
16651 }
16652
16653 /* Look for the `::' operator. */
16654 globalscope = cp_parser_global_scope_opt (parser,
16655 /*current_scope_valid_p=*/false);
16656 /* Look for the nested-name-specifier. */
16657 if (tag_type == typename_type && !globalscope)
16658 {
16659 if (!cp_parser_nested_name_specifier (parser,
16660 /*typename_keyword_p=*/true,
16661 /*check_dependency_p=*/true,
16662 /*type_p=*/true,
16663 is_declaration))
16664 return error_mark_node;
16665 }
16666 else
16667 /* Even though `typename' is not present, the proposed resolution
16668 to Core Issue 180 says that in `class A<T>::B', `B' should be
16669 considered a type-name, even if `A<T>' is dependent. */
16670 cp_parser_nested_name_specifier_opt (parser,
16671 /*typename_keyword_p=*/true,
16672 /*check_dependency_p=*/true,
16673 /*type_p=*/true,
16674 is_declaration);
16675 /* For everything but enumeration types, consider a template-id.
16676 For an enumeration type, consider only a plain identifier. */
16677 if (tag_type != enum_type)
16678 {
16679 bool template_p = false;
16680 tree decl;
16681
16682 /* Allow the `template' keyword. */
16683 template_p = cp_parser_optional_template_keyword (parser);
16684 /* If we didn't see `template', we don't know if there's a
16685 template-id or not. */
16686 if (!template_p)
16687 cp_parser_parse_tentatively (parser);
16688 /* Parse the template-id. */
16689 token = cp_lexer_peek_token (parser->lexer);
16690 decl = cp_parser_template_id (parser, template_p,
16691 /*check_dependency_p=*/true,
16692 tag_type,
16693 is_declaration);
16694 /* If we didn't find a template-id, look for an ordinary
16695 identifier. */
16696 if (!template_p && !cp_parser_parse_definitely (parser))
16697 ;
16698 /* We can get here when cp_parser_template_id, called by
16699 cp_parser_class_name with tag_type == none_type, succeeds
16700 and caches a BASELINK. Then, when called again here,
16701 instead of failing and returning an error_mark_node
16702 returns it (see template/typename17.C in C++11).
16703 ??? Could we diagnose this earlier? */
16704 else if (tag_type == typename_type && BASELINK_P (decl))
16705 {
16706 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16707 type = error_mark_node;
16708 }
16709 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16710 in effect, then we must assume that, upon instantiation, the
16711 template will correspond to a class. */
16712 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16713 && tag_type == typename_type)
16714 type = make_typename_type (parser->scope, decl,
16715 typename_type,
16716 /*complain=*/tf_error);
16717 /* If the `typename' keyword is in effect and DECL is not a type
16718 decl, then type is non existent. */
16719 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16720 ;
16721 else if (TREE_CODE (decl) == TYPE_DECL)
16722 type = check_elaborated_type_specifier (tag_type, decl,
16723 /*allow_template_p=*/true);
16724 else if (decl == error_mark_node)
16725 type = error_mark_node;
16726 }
16727
16728 if (!type)
16729 {
16730 token = cp_lexer_peek_token (parser->lexer);
16731 identifier = cp_parser_identifier (parser);
16732
16733 if (identifier == error_mark_node)
16734 {
16735 parser->scope = NULL_TREE;
16736 return error_mark_node;
16737 }
16738
16739 /* For a `typename', we needn't call xref_tag. */
16740 if (tag_type == typename_type
16741 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16742 return cp_parser_make_typename_type (parser, identifier,
16743 token->location);
16744
16745 /* Template parameter lists apply only if we are not within a
16746 function parameter list. */
16747 bool template_parm_lists_apply
16748 = parser->num_template_parameter_lists;
16749 if (template_parm_lists_apply)
16750 for (cp_binding_level *s = current_binding_level;
16751 s && s->kind != sk_template_parms;
16752 s = s->level_chain)
16753 if (s->kind == sk_function_parms)
16754 template_parm_lists_apply = false;
16755
16756 /* Look up a qualified name in the usual way. */
16757 if (parser->scope)
16758 {
16759 tree decl;
16760 tree ambiguous_decls;
16761
16762 decl = cp_parser_lookup_name (parser, identifier,
16763 tag_type,
16764 /*is_template=*/false,
16765 /*is_namespace=*/false,
16766 /*check_dependency=*/true,
16767 &ambiguous_decls,
16768 token->location);
16769
16770 /* If the lookup was ambiguous, an error will already have been
16771 issued. */
16772 if (ambiguous_decls)
16773 return error_mark_node;
16774
16775 /* If we are parsing friend declaration, DECL may be a
16776 TEMPLATE_DECL tree node here. However, we need to check
16777 whether this TEMPLATE_DECL results in valid code. Consider
16778 the following example:
16779
16780 namespace N {
16781 template <class T> class C {};
16782 }
16783 class X {
16784 template <class T> friend class N::C; // #1, valid code
16785 };
16786 template <class T> class Y {
16787 friend class N::C; // #2, invalid code
16788 };
16789
16790 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16791 name lookup of `N::C'. We see that friend declaration must
16792 be template for the code to be valid. Note that
16793 processing_template_decl does not work here since it is
16794 always 1 for the above two cases. */
16795
16796 decl = (cp_parser_maybe_treat_template_as_class
16797 (decl, /*tag_name_p=*/is_friend
16798 && template_parm_lists_apply));
16799
16800 if (TREE_CODE (decl) != TYPE_DECL)
16801 {
16802 cp_parser_diagnose_invalid_type_name (parser,
16803 identifier,
16804 token->location);
16805 return error_mark_node;
16806 }
16807
16808 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16809 {
16810 bool allow_template = (template_parm_lists_apply
16811 || DECL_SELF_REFERENCE_P (decl));
16812 type = check_elaborated_type_specifier (tag_type, decl,
16813 allow_template);
16814
16815 if (type == error_mark_node)
16816 return error_mark_node;
16817 }
16818
16819 /* Forward declarations of nested types, such as
16820
16821 class C1::C2;
16822 class C1::C2::C3;
16823
16824 are invalid unless all components preceding the final '::'
16825 are complete. If all enclosing types are complete, these
16826 declarations become merely pointless.
16827
16828 Invalid forward declarations of nested types are errors
16829 caught elsewhere in parsing. Those that are pointless arrive
16830 here. */
16831
16832 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16833 && !is_friend && !processing_explicit_instantiation)
16834 warning (0, "declaration %qD does not declare anything", decl);
16835
16836 type = TREE_TYPE (decl);
16837 }
16838 else
16839 {
16840 /* An elaborated-type-specifier sometimes introduces a new type and
16841 sometimes names an existing type. Normally, the rule is that it
16842 introduces a new type only if there is not an existing type of
16843 the same name already in scope. For example, given:
16844
16845 struct S {};
16846 void f() { struct S s; }
16847
16848 the `struct S' in the body of `f' is the same `struct S' as in
16849 the global scope; the existing definition is used. However, if
16850 there were no global declaration, this would introduce a new
16851 local class named `S'.
16852
16853 An exception to this rule applies to the following code:
16854
16855 namespace N { struct S; }
16856
16857 Here, the elaborated-type-specifier names a new type
16858 unconditionally; even if there is already an `S' in the
16859 containing scope this declaration names a new type.
16860 This exception only applies if the elaborated-type-specifier
16861 forms the complete declaration:
16862
16863 [class.name]
16864
16865 A declaration consisting solely of `class-key identifier ;' is
16866 either a redeclaration of the name in the current scope or a
16867 forward declaration of the identifier as a class name. It
16868 introduces the name into the current scope.
16869
16870 We are in this situation precisely when the next token is a `;'.
16871
16872 An exception to the exception is that a `friend' declaration does
16873 *not* name a new type; i.e., given:
16874
16875 struct S { friend struct T; };
16876
16877 `T' is not a new type in the scope of `S'.
16878
16879 Also, `new struct S' or `sizeof (struct S)' never results in the
16880 definition of a new type; a new type can only be declared in a
16881 declaration context. */
16882
16883 tag_scope ts;
16884 bool template_p;
16885
16886 if (is_friend)
16887 /* Friends have special name lookup rules. */
16888 ts = ts_within_enclosing_non_class;
16889 else if (is_declaration
16890 && cp_lexer_next_token_is (parser->lexer,
16891 CPP_SEMICOLON))
16892 /* This is a `class-key identifier ;' */
16893 ts = ts_current;
16894 else
16895 ts = ts_global;
16896
16897 template_p =
16898 (template_parm_lists_apply
16899 && (cp_parser_next_token_starts_class_definition_p (parser)
16900 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16901 /* An unqualified name was used to reference this type, so
16902 there were no qualifying templates. */
16903 if (template_parm_lists_apply
16904 && !cp_parser_check_template_parameters (parser,
16905 /*num_templates=*/0,
16906 token->location,
16907 /*declarator=*/NULL))
16908 return error_mark_node;
16909 type = xref_tag (tag_type, identifier, ts, template_p);
16910 }
16911 }
16912
16913 if (type == error_mark_node)
16914 return error_mark_node;
16915
16916 /* Allow attributes on forward declarations of classes. */
16917 if (attributes)
16918 {
16919 if (TREE_CODE (type) == TYPENAME_TYPE)
16920 warning (OPT_Wattributes,
16921 "attributes ignored on uninstantiated type");
16922 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16923 && ! processing_explicit_instantiation)
16924 warning (OPT_Wattributes,
16925 "attributes ignored on template instantiation");
16926 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16927 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16928 else
16929 warning (OPT_Wattributes,
16930 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16931 }
16932
16933 if (tag_type != enum_type)
16934 {
16935 /* Indicate whether this class was declared as a `class' or as a
16936 `struct'. */
16937 if (CLASS_TYPE_P (type))
16938 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16939 cp_parser_check_class_key (tag_type, type);
16940 }
16941
16942 /* A "<" cannot follow an elaborated type specifier. If that
16943 happens, the user was probably trying to form a template-id. */
16944 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16945 token->location);
16946
16947 return type;
16948 }
16949
16950 /* Parse an enum-specifier.
16951
16952 enum-specifier:
16953 enum-head { enumerator-list [opt] }
16954 enum-head { enumerator-list , } [C++0x]
16955
16956 enum-head:
16957 enum-key identifier [opt] enum-base [opt]
16958 enum-key nested-name-specifier identifier enum-base [opt]
16959
16960 enum-key:
16961 enum
16962 enum class [C++0x]
16963 enum struct [C++0x]
16964
16965 enum-base: [C++0x]
16966 : type-specifier-seq
16967
16968 opaque-enum-specifier:
16969 enum-key identifier enum-base [opt] ;
16970
16971 GNU Extensions:
16972 enum-key attributes[opt] identifier [opt] enum-base [opt]
16973 { enumerator-list [opt] }attributes[opt]
16974 enum-key attributes[opt] identifier [opt] enum-base [opt]
16975 { enumerator-list, }attributes[opt] [C++0x]
16976
16977 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16978 if the token stream isn't an enum-specifier after all. */
16979
16980 static tree
16981 cp_parser_enum_specifier (cp_parser* parser)
16982 {
16983 tree identifier;
16984 tree type = NULL_TREE;
16985 tree prev_scope;
16986 tree nested_name_specifier = NULL_TREE;
16987 tree attributes;
16988 bool scoped_enum_p = false;
16989 bool has_underlying_type = false;
16990 bool nested_being_defined = false;
16991 bool new_value_list = false;
16992 bool is_new_type = false;
16993 bool is_anonymous = false;
16994 tree underlying_type = NULL_TREE;
16995 cp_token *type_start_token = NULL;
16996 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
16997
16998 parser->colon_corrects_to_scope_p = false;
16999
17000 /* Parse tentatively so that we can back up if we don't find a
17001 enum-specifier. */
17002 cp_parser_parse_tentatively (parser);
17003
17004 /* Caller guarantees that the current token is 'enum', an identifier
17005 possibly follows, and the token after that is an opening brace.
17006 If we don't have an identifier, fabricate an anonymous name for
17007 the enumeration being defined. */
17008 cp_lexer_consume_token (parser->lexer);
17009
17010 /* Parse the "class" or "struct", which indicates a scoped
17011 enumeration type in C++0x. */
17012 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17013 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17014 {
17015 if (cxx_dialect < cxx11)
17016 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17017
17018 /* Consume the `struct' or `class' token. */
17019 cp_lexer_consume_token (parser->lexer);
17020
17021 scoped_enum_p = true;
17022 }
17023
17024 attributes = cp_parser_attributes_opt (parser);
17025
17026 /* Clear the qualification. */
17027 parser->scope = NULL_TREE;
17028 parser->qualifying_scope = NULL_TREE;
17029 parser->object_scope = NULL_TREE;
17030
17031 /* Figure out in what scope the declaration is being placed. */
17032 prev_scope = current_scope ();
17033
17034 type_start_token = cp_lexer_peek_token (parser->lexer);
17035
17036 push_deferring_access_checks (dk_no_check);
17037 nested_name_specifier
17038 = cp_parser_nested_name_specifier_opt (parser,
17039 /*typename_keyword_p=*/true,
17040 /*check_dependency_p=*/false,
17041 /*type_p=*/false,
17042 /*is_declaration=*/false);
17043
17044 if (nested_name_specifier)
17045 {
17046 tree name;
17047
17048 identifier = cp_parser_identifier (parser);
17049 name = cp_parser_lookup_name (parser, identifier,
17050 enum_type,
17051 /*is_template=*/false,
17052 /*is_namespace=*/false,
17053 /*check_dependency=*/true,
17054 /*ambiguous_decls=*/NULL,
17055 input_location);
17056 if (name && name != error_mark_node)
17057 {
17058 type = TREE_TYPE (name);
17059 if (TREE_CODE (type) == TYPENAME_TYPE)
17060 {
17061 /* Are template enums allowed in ISO? */
17062 if (template_parm_scope_p ())
17063 pedwarn (type_start_token->location, OPT_Wpedantic,
17064 "%qD is an enumeration template", name);
17065 /* ignore a typename reference, for it will be solved by name
17066 in start_enum. */
17067 type = NULL_TREE;
17068 }
17069 }
17070 else if (nested_name_specifier == error_mark_node)
17071 /* We already issued an error. */;
17072 else
17073 {
17074 error_at (type_start_token->location,
17075 "%qD does not name an enumeration in %qT",
17076 identifier, nested_name_specifier);
17077 nested_name_specifier = error_mark_node;
17078 }
17079 }
17080 else
17081 {
17082 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17083 identifier = cp_parser_identifier (parser);
17084 else
17085 {
17086 identifier = make_anon_name ();
17087 is_anonymous = true;
17088 if (scoped_enum_p)
17089 error_at (type_start_token->location,
17090 "anonymous scoped enum is not allowed");
17091 }
17092 }
17093 pop_deferring_access_checks ();
17094
17095 /* Check for the `:' that denotes a specified underlying type in C++0x.
17096 Note that a ':' could also indicate a bitfield width, however. */
17097 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17098 {
17099 cp_decl_specifier_seq type_specifiers;
17100
17101 /* Consume the `:'. */
17102 cp_lexer_consume_token (parser->lexer);
17103
17104 /* Parse the type-specifier-seq. */
17105 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17106 /*is_trailing_return=*/false,
17107 &type_specifiers);
17108
17109 /* At this point this is surely not elaborated type specifier. */
17110 if (!cp_parser_parse_definitely (parser))
17111 return NULL_TREE;
17112
17113 if (cxx_dialect < cxx11)
17114 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17115
17116 has_underlying_type = true;
17117
17118 /* If that didn't work, stop. */
17119 if (type_specifiers.type != error_mark_node)
17120 {
17121 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17122 /*initialized=*/0, NULL);
17123 if (underlying_type == error_mark_node
17124 || check_for_bare_parameter_packs (underlying_type))
17125 underlying_type = NULL_TREE;
17126 }
17127 }
17128
17129 /* Look for the `{' but don't consume it yet. */
17130 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17131 {
17132 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17133 {
17134 cp_parser_error (parser, "expected %<{%>");
17135 if (has_underlying_type)
17136 {
17137 type = NULL_TREE;
17138 goto out;
17139 }
17140 }
17141 /* An opaque-enum-specifier must have a ';' here. */
17142 if ((scoped_enum_p || underlying_type)
17143 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17144 {
17145 cp_parser_error (parser, "expected %<;%> or %<{%>");
17146 if (has_underlying_type)
17147 {
17148 type = NULL_TREE;
17149 goto out;
17150 }
17151 }
17152 }
17153
17154 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17155 return NULL_TREE;
17156
17157 if (nested_name_specifier)
17158 {
17159 if (CLASS_TYPE_P (nested_name_specifier))
17160 {
17161 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17162 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17163 push_scope (nested_name_specifier);
17164 }
17165 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17166 {
17167 push_nested_namespace (nested_name_specifier);
17168 }
17169 }
17170
17171 /* Issue an error message if type-definitions are forbidden here. */
17172 if (!cp_parser_check_type_definition (parser))
17173 type = error_mark_node;
17174 else
17175 /* Create the new type. We do this before consuming the opening
17176 brace so the enum will be recorded as being on the line of its
17177 tag (or the 'enum' keyword, if there is no tag). */
17178 type = start_enum (identifier, type, underlying_type,
17179 attributes, scoped_enum_p, &is_new_type);
17180
17181 /* If the next token is not '{' it is an opaque-enum-specifier or an
17182 elaborated-type-specifier. */
17183 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17184 {
17185 timevar_push (TV_PARSE_ENUM);
17186 if (nested_name_specifier
17187 && nested_name_specifier != error_mark_node)
17188 {
17189 /* The following catches invalid code such as:
17190 enum class S<int>::E { A, B, C }; */
17191 if (!processing_specialization
17192 && CLASS_TYPE_P (nested_name_specifier)
17193 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17194 error_at (type_start_token->location, "cannot add an enumerator "
17195 "list to a template instantiation");
17196
17197 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17198 {
17199 error_at (type_start_token->location,
17200 "%<%T::%E%> has not been declared",
17201 TYPE_CONTEXT (nested_name_specifier),
17202 nested_name_specifier);
17203 type = error_mark_node;
17204 }
17205 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17206 && !CLASS_TYPE_P (nested_name_specifier))
17207 {
17208 error_at (type_start_token->location, "nested name specifier "
17209 "%qT for enum declaration does not name a class "
17210 "or namespace", nested_name_specifier);
17211 type = error_mark_node;
17212 }
17213 /* If that scope does not contain the scope in which the
17214 class was originally declared, the program is invalid. */
17215 else if (prev_scope && !is_ancestor (prev_scope,
17216 nested_name_specifier))
17217 {
17218 if (at_namespace_scope_p ())
17219 error_at (type_start_token->location,
17220 "declaration of %qD in namespace %qD which does not "
17221 "enclose %qD",
17222 type, prev_scope, nested_name_specifier);
17223 else
17224 error_at (type_start_token->location,
17225 "declaration of %qD in %qD which does not "
17226 "enclose %qD",
17227 type, prev_scope, nested_name_specifier);
17228 type = error_mark_node;
17229 }
17230 /* If that scope is the scope where the declaration is being placed
17231 the program is invalid. */
17232 else if (CLASS_TYPE_P (nested_name_specifier)
17233 && CLASS_TYPE_P (prev_scope)
17234 && same_type_p (nested_name_specifier, prev_scope))
17235 {
17236 permerror (type_start_token->location,
17237 "extra qualification not allowed");
17238 nested_name_specifier = NULL_TREE;
17239 }
17240 }
17241
17242 if (scoped_enum_p)
17243 begin_scope (sk_scoped_enum, type);
17244
17245 /* Consume the opening brace. */
17246 cp_lexer_consume_token (parser->lexer);
17247
17248 if (type == error_mark_node)
17249 ; /* Nothing to add */
17250 else if (OPAQUE_ENUM_P (type)
17251 || (cxx_dialect > cxx98 && processing_specialization))
17252 {
17253 new_value_list = true;
17254 SET_OPAQUE_ENUM_P (type, false);
17255 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17256 }
17257 else
17258 {
17259 error_at (type_start_token->location,
17260 "multiple definition of %q#T", type);
17261 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17262 "previous definition here");
17263 type = error_mark_node;
17264 }
17265
17266 if (type == error_mark_node)
17267 cp_parser_skip_to_end_of_block_or_statement (parser);
17268 /* If the next token is not '}', then there are some enumerators. */
17269 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17270 {
17271 if (is_anonymous && !scoped_enum_p)
17272 pedwarn (type_start_token->location, OPT_Wpedantic,
17273 "ISO C++ forbids empty anonymous enum");
17274 }
17275 else
17276 cp_parser_enumerator_list (parser, type);
17277
17278 /* Consume the final '}'. */
17279 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17280
17281 if (scoped_enum_p)
17282 finish_scope ();
17283 timevar_pop (TV_PARSE_ENUM);
17284 }
17285 else
17286 {
17287 /* If a ';' follows, then it is an opaque-enum-specifier
17288 and additional restrictions apply. */
17289 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17290 {
17291 if (is_anonymous)
17292 error_at (type_start_token->location,
17293 "opaque-enum-specifier without name");
17294 else if (nested_name_specifier)
17295 error_at (type_start_token->location,
17296 "opaque-enum-specifier must use a simple identifier");
17297 }
17298 }
17299
17300 /* Look for trailing attributes to apply to this enumeration, and
17301 apply them if appropriate. */
17302 if (cp_parser_allow_gnu_extensions_p (parser))
17303 {
17304 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17305 cplus_decl_attributes (&type,
17306 trailing_attr,
17307 (int) ATTR_FLAG_TYPE_IN_PLACE);
17308 }
17309
17310 /* Finish up the enumeration. */
17311 if (type != error_mark_node)
17312 {
17313 if (new_value_list)
17314 finish_enum_value_list (type);
17315 if (is_new_type)
17316 finish_enum (type);
17317 }
17318
17319 if (nested_name_specifier)
17320 {
17321 if (CLASS_TYPE_P (nested_name_specifier))
17322 {
17323 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17324 pop_scope (nested_name_specifier);
17325 }
17326 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17327 {
17328 pop_nested_namespace (nested_name_specifier);
17329 }
17330 }
17331 out:
17332 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17333 return type;
17334 }
17335
17336 /* Parse an enumerator-list. The enumerators all have the indicated
17337 TYPE.
17338
17339 enumerator-list:
17340 enumerator-definition
17341 enumerator-list , enumerator-definition */
17342
17343 static void
17344 cp_parser_enumerator_list (cp_parser* parser, tree type)
17345 {
17346 while (true)
17347 {
17348 /* Parse an enumerator-definition. */
17349 cp_parser_enumerator_definition (parser, type);
17350
17351 /* If the next token is not a ',', we've reached the end of
17352 the list. */
17353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17354 break;
17355 /* Otherwise, consume the `,' and keep going. */
17356 cp_lexer_consume_token (parser->lexer);
17357 /* If the next token is a `}', there is a trailing comma. */
17358 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17359 {
17360 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17361 pedwarn (input_location, OPT_Wpedantic,
17362 "comma at end of enumerator list");
17363 break;
17364 }
17365 }
17366 }
17367
17368 /* Parse an enumerator-definition. The enumerator has the indicated
17369 TYPE.
17370
17371 enumerator-definition:
17372 enumerator
17373 enumerator = constant-expression
17374
17375 enumerator:
17376 identifier
17377
17378 GNU Extensions:
17379
17380 enumerator-definition:
17381 enumerator attributes [opt]
17382 enumerator attributes [opt] = constant-expression */
17383
17384 static void
17385 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17386 {
17387 tree identifier;
17388 tree value;
17389 location_t loc;
17390
17391 /* Save the input location because we are interested in the location
17392 of the identifier and not the location of the explicit value. */
17393 loc = cp_lexer_peek_token (parser->lexer)->location;
17394
17395 /* Look for the identifier. */
17396 identifier = cp_parser_identifier (parser);
17397 if (identifier == error_mark_node)
17398 return;
17399
17400 /* Parse any specified attributes. */
17401 tree attrs = cp_parser_attributes_opt (parser);
17402
17403 /* If the next token is an '=', then there is an explicit value. */
17404 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17405 {
17406 /* Consume the `=' token. */
17407 cp_lexer_consume_token (parser->lexer);
17408 /* Parse the value. */
17409 value = cp_parser_constant_expression (parser);
17410 }
17411 else
17412 value = NULL_TREE;
17413
17414 /* If we are processing a template, make sure the initializer of the
17415 enumerator doesn't contain any bare template parameter pack. */
17416 if (check_for_bare_parameter_packs (value))
17417 value = error_mark_node;
17418
17419 /* Create the enumerator. */
17420 build_enumerator (identifier, value, type, attrs, loc);
17421 }
17422
17423 /* Parse a namespace-name.
17424
17425 namespace-name:
17426 original-namespace-name
17427 namespace-alias
17428
17429 Returns the NAMESPACE_DECL for the namespace. */
17430
17431 static tree
17432 cp_parser_namespace_name (cp_parser* parser)
17433 {
17434 tree identifier;
17435 tree namespace_decl;
17436
17437 cp_token *token = cp_lexer_peek_token (parser->lexer);
17438
17439 /* Get the name of the namespace. */
17440 identifier = cp_parser_identifier (parser);
17441 if (identifier == error_mark_node)
17442 return error_mark_node;
17443
17444 /* Look up the identifier in the currently active scope. Look only
17445 for namespaces, due to:
17446
17447 [basic.lookup.udir]
17448
17449 When looking up a namespace-name in a using-directive or alias
17450 definition, only namespace names are considered.
17451
17452 And:
17453
17454 [basic.lookup.qual]
17455
17456 During the lookup of a name preceding the :: scope resolution
17457 operator, object, function, and enumerator names are ignored.
17458
17459 (Note that cp_parser_qualifying_entity only calls this
17460 function if the token after the name is the scope resolution
17461 operator.) */
17462 namespace_decl = cp_parser_lookup_name (parser, identifier,
17463 none_type,
17464 /*is_template=*/false,
17465 /*is_namespace=*/true,
17466 /*check_dependency=*/true,
17467 /*ambiguous_decls=*/NULL,
17468 token->location);
17469 /* If it's not a namespace, issue an error. */
17470 if (namespace_decl == error_mark_node
17471 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17472 {
17473 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17474 error_at (token->location, "%qD is not a namespace-name", identifier);
17475 cp_parser_error (parser, "expected namespace-name");
17476 namespace_decl = error_mark_node;
17477 }
17478
17479 return namespace_decl;
17480 }
17481
17482 /* Parse a namespace-definition.
17483
17484 namespace-definition:
17485 named-namespace-definition
17486 unnamed-namespace-definition
17487
17488 named-namespace-definition:
17489 original-namespace-definition
17490 extension-namespace-definition
17491
17492 original-namespace-definition:
17493 namespace identifier { namespace-body }
17494
17495 extension-namespace-definition:
17496 namespace original-namespace-name { namespace-body }
17497
17498 unnamed-namespace-definition:
17499 namespace { namespace-body } */
17500
17501 static void
17502 cp_parser_namespace_definition (cp_parser* parser)
17503 {
17504 tree identifier, attribs;
17505 bool has_visibility;
17506 bool is_inline;
17507 cp_token* token;
17508 int nested_definition_count = 0;
17509
17510 cp_ensure_no_omp_declare_simd (parser);
17511 cp_ensure_no_oacc_routine (parser);
17512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17513 {
17514 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17515 is_inline = true;
17516 cp_lexer_consume_token (parser->lexer);
17517 }
17518 else
17519 is_inline = false;
17520
17521 /* Look for the `namespace' keyword. */
17522 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17523
17524 /* Parse any specified attributes before the identifier. */
17525 attribs = cp_parser_attributes_opt (parser);
17526
17527 /* Get the name of the namespace. We do not attempt to distinguish
17528 between an original-namespace-definition and an
17529 extension-namespace-definition at this point. The semantic
17530 analysis routines are responsible for that. */
17531 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17532 identifier = cp_parser_identifier (parser);
17533 else
17534 identifier = NULL_TREE;
17535
17536 /* Parse any specified attributes after the identifier. */
17537 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17538 if (post_ident_attribs)
17539 {
17540 if (attribs)
17541 attribs = chainon (attribs, post_ident_attribs);
17542 else
17543 attribs = post_ident_attribs;
17544 }
17545
17546 /* Start the namespace. */
17547 push_namespace (identifier);
17548
17549 /* Parse any nested namespace definition. */
17550 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17551 {
17552 if (attribs)
17553 error_at (token->location, "a nested namespace definition cannot have attributes");
17554 if (cxx_dialect < cxx1z)
17555 pedwarn (input_location, OPT_Wpedantic,
17556 "nested namespace definitions only available with "
17557 "-std=c++1z or -std=gnu++1z");
17558 if (is_inline)
17559 error_at (token->location, "a nested namespace definition cannot be inline");
17560 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17561 {
17562 cp_lexer_consume_token (parser->lexer);
17563 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17564 identifier = cp_parser_identifier (parser);
17565 else
17566 {
17567 cp_parser_error (parser, "nested identifier required");
17568 break;
17569 }
17570 ++nested_definition_count;
17571 push_namespace (identifier);
17572 }
17573 }
17574
17575 /* Look for the `{' to validate starting the namespace. */
17576 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17577
17578 /* "inline namespace" is equivalent to a stub namespace definition
17579 followed by a strong using directive. */
17580 if (is_inline)
17581 {
17582 tree name_space = current_namespace;
17583 /* Set up namespace association. */
17584 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17585 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17586 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17587 /* Import the contents of the inline namespace. */
17588 pop_namespace ();
17589 do_using_directive (name_space);
17590 push_namespace (identifier);
17591 }
17592
17593 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17594
17595 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17596
17597 /* Parse the body of the namespace. */
17598 cp_parser_namespace_body (parser);
17599
17600 if (has_visibility)
17601 pop_visibility (1);
17602
17603 /* Finish the nested namespace definitions. */
17604 while (nested_definition_count--)
17605 pop_namespace ();
17606
17607 /* Finish the namespace. */
17608 pop_namespace ();
17609 /* Look for the final `}'. */
17610 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17611 }
17612
17613 /* Parse a namespace-body.
17614
17615 namespace-body:
17616 declaration-seq [opt] */
17617
17618 static void
17619 cp_parser_namespace_body (cp_parser* parser)
17620 {
17621 cp_parser_declaration_seq_opt (parser);
17622 }
17623
17624 /* Parse a namespace-alias-definition.
17625
17626 namespace-alias-definition:
17627 namespace identifier = qualified-namespace-specifier ; */
17628
17629 static void
17630 cp_parser_namespace_alias_definition (cp_parser* parser)
17631 {
17632 tree identifier;
17633 tree namespace_specifier;
17634
17635 cp_token *token = cp_lexer_peek_token (parser->lexer);
17636
17637 /* Look for the `namespace' keyword. */
17638 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17639 /* Look for the identifier. */
17640 identifier = cp_parser_identifier (parser);
17641 if (identifier == error_mark_node)
17642 return;
17643 /* Look for the `=' token. */
17644 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17645 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17646 {
17647 error_at (token->location, "%<namespace%> definition is not allowed here");
17648 /* Skip the definition. */
17649 cp_lexer_consume_token (parser->lexer);
17650 if (cp_parser_skip_to_closing_brace (parser))
17651 cp_lexer_consume_token (parser->lexer);
17652 return;
17653 }
17654 cp_parser_require (parser, CPP_EQ, RT_EQ);
17655 /* Look for the qualified-namespace-specifier. */
17656 namespace_specifier
17657 = cp_parser_qualified_namespace_specifier (parser);
17658 /* Look for the `;' token. */
17659 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17660
17661 /* Register the alias in the symbol table. */
17662 do_namespace_alias (identifier, namespace_specifier);
17663 }
17664
17665 /* Parse a qualified-namespace-specifier.
17666
17667 qualified-namespace-specifier:
17668 :: [opt] nested-name-specifier [opt] namespace-name
17669
17670 Returns a NAMESPACE_DECL corresponding to the specified
17671 namespace. */
17672
17673 static tree
17674 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17675 {
17676 /* Look for the optional `::'. */
17677 cp_parser_global_scope_opt (parser,
17678 /*current_scope_valid_p=*/false);
17679
17680 /* Look for the optional nested-name-specifier. */
17681 cp_parser_nested_name_specifier_opt (parser,
17682 /*typename_keyword_p=*/false,
17683 /*check_dependency_p=*/true,
17684 /*type_p=*/false,
17685 /*is_declaration=*/true);
17686
17687 return cp_parser_namespace_name (parser);
17688 }
17689
17690 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17691 access declaration.
17692
17693 using-declaration:
17694 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17695 using :: unqualified-id ;
17696
17697 access-declaration:
17698 qualified-id ;
17699
17700 */
17701
17702 static bool
17703 cp_parser_using_declaration (cp_parser* parser,
17704 bool access_declaration_p)
17705 {
17706 cp_token *token;
17707 bool typename_p = false;
17708 bool global_scope_p;
17709 tree decl;
17710 tree identifier;
17711 tree qscope;
17712 int oldcount = errorcount;
17713 cp_token *diag_token = NULL;
17714
17715 if (access_declaration_p)
17716 {
17717 diag_token = cp_lexer_peek_token (parser->lexer);
17718 cp_parser_parse_tentatively (parser);
17719 }
17720 else
17721 {
17722 /* Look for the `using' keyword. */
17723 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17724
17725 /* Peek at the next token. */
17726 token = cp_lexer_peek_token (parser->lexer);
17727 /* See if it's `typename'. */
17728 if (token->keyword == RID_TYPENAME)
17729 {
17730 /* Remember that we've seen it. */
17731 typename_p = true;
17732 /* Consume the `typename' token. */
17733 cp_lexer_consume_token (parser->lexer);
17734 }
17735 }
17736
17737 /* Look for the optional global scope qualification. */
17738 global_scope_p
17739 = (cp_parser_global_scope_opt (parser,
17740 /*current_scope_valid_p=*/false)
17741 != NULL_TREE);
17742
17743 /* If we saw `typename', or didn't see `::', then there must be a
17744 nested-name-specifier present. */
17745 if (typename_p || !global_scope_p)
17746 {
17747 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17748 /*check_dependency_p=*/true,
17749 /*type_p=*/false,
17750 /*is_declaration=*/true);
17751 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17752 {
17753 cp_parser_skip_to_end_of_block_or_statement (parser);
17754 return false;
17755 }
17756 }
17757 /* Otherwise, we could be in either of the two productions. In that
17758 case, treat the nested-name-specifier as optional. */
17759 else
17760 qscope = cp_parser_nested_name_specifier_opt (parser,
17761 /*typename_keyword_p=*/false,
17762 /*check_dependency_p=*/true,
17763 /*type_p=*/false,
17764 /*is_declaration=*/true);
17765 if (!qscope)
17766 qscope = global_namespace;
17767 else if (UNSCOPED_ENUM_P (qscope))
17768 qscope = CP_TYPE_CONTEXT (qscope);
17769
17770 if (access_declaration_p && cp_parser_error_occurred (parser))
17771 /* Something has already gone wrong; there's no need to parse
17772 further. Since an error has occurred, the return value of
17773 cp_parser_parse_definitely will be false, as required. */
17774 return cp_parser_parse_definitely (parser);
17775
17776 token = cp_lexer_peek_token (parser->lexer);
17777 /* Parse the unqualified-id. */
17778 identifier = cp_parser_unqualified_id (parser,
17779 /*template_keyword_p=*/false,
17780 /*check_dependency_p=*/true,
17781 /*declarator_p=*/true,
17782 /*optional_p=*/false);
17783
17784 if (access_declaration_p)
17785 {
17786 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17787 cp_parser_simulate_error (parser);
17788 if (!cp_parser_parse_definitely (parser))
17789 return false;
17790 }
17791
17792 /* The function we call to handle a using-declaration is different
17793 depending on what scope we are in. */
17794 if (qscope == error_mark_node || identifier == error_mark_node)
17795 ;
17796 else if (!identifier_p (identifier)
17797 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17798 /* [namespace.udecl]
17799
17800 A using declaration shall not name a template-id. */
17801 error_at (token->location,
17802 "a template-id may not appear in a using-declaration");
17803 else
17804 {
17805 if (at_class_scope_p ())
17806 {
17807 /* Create the USING_DECL. */
17808 decl = do_class_using_decl (parser->scope, identifier);
17809
17810 if (decl && typename_p)
17811 USING_DECL_TYPENAME_P (decl) = 1;
17812
17813 if (check_for_bare_parameter_packs (decl))
17814 {
17815 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17816 return false;
17817 }
17818 else
17819 /* Add it to the list of members in this class. */
17820 finish_member_declaration (decl);
17821 }
17822 else
17823 {
17824 decl = cp_parser_lookup_name_simple (parser,
17825 identifier,
17826 token->location);
17827 if (decl == error_mark_node)
17828 cp_parser_name_lookup_error (parser, identifier,
17829 decl, NLE_NULL,
17830 token->location);
17831 else if (check_for_bare_parameter_packs (decl))
17832 {
17833 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17834 return false;
17835 }
17836 else if (!at_namespace_scope_p ())
17837 do_local_using_decl (decl, qscope, identifier);
17838 else
17839 do_toplevel_using_decl (decl, qscope, identifier);
17840 }
17841 }
17842
17843 /* Look for the final `;'. */
17844 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17845
17846 if (access_declaration_p && errorcount == oldcount)
17847 warning_at (diag_token->location, OPT_Wdeprecated,
17848 "access declarations are deprecated "
17849 "in favour of using-declarations; "
17850 "suggestion: add the %<using%> keyword");
17851
17852 return true;
17853 }
17854
17855 /* Parse an alias-declaration.
17856
17857 alias-declaration:
17858 using identifier attribute-specifier-seq [opt] = type-id */
17859
17860 static tree
17861 cp_parser_alias_declaration (cp_parser* parser)
17862 {
17863 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17864 location_t id_location;
17865 cp_declarator *declarator;
17866 cp_decl_specifier_seq decl_specs;
17867 bool member_p;
17868 const char *saved_message = NULL;
17869
17870 /* Look for the `using' keyword. */
17871 cp_token *using_token
17872 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17873 if (using_token == NULL)
17874 return error_mark_node;
17875
17876 id_location = cp_lexer_peek_token (parser->lexer)->location;
17877 id = cp_parser_identifier (parser);
17878 if (id == error_mark_node)
17879 return error_mark_node;
17880
17881 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17882 attributes = cp_parser_attributes_opt (parser);
17883 if (attributes == error_mark_node)
17884 return error_mark_node;
17885
17886 cp_parser_require (parser, CPP_EQ, RT_EQ);
17887
17888 if (cp_parser_error_occurred (parser))
17889 return error_mark_node;
17890
17891 cp_parser_commit_to_tentative_parse (parser);
17892
17893 /* Now we are going to parse the type-id of the declaration. */
17894
17895 /*
17896 [dcl.type]/3 says:
17897
17898 "A type-specifier-seq shall not define a class or enumeration
17899 unless it appears in the type-id of an alias-declaration (7.1.3) that
17900 is not the declaration of a template-declaration."
17901
17902 In other words, if we currently are in an alias template, the
17903 type-id should not define a type.
17904
17905 So let's set parser->type_definition_forbidden_message in that
17906 case; cp_parser_check_type_definition (called by
17907 cp_parser_class_specifier) will then emit an error if a type is
17908 defined in the type-id. */
17909 if (parser->num_template_parameter_lists)
17910 {
17911 saved_message = parser->type_definition_forbidden_message;
17912 parser->type_definition_forbidden_message =
17913 G_("types may not be defined in alias template declarations");
17914 }
17915
17916 type = cp_parser_type_id (parser);
17917
17918 /* Restore the error message if need be. */
17919 if (parser->num_template_parameter_lists)
17920 parser->type_definition_forbidden_message = saved_message;
17921
17922 if (type == error_mark_node
17923 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17924 {
17925 cp_parser_skip_to_end_of_block_or_statement (parser);
17926 return error_mark_node;
17927 }
17928
17929 /* A typedef-name can also be introduced by an alias-declaration. The
17930 identifier following the using keyword becomes a typedef-name. It has
17931 the same semantics as if it were introduced by the typedef
17932 specifier. In particular, it does not define a new type and it shall
17933 not appear in the type-id. */
17934
17935 clear_decl_specs (&decl_specs);
17936 decl_specs.type = type;
17937 if (attributes != NULL_TREE)
17938 {
17939 decl_specs.attributes = attributes;
17940 set_and_check_decl_spec_loc (&decl_specs,
17941 ds_attribute,
17942 attrs_token);
17943 }
17944 set_and_check_decl_spec_loc (&decl_specs,
17945 ds_typedef,
17946 using_token);
17947 set_and_check_decl_spec_loc (&decl_specs,
17948 ds_alias,
17949 using_token);
17950
17951 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17952 declarator->id_loc = id_location;
17953
17954 member_p = at_class_scope_p ();
17955 if (member_p)
17956 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17957 NULL_TREE, attributes);
17958 else
17959 decl = start_decl (declarator, &decl_specs, 0,
17960 attributes, NULL_TREE, &pushed_scope);
17961 if (decl == error_mark_node)
17962 return decl;
17963
17964 // Attach constraints to the alias declaration.
17965 if (flag_concepts && current_template_parms)
17966 {
17967 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17968 tree constr = build_constraints (reqs, NULL_TREE);
17969 set_constraints (decl, constr);
17970 }
17971
17972 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17973
17974 if (pushed_scope)
17975 pop_scope (pushed_scope);
17976
17977 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17978 added into the symbol table; otherwise, return the TYPE_DECL. */
17979 if (DECL_LANG_SPECIFIC (decl)
17980 && DECL_TEMPLATE_INFO (decl)
17981 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17982 {
17983 decl = DECL_TI_TEMPLATE (decl);
17984 if (member_p)
17985 check_member_template (decl);
17986 }
17987
17988 return decl;
17989 }
17990
17991 /* Parse a using-directive.
17992
17993 using-directive:
17994 using namespace :: [opt] nested-name-specifier [opt]
17995 namespace-name ; */
17996
17997 static void
17998 cp_parser_using_directive (cp_parser* parser)
17999 {
18000 tree namespace_decl;
18001 tree attribs;
18002
18003 /* Look for the `using' keyword. */
18004 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18005 /* And the `namespace' keyword. */
18006 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18007 /* Look for the optional `::' operator. */
18008 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18009 /* And the optional nested-name-specifier. */
18010 cp_parser_nested_name_specifier_opt (parser,
18011 /*typename_keyword_p=*/false,
18012 /*check_dependency_p=*/true,
18013 /*type_p=*/false,
18014 /*is_declaration=*/true);
18015 /* Get the namespace being used. */
18016 namespace_decl = cp_parser_namespace_name (parser);
18017 /* And any specified attributes. */
18018 attribs = cp_parser_attributes_opt (parser);
18019 /* Update the symbol table. */
18020 parse_using_directive (namespace_decl, attribs);
18021 /* Look for the final `;'. */
18022 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18023 }
18024
18025 /* Parse an asm-definition.
18026
18027 asm-definition:
18028 asm ( string-literal ) ;
18029
18030 GNU Extension:
18031
18032 asm-definition:
18033 asm volatile [opt] ( string-literal ) ;
18034 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18035 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18036 : asm-operand-list [opt] ) ;
18037 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18038 : asm-operand-list [opt]
18039 : asm-clobber-list [opt] ) ;
18040 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18041 : asm-clobber-list [opt]
18042 : asm-goto-list ) ; */
18043
18044 static void
18045 cp_parser_asm_definition (cp_parser* parser)
18046 {
18047 tree string;
18048 tree outputs = NULL_TREE;
18049 tree inputs = NULL_TREE;
18050 tree clobbers = NULL_TREE;
18051 tree labels = NULL_TREE;
18052 tree asm_stmt;
18053 bool volatile_p = false;
18054 bool extended_p = false;
18055 bool invalid_inputs_p = false;
18056 bool invalid_outputs_p = false;
18057 bool goto_p = false;
18058 required_token missing = RT_NONE;
18059
18060 /* Look for the `asm' keyword. */
18061 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18062
18063 if (parser->in_function_body
18064 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18065 {
18066 error ("%<asm%> in %<constexpr%> function");
18067 cp_function_chain->invalid_constexpr = true;
18068 }
18069
18070 /* See if the next token is `volatile'. */
18071 if (cp_parser_allow_gnu_extensions_p (parser)
18072 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18073 {
18074 /* Remember that we saw the `volatile' keyword. */
18075 volatile_p = true;
18076 /* Consume the token. */
18077 cp_lexer_consume_token (parser->lexer);
18078 }
18079 if (cp_parser_allow_gnu_extensions_p (parser)
18080 && parser->in_function_body
18081 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18082 {
18083 /* Remember that we saw the `goto' keyword. */
18084 goto_p = true;
18085 /* Consume the token. */
18086 cp_lexer_consume_token (parser->lexer);
18087 }
18088 /* Look for the opening `('. */
18089 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18090 return;
18091 /* Look for the string. */
18092 string = cp_parser_string_literal (parser, false, false);
18093 if (string == error_mark_node)
18094 {
18095 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18096 /*consume_paren=*/true);
18097 return;
18098 }
18099
18100 /* If we're allowing GNU extensions, check for the extended assembly
18101 syntax. Unfortunately, the `:' tokens need not be separated by
18102 a space in C, and so, for compatibility, we tolerate that here
18103 too. Doing that means that we have to treat the `::' operator as
18104 two `:' tokens. */
18105 if (cp_parser_allow_gnu_extensions_p (parser)
18106 && parser->in_function_body
18107 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18108 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18109 {
18110 bool inputs_p = false;
18111 bool clobbers_p = false;
18112 bool labels_p = false;
18113
18114 /* The extended syntax was used. */
18115 extended_p = true;
18116
18117 /* Look for outputs. */
18118 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18119 {
18120 /* Consume the `:'. */
18121 cp_lexer_consume_token (parser->lexer);
18122 /* Parse the output-operands. */
18123 if (cp_lexer_next_token_is_not (parser->lexer,
18124 CPP_COLON)
18125 && cp_lexer_next_token_is_not (parser->lexer,
18126 CPP_SCOPE)
18127 && cp_lexer_next_token_is_not (parser->lexer,
18128 CPP_CLOSE_PAREN)
18129 && !goto_p)
18130 {
18131 outputs = cp_parser_asm_operand_list (parser);
18132 if (outputs == error_mark_node)
18133 invalid_outputs_p = true;
18134 }
18135 }
18136 /* If the next token is `::', there are no outputs, and the
18137 next token is the beginning of the inputs. */
18138 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18139 /* The inputs are coming next. */
18140 inputs_p = true;
18141
18142 /* Look for inputs. */
18143 if (inputs_p
18144 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18145 {
18146 /* Consume the `:' or `::'. */
18147 cp_lexer_consume_token (parser->lexer);
18148 /* Parse the output-operands. */
18149 if (cp_lexer_next_token_is_not (parser->lexer,
18150 CPP_COLON)
18151 && cp_lexer_next_token_is_not (parser->lexer,
18152 CPP_SCOPE)
18153 && cp_lexer_next_token_is_not (parser->lexer,
18154 CPP_CLOSE_PAREN))
18155 {
18156 inputs = cp_parser_asm_operand_list (parser);
18157 if (inputs == error_mark_node)
18158 invalid_inputs_p = true;
18159 }
18160 }
18161 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18162 /* The clobbers are coming next. */
18163 clobbers_p = true;
18164
18165 /* Look for clobbers. */
18166 if (clobbers_p
18167 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18168 {
18169 clobbers_p = true;
18170 /* Consume the `:' or `::'. */
18171 cp_lexer_consume_token (parser->lexer);
18172 /* Parse the clobbers. */
18173 if (cp_lexer_next_token_is_not (parser->lexer,
18174 CPP_COLON)
18175 && cp_lexer_next_token_is_not (parser->lexer,
18176 CPP_CLOSE_PAREN))
18177 clobbers = cp_parser_asm_clobber_list (parser);
18178 }
18179 else if (goto_p
18180 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18181 /* The labels are coming next. */
18182 labels_p = true;
18183
18184 /* Look for labels. */
18185 if (labels_p
18186 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18187 {
18188 labels_p = true;
18189 /* Consume the `:' or `::'. */
18190 cp_lexer_consume_token (parser->lexer);
18191 /* Parse the labels. */
18192 labels = cp_parser_asm_label_list (parser);
18193 }
18194
18195 if (goto_p && !labels_p)
18196 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18197 }
18198 else if (goto_p)
18199 missing = RT_COLON_SCOPE;
18200
18201 /* Look for the closing `)'. */
18202 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18203 missing ? missing : RT_CLOSE_PAREN))
18204 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18205 /*consume_paren=*/true);
18206 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18207
18208 if (!invalid_inputs_p && !invalid_outputs_p)
18209 {
18210 /* Create the ASM_EXPR. */
18211 if (parser->in_function_body)
18212 {
18213 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18214 inputs, clobbers, labels);
18215 /* If the extended syntax was not used, mark the ASM_EXPR. */
18216 if (!extended_p)
18217 {
18218 tree temp = asm_stmt;
18219 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18220 temp = TREE_OPERAND (temp, 0);
18221
18222 ASM_INPUT_P (temp) = 1;
18223 }
18224 }
18225 else
18226 symtab->finalize_toplevel_asm (string);
18227 }
18228 }
18229
18230 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18231 type that comes from the decl-specifier-seq. */
18232
18233 static tree
18234 strip_declarator_types (tree type, cp_declarator *declarator)
18235 {
18236 for (cp_declarator *d = declarator; d;)
18237 switch (d->kind)
18238 {
18239 case cdk_id:
18240 case cdk_error:
18241 d = NULL;
18242 break;
18243
18244 default:
18245 if (TYPE_PTRMEMFUNC_P (type))
18246 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18247 type = TREE_TYPE (type);
18248 d = d->declarator;
18249 break;
18250 }
18251
18252 return type;
18253 }
18254
18255 /* Declarators [gram.dcl.decl] */
18256
18257 /* Parse an init-declarator.
18258
18259 init-declarator:
18260 declarator initializer [opt]
18261
18262 GNU Extension:
18263
18264 init-declarator:
18265 declarator asm-specification [opt] attributes [opt] initializer [opt]
18266
18267 function-definition:
18268 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18269 function-body
18270 decl-specifier-seq [opt] declarator function-try-block
18271
18272 GNU Extension:
18273
18274 function-definition:
18275 __extension__ function-definition
18276
18277 TM Extension:
18278
18279 function-definition:
18280 decl-specifier-seq [opt] declarator function-transaction-block
18281
18282 The DECL_SPECIFIERS apply to this declarator. Returns a
18283 representation of the entity declared. If MEMBER_P is TRUE, then
18284 this declarator appears in a class scope. The new DECL created by
18285 this declarator is returned.
18286
18287 The CHECKS are access checks that should be performed once we know
18288 what entity is being declared (and, therefore, what classes have
18289 befriended it).
18290
18291 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18292 for a function-definition here as well. If the declarator is a
18293 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18294 be TRUE upon return. By that point, the function-definition will
18295 have been completely parsed.
18296
18297 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18298 is FALSE.
18299
18300 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18301 parsed declaration if it is an uninitialized single declarator not followed
18302 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18303 if present, will not be consumed. If returned, this declarator will be
18304 created with SD_INITIALIZED but will not call cp_finish_decl.
18305
18306 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18307 and there is an initializer, the pointed location_t is set to the
18308 location of the '=' or `(', or '{' in C++11 token introducing the
18309 initializer. */
18310
18311 static tree
18312 cp_parser_init_declarator (cp_parser* parser,
18313 cp_decl_specifier_seq *decl_specifiers,
18314 vec<deferred_access_check, va_gc> *checks,
18315 bool function_definition_allowed_p,
18316 bool member_p,
18317 int declares_class_or_enum,
18318 bool* function_definition_p,
18319 tree* maybe_range_for_decl,
18320 location_t* init_loc,
18321 tree* auto_result)
18322 {
18323 cp_token *token = NULL, *asm_spec_start_token = NULL,
18324 *attributes_start_token = NULL;
18325 cp_declarator *declarator;
18326 tree prefix_attributes;
18327 tree attributes = NULL;
18328 tree asm_specification;
18329 tree initializer;
18330 tree decl = NULL_TREE;
18331 tree scope;
18332 int is_initialized;
18333 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18334 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18335 "(...)". */
18336 enum cpp_ttype initialization_kind;
18337 bool is_direct_init = false;
18338 bool is_non_constant_init;
18339 int ctor_dtor_or_conv_p;
18340 bool friend_p = cp_parser_friend_p (decl_specifiers);
18341 tree pushed_scope = NULL_TREE;
18342 bool range_for_decl_p = false;
18343 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18344 location_t tmp_init_loc = UNKNOWN_LOCATION;
18345
18346 /* Gather the attributes that were provided with the
18347 decl-specifiers. */
18348 prefix_attributes = decl_specifiers->attributes;
18349
18350 /* Assume that this is not the declarator for a function
18351 definition. */
18352 if (function_definition_p)
18353 *function_definition_p = false;
18354
18355 /* Default arguments are only permitted for function parameters. */
18356 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18357 parser->default_arg_ok_p = false;
18358
18359 /* Defer access checks while parsing the declarator; we cannot know
18360 what names are accessible until we know what is being
18361 declared. */
18362 resume_deferring_access_checks ();
18363
18364 /* Parse the declarator. */
18365 token = cp_lexer_peek_token (parser->lexer);
18366 declarator
18367 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18368 &ctor_dtor_or_conv_p,
18369 /*parenthesized_p=*/NULL,
18370 member_p, friend_p);
18371 /* Gather up the deferred checks. */
18372 stop_deferring_access_checks ();
18373
18374 parser->default_arg_ok_p = saved_default_arg_ok_p;
18375
18376 /* If the DECLARATOR was erroneous, there's no need to go
18377 further. */
18378 if (declarator == cp_error_declarator)
18379 return error_mark_node;
18380
18381 /* Check that the number of template-parameter-lists is OK. */
18382 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18383 token->location))
18384 return error_mark_node;
18385
18386 if (declares_class_or_enum & 2)
18387 cp_parser_check_for_definition_in_return_type (declarator,
18388 decl_specifiers->type,
18389 decl_specifiers->locations[ds_type_spec]);
18390
18391 /* Figure out what scope the entity declared by the DECLARATOR is
18392 located in. `grokdeclarator' sometimes changes the scope, so
18393 we compute it now. */
18394 scope = get_scope_of_declarator (declarator);
18395
18396 /* Perform any lookups in the declared type which were thought to be
18397 dependent, but are not in the scope of the declarator. */
18398 decl_specifiers->type
18399 = maybe_update_decl_type (decl_specifiers->type, scope);
18400
18401 /* If we're allowing GNU extensions, look for an
18402 asm-specification. */
18403 if (cp_parser_allow_gnu_extensions_p (parser))
18404 {
18405 /* Look for an asm-specification. */
18406 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18407 asm_specification = cp_parser_asm_specification_opt (parser);
18408 }
18409 else
18410 asm_specification = NULL_TREE;
18411
18412 /* Look for attributes. */
18413 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18414 attributes = cp_parser_attributes_opt (parser);
18415
18416 /* Peek at the next token. */
18417 token = cp_lexer_peek_token (parser->lexer);
18418
18419 bool bogus_implicit_tmpl = false;
18420
18421 if (function_declarator_p (declarator))
18422 {
18423 /* Check to see if the token indicates the start of a
18424 function-definition. */
18425 if (cp_parser_token_starts_function_definition_p (token))
18426 {
18427 if (!function_definition_allowed_p)
18428 {
18429 /* If a function-definition should not appear here, issue an
18430 error message. */
18431 cp_parser_error (parser,
18432 "a function-definition is not allowed here");
18433 return error_mark_node;
18434 }
18435
18436 location_t func_brace_location
18437 = cp_lexer_peek_token (parser->lexer)->location;
18438
18439 /* Neither attributes nor an asm-specification are allowed
18440 on a function-definition. */
18441 if (asm_specification)
18442 error_at (asm_spec_start_token->location,
18443 "an asm-specification is not allowed "
18444 "on a function-definition");
18445 if (attributes)
18446 error_at (attributes_start_token->location,
18447 "attributes are not allowed "
18448 "on a function-definition");
18449 /* This is a function-definition. */
18450 *function_definition_p = true;
18451
18452 /* Parse the function definition. */
18453 if (member_p)
18454 decl = cp_parser_save_member_function_body (parser,
18455 decl_specifiers,
18456 declarator,
18457 prefix_attributes);
18458 else
18459 decl =
18460 (cp_parser_function_definition_from_specifiers_and_declarator
18461 (parser, decl_specifiers, prefix_attributes, declarator));
18462
18463 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18464 {
18465 /* This is where the prologue starts... */
18466 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18467 = func_brace_location;
18468 }
18469
18470 return decl;
18471 }
18472 }
18473 else if (parser->fully_implicit_function_template_p)
18474 {
18475 /* A non-template declaration involving a function parameter list
18476 containing an implicit template parameter will be made into a
18477 template. If the resulting declaration is not going to be an
18478 actual function then finish the template scope here to prevent it.
18479 An error message will be issued once we have a decl to talk about.
18480
18481 FIXME probably we should do type deduction rather than create an
18482 implicit template, but the standard currently doesn't allow it. */
18483 bogus_implicit_tmpl = true;
18484 finish_fully_implicit_template (parser, NULL_TREE);
18485 }
18486
18487 /* [dcl.dcl]
18488
18489 Only in function declarations for constructors, destructors, and
18490 type conversions can the decl-specifier-seq be omitted.
18491
18492 We explicitly postpone this check past the point where we handle
18493 function-definitions because we tolerate function-definitions
18494 that are missing their return types in some modes. */
18495 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18496 {
18497 cp_parser_error (parser,
18498 "expected constructor, destructor, or type conversion");
18499 return error_mark_node;
18500 }
18501
18502 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18503 if (token->type == CPP_EQ
18504 || token->type == CPP_OPEN_PAREN
18505 || token->type == CPP_OPEN_BRACE)
18506 {
18507 is_initialized = SD_INITIALIZED;
18508 initialization_kind = token->type;
18509 if (maybe_range_for_decl)
18510 *maybe_range_for_decl = error_mark_node;
18511 tmp_init_loc = token->location;
18512 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18513 *init_loc = tmp_init_loc;
18514
18515 if (token->type == CPP_EQ
18516 && function_declarator_p (declarator))
18517 {
18518 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18519 if (t2->keyword == RID_DEFAULT)
18520 is_initialized = SD_DEFAULTED;
18521 else if (t2->keyword == RID_DELETE)
18522 is_initialized = SD_DELETED;
18523 }
18524 }
18525 else
18526 {
18527 /* If the init-declarator isn't initialized and isn't followed by a
18528 `,' or `;', it's not a valid init-declarator. */
18529 if (token->type != CPP_COMMA
18530 && token->type != CPP_SEMICOLON)
18531 {
18532 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18533 range_for_decl_p = true;
18534 else
18535 {
18536 if (!maybe_range_for_decl)
18537 cp_parser_error (parser, "expected initializer");
18538 return error_mark_node;
18539 }
18540 }
18541 is_initialized = SD_UNINITIALIZED;
18542 initialization_kind = CPP_EOF;
18543 }
18544
18545 /* Because start_decl has side-effects, we should only call it if we
18546 know we're going ahead. By this point, we know that we cannot
18547 possibly be looking at any other construct. */
18548 cp_parser_commit_to_tentative_parse (parser);
18549
18550 /* Enter the newly declared entry in the symbol table. If we're
18551 processing a declaration in a class-specifier, we wait until
18552 after processing the initializer. */
18553 if (!member_p)
18554 {
18555 if (parser->in_unbraced_linkage_specification_p)
18556 decl_specifiers->storage_class = sc_extern;
18557 decl = start_decl (declarator, decl_specifiers,
18558 range_for_decl_p? SD_INITIALIZED : is_initialized,
18559 attributes, prefix_attributes, &pushed_scope);
18560 cp_finalize_omp_declare_simd (parser, decl);
18561 cp_finalize_oacc_routine (parser, decl, false);
18562 /* Adjust location of decl if declarator->id_loc is more appropriate:
18563 set, and decl wasn't merged with another decl, in which case its
18564 location would be different from input_location, and more accurate. */
18565 if (DECL_P (decl)
18566 && declarator->id_loc != UNKNOWN_LOCATION
18567 && DECL_SOURCE_LOCATION (decl) == input_location)
18568 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18569 }
18570 else if (scope)
18571 /* Enter the SCOPE. That way unqualified names appearing in the
18572 initializer will be looked up in SCOPE. */
18573 pushed_scope = push_scope (scope);
18574
18575 /* Perform deferred access control checks, now that we know in which
18576 SCOPE the declared entity resides. */
18577 if (!member_p && decl)
18578 {
18579 tree saved_current_function_decl = NULL_TREE;
18580
18581 /* If the entity being declared is a function, pretend that we
18582 are in its scope. If it is a `friend', it may have access to
18583 things that would not otherwise be accessible. */
18584 if (TREE_CODE (decl) == FUNCTION_DECL)
18585 {
18586 saved_current_function_decl = current_function_decl;
18587 current_function_decl = decl;
18588 }
18589
18590 /* Perform access checks for template parameters. */
18591 cp_parser_perform_template_parameter_access_checks (checks);
18592
18593 /* Perform the access control checks for the declarator and the
18594 decl-specifiers. */
18595 perform_deferred_access_checks (tf_warning_or_error);
18596
18597 /* Restore the saved value. */
18598 if (TREE_CODE (decl) == FUNCTION_DECL)
18599 current_function_decl = saved_current_function_decl;
18600 }
18601
18602 /* Parse the initializer. */
18603 initializer = NULL_TREE;
18604 is_direct_init = false;
18605 is_non_constant_init = true;
18606 if (is_initialized)
18607 {
18608 if (function_declarator_p (declarator))
18609 {
18610 if (initialization_kind == CPP_EQ)
18611 initializer = cp_parser_pure_specifier (parser);
18612 else
18613 {
18614 /* If the declaration was erroneous, we don't really
18615 know what the user intended, so just silently
18616 consume the initializer. */
18617 if (decl != error_mark_node)
18618 error_at (tmp_init_loc, "initializer provided for function");
18619 cp_parser_skip_to_closing_parenthesis (parser,
18620 /*recovering=*/true,
18621 /*or_comma=*/false,
18622 /*consume_paren=*/true);
18623 }
18624 }
18625 else
18626 {
18627 /* We want to record the extra mangling scope for in-class
18628 initializers of class members and initializers of static data
18629 member templates. The former involves deferring
18630 parsing of the initializer until end of class as with default
18631 arguments. So right here we only handle the latter. */
18632 if (!member_p && processing_template_decl)
18633 start_lambda_scope (decl);
18634 initializer = cp_parser_initializer (parser,
18635 &is_direct_init,
18636 &is_non_constant_init);
18637 if (!member_p && processing_template_decl)
18638 finish_lambda_scope ();
18639 if (initializer == error_mark_node)
18640 cp_parser_skip_to_end_of_statement (parser);
18641 }
18642 }
18643
18644 /* The old parser allows attributes to appear after a parenthesized
18645 initializer. Mark Mitchell proposed removing this functionality
18646 on the GCC mailing lists on 2002-08-13. This parser accepts the
18647 attributes -- but ignores them. */
18648 if (cp_parser_allow_gnu_extensions_p (parser)
18649 && initialization_kind == CPP_OPEN_PAREN)
18650 if (cp_parser_attributes_opt (parser))
18651 warning (OPT_Wattributes,
18652 "attributes after parenthesized initializer ignored");
18653
18654 /* And now complain about a non-function implicit template. */
18655 if (bogus_implicit_tmpl && decl != error_mark_node)
18656 error_at (DECL_SOURCE_LOCATION (decl),
18657 "non-function %qD declared as implicit template", decl);
18658
18659 /* For an in-class declaration, use `grokfield' to create the
18660 declaration. */
18661 if (member_p)
18662 {
18663 if (pushed_scope)
18664 {
18665 pop_scope (pushed_scope);
18666 pushed_scope = NULL_TREE;
18667 }
18668 decl = grokfield (declarator, decl_specifiers,
18669 initializer, !is_non_constant_init,
18670 /*asmspec=*/NULL_TREE,
18671 chainon (attributes, prefix_attributes));
18672 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18673 cp_parser_save_default_args (parser, decl);
18674 cp_finalize_omp_declare_simd (parser, decl);
18675 cp_finalize_oacc_routine (parser, decl, false);
18676 }
18677
18678 /* Finish processing the declaration. But, skip member
18679 declarations. */
18680 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18681 {
18682 cp_finish_decl (decl,
18683 initializer, !is_non_constant_init,
18684 asm_specification,
18685 /* If the initializer is in parentheses, then this is
18686 a direct-initialization, which means that an
18687 `explicit' constructor is OK. Otherwise, an
18688 `explicit' constructor cannot be used. */
18689 ((is_direct_init || !is_initialized)
18690 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18691 }
18692 else if ((cxx_dialect != cxx98) && friend_p
18693 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18694 /* Core issue #226 (C++0x only): A default template-argument
18695 shall not be specified in a friend class template
18696 declaration. */
18697 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18698 /*is_partial=*/false, /*is_friend_decl=*/1);
18699
18700 if (!friend_p && pushed_scope)
18701 pop_scope (pushed_scope);
18702
18703 if (function_declarator_p (declarator)
18704 && parser->fully_implicit_function_template_p)
18705 {
18706 if (member_p)
18707 decl = finish_fully_implicit_template (parser, decl);
18708 else
18709 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18710 }
18711
18712 if (auto_result && is_initialized && decl_specifiers->type
18713 && type_uses_auto (decl_specifiers->type))
18714 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18715
18716 return decl;
18717 }
18718
18719 /* Parse a declarator.
18720
18721 declarator:
18722 direct-declarator
18723 ptr-operator declarator
18724
18725 abstract-declarator:
18726 ptr-operator abstract-declarator [opt]
18727 direct-abstract-declarator
18728
18729 GNU Extensions:
18730
18731 declarator:
18732 attributes [opt] direct-declarator
18733 attributes [opt] ptr-operator declarator
18734
18735 abstract-declarator:
18736 attributes [opt] ptr-operator abstract-declarator [opt]
18737 attributes [opt] direct-abstract-declarator
18738
18739 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18740 detect constructor, destructor or conversion operators. It is set
18741 to -1 if the declarator is a name, and +1 if it is a
18742 function. Otherwise it is set to zero. Usually you just want to
18743 test for >0, but internally the negative value is used.
18744
18745 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18746 a decl-specifier-seq unless it declares a constructor, destructor,
18747 or conversion. It might seem that we could check this condition in
18748 semantic analysis, rather than parsing, but that makes it difficult
18749 to handle something like `f()'. We want to notice that there are
18750 no decl-specifiers, and therefore realize that this is an
18751 expression, not a declaration.)
18752
18753 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18754 the declarator is a direct-declarator of the form "(...)".
18755
18756 MEMBER_P is true iff this declarator is a member-declarator.
18757
18758 FRIEND_P is true iff this declarator is a friend. */
18759
18760 static cp_declarator *
18761 cp_parser_declarator (cp_parser* parser,
18762 cp_parser_declarator_kind dcl_kind,
18763 int* ctor_dtor_or_conv_p,
18764 bool* parenthesized_p,
18765 bool member_p, bool friend_p)
18766 {
18767 cp_declarator *declarator;
18768 enum tree_code code;
18769 cp_cv_quals cv_quals;
18770 tree class_type;
18771 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18772
18773 /* Assume this is not a constructor, destructor, or type-conversion
18774 operator. */
18775 if (ctor_dtor_or_conv_p)
18776 *ctor_dtor_or_conv_p = 0;
18777
18778 if (cp_parser_allow_gnu_extensions_p (parser))
18779 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18780
18781 /* Check for the ptr-operator production. */
18782 cp_parser_parse_tentatively (parser);
18783 /* Parse the ptr-operator. */
18784 code = cp_parser_ptr_operator (parser,
18785 &class_type,
18786 &cv_quals,
18787 &std_attributes);
18788
18789 /* If that worked, then we have a ptr-operator. */
18790 if (cp_parser_parse_definitely (parser))
18791 {
18792 /* If a ptr-operator was found, then this declarator was not
18793 parenthesized. */
18794 if (parenthesized_p)
18795 *parenthesized_p = true;
18796 /* The dependent declarator is optional if we are parsing an
18797 abstract-declarator. */
18798 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18799 cp_parser_parse_tentatively (parser);
18800
18801 /* Parse the dependent declarator. */
18802 declarator = cp_parser_declarator (parser, dcl_kind,
18803 /*ctor_dtor_or_conv_p=*/NULL,
18804 /*parenthesized_p=*/NULL,
18805 /*member_p=*/false,
18806 friend_p);
18807
18808 /* If we are parsing an abstract-declarator, we must handle the
18809 case where the dependent declarator is absent. */
18810 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18811 && !cp_parser_parse_definitely (parser))
18812 declarator = NULL;
18813
18814 declarator = cp_parser_make_indirect_declarator
18815 (code, class_type, cv_quals, declarator, std_attributes);
18816 }
18817 /* Everything else is a direct-declarator. */
18818 else
18819 {
18820 if (parenthesized_p)
18821 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18822 CPP_OPEN_PAREN);
18823 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18824 ctor_dtor_or_conv_p,
18825 member_p, friend_p);
18826 }
18827
18828 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18829 declarator->attributes = gnu_attributes;
18830 return declarator;
18831 }
18832
18833 /* Parse a direct-declarator or direct-abstract-declarator.
18834
18835 direct-declarator:
18836 declarator-id
18837 direct-declarator ( parameter-declaration-clause )
18838 cv-qualifier-seq [opt]
18839 ref-qualifier [opt]
18840 exception-specification [opt]
18841 direct-declarator [ constant-expression [opt] ]
18842 ( declarator )
18843
18844 direct-abstract-declarator:
18845 direct-abstract-declarator [opt]
18846 ( parameter-declaration-clause )
18847 cv-qualifier-seq [opt]
18848 ref-qualifier [opt]
18849 exception-specification [opt]
18850 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18851 ( abstract-declarator )
18852
18853 Returns a representation of the declarator. DCL_KIND is
18854 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18855 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18856 we are parsing a direct-declarator. It is
18857 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18858 of ambiguity we prefer an abstract declarator, as per
18859 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18860 as for cp_parser_declarator. */
18861
18862 static cp_declarator *
18863 cp_parser_direct_declarator (cp_parser* parser,
18864 cp_parser_declarator_kind dcl_kind,
18865 int* ctor_dtor_or_conv_p,
18866 bool member_p, bool friend_p)
18867 {
18868 cp_token *token;
18869 cp_declarator *declarator = NULL;
18870 tree scope = NULL_TREE;
18871 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18872 bool saved_in_declarator_p = parser->in_declarator_p;
18873 bool first = true;
18874 tree pushed_scope = NULL_TREE;
18875
18876 while (true)
18877 {
18878 /* Peek at the next token. */
18879 token = cp_lexer_peek_token (parser->lexer);
18880 if (token->type == CPP_OPEN_PAREN)
18881 {
18882 /* This is either a parameter-declaration-clause, or a
18883 parenthesized declarator. When we know we are parsing a
18884 named declarator, it must be a parenthesized declarator
18885 if FIRST is true. For instance, `(int)' is a
18886 parameter-declaration-clause, with an omitted
18887 direct-abstract-declarator. But `((*))', is a
18888 parenthesized abstract declarator. Finally, when T is a
18889 template parameter `(T)' is a
18890 parameter-declaration-clause, and not a parenthesized
18891 named declarator.
18892
18893 We first try and parse a parameter-declaration-clause,
18894 and then try a nested declarator (if FIRST is true).
18895
18896 It is not an error for it not to be a
18897 parameter-declaration-clause, even when FIRST is
18898 false. Consider,
18899
18900 int i (int);
18901 int i (3);
18902
18903 The first is the declaration of a function while the
18904 second is the definition of a variable, including its
18905 initializer.
18906
18907 Having seen only the parenthesis, we cannot know which of
18908 these two alternatives should be selected. Even more
18909 complex are examples like:
18910
18911 int i (int (a));
18912 int i (int (3));
18913
18914 The former is a function-declaration; the latter is a
18915 variable initialization.
18916
18917 Thus again, we try a parameter-declaration-clause, and if
18918 that fails, we back out and return. */
18919
18920 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18921 {
18922 tree params;
18923 bool is_declarator = false;
18924
18925 /* In a member-declarator, the only valid interpretation
18926 of a parenthesis is the start of a
18927 parameter-declaration-clause. (It is invalid to
18928 initialize a static data member with a parenthesized
18929 initializer; only the "=" form of initialization is
18930 permitted.) */
18931 if (!member_p)
18932 cp_parser_parse_tentatively (parser);
18933
18934 /* Consume the `('. */
18935 cp_lexer_consume_token (parser->lexer);
18936 if (first)
18937 {
18938 /* If this is going to be an abstract declarator, we're
18939 in a declarator and we can't have default args. */
18940 parser->default_arg_ok_p = false;
18941 parser->in_declarator_p = true;
18942 }
18943
18944 begin_scope (sk_function_parms, NULL_TREE);
18945
18946 /* Parse the parameter-declaration-clause. */
18947 params = cp_parser_parameter_declaration_clause (parser);
18948
18949 /* Consume the `)'. */
18950 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18951
18952 /* If all went well, parse the cv-qualifier-seq,
18953 ref-qualifier and the exception-specification. */
18954 if (member_p || cp_parser_parse_definitely (parser))
18955 {
18956 cp_cv_quals cv_quals;
18957 cp_virt_specifiers virt_specifiers;
18958 cp_ref_qualifier ref_qual;
18959 tree exception_specification;
18960 tree late_return;
18961 tree attrs;
18962 bool memfn = (member_p || (pushed_scope
18963 && CLASS_TYPE_P (pushed_scope)));
18964
18965 is_declarator = true;
18966
18967 if (ctor_dtor_or_conv_p)
18968 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18969 first = false;
18970
18971 /* Parse the cv-qualifier-seq. */
18972 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18973 /* Parse the ref-qualifier. */
18974 ref_qual = cp_parser_ref_qualifier_opt (parser);
18975 /* Parse the tx-qualifier. */
18976 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18977 /* And the exception-specification. */
18978 exception_specification
18979 = cp_parser_exception_specification_opt (parser);
18980
18981 attrs = cp_parser_std_attribute_spec_seq (parser);
18982
18983 /* In here, we handle cases where attribute is used after
18984 the function declaration. For example:
18985 void func (int x) __attribute__((vector(..))); */
18986 tree gnu_attrs = NULL_TREE;
18987 if (flag_cilkplus
18988 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18989 {
18990 cp_parser_parse_tentatively (parser);
18991 tree attr = cp_parser_gnu_attributes_opt (parser);
18992 if (cp_lexer_next_token_is_not (parser->lexer,
18993 CPP_SEMICOLON)
18994 && cp_lexer_next_token_is_not (parser->lexer,
18995 CPP_OPEN_BRACE))
18996 cp_parser_abort_tentative_parse (parser);
18997 else if (!cp_parser_parse_definitely (parser))
18998 ;
18999 else
19000 gnu_attrs = attr;
19001 }
19002 tree requires_clause = NULL_TREE;
19003 late_return = (cp_parser_late_return_type_opt
19004 (parser, declarator, requires_clause,
19005 memfn ? cv_quals : -1));
19006
19007 /* Parse the virt-specifier-seq. */
19008 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19009
19010 /* Create the function-declarator. */
19011 declarator = make_call_declarator (declarator,
19012 params,
19013 cv_quals,
19014 virt_specifiers,
19015 ref_qual,
19016 tx_qual,
19017 exception_specification,
19018 late_return,
19019 requires_clause);
19020 declarator->std_attributes = attrs;
19021 declarator->attributes = gnu_attrs;
19022 /* Any subsequent parameter lists are to do with
19023 return type, so are not those of the declared
19024 function. */
19025 parser->default_arg_ok_p = false;
19026 }
19027
19028 /* Remove the function parms from scope. */
19029 pop_bindings_and_leave_scope ();
19030
19031 if (is_declarator)
19032 /* Repeat the main loop. */
19033 continue;
19034 }
19035
19036 /* If this is the first, we can try a parenthesized
19037 declarator. */
19038 if (first)
19039 {
19040 bool saved_in_type_id_in_expr_p;
19041
19042 parser->default_arg_ok_p = saved_default_arg_ok_p;
19043 parser->in_declarator_p = saved_in_declarator_p;
19044
19045 /* Consume the `('. */
19046 cp_lexer_consume_token (parser->lexer);
19047 /* Parse the nested declarator. */
19048 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19049 parser->in_type_id_in_expr_p = true;
19050 declarator
19051 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19052 /*parenthesized_p=*/NULL,
19053 member_p, friend_p);
19054 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19055 first = false;
19056 /* Expect a `)'. */
19057 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19058 declarator = cp_error_declarator;
19059 if (declarator == cp_error_declarator)
19060 break;
19061
19062 goto handle_declarator;
19063 }
19064 /* Otherwise, we must be done. */
19065 else
19066 break;
19067 }
19068 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19069 && token->type == CPP_OPEN_SQUARE
19070 && !cp_next_tokens_can_be_attribute_p (parser))
19071 {
19072 /* Parse an array-declarator. */
19073 tree bounds, attrs;
19074
19075 if (ctor_dtor_or_conv_p)
19076 *ctor_dtor_or_conv_p = 0;
19077
19078 first = false;
19079 parser->default_arg_ok_p = false;
19080 parser->in_declarator_p = true;
19081 /* Consume the `['. */
19082 cp_lexer_consume_token (parser->lexer);
19083 /* Peek at the next token. */
19084 token = cp_lexer_peek_token (parser->lexer);
19085 /* If the next token is `]', then there is no
19086 constant-expression. */
19087 if (token->type != CPP_CLOSE_SQUARE)
19088 {
19089 bool non_constant_p;
19090 bounds
19091 = cp_parser_constant_expression (parser,
19092 /*allow_non_constant=*/true,
19093 &non_constant_p);
19094 if (!non_constant_p)
19095 /* OK */;
19096 else if (error_operand_p (bounds))
19097 /* Already gave an error. */;
19098 else if (!parser->in_function_body
19099 || current_binding_level->kind == sk_function_parms)
19100 {
19101 /* Normally, the array bound must be an integral constant
19102 expression. However, as an extension, we allow VLAs
19103 in function scopes as long as they aren't part of a
19104 parameter declaration. */
19105 cp_parser_error (parser,
19106 "array bound is not an integer constant");
19107 bounds = error_mark_node;
19108 }
19109 else if (processing_template_decl
19110 && !type_dependent_expression_p (bounds))
19111 {
19112 /* Remember this wasn't a constant-expression. */
19113 bounds = build_nop (TREE_TYPE (bounds), bounds);
19114 TREE_SIDE_EFFECTS (bounds) = 1;
19115 }
19116 }
19117 else
19118 bounds = NULL_TREE;
19119 /* Look for the closing `]'. */
19120 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19121 {
19122 declarator = cp_error_declarator;
19123 break;
19124 }
19125
19126 attrs = cp_parser_std_attribute_spec_seq (parser);
19127 declarator = make_array_declarator (declarator, bounds);
19128 declarator->std_attributes = attrs;
19129 }
19130 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19131 {
19132 {
19133 tree qualifying_scope;
19134 tree unqualified_name;
19135 tree attrs;
19136 special_function_kind sfk;
19137 bool abstract_ok;
19138 bool pack_expansion_p = false;
19139 cp_token *declarator_id_start_token;
19140
19141 /* Parse a declarator-id */
19142 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19143 if (abstract_ok)
19144 {
19145 cp_parser_parse_tentatively (parser);
19146
19147 /* If we see an ellipsis, we should be looking at a
19148 parameter pack. */
19149 if (token->type == CPP_ELLIPSIS)
19150 {
19151 /* Consume the `...' */
19152 cp_lexer_consume_token (parser->lexer);
19153
19154 pack_expansion_p = true;
19155 }
19156 }
19157
19158 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19159 unqualified_name
19160 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19161 qualifying_scope = parser->scope;
19162 if (abstract_ok)
19163 {
19164 bool okay = false;
19165
19166 if (!unqualified_name && pack_expansion_p)
19167 {
19168 /* Check whether an error occurred. */
19169 okay = !cp_parser_error_occurred (parser);
19170
19171 /* We already consumed the ellipsis to mark a
19172 parameter pack, but we have no way to report it,
19173 so abort the tentative parse. We will be exiting
19174 immediately anyway. */
19175 cp_parser_abort_tentative_parse (parser);
19176 }
19177 else
19178 okay = cp_parser_parse_definitely (parser);
19179
19180 if (!okay)
19181 unqualified_name = error_mark_node;
19182 else if (unqualified_name
19183 && (qualifying_scope
19184 || (!identifier_p (unqualified_name))))
19185 {
19186 cp_parser_error (parser, "expected unqualified-id");
19187 unqualified_name = error_mark_node;
19188 }
19189 }
19190
19191 if (!unqualified_name)
19192 return NULL;
19193 if (unqualified_name == error_mark_node)
19194 {
19195 declarator = cp_error_declarator;
19196 pack_expansion_p = false;
19197 declarator->parameter_pack_p = false;
19198 break;
19199 }
19200
19201 attrs = cp_parser_std_attribute_spec_seq (parser);
19202
19203 if (qualifying_scope && at_namespace_scope_p ()
19204 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19205 {
19206 /* In the declaration of a member of a template class
19207 outside of the class itself, the SCOPE will sometimes
19208 be a TYPENAME_TYPE. For example, given:
19209
19210 template <typename T>
19211 int S<T>::R::i = 3;
19212
19213 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19214 this context, we must resolve S<T>::R to an ordinary
19215 type, rather than a typename type.
19216
19217 The reason we normally avoid resolving TYPENAME_TYPEs
19218 is that a specialization of `S' might render
19219 `S<T>::R' not a type. However, if `S' is
19220 specialized, then this `i' will not be used, so there
19221 is no harm in resolving the types here. */
19222 tree type;
19223
19224 /* Resolve the TYPENAME_TYPE. */
19225 type = resolve_typename_type (qualifying_scope,
19226 /*only_current_p=*/false);
19227 /* If that failed, the declarator is invalid. */
19228 if (TREE_CODE (type) == TYPENAME_TYPE)
19229 {
19230 if (typedef_variant_p (type))
19231 error_at (declarator_id_start_token->location,
19232 "cannot define member of dependent typedef "
19233 "%qT", type);
19234 else
19235 error_at (declarator_id_start_token->location,
19236 "%<%T::%E%> is not a type",
19237 TYPE_CONTEXT (qualifying_scope),
19238 TYPE_IDENTIFIER (qualifying_scope));
19239 }
19240 qualifying_scope = type;
19241 }
19242
19243 sfk = sfk_none;
19244
19245 if (unqualified_name)
19246 {
19247 tree class_type;
19248
19249 if (qualifying_scope
19250 && CLASS_TYPE_P (qualifying_scope))
19251 class_type = qualifying_scope;
19252 else
19253 class_type = current_class_type;
19254
19255 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19256 {
19257 tree name_type = TREE_TYPE (unqualified_name);
19258 if (class_type && same_type_p (name_type, class_type))
19259 {
19260 if (qualifying_scope
19261 && CLASSTYPE_USE_TEMPLATE (name_type))
19262 {
19263 error_at (declarator_id_start_token->location,
19264 "invalid use of constructor as a template");
19265 inform (declarator_id_start_token->location,
19266 "use %<%T::%D%> instead of %<%T::%D%> to "
19267 "name the constructor in a qualified name",
19268 class_type,
19269 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19270 class_type, name_type);
19271 declarator = cp_error_declarator;
19272 break;
19273 }
19274 else
19275 unqualified_name = constructor_name (class_type);
19276 }
19277 else
19278 {
19279 /* We do not attempt to print the declarator
19280 here because we do not have enough
19281 information about its original syntactic
19282 form. */
19283 cp_parser_error (parser, "invalid declarator");
19284 declarator = cp_error_declarator;
19285 break;
19286 }
19287 }
19288
19289 if (class_type)
19290 {
19291 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19292 sfk = sfk_destructor;
19293 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19294 sfk = sfk_conversion;
19295 else if (/* There's no way to declare a constructor
19296 for an anonymous type, even if the type
19297 got a name for linkage purposes. */
19298 !TYPE_WAS_ANONYMOUS (class_type)
19299 /* Handle correctly (c++/19200):
19300
19301 struct S {
19302 struct T{};
19303 friend void S(T);
19304 };
19305
19306 and also:
19307
19308 namespace N {
19309 void S();
19310 }
19311
19312 struct S {
19313 friend void N::S();
19314 }; */
19315 && !(friend_p
19316 && class_type != qualifying_scope)
19317 && constructor_name_p (unqualified_name,
19318 class_type))
19319 {
19320 unqualified_name = constructor_name (class_type);
19321 sfk = sfk_constructor;
19322 }
19323 else if (is_overloaded_fn (unqualified_name)
19324 && DECL_CONSTRUCTOR_P (get_first_fn
19325 (unqualified_name)))
19326 sfk = sfk_constructor;
19327
19328 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19329 *ctor_dtor_or_conv_p = -1;
19330 }
19331 }
19332 declarator = make_id_declarator (qualifying_scope,
19333 unqualified_name,
19334 sfk);
19335 declarator->std_attributes = attrs;
19336 declarator->id_loc = token->location;
19337 declarator->parameter_pack_p = pack_expansion_p;
19338
19339 if (pack_expansion_p)
19340 maybe_warn_variadic_templates ();
19341 }
19342
19343 handle_declarator:;
19344 scope = get_scope_of_declarator (declarator);
19345 if (scope)
19346 {
19347 /* Any names that appear after the declarator-id for a
19348 member are looked up in the containing scope. */
19349 if (at_function_scope_p ())
19350 {
19351 /* But declarations with qualified-ids can't appear in a
19352 function. */
19353 cp_parser_error (parser, "qualified-id in declaration");
19354 declarator = cp_error_declarator;
19355 break;
19356 }
19357 pushed_scope = push_scope (scope);
19358 }
19359 parser->in_declarator_p = true;
19360 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19361 || (declarator && declarator->kind == cdk_id))
19362 /* Default args are only allowed on function
19363 declarations. */
19364 parser->default_arg_ok_p = saved_default_arg_ok_p;
19365 else
19366 parser->default_arg_ok_p = false;
19367
19368 first = false;
19369 }
19370 /* We're done. */
19371 else
19372 break;
19373 }
19374
19375 /* For an abstract declarator, we might wind up with nothing at this
19376 point. That's an error; the declarator is not optional. */
19377 if (!declarator)
19378 cp_parser_error (parser, "expected declarator");
19379
19380 /* If we entered a scope, we must exit it now. */
19381 if (pushed_scope)
19382 pop_scope (pushed_scope);
19383
19384 parser->default_arg_ok_p = saved_default_arg_ok_p;
19385 parser->in_declarator_p = saved_in_declarator_p;
19386
19387 return declarator;
19388 }
19389
19390 /* Parse a ptr-operator.
19391
19392 ptr-operator:
19393 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19394 * cv-qualifier-seq [opt]
19395 &
19396 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19397 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19398
19399 GNU Extension:
19400
19401 ptr-operator:
19402 & cv-qualifier-seq [opt]
19403
19404 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19405 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19406 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19407 filled in with the TYPE containing the member. *CV_QUALS is
19408 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19409 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19410 Note that the tree codes returned by this function have nothing
19411 to do with the types of trees that will be eventually be created
19412 to represent the pointer or reference type being parsed. They are
19413 just constants with suggestive names. */
19414 static enum tree_code
19415 cp_parser_ptr_operator (cp_parser* parser,
19416 tree* type,
19417 cp_cv_quals *cv_quals,
19418 tree *attributes)
19419 {
19420 enum tree_code code = ERROR_MARK;
19421 cp_token *token;
19422 tree attrs = NULL_TREE;
19423
19424 /* Assume that it's not a pointer-to-member. */
19425 *type = NULL_TREE;
19426 /* And that there are no cv-qualifiers. */
19427 *cv_quals = TYPE_UNQUALIFIED;
19428
19429 /* Peek at the next token. */
19430 token = cp_lexer_peek_token (parser->lexer);
19431
19432 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19433 if (token->type == CPP_MULT)
19434 code = INDIRECT_REF;
19435 else if (token->type == CPP_AND)
19436 code = ADDR_EXPR;
19437 else if ((cxx_dialect != cxx98) &&
19438 token->type == CPP_AND_AND) /* C++0x only */
19439 code = NON_LVALUE_EXPR;
19440
19441 if (code != ERROR_MARK)
19442 {
19443 /* Consume the `*', `&' or `&&'. */
19444 cp_lexer_consume_token (parser->lexer);
19445
19446 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19447 `&', if we are allowing GNU extensions. (The only qualifier
19448 that can legally appear after `&' is `restrict', but that is
19449 enforced during semantic analysis. */
19450 if (code == INDIRECT_REF
19451 || cp_parser_allow_gnu_extensions_p (parser))
19452 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19453
19454 attrs = cp_parser_std_attribute_spec_seq (parser);
19455 if (attributes != NULL)
19456 *attributes = attrs;
19457 }
19458 else
19459 {
19460 /* Try the pointer-to-member case. */
19461 cp_parser_parse_tentatively (parser);
19462 /* Look for the optional `::' operator. */
19463 cp_parser_global_scope_opt (parser,
19464 /*current_scope_valid_p=*/false);
19465 /* Look for the nested-name specifier. */
19466 token = cp_lexer_peek_token (parser->lexer);
19467 cp_parser_nested_name_specifier (parser,
19468 /*typename_keyword_p=*/false,
19469 /*check_dependency_p=*/true,
19470 /*type_p=*/false,
19471 /*is_declaration=*/false);
19472 /* If we found it, and the next token is a `*', then we are
19473 indeed looking at a pointer-to-member operator. */
19474 if (!cp_parser_error_occurred (parser)
19475 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19476 {
19477 /* Indicate that the `*' operator was used. */
19478 code = INDIRECT_REF;
19479
19480 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19481 error_at (token->location, "%qD is a namespace", parser->scope);
19482 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19483 error_at (token->location, "cannot form pointer to member of "
19484 "non-class %q#T", parser->scope);
19485 else
19486 {
19487 /* The type of which the member is a member is given by the
19488 current SCOPE. */
19489 *type = parser->scope;
19490 /* The next name will not be qualified. */
19491 parser->scope = NULL_TREE;
19492 parser->qualifying_scope = NULL_TREE;
19493 parser->object_scope = NULL_TREE;
19494 /* Look for optional c++11 attributes. */
19495 attrs = cp_parser_std_attribute_spec_seq (parser);
19496 if (attributes != NULL)
19497 *attributes = attrs;
19498 /* Look for the optional cv-qualifier-seq. */
19499 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19500 }
19501 }
19502 /* If that didn't work we don't have a ptr-operator. */
19503 if (!cp_parser_parse_definitely (parser))
19504 cp_parser_error (parser, "expected ptr-operator");
19505 }
19506
19507 return code;
19508 }
19509
19510 /* Parse an (optional) cv-qualifier-seq.
19511
19512 cv-qualifier-seq:
19513 cv-qualifier cv-qualifier-seq [opt]
19514
19515 cv-qualifier:
19516 const
19517 volatile
19518
19519 GNU Extension:
19520
19521 cv-qualifier:
19522 __restrict__
19523
19524 Returns a bitmask representing the cv-qualifiers. */
19525
19526 static cp_cv_quals
19527 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19528 {
19529 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19530
19531 while (true)
19532 {
19533 cp_token *token;
19534 cp_cv_quals cv_qualifier;
19535
19536 /* Peek at the next token. */
19537 token = cp_lexer_peek_token (parser->lexer);
19538 /* See if it's a cv-qualifier. */
19539 switch (token->keyword)
19540 {
19541 case RID_CONST:
19542 cv_qualifier = TYPE_QUAL_CONST;
19543 break;
19544
19545 case RID_VOLATILE:
19546 cv_qualifier = TYPE_QUAL_VOLATILE;
19547 break;
19548
19549 case RID_RESTRICT:
19550 cv_qualifier = TYPE_QUAL_RESTRICT;
19551 break;
19552
19553 default:
19554 cv_qualifier = TYPE_UNQUALIFIED;
19555 break;
19556 }
19557
19558 if (!cv_qualifier)
19559 break;
19560
19561 if (cv_quals & cv_qualifier)
19562 {
19563 error_at (token->location, "duplicate cv-qualifier");
19564 cp_lexer_purge_token (parser->lexer);
19565 }
19566 else
19567 {
19568 cp_lexer_consume_token (parser->lexer);
19569 cv_quals |= cv_qualifier;
19570 }
19571 }
19572
19573 return cv_quals;
19574 }
19575
19576 /* Parse an (optional) ref-qualifier
19577
19578 ref-qualifier:
19579 &
19580 &&
19581
19582 Returns cp_ref_qualifier representing ref-qualifier. */
19583
19584 static cp_ref_qualifier
19585 cp_parser_ref_qualifier_opt (cp_parser* parser)
19586 {
19587 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19588
19589 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19590 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19591 return ref_qual;
19592
19593 while (true)
19594 {
19595 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19596 cp_token *token = cp_lexer_peek_token (parser->lexer);
19597
19598 switch (token->type)
19599 {
19600 case CPP_AND:
19601 curr_ref_qual = REF_QUAL_LVALUE;
19602 break;
19603
19604 case CPP_AND_AND:
19605 curr_ref_qual = REF_QUAL_RVALUE;
19606 break;
19607
19608 default:
19609 curr_ref_qual = REF_QUAL_NONE;
19610 break;
19611 }
19612
19613 if (!curr_ref_qual)
19614 break;
19615 else if (ref_qual)
19616 {
19617 error_at (token->location, "multiple ref-qualifiers");
19618 cp_lexer_purge_token (parser->lexer);
19619 }
19620 else
19621 {
19622 ref_qual = curr_ref_qual;
19623 cp_lexer_consume_token (parser->lexer);
19624 }
19625 }
19626
19627 return ref_qual;
19628 }
19629
19630 /* Parse an optional tx-qualifier.
19631
19632 tx-qualifier:
19633 transaction_safe
19634 transaction_safe_dynamic */
19635
19636 static tree
19637 cp_parser_tx_qualifier_opt (cp_parser *parser)
19638 {
19639 cp_token *token = cp_lexer_peek_token (parser->lexer);
19640 if (token->type == CPP_NAME)
19641 {
19642 tree name = token->u.value;
19643 const char *p = IDENTIFIER_POINTER (name);
19644 const int len = strlen ("transaction_safe");
19645 if (!strncmp (p, "transaction_safe", len))
19646 {
19647 p += len;
19648 if (*p == '\0'
19649 || !strcmp (p, "_dynamic"))
19650 {
19651 cp_lexer_consume_token (parser->lexer);
19652 if (!flag_tm)
19653 {
19654 error ("%E requires %<-fgnu-tm%>", name);
19655 return NULL_TREE;
19656 }
19657 else
19658 return name;
19659 }
19660 }
19661 }
19662 return NULL_TREE;
19663 }
19664
19665 /* Parse an (optional) virt-specifier-seq.
19666
19667 virt-specifier-seq:
19668 virt-specifier virt-specifier-seq [opt]
19669
19670 virt-specifier:
19671 override
19672 final
19673
19674 Returns a bitmask representing the virt-specifiers. */
19675
19676 static cp_virt_specifiers
19677 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19678 {
19679 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19680
19681 while (true)
19682 {
19683 cp_token *token;
19684 cp_virt_specifiers virt_specifier;
19685
19686 /* Peek at the next token. */
19687 token = cp_lexer_peek_token (parser->lexer);
19688 /* See if it's a virt-specifier-qualifier. */
19689 if (token->type != CPP_NAME)
19690 break;
19691 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19692 {
19693 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19694 virt_specifier = VIRT_SPEC_OVERRIDE;
19695 }
19696 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19697 {
19698 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19699 virt_specifier = VIRT_SPEC_FINAL;
19700 }
19701 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19702 {
19703 virt_specifier = VIRT_SPEC_FINAL;
19704 }
19705 else
19706 break;
19707
19708 if (virt_specifiers & virt_specifier)
19709 {
19710 error_at (token->location, "duplicate virt-specifier");
19711 cp_lexer_purge_token (parser->lexer);
19712 }
19713 else
19714 {
19715 cp_lexer_consume_token (parser->lexer);
19716 virt_specifiers |= virt_specifier;
19717 }
19718 }
19719 return virt_specifiers;
19720 }
19721
19722 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19723 is in scope even though it isn't real. */
19724
19725 void
19726 inject_this_parameter (tree ctype, cp_cv_quals quals)
19727 {
19728 tree this_parm;
19729
19730 if (current_class_ptr)
19731 {
19732 /* We don't clear this between NSDMIs. Is it already what we want? */
19733 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19734 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19735 && cp_type_quals (type) == quals)
19736 return;
19737 }
19738
19739 this_parm = build_this_parm (ctype, quals);
19740 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19741 current_class_ptr = NULL_TREE;
19742 current_class_ref
19743 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19744 current_class_ptr = this_parm;
19745 }
19746
19747 /* Return true iff our current scope is a non-static data member
19748 initializer. */
19749
19750 bool
19751 parsing_nsdmi (void)
19752 {
19753 /* We recognize NSDMI context by the context-less 'this' pointer set up
19754 by the function above. */
19755 if (current_class_ptr
19756 && TREE_CODE (current_class_ptr) == PARM_DECL
19757 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19758 return true;
19759 return false;
19760 }
19761
19762 /* Parse a late-specified return type, if any. This is not a separate
19763 non-terminal, but part of a function declarator, which looks like
19764
19765 -> trailing-type-specifier-seq abstract-declarator(opt)
19766
19767 Returns the type indicated by the type-id.
19768
19769 In addition to this, parse any queued up omp declare simd
19770 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19771
19772 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19773 function. */
19774
19775 static tree
19776 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19777 tree& requires_clause, cp_cv_quals quals)
19778 {
19779 cp_token *token;
19780 tree type = NULL_TREE;
19781 bool declare_simd_p = (parser->omp_declare_simd
19782 && declarator
19783 && declarator->kind == cdk_id);
19784
19785 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19786 && declarator && declarator->kind == cdk_id);
19787
19788 bool oacc_routine_p = (parser->oacc_routine
19789 && declarator
19790 && declarator->kind == cdk_id);
19791
19792 /* Peek at the next token. */
19793 token = cp_lexer_peek_token (parser->lexer);
19794 /* A late-specified return type is indicated by an initial '->'. */
19795 if (token->type != CPP_DEREF
19796 && token->keyword != RID_REQUIRES
19797 && !(token->type == CPP_NAME
19798 && token->u.value == ridpointers[RID_REQUIRES])
19799 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19800 return NULL_TREE;
19801
19802 tree save_ccp = current_class_ptr;
19803 tree save_ccr = current_class_ref;
19804 if (quals >= 0)
19805 {
19806 /* DR 1207: 'this' is in scope in the trailing return type. */
19807 inject_this_parameter (current_class_type, quals);
19808 }
19809
19810 if (token->type == CPP_DEREF)
19811 {
19812 /* Consume the ->. */
19813 cp_lexer_consume_token (parser->lexer);
19814
19815 type = cp_parser_trailing_type_id (parser);
19816 }
19817
19818 /* Function declarations may be followed by a trailing
19819 requires-clause. */
19820 requires_clause = cp_parser_requires_clause_opt (parser);
19821
19822 if (cilk_simd_fn_vector_p)
19823 declarator->attributes
19824 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19825 declarator->attributes);
19826 if (declare_simd_p)
19827 declarator->attributes
19828 = cp_parser_late_parsing_omp_declare_simd (parser,
19829 declarator->attributes);
19830 if (oacc_routine_p)
19831 declarator->attributes
19832 = cp_parser_late_parsing_oacc_routine (parser,
19833 declarator->attributes);
19834
19835 if (quals >= 0)
19836 {
19837 current_class_ptr = save_ccp;
19838 current_class_ref = save_ccr;
19839 }
19840
19841 return type;
19842 }
19843
19844 /* Parse a declarator-id.
19845
19846 declarator-id:
19847 id-expression
19848 :: [opt] nested-name-specifier [opt] type-name
19849
19850 In the `id-expression' case, the value returned is as for
19851 cp_parser_id_expression if the id-expression was an unqualified-id.
19852 If the id-expression was a qualified-id, then a SCOPE_REF is
19853 returned. The first operand is the scope (either a NAMESPACE_DECL
19854 or TREE_TYPE), but the second is still just a representation of an
19855 unqualified-id. */
19856
19857 static tree
19858 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19859 {
19860 tree id;
19861 /* The expression must be an id-expression. Assume that qualified
19862 names are the names of types so that:
19863
19864 template <class T>
19865 int S<T>::R::i = 3;
19866
19867 will work; we must treat `S<T>::R' as the name of a type.
19868 Similarly, assume that qualified names are templates, where
19869 required, so that:
19870
19871 template <class T>
19872 int S<T>::R<T>::i = 3;
19873
19874 will work, too. */
19875 id = cp_parser_id_expression (parser,
19876 /*template_keyword_p=*/false,
19877 /*check_dependency_p=*/false,
19878 /*template_p=*/NULL,
19879 /*declarator_p=*/true,
19880 optional_p);
19881 if (id && BASELINK_P (id))
19882 id = BASELINK_FUNCTIONS (id);
19883 return id;
19884 }
19885
19886 /* Parse a type-id.
19887
19888 type-id:
19889 type-specifier-seq abstract-declarator [opt]
19890
19891 Returns the TYPE specified. */
19892
19893 static tree
19894 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19895 bool is_trailing_return)
19896 {
19897 cp_decl_specifier_seq type_specifier_seq;
19898 cp_declarator *abstract_declarator;
19899
19900 /* Parse the type-specifier-seq. */
19901 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19902 is_trailing_return,
19903 &type_specifier_seq);
19904 if (type_specifier_seq.type == error_mark_node)
19905 return error_mark_node;
19906
19907 /* There might or might not be an abstract declarator. */
19908 cp_parser_parse_tentatively (parser);
19909 /* Look for the declarator. */
19910 abstract_declarator
19911 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19912 /*parenthesized_p=*/NULL,
19913 /*member_p=*/false,
19914 /*friend_p=*/false);
19915 /* Check to see if there really was a declarator. */
19916 if (!cp_parser_parse_definitely (parser))
19917 abstract_declarator = NULL;
19918
19919 if (type_specifier_seq.type
19920 /* The concepts TS allows 'auto' as a type-id. */
19921 && (!flag_concepts || parser->in_type_id_in_expr_p)
19922 /* None of the valid uses of 'auto' in C++14 involve the type-id
19923 nonterminal, but it is valid in a trailing-return-type. */
19924 && !(cxx_dialect >= cxx14 && is_trailing_return)
19925 && type_uses_auto (type_specifier_seq.type))
19926 {
19927 /* A type-id with type 'auto' is only ok if the abstract declarator
19928 is a function declarator with a late-specified return type.
19929
19930 A type-id with 'auto' is also valid in a trailing-return-type
19931 in a compound-requirement. */
19932 if (abstract_declarator
19933 && abstract_declarator->kind == cdk_function
19934 && abstract_declarator->u.function.late_return_type)
19935 /* OK */;
19936 else if (parser->in_result_type_constraint_p)
19937 /* OK */;
19938 else
19939 {
19940 error ("invalid use of %<auto%>");
19941 return error_mark_node;
19942 }
19943 }
19944
19945 return groktypename (&type_specifier_seq, abstract_declarator,
19946 is_template_arg);
19947 }
19948
19949 static tree
19950 cp_parser_type_id (cp_parser *parser)
19951 {
19952 return cp_parser_type_id_1 (parser, false, false);
19953 }
19954
19955 static tree
19956 cp_parser_template_type_arg (cp_parser *parser)
19957 {
19958 tree r;
19959 const char *saved_message = parser->type_definition_forbidden_message;
19960 parser->type_definition_forbidden_message
19961 = G_("types may not be defined in template arguments");
19962 r = cp_parser_type_id_1 (parser, true, false);
19963 parser->type_definition_forbidden_message = saved_message;
19964 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19965 {
19966 error ("invalid use of %<auto%> in template argument");
19967 r = error_mark_node;
19968 }
19969 return r;
19970 }
19971
19972 static tree
19973 cp_parser_trailing_type_id (cp_parser *parser)
19974 {
19975 return cp_parser_type_id_1 (parser, false, true);
19976 }
19977
19978 /* Parse a type-specifier-seq.
19979
19980 type-specifier-seq:
19981 type-specifier type-specifier-seq [opt]
19982
19983 GNU extension:
19984
19985 type-specifier-seq:
19986 attributes type-specifier-seq [opt]
19987
19988 If IS_DECLARATION is true, we are at the start of a "condition" or
19989 exception-declaration, so we might be followed by a declarator-id.
19990
19991 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19992 i.e. we've just seen "->".
19993
19994 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
19995
19996 static void
19997 cp_parser_type_specifier_seq (cp_parser* parser,
19998 bool is_declaration,
19999 bool is_trailing_return,
20000 cp_decl_specifier_seq *type_specifier_seq)
20001 {
20002 bool seen_type_specifier = false;
20003 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20004 cp_token *start_token = NULL;
20005
20006 /* Clear the TYPE_SPECIFIER_SEQ. */
20007 clear_decl_specs (type_specifier_seq);
20008
20009 /* In the context of a trailing return type, enum E { } is an
20010 elaborated-type-specifier followed by a function-body, not an
20011 enum-specifier. */
20012 if (is_trailing_return)
20013 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20014
20015 /* Parse the type-specifiers and attributes. */
20016 while (true)
20017 {
20018 tree type_specifier;
20019 bool is_cv_qualifier;
20020
20021 /* Check for attributes first. */
20022 if (cp_next_tokens_can_be_attribute_p (parser))
20023 {
20024 type_specifier_seq->attributes =
20025 chainon (type_specifier_seq->attributes,
20026 cp_parser_attributes_opt (parser));
20027 continue;
20028 }
20029
20030 /* record the token of the beginning of the type specifier seq,
20031 for error reporting purposes*/
20032 if (!start_token)
20033 start_token = cp_lexer_peek_token (parser->lexer);
20034
20035 /* Look for the type-specifier. */
20036 type_specifier = cp_parser_type_specifier (parser,
20037 flags,
20038 type_specifier_seq,
20039 /*is_declaration=*/false,
20040 NULL,
20041 &is_cv_qualifier);
20042 if (!type_specifier)
20043 {
20044 /* If the first type-specifier could not be found, this is not a
20045 type-specifier-seq at all. */
20046 if (!seen_type_specifier)
20047 {
20048 /* Set in_declarator_p to avoid skipping to the semicolon. */
20049 int in_decl = parser->in_declarator_p;
20050 parser->in_declarator_p = true;
20051
20052 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20053 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20054 cp_parser_error (parser, "expected type-specifier");
20055
20056 parser->in_declarator_p = in_decl;
20057
20058 type_specifier_seq->type = error_mark_node;
20059 return;
20060 }
20061 /* If subsequent type-specifiers could not be found, the
20062 type-specifier-seq is complete. */
20063 break;
20064 }
20065
20066 seen_type_specifier = true;
20067 /* The standard says that a condition can be:
20068
20069 type-specifier-seq declarator = assignment-expression
20070
20071 However, given:
20072
20073 struct S {};
20074 if (int S = ...)
20075
20076 we should treat the "S" as a declarator, not as a
20077 type-specifier. The standard doesn't say that explicitly for
20078 type-specifier-seq, but it does say that for
20079 decl-specifier-seq in an ordinary declaration. Perhaps it
20080 would be clearer just to allow a decl-specifier-seq here, and
20081 then add a semantic restriction that if any decl-specifiers
20082 that are not type-specifiers appear, the program is invalid. */
20083 if (is_declaration && !is_cv_qualifier)
20084 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20085 }
20086 }
20087
20088 /* Return whether the function currently being declared has an associated
20089 template parameter list. */
20090
20091 static bool
20092 function_being_declared_is_template_p (cp_parser* parser)
20093 {
20094 if (!current_template_parms || processing_template_parmlist)
20095 return false;
20096
20097 if (parser->implicit_template_scope)
20098 return true;
20099
20100 if (at_class_scope_p ()
20101 && TYPE_BEING_DEFINED (current_class_type))
20102 return parser->num_template_parameter_lists != 0;
20103
20104 return ((int) parser->num_template_parameter_lists > template_class_depth
20105 (current_class_type));
20106 }
20107
20108 /* Parse a parameter-declaration-clause.
20109
20110 parameter-declaration-clause:
20111 parameter-declaration-list [opt] ... [opt]
20112 parameter-declaration-list , ...
20113
20114 Returns a representation for the parameter declarations. A return
20115 value of NULL indicates a parameter-declaration-clause consisting
20116 only of an ellipsis. */
20117
20118 static tree
20119 cp_parser_parameter_declaration_clause (cp_parser* parser)
20120 {
20121 tree parameters;
20122 cp_token *token;
20123 bool ellipsis_p;
20124 bool is_error;
20125
20126 struct cleanup {
20127 cp_parser* parser;
20128 int auto_is_implicit_function_template_parm_p;
20129 ~cleanup() {
20130 parser->auto_is_implicit_function_template_parm_p
20131 = auto_is_implicit_function_template_parm_p;
20132 }
20133 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20134
20135 (void) cleanup;
20136
20137 if (!processing_specialization
20138 && !processing_template_parmlist
20139 && !processing_explicit_instantiation)
20140 if (!current_function_decl
20141 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20142 parser->auto_is_implicit_function_template_parm_p = true;
20143
20144 /* Peek at the next token. */
20145 token = cp_lexer_peek_token (parser->lexer);
20146 /* Check for trivial parameter-declaration-clauses. */
20147 if (token->type == CPP_ELLIPSIS)
20148 {
20149 /* Consume the `...' token. */
20150 cp_lexer_consume_token (parser->lexer);
20151 return NULL_TREE;
20152 }
20153 else if (token->type == CPP_CLOSE_PAREN)
20154 /* There are no parameters. */
20155 {
20156 #ifndef NO_IMPLICIT_EXTERN_C
20157 if (in_system_header_at (input_location)
20158 && current_class_type == NULL
20159 && current_lang_name == lang_name_c)
20160 return NULL_TREE;
20161 else
20162 #endif
20163 return void_list_node;
20164 }
20165 /* Check for `(void)', too, which is a special case. */
20166 else if (token->keyword == RID_VOID
20167 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20168 == CPP_CLOSE_PAREN))
20169 {
20170 /* Consume the `void' token. */
20171 cp_lexer_consume_token (parser->lexer);
20172 /* There are no parameters. */
20173 return void_list_node;
20174 }
20175
20176 /* Parse the parameter-declaration-list. */
20177 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20178 /* If a parse error occurred while parsing the
20179 parameter-declaration-list, then the entire
20180 parameter-declaration-clause is erroneous. */
20181 if (is_error)
20182 return NULL;
20183
20184 /* Peek at the next token. */
20185 token = cp_lexer_peek_token (parser->lexer);
20186 /* If it's a `,', the clause should terminate with an ellipsis. */
20187 if (token->type == CPP_COMMA)
20188 {
20189 /* Consume the `,'. */
20190 cp_lexer_consume_token (parser->lexer);
20191 /* Expect an ellipsis. */
20192 ellipsis_p
20193 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20194 }
20195 /* It might also be `...' if the optional trailing `,' was
20196 omitted. */
20197 else if (token->type == CPP_ELLIPSIS)
20198 {
20199 /* Consume the `...' token. */
20200 cp_lexer_consume_token (parser->lexer);
20201 /* And remember that we saw it. */
20202 ellipsis_p = true;
20203 }
20204 else
20205 ellipsis_p = false;
20206
20207 /* Finish the parameter list. */
20208 if (!ellipsis_p)
20209 parameters = chainon (parameters, void_list_node);
20210
20211 return parameters;
20212 }
20213
20214 /* Parse a parameter-declaration-list.
20215
20216 parameter-declaration-list:
20217 parameter-declaration
20218 parameter-declaration-list , parameter-declaration
20219
20220 Returns a representation of the parameter-declaration-list, as for
20221 cp_parser_parameter_declaration_clause. However, the
20222 `void_list_node' is never appended to the list. Upon return,
20223 *IS_ERROR will be true iff an error occurred. */
20224
20225 static tree
20226 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20227 {
20228 tree parameters = NULL_TREE;
20229 tree *tail = &parameters;
20230 bool saved_in_unbraced_linkage_specification_p;
20231 int index = 0;
20232
20233 /* Assume all will go well. */
20234 *is_error = false;
20235 /* The special considerations that apply to a function within an
20236 unbraced linkage specifications do not apply to the parameters
20237 to the function. */
20238 saved_in_unbraced_linkage_specification_p
20239 = parser->in_unbraced_linkage_specification_p;
20240 parser->in_unbraced_linkage_specification_p = false;
20241
20242 /* Look for more parameters. */
20243 while (true)
20244 {
20245 cp_parameter_declarator *parameter;
20246 tree decl = error_mark_node;
20247 bool parenthesized_p = false;
20248 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20249 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20250 (current_template_parms)) : 0);
20251
20252 /* Parse the parameter. */
20253 parameter
20254 = cp_parser_parameter_declaration (parser,
20255 /*template_parm_p=*/false,
20256 &parenthesized_p);
20257
20258 /* We don't know yet if the enclosing context is deprecated, so wait
20259 and warn in grokparms if appropriate. */
20260 deprecated_state = DEPRECATED_SUPPRESS;
20261
20262 if (parameter)
20263 {
20264 /* If a function parameter pack was specified and an implicit template
20265 parameter was introduced during cp_parser_parameter_declaration,
20266 change any implicit parameters introduced into packs. */
20267 if (parser->implicit_template_parms
20268 && parameter->declarator
20269 && parameter->declarator->parameter_pack_p)
20270 {
20271 int latest_template_parm_idx = TREE_VEC_LENGTH
20272 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20273
20274 if (latest_template_parm_idx != template_parm_idx)
20275 parameter->decl_specifiers.type = convert_generic_types_to_packs
20276 (parameter->decl_specifiers.type,
20277 template_parm_idx, latest_template_parm_idx);
20278 }
20279
20280 decl = grokdeclarator (parameter->declarator,
20281 &parameter->decl_specifiers,
20282 PARM,
20283 parameter->default_argument != NULL_TREE,
20284 &parameter->decl_specifiers.attributes);
20285 }
20286
20287 deprecated_state = DEPRECATED_NORMAL;
20288
20289 /* If a parse error occurred parsing the parameter declaration,
20290 then the entire parameter-declaration-list is erroneous. */
20291 if (decl == error_mark_node)
20292 {
20293 *is_error = true;
20294 parameters = error_mark_node;
20295 break;
20296 }
20297
20298 if (parameter->decl_specifiers.attributes)
20299 cplus_decl_attributes (&decl,
20300 parameter->decl_specifiers.attributes,
20301 0);
20302 if (DECL_NAME (decl))
20303 decl = pushdecl (decl);
20304
20305 if (decl != error_mark_node)
20306 {
20307 retrofit_lang_decl (decl);
20308 DECL_PARM_INDEX (decl) = ++index;
20309 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20310 }
20311
20312 /* Add the new parameter to the list. */
20313 *tail = build_tree_list (parameter->default_argument, decl);
20314 tail = &TREE_CHAIN (*tail);
20315
20316 /* Peek at the next token. */
20317 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20318 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20319 /* These are for Objective-C++ */
20320 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20321 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20322 /* The parameter-declaration-list is complete. */
20323 break;
20324 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20325 {
20326 cp_token *token;
20327
20328 /* Peek at the next token. */
20329 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20330 /* If it's an ellipsis, then the list is complete. */
20331 if (token->type == CPP_ELLIPSIS)
20332 break;
20333 /* Otherwise, there must be more parameters. Consume the
20334 `,'. */
20335 cp_lexer_consume_token (parser->lexer);
20336 /* When parsing something like:
20337
20338 int i(float f, double d)
20339
20340 we can tell after seeing the declaration for "f" that we
20341 are not looking at an initialization of a variable "i",
20342 but rather at the declaration of a function "i".
20343
20344 Due to the fact that the parsing of template arguments
20345 (as specified to a template-id) requires backtracking we
20346 cannot use this technique when inside a template argument
20347 list. */
20348 if (!parser->in_template_argument_list_p
20349 && !parser->in_type_id_in_expr_p
20350 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20351 /* However, a parameter-declaration of the form
20352 "float(f)" (which is a valid declaration of a
20353 parameter "f") can also be interpreted as an
20354 expression (the conversion of "f" to "float"). */
20355 && !parenthesized_p)
20356 cp_parser_commit_to_tentative_parse (parser);
20357 }
20358 else
20359 {
20360 cp_parser_error (parser, "expected %<,%> or %<...%>");
20361 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20362 cp_parser_skip_to_closing_parenthesis (parser,
20363 /*recovering=*/true,
20364 /*or_comma=*/false,
20365 /*consume_paren=*/false);
20366 break;
20367 }
20368 }
20369
20370 parser->in_unbraced_linkage_specification_p
20371 = saved_in_unbraced_linkage_specification_p;
20372
20373 /* Reset implicit_template_scope if we are about to leave the function
20374 parameter list that introduced it. Note that for out-of-line member
20375 definitions, there will be one or more class scopes before we get to
20376 the template parameter scope. */
20377
20378 if (cp_binding_level *its = parser->implicit_template_scope)
20379 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20380 {
20381 while (maybe_its->kind == sk_class)
20382 maybe_its = maybe_its->level_chain;
20383 if (maybe_its == its)
20384 {
20385 parser->implicit_template_parms = 0;
20386 parser->implicit_template_scope = 0;
20387 }
20388 }
20389
20390 return parameters;
20391 }
20392
20393 /* Parse a parameter declaration.
20394
20395 parameter-declaration:
20396 decl-specifier-seq ... [opt] declarator
20397 decl-specifier-seq declarator = assignment-expression
20398 decl-specifier-seq ... [opt] abstract-declarator [opt]
20399 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20400
20401 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20402 declares a template parameter. (In that case, a non-nested `>'
20403 token encountered during the parsing of the assignment-expression
20404 is not interpreted as a greater-than operator.)
20405
20406 Returns a representation of the parameter, or NULL if an error
20407 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20408 true iff the declarator is of the form "(p)". */
20409
20410 static cp_parameter_declarator *
20411 cp_parser_parameter_declaration (cp_parser *parser,
20412 bool template_parm_p,
20413 bool *parenthesized_p)
20414 {
20415 int declares_class_or_enum;
20416 cp_decl_specifier_seq decl_specifiers;
20417 cp_declarator *declarator;
20418 tree default_argument;
20419 cp_token *token = NULL, *declarator_token_start = NULL;
20420 const char *saved_message;
20421 bool template_parameter_pack_p = false;
20422
20423 /* In a template parameter, `>' is not an operator.
20424
20425 [temp.param]
20426
20427 When parsing a default template-argument for a non-type
20428 template-parameter, the first non-nested `>' is taken as the end
20429 of the template parameter-list rather than a greater-than
20430 operator. */
20431
20432 /* Type definitions may not appear in parameter types. */
20433 saved_message = parser->type_definition_forbidden_message;
20434 parser->type_definition_forbidden_message
20435 = G_("types may not be defined in parameter types");
20436
20437 /* Parse the declaration-specifiers. */
20438 cp_parser_decl_specifier_seq (parser,
20439 CP_PARSER_FLAGS_NONE,
20440 &decl_specifiers,
20441 &declares_class_or_enum);
20442
20443 /* Complain about missing 'typename' or other invalid type names. */
20444 if (!decl_specifiers.any_type_specifiers_p
20445 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20446 decl_specifiers.type = error_mark_node;
20447
20448 /* If an error occurred, there's no reason to attempt to parse the
20449 rest of the declaration. */
20450 if (cp_parser_error_occurred (parser))
20451 {
20452 parser->type_definition_forbidden_message = saved_message;
20453 return NULL;
20454 }
20455
20456 /* Peek at the next token. */
20457 token = cp_lexer_peek_token (parser->lexer);
20458
20459 /* If the next token is a `)', `,', `=', `>', or `...', then there
20460 is no declarator. However, when variadic templates are enabled,
20461 there may be a declarator following `...'. */
20462 if (token->type == CPP_CLOSE_PAREN
20463 || token->type == CPP_COMMA
20464 || token->type == CPP_EQ
20465 || token->type == CPP_GREATER)
20466 {
20467 declarator = NULL;
20468 if (parenthesized_p)
20469 *parenthesized_p = false;
20470 }
20471 /* Otherwise, there should be a declarator. */
20472 else
20473 {
20474 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20475 parser->default_arg_ok_p = false;
20476
20477 /* After seeing a decl-specifier-seq, if the next token is not a
20478 "(", there is no possibility that the code is a valid
20479 expression. Therefore, if parsing tentatively, we commit at
20480 this point. */
20481 if (!parser->in_template_argument_list_p
20482 /* In an expression context, having seen:
20483
20484 (int((char ...
20485
20486 we cannot be sure whether we are looking at a
20487 function-type (taking a "char" as a parameter) or a cast
20488 of some object of type "char" to "int". */
20489 && !parser->in_type_id_in_expr_p
20490 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20491 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20492 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20493 cp_parser_commit_to_tentative_parse (parser);
20494 /* Parse the declarator. */
20495 declarator_token_start = token;
20496 declarator = cp_parser_declarator (parser,
20497 CP_PARSER_DECLARATOR_EITHER,
20498 /*ctor_dtor_or_conv_p=*/NULL,
20499 parenthesized_p,
20500 /*member_p=*/false,
20501 /*friend_p=*/false);
20502 parser->default_arg_ok_p = saved_default_arg_ok_p;
20503 /* After the declarator, allow more attributes. */
20504 decl_specifiers.attributes
20505 = chainon (decl_specifiers.attributes,
20506 cp_parser_attributes_opt (parser));
20507
20508 /* If the declarator is a template parameter pack, remember that and
20509 clear the flag in the declarator itself so we don't get errors
20510 from grokdeclarator. */
20511 if (template_parm_p && declarator && declarator->parameter_pack_p)
20512 {
20513 declarator->parameter_pack_p = false;
20514 template_parameter_pack_p = true;
20515 }
20516 }
20517
20518 /* If the next token is an ellipsis, and we have not seen a declarator
20519 name, and if either the type of the declarator contains parameter
20520 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20521 for, eg, abbreviated integral type names), then we actually have a
20522 parameter pack expansion expression. Otherwise, leave the ellipsis
20523 for a C-style variadic function. */
20524 token = cp_lexer_peek_token (parser->lexer);
20525 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20526 {
20527 tree type = decl_specifiers.type;
20528
20529 if (type && DECL_P (type))
20530 type = TREE_TYPE (type);
20531
20532 if (((type
20533 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20534 && (template_parm_p || uses_parameter_packs (type)))
20535 || (!type && template_parm_p))
20536 && declarator_can_be_parameter_pack (declarator))
20537 {
20538 /* Consume the `...'. */
20539 cp_lexer_consume_token (parser->lexer);
20540 maybe_warn_variadic_templates ();
20541
20542 /* Build a pack expansion type */
20543 if (template_parm_p)
20544 template_parameter_pack_p = true;
20545 else if (declarator)
20546 declarator->parameter_pack_p = true;
20547 else
20548 decl_specifiers.type = make_pack_expansion (type);
20549 }
20550 }
20551
20552 /* The restriction on defining new types applies only to the type
20553 of the parameter, not to the default argument. */
20554 parser->type_definition_forbidden_message = saved_message;
20555
20556 /* If the next token is `=', then process a default argument. */
20557 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20558 {
20559 tree type = decl_specifiers.type;
20560 token = cp_lexer_peek_token (parser->lexer);
20561 /* If we are defining a class, then the tokens that make up the
20562 default argument must be saved and processed later. */
20563 if (!template_parm_p && at_class_scope_p ()
20564 && TYPE_BEING_DEFINED (current_class_type)
20565 && !LAMBDA_TYPE_P (current_class_type))
20566 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20567
20568 // A constrained-type-specifier may declare a type template-parameter.
20569 else if (declares_constrained_type_template_parameter (type))
20570 default_argument
20571 = cp_parser_default_type_template_argument (parser);
20572
20573 // A constrained-type-specifier may declare a template-template-parameter.
20574 else if (declares_constrained_template_template_parameter (type))
20575 default_argument
20576 = cp_parser_default_template_template_argument (parser);
20577
20578 /* Outside of a class definition, we can just parse the
20579 assignment-expression. */
20580 else
20581 default_argument
20582 = cp_parser_default_argument (parser, template_parm_p);
20583
20584 if (!parser->default_arg_ok_p)
20585 {
20586 permerror (token->location,
20587 "default arguments are only "
20588 "permitted for function parameters");
20589 }
20590 else if ((declarator && declarator->parameter_pack_p)
20591 || template_parameter_pack_p
20592 || (decl_specifiers.type
20593 && PACK_EXPANSION_P (decl_specifiers.type)))
20594 {
20595 /* Find the name of the parameter pack. */
20596 cp_declarator *id_declarator = declarator;
20597 while (id_declarator && id_declarator->kind != cdk_id)
20598 id_declarator = id_declarator->declarator;
20599
20600 if (id_declarator && id_declarator->kind == cdk_id)
20601 error_at (declarator_token_start->location,
20602 template_parm_p
20603 ? G_("template parameter pack %qD "
20604 "cannot have a default argument")
20605 : G_("parameter pack %qD cannot have "
20606 "a default argument"),
20607 id_declarator->u.id.unqualified_name);
20608 else
20609 error_at (declarator_token_start->location,
20610 template_parm_p
20611 ? G_("template parameter pack cannot have "
20612 "a default argument")
20613 : G_("parameter pack cannot have a "
20614 "default argument"));
20615
20616 default_argument = NULL_TREE;
20617 }
20618 }
20619 else
20620 default_argument = NULL_TREE;
20621
20622 return make_parameter_declarator (&decl_specifiers,
20623 declarator,
20624 default_argument,
20625 template_parameter_pack_p);
20626 }
20627
20628 /* Parse a default argument and return it.
20629
20630 TEMPLATE_PARM_P is true if this is a default argument for a
20631 non-type template parameter. */
20632 static tree
20633 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20634 {
20635 tree default_argument = NULL_TREE;
20636 bool saved_greater_than_is_operator_p;
20637 bool saved_local_variables_forbidden_p;
20638 bool non_constant_p, is_direct_init;
20639
20640 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20641 set correctly. */
20642 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20643 parser->greater_than_is_operator_p = !template_parm_p;
20644 /* Local variable names (and the `this' keyword) may not
20645 appear in a default argument. */
20646 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20647 parser->local_variables_forbidden_p = true;
20648 /* Parse the assignment-expression. */
20649 if (template_parm_p)
20650 push_deferring_access_checks (dk_no_deferred);
20651 tree saved_class_ptr = NULL_TREE;
20652 tree saved_class_ref = NULL_TREE;
20653 /* The "this" pointer is not valid in a default argument. */
20654 if (cfun)
20655 {
20656 saved_class_ptr = current_class_ptr;
20657 cp_function_chain->x_current_class_ptr = NULL_TREE;
20658 saved_class_ref = current_class_ref;
20659 cp_function_chain->x_current_class_ref = NULL_TREE;
20660 }
20661 default_argument
20662 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20663 /* Restore the "this" pointer. */
20664 if (cfun)
20665 {
20666 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20667 cp_function_chain->x_current_class_ref = saved_class_ref;
20668 }
20669 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20670 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20671 if (template_parm_p)
20672 pop_deferring_access_checks ();
20673 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20674 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20675
20676 return default_argument;
20677 }
20678
20679 /* Parse a function-body.
20680
20681 function-body:
20682 compound_statement */
20683
20684 static void
20685 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20686 {
20687 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20688 ? BCS_TRY_BLOCK : BCS_NORMAL),
20689 true);
20690 }
20691
20692 /* Parse a ctor-initializer-opt followed by a function-body. Return
20693 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20694 is true we are parsing a function-try-block. */
20695
20696 static bool
20697 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20698 bool in_function_try_block)
20699 {
20700 tree body, list;
20701 bool ctor_initializer_p;
20702 const bool check_body_p =
20703 DECL_CONSTRUCTOR_P (current_function_decl)
20704 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20705 tree last = NULL;
20706
20707 /* Begin the function body. */
20708 body = begin_function_body ();
20709 /* Parse the optional ctor-initializer. */
20710 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20711
20712 /* If we're parsing a constexpr constructor definition, we need
20713 to check that the constructor body is indeed empty. However,
20714 before we get to cp_parser_function_body lot of junk has been
20715 generated, so we can't just check that we have an empty block.
20716 Rather we take a snapshot of the outermost block, and check whether
20717 cp_parser_function_body changed its state. */
20718 if (check_body_p)
20719 {
20720 list = cur_stmt_list;
20721 if (STATEMENT_LIST_TAIL (list))
20722 last = STATEMENT_LIST_TAIL (list)->stmt;
20723 }
20724 /* Parse the function-body. */
20725 cp_parser_function_body (parser, in_function_try_block);
20726 if (check_body_p)
20727 check_constexpr_ctor_body (last, list, /*complain=*/true);
20728 /* Finish the function body. */
20729 finish_function_body (body);
20730
20731 return ctor_initializer_p;
20732 }
20733
20734 /* Parse an initializer.
20735
20736 initializer:
20737 = initializer-clause
20738 ( expression-list )
20739
20740 Returns an expression representing the initializer. If no
20741 initializer is present, NULL_TREE is returned.
20742
20743 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20744 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20745 set to TRUE if there is no initializer present. If there is an
20746 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20747 is set to true; otherwise it is set to false. */
20748
20749 static tree
20750 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20751 bool* non_constant_p)
20752 {
20753 cp_token *token;
20754 tree init;
20755
20756 /* Peek at the next token. */
20757 token = cp_lexer_peek_token (parser->lexer);
20758
20759 /* Let our caller know whether or not this initializer was
20760 parenthesized. */
20761 *is_direct_init = (token->type != CPP_EQ);
20762 /* Assume that the initializer is constant. */
20763 *non_constant_p = false;
20764
20765 if (token->type == CPP_EQ)
20766 {
20767 /* Consume the `='. */
20768 cp_lexer_consume_token (parser->lexer);
20769 /* Parse the initializer-clause. */
20770 init = cp_parser_initializer_clause (parser, non_constant_p);
20771 }
20772 else if (token->type == CPP_OPEN_PAREN)
20773 {
20774 vec<tree, va_gc> *vec;
20775 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20776 /*cast_p=*/false,
20777 /*allow_expansion_p=*/true,
20778 non_constant_p);
20779 if (vec == NULL)
20780 return error_mark_node;
20781 init = build_tree_list_vec (vec);
20782 release_tree_vector (vec);
20783 }
20784 else if (token->type == CPP_OPEN_BRACE)
20785 {
20786 cp_lexer_set_source_position (parser->lexer);
20787 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20788 init = cp_parser_braced_list (parser, non_constant_p);
20789 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20790 }
20791 else
20792 {
20793 /* Anything else is an error. */
20794 cp_parser_error (parser, "expected initializer");
20795 init = error_mark_node;
20796 }
20797
20798 return init;
20799 }
20800
20801 /* Parse an initializer-clause.
20802
20803 initializer-clause:
20804 assignment-expression
20805 braced-init-list
20806
20807 Returns an expression representing the initializer.
20808
20809 If the `assignment-expression' production is used the value
20810 returned is simply a representation for the expression.
20811
20812 Otherwise, calls cp_parser_braced_list. */
20813
20814 static cp_expr
20815 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20816 {
20817 cp_expr initializer;
20818
20819 /* Assume the expression is constant. */
20820 *non_constant_p = false;
20821
20822 /* If it is not a `{', then we are looking at an
20823 assignment-expression. */
20824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20825 {
20826 initializer
20827 = cp_parser_constant_expression (parser,
20828 /*allow_non_constant_p=*/true,
20829 non_constant_p);
20830 }
20831 else
20832 initializer = cp_parser_braced_list (parser, non_constant_p);
20833
20834 return initializer;
20835 }
20836
20837 /* Parse a brace-enclosed initializer list.
20838
20839 braced-init-list:
20840 { initializer-list , [opt] }
20841 { }
20842
20843 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20844 the elements of the initializer-list (or NULL, if the last
20845 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20846 NULL_TREE. There is no way to detect whether or not the optional
20847 trailing `,' was provided. NON_CONSTANT_P is as for
20848 cp_parser_initializer. */
20849
20850 static cp_expr
20851 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20852 {
20853 tree initializer;
20854 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20855
20856 /* Consume the `{' token. */
20857 cp_lexer_consume_token (parser->lexer);
20858 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20859 initializer = make_node (CONSTRUCTOR);
20860 /* If it's not a `}', then there is a non-trivial initializer. */
20861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20862 {
20863 /* Parse the initializer list. */
20864 CONSTRUCTOR_ELTS (initializer)
20865 = cp_parser_initializer_list (parser, non_constant_p);
20866 /* A trailing `,' token is allowed. */
20867 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20868 cp_lexer_consume_token (parser->lexer);
20869 }
20870 else
20871 *non_constant_p = false;
20872 /* Now, there should be a trailing `}'. */
20873 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20874 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20875 TREE_TYPE (initializer) = init_list_type_node;
20876
20877 cp_expr result (initializer);
20878 /* Build a location of the form:
20879 { ... }
20880 ^~~~~~~
20881 with caret==start at the open brace, finish at the close brace. */
20882 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20883 result.set_location (combined_loc);
20884 return result;
20885 }
20886
20887 /* Consume tokens up to, and including, the next non-nested closing `]'.
20888 Returns true iff we found a closing `]'. */
20889
20890 static bool
20891 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20892 {
20893 unsigned square_depth = 0;
20894
20895 while (true)
20896 {
20897 cp_token * token = cp_lexer_peek_token (parser->lexer);
20898
20899 switch (token->type)
20900 {
20901 case CPP_EOF:
20902 case CPP_PRAGMA_EOL:
20903 /* If we've run out of tokens, then there is no closing `]'. */
20904 return false;
20905
20906 case CPP_OPEN_SQUARE:
20907 ++square_depth;
20908 break;
20909
20910 case CPP_CLOSE_SQUARE:
20911 if (!square_depth--)
20912 {
20913 cp_lexer_consume_token (parser->lexer);
20914 return true;
20915 }
20916 break;
20917
20918 default:
20919 break;
20920 }
20921
20922 /* Consume the token. */
20923 cp_lexer_consume_token (parser->lexer);
20924 }
20925 }
20926
20927 /* Return true if we are looking at an array-designator, false otherwise. */
20928
20929 static bool
20930 cp_parser_array_designator_p (cp_parser *parser)
20931 {
20932 /* Consume the `['. */
20933 cp_lexer_consume_token (parser->lexer);
20934
20935 cp_lexer_save_tokens (parser->lexer);
20936
20937 /* Skip tokens until the next token is a closing square bracket.
20938 If we find the closing `]', and the next token is a `=', then
20939 we are looking at an array designator. */
20940 bool array_designator_p
20941 = (cp_parser_skip_to_closing_square_bracket (parser)
20942 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20943
20944 /* Roll back the tokens we skipped. */
20945 cp_lexer_rollback_tokens (parser->lexer);
20946
20947 return array_designator_p;
20948 }
20949
20950 /* Parse an initializer-list.
20951
20952 initializer-list:
20953 initializer-clause ... [opt]
20954 initializer-list , initializer-clause ... [opt]
20955
20956 GNU Extension:
20957
20958 initializer-list:
20959 designation initializer-clause ...[opt]
20960 initializer-list , designation initializer-clause ...[opt]
20961
20962 designation:
20963 . identifier =
20964 identifier :
20965 [ constant-expression ] =
20966
20967 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20968 for the initializer. If the INDEX of the elt is non-NULL, it is the
20969 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20970 as for cp_parser_initializer. */
20971
20972 static vec<constructor_elt, va_gc> *
20973 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20974 {
20975 vec<constructor_elt, va_gc> *v = NULL;
20976
20977 /* Assume all of the expressions are constant. */
20978 *non_constant_p = false;
20979
20980 /* Parse the rest of the list. */
20981 while (true)
20982 {
20983 cp_token *token;
20984 tree designator;
20985 tree initializer;
20986 bool clause_non_constant_p;
20987
20988 /* If the next token is an identifier and the following one is a
20989 colon, we are looking at the GNU designated-initializer
20990 syntax. */
20991 if (cp_parser_allow_gnu_extensions_p (parser)
20992 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20993 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
20994 {
20995 /* Warn the user that they are using an extension. */
20996 pedwarn (input_location, OPT_Wpedantic,
20997 "ISO C++ does not allow designated initializers");
20998 /* Consume the identifier. */
20999 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21000 /* Consume the `:'. */
21001 cp_lexer_consume_token (parser->lexer);
21002 }
21003 /* Also handle the C99 syntax, '. id ='. */
21004 else if (cp_parser_allow_gnu_extensions_p (parser)
21005 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21006 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21007 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21008 {
21009 /* Warn the user that they are using an extension. */
21010 pedwarn (input_location, OPT_Wpedantic,
21011 "ISO C++ does not allow C99 designated initializers");
21012 /* Consume the `.'. */
21013 cp_lexer_consume_token (parser->lexer);
21014 /* Consume the identifier. */
21015 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21016 /* Consume the `='. */
21017 cp_lexer_consume_token (parser->lexer);
21018 }
21019 /* Also handle C99 array designators, '[ const ] ='. */
21020 else if (cp_parser_allow_gnu_extensions_p (parser)
21021 && !c_dialect_objc ()
21022 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21023 {
21024 /* In C++11, [ could start a lambda-introducer. */
21025 bool non_const = false;
21026
21027 cp_parser_parse_tentatively (parser);
21028
21029 if (!cp_parser_array_designator_p (parser))
21030 {
21031 cp_parser_simulate_error (parser);
21032 designator = NULL_TREE;
21033 }
21034 else
21035 {
21036 designator = cp_parser_constant_expression (parser, true,
21037 &non_const);
21038 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21039 cp_parser_require (parser, CPP_EQ, RT_EQ);
21040 }
21041
21042 if (!cp_parser_parse_definitely (parser))
21043 designator = NULL_TREE;
21044 else if (non_const)
21045 require_potential_rvalue_constant_expression (designator);
21046 }
21047 else
21048 designator = NULL_TREE;
21049
21050 /* Parse the initializer. */
21051 initializer = cp_parser_initializer_clause (parser,
21052 &clause_non_constant_p);
21053 /* If any clause is non-constant, so is the entire initializer. */
21054 if (clause_non_constant_p)
21055 *non_constant_p = true;
21056
21057 /* If we have an ellipsis, this is an initializer pack
21058 expansion. */
21059 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21060 {
21061 /* Consume the `...'. */
21062 cp_lexer_consume_token (parser->lexer);
21063
21064 /* Turn the initializer into an initializer expansion. */
21065 initializer = make_pack_expansion (initializer);
21066 }
21067
21068 /* Add it to the vector. */
21069 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21070
21071 /* If the next token is not a comma, we have reached the end of
21072 the list. */
21073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21074 break;
21075
21076 /* Peek at the next token. */
21077 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21078 /* If the next token is a `}', then we're still done. An
21079 initializer-clause can have a trailing `,' after the
21080 initializer-list and before the closing `}'. */
21081 if (token->type == CPP_CLOSE_BRACE)
21082 break;
21083
21084 /* Consume the `,' token. */
21085 cp_lexer_consume_token (parser->lexer);
21086 }
21087
21088 return v;
21089 }
21090
21091 /* Classes [gram.class] */
21092
21093 /* Parse a class-name.
21094
21095 class-name:
21096 identifier
21097 template-id
21098
21099 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21100 to indicate that names looked up in dependent types should be
21101 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21102 keyword has been used to indicate that the name that appears next
21103 is a template. TAG_TYPE indicates the explicit tag given before
21104 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21105 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21106 is the class being defined in a class-head. If ENUM_OK is TRUE,
21107 enum-names are also accepted.
21108
21109 Returns the TYPE_DECL representing the class. */
21110
21111 static tree
21112 cp_parser_class_name (cp_parser *parser,
21113 bool typename_keyword_p,
21114 bool template_keyword_p,
21115 enum tag_types tag_type,
21116 bool check_dependency_p,
21117 bool class_head_p,
21118 bool is_declaration,
21119 bool enum_ok)
21120 {
21121 tree decl;
21122 tree scope;
21123 bool typename_p;
21124 cp_token *token;
21125 tree identifier = NULL_TREE;
21126
21127 /* All class-names start with an identifier. */
21128 token = cp_lexer_peek_token (parser->lexer);
21129 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21130 {
21131 cp_parser_error (parser, "expected class-name");
21132 return error_mark_node;
21133 }
21134
21135 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21136 to a template-id, so we save it here. */
21137 scope = parser->scope;
21138 if (scope == error_mark_node)
21139 return error_mark_node;
21140
21141 /* Any name names a type if we're following the `typename' keyword
21142 in a qualified name where the enclosing scope is type-dependent. */
21143 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21144 && dependent_type_p (scope));
21145 /* Handle the common case (an identifier, but not a template-id)
21146 efficiently. */
21147 if (token->type == CPP_NAME
21148 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21149 {
21150 cp_token *identifier_token;
21151 bool ambiguous_p;
21152
21153 /* Look for the identifier. */
21154 identifier_token = cp_lexer_peek_token (parser->lexer);
21155 ambiguous_p = identifier_token->error_reported;
21156 identifier = cp_parser_identifier (parser);
21157 /* If the next token isn't an identifier, we are certainly not
21158 looking at a class-name. */
21159 if (identifier == error_mark_node)
21160 decl = error_mark_node;
21161 /* If we know this is a type-name, there's no need to look it
21162 up. */
21163 else if (typename_p)
21164 decl = identifier;
21165 else
21166 {
21167 tree ambiguous_decls;
21168 /* If we already know that this lookup is ambiguous, then
21169 we've already issued an error message; there's no reason
21170 to check again. */
21171 if (ambiguous_p)
21172 {
21173 cp_parser_simulate_error (parser);
21174 return error_mark_node;
21175 }
21176 /* If the next token is a `::', then the name must be a type
21177 name.
21178
21179 [basic.lookup.qual]
21180
21181 During the lookup for a name preceding the :: scope
21182 resolution operator, object, function, and enumerator
21183 names are ignored. */
21184 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21185 tag_type = typename_type;
21186 /* Look up the name. */
21187 decl = cp_parser_lookup_name (parser, identifier,
21188 tag_type,
21189 /*is_template=*/false,
21190 /*is_namespace=*/false,
21191 check_dependency_p,
21192 &ambiguous_decls,
21193 identifier_token->location);
21194 if (ambiguous_decls)
21195 {
21196 if (cp_parser_parsing_tentatively (parser))
21197 cp_parser_simulate_error (parser);
21198 return error_mark_node;
21199 }
21200 }
21201 }
21202 else
21203 {
21204 /* Try a template-id. */
21205 decl = cp_parser_template_id (parser, template_keyword_p,
21206 check_dependency_p,
21207 tag_type,
21208 is_declaration);
21209 if (decl == error_mark_node)
21210 return error_mark_node;
21211 }
21212
21213 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21214
21215 /* If this is a typename, create a TYPENAME_TYPE. */
21216 if (typename_p && decl != error_mark_node)
21217 {
21218 decl = make_typename_type (scope, decl, typename_type,
21219 /*complain=*/tf_error);
21220 if (decl != error_mark_node)
21221 decl = TYPE_NAME (decl);
21222 }
21223
21224 decl = strip_using_decl (decl);
21225
21226 /* Check to see that it is really the name of a class. */
21227 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21228 && identifier_p (TREE_OPERAND (decl, 0))
21229 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21230 /* Situations like this:
21231
21232 template <typename T> struct A {
21233 typename T::template X<int>::I i;
21234 };
21235
21236 are problematic. Is `T::template X<int>' a class-name? The
21237 standard does not seem to be definitive, but there is no other
21238 valid interpretation of the following `::'. Therefore, those
21239 names are considered class-names. */
21240 {
21241 decl = make_typename_type (scope, decl, tag_type, tf_error);
21242 if (decl != error_mark_node)
21243 decl = TYPE_NAME (decl);
21244 }
21245 else if (TREE_CODE (decl) != TYPE_DECL
21246 || TREE_TYPE (decl) == error_mark_node
21247 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21248 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21249 /* In Objective-C 2.0, a classname followed by '.' starts a
21250 dot-syntax expression, and it's not a type-name. */
21251 || (c_dialect_objc ()
21252 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21253 && objc_is_class_name (decl)))
21254 decl = error_mark_node;
21255
21256 if (decl == error_mark_node)
21257 cp_parser_error (parser, "expected class-name");
21258 else if (identifier && !parser->scope)
21259 maybe_note_name_used_in_class (identifier, decl);
21260
21261 return decl;
21262 }
21263
21264 /* Parse a class-specifier.
21265
21266 class-specifier:
21267 class-head { member-specification [opt] }
21268
21269 Returns the TREE_TYPE representing the class. */
21270
21271 static tree
21272 cp_parser_class_specifier_1 (cp_parser* parser)
21273 {
21274 tree type;
21275 tree attributes = NULL_TREE;
21276 bool nested_name_specifier_p;
21277 unsigned saved_num_template_parameter_lists;
21278 bool saved_in_function_body;
21279 unsigned char in_statement;
21280 bool in_switch_statement_p;
21281 bool saved_in_unbraced_linkage_specification_p;
21282 tree old_scope = NULL_TREE;
21283 tree scope = NULL_TREE;
21284 cp_token *closing_brace;
21285
21286 push_deferring_access_checks (dk_no_deferred);
21287
21288 /* Parse the class-head. */
21289 type = cp_parser_class_head (parser,
21290 &nested_name_specifier_p);
21291 /* If the class-head was a semantic disaster, skip the entire body
21292 of the class. */
21293 if (!type)
21294 {
21295 cp_parser_skip_to_end_of_block_or_statement (parser);
21296 pop_deferring_access_checks ();
21297 return error_mark_node;
21298 }
21299
21300 /* Look for the `{'. */
21301 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21302 {
21303 pop_deferring_access_checks ();
21304 return error_mark_node;
21305 }
21306
21307 cp_ensure_no_omp_declare_simd (parser);
21308 cp_ensure_no_oacc_routine (parser);
21309
21310 /* Issue an error message if type-definitions are forbidden here. */
21311 cp_parser_check_type_definition (parser);
21312 /* Remember that we are defining one more class. */
21313 ++parser->num_classes_being_defined;
21314 /* Inside the class, surrounding template-parameter-lists do not
21315 apply. */
21316 saved_num_template_parameter_lists
21317 = parser->num_template_parameter_lists;
21318 parser->num_template_parameter_lists = 0;
21319 /* We are not in a function body. */
21320 saved_in_function_body = parser->in_function_body;
21321 parser->in_function_body = false;
21322 /* Or in a loop. */
21323 in_statement = parser->in_statement;
21324 parser->in_statement = 0;
21325 /* Or in a switch. */
21326 in_switch_statement_p = parser->in_switch_statement_p;
21327 parser->in_switch_statement_p = false;
21328 /* We are not immediately inside an extern "lang" block. */
21329 saved_in_unbraced_linkage_specification_p
21330 = parser->in_unbraced_linkage_specification_p;
21331 parser->in_unbraced_linkage_specification_p = false;
21332
21333 // Associate constraints with the type.
21334 if (flag_concepts)
21335 type = associate_classtype_constraints (type);
21336
21337 /* Start the class. */
21338 if (nested_name_specifier_p)
21339 {
21340 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21341 old_scope = push_inner_scope (scope);
21342 }
21343 type = begin_class_definition (type);
21344
21345 if (type == error_mark_node)
21346 /* If the type is erroneous, skip the entire body of the class. */
21347 cp_parser_skip_to_closing_brace (parser);
21348 else
21349 /* Parse the member-specification. */
21350 cp_parser_member_specification_opt (parser);
21351
21352 /* Look for the trailing `}'. */
21353 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21354 /* Look for trailing attributes to apply to this class. */
21355 if (cp_parser_allow_gnu_extensions_p (parser))
21356 attributes = cp_parser_gnu_attributes_opt (parser);
21357 if (type != error_mark_node)
21358 type = finish_struct (type, attributes);
21359 if (nested_name_specifier_p)
21360 pop_inner_scope (old_scope, scope);
21361
21362 /* We've finished a type definition. Check for the common syntax
21363 error of forgetting a semicolon after the definition. We need to
21364 be careful, as we can't just check for not-a-semicolon and be done
21365 with it; the user might have typed:
21366
21367 class X { } c = ...;
21368 class X { } *p = ...;
21369
21370 and so forth. Instead, enumerate all the possible tokens that
21371 might follow this production; if we don't see one of them, then
21372 complain and silently insert the semicolon. */
21373 {
21374 cp_token *token = cp_lexer_peek_token (parser->lexer);
21375 bool want_semicolon = true;
21376
21377 if (cp_next_tokens_can_be_std_attribute_p (parser))
21378 /* Don't try to parse c++11 attributes here. As per the
21379 grammar, that should be a task for
21380 cp_parser_decl_specifier_seq. */
21381 want_semicolon = false;
21382
21383 switch (token->type)
21384 {
21385 case CPP_NAME:
21386 case CPP_SEMICOLON:
21387 case CPP_MULT:
21388 case CPP_AND:
21389 case CPP_OPEN_PAREN:
21390 case CPP_CLOSE_PAREN:
21391 case CPP_COMMA:
21392 want_semicolon = false;
21393 break;
21394
21395 /* While it's legal for type qualifiers and storage class
21396 specifiers to follow type definitions in the grammar, only
21397 compiler testsuites contain code like that. Assume that if
21398 we see such code, then what we're really seeing is a case
21399 like:
21400
21401 class X { }
21402 const <type> var = ...;
21403
21404 or
21405
21406 class Y { }
21407 static <type> func (...) ...
21408
21409 i.e. the qualifier or specifier applies to the next
21410 declaration. To do so, however, we need to look ahead one
21411 more token to see if *that* token is a type specifier.
21412
21413 This code could be improved to handle:
21414
21415 class Z { }
21416 static const <type> var = ...; */
21417 case CPP_KEYWORD:
21418 if (keyword_is_decl_specifier (token->keyword))
21419 {
21420 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21421
21422 /* Handling user-defined types here would be nice, but very
21423 tricky. */
21424 want_semicolon
21425 = (lookahead->type == CPP_KEYWORD
21426 && keyword_begins_type_specifier (lookahead->keyword));
21427 }
21428 break;
21429 default:
21430 break;
21431 }
21432
21433 /* If we don't have a type, then something is very wrong and we
21434 shouldn't try to do anything clever. Likewise for not seeing the
21435 closing brace. */
21436 if (closing_brace && TYPE_P (type) && want_semicolon)
21437 {
21438 cp_token_position prev
21439 = cp_lexer_previous_token_position (parser->lexer);
21440 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21441 location_t loc = prev_token->location;
21442
21443 if (CLASSTYPE_DECLARED_CLASS (type))
21444 error_at (loc, "expected %<;%> after class definition");
21445 else if (TREE_CODE (type) == RECORD_TYPE)
21446 error_at (loc, "expected %<;%> after struct definition");
21447 else if (TREE_CODE (type) == UNION_TYPE)
21448 error_at (loc, "expected %<;%> after union definition");
21449 else
21450 gcc_unreachable ();
21451
21452 /* Unget one token and smash it to look as though we encountered
21453 a semicolon in the input stream. */
21454 cp_lexer_set_token_position (parser->lexer, prev);
21455 token = cp_lexer_peek_token (parser->lexer);
21456 token->type = CPP_SEMICOLON;
21457 token->keyword = RID_MAX;
21458 }
21459 }
21460
21461 /* If this class is not itself within the scope of another class,
21462 then we need to parse the bodies of all of the queued function
21463 definitions. Note that the queued functions defined in a class
21464 are not always processed immediately following the
21465 class-specifier for that class. Consider:
21466
21467 struct A {
21468 struct B { void f() { sizeof (A); } };
21469 };
21470
21471 If `f' were processed before the processing of `A' were
21472 completed, there would be no way to compute the size of `A'.
21473 Note that the nesting we are interested in here is lexical --
21474 not the semantic nesting given by TYPE_CONTEXT. In particular,
21475 for:
21476
21477 struct A { struct B; };
21478 struct A::B { void f() { } };
21479
21480 there is no need to delay the parsing of `A::B::f'. */
21481 if (--parser->num_classes_being_defined == 0)
21482 {
21483 tree decl;
21484 tree class_type = NULL_TREE;
21485 tree pushed_scope = NULL_TREE;
21486 unsigned ix;
21487 cp_default_arg_entry *e;
21488 tree save_ccp, save_ccr;
21489
21490 /* In a first pass, parse default arguments to the functions.
21491 Then, in a second pass, parse the bodies of the functions.
21492 This two-phased approach handles cases like:
21493
21494 struct S {
21495 void f() { g(); }
21496 void g(int i = 3);
21497 };
21498
21499 */
21500 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21501 {
21502 decl = e->decl;
21503 /* If there are default arguments that have not yet been processed,
21504 take care of them now. */
21505 if (class_type != e->class_type)
21506 {
21507 if (pushed_scope)
21508 pop_scope (pushed_scope);
21509 class_type = e->class_type;
21510 pushed_scope = push_scope (class_type);
21511 }
21512 /* Make sure that any template parameters are in scope. */
21513 maybe_begin_member_template_processing (decl);
21514 /* Parse the default argument expressions. */
21515 cp_parser_late_parsing_default_args (parser, decl);
21516 /* Remove any template parameters from the symbol table. */
21517 maybe_end_member_template_processing ();
21518 }
21519 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21520 /* Now parse any NSDMIs. */
21521 save_ccp = current_class_ptr;
21522 save_ccr = current_class_ref;
21523 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21524 {
21525 if (class_type != DECL_CONTEXT (decl))
21526 {
21527 if (pushed_scope)
21528 pop_scope (pushed_scope);
21529 class_type = DECL_CONTEXT (decl);
21530 pushed_scope = push_scope (class_type);
21531 }
21532 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21533 cp_parser_late_parsing_nsdmi (parser, decl);
21534 }
21535 vec_safe_truncate (unparsed_nsdmis, 0);
21536 current_class_ptr = save_ccp;
21537 current_class_ref = save_ccr;
21538 if (pushed_scope)
21539 pop_scope (pushed_scope);
21540
21541 /* Now do some post-NSDMI bookkeeping. */
21542 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21543 after_nsdmi_defaulted_late_checks (class_type);
21544 vec_safe_truncate (unparsed_classes, 0);
21545 after_nsdmi_defaulted_late_checks (type);
21546
21547 /* Now parse the body of the functions. */
21548 if (flag_openmp)
21549 {
21550 /* OpenMP UDRs need to be parsed before all other functions. */
21551 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21552 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21553 cp_parser_late_parsing_for_member (parser, decl);
21554 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21555 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21556 cp_parser_late_parsing_for_member (parser, decl);
21557 }
21558 else
21559 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21560 cp_parser_late_parsing_for_member (parser, decl);
21561 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21562 }
21563 else
21564 vec_safe_push (unparsed_classes, type);
21565
21566 /* Put back any saved access checks. */
21567 pop_deferring_access_checks ();
21568
21569 /* Restore saved state. */
21570 parser->in_switch_statement_p = in_switch_statement_p;
21571 parser->in_statement = in_statement;
21572 parser->in_function_body = saved_in_function_body;
21573 parser->num_template_parameter_lists
21574 = saved_num_template_parameter_lists;
21575 parser->in_unbraced_linkage_specification_p
21576 = saved_in_unbraced_linkage_specification_p;
21577
21578 return type;
21579 }
21580
21581 static tree
21582 cp_parser_class_specifier (cp_parser* parser)
21583 {
21584 tree ret;
21585 timevar_push (TV_PARSE_STRUCT);
21586 ret = cp_parser_class_specifier_1 (parser);
21587 timevar_pop (TV_PARSE_STRUCT);
21588 return ret;
21589 }
21590
21591 /* Parse a class-head.
21592
21593 class-head:
21594 class-key identifier [opt] base-clause [opt]
21595 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21596 class-key nested-name-specifier [opt] template-id
21597 base-clause [opt]
21598
21599 class-virt-specifier:
21600 final
21601
21602 GNU Extensions:
21603 class-key attributes identifier [opt] base-clause [opt]
21604 class-key attributes nested-name-specifier identifier base-clause [opt]
21605 class-key attributes nested-name-specifier [opt] template-id
21606 base-clause [opt]
21607
21608 Upon return BASES is initialized to the list of base classes (or
21609 NULL, if there are none) in the same form returned by
21610 cp_parser_base_clause.
21611
21612 Returns the TYPE of the indicated class. Sets
21613 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21614 involving a nested-name-specifier was used, and FALSE otherwise.
21615
21616 Returns error_mark_node if this is not a class-head.
21617
21618 Returns NULL_TREE if the class-head is syntactically valid, but
21619 semantically invalid in a way that means we should skip the entire
21620 body of the class. */
21621
21622 static tree
21623 cp_parser_class_head (cp_parser* parser,
21624 bool* nested_name_specifier_p)
21625 {
21626 tree nested_name_specifier;
21627 enum tag_types class_key;
21628 tree id = NULL_TREE;
21629 tree type = NULL_TREE;
21630 tree attributes;
21631 tree bases;
21632 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21633 bool template_id_p = false;
21634 bool qualified_p = false;
21635 bool invalid_nested_name_p = false;
21636 bool invalid_explicit_specialization_p = false;
21637 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21638 tree pushed_scope = NULL_TREE;
21639 unsigned num_templates;
21640 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21641 /* Assume no nested-name-specifier will be present. */
21642 *nested_name_specifier_p = false;
21643 /* Assume no template parameter lists will be used in defining the
21644 type. */
21645 num_templates = 0;
21646 parser->colon_corrects_to_scope_p = false;
21647
21648 /* Look for the class-key. */
21649 class_key = cp_parser_class_key (parser);
21650 if (class_key == none_type)
21651 return error_mark_node;
21652
21653 location_t class_head_start_location = input_location;
21654
21655 /* Parse the attributes. */
21656 attributes = cp_parser_attributes_opt (parser);
21657
21658 /* If the next token is `::', that is invalid -- but sometimes
21659 people do try to write:
21660
21661 struct ::S {};
21662
21663 Handle this gracefully by accepting the extra qualifier, and then
21664 issuing an error about it later if this really is a
21665 class-head. If it turns out just to be an elaborated type
21666 specifier, remain silent. */
21667 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21668 qualified_p = true;
21669
21670 push_deferring_access_checks (dk_no_check);
21671
21672 /* Determine the name of the class. Begin by looking for an
21673 optional nested-name-specifier. */
21674 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21675 nested_name_specifier
21676 = cp_parser_nested_name_specifier_opt (parser,
21677 /*typename_keyword_p=*/false,
21678 /*check_dependency_p=*/false,
21679 /*type_p=*/true,
21680 /*is_declaration=*/false);
21681 /* If there was a nested-name-specifier, then there *must* be an
21682 identifier. */
21683 if (nested_name_specifier)
21684 {
21685 type_start_token = cp_lexer_peek_token (parser->lexer);
21686 /* Although the grammar says `identifier', it really means
21687 `class-name' or `template-name'. You are only allowed to
21688 define a class that has already been declared with this
21689 syntax.
21690
21691 The proposed resolution for Core Issue 180 says that wherever
21692 you see `class T::X' you should treat `X' as a type-name.
21693
21694 It is OK to define an inaccessible class; for example:
21695
21696 class A { class B; };
21697 class A::B {};
21698
21699 We do not know if we will see a class-name, or a
21700 template-name. We look for a class-name first, in case the
21701 class-name is a template-id; if we looked for the
21702 template-name first we would stop after the template-name. */
21703 cp_parser_parse_tentatively (parser);
21704 type = cp_parser_class_name (parser,
21705 /*typename_keyword_p=*/false,
21706 /*template_keyword_p=*/false,
21707 class_type,
21708 /*check_dependency_p=*/false,
21709 /*class_head_p=*/true,
21710 /*is_declaration=*/false);
21711 /* If that didn't work, ignore the nested-name-specifier. */
21712 if (!cp_parser_parse_definitely (parser))
21713 {
21714 invalid_nested_name_p = true;
21715 type_start_token = cp_lexer_peek_token (parser->lexer);
21716 id = cp_parser_identifier (parser);
21717 if (id == error_mark_node)
21718 id = NULL_TREE;
21719 }
21720 /* If we could not find a corresponding TYPE, treat this
21721 declaration like an unqualified declaration. */
21722 if (type == error_mark_node)
21723 nested_name_specifier = NULL_TREE;
21724 /* Otherwise, count the number of templates used in TYPE and its
21725 containing scopes. */
21726 else
21727 {
21728 tree scope;
21729
21730 for (scope = TREE_TYPE (type);
21731 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21732 scope = get_containing_scope (scope))
21733 if (TYPE_P (scope)
21734 && CLASS_TYPE_P (scope)
21735 && CLASSTYPE_TEMPLATE_INFO (scope)
21736 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21737 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21738 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21739 ++num_templates;
21740 }
21741 }
21742 /* Otherwise, the identifier is optional. */
21743 else
21744 {
21745 /* We don't know whether what comes next is a template-id,
21746 an identifier, or nothing at all. */
21747 cp_parser_parse_tentatively (parser);
21748 /* Check for a template-id. */
21749 type_start_token = cp_lexer_peek_token (parser->lexer);
21750 id = cp_parser_template_id (parser,
21751 /*template_keyword_p=*/false,
21752 /*check_dependency_p=*/true,
21753 class_key,
21754 /*is_declaration=*/true);
21755 /* If that didn't work, it could still be an identifier. */
21756 if (!cp_parser_parse_definitely (parser))
21757 {
21758 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21759 {
21760 type_start_token = cp_lexer_peek_token (parser->lexer);
21761 id = cp_parser_identifier (parser);
21762 }
21763 else
21764 id = NULL_TREE;
21765 }
21766 else
21767 {
21768 template_id_p = true;
21769 ++num_templates;
21770 }
21771 }
21772
21773 pop_deferring_access_checks ();
21774
21775 if (id)
21776 {
21777 cp_parser_check_for_invalid_template_id (parser, id,
21778 class_key,
21779 type_start_token->location);
21780 }
21781 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21782
21783 /* If it's not a `:' or a `{' then we can't really be looking at a
21784 class-head, since a class-head only appears as part of a
21785 class-specifier. We have to detect this situation before calling
21786 xref_tag, since that has irreversible side-effects. */
21787 if (!cp_parser_next_token_starts_class_definition_p (parser))
21788 {
21789 cp_parser_error (parser, "expected %<{%> or %<:%>");
21790 type = error_mark_node;
21791 goto out;
21792 }
21793
21794 /* At this point, we're going ahead with the class-specifier, even
21795 if some other problem occurs. */
21796 cp_parser_commit_to_tentative_parse (parser);
21797 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21798 {
21799 cp_parser_error (parser,
21800 "cannot specify %<override%> for a class");
21801 type = error_mark_node;
21802 goto out;
21803 }
21804 /* Issue the error about the overly-qualified name now. */
21805 if (qualified_p)
21806 {
21807 cp_parser_error (parser,
21808 "global qualification of class name is invalid");
21809 type = error_mark_node;
21810 goto out;
21811 }
21812 else if (invalid_nested_name_p)
21813 {
21814 cp_parser_error (parser,
21815 "qualified name does not name a class");
21816 type = error_mark_node;
21817 goto out;
21818 }
21819 else if (nested_name_specifier)
21820 {
21821 tree scope;
21822
21823 /* Reject typedef-names in class heads. */
21824 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21825 {
21826 error_at (type_start_token->location,
21827 "invalid class name in declaration of %qD",
21828 type);
21829 type = NULL_TREE;
21830 goto done;
21831 }
21832
21833 /* Figure out in what scope the declaration is being placed. */
21834 scope = current_scope ();
21835 /* If that scope does not contain the scope in which the
21836 class was originally declared, the program is invalid. */
21837 if (scope && !is_ancestor (scope, nested_name_specifier))
21838 {
21839 if (at_namespace_scope_p ())
21840 error_at (type_start_token->location,
21841 "declaration of %qD in namespace %qD which does not "
21842 "enclose %qD",
21843 type, scope, nested_name_specifier);
21844 else
21845 error_at (type_start_token->location,
21846 "declaration of %qD in %qD which does not enclose %qD",
21847 type, scope, nested_name_specifier);
21848 type = NULL_TREE;
21849 goto done;
21850 }
21851 /* [dcl.meaning]
21852
21853 A declarator-id shall not be qualified except for the
21854 definition of a ... nested class outside of its class
21855 ... [or] the definition or explicit instantiation of a
21856 class member of a namespace outside of its namespace. */
21857 if (scope == nested_name_specifier)
21858 {
21859 permerror (nested_name_specifier_token_start->location,
21860 "extra qualification not allowed");
21861 nested_name_specifier = NULL_TREE;
21862 num_templates = 0;
21863 }
21864 }
21865 /* An explicit-specialization must be preceded by "template <>". If
21866 it is not, try to recover gracefully. */
21867 if (at_namespace_scope_p ()
21868 && parser->num_template_parameter_lists == 0
21869 && template_id_p)
21870 {
21871 /* Build a location of this form:
21872 struct typename <ARGS>
21873 ^~~~~~~~~~~~~~~~~~~~~~
21874 with caret==start at the start token, and
21875 finishing at the end of the type. */
21876 location_t reported_loc
21877 = make_location (class_head_start_location,
21878 class_head_start_location,
21879 get_finish (type_start_token->location));
21880 rich_location richloc (line_table, reported_loc);
21881 richloc.add_fixit_insert (class_head_start_location, "template <> ");
21882 error_at_rich_loc
21883 (&richloc,
21884 "an explicit specialization must be preceded by %<template <>%>");
21885 invalid_explicit_specialization_p = true;
21886 /* Take the same action that would have been taken by
21887 cp_parser_explicit_specialization. */
21888 ++parser->num_template_parameter_lists;
21889 begin_specialization ();
21890 }
21891 /* There must be no "return" statements between this point and the
21892 end of this function; set "type "to the correct return value and
21893 use "goto done;" to return. */
21894 /* Make sure that the right number of template parameters were
21895 present. */
21896 if (!cp_parser_check_template_parameters (parser, num_templates,
21897 type_start_token->location,
21898 /*declarator=*/NULL))
21899 {
21900 /* If something went wrong, there is no point in even trying to
21901 process the class-definition. */
21902 type = NULL_TREE;
21903 goto done;
21904 }
21905
21906 /* Look up the type. */
21907 if (template_id_p)
21908 {
21909 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21910 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21911 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21912 {
21913 error_at (type_start_token->location,
21914 "function template %qD redeclared as a class template", id);
21915 type = error_mark_node;
21916 }
21917 else
21918 {
21919 type = TREE_TYPE (id);
21920 type = maybe_process_partial_specialization (type);
21921 }
21922 if (nested_name_specifier)
21923 pushed_scope = push_scope (nested_name_specifier);
21924 }
21925 else if (nested_name_specifier)
21926 {
21927 tree class_type;
21928
21929 /* Given:
21930
21931 template <typename T> struct S { struct T };
21932 template <typename T> struct S<T>::T { };
21933
21934 we will get a TYPENAME_TYPE when processing the definition of
21935 `S::T'. We need to resolve it to the actual type before we
21936 try to define it. */
21937 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21938 {
21939 class_type = resolve_typename_type (TREE_TYPE (type),
21940 /*only_current_p=*/false);
21941 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21942 type = TYPE_NAME (class_type);
21943 else
21944 {
21945 cp_parser_error (parser, "could not resolve typename type");
21946 type = error_mark_node;
21947 }
21948 }
21949
21950 if (maybe_process_partial_specialization (TREE_TYPE (type))
21951 == error_mark_node)
21952 {
21953 type = NULL_TREE;
21954 goto done;
21955 }
21956
21957 class_type = current_class_type;
21958 /* Enter the scope indicated by the nested-name-specifier. */
21959 pushed_scope = push_scope (nested_name_specifier);
21960 /* Get the canonical version of this type. */
21961 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21962 /* Call push_template_decl if it seems like we should be defining a
21963 template either from the template headers or the type we're
21964 defining, so that we diagnose both extra and missing headers. */
21965 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21966 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21967 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21968 {
21969 type = push_template_decl (type);
21970 if (type == error_mark_node)
21971 {
21972 type = NULL_TREE;
21973 goto done;
21974 }
21975 }
21976
21977 type = TREE_TYPE (type);
21978 *nested_name_specifier_p = true;
21979 }
21980 else /* The name is not a nested name. */
21981 {
21982 /* If the class was unnamed, create a dummy name. */
21983 if (!id)
21984 id = make_anon_name ();
21985 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21986 parser->num_template_parameter_lists);
21987 }
21988
21989 /* Indicate whether this class was declared as a `class' or as a
21990 `struct'. */
21991 if (TREE_CODE (type) == RECORD_TYPE)
21992 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21993 cp_parser_check_class_key (class_key, type);
21994
21995 /* If this type was already complete, and we see another definition,
21996 that's an error. */
21997 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21998 {
21999 error_at (type_start_token->location, "redefinition of %q#T",
22000 type);
22001 error_at (type_start_token->location, "previous definition of %q+#T",
22002 type);
22003 type = NULL_TREE;
22004 goto done;
22005 }
22006 else if (type == error_mark_node)
22007 type = NULL_TREE;
22008
22009 if (type)
22010 {
22011 /* Apply attributes now, before any use of the class as a template
22012 argument in its base list. */
22013 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22014 fixup_attribute_variants (type);
22015 }
22016
22017 /* We will have entered the scope containing the class; the names of
22018 base classes should be looked up in that context. For example:
22019
22020 struct A { struct B {}; struct C; };
22021 struct A::C : B {};
22022
22023 is valid. */
22024
22025 /* Get the list of base-classes, if there is one. */
22026 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22027 {
22028 /* PR59482: enter the class scope so that base-specifiers are looked
22029 up correctly. */
22030 if (type)
22031 pushclass (type);
22032 bases = cp_parser_base_clause (parser);
22033 /* PR59482: get out of the previously pushed class scope so that the
22034 subsequent pops pop the right thing. */
22035 if (type)
22036 popclass ();
22037 }
22038 else
22039 bases = NULL_TREE;
22040
22041 /* If we're really defining a class, process the base classes.
22042 If they're invalid, fail. */
22043 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22044 && !xref_basetypes (type, bases))
22045 type = NULL_TREE;
22046
22047 done:
22048 /* Leave the scope given by the nested-name-specifier. We will
22049 enter the class scope itself while processing the members. */
22050 if (pushed_scope)
22051 pop_scope (pushed_scope);
22052
22053 if (invalid_explicit_specialization_p)
22054 {
22055 end_specialization ();
22056 --parser->num_template_parameter_lists;
22057 }
22058
22059 if (type)
22060 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22061 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22062 CLASSTYPE_FINAL (type) = 1;
22063 out:
22064 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22065 return type;
22066 }
22067
22068 /* Parse a class-key.
22069
22070 class-key:
22071 class
22072 struct
22073 union
22074
22075 Returns the kind of class-key specified, or none_type to indicate
22076 error. */
22077
22078 static enum tag_types
22079 cp_parser_class_key (cp_parser* parser)
22080 {
22081 cp_token *token;
22082 enum tag_types tag_type;
22083
22084 /* Look for the class-key. */
22085 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22086 if (!token)
22087 return none_type;
22088
22089 /* Check to see if the TOKEN is a class-key. */
22090 tag_type = cp_parser_token_is_class_key (token);
22091 if (!tag_type)
22092 cp_parser_error (parser, "expected class-key");
22093 return tag_type;
22094 }
22095
22096 /* Parse a type-parameter-key.
22097
22098 type-parameter-key:
22099 class
22100 typename
22101 */
22102
22103 static void
22104 cp_parser_type_parameter_key (cp_parser* parser)
22105 {
22106 /* Look for the type-parameter-key. */
22107 enum tag_types tag_type = none_type;
22108 cp_token *token = cp_lexer_peek_token (parser->lexer);
22109 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22110 {
22111 cp_lexer_consume_token (parser->lexer);
22112 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22113 /* typename is not allowed in a template template parameter
22114 by the standard until C++1Z. */
22115 pedwarn (token->location, OPT_Wpedantic,
22116 "ISO C++ forbids typename key in template template parameter;"
22117 " use -std=c++1z or -std=gnu++1z");
22118 }
22119 else
22120 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22121
22122 return;
22123 }
22124
22125 /* Parse an (optional) member-specification.
22126
22127 member-specification:
22128 member-declaration member-specification [opt]
22129 access-specifier : member-specification [opt] */
22130
22131 static void
22132 cp_parser_member_specification_opt (cp_parser* parser)
22133 {
22134 while (true)
22135 {
22136 cp_token *token;
22137 enum rid keyword;
22138
22139 /* Peek at the next token. */
22140 token = cp_lexer_peek_token (parser->lexer);
22141 /* If it's a `}', or EOF then we've seen all the members. */
22142 if (token->type == CPP_CLOSE_BRACE
22143 || token->type == CPP_EOF
22144 || token->type == CPP_PRAGMA_EOL)
22145 break;
22146
22147 /* See if this token is a keyword. */
22148 keyword = token->keyword;
22149 switch (keyword)
22150 {
22151 case RID_PUBLIC:
22152 case RID_PROTECTED:
22153 case RID_PRIVATE:
22154 /* Consume the access-specifier. */
22155 cp_lexer_consume_token (parser->lexer);
22156 /* Remember which access-specifier is active. */
22157 current_access_specifier = token->u.value;
22158 /* Look for the `:'. */
22159 cp_parser_require (parser, CPP_COLON, RT_COLON);
22160 break;
22161
22162 default:
22163 /* Accept #pragmas at class scope. */
22164 if (token->type == CPP_PRAGMA)
22165 {
22166 cp_parser_pragma (parser, pragma_member, NULL);
22167 break;
22168 }
22169
22170 /* Otherwise, the next construction must be a
22171 member-declaration. */
22172 cp_parser_member_declaration (parser);
22173 }
22174 }
22175 }
22176
22177 /* Parse a member-declaration.
22178
22179 member-declaration:
22180 decl-specifier-seq [opt] member-declarator-list [opt] ;
22181 function-definition ; [opt]
22182 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22183 using-declaration
22184 template-declaration
22185 alias-declaration
22186
22187 member-declarator-list:
22188 member-declarator
22189 member-declarator-list , member-declarator
22190
22191 member-declarator:
22192 declarator pure-specifier [opt]
22193 declarator constant-initializer [opt]
22194 identifier [opt] : constant-expression
22195
22196 GNU Extensions:
22197
22198 member-declaration:
22199 __extension__ member-declaration
22200
22201 member-declarator:
22202 declarator attributes [opt] pure-specifier [opt]
22203 declarator attributes [opt] constant-initializer [opt]
22204 identifier [opt] attributes [opt] : constant-expression
22205
22206 C++0x Extensions:
22207
22208 member-declaration:
22209 static_assert-declaration */
22210
22211 static void
22212 cp_parser_member_declaration (cp_parser* parser)
22213 {
22214 cp_decl_specifier_seq decl_specifiers;
22215 tree prefix_attributes;
22216 tree decl;
22217 int declares_class_or_enum;
22218 bool friend_p;
22219 cp_token *token = NULL;
22220 cp_token *decl_spec_token_start = NULL;
22221 cp_token *initializer_token_start = NULL;
22222 int saved_pedantic;
22223 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22224
22225 /* Check for the `__extension__' keyword. */
22226 if (cp_parser_extension_opt (parser, &saved_pedantic))
22227 {
22228 /* Recurse. */
22229 cp_parser_member_declaration (parser);
22230 /* Restore the old value of the PEDANTIC flag. */
22231 pedantic = saved_pedantic;
22232
22233 return;
22234 }
22235
22236 /* Check for a template-declaration. */
22237 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22238 {
22239 /* An explicit specialization here is an error condition, and we
22240 expect the specialization handler to detect and report this. */
22241 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22242 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22243 cp_parser_explicit_specialization (parser);
22244 else
22245 cp_parser_template_declaration (parser, /*member_p=*/true);
22246
22247 return;
22248 }
22249 /* Check for a template introduction. */
22250 else if (cp_parser_template_declaration_after_export (parser, true))
22251 return;
22252
22253 /* Check for a using-declaration. */
22254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22255 {
22256 if (cxx_dialect < cxx11)
22257 {
22258 /* Parse the using-declaration. */
22259 cp_parser_using_declaration (parser,
22260 /*access_declaration_p=*/false);
22261 return;
22262 }
22263 else
22264 {
22265 tree decl;
22266 bool alias_decl_expected;
22267 cp_parser_parse_tentatively (parser);
22268 decl = cp_parser_alias_declaration (parser);
22269 /* Note that if we actually see the '=' token after the
22270 identifier, cp_parser_alias_declaration commits the
22271 tentative parse. In that case, we really expect an
22272 alias-declaration. Otherwise, we expect a using
22273 declaration. */
22274 alias_decl_expected =
22275 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22276 cp_parser_parse_definitely (parser);
22277
22278 if (alias_decl_expected)
22279 finish_member_declaration (decl);
22280 else
22281 cp_parser_using_declaration (parser,
22282 /*access_declaration_p=*/false);
22283 return;
22284 }
22285 }
22286
22287 /* Check for @defs. */
22288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22289 {
22290 tree ivar, member;
22291 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22292 ivar = ivar_chains;
22293 while (ivar)
22294 {
22295 member = ivar;
22296 ivar = TREE_CHAIN (member);
22297 TREE_CHAIN (member) = NULL_TREE;
22298 finish_member_declaration (member);
22299 }
22300 return;
22301 }
22302
22303 /* If the next token is `static_assert' we have a static assertion. */
22304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22305 {
22306 cp_parser_static_assert (parser, /*member_p=*/true);
22307 return;
22308 }
22309
22310 parser->colon_corrects_to_scope_p = false;
22311
22312 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22313 goto out;
22314
22315 /* Parse the decl-specifier-seq. */
22316 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22317 cp_parser_decl_specifier_seq (parser,
22318 CP_PARSER_FLAGS_OPTIONAL,
22319 &decl_specifiers,
22320 &declares_class_or_enum);
22321 /* Check for an invalid type-name. */
22322 if (!decl_specifiers.any_type_specifiers_p
22323 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22324 goto out;
22325 /* If there is no declarator, then the decl-specifier-seq should
22326 specify a type. */
22327 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22328 {
22329 /* If there was no decl-specifier-seq, and the next token is a
22330 `;', then we have something like:
22331
22332 struct S { ; };
22333
22334 [class.mem]
22335
22336 Each member-declaration shall declare at least one member
22337 name of the class. */
22338 if (!decl_specifiers.any_specifiers_p)
22339 {
22340 cp_token *token = cp_lexer_peek_token (parser->lexer);
22341 if (!in_system_header_at (token->location))
22342 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22343 }
22344 else
22345 {
22346 tree type;
22347
22348 /* See if this declaration is a friend. */
22349 friend_p = cp_parser_friend_p (&decl_specifiers);
22350 /* If there were decl-specifiers, check to see if there was
22351 a class-declaration. */
22352 type = check_tag_decl (&decl_specifiers,
22353 /*explicit_type_instantiation_p=*/false);
22354 /* Nested classes have already been added to the class, but
22355 a `friend' needs to be explicitly registered. */
22356 if (friend_p)
22357 {
22358 /* If the `friend' keyword was present, the friend must
22359 be introduced with a class-key. */
22360 if (!declares_class_or_enum && cxx_dialect < cxx11)
22361 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22362 "in C++03 a class-key must be used "
22363 "when declaring a friend");
22364 /* In this case:
22365
22366 template <typename T> struct A {
22367 friend struct A<T>::B;
22368 };
22369
22370 A<T>::B will be represented by a TYPENAME_TYPE, and
22371 therefore not recognized by check_tag_decl. */
22372 if (!type)
22373 {
22374 type = decl_specifiers.type;
22375 if (type && TREE_CODE (type) == TYPE_DECL)
22376 type = TREE_TYPE (type);
22377 }
22378 if (!type || !TYPE_P (type))
22379 error_at (decl_spec_token_start->location,
22380 "friend declaration does not name a class or "
22381 "function");
22382 else
22383 make_friend_class (current_class_type, type,
22384 /*complain=*/true);
22385 }
22386 /* If there is no TYPE, an error message will already have
22387 been issued. */
22388 else if (!type || type == error_mark_node)
22389 ;
22390 /* An anonymous aggregate has to be handled specially; such
22391 a declaration really declares a data member (with a
22392 particular type), as opposed to a nested class. */
22393 else if (ANON_AGGR_TYPE_P (type))
22394 {
22395 /* C++11 9.5/6. */
22396 if (decl_specifiers.storage_class != sc_none)
22397 error_at (decl_spec_token_start->location,
22398 "a storage class on an anonymous aggregate "
22399 "in class scope is not allowed");
22400
22401 /* Remove constructors and such from TYPE, now that we
22402 know it is an anonymous aggregate. */
22403 fixup_anonymous_aggr (type);
22404 /* And make the corresponding data member. */
22405 decl = build_decl (decl_spec_token_start->location,
22406 FIELD_DECL, NULL_TREE, type);
22407 /* Add it to the class. */
22408 finish_member_declaration (decl);
22409 }
22410 else
22411 cp_parser_check_access_in_redeclaration
22412 (TYPE_NAME (type),
22413 decl_spec_token_start->location);
22414 }
22415 }
22416 else
22417 {
22418 bool assume_semicolon = false;
22419
22420 /* Clear attributes from the decl_specifiers but keep them
22421 around as prefix attributes that apply them to the entity
22422 being declared. */
22423 prefix_attributes = decl_specifiers.attributes;
22424 decl_specifiers.attributes = NULL_TREE;
22425
22426 /* See if these declarations will be friends. */
22427 friend_p = cp_parser_friend_p (&decl_specifiers);
22428
22429 /* Keep going until we hit the `;' at the end of the
22430 declaration. */
22431 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22432 {
22433 tree attributes = NULL_TREE;
22434 tree first_attribute;
22435
22436 /* Peek at the next token. */
22437 token = cp_lexer_peek_token (parser->lexer);
22438
22439 /* Check for a bitfield declaration. */
22440 if (token->type == CPP_COLON
22441 || (token->type == CPP_NAME
22442 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22443 == CPP_COLON))
22444 {
22445 tree identifier;
22446 tree width;
22447
22448 /* Get the name of the bitfield. Note that we cannot just
22449 check TOKEN here because it may have been invalidated by
22450 the call to cp_lexer_peek_nth_token above. */
22451 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22452 identifier = cp_parser_identifier (parser);
22453 else
22454 identifier = NULL_TREE;
22455
22456 /* Consume the `:' token. */
22457 cp_lexer_consume_token (parser->lexer);
22458 /* Get the width of the bitfield. */
22459 width
22460 = cp_parser_constant_expression (parser);
22461
22462 /* Look for attributes that apply to the bitfield. */
22463 attributes = cp_parser_attributes_opt (parser);
22464 /* Remember which attributes are prefix attributes and
22465 which are not. */
22466 first_attribute = attributes;
22467 /* Combine the attributes. */
22468 attributes = chainon (prefix_attributes, attributes);
22469
22470 /* Create the bitfield declaration. */
22471 decl = grokbitfield (identifier
22472 ? make_id_declarator (NULL_TREE,
22473 identifier,
22474 sfk_none)
22475 : NULL,
22476 &decl_specifiers,
22477 width,
22478 attributes);
22479 }
22480 else
22481 {
22482 cp_declarator *declarator;
22483 tree initializer;
22484 tree asm_specification;
22485 int ctor_dtor_or_conv_p;
22486
22487 /* Parse the declarator. */
22488 declarator
22489 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22490 &ctor_dtor_or_conv_p,
22491 /*parenthesized_p=*/NULL,
22492 /*member_p=*/true,
22493 friend_p);
22494
22495 /* If something went wrong parsing the declarator, make sure
22496 that we at least consume some tokens. */
22497 if (declarator == cp_error_declarator)
22498 {
22499 /* Skip to the end of the statement. */
22500 cp_parser_skip_to_end_of_statement (parser);
22501 /* If the next token is not a semicolon, that is
22502 probably because we just skipped over the body of
22503 a function. So, we consume a semicolon if
22504 present, but do not issue an error message if it
22505 is not present. */
22506 if (cp_lexer_next_token_is (parser->lexer,
22507 CPP_SEMICOLON))
22508 cp_lexer_consume_token (parser->lexer);
22509 goto out;
22510 }
22511
22512 if (declares_class_or_enum & 2)
22513 cp_parser_check_for_definition_in_return_type
22514 (declarator, decl_specifiers.type,
22515 decl_specifiers.locations[ds_type_spec]);
22516
22517 /* Look for an asm-specification. */
22518 asm_specification = cp_parser_asm_specification_opt (parser);
22519 /* Look for attributes that apply to the declaration. */
22520 attributes = cp_parser_attributes_opt (parser);
22521 /* Remember which attributes are prefix attributes and
22522 which are not. */
22523 first_attribute = attributes;
22524 /* Combine the attributes. */
22525 attributes = chainon (prefix_attributes, attributes);
22526
22527 /* If it's an `=', then we have a constant-initializer or a
22528 pure-specifier. It is not correct to parse the
22529 initializer before registering the member declaration
22530 since the member declaration should be in scope while
22531 its initializer is processed. However, the rest of the
22532 front end does not yet provide an interface that allows
22533 us to handle this correctly. */
22534 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22535 {
22536 /* In [class.mem]:
22537
22538 A pure-specifier shall be used only in the declaration of
22539 a virtual function.
22540
22541 A member-declarator can contain a constant-initializer
22542 only if it declares a static member of integral or
22543 enumeration type.
22544
22545 Therefore, if the DECLARATOR is for a function, we look
22546 for a pure-specifier; otherwise, we look for a
22547 constant-initializer. When we call `grokfield', it will
22548 perform more stringent semantics checks. */
22549 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22550 if (function_declarator_p (declarator)
22551 || (decl_specifiers.type
22552 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22553 && declarator->kind == cdk_id
22554 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22555 == FUNCTION_TYPE)))
22556 initializer = cp_parser_pure_specifier (parser);
22557 else if (decl_specifiers.storage_class != sc_static)
22558 initializer = cp_parser_save_nsdmi (parser);
22559 else if (cxx_dialect >= cxx11)
22560 {
22561 bool nonconst;
22562 /* Don't require a constant rvalue in C++11, since we
22563 might want a reference constant. We'll enforce
22564 constancy later. */
22565 cp_lexer_consume_token (parser->lexer);
22566 /* Parse the initializer. */
22567 initializer = cp_parser_initializer_clause (parser,
22568 &nonconst);
22569 }
22570 else
22571 /* Parse the initializer. */
22572 initializer = cp_parser_constant_initializer (parser);
22573 }
22574 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22575 && !function_declarator_p (declarator))
22576 {
22577 bool x;
22578 if (decl_specifiers.storage_class != sc_static)
22579 initializer = cp_parser_save_nsdmi (parser);
22580 else
22581 initializer = cp_parser_initializer (parser, &x, &x);
22582 }
22583 /* Otherwise, there is no initializer. */
22584 else
22585 initializer = NULL_TREE;
22586
22587 /* See if we are probably looking at a function
22588 definition. We are certainly not looking at a
22589 member-declarator. Calling `grokfield' has
22590 side-effects, so we must not do it unless we are sure
22591 that we are looking at a member-declarator. */
22592 if (cp_parser_token_starts_function_definition_p
22593 (cp_lexer_peek_token (parser->lexer)))
22594 {
22595 /* The grammar does not allow a pure-specifier to be
22596 used when a member function is defined. (It is
22597 possible that this fact is an oversight in the
22598 standard, since a pure function may be defined
22599 outside of the class-specifier. */
22600 if (initializer && initializer_token_start)
22601 error_at (initializer_token_start->location,
22602 "pure-specifier on function-definition");
22603 decl = cp_parser_save_member_function_body (parser,
22604 &decl_specifiers,
22605 declarator,
22606 attributes);
22607 if (parser->fully_implicit_function_template_p)
22608 decl = finish_fully_implicit_template (parser, decl);
22609 /* If the member was not a friend, declare it here. */
22610 if (!friend_p)
22611 finish_member_declaration (decl);
22612 /* Peek at the next token. */
22613 token = cp_lexer_peek_token (parser->lexer);
22614 /* If the next token is a semicolon, consume it. */
22615 if (token->type == CPP_SEMICOLON)
22616 cp_lexer_consume_token (parser->lexer);
22617 goto out;
22618 }
22619 else
22620 if (declarator->kind == cdk_function)
22621 declarator->id_loc = token->location;
22622 /* Create the declaration. */
22623 decl = grokfield (declarator, &decl_specifiers,
22624 initializer, /*init_const_expr_p=*/true,
22625 asm_specification, attributes);
22626 if (parser->fully_implicit_function_template_p)
22627 {
22628 if (friend_p)
22629 finish_fully_implicit_template (parser, 0);
22630 else
22631 decl = finish_fully_implicit_template (parser, decl);
22632 }
22633 }
22634
22635 cp_finalize_omp_declare_simd (parser, decl);
22636 cp_finalize_oacc_routine (parser, decl, false);
22637
22638 /* Reset PREFIX_ATTRIBUTES. */
22639 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22640 attributes = TREE_CHAIN (attributes);
22641 if (attributes)
22642 TREE_CHAIN (attributes) = NULL_TREE;
22643
22644 /* If there is any qualification still in effect, clear it
22645 now; we will be starting fresh with the next declarator. */
22646 parser->scope = NULL_TREE;
22647 parser->qualifying_scope = NULL_TREE;
22648 parser->object_scope = NULL_TREE;
22649 /* If it's a `,', then there are more declarators. */
22650 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22651 {
22652 cp_lexer_consume_token (parser->lexer);
22653 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22654 {
22655 cp_token *token = cp_lexer_previous_token (parser->lexer);
22656 error_at (token->location,
22657 "stray %<,%> at end of member declaration");
22658 }
22659 }
22660 /* If the next token isn't a `;', then we have a parse error. */
22661 else if (cp_lexer_next_token_is_not (parser->lexer,
22662 CPP_SEMICOLON))
22663 {
22664 /* The next token might be a ways away from where the
22665 actual semicolon is missing. Find the previous token
22666 and use that for our error position. */
22667 cp_token *token = cp_lexer_previous_token (parser->lexer);
22668 error_at (token->location,
22669 "expected %<;%> at end of member declaration");
22670
22671 /* Assume that the user meant to provide a semicolon. If
22672 we were to cp_parser_skip_to_end_of_statement, we might
22673 skip to a semicolon inside a member function definition
22674 and issue nonsensical error messages. */
22675 assume_semicolon = true;
22676 }
22677
22678 if (decl)
22679 {
22680 /* Add DECL to the list of members. */
22681 if (!friend_p
22682 /* Explicitly include, eg, NSDMIs, for better error
22683 recovery (c++/58650). */
22684 || !DECL_DECLARES_FUNCTION_P (decl))
22685 finish_member_declaration (decl);
22686
22687 if (TREE_CODE (decl) == FUNCTION_DECL)
22688 cp_parser_save_default_args (parser, decl);
22689 else if (TREE_CODE (decl) == FIELD_DECL
22690 && !DECL_C_BIT_FIELD (decl)
22691 && DECL_INITIAL (decl))
22692 /* Add DECL to the queue of NSDMI to be parsed later. */
22693 vec_safe_push (unparsed_nsdmis, decl);
22694 }
22695
22696 if (assume_semicolon)
22697 goto out;
22698 }
22699 }
22700
22701 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22702 out:
22703 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22704 }
22705
22706 /* Parse a pure-specifier.
22707
22708 pure-specifier:
22709 = 0
22710
22711 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22712 Otherwise, ERROR_MARK_NODE is returned. */
22713
22714 static tree
22715 cp_parser_pure_specifier (cp_parser* parser)
22716 {
22717 cp_token *token;
22718
22719 /* Look for the `=' token. */
22720 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22721 return error_mark_node;
22722 /* Look for the `0' token. */
22723 token = cp_lexer_peek_token (parser->lexer);
22724
22725 if (token->type == CPP_EOF
22726 || token->type == CPP_PRAGMA_EOL)
22727 return error_mark_node;
22728
22729 cp_lexer_consume_token (parser->lexer);
22730
22731 /* Accept = default or = delete in c++0x mode. */
22732 if (token->keyword == RID_DEFAULT
22733 || token->keyword == RID_DELETE)
22734 {
22735 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22736 return token->u.value;
22737 }
22738
22739 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22740 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22741 {
22742 cp_parser_error (parser,
22743 "invalid pure specifier (only %<= 0%> is allowed)");
22744 cp_parser_skip_to_end_of_statement (parser);
22745 return error_mark_node;
22746 }
22747 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22748 {
22749 error_at (token->location, "templates may not be %<virtual%>");
22750 return error_mark_node;
22751 }
22752
22753 return integer_zero_node;
22754 }
22755
22756 /* Parse a constant-initializer.
22757
22758 constant-initializer:
22759 = constant-expression
22760
22761 Returns a representation of the constant-expression. */
22762
22763 static tree
22764 cp_parser_constant_initializer (cp_parser* parser)
22765 {
22766 /* Look for the `=' token. */
22767 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22768 return error_mark_node;
22769
22770 /* It is invalid to write:
22771
22772 struct S { static const int i = { 7 }; };
22773
22774 */
22775 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22776 {
22777 cp_parser_error (parser,
22778 "a brace-enclosed initializer is not allowed here");
22779 /* Consume the opening brace. */
22780 cp_lexer_consume_token (parser->lexer);
22781 /* Skip the initializer. */
22782 cp_parser_skip_to_closing_brace (parser);
22783 /* Look for the trailing `}'. */
22784 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22785
22786 return error_mark_node;
22787 }
22788
22789 return cp_parser_constant_expression (parser);
22790 }
22791
22792 /* Derived classes [gram.class.derived] */
22793
22794 /* Parse a base-clause.
22795
22796 base-clause:
22797 : base-specifier-list
22798
22799 base-specifier-list:
22800 base-specifier ... [opt]
22801 base-specifier-list , base-specifier ... [opt]
22802
22803 Returns a TREE_LIST representing the base-classes, in the order in
22804 which they were declared. The representation of each node is as
22805 described by cp_parser_base_specifier.
22806
22807 In the case that no bases are specified, this function will return
22808 NULL_TREE, not ERROR_MARK_NODE. */
22809
22810 static tree
22811 cp_parser_base_clause (cp_parser* parser)
22812 {
22813 tree bases = NULL_TREE;
22814
22815 /* Look for the `:' that begins the list. */
22816 cp_parser_require (parser, CPP_COLON, RT_COLON);
22817
22818 /* Scan the base-specifier-list. */
22819 while (true)
22820 {
22821 cp_token *token;
22822 tree base;
22823 bool pack_expansion_p = false;
22824
22825 /* Look for the base-specifier. */
22826 base = cp_parser_base_specifier (parser);
22827 /* Look for the (optional) ellipsis. */
22828 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22829 {
22830 /* Consume the `...'. */
22831 cp_lexer_consume_token (parser->lexer);
22832
22833 pack_expansion_p = true;
22834 }
22835
22836 /* Add BASE to the front of the list. */
22837 if (base && base != error_mark_node)
22838 {
22839 if (pack_expansion_p)
22840 /* Make this a pack expansion type. */
22841 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22842
22843 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22844 {
22845 TREE_CHAIN (base) = bases;
22846 bases = base;
22847 }
22848 }
22849 /* Peek at the next token. */
22850 token = cp_lexer_peek_token (parser->lexer);
22851 /* If it's not a comma, then the list is complete. */
22852 if (token->type != CPP_COMMA)
22853 break;
22854 /* Consume the `,'. */
22855 cp_lexer_consume_token (parser->lexer);
22856 }
22857
22858 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22859 base class had a qualified name. However, the next name that
22860 appears is certainly not qualified. */
22861 parser->scope = NULL_TREE;
22862 parser->qualifying_scope = NULL_TREE;
22863 parser->object_scope = NULL_TREE;
22864
22865 return nreverse (bases);
22866 }
22867
22868 /* Parse a base-specifier.
22869
22870 base-specifier:
22871 :: [opt] nested-name-specifier [opt] class-name
22872 virtual access-specifier [opt] :: [opt] nested-name-specifier
22873 [opt] class-name
22874 access-specifier virtual [opt] :: [opt] nested-name-specifier
22875 [opt] class-name
22876
22877 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22878 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22879 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22880 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22881
22882 static tree
22883 cp_parser_base_specifier (cp_parser* parser)
22884 {
22885 cp_token *token;
22886 bool done = false;
22887 bool virtual_p = false;
22888 bool duplicate_virtual_error_issued_p = false;
22889 bool duplicate_access_error_issued_p = false;
22890 bool class_scope_p, template_p;
22891 tree access = access_default_node;
22892 tree type;
22893
22894 /* Process the optional `virtual' and `access-specifier'. */
22895 while (!done)
22896 {
22897 /* Peek at the next token. */
22898 token = cp_lexer_peek_token (parser->lexer);
22899 /* Process `virtual'. */
22900 switch (token->keyword)
22901 {
22902 case RID_VIRTUAL:
22903 /* If `virtual' appears more than once, issue an error. */
22904 if (virtual_p && !duplicate_virtual_error_issued_p)
22905 {
22906 cp_parser_error (parser,
22907 "%<virtual%> specified more than once in base-specified");
22908 duplicate_virtual_error_issued_p = true;
22909 }
22910
22911 virtual_p = true;
22912
22913 /* Consume the `virtual' token. */
22914 cp_lexer_consume_token (parser->lexer);
22915
22916 break;
22917
22918 case RID_PUBLIC:
22919 case RID_PROTECTED:
22920 case RID_PRIVATE:
22921 /* If more than one access specifier appears, issue an
22922 error. */
22923 if (access != access_default_node
22924 && !duplicate_access_error_issued_p)
22925 {
22926 cp_parser_error (parser,
22927 "more than one access specifier in base-specified");
22928 duplicate_access_error_issued_p = true;
22929 }
22930
22931 access = ridpointers[(int) token->keyword];
22932
22933 /* Consume the access-specifier. */
22934 cp_lexer_consume_token (parser->lexer);
22935
22936 break;
22937
22938 default:
22939 done = true;
22940 break;
22941 }
22942 }
22943 /* It is not uncommon to see programs mechanically, erroneously, use
22944 the 'typename' keyword to denote (dependent) qualified types
22945 as base classes. */
22946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22947 {
22948 token = cp_lexer_peek_token (parser->lexer);
22949 if (!processing_template_decl)
22950 error_at (token->location,
22951 "keyword %<typename%> not allowed outside of templates");
22952 else
22953 error_at (token->location,
22954 "keyword %<typename%> not allowed in this context "
22955 "(the base class is implicitly a type)");
22956 cp_lexer_consume_token (parser->lexer);
22957 }
22958
22959 /* Look for the optional `::' operator. */
22960 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22961 /* Look for the nested-name-specifier. The simplest way to
22962 implement:
22963
22964 [temp.res]
22965
22966 The keyword `typename' is not permitted in a base-specifier or
22967 mem-initializer; in these contexts a qualified name that
22968 depends on a template-parameter is implicitly assumed to be a
22969 type name.
22970
22971 is to pretend that we have seen the `typename' keyword at this
22972 point. */
22973 cp_parser_nested_name_specifier_opt (parser,
22974 /*typename_keyword_p=*/true,
22975 /*check_dependency_p=*/true,
22976 typename_type,
22977 /*is_declaration=*/true);
22978 /* If the base class is given by a qualified name, assume that names
22979 we see are type names or templates, as appropriate. */
22980 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22981 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22982
22983 if (!parser->scope
22984 && cp_lexer_next_token_is_decltype (parser->lexer))
22985 /* DR 950 allows decltype as a base-specifier. */
22986 type = cp_parser_decltype (parser);
22987 else
22988 {
22989 /* Otherwise, look for the class-name. */
22990 type = cp_parser_class_name (parser,
22991 class_scope_p,
22992 template_p,
22993 typename_type,
22994 /*check_dependency_p=*/true,
22995 /*class_head_p=*/false,
22996 /*is_declaration=*/true);
22997 type = TREE_TYPE (type);
22998 }
22999
23000 if (type == error_mark_node)
23001 return error_mark_node;
23002
23003 return finish_base_specifier (type, access, virtual_p);
23004 }
23005
23006 /* Exception handling [gram.exception] */
23007
23008 /* Parse an (optional) noexcept-specification.
23009
23010 noexcept-specification:
23011 noexcept ( constant-expression ) [opt]
23012
23013 If no noexcept-specification is present, returns NULL_TREE.
23014 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23015 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23016 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23017 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23018 in which case a boolean condition is returned instead. */
23019
23020 static tree
23021 cp_parser_noexcept_specification_opt (cp_parser* parser,
23022 bool require_constexpr,
23023 bool* consumed_expr,
23024 bool return_cond)
23025 {
23026 cp_token *token;
23027 const char *saved_message;
23028
23029 /* Peek at the next token. */
23030 token = cp_lexer_peek_token (parser->lexer);
23031
23032 /* Is it a noexcept-specification? */
23033 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23034 {
23035 tree expr;
23036 cp_lexer_consume_token (parser->lexer);
23037
23038 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23039 {
23040 cp_lexer_consume_token (parser->lexer);
23041
23042 if (require_constexpr)
23043 {
23044 /* Types may not be defined in an exception-specification. */
23045 saved_message = parser->type_definition_forbidden_message;
23046 parser->type_definition_forbidden_message
23047 = G_("types may not be defined in an exception-specification");
23048
23049 expr = cp_parser_constant_expression (parser);
23050
23051 /* Restore the saved message. */
23052 parser->type_definition_forbidden_message = saved_message;
23053 }
23054 else
23055 {
23056 expr = cp_parser_expression (parser);
23057 *consumed_expr = true;
23058 }
23059
23060 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23061 }
23062 else
23063 {
23064 expr = boolean_true_node;
23065 if (!require_constexpr)
23066 *consumed_expr = false;
23067 }
23068
23069 /* We cannot build a noexcept-spec right away because this will check
23070 that expr is a constexpr. */
23071 if (!return_cond)
23072 return build_noexcept_spec (expr, tf_warning_or_error);
23073 else
23074 return expr;
23075 }
23076 else
23077 return NULL_TREE;
23078 }
23079
23080 /* Parse an (optional) exception-specification.
23081
23082 exception-specification:
23083 throw ( type-id-list [opt] )
23084
23085 Returns a TREE_LIST representing the exception-specification. The
23086 TREE_VALUE of each node is a type. */
23087
23088 static tree
23089 cp_parser_exception_specification_opt (cp_parser* parser)
23090 {
23091 cp_token *token;
23092 tree type_id_list;
23093 const char *saved_message;
23094
23095 /* Peek at the next token. */
23096 token = cp_lexer_peek_token (parser->lexer);
23097
23098 /* Is it a noexcept-specification? */
23099 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23100 false);
23101 if (type_id_list != NULL_TREE)
23102 return type_id_list;
23103
23104 /* If it's not `throw', then there's no exception-specification. */
23105 if (!cp_parser_is_keyword (token, RID_THROW))
23106 return NULL_TREE;
23107
23108 #if 0
23109 /* Enable this once a lot of code has transitioned to noexcept? */
23110 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23111 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23112 "deprecated in C++0x; use %<noexcept%> instead");
23113 #endif
23114
23115 /* Consume the `throw'. */
23116 cp_lexer_consume_token (parser->lexer);
23117
23118 /* Look for the `('. */
23119 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23120
23121 /* Peek at the next token. */
23122 token = cp_lexer_peek_token (parser->lexer);
23123 /* If it's not a `)', then there is a type-id-list. */
23124 if (token->type != CPP_CLOSE_PAREN)
23125 {
23126 /* Types may not be defined in an exception-specification. */
23127 saved_message = parser->type_definition_forbidden_message;
23128 parser->type_definition_forbidden_message
23129 = G_("types may not be defined in an exception-specification");
23130 /* Parse the type-id-list. */
23131 type_id_list = cp_parser_type_id_list (parser);
23132 /* Restore the saved message. */
23133 parser->type_definition_forbidden_message = saved_message;
23134 }
23135 else
23136 type_id_list = empty_except_spec;
23137
23138 /* Look for the `)'. */
23139 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23140
23141 return type_id_list;
23142 }
23143
23144 /* Parse an (optional) type-id-list.
23145
23146 type-id-list:
23147 type-id ... [opt]
23148 type-id-list , type-id ... [opt]
23149
23150 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23151 in the order that the types were presented. */
23152
23153 static tree
23154 cp_parser_type_id_list (cp_parser* parser)
23155 {
23156 tree types = NULL_TREE;
23157
23158 while (true)
23159 {
23160 cp_token *token;
23161 tree type;
23162
23163 token = cp_lexer_peek_token (parser->lexer);
23164
23165 /* Get the next type-id. */
23166 type = cp_parser_type_id (parser);
23167 /* Check for invalid 'auto'. */
23168 if (flag_concepts && type_uses_auto (type))
23169 {
23170 error_at (token->location,
23171 "invalid use of %<auto%> in exception-specification");
23172 type = error_mark_node;
23173 }
23174 /* Parse the optional ellipsis. */
23175 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23176 {
23177 /* Consume the `...'. */
23178 cp_lexer_consume_token (parser->lexer);
23179
23180 /* Turn the type into a pack expansion expression. */
23181 type = make_pack_expansion (type);
23182 }
23183 /* Add it to the list. */
23184 types = add_exception_specifier (types, type, /*complain=*/1);
23185 /* Peek at the next token. */
23186 token = cp_lexer_peek_token (parser->lexer);
23187 /* If it is not a `,', we are done. */
23188 if (token->type != CPP_COMMA)
23189 break;
23190 /* Consume the `,'. */
23191 cp_lexer_consume_token (parser->lexer);
23192 }
23193
23194 return nreverse (types);
23195 }
23196
23197 /* Parse a try-block.
23198
23199 try-block:
23200 try compound-statement handler-seq */
23201
23202 static tree
23203 cp_parser_try_block (cp_parser* parser)
23204 {
23205 tree try_block;
23206
23207 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23208 if (parser->in_function_body
23209 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23210 error ("%<try%> in %<constexpr%> function");
23211
23212 try_block = begin_try_block ();
23213 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23214 finish_try_block (try_block);
23215 cp_parser_handler_seq (parser);
23216 finish_handler_sequence (try_block);
23217
23218 return try_block;
23219 }
23220
23221 /* Parse a function-try-block.
23222
23223 function-try-block:
23224 try ctor-initializer [opt] function-body handler-seq */
23225
23226 static bool
23227 cp_parser_function_try_block (cp_parser* parser)
23228 {
23229 tree compound_stmt;
23230 tree try_block;
23231 bool ctor_initializer_p;
23232
23233 /* Look for the `try' keyword. */
23234 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23235 return false;
23236 /* Let the rest of the front end know where we are. */
23237 try_block = begin_function_try_block (&compound_stmt);
23238 /* Parse the function-body. */
23239 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23240 (parser, /*in_function_try_block=*/true);
23241 /* We're done with the `try' part. */
23242 finish_function_try_block (try_block);
23243 /* Parse the handlers. */
23244 cp_parser_handler_seq (parser);
23245 /* We're done with the handlers. */
23246 finish_function_handler_sequence (try_block, compound_stmt);
23247
23248 return ctor_initializer_p;
23249 }
23250
23251 /* Parse a handler-seq.
23252
23253 handler-seq:
23254 handler handler-seq [opt] */
23255
23256 static void
23257 cp_parser_handler_seq (cp_parser* parser)
23258 {
23259 while (true)
23260 {
23261 cp_token *token;
23262
23263 /* Parse the handler. */
23264 cp_parser_handler (parser);
23265 /* Peek at the next token. */
23266 token = cp_lexer_peek_token (parser->lexer);
23267 /* If it's not `catch' then there are no more handlers. */
23268 if (!cp_parser_is_keyword (token, RID_CATCH))
23269 break;
23270 }
23271 }
23272
23273 /* Parse a handler.
23274
23275 handler:
23276 catch ( exception-declaration ) compound-statement */
23277
23278 static void
23279 cp_parser_handler (cp_parser* parser)
23280 {
23281 tree handler;
23282 tree declaration;
23283
23284 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23285 handler = begin_handler ();
23286 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23287 declaration = cp_parser_exception_declaration (parser);
23288 finish_handler_parms (declaration, handler);
23289 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23290 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23291 finish_handler (handler);
23292 }
23293
23294 /* Parse an exception-declaration.
23295
23296 exception-declaration:
23297 type-specifier-seq declarator
23298 type-specifier-seq abstract-declarator
23299 type-specifier-seq
23300 ...
23301
23302 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23303 ellipsis variant is used. */
23304
23305 static tree
23306 cp_parser_exception_declaration (cp_parser* parser)
23307 {
23308 cp_decl_specifier_seq type_specifiers;
23309 cp_declarator *declarator;
23310 const char *saved_message;
23311
23312 /* If it's an ellipsis, it's easy to handle. */
23313 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23314 {
23315 /* Consume the `...' token. */
23316 cp_lexer_consume_token (parser->lexer);
23317 return NULL_TREE;
23318 }
23319
23320 /* Types may not be defined in exception-declarations. */
23321 saved_message = parser->type_definition_forbidden_message;
23322 parser->type_definition_forbidden_message
23323 = G_("types may not be defined in exception-declarations");
23324
23325 /* Parse the type-specifier-seq. */
23326 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23327 /*is_trailing_return=*/false,
23328 &type_specifiers);
23329 /* If it's a `)', then there is no declarator. */
23330 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23331 declarator = NULL;
23332 else
23333 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23334 /*ctor_dtor_or_conv_p=*/NULL,
23335 /*parenthesized_p=*/NULL,
23336 /*member_p=*/false,
23337 /*friend_p=*/false);
23338
23339 /* Restore the saved message. */
23340 parser->type_definition_forbidden_message = saved_message;
23341
23342 if (!type_specifiers.any_specifiers_p)
23343 return error_mark_node;
23344
23345 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23346 }
23347
23348 /* Parse a throw-expression.
23349
23350 throw-expression:
23351 throw assignment-expression [opt]
23352
23353 Returns a THROW_EXPR representing the throw-expression. */
23354
23355 static tree
23356 cp_parser_throw_expression (cp_parser* parser)
23357 {
23358 tree expression;
23359 cp_token* token;
23360
23361 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23362 token = cp_lexer_peek_token (parser->lexer);
23363 /* Figure out whether or not there is an assignment-expression
23364 following the "throw" keyword. */
23365 if (token->type == CPP_COMMA
23366 || token->type == CPP_SEMICOLON
23367 || token->type == CPP_CLOSE_PAREN
23368 || token->type == CPP_CLOSE_SQUARE
23369 || token->type == CPP_CLOSE_BRACE
23370 || token->type == CPP_COLON)
23371 expression = NULL_TREE;
23372 else
23373 expression = cp_parser_assignment_expression (parser);
23374
23375 return build_throw (expression);
23376 }
23377
23378 /* GNU Extensions */
23379
23380 /* Parse an (optional) asm-specification.
23381
23382 asm-specification:
23383 asm ( string-literal )
23384
23385 If the asm-specification is present, returns a STRING_CST
23386 corresponding to the string-literal. Otherwise, returns
23387 NULL_TREE. */
23388
23389 static tree
23390 cp_parser_asm_specification_opt (cp_parser* parser)
23391 {
23392 cp_token *token;
23393 tree asm_specification;
23394
23395 /* Peek at the next token. */
23396 token = cp_lexer_peek_token (parser->lexer);
23397 /* If the next token isn't the `asm' keyword, then there's no
23398 asm-specification. */
23399 if (!cp_parser_is_keyword (token, RID_ASM))
23400 return NULL_TREE;
23401
23402 /* Consume the `asm' token. */
23403 cp_lexer_consume_token (parser->lexer);
23404 /* Look for the `('. */
23405 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23406
23407 /* Look for the string-literal. */
23408 asm_specification = cp_parser_string_literal (parser, false, false);
23409
23410 /* Look for the `)'. */
23411 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23412
23413 return asm_specification;
23414 }
23415
23416 /* Parse an asm-operand-list.
23417
23418 asm-operand-list:
23419 asm-operand
23420 asm-operand-list , asm-operand
23421
23422 asm-operand:
23423 string-literal ( expression )
23424 [ string-literal ] string-literal ( expression )
23425
23426 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23427 each node is the expression. The TREE_PURPOSE is itself a
23428 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23429 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23430 is a STRING_CST for the string literal before the parenthesis. Returns
23431 ERROR_MARK_NODE if any of the operands are invalid. */
23432
23433 static tree
23434 cp_parser_asm_operand_list (cp_parser* parser)
23435 {
23436 tree asm_operands = NULL_TREE;
23437 bool invalid_operands = false;
23438
23439 while (true)
23440 {
23441 tree string_literal;
23442 tree expression;
23443 tree name;
23444
23445 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23446 {
23447 /* Consume the `[' token. */
23448 cp_lexer_consume_token (parser->lexer);
23449 /* Read the operand name. */
23450 name = cp_parser_identifier (parser);
23451 if (name != error_mark_node)
23452 name = build_string (IDENTIFIER_LENGTH (name),
23453 IDENTIFIER_POINTER (name));
23454 /* Look for the closing `]'. */
23455 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23456 }
23457 else
23458 name = NULL_TREE;
23459 /* Look for the string-literal. */
23460 string_literal = cp_parser_string_literal (parser, false, false);
23461
23462 /* Look for the `('. */
23463 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23464 /* Parse the expression. */
23465 expression = cp_parser_expression (parser);
23466 /* Look for the `)'. */
23467 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23468
23469 if (name == error_mark_node
23470 || string_literal == error_mark_node
23471 || expression == error_mark_node)
23472 invalid_operands = true;
23473
23474 /* Add this operand to the list. */
23475 asm_operands = tree_cons (build_tree_list (name, string_literal),
23476 expression,
23477 asm_operands);
23478 /* If the next token is not a `,', there are no more
23479 operands. */
23480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23481 break;
23482 /* Consume the `,'. */
23483 cp_lexer_consume_token (parser->lexer);
23484 }
23485
23486 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23487 }
23488
23489 /* Parse an asm-clobber-list.
23490
23491 asm-clobber-list:
23492 string-literal
23493 asm-clobber-list , string-literal
23494
23495 Returns a TREE_LIST, indicating the clobbers in the order that they
23496 appeared. The TREE_VALUE of each node is a STRING_CST. */
23497
23498 static tree
23499 cp_parser_asm_clobber_list (cp_parser* parser)
23500 {
23501 tree clobbers = NULL_TREE;
23502
23503 while (true)
23504 {
23505 tree string_literal;
23506
23507 /* Look for the string literal. */
23508 string_literal = cp_parser_string_literal (parser, false, false);
23509 /* Add it to the list. */
23510 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23511 /* If the next token is not a `,', then the list is
23512 complete. */
23513 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23514 break;
23515 /* Consume the `,' token. */
23516 cp_lexer_consume_token (parser->lexer);
23517 }
23518
23519 return clobbers;
23520 }
23521
23522 /* Parse an asm-label-list.
23523
23524 asm-label-list:
23525 identifier
23526 asm-label-list , identifier
23527
23528 Returns a TREE_LIST, indicating the labels in the order that they
23529 appeared. The TREE_VALUE of each node is a label. */
23530
23531 static tree
23532 cp_parser_asm_label_list (cp_parser* parser)
23533 {
23534 tree labels = NULL_TREE;
23535
23536 while (true)
23537 {
23538 tree identifier, label, name;
23539
23540 /* Look for the identifier. */
23541 identifier = cp_parser_identifier (parser);
23542 if (!error_operand_p (identifier))
23543 {
23544 label = lookup_label (identifier);
23545 if (TREE_CODE (label) == LABEL_DECL)
23546 {
23547 TREE_USED (label) = 1;
23548 check_goto (label);
23549 name = build_string (IDENTIFIER_LENGTH (identifier),
23550 IDENTIFIER_POINTER (identifier));
23551 labels = tree_cons (name, label, labels);
23552 }
23553 }
23554 /* If the next token is not a `,', then the list is
23555 complete. */
23556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23557 break;
23558 /* Consume the `,' token. */
23559 cp_lexer_consume_token (parser->lexer);
23560 }
23561
23562 return nreverse (labels);
23563 }
23564
23565 /* Return TRUE iff the next tokens in the stream are possibly the
23566 beginning of a GNU extension attribute. */
23567
23568 static bool
23569 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23570 {
23571 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23572 }
23573
23574 /* Return TRUE iff the next tokens in the stream are possibly the
23575 beginning of a standard C++-11 attribute specifier. */
23576
23577 static bool
23578 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23579 {
23580 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23581 }
23582
23583 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23584 beginning of a standard C++-11 attribute specifier. */
23585
23586 static bool
23587 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23588 {
23589 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23590
23591 return (cxx_dialect >= cxx11
23592 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23593 || (token->type == CPP_OPEN_SQUARE
23594 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23595 && token->type == CPP_OPEN_SQUARE)));
23596 }
23597
23598 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23599 beginning of a GNU extension attribute. */
23600
23601 static bool
23602 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23603 {
23604 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23605
23606 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23607 }
23608
23609 /* Return true iff the next tokens can be the beginning of either a
23610 GNU attribute list, or a standard C++11 attribute sequence. */
23611
23612 static bool
23613 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23614 {
23615 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23616 || cp_next_tokens_can_be_std_attribute_p (parser));
23617 }
23618
23619 /* Return true iff the next Nth tokens can be the beginning of either
23620 a GNU attribute list, or a standard C++11 attribute sequence. */
23621
23622 static bool
23623 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23624 {
23625 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23626 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23627 }
23628
23629 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23630 of GNU attributes, or return NULL. */
23631
23632 static tree
23633 cp_parser_attributes_opt (cp_parser *parser)
23634 {
23635 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23636 return cp_parser_gnu_attributes_opt (parser);
23637 return cp_parser_std_attribute_spec_seq (parser);
23638 }
23639
23640 #define CILK_SIMD_FN_CLAUSE_MASK \
23641 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23642 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23643 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23644 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23645 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23646
23647 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23648 vector [(<clauses>)] */
23649
23650 static void
23651 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23652 {
23653 bool first_p = parser->cilk_simd_fn_info == NULL;
23654 cp_token *token = v_token;
23655 if (first_p)
23656 {
23657 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23658 parser->cilk_simd_fn_info->error_seen = false;
23659 parser->cilk_simd_fn_info->fndecl_seen = false;
23660 parser->cilk_simd_fn_info->tokens = vNULL;
23661 }
23662 int paren_scope = 0;
23663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23664 {
23665 cp_lexer_consume_token (parser->lexer);
23666 v_token = cp_lexer_peek_token (parser->lexer);
23667 paren_scope++;
23668 }
23669 while (paren_scope > 0)
23670 {
23671 token = cp_lexer_peek_token (parser->lexer);
23672 if (token->type == CPP_OPEN_PAREN)
23673 paren_scope++;
23674 else if (token->type == CPP_CLOSE_PAREN)
23675 paren_scope--;
23676 /* Do not push the last ')' */
23677 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23678 cp_lexer_consume_token (parser->lexer);
23679 }
23680
23681 token->type = CPP_PRAGMA_EOL;
23682 parser->lexer->next_token = token;
23683 cp_lexer_consume_token (parser->lexer);
23684
23685 struct cp_token_cache *cp
23686 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23687 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23688 }
23689
23690 /* Parse an (optional) series of attributes.
23691
23692 attributes:
23693 attributes attribute
23694
23695 attribute:
23696 __attribute__ (( attribute-list [opt] ))
23697
23698 The return value is as for cp_parser_gnu_attribute_list. */
23699
23700 static tree
23701 cp_parser_gnu_attributes_opt (cp_parser* parser)
23702 {
23703 tree attributes = NULL_TREE;
23704
23705 while (true)
23706 {
23707 cp_token *token;
23708 tree attribute_list;
23709 bool ok = true;
23710
23711 /* Peek at the next token. */
23712 token = cp_lexer_peek_token (parser->lexer);
23713 /* If it's not `__attribute__', then we're done. */
23714 if (token->keyword != RID_ATTRIBUTE)
23715 break;
23716
23717 /* Consume the `__attribute__' keyword. */
23718 cp_lexer_consume_token (parser->lexer);
23719 /* Look for the two `(' tokens. */
23720 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23721 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23722
23723 /* Peek at the next token. */
23724 token = cp_lexer_peek_token (parser->lexer);
23725 if (token->type != CPP_CLOSE_PAREN)
23726 /* Parse the attribute-list. */
23727 attribute_list = cp_parser_gnu_attribute_list (parser);
23728 else
23729 /* If the next token is a `)', then there is no attribute
23730 list. */
23731 attribute_list = NULL;
23732
23733 /* Look for the two `)' tokens. */
23734 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23735 ok = false;
23736 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23737 ok = false;
23738 if (!ok)
23739 cp_parser_skip_to_end_of_statement (parser);
23740
23741 /* Add these new attributes to the list. */
23742 attributes = chainon (attributes, attribute_list);
23743 }
23744
23745 return attributes;
23746 }
23747
23748 /* Parse a GNU attribute-list.
23749
23750 attribute-list:
23751 attribute
23752 attribute-list , attribute
23753
23754 attribute:
23755 identifier
23756 identifier ( identifier )
23757 identifier ( identifier , expression-list )
23758 identifier ( expression-list )
23759
23760 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23761 to an attribute. The TREE_PURPOSE of each node is the identifier
23762 indicating which attribute is in use. The TREE_VALUE represents
23763 the arguments, if any. */
23764
23765 static tree
23766 cp_parser_gnu_attribute_list (cp_parser* parser)
23767 {
23768 tree attribute_list = NULL_TREE;
23769 bool save_translate_strings_p = parser->translate_strings_p;
23770
23771 parser->translate_strings_p = false;
23772 while (true)
23773 {
23774 cp_token *token;
23775 tree identifier;
23776 tree attribute;
23777
23778 /* Look for the identifier. We also allow keywords here; for
23779 example `__attribute__ ((const))' is legal. */
23780 token = cp_lexer_peek_token (parser->lexer);
23781 if (token->type == CPP_NAME
23782 || token->type == CPP_KEYWORD)
23783 {
23784 tree arguments = NULL_TREE;
23785
23786 /* Consume the token, but save it since we need it for the
23787 SIMD enabled function parsing. */
23788 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23789
23790 /* Save away the identifier that indicates which attribute
23791 this is. */
23792 identifier = (token->type == CPP_KEYWORD)
23793 /* For keywords, use the canonical spelling, not the
23794 parsed identifier. */
23795 ? ridpointers[(int) token->keyword]
23796 : id_token->u.value;
23797
23798 attribute = build_tree_list (identifier, NULL_TREE);
23799
23800 /* Peek at the next token. */
23801 token = cp_lexer_peek_token (parser->lexer);
23802 /* If it's an `(', then parse the attribute arguments. */
23803 if (token->type == CPP_OPEN_PAREN)
23804 {
23805 vec<tree, va_gc> *vec;
23806 int attr_flag = (attribute_takes_identifier_p (identifier)
23807 ? id_attr : normal_attr);
23808 if (is_cilkplus_vector_p (identifier))
23809 {
23810 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23811 continue;
23812 }
23813 else
23814 vec = cp_parser_parenthesized_expression_list
23815 (parser, attr_flag, /*cast_p=*/false,
23816 /*allow_expansion_p=*/false,
23817 /*non_constant_p=*/NULL);
23818 if (vec == NULL)
23819 arguments = error_mark_node;
23820 else
23821 {
23822 arguments = build_tree_list_vec (vec);
23823 release_tree_vector (vec);
23824 }
23825 /* Save the arguments away. */
23826 TREE_VALUE (attribute) = arguments;
23827 }
23828 else if (is_cilkplus_vector_p (identifier))
23829 {
23830 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23831 continue;
23832 }
23833
23834 if (arguments != error_mark_node)
23835 {
23836 /* Add this attribute to the list. */
23837 TREE_CHAIN (attribute) = attribute_list;
23838 attribute_list = attribute;
23839 }
23840
23841 token = cp_lexer_peek_token (parser->lexer);
23842 }
23843 /* Now, look for more attributes. If the next token isn't a
23844 `,', we're done. */
23845 if (token->type != CPP_COMMA)
23846 break;
23847
23848 /* Consume the comma and keep going. */
23849 cp_lexer_consume_token (parser->lexer);
23850 }
23851 parser->translate_strings_p = save_translate_strings_p;
23852
23853 /* We built up the list in reverse order. */
23854 return nreverse (attribute_list);
23855 }
23856
23857 /* Parse a standard C++11 attribute.
23858
23859 The returned representation is a TREE_LIST which TREE_PURPOSE is
23860 the scoped name of the attribute, and the TREE_VALUE is its
23861 arguments list.
23862
23863 Note that the scoped name of the attribute is itself a TREE_LIST
23864 which TREE_PURPOSE is the namespace of the attribute, and
23865 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23866 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23867 and which TREE_PURPOSE is directly the attribute name.
23868
23869 Clients of the attribute code should use get_attribute_namespace
23870 and get_attribute_name to get the actual namespace and name of
23871 attributes, regardless of their being GNU or C++11 attributes.
23872
23873 attribute:
23874 attribute-token attribute-argument-clause [opt]
23875
23876 attribute-token:
23877 identifier
23878 attribute-scoped-token
23879
23880 attribute-scoped-token:
23881 attribute-namespace :: identifier
23882
23883 attribute-namespace:
23884 identifier
23885
23886 attribute-argument-clause:
23887 ( balanced-token-seq )
23888
23889 balanced-token-seq:
23890 balanced-token [opt]
23891 balanced-token-seq balanced-token
23892
23893 balanced-token:
23894 ( balanced-token-seq )
23895 [ balanced-token-seq ]
23896 { balanced-token-seq }. */
23897
23898 static tree
23899 cp_parser_std_attribute (cp_parser *parser)
23900 {
23901 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23902 cp_token *token;
23903
23904 /* First, parse name of the attribute, a.k.a attribute-token. */
23905
23906 token = cp_lexer_peek_token (parser->lexer);
23907 if (token->type == CPP_NAME)
23908 attr_id = token->u.value;
23909 else if (token->type == CPP_KEYWORD)
23910 attr_id = ridpointers[(int) token->keyword];
23911 else if (token->flags & NAMED_OP)
23912 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23913
23914 if (attr_id == NULL_TREE)
23915 return NULL_TREE;
23916
23917 cp_lexer_consume_token (parser->lexer);
23918
23919 token = cp_lexer_peek_token (parser->lexer);
23920 if (token->type == CPP_SCOPE)
23921 {
23922 /* We are seeing a scoped attribute token. */
23923
23924 cp_lexer_consume_token (parser->lexer);
23925 attr_ns = attr_id;
23926
23927 token = cp_lexer_consume_token (parser->lexer);
23928 if (token->type == CPP_NAME)
23929 attr_id = token->u.value;
23930 else if (token->type == CPP_KEYWORD)
23931 attr_id = ridpointers[(int) token->keyword];
23932 else
23933 {
23934 error_at (token->location,
23935 "expected an identifier for the attribute name");
23936 return error_mark_node;
23937 }
23938 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23939 NULL_TREE);
23940 token = cp_lexer_peek_token (parser->lexer);
23941 }
23942 else
23943 {
23944 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23945 NULL_TREE);
23946 /* C++11 noreturn attribute is equivalent to GNU's. */
23947 if (is_attribute_p ("noreturn", attr_id))
23948 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23949 /* C++14 deprecated attribute is equivalent to GNU's. */
23950 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23951 {
23952 if (cxx_dialect == cxx11)
23953 pedwarn (token->location, OPT_Wpedantic,
23954 "%<deprecated%> is a C++14 feature;"
23955 " use %<gnu::deprecated%>");
23956 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23957 }
23958 /* Transactional Memory TS optimize_for_synchronized attribute is
23959 equivalent to GNU transaction_callable. */
23960 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23961 TREE_PURPOSE (attribute)
23962 = get_identifier ("transaction_callable");
23963 /* Transactional Memory attributes are GNU attributes. */
23964 else if (tm_attr_to_mask (attr_id))
23965 TREE_PURPOSE (attribute) = attr_id;
23966 }
23967
23968 /* Now parse the optional argument clause of the attribute. */
23969
23970 if (token->type != CPP_OPEN_PAREN)
23971 return attribute;
23972
23973 {
23974 vec<tree, va_gc> *vec;
23975 int attr_flag = normal_attr;
23976
23977 if (attr_ns == get_identifier ("gnu")
23978 && attribute_takes_identifier_p (attr_id))
23979 /* A GNU attribute that takes an identifier in parameter. */
23980 attr_flag = id_attr;
23981
23982 vec = cp_parser_parenthesized_expression_list
23983 (parser, attr_flag, /*cast_p=*/false,
23984 /*allow_expansion_p=*/true,
23985 /*non_constant_p=*/NULL);
23986 if (vec == NULL)
23987 arguments = error_mark_node;
23988 else
23989 {
23990 arguments = build_tree_list_vec (vec);
23991 release_tree_vector (vec);
23992 }
23993
23994 if (arguments == error_mark_node)
23995 attribute = error_mark_node;
23996 else
23997 TREE_VALUE (attribute) = arguments;
23998 }
23999
24000 return attribute;
24001 }
24002
24003 /* Check that the attribute ATTRIBUTE appears at most once in the
24004 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24005 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24006 isn't implemented yet in GCC. */
24007
24008 static void
24009 cp_parser_check_std_attribute (tree attributes, tree attribute)
24010 {
24011 if (attributes)
24012 {
24013 tree name = get_attribute_name (attribute);
24014 if (is_attribute_p ("noreturn", name)
24015 && lookup_attribute ("noreturn", attributes))
24016 error ("attribute noreturn can appear at most once "
24017 "in an attribute-list");
24018 else if (is_attribute_p ("deprecated", name)
24019 && lookup_attribute ("deprecated", attributes))
24020 error ("attribute deprecated can appear at most once "
24021 "in an attribute-list");
24022 }
24023 }
24024
24025 /* Parse a list of standard C++-11 attributes.
24026
24027 attribute-list:
24028 attribute [opt]
24029 attribute-list , attribute[opt]
24030 attribute ...
24031 attribute-list , attribute ...
24032 */
24033
24034 static tree
24035 cp_parser_std_attribute_list (cp_parser *parser)
24036 {
24037 tree attributes = NULL_TREE, attribute = NULL_TREE;
24038 cp_token *token = NULL;
24039
24040 while (true)
24041 {
24042 attribute = cp_parser_std_attribute (parser);
24043 if (attribute == error_mark_node)
24044 break;
24045 if (attribute != NULL_TREE)
24046 {
24047 cp_parser_check_std_attribute (attributes, attribute);
24048 TREE_CHAIN (attribute) = attributes;
24049 attributes = attribute;
24050 }
24051 token = cp_lexer_peek_token (parser->lexer);
24052 if (token->type == CPP_ELLIPSIS)
24053 {
24054 cp_lexer_consume_token (parser->lexer);
24055 TREE_VALUE (attribute)
24056 = make_pack_expansion (TREE_VALUE (attribute));
24057 token = cp_lexer_peek_token (parser->lexer);
24058 }
24059 if (token->type != CPP_COMMA)
24060 break;
24061 cp_lexer_consume_token (parser->lexer);
24062 }
24063 attributes = nreverse (attributes);
24064 return attributes;
24065 }
24066
24067 /* Parse a standard C++-11 attribute specifier.
24068
24069 attribute-specifier:
24070 [ [ attribute-list ] ]
24071 alignment-specifier
24072
24073 alignment-specifier:
24074 alignas ( type-id ... [opt] )
24075 alignas ( alignment-expression ... [opt] ). */
24076
24077 static tree
24078 cp_parser_std_attribute_spec (cp_parser *parser)
24079 {
24080 tree attributes = NULL_TREE;
24081 cp_token *token = cp_lexer_peek_token (parser->lexer);
24082
24083 if (token->type == CPP_OPEN_SQUARE
24084 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24085 {
24086 cp_lexer_consume_token (parser->lexer);
24087 cp_lexer_consume_token (parser->lexer);
24088
24089 attributes = cp_parser_std_attribute_list (parser);
24090
24091 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24092 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24093 cp_parser_skip_to_end_of_statement (parser);
24094 else
24095 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24096 when we are sure that we have actually parsed them. */
24097 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24098 }
24099 else
24100 {
24101 tree alignas_expr;
24102
24103 /* Look for an alignment-specifier. */
24104
24105 token = cp_lexer_peek_token (parser->lexer);
24106
24107 if (token->type != CPP_KEYWORD
24108 || token->keyword != RID_ALIGNAS)
24109 return NULL_TREE;
24110
24111 cp_lexer_consume_token (parser->lexer);
24112 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24113
24114 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24115 {
24116 cp_parser_error (parser, "expected %<(%>");
24117 return error_mark_node;
24118 }
24119
24120 cp_parser_parse_tentatively (parser);
24121 alignas_expr = cp_parser_type_id (parser);
24122
24123 if (!cp_parser_parse_definitely (parser))
24124 {
24125 gcc_assert (alignas_expr == error_mark_node
24126 || alignas_expr == NULL_TREE);
24127
24128 alignas_expr =
24129 cp_parser_assignment_expression (parser);
24130 if (alignas_expr == error_mark_node)
24131 cp_parser_skip_to_end_of_statement (parser);
24132 if (alignas_expr == NULL_TREE
24133 || alignas_expr == error_mark_node)
24134 return alignas_expr;
24135 }
24136
24137 alignas_expr = cxx_alignas_expr (alignas_expr);
24138 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24139
24140 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24141 {
24142 cp_lexer_consume_token (parser->lexer);
24143 alignas_expr = make_pack_expansion (alignas_expr);
24144 }
24145
24146 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24147 {
24148 cp_parser_error (parser, "expected %<)%>");
24149 return error_mark_node;
24150 }
24151
24152 /* Build the C++-11 representation of an 'aligned'
24153 attribute. */
24154 attributes =
24155 build_tree_list (build_tree_list (get_identifier ("gnu"),
24156 get_identifier ("aligned")),
24157 alignas_expr);
24158 }
24159
24160 return attributes;
24161 }
24162
24163 /* Parse a standard C++-11 attribute-specifier-seq.
24164
24165 attribute-specifier-seq:
24166 attribute-specifier-seq [opt] attribute-specifier
24167 */
24168
24169 static tree
24170 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24171 {
24172 tree attr_specs = NULL_TREE;
24173 tree attr_last = NULL_TREE;
24174
24175 while (true)
24176 {
24177 tree attr_spec = cp_parser_std_attribute_spec (parser);
24178 if (attr_spec == NULL_TREE)
24179 break;
24180 if (attr_spec == error_mark_node)
24181 return error_mark_node;
24182
24183 if (attr_last)
24184 TREE_CHAIN (attr_last) = attr_spec;
24185 else
24186 attr_specs = attr_last = attr_spec;
24187 attr_last = tree_last (attr_last);
24188 }
24189
24190 return attr_specs;
24191 }
24192
24193 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24194 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24195 current value of the PEDANTIC flag, regardless of whether or not
24196 the `__extension__' keyword is present. The caller is responsible
24197 for restoring the value of the PEDANTIC flag. */
24198
24199 static bool
24200 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24201 {
24202 /* Save the old value of the PEDANTIC flag. */
24203 *saved_pedantic = pedantic;
24204
24205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24206 {
24207 /* Consume the `__extension__' token. */
24208 cp_lexer_consume_token (parser->lexer);
24209 /* We're not being pedantic while the `__extension__' keyword is
24210 in effect. */
24211 pedantic = 0;
24212
24213 return true;
24214 }
24215
24216 return false;
24217 }
24218
24219 /* Parse a label declaration.
24220
24221 label-declaration:
24222 __label__ label-declarator-seq ;
24223
24224 label-declarator-seq:
24225 identifier , label-declarator-seq
24226 identifier */
24227
24228 static void
24229 cp_parser_label_declaration (cp_parser* parser)
24230 {
24231 /* Look for the `__label__' keyword. */
24232 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24233
24234 while (true)
24235 {
24236 tree identifier;
24237
24238 /* Look for an identifier. */
24239 identifier = cp_parser_identifier (parser);
24240 /* If we failed, stop. */
24241 if (identifier == error_mark_node)
24242 break;
24243 /* Declare it as a label. */
24244 finish_label_decl (identifier);
24245 /* If the next token is a `;', stop. */
24246 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24247 break;
24248 /* Look for the `,' separating the label declarations. */
24249 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24250 }
24251
24252 /* Look for the final `;'. */
24253 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24254 }
24255
24256 // -------------------------------------------------------------------------- //
24257 // Requires Clause
24258
24259 // Parse a requires clause.
24260 //
24261 // requires-clause:
24262 // 'requires' logical-or-expression
24263 //
24264 // The required logical-or-expression must be a constant expression. Note
24265 // that we don't check that the expression is constepxr here. We defer until
24266 // we analyze constraints and then, we only check atomic constraints.
24267 static tree
24268 cp_parser_requires_clause (cp_parser *parser)
24269 {
24270 // Parse the requires clause so that it is not automatically folded.
24271 ++processing_template_decl;
24272 tree expr = cp_parser_binary_expression (parser, false, false,
24273 PREC_NOT_OPERATOR, NULL);
24274 if (check_for_bare_parameter_packs (expr))
24275 expr = error_mark_node;
24276 --processing_template_decl;
24277 return expr;
24278 }
24279
24280 // Optionally parse a requires clause:
24281 static tree
24282 cp_parser_requires_clause_opt (cp_parser *parser)
24283 {
24284 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24285 if (tok->keyword != RID_REQUIRES)
24286 {
24287 if (!flag_concepts && tok->type == CPP_NAME
24288 && tok->u.value == ridpointers[RID_REQUIRES])
24289 {
24290 error_at (cp_lexer_peek_token (parser->lexer)->location,
24291 "%<requires%> only available with -fconcepts");
24292 /* Parse and discard the requires-clause. */
24293 cp_lexer_consume_token (parser->lexer);
24294 cp_parser_requires_clause (parser);
24295 }
24296 return NULL_TREE;
24297 }
24298 cp_lexer_consume_token (parser->lexer);
24299 return cp_parser_requires_clause (parser);
24300 }
24301
24302
24303 /*---------------------------------------------------------------------------
24304 Requires expressions
24305 ---------------------------------------------------------------------------*/
24306
24307 /* Parse a requires expression
24308
24309 requirement-expression:
24310 'requires' requirement-parameter-list [opt] requirement-body */
24311 static tree
24312 cp_parser_requires_expression (cp_parser *parser)
24313 {
24314 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24315 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24316
24317 /* A requires-expression shall appear only within a concept
24318 definition or a requires-clause.
24319
24320 TODO: Implement this diagnostic correctly. */
24321 if (!processing_template_decl)
24322 {
24323 error_at (loc, "a requires expression cannot appear outside a template");
24324 cp_parser_skip_to_end_of_statement (parser);
24325 return error_mark_node;
24326 }
24327
24328 tree parms, reqs;
24329 {
24330 /* Local parameters are delared as variables within the scope
24331 of the expression. They are not visible past the end of
24332 the expression. Expressions within the requires-expression
24333 are unevaluated. */
24334 struct scope_sentinel
24335 {
24336 scope_sentinel ()
24337 {
24338 ++cp_unevaluated_operand;
24339 begin_scope (sk_block, NULL_TREE);
24340 }
24341
24342 ~scope_sentinel ()
24343 {
24344 pop_bindings_and_leave_scope ();
24345 --cp_unevaluated_operand;
24346 }
24347 } s;
24348
24349 /* Parse the optional parameter list. */
24350 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24351 {
24352 parms = cp_parser_requirement_parameter_list (parser);
24353 if (parms == error_mark_node)
24354 return error_mark_node;
24355 }
24356 else
24357 parms = NULL_TREE;
24358
24359 /* Parse the requirement body. */
24360 reqs = cp_parser_requirement_body (parser);
24361 if (reqs == error_mark_node)
24362 return error_mark_node;
24363 }
24364
24365 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24366 the parm chain. */
24367 grokparms (parms, &parms);
24368 return finish_requires_expr (parms, reqs);
24369 }
24370
24371 /* Parse a parameterized requirement.
24372
24373 requirement-parameter-list:
24374 '(' parameter-declaration-clause ')' */
24375 static tree
24376 cp_parser_requirement_parameter_list (cp_parser *parser)
24377 {
24378 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24379 return error_mark_node;
24380
24381 tree parms = cp_parser_parameter_declaration_clause (parser);
24382
24383 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24384 return error_mark_node;
24385
24386 return parms;
24387 }
24388
24389 /* Parse the body of a requirement.
24390
24391 requirement-body:
24392 '{' requirement-list '}' */
24393 static tree
24394 cp_parser_requirement_body (cp_parser *parser)
24395 {
24396 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24397 return error_mark_node;
24398
24399 tree reqs = cp_parser_requirement_list (parser);
24400
24401 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24402 return error_mark_node;
24403
24404 return reqs;
24405 }
24406
24407 /* Parse a list of requirements.
24408
24409 requirement-list:
24410 requirement
24411 requirement-list ';' requirement[opt] */
24412 static tree
24413 cp_parser_requirement_list (cp_parser *parser)
24414 {
24415 tree result = NULL_TREE;
24416 while (true)
24417 {
24418 tree req = cp_parser_requirement (parser);
24419 if (req == error_mark_node)
24420 return error_mark_node;
24421
24422 result = tree_cons (NULL_TREE, req, result);
24423
24424 /* If we see a semi-colon, consume it. */
24425 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24426 cp_lexer_consume_token (parser->lexer);
24427
24428 /* Stop processing at the end of the list. */
24429 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24430 break;
24431 }
24432
24433 /* Reverse the order of requirements so they are analyzed in
24434 declaration order. */
24435 return nreverse (result);
24436 }
24437
24438 /* Parse a syntactic requirement or type requirement.
24439
24440 requirement:
24441 simple-requirement
24442 compound-requirement
24443 type-requirement
24444 nested-requirement */
24445 static tree
24446 cp_parser_requirement (cp_parser *parser)
24447 {
24448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24449 return cp_parser_compound_requirement (parser);
24450 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24451 return cp_parser_type_requirement (parser);
24452 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24453 return cp_parser_nested_requirement (parser);
24454 else
24455 return cp_parser_simple_requirement (parser);
24456 }
24457
24458 /* Parse a simple requirement.
24459
24460 simple-requirement:
24461 expression ';' */
24462 static tree
24463 cp_parser_simple_requirement (cp_parser *parser)
24464 {
24465 tree expr = cp_parser_expression (parser, NULL, false, false);
24466 if (!expr || expr == error_mark_node)
24467 return error_mark_node;
24468
24469 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24470 return error_mark_node;
24471
24472 return finish_simple_requirement (expr);
24473 }
24474
24475 /* Parse a type requirement
24476
24477 type-requirement
24478 nested-name-specifier [opt] required-type-name ';'
24479
24480 required-type-name:
24481 type-name
24482 'template' [opt] simple-template-id */
24483 static tree
24484 cp_parser_type_requirement (cp_parser *parser)
24485 {
24486 cp_lexer_consume_token (parser->lexer);
24487
24488 // Save the scope before parsing name specifiers.
24489 tree saved_scope = parser->scope;
24490 tree saved_object_scope = parser->object_scope;
24491 tree saved_qualifying_scope = parser->qualifying_scope;
24492 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24493 cp_parser_nested_name_specifier_opt (parser,
24494 /*typename_keyword_p=*/true,
24495 /*check_dependency_p=*/false,
24496 /*type_p=*/true,
24497 /*is_declaration=*/false);
24498
24499 tree type;
24500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24501 {
24502 cp_lexer_consume_token (parser->lexer);
24503 type = cp_parser_template_id (parser,
24504 /*template_keyword_p=*/true,
24505 /*check_dependency=*/false,
24506 /*tag_type=*/none_type,
24507 /*is_declaration=*/false);
24508 type = make_typename_type (parser->scope, type, typename_type,
24509 /*complain=*/tf_error);
24510 }
24511 else
24512 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24513
24514 if (TREE_CODE (type) == TYPE_DECL)
24515 type = TREE_TYPE (type);
24516
24517 parser->scope = saved_scope;
24518 parser->object_scope = saved_object_scope;
24519 parser->qualifying_scope = saved_qualifying_scope;
24520
24521 if (type == error_mark_node)
24522 cp_parser_skip_to_end_of_statement (parser);
24523
24524 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24525 return error_mark_node;
24526 if (type == error_mark_node)
24527 return error_mark_node;
24528
24529 return finish_type_requirement (type);
24530 }
24531
24532 /* Parse a compound requirement
24533
24534 compound-requirement:
24535 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24536 static tree
24537 cp_parser_compound_requirement (cp_parser *parser)
24538 {
24539 /* Parse an expression enclosed in '{ }'s. */
24540 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24541 return error_mark_node;
24542
24543 tree expr = cp_parser_expression (parser, NULL, false, false);
24544 if (!expr || expr == error_mark_node)
24545 return error_mark_node;
24546
24547 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24548 return error_mark_node;
24549
24550 /* Parse the optional noexcept. */
24551 bool noexcept_p = false;
24552 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24553 {
24554 cp_lexer_consume_token (parser->lexer);
24555 noexcept_p = true;
24556 }
24557
24558 /* Parse the optional trailing return type. */
24559 tree type = NULL_TREE;
24560 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24561 {
24562 cp_lexer_consume_token (parser->lexer);
24563 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24564 parser->in_result_type_constraint_p = true;
24565 type = cp_parser_trailing_type_id (parser);
24566 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24567 if (type == error_mark_node)
24568 return error_mark_node;
24569 }
24570
24571 return finish_compound_requirement (expr, type, noexcept_p);
24572 }
24573
24574 /* Parse a nested requirement. This is the same as a requires clause.
24575
24576 nested-requirement:
24577 requires-clause */
24578 static tree
24579 cp_parser_nested_requirement (cp_parser *parser)
24580 {
24581 cp_lexer_consume_token (parser->lexer);
24582 tree req = cp_parser_requires_clause (parser);
24583 if (req == error_mark_node)
24584 return error_mark_node;
24585 return finish_nested_requirement (req);
24586 }
24587
24588 /* Support Functions */
24589
24590 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24591 NAME should have one of the representations used for an
24592 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24593 is returned. If PARSER->SCOPE is a dependent type, then a
24594 SCOPE_REF is returned.
24595
24596 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24597 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24598 was formed. Abstractly, such entities should not be passed to this
24599 function, because they do not need to be looked up, but it is
24600 simpler to check for this special case here, rather than at the
24601 call-sites.
24602
24603 In cases not explicitly covered above, this function returns a
24604 DECL, OVERLOAD, or baselink representing the result of the lookup.
24605 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24606 is returned.
24607
24608 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24609 (e.g., "struct") that was used. In that case bindings that do not
24610 refer to types are ignored.
24611
24612 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24613 ignored.
24614
24615 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24616 are ignored.
24617
24618 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24619 types.
24620
24621 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24622 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24623 NULL_TREE otherwise. */
24624
24625 static cp_expr
24626 cp_parser_lookup_name (cp_parser *parser, tree name,
24627 enum tag_types tag_type,
24628 bool is_template,
24629 bool is_namespace,
24630 bool check_dependency,
24631 tree *ambiguous_decls,
24632 location_t name_location)
24633 {
24634 tree decl;
24635 tree object_type = parser->context->object_type;
24636
24637 /* Assume that the lookup will be unambiguous. */
24638 if (ambiguous_decls)
24639 *ambiguous_decls = NULL_TREE;
24640
24641 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24642 no longer valid. Note that if we are parsing tentatively, and
24643 the parse fails, OBJECT_TYPE will be automatically restored. */
24644 parser->context->object_type = NULL_TREE;
24645
24646 if (name == error_mark_node)
24647 return error_mark_node;
24648
24649 /* A template-id has already been resolved; there is no lookup to
24650 do. */
24651 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24652 return name;
24653 if (BASELINK_P (name))
24654 {
24655 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24656 == TEMPLATE_ID_EXPR);
24657 return name;
24658 }
24659
24660 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24661 it should already have been checked to make sure that the name
24662 used matches the type being destroyed. */
24663 if (TREE_CODE (name) == BIT_NOT_EXPR)
24664 {
24665 tree type;
24666
24667 /* Figure out to which type this destructor applies. */
24668 if (parser->scope)
24669 type = parser->scope;
24670 else if (object_type)
24671 type = object_type;
24672 else
24673 type = current_class_type;
24674 /* If that's not a class type, there is no destructor. */
24675 if (!type || !CLASS_TYPE_P (type))
24676 return error_mark_node;
24677 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24678 lazily_declare_fn (sfk_destructor, type);
24679 if (!CLASSTYPE_DESTRUCTORS (type))
24680 return error_mark_node;
24681 /* If it was a class type, return the destructor. */
24682 return CLASSTYPE_DESTRUCTORS (type);
24683 }
24684
24685 /* By this point, the NAME should be an ordinary identifier. If
24686 the id-expression was a qualified name, the qualifying scope is
24687 stored in PARSER->SCOPE at this point. */
24688 gcc_assert (identifier_p (name));
24689
24690 /* Perform the lookup. */
24691 if (parser->scope)
24692 {
24693 bool dependent_p;
24694
24695 if (parser->scope == error_mark_node)
24696 return error_mark_node;
24697
24698 /* If the SCOPE is dependent, the lookup must be deferred until
24699 the template is instantiated -- unless we are explicitly
24700 looking up names in uninstantiated templates. Even then, we
24701 cannot look up the name if the scope is not a class type; it
24702 might, for example, be a template type parameter. */
24703 dependent_p = (TYPE_P (parser->scope)
24704 && dependent_scope_p (parser->scope));
24705 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24706 && dependent_p)
24707 /* Defer lookup. */
24708 decl = error_mark_node;
24709 else
24710 {
24711 tree pushed_scope = NULL_TREE;
24712
24713 /* If PARSER->SCOPE is a dependent type, then it must be a
24714 class type, and we must not be checking dependencies;
24715 otherwise, we would have processed this lookup above. So
24716 that PARSER->SCOPE is not considered a dependent base by
24717 lookup_member, we must enter the scope here. */
24718 if (dependent_p)
24719 pushed_scope = push_scope (parser->scope);
24720
24721 /* If the PARSER->SCOPE is a template specialization, it
24722 may be instantiated during name lookup. In that case,
24723 errors may be issued. Even if we rollback the current
24724 tentative parse, those errors are valid. */
24725 decl = lookup_qualified_name (parser->scope, name,
24726 tag_type != none_type,
24727 /*complain=*/true);
24728
24729 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24730 lookup result and the nested-name-specifier nominates a class C:
24731 * if the name specified after the nested-name-specifier, when
24732 looked up in C, is the injected-class-name of C (Clause 9), or
24733 * if the name specified after the nested-name-specifier is the
24734 same as the identifier or the simple-template-id's template-
24735 name in the last component of the nested-name-specifier,
24736 the name is instead considered to name the constructor of
24737 class C. [ Note: for example, the constructor is not an
24738 acceptable lookup result in an elaborated-type-specifier so
24739 the constructor would not be used in place of the
24740 injected-class-name. --end note ] Such a constructor name
24741 shall be used only in the declarator-id of a declaration that
24742 names a constructor or in a using-declaration. */
24743 if (tag_type == none_type
24744 && DECL_SELF_REFERENCE_P (decl)
24745 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24746 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24747 tag_type != none_type,
24748 /*complain=*/true);
24749
24750 /* If we have a single function from a using decl, pull it out. */
24751 if (TREE_CODE (decl) == OVERLOAD
24752 && !really_overloaded_fn (decl))
24753 decl = OVL_FUNCTION (decl);
24754
24755 if (pushed_scope)
24756 pop_scope (pushed_scope);
24757 }
24758
24759 /* If the scope is a dependent type and either we deferred lookup or
24760 we did lookup but didn't find the name, rememeber the name. */
24761 if (decl == error_mark_node && TYPE_P (parser->scope)
24762 && dependent_type_p (parser->scope))
24763 {
24764 if (tag_type)
24765 {
24766 tree type;
24767
24768 /* The resolution to Core Issue 180 says that `struct
24769 A::B' should be considered a type-name, even if `A'
24770 is dependent. */
24771 type = make_typename_type (parser->scope, name, tag_type,
24772 /*complain=*/tf_error);
24773 if (type != error_mark_node)
24774 decl = TYPE_NAME (type);
24775 }
24776 else if (is_template
24777 && (cp_parser_next_token_ends_template_argument_p (parser)
24778 || cp_lexer_next_token_is (parser->lexer,
24779 CPP_CLOSE_PAREN)))
24780 decl = make_unbound_class_template (parser->scope,
24781 name, NULL_TREE,
24782 /*complain=*/tf_error);
24783 else
24784 decl = build_qualified_name (/*type=*/NULL_TREE,
24785 parser->scope, name,
24786 is_template);
24787 }
24788 parser->qualifying_scope = parser->scope;
24789 parser->object_scope = NULL_TREE;
24790 }
24791 else if (object_type)
24792 {
24793 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24794 OBJECT_TYPE is not a class. */
24795 if (CLASS_TYPE_P (object_type))
24796 /* If the OBJECT_TYPE is a template specialization, it may
24797 be instantiated during name lookup. In that case, errors
24798 may be issued. Even if we rollback the current tentative
24799 parse, those errors are valid. */
24800 decl = lookup_member (object_type,
24801 name,
24802 /*protect=*/0,
24803 tag_type != none_type,
24804 tf_warning_or_error);
24805 else
24806 decl = NULL_TREE;
24807
24808 if (!decl)
24809 {
24810 /* Look it up in the enclosing context. */
24811 decl = lookup_name_real (name, tag_type != none_type,
24812 /*nonclass=*/0,
24813 /*block_p=*/true, is_namespace, 0);
24814 /* DR 141 says when looking for a template-name after -> or ., only
24815 consider class templates. We need to fix our handling of
24816 dependent expressions to implement that properly, but for now
24817 let's ignore namespace-scope function templates. */
24818 if (decl && is_template && !DECL_TYPE_TEMPLATE_P (decl))
24819 {
24820 tree d = decl;
24821 if (is_overloaded_fn (d))
24822 d = get_first_fn (d);
24823 if (DECL_P (d) && !DECL_CLASS_SCOPE_P (d))
24824 decl = NULL_TREE;
24825 }
24826 }
24827 if (object_type == unknown_type_node)
24828 /* The object is type-dependent, so we can't look anything up; we used
24829 this to get the DR 141 behavior. */
24830 object_type = NULL_TREE;
24831 parser->object_scope = object_type;
24832 parser->qualifying_scope = NULL_TREE;
24833 }
24834 else
24835 {
24836 decl = lookup_name_real (name, tag_type != none_type,
24837 /*nonclass=*/0,
24838 /*block_p=*/true, is_namespace, 0);
24839 parser->qualifying_scope = NULL_TREE;
24840 parser->object_scope = NULL_TREE;
24841 }
24842
24843 /* If the lookup failed, let our caller know. */
24844 if (!decl || decl == error_mark_node)
24845 return error_mark_node;
24846
24847 /* Pull out the template from an injected-class-name (or multiple). */
24848 if (is_template)
24849 decl = maybe_get_template_decl_from_type_decl (decl);
24850
24851 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24852 if (TREE_CODE (decl) == TREE_LIST)
24853 {
24854 if (ambiguous_decls)
24855 *ambiguous_decls = decl;
24856 /* The error message we have to print is too complicated for
24857 cp_parser_error, so we incorporate its actions directly. */
24858 if (!cp_parser_simulate_error (parser))
24859 {
24860 error_at (name_location, "reference to %qD is ambiguous",
24861 name);
24862 print_candidates (decl);
24863 }
24864 return error_mark_node;
24865 }
24866
24867 gcc_assert (DECL_P (decl)
24868 || TREE_CODE (decl) == OVERLOAD
24869 || TREE_CODE (decl) == SCOPE_REF
24870 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24871 || BASELINK_P (decl));
24872
24873 /* If we have resolved the name of a member declaration, check to
24874 see if the declaration is accessible. When the name resolves to
24875 set of overloaded functions, accessibility is checked when
24876 overload resolution is done.
24877
24878 During an explicit instantiation, access is not checked at all,
24879 as per [temp.explicit]. */
24880 if (DECL_P (decl))
24881 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24882
24883 maybe_record_typedef_use (decl);
24884
24885 return cp_expr (decl, name_location);
24886 }
24887
24888 /* Like cp_parser_lookup_name, but for use in the typical case where
24889 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24890 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24891
24892 static tree
24893 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24894 {
24895 return cp_parser_lookup_name (parser, name,
24896 none_type,
24897 /*is_template=*/false,
24898 /*is_namespace=*/false,
24899 /*check_dependency=*/true,
24900 /*ambiguous_decls=*/NULL,
24901 location);
24902 }
24903
24904 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24905 the current context, return the TYPE_DECL. If TAG_NAME_P is
24906 true, the DECL indicates the class being defined in a class-head,
24907 or declared in an elaborated-type-specifier.
24908
24909 Otherwise, return DECL. */
24910
24911 static tree
24912 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24913 {
24914 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24915 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24916
24917 struct A {
24918 template <typename T> struct B;
24919 };
24920
24921 template <typename T> struct A::B {};
24922
24923 Similarly, in an elaborated-type-specifier:
24924
24925 namespace N { struct X{}; }
24926
24927 struct A {
24928 template <typename T> friend struct N::X;
24929 };
24930
24931 However, if the DECL refers to a class type, and we are in
24932 the scope of the class, then the name lookup automatically
24933 finds the TYPE_DECL created by build_self_reference rather
24934 than a TEMPLATE_DECL. For example, in:
24935
24936 template <class T> struct S {
24937 S s;
24938 };
24939
24940 there is no need to handle such case. */
24941
24942 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24943 return DECL_TEMPLATE_RESULT (decl);
24944
24945 return decl;
24946 }
24947
24948 /* If too many, or too few, template-parameter lists apply to the
24949 declarator, issue an error message. Returns TRUE if all went well,
24950 and FALSE otherwise. */
24951
24952 static bool
24953 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24954 cp_declarator *declarator,
24955 location_t declarator_location)
24956 {
24957 switch (declarator->kind)
24958 {
24959 case cdk_id:
24960 {
24961 unsigned num_templates = 0;
24962 tree scope = declarator->u.id.qualifying_scope;
24963
24964 if (scope)
24965 num_templates = num_template_headers_for_class (scope);
24966 else if (TREE_CODE (declarator->u.id.unqualified_name)
24967 == TEMPLATE_ID_EXPR)
24968 /* If the DECLARATOR has the form `X<y>' then it uses one
24969 additional level of template parameters. */
24970 ++num_templates;
24971
24972 return cp_parser_check_template_parameters
24973 (parser, num_templates, declarator_location, declarator);
24974 }
24975
24976 case cdk_function:
24977 case cdk_array:
24978 case cdk_pointer:
24979 case cdk_reference:
24980 case cdk_ptrmem:
24981 return (cp_parser_check_declarator_template_parameters
24982 (parser, declarator->declarator, declarator_location));
24983
24984 case cdk_error:
24985 return true;
24986
24987 default:
24988 gcc_unreachable ();
24989 }
24990 return false;
24991 }
24992
24993 /* NUM_TEMPLATES were used in the current declaration. If that is
24994 invalid, return FALSE and issue an error messages. Otherwise,
24995 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24996 declarator and we can print more accurate diagnostics. */
24997
24998 static bool
24999 cp_parser_check_template_parameters (cp_parser* parser,
25000 unsigned num_templates,
25001 location_t location,
25002 cp_declarator *declarator)
25003 {
25004 /* If there are the same number of template classes and parameter
25005 lists, that's OK. */
25006 if (parser->num_template_parameter_lists == num_templates)
25007 return true;
25008 /* If there are more, but only one more, then we are referring to a
25009 member template. That's OK too. */
25010 if (parser->num_template_parameter_lists == num_templates + 1)
25011 return true;
25012 /* If there are more template classes than parameter lists, we have
25013 something like:
25014
25015 template <class T> void S<T>::R<T>::f (); */
25016 if (parser->num_template_parameter_lists < num_templates)
25017 {
25018 if (declarator && !current_function_decl)
25019 error_at (location, "specializing member %<%T::%E%> "
25020 "requires %<template<>%> syntax",
25021 declarator->u.id.qualifying_scope,
25022 declarator->u.id.unqualified_name);
25023 else if (declarator)
25024 error_at (location, "invalid declaration of %<%T::%E%>",
25025 declarator->u.id.qualifying_scope,
25026 declarator->u.id.unqualified_name);
25027 else
25028 error_at (location, "too few template-parameter-lists");
25029 return false;
25030 }
25031 /* Otherwise, there are too many template parameter lists. We have
25032 something like:
25033
25034 template <class T> template <class U> void S::f(); */
25035 error_at (location, "too many template-parameter-lists");
25036 return false;
25037 }
25038
25039 /* Parse an optional `::' token indicating that the following name is
25040 from the global namespace. If so, PARSER->SCOPE is set to the
25041 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25042 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25043 Returns the new value of PARSER->SCOPE, if the `::' token is
25044 present, and NULL_TREE otherwise. */
25045
25046 static tree
25047 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25048 {
25049 cp_token *token;
25050
25051 /* Peek at the next token. */
25052 token = cp_lexer_peek_token (parser->lexer);
25053 /* If we're looking at a `::' token then we're starting from the
25054 global namespace, not our current location. */
25055 if (token->type == CPP_SCOPE)
25056 {
25057 /* Consume the `::' token. */
25058 cp_lexer_consume_token (parser->lexer);
25059 /* Set the SCOPE so that we know where to start the lookup. */
25060 parser->scope = global_namespace;
25061 parser->qualifying_scope = global_namespace;
25062 parser->object_scope = NULL_TREE;
25063
25064 return parser->scope;
25065 }
25066 else if (!current_scope_valid_p)
25067 {
25068 parser->scope = NULL_TREE;
25069 parser->qualifying_scope = NULL_TREE;
25070 parser->object_scope = NULL_TREE;
25071 }
25072
25073 return NULL_TREE;
25074 }
25075
25076 /* Returns TRUE if the upcoming token sequence is the start of a
25077 constructor declarator. If FRIEND_P is true, the declarator is
25078 preceded by the `friend' specifier. */
25079
25080 static bool
25081 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25082 {
25083 bool constructor_p;
25084 bool outside_class_specifier_p;
25085 tree nested_name_specifier;
25086 cp_token *next_token;
25087
25088 /* The common case is that this is not a constructor declarator, so
25089 try to avoid doing lots of work if at all possible. It's not
25090 valid declare a constructor at function scope. */
25091 if (parser->in_function_body)
25092 return false;
25093 /* And only certain tokens can begin a constructor declarator. */
25094 next_token = cp_lexer_peek_token (parser->lexer);
25095 if (next_token->type != CPP_NAME
25096 && next_token->type != CPP_SCOPE
25097 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25098 && next_token->type != CPP_TEMPLATE_ID)
25099 return false;
25100
25101 /* Parse tentatively; we are going to roll back all of the tokens
25102 consumed here. */
25103 cp_parser_parse_tentatively (parser);
25104 /* Assume that we are looking at a constructor declarator. */
25105 constructor_p = true;
25106
25107 /* Look for the optional `::' operator. */
25108 cp_parser_global_scope_opt (parser,
25109 /*current_scope_valid_p=*/false);
25110 /* Look for the nested-name-specifier. */
25111 nested_name_specifier
25112 = (cp_parser_nested_name_specifier_opt (parser,
25113 /*typename_keyword_p=*/false,
25114 /*check_dependency_p=*/false,
25115 /*type_p=*/false,
25116 /*is_declaration=*/false));
25117
25118 outside_class_specifier_p = (!at_class_scope_p ()
25119 || !TYPE_BEING_DEFINED (current_class_type)
25120 || friend_p);
25121
25122 /* Outside of a class-specifier, there must be a
25123 nested-name-specifier. */
25124 if (!nested_name_specifier && outside_class_specifier_p)
25125 constructor_p = false;
25126 else if (nested_name_specifier == error_mark_node)
25127 constructor_p = false;
25128
25129 /* If we have a class scope, this is easy; DR 147 says that S::S always
25130 names the constructor, and no other qualified name could. */
25131 if (constructor_p && nested_name_specifier
25132 && CLASS_TYPE_P (nested_name_specifier))
25133 {
25134 tree id = cp_parser_unqualified_id (parser,
25135 /*template_keyword_p=*/false,
25136 /*check_dependency_p=*/false,
25137 /*declarator_p=*/true,
25138 /*optional_p=*/false);
25139 if (is_overloaded_fn (id))
25140 id = DECL_NAME (get_first_fn (id));
25141 if (!constructor_name_p (id, nested_name_specifier))
25142 constructor_p = false;
25143 }
25144 /* If we still think that this might be a constructor-declarator,
25145 look for a class-name. */
25146 else if (constructor_p)
25147 {
25148 /* If we have:
25149
25150 template <typename T> struct S {
25151 S();
25152 };
25153
25154 we must recognize that the nested `S' names a class. */
25155 tree type_decl;
25156 type_decl = cp_parser_class_name (parser,
25157 /*typename_keyword_p=*/false,
25158 /*template_keyword_p=*/false,
25159 none_type,
25160 /*check_dependency_p=*/false,
25161 /*class_head_p=*/false,
25162 /*is_declaration=*/false);
25163 /* If there was no class-name, then this is not a constructor.
25164 Otherwise, if we are in a class-specifier and we aren't
25165 handling a friend declaration, check that its type matches
25166 current_class_type (c++/38313). Note: error_mark_node
25167 is left alone for error recovery purposes. */
25168 constructor_p = (!cp_parser_error_occurred (parser)
25169 && (outside_class_specifier_p
25170 || type_decl == error_mark_node
25171 || same_type_p (current_class_type,
25172 TREE_TYPE (type_decl))));
25173
25174 /* If we're still considering a constructor, we have to see a `(',
25175 to begin the parameter-declaration-clause, followed by either a
25176 `)', an `...', or a decl-specifier. We need to check for a
25177 type-specifier to avoid being fooled into thinking that:
25178
25179 S (f) (int);
25180
25181 is a constructor. (It is actually a function named `f' that
25182 takes one parameter (of type `int') and returns a value of type
25183 `S'. */
25184 if (constructor_p
25185 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25186 constructor_p = false;
25187
25188 if (constructor_p
25189 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25190 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25191 /* A parameter declaration begins with a decl-specifier,
25192 which is either the "attribute" keyword, a storage class
25193 specifier, or (usually) a type-specifier. */
25194 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25195 {
25196 tree type;
25197 tree pushed_scope = NULL_TREE;
25198 unsigned saved_num_template_parameter_lists;
25199
25200 /* Names appearing in the type-specifier should be looked up
25201 in the scope of the class. */
25202 if (current_class_type)
25203 type = NULL_TREE;
25204 else
25205 {
25206 type = TREE_TYPE (type_decl);
25207 if (TREE_CODE (type) == TYPENAME_TYPE)
25208 {
25209 type = resolve_typename_type (type,
25210 /*only_current_p=*/false);
25211 if (TREE_CODE (type) == TYPENAME_TYPE)
25212 {
25213 cp_parser_abort_tentative_parse (parser);
25214 return false;
25215 }
25216 }
25217 pushed_scope = push_scope (type);
25218 }
25219
25220 /* Inside the constructor parameter list, surrounding
25221 template-parameter-lists do not apply. */
25222 saved_num_template_parameter_lists
25223 = parser->num_template_parameter_lists;
25224 parser->num_template_parameter_lists = 0;
25225
25226 /* Look for the type-specifier. */
25227 cp_parser_type_specifier (parser,
25228 CP_PARSER_FLAGS_NONE,
25229 /*decl_specs=*/NULL,
25230 /*is_declarator=*/true,
25231 /*declares_class_or_enum=*/NULL,
25232 /*is_cv_qualifier=*/NULL);
25233
25234 parser->num_template_parameter_lists
25235 = saved_num_template_parameter_lists;
25236
25237 /* Leave the scope of the class. */
25238 if (pushed_scope)
25239 pop_scope (pushed_scope);
25240
25241 constructor_p = !cp_parser_error_occurred (parser);
25242 }
25243 }
25244
25245 /* We did not really want to consume any tokens. */
25246 cp_parser_abort_tentative_parse (parser);
25247
25248 return constructor_p;
25249 }
25250
25251 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25252 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25253 they must be performed once we are in the scope of the function.
25254
25255 Returns the function defined. */
25256
25257 static tree
25258 cp_parser_function_definition_from_specifiers_and_declarator
25259 (cp_parser* parser,
25260 cp_decl_specifier_seq *decl_specifiers,
25261 tree attributes,
25262 const cp_declarator *declarator)
25263 {
25264 tree fn;
25265 bool success_p;
25266
25267 /* Begin the function-definition. */
25268 success_p = start_function (decl_specifiers, declarator, attributes);
25269
25270 /* The things we're about to see are not directly qualified by any
25271 template headers we've seen thus far. */
25272 reset_specialization ();
25273
25274 /* If there were names looked up in the decl-specifier-seq that we
25275 did not check, check them now. We must wait until we are in the
25276 scope of the function to perform the checks, since the function
25277 might be a friend. */
25278 perform_deferred_access_checks (tf_warning_or_error);
25279
25280 if (success_p)
25281 {
25282 cp_finalize_omp_declare_simd (parser, current_function_decl);
25283 parser->omp_declare_simd = NULL;
25284 cp_finalize_oacc_routine (parser, current_function_decl, true);
25285 parser->oacc_routine = NULL;
25286 }
25287
25288 if (!success_p)
25289 {
25290 /* Skip the entire function. */
25291 cp_parser_skip_to_end_of_block_or_statement (parser);
25292 fn = error_mark_node;
25293 }
25294 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25295 {
25296 /* Seen already, skip it. An error message has already been output. */
25297 cp_parser_skip_to_end_of_block_or_statement (parser);
25298 fn = current_function_decl;
25299 current_function_decl = NULL_TREE;
25300 /* If this is a function from a class, pop the nested class. */
25301 if (current_class_name)
25302 pop_nested_class ();
25303 }
25304 else
25305 {
25306 timevar_id_t tv;
25307 if (DECL_DECLARED_INLINE_P (current_function_decl))
25308 tv = TV_PARSE_INLINE;
25309 else
25310 tv = TV_PARSE_FUNC;
25311 timevar_push (tv);
25312 fn = cp_parser_function_definition_after_declarator (parser,
25313 /*inline_p=*/false);
25314 timevar_pop (tv);
25315 }
25316
25317 return fn;
25318 }
25319
25320 /* Parse the part of a function-definition that follows the
25321 declarator. INLINE_P is TRUE iff this function is an inline
25322 function defined within a class-specifier.
25323
25324 Returns the function defined. */
25325
25326 static tree
25327 cp_parser_function_definition_after_declarator (cp_parser* parser,
25328 bool inline_p)
25329 {
25330 tree fn;
25331 bool ctor_initializer_p = false;
25332 bool saved_in_unbraced_linkage_specification_p;
25333 bool saved_in_function_body;
25334 unsigned saved_num_template_parameter_lists;
25335 cp_token *token;
25336 bool fully_implicit_function_template_p
25337 = parser->fully_implicit_function_template_p;
25338 parser->fully_implicit_function_template_p = false;
25339 tree implicit_template_parms
25340 = parser->implicit_template_parms;
25341 parser->implicit_template_parms = 0;
25342 cp_binding_level* implicit_template_scope
25343 = parser->implicit_template_scope;
25344 parser->implicit_template_scope = 0;
25345
25346 saved_in_function_body = parser->in_function_body;
25347 parser->in_function_body = true;
25348 /* If the next token is `return', then the code may be trying to
25349 make use of the "named return value" extension that G++ used to
25350 support. */
25351 token = cp_lexer_peek_token (parser->lexer);
25352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25353 {
25354 /* Consume the `return' keyword. */
25355 cp_lexer_consume_token (parser->lexer);
25356 /* Look for the identifier that indicates what value is to be
25357 returned. */
25358 cp_parser_identifier (parser);
25359 /* Issue an error message. */
25360 error_at (token->location,
25361 "named return values are no longer supported");
25362 /* Skip tokens until we reach the start of the function body. */
25363 while (true)
25364 {
25365 cp_token *token = cp_lexer_peek_token (parser->lexer);
25366 if (token->type == CPP_OPEN_BRACE
25367 || token->type == CPP_EOF
25368 || token->type == CPP_PRAGMA_EOL)
25369 break;
25370 cp_lexer_consume_token (parser->lexer);
25371 }
25372 }
25373 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25374 anything declared inside `f'. */
25375 saved_in_unbraced_linkage_specification_p
25376 = parser->in_unbraced_linkage_specification_p;
25377 parser->in_unbraced_linkage_specification_p = false;
25378 /* Inside the function, surrounding template-parameter-lists do not
25379 apply. */
25380 saved_num_template_parameter_lists
25381 = parser->num_template_parameter_lists;
25382 parser->num_template_parameter_lists = 0;
25383
25384 start_lambda_scope (current_function_decl);
25385
25386 /* If the next token is `try', `__transaction_atomic', or
25387 `__transaction_relaxed`, then we are looking at either function-try-block
25388 or function-transaction-block. Note that all of these include the
25389 function-body. */
25390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25391 ctor_initializer_p = cp_parser_function_transaction (parser,
25392 RID_TRANSACTION_ATOMIC);
25393 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25394 RID_TRANSACTION_RELAXED))
25395 ctor_initializer_p = cp_parser_function_transaction (parser,
25396 RID_TRANSACTION_RELAXED);
25397 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25398 ctor_initializer_p = cp_parser_function_try_block (parser);
25399 else
25400 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25401 (parser, /*in_function_try_block=*/false);
25402
25403 finish_lambda_scope ();
25404
25405 /* Finish the function. */
25406 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25407 (inline_p ? 2 : 0));
25408 /* Generate code for it, if necessary. */
25409 expand_or_defer_fn (fn);
25410 /* Restore the saved values. */
25411 parser->in_unbraced_linkage_specification_p
25412 = saved_in_unbraced_linkage_specification_p;
25413 parser->num_template_parameter_lists
25414 = saved_num_template_parameter_lists;
25415 parser->in_function_body = saved_in_function_body;
25416
25417 parser->fully_implicit_function_template_p
25418 = fully_implicit_function_template_p;
25419 parser->implicit_template_parms
25420 = implicit_template_parms;
25421 parser->implicit_template_scope
25422 = implicit_template_scope;
25423
25424 if (parser->fully_implicit_function_template_p)
25425 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25426
25427 return fn;
25428 }
25429
25430 /* Parse a template-declaration body (following argument list). */
25431
25432 static void
25433 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25434 tree parameter_list,
25435 bool member_p)
25436 {
25437 tree decl = NULL_TREE;
25438 bool friend_p = false;
25439
25440 /* We just processed one more parameter list. */
25441 ++parser->num_template_parameter_lists;
25442
25443 /* Get the deferred access checks from the parameter list. These
25444 will be checked once we know what is being declared, as for a
25445 member template the checks must be performed in the scope of the
25446 class containing the member. */
25447 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25448
25449 /* Tentatively parse for a new template parameter list, which can either be
25450 the template keyword or a template introduction. */
25451 if (cp_parser_template_declaration_after_export (parser, member_p))
25452 /* OK */;
25453 else if (cxx_dialect >= cxx11
25454 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25455 decl = cp_parser_alias_declaration (parser);
25456 else
25457 {
25458 /* There are no access checks when parsing a template, as we do not
25459 know if a specialization will be a friend. */
25460 push_deferring_access_checks (dk_no_check);
25461 cp_token *token = cp_lexer_peek_token (parser->lexer);
25462 decl = cp_parser_single_declaration (parser,
25463 checks,
25464 member_p,
25465 /*explicit_specialization_p=*/false,
25466 &friend_p);
25467 pop_deferring_access_checks ();
25468
25469 /* If this is a member template declaration, let the front
25470 end know. */
25471 if (member_p && !friend_p && decl)
25472 {
25473 if (TREE_CODE (decl) == TYPE_DECL)
25474 cp_parser_check_access_in_redeclaration (decl, token->location);
25475
25476 decl = finish_member_template_decl (decl);
25477 }
25478 else if (friend_p && decl
25479 && DECL_DECLARES_TYPE_P (decl))
25480 make_friend_class (current_class_type, TREE_TYPE (decl),
25481 /*complain=*/true);
25482 }
25483 /* We are done with the current parameter list. */
25484 --parser->num_template_parameter_lists;
25485
25486 pop_deferring_access_checks ();
25487
25488 /* Finish up. */
25489 finish_template_decl (parameter_list);
25490
25491 /* Check the template arguments for a literal operator template. */
25492 if (decl
25493 && DECL_DECLARES_FUNCTION_P (decl)
25494 && UDLIT_OPER_P (DECL_NAME (decl)))
25495 {
25496 bool ok = true;
25497 if (parameter_list == NULL_TREE)
25498 ok = false;
25499 else
25500 {
25501 int num_parms = TREE_VEC_LENGTH (parameter_list);
25502 if (num_parms == 1)
25503 {
25504 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25505 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25506 if (TREE_TYPE (parm) != char_type_node
25507 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25508 ok = false;
25509 }
25510 else if (num_parms == 2 && cxx_dialect >= cxx14)
25511 {
25512 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25513 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25514 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25515 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25516 if (TREE_TYPE (parm) != TREE_TYPE (type)
25517 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25518 ok = false;
25519 }
25520 else
25521 ok = false;
25522 }
25523 if (!ok)
25524 {
25525 if (cxx_dialect >= cxx14)
25526 error ("literal operator template %qD has invalid parameter list."
25527 " Expected non-type template argument pack <char...>"
25528 " or <typename CharT, CharT...>",
25529 decl);
25530 else
25531 error ("literal operator template %qD has invalid parameter list."
25532 " Expected non-type template argument pack <char...>",
25533 decl);
25534 }
25535 }
25536
25537 /* Register member declarations. */
25538 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25539 finish_member_declaration (decl);
25540 /* If DECL is a function template, we must return to parse it later.
25541 (Even though there is no definition, there might be default
25542 arguments that need handling.) */
25543 if (member_p && decl
25544 && DECL_DECLARES_FUNCTION_P (decl))
25545 vec_safe_push (unparsed_funs_with_definitions, decl);
25546 }
25547
25548 /* Parse a template introduction header for a template-declaration. Returns
25549 false if tentative parse fails. */
25550
25551 static bool
25552 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25553 {
25554 cp_parser_parse_tentatively (parser);
25555
25556 tree saved_scope = parser->scope;
25557 tree saved_object_scope = parser->object_scope;
25558 tree saved_qualifying_scope = parser->qualifying_scope;
25559
25560 /* Look for the optional `::' operator. */
25561 cp_parser_global_scope_opt (parser,
25562 /*current_scope_valid_p=*/false);
25563 /* Look for the nested-name-specifier. */
25564 cp_parser_nested_name_specifier_opt (parser,
25565 /*typename_keyword_p=*/false,
25566 /*check_dependency_p=*/true,
25567 /*type_p=*/false,
25568 /*is_declaration=*/false);
25569
25570 cp_token *token = cp_lexer_peek_token (parser->lexer);
25571 tree concept_name = cp_parser_identifier (parser);
25572
25573 /* Look up the concept for which we will be matching
25574 template parameters. */
25575 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25576 token->location);
25577 parser->scope = saved_scope;
25578 parser->object_scope = saved_object_scope;
25579 parser->qualifying_scope = saved_qualifying_scope;
25580
25581 if (concept_name == error_mark_node)
25582 cp_parser_simulate_error (parser);
25583
25584 /* Look for opening brace for introduction. */
25585 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25586
25587 if (!cp_parser_parse_definitely (parser))
25588 return false;
25589
25590 push_deferring_access_checks (dk_deferred);
25591
25592 /* Build vector of placeholder parameters and grab
25593 matching identifiers. */
25594 tree introduction_list = cp_parser_introduction_list (parser);
25595
25596 /* The introduction-list shall not be empty. */
25597 int nargs = TREE_VEC_LENGTH (introduction_list);
25598 if (nargs == 0)
25599 {
25600 error ("empty introduction-list");
25601 return true;
25602 }
25603
25604 /* Look for closing brace for introduction. */
25605 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25606 return true;
25607
25608 if (tmpl_decl == error_mark_node)
25609 {
25610 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25611 token->location);
25612 return true;
25613 }
25614
25615 /* Build and associate the constraint. */
25616 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25617 if (parms && parms != error_mark_node)
25618 {
25619 cp_parser_template_declaration_after_parameters (parser, parms,
25620 member_p);
25621 return true;
25622 }
25623
25624 error_at (token->location, "no matching concept for template-introduction");
25625 return true;
25626 }
25627
25628 /* Parse a normal template-declaration following the template keyword. */
25629
25630 static void
25631 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25632 {
25633 tree parameter_list;
25634 bool need_lang_pop;
25635 location_t location = input_location;
25636
25637 /* Look for the `<' token. */
25638 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25639 return;
25640 if (at_class_scope_p () && current_function_decl)
25641 {
25642 /* 14.5.2.2 [temp.mem]
25643
25644 A local class shall not have member templates. */
25645 error_at (location,
25646 "invalid declaration of member template in local class");
25647 cp_parser_skip_to_end_of_block_or_statement (parser);
25648 return;
25649 }
25650 /* [temp]
25651
25652 A template ... shall not have C linkage. */
25653 if (current_lang_name == lang_name_c)
25654 {
25655 error_at (location, "template with C linkage");
25656 /* Give it C++ linkage to avoid confusing other parts of the
25657 front end. */
25658 push_lang_context (lang_name_cplusplus);
25659 need_lang_pop = true;
25660 }
25661 else
25662 need_lang_pop = false;
25663
25664 /* We cannot perform access checks on the template parameter
25665 declarations until we know what is being declared, just as we
25666 cannot check the decl-specifier list. */
25667 push_deferring_access_checks (dk_deferred);
25668
25669 /* If the next token is `>', then we have an invalid
25670 specialization. Rather than complain about an invalid template
25671 parameter, issue an error message here. */
25672 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25673 {
25674 cp_parser_error (parser, "invalid explicit specialization");
25675 begin_specialization ();
25676 parameter_list = NULL_TREE;
25677 }
25678 else
25679 {
25680 /* Parse the template parameters. */
25681 parameter_list = cp_parser_template_parameter_list (parser);
25682 }
25683
25684 /* Look for the `>'. */
25685 cp_parser_skip_to_end_of_template_parameter_list (parser);
25686
25687 /* Manage template requirements */
25688 tree reqs = get_shorthand_constraints (current_template_parms);
25689 if (tree r = cp_parser_requires_clause_opt (parser))
25690 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25691 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25692
25693 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25694 member_p);
25695
25696 /* For the erroneous case of a template with C linkage, we pushed an
25697 implicit C++ linkage scope; exit that scope now. */
25698 if (need_lang_pop)
25699 pop_lang_context ();
25700 }
25701
25702 /* Parse a template-declaration, assuming that the `export' (and
25703 `extern') keywords, if present, has already been scanned. MEMBER_P
25704 is as for cp_parser_template_declaration. */
25705
25706 static bool
25707 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25708 {
25709 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25710 {
25711 cp_lexer_consume_token (parser->lexer);
25712 cp_parser_explicit_template_declaration (parser, member_p);
25713 return true;
25714 }
25715 else if (flag_concepts)
25716 return cp_parser_template_introduction (parser, member_p);
25717
25718 return false;
25719 }
25720
25721 /* Perform the deferred access checks from a template-parameter-list.
25722 CHECKS is a TREE_LIST of access checks, as returned by
25723 get_deferred_access_checks. */
25724
25725 static void
25726 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25727 {
25728 ++processing_template_parmlist;
25729 perform_access_checks (checks, tf_warning_or_error);
25730 --processing_template_parmlist;
25731 }
25732
25733 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25734 `function-definition' sequence that follows a template header.
25735 If MEMBER_P is true, this declaration appears in a class scope.
25736
25737 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25738 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25739
25740 static tree
25741 cp_parser_single_declaration (cp_parser* parser,
25742 vec<deferred_access_check, va_gc> *checks,
25743 bool member_p,
25744 bool explicit_specialization_p,
25745 bool* friend_p)
25746 {
25747 int declares_class_or_enum;
25748 tree decl = NULL_TREE;
25749 cp_decl_specifier_seq decl_specifiers;
25750 bool function_definition_p = false;
25751 cp_token *decl_spec_token_start;
25752
25753 /* This function is only used when processing a template
25754 declaration. */
25755 gcc_assert (innermost_scope_kind () == sk_template_parms
25756 || innermost_scope_kind () == sk_template_spec);
25757
25758 /* Defer access checks until we know what is being declared. */
25759 push_deferring_access_checks (dk_deferred);
25760
25761 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25762 alternative. */
25763 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25764 cp_parser_decl_specifier_seq (parser,
25765 CP_PARSER_FLAGS_OPTIONAL,
25766 &decl_specifiers,
25767 &declares_class_or_enum);
25768 if (friend_p)
25769 *friend_p = cp_parser_friend_p (&decl_specifiers);
25770
25771 /* There are no template typedefs. */
25772 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25773 {
25774 error_at (decl_spec_token_start->location,
25775 "template declaration of %<typedef%>");
25776 decl = error_mark_node;
25777 }
25778
25779 /* Gather up the access checks that occurred the
25780 decl-specifier-seq. */
25781 stop_deferring_access_checks ();
25782
25783 /* Check for the declaration of a template class. */
25784 if (declares_class_or_enum)
25785 {
25786 if (cp_parser_declares_only_class_p (parser)
25787 || (declares_class_or_enum & 2))
25788 {
25789 // If this is a declaration, but not a definition, associate
25790 // any constraints with the type declaration. Constraints
25791 // are associated with definitions in cp_parser_class_specifier.
25792 if (declares_class_or_enum == 1)
25793 associate_classtype_constraints (decl_specifiers.type);
25794
25795 decl = shadow_tag (&decl_specifiers);
25796
25797 /* In this case:
25798
25799 struct C {
25800 friend template <typename T> struct A<T>::B;
25801 };
25802
25803 A<T>::B will be represented by a TYPENAME_TYPE, and
25804 therefore not recognized by shadow_tag. */
25805 if (friend_p && *friend_p
25806 && !decl
25807 && decl_specifiers.type
25808 && TYPE_P (decl_specifiers.type))
25809 decl = decl_specifiers.type;
25810
25811 if (decl && decl != error_mark_node)
25812 decl = TYPE_NAME (decl);
25813 else
25814 decl = error_mark_node;
25815
25816 /* Perform access checks for template parameters. */
25817 cp_parser_perform_template_parameter_access_checks (checks);
25818
25819 /* Give a helpful diagnostic for
25820 template <class T> struct A { } a;
25821 if we aren't already recovering from an error. */
25822 if (!cp_parser_declares_only_class_p (parser)
25823 && !seen_error ())
25824 {
25825 error_at (cp_lexer_peek_token (parser->lexer)->location,
25826 "a class template declaration must not declare "
25827 "anything else");
25828 cp_parser_skip_to_end_of_block_or_statement (parser);
25829 goto out;
25830 }
25831 }
25832 }
25833
25834 /* Complain about missing 'typename' or other invalid type names. */
25835 if (!decl_specifiers.any_type_specifiers_p
25836 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25837 {
25838 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25839 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25840 the rest of this declaration. */
25841 decl = error_mark_node;
25842 goto out;
25843 }
25844
25845 /* If it's not a template class, try for a template function. If
25846 the next token is a `;', then this declaration does not declare
25847 anything. But, if there were errors in the decl-specifiers, then
25848 the error might well have come from an attempted class-specifier.
25849 In that case, there's no need to warn about a missing declarator. */
25850 if (!decl
25851 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25852 || decl_specifiers.type != error_mark_node))
25853 {
25854 decl = cp_parser_init_declarator (parser,
25855 &decl_specifiers,
25856 checks,
25857 /*function_definition_allowed_p=*/true,
25858 member_p,
25859 declares_class_or_enum,
25860 &function_definition_p,
25861 NULL, NULL, NULL);
25862
25863 /* 7.1.1-1 [dcl.stc]
25864
25865 A storage-class-specifier shall not be specified in an explicit
25866 specialization... */
25867 if (decl
25868 && explicit_specialization_p
25869 && decl_specifiers.storage_class != sc_none)
25870 {
25871 error_at (decl_spec_token_start->location,
25872 "explicit template specialization cannot have a storage class");
25873 decl = error_mark_node;
25874 }
25875
25876 if (decl && VAR_P (decl))
25877 check_template_variable (decl);
25878 }
25879
25880 /* Look for a trailing `;' after the declaration. */
25881 if (!function_definition_p
25882 && (decl == error_mark_node
25883 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25884 cp_parser_skip_to_end_of_block_or_statement (parser);
25885
25886 out:
25887 pop_deferring_access_checks ();
25888
25889 /* Clear any current qualification; whatever comes next is the start
25890 of something new. */
25891 parser->scope = NULL_TREE;
25892 parser->qualifying_scope = NULL_TREE;
25893 parser->object_scope = NULL_TREE;
25894
25895 return decl;
25896 }
25897
25898 /* Parse a cast-expression that is not the operand of a unary "&". */
25899
25900 static cp_expr
25901 cp_parser_simple_cast_expression (cp_parser *parser)
25902 {
25903 return cp_parser_cast_expression (parser, /*address_p=*/false,
25904 /*cast_p=*/false, /*decltype*/false, NULL);
25905 }
25906
25907 /* Parse a functional cast to TYPE. Returns an expression
25908 representing the cast. */
25909
25910 static cp_expr
25911 cp_parser_functional_cast (cp_parser* parser, tree type)
25912 {
25913 vec<tree, va_gc> *vec;
25914 tree expression_list;
25915 cp_expr cast;
25916 bool nonconst_p;
25917
25918 location_t start_loc = input_location;
25919
25920 if (!type)
25921 type = error_mark_node;
25922
25923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25924 {
25925 cp_lexer_set_source_position (parser->lexer);
25926 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25927 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25928 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25929 if (TREE_CODE (type) == TYPE_DECL)
25930 type = TREE_TYPE (type);
25931
25932 cast = finish_compound_literal (type, expression_list,
25933 tf_warning_or_error);
25934 /* Create a location of the form:
25935 type_name{i, f}
25936 ^~~~~~~~~~~~~~~
25937 with caret == start at the start of the type name,
25938 finishing at the closing brace. */
25939 location_t finish_loc
25940 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25941 location_t combined_loc = make_location (start_loc, start_loc,
25942 finish_loc);
25943 cast.set_location (combined_loc);
25944 return cast;
25945 }
25946
25947
25948 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25949 /*cast_p=*/true,
25950 /*allow_expansion_p=*/true,
25951 /*non_constant_p=*/NULL);
25952 if (vec == NULL)
25953 expression_list = error_mark_node;
25954 else
25955 {
25956 expression_list = build_tree_list_vec (vec);
25957 release_tree_vector (vec);
25958 }
25959
25960 cast = build_functional_cast (type, expression_list,
25961 tf_warning_or_error);
25962 /* [expr.const]/1: In an integral constant expression "only type
25963 conversions to integral or enumeration type can be used". */
25964 if (TREE_CODE (type) == TYPE_DECL)
25965 type = TREE_TYPE (type);
25966 if (cast != error_mark_node
25967 && !cast_valid_in_integral_constant_expression_p (type)
25968 && cp_parser_non_integral_constant_expression (parser,
25969 NIC_CONSTRUCTOR))
25970 return error_mark_node;
25971
25972 /* Create a location of the form:
25973 float(i)
25974 ^~~~~~~~
25975 with caret == start at the start of the type name,
25976 finishing at the closing paren. */
25977 location_t finish_loc
25978 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25979 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25980 cast.set_location (combined_loc);
25981 return cast;
25982 }
25983
25984 /* Save the tokens that make up the body of a member function defined
25985 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25986 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25987 specifiers applied to the declaration. Returns the FUNCTION_DECL
25988 for the member function. */
25989
25990 static tree
25991 cp_parser_save_member_function_body (cp_parser* parser,
25992 cp_decl_specifier_seq *decl_specifiers,
25993 cp_declarator *declarator,
25994 tree attributes)
25995 {
25996 cp_token *first;
25997 cp_token *last;
25998 tree fn;
25999
26000 /* Create the FUNCTION_DECL. */
26001 fn = grokmethod (decl_specifiers, declarator, attributes);
26002 cp_finalize_omp_declare_simd (parser, fn);
26003 cp_finalize_oacc_routine (parser, fn, true);
26004 /* If something went badly wrong, bail out now. */
26005 if (fn == error_mark_node)
26006 {
26007 /* If there's a function-body, skip it. */
26008 if (cp_parser_token_starts_function_definition_p
26009 (cp_lexer_peek_token (parser->lexer)))
26010 cp_parser_skip_to_end_of_block_or_statement (parser);
26011 return error_mark_node;
26012 }
26013
26014 /* Remember it, if there default args to post process. */
26015 cp_parser_save_default_args (parser, fn);
26016
26017 /* Save away the tokens that make up the body of the
26018 function. */
26019 first = parser->lexer->next_token;
26020 /* Handle function try blocks. */
26021 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26022 cp_lexer_consume_token (parser->lexer);
26023 /* We can have braced-init-list mem-initializers before the fn body. */
26024 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26025 {
26026 cp_lexer_consume_token (parser->lexer);
26027 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26028 {
26029 /* cache_group will stop after an un-nested { } pair, too. */
26030 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26031 break;
26032
26033 /* variadic mem-inits have ... after the ')'. */
26034 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26035 cp_lexer_consume_token (parser->lexer);
26036 }
26037 }
26038 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26039 /* Handle function try blocks. */
26040 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26041 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26042 last = parser->lexer->next_token;
26043
26044 /* Save away the inline definition; we will process it when the
26045 class is complete. */
26046 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26047 DECL_PENDING_INLINE_P (fn) = 1;
26048
26049 /* We need to know that this was defined in the class, so that
26050 friend templates are handled correctly. */
26051 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26052
26053 /* Add FN to the queue of functions to be parsed later. */
26054 vec_safe_push (unparsed_funs_with_definitions, fn);
26055
26056 return fn;
26057 }
26058
26059 /* Save the tokens that make up the in-class initializer for a non-static
26060 data member. Returns a DEFAULT_ARG. */
26061
26062 static tree
26063 cp_parser_save_nsdmi (cp_parser* parser)
26064 {
26065 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26066 }
26067
26068 /* Parse a template-argument-list, as well as the trailing ">" (but
26069 not the opening "<"). See cp_parser_template_argument_list for the
26070 return value. */
26071
26072 static tree
26073 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26074 {
26075 tree arguments;
26076 tree saved_scope;
26077 tree saved_qualifying_scope;
26078 tree saved_object_scope;
26079 bool saved_greater_than_is_operator_p;
26080 int saved_unevaluated_operand;
26081 int saved_inhibit_evaluation_warnings;
26082
26083 /* [temp.names]
26084
26085 When parsing a template-id, the first non-nested `>' is taken as
26086 the end of the template-argument-list rather than a greater-than
26087 operator. */
26088 saved_greater_than_is_operator_p
26089 = parser->greater_than_is_operator_p;
26090 parser->greater_than_is_operator_p = false;
26091 /* Parsing the argument list may modify SCOPE, so we save it
26092 here. */
26093 saved_scope = parser->scope;
26094 saved_qualifying_scope = parser->qualifying_scope;
26095 saved_object_scope = parser->object_scope;
26096 /* We need to evaluate the template arguments, even though this
26097 template-id may be nested within a "sizeof". */
26098 saved_unevaluated_operand = cp_unevaluated_operand;
26099 cp_unevaluated_operand = 0;
26100 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26101 c_inhibit_evaluation_warnings = 0;
26102 /* Parse the template-argument-list itself. */
26103 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26104 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26105 arguments = NULL_TREE;
26106 else
26107 arguments = cp_parser_template_argument_list (parser);
26108 /* Look for the `>' that ends the template-argument-list. If we find
26109 a '>>' instead, it's probably just a typo. */
26110 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26111 {
26112 if (cxx_dialect != cxx98)
26113 {
26114 /* In C++0x, a `>>' in a template argument list or cast
26115 expression is considered to be two separate `>'
26116 tokens. So, change the current token to a `>', but don't
26117 consume it: it will be consumed later when the outer
26118 template argument list (or cast expression) is parsed.
26119 Note that this replacement of `>' for `>>' is necessary
26120 even if we are parsing tentatively: in the tentative
26121 case, after calling
26122 cp_parser_enclosed_template_argument_list we will always
26123 throw away all of the template arguments and the first
26124 closing `>', either because the template argument list
26125 was erroneous or because we are replacing those tokens
26126 with a CPP_TEMPLATE_ID token. The second `>' (which will
26127 not have been thrown away) is needed either to close an
26128 outer template argument list or to complete a new-style
26129 cast. */
26130 cp_token *token = cp_lexer_peek_token (parser->lexer);
26131 token->type = CPP_GREATER;
26132 }
26133 else if (!saved_greater_than_is_operator_p)
26134 {
26135 /* If we're in a nested template argument list, the '>>' has
26136 to be a typo for '> >'. We emit the error message, but we
26137 continue parsing and we push a '>' as next token, so that
26138 the argument list will be parsed correctly. Note that the
26139 global source location is still on the token before the
26140 '>>', so we need to say explicitly where we want it. */
26141 cp_token *token = cp_lexer_peek_token (parser->lexer);
26142 error_at (token->location, "%<>>%> should be %<> >%> "
26143 "within a nested template argument list");
26144
26145 token->type = CPP_GREATER;
26146 }
26147 else
26148 {
26149 /* If this is not a nested template argument list, the '>>'
26150 is a typo for '>'. Emit an error message and continue.
26151 Same deal about the token location, but here we can get it
26152 right by consuming the '>>' before issuing the diagnostic. */
26153 cp_token *token = cp_lexer_consume_token (parser->lexer);
26154 error_at (token->location,
26155 "spurious %<>>%>, use %<>%> to terminate "
26156 "a template argument list");
26157 }
26158 }
26159 else
26160 cp_parser_skip_to_end_of_template_parameter_list (parser);
26161 /* The `>' token might be a greater-than operator again now. */
26162 parser->greater_than_is_operator_p
26163 = saved_greater_than_is_operator_p;
26164 /* Restore the SAVED_SCOPE. */
26165 parser->scope = saved_scope;
26166 parser->qualifying_scope = saved_qualifying_scope;
26167 parser->object_scope = saved_object_scope;
26168 cp_unevaluated_operand = saved_unevaluated_operand;
26169 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26170
26171 return arguments;
26172 }
26173
26174 /* MEMBER_FUNCTION is a member function, or a friend. If default
26175 arguments, or the body of the function have not yet been parsed,
26176 parse them now. */
26177
26178 static void
26179 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26180 {
26181 timevar_push (TV_PARSE_INMETH);
26182 /* If this member is a template, get the underlying
26183 FUNCTION_DECL. */
26184 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26185 member_function = DECL_TEMPLATE_RESULT (member_function);
26186
26187 /* There should not be any class definitions in progress at this
26188 point; the bodies of members are only parsed outside of all class
26189 definitions. */
26190 gcc_assert (parser->num_classes_being_defined == 0);
26191 /* While we're parsing the member functions we might encounter more
26192 classes. We want to handle them right away, but we don't want
26193 them getting mixed up with functions that are currently in the
26194 queue. */
26195 push_unparsed_function_queues (parser);
26196
26197 /* Make sure that any template parameters are in scope. */
26198 maybe_begin_member_template_processing (member_function);
26199
26200 /* If the body of the function has not yet been parsed, parse it
26201 now. */
26202 if (DECL_PENDING_INLINE_P (member_function))
26203 {
26204 tree function_scope;
26205 cp_token_cache *tokens;
26206
26207 /* The function is no longer pending; we are processing it. */
26208 tokens = DECL_PENDING_INLINE_INFO (member_function);
26209 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26210 DECL_PENDING_INLINE_P (member_function) = 0;
26211
26212 /* If this is a local class, enter the scope of the containing
26213 function. */
26214 function_scope = current_function_decl;
26215 if (function_scope)
26216 push_function_context ();
26217
26218 /* Push the body of the function onto the lexer stack. */
26219 cp_parser_push_lexer_for_tokens (parser, tokens);
26220
26221 /* Let the front end know that we going to be defining this
26222 function. */
26223 start_preparsed_function (member_function, NULL_TREE,
26224 SF_PRE_PARSED | SF_INCLASS_INLINE);
26225
26226 /* Don't do access checking if it is a templated function. */
26227 if (processing_template_decl)
26228 push_deferring_access_checks (dk_no_check);
26229
26230 /* #pragma omp declare reduction needs special parsing. */
26231 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26232 {
26233 parser->lexer->in_pragma = true;
26234 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26235 finish_function (/*inline*/2);
26236 cp_check_omp_declare_reduction (member_function);
26237 }
26238 else
26239 /* Now, parse the body of the function. */
26240 cp_parser_function_definition_after_declarator (parser,
26241 /*inline_p=*/true);
26242
26243 if (processing_template_decl)
26244 pop_deferring_access_checks ();
26245
26246 /* Leave the scope of the containing function. */
26247 if (function_scope)
26248 pop_function_context ();
26249 cp_parser_pop_lexer (parser);
26250 }
26251
26252 /* Remove any template parameters from the symbol table. */
26253 maybe_end_member_template_processing ();
26254
26255 /* Restore the queue. */
26256 pop_unparsed_function_queues (parser);
26257 timevar_pop (TV_PARSE_INMETH);
26258 }
26259
26260 /* If DECL contains any default args, remember it on the unparsed
26261 functions queue. */
26262
26263 static void
26264 cp_parser_save_default_args (cp_parser* parser, tree decl)
26265 {
26266 tree probe;
26267
26268 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26269 probe;
26270 probe = TREE_CHAIN (probe))
26271 if (TREE_PURPOSE (probe))
26272 {
26273 cp_default_arg_entry entry = {current_class_type, decl};
26274 vec_safe_push (unparsed_funs_with_default_args, entry);
26275 break;
26276 }
26277 }
26278
26279 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26280 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26281 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26282 from the parameter-type-list. */
26283
26284 static tree
26285 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26286 tree default_arg, tree parmtype)
26287 {
26288 cp_token_cache *tokens;
26289 tree parsed_arg;
26290 bool dummy;
26291
26292 if (default_arg == error_mark_node)
26293 return error_mark_node;
26294
26295 /* Push the saved tokens for the default argument onto the parser's
26296 lexer stack. */
26297 tokens = DEFARG_TOKENS (default_arg);
26298 cp_parser_push_lexer_for_tokens (parser, tokens);
26299
26300 start_lambda_scope (decl);
26301
26302 /* Parse the default argument. */
26303 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26304 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26305 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26306
26307 finish_lambda_scope ();
26308
26309 if (parsed_arg == error_mark_node)
26310 cp_parser_skip_to_end_of_statement (parser);
26311
26312 if (!processing_template_decl)
26313 {
26314 /* In a non-template class, check conversions now. In a template,
26315 we'll wait and instantiate these as needed. */
26316 if (TREE_CODE (decl) == PARM_DECL)
26317 parsed_arg = check_default_argument (parmtype, parsed_arg,
26318 tf_warning_or_error);
26319 else
26320 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26321 }
26322
26323 /* If the token stream has not been completely used up, then
26324 there was extra junk after the end of the default
26325 argument. */
26326 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26327 {
26328 if (TREE_CODE (decl) == PARM_DECL)
26329 cp_parser_error (parser, "expected %<,%>");
26330 else
26331 cp_parser_error (parser, "expected %<;%>");
26332 }
26333
26334 /* Revert to the main lexer. */
26335 cp_parser_pop_lexer (parser);
26336
26337 return parsed_arg;
26338 }
26339
26340 /* FIELD is a non-static data member with an initializer which we saved for
26341 later; parse it now. */
26342
26343 static void
26344 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26345 {
26346 tree def;
26347
26348 maybe_begin_member_template_processing (field);
26349
26350 push_unparsed_function_queues (parser);
26351 def = cp_parser_late_parse_one_default_arg (parser, field,
26352 DECL_INITIAL (field),
26353 NULL_TREE);
26354 pop_unparsed_function_queues (parser);
26355
26356 maybe_end_member_template_processing ();
26357
26358 DECL_INITIAL (field) = def;
26359 }
26360
26361 /* FN is a FUNCTION_DECL which may contains a parameter with an
26362 unparsed DEFAULT_ARG. Parse the default args now. This function
26363 assumes that the current scope is the scope in which the default
26364 argument should be processed. */
26365
26366 static void
26367 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26368 {
26369 bool saved_local_variables_forbidden_p;
26370 tree parm, parmdecl;
26371
26372 /* While we're parsing the default args, we might (due to the
26373 statement expression extension) encounter more classes. We want
26374 to handle them right away, but we don't want them getting mixed
26375 up with default args that are currently in the queue. */
26376 push_unparsed_function_queues (parser);
26377
26378 /* Local variable names (and the `this' keyword) may not appear
26379 in a default argument. */
26380 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26381 parser->local_variables_forbidden_p = true;
26382
26383 push_defarg_context (fn);
26384
26385 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26386 parmdecl = DECL_ARGUMENTS (fn);
26387 parm && parm != void_list_node;
26388 parm = TREE_CHAIN (parm),
26389 parmdecl = DECL_CHAIN (parmdecl))
26390 {
26391 tree default_arg = TREE_PURPOSE (parm);
26392 tree parsed_arg;
26393 vec<tree, va_gc> *insts;
26394 tree copy;
26395 unsigned ix;
26396
26397 if (!default_arg)
26398 continue;
26399
26400 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26401 /* This can happen for a friend declaration for a function
26402 already declared with default arguments. */
26403 continue;
26404
26405 parsed_arg
26406 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26407 default_arg,
26408 TREE_VALUE (parm));
26409 if (parsed_arg == error_mark_node)
26410 {
26411 continue;
26412 }
26413
26414 TREE_PURPOSE (parm) = parsed_arg;
26415
26416 /* Update any instantiations we've already created. */
26417 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26418 vec_safe_iterate (insts, ix, &copy); ix++)
26419 TREE_PURPOSE (copy) = parsed_arg;
26420 }
26421
26422 pop_defarg_context ();
26423
26424 /* Make sure no default arg is missing. */
26425 check_default_args (fn);
26426
26427 /* Restore the state of local_variables_forbidden_p. */
26428 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26429
26430 /* Restore the queue. */
26431 pop_unparsed_function_queues (parser);
26432 }
26433
26434 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26435
26436 sizeof ... ( identifier )
26437
26438 where the 'sizeof' token has already been consumed. */
26439
26440 static tree
26441 cp_parser_sizeof_pack (cp_parser *parser)
26442 {
26443 /* Consume the `...'. */
26444 cp_lexer_consume_token (parser->lexer);
26445 maybe_warn_variadic_templates ();
26446
26447 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26448 if (paren)
26449 cp_lexer_consume_token (parser->lexer);
26450 else
26451 permerror (cp_lexer_peek_token (parser->lexer)->location,
26452 "%<sizeof...%> argument must be surrounded by parentheses");
26453
26454 cp_token *token = cp_lexer_peek_token (parser->lexer);
26455 tree name = cp_parser_identifier (parser);
26456 if (name == error_mark_node)
26457 return error_mark_node;
26458 /* The name is not qualified. */
26459 parser->scope = NULL_TREE;
26460 parser->qualifying_scope = NULL_TREE;
26461 parser->object_scope = NULL_TREE;
26462 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26463 if (expr == error_mark_node)
26464 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26465 token->location);
26466 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26467 expr = TREE_TYPE (expr);
26468 else if (TREE_CODE (expr) == CONST_DECL)
26469 expr = DECL_INITIAL (expr);
26470 expr = make_pack_expansion (expr);
26471 PACK_EXPANSION_SIZEOF_P (expr) = true;
26472
26473 if (paren)
26474 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26475
26476 return expr;
26477 }
26478
26479 /* Parse the operand of `sizeof' (or a similar operator). Returns
26480 either a TYPE or an expression, depending on the form of the
26481 input. The KEYWORD indicates which kind of expression we have
26482 encountered. */
26483
26484 static tree
26485 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26486 {
26487 tree expr = NULL_TREE;
26488 const char *saved_message;
26489 char *tmp;
26490 bool saved_integral_constant_expression_p;
26491 bool saved_non_integral_constant_expression_p;
26492
26493 /* If it's a `...', then we are computing the length of a parameter
26494 pack. */
26495 if (keyword == RID_SIZEOF
26496 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26497 return cp_parser_sizeof_pack (parser);
26498
26499 /* Types cannot be defined in a `sizeof' expression. Save away the
26500 old message. */
26501 saved_message = parser->type_definition_forbidden_message;
26502 /* And create the new one. */
26503 tmp = concat ("types may not be defined in %<",
26504 IDENTIFIER_POINTER (ridpointers[keyword]),
26505 "%> expressions", NULL);
26506 parser->type_definition_forbidden_message = tmp;
26507
26508 /* The restrictions on constant-expressions do not apply inside
26509 sizeof expressions. */
26510 saved_integral_constant_expression_p
26511 = parser->integral_constant_expression_p;
26512 saved_non_integral_constant_expression_p
26513 = parser->non_integral_constant_expression_p;
26514 parser->integral_constant_expression_p = false;
26515
26516 /* Do not actually evaluate the expression. */
26517 ++cp_unevaluated_operand;
26518 ++c_inhibit_evaluation_warnings;
26519 /* If it's a `(', then we might be looking at the type-id
26520 construction. */
26521 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26522 {
26523 tree type = NULL_TREE;
26524
26525 /* We can't be sure yet whether we're looking at a type-id or an
26526 expression. */
26527 cp_parser_parse_tentatively (parser);
26528 /* Note: as a GNU Extension, compound literals are considered
26529 postfix-expressions as they are in C99, so they are valid
26530 arguments to sizeof. See comment in cp_parser_cast_expression
26531 for details. */
26532 if (cp_parser_compound_literal_p (parser))
26533 cp_parser_simulate_error (parser);
26534 else
26535 {
26536 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26537 parser->in_type_id_in_expr_p = true;
26538 /* Look for the type-id. */
26539 type = cp_parser_type_id (parser);
26540 /* Look for the closing `)'. */
26541 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26542 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26543 }
26544
26545 /* If all went well, then we're done. */
26546 if (cp_parser_parse_definitely (parser))
26547 {
26548 cp_decl_specifier_seq decl_specs;
26549
26550 /* Build a trivial decl-specifier-seq. */
26551 clear_decl_specs (&decl_specs);
26552 decl_specs.type = type;
26553
26554 /* Call grokdeclarator to figure out what type this is. */
26555 expr = grokdeclarator (NULL,
26556 &decl_specs,
26557 TYPENAME,
26558 /*initialized=*/0,
26559 /*attrlist=*/NULL);
26560 }
26561 }
26562
26563 /* If the type-id production did not work out, then we must be
26564 looking at the unary-expression production. */
26565 if (!expr)
26566 expr = cp_parser_unary_expression (parser);
26567
26568 /* Go back to evaluating expressions. */
26569 --cp_unevaluated_operand;
26570 --c_inhibit_evaluation_warnings;
26571
26572 /* Free the message we created. */
26573 free (tmp);
26574 /* And restore the old one. */
26575 parser->type_definition_forbidden_message = saved_message;
26576 parser->integral_constant_expression_p
26577 = saved_integral_constant_expression_p;
26578 parser->non_integral_constant_expression_p
26579 = saved_non_integral_constant_expression_p;
26580
26581 return expr;
26582 }
26583
26584 /* If the current declaration has no declarator, return true. */
26585
26586 static bool
26587 cp_parser_declares_only_class_p (cp_parser *parser)
26588 {
26589 /* If the next token is a `;' or a `,' then there is no
26590 declarator. */
26591 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26592 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26593 }
26594
26595 /* Update the DECL_SPECS to reflect the storage class indicated by
26596 KEYWORD. */
26597
26598 static void
26599 cp_parser_set_storage_class (cp_parser *parser,
26600 cp_decl_specifier_seq *decl_specs,
26601 enum rid keyword,
26602 cp_token *token)
26603 {
26604 cp_storage_class storage_class;
26605
26606 if (parser->in_unbraced_linkage_specification_p)
26607 {
26608 error_at (token->location, "invalid use of %qD in linkage specification",
26609 ridpointers[keyword]);
26610 return;
26611 }
26612 else if (decl_specs->storage_class != sc_none)
26613 {
26614 decl_specs->conflicting_specifiers_p = true;
26615 return;
26616 }
26617
26618 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26619 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26620 && decl_specs->gnu_thread_keyword_p)
26621 {
26622 pedwarn (decl_specs->locations[ds_thread], 0,
26623 "%<__thread%> before %qD", ridpointers[keyword]);
26624 }
26625
26626 switch (keyword)
26627 {
26628 case RID_AUTO:
26629 storage_class = sc_auto;
26630 break;
26631 case RID_REGISTER:
26632 storage_class = sc_register;
26633 break;
26634 case RID_STATIC:
26635 storage_class = sc_static;
26636 break;
26637 case RID_EXTERN:
26638 storage_class = sc_extern;
26639 break;
26640 case RID_MUTABLE:
26641 storage_class = sc_mutable;
26642 break;
26643 default:
26644 gcc_unreachable ();
26645 }
26646 decl_specs->storage_class = storage_class;
26647 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26648
26649 /* A storage class specifier cannot be applied alongside a typedef
26650 specifier. If there is a typedef specifier present then set
26651 conflicting_specifiers_p which will trigger an error later
26652 on in grokdeclarator. */
26653 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26654 decl_specs->conflicting_specifiers_p = true;
26655 }
26656
26657 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26658 is true, the type is a class or enum definition. */
26659
26660 static void
26661 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26662 tree type_spec,
26663 cp_token *token,
26664 bool type_definition_p)
26665 {
26666 decl_specs->any_specifiers_p = true;
26667
26668 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26669 (with, for example, in "typedef int wchar_t;") we remember that
26670 this is what happened. In system headers, we ignore these
26671 declarations so that G++ can work with system headers that are not
26672 C++-safe. */
26673 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26674 && !type_definition_p
26675 && (type_spec == boolean_type_node
26676 || type_spec == char16_type_node
26677 || type_spec == char32_type_node
26678 || type_spec == wchar_type_node)
26679 && (decl_specs->type
26680 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26681 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26682 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26683 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26684 {
26685 decl_specs->redefined_builtin_type = type_spec;
26686 set_and_check_decl_spec_loc (decl_specs,
26687 ds_redefined_builtin_type_spec,
26688 token);
26689 if (!decl_specs->type)
26690 {
26691 decl_specs->type = type_spec;
26692 decl_specs->type_definition_p = false;
26693 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26694 }
26695 }
26696 else if (decl_specs->type)
26697 decl_specs->multiple_types_p = true;
26698 else
26699 {
26700 decl_specs->type = type_spec;
26701 decl_specs->type_definition_p = type_definition_p;
26702 decl_specs->redefined_builtin_type = NULL_TREE;
26703 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26704 }
26705 }
26706
26707 /* True iff TOKEN is the GNU keyword __thread. */
26708
26709 static bool
26710 token_is__thread (cp_token *token)
26711 {
26712 gcc_assert (token->keyword == RID_THREAD);
26713 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26714 }
26715
26716 /* Set the location for a declarator specifier and check if it is
26717 duplicated.
26718
26719 DECL_SPECS is the sequence of declarator specifiers onto which to
26720 set the location.
26721
26722 DS is the single declarator specifier to set which location is to
26723 be set onto the existing sequence of declarators.
26724
26725 LOCATION is the location for the declarator specifier to
26726 consider. */
26727
26728 static void
26729 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26730 cp_decl_spec ds, cp_token *token)
26731 {
26732 gcc_assert (ds < ds_last);
26733
26734 if (decl_specs == NULL)
26735 return;
26736
26737 source_location location = token->location;
26738
26739 if (decl_specs->locations[ds] == 0)
26740 {
26741 decl_specs->locations[ds] = location;
26742 if (ds == ds_thread)
26743 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26744 }
26745 else
26746 {
26747 if (ds == ds_long)
26748 {
26749 if (decl_specs->locations[ds_long_long] != 0)
26750 error_at (location,
26751 "%<long long long%> is too long for GCC");
26752 else
26753 {
26754 decl_specs->locations[ds_long_long] = location;
26755 pedwarn_cxx98 (location,
26756 OPT_Wlong_long,
26757 "ISO C++ 1998 does not support %<long long%>");
26758 }
26759 }
26760 else if (ds == ds_thread)
26761 {
26762 bool gnu = token_is__thread (token);
26763 if (gnu != decl_specs->gnu_thread_keyword_p)
26764 error_at (location,
26765 "both %<__thread%> and %<thread_local%> specified");
26766 else
26767 error_at (location, "duplicate %qD", token->u.value);
26768 }
26769 else
26770 {
26771 static const char *const decl_spec_names[] = {
26772 "signed",
26773 "unsigned",
26774 "short",
26775 "long",
26776 "const",
26777 "volatile",
26778 "restrict",
26779 "inline",
26780 "virtual",
26781 "explicit",
26782 "friend",
26783 "typedef",
26784 "using",
26785 "constexpr",
26786 "__complex"
26787 };
26788 error_at (location,
26789 "duplicate %qs", decl_spec_names[ds]);
26790 }
26791 }
26792 }
26793
26794 /* Return true iff the declarator specifier DS is present in the
26795 sequence of declarator specifiers DECL_SPECS. */
26796
26797 bool
26798 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26799 cp_decl_spec ds)
26800 {
26801 gcc_assert (ds < ds_last);
26802
26803 if (decl_specs == NULL)
26804 return false;
26805
26806 return decl_specs->locations[ds] != 0;
26807 }
26808
26809 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26810 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26811
26812 static bool
26813 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26814 {
26815 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26816 }
26817
26818 /* Issue an error message indicating that TOKEN_DESC was expected.
26819 If KEYWORD is true, it indicated this function is called by
26820 cp_parser_require_keword and the required token can only be
26821 a indicated keyword. */
26822
26823 static void
26824 cp_parser_required_error (cp_parser *parser,
26825 required_token token_desc,
26826 bool keyword)
26827 {
26828 switch (token_desc)
26829 {
26830 case RT_NEW:
26831 cp_parser_error (parser, "expected %<new%>");
26832 return;
26833 case RT_DELETE:
26834 cp_parser_error (parser, "expected %<delete%>");
26835 return;
26836 case RT_RETURN:
26837 cp_parser_error (parser, "expected %<return%>");
26838 return;
26839 case RT_WHILE:
26840 cp_parser_error (parser, "expected %<while%>");
26841 return;
26842 case RT_EXTERN:
26843 cp_parser_error (parser, "expected %<extern%>");
26844 return;
26845 case RT_STATIC_ASSERT:
26846 cp_parser_error (parser, "expected %<static_assert%>");
26847 return;
26848 case RT_DECLTYPE:
26849 cp_parser_error (parser, "expected %<decltype%>");
26850 return;
26851 case RT_OPERATOR:
26852 cp_parser_error (parser, "expected %<operator%>");
26853 return;
26854 case RT_CLASS:
26855 cp_parser_error (parser, "expected %<class%>");
26856 return;
26857 case RT_TEMPLATE:
26858 cp_parser_error (parser, "expected %<template%>");
26859 return;
26860 case RT_NAMESPACE:
26861 cp_parser_error (parser, "expected %<namespace%>");
26862 return;
26863 case RT_USING:
26864 cp_parser_error (parser, "expected %<using%>");
26865 return;
26866 case RT_ASM:
26867 cp_parser_error (parser, "expected %<asm%>");
26868 return;
26869 case RT_TRY:
26870 cp_parser_error (parser, "expected %<try%>");
26871 return;
26872 case RT_CATCH:
26873 cp_parser_error (parser, "expected %<catch%>");
26874 return;
26875 case RT_THROW:
26876 cp_parser_error (parser, "expected %<throw%>");
26877 return;
26878 case RT_LABEL:
26879 cp_parser_error (parser, "expected %<__label__%>");
26880 return;
26881 case RT_AT_TRY:
26882 cp_parser_error (parser, "expected %<@try%>");
26883 return;
26884 case RT_AT_SYNCHRONIZED:
26885 cp_parser_error (parser, "expected %<@synchronized%>");
26886 return;
26887 case RT_AT_THROW:
26888 cp_parser_error (parser, "expected %<@throw%>");
26889 return;
26890 case RT_TRANSACTION_ATOMIC:
26891 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26892 return;
26893 case RT_TRANSACTION_RELAXED:
26894 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26895 return;
26896 default:
26897 break;
26898 }
26899 if (!keyword)
26900 {
26901 switch (token_desc)
26902 {
26903 case RT_SEMICOLON:
26904 cp_parser_error (parser, "expected %<;%>");
26905 return;
26906 case RT_OPEN_PAREN:
26907 cp_parser_error (parser, "expected %<(%>");
26908 return;
26909 case RT_CLOSE_BRACE:
26910 cp_parser_error (parser, "expected %<}%>");
26911 return;
26912 case RT_OPEN_BRACE:
26913 cp_parser_error (parser, "expected %<{%>");
26914 return;
26915 case RT_CLOSE_SQUARE:
26916 cp_parser_error (parser, "expected %<]%>");
26917 return;
26918 case RT_OPEN_SQUARE:
26919 cp_parser_error (parser, "expected %<[%>");
26920 return;
26921 case RT_COMMA:
26922 cp_parser_error (parser, "expected %<,%>");
26923 return;
26924 case RT_SCOPE:
26925 cp_parser_error (parser, "expected %<::%>");
26926 return;
26927 case RT_LESS:
26928 cp_parser_error (parser, "expected %<<%>");
26929 return;
26930 case RT_GREATER:
26931 cp_parser_error (parser, "expected %<>%>");
26932 return;
26933 case RT_EQ:
26934 cp_parser_error (parser, "expected %<=%>");
26935 return;
26936 case RT_ELLIPSIS:
26937 cp_parser_error (parser, "expected %<...%>");
26938 return;
26939 case RT_MULT:
26940 cp_parser_error (parser, "expected %<*%>");
26941 return;
26942 case RT_COMPL:
26943 cp_parser_error (parser, "expected %<~%>");
26944 return;
26945 case RT_COLON:
26946 cp_parser_error (parser, "expected %<:%>");
26947 return;
26948 case RT_COLON_SCOPE:
26949 cp_parser_error (parser, "expected %<:%> or %<::%>");
26950 return;
26951 case RT_CLOSE_PAREN:
26952 cp_parser_error (parser, "expected %<)%>");
26953 return;
26954 case RT_COMMA_CLOSE_PAREN:
26955 cp_parser_error (parser, "expected %<,%> or %<)%>");
26956 return;
26957 case RT_PRAGMA_EOL:
26958 cp_parser_error (parser, "expected end of line");
26959 return;
26960 case RT_NAME:
26961 cp_parser_error (parser, "expected identifier");
26962 return;
26963 case RT_SELECT:
26964 cp_parser_error (parser, "expected selection-statement");
26965 return;
26966 case RT_INTERATION:
26967 cp_parser_error (parser, "expected iteration-statement");
26968 return;
26969 case RT_JUMP:
26970 cp_parser_error (parser, "expected jump-statement");
26971 return;
26972 case RT_CLASS_KEY:
26973 cp_parser_error (parser, "expected class-key");
26974 return;
26975 case RT_CLASS_TYPENAME_TEMPLATE:
26976 cp_parser_error (parser,
26977 "expected %<class%>, %<typename%>, or %<template%>");
26978 return;
26979 default:
26980 gcc_unreachable ();
26981 }
26982 }
26983 else
26984 gcc_unreachable ();
26985 }
26986
26987
26988
26989 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26990 issue an error message indicating that TOKEN_DESC was expected.
26991
26992 Returns the token consumed, if the token had the appropriate type.
26993 Otherwise, returns NULL. */
26994
26995 static cp_token *
26996 cp_parser_require (cp_parser* parser,
26997 enum cpp_ttype type,
26998 required_token token_desc)
26999 {
27000 if (cp_lexer_next_token_is (parser->lexer, type))
27001 return cp_lexer_consume_token (parser->lexer);
27002 else
27003 {
27004 /* Output the MESSAGE -- unless we're parsing tentatively. */
27005 if (!cp_parser_simulate_error (parser))
27006 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27007 return NULL;
27008 }
27009 }
27010
27011 /* An error message is produced if the next token is not '>'.
27012 All further tokens are skipped until the desired token is
27013 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27014
27015 static void
27016 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27017 {
27018 /* Current level of '< ... >'. */
27019 unsigned level = 0;
27020 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27021 unsigned nesting_depth = 0;
27022
27023 /* Are we ready, yet? If not, issue error message. */
27024 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27025 return;
27026
27027 /* Skip tokens until the desired token is found. */
27028 while (true)
27029 {
27030 /* Peek at the next token. */
27031 switch (cp_lexer_peek_token (parser->lexer)->type)
27032 {
27033 case CPP_LESS:
27034 if (!nesting_depth)
27035 ++level;
27036 break;
27037
27038 case CPP_RSHIFT:
27039 if (cxx_dialect == cxx98)
27040 /* C++0x views the `>>' operator as two `>' tokens, but
27041 C++98 does not. */
27042 break;
27043 else if (!nesting_depth && level-- == 0)
27044 {
27045 /* We've hit a `>>' where the first `>' closes the
27046 template argument list, and the second `>' is
27047 spurious. Just consume the `>>' and stop; we've
27048 already produced at least one error. */
27049 cp_lexer_consume_token (parser->lexer);
27050 return;
27051 }
27052 /* Fall through for C++0x, so we handle the second `>' in
27053 the `>>'. */
27054
27055 case CPP_GREATER:
27056 if (!nesting_depth && level-- == 0)
27057 {
27058 /* We've reached the token we want, consume it and stop. */
27059 cp_lexer_consume_token (parser->lexer);
27060 return;
27061 }
27062 break;
27063
27064 case CPP_OPEN_PAREN:
27065 case CPP_OPEN_SQUARE:
27066 ++nesting_depth;
27067 break;
27068
27069 case CPP_CLOSE_PAREN:
27070 case CPP_CLOSE_SQUARE:
27071 if (nesting_depth-- == 0)
27072 return;
27073 break;
27074
27075 case CPP_EOF:
27076 case CPP_PRAGMA_EOL:
27077 case CPP_SEMICOLON:
27078 case CPP_OPEN_BRACE:
27079 case CPP_CLOSE_BRACE:
27080 /* The '>' was probably forgotten, don't look further. */
27081 return;
27082
27083 default:
27084 break;
27085 }
27086
27087 /* Consume this token. */
27088 cp_lexer_consume_token (parser->lexer);
27089 }
27090 }
27091
27092 /* If the next token is the indicated keyword, consume it. Otherwise,
27093 issue an error message indicating that TOKEN_DESC was expected.
27094
27095 Returns the token consumed, if the token had the appropriate type.
27096 Otherwise, returns NULL. */
27097
27098 static cp_token *
27099 cp_parser_require_keyword (cp_parser* parser,
27100 enum rid keyword,
27101 required_token token_desc)
27102 {
27103 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27104
27105 if (token && token->keyword != keyword)
27106 {
27107 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27108 return NULL;
27109 }
27110
27111 return token;
27112 }
27113
27114 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27115 function-definition. */
27116
27117 static bool
27118 cp_parser_token_starts_function_definition_p (cp_token* token)
27119 {
27120 return (/* An ordinary function-body begins with an `{'. */
27121 token->type == CPP_OPEN_BRACE
27122 /* A ctor-initializer begins with a `:'. */
27123 || token->type == CPP_COLON
27124 /* A function-try-block begins with `try'. */
27125 || token->keyword == RID_TRY
27126 /* A function-transaction-block begins with `__transaction_atomic'
27127 or `__transaction_relaxed'. */
27128 || token->keyword == RID_TRANSACTION_ATOMIC
27129 || token->keyword == RID_TRANSACTION_RELAXED
27130 /* The named return value extension begins with `return'. */
27131 || token->keyword == RID_RETURN);
27132 }
27133
27134 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27135 definition. */
27136
27137 static bool
27138 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27139 {
27140 cp_token *token;
27141
27142 token = cp_lexer_peek_token (parser->lexer);
27143 return (token->type == CPP_OPEN_BRACE
27144 || (token->type == CPP_COLON
27145 && !parser->colon_doesnt_start_class_def_p));
27146 }
27147
27148 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27149 C++0x) ending a template-argument. */
27150
27151 static bool
27152 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27153 {
27154 cp_token *token;
27155
27156 token = cp_lexer_peek_token (parser->lexer);
27157 return (token->type == CPP_COMMA
27158 || token->type == CPP_GREATER
27159 || token->type == CPP_ELLIPSIS
27160 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27161 }
27162
27163 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27164 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27165
27166 static bool
27167 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27168 size_t n)
27169 {
27170 cp_token *token;
27171
27172 token = cp_lexer_peek_nth_token (parser->lexer, n);
27173 if (token->type == CPP_LESS)
27174 return true;
27175 /* Check for the sequence `<::' in the original code. It would be lexed as
27176 `[:', where `[' is a digraph, and there is no whitespace before
27177 `:'. */
27178 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27179 {
27180 cp_token *token2;
27181 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27182 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27183 return true;
27184 }
27185 return false;
27186 }
27187
27188 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27189 or none_type otherwise. */
27190
27191 static enum tag_types
27192 cp_parser_token_is_class_key (cp_token* token)
27193 {
27194 switch (token->keyword)
27195 {
27196 case RID_CLASS:
27197 return class_type;
27198 case RID_STRUCT:
27199 return record_type;
27200 case RID_UNION:
27201 return union_type;
27202
27203 default:
27204 return none_type;
27205 }
27206 }
27207
27208 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27209 or none_type otherwise or if the token is null. */
27210
27211 static enum tag_types
27212 cp_parser_token_is_type_parameter_key (cp_token* token)
27213 {
27214 if (!token)
27215 return none_type;
27216
27217 switch (token->keyword)
27218 {
27219 case RID_CLASS:
27220 return class_type;
27221 case RID_TYPENAME:
27222 return typename_type;
27223
27224 default:
27225 return none_type;
27226 }
27227 }
27228
27229 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27230
27231 static void
27232 cp_parser_check_class_key (enum tag_types class_key, tree type)
27233 {
27234 if (type == error_mark_node)
27235 return;
27236 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27237 {
27238 if (permerror (input_location, "%qs tag used in naming %q#T",
27239 class_key == union_type ? "union"
27240 : class_key == record_type ? "struct" : "class",
27241 type))
27242 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27243 "%q#T was previously declared here", type);
27244 }
27245 }
27246
27247 /* Issue an error message if DECL is redeclared with different
27248 access than its original declaration [class.access.spec/3].
27249 This applies to nested classes, nested class templates and
27250 enumerations [class.mem/1]. */
27251
27252 static void
27253 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27254 {
27255 if (!decl
27256 || (!CLASS_TYPE_P (TREE_TYPE (decl))
27257 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
27258 return;
27259
27260 if ((TREE_PRIVATE (decl)
27261 != (current_access_specifier == access_private_node))
27262 || (TREE_PROTECTED (decl)
27263 != (current_access_specifier == access_protected_node)))
27264 error_at (location, "%qD redeclared with different access", decl);
27265 }
27266
27267 /* Look for the `template' keyword, as a syntactic disambiguator.
27268 Return TRUE iff it is present, in which case it will be
27269 consumed. */
27270
27271 static bool
27272 cp_parser_optional_template_keyword (cp_parser *parser)
27273 {
27274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27275 {
27276 /* In C++98 the `template' keyword can only be used within templates;
27277 outside templates the parser can always figure out what is a
27278 template and what is not. In C++11, per the resolution of DR 468,
27279 `template' is allowed in cases where it is not strictly necessary. */
27280 if (!processing_template_decl
27281 && pedantic && cxx_dialect == cxx98)
27282 {
27283 cp_token *token = cp_lexer_peek_token (parser->lexer);
27284 pedwarn (token->location, OPT_Wpedantic,
27285 "in C++98 %<template%> (as a disambiguator) is only "
27286 "allowed within templates");
27287 /* If this part of the token stream is rescanned, the same
27288 error message would be generated. So, we purge the token
27289 from the stream. */
27290 cp_lexer_purge_token (parser->lexer);
27291 return false;
27292 }
27293 else
27294 {
27295 /* Consume the `template' keyword. */
27296 cp_lexer_consume_token (parser->lexer);
27297 return true;
27298 }
27299 }
27300 return false;
27301 }
27302
27303 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27304 set PARSER->SCOPE, and perform other related actions. */
27305
27306 static void
27307 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27308 {
27309 struct tree_check *check_value;
27310
27311 /* Get the stored value. */
27312 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27313 /* Set the scope from the stored value. */
27314 parser->scope = saved_checks_value (check_value);
27315 parser->qualifying_scope = check_value->qualifying_scope;
27316 parser->object_scope = NULL_TREE;
27317 }
27318
27319 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27320 encounter the end of a block before what we were looking for. */
27321
27322 static bool
27323 cp_parser_cache_group (cp_parser *parser,
27324 enum cpp_ttype end,
27325 unsigned depth)
27326 {
27327 while (true)
27328 {
27329 cp_token *token = cp_lexer_peek_token (parser->lexer);
27330
27331 /* Abort a parenthesized expression if we encounter a semicolon. */
27332 if ((end == CPP_CLOSE_PAREN || depth == 0)
27333 && token->type == CPP_SEMICOLON)
27334 return true;
27335 /* If we've reached the end of the file, stop. */
27336 if (token->type == CPP_EOF
27337 || (end != CPP_PRAGMA_EOL
27338 && token->type == CPP_PRAGMA_EOL))
27339 return true;
27340 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27341 /* We've hit the end of an enclosing block, so there's been some
27342 kind of syntax error. */
27343 return true;
27344
27345 /* Consume the token. */
27346 cp_lexer_consume_token (parser->lexer);
27347 /* See if it starts a new group. */
27348 if (token->type == CPP_OPEN_BRACE)
27349 {
27350 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27351 /* In theory this should probably check end == '}', but
27352 cp_parser_save_member_function_body needs it to exit
27353 after either '}' or ')' when called with ')'. */
27354 if (depth == 0)
27355 return false;
27356 }
27357 else if (token->type == CPP_OPEN_PAREN)
27358 {
27359 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27360 if (depth == 0 && end == CPP_CLOSE_PAREN)
27361 return false;
27362 }
27363 else if (token->type == CPP_PRAGMA)
27364 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27365 else if (token->type == end)
27366 return false;
27367 }
27368 }
27369
27370 /* Like above, for caching a default argument or NSDMI. Both of these are
27371 terminated by a non-nested comma, but it can be unclear whether or not a
27372 comma is nested in a template argument list unless we do more parsing.
27373 In order to handle this ambiguity, when we encounter a ',' after a '<'
27374 we try to parse what follows as a parameter-declaration-list (in the
27375 case of a default argument) or a member-declarator (in the case of an
27376 NSDMI). If that succeeds, then we stop caching. */
27377
27378 static tree
27379 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27380 {
27381 unsigned depth = 0;
27382 int maybe_template_id = 0;
27383 cp_token *first_token;
27384 cp_token *token;
27385 tree default_argument;
27386
27387 /* Add tokens until we have processed the entire default
27388 argument. We add the range [first_token, token). */
27389 first_token = cp_lexer_peek_token (parser->lexer);
27390 if (first_token->type == CPP_OPEN_BRACE)
27391 {
27392 /* For list-initialization, this is straightforward. */
27393 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27394 token = cp_lexer_peek_token (parser->lexer);
27395 }
27396 else while (true)
27397 {
27398 bool done = false;
27399
27400 /* Peek at the next token. */
27401 token = cp_lexer_peek_token (parser->lexer);
27402 /* What we do depends on what token we have. */
27403 switch (token->type)
27404 {
27405 /* In valid code, a default argument must be
27406 immediately followed by a `,' `)', or `...'. */
27407 case CPP_COMMA:
27408 if (depth == 0 && maybe_template_id)
27409 {
27410 /* If we've seen a '<', we might be in a
27411 template-argument-list. Until Core issue 325 is
27412 resolved, we don't know how this situation ought
27413 to be handled, so try to DTRT. We check whether
27414 what comes after the comma is a valid parameter
27415 declaration list. If it is, then the comma ends
27416 the default argument; otherwise the default
27417 argument continues. */
27418 bool error = false;
27419 cp_token *peek;
27420
27421 /* Set ITALP so cp_parser_parameter_declaration_list
27422 doesn't decide to commit to this parse. */
27423 bool saved_italp = parser->in_template_argument_list_p;
27424 parser->in_template_argument_list_p = true;
27425
27426 cp_parser_parse_tentatively (parser);
27427
27428 if (nsdmi)
27429 {
27430 /* Parse declarators until we reach a non-comma or
27431 somthing that cannot be an initializer.
27432 Just checking whether we're looking at a single
27433 declarator is insufficient. Consider:
27434 int var = tuple<T,U>::x;
27435 The template parameter 'U' looks exactly like a
27436 declarator. */
27437 do
27438 {
27439 int ctor_dtor_or_conv_p;
27440 cp_lexer_consume_token (parser->lexer);
27441 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27442 &ctor_dtor_or_conv_p,
27443 /*parenthesized_p=*/NULL,
27444 /*member_p=*/true,
27445 /*friend_p=*/false);
27446 peek = cp_lexer_peek_token (parser->lexer);
27447 if (cp_parser_error_occurred (parser))
27448 break;
27449 }
27450 while (peek->type == CPP_COMMA);
27451 /* If we met an '=' or ';' then the original comma
27452 was the end of the NSDMI. Otherwise assume
27453 we're still in the NSDMI. */
27454 error = (peek->type != CPP_EQ
27455 && peek->type != CPP_SEMICOLON);
27456 }
27457 else
27458 {
27459 cp_lexer_consume_token (parser->lexer);
27460 begin_scope (sk_function_parms, NULL_TREE);
27461 cp_parser_parameter_declaration_list (parser, &error);
27462 pop_bindings_and_leave_scope ();
27463 }
27464 if (!cp_parser_error_occurred (parser) && !error)
27465 done = true;
27466 cp_parser_abort_tentative_parse (parser);
27467
27468 parser->in_template_argument_list_p = saved_italp;
27469 break;
27470 }
27471 case CPP_CLOSE_PAREN:
27472 case CPP_ELLIPSIS:
27473 /* If we run into a non-nested `;', `}', or `]',
27474 then the code is invalid -- but the default
27475 argument is certainly over. */
27476 case CPP_SEMICOLON:
27477 case CPP_CLOSE_BRACE:
27478 case CPP_CLOSE_SQUARE:
27479 if (depth == 0
27480 /* Handle correctly int n = sizeof ... ( p ); */
27481 && token->type != CPP_ELLIPSIS)
27482 done = true;
27483 /* Update DEPTH, if necessary. */
27484 else if (token->type == CPP_CLOSE_PAREN
27485 || token->type == CPP_CLOSE_BRACE
27486 || token->type == CPP_CLOSE_SQUARE)
27487 --depth;
27488 break;
27489
27490 case CPP_OPEN_PAREN:
27491 case CPP_OPEN_SQUARE:
27492 case CPP_OPEN_BRACE:
27493 ++depth;
27494 break;
27495
27496 case CPP_LESS:
27497 if (depth == 0)
27498 /* This might be the comparison operator, or it might
27499 start a template argument list. */
27500 ++maybe_template_id;
27501 break;
27502
27503 case CPP_RSHIFT:
27504 if (cxx_dialect == cxx98)
27505 break;
27506 /* Fall through for C++0x, which treats the `>>'
27507 operator like two `>' tokens in certain
27508 cases. */
27509
27510 case CPP_GREATER:
27511 if (depth == 0)
27512 {
27513 /* This might be an operator, or it might close a
27514 template argument list. But if a previous '<'
27515 started a template argument list, this will have
27516 closed it, so we can't be in one anymore. */
27517 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27518 if (maybe_template_id < 0)
27519 maybe_template_id = 0;
27520 }
27521 break;
27522
27523 /* If we run out of tokens, issue an error message. */
27524 case CPP_EOF:
27525 case CPP_PRAGMA_EOL:
27526 error_at (token->location, "file ends in default argument");
27527 return error_mark_node;
27528
27529 case CPP_NAME:
27530 case CPP_SCOPE:
27531 /* In these cases, we should look for template-ids.
27532 For example, if the default argument is
27533 `X<int, double>()', we need to do name lookup to
27534 figure out whether or not `X' is a template; if
27535 so, the `,' does not end the default argument.
27536
27537 That is not yet done. */
27538 break;
27539
27540 default:
27541 break;
27542 }
27543
27544 /* If we've reached the end, stop. */
27545 if (done)
27546 break;
27547
27548 /* Add the token to the token block. */
27549 token = cp_lexer_consume_token (parser->lexer);
27550 }
27551
27552 /* Create a DEFAULT_ARG to represent the unparsed default
27553 argument. */
27554 default_argument = make_node (DEFAULT_ARG);
27555 DEFARG_TOKENS (default_argument)
27556 = cp_token_cache_new (first_token, token);
27557 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27558
27559 return default_argument;
27560 }
27561
27562 /* Begin parsing tentatively. We always save tokens while parsing
27563 tentatively so that if the tentative parsing fails we can restore the
27564 tokens. */
27565
27566 static void
27567 cp_parser_parse_tentatively (cp_parser* parser)
27568 {
27569 /* Enter a new parsing context. */
27570 parser->context = cp_parser_context_new (parser->context);
27571 /* Begin saving tokens. */
27572 cp_lexer_save_tokens (parser->lexer);
27573 /* In order to avoid repetitive access control error messages,
27574 access checks are queued up until we are no longer parsing
27575 tentatively. */
27576 push_deferring_access_checks (dk_deferred);
27577 }
27578
27579 /* Commit to the currently active tentative parse. */
27580
27581 static void
27582 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27583 {
27584 cp_parser_context *context;
27585 cp_lexer *lexer;
27586
27587 /* Mark all of the levels as committed. */
27588 lexer = parser->lexer;
27589 for (context = parser->context; context->next; context = context->next)
27590 {
27591 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27592 break;
27593 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27594 while (!cp_lexer_saving_tokens (lexer))
27595 lexer = lexer->next;
27596 cp_lexer_commit_tokens (lexer);
27597 }
27598 }
27599
27600 /* Commit to the topmost currently active tentative parse.
27601
27602 Note that this function shouldn't be called when there are
27603 irreversible side-effects while in a tentative state. For
27604 example, we shouldn't create a permanent entry in the symbol
27605 table, or issue an error message that might not apply if the
27606 tentative parse is aborted. */
27607
27608 static void
27609 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27610 {
27611 cp_parser_context *context = parser->context;
27612 cp_lexer *lexer = parser->lexer;
27613
27614 if (context)
27615 {
27616 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27617 return;
27618 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27619
27620 while (!cp_lexer_saving_tokens (lexer))
27621 lexer = lexer->next;
27622 cp_lexer_commit_tokens (lexer);
27623 }
27624 }
27625
27626 /* Abort the currently active tentative parse. All consumed tokens
27627 will be rolled back, and no diagnostics will be issued. */
27628
27629 static void
27630 cp_parser_abort_tentative_parse (cp_parser* parser)
27631 {
27632 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27633 || errorcount > 0);
27634 cp_parser_simulate_error (parser);
27635 /* Now, pretend that we want to see if the construct was
27636 successfully parsed. */
27637 cp_parser_parse_definitely (parser);
27638 }
27639
27640 /* Stop parsing tentatively. If a parse error has occurred, restore the
27641 token stream. Otherwise, commit to the tokens we have consumed.
27642 Returns true if no error occurred; false otherwise. */
27643
27644 static bool
27645 cp_parser_parse_definitely (cp_parser* parser)
27646 {
27647 bool error_occurred;
27648 cp_parser_context *context;
27649
27650 /* Remember whether or not an error occurred, since we are about to
27651 destroy that information. */
27652 error_occurred = cp_parser_error_occurred (parser);
27653 /* Remove the topmost context from the stack. */
27654 context = parser->context;
27655 parser->context = context->next;
27656 /* If no parse errors occurred, commit to the tentative parse. */
27657 if (!error_occurred)
27658 {
27659 /* Commit to the tokens read tentatively, unless that was
27660 already done. */
27661 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27662 cp_lexer_commit_tokens (parser->lexer);
27663
27664 pop_to_parent_deferring_access_checks ();
27665 }
27666 /* Otherwise, if errors occurred, roll back our state so that things
27667 are just as they were before we began the tentative parse. */
27668 else
27669 {
27670 cp_lexer_rollback_tokens (parser->lexer);
27671 pop_deferring_access_checks ();
27672 }
27673 /* Add the context to the front of the free list. */
27674 context->next = cp_parser_context_free_list;
27675 cp_parser_context_free_list = context;
27676
27677 return !error_occurred;
27678 }
27679
27680 /* Returns true if we are parsing tentatively and are not committed to
27681 this tentative parse. */
27682
27683 static bool
27684 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27685 {
27686 return (cp_parser_parsing_tentatively (parser)
27687 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27688 }
27689
27690 /* Returns nonzero iff an error has occurred during the most recent
27691 tentative parse. */
27692
27693 static bool
27694 cp_parser_error_occurred (cp_parser* parser)
27695 {
27696 return (cp_parser_parsing_tentatively (parser)
27697 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27698 }
27699
27700 /* Returns nonzero if GNU extensions are allowed. */
27701
27702 static bool
27703 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27704 {
27705 return parser->allow_gnu_extensions_p;
27706 }
27707 \f
27708 /* Objective-C++ Productions */
27709
27710
27711 /* Parse an Objective-C expression, which feeds into a primary-expression
27712 above.
27713
27714 objc-expression:
27715 objc-message-expression
27716 objc-string-literal
27717 objc-encode-expression
27718 objc-protocol-expression
27719 objc-selector-expression
27720
27721 Returns a tree representation of the expression. */
27722
27723 static cp_expr
27724 cp_parser_objc_expression (cp_parser* parser)
27725 {
27726 /* Try to figure out what kind of declaration is present. */
27727 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27728
27729 switch (kwd->type)
27730 {
27731 case CPP_OPEN_SQUARE:
27732 return cp_parser_objc_message_expression (parser);
27733
27734 case CPP_OBJC_STRING:
27735 kwd = cp_lexer_consume_token (parser->lexer);
27736 return objc_build_string_object (kwd->u.value);
27737
27738 case CPP_KEYWORD:
27739 switch (kwd->keyword)
27740 {
27741 case RID_AT_ENCODE:
27742 return cp_parser_objc_encode_expression (parser);
27743
27744 case RID_AT_PROTOCOL:
27745 return cp_parser_objc_protocol_expression (parser);
27746
27747 case RID_AT_SELECTOR:
27748 return cp_parser_objc_selector_expression (parser);
27749
27750 default:
27751 break;
27752 }
27753 default:
27754 error_at (kwd->location,
27755 "misplaced %<@%D%> Objective-C++ construct",
27756 kwd->u.value);
27757 cp_parser_skip_to_end_of_block_or_statement (parser);
27758 }
27759
27760 return error_mark_node;
27761 }
27762
27763 /* Parse an Objective-C message expression.
27764
27765 objc-message-expression:
27766 [ objc-message-receiver objc-message-args ]
27767
27768 Returns a representation of an Objective-C message. */
27769
27770 static tree
27771 cp_parser_objc_message_expression (cp_parser* parser)
27772 {
27773 tree receiver, messageargs;
27774
27775 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27776 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27777 receiver = cp_parser_objc_message_receiver (parser);
27778 messageargs = cp_parser_objc_message_args (parser);
27779 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27780 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27781
27782 tree result = objc_build_message_expr (receiver, messageargs);
27783
27784 /* Construct a location e.g.
27785 [self func1:5]
27786 ^~~~~~~~~~~~~~
27787 ranging from the '[' to the ']', with the caret at the start. */
27788 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27789 protected_set_expr_location (result, combined_loc);
27790
27791 return result;
27792 }
27793
27794 /* Parse an objc-message-receiver.
27795
27796 objc-message-receiver:
27797 expression
27798 simple-type-specifier
27799
27800 Returns a representation of the type or expression. */
27801
27802 static tree
27803 cp_parser_objc_message_receiver (cp_parser* parser)
27804 {
27805 tree rcv;
27806
27807 /* An Objective-C message receiver may be either (1) a type
27808 or (2) an expression. */
27809 cp_parser_parse_tentatively (parser);
27810 rcv = cp_parser_expression (parser);
27811
27812 /* If that worked out, fine. */
27813 if (cp_parser_parse_definitely (parser))
27814 return rcv;
27815
27816 cp_parser_parse_tentatively (parser);
27817 rcv = cp_parser_simple_type_specifier (parser,
27818 /*decl_specs=*/NULL,
27819 CP_PARSER_FLAGS_NONE);
27820
27821 if (cp_parser_parse_definitely (parser))
27822 return objc_get_class_reference (rcv);
27823
27824 cp_parser_error (parser, "objective-c++ message receiver expected");
27825 return error_mark_node;
27826 }
27827
27828 /* Parse the arguments and selectors comprising an Objective-C message.
27829
27830 objc-message-args:
27831 objc-selector
27832 objc-selector-args
27833 objc-selector-args , objc-comma-args
27834
27835 objc-selector-args:
27836 objc-selector [opt] : assignment-expression
27837 objc-selector-args objc-selector [opt] : assignment-expression
27838
27839 objc-comma-args:
27840 assignment-expression
27841 objc-comma-args , assignment-expression
27842
27843 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27844 selector arguments and TREE_VALUE containing a list of comma
27845 arguments. */
27846
27847 static tree
27848 cp_parser_objc_message_args (cp_parser* parser)
27849 {
27850 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27851 bool maybe_unary_selector_p = true;
27852 cp_token *token = cp_lexer_peek_token (parser->lexer);
27853
27854 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27855 {
27856 tree selector = NULL_TREE, arg;
27857
27858 if (token->type != CPP_COLON)
27859 selector = cp_parser_objc_selector (parser);
27860
27861 /* Detect if we have a unary selector. */
27862 if (maybe_unary_selector_p
27863 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27864 return build_tree_list (selector, NULL_TREE);
27865
27866 maybe_unary_selector_p = false;
27867 cp_parser_require (parser, CPP_COLON, RT_COLON);
27868 arg = cp_parser_assignment_expression (parser);
27869
27870 sel_args
27871 = chainon (sel_args,
27872 build_tree_list (selector, arg));
27873
27874 token = cp_lexer_peek_token (parser->lexer);
27875 }
27876
27877 /* Handle non-selector arguments, if any. */
27878 while (token->type == CPP_COMMA)
27879 {
27880 tree arg;
27881
27882 cp_lexer_consume_token (parser->lexer);
27883 arg = cp_parser_assignment_expression (parser);
27884
27885 addl_args
27886 = chainon (addl_args,
27887 build_tree_list (NULL_TREE, arg));
27888
27889 token = cp_lexer_peek_token (parser->lexer);
27890 }
27891
27892 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27893 {
27894 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27895 return build_tree_list (error_mark_node, error_mark_node);
27896 }
27897
27898 return build_tree_list (sel_args, addl_args);
27899 }
27900
27901 /* Parse an Objective-C encode expression.
27902
27903 objc-encode-expression:
27904 @encode objc-typename
27905
27906 Returns an encoded representation of the type argument. */
27907
27908 static cp_expr
27909 cp_parser_objc_encode_expression (cp_parser* parser)
27910 {
27911 tree type;
27912 cp_token *token;
27913 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27914
27915 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27916 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27917 token = cp_lexer_peek_token (parser->lexer);
27918 type = complete_type (cp_parser_type_id (parser));
27919 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27920
27921 if (!type)
27922 {
27923 error_at (token->location,
27924 "%<@encode%> must specify a type as an argument");
27925 return error_mark_node;
27926 }
27927
27928 /* This happens if we find @encode(T) (where T is a template
27929 typename or something dependent on a template typename) when
27930 parsing a template. In that case, we can't compile it
27931 immediately, but we rather create an AT_ENCODE_EXPR which will
27932 need to be instantiated when the template is used.
27933 */
27934 if (dependent_type_p (type))
27935 {
27936 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27937 TREE_READONLY (value) = 1;
27938 return value;
27939 }
27940
27941
27942 /* Build a location of the form:
27943 @encode(int)
27944 ^~~~~~~~~~~~
27945 with caret==start at the @ token, finishing at the close paren. */
27946 location_t combined_loc
27947 = make_location (start_loc, start_loc,
27948 cp_lexer_previous_token (parser->lexer)->location);
27949
27950 return cp_expr (objc_build_encode_expr (type), combined_loc);
27951 }
27952
27953 /* Parse an Objective-C @defs expression. */
27954
27955 static tree
27956 cp_parser_objc_defs_expression (cp_parser *parser)
27957 {
27958 tree name;
27959
27960 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27961 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27962 name = cp_parser_identifier (parser);
27963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27964
27965 return objc_get_class_ivars (name);
27966 }
27967
27968 /* Parse an Objective-C protocol expression.
27969
27970 objc-protocol-expression:
27971 @protocol ( identifier )
27972
27973 Returns a representation of the protocol expression. */
27974
27975 static tree
27976 cp_parser_objc_protocol_expression (cp_parser* parser)
27977 {
27978 tree proto;
27979 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27980
27981 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27982 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27983 proto = cp_parser_identifier (parser);
27984 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27985
27986 /* Build a location of the form:
27987 @protocol(prot)
27988 ^~~~~~~~~~~~~~~
27989 with caret==start at the @ token, finishing at the close paren. */
27990 location_t combined_loc
27991 = make_location (start_loc, start_loc,
27992 cp_lexer_previous_token (parser->lexer)->location);
27993 tree result = objc_build_protocol_expr (proto);
27994 protected_set_expr_location (result, combined_loc);
27995 return result;
27996 }
27997
27998 /* Parse an Objective-C selector expression.
27999
28000 objc-selector-expression:
28001 @selector ( objc-method-signature )
28002
28003 objc-method-signature:
28004 objc-selector
28005 objc-selector-seq
28006
28007 objc-selector-seq:
28008 objc-selector :
28009 objc-selector-seq objc-selector :
28010
28011 Returns a representation of the method selector. */
28012
28013 static tree
28014 cp_parser_objc_selector_expression (cp_parser* parser)
28015 {
28016 tree sel_seq = NULL_TREE;
28017 bool maybe_unary_selector_p = true;
28018 cp_token *token;
28019 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28020
28021 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28022 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28023 token = cp_lexer_peek_token (parser->lexer);
28024
28025 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28026 || token->type == CPP_SCOPE)
28027 {
28028 tree selector = NULL_TREE;
28029
28030 if (token->type != CPP_COLON
28031 || token->type == CPP_SCOPE)
28032 selector = cp_parser_objc_selector (parser);
28033
28034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28035 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28036 {
28037 /* Detect if we have a unary selector. */
28038 if (maybe_unary_selector_p)
28039 {
28040 sel_seq = selector;
28041 goto finish_selector;
28042 }
28043 else
28044 {
28045 cp_parser_error (parser, "expected %<:%>");
28046 }
28047 }
28048 maybe_unary_selector_p = false;
28049 token = cp_lexer_consume_token (parser->lexer);
28050
28051 if (token->type == CPP_SCOPE)
28052 {
28053 sel_seq
28054 = chainon (sel_seq,
28055 build_tree_list (selector, NULL_TREE));
28056 sel_seq
28057 = chainon (sel_seq,
28058 build_tree_list (NULL_TREE, NULL_TREE));
28059 }
28060 else
28061 sel_seq
28062 = chainon (sel_seq,
28063 build_tree_list (selector, NULL_TREE));
28064
28065 token = cp_lexer_peek_token (parser->lexer);
28066 }
28067
28068 finish_selector:
28069 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28070
28071
28072 /* Build a location of the form:
28073 @selector(func)
28074 ^~~~~~~~~~~~~~~
28075 with caret==start at the @ token, finishing at the close paren. */
28076 location_t combined_loc
28077 = make_location (loc, loc,
28078 cp_lexer_previous_token (parser->lexer)->location);
28079 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28080 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28081 protected_set_expr_location (result, combined_loc);
28082 return result;
28083 }
28084
28085 /* Parse a list of identifiers.
28086
28087 objc-identifier-list:
28088 identifier
28089 objc-identifier-list , identifier
28090
28091 Returns a TREE_LIST of identifier nodes. */
28092
28093 static tree
28094 cp_parser_objc_identifier_list (cp_parser* parser)
28095 {
28096 tree identifier;
28097 tree list;
28098 cp_token *sep;
28099
28100 identifier = cp_parser_identifier (parser);
28101 if (identifier == error_mark_node)
28102 return error_mark_node;
28103
28104 list = build_tree_list (NULL_TREE, identifier);
28105 sep = cp_lexer_peek_token (parser->lexer);
28106
28107 while (sep->type == CPP_COMMA)
28108 {
28109 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28110 identifier = cp_parser_identifier (parser);
28111 if (identifier == error_mark_node)
28112 return list;
28113
28114 list = chainon (list, build_tree_list (NULL_TREE,
28115 identifier));
28116 sep = cp_lexer_peek_token (parser->lexer);
28117 }
28118
28119 return list;
28120 }
28121
28122 /* Parse an Objective-C alias declaration.
28123
28124 objc-alias-declaration:
28125 @compatibility_alias identifier identifier ;
28126
28127 This function registers the alias mapping with the Objective-C front end.
28128 It returns nothing. */
28129
28130 static void
28131 cp_parser_objc_alias_declaration (cp_parser* parser)
28132 {
28133 tree alias, orig;
28134
28135 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28136 alias = cp_parser_identifier (parser);
28137 orig = cp_parser_identifier (parser);
28138 objc_declare_alias (alias, orig);
28139 cp_parser_consume_semicolon_at_end_of_statement (parser);
28140 }
28141
28142 /* Parse an Objective-C class forward-declaration.
28143
28144 objc-class-declaration:
28145 @class objc-identifier-list ;
28146
28147 The function registers the forward declarations with the Objective-C
28148 front end. It returns nothing. */
28149
28150 static void
28151 cp_parser_objc_class_declaration (cp_parser* parser)
28152 {
28153 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28154 while (true)
28155 {
28156 tree id;
28157
28158 id = cp_parser_identifier (parser);
28159 if (id == error_mark_node)
28160 break;
28161
28162 objc_declare_class (id);
28163
28164 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28165 cp_lexer_consume_token (parser->lexer);
28166 else
28167 break;
28168 }
28169 cp_parser_consume_semicolon_at_end_of_statement (parser);
28170 }
28171
28172 /* Parse a list of Objective-C protocol references.
28173
28174 objc-protocol-refs-opt:
28175 objc-protocol-refs [opt]
28176
28177 objc-protocol-refs:
28178 < objc-identifier-list >
28179
28180 Returns a TREE_LIST of identifiers, if any. */
28181
28182 static tree
28183 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28184 {
28185 tree protorefs = NULL_TREE;
28186
28187 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28188 {
28189 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28190 protorefs = cp_parser_objc_identifier_list (parser);
28191 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28192 }
28193
28194 return protorefs;
28195 }
28196
28197 /* Parse a Objective-C visibility specification. */
28198
28199 static void
28200 cp_parser_objc_visibility_spec (cp_parser* parser)
28201 {
28202 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28203
28204 switch (vis->keyword)
28205 {
28206 case RID_AT_PRIVATE:
28207 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28208 break;
28209 case RID_AT_PROTECTED:
28210 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28211 break;
28212 case RID_AT_PUBLIC:
28213 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28214 break;
28215 case RID_AT_PACKAGE:
28216 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28217 break;
28218 default:
28219 return;
28220 }
28221
28222 /* Eat '@private'/'@protected'/'@public'. */
28223 cp_lexer_consume_token (parser->lexer);
28224 }
28225
28226 /* Parse an Objective-C method type. Return 'true' if it is a class
28227 (+) method, and 'false' if it is an instance (-) method. */
28228
28229 static inline bool
28230 cp_parser_objc_method_type (cp_parser* parser)
28231 {
28232 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28233 return true;
28234 else
28235 return false;
28236 }
28237
28238 /* Parse an Objective-C protocol qualifier. */
28239
28240 static tree
28241 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28242 {
28243 tree quals = NULL_TREE, node;
28244 cp_token *token = cp_lexer_peek_token (parser->lexer);
28245
28246 node = token->u.value;
28247
28248 while (node && identifier_p (node)
28249 && (node == ridpointers [(int) RID_IN]
28250 || node == ridpointers [(int) RID_OUT]
28251 || node == ridpointers [(int) RID_INOUT]
28252 || node == ridpointers [(int) RID_BYCOPY]
28253 || node == ridpointers [(int) RID_BYREF]
28254 || node == ridpointers [(int) RID_ONEWAY]))
28255 {
28256 quals = tree_cons (NULL_TREE, node, quals);
28257 cp_lexer_consume_token (parser->lexer);
28258 token = cp_lexer_peek_token (parser->lexer);
28259 node = token->u.value;
28260 }
28261
28262 return quals;
28263 }
28264
28265 /* Parse an Objective-C typename. */
28266
28267 static tree
28268 cp_parser_objc_typename (cp_parser* parser)
28269 {
28270 tree type_name = NULL_TREE;
28271
28272 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28273 {
28274 tree proto_quals, cp_type = NULL_TREE;
28275
28276 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28277 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28278
28279 /* An ObjC type name may consist of just protocol qualifiers, in which
28280 case the type shall default to 'id'. */
28281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28282 {
28283 cp_type = cp_parser_type_id (parser);
28284
28285 /* If the type could not be parsed, an error has already
28286 been produced. For error recovery, behave as if it had
28287 not been specified, which will use the default type
28288 'id'. */
28289 if (cp_type == error_mark_node)
28290 {
28291 cp_type = NULL_TREE;
28292 /* We need to skip to the closing parenthesis as
28293 cp_parser_type_id() does not seem to do it for
28294 us. */
28295 cp_parser_skip_to_closing_parenthesis (parser,
28296 /*recovering=*/true,
28297 /*or_comma=*/false,
28298 /*consume_paren=*/false);
28299 }
28300 }
28301
28302 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28303 type_name = build_tree_list (proto_quals, cp_type);
28304 }
28305
28306 return type_name;
28307 }
28308
28309 /* Check to see if TYPE refers to an Objective-C selector name. */
28310
28311 static bool
28312 cp_parser_objc_selector_p (enum cpp_ttype type)
28313 {
28314 return (type == CPP_NAME || type == CPP_KEYWORD
28315 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28316 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28317 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28318 || type == CPP_XOR || type == CPP_XOR_EQ);
28319 }
28320
28321 /* Parse an Objective-C selector. */
28322
28323 static tree
28324 cp_parser_objc_selector (cp_parser* parser)
28325 {
28326 cp_token *token = cp_lexer_consume_token (parser->lexer);
28327
28328 if (!cp_parser_objc_selector_p (token->type))
28329 {
28330 error_at (token->location, "invalid Objective-C++ selector name");
28331 return error_mark_node;
28332 }
28333
28334 /* C++ operator names are allowed to appear in ObjC selectors. */
28335 switch (token->type)
28336 {
28337 case CPP_AND_AND: return get_identifier ("and");
28338 case CPP_AND_EQ: return get_identifier ("and_eq");
28339 case CPP_AND: return get_identifier ("bitand");
28340 case CPP_OR: return get_identifier ("bitor");
28341 case CPP_COMPL: return get_identifier ("compl");
28342 case CPP_NOT: return get_identifier ("not");
28343 case CPP_NOT_EQ: return get_identifier ("not_eq");
28344 case CPP_OR_OR: return get_identifier ("or");
28345 case CPP_OR_EQ: return get_identifier ("or_eq");
28346 case CPP_XOR: return get_identifier ("xor");
28347 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28348 default: return token->u.value;
28349 }
28350 }
28351
28352 /* Parse an Objective-C params list. */
28353
28354 static tree
28355 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28356 {
28357 tree params = NULL_TREE;
28358 bool maybe_unary_selector_p = true;
28359 cp_token *token = cp_lexer_peek_token (parser->lexer);
28360
28361 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28362 {
28363 tree selector = NULL_TREE, type_name, identifier;
28364 tree parm_attr = NULL_TREE;
28365
28366 if (token->keyword == RID_ATTRIBUTE)
28367 break;
28368
28369 if (token->type != CPP_COLON)
28370 selector = cp_parser_objc_selector (parser);
28371
28372 /* Detect if we have a unary selector. */
28373 if (maybe_unary_selector_p
28374 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28375 {
28376 params = selector; /* Might be followed by attributes. */
28377 break;
28378 }
28379
28380 maybe_unary_selector_p = false;
28381 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28382 {
28383 /* Something went quite wrong. There should be a colon
28384 here, but there is not. Stop parsing parameters. */
28385 break;
28386 }
28387 type_name = cp_parser_objc_typename (parser);
28388 /* New ObjC allows attributes on parameters too. */
28389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28390 parm_attr = cp_parser_attributes_opt (parser);
28391 identifier = cp_parser_identifier (parser);
28392
28393 params
28394 = chainon (params,
28395 objc_build_keyword_decl (selector,
28396 type_name,
28397 identifier,
28398 parm_attr));
28399
28400 token = cp_lexer_peek_token (parser->lexer);
28401 }
28402
28403 if (params == NULL_TREE)
28404 {
28405 cp_parser_error (parser, "objective-c++ method declaration is expected");
28406 return error_mark_node;
28407 }
28408
28409 /* We allow tail attributes for the method. */
28410 if (token->keyword == RID_ATTRIBUTE)
28411 {
28412 *attributes = cp_parser_attributes_opt (parser);
28413 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28414 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28415 return params;
28416 cp_parser_error (parser,
28417 "method attributes must be specified at the end");
28418 return error_mark_node;
28419 }
28420
28421 if (params == NULL_TREE)
28422 {
28423 cp_parser_error (parser, "objective-c++ method declaration is expected");
28424 return error_mark_node;
28425 }
28426 return params;
28427 }
28428
28429 /* Parse the non-keyword Objective-C params. */
28430
28431 static tree
28432 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28433 tree* attributes)
28434 {
28435 tree params = make_node (TREE_LIST);
28436 cp_token *token = cp_lexer_peek_token (parser->lexer);
28437 *ellipsisp = false; /* Initially, assume no ellipsis. */
28438
28439 while (token->type == CPP_COMMA)
28440 {
28441 cp_parameter_declarator *parmdecl;
28442 tree parm;
28443
28444 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28445 token = cp_lexer_peek_token (parser->lexer);
28446
28447 if (token->type == CPP_ELLIPSIS)
28448 {
28449 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28450 *ellipsisp = true;
28451 token = cp_lexer_peek_token (parser->lexer);
28452 break;
28453 }
28454
28455 /* TODO: parse attributes for tail parameters. */
28456 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28457 parm = grokdeclarator (parmdecl->declarator,
28458 &parmdecl->decl_specifiers,
28459 PARM, /*initialized=*/0,
28460 /*attrlist=*/NULL);
28461
28462 chainon (params, build_tree_list (NULL_TREE, parm));
28463 token = cp_lexer_peek_token (parser->lexer);
28464 }
28465
28466 /* We allow tail attributes for the method. */
28467 if (token->keyword == RID_ATTRIBUTE)
28468 {
28469 if (*attributes == NULL_TREE)
28470 {
28471 *attributes = cp_parser_attributes_opt (parser);
28472 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28473 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28474 return params;
28475 }
28476 else
28477 /* We have an error, but parse the attributes, so that we can
28478 carry on. */
28479 *attributes = cp_parser_attributes_opt (parser);
28480
28481 cp_parser_error (parser,
28482 "method attributes must be specified at the end");
28483 return error_mark_node;
28484 }
28485
28486 return params;
28487 }
28488
28489 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28490
28491 static void
28492 cp_parser_objc_interstitial_code (cp_parser* parser)
28493 {
28494 cp_token *token = cp_lexer_peek_token (parser->lexer);
28495
28496 /* If the next token is `extern' and the following token is a string
28497 literal, then we have a linkage specification. */
28498 if (token->keyword == RID_EXTERN
28499 && cp_parser_is_pure_string_literal
28500 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28501 cp_parser_linkage_specification (parser);
28502 /* Handle #pragma, if any. */
28503 else if (token->type == CPP_PRAGMA)
28504 cp_parser_pragma (parser, pragma_objc_icode, NULL);
28505 /* Allow stray semicolons. */
28506 else if (token->type == CPP_SEMICOLON)
28507 cp_lexer_consume_token (parser->lexer);
28508 /* Mark methods as optional or required, when building protocols. */
28509 else if (token->keyword == RID_AT_OPTIONAL)
28510 {
28511 cp_lexer_consume_token (parser->lexer);
28512 objc_set_method_opt (true);
28513 }
28514 else if (token->keyword == RID_AT_REQUIRED)
28515 {
28516 cp_lexer_consume_token (parser->lexer);
28517 objc_set_method_opt (false);
28518 }
28519 else if (token->keyword == RID_NAMESPACE)
28520 cp_parser_namespace_definition (parser);
28521 /* Other stray characters must generate errors. */
28522 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28523 {
28524 cp_lexer_consume_token (parser->lexer);
28525 error ("stray %qs between Objective-C++ methods",
28526 token->type == CPP_OPEN_BRACE ? "{" : "}");
28527 }
28528 /* Finally, try to parse a block-declaration, or a function-definition. */
28529 else
28530 cp_parser_block_declaration (parser, /*statement_p=*/false);
28531 }
28532
28533 /* Parse a method signature. */
28534
28535 static tree
28536 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28537 {
28538 tree rettype, kwdparms, optparms;
28539 bool ellipsis = false;
28540 bool is_class_method;
28541
28542 is_class_method = cp_parser_objc_method_type (parser);
28543 rettype = cp_parser_objc_typename (parser);
28544 *attributes = NULL_TREE;
28545 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28546 if (kwdparms == error_mark_node)
28547 return error_mark_node;
28548 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28549 if (optparms == error_mark_node)
28550 return error_mark_node;
28551
28552 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28553 }
28554
28555 static bool
28556 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28557 {
28558 tree tattr;
28559 cp_lexer_save_tokens (parser->lexer);
28560 tattr = cp_parser_attributes_opt (parser);
28561 gcc_assert (tattr) ;
28562
28563 /* If the attributes are followed by a method introducer, this is not allowed.
28564 Dump the attributes and flag the situation. */
28565 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28566 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28567 return true;
28568
28569 /* Otherwise, the attributes introduce some interstitial code, possibly so
28570 rewind to allow that check. */
28571 cp_lexer_rollback_tokens (parser->lexer);
28572 return false;
28573 }
28574
28575 /* Parse an Objective-C method prototype list. */
28576
28577 static void
28578 cp_parser_objc_method_prototype_list (cp_parser* parser)
28579 {
28580 cp_token *token = cp_lexer_peek_token (parser->lexer);
28581
28582 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28583 {
28584 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28585 {
28586 tree attributes, sig;
28587 bool is_class_method;
28588 if (token->type == CPP_PLUS)
28589 is_class_method = true;
28590 else
28591 is_class_method = false;
28592 sig = cp_parser_objc_method_signature (parser, &attributes);
28593 if (sig == error_mark_node)
28594 {
28595 cp_parser_skip_to_end_of_block_or_statement (parser);
28596 token = cp_lexer_peek_token (parser->lexer);
28597 continue;
28598 }
28599 objc_add_method_declaration (is_class_method, sig, attributes);
28600 cp_parser_consume_semicolon_at_end_of_statement (parser);
28601 }
28602 else if (token->keyword == RID_AT_PROPERTY)
28603 cp_parser_objc_at_property_declaration (parser);
28604 else if (token->keyword == RID_ATTRIBUTE
28605 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28606 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28607 OPT_Wattributes,
28608 "prefix attributes are ignored for methods");
28609 else
28610 /* Allow for interspersed non-ObjC++ code. */
28611 cp_parser_objc_interstitial_code (parser);
28612
28613 token = cp_lexer_peek_token (parser->lexer);
28614 }
28615
28616 if (token->type != CPP_EOF)
28617 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28618 else
28619 cp_parser_error (parser, "expected %<@end%>");
28620
28621 objc_finish_interface ();
28622 }
28623
28624 /* Parse an Objective-C method definition list. */
28625
28626 static void
28627 cp_parser_objc_method_definition_list (cp_parser* parser)
28628 {
28629 cp_token *token = cp_lexer_peek_token (parser->lexer);
28630
28631 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28632 {
28633 tree meth;
28634
28635 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28636 {
28637 cp_token *ptk;
28638 tree sig, attribute;
28639 bool is_class_method;
28640 if (token->type == CPP_PLUS)
28641 is_class_method = true;
28642 else
28643 is_class_method = false;
28644 push_deferring_access_checks (dk_deferred);
28645 sig = cp_parser_objc_method_signature (parser, &attribute);
28646 if (sig == error_mark_node)
28647 {
28648 cp_parser_skip_to_end_of_block_or_statement (parser);
28649 token = cp_lexer_peek_token (parser->lexer);
28650 continue;
28651 }
28652 objc_start_method_definition (is_class_method, sig, attribute,
28653 NULL_TREE);
28654
28655 /* For historical reasons, we accept an optional semicolon. */
28656 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28657 cp_lexer_consume_token (parser->lexer);
28658
28659 ptk = cp_lexer_peek_token (parser->lexer);
28660 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28661 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28662 {
28663 perform_deferred_access_checks (tf_warning_or_error);
28664 stop_deferring_access_checks ();
28665 meth = cp_parser_function_definition_after_declarator (parser,
28666 false);
28667 pop_deferring_access_checks ();
28668 objc_finish_method_definition (meth);
28669 }
28670 }
28671 /* The following case will be removed once @synthesize is
28672 completely implemented. */
28673 else if (token->keyword == RID_AT_PROPERTY)
28674 cp_parser_objc_at_property_declaration (parser);
28675 else if (token->keyword == RID_AT_SYNTHESIZE)
28676 cp_parser_objc_at_synthesize_declaration (parser);
28677 else if (token->keyword == RID_AT_DYNAMIC)
28678 cp_parser_objc_at_dynamic_declaration (parser);
28679 else if (token->keyword == RID_ATTRIBUTE
28680 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28681 warning_at (token->location, OPT_Wattributes,
28682 "prefix attributes are ignored for methods");
28683 else
28684 /* Allow for interspersed non-ObjC++ code. */
28685 cp_parser_objc_interstitial_code (parser);
28686
28687 token = cp_lexer_peek_token (parser->lexer);
28688 }
28689
28690 if (token->type != CPP_EOF)
28691 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28692 else
28693 cp_parser_error (parser, "expected %<@end%>");
28694
28695 objc_finish_implementation ();
28696 }
28697
28698 /* Parse Objective-C ivars. */
28699
28700 static void
28701 cp_parser_objc_class_ivars (cp_parser* parser)
28702 {
28703 cp_token *token = cp_lexer_peek_token (parser->lexer);
28704
28705 if (token->type != CPP_OPEN_BRACE)
28706 return; /* No ivars specified. */
28707
28708 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28709 token = cp_lexer_peek_token (parser->lexer);
28710
28711 while (token->type != CPP_CLOSE_BRACE
28712 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28713 {
28714 cp_decl_specifier_seq declspecs;
28715 int decl_class_or_enum_p;
28716 tree prefix_attributes;
28717
28718 cp_parser_objc_visibility_spec (parser);
28719
28720 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28721 break;
28722
28723 cp_parser_decl_specifier_seq (parser,
28724 CP_PARSER_FLAGS_OPTIONAL,
28725 &declspecs,
28726 &decl_class_or_enum_p);
28727
28728 /* auto, register, static, extern, mutable. */
28729 if (declspecs.storage_class != sc_none)
28730 {
28731 cp_parser_error (parser, "invalid type for instance variable");
28732 declspecs.storage_class = sc_none;
28733 }
28734
28735 /* thread_local. */
28736 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28737 {
28738 cp_parser_error (parser, "invalid type for instance variable");
28739 declspecs.locations[ds_thread] = 0;
28740 }
28741
28742 /* typedef. */
28743 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28744 {
28745 cp_parser_error (parser, "invalid type for instance variable");
28746 declspecs.locations[ds_typedef] = 0;
28747 }
28748
28749 prefix_attributes = declspecs.attributes;
28750 declspecs.attributes = NULL_TREE;
28751
28752 /* Keep going until we hit the `;' at the end of the
28753 declaration. */
28754 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28755 {
28756 tree width = NULL_TREE, attributes, first_attribute, decl;
28757 cp_declarator *declarator = NULL;
28758 int ctor_dtor_or_conv_p;
28759
28760 /* Check for a (possibly unnamed) bitfield declaration. */
28761 token = cp_lexer_peek_token (parser->lexer);
28762 if (token->type == CPP_COLON)
28763 goto eat_colon;
28764
28765 if (token->type == CPP_NAME
28766 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28767 == CPP_COLON))
28768 {
28769 /* Get the name of the bitfield. */
28770 declarator = make_id_declarator (NULL_TREE,
28771 cp_parser_identifier (parser),
28772 sfk_none);
28773
28774 eat_colon:
28775 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28776 /* Get the width of the bitfield. */
28777 width
28778 = cp_parser_constant_expression (parser);
28779 }
28780 else
28781 {
28782 /* Parse the declarator. */
28783 declarator
28784 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28785 &ctor_dtor_or_conv_p,
28786 /*parenthesized_p=*/NULL,
28787 /*member_p=*/false,
28788 /*friend_p=*/false);
28789 }
28790
28791 /* Look for attributes that apply to the ivar. */
28792 attributes = cp_parser_attributes_opt (parser);
28793 /* Remember which attributes are prefix attributes and
28794 which are not. */
28795 first_attribute = attributes;
28796 /* Combine the attributes. */
28797 attributes = chainon (prefix_attributes, attributes);
28798
28799 if (width)
28800 /* Create the bitfield declaration. */
28801 decl = grokbitfield (declarator, &declspecs,
28802 width,
28803 attributes);
28804 else
28805 decl = grokfield (declarator, &declspecs,
28806 NULL_TREE, /*init_const_expr_p=*/false,
28807 NULL_TREE, attributes);
28808
28809 /* Add the instance variable. */
28810 if (decl != error_mark_node && decl != NULL_TREE)
28811 objc_add_instance_variable (decl);
28812
28813 /* Reset PREFIX_ATTRIBUTES. */
28814 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28815 attributes = TREE_CHAIN (attributes);
28816 if (attributes)
28817 TREE_CHAIN (attributes) = NULL_TREE;
28818
28819 token = cp_lexer_peek_token (parser->lexer);
28820
28821 if (token->type == CPP_COMMA)
28822 {
28823 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28824 continue;
28825 }
28826 break;
28827 }
28828
28829 cp_parser_consume_semicolon_at_end_of_statement (parser);
28830 token = cp_lexer_peek_token (parser->lexer);
28831 }
28832
28833 if (token->keyword == RID_AT_END)
28834 cp_parser_error (parser, "expected %<}%>");
28835
28836 /* Do not consume the RID_AT_END, so it will be read again as terminating
28837 the @interface of @implementation. */
28838 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28839 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28840
28841 /* For historical reasons, we accept an optional semicolon. */
28842 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28843 cp_lexer_consume_token (parser->lexer);
28844 }
28845
28846 /* Parse an Objective-C protocol declaration. */
28847
28848 static void
28849 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28850 {
28851 tree proto, protorefs;
28852 cp_token *tok;
28853
28854 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28856 {
28857 tok = cp_lexer_peek_token (parser->lexer);
28858 error_at (tok->location, "identifier expected after %<@protocol%>");
28859 cp_parser_consume_semicolon_at_end_of_statement (parser);
28860 return;
28861 }
28862
28863 /* See if we have a forward declaration or a definition. */
28864 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28865
28866 /* Try a forward declaration first. */
28867 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28868 {
28869 while (true)
28870 {
28871 tree id;
28872
28873 id = cp_parser_identifier (parser);
28874 if (id == error_mark_node)
28875 break;
28876
28877 objc_declare_protocol (id, attributes);
28878
28879 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28880 cp_lexer_consume_token (parser->lexer);
28881 else
28882 break;
28883 }
28884 cp_parser_consume_semicolon_at_end_of_statement (parser);
28885 }
28886
28887 /* Ok, we got a full-fledged definition (or at least should). */
28888 else
28889 {
28890 proto = cp_parser_identifier (parser);
28891 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28892 objc_start_protocol (proto, protorefs, attributes);
28893 cp_parser_objc_method_prototype_list (parser);
28894 }
28895 }
28896
28897 /* Parse an Objective-C superclass or category. */
28898
28899 static void
28900 cp_parser_objc_superclass_or_category (cp_parser *parser,
28901 bool iface_p,
28902 tree *super,
28903 tree *categ, bool *is_class_extension)
28904 {
28905 cp_token *next = cp_lexer_peek_token (parser->lexer);
28906
28907 *super = *categ = NULL_TREE;
28908 *is_class_extension = false;
28909 if (next->type == CPP_COLON)
28910 {
28911 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28912 *super = cp_parser_identifier (parser);
28913 }
28914 else if (next->type == CPP_OPEN_PAREN)
28915 {
28916 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28917
28918 /* If there is no category name, and this is an @interface, we
28919 have a class extension. */
28920 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28921 {
28922 *categ = NULL_TREE;
28923 *is_class_extension = true;
28924 }
28925 else
28926 *categ = cp_parser_identifier (parser);
28927
28928 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28929 }
28930 }
28931
28932 /* Parse an Objective-C class interface. */
28933
28934 static void
28935 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28936 {
28937 tree name, super, categ, protos;
28938 bool is_class_extension;
28939
28940 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28941 name = cp_parser_identifier (parser);
28942 if (name == error_mark_node)
28943 {
28944 /* It's hard to recover because even if valid @interface stuff
28945 is to follow, we can't compile it (or validate it) if we
28946 don't even know which class it refers to. Let's assume this
28947 was a stray '@interface' token in the stream and skip it.
28948 */
28949 return;
28950 }
28951 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28952 &is_class_extension);
28953 protos = cp_parser_objc_protocol_refs_opt (parser);
28954
28955 /* We have either a class or a category on our hands. */
28956 if (categ || is_class_extension)
28957 objc_start_category_interface (name, categ, protos, attributes);
28958 else
28959 {
28960 objc_start_class_interface (name, super, protos, attributes);
28961 /* Handle instance variable declarations, if any. */
28962 cp_parser_objc_class_ivars (parser);
28963 objc_continue_interface ();
28964 }
28965
28966 cp_parser_objc_method_prototype_list (parser);
28967 }
28968
28969 /* Parse an Objective-C class implementation. */
28970
28971 static void
28972 cp_parser_objc_class_implementation (cp_parser* parser)
28973 {
28974 tree name, super, categ;
28975 bool is_class_extension;
28976
28977 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28978 name = cp_parser_identifier (parser);
28979 if (name == error_mark_node)
28980 {
28981 /* It's hard to recover because even if valid @implementation
28982 stuff is to follow, we can't compile it (or validate it) if
28983 we don't even know which class it refers to. Let's assume
28984 this was a stray '@implementation' token in the stream and
28985 skip it.
28986 */
28987 return;
28988 }
28989 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
28990 &is_class_extension);
28991
28992 /* We have either a class or a category on our hands. */
28993 if (categ)
28994 objc_start_category_implementation (name, categ);
28995 else
28996 {
28997 objc_start_class_implementation (name, super);
28998 /* Handle instance variable declarations, if any. */
28999 cp_parser_objc_class_ivars (parser);
29000 objc_continue_implementation ();
29001 }
29002
29003 cp_parser_objc_method_definition_list (parser);
29004 }
29005
29006 /* Consume the @end token and finish off the implementation. */
29007
29008 static void
29009 cp_parser_objc_end_implementation (cp_parser* parser)
29010 {
29011 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29012 objc_finish_implementation ();
29013 }
29014
29015 /* Parse an Objective-C declaration. */
29016
29017 static void
29018 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29019 {
29020 /* Try to figure out what kind of declaration is present. */
29021 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29022
29023 if (attributes)
29024 switch (kwd->keyword)
29025 {
29026 case RID_AT_ALIAS:
29027 case RID_AT_CLASS:
29028 case RID_AT_END:
29029 error_at (kwd->location, "attributes may not be specified before"
29030 " the %<@%D%> Objective-C++ keyword",
29031 kwd->u.value);
29032 attributes = NULL;
29033 break;
29034 case RID_AT_IMPLEMENTATION:
29035 warning_at (kwd->location, OPT_Wattributes,
29036 "prefix attributes are ignored before %<@%D%>",
29037 kwd->u.value);
29038 attributes = NULL;
29039 default:
29040 break;
29041 }
29042
29043 switch (kwd->keyword)
29044 {
29045 case RID_AT_ALIAS:
29046 cp_parser_objc_alias_declaration (parser);
29047 break;
29048 case RID_AT_CLASS:
29049 cp_parser_objc_class_declaration (parser);
29050 break;
29051 case RID_AT_PROTOCOL:
29052 cp_parser_objc_protocol_declaration (parser, attributes);
29053 break;
29054 case RID_AT_INTERFACE:
29055 cp_parser_objc_class_interface (parser, attributes);
29056 break;
29057 case RID_AT_IMPLEMENTATION:
29058 cp_parser_objc_class_implementation (parser);
29059 break;
29060 case RID_AT_END:
29061 cp_parser_objc_end_implementation (parser);
29062 break;
29063 default:
29064 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29065 kwd->u.value);
29066 cp_parser_skip_to_end_of_block_or_statement (parser);
29067 }
29068 }
29069
29070 /* Parse an Objective-C try-catch-finally statement.
29071
29072 objc-try-catch-finally-stmt:
29073 @try compound-statement objc-catch-clause-seq [opt]
29074 objc-finally-clause [opt]
29075
29076 objc-catch-clause-seq:
29077 objc-catch-clause objc-catch-clause-seq [opt]
29078
29079 objc-catch-clause:
29080 @catch ( objc-exception-declaration ) compound-statement
29081
29082 objc-finally-clause:
29083 @finally compound-statement
29084
29085 objc-exception-declaration:
29086 parameter-declaration
29087 '...'
29088
29089 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29090
29091 Returns NULL_TREE.
29092
29093 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29094 for C. Keep them in sync. */
29095
29096 static tree
29097 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29098 {
29099 location_t location;
29100 tree stmt;
29101
29102 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29103 location = cp_lexer_peek_token (parser->lexer)->location;
29104 objc_maybe_warn_exceptions (location);
29105 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29106 node, lest it get absorbed into the surrounding block. */
29107 stmt = push_stmt_list ();
29108 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29109 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29110
29111 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29112 {
29113 cp_parameter_declarator *parm;
29114 tree parameter_declaration = error_mark_node;
29115 bool seen_open_paren = false;
29116
29117 cp_lexer_consume_token (parser->lexer);
29118 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29119 seen_open_paren = true;
29120 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29121 {
29122 /* We have "@catch (...)" (where the '...' are literally
29123 what is in the code). Skip the '...'.
29124 parameter_declaration is set to NULL_TREE, and
29125 objc_being_catch_clauses() knows that that means
29126 '...'. */
29127 cp_lexer_consume_token (parser->lexer);
29128 parameter_declaration = NULL_TREE;
29129 }
29130 else
29131 {
29132 /* We have "@catch (NSException *exception)" or something
29133 like that. Parse the parameter declaration. */
29134 parm = cp_parser_parameter_declaration (parser, false, NULL);
29135 if (parm == NULL)
29136 parameter_declaration = error_mark_node;
29137 else
29138 parameter_declaration = grokdeclarator (parm->declarator,
29139 &parm->decl_specifiers,
29140 PARM, /*initialized=*/0,
29141 /*attrlist=*/NULL);
29142 }
29143 if (seen_open_paren)
29144 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29145 else
29146 {
29147 /* If there was no open parenthesis, we are recovering from
29148 an error, and we are trying to figure out what mistake
29149 the user has made. */
29150
29151 /* If there is an immediate closing parenthesis, the user
29152 probably forgot the opening one (ie, they typed "@catch
29153 NSException *e)". Parse the closing parenthesis and keep
29154 going. */
29155 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29156 cp_lexer_consume_token (parser->lexer);
29157
29158 /* If these is no immediate closing parenthesis, the user
29159 probably doesn't know that parenthesis are required at
29160 all (ie, they typed "@catch NSException *e"). So, just
29161 forget about the closing parenthesis and keep going. */
29162 }
29163 objc_begin_catch_clause (parameter_declaration);
29164 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29165 objc_finish_catch_clause ();
29166 }
29167 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29168 {
29169 cp_lexer_consume_token (parser->lexer);
29170 location = cp_lexer_peek_token (parser->lexer)->location;
29171 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29172 node, lest it get absorbed into the surrounding block. */
29173 stmt = push_stmt_list ();
29174 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29175 objc_build_finally_clause (location, pop_stmt_list (stmt));
29176 }
29177
29178 return objc_finish_try_stmt ();
29179 }
29180
29181 /* Parse an Objective-C synchronized statement.
29182
29183 objc-synchronized-stmt:
29184 @synchronized ( expression ) compound-statement
29185
29186 Returns NULL_TREE. */
29187
29188 static tree
29189 cp_parser_objc_synchronized_statement (cp_parser *parser)
29190 {
29191 location_t location;
29192 tree lock, stmt;
29193
29194 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29195
29196 location = cp_lexer_peek_token (parser->lexer)->location;
29197 objc_maybe_warn_exceptions (location);
29198 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29199 lock = cp_parser_expression (parser);
29200 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29201
29202 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29203 node, lest it get absorbed into the surrounding block. */
29204 stmt = push_stmt_list ();
29205 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29206
29207 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29208 }
29209
29210 /* Parse an Objective-C throw statement.
29211
29212 objc-throw-stmt:
29213 @throw assignment-expression [opt] ;
29214
29215 Returns a constructed '@throw' statement. */
29216
29217 static tree
29218 cp_parser_objc_throw_statement (cp_parser *parser)
29219 {
29220 tree expr = NULL_TREE;
29221 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29222
29223 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29224
29225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29226 expr = cp_parser_expression (parser);
29227
29228 cp_parser_consume_semicolon_at_end_of_statement (parser);
29229
29230 return objc_build_throw_stmt (loc, expr);
29231 }
29232
29233 /* Parse an Objective-C statement. */
29234
29235 static tree
29236 cp_parser_objc_statement (cp_parser * parser)
29237 {
29238 /* Try to figure out what kind of declaration is present. */
29239 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29240
29241 switch (kwd->keyword)
29242 {
29243 case RID_AT_TRY:
29244 return cp_parser_objc_try_catch_finally_statement (parser);
29245 case RID_AT_SYNCHRONIZED:
29246 return cp_parser_objc_synchronized_statement (parser);
29247 case RID_AT_THROW:
29248 return cp_parser_objc_throw_statement (parser);
29249 default:
29250 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29251 kwd->u.value);
29252 cp_parser_skip_to_end_of_block_or_statement (parser);
29253 }
29254
29255 return error_mark_node;
29256 }
29257
29258 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29259 look ahead to see if an objc keyword follows the attributes. This
29260 is to detect the use of prefix attributes on ObjC @interface and
29261 @protocol. */
29262
29263 static bool
29264 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29265 {
29266 cp_lexer_save_tokens (parser->lexer);
29267 *attrib = cp_parser_attributes_opt (parser);
29268 gcc_assert (*attrib);
29269 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29270 {
29271 cp_lexer_commit_tokens (parser->lexer);
29272 return true;
29273 }
29274 cp_lexer_rollback_tokens (parser->lexer);
29275 return false;
29276 }
29277
29278 /* This routine is a minimal replacement for
29279 c_parser_struct_declaration () used when parsing the list of
29280 types/names or ObjC++ properties. For example, when parsing the
29281 code
29282
29283 @property (readonly) int a, b, c;
29284
29285 this function is responsible for parsing "int a, int b, int c" and
29286 returning the declarations as CHAIN of DECLs.
29287
29288 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29289 similar parsing. */
29290 static tree
29291 cp_parser_objc_struct_declaration (cp_parser *parser)
29292 {
29293 tree decls = NULL_TREE;
29294 cp_decl_specifier_seq declspecs;
29295 int decl_class_or_enum_p;
29296 tree prefix_attributes;
29297
29298 cp_parser_decl_specifier_seq (parser,
29299 CP_PARSER_FLAGS_NONE,
29300 &declspecs,
29301 &decl_class_or_enum_p);
29302
29303 if (declspecs.type == error_mark_node)
29304 return error_mark_node;
29305
29306 /* auto, register, static, extern, mutable. */
29307 if (declspecs.storage_class != sc_none)
29308 {
29309 cp_parser_error (parser, "invalid type for property");
29310 declspecs.storage_class = sc_none;
29311 }
29312
29313 /* thread_local. */
29314 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29315 {
29316 cp_parser_error (parser, "invalid type for property");
29317 declspecs.locations[ds_thread] = 0;
29318 }
29319
29320 /* typedef. */
29321 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29322 {
29323 cp_parser_error (parser, "invalid type for property");
29324 declspecs.locations[ds_typedef] = 0;
29325 }
29326
29327 prefix_attributes = declspecs.attributes;
29328 declspecs.attributes = NULL_TREE;
29329
29330 /* Keep going until we hit the `;' at the end of the declaration. */
29331 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29332 {
29333 tree attributes, first_attribute, decl;
29334 cp_declarator *declarator;
29335 cp_token *token;
29336
29337 /* Parse the declarator. */
29338 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29339 NULL, NULL, false, false);
29340
29341 /* Look for attributes that apply to the ivar. */
29342 attributes = cp_parser_attributes_opt (parser);
29343 /* Remember which attributes are prefix attributes and
29344 which are not. */
29345 first_attribute = attributes;
29346 /* Combine the attributes. */
29347 attributes = chainon (prefix_attributes, attributes);
29348
29349 decl = grokfield (declarator, &declspecs,
29350 NULL_TREE, /*init_const_expr_p=*/false,
29351 NULL_TREE, attributes);
29352
29353 if (decl == error_mark_node || decl == NULL_TREE)
29354 return error_mark_node;
29355
29356 /* Reset PREFIX_ATTRIBUTES. */
29357 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29358 attributes = TREE_CHAIN (attributes);
29359 if (attributes)
29360 TREE_CHAIN (attributes) = NULL_TREE;
29361
29362 DECL_CHAIN (decl) = decls;
29363 decls = decl;
29364
29365 token = cp_lexer_peek_token (parser->lexer);
29366 if (token->type == CPP_COMMA)
29367 {
29368 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29369 continue;
29370 }
29371 else
29372 break;
29373 }
29374 return decls;
29375 }
29376
29377 /* Parse an Objective-C @property declaration. The syntax is:
29378
29379 objc-property-declaration:
29380 '@property' objc-property-attributes[opt] struct-declaration ;
29381
29382 objc-property-attributes:
29383 '(' objc-property-attribute-list ')'
29384
29385 objc-property-attribute-list:
29386 objc-property-attribute
29387 objc-property-attribute-list, objc-property-attribute
29388
29389 objc-property-attribute
29390 'getter' = identifier
29391 'setter' = identifier
29392 'readonly'
29393 'readwrite'
29394 'assign'
29395 'retain'
29396 'copy'
29397 'nonatomic'
29398
29399 For example:
29400 @property NSString *name;
29401 @property (readonly) id object;
29402 @property (retain, nonatomic, getter=getTheName) id name;
29403 @property int a, b, c;
29404
29405 PS: This function is identical to
29406 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29407 static void
29408 cp_parser_objc_at_property_declaration (cp_parser *parser)
29409 {
29410 /* The following variables hold the attributes of the properties as
29411 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29412 seen. When we see an attribute, we set them to 'true' (if they
29413 are boolean properties) or to the identifier (if they have an
29414 argument, ie, for getter and setter). Note that here we only
29415 parse the list of attributes, check the syntax and accumulate the
29416 attributes that we find. objc_add_property_declaration() will
29417 then process the information. */
29418 bool property_assign = false;
29419 bool property_copy = false;
29420 tree property_getter_ident = NULL_TREE;
29421 bool property_nonatomic = false;
29422 bool property_readonly = false;
29423 bool property_readwrite = false;
29424 bool property_retain = false;
29425 tree property_setter_ident = NULL_TREE;
29426
29427 /* 'properties' is the list of properties that we read. Usually a
29428 single one, but maybe more (eg, in "@property int a, b, c;" there
29429 are three). */
29430 tree properties;
29431 location_t loc;
29432
29433 loc = cp_lexer_peek_token (parser->lexer)->location;
29434
29435 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29436
29437 /* Parse the optional attribute list... */
29438 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29439 {
29440 /* Eat the '('. */
29441 cp_lexer_consume_token (parser->lexer);
29442
29443 while (true)
29444 {
29445 bool syntax_error = false;
29446 cp_token *token = cp_lexer_peek_token (parser->lexer);
29447 enum rid keyword;
29448
29449 if (token->type != CPP_NAME)
29450 {
29451 cp_parser_error (parser, "expected identifier");
29452 break;
29453 }
29454 keyword = C_RID_CODE (token->u.value);
29455 cp_lexer_consume_token (parser->lexer);
29456 switch (keyword)
29457 {
29458 case RID_ASSIGN: property_assign = true; break;
29459 case RID_COPY: property_copy = true; break;
29460 case RID_NONATOMIC: property_nonatomic = true; break;
29461 case RID_READONLY: property_readonly = true; break;
29462 case RID_READWRITE: property_readwrite = true; break;
29463 case RID_RETAIN: property_retain = true; break;
29464
29465 case RID_GETTER:
29466 case RID_SETTER:
29467 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29468 {
29469 if (keyword == RID_GETTER)
29470 cp_parser_error (parser,
29471 "missing %<=%> (after %<getter%> attribute)");
29472 else
29473 cp_parser_error (parser,
29474 "missing %<=%> (after %<setter%> attribute)");
29475 syntax_error = true;
29476 break;
29477 }
29478 cp_lexer_consume_token (parser->lexer); /* eat the = */
29479 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29480 {
29481 cp_parser_error (parser, "expected identifier");
29482 syntax_error = true;
29483 break;
29484 }
29485 if (keyword == RID_SETTER)
29486 {
29487 if (property_setter_ident != NULL_TREE)
29488 {
29489 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29490 cp_lexer_consume_token (parser->lexer);
29491 }
29492 else
29493 property_setter_ident = cp_parser_objc_selector (parser);
29494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29495 cp_parser_error (parser, "setter name must terminate with %<:%>");
29496 else
29497 cp_lexer_consume_token (parser->lexer);
29498 }
29499 else
29500 {
29501 if (property_getter_ident != NULL_TREE)
29502 {
29503 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29504 cp_lexer_consume_token (parser->lexer);
29505 }
29506 else
29507 property_getter_ident = cp_parser_objc_selector (parser);
29508 }
29509 break;
29510 default:
29511 cp_parser_error (parser, "unknown property attribute");
29512 syntax_error = true;
29513 break;
29514 }
29515
29516 if (syntax_error)
29517 break;
29518
29519 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29520 cp_lexer_consume_token (parser->lexer);
29521 else
29522 break;
29523 }
29524
29525 /* FIXME: "@property (setter, assign);" will generate a spurious
29526 "error: expected ‘)’ before ‘,’ token". This is because
29527 cp_parser_require, unlike the C counterpart, will produce an
29528 error even if we are in error recovery. */
29529 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29530 {
29531 cp_parser_skip_to_closing_parenthesis (parser,
29532 /*recovering=*/true,
29533 /*or_comma=*/false,
29534 /*consume_paren=*/true);
29535 }
29536 }
29537
29538 /* ... and the property declaration(s). */
29539 properties = cp_parser_objc_struct_declaration (parser);
29540
29541 if (properties == error_mark_node)
29542 {
29543 cp_parser_skip_to_end_of_statement (parser);
29544 /* If the next token is now a `;', consume it. */
29545 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29546 cp_lexer_consume_token (parser->lexer);
29547 return;
29548 }
29549
29550 if (properties == NULL_TREE)
29551 cp_parser_error (parser, "expected identifier");
29552 else
29553 {
29554 /* Comma-separated properties are chained together in
29555 reverse order; add them one by one. */
29556 properties = nreverse (properties);
29557
29558 for (; properties; properties = TREE_CHAIN (properties))
29559 objc_add_property_declaration (loc, copy_node (properties),
29560 property_readonly, property_readwrite,
29561 property_assign, property_retain,
29562 property_copy, property_nonatomic,
29563 property_getter_ident, property_setter_ident);
29564 }
29565
29566 cp_parser_consume_semicolon_at_end_of_statement (parser);
29567 }
29568
29569 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29570
29571 objc-synthesize-declaration:
29572 @synthesize objc-synthesize-identifier-list ;
29573
29574 objc-synthesize-identifier-list:
29575 objc-synthesize-identifier
29576 objc-synthesize-identifier-list, objc-synthesize-identifier
29577
29578 objc-synthesize-identifier
29579 identifier
29580 identifier = identifier
29581
29582 For example:
29583 @synthesize MyProperty;
29584 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29585
29586 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29587 for C. Keep them in sync.
29588 */
29589 static void
29590 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29591 {
29592 tree list = NULL_TREE;
29593 location_t loc;
29594 loc = cp_lexer_peek_token (parser->lexer)->location;
29595
29596 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29597 while (true)
29598 {
29599 tree property, ivar;
29600 property = cp_parser_identifier (parser);
29601 if (property == error_mark_node)
29602 {
29603 cp_parser_consume_semicolon_at_end_of_statement (parser);
29604 return;
29605 }
29606 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29607 {
29608 cp_lexer_consume_token (parser->lexer);
29609 ivar = cp_parser_identifier (parser);
29610 if (ivar == error_mark_node)
29611 {
29612 cp_parser_consume_semicolon_at_end_of_statement (parser);
29613 return;
29614 }
29615 }
29616 else
29617 ivar = NULL_TREE;
29618 list = chainon (list, build_tree_list (ivar, property));
29619 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29620 cp_lexer_consume_token (parser->lexer);
29621 else
29622 break;
29623 }
29624 cp_parser_consume_semicolon_at_end_of_statement (parser);
29625 objc_add_synthesize_declaration (loc, list);
29626 }
29627
29628 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29629
29630 objc-dynamic-declaration:
29631 @dynamic identifier-list ;
29632
29633 For example:
29634 @dynamic MyProperty;
29635 @dynamic MyProperty, AnotherProperty;
29636
29637 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29638 for C. Keep them in sync.
29639 */
29640 static void
29641 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29642 {
29643 tree list = NULL_TREE;
29644 location_t loc;
29645 loc = cp_lexer_peek_token (parser->lexer)->location;
29646
29647 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29648 while (true)
29649 {
29650 tree property;
29651 property = cp_parser_identifier (parser);
29652 if (property == error_mark_node)
29653 {
29654 cp_parser_consume_semicolon_at_end_of_statement (parser);
29655 return;
29656 }
29657 list = chainon (list, build_tree_list (NULL, property));
29658 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29659 cp_lexer_consume_token (parser->lexer);
29660 else
29661 break;
29662 }
29663 cp_parser_consume_semicolon_at_end_of_statement (parser);
29664 objc_add_dynamic_declaration (loc, list);
29665 }
29666
29667 \f
29668 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29669
29670 /* Returns name of the next clause.
29671 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29672 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29673 returned and the token is consumed. */
29674
29675 static pragma_omp_clause
29676 cp_parser_omp_clause_name (cp_parser *parser)
29677 {
29678 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29679
29680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29681 result = PRAGMA_OACC_CLAUSE_AUTO;
29682 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29683 result = PRAGMA_OMP_CLAUSE_IF;
29684 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29685 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29686 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29687 result = PRAGMA_OACC_CLAUSE_DELETE;
29688 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29689 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29690 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29691 result = PRAGMA_OMP_CLAUSE_FOR;
29692 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29693 {
29694 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29695 const char *p = IDENTIFIER_POINTER (id);
29696
29697 switch (p[0])
29698 {
29699 case 'a':
29700 if (!strcmp ("aligned", p))
29701 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29702 else if (!strcmp ("async", p))
29703 result = PRAGMA_OACC_CLAUSE_ASYNC;
29704 break;
29705 case 'c':
29706 if (!strcmp ("collapse", p))
29707 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29708 else if (!strcmp ("copy", p))
29709 result = PRAGMA_OACC_CLAUSE_COPY;
29710 else if (!strcmp ("copyin", p))
29711 result = PRAGMA_OMP_CLAUSE_COPYIN;
29712 else if (!strcmp ("copyout", p))
29713 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29714 else if (!strcmp ("copyprivate", p))
29715 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29716 else if (!strcmp ("create", p))
29717 result = PRAGMA_OACC_CLAUSE_CREATE;
29718 break;
29719 case 'd':
29720 if (!strcmp ("defaultmap", p))
29721 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29722 else if (!strcmp ("depend", p))
29723 result = PRAGMA_OMP_CLAUSE_DEPEND;
29724 else if (!strcmp ("device", p))
29725 result = PRAGMA_OMP_CLAUSE_DEVICE;
29726 else if (!strcmp ("deviceptr", p))
29727 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29728 else if (!strcmp ("device_resident", p))
29729 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29730 else if (!strcmp ("dist_schedule", p))
29731 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29732 break;
29733 case 'f':
29734 if (!strcmp ("final", p))
29735 result = PRAGMA_OMP_CLAUSE_FINAL;
29736 else if (!strcmp ("firstprivate", p))
29737 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29738 else if (!strcmp ("from", p))
29739 result = PRAGMA_OMP_CLAUSE_FROM;
29740 break;
29741 case 'g':
29742 if (!strcmp ("gang", p))
29743 result = PRAGMA_OACC_CLAUSE_GANG;
29744 else if (!strcmp ("grainsize", p))
29745 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29746 break;
29747 case 'h':
29748 if (!strcmp ("hint", p))
29749 result = PRAGMA_OMP_CLAUSE_HINT;
29750 else if (!strcmp ("host", p))
29751 result = PRAGMA_OACC_CLAUSE_HOST;
29752 break;
29753 case 'i':
29754 if (!strcmp ("inbranch", p))
29755 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29756 else if (!strcmp ("independent", p))
29757 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29758 else if (!strcmp ("is_device_ptr", p))
29759 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29760 break;
29761 case 'l':
29762 if (!strcmp ("lastprivate", p))
29763 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29764 else if (!strcmp ("linear", p))
29765 result = PRAGMA_OMP_CLAUSE_LINEAR;
29766 else if (!strcmp ("link", p))
29767 result = PRAGMA_OMP_CLAUSE_LINK;
29768 break;
29769 case 'm':
29770 if (!strcmp ("map", p))
29771 result = PRAGMA_OMP_CLAUSE_MAP;
29772 else if (!strcmp ("mergeable", p))
29773 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29774 else if (flag_cilkplus && !strcmp ("mask", p))
29775 result = PRAGMA_CILK_CLAUSE_MASK;
29776 break;
29777 case 'n':
29778 if (!strcmp ("nogroup", p))
29779 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29780 else if (!strcmp ("notinbranch", p))
29781 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29782 else if (!strcmp ("nowait", p))
29783 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29784 else if (flag_cilkplus && !strcmp ("nomask", p))
29785 result = PRAGMA_CILK_CLAUSE_NOMASK;
29786 else if (!strcmp ("num_gangs", p))
29787 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29788 else if (!strcmp ("num_tasks", p))
29789 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29790 else if (!strcmp ("num_teams", p))
29791 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29792 else if (!strcmp ("num_threads", p))
29793 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29794 else if (!strcmp ("num_workers", p))
29795 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29796 break;
29797 case 'o':
29798 if (!strcmp ("ordered", p))
29799 result = PRAGMA_OMP_CLAUSE_ORDERED;
29800 break;
29801 case 'p':
29802 if (!strcmp ("parallel", p))
29803 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29804 else if (!strcmp ("present", p))
29805 result = PRAGMA_OACC_CLAUSE_PRESENT;
29806 else if (!strcmp ("present_or_copy", p)
29807 || !strcmp ("pcopy", p))
29808 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29809 else if (!strcmp ("present_or_copyin", p)
29810 || !strcmp ("pcopyin", p))
29811 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29812 else if (!strcmp ("present_or_copyout", p)
29813 || !strcmp ("pcopyout", p))
29814 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29815 else if (!strcmp ("present_or_create", p)
29816 || !strcmp ("pcreate", p))
29817 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29818 else if (!strcmp ("priority", p))
29819 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29820 else if (!strcmp ("proc_bind", p))
29821 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29822 break;
29823 case 'r':
29824 if (!strcmp ("reduction", p))
29825 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29826 break;
29827 case 's':
29828 if (!strcmp ("safelen", p))
29829 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29830 else if (!strcmp ("schedule", p))
29831 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29832 else if (!strcmp ("sections", p))
29833 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29834 else if (!strcmp ("self", p))
29835 result = PRAGMA_OACC_CLAUSE_SELF;
29836 else if (!strcmp ("seq", p))
29837 result = PRAGMA_OACC_CLAUSE_SEQ;
29838 else if (!strcmp ("shared", p))
29839 result = PRAGMA_OMP_CLAUSE_SHARED;
29840 else if (!strcmp ("simd", p))
29841 result = PRAGMA_OMP_CLAUSE_SIMD;
29842 else if (!strcmp ("simdlen", p))
29843 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29844 break;
29845 case 't':
29846 if (!strcmp ("taskgroup", p))
29847 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29848 else if (!strcmp ("thread_limit", p))
29849 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29850 else if (!strcmp ("threads", p))
29851 result = PRAGMA_OMP_CLAUSE_THREADS;
29852 else if (!strcmp ("tile", p))
29853 result = PRAGMA_OACC_CLAUSE_TILE;
29854 else if (!strcmp ("to", p))
29855 result = PRAGMA_OMP_CLAUSE_TO;
29856 break;
29857 case 'u':
29858 if (!strcmp ("uniform", p))
29859 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29860 else if (!strcmp ("untied", p))
29861 result = PRAGMA_OMP_CLAUSE_UNTIED;
29862 else if (!strcmp ("use_device", p))
29863 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29864 else if (!strcmp ("use_device_ptr", p))
29865 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29866 break;
29867 case 'v':
29868 if (!strcmp ("vector", p))
29869 result = PRAGMA_OACC_CLAUSE_VECTOR;
29870 else if (!strcmp ("vector_length", p))
29871 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29872 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29873 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29874 break;
29875 case 'w':
29876 if (!strcmp ("wait", p))
29877 result = PRAGMA_OACC_CLAUSE_WAIT;
29878 else if (!strcmp ("worker", p))
29879 result = PRAGMA_OACC_CLAUSE_WORKER;
29880 break;
29881 }
29882 }
29883
29884 if (result != PRAGMA_OMP_CLAUSE_NONE)
29885 cp_lexer_consume_token (parser->lexer);
29886
29887 return result;
29888 }
29889
29890 /* Validate that a clause of the given type does not already exist. */
29891
29892 static void
29893 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29894 const char *name, location_t location)
29895 {
29896 tree c;
29897
29898 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29899 if (OMP_CLAUSE_CODE (c) == code)
29900 {
29901 error_at (location, "too many %qs clauses", name);
29902 break;
29903 }
29904 }
29905
29906 /* OpenMP 2.5:
29907 variable-list:
29908 identifier
29909 variable-list , identifier
29910
29911 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29912 colon). An opening parenthesis will have been consumed by the caller.
29913
29914 If KIND is nonzero, create the appropriate node and install the decl
29915 in OMP_CLAUSE_DECL and add the node to the head of the list.
29916
29917 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29918 return the list created.
29919
29920 COLON can be NULL if only closing parenthesis should end the list,
29921 or pointer to bool which will receive false if the list is terminated
29922 by closing parenthesis or true if the list is terminated by colon. */
29923
29924 static tree
29925 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29926 tree list, bool *colon)
29927 {
29928 cp_token *token;
29929 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29930 if (colon)
29931 {
29932 parser->colon_corrects_to_scope_p = false;
29933 *colon = false;
29934 }
29935 while (1)
29936 {
29937 tree name, decl;
29938
29939 token = cp_lexer_peek_token (parser->lexer);
29940 if (kind != 0
29941 && current_class_ptr
29942 && cp_parser_is_keyword (token, RID_THIS))
29943 {
29944 decl = finish_this_expr ();
29945 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29946 || CONVERT_EXPR_P (decl))
29947 decl = TREE_OPERAND (decl, 0);
29948 cp_lexer_consume_token (parser->lexer);
29949 }
29950 else
29951 {
29952 name = cp_parser_id_expression (parser, /*template_p=*/false,
29953 /*check_dependency_p=*/true,
29954 /*template_p=*/NULL,
29955 /*declarator_p=*/false,
29956 /*optional_p=*/false);
29957 if (name == error_mark_node)
29958 goto skip_comma;
29959
29960 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29961 if (decl == error_mark_node)
29962 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29963 token->location);
29964 }
29965 if (decl == error_mark_node)
29966 ;
29967 else if (kind != 0)
29968 {
29969 switch (kind)
29970 {
29971 case OMP_CLAUSE__CACHE_:
29972 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29973 {
29974 error_at (token->location, "expected %<[%>");
29975 decl = error_mark_node;
29976 break;
29977 }
29978 /* FALLTHROUGH. */
29979 case OMP_CLAUSE_MAP:
29980 case OMP_CLAUSE_FROM:
29981 case OMP_CLAUSE_TO:
29982 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
29983 {
29984 location_t loc
29985 = cp_lexer_peek_token (parser->lexer)->location;
29986 cp_id_kind idk = CP_ID_KIND_NONE;
29987 cp_lexer_consume_token (parser->lexer);
29988 decl
29989 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
29990 decl, false,
29991 &idk, loc);
29992 }
29993 /* FALLTHROUGH. */
29994 case OMP_CLAUSE_DEPEND:
29995 case OMP_CLAUSE_REDUCTION:
29996 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29997 {
29998 tree low_bound = NULL_TREE, length = NULL_TREE;
29999
30000 parser->colon_corrects_to_scope_p = false;
30001 cp_lexer_consume_token (parser->lexer);
30002 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30003 low_bound = cp_parser_expression (parser);
30004 if (!colon)
30005 parser->colon_corrects_to_scope_p
30006 = saved_colon_corrects_to_scope_p;
30007 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30008 length = integer_one_node;
30009 else
30010 {
30011 /* Look for `:'. */
30012 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30013 goto skip_comma;
30014 if (!cp_lexer_next_token_is (parser->lexer,
30015 CPP_CLOSE_SQUARE))
30016 length = cp_parser_expression (parser);
30017 }
30018 /* Look for the closing `]'. */
30019 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30020 RT_CLOSE_SQUARE))
30021 goto skip_comma;
30022
30023 if (kind == OMP_CLAUSE__CACHE_)
30024 {
30025 if (TREE_CODE (low_bound) != INTEGER_CST
30026 && !TREE_READONLY (low_bound))
30027 {
30028 error_at (token->location,
30029 "%qD is not a constant", low_bound);
30030 decl = error_mark_node;
30031 }
30032
30033 if (TREE_CODE (length) != INTEGER_CST
30034 && !TREE_READONLY (length))
30035 {
30036 error_at (token->location,
30037 "%qD is not a constant", length);
30038 decl = error_mark_node;
30039 }
30040 }
30041
30042 decl = tree_cons (low_bound, length, decl);
30043 }
30044 break;
30045 default:
30046 break;
30047 }
30048
30049 tree u = build_omp_clause (token->location, kind);
30050 OMP_CLAUSE_DECL (u) = decl;
30051 OMP_CLAUSE_CHAIN (u) = list;
30052 list = u;
30053 }
30054 else
30055 list = tree_cons (decl, NULL_TREE, list);
30056
30057 get_comma:
30058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30059 break;
30060 cp_lexer_consume_token (parser->lexer);
30061 }
30062
30063 if (colon)
30064 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30065
30066 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30067 {
30068 *colon = true;
30069 cp_parser_require (parser, CPP_COLON, RT_COLON);
30070 return list;
30071 }
30072
30073 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30074 {
30075 int ending;
30076
30077 /* Try to resync to an unnested comma. Copied from
30078 cp_parser_parenthesized_expression_list. */
30079 skip_comma:
30080 if (colon)
30081 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30082 ending = cp_parser_skip_to_closing_parenthesis (parser,
30083 /*recovering=*/true,
30084 /*or_comma=*/true,
30085 /*consume_paren=*/true);
30086 if (ending < 0)
30087 goto get_comma;
30088 }
30089
30090 return list;
30091 }
30092
30093 /* Similarly, but expect leading and trailing parenthesis. This is a very
30094 common case for omp clauses. */
30095
30096 static tree
30097 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30098 {
30099 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30100 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30101 return list;
30102 }
30103
30104 /* OpenACC 2.0:
30105 copy ( variable-list )
30106 copyin ( variable-list )
30107 copyout ( variable-list )
30108 create ( variable-list )
30109 delete ( variable-list )
30110 present ( variable-list )
30111 present_or_copy ( variable-list )
30112 pcopy ( variable-list )
30113 present_or_copyin ( variable-list )
30114 pcopyin ( variable-list )
30115 present_or_copyout ( variable-list )
30116 pcopyout ( variable-list )
30117 present_or_create ( variable-list )
30118 pcreate ( variable-list ) */
30119
30120 static tree
30121 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30122 tree list)
30123 {
30124 enum gomp_map_kind kind;
30125 switch (c_kind)
30126 {
30127 case PRAGMA_OACC_CLAUSE_COPY:
30128 kind = GOMP_MAP_FORCE_TOFROM;
30129 break;
30130 case PRAGMA_OACC_CLAUSE_COPYIN:
30131 kind = GOMP_MAP_FORCE_TO;
30132 break;
30133 case PRAGMA_OACC_CLAUSE_COPYOUT:
30134 kind = GOMP_MAP_FORCE_FROM;
30135 break;
30136 case PRAGMA_OACC_CLAUSE_CREATE:
30137 kind = GOMP_MAP_FORCE_ALLOC;
30138 break;
30139 case PRAGMA_OACC_CLAUSE_DELETE:
30140 kind = GOMP_MAP_DELETE;
30141 break;
30142 case PRAGMA_OACC_CLAUSE_DEVICE:
30143 kind = GOMP_MAP_FORCE_TO;
30144 break;
30145 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30146 kind = GOMP_MAP_DEVICE_RESIDENT;
30147 break;
30148 case PRAGMA_OACC_CLAUSE_HOST:
30149 case PRAGMA_OACC_CLAUSE_SELF:
30150 kind = GOMP_MAP_FORCE_FROM;
30151 break;
30152 case PRAGMA_OACC_CLAUSE_LINK:
30153 kind = GOMP_MAP_LINK;
30154 break;
30155 case PRAGMA_OACC_CLAUSE_PRESENT:
30156 kind = GOMP_MAP_FORCE_PRESENT;
30157 break;
30158 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30159 kind = GOMP_MAP_TOFROM;
30160 break;
30161 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30162 kind = GOMP_MAP_TO;
30163 break;
30164 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30165 kind = GOMP_MAP_FROM;
30166 break;
30167 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30168 kind = GOMP_MAP_ALLOC;
30169 break;
30170 default:
30171 gcc_unreachable ();
30172 }
30173 tree nl, c;
30174 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30175
30176 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30177 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30178
30179 return nl;
30180 }
30181
30182 /* OpenACC 2.0:
30183 deviceptr ( variable-list ) */
30184
30185 static tree
30186 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30187 {
30188 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30189 tree vars, t;
30190
30191 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30192 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30193 variable-list must only allow for pointer variables. */
30194 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30195 for (t = vars; t; t = TREE_CHAIN (t))
30196 {
30197 tree v = TREE_PURPOSE (t);
30198 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30199 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30200 OMP_CLAUSE_DECL (u) = v;
30201 OMP_CLAUSE_CHAIN (u) = list;
30202 list = u;
30203 }
30204
30205 return list;
30206 }
30207
30208 /* OpenACC 2.0:
30209 auto
30210 independent
30211 nohost
30212 seq */
30213
30214 static tree
30215 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30216 enum omp_clause_code code,
30217 tree list, location_t location)
30218 {
30219 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30220 tree c = build_omp_clause (location, code);
30221 OMP_CLAUSE_CHAIN (c) = list;
30222 return c;
30223 }
30224
30225 /* OpenACC:
30226 num_gangs ( expression )
30227 num_workers ( expression )
30228 vector_length ( expression ) */
30229
30230 static tree
30231 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30232 const char *str, tree list)
30233 {
30234 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30235
30236 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30237 return list;
30238
30239 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30240
30241 if (t == error_mark_node
30242 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30243 {
30244 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30245 /*or_comma=*/false,
30246 /*consume_paren=*/true);
30247 return list;
30248 }
30249
30250 check_no_duplicate_clause (list, code, str, loc);
30251
30252 tree c = build_omp_clause (loc, code);
30253 OMP_CLAUSE_OPERAND (c, 0) = t;
30254 OMP_CLAUSE_CHAIN (c) = list;
30255 return c;
30256 }
30257
30258 /* OpenACC:
30259
30260 gang [( gang-arg-list )]
30261 worker [( [num:] int-expr )]
30262 vector [( [length:] int-expr )]
30263
30264 where gang-arg is one of:
30265
30266 [num:] int-expr
30267 static: size-expr
30268
30269 and size-expr may be:
30270
30271 *
30272 int-expr
30273 */
30274
30275 static tree
30276 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30277 const char *str, tree list)
30278 {
30279 const char *id = "num";
30280 cp_lexer *lexer = parser->lexer;
30281 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30282 location_t loc = cp_lexer_peek_token (lexer)->location;
30283
30284 if (kind == OMP_CLAUSE_VECTOR)
30285 id = "length";
30286
30287 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30288 {
30289 cp_lexer_consume_token (lexer);
30290
30291 do
30292 {
30293 cp_token *next = cp_lexer_peek_token (lexer);
30294 int idx = 0;
30295
30296 /* Gang static argument. */
30297 if (kind == OMP_CLAUSE_GANG
30298 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30299 {
30300 cp_lexer_consume_token (lexer);
30301
30302 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30303 goto cleanup_error;
30304
30305 idx = 1;
30306 if (ops[idx] != NULL)
30307 {
30308 cp_parser_error (parser, "too many %<static%> arguments");
30309 goto cleanup_error;
30310 }
30311
30312 /* Check for the '*' argument. */
30313 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30314 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30315 || cp_lexer_nth_token_is (parser->lexer, 2,
30316 CPP_CLOSE_PAREN)))
30317 {
30318 cp_lexer_consume_token (lexer);
30319 ops[idx] = integer_minus_one_node;
30320
30321 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30322 {
30323 cp_lexer_consume_token (lexer);
30324 continue;
30325 }
30326 else break;
30327 }
30328 }
30329 /* Worker num: argument and vector length: arguments. */
30330 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30331 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30332 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30333 {
30334 cp_lexer_consume_token (lexer); /* id */
30335 cp_lexer_consume_token (lexer); /* ':' */
30336 }
30337
30338 /* Now collect the actual argument. */
30339 if (ops[idx] != NULL_TREE)
30340 {
30341 cp_parser_error (parser, "unexpected argument");
30342 goto cleanup_error;
30343 }
30344
30345 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30346 false);
30347 if (expr == error_mark_node)
30348 goto cleanup_error;
30349
30350 mark_exp_read (expr);
30351 ops[idx] = expr;
30352
30353 if (kind == OMP_CLAUSE_GANG
30354 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30355 {
30356 cp_lexer_consume_token (lexer);
30357 continue;
30358 }
30359 break;
30360 }
30361 while (1);
30362
30363 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30364 goto cleanup_error;
30365 }
30366
30367 check_no_duplicate_clause (list, kind, str, loc);
30368
30369 c = build_omp_clause (loc, kind);
30370
30371 if (ops[1])
30372 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30373
30374 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30375 OMP_CLAUSE_CHAIN (c) = list;
30376
30377 return c;
30378
30379 cleanup_error:
30380 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30381 return list;
30382 }
30383
30384 /* OpenACC 2.0:
30385 tile ( size-expr-list ) */
30386
30387 static tree
30388 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30389 {
30390 tree c, expr = error_mark_node;
30391 tree tile = NULL_TREE;
30392
30393 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30394
30395 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30396 return list;
30397
30398 do
30399 {
30400 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30401 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30402 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30403 {
30404 cp_lexer_consume_token (parser->lexer);
30405 expr = integer_minus_one_node;
30406 }
30407 else
30408 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30409
30410 if (expr == error_mark_node)
30411 return list;
30412
30413 tile = tree_cons (NULL_TREE, expr, tile);
30414
30415 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30416 cp_lexer_consume_token (parser->lexer);
30417 }
30418 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30419
30420 /* Consume the trailing ')'. */
30421 cp_lexer_consume_token (parser->lexer);
30422
30423 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30424 tile = nreverse (tile);
30425 OMP_CLAUSE_TILE_LIST (c) = tile;
30426 OMP_CLAUSE_CHAIN (c) = list;
30427 return c;
30428 }
30429
30430 /* OpenACC 2.0
30431 Parse wait clause or directive parameters. */
30432
30433 static tree
30434 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30435 {
30436 vec<tree, va_gc> *args;
30437 tree t, args_tree;
30438
30439 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30440 /*cast_p=*/false,
30441 /*allow_expansion_p=*/true,
30442 /*non_constant_p=*/NULL);
30443
30444 if (args == NULL || args->length () == 0)
30445 {
30446 cp_parser_error (parser, "expected integer expression before ')'");
30447 if (args != NULL)
30448 release_tree_vector (args);
30449 return list;
30450 }
30451
30452 args_tree = build_tree_list_vec (args);
30453
30454 release_tree_vector (args);
30455
30456 for (t = args_tree; t; t = TREE_CHAIN (t))
30457 {
30458 tree targ = TREE_VALUE (t);
30459
30460 if (targ != error_mark_node)
30461 {
30462 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30463 error ("%<wait%> expression must be integral");
30464 else
30465 {
30466 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30467
30468 mark_rvalue_use (targ);
30469 OMP_CLAUSE_DECL (c) = targ;
30470 OMP_CLAUSE_CHAIN (c) = list;
30471 list = c;
30472 }
30473 }
30474 }
30475
30476 return list;
30477 }
30478
30479 /* OpenACC:
30480 wait ( int-expr-list ) */
30481
30482 static tree
30483 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30484 {
30485 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30486
30487 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30488 return list;
30489
30490 list = cp_parser_oacc_wait_list (parser, location, list);
30491
30492 return list;
30493 }
30494
30495 /* OpenMP 3.0:
30496 collapse ( constant-expression ) */
30497
30498 static tree
30499 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30500 {
30501 tree c, num;
30502 location_t loc;
30503 HOST_WIDE_INT n;
30504
30505 loc = cp_lexer_peek_token (parser->lexer)->location;
30506 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30507 return list;
30508
30509 num = cp_parser_constant_expression (parser);
30510
30511 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30512 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30513 /*or_comma=*/false,
30514 /*consume_paren=*/true);
30515
30516 if (num == error_mark_node)
30517 return list;
30518 num = fold_non_dependent_expr (num);
30519 if (!tree_fits_shwi_p (num)
30520 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30521 || (n = tree_to_shwi (num)) <= 0
30522 || (int) n != n)
30523 {
30524 error_at (loc, "collapse argument needs positive constant integer expression");
30525 return list;
30526 }
30527
30528 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30529 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30530 OMP_CLAUSE_CHAIN (c) = list;
30531 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30532
30533 return c;
30534 }
30535
30536 /* OpenMP 2.5:
30537 default ( shared | none )
30538
30539 OpenACC 2.0
30540 default (none) */
30541
30542 static tree
30543 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30544 location_t location, bool is_oacc)
30545 {
30546 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30547 tree c;
30548
30549 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30550 return list;
30551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30552 {
30553 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30554 const char *p = IDENTIFIER_POINTER (id);
30555
30556 switch (p[0])
30557 {
30558 case 'n':
30559 if (strcmp ("none", p) != 0)
30560 goto invalid_kind;
30561 kind = OMP_CLAUSE_DEFAULT_NONE;
30562 break;
30563
30564 case 's':
30565 if (strcmp ("shared", p) != 0 || is_oacc)
30566 goto invalid_kind;
30567 kind = OMP_CLAUSE_DEFAULT_SHARED;
30568 break;
30569
30570 default:
30571 goto invalid_kind;
30572 }
30573
30574 cp_lexer_consume_token (parser->lexer);
30575 }
30576 else
30577 {
30578 invalid_kind:
30579 if (is_oacc)
30580 cp_parser_error (parser, "expected %<none%>");
30581 else
30582 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30583 }
30584
30585 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30586 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30587 /*or_comma=*/false,
30588 /*consume_paren=*/true);
30589
30590 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30591 return list;
30592
30593 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30594 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30595 OMP_CLAUSE_CHAIN (c) = list;
30596 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30597
30598 return c;
30599 }
30600
30601 /* OpenMP 3.1:
30602 final ( expression ) */
30603
30604 static tree
30605 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30606 {
30607 tree t, c;
30608
30609 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30610 return list;
30611
30612 t = cp_parser_condition (parser);
30613
30614 if (t == error_mark_node
30615 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30616 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30617 /*or_comma=*/false,
30618 /*consume_paren=*/true);
30619
30620 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30621
30622 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30623 OMP_CLAUSE_FINAL_EXPR (c) = t;
30624 OMP_CLAUSE_CHAIN (c) = list;
30625
30626 return c;
30627 }
30628
30629 /* OpenMP 2.5:
30630 if ( expression )
30631
30632 OpenMP 4.5:
30633 if ( directive-name-modifier : expression )
30634
30635 directive-name-modifier:
30636 parallel | task | taskloop | target data | target | target update
30637 | target enter data | target exit data */
30638
30639 static tree
30640 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30641 bool is_omp)
30642 {
30643 tree t, c;
30644 enum tree_code if_modifier = ERROR_MARK;
30645
30646 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30647 return list;
30648
30649 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30650 {
30651 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30652 const char *p = IDENTIFIER_POINTER (id);
30653 int n = 2;
30654
30655 if (strcmp ("parallel", p) == 0)
30656 if_modifier = OMP_PARALLEL;
30657 else if (strcmp ("task", p) == 0)
30658 if_modifier = OMP_TASK;
30659 else if (strcmp ("taskloop", p) == 0)
30660 if_modifier = OMP_TASKLOOP;
30661 else if (strcmp ("target", p) == 0)
30662 {
30663 if_modifier = OMP_TARGET;
30664 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30665 {
30666 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30667 p = IDENTIFIER_POINTER (id);
30668 if (strcmp ("data", p) == 0)
30669 if_modifier = OMP_TARGET_DATA;
30670 else if (strcmp ("update", p) == 0)
30671 if_modifier = OMP_TARGET_UPDATE;
30672 else if (strcmp ("enter", p) == 0)
30673 if_modifier = OMP_TARGET_ENTER_DATA;
30674 else if (strcmp ("exit", p) == 0)
30675 if_modifier = OMP_TARGET_EXIT_DATA;
30676 if (if_modifier != OMP_TARGET)
30677 n = 3;
30678 else
30679 {
30680 location_t loc
30681 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30682 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30683 "or %<exit%>");
30684 if_modifier = ERROR_MARK;
30685 }
30686 if (if_modifier == OMP_TARGET_ENTER_DATA
30687 || if_modifier == OMP_TARGET_EXIT_DATA)
30688 {
30689 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30690 {
30691 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30692 p = IDENTIFIER_POINTER (id);
30693 if (strcmp ("data", p) == 0)
30694 n = 4;
30695 }
30696 if (n != 4)
30697 {
30698 location_t loc
30699 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30700 error_at (loc, "expected %<data%>");
30701 if_modifier = ERROR_MARK;
30702 }
30703 }
30704 }
30705 }
30706 if (if_modifier != ERROR_MARK)
30707 {
30708 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30709 {
30710 while (n-- > 0)
30711 cp_lexer_consume_token (parser->lexer);
30712 }
30713 else
30714 {
30715 if (n > 2)
30716 {
30717 location_t loc
30718 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30719 error_at (loc, "expected %<:%>");
30720 }
30721 if_modifier = ERROR_MARK;
30722 }
30723 }
30724 }
30725
30726 t = cp_parser_condition (parser);
30727
30728 if (t == error_mark_node
30729 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30730 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30731 /*or_comma=*/false,
30732 /*consume_paren=*/true);
30733
30734 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30735 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30736 {
30737 if (if_modifier != ERROR_MARK
30738 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30739 {
30740 const char *p = NULL;
30741 switch (if_modifier)
30742 {
30743 case OMP_PARALLEL: p = "parallel"; break;
30744 case OMP_TASK: p = "task"; break;
30745 case OMP_TASKLOOP: p = "taskloop"; break;
30746 case OMP_TARGET_DATA: p = "target data"; break;
30747 case OMP_TARGET: p = "target"; break;
30748 case OMP_TARGET_UPDATE: p = "target update"; break;
30749 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30750 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30751 default: gcc_unreachable ();
30752 }
30753 error_at (location, "too many %<if%> clauses with %qs modifier",
30754 p);
30755 return list;
30756 }
30757 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30758 {
30759 if (!is_omp)
30760 error_at (location, "too many %<if%> clauses");
30761 else
30762 error_at (location, "too many %<if%> clauses without modifier");
30763 return list;
30764 }
30765 else if (if_modifier == ERROR_MARK
30766 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30767 {
30768 error_at (location, "if any %<if%> clause has modifier, then all "
30769 "%<if%> clauses have to use modifier");
30770 return list;
30771 }
30772 }
30773
30774 c = build_omp_clause (location, OMP_CLAUSE_IF);
30775 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30776 OMP_CLAUSE_IF_EXPR (c) = t;
30777 OMP_CLAUSE_CHAIN (c) = list;
30778
30779 return c;
30780 }
30781
30782 /* OpenMP 3.1:
30783 mergeable */
30784
30785 static tree
30786 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30787 tree list, location_t location)
30788 {
30789 tree c;
30790
30791 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30792 location);
30793
30794 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30795 OMP_CLAUSE_CHAIN (c) = list;
30796 return c;
30797 }
30798
30799 /* OpenMP 2.5:
30800 nowait */
30801
30802 static tree
30803 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30804 tree list, location_t location)
30805 {
30806 tree c;
30807
30808 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30809
30810 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30811 OMP_CLAUSE_CHAIN (c) = list;
30812 return c;
30813 }
30814
30815 /* OpenMP 2.5:
30816 num_threads ( expression ) */
30817
30818 static tree
30819 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30820 location_t location)
30821 {
30822 tree t, c;
30823
30824 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30825 return list;
30826
30827 t = cp_parser_expression (parser);
30828
30829 if (t == error_mark_node
30830 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30831 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30832 /*or_comma=*/false,
30833 /*consume_paren=*/true);
30834
30835 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30836 "num_threads", location);
30837
30838 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30839 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30840 OMP_CLAUSE_CHAIN (c) = list;
30841
30842 return c;
30843 }
30844
30845 /* OpenMP 4.5:
30846 num_tasks ( expression ) */
30847
30848 static tree
30849 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30850 location_t location)
30851 {
30852 tree t, c;
30853
30854 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30855 return list;
30856
30857 t = cp_parser_expression (parser);
30858
30859 if (t == error_mark_node
30860 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30861 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30862 /*or_comma=*/false,
30863 /*consume_paren=*/true);
30864
30865 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30866 "num_tasks", location);
30867
30868 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30869 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30870 OMP_CLAUSE_CHAIN (c) = list;
30871
30872 return c;
30873 }
30874
30875 /* OpenMP 4.5:
30876 grainsize ( expression ) */
30877
30878 static tree
30879 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30880 location_t location)
30881 {
30882 tree t, c;
30883
30884 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30885 return list;
30886
30887 t = cp_parser_expression (parser);
30888
30889 if (t == error_mark_node
30890 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30891 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30892 /*or_comma=*/false,
30893 /*consume_paren=*/true);
30894
30895 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30896 "grainsize", location);
30897
30898 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30899 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30900 OMP_CLAUSE_CHAIN (c) = list;
30901
30902 return c;
30903 }
30904
30905 /* OpenMP 4.5:
30906 priority ( expression ) */
30907
30908 static tree
30909 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30910 location_t location)
30911 {
30912 tree t, c;
30913
30914 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30915 return list;
30916
30917 t = cp_parser_expression (parser);
30918
30919 if (t == error_mark_node
30920 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30921 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30922 /*or_comma=*/false,
30923 /*consume_paren=*/true);
30924
30925 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30926 "priority", location);
30927
30928 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30929 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30930 OMP_CLAUSE_CHAIN (c) = list;
30931
30932 return c;
30933 }
30934
30935 /* OpenMP 4.5:
30936 hint ( expression ) */
30937
30938 static tree
30939 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30940 location_t location)
30941 {
30942 tree t, c;
30943
30944 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30945 return list;
30946
30947 t = cp_parser_expression (parser);
30948
30949 if (t == error_mark_node
30950 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30951 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30952 /*or_comma=*/false,
30953 /*consume_paren=*/true);
30954
30955 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30956
30957 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30958 OMP_CLAUSE_HINT_EXPR (c) = t;
30959 OMP_CLAUSE_CHAIN (c) = list;
30960
30961 return c;
30962 }
30963
30964 /* OpenMP 4.5:
30965 defaultmap ( tofrom : scalar ) */
30966
30967 static tree
30968 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30969 location_t location)
30970 {
30971 tree c, id;
30972 const char *p;
30973
30974 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30975 return list;
30976
30977 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30978 {
30979 cp_parser_error (parser, "expected %<tofrom%>");
30980 goto out_err;
30981 }
30982 id = cp_lexer_peek_token (parser->lexer)->u.value;
30983 p = IDENTIFIER_POINTER (id);
30984 if (strcmp (p, "tofrom") != 0)
30985 {
30986 cp_parser_error (parser, "expected %<tofrom%>");
30987 goto out_err;
30988 }
30989 cp_lexer_consume_token (parser->lexer);
30990 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30991 goto out_err;
30992
30993 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30994 {
30995 cp_parser_error (parser, "expected %<scalar%>");
30996 goto out_err;
30997 }
30998 id = cp_lexer_peek_token (parser->lexer)->u.value;
30999 p = IDENTIFIER_POINTER (id);
31000 if (strcmp (p, "scalar") != 0)
31001 {
31002 cp_parser_error (parser, "expected %<scalar%>");
31003 goto out_err;
31004 }
31005 cp_lexer_consume_token (parser->lexer);
31006 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31007 goto out_err;
31008
31009 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31010 location);
31011
31012 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31013 OMP_CLAUSE_CHAIN (c) = list;
31014 return c;
31015
31016 out_err:
31017 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31018 /*or_comma=*/false,
31019 /*consume_paren=*/true);
31020 return list;
31021 }
31022
31023 /* OpenMP 2.5:
31024 ordered
31025
31026 OpenMP 4.5:
31027 ordered ( constant-expression ) */
31028
31029 static tree
31030 cp_parser_omp_clause_ordered (cp_parser *parser,
31031 tree list, location_t location)
31032 {
31033 tree c, num = NULL_TREE;
31034 HOST_WIDE_INT n;
31035
31036 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31037 "ordered", location);
31038
31039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31040 {
31041 cp_lexer_consume_token (parser->lexer);
31042
31043 num = cp_parser_constant_expression (parser);
31044
31045 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31046 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31047 /*or_comma=*/false,
31048 /*consume_paren=*/true);
31049
31050 if (num == error_mark_node)
31051 return list;
31052 num = fold_non_dependent_expr (num);
31053 if (!tree_fits_shwi_p (num)
31054 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31055 || (n = tree_to_shwi (num)) <= 0
31056 || (int) n != n)
31057 {
31058 error_at (location,
31059 "ordered argument needs positive constant integer "
31060 "expression");
31061 return list;
31062 }
31063 }
31064
31065 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31066 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31067 OMP_CLAUSE_CHAIN (c) = list;
31068 return c;
31069 }
31070
31071 /* OpenMP 2.5:
31072 reduction ( reduction-operator : variable-list )
31073
31074 reduction-operator:
31075 One of: + * - & ^ | && ||
31076
31077 OpenMP 3.1:
31078
31079 reduction-operator:
31080 One of: + * - & ^ | && || min max
31081
31082 OpenMP 4.0:
31083
31084 reduction-operator:
31085 One of: + * - & ^ | && ||
31086 id-expression */
31087
31088 static tree
31089 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31090 {
31091 enum tree_code code = ERROR_MARK;
31092 tree nlist, c, id = NULL_TREE;
31093
31094 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31095 return list;
31096
31097 switch (cp_lexer_peek_token (parser->lexer)->type)
31098 {
31099 case CPP_PLUS: code = PLUS_EXPR; break;
31100 case CPP_MULT: code = MULT_EXPR; break;
31101 case CPP_MINUS: code = MINUS_EXPR; break;
31102 case CPP_AND: code = BIT_AND_EXPR; break;
31103 case CPP_XOR: code = BIT_XOR_EXPR; break;
31104 case CPP_OR: code = BIT_IOR_EXPR; break;
31105 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31106 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31107 default: break;
31108 }
31109
31110 if (code != ERROR_MARK)
31111 cp_lexer_consume_token (parser->lexer);
31112 else
31113 {
31114 bool saved_colon_corrects_to_scope_p;
31115 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31116 parser->colon_corrects_to_scope_p = false;
31117 id = cp_parser_id_expression (parser, /*template_p=*/false,
31118 /*check_dependency_p=*/true,
31119 /*template_p=*/NULL,
31120 /*declarator_p=*/false,
31121 /*optional_p=*/false);
31122 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31123 if (identifier_p (id))
31124 {
31125 const char *p = IDENTIFIER_POINTER (id);
31126
31127 if (strcmp (p, "min") == 0)
31128 code = MIN_EXPR;
31129 else if (strcmp (p, "max") == 0)
31130 code = MAX_EXPR;
31131 else if (id == ansi_opname (PLUS_EXPR))
31132 code = PLUS_EXPR;
31133 else if (id == ansi_opname (MULT_EXPR))
31134 code = MULT_EXPR;
31135 else if (id == ansi_opname (MINUS_EXPR))
31136 code = MINUS_EXPR;
31137 else if (id == ansi_opname (BIT_AND_EXPR))
31138 code = BIT_AND_EXPR;
31139 else if (id == ansi_opname (BIT_IOR_EXPR))
31140 code = BIT_IOR_EXPR;
31141 else if (id == ansi_opname (BIT_XOR_EXPR))
31142 code = BIT_XOR_EXPR;
31143 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31144 code = TRUTH_ANDIF_EXPR;
31145 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31146 code = TRUTH_ORIF_EXPR;
31147 id = omp_reduction_id (code, id, NULL_TREE);
31148 tree scope = parser->scope;
31149 if (scope)
31150 id = build_qualified_name (NULL_TREE, scope, id, false);
31151 parser->scope = NULL_TREE;
31152 parser->qualifying_scope = NULL_TREE;
31153 parser->object_scope = NULL_TREE;
31154 }
31155 else
31156 {
31157 error ("invalid reduction-identifier");
31158 resync_fail:
31159 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31160 /*or_comma=*/false,
31161 /*consume_paren=*/true);
31162 return list;
31163 }
31164 }
31165
31166 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31167 goto resync_fail;
31168
31169 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31170 NULL);
31171 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31172 {
31173 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31174 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31175 }
31176
31177 return nlist;
31178 }
31179
31180 /* OpenMP 2.5:
31181 schedule ( schedule-kind )
31182 schedule ( schedule-kind , expression )
31183
31184 schedule-kind:
31185 static | dynamic | guided | runtime | auto
31186
31187 OpenMP 4.5:
31188 schedule ( schedule-modifier : schedule-kind )
31189 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31190
31191 schedule-modifier:
31192 simd
31193 monotonic
31194 nonmonotonic */
31195
31196 static tree
31197 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31198 {
31199 tree c, t;
31200 int modifiers = 0, nmodifiers = 0;
31201
31202 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31203 return list;
31204
31205 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31206
31207 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31208 {
31209 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31210 const char *p = IDENTIFIER_POINTER (id);
31211 if (strcmp ("simd", p) == 0)
31212 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31213 else if (strcmp ("monotonic", p) == 0)
31214 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31215 else if (strcmp ("nonmonotonic", p) == 0)
31216 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31217 else
31218 break;
31219 cp_lexer_consume_token (parser->lexer);
31220 if (nmodifiers++ == 0
31221 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31222 cp_lexer_consume_token (parser->lexer);
31223 else
31224 {
31225 cp_parser_require (parser, CPP_COLON, RT_COLON);
31226 break;
31227 }
31228 }
31229
31230 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31231 {
31232 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31233 const char *p = IDENTIFIER_POINTER (id);
31234
31235 switch (p[0])
31236 {
31237 case 'd':
31238 if (strcmp ("dynamic", p) != 0)
31239 goto invalid_kind;
31240 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31241 break;
31242
31243 case 'g':
31244 if (strcmp ("guided", p) != 0)
31245 goto invalid_kind;
31246 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31247 break;
31248
31249 case 'r':
31250 if (strcmp ("runtime", p) != 0)
31251 goto invalid_kind;
31252 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31253 break;
31254
31255 default:
31256 goto invalid_kind;
31257 }
31258 }
31259 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31260 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31261 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31262 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31263 else
31264 goto invalid_kind;
31265 cp_lexer_consume_token (parser->lexer);
31266
31267 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31268 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31269 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31270 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31271 {
31272 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31273 "specified");
31274 modifiers = 0;
31275 }
31276
31277 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31278 {
31279 cp_token *token;
31280 cp_lexer_consume_token (parser->lexer);
31281
31282 token = cp_lexer_peek_token (parser->lexer);
31283 t = cp_parser_assignment_expression (parser);
31284
31285 if (t == error_mark_node)
31286 goto resync_fail;
31287 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31288 error_at (token->location, "schedule %<runtime%> does not take "
31289 "a %<chunk_size%> parameter");
31290 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31291 error_at (token->location, "schedule %<auto%> does not take "
31292 "a %<chunk_size%> parameter");
31293 else
31294 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31295
31296 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31297 goto resync_fail;
31298 }
31299 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31300 goto resync_fail;
31301
31302 OMP_CLAUSE_SCHEDULE_KIND (c)
31303 = (enum omp_clause_schedule_kind)
31304 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31305
31306 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31307 OMP_CLAUSE_CHAIN (c) = list;
31308 return c;
31309
31310 invalid_kind:
31311 cp_parser_error (parser, "invalid schedule kind");
31312 resync_fail:
31313 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31314 /*or_comma=*/false,
31315 /*consume_paren=*/true);
31316 return list;
31317 }
31318
31319 /* OpenMP 3.0:
31320 untied */
31321
31322 static tree
31323 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31324 tree list, location_t location)
31325 {
31326 tree c;
31327
31328 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31329
31330 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31331 OMP_CLAUSE_CHAIN (c) = list;
31332 return c;
31333 }
31334
31335 /* OpenMP 4.0:
31336 inbranch
31337 notinbranch */
31338
31339 static tree
31340 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31341 tree list, location_t location)
31342 {
31343 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31344 tree c = build_omp_clause (location, code);
31345 OMP_CLAUSE_CHAIN (c) = list;
31346 return c;
31347 }
31348
31349 /* OpenMP 4.0:
31350 parallel
31351 for
31352 sections
31353 taskgroup */
31354
31355 static tree
31356 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31357 enum omp_clause_code code,
31358 tree list, location_t location)
31359 {
31360 tree c = build_omp_clause (location, code);
31361 OMP_CLAUSE_CHAIN (c) = list;
31362 return c;
31363 }
31364
31365 /* OpenMP 4.5:
31366 nogroup */
31367
31368 static tree
31369 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31370 tree list, location_t location)
31371 {
31372 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31373 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31374 OMP_CLAUSE_CHAIN (c) = list;
31375 return c;
31376 }
31377
31378 /* OpenMP 4.5:
31379 simd
31380 threads */
31381
31382 static tree
31383 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31384 enum omp_clause_code code,
31385 tree list, location_t location)
31386 {
31387 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31388 tree c = build_omp_clause (location, code);
31389 OMP_CLAUSE_CHAIN (c) = list;
31390 return c;
31391 }
31392
31393 /* OpenMP 4.0:
31394 num_teams ( expression ) */
31395
31396 static tree
31397 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31398 location_t location)
31399 {
31400 tree t, c;
31401
31402 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31403 return list;
31404
31405 t = cp_parser_expression (parser);
31406
31407 if (t == error_mark_node
31408 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31409 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31410 /*or_comma=*/false,
31411 /*consume_paren=*/true);
31412
31413 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31414 "num_teams", location);
31415
31416 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31417 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31418 OMP_CLAUSE_CHAIN (c) = list;
31419
31420 return c;
31421 }
31422
31423 /* OpenMP 4.0:
31424 thread_limit ( expression ) */
31425
31426 static tree
31427 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31428 location_t location)
31429 {
31430 tree t, c;
31431
31432 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31433 return list;
31434
31435 t = cp_parser_expression (parser);
31436
31437 if (t == error_mark_node
31438 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31439 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31440 /*or_comma=*/false,
31441 /*consume_paren=*/true);
31442
31443 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31444 "thread_limit", location);
31445
31446 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31447 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31448 OMP_CLAUSE_CHAIN (c) = list;
31449
31450 return c;
31451 }
31452
31453 /* OpenMP 4.0:
31454 aligned ( variable-list )
31455 aligned ( variable-list : constant-expression ) */
31456
31457 static tree
31458 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31459 {
31460 tree nlist, c, alignment = NULL_TREE;
31461 bool colon;
31462
31463 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31464 return list;
31465
31466 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31467 &colon);
31468
31469 if (colon)
31470 {
31471 alignment = cp_parser_constant_expression (parser);
31472
31473 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31474 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31475 /*or_comma=*/false,
31476 /*consume_paren=*/true);
31477
31478 if (alignment == error_mark_node)
31479 alignment = NULL_TREE;
31480 }
31481
31482 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31483 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31484
31485 return nlist;
31486 }
31487
31488 /* OpenMP 4.0:
31489 linear ( variable-list )
31490 linear ( variable-list : expression )
31491
31492 OpenMP 4.5:
31493 linear ( modifier ( variable-list ) )
31494 linear ( modifier ( variable-list ) : expression ) */
31495
31496 static tree
31497 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31498 bool is_cilk_simd_fn, bool declare_simd)
31499 {
31500 tree nlist, c, step = integer_one_node;
31501 bool colon;
31502 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31503
31504 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31505 return list;
31506
31507 if (!is_cilk_simd_fn
31508 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31509 {
31510 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31511 const char *p = IDENTIFIER_POINTER (id);
31512
31513 if (strcmp ("ref", p) == 0)
31514 kind = OMP_CLAUSE_LINEAR_REF;
31515 else if (strcmp ("val", p) == 0)
31516 kind = OMP_CLAUSE_LINEAR_VAL;
31517 else if (strcmp ("uval", p) == 0)
31518 kind = OMP_CLAUSE_LINEAR_UVAL;
31519 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31520 cp_lexer_consume_token (parser->lexer);
31521 else
31522 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31523 }
31524
31525 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31526 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31527 &colon);
31528 else
31529 {
31530 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31531 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31532 if (colon)
31533 cp_parser_require (parser, CPP_COLON, RT_COLON);
31534 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31535 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31536 /*or_comma=*/false,
31537 /*consume_paren=*/true);
31538 }
31539
31540 if (colon)
31541 {
31542 step = NULL_TREE;
31543 if (declare_simd
31544 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31545 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31546 {
31547 cp_token *token = cp_lexer_peek_token (parser->lexer);
31548 cp_parser_parse_tentatively (parser);
31549 step = cp_parser_id_expression (parser, /*template_p=*/false,
31550 /*check_dependency_p=*/true,
31551 /*template_p=*/NULL,
31552 /*declarator_p=*/false,
31553 /*optional_p=*/false);
31554 if (step != error_mark_node)
31555 step = cp_parser_lookup_name_simple (parser, step, token->location);
31556 if (step == error_mark_node)
31557 {
31558 step = NULL_TREE;
31559 cp_parser_abort_tentative_parse (parser);
31560 }
31561 else if (!cp_parser_parse_definitely (parser))
31562 step = NULL_TREE;
31563 }
31564 if (!step)
31565 step = cp_parser_expression (parser);
31566
31567 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31568 {
31569 sorry ("using parameters for %<linear%> step is not supported yet");
31570 step = integer_one_node;
31571 }
31572 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31573 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31574 /*or_comma=*/false,
31575 /*consume_paren=*/true);
31576
31577 if (step == error_mark_node)
31578 return list;
31579 }
31580
31581 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31582 {
31583 OMP_CLAUSE_LINEAR_STEP (c) = step;
31584 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31585 }
31586
31587 return nlist;
31588 }
31589
31590 /* OpenMP 4.0:
31591 safelen ( constant-expression ) */
31592
31593 static tree
31594 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31595 location_t location)
31596 {
31597 tree t, c;
31598
31599 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31600 return list;
31601
31602 t = cp_parser_constant_expression (parser);
31603
31604 if (t == error_mark_node
31605 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31606 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31607 /*or_comma=*/false,
31608 /*consume_paren=*/true);
31609
31610 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31611
31612 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31613 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31614 OMP_CLAUSE_CHAIN (c) = list;
31615
31616 return c;
31617 }
31618
31619 /* OpenMP 4.0:
31620 simdlen ( constant-expression ) */
31621
31622 static tree
31623 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31624 location_t location)
31625 {
31626 tree t, c;
31627
31628 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31629 return list;
31630
31631 t = cp_parser_constant_expression (parser);
31632
31633 if (t == error_mark_node
31634 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31635 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31636 /*or_comma=*/false,
31637 /*consume_paren=*/true);
31638
31639 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31640
31641 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31642 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31643 OMP_CLAUSE_CHAIN (c) = list;
31644
31645 return c;
31646 }
31647
31648 /* OpenMP 4.5:
31649 vec:
31650 identifier [+/- integer]
31651 vec , identifier [+/- integer]
31652 */
31653
31654 static tree
31655 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31656 tree list)
31657 {
31658 tree vec = NULL;
31659
31660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31661 {
31662 cp_parser_error (parser, "expected identifier");
31663 return list;
31664 }
31665
31666 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31667 {
31668 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31669 tree t, identifier = cp_parser_identifier (parser);
31670 tree addend = NULL;
31671
31672 if (identifier == error_mark_node)
31673 t = error_mark_node;
31674 else
31675 {
31676 t = cp_parser_lookup_name_simple
31677 (parser, identifier,
31678 cp_lexer_peek_token (parser->lexer)->location);
31679 if (t == error_mark_node)
31680 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31681 id_loc);
31682 }
31683
31684 bool neg = false;
31685 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31686 neg = true;
31687 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31688 {
31689 addend = integer_zero_node;
31690 goto add_to_vector;
31691 }
31692 cp_lexer_consume_token (parser->lexer);
31693
31694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31695 {
31696 cp_parser_error (parser, "expected integer");
31697 return list;
31698 }
31699
31700 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31701 if (TREE_CODE (addend) != INTEGER_CST)
31702 {
31703 cp_parser_error (parser, "expected integer");
31704 return list;
31705 }
31706 cp_lexer_consume_token (parser->lexer);
31707
31708 add_to_vector:
31709 if (t != error_mark_node)
31710 {
31711 vec = tree_cons (addend, t, vec);
31712 if (neg)
31713 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31714 }
31715
31716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31717 break;
31718
31719 cp_lexer_consume_token (parser->lexer);
31720 }
31721
31722 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31723 {
31724 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31725 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31726 OMP_CLAUSE_DECL (u) = nreverse (vec);
31727 OMP_CLAUSE_CHAIN (u) = list;
31728 return u;
31729 }
31730 return list;
31731 }
31732
31733 /* OpenMP 4.0:
31734 depend ( depend-kind : variable-list )
31735
31736 depend-kind:
31737 in | out | inout
31738
31739 OpenMP 4.5:
31740 depend ( source )
31741
31742 depend ( sink : vec ) */
31743
31744 static tree
31745 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31746 {
31747 tree nlist, c;
31748 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31749
31750 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31751 return list;
31752
31753 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31754 {
31755 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31756 const char *p = IDENTIFIER_POINTER (id);
31757
31758 if (strcmp ("in", p) == 0)
31759 kind = OMP_CLAUSE_DEPEND_IN;
31760 else if (strcmp ("inout", p) == 0)
31761 kind = OMP_CLAUSE_DEPEND_INOUT;
31762 else if (strcmp ("out", p) == 0)
31763 kind = OMP_CLAUSE_DEPEND_OUT;
31764 else if (strcmp ("source", p) == 0)
31765 kind = OMP_CLAUSE_DEPEND_SOURCE;
31766 else if (strcmp ("sink", p) == 0)
31767 kind = OMP_CLAUSE_DEPEND_SINK;
31768 else
31769 goto invalid_kind;
31770 }
31771 else
31772 goto invalid_kind;
31773
31774 cp_lexer_consume_token (parser->lexer);
31775
31776 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31777 {
31778 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31779 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31780 OMP_CLAUSE_DECL (c) = NULL_TREE;
31781 OMP_CLAUSE_CHAIN (c) = list;
31782 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31783 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31784 /*or_comma=*/false,
31785 /*consume_paren=*/true);
31786 return c;
31787 }
31788
31789 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31790 goto resync_fail;
31791
31792 if (kind == OMP_CLAUSE_DEPEND_SINK)
31793 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31794 else
31795 {
31796 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31797 list, NULL);
31798
31799 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31800 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31801 }
31802 return nlist;
31803
31804 invalid_kind:
31805 cp_parser_error (parser, "invalid depend kind");
31806 resync_fail:
31807 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31808 /*or_comma=*/false,
31809 /*consume_paren=*/true);
31810 return list;
31811 }
31812
31813 /* OpenMP 4.0:
31814 map ( map-kind : variable-list )
31815 map ( variable-list )
31816
31817 map-kind:
31818 alloc | to | from | tofrom
31819
31820 OpenMP 4.5:
31821 map-kind:
31822 alloc | to | from | tofrom | release | delete
31823
31824 map ( always [,] map-kind: variable-list ) */
31825
31826 static tree
31827 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31828 {
31829 tree nlist, c;
31830 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31831 bool always = false;
31832
31833 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31834 return list;
31835
31836 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31837 {
31838 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31839 const char *p = IDENTIFIER_POINTER (id);
31840
31841 if (strcmp ("always", p) == 0)
31842 {
31843 int nth = 2;
31844 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31845 nth++;
31846 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31847 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31848 == RID_DELETE))
31849 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31850 == CPP_COLON))
31851 {
31852 always = true;
31853 cp_lexer_consume_token (parser->lexer);
31854 if (nth == 3)
31855 cp_lexer_consume_token (parser->lexer);
31856 }
31857 }
31858 }
31859
31860 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31861 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31862 {
31863 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31864 const char *p = IDENTIFIER_POINTER (id);
31865
31866 if (strcmp ("alloc", p) == 0)
31867 kind = GOMP_MAP_ALLOC;
31868 else if (strcmp ("to", p) == 0)
31869 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31870 else if (strcmp ("from", p) == 0)
31871 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31872 else if (strcmp ("tofrom", p) == 0)
31873 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31874 else if (strcmp ("release", p) == 0)
31875 kind = GOMP_MAP_RELEASE;
31876 else
31877 {
31878 cp_parser_error (parser, "invalid map kind");
31879 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31880 /*or_comma=*/false,
31881 /*consume_paren=*/true);
31882 return list;
31883 }
31884 cp_lexer_consume_token (parser->lexer);
31885 cp_lexer_consume_token (parser->lexer);
31886 }
31887 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31888 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31889 {
31890 kind = GOMP_MAP_DELETE;
31891 cp_lexer_consume_token (parser->lexer);
31892 cp_lexer_consume_token (parser->lexer);
31893 }
31894
31895 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31896 NULL);
31897
31898 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31899 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31900
31901 return nlist;
31902 }
31903
31904 /* OpenMP 4.0:
31905 device ( expression ) */
31906
31907 static tree
31908 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31909 location_t location)
31910 {
31911 tree t, c;
31912
31913 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31914 return list;
31915
31916 t = cp_parser_expression (parser);
31917
31918 if (t == error_mark_node
31919 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31920 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31921 /*or_comma=*/false,
31922 /*consume_paren=*/true);
31923
31924 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31925 "device", location);
31926
31927 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31928 OMP_CLAUSE_DEVICE_ID (c) = t;
31929 OMP_CLAUSE_CHAIN (c) = list;
31930
31931 return c;
31932 }
31933
31934 /* OpenMP 4.0:
31935 dist_schedule ( static )
31936 dist_schedule ( static , expression ) */
31937
31938 static tree
31939 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31940 location_t location)
31941 {
31942 tree c, t;
31943
31944 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31945 return list;
31946
31947 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31948
31949 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31950 goto invalid_kind;
31951 cp_lexer_consume_token (parser->lexer);
31952
31953 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31954 {
31955 cp_lexer_consume_token (parser->lexer);
31956
31957 t = cp_parser_assignment_expression (parser);
31958
31959 if (t == error_mark_node)
31960 goto resync_fail;
31961 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31962
31963 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31964 goto resync_fail;
31965 }
31966 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31967 goto resync_fail;
31968
31969 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31970 location);
31971 OMP_CLAUSE_CHAIN (c) = list;
31972 return c;
31973
31974 invalid_kind:
31975 cp_parser_error (parser, "invalid dist_schedule kind");
31976 resync_fail:
31977 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31978 /*or_comma=*/false,
31979 /*consume_paren=*/true);
31980 return list;
31981 }
31982
31983 /* OpenMP 4.0:
31984 proc_bind ( proc-bind-kind )
31985
31986 proc-bind-kind:
31987 master | close | spread */
31988
31989 static tree
31990 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
31991 location_t location)
31992 {
31993 tree c;
31994 enum omp_clause_proc_bind_kind kind;
31995
31996 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31997 return list;
31998
31999 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32000 {
32001 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32002 const char *p = IDENTIFIER_POINTER (id);
32003
32004 if (strcmp ("master", p) == 0)
32005 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32006 else if (strcmp ("close", p) == 0)
32007 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32008 else if (strcmp ("spread", p) == 0)
32009 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32010 else
32011 goto invalid_kind;
32012 }
32013 else
32014 goto invalid_kind;
32015
32016 cp_lexer_consume_token (parser->lexer);
32017 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32018 goto resync_fail;
32019
32020 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32021 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32022 location);
32023 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32024 OMP_CLAUSE_CHAIN (c) = list;
32025 return c;
32026
32027 invalid_kind:
32028 cp_parser_error (parser, "invalid depend kind");
32029 resync_fail:
32030 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32031 /*or_comma=*/false,
32032 /*consume_paren=*/true);
32033 return list;
32034 }
32035
32036 /* OpenACC:
32037 async [( int-expr )] */
32038
32039 static tree
32040 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32041 {
32042 tree c, t;
32043 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32044
32045 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32046
32047 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32048 {
32049 cp_lexer_consume_token (parser->lexer);
32050
32051 t = cp_parser_expression (parser);
32052 if (t == error_mark_node
32053 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32054 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32055 /*or_comma=*/false,
32056 /*consume_paren=*/true);
32057 }
32058
32059 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32060
32061 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32062 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32063 OMP_CLAUSE_CHAIN (c) = list;
32064 list = c;
32065
32066 return list;
32067 }
32068
32069 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32070 is a bitmask in MASK. Return the list of clauses found. */
32071
32072 static tree
32073 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32074 const char *where, cp_token *pragma_tok,
32075 bool finish_p = true)
32076 {
32077 tree clauses = NULL;
32078 bool first = true;
32079
32080 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32081 {
32082 location_t here;
32083 pragma_omp_clause c_kind;
32084 omp_clause_code code;
32085 const char *c_name;
32086 tree prev = clauses;
32087
32088 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32089 cp_lexer_consume_token (parser->lexer);
32090
32091 here = cp_lexer_peek_token (parser->lexer)->location;
32092 c_kind = cp_parser_omp_clause_name (parser);
32093
32094 switch (c_kind)
32095 {
32096 case PRAGMA_OACC_CLAUSE_ASYNC:
32097 clauses = cp_parser_oacc_clause_async (parser, clauses);
32098 c_name = "async";
32099 break;
32100 case PRAGMA_OACC_CLAUSE_AUTO:
32101 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32102 clauses, here);
32103 c_name = "auto";
32104 break;
32105 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32106 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32107 c_name = "collapse";
32108 break;
32109 case PRAGMA_OACC_CLAUSE_COPY:
32110 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32111 c_name = "copy";
32112 break;
32113 case PRAGMA_OACC_CLAUSE_COPYIN:
32114 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32115 c_name = "copyin";
32116 break;
32117 case PRAGMA_OACC_CLAUSE_COPYOUT:
32118 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32119 c_name = "copyout";
32120 break;
32121 case PRAGMA_OACC_CLAUSE_CREATE:
32122 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32123 c_name = "create";
32124 break;
32125 case PRAGMA_OACC_CLAUSE_DELETE:
32126 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32127 c_name = "delete";
32128 break;
32129 case PRAGMA_OMP_CLAUSE_DEFAULT:
32130 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32131 c_name = "default";
32132 break;
32133 case PRAGMA_OACC_CLAUSE_DEVICE:
32134 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32135 c_name = "device";
32136 break;
32137 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32138 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32139 c_name = "deviceptr";
32140 break;
32141 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32142 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32143 c_name = "device_resident";
32144 break;
32145 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32146 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32147 clauses);
32148 c_name = "firstprivate";
32149 break;
32150 case PRAGMA_OACC_CLAUSE_GANG:
32151 c_name = "gang";
32152 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32153 c_name, clauses);
32154 break;
32155 case PRAGMA_OACC_CLAUSE_HOST:
32156 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32157 c_name = "host";
32158 break;
32159 case PRAGMA_OACC_CLAUSE_IF:
32160 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32161 c_name = "if";
32162 break;
32163 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32164 clauses = cp_parser_oacc_simple_clause (parser,
32165 OMP_CLAUSE_INDEPENDENT,
32166 clauses, here);
32167 c_name = "independent";
32168 break;
32169 case PRAGMA_OACC_CLAUSE_LINK:
32170 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32171 c_name = "link";
32172 break;
32173 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32174 code = OMP_CLAUSE_NUM_GANGS;
32175 c_name = "num_gangs";
32176 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32177 clauses);
32178 break;
32179 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32180 c_name = "num_workers";
32181 code = OMP_CLAUSE_NUM_WORKERS;
32182 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32183 clauses);
32184 break;
32185 case PRAGMA_OACC_CLAUSE_PRESENT:
32186 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32187 c_name = "present";
32188 break;
32189 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32190 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32191 c_name = "present_or_copy";
32192 break;
32193 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32194 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32195 c_name = "present_or_copyin";
32196 break;
32197 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32198 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32199 c_name = "present_or_copyout";
32200 break;
32201 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32202 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32203 c_name = "present_or_create";
32204 break;
32205 case PRAGMA_OACC_CLAUSE_PRIVATE:
32206 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32207 clauses);
32208 c_name = "private";
32209 break;
32210 case PRAGMA_OACC_CLAUSE_REDUCTION:
32211 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32212 c_name = "reduction";
32213 break;
32214 case PRAGMA_OACC_CLAUSE_SELF:
32215 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32216 c_name = "self";
32217 break;
32218 case PRAGMA_OACC_CLAUSE_SEQ:
32219 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32220 clauses, here);
32221 c_name = "seq";
32222 break;
32223 case PRAGMA_OACC_CLAUSE_TILE:
32224 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32225 c_name = "tile";
32226 break;
32227 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32228 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32229 clauses);
32230 c_name = "use_device";
32231 break;
32232 case PRAGMA_OACC_CLAUSE_VECTOR:
32233 c_name = "vector";
32234 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32235 c_name, clauses);
32236 break;
32237 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32238 c_name = "vector_length";
32239 code = OMP_CLAUSE_VECTOR_LENGTH;
32240 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32241 clauses);
32242 break;
32243 case PRAGMA_OACC_CLAUSE_WAIT:
32244 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32245 c_name = "wait";
32246 break;
32247 case PRAGMA_OACC_CLAUSE_WORKER:
32248 c_name = "worker";
32249 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32250 c_name, clauses);
32251 break;
32252 default:
32253 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32254 goto saw_error;
32255 }
32256
32257 first = false;
32258
32259 if (((mask >> c_kind) & 1) == 0)
32260 {
32261 /* Remove the invalid clause(s) from the list to avoid
32262 confusing the rest of the compiler. */
32263 clauses = prev;
32264 error_at (here, "%qs is not valid for %qs", c_name, where);
32265 }
32266 }
32267
32268 saw_error:
32269 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32270
32271 if (finish_p)
32272 return finish_omp_clauses (clauses, C_ORT_ACC);
32273
32274 return clauses;
32275 }
32276
32277 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32278 is a bitmask in MASK. Return the list of clauses found; the result
32279 of clause default goes in *pdefault. */
32280
32281 static tree
32282 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32283 const char *where, cp_token *pragma_tok,
32284 bool finish_p = true)
32285 {
32286 tree clauses = NULL;
32287 bool first = true;
32288 cp_token *token = NULL;
32289
32290 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32291 {
32292 pragma_omp_clause c_kind;
32293 const char *c_name;
32294 tree prev = clauses;
32295
32296 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32297 cp_lexer_consume_token (parser->lexer);
32298
32299 token = cp_lexer_peek_token (parser->lexer);
32300 c_kind = cp_parser_omp_clause_name (parser);
32301
32302 switch (c_kind)
32303 {
32304 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32305 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32306 token->location);
32307 c_name = "collapse";
32308 break;
32309 case PRAGMA_OMP_CLAUSE_COPYIN:
32310 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32311 c_name = "copyin";
32312 break;
32313 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32314 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32315 clauses);
32316 c_name = "copyprivate";
32317 break;
32318 case PRAGMA_OMP_CLAUSE_DEFAULT:
32319 clauses = cp_parser_omp_clause_default (parser, clauses,
32320 token->location, false);
32321 c_name = "default";
32322 break;
32323 case PRAGMA_OMP_CLAUSE_FINAL:
32324 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32325 c_name = "final";
32326 break;
32327 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32328 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32329 clauses);
32330 c_name = "firstprivate";
32331 break;
32332 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32333 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32334 token->location);
32335 c_name = "grainsize";
32336 break;
32337 case PRAGMA_OMP_CLAUSE_HINT:
32338 clauses = cp_parser_omp_clause_hint (parser, clauses,
32339 token->location);
32340 c_name = "hint";
32341 break;
32342 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32343 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32344 token->location);
32345 c_name = "defaultmap";
32346 break;
32347 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32348 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32349 clauses);
32350 c_name = "use_device_ptr";
32351 break;
32352 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32353 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32354 clauses);
32355 c_name = "is_device_ptr";
32356 break;
32357 case PRAGMA_OMP_CLAUSE_IF:
32358 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32359 true);
32360 c_name = "if";
32361 break;
32362 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32363 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32364 clauses);
32365 c_name = "lastprivate";
32366 break;
32367 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32368 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32369 token->location);
32370 c_name = "mergeable";
32371 break;
32372 case PRAGMA_OMP_CLAUSE_NOWAIT:
32373 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32374 c_name = "nowait";
32375 break;
32376 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32377 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32378 token->location);
32379 c_name = "num_tasks";
32380 break;
32381 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32382 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32383 token->location);
32384 c_name = "num_threads";
32385 break;
32386 case PRAGMA_OMP_CLAUSE_ORDERED:
32387 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32388 token->location);
32389 c_name = "ordered";
32390 break;
32391 case PRAGMA_OMP_CLAUSE_PRIORITY:
32392 clauses = cp_parser_omp_clause_priority (parser, clauses,
32393 token->location);
32394 c_name = "priority";
32395 break;
32396 case PRAGMA_OMP_CLAUSE_PRIVATE:
32397 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32398 clauses);
32399 c_name = "private";
32400 break;
32401 case PRAGMA_OMP_CLAUSE_REDUCTION:
32402 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32403 c_name = "reduction";
32404 break;
32405 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32406 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32407 token->location);
32408 c_name = "schedule";
32409 break;
32410 case PRAGMA_OMP_CLAUSE_SHARED:
32411 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32412 clauses);
32413 c_name = "shared";
32414 break;
32415 case PRAGMA_OMP_CLAUSE_UNTIED:
32416 clauses = cp_parser_omp_clause_untied (parser, clauses,
32417 token->location);
32418 c_name = "untied";
32419 break;
32420 case PRAGMA_OMP_CLAUSE_INBRANCH:
32421 case PRAGMA_CILK_CLAUSE_MASK:
32422 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32423 clauses, token->location);
32424 c_name = "inbranch";
32425 break;
32426 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32427 case PRAGMA_CILK_CLAUSE_NOMASK:
32428 clauses = cp_parser_omp_clause_branch (parser,
32429 OMP_CLAUSE_NOTINBRANCH,
32430 clauses, token->location);
32431 c_name = "notinbranch";
32432 break;
32433 case PRAGMA_OMP_CLAUSE_PARALLEL:
32434 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32435 clauses, token->location);
32436 c_name = "parallel";
32437 if (!first)
32438 {
32439 clause_not_first:
32440 error_at (token->location, "%qs must be the first clause of %qs",
32441 c_name, where);
32442 clauses = prev;
32443 }
32444 break;
32445 case PRAGMA_OMP_CLAUSE_FOR:
32446 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32447 clauses, token->location);
32448 c_name = "for";
32449 if (!first)
32450 goto clause_not_first;
32451 break;
32452 case PRAGMA_OMP_CLAUSE_SECTIONS:
32453 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32454 clauses, token->location);
32455 c_name = "sections";
32456 if (!first)
32457 goto clause_not_first;
32458 break;
32459 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32460 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32461 clauses, token->location);
32462 c_name = "taskgroup";
32463 if (!first)
32464 goto clause_not_first;
32465 break;
32466 case PRAGMA_OMP_CLAUSE_LINK:
32467 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32468 c_name = "to";
32469 break;
32470 case PRAGMA_OMP_CLAUSE_TO:
32471 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32472 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32473 clauses);
32474 else
32475 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32476 c_name = "to";
32477 break;
32478 case PRAGMA_OMP_CLAUSE_FROM:
32479 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32480 c_name = "from";
32481 break;
32482 case PRAGMA_OMP_CLAUSE_UNIFORM:
32483 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32484 clauses);
32485 c_name = "uniform";
32486 break;
32487 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32488 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32489 token->location);
32490 c_name = "num_teams";
32491 break;
32492 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32493 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32494 token->location);
32495 c_name = "thread_limit";
32496 break;
32497 case PRAGMA_OMP_CLAUSE_ALIGNED:
32498 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32499 c_name = "aligned";
32500 break;
32501 case PRAGMA_OMP_CLAUSE_LINEAR:
32502 {
32503 bool cilk_simd_fn = false, declare_simd = false;
32504 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32505 cilk_simd_fn = true;
32506 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32507 declare_simd = true;
32508 clauses = cp_parser_omp_clause_linear (parser, clauses,
32509 cilk_simd_fn, declare_simd);
32510 }
32511 c_name = "linear";
32512 break;
32513 case PRAGMA_OMP_CLAUSE_DEPEND:
32514 clauses = cp_parser_omp_clause_depend (parser, clauses,
32515 token->location);
32516 c_name = "depend";
32517 break;
32518 case PRAGMA_OMP_CLAUSE_MAP:
32519 clauses = cp_parser_omp_clause_map (parser, clauses);
32520 c_name = "map";
32521 break;
32522 case PRAGMA_OMP_CLAUSE_DEVICE:
32523 clauses = cp_parser_omp_clause_device (parser, clauses,
32524 token->location);
32525 c_name = "device";
32526 break;
32527 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32528 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32529 token->location);
32530 c_name = "dist_schedule";
32531 break;
32532 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32533 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32534 token->location);
32535 c_name = "proc_bind";
32536 break;
32537 case PRAGMA_OMP_CLAUSE_SAFELEN:
32538 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32539 token->location);
32540 c_name = "safelen";
32541 break;
32542 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32543 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32544 token->location);
32545 c_name = "simdlen";
32546 break;
32547 case PRAGMA_OMP_CLAUSE_NOGROUP:
32548 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32549 token->location);
32550 c_name = "nogroup";
32551 break;
32552 case PRAGMA_OMP_CLAUSE_THREADS:
32553 clauses
32554 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32555 clauses, token->location);
32556 c_name = "threads";
32557 break;
32558 case PRAGMA_OMP_CLAUSE_SIMD:
32559 clauses
32560 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32561 clauses, token->location);
32562 c_name = "simd";
32563 break;
32564 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32565 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32566 c_name = "simdlen";
32567 break;
32568 default:
32569 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32570 goto saw_error;
32571 }
32572
32573 first = false;
32574
32575 if (((mask >> c_kind) & 1) == 0)
32576 {
32577 /* Remove the invalid clause(s) from the list to avoid
32578 confusing the rest of the compiler. */
32579 clauses = prev;
32580 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32581 }
32582 }
32583 saw_error:
32584 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32585 no reason to skip to the end. */
32586 if (!(flag_cilkplus && pragma_tok == NULL))
32587 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32588 if (finish_p)
32589 {
32590 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32591 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
32592 else
32593 return finish_omp_clauses (clauses, C_ORT_OMP);
32594 }
32595 return clauses;
32596 }
32597
32598 /* OpenMP 2.5:
32599 structured-block:
32600 statement
32601
32602 In practice, we're also interested in adding the statement to an
32603 outer node. So it is convenient if we work around the fact that
32604 cp_parser_statement calls add_stmt. */
32605
32606 static unsigned
32607 cp_parser_begin_omp_structured_block (cp_parser *parser)
32608 {
32609 unsigned save = parser->in_statement;
32610
32611 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32612 This preserves the "not within loop or switch" style error messages
32613 for nonsense cases like
32614 void foo() {
32615 #pragma omp single
32616 break;
32617 }
32618 */
32619 if (parser->in_statement)
32620 parser->in_statement = IN_OMP_BLOCK;
32621
32622 return save;
32623 }
32624
32625 static void
32626 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32627 {
32628 parser->in_statement = save;
32629 }
32630
32631 static tree
32632 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32633 {
32634 tree stmt = begin_omp_structured_block ();
32635 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32636
32637 cp_parser_statement (parser, NULL_TREE, false, if_p);
32638
32639 cp_parser_end_omp_structured_block (parser, save);
32640 return finish_omp_structured_block (stmt);
32641 }
32642
32643 /* OpenMP 2.5:
32644 # pragma omp atomic new-line
32645 expression-stmt
32646
32647 expression-stmt:
32648 x binop= expr | x++ | ++x | x-- | --x
32649 binop:
32650 +, *, -, /, &, ^, |, <<, >>
32651
32652 where x is an lvalue expression with scalar type.
32653
32654 OpenMP 3.1:
32655 # pragma omp atomic new-line
32656 update-stmt
32657
32658 # pragma omp atomic read new-line
32659 read-stmt
32660
32661 # pragma omp atomic write new-line
32662 write-stmt
32663
32664 # pragma omp atomic update new-line
32665 update-stmt
32666
32667 # pragma omp atomic capture new-line
32668 capture-stmt
32669
32670 # pragma omp atomic capture new-line
32671 capture-block
32672
32673 read-stmt:
32674 v = x
32675 write-stmt:
32676 x = expr
32677 update-stmt:
32678 expression-stmt | x = x binop expr
32679 capture-stmt:
32680 v = expression-stmt
32681 capture-block:
32682 { v = x; update-stmt; } | { update-stmt; v = x; }
32683
32684 OpenMP 4.0:
32685 update-stmt:
32686 expression-stmt | x = x binop expr | x = expr binop x
32687 capture-stmt:
32688 v = update-stmt
32689 capture-block:
32690 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32691
32692 where x and v are lvalue expressions with scalar type. */
32693
32694 static void
32695 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32696 {
32697 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32698 tree rhs1 = NULL_TREE, orig_lhs;
32699 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32700 bool structured_block = false;
32701 bool seq_cst = false;
32702
32703 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32704 {
32705 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32706 const char *p = IDENTIFIER_POINTER (id);
32707
32708 if (!strcmp (p, "seq_cst"))
32709 {
32710 seq_cst = true;
32711 cp_lexer_consume_token (parser->lexer);
32712 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32713 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32714 cp_lexer_consume_token (parser->lexer);
32715 }
32716 }
32717 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32718 {
32719 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32720 const char *p = IDENTIFIER_POINTER (id);
32721
32722 if (!strcmp (p, "read"))
32723 code = OMP_ATOMIC_READ;
32724 else if (!strcmp (p, "write"))
32725 code = NOP_EXPR;
32726 else if (!strcmp (p, "update"))
32727 code = OMP_ATOMIC;
32728 else if (!strcmp (p, "capture"))
32729 code = OMP_ATOMIC_CAPTURE_NEW;
32730 else
32731 p = NULL;
32732 if (p)
32733 cp_lexer_consume_token (parser->lexer);
32734 }
32735 if (!seq_cst)
32736 {
32737 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32738 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32739 cp_lexer_consume_token (parser->lexer);
32740
32741 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32742 {
32743 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32744 const char *p = IDENTIFIER_POINTER (id);
32745
32746 if (!strcmp (p, "seq_cst"))
32747 {
32748 seq_cst = true;
32749 cp_lexer_consume_token (parser->lexer);
32750 }
32751 }
32752 }
32753 cp_parser_require_pragma_eol (parser, pragma_tok);
32754
32755 switch (code)
32756 {
32757 case OMP_ATOMIC_READ:
32758 case NOP_EXPR: /* atomic write */
32759 v = cp_parser_unary_expression (parser);
32760 if (v == error_mark_node)
32761 goto saw_error;
32762 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32763 goto saw_error;
32764 if (code == NOP_EXPR)
32765 lhs = cp_parser_expression (parser);
32766 else
32767 lhs = cp_parser_unary_expression (parser);
32768 if (lhs == error_mark_node)
32769 goto saw_error;
32770 if (code == NOP_EXPR)
32771 {
32772 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32773 opcode. */
32774 code = OMP_ATOMIC;
32775 rhs = lhs;
32776 lhs = v;
32777 v = NULL_TREE;
32778 }
32779 goto done;
32780 case OMP_ATOMIC_CAPTURE_NEW:
32781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32782 {
32783 cp_lexer_consume_token (parser->lexer);
32784 structured_block = true;
32785 }
32786 else
32787 {
32788 v = cp_parser_unary_expression (parser);
32789 if (v == error_mark_node)
32790 goto saw_error;
32791 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32792 goto saw_error;
32793 }
32794 default:
32795 break;
32796 }
32797
32798 restart:
32799 lhs = cp_parser_unary_expression (parser);
32800 orig_lhs = lhs;
32801 switch (TREE_CODE (lhs))
32802 {
32803 case ERROR_MARK:
32804 goto saw_error;
32805
32806 case POSTINCREMENT_EXPR:
32807 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32808 code = OMP_ATOMIC_CAPTURE_OLD;
32809 /* FALLTHROUGH */
32810 case PREINCREMENT_EXPR:
32811 lhs = TREE_OPERAND (lhs, 0);
32812 opcode = PLUS_EXPR;
32813 rhs = integer_one_node;
32814 break;
32815
32816 case POSTDECREMENT_EXPR:
32817 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32818 code = OMP_ATOMIC_CAPTURE_OLD;
32819 /* FALLTHROUGH */
32820 case PREDECREMENT_EXPR:
32821 lhs = TREE_OPERAND (lhs, 0);
32822 opcode = MINUS_EXPR;
32823 rhs = integer_one_node;
32824 break;
32825
32826 case COMPOUND_EXPR:
32827 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32828 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32829 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32830 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32831 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32832 (TREE_OPERAND (lhs, 1), 0), 0)))
32833 == BOOLEAN_TYPE)
32834 /* Undo effects of boolean_increment for post {in,de}crement. */
32835 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32836 /* FALLTHRU */
32837 case MODIFY_EXPR:
32838 if (TREE_CODE (lhs) == MODIFY_EXPR
32839 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32840 {
32841 /* Undo effects of boolean_increment. */
32842 if (integer_onep (TREE_OPERAND (lhs, 1)))
32843 {
32844 /* This is pre or post increment. */
32845 rhs = TREE_OPERAND (lhs, 1);
32846 lhs = TREE_OPERAND (lhs, 0);
32847 opcode = NOP_EXPR;
32848 if (code == OMP_ATOMIC_CAPTURE_NEW
32849 && !structured_block
32850 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32851 code = OMP_ATOMIC_CAPTURE_OLD;
32852 break;
32853 }
32854 }
32855 /* FALLTHRU */
32856 default:
32857 switch (cp_lexer_peek_token (parser->lexer)->type)
32858 {
32859 case CPP_MULT_EQ:
32860 opcode = MULT_EXPR;
32861 break;
32862 case CPP_DIV_EQ:
32863 opcode = TRUNC_DIV_EXPR;
32864 break;
32865 case CPP_PLUS_EQ:
32866 opcode = PLUS_EXPR;
32867 break;
32868 case CPP_MINUS_EQ:
32869 opcode = MINUS_EXPR;
32870 break;
32871 case CPP_LSHIFT_EQ:
32872 opcode = LSHIFT_EXPR;
32873 break;
32874 case CPP_RSHIFT_EQ:
32875 opcode = RSHIFT_EXPR;
32876 break;
32877 case CPP_AND_EQ:
32878 opcode = BIT_AND_EXPR;
32879 break;
32880 case CPP_OR_EQ:
32881 opcode = BIT_IOR_EXPR;
32882 break;
32883 case CPP_XOR_EQ:
32884 opcode = BIT_XOR_EXPR;
32885 break;
32886 case CPP_EQ:
32887 enum cp_parser_prec oprec;
32888 cp_token *token;
32889 cp_lexer_consume_token (parser->lexer);
32890 cp_parser_parse_tentatively (parser);
32891 rhs1 = cp_parser_simple_cast_expression (parser);
32892 if (rhs1 == error_mark_node)
32893 {
32894 cp_parser_abort_tentative_parse (parser);
32895 cp_parser_simple_cast_expression (parser);
32896 goto saw_error;
32897 }
32898 token = cp_lexer_peek_token (parser->lexer);
32899 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32900 {
32901 cp_parser_abort_tentative_parse (parser);
32902 cp_parser_parse_tentatively (parser);
32903 rhs = cp_parser_binary_expression (parser, false, true,
32904 PREC_NOT_OPERATOR, NULL);
32905 if (rhs == error_mark_node)
32906 {
32907 cp_parser_abort_tentative_parse (parser);
32908 cp_parser_binary_expression (parser, false, true,
32909 PREC_NOT_OPERATOR, NULL);
32910 goto saw_error;
32911 }
32912 switch (TREE_CODE (rhs))
32913 {
32914 case MULT_EXPR:
32915 case TRUNC_DIV_EXPR:
32916 case RDIV_EXPR:
32917 case PLUS_EXPR:
32918 case MINUS_EXPR:
32919 case LSHIFT_EXPR:
32920 case RSHIFT_EXPR:
32921 case BIT_AND_EXPR:
32922 case BIT_IOR_EXPR:
32923 case BIT_XOR_EXPR:
32924 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32925 {
32926 if (cp_parser_parse_definitely (parser))
32927 {
32928 opcode = TREE_CODE (rhs);
32929 rhs1 = TREE_OPERAND (rhs, 0);
32930 rhs = TREE_OPERAND (rhs, 1);
32931 goto stmt_done;
32932 }
32933 else
32934 goto saw_error;
32935 }
32936 break;
32937 default:
32938 break;
32939 }
32940 cp_parser_abort_tentative_parse (parser);
32941 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32942 {
32943 rhs = cp_parser_expression (parser);
32944 if (rhs == error_mark_node)
32945 goto saw_error;
32946 opcode = NOP_EXPR;
32947 rhs1 = NULL_TREE;
32948 goto stmt_done;
32949 }
32950 cp_parser_error (parser,
32951 "invalid form of %<#pragma omp atomic%>");
32952 goto saw_error;
32953 }
32954 if (!cp_parser_parse_definitely (parser))
32955 goto saw_error;
32956 switch (token->type)
32957 {
32958 case CPP_SEMICOLON:
32959 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32960 {
32961 code = OMP_ATOMIC_CAPTURE_OLD;
32962 v = lhs;
32963 lhs = NULL_TREE;
32964 lhs1 = rhs1;
32965 rhs1 = NULL_TREE;
32966 cp_lexer_consume_token (parser->lexer);
32967 goto restart;
32968 }
32969 else if (structured_block)
32970 {
32971 opcode = NOP_EXPR;
32972 rhs = rhs1;
32973 rhs1 = NULL_TREE;
32974 goto stmt_done;
32975 }
32976 cp_parser_error (parser,
32977 "invalid form of %<#pragma omp atomic%>");
32978 goto saw_error;
32979 case CPP_MULT:
32980 opcode = MULT_EXPR;
32981 break;
32982 case CPP_DIV:
32983 opcode = TRUNC_DIV_EXPR;
32984 break;
32985 case CPP_PLUS:
32986 opcode = PLUS_EXPR;
32987 break;
32988 case CPP_MINUS:
32989 opcode = MINUS_EXPR;
32990 break;
32991 case CPP_LSHIFT:
32992 opcode = LSHIFT_EXPR;
32993 break;
32994 case CPP_RSHIFT:
32995 opcode = RSHIFT_EXPR;
32996 break;
32997 case CPP_AND:
32998 opcode = BIT_AND_EXPR;
32999 break;
33000 case CPP_OR:
33001 opcode = BIT_IOR_EXPR;
33002 break;
33003 case CPP_XOR:
33004 opcode = BIT_XOR_EXPR;
33005 break;
33006 default:
33007 cp_parser_error (parser,
33008 "invalid operator for %<#pragma omp atomic%>");
33009 goto saw_error;
33010 }
33011 oprec = TOKEN_PRECEDENCE (token);
33012 gcc_assert (oprec != PREC_NOT_OPERATOR);
33013 if (commutative_tree_code (opcode))
33014 oprec = (enum cp_parser_prec) (oprec - 1);
33015 cp_lexer_consume_token (parser->lexer);
33016 rhs = cp_parser_binary_expression (parser, false, false,
33017 oprec, NULL);
33018 if (rhs == error_mark_node)
33019 goto saw_error;
33020 goto stmt_done;
33021 /* FALLTHROUGH */
33022 default:
33023 cp_parser_error (parser,
33024 "invalid operator for %<#pragma omp atomic%>");
33025 goto saw_error;
33026 }
33027 cp_lexer_consume_token (parser->lexer);
33028
33029 rhs = cp_parser_expression (parser);
33030 if (rhs == error_mark_node)
33031 goto saw_error;
33032 break;
33033 }
33034 stmt_done:
33035 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33036 {
33037 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33038 goto saw_error;
33039 v = cp_parser_unary_expression (parser);
33040 if (v == error_mark_node)
33041 goto saw_error;
33042 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33043 goto saw_error;
33044 lhs1 = cp_parser_unary_expression (parser);
33045 if (lhs1 == error_mark_node)
33046 goto saw_error;
33047 }
33048 if (structured_block)
33049 {
33050 cp_parser_consume_semicolon_at_end_of_statement (parser);
33051 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33052 }
33053 done:
33054 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33055 if (!structured_block)
33056 cp_parser_consume_semicolon_at_end_of_statement (parser);
33057 return;
33058
33059 saw_error:
33060 cp_parser_skip_to_end_of_block_or_statement (parser);
33061 if (structured_block)
33062 {
33063 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33064 cp_lexer_consume_token (parser->lexer);
33065 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33066 {
33067 cp_parser_skip_to_end_of_block_or_statement (parser);
33068 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33069 cp_lexer_consume_token (parser->lexer);
33070 }
33071 }
33072 }
33073
33074
33075 /* OpenMP 2.5:
33076 # pragma omp barrier new-line */
33077
33078 static void
33079 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33080 {
33081 cp_parser_require_pragma_eol (parser, pragma_tok);
33082 finish_omp_barrier ();
33083 }
33084
33085 /* OpenMP 2.5:
33086 # pragma omp critical [(name)] new-line
33087 structured-block
33088
33089 OpenMP 4.5:
33090 # pragma omp critical [(name) [hint(expression)]] new-line
33091 structured-block */
33092
33093 #define OMP_CRITICAL_CLAUSE_MASK \
33094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33095
33096 static tree
33097 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33098 {
33099 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33100
33101 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33102 {
33103 cp_lexer_consume_token (parser->lexer);
33104
33105 name = cp_parser_identifier (parser);
33106
33107 if (name == error_mark_node
33108 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33109 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33110 /*or_comma=*/false,
33111 /*consume_paren=*/true);
33112 if (name == error_mark_node)
33113 name = NULL;
33114
33115 clauses = cp_parser_omp_all_clauses (parser,
33116 OMP_CRITICAL_CLAUSE_MASK,
33117 "#pragma omp critical", pragma_tok);
33118 }
33119 else
33120 cp_parser_require_pragma_eol (parser, pragma_tok);
33121
33122 stmt = cp_parser_omp_structured_block (parser, if_p);
33123 return c_finish_omp_critical (input_location, stmt, name, clauses);
33124 }
33125
33126 /* OpenMP 2.5:
33127 # pragma omp flush flush-vars[opt] new-line
33128
33129 flush-vars:
33130 ( variable-list ) */
33131
33132 static void
33133 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33134 {
33135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33136 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33137 cp_parser_require_pragma_eol (parser, pragma_tok);
33138
33139 finish_omp_flush ();
33140 }
33141
33142 /* Helper function, to parse omp for increment expression. */
33143
33144 static tree
33145 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33146 {
33147 tree cond = cp_parser_binary_expression (parser, false, true,
33148 PREC_NOT_OPERATOR, NULL);
33149 if (cond == error_mark_node
33150 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33151 {
33152 cp_parser_skip_to_end_of_statement (parser);
33153 return error_mark_node;
33154 }
33155
33156 switch (TREE_CODE (cond))
33157 {
33158 case GT_EXPR:
33159 case GE_EXPR:
33160 case LT_EXPR:
33161 case LE_EXPR:
33162 break;
33163 case NE_EXPR:
33164 if (code == CILK_SIMD || code == CILK_FOR)
33165 break;
33166 /* Fall through: OpenMP disallows NE_EXPR. */
33167 default:
33168 return error_mark_node;
33169 }
33170
33171 /* If decl is an iterator, preserve LHS and RHS of the relational
33172 expr until finish_omp_for. */
33173 if (decl
33174 && (type_dependent_expression_p (decl)
33175 || CLASS_TYPE_P (TREE_TYPE (decl))))
33176 return cond;
33177
33178 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33179 TREE_CODE (cond),
33180 TREE_OPERAND (cond, 0), ERROR_MARK,
33181 TREE_OPERAND (cond, 1), ERROR_MARK,
33182 /*overload=*/NULL, tf_warning_or_error);
33183 }
33184
33185 /* Helper function, to parse omp for increment expression. */
33186
33187 static tree
33188 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33189 {
33190 cp_token *token = cp_lexer_peek_token (parser->lexer);
33191 enum tree_code op;
33192 tree lhs, rhs;
33193 cp_id_kind idk;
33194 bool decl_first;
33195
33196 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33197 {
33198 op = (token->type == CPP_PLUS_PLUS
33199 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33200 cp_lexer_consume_token (parser->lexer);
33201 lhs = cp_parser_simple_cast_expression (parser);
33202 if (lhs != decl
33203 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33204 return error_mark_node;
33205 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33206 }
33207
33208 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33209 if (lhs != decl
33210 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33211 return error_mark_node;
33212
33213 token = cp_lexer_peek_token (parser->lexer);
33214 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33215 {
33216 op = (token->type == CPP_PLUS_PLUS
33217 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33218 cp_lexer_consume_token (parser->lexer);
33219 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33220 }
33221
33222 op = cp_parser_assignment_operator_opt (parser);
33223 if (op == ERROR_MARK)
33224 return error_mark_node;
33225
33226 if (op != NOP_EXPR)
33227 {
33228 rhs = cp_parser_assignment_expression (parser);
33229 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33230 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33231 }
33232
33233 lhs = cp_parser_binary_expression (parser, false, false,
33234 PREC_ADDITIVE_EXPRESSION, NULL);
33235 token = cp_lexer_peek_token (parser->lexer);
33236 decl_first = (lhs == decl
33237 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33238 if (decl_first)
33239 lhs = NULL_TREE;
33240 if (token->type != CPP_PLUS
33241 && token->type != CPP_MINUS)
33242 return error_mark_node;
33243
33244 do
33245 {
33246 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33247 cp_lexer_consume_token (parser->lexer);
33248 rhs = cp_parser_binary_expression (parser, false, false,
33249 PREC_ADDITIVE_EXPRESSION, NULL);
33250 token = cp_lexer_peek_token (parser->lexer);
33251 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33252 {
33253 if (lhs == NULL_TREE)
33254 {
33255 if (op == PLUS_EXPR)
33256 lhs = rhs;
33257 else
33258 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33259 tf_warning_or_error);
33260 }
33261 else
33262 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33263 ERROR_MARK, NULL, tf_warning_or_error);
33264 }
33265 }
33266 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33267
33268 if (!decl_first)
33269 {
33270 if ((rhs != decl
33271 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33272 || op == MINUS_EXPR)
33273 return error_mark_node;
33274 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33275 }
33276 else
33277 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33278
33279 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33280 }
33281
33282 /* Parse the initialization statement of either an OpenMP for loop or
33283 a Cilk Plus for loop.
33284
33285 Return true if the resulting construct should have an
33286 OMP_CLAUSE_PRIVATE added to it. */
33287
33288 static tree
33289 cp_parser_omp_for_loop_init (cp_parser *parser,
33290 enum tree_code code,
33291 tree &this_pre_body,
33292 vec<tree, va_gc> *for_block,
33293 tree &init,
33294 tree &orig_init,
33295 tree &decl,
33296 tree &real_decl)
33297 {
33298 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33299 return NULL_TREE;
33300
33301 tree add_private_clause = NULL_TREE;
33302
33303 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33304
33305 init-expr:
33306 var = lb
33307 integer-type var = lb
33308 random-access-iterator-type var = lb
33309 pointer-type var = lb
33310 */
33311 cp_decl_specifier_seq type_specifiers;
33312
33313 /* First, try to parse as an initialized declaration. See
33314 cp_parser_condition, from whence the bulk of this is copied. */
33315
33316 cp_parser_parse_tentatively (parser);
33317 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33318 /*is_trailing_return=*/false,
33319 &type_specifiers);
33320 if (cp_parser_parse_definitely (parser))
33321 {
33322 /* If parsing a type specifier seq succeeded, then this
33323 MUST be a initialized declaration. */
33324 tree asm_specification, attributes;
33325 cp_declarator *declarator;
33326
33327 declarator = cp_parser_declarator (parser,
33328 CP_PARSER_DECLARATOR_NAMED,
33329 /*ctor_dtor_or_conv_p=*/NULL,
33330 /*parenthesized_p=*/NULL,
33331 /*member_p=*/false,
33332 /*friend_p=*/false);
33333 attributes = cp_parser_attributes_opt (parser);
33334 asm_specification = cp_parser_asm_specification_opt (parser);
33335
33336 if (declarator == cp_error_declarator)
33337 cp_parser_skip_to_end_of_statement (parser);
33338
33339 else
33340 {
33341 tree pushed_scope, auto_node;
33342
33343 decl = start_decl (declarator, &type_specifiers,
33344 SD_INITIALIZED, attributes,
33345 /*prefix_attributes=*/NULL_TREE,
33346 &pushed_scope);
33347
33348 auto_node = type_uses_auto (TREE_TYPE (decl));
33349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33350 {
33351 if (cp_lexer_next_token_is (parser->lexer,
33352 CPP_OPEN_PAREN))
33353 {
33354 if (code != CILK_SIMD && code != CILK_FOR)
33355 error ("parenthesized initialization is not allowed in "
33356 "OpenMP %<for%> loop");
33357 else
33358 error ("parenthesized initialization is "
33359 "not allowed in for-loop");
33360 }
33361 else
33362 /* Trigger an error. */
33363 cp_parser_require (parser, CPP_EQ, RT_EQ);
33364
33365 init = error_mark_node;
33366 cp_parser_skip_to_end_of_statement (parser);
33367 }
33368 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33369 || type_dependent_expression_p (decl)
33370 || auto_node)
33371 {
33372 bool is_direct_init, is_non_constant_init;
33373
33374 init = cp_parser_initializer (parser,
33375 &is_direct_init,
33376 &is_non_constant_init);
33377
33378 if (auto_node)
33379 {
33380 TREE_TYPE (decl)
33381 = do_auto_deduction (TREE_TYPE (decl), init,
33382 auto_node);
33383
33384 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33385 && !type_dependent_expression_p (decl))
33386 goto non_class;
33387 }
33388
33389 cp_finish_decl (decl, init, !is_non_constant_init,
33390 asm_specification,
33391 LOOKUP_ONLYCONVERTING);
33392 orig_init = init;
33393 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33394 {
33395 vec_safe_push (for_block, this_pre_body);
33396 init = NULL_TREE;
33397 }
33398 else
33399 init = pop_stmt_list (this_pre_body);
33400 this_pre_body = NULL_TREE;
33401 }
33402 else
33403 {
33404 /* Consume '='. */
33405 cp_lexer_consume_token (parser->lexer);
33406 init = cp_parser_assignment_expression (parser);
33407
33408 non_class:
33409 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33410 init = error_mark_node;
33411 else
33412 cp_finish_decl (decl, NULL_TREE,
33413 /*init_const_expr_p=*/false,
33414 asm_specification,
33415 LOOKUP_ONLYCONVERTING);
33416 }
33417
33418 if (pushed_scope)
33419 pop_scope (pushed_scope);
33420 }
33421 }
33422 else
33423 {
33424 cp_id_kind idk;
33425 /* If parsing a type specifier sequence failed, then
33426 this MUST be a simple expression. */
33427 if (code == CILK_FOR)
33428 error ("%<_Cilk_for%> allows expression instead of declaration only "
33429 "in C, not in C++");
33430 cp_parser_parse_tentatively (parser);
33431 decl = cp_parser_primary_expression (parser, false, false,
33432 false, &idk);
33433 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33434 if (!cp_parser_error_occurred (parser)
33435 && decl
33436 && (TREE_CODE (decl) == COMPONENT_REF
33437 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33438 {
33439 cp_parser_abort_tentative_parse (parser);
33440 cp_parser_parse_tentatively (parser);
33441 cp_token *token = cp_lexer_peek_token (parser->lexer);
33442 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33443 /*check_dependency_p=*/true,
33444 /*template_p=*/NULL,
33445 /*declarator_p=*/false,
33446 /*optional_p=*/false);
33447 if (name != error_mark_node
33448 && last_tok == cp_lexer_peek_token (parser->lexer))
33449 {
33450 decl = cp_parser_lookup_name_simple (parser, name,
33451 token->location);
33452 if (TREE_CODE (decl) == FIELD_DECL)
33453 add_private_clause = omp_privatize_field (decl, false);
33454 }
33455 cp_parser_abort_tentative_parse (parser);
33456 cp_parser_parse_tentatively (parser);
33457 decl = cp_parser_primary_expression (parser, false, false,
33458 false, &idk);
33459 }
33460 if (!cp_parser_error_occurred (parser)
33461 && decl
33462 && DECL_P (decl)
33463 && CLASS_TYPE_P (TREE_TYPE (decl)))
33464 {
33465 tree rhs;
33466
33467 cp_parser_parse_definitely (parser);
33468 cp_parser_require (parser, CPP_EQ, RT_EQ);
33469 rhs = cp_parser_assignment_expression (parser);
33470 orig_init = rhs;
33471 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33472 decl, NOP_EXPR,
33473 rhs,
33474 tf_warning_or_error));
33475 if (!add_private_clause)
33476 add_private_clause = decl;
33477 }
33478 else
33479 {
33480 decl = NULL;
33481 cp_parser_abort_tentative_parse (parser);
33482 init = cp_parser_expression (parser);
33483 if (init)
33484 {
33485 if (TREE_CODE (init) == MODIFY_EXPR
33486 || TREE_CODE (init) == MODOP_EXPR)
33487 real_decl = TREE_OPERAND (init, 0);
33488 }
33489 }
33490 }
33491 return add_private_clause;
33492 }
33493
33494 /* Parse the restricted form of the for statement allowed by OpenMP. */
33495
33496 static tree
33497 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33498 tree *cclauses, bool *if_p)
33499 {
33500 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33501 tree real_decl, initv, condv, incrv, declv;
33502 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33503 location_t loc_first;
33504 bool collapse_err = false;
33505 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33506 vec<tree, va_gc> *for_block = make_tree_vector ();
33507 auto_vec<tree, 4> orig_inits;
33508
33509 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33510 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33511 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33512 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33513 && OMP_CLAUSE_ORDERED_EXPR (cl))
33514 {
33515 ordered_cl = cl;
33516 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33517 }
33518
33519 if (ordered && ordered < collapse)
33520 {
33521 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33522 "%<ordered%> clause parameter is less than %<collapse%>");
33523 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33524 = build_int_cst (NULL_TREE, collapse);
33525 ordered = collapse;
33526 }
33527 if (ordered)
33528 {
33529 for (tree *pc = &clauses; *pc; )
33530 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33531 {
33532 error_at (OMP_CLAUSE_LOCATION (*pc),
33533 "%<linear%> clause may not be specified together "
33534 "with %<ordered%> clause with a parameter");
33535 *pc = OMP_CLAUSE_CHAIN (*pc);
33536 }
33537 else
33538 pc = &OMP_CLAUSE_CHAIN (*pc);
33539 }
33540
33541 gcc_assert (collapse >= 1 && ordered >= 0);
33542 count = ordered ? ordered : collapse;
33543
33544 declv = make_tree_vec (count);
33545 initv = make_tree_vec (count);
33546 condv = make_tree_vec (count);
33547 incrv = make_tree_vec (count);
33548
33549 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33550
33551 for (i = 0; i < count; i++)
33552 {
33553 int bracecount = 0;
33554 tree add_private_clause = NULL_TREE;
33555 location_t loc;
33556
33557 if (code != CILK_FOR
33558 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33559 {
33560 cp_parser_error (parser, "for statement expected");
33561 return NULL;
33562 }
33563 if (code == CILK_FOR
33564 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33565 {
33566 cp_parser_error (parser, "_Cilk_for statement expected");
33567 return NULL;
33568 }
33569 loc = cp_lexer_consume_token (parser->lexer)->location;
33570
33571 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33572 return NULL;
33573
33574 init = orig_init = decl = real_decl = NULL;
33575 this_pre_body = push_stmt_list ();
33576
33577 add_private_clause
33578 = cp_parser_omp_for_loop_init (parser, code,
33579 this_pre_body, for_block,
33580 init, orig_init, decl, real_decl);
33581
33582 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33583 if (this_pre_body)
33584 {
33585 this_pre_body = pop_stmt_list (this_pre_body);
33586 if (pre_body)
33587 {
33588 tree t = pre_body;
33589 pre_body = push_stmt_list ();
33590 add_stmt (t);
33591 add_stmt (this_pre_body);
33592 pre_body = pop_stmt_list (pre_body);
33593 }
33594 else
33595 pre_body = this_pre_body;
33596 }
33597
33598 if (decl)
33599 real_decl = decl;
33600 if (cclauses != NULL
33601 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33602 && real_decl != NULL_TREE)
33603 {
33604 tree *c;
33605 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33606 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33607 && OMP_CLAUSE_DECL (*c) == real_decl)
33608 {
33609 error_at (loc, "iteration variable %qD"
33610 " should not be firstprivate", real_decl);
33611 *c = OMP_CLAUSE_CHAIN (*c);
33612 }
33613 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33614 && OMP_CLAUSE_DECL (*c) == real_decl)
33615 {
33616 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33617 tree l = *c;
33618 *c = OMP_CLAUSE_CHAIN (*c);
33619 if (code == OMP_SIMD)
33620 {
33621 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33622 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33623 }
33624 else
33625 {
33626 OMP_CLAUSE_CHAIN (l) = clauses;
33627 clauses = l;
33628 }
33629 add_private_clause = NULL_TREE;
33630 }
33631 else
33632 {
33633 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33634 && OMP_CLAUSE_DECL (*c) == real_decl)
33635 add_private_clause = NULL_TREE;
33636 c = &OMP_CLAUSE_CHAIN (*c);
33637 }
33638 }
33639
33640 if (add_private_clause)
33641 {
33642 tree c;
33643 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33644 {
33645 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33646 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33647 && OMP_CLAUSE_DECL (c) == decl)
33648 break;
33649 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33650 && OMP_CLAUSE_DECL (c) == decl)
33651 error_at (loc, "iteration variable %qD "
33652 "should not be firstprivate",
33653 decl);
33654 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33655 && OMP_CLAUSE_DECL (c) == decl)
33656 error_at (loc, "iteration variable %qD should not be reduction",
33657 decl);
33658 }
33659 if (c == NULL)
33660 {
33661 if (code != OMP_SIMD)
33662 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33663 else if (collapse == 1)
33664 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33665 else
33666 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33667 OMP_CLAUSE_DECL (c) = add_private_clause;
33668 c = finish_omp_clauses (c, C_ORT_OMP);
33669 if (c)
33670 {
33671 OMP_CLAUSE_CHAIN (c) = clauses;
33672 clauses = c;
33673 /* For linear, signal that we need to fill up
33674 the so far unknown linear step. */
33675 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33676 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33677 }
33678 }
33679 }
33680
33681 cond = NULL;
33682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33683 cond = cp_parser_omp_for_cond (parser, decl, code);
33684 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33685
33686 incr = NULL;
33687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33688 {
33689 /* If decl is an iterator, preserve the operator on decl
33690 until finish_omp_for. */
33691 if (real_decl
33692 && ((processing_template_decl
33693 && (TREE_TYPE (real_decl) == NULL_TREE
33694 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33695 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33696 incr = cp_parser_omp_for_incr (parser, real_decl);
33697 else
33698 incr = cp_parser_expression (parser);
33699 if (!EXPR_HAS_LOCATION (incr))
33700 protected_set_expr_location (incr, input_location);
33701 }
33702
33703 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33704 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33705 /*or_comma=*/false,
33706 /*consume_paren=*/true);
33707
33708 TREE_VEC_ELT (declv, i) = decl;
33709 TREE_VEC_ELT (initv, i) = init;
33710 TREE_VEC_ELT (condv, i) = cond;
33711 TREE_VEC_ELT (incrv, i) = incr;
33712 if (orig_init)
33713 {
33714 orig_inits.safe_grow_cleared (i + 1);
33715 orig_inits[i] = orig_init;
33716 }
33717
33718 if (i == count - 1)
33719 break;
33720
33721 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33722 in between the collapsed for loops to be still considered perfectly
33723 nested. Hopefully the final version clarifies this.
33724 For now handle (multiple) {'s and empty statements. */
33725 cp_parser_parse_tentatively (parser);
33726 do
33727 {
33728 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33729 break;
33730 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33731 {
33732 cp_lexer_consume_token (parser->lexer);
33733 bracecount++;
33734 }
33735 else if (bracecount
33736 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33737 cp_lexer_consume_token (parser->lexer);
33738 else
33739 {
33740 loc = cp_lexer_peek_token (parser->lexer)->location;
33741 error_at (loc, "not enough collapsed for loops");
33742 collapse_err = true;
33743 cp_parser_abort_tentative_parse (parser);
33744 declv = NULL_TREE;
33745 break;
33746 }
33747 }
33748 while (1);
33749
33750 if (declv)
33751 {
33752 cp_parser_parse_definitely (parser);
33753 nbraces += bracecount;
33754 }
33755 }
33756
33757 if (nbraces)
33758 if_p = NULL;
33759
33760 /* Note that we saved the original contents of this flag when we entered
33761 the structured block, and so we don't need to re-save it here. */
33762 if (code == CILK_SIMD || code == CILK_FOR)
33763 parser->in_statement = IN_CILK_SIMD_FOR;
33764 else
33765 parser->in_statement = IN_OMP_FOR;
33766
33767 /* Note that the grammar doesn't call for a structured block here,
33768 though the loop as a whole is a structured block. */
33769 body = push_stmt_list ();
33770 cp_parser_statement (parser, NULL_TREE, false, if_p);
33771 body = pop_stmt_list (body);
33772
33773 if (declv == NULL_TREE)
33774 ret = NULL_TREE;
33775 else
33776 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33777 body, pre_body, &orig_inits, clauses);
33778
33779 while (nbraces)
33780 {
33781 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33782 {
33783 cp_lexer_consume_token (parser->lexer);
33784 nbraces--;
33785 }
33786 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33787 cp_lexer_consume_token (parser->lexer);
33788 else
33789 {
33790 if (!collapse_err)
33791 {
33792 error_at (cp_lexer_peek_token (parser->lexer)->location,
33793 "collapsed loops not perfectly nested");
33794 }
33795 collapse_err = true;
33796 cp_parser_statement_seq_opt (parser, NULL);
33797 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33798 break;
33799 }
33800 }
33801
33802 while (!for_block->is_empty ())
33803 add_stmt (pop_stmt_list (for_block->pop ()));
33804 release_tree_vector (for_block);
33805
33806 return ret;
33807 }
33808
33809 /* Helper function for OpenMP parsing, split clauses and call
33810 finish_omp_clauses on each of the set of clauses afterwards. */
33811
33812 static void
33813 cp_omp_split_clauses (location_t loc, enum tree_code code,
33814 omp_clause_mask mask, tree clauses, tree *cclauses)
33815 {
33816 int i;
33817 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33818 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33819 if (cclauses[i])
33820 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
33821 }
33822
33823 /* OpenMP 4.0:
33824 #pragma omp simd simd-clause[optseq] new-line
33825 for-loop */
33826
33827 #define OMP_SIMD_CLAUSE_MASK \
33828 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33836
33837 static tree
33838 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33839 char *p_name, omp_clause_mask mask, tree *cclauses,
33840 bool *if_p)
33841 {
33842 tree clauses, sb, ret;
33843 unsigned int save;
33844 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33845
33846 strcat (p_name, " simd");
33847 mask |= OMP_SIMD_CLAUSE_MASK;
33848
33849 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33850 cclauses == NULL);
33851 if (cclauses)
33852 {
33853 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33854 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33855 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33856 OMP_CLAUSE_ORDERED);
33857 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33858 {
33859 error_at (OMP_CLAUSE_LOCATION (c),
33860 "%<ordered%> clause with parameter may not be specified "
33861 "on %qs construct", p_name);
33862 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33863 }
33864 }
33865
33866 sb = begin_omp_structured_block ();
33867 save = cp_parser_begin_omp_structured_block (parser);
33868
33869 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
33870
33871 cp_parser_end_omp_structured_block (parser, save);
33872 add_stmt (finish_omp_structured_block (sb));
33873
33874 return ret;
33875 }
33876
33877 /* OpenMP 2.5:
33878 #pragma omp for for-clause[optseq] new-line
33879 for-loop
33880
33881 OpenMP 4.0:
33882 #pragma omp for simd for-simd-clause[optseq] new-line
33883 for-loop */
33884
33885 #define OMP_FOR_CLAUSE_MASK \
33886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33895
33896 static tree
33897 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33898 char *p_name, omp_clause_mask mask, tree *cclauses,
33899 bool *if_p)
33900 {
33901 tree clauses, sb, ret;
33902 unsigned int save;
33903 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33904
33905 strcat (p_name, " for");
33906 mask |= OMP_FOR_CLAUSE_MASK;
33907 if (cclauses)
33908 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33909 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33910 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33911 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33912
33913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33914 {
33915 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33916 const char *p = IDENTIFIER_POINTER (id);
33917
33918 if (strcmp (p, "simd") == 0)
33919 {
33920 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33921 if (cclauses == NULL)
33922 cclauses = cclauses_buf;
33923
33924 cp_lexer_consume_token (parser->lexer);
33925 if (!flag_openmp) /* flag_openmp_simd */
33926 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33927 cclauses, if_p);
33928 sb = begin_omp_structured_block ();
33929 save = cp_parser_begin_omp_structured_block (parser);
33930 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33931 cclauses, if_p);
33932 cp_parser_end_omp_structured_block (parser, save);
33933 tree body = finish_omp_structured_block (sb);
33934 if (ret == NULL)
33935 return ret;
33936 ret = make_node (OMP_FOR);
33937 TREE_TYPE (ret) = void_type_node;
33938 OMP_FOR_BODY (ret) = body;
33939 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33940 SET_EXPR_LOCATION (ret, loc);
33941 add_stmt (ret);
33942 return ret;
33943 }
33944 }
33945 if (!flag_openmp) /* flag_openmp_simd */
33946 {
33947 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33948 return NULL_TREE;
33949 }
33950
33951 /* Composite distribute parallel for disallows linear clause. */
33952 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33953 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33954
33955 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33956 cclauses == NULL);
33957 if (cclauses)
33958 {
33959 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33960 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33961 }
33962
33963 sb = begin_omp_structured_block ();
33964 save = cp_parser_begin_omp_structured_block (parser);
33965
33966 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
33967
33968 cp_parser_end_omp_structured_block (parser, save);
33969 add_stmt (finish_omp_structured_block (sb));
33970
33971 return ret;
33972 }
33973
33974 /* OpenMP 2.5:
33975 # pragma omp master new-line
33976 structured-block */
33977
33978 static tree
33979 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33980 {
33981 cp_parser_require_pragma_eol (parser, pragma_tok);
33982 return c_finish_omp_master (input_location,
33983 cp_parser_omp_structured_block (parser, if_p));
33984 }
33985
33986 /* OpenMP 2.5:
33987 # pragma omp ordered new-line
33988 structured-block
33989
33990 OpenMP 4.5:
33991 # pragma omp ordered ordered-clauses new-line
33992 structured-block */
33993
33994 #define OMP_ORDERED_CLAUSE_MASK \
33995 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33997
33998 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33999 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34000
34001 static bool
34002 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34003 enum pragma_context context, bool *if_p)
34004 {
34005 location_t loc = pragma_tok->location;
34006
34007 if (context != pragma_stmt && context != pragma_compound)
34008 {
34009 cp_parser_error (parser, "expected declaration specifiers");
34010 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34011 return false;
34012 }
34013
34014 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34015 {
34016 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34017 const char *p = IDENTIFIER_POINTER (id);
34018
34019 if (strcmp (p, "depend") == 0)
34020 {
34021 if (context == pragma_stmt)
34022 {
34023 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34024 "%<depend%> clause may only be used in compound "
34025 "statements");
34026 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34027 return false;
34028 }
34029 tree clauses
34030 = cp_parser_omp_all_clauses (parser,
34031 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34032 "#pragma omp ordered", pragma_tok);
34033 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34034 return false;
34035 }
34036 }
34037
34038 tree clauses
34039 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34040 "#pragma omp ordered", pragma_tok);
34041 c_finish_omp_ordered (loc, clauses,
34042 cp_parser_omp_structured_block (parser, if_p));
34043 return true;
34044 }
34045
34046 /* OpenMP 2.5:
34047
34048 section-scope:
34049 { section-sequence }
34050
34051 section-sequence:
34052 section-directive[opt] structured-block
34053 section-sequence section-directive structured-block */
34054
34055 static tree
34056 cp_parser_omp_sections_scope (cp_parser *parser)
34057 {
34058 tree stmt, substmt;
34059 bool error_suppress = false;
34060 cp_token *tok;
34061
34062 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34063 return NULL_TREE;
34064
34065 stmt = push_stmt_list ();
34066
34067 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34068 != PRAGMA_OMP_SECTION)
34069 {
34070 substmt = cp_parser_omp_structured_block (parser, NULL);
34071 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34072 add_stmt (substmt);
34073 }
34074
34075 while (1)
34076 {
34077 tok = cp_lexer_peek_token (parser->lexer);
34078 if (tok->type == CPP_CLOSE_BRACE)
34079 break;
34080 if (tok->type == CPP_EOF)
34081 break;
34082
34083 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34084 {
34085 cp_lexer_consume_token (parser->lexer);
34086 cp_parser_require_pragma_eol (parser, tok);
34087 error_suppress = false;
34088 }
34089 else if (!error_suppress)
34090 {
34091 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34092 error_suppress = true;
34093 }
34094
34095 substmt = cp_parser_omp_structured_block (parser, NULL);
34096 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34097 add_stmt (substmt);
34098 }
34099 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34100
34101 substmt = pop_stmt_list (stmt);
34102
34103 stmt = make_node (OMP_SECTIONS);
34104 TREE_TYPE (stmt) = void_type_node;
34105 OMP_SECTIONS_BODY (stmt) = substmt;
34106
34107 add_stmt (stmt);
34108 return stmt;
34109 }
34110
34111 /* OpenMP 2.5:
34112 # pragma omp sections sections-clause[optseq] newline
34113 sections-scope */
34114
34115 #define OMP_SECTIONS_CLAUSE_MASK \
34116 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34121
34122 static tree
34123 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34124 char *p_name, omp_clause_mask mask, tree *cclauses)
34125 {
34126 tree clauses, ret;
34127 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34128
34129 strcat (p_name, " sections");
34130 mask |= OMP_SECTIONS_CLAUSE_MASK;
34131 if (cclauses)
34132 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34133
34134 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34135 cclauses == NULL);
34136 if (cclauses)
34137 {
34138 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34139 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34140 }
34141
34142 ret = cp_parser_omp_sections_scope (parser);
34143 if (ret)
34144 OMP_SECTIONS_CLAUSES (ret) = clauses;
34145
34146 return ret;
34147 }
34148
34149 /* OpenMP 2.5:
34150 # pragma omp parallel parallel-clause[optseq] new-line
34151 structured-block
34152 # pragma omp parallel for parallel-for-clause[optseq] new-line
34153 structured-block
34154 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34155 structured-block
34156
34157 OpenMP 4.0:
34158 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34159 structured-block */
34160
34161 #define OMP_PARALLEL_CLAUSE_MASK \
34162 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34171
34172 static tree
34173 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34174 char *p_name, omp_clause_mask mask, tree *cclauses,
34175 bool *if_p)
34176 {
34177 tree stmt, clauses, block;
34178 unsigned int save;
34179 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34180
34181 strcat (p_name, " parallel");
34182 mask |= OMP_PARALLEL_CLAUSE_MASK;
34183 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34184 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34185 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34186 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34187
34188 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34189 {
34190 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34191 if (cclauses == NULL)
34192 cclauses = cclauses_buf;
34193
34194 cp_lexer_consume_token (parser->lexer);
34195 if (!flag_openmp) /* flag_openmp_simd */
34196 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34197 if_p);
34198 block = begin_omp_parallel ();
34199 save = cp_parser_begin_omp_structured_block (parser);
34200 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34201 if_p);
34202 cp_parser_end_omp_structured_block (parser, save);
34203 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34204 block);
34205 if (ret == NULL_TREE)
34206 return ret;
34207 OMP_PARALLEL_COMBINED (stmt) = 1;
34208 return stmt;
34209 }
34210 /* When combined with distribute, parallel has to be followed by for.
34211 #pragma omp target parallel is allowed though. */
34212 else if (cclauses
34213 && (mask & (OMP_CLAUSE_MASK_1
34214 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34215 {
34216 error_at (loc, "expected %<for%> after %qs", p_name);
34217 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34218 return NULL_TREE;
34219 }
34220 else if (!flag_openmp) /* flag_openmp_simd */
34221 {
34222 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34223 return NULL_TREE;
34224 }
34225 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34226 {
34227 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34228 const char *p = IDENTIFIER_POINTER (id);
34229 if (strcmp (p, "sections") == 0)
34230 {
34231 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34232 cclauses = cclauses_buf;
34233
34234 cp_lexer_consume_token (parser->lexer);
34235 block = begin_omp_parallel ();
34236 save = cp_parser_begin_omp_structured_block (parser);
34237 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34238 cp_parser_end_omp_structured_block (parser, save);
34239 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34240 block);
34241 OMP_PARALLEL_COMBINED (stmt) = 1;
34242 return stmt;
34243 }
34244 }
34245
34246 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
34247 if (cclauses)
34248 {
34249 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34250 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34251 }
34252
34253 block = begin_omp_parallel ();
34254 save = cp_parser_begin_omp_structured_block (parser);
34255 cp_parser_statement (parser, NULL_TREE, false, if_p);
34256 cp_parser_end_omp_structured_block (parser, save);
34257 stmt = finish_omp_parallel (clauses, block);
34258 return stmt;
34259 }
34260
34261 /* OpenMP 2.5:
34262 # pragma omp single single-clause[optseq] new-line
34263 structured-block */
34264
34265 #define OMP_SINGLE_CLAUSE_MASK \
34266 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34270
34271 static tree
34272 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34273 {
34274 tree stmt = make_node (OMP_SINGLE);
34275 TREE_TYPE (stmt) = void_type_node;
34276
34277 OMP_SINGLE_CLAUSES (stmt)
34278 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34279 "#pragma omp single", pragma_tok);
34280 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34281
34282 return add_stmt (stmt);
34283 }
34284
34285 /* OpenMP 3.0:
34286 # pragma omp task task-clause[optseq] new-line
34287 structured-block */
34288
34289 #define OMP_TASK_CLAUSE_MASK \
34290 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34300
34301 static tree
34302 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34303 {
34304 tree clauses, block;
34305 unsigned int save;
34306
34307 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34308 "#pragma omp task", pragma_tok);
34309 block = begin_omp_task ();
34310 save = cp_parser_begin_omp_structured_block (parser);
34311 cp_parser_statement (parser, NULL_TREE, false, if_p);
34312 cp_parser_end_omp_structured_block (parser, save);
34313 return finish_omp_task (clauses, block);
34314 }
34315
34316 /* OpenMP 3.0:
34317 # pragma omp taskwait new-line */
34318
34319 static void
34320 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34321 {
34322 cp_parser_require_pragma_eol (parser, pragma_tok);
34323 finish_omp_taskwait ();
34324 }
34325
34326 /* OpenMP 3.1:
34327 # pragma omp taskyield new-line */
34328
34329 static void
34330 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34331 {
34332 cp_parser_require_pragma_eol (parser, pragma_tok);
34333 finish_omp_taskyield ();
34334 }
34335
34336 /* OpenMP 4.0:
34337 # pragma omp taskgroup new-line
34338 structured-block */
34339
34340 static tree
34341 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34342 {
34343 cp_parser_require_pragma_eol (parser, pragma_tok);
34344 return c_finish_omp_taskgroup (input_location,
34345 cp_parser_omp_structured_block (parser,
34346 if_p));
34347 }
34348
34349
34350 /* OpenMP 2.5:
34351 # pragma omp threadprivate (variable-list) */
34352
34353 static void
34354 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34355 {
34356 tree vars;
34357
34358 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34359 cp_parser_require_pragma_eol (parser, pragma_tok);
34360
34361 finish_omp_threadprivate (vars);
34362 }
34363
34364 /* OpenMP 4.0:
34365 # pragma omp cancel cancel-clause[optseq] new-line */
34366
34367 #define OMP_CANCEL_CLAUSE_MASK \
34368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34373
34374 static void
34375 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34376 {
34377 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34378 "#pragma omp cancel", pragma_tok);
34379 finish_omp_cancel (clauses);
34380 }
34381
34382 /* OpenMP 4.0:
34383 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34384
34385 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34386 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34390
34391 static void
34392 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34393 {
34394 tree clauses;
34395 bool point_seen = false;
34396
34397 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34398 {
34399 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34400 const char *p = IDENTIFIER_POINTER (id);
34401
34402 if (strcmp (p, "point") == 0)
34403 {
34404 cp_lexer_consume_token (parser->lexer);
34405 point_seen = true;
34406 }
34407 }
34408 if (!point_seen)
34409 {
34410 cp_parser_error (parser, "expected %<point%>");
34411 cp_parser_require_pragma_eol (parser, pragma_tok);
34412 return;
34413 }
34414
34415 clauses = cp_parser_omp_all_clauses (parser,
34416 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34417 "#pragma omp cancellation point",
34418 pragma_tok);
34419 finish_omp_cancellation_point (clauses);
34420 }
34421
34422 /* OpenMP 4.0:
34423 #pragma omp distribute distribute-clause[optseq] new-line
34424 for-loop */
34425
34426 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34427 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34432
34433 static tree
34434 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34435 char *p_name, omp_clause_mask mask, tree *cclauses,
34436 bool *if_p)
34437 {
34438 tree clauses, sb, ret;
34439 unsigned int save;
34440 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34441
34442 strcat (p_name, " distribute");
34443 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34444
34445 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34446 {
34447 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34448 const char *p = IDENTIFIER_POINTER (id);
34449 bool simd = false;
34450 bool parallel = false;
34451
34452 if (strcmp (p, "simd") == 0)
34453 simd = true;
34454 else
34455 parallel = strcmp (p, "parallel") == 0;
34456 if (parallel || simd)
34457 {
34458 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34459 if (cclauses == NULL)
34460 cclauses = cclauses_buf;
34461 cp_lexer_consume_token (parser->lexer);
34462 if (!flag_openmp) /* flag_openmp_simd */
34463 {
34464 if (simd)
34465 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34466 cclauses, if_p);
34467 else
34468 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34469 cclauses, if_p);
34470 }
34471 sb = begin_omp_structured_block ();
34472 save = cp_parser_begin_omp_structured_block (parser);
34473 if (simd)
34474 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34475 cclauses, if_p);
34476 else
34477 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34478 cclauses, if_p);
34479 cp_parser_end_omp_structured_block (parser, save);
34480 tree body = finish_omp_structured_block (sb);
34481 if (ret == NULL)
34482 return ret;
34483 ret = make_node (OMP_DISTRIBUTE);
34484 TREE_TYPE (ret) = void_type_node;
34485 OMP_FOR_BODY (ret) = body;
34486 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34487 SET_EXPR_LOCATION (ret, loc);
34488 add_stmt (ret);
34489 return ret;
34490 }
34491 }
34492 if (!flag_openmp) /* flag_openmp_simd */
34493 {
34494 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34495 return NULL_TREE;
34496 }
34497
34498 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34499 cclauses == NULL);
34500 if (cclauses)
34501 {
34502 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34503 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34504 }
34505
34506 sb = begin_omp_structured_block ();
34507 save = cp_parser_begin_omp_structured_block (parser);
34508
34509 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34510
34511 cp_parser_end_omp_structured_block (parser, save);
34512 add_stmt (finish_omp_structured_block (sb));
34513
34514 return ret;
34515 }
34516
34517 /* OpenMP 4.0:
34518 # pragma omp teams teams-clause[optseq] new-line
34519 structured-block */
34520
34521 #define OMP_TEAMS_CLAUSE_MASK \
34522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34529
34530 static tree
34531 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34532 char *p_name, omp_clause_mask mask, tree *cclauses,
34533 bool *if_p)
34534 {
34535 tree clauses, sb, ret;
34536 unsigned int save;
34537 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34538
34539 strcat (p_name, " teams");
34540 mask |= OMP_TEAMS_CLAUSE_MASK;
34541
34542 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34543 {
34544 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34545 const char *p = IDENTIFIER_POINTER (id);
34546 if (strcmp (p, "distribute") == 0)
34547 {
34548 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34549 if (cclauses == NULL)
34550 cclauses = cclauses_buf;
34551
34552 cp_lexer_consume_token (parser->lexer);
34553 if (!flag_openmp) /* flag_openmp_simd */
34554 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34555 cclauses, if_p);
34556 sb = begin_omp_structured_block ();
34557 save = cp_parser_begin_omp_structured_block (parser);
34558 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34559 cclauses, if_p);
34560 cp_parser_end_omp_structured_block (parser, save);
34561 tree body = finish_omp_structured_block (sb);
34562 if (ret == NULL)
34563 return ret;
34564 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34565 ret = make_node (OMP_TEAMS);
34566 TREE_TYPE (ret) = void_type_node;
34567 OMP_TEAMS_CLAUSES (ret) = clauses;
34568 OMP_TEAMS_BODY (ret) = body;
34569 OMP_TEAMS_COMBINED (ret) = 1;
34570 return add_stmt (ret);
34571 }
34572 }
34573 if (!flag_openmp) /* flag_openmp_simd */
34574 {
34575 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34576 return NULL_TREE;
34577 }
34578
34579 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34580 cclauses == NULL);
34581 if (cclauses)
34582 {
34583 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34584 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34585 }
34586
34587 tree stmt = make_node (OMP_TEAMS);
34588 TREE_TYPE (stmt) = void_type_node;
34589 OMP_TEAMS_CLAUSES (stmt) = clauses;
34590 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34591
34592 return add_stmt (stmt);
34593 }
34594
34595 /* OpenMP 4.0:
34596 # pragma omp target data target-data-clause[optseq] new-line
34597 structured-block */
34598
34599 #define OMP_TARGET_DATA_CLAUSE_MASK \
34600 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34604
34605 static tree
34606 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34607 {
34608 tree clauses
34609 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34610 "#pragma omp target data", pragma_tok);
34611 int map_seen = 0;
34612 for (tree *pc = &clauses; *pc;)
34613 {
34614 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34615 switch (OMP_CLAUSE_MAP_KIND (*pc))
34616 {
34617 case GOMP_MAP_TO:
34618 case GOMP_MAP_ALWAYS_TO:
34619 case GOMP_MAP_FROM:
34620 case GOMP_MAP_ALWAYS_FROM:
34621 case GOMP_MAP_TOFROM:
34622 case GOMP_MAP_ALWAYS_TOFROM:
34623 case GOMP_MAP_ALLOC:
34624 map_seen = 3;
34625 break;
34626 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34627 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34628 case GOMP_MAP_ALWAYS_POINTER:
34629 break;
34630 default:
34631 map_seen |= 1;
34632 error_at (OMP_CLAUSE_LOCATION (*pc),
34633 "%<#pragma omp target data%> with map-type other "
34634 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34635 "on %<map%> clause");
34636 *pc = OMP_CLAUSE_CHAIN (*pc);
34637 continue;
34638 }
34639 pc = &OMP_CLAUSE_CHAIN (*pc);
34640 }
34641
34642 if (map_seen != 3)
34643 {
34644 if (map_seen == 0)
34645 error_at (pragma_tok->location,
34646 "%<#pragma omp target data%> must contain at least "
34647 "one %<map%> clause");
34648 return NULL_TREE;
34649 }
34650
34651 tree stmt = make_node (OMP_TARGET_DATA);
34652 TREE_TYPE (stmt) = void_type_node;
34653 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34654
34655 keep_next_level (true);
34656 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34657
34658 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34659 return add_stmt (stmt);
34660 }
34661
34662 /* OpenMP 4.5:
34663 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34664 structured-block */
34665
34666 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34672
34673 static tree
34674 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34675 enum pragma_context context)
34676 {
34677 bool data_seen = false;
34678 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34679 {
34680 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34681 const char *p = IDENTIFIER_POINTER (id);
34682
34683 if (strcmp (p, "data") == 0)
34684 {
34685 cp_lexer_consume_token (parser->lexer);
34686 data_seen = true;
34687 }
34688 }
34689 if (!data_seen)
34690 {
34691 cp_parser_error (parser, "expected %<data%>");
34692 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34693 return NULL_TREE;
34694 }
34695
34696 if (context == pragma_stmt)
34697 {
34698 error_at (pragma_tok->location,
34699 "%<#pragma omp target enter data%> may only be "
34700 "used in compound statements");
34701 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34702 return NULL_TREE;
34703 }
34704
34705 tree clauses
34706 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34707 "#pragma omp target enter data", pragma_tok);
34708 int map_seen = 0;
34709 for (tree *pc = &clauses; *pc;)
34710 {
34711 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34712 switch (OMP_CLAUSE_MAP_KIND (*pc))
34713 {
34714 case GOMP_MAP_TO:
34715 case GOMP_MAP_ALWAYS_TO:
34716 case GOMP_MAP_ALLOC:
34717 map_seen = 3;
34718 break;
34719 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34720 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34721 case GOMP_MAP_ALWAYS_POINTER:
34722 break;
34723 default:
34724 map_seen |= 1;
34725 error_at (OMP_CLAUSE_LOCATION (*pc),
34726 "%<#pragma omp target enter data%> with map-type other "
34727 "than %<to%> or %<alloc%> on %<map%> clause");
34728 *pc = OMP_CLAUSE_CHAIN (*pc);
34729 continue;
34730 }
34731 pc = &OMP_CLAUSE_CHAIN (*pc);
34732 }
34733
34734 if (map_seen != 3)
34735 {
34736 if (map_seen == 0)
34737 error_at (pragma_tok->location,
34738 "%<#pragma omp target enter data%> must contain at least "
34739 "one %<map%> clause");
34740 return NULL_TREE;
34741 }
34742
34743 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34744 TREE_TYPE (stmt) = void_type_node;
34745 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34746 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34747 return add_stmt (stmt);
34748 }
34749
34750 /* OpenMP 4.5:
34751 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34752 structured-block */
34753
34754 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34755 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34760
34761 static tree
34762 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34763 enum pragma_context context)
34764 {
34765 bool data_seen = false;
34766 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34767 {
34768 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34769 const char *p = IDENTIFIER_POINTER (id);
34770
34771 if (strcmp (p, "data") == 0)
34772 {
34773 cp_lexer_consume_token (parser->lexer);
34774 data_seen = true;
34775 }
34776 }
34777 if (!data_seen)
34778 {
34779 cp_parser_error (parser, "expected %<data%>");
34780 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34781 return NULL_TREE;
34782 }
34783
34784 if (context == pragma_stmt)
34785 {
34786 error_at (pragma_tok->location,
34787 "%<#pragma omp target exit data%> may only be "
34788 "used in compound statements");
34789 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34790 return NULL_TREE;
34791 }
34792
34793 tree clauses
34794 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34795 "#pragma omp target exit data", pragma_tok);
34796 int map_seen = 0;
34797 for (tree *pc = &clauses; *pc;)
34798 {
34799 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34800 switch (OMP_CLAUSE_MAP_KIND (*pc))
34801 {
34802 case GOMP_MAP_FROM:
34803 case GOMP_MAP_ALWAYS_FROM:
34804 case GOMP_MAP_RELEASE:
34805 case GOMP_MAP_DELETE:
34806 map_seen = 3;
34807 break;
34808 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34809 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34810 case GOMP_MAP_ALWAYS_POINTER:
34811 break;
34812 default:
34813 map_seen |= 1;
34814 error_at (OMP_CLAUSE_LOCATION (*pc),
34815 "%<#pragma omp target exit data%> with map-type other "
34816 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34817 " clause");
34818 *pc = OMP_CLAUSE_CHAIN (*pc);
34819 continue;
34820 }
34821 pc = &OMP_CLAUSE_CHAIN (*pc);
34822 }
34823
34824 if (map_seen != 3)
34825 {
34826 if (map_seen == 0)
34827 error_at (pragma_tok->location,
34828 "%<#pragma omp target exit data%> must contain at least "
34829 "one %<map%> clause");
34830 return NULL_TREE;
34831 }
34832
34833 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34834 TREE_TYPE (stmt) = void_type_node;
34835 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34836 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34837 return add_stmt (stmt);
34838 }
34839
34840 /* OpenMP 4.0:
34841 # pragma omp target update target-update-clause[optseq] new-line */
34842
34843 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34844 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34850
34851 static bool
34852 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34853 enum pragma_context context)
34854 {
34855 if (context == pragma_stmt)
34856 {
34857 error_at (pragma_tok->location,
34858 "%<#pragma omp target update%> may only be "
34859 "used in compound statements");
34860 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34861 return false;
34862 }
34863
34864 tree clauses
34865 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34866 "#pragma omp target update", pragma_tok);
34867 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34868 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34869 {
34870 error_at (pragma_tok->location,
34871 "%<#pragma omp target update%> must contain at least one "
34872 "%<from%> or %<to%> clauses");
34873 return false;
34874 }
34875
34876 tree stmt = make_node (OMP_TARGET_UPDATE);
34877 TREE_TYPE (stmt) = void_type_node;
34878 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34879 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34880 add_stmt (stmt);
34881 return false;
34882 }
34883
34884 /* OpenMP 4.0:
34885 # pragma omp target target-clause[optseq] new-line
34886 structured-block */
34887
34888 #define OMP_TARGET_CLAUSE_MASK \
34889 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34898
34899 static bool
34900 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34901 enum pragma_context context, bool *if_p)
34902 {
34903 tree *pc = NULL, stmt;
34904
34905 if (context != pragma_stmt && context != pragma_compound)
34906 {
34907 cp_parser_error (parser, "expected declaration specifiers");
34908 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34909 return false;
34910 }
34911
34912 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34913 {
34914 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34915 const char *p = IDENTIFIER_POINTER (id);
34916 enum tree_code ccode = ERROR_MARK;
34917
34918 if (strcmp (p, "teams") == 0)
34919 ccode = OMP_TEAMS;
34920 else if (strcmp (p, "parallel") == 0)
34921 ccode = OMP_PARALLEL;
34922 else if (strcmp (p, "simd") == 0)
34923 ccode = OMP_SIMD;
34924 if (ccode != ERROR_MARK)
34925 {
34926 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34927 char p_name[sizeof ("#pragma omp target teams distribute "
34928 "parallel for simd")];
34929
34930 cp_lexer_consume_token (parser->lexer);
34931 strcpy (p_name, "#pragma omp target");
34932 if (!flag_openmp) /* flag_openmp_simd */
34933 {
34934 tree stmt;
34935 switch (ccode)
34936 {
34937 case OMP_TEAMS:
34938 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34939 OMP_TARGET_CLAUSE_MASK,
34940 cclauses, if_p);
34941 break;
34942 case OMP_PARALLEL:
34943 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34944 OMP_TARGET_CLAUSE_MASK,
34945 cclauses, if_p);
34946 break;
34947 case OMP_SIMD:
34948 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34949 OMP_TARGET_CLAUSE_MASK,
34950 cclauses, if_p);
34951 break;
34952 default:
34953 gcc_unreachable ();
34954 }
34955 return stmt != NULL_TREE;
34956 }
34957 keep_next_level (true);
34958 tree sb = begin_omp_structured_block (), ret;
34959 unsigned save = cp_parser_begin_omp_structured_block (parser);
34960 switch (ccode)
34961 {
34962 case OMP_TEAMS:
34963 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34964 OMP_TARGET_CLAUSE_MASK, cclauses,
34965 if_p);
34966 break;
34967 case OMP_PARALLEL:
34968 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34969 OMP_TARGET_CLAUSE_MASK, cclauses,
34970 if_p);
34971 break;
34972 case OMP_SIMD:
34973 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
34974 OMP_TARGET_CLAUSE_MASK, cclauses,
34975 if_p);
34976 break;
34977 default:
34978 gcc_unreachable ();
34979 }
34980 cp_parser_end_omp_structured_block (parser, save);
34981 tree body = finish_omp_structured_block (sb);
34982 if (ret == NULL_TREE)
34983 return false;
34984 if (ccode == OMP_TEAMS && !processing_template_decl)
34985 {
34986 /* For combined target teams, ensure the num_teams and
34987 thread_limit clause expressions are evaluated on the host,
34988 before entering the target construct. */
34989 tree c;
34990 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34991 c; c = OMP_CLAUSE_CHAIN (c))
34992 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
34993 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
34994 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
34995 {
34996 tree expr = OMP_CLAUSE_OPERAND (c, 0);
34997 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
34998 if (expr == error_mark_node)
34999 continue;
35000 tree tmp = TARGET_EXPR_SLOT (expr);
35001 add_stmt (expr);
35002 OMP_CLAUSE_OPERAND (c, 0) = expr;
35003 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35004 OMP_CLAUSE_FIRSTPRIVATE);
35005 OMP_CLAUSE_DECL (tc) = tmp;
35006 OMP_CLAUSE_CHAIN (tc)
35007 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35008 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35009 }
35010 }
35011 tree stmt = make_node (OMP_TARGET);
35012 TREE_TYPE (stmt) = void_type_node;
35013 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35014 OMP_TARGET_BODY (stmt) = body;
35015 OMP_TARGET_COMBINED (stmt) = 1;
35016 add_stmt (stmt);
35017 pc = &OMP_TARGET_CLAUSES (stmt);
35018 goto check_clauses;
35019 }
35020 else if (!flag_openmp) /* flag_openmp_simd */
35021 {
35022 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35023 return false;
35024 }
35025 else if (strcmp (p, "data") == 0)
35026 {
35027 cp_lexer_consume_token (parser->lexer);
35028 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35029 return true;
35030 }
35031 else if (strcmp (p, "enter") == 0)
35032 {
35033 cp_lexer_consume_token (parser->lexer);
35034 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35035 return false;
35036 }
35037 else if (strcmp (p, "exit") == 0)
35038 {
35039 cp_lexer_consume_token (parser->lexer);
35040 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35041 return false;
35042 }
35043 else if (strcmp (p, "update") == 0)
35044 {
35045 cp_lexer_consume_token (parser->lexer);
35046 return cp_parser_omp_target_update (parser, pragma_tok, context);
35047 }
35048 }
35049
35050 stmt = make_node (OMP_TARGET);
35051 TREE_TYPE (stmt) = void_type_node;
35052
35053 OMP_TARGET_CLAUSES (stmt)
35054 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35055 "#pragma omp target", pragma_tok);
35056 pc = &OMP_TARGET_CLAUSES (stmt);
35057 keep_next_level (true);
35058 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35059
35060 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35061 add_stmt (stmt);
35062
35063 check_clauses:
35064 while (*pc)
35065 {
35066 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35067 switch (OMP_CLAUSE_MAP_KIND (*pc))
35068 {
35069 case GOMP_MAP_TO:
35070 case GOMP_MAP_ALWAYS_TO:
35071 case GOMP_MAP_FROM:
35072 case GOMP_MAP_ALWAYS_FROM:
35073 case GOMP_MAP_TOFROM:
35074 case GOMP_MAP_ALWAYS_TOFROM:
35075 case GOMP_MAP_ALLOC:
35076 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35077 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35078 case GOMP_MAP_ALWAYS_POINTER:
35079 break;
35080 default:
35081 error_at (OMP_CLAUSE_LOCATION (*pc),
35082 "%<#pragma omp target%> with map-type other "
35083 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35084 "on %<map%> clause");
35085 *pc = OMP_CLAUSE_CHAIN (*pc);
35086 continue;
35087 }
35088 pc = &OMP_CLAUSE_CHAIN (*pc);
35089 }
35090 return true;
35091 }
35092
35093 /* OpenACC 2.0:
35094 # pragma acc cache (variable-list) new-line
35095 */
35096
35097 static tree
35098 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35099 {
35100 tree stmt, clauses;
35101
35102 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35103 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35104
35105 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35106
35107 stmt = make_node (OACC_CACHE);
35108 TREE_TYPE (stmt) = void_type_node;
35109 OACC_CACHE_CLAUSES (stmt) = clauses;
35110 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35111 add_stmt (stmt);
35112
35113 return stmt;
35114 }
35115
35116 /* OpenACC 2.0:
35117 # pragma acc data oacc-data-clause[optseq] new-line
35118 structured-block */
35119
35120 #define OACC_DATA_CLAUSE_MASK \
35121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35131 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35132
35133 static tree
35134 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35135 {
35136 tree stmt, clauses, block;
35137 unsigned int save;
35138
35139 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35140 "#pragma acc data", pragma_tok);
35141
35142 block = begin_omp_parallel ();
35143 save = cp_parser_begin_omp_structured_block (parser);
35144 cp_parser_statement (parser, NULL_TREE, false, if_p);
35145 cp_parser_end_omp_structured_block (parser, save);
35146 stmt = finish_oacc_data (clauses, block);
35147 return stmt;
35148 }
35149
35150 /* OpenACC 2.0:
35151 # pragma acc host_data <clauses> new-line
35152 structured-block */
35153
35154 #define OACC_HOST_DATA_CLAUSE_MASK \
35155 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35156
35157 static tree
35158 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35159 {
35160 tree stmt, clauses, block;
35161 unsigned int save;
35162
35163 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35164 "#pragma acc host_data", pragma_tok);
35165
35166 block = begin_omp_parallel ();
35167 save = cp_parser_begin_omp_structured_block (parser);
35168 cp_parser_statement (parser, NULL_TREE, false, if_p);
35169 cp_parser_end_omp_structured_block (parser, save);
35170 stmt = finish_oacc_host_data (clauses, block);
35171 return stmt;
35172 }
35173
35174 /* OpenACC 2.0:
35175 # pragma acc declare oacc-data-clause[optseq] new-line
35176 */
35177
35178 #define OACC_DECLARE_CLAUSE_MASK \
35179 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35191
35192 static tree
35193 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35194 {
35195 tree clauses, stmt;
35196 bool error = false;
35197
35198 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35199 "#pragma acc declare", pragma_tok, true);
35200
35201
35202 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35203 {
35204 error_at (pragma_tok->location,
35205 "no valid clauses specified in %<#pragma acc declare%>");
35206 return NULL_TREE;
35207 }
35208
35209 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35210 {
35211 location_t loc = OMP_CLAUSE_LOCATION (t);
35212 tree decl = OMP_CLAUSE_DECL (t);
35213 if (!DECL_P (decl))
35214 {
35215 error_at (loc, "array section in %<#pragma acc declare%>");
35216 error = true;
35217 continue;
35218 }
35219 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35220 switch (OMP_CLAUSE_MAP_KIND (t))
35221 {
35222 case GOMP_MAP_FORCE_ALLOC:
35223 case GOMP_MAP_FORCE_TO:
35224 case GOMP_MAP_FORCE_DEVICEPTR:
35225 case GOMP_MAP_DEVICE_RESIDENT:
35226 break;
35227
35228 case GOMP_MAP_POINTER:
35229 /* Generated by c_finish_omp_clauses from array sections;
35230 avoid spurious diagnostics. */
35231 break;
35232
35233 case GOMP_MAP_LINK:
35234 if (!global_bindings_p ()
35235 && (TREE_STATIC (decl)
35236 || !DECL_EXTERNAL (decl)))
35237 {
35238 error_at (loc,
35239 "%qD must be a global variable in"
35240 "%<#pragma acc declare link%>",
35241 decl);
35242 error = true;
35243 continue;
35244 }
35245 break;
35246
35247 default:
35248 if (global_bindings_p ())
35249 {
35250 error_at (loc, "invalid OpenACC clause at file scope");
35251 error = true;
35252 continue;
35253 }
35254 if (DECL_EXTERNAL (decl))
35255 {
35256 error_at (loc,
35257 "invalid use of %<extern%> variable %qD "
35258 "in %<#pragma acc declare%>", decl);
35259 error = true;
35260 continue;
35261 }
35262 else if (TREE_PUBLIC (decl))
35263 {
35264 error_at (loc,
35265 "invalid use of %<global%> variable %qD "
35266 "in %<#pragma acc declare%>", decl);
35267 error = true;
35268 continue;
35269 }
35270 break;
35271 }
35272
35273 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35274 || lookup_attribute ("omp declare target link",
35275 DECL_ATTRIBUTES (decl)))
35276 {
35277 error_at (loc, "variable %qD used more than once with "
35278 "%<#pragma acc declare%>", decl);
35279 error = true;
35280 continue;
35281 }
35282
35283 if (!error)
35284 {
35285 tree id;
35286
35287 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35288 id = get_identifier ("omp declare target link");
35289 else
35290 id = get_identifier ("omp declare target");
35291
35292 DECL_ATTRIBUTES (decl)
35293 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35294 if (global_bindings_p ())
35295 {
35296 symtab_node *node = symtab_node::get (decl);
35297 if (node != NULL)
35298 {
35299 node->offloadable = 1;
35300 if (ENABLE_OFFLOADING)
35301 {
35302 g->have_offload = true;
35303 if (is_a <varpool_node *> (node))
35304 vec_safe_push (offload_vars, decl);
35305 }
35306 }
35307 }
35308 }
35309 }
35310
35311 if (error || global_bindings_p ())
35312 return NULL_TREE;
35313
35314 stmt = make_node (OACC_DECLARE);
35315 TREE_TYPE (stmt) = void_type_node;
35316 OACC_DECLARE_CLAUSES (stmt) = clauses;
35317 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35318
35319 add_stmt (stmt);
35320
35321 return NULL_TREE;
35322 }
35323
35324 /* OpenACC 2.0:
35325 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35326
35327 or
35328
35329 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35330
35331 LOC is the location of the #pragma token.
35332 */
35333
35334 #define OACC_ENTER_DATA_CLAUSE_MASK \
35335 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35342
35343 #define OACC_EXIT_DATA_CLAUSE_MASK \
35344 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35349
35350 static tree
35351 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35352 bool enter)
35353 {
35354 tree stmt, clauses;
35355
35356 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35357 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35358 {
35359 cp_parser_error (parser, enter
35360 ? "expected %<data%> in %<#pragma acc enter data%>"
35361 : "expected %<data%> in %<#pragma acc exit data%>");
35362 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35363 return NULL_TREE;
35364 }
35365
35366 const char *p =
35367 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35368 if (strcmp (p, "data") != 0)
35369 {
35370 cp_parser_error (parser, "invalid pragma");
35371 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35372 return NULL_TREE;
35373 }
35374
35375 cp_lexer_consume_token (parser->lexer);
35376
35377 if (enter)
35378 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35379 "#pragma acc enter data", pragma_tok);
35380 else
35381 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35382 "#pragma acc exit data", pragma_tok);
35383
35384 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35385 {
35386 error_at (pragma_tok->location,
35387 "%<#pragma acc enter data%> has no data movement clause");
35388 return NULL_TREE;
35389 }
35390
35391 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35392 TREE_TYPE (stmt) = void_type_node;
35393 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35394 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35395 add_stmt (stmt);
35396 return stmt;
35397 }
35398
35399 /* OpenACC 2.0:
35400 # pragma acc loop oacc-loop-clause[optseq] new-line
35401 structured-block */
35402
35403 #define OACC_LOOP_CLAUSE_MASK \
35404 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35414
35415 static tree
35416 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35417 omp_clause_mask mask, tree *cclauses, bool *if_p)
35418 {
35419 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35420
35421 strcat (p_name, " loop");
35422 mask |= OACC_LOOP_CLAUSE_MASK;
35423
35424 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35425 cclauses == NULL);
35426 if (cclauses)
35427 {
35428 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35429 if (*cclauses)
35430 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
35431 if (clauses)
35432 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35433 }
35434
35435 tree block = begin_omp_structured_block ();
35436 int save = cp_parser_begin_omp_structured_block (parser);
35437 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35438 cp_parser_end_omp_structured_block (parser, save);
35439 add_stmt (finish_omp_structured_block (block));
35440
35441 return stmt;
35442 }
35443
35444 /* OpenACC 2.0:
35445 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35446 structured-block
35447
35448 or
35449
35450 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35451 structured-block
35452 */
35453
35454 #define OACC_KERNELS_CLAUSE_MASK \
35455 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35469
35470 #define OACC_PARALLEL_CLAUSE_MASK \
35471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35483 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35491
35492 static tree
35493 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35494 char *p_name, bool *if_p)
35495 {
35496 omp_clause_mask mask;
35497 enum tree_code code;
35498 switch (cp_parser_pragma_kind (pragma_tok))
35499 {
35500 case PRAGMA_OACC_KERNELS:
35501 strcat (p_name, " kernels");
35502 mask = OACC_KERNELS_CLAUSE_MASK;
35503 code = OACC_KERNELS;
35504 break;
35505 case PRAGMA_OACC_PARALLEL:
35506 strcat (p_name, " parallel");
35507 mask = OACC_PARALLEL_CLAUSE_MASK;
35508 code = OACC_PARALLEL;
35509 break;
35510 default:
35511 gcc_unreachable ();
35512 }
35513
35514 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35515 {
35516 const char *p
35517 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35518 if (strcmp (p, "loop") == 0)
35519 {
35520 cp_lexer_consume_token (parser->lexer);
35521 tree block = begin_omp_parallel ();
35522 tree clauses;
35523 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35524 if_p);
35525 return finish_omp_construct (code, block, clauses);
35526 }
35527 }
35528
35529 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35530
35531 tree block = begin_omp_parallel ();
35532 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35533 cp_parser_statement (parser, NULL_TREE, false, if_p);
35534 cp_parser_end_omp_structured_block (parser, save);
35535 return finish_omp_construct (code, block, clauses);
35536 }
35537
35538 /* OpenACC 2.0:
35539 # pragma acc update oacc-update-clause[optseq] new-line
35540 */
35541
35542 #define OACC_UPDATE_CLAUSE_MASK \
35543 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35549
35550 static tree
35551 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35552 {
35553 tree stmt, clauses;
35554
35555 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35556 "#pragma acc update", pragma_tok);
35557
35558 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35559 {
35560 error_at (pragma_tok->location,
35561 "%<#pragma acc update%> must contain at least one "
35562 "%<device%> or %<host%> or %<self%> clause");
35563 return NULL_TREE;
35564 }
35565
35566 stmt = make_node (OACC_UPDATE);
35567 TREE_TYPE (stmt) = void_type_node;
35568 OACC_UPDATE_CLAUSES (stmt) = clauses;
35569 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35570 add_stmt (stmt);
35571 return stmt;
35572 }
35573
35574 /* OpenACC 2.0:
35575 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35576
35577 LOC is the location of the #pragma token.
35578 */
35579
35580 #define OACC_WAIT_CLAUSE_MASK \
35581 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35582
35583 static tree
35584 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35585 {
35586 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35587 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35588
35589 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35590 list = cp_parser_oacc_wait_list (parser, loc, list);
35591
35592 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35593 "#pragma acc wait", pragma_tok);
35594
35595 stmt = c_finish_oacc_wait (loc, list, clauses);
35596 stmt = finish_expr_stmt (stmt);
35597
35598 return stmt;
35599 }
35600
35601 /* OpenMP 4.0:
35602 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35603
35604 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35605 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35606 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35607 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35608 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35611
35612 static void
35613 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35614 enum pragma_context context)
35615 {
35616 bool first_p = parser->omp_declare_simd == NULL;
35617 cp_omp_declare_simd_data data;
35618 if (first_p)
35619 {
35620 data.error_seen = false;
35621 data.fndecl_seen = false;
35622 data.tokens = vNULL;
35623 parser->omp_declare_simd = &data;
35624 }
35625 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35626 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35627 cp_lexer_consume_token (parser->lexer);
35628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35629 parser->omp_declare_simd->error_seen = true;
35630 cp_parser_require_pragma_eol (parser, pragma_tok);
35631 struct cp_token_cache *cp
35632 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35633 parser->omp_declare_simd->tokens.safe_push (cp);
35634 if (first_p)
35635 {
35636 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35637 cp_parser_pragma (parser, context, NULL);
35638 switch (context)
35639 {
35640 case pragma_external:
35641 cp_parser_declaration (parser);
35642 break;
35643 case pragma_member:
35644 cp_parser_member_declaration (parser);
35645 break;
35646 case pragma_objc_icode:
35647 cp_parser_block_declaration (parser, /*statement_p=*/false);
35648 break;
35649 default:
35650 cp_parser_declaration_statement (parser);
35651 break;
35652 }
35653 if (parser->omp_declare_simd
35654 && !parser->omp_declare_simd->error_seen
35655 && !parser->omp_declare_simd->fndecl_seen)
35656 error_at (pragma_tok->location,
35657 "%<#pragma omp declare simd%> not immediately followed by "
35658 "function declaration or definition");
35659 data.tokens.release ();
35660 parser->omp_declare_simd = NULL;
35661 }
35662 }
35663
35664 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35665 This function is modelled similar to the late parsing of omp declare
35666 simd. */
35667
35668 static tree
35669 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35670 {
35671 struct cp_token_cache *ce;
35672 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35673 int ii = 0;
35674
35675 if (parser->omp_declare_simd != NULL
35676 || lookup_attribute ("simd", attrs))
35677 {
35678 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35679 "used in the same function marked as a Cilk Plus SIMD-enabled "
35680 " function");
35681 parser->cilk_simd_fn_info->tokens.release ();
35682 XDELETE (parser->cilk_simd_fn_info);
35683 parser->cilk_simd_fn_info = NULL;
35684 return attrs;
35685 }
35686 if (!info->error_seen && info->fndecl_seen)
35687 {
35688 error ("vector attribute not immediately followed by a single function"
35689 " declaration or definition");
35690 info->error_seen = true;
35691 }
35692 if (info->error_seen)
35693 return attrs;
35694
35695 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35696 {
35697 tree c, cl;
35698
35699 cp_parser_push_lexer_for_tokens (parser, ce);
35700 parser->lexer->in_pragma = true;
35701 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35702 "SIMD-enabled functions attribute",
35703 NULL);
35704 cp_parser_pop_lexer (parser);
35705 if (cl)
35706 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35707
35708 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35709 TREE_CHAIN (c) = attrs;
35710 attrs = c;
35711
35712 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35713 TREE_CHAIN (c) = attrs;
35714 if (processing_template_decl)
35715 ATTR_IS_DEPENDENT (c) = 1;
35716 attrs = c;
35717 }
35718 info->fndecl_seen = true;
35719 parser->cilk_simd_fn_info->tokens.release ();
35720 XDELETE (parser->cilk_simd_fn_info);
35721 parser->cilk_simd_fn_info = NULL;
35722 return attrs;
35723 }
35724
35725 /* Finalize #pragma omp declare simd clauses after direct declarator has
35726 been parsed, and put that into "omp declare simd" attribute. */
35727
35728 static tree
35729 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35730 {
35731 struct cp_token_cache *ce;
35732 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35733 int i;
35734
35735 if (!data->error_seen && data->fndecl_seen)
35736 {
35737 error ("%<#pragma omp declare simd%> not immediately followed by "
35738 "a single function declaration or definition");
35739 data->error_seen = true;
35740 return attrs;
35741 }
35742 if (data->error_seen)
35743 return attrs;
35744
35745 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35746 {
35747 tree c, cl;
35748
35749 cp_parser_push_lexer_for_tokens (parser, ce);
35750 parser->lexer->in_pragma = true;
35751 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35752 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35753 cp_lexer_consume_token (parser->lexer);
35754 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35755 "#pragma omp declare simd", pragma_tok);
35756 cp_parser_pop_lexer (parser);
35757 if (cl)
35758 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35759 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35760 TREE_CHAIN (c) = attrs;
35761 if (processing_template_decl)
35762 ATTR_IS_DEPENDENT (c) = 1;
35763 attrs = c;
35764 }
35765
35766 data->fndecl_seen = true;
35767 return attrs;
35768 }
35769
35770
35771 /* OpenMP 4.0:
35772 # pragma omp declare target new-line
35773 declarations and definitions
35774 # pragma omp end declare target new-line
35775
35776 OpenMP 4.5:
35777 # pragma omp declare target ( extended-list ) new-line
35778
35779 # pragma omp declare target declare-target-clauses[seq] new-line */
35780
35781 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35784
35785 static void
35786 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35787 {
35788 tree clauses = NULL_TREE;
35789 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35790 clauses
35791 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35792 "#pragma omp declare target", pragma_tok);
35793 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35794 {
35795 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35796 clauses);
35797 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35798 cp_parser_require_pragma_eol (parser, pragma_tok);
35799 }
35800 else
35801 {
35802 cp_parser_require_pragma_eol (parser, pragma_tok);
35803 scope_chain->omp_declare_target_attribute++;
35804 return;
35805 }
35806 if (scope_chain->omp_declare_target_attribute)
35807 error_at (pragma_tok->location,
35808 "%<#pragma omp declare target%> with clauses in between "
35809 "%<#pragma omp declare target%> without clauses and "
35810 "%<#pragma omp end declare target%>");
35811 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35812 {
35813 tree t = OMP_CLAUSE_DECL (c), id;
35814 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35815 tree at2 = lookup_attribute ("omp declare target link",
35816 DECL_ATTRIBUTES (t));
35817 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35818 {
35819 id = get_identifier ("omp declare target link");
35820 std::swap (at1, at2);
35821 }
35822 else
35823 id = get_identifier ("omp declare target");
35824 if (at2)
35825 {
35826 error_at (OMP_CLAUSE_LOCATION (c),
35827 "%qD specified both in declare target %<link%> and %<to%>"
35828 " clauses", t);
35829 continue;
35830 }
35831 if (!at1)
35832 {
35833 symtab_node *node = symtab_node::get (t);
35834 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35835 if (node != NULL)
35836 {
35837 node->offloadable = 1;
35838 if (ENABLE_OFFLOADING)
35839 {
35840 g->have_offload = true;
35841 if (is_a <varpool_node *> (node))
35842 vec_safe_push (offload_vars, t);
35843 }
35844 }
35845 }
35846 }
35847 }
35848
35849 static void
35850 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35851 {
35852 const char *p = "";
35853 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35854 {
35855 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35856 p = IDENTIFIER_POINTER (id);
35857 }
35858 if (strcmp (p, "declare") == 0)
35859 {
35860 cp_lexer_consume_token (parser->lexer);
35861 p = "";
35862 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35863 {
35864 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35865 p = IDENTIFIER_POINTER (id);
35866 }
35867 if (strcmp (p, "target") == 0)
35868 cp_lexer_consume_token (parser->lexer);
35869 else
35870 {
35871 cp_parser_error (parser, "expected %<target%>");
35872 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35873 return;
35874 }
35875 }
35876 else
35877 {
35878 cp_parser_error (parser, "expected %<declare%>");
35879 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35880 return;
35881 }
35882 cp_parser_require_pragma_eol (parser, pragma_tok);
35883 if (!scope_chain->omp_declare_target_attribute)
35884 error_at (pragma_tok->location,
35885 "%<#pragma omp end declare target%> without corresponding "
35886 "%<#pragma omp declare target%>");
35887 else
35888 scope_chain->omp_declare_target_attribute--;
35889 }
35890
35891 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35892 expression and optional initializer clause of
35893 #pragma omp declare reduction. We store the expression(s) as
35894 either 3, 6 or 7 special statements inside of the artificial function's
35895 body. The first two statements are DECL_EXPRs for the artificial
35896 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35897 expression that uses those variables.
35898 If there was any INITIALIZER clause, this is followed by further statements,
35899 the fourth and fifth statements are DECL_EXPRs for the artificial
35900 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35901 constructor variant (first token after open paren is not omp_priv),
35902 then the sixth statement is a statement with the function call expression
35903 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35904 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35905 to initialize the OMP_PRIV artificial variable and there is seventh
35906 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35907
35908 static bool
35909 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35910 {
35911 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35912 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35913 type = TREE_TYPE (type);
35914 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35915 DECL_ARTIFICIAL (omp_out) = 1;
35916 pushdecl (omp_out);
35917 add_decl_expr (omp_out);
35918 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35919 DECL_ARTIFICIAL (omp_in) = 1;
35920 pushdecl (omp_in);
35921 add_decl_expr (omp_in);
35922 tree combiner;
35923 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35924
35925 keep_next_level (true);
35926 tree block = begin_omp_structured_block ();
35927 combiner = cp_parser_expression (parser);
35928 finish_expr_stmt (combiner);
35929 block = finish_omp_structured_block (block);
35930 add_stmt (block);
35931
35932 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35933 return false;
35934
35935 const char *p = "";
35936 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35937 {
35938 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35939 p = IDENTIFIER_POINTER (id);
35940 }
35941
35942 if (strcmp (p, "initializer") == 0)
35943 {
35944 cp_lexer_consume_token (parser->lexer);
35945 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35946 return false;
35947
35948 p = "";
35949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35950 {
35951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35952 p = IDENTIFIER_POINTER (id);
35953 }
35954
35955 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35956 DECL_ARTIFICIAL (omp_priv) = 1;
35957 pushdecl (omp_priv);
35958 add_decl_expr (omp_priv);
35959 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35960 DECL_ARTIFICIAL (omp_orig) = 1;
35961 pushdecl (omp_orig);
35962 add_decl_expr (omp_orig);
35963
35964 keep_next_level (true);
35965 block = begin_omp_structured_block ();
35966
35967 bool ctor = false;
35968 if (strcmp (p, "omp_priv") == 0)
35969 {
35970 bool is_direct_init, is_non_constant_init;
35971 ctor = true;
35972 cp_lexer_consume_token (parser->lexer);
35973 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35974 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
35975 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35976 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
35977 == CPP_CLOSE_PAREN
35978 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
35979 == CPP_CLOSE_PAREN))
35980 {
35981 finish_omp_structured_block (block);
35982 error ("invalid initializer clause");
35983 return false;
35984 }
35985 initializer = cp_parser_initializer (parser, &is_direct_init,
35986 &is_non_constant_init);
35987 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
35988 NULL_TREE, LOOKUP_ONLYCONVERTING);
35989 }
35990 else
35991 {
35992 cp_parser_parse_tentatively (parser);
35993 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
35994 /*check_dependency_p=*/true,
35995 /*template_p=*/NULL,
35996 /*declarator_p=*/false,
35997 /*optional_p=*/false);
35998 vec<tree, va_gc> *args;
35999 if (fn_name == error_mark_node
36000 || cp_parser_error_occurred (parser)
36001 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36002 || ((args = cp_parser_parenthesized_expression_list
36003 (parser, non_attr, /*cast_p=*/false,
36004 /*allow_expansion_p=*/true,
36005 /*non_constant_p=*/NULL)),
36006 cp_parser_error_occurred (parser)))
36007 {
36008 finish_omp_structured_block (block);
36009 cp_parser_abort_tentative_parse (parser);
36010 cp_parser_error (parser, "expected id-expression (arguments)");
36011 return false;
36012 }
36013 unsigned int i;
36014 tree arg;
36015 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36016 if (arg == omp_priv
36017 || (TREE_CODE (arg) == ADDR_EXPR
36018 && TREE_OPERAND (arg, 0) == omp_priv))
36019 break;
36020 cp_parser_abort_tentative_parse (parser);
36021 if (arg == NULL_TREE)
36022 error ("one of the initializer call arguments should be %<omp_priv%>"
36023 " or %<&omp_priv%>");
36024 initializer = cp_parser_postfix_expression (parser, false, false, false,
36025 false, NULL);
36026 finish_expr_stmt (initializer);
36027 }
36028
36029 block = finish_omp_structured_block (block);
36030 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36031 add_stmt (block);
36032
36033 if (ctor)
36034 add_decl_expr (omp_orig);
36035
36036 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36037 return false;
36038 }
36039
36040 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36041 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36042
36043 return true;
36044 }
36045
36046 /* OpenMP 4.0
36047 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36048 initializer-clause[opt] new-line
36049
36050 initializer-clause:
36051 initializer (omp_priv initializer)
36052 initializer (function-name (argument-list)) */
36053
36054 static void
36055 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36056 enum pragma_context)
36057 {
36058 auto_vec<tree> types;
36059 enum tree_code reduc_code = ERROR_MARK;
36060 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36061 unsigned int i;
36062 cp_token *first_token;
36063 cp_token_cache *cp;
36064 int errs;
36065 void *p;
36066
36067 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36068 p = obstack_alloc (&declarator_obstack, 0);
36069
36070 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36071 goto fail;
36072
36073 switch (cp_lexer_peek_token (parser->lexer)->type)
36074 {
36075 case CPP_PLUS:
36076 reduc_code = PLUS_EXPR;
36077 break;
36078 case CPP_MULT:
36079 reduc_code = MULT_EXPR;
36080 break;
36081 case CPP_MINUS:
36082 reduc_code = MINUS_EXPR;
36083 break;
36084 case CPP_AND:
36085 reduc_code = BIT_AND_EXPR;
36086 break;
36087 case CPP_XOR:
36088 reduc_code = BIT_XOR_EXPR;
36089 break;
36090 case CPP_OR:
36091 reduc_code = BIT_IOR_EXPR;
36092 break;
36093 case CPP_AND_AND:
36094 reduc_code = TRUTH_ANDIF_EXPR;
36095 break;
36096 case CPP_OR_OR:
36097 reduc_code = TRUTH_ORIF_EXPR;
36098 break;
36099 case CPP_NAME:
36100 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36101 break;
36102 default:
36103 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36104 "%<|%>, %<&&%>, %<||%> or identifier");
36105 goto fail;
36106 }
36107
36108 if (reduc_code != ERROR_MARK)
36109 cp_lexer_consume_token (parser->lexer);
36110
36111 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36112 if (reduc_id == error_mark_node)
36113 goto fail;
36114
36115 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36116 goto fail;
36117
36118 /* Types may not be defined in declare reduction type list. */
36119 const char *saved_message;
36120 saved_message = parser->type_definition_forbidden_message;
36121 parser->type_definition_forbidden_message
36122 = G_("types may not be defined in declare reduction type list");
36123 bool saved_colon_corrects_to_scope_p;
36124 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36125 parser->colon_corrects_to_scope_p = false;
36126 bool saved_colon_doesnt_start_class_def_p;
36127 saved_colon_doesnt_start_class_def_p
36128 = parser->colon_doesnt_start_class_def_p;
36129 parser->colon_doesnt_start_class_def_p = true;
36130
36131 while (true)
36132 {
36133 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36134 type = cp_parser_type_id (parser);
36135 if (type == error_mark_node)
36136 ;
36137 else if (ARITHMETIC_TYPE_P (type)
36138 && (orig_reduc_id == NULL_TREE
36139 || (TREE_CODE (type) != COMPLEX_TYPE
36140 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36141 "min") == 0
36142 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36143 "max") == 0))))
36144 error_at (loc, "predeclared arithmetic type %qT in "
36145 "%<#pragma omp declare reduction%>", type);
36146 else if (TREE_CODE (type) == FUNCTION_TYPE
36147 || TREE_CODE (type) == METHOD_TYPE
36148 || TREE_CODE (type) == ARRAY_TYPE)
36149 error_at (loc, "function or array type %qT in "
36150 "%<#pragma omp declare reduction%>", type);
36151 else if (TREE_CODE (type) == REFERENCE_TYPE)
36152 error_at (loc, "reference type %qT in "
36153 "%<#pragma omp declare reduction%>", type);
36154 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36155 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36156 "%<#pragma omp declare reduction%>", type);
36157 else
36158 types.safe_push (type);
36159
36160 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36161 cp_lexer_consume_token (parser->lexer);
36162 else
36163 break;
36164 }
36165
36166 /* Restore the saved message. */
36167 parser->type_definition_forbidden_message = saved_message;
36168 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36169 parser->colon_doesnt_start_class_def_p
36170 = saved_colon_doesnt_start_class_def_p;
36171
36172 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36173 || types.is_empty ())
36174 {
36175 fail:
36176 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36177 goto done;
36178 }
36179
36180 first_token = cp_lexer_peek_token (parser->lexer);
36181 cp = NULL;
36182 errs = errorcount;
36183 FOR_EACH_VEC_ELT (types, i, type)
36184 {
36185 tree fntype
36186 = build_function_type_list (void_type_node,
36187 cp_build_reference_type (type, false),
36188 NULL_TREE);
36189 tree this_reduc_id = reduc_id;
36190 if (!dependent_type_p (type))
36191 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36192 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36193 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36194 DECL_ARTIFICIAL (fndecl) = 1;
36195 DECL_EXTERNAL (fndecl) = 1;
36196 DECL_DECLARED_INLINE_P (fndecl) = 1;
36197 DECL_IGNORED_P (fndecl) = 1;
36198 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36199 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36200 DECL_ATTRIBUTES (fndecl)
36201 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36202 DECL_ATTRIBUTES (fndecl));
36203 if (processing_template_decl)
36204 fndecl = push_template_decl (fndecl);
36205 bool block_scope = false;
36206 tree block = NULL_TREE;
36207 if (current_function_decl)
36208 {
36209 block_scope = true;
36210 DECL_CONTEXT (fndecl) = global_namespace;
36211 if (!processing_template_decl)
36212 pushdecl (fndecl);
36213 }
36214 else if (current_class_type)
36215 {
36216 if (cp == NULL)
36217 {
36218 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36219 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36220 cp_lexer_consume_token (parser->lexer);
36221 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36222 goto fail;
36223 cp = cp_token_cache_new (first_token,
36224 cp_lexer_peek_nth_token (parser->lexer,
36225 2));
36226 }
36227 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36228 finish_member_declaration (fndecl);
36229 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36230 DECL_PENDING_INLINE_P (fndecl) = 1;
36231 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36232 continue;
36233 }
36234 else
36235 {
36236 DECL_CONTEXT (fndecl) = current_namespace;
36237 pushdecl (fndecl);
36238 }
36239 if (!block_scope)
36240 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36241 else
36242 block = begin_omp_structured_block ();
36243 if (cp)
36244 {
36245 cp_parser_push_lexer_for_tokens (parser, cp);
36246 parser->lexer->in_pragma = true;
36247 }
36248 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36249 {
36250 if (!block_scope)
36251 finish_function (0);
36252 else
36253 DECL_CONTEXT (fndecl) = current_function_decl;
36254 if (cp)
36255 cp_parser_pop_lexer (parser);
36256 goto fail;
36257 }
36258 if (cp)
36259 cp_parser_pop_lexer (parser);
36260 if (!block_scope)
36261 finish_function (0);
36262 else
36263 {
36264 DECL_CONTEXT (fndecl) = current_function_decl;
36265 block = finish_omp_structured_block (block);
36266 if (TREE_CODE (block) == BIND_EXPR)
36267 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36268 else if (TREE_CODE (block) == STATEMENT_LIST)
36269 DECL_SAVED_TREE (fndecl) = block;
36270 if (processing_template_decl)
36271 add_decl_expr (fndecl);
36272 }
36273 cp_check_omp_declare_reduction (fndecl);
36274 if (cp == NULL && types.length () > 1)
36275 cp = cp_token_cache_new (first_token,
36276 cp_lexer_peek_nth_token (parser->lexer, 2));
36277 if (errs != errorcount)
36278 break;
36279 }
36280
36281 cp_parser_require_pragma_eol (parser, pragma_tok);
36282
36283 done:
36284 /* Free any declarators allocated. */
36285 obstack_free (&declarator_obstack, p);
36286 }
36287
36288 /* OpenMP 4.0
36289 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36290 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36291 initializer-clause[opt] new-line
36292 #pragma omp declare target new-line */
36293
36294 static void
36295 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36296 enum pragma_context context)
36297 {
36298 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36299 {
36300 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36301 const char *p = IDENTIFIER_POINTER (id);
36302
36303 if (strcmp (p, "simd") == 0)
36304 {
36305 cp_lexer_consume_token (parser->lexer);
36306 cp_parser_omp_declare_simd (parser, pragma_tok,
36307 context);
36308 return;
36309 }
36310 cp_ensure_no_omp_declare_simd (parser);
36311 if (strcmp (p, "reduction") == 0)
36312 {
36313 cp_lexer_consume_token (parser->lexer);
36314 cp_parser_omp_declare_reduction (parser, pragma_tok,
36315 context);
36316 return;
36317 }
36318 if (!flag_openmp) /* flag_openmp_simd */
36319 {
36320 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36321 return;
36322 }
36323 if (strcmp (p, "target") == 0)
36324 {
36325 cp_lexer_consume_token (parser->lexer);
36326 cp_parser_omp_declare_target (parser, pragma_tok);
36327 return;
36328 }
36329 }
36330 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36331 "or %<target%>");
36332 cp_parser_require_pragma_eol (parser, pragma_tok);
36333 }
36334
36335 /* OpenMP 4.5:
36336 #pragma omp taskloop taskloop-clause[optseq] new-line
36337 for-loop
36338
36339 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36340 for-loop */
36341
36342 #define OMP_TASKLOOP_CLAUSE_MASK \
36343 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36357
36358 static tree
36359 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36360 char *p_name, omp_clause_mask mask, tree *cclauses,
36361 bool *if_p)
36362 {
36363 tree clauses, sb, ret;
36364 unsigned int save;
36365 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36366
36367 strcat (p_name, " taskloop");
36368 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36369
36370 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36371 {
36372 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36373 const char *p = IDENTIFIER_POINTER (id);
36374
36375 if (strcmp (p, "simd") == 0)
36376 {
36377 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36378 if (cclauses == NULL)
36379 cclauses = cclauses_buf;
36380
36381 cp_lexer_consume_token (parser->lexer);
36382 if (!flag_openmp) /* flag_openmp_simd */
36383 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36384 cclauses, if_p);
36385 sb = begin_omp_structured_block ();
36386 save = cp_parser_begin_omp_structured_block (parser);
36387 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36388 cclauses, if_p);
36389 cp_parser_end_omp_structured_block (parser, save);
36390 tree body = finish_omp_structured_block (sb);
36391 if (ret == NULL)
36392 return ret;
36393 ret = make_node (OMP_TASKLOOP);
36394 TREE_TYPE (ret) = void_type_node;
36395 OMP_FOR_BODY (ret) = body;
36396 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36397 SET_EXPR_LOCATION (ret, loc);
36398 add_stmt (ret);
36399 return ret;
36400 }
36401 }
36402 if (!flag_openmp) /* flag_openmp_simd */
36403 {
36404 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36405 return NULL_TREE;
36406 }
36407
36408 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36409 cclauses == NULL);
36410 if (cclauses)
36411 {
36412 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36413 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36414 }
36415
36416 sb = begin_omp_structured_block ();
36417 save = cp_parser_begin_omp_structured_block (parser);
36418
36419 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36420 if_p);
36421
36422 cp_parser_end_omp_structured_block (parser, save);
36423 add_stmt (finish_omp_structured_block (sb));
36424
36425 return ret;
36426 }
36427
36428
36429 /* OpenACC 2.0:
36430 # pragma acc routine oacc-routine-clause[optseq] new-line
36431 function-definition
36432
36433 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36434 */
36435
36436 #define OACC_ROUTINE_CLAUSE_MASK \
36437 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36441
36442
36443 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36444 component, which must resolve to a declared namespace-scope
36445 function. The clauses are either processed directly (for a named
36446 function), or defered until the immediatley following declaration
36447 is parsed. */
36448
36449 static void
36450 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36451 enum pragma_context context)
36452 {
36453 bool first_p = parser->oacc_routine == NULL;
36454 location_t loc = pragma_tok->location;
36455 cp_omp_declare_simd_data data;
36456 if (first_p)
36457 {
36458 data.error_seen = false;
36459 data.fndecl_seen = false;
36460 data.tokens = vNULL;
36461 data.clauses = NULL_TREE;
36462 parser->oacc_routine = &data;
36463 }
36464
36465 tree decl = NULL_TREE;
36466 /* Create a dummy claue, to record location. */
36467 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36468
36469 if (context != pragma_external)
36470 {
36471 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36472 parser->oacc_routine->error_seen = true;
36473 parser->oacc_routine = NULL;
36474 return;
36475 }
36476
36477 /* Look for optional '( name )'. */
36478 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36479 {
36480 if (!first_p)
36481 {
36482 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36483 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36484 cp_lexer_consume_token (parser->lexer);
36485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36486 parser->oacc_routine->error_seen = true;
36487 cp_parser_require_pragma_eol (parser, pragma_tok);
36488
36489 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36490 "%<#pragma acc routine%> not followed by a "
36491 "function declaration or definition");
36492
36493 parser->oacc_routine->error_seen = true;
36494 return;
36495 }
36496
36497 cp_lexer_consume_token (parser->lexer);
36498 cp_token *token = cp_lexer_peek_token (parser->lexer);
36499
36500 /* We parse the name as an id-expression. If it resolves to
36501 anything other than a non-overloaded function at namespace
36502 scope, it's an error. */
36503 tree id = cp_parser_id_expression (parser,
36504 /*template_keyword_p=*/false,
36505 /*check_dependency_p=*/false,
36506 /*template_p=*/NULL,
36507 /*declarator_p=*/false,
36508 /*optional_p=*/false);
36509 decl = cp_parser_lookup_name_simple (parser, id, token->location);
36510 if (id != error_mark_node && decl == error_mark_node)
36511 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36512 token->location);
36513
36514 if (decl == error_mark_node
36515 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36516 {
36517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36518 parser->oacc_routine = NULL;
36519 return;
36520 }
36521
36522 /* Build a chain of clauses. */
36523 parser->lexer->in_pragma = true;
36524 tree clauses = NULL_TREE;
36525 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36526 "#pragma acc routine",
36527 cp_lexer_peek_token
36528 (parser->lexer));
36529
36530 /* Force clauses to be non-null, by attaching context to it. */
36531 clauses = tree_cons (c_head, clauses, NULL_TREE);
36532
36533 if (decl && is_overloaded_fn (decl)
36534 && (TREE_CODE (decl) != FUNCTION_DECL
36535 || DECL_FUNCTION_TEMPLATE_P (decl)))
36536 {
36537 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36538 parser->oacc_routine = NULL;
36539 return;
36540 }
36541
36542 /* Perhaps we should use the same rule as declarations in different
36543 namespaces? */
36544 if (!DECL_NAMESPACE_SCOPE_P (decl))
36545 {
36546 error_at (loc, "%<#pragma acc routine%> does not refer to a "
36547 "namespace scope function");
36548 parser->oacc_routine = NULL;
36549 return;
36550 }
36551
36552 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36553 {
36554 error_at (loc,
36555 "%<#pragma acc routine%> does not refer to a function");
36556 parser->oacc_routine = NULL;
36557 return;
36558 }
36559
36560 data.clauses = clauses;
36561
36562 cp_finalize_oacc_routine (parser, decl, false);
36563 data.tokens.release ();
36564 parser->oacc_routine = NULL;
36565 }
36566 else
36567 {
36568 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36569 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36570 cp_lexer_consume_token (parser->lexer);
36571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36572 parser->oacc_routine->error_seen = true;
36573 cp_parser_require_pragma_eol (parser, pragma_tok);
36574
36575 struct cp_token_cache *cp
36576 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36577 parser->oacc_routine->tokens.safe_push (cp);
36578
36579 if (first_p)
36580 parser->oacc_routine->clauses = c_head;
36581
36582 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36583 cp_parser_pragma (parser, context, NULL);
36584
36585 if (first_p)
36586 {
36587 /* Create an empty list of clauses. */
36588 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36589 NULL_TREE);
36590 cp_parser_declaration (parser);
36591
36592 if (parser->oacc_routine
36593 && !parser->oacc_routine->error_seen
36594 && !parser->oacc_routine->fndecl_seen)
36595 error_at (loc, "%<#pragma acc routine%> not followed by a "
36596 "function declaration or definition");
36597
36598 data.tokens.release ();
36599 parser->oacc_routine = NULL;
36600 }
36601 }
36602 }
36603
36604 /* Finalize #pragma acc routine clauses after direct declarator has
36605 been parsed, and put that into "oacc function" attribute. */
36606
36607 static tree
36608 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36609 {
36610 struct cp_token_cache *ce;
36611 cp_omp_declare_simd_data *data = parser->oacc_routine;
36612 tree cl, clauses = parser->oacc_routine->clauses;
36613 location_t loc;
36614
36615 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36616
36617 if ((!data->error_seen && data->fndecl_seen)
36618 || data->tokens.length () != 1)
36619 {
36620 error_at (loc, "%<#pragma acc routine%> not followed by a "
36621 "function declaration or definition");
36622 data->error_seen = true;
36623 return attrs;
36624 }
36625 if (data->error_seen)
36626 return attrs;
36627
36628 ce = data->tokens[0];
36629
36630 cp_parser_push_lexer_for_tokens (parser, ce);
36631 parser->lexer->in_pragma = true;
36632 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36633
36634 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36635 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36636 "#pragma acc routine", pragma_tok);
36637 cp_parser_pop_lexer (parser);
36638
36639 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36640
36641 /* Force clauses to be non-null, by attaching context to it. */
36642 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36643
36644 data->fndecl_seen = true;
36645 return attrs;
36646 }
36647
36648 /* Apply any saved OpenACC routine clauses to a just-parsed
36649 declaration. */
36650
36651 static void
36652 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36653 {
36654 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36655 {
36656 tree clauses = parser->oacc_routine->clauses;
36657 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36658
36659 if (parser->oacc_routine->error_seen)
36660 return;
36661
36662 if (fndecl == error_mark_node)
36663 {
36664 parser->oacc_routine = NULL;
36665 return;
36666 }
36667
36668 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36669 {
36670 cp_ensure_no_oacc_routine (parser);
36671 return;
36672 }
36673
36674 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36675 {
36676 error_at (loc,
36677 "%<#pragma acc routine%> not followed by a function "
36678 "declaration or definition");
36679 parser->oacc_routine = NULL;
36680 }
36681
36682 if (get_oacc_fn_attrib (fndecl))
36683 {
36684 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36685 fndecl);
36686 parser->oacc_routine = NULL;
36687 }
36688
36689 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36690 {
36691 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36692 TREE_USED (fndecl) ? "use" : "definition");
36693 parser->oacc_routine = NULL;
36694 }
36695
36696 /* Process for function attrib */
36697 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36698 replace_oacc_fn_attrib (fndecl, dims);
36699
36700 /* Add an "omp target" attribute. */
36701 DECL_ATTRIBUTES (fndecl)
36702 = tree_cons (get_identifier ("omp declare target"),
36703 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36704 }
36705 }
36706
36707 /* Main entry point to OpenMP statement pragmas. */
36708
36709 static void
36710 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36711 {
36712 tree stmt;
36713 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36714 omp_clause_mask mask (0);
36715
36716 switch (cp_parser_pragma_kind (pragma_tok))
36717 {
36718 case PRAGMA_OACC_ATOMIC:
36719 cp_parser_omp_atomic (parser, pragma_tok);
36720 return;
36721 case PRAGMA_OACC_CACHE:
36722 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36723 break;
36724 case PRAGMA_OACC_DATA:
36725 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36726 break;
36727 case PRAGMA_OACC_ENTER_DATA:
36728 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36729 break;
36730 case PRAGMA_OACC_EXIT_DATA:
36731 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36732 break;
36733 case PRAGMA_OACC_HOST_DATA:
36734 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36735 break;
36736 case PRAGMA_OACC_KERNELS:
36737 case PRAGMA_OACC_PARALLEL:
36738 strcpy (p_name, "#pragma acc");
36739 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36740 if_p);
36741 break;
36742 case PRAGMA_OACC_LOOP:
36743 strcpy (p_name, "#pragma acc");
36744 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36745 if_p);
36746 break;
36747 case PRAGMA_OACC_UPDATE:
36748 stmt = cp_parser_oacc_update (parser, pragma_tok);
36749 break;
36750 case PRAGMA_OACC_WAIT:
36751 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36752 break;
36753 case PRAGMA_OMP_ATOMIC:
36754 cp_parser_omp_atomic (parser, pragma_tok);
36755 return;
36756 case PRAGMA_OMP_CRITICAL:
36757 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36758 break;
36759 case PRAGMA_OMP_DISTRIBUTE:
36760 strcpy (p_name, "#pragma omp");
36761 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36762 if_p);
36763 break;
36764 case PRAGMA_OMP_FOR:
36765 strcpy (p_name, "#pragma omp");
36766 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36767 if_p);
36768 break;
36769 case PRAGMA_OMP_MASTER:
36770 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36771 break;
36772 case PRAGMA_OMP_PARALLEL:
36773 strcpy (p_name, "#pragma omp");
36774 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36775 if_p);
36776 break;
36777 case PRAGMA_OMP_SECTIONS:
36778 strcpy (p_name, "#pragma omp");
36779 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36780 break;
36781 case PRAGMA_OMP_SIMD:
36782 strcpy (p_name, "#pragma omp");
36783 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36784 if_p);
36785 break;
36786 case PRAGMA_OMP_SINGLE:
36787 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36788 break;
36789 case PRAGMA_OMP_TASK:
36790 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36791 break;
36792 case PRAGMA_OMP_TASKGROUP:
36793 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36794 break;
36795 case PRAGMA_OMP_TASKLOOP:
36796 strcpy (p_name, "#pragma omp");
36797 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36798 if_p);
36799 break;
36800 case PRAGMA_OMP_TEAMS:
36801 strcpy (p_name, "#pragma omp");
36802 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36803 if_p);
36804 break;
36805 default:
36806 gcc_unreachable ();
36807 }
36808
36809 protected_set_expr_location (stmt, pragma_tok->location);
36810 }
36811 \f
36812 /* Transactional Memory parsing routines. */
36813
36814 /* Parse a transaction attribute.
36815
36816 txn-attribute:
36817 attribute
36818 [ [ identifier ] ]
36819
36820 We use this instead of cp_parser_attributes_opt for transactions to avoid
36821 the pedwarn in C++98 mode. */
36822
36823 static tree
36824 cp_parser_txn_attribute_opt (cp_parser *parser)
36825 {
36826 cp_token *token;
36827 tree attr_name, attr = NULL;
36828
36829 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36830 return cp_parser_attributes_opt (parser);
36831
36832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36833 return NULL_TREE;
36834 cp_lexer_consume_token (parser->lexer);
36835 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36836 goto error1;
36837
36838 token = cp_lexer_peek_token (parser->lexer);
36839 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36840 {
36841 token = cp_lexer_consume_token (parser->lexer);
36842
36843 attr_name = (token->type == CPP_KEYWORD
36844 /* For keywords, use the canonical spelling,
36845 not the parsed identifier. */
36846 ? ridpointers[(int) token->keyword]
36847 : token->u.value);
36848 attr = build_tree_list (attr_name, NULL_TREE);
36849 }
36850 else
36851 cp_parser_error (parser, "expected identifier");
36852
36853 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36854 error1:
36855 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36856 return attr;
36857 }
36858
36859 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36860
36861 transaction-statement:
36862 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36863 compound-statement
36864 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36865 */
36866
36867 static tree
36868 cp_parser_transaction (cp_parser *parser, cp_token *token)
36869 {
36870 unsigned char old_in = parser->in_transaction;
36871 unsigned char this_in = 1, new_in;
36872 enum rid keyword = token->keyword;
36873 tree stmt, attrs, noex;
36874
36875 cp_lexer_consume_token (parser->lexer);
36876
36877 if (keyword == RID_TRANSACTION_RELAXED
36878 || keyword == RID_SYNCHRONIZED)
36879 this_in |= TM_STMT_ATTR_RELAXED;
36880 else
36881 {
36882 attrs = cp_parser_txn_attribute_opt (parser);
36883 if (attrs)
36884 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36885 }
36886
36887 /* Parse a noexcept specification. */
36888 if (keyword == RID_ATOMIC_NOEXCEPT)
36889 noex = boolean_true_node;
36890 else if (keyword == RID_ATOMIC_CANCEL)
36891 {
36892 /* cancel-and-throw is unimplemented. */
36893 sorry ("atomic_cancel");
36894 noex = NULL_TREE;
36895 }
36896 else
36897 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36898
36899 /* Keep track if we're in the lexical scope of an outer transaction. */
36900 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36901
36902 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36903
36904 parser->in_transaction = new_in;
36905 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36906 parser->in_transaction = old_in;
36907
36908 finish_transaction_stmt (stmt, NULL, this_in, noex);
36909
36910 return stmt;
36911 }
36912
36913 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36914
36915 transaction-expression:
36916 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36917 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36918 */
36919
36920 static tree
36921 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36922 {
36923 unsigned char old_in = parser->in_transaction;
36924 unsigned char this_in = 1;
36925 cp_token *token;
36926 tree expr, noex;
36927 bool noex_expr;
36928 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36929
36930 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36931 || keyword == RID_TRANSACTION_RELAXED);
36932
36933 if (!flag_tm)
36934 error_at (loc,
36935 keyword == RID_TRANSACTION_RELAXED
36936 ? G_("%<__transaction_relaxed%> without transactional memory "
36937 "support enabled")
36938 : G_("%<__transaction_atomic%> without transactional memory "
36939 "support enabled"));
36940
36941 token = cp_parser_require_keyword (parser, keyword,
36942 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36943 : RT_TRANSACTION_RELAXED));
36944 gcc_assert (token != NULL);
36945
36946 if (keyword == RID_TRANSACTION_RELAXED)
36947 this_in |= TM_STMT_ATTR_RELAXED;
36948
36949 /* Set this early. This might mean that we allow transaction_cancel in
36950 an expression that we find out later actually has to be a constexpr.
36951 However, we expect that cxx_constant_value will be able to deal with
36952 this; also, if the noexcept has no constexpr, then what we parse next
36953 really is a transaction's body. */
36954 parser->in_transaction = this_in;
36955
36956 /* Parse a noexcept specification. */
36957 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36958 true);
36959
36960 if (!noex || !noex_expr
36961 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36962 {
36963 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36964
36965 expr = cp_parser_expression (parser);
36966 expr = finish_parenthesized_expr (expr);
36967
36968 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
36969 }
36970 else
36971 {
36972 /* The only expression that is available got parsed for the noexcept
36973 already. noexcept is true then. */
36974 expr = noex;
36975 noex = boolean_true_node;
36976 }
36977
36978 expr = build_transaction_expr (token->location, expr, this_in, noex);
36979 parser->in_transaction = old_in;
36980
36981 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
36982 return error_mark_node;
36983
36984 return (flag_tm ? expr : error_mark_node);
36985 }
36986
36987 /* Parse a function-transaction-block.
36988
36989 function-transaction-block:
36990 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36991 function-body
36992 __transaction_atomic txn-attribute[opt] function-try-block
36993 __transaction_relaxed ctor-initializer[opt] function-body
36994 __transaction_relaxed function-try-block
36995 */
36996
36997 static bool
36998 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
36999 {
37000 unsigned char old_in = parser->in_transaction;
37001 unsigned char new_in = 1;
37002 tree compound_stmt, stmt, attrs;
37003 bool ctor_initializer_p;
37004 cp_token *token;
37005
37006 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37007 || keyword == RID_TRANSACTION_RELAXED);
37008 token = cp_parser_require_keyword (parser, keyword,
37009 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37010 : RT_TRANSACTION_RELAXED));
37011 gcc_assert (token != NULL);
37012
37013 if (keyword == RID_TRANSACTION_RELAXED)
37014 new_in |= TM_STMT_ATTR_RELAXED;
37015 else
37016 {
37017 attrs = cp_parser_txn_attribute_opt (parser);
37018 if (attrs)
37019 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37020 }
37021
37022 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37023
37024 parser->in_transaction = new_in;
37025
37026 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37027 ctor_initializer_p = cp_parser_function_try_block (parser);
37028 else
37029 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37030 (parser, /*in_function_try_block=*/false);
37031
37032 parser->in_transaction = old_in;
37033
37034 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37035
37036 return ctor_initializer_p;
37037 }
37038
37039 /* Parse a __transaction_cancel statement.
37040
37041 cancel-statement:
37042 __transaction_cancel txn-attribute[opt] ;
37043 __transaction_cancel txn-attribute[opt] throw-expression ;
37044
37045 ??? Cancel and throw is not yet implemented. */
37046
37047 static tree
37048 cp_parser_transaction_cancel (cp_parser *parser)
37049 {
37050 cp_token *token;
37051 bool is_outer = false;
37052 tree stmt, attrs;
37053
37054 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37055 RT_TRANSACTION_CANCEL);
37056 gcc_assert (token != NULL);
37057
37058 attrs = cp_parser_txn_attribute_opt (parser);
37059 if (attrs)
37060 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37061
37062 /* ??? Parse cancel-and-throw here. */
37063
37064 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37065
37066 if (!flag_tm)
37067 {
37068 error_at (token->location, "%<__transaction_cancel%> without "
37069 "transactional memory support enabled");
37070 return error_mark_node;
37071 }
37072 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37073 {
37074 error_at (token->location, "%<__transaction_cancel%> within a "
37075 "%<__transaction_relaxed%>");
37076 return error_mark_node;
37077 }
37078 else if (is_outer)
37079 {
37080 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37081 && !is_tm_may_cancel_outer (current_function_decl))
37082 {
37083 error_at (token->location, "outer %<__transaction_cancel%> not "
37084 "within outer %<__transaction_atomic%>");
37085 error_at (token->location,
37086 " or a %<transaction_may_cancel_outer%> function");
37087 return error_mark_node;
37088 }
37089 }
37090 else if (parser->in_transaction == 0)
37091 {
37092 error_at (token->location, "%<__transaction_cancel%> not within "
37093 "%<__transaction_atomic%>");
37094 return error_mark_node;
37095 }
37096
37097 stmt = build_tm_abort_call (token->location, is_outer);
37098 add_stmt (stmt);
37099
37100 return stmt;
37101 }
37102 \f
37103 /* The parser. */
37104
37105 static GTY (()) cp_parser *the_parser;
37106
37107 \f
37108 /* Special handling for the first token or line in the file. The first
37109 thing in the file might be #pragma GCC pch_preprocess, which loads a
37110 PCH file, which is a GC collection point. So we need to handle this
37111 first pragma without benefit of an existing lexer structure.
37112
37113 Always returns one token to the caller in *FIRST_TOKEN. This is
37114 either the true first token of the file, or the first token after
37115 the initial pragma. */
37116
37117 static void
37118 cp_parser_initial_pragma (cp_token *first_token)
37119 {
37120 tree name = NULL;
37121
37122 cp_lexer_get_preprocessor_token (NULL, first_token);
37123 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37124 return;
37125
37126 cp_lexer_get_preprocessor_token (NULL, first_token);
37127 if (first_token->type == CPP_STRING)
37128 {
37129 name = first_token->u.value;
37130
37131 cp_lexer_get_preprocessor_token (NULL, first_token);
37132 if (first_token->type != CPP_PRAGMA_EOL)
37133 error_at (first_token->location,
37134 "junk at end of %<#pragma GCC pch_preprocess%>");
37135 }
37136 else
37137 error_at (first_token->location, "expected string literal");
37138
37139 /* Skip to the end of the pragma. */
37140 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37141 cp_lexer_get_preprocessor_token (NULL, first_token);
37142
37143 /* Now actually load the PCH file. */
37144 if (name)
37145 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37146
37147 /* Read one more token to return to our caller. We have to do this
37148 after reading the PCH file in, since its pointers have to be
37149 live. */
37150 cp_lexer_get_preprocessor_token (NULL, first_token);
37151 }
37152
37153 /* Parses the grainsize pragma for the _Cilk_for statement.
37154 Syntax:
37155 #pragma cilk grainsize = <VALUE>. */
37156
37157 static void
37158 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37159 {
37160 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37161 {
37162 tree exp = cp_parser_binary_expression (parser, false, false,
37163 PREC_NOT_OPERATOR, NULL);
37164 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37165 if (!exp || exp == error_mark_node)
37166 {
37167 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37168 return;
37169 }
37170
37171 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37172 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37173 cp_parser_cilk_for (parser, exp, if_p);
37174 else
37175 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37176 "%<#pragma cilk grainsize%> is not followed by "
37177 "%<_Cilk_for%>");
37178 return;
37179 }
37180 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37181 }
37182
37183 /* Normal parsing of a pragma token. Here we can (and must) use the
37184 regular lexer. */
37185
37186 static bool
37187 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37188 {
37189 cp_token *pragma_tok;
37190 unsigned int id;
37191 tree stmt;
37192 bool ret;
37193
37194 pragma_tok = cp_lexer_consume_token (parser->lexer);
37195 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37196 parser->lexer->in_pragma = true;
37197
37198 id = cp_parser_pragma_kind (pragma_tok);
37199 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37200 cp_ensure_no_omp_declare_simd (parser);
37201 switch (id)
37202 {
37203 case PRAGMA_GCC_PCH_PREPROCESS:
37204 error_at (pragma_tok->location,
37205 "%<#pragma GCC pch_preprocess%> must be first");
37206 break;
37207
37208 case PRAGMA_OMP_BARRIER:
37209 switch (context)
37210 {
37211 case pragma_compound:
37212 cp_parser_omp_barrier (parser, pragma_tok);
37213 return false;
37214 case pragma_stmt:
37215 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37216 "used in compound statements");
37217 break;
37218 default:
37219 goto bad_stmt;
37220 }
37221 break;
37222
37223 case PRAGMA_OMP_FLUSH:
37224 switch (context)
37225 {
37226 case pragma_compound:
37227 cp_parser_omp_flush (parser, pragma_tok);
37228 return false;
37229 case pragma_stmt:
37230 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37231 "used in compound statements");
37232 break;
37233 default:
37234 goto bad_stmt;
37235 }
37236 break;
37237
37238 case PRAGMA_OMP_TASKWAIT:
37239 switch (context)
37240 {
37241 case pragma_compound:
37242 cp_parser_omp_taskwait (parser, pragma_tok);
37243 return false;
37244 case pragma_stmt:
37245 error_at (pragma_tok->location,
37246 "%<#pragma omp taskwait%> may only be "
37247 "used in compound statements");
37248 break;
37249 default:
37250 goto bad_stmt;
37251 }
37252 break;
37253
37254 case PRAGMA_OMP_TASKYIELD:
37255 switch (context)
37256 {
37257 case pragma_compound:
37258 cp_parser_omp_taskyield (parser, pragma_tok);
37259 return false;
37260 case pragma_stmt:
37261 error_at (pragma_tok->location,
37262 "%<#pragma omp taskyield%> may only be "
37263 "used in compound statements");
37264 break;
37265 default:
37266 goto bad_stmt;
37267 }
37268 break;
37269
37270 case PRAGMA_OMP_CANCEL:
37271 switch (context)
37272 {
37273 case pragma_compound:
37274 cp_parser_omp_cancel (parser, pragma_tok);
37275 return false;
37276 case pragma_stmt:
37277 error_at (pragma_tok->location,
37278 "%<#pragma omp cancel%> may only be "
37279 "used in compound statements");
37280 break;
37281 default:
37282 goto bad_stmt;
37283 }
37284 break;
37285
37286 case PRAGMA_OMP_CANCELLATION_POINT:
37287 switch (context)
37288 {
37289 case pragma_compound:
37290 cp_parser_omp_cancellation_point (parser, pragma_tok);
37291 return false;
37292 case pragma_stmt:
37293 error_at (pragma_tok->location,
37294 "%<#pragma omp cancellation point%> may only be "
37295 "used in compound statements");
37296 break;
37297 default:
37298 goto bad_stmt;
37299 }
37300 break;
37301
37302 case PRAGMA_OMP_THREADPRIVATE:
37303 cp_parser_omp_threadprivate (parser, pragma_tok);
37304 return false;
37305
37306 case PRAGMA_OMP_DECLARE_REDUCTION:
37307 cp_parser_omp_declare (parser, pragma_tok, context);
37308 return false;
37309
37310 case PRAGMA_OACC_DECLARE:
37311 cp_parser_oacc_declare (parser, pragma_tok);
37312 return false;
37313
37314 case PRAGMA_OACC_ROUTINE:
37315 cp_parser_oacc_routine (parser, pragma_tok, context);
37316 return false;
37317
37318 case PRAGMA_OACC_ATOMIC:
37319 case PRAGMA_OACC_CACHE:
37320 case PRAGMA_OACC_DATA:
37321 case PRAGMA_OACC_ENTER_DATA:
37322 case PRAGMA_OACC_EXIT_DATA:
37323 case PRAGMA_OACC_HOST_DATA:
37324 case PRAGMA_OACC_KERNELS:
37325 case PRAGMA_OACC_PARALLEL:
37326 case PRAGMA_OACC_LOOP:
37327 case PRAGMA_OACC_UPDATE:
37328 case PRAGMA_OACC_WAIT:
37329 case PRAGMA_OMP_ATOMIC:
37330 case PRAGMA_OMP_CRITICAL:
37331 case PRAGMA_OMP_DISTRIBUTE:
37332 case PRAGMA_OMP_FOR:
37333 case PRAGMA_OMP_MASTER:
37334 case PRAGMA_OMP_PARALLEL:
37335 case PRAGMA_OMP_SECTIONS:
37336 case PRAGMA_OMP_SIMD:
37337 case PRAGMA_OMP_SINGLE:
37338 case PRAGMA_OMP_TASK:
37339 case PRAGMA_OMP_TASKGROUP:
37340 case PRAGMA_OMP_TASKLOOP:
37341 case PRAGMA_OMP_TEAMS:
37342 if (context != pragma_stmt && context != pragma_compound)
37343 goto bad_stmt;
37344 stmt = push_omp_privatization_clauses (false);
37345 cp_parser_omp_construct (parser, pragma_tok, if_p);
37346 pop_omp_privatization_clauses (stmt);
37347 return true;
37348
37349 case PRAGMA_OMP_ORDERED:
37350 stmt = push_omp_privatization_clauses (false);
37351 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37352 pop_omp_privatization_clauses (stmt);
37353 return ret;
37354
37355 case PRAGMA_OMP_TARGET:
37356 stmt = push_omp_privatization_clauses (false);
37357 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37358 pop_omp_privatization_clauses (stmt);
37359 return ret;
37360
37361 case PRAGMA_OMP_END_DECLARE_TARGET:
37362 cp_parser_omp_end_declare_target (parser, pragma_tok);
37363 return false;
37364
37365 case PRAGMA_OMP_SECTION:
37366 error_at (pragma_tok->location,
37367 "%<#pragma omp section%> may only be used in "
37368 "%<#pragma omp sections%> construct");
37369 break;
37370
37371 case PRAGMA_IVDEP:
37372 {
37373 if (context == pragma_external)
37374 {
37375 error_at (pragma_tok->location,
37376 "%<#pragma GCC ivdep%> must be inside a function");
37377 break;
37378 }
37379 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37380 cp_token *tok;
37381 tok = cp_lexer_peek_token (the_parser->lexer);
37382 if (tok->type != CPP_KEYWORD
37383 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37384 && tok->keyword != RID_DO))
37385 {
37386 cp_parser_error (parser, "for, while or do statement expected");
37387 return false;
37388 }
37389 cp_parser_iteration_statement (parser, if_p, true);
37390 return true;
37391 }
37392
37393 case PRAGMA_CILK_SIMD:
37394 if (context == pragma_external)
37395 {
37396 error_at (pragma_tok->location,
37397 "%<#pragma simd%> must be inside a function");
37398 break;
37399 }
37400 stmt = push_omp_privatization_clauses (false);
37401 cp_parser_cilk_simd (parser, pragma_tok, if_p);
37402 pop_omp_privatization_clauses (stmt);
37403 return true;
37404
37405 case PRAGMA_CILK_GRAINSIZE:
37406 if (context == pragma_external)
37407 {
37408 error_at (pragma_tok->location,
37409 "%<#pragma cilk grainsize%> must be inside a function");
37410 break;
37411 }
37412
37413 /* Ignore the pragma if Cilk Plus is not enabled. */
37414 if (flag_cilkplus)
37415 {
37416 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37417 return true;
37418 }
37419 else
37420 {
37421 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37422 "%<#pragma cilk grainsize%>");
37423 break;
37424 }
37425
37426 default:
37427 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37428 c_invoke_pragma_handler (id);
37429 break;
37430
37431 bad_stmt:
37432 cp_parser_error (parser, "expected declaration specifiers");
37433 break;
37434 }
37435
37436 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37437 return false;
37438 }
37439
37440 /* The interface the pragma parsers have to the lexer. */
37441
37442 enum cpp_ttype
37443 pragma_lex (tree *value, location_t *loc)
37444 {
37445 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37446 enum cpp_ttype ret = tok->type;
37447
37448 *value = tok->u.value;
37449 if (loc)
37450 *loc = tok->location;
37451
37452 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37453 ret = CPP_EOF;
37454 else if (ret == CPP_STRING)
37455 *value = cp_parser_string_literal (the_parser, false, false);
37456 else
37457 {
37458 if (ret == CPP_KEYWORD)
37459 ret = CPP_NAME;
37460 cp_lexer_consume_token (the_parser->lexer);
37461 }
37462
37463 return ret;
37464 }
37465
37466 \f
37467 /* External interface. */
37468
37469 /* Parse one entire translation unit. */
37470
37471 void
37472 c_parse_file (void)
37473 {
37474 static bool already_called = false;
37475
37476 if (already_called)
37477 fatal_error (input_location,
37478 "inter-module optimizations not implemented for C++");
37479 already_called = true;
37480
37481 the_parser = cp_parser_new ();
37482 push_deferring_access_checks (flag_access_control
37483 ? dk_no_deferred : dk_no_check);
37484 cp_parser_translation_unit (the_parser);
37485 the_parser = NULL;
37486 }
37487
37488 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37489 vectorlength clause:
37490 Syntax:
37491 vectorlength ( constant-expression ) */
37492
37493 static tree
37494 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37495 bool is_simd_fn)
37496 {
37497 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37498 tree expr;
37499 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37500 safelen clause. Thus, vectorlength is represented as OMP 4.0
37501 safelen. For SIMD-enabled function it is represented by OMP 4.0
37502 simdlen. */
37503 if (!is_simd_fn)
37504 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37505 loc);
37506 else
37507 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37508 loc);
37509
37510 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37511 return error_mark_node;
37512
37513 expr = cp_parser_constant_expression (parser);
37514 expr = maybe_constant_value (expr);
37515
37516 /* If expr == error_mark_node, then don't emit any errors nor
37517 create a clause. if any of the above functions returns
37518 error mark node then they would have emitted an error message. */
37519 if (expr == error_mark_node)
37520 ;
37521 else if (!TREE_TYPE (expr)
37522 || !TREE_CONSTANT (expr)
37523 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37524 error_at (loc, "vectorlength must be an integer constant");
37525 else if (TREE_CONSTANT (expr)
37526 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37527 error_at (loc, "vectorlength must be a power of 2");
37528 else
37529 {
37530 tree c;
37531 if (!is_simd_fn)
37532 {
37533 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37534 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37535 OMP_CLAUSE_CHAIN (c) = clauses;
37536 clauses = c;
37537 }
37538 else
37539 {
37540 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37541 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37542 OMP_CLAUSE_CHAIN (c) = clauses;
37543 clauses = c;
37544 }
37545 }
37546
37547 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37548 return error_mark_node;
37549 return clauses;
37550 }
37551
37552 /* Handles the Cilk Plus #pragma simd linear clause.
37553 Syntax:
37554 linear ( simd-linear-variable-list )
37555
37556 simd-linear-variable-list:
37557 simd-linear-variable
37558 simd-linear-variable-list , simd-linear-variable
37559
37560 simd-linear-variable:
37561 id-expression
37562 id-expression : simd-linear-step
37563
37564 simd-linear-step:
37565 conditional-expression */
37566
37567 static tree
37568 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37569 {
37570 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37571
37572 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37573 return clauses;
37574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37575 {
37576 cp_parser_error (parser, "expected identifier");
37577 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37578 return error_mark_node;
37579 }
37580
37581 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37582 parser->colon_corrects_to_scope_p = false;
37583 while (1)
37584 {
37585 cp_token *token = cp_lexer_peek_token (parser->lexer);
37586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37587 {
37588 cp_parser_error (parser, "expected variable-name");
37589 clauses = error_mark_node;
37590 break;
37591 }
37592
37593 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37594 false, false);
37595 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37596 token->location);
37597 if (decl == error_mark_node)
37598 {
37599 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37600 token->location);
37601 clauses = error_mark_node;
37602 }
37603 else
37604 {
37605 tree e = NULL_TREE;
37606 tree step_size = integer_one_node;
37607
37608 /* If present, parse the linear step. Otherwise, assume the default
37609 value of 1. */
37610 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37611 {
37612 cp_lexer_consume_token (parser->lexer);
37613
37614 e = cp_parser_assignment_expression (parser);
37615 e = maybe_constant_value (e);
37616
37617 if (e == error_mark_node)
37618 {
37619 /* If an error has occurred, then the whole pragma is
37620 considered ill-formed. Thus, no reason to keep
37621 parsing. */
37622 clauses = error_mark_node;
37623 break;
37624 }
37625 else if (type_dependent_expression_p (e)
37626 || value_dependent_expression_p (e)
37627 || (TREE_TYPE (e)
37628 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37629 && (TREE_CONSTANT (e)
37630 || DECL_P (e))))
37631 step_size = e;
37632 else
37633 cp_parser_error (parser,
37634 "step size must be an integer constant "
37635 "expression or an integer variable");
37636 }
37637
37638 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37639 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37640 OMP_CLAUSE_DECL (l) = decl;
37641 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37642 OMP_CLAUSE_CHAIN (l) = clauses;
37643 clauses = l;
37644 }
37645 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37646 cp_lexer_consume_token (parser->lexer);
37647 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37648 break;
37649 else
37650 {
37651 error_at (cp_lexer_peek_token (parser->lexer)->location,
37652 "expected %<,%> or %<)%> after %qE", decl);
37653 clauses = error_mark_node;
37654 break;
37655 }
37656 }
37657 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37658 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37659 return clauses;
37660 }
37661
37662 /* Returns the name of the next clause. If the clause is not
37663 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37664 token is not consumed. Otherwise, the appropriate enum from the
37665 pragma_simd_clause is returned and the token is consumed. */
37666
37667 static pragma_omp_clause
37668 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37669 {
37670 pragma_omp_clause clause_type;
37671 cp_token *token = cp_lexer_peek_token (parser->lexer);
37672
37673 if (token->keyword == RID_PRIVATE)
37674 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37675 else if (!token->u.value || token->type != CPP_NAME)
37676 return PRAGMA_CILK_CLAUSE_NONE;
37677 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37678 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37679 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37680 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37681 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37682 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37683 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37684 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37685 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37686 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37687 else
37688 return PRAGMA_CILK_CLAUSE_NONE;
37689
37690 cp_lexer_consume_token (parser->lexer);
37691 return clause_type;
37692 }
37693
37694 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37695
37696 static tree
37697 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37698 {
37699 tree clauses = NULL_TREE;
37700
37701 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37702 && clauses != error_mark_node)
37703 {
37704 pragma_omp_clause c_kind;
37705 c_kind = cp_parser_cilk_simd_clause_name (parser);
37706 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37707 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37708 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37709 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37710 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37711 /* Use the OpenMP 4.0 equivalent function. */
37712 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37713 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37714 /* Use the OpenMP 4.0 equivalent function. */
37715 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37716 clauses);
37717 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37718 /* Use the OMP 4.0 equivalent function. */
37719 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37720 clauses);
37721 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37722 /* Use the OMP 4.0 equivalent function. */
37723 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37724 else
37725 {
37726 clauses = error_mark_node;
37727 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37728 break;
37729 }
37730 }
37731
37732 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37733
37734 if (clauses == error_mark_node)
37735 return error_mark_node;
37736 else
37737 return finish_omp_clauses (clauses, C_ORT_CILK);
37738 }
37739
37740 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37741
37742 static void
37743 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37744 {
37745 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37746
37747 if (clauses == error_mark_node)
37748 return;
37749
37750 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37751 {
37752 error_at (cp_lexer_peek_token (parser->lexer)->location,
37753 "for statement expected");
37754 return;
37755 }
37756
37757 tree sb = begin_omp_structured_block ();
37758 int save = cp_parser_begin_omp_structured_block (parser);
37759 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37760 if (ret)
37761 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37762 cp_parser_end_omp_structured_block (parser, save);
37763 add_stmt (finish_omp_structured_block (sb));
37764 }
37765
37766 /* Main entry-point for parsing Cilk Plus _Cilk_for
37767 loops. The return value is error_mark_node
37768 when errors happen and CILK_FOR tree on success. */
37769
37770 static tree
37771 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37772 {
37773 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37774 gcc_unreachable ();
37775
37776 tree sb = begin_omp_structured_block ();
37777 int save = cp_parser_begin_omp_structured_block (parser);
37778
37779 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37780 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37781 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37782 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
37783
37784 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37785 if (ret)
37786 cpp_validate_cilk_plus_loop (ret);
37787 else
37788 ret = error_mark_node;
37789
37790 cp_parser_end_omp_structured_block (parser, save);
37791 add_stmt (finish_omp_structured_block (sb));
37792 return ret;
37793 }
37794
37795 /* Create an identifier for a generic parameter type (a synthesized
37796 template parameter implied by `auto' or a concept identifier). */
37797
37798 static GTY(()) int generic_parm_count;
37799 static tree
37800 make_generic_type_name ()
37801 {
37802 char buf[32];
37803 sprintf (buf, "auto:%d", ++generic_parm_count);
37804 return get_identifier (buf);
37805 }
37806
37807 /* Predicate that behaves as is_auto_or_concept but matches the parent
37808 node of the generic type rather than the generic type itself. This
37809 allows for type transformation in add_implicit_template_parms. */
37810
37811 static inline bool
37812 tree_type_is_auto_or_concept (const_tree t)
37813 {
37814 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37815 }
37816
37817 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37818 (creating a new template parameter list if necessary). Returns the newly
37819 created template type parm. */
37820
37821 static tree
37822 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37823 {
37824 gcc_assert (current_binding_level->kind == sk_function_parms);
37825
37826 /* Before committing to modifying any scope, if we're in an
37827 implicit template scope, and we're trying to synthesize a
37828 constrained parameter, try to find a previous parameter with
37829 the same name. This is the same-type rule for abbreviated
37830 function templates. */
37831 if (parser->implicit_template_scope && constr)
37832 {
37833 tree t = parser->implicit_template_parms;
37834 while (t)
37835 {
37836 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37837 {
37838 tree d = TREE_VALUE (t);
37839 if (TREE_CODE (d) == PARM_DECL)
37840 /* Return the TEMPLATE_PARM_INDEX. */
37841 d = DECL_INITIAL (d);
37842 return d;
37843 }
37844 t = TREE_CHAIN (t);
37845 }
37846 }
37847
37848 /* We are either continuing a function template that already contains implicit
37849 template parameters, creating a new fully-implicit function template, or
37850 extending an existing explicit function template with implicit template
37851 parameters. */
37852
37853 cp_binding_level *const entry_scope = current_binding_level;
37854
37855 bool become_template = false;
37856 cp_binding_level *parent_scope = 0;
37857
37858 if (parser->implicit_template_scope)
37859 {
37860 gcc_assert (parser->implicit_template_parms);
37861
37862 current_binding_level = parser->implicit_template_scope;
37863 }
37864 else
37865 {
37866 /* Roll back to the existing template parameter scope (in the case of
37867 extending an explicit function template) or introduce a new template
37868 parameter scope ahead of the function parameter scope (or class scope
37869 in the case of out-of-line member definitions). The function scope is
37870 added back after template parameter synthesis below. */
37871
37872 cp_binding_level *scope = entry_scope;
37873
37874 while (scope->kind == sk_function_parms)
37875 {
37876 parent_scope = scope;
37877 scope = scope->level_chain;
37878 }
37879 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37880 {
37881 /* If not defining a class, then any class scope is a scope level in
37882 an out-of-line member definition. In this case simply wind back
37883 beyond the first such scope to inject the template parameter list.
37884 Otherwise wind back to the class being defined. The latter can
37885 occur in class member friend declarations such as:
37886
37887 class A {
37888 void foo (auto);
37889 };
37890 class B {
37891 friend void A::foo (auto);
37892 };
37893
37894 The template parameter list synthesized for the friend declaration
37895 must be injected in the scope of 'B'. This can also occur in
37896 erroneous cases such as:
37897
37898 struct A {
37899 struct B {
37900 void foo (auto);
37901 };
37902 void B::foo (auto) {}
37903 };
37904
37905 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37906 but, nevertheless, the template parameter list synthesized for the
37907 declarator should be injected into the scope of 'A' as if the
37908 ill-formed template was specified explicitly. */
37909
37910 while (scope->kind == sk_class && !scope->defining_class_p)
37911 {
37912 parent_scope = scope;
37913 scope = scope->level_chain;
37914 }
37915 }
37916
37917 current_binding_level = scope;
37918
37919 if (scope->kind != sk_template_parms
37920 || !function_being_declared_is_template_p (parser))
37921 {
37922 /* Introduce a new template parameter list for implicit template
37923 parameters. */
37924
37925 become_template = true;
37926
37927 parser->implicit_template_scope
37928 = begin_scope (sk_template_parms, NULL);
37929
37930 ++processing_template_decl;
37931
37932 parser->fully_implicit_function_template_p = true;
37933 ++parser->num_template_parameter_lists;
37934 }
37935 else
37936 {
37937 /* Synthesize implicit template parameters at the end of the explicit
37938 template parameter list. */
37939
37940 gcc_assert (current_template_parms);
37941
37942 parser->implicit_template_scope = scope;
37943
37944 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37945 parser->implicit_template_parms
37946 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
37947 }
37948 }
37949
37950 /* Synthesize a new template parameter and track the current template
37951 parameter chain with implicit_template_parms. */
37952
37953 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
37954 tree synth_id = make_generic_type_name ();
37955 tree synth_tmpl_parm;
37956 bool non_type = false;
37957
37958 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37959 synth_tmpl_parm
37960 = finish_template_type_parm (class_type_node, synth_id);
37961 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37962 synth_tmpl_parm
37963 = finish_constrained_template_template_parm (proto, synth_id);
37964 else
37965 {
37966 synth_tmpl_parm = copy_decl (proto);
37967 DECL_NAME (synth_tmpl_parm) = synth_id;
37968 non_type = true;
37969 }
37970
37971 // Attach the constraint to the parm before processing.
37972 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37973 TREE_TYPE (node) = constr;
37974 tree new_parm
37975 = process_template_parm (parser->implicit_template_parms,
37976 input_location,
37977 node,
37978 /*non_type=*/non_type,
37979 /*param_pack=*/false);
37980
37981 // Chain the new parameter to the list of implicit parameters.
37982 if (parser->implicit_template_parms)
37983 parser->implicit_template_parms
37984 = TREE_CHAIN (parser->implicit_template_parms);
37985 else
37986 parser->implicit_template_parms = new_parm;
37987
37988 tree new_decl = getdecls ();
37989 if (non_type)
37990 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37991 new_decl = DECL_INITIAL (new_decl);
37992
37993 /* If creating a fully implicit function template, start the new implicit
37994 template parameter list with this synthesized type, otherwise grow the
37995 current template parameter list. */
37996
37997 if (become_template)
37998 {
37999 parent_scope->level_chain = current_binding_level;
38000
38001 tree new_parms = make_tree_vec (1);
38002 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38003 current_template_parms = tree_cons (size_int (processing_template_decl),
38004 new_parms, current_template_parms);
38005 }
38006 else
38007 {
38008 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38009 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38010 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38011 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38012 }
38013
38014 // If the new parameter was constrained, we need to add that to the
38015 // constraints in the template parameter list.
38016 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38017 {
38018 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38019 reqs = conjoin_constraints (reqs, req);
38020 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38021 }
38022
38023 current_binding_level = entry_scope;
38024
38025 return new_decl;
38026 }
38027
38028 /* Finish the declaration of a fully implicit function template. Such a
38029 template has no explicit template parameter list so has not been through the
38030 normal template head and tail processing. synthesize_implicit_template_parm
38031 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38032 provided if the declaration is a class member such that its template
38033 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38034 form is returned. Otherwise NULL_TREE is returned. */
38035
38036 static tree
38037 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38038 {
38039 gcc_assert (parser->fully_implicit_function_template_p);
38040
38041 if (member_decl_opt && member_decl_opt != error_mark_node
38042 && DECL_VIRTUAL_P (member_decl_opt))
38043 {
38044 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38045 "implicit templates may not be %<virtual%>");
38046 DECL_VIRTUAL_P (member_decl_opt) = false;
38047 }
38048
38049 if (member_decl_opt)
38050 member_decl_opt = finish_member_template_decl (member_decl_opt);
38051 end_template_decl ();
38052
38053 parser->fully_implicit_function_template_p = false;
38054 --parser->num_template_parameter_lists;
38055
38056 return member_decl_opt;
38057 }
38058
38059 #include "gt-cp-parser.h"